1//===- MCDisassembler.cpp - Disassembler interface ------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/MC/MCDisassembler/MCDisassembler.h"
10#include "llvm/ADT/ArrayRef.h"
11
12using namespace llvm;
13
14MCDisassembler::~MCDisassembler() = default;
15
16Expected<bool> MCDisassembler::onSymbolStart(SymbolInfoTy &Symbol,
17 uint64_t &Size,
18 ArrayRef<uint8_t> Bytes,
19 uint64_t Address) const {
20 return false;
21}
22
23uint64_t MCDisassembler::suggestBytesToSkip(ArrayRef<uint8_t> Bytes,
24 uint64_t Address) const {
25 return 1;
26}
27
28bool MCDisassembler::tryAddingSymbolicOperand(MCInst &Inst, int64_t Value,
29 uint64_t Address, bool IsBranch,
30 uint64_t Offset, uint64_t OpSize,
31 uint64_t InstSize) const {
32 if (Symbolizer) {
33 assert(CommentStream && "CommentStream is not set.");
34 return Symbolizer->tryAddingSymbolicOperand(Inst, cStream&: *CommentStream, Value,
35 Address, IsBranch, Offset,
36 OpSize, InstSize);
37 }
38 return false;
39}
40
41void MCDisassembler::tryAddingPcLoadReferenceComment(int64_t Value,
42 uint64_t Address) const {
43 if (Symbolizer) {
44 assert(CommentStream && "CommentStream is not set.");
45 Symbolizer->tryAddingPcLoadReferenceComment(cStream&: *CommentStream, Value, Address);
46 }
47}
48
49void MCDisassembler::setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer) {
50 Symbolizer = std::move(Symzer);
51}
52
53#define SMC_PCASE(A, P) \
54 case XCOFF::XMC_##A: \
55 return P;
56
57static uint8_t getSMCPriority(XCOFF::StorageMappingClass SMC) {
58 switch (SMC) {
59 SMC_PCASE(PR, 1)
60 SMC_PCASE(RO, 1)
61 SMC_PCASE(DB, 1)
62 SMC_PCASE(GL, 1)
63 SMC_PCASE(XO, 1)
64 SMC_PCASE(SV, 1)
65 SMC_PCASE(SV64, 1)
66 SMC_PCASE(SV3264, 1)
67 SMC_PCASE(TI, 1)
68 SMC_PCASE(TB, 1)
69 SMC_PCASE(RW, 1)
70 SMC_PCASE(TC0, 0)
71 SMC_PCASE(TC, 1)
72 SMC_PCASE(TD, 1)
73 SMC_PCASE(DS, 1)
74 SMC_PCASE(UA, 1)
75 SMC_PCASE(BS, 1)
76 SMC_PCASE(UC, 1)
77 SMC_PCASE(TL, 1)
78 SMC_PCASE(UL, 1)
79 SMC_PCASE(TE, 1)
80#undef SMC_PCASE
81 }
82 return 0;
83}
84
85/// The function is for symbol sorting when symbols have the same address.
86/// The symbols in the same section are sorted in ascending order.
87/// llvm-objdump -D will choose the highest priority symbol to display when
88/// there are symbols with the same address.
89bool XCOFFSymbolInfoTy::operator<(const XCOFFSymbolInfoTy &SymInfo) const {
90 // Label symbols have higher priority than non-label symbols.
91 if (IsLabel != SymInfo.IsLabel)
92 return SymInfo.IsLabel;
93
94 // Symbols with a StorageMappingClass have higher priority than those without.
95 if (StorageMappingClass.has_value() !=
96 SymInfo.StorageMappingClass.has_value())
97 return SymInfo.StorageMappingClass.has_value();
98
99 if (StorageMappingClass) {
100 return getSMCPriority(SMC: *StorageMappingClass) <
101 getSMCPriority(SMC: *SymInfo.StorageMappingClass);
102 }
103
104 return false;
105}
106