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