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 
12 using namespace llvm;
13 
14 MCDisassembler::~MCDisassembler() = default;
15 
16 Optional<MCDisassembler::DecodeStatus>
17 MCDisassembler::onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
18                               ArrayRef<uint8_t> Bytes, uint64_t Address,
19                               raw_ostream &CStream) const {
20   return None;
21 }
22 
23 bool MCDisassembler::tryAddingSymbolicOperand(MCInst &Inst, int64_t Value,
24                                               uint64_t Address, bool IsBranch,
25                                               uint64_t Offset,
26                                               uint64_t InstSize) const {
27   if (Symbolizer)
28     return Symbolizer->tryAddingSymbolicOperand(
29         Inst, *CommentStream, Value, Address, IsBranch, Offset, InstSize);
30   return false;
31 }
32 
33 void MCDisassembler::tryAddingPcLoadReferenceComment(int64_t Value,
34                                                      uint64_t Address) const {
35   if (Symbolizer)
36     Symbolizer->tryAddingPcLoadReferenceComment(*CommentStream, Value, Address);
37 }
38 
39 void MCDisassembler::setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer) {
40   Symbolizer = std::move(Symzer);
41 }
42 
43 #define SMC_PCASE(A, P)                                                         \
44   case XCOFF::XMC_##A:                                                         \
45     return P;
46 
47 static uint8_t getSMCPriority(XCOFF::StorageMappingClass SMC) {
48   switch (SMC) {
49     SMC_PCASE(PR, 1)
50     SMC_PCASE(RO, 1)
51     SMC_PCASE(DB, 1)
52     SMC_PCASE(GL, 1)
53     SMC_PCASE(XO, 1)
54     SMC_PCASE(SV, 1)
55     SMC_PCASE(SV64, 1)
56     SMC_PCASE(SV3264, 1)
57     SMC_PCASE(TI, 1)
58     SMC_PCASE(TB, 1)
59     SMC_PCASE(RW, 1)
60     SMC_PCASE(TC0, 0)
61     SMC_PCASE(TC, 1)
62     SMC_PCASE(TD, 1)
63     SMC_PCASE(DS, 1)
64     SMC_PCASE(UA, 1)
65     SMC_PCASE(BS, 1)
66     SMC_PCASE(UC, 1)
67     SMC_PCASE(TL, 1)
68     SMC_PCASE(UL, 1)
69     SMC_PCASE(TE, 1)
70 #undef SMC_PCASE
71   }
72   return 0;
73 }
74 
75 /// The function is for symbol sorting when symbols have the same address.
76 /// The symbols in the same section are sorted in ascending order.
77 /// llvm-objdump -D will choose the highest priority symbol to display when
78 /// there are symbols with the same address.
79 bool XCOFFSymbolInfo::operator<(const XCOFFSymbolInfo &SymInfo) const {
80   // Label symbols have higher priority than non-label symbols.
81   if (IsLabel != SymInfo.IsLabel)
82     return SymInfo.IsLabel;
83 
84   // Symbols with a StorageMappingClass have higher priority than those without.
85   if (StorageMappingClass.hasValue() != SymInfo.StorageMappingClass.hasValue())
86     return SymInfo.StorageMappingClass.hasValue();
87 
88   if (StorageMappingClass.hasValue()) {
89     return getSMCPriority(StorageMappingClass.getValue()) <
90            getSMCPriority(SymInfo.StorageMappingClass.getValue());
91   }
92 
93   return false;
94 }
95