10b57cec5SDimitry Andric //===- MCDisassembler.cpp - Disassembler interface ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/MC/MCDisassembler/MCDisassembler.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric
120b57cec5SDimitry Andric using namespace llvm;
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric MCDisassembler::~MCDisassembler() = default;
150b57cec5SDimitry Andric
onSymbolStart(SymbolInfoTy & Symbol,uint64_t & Size,ArrayRef<uint8_t> Bytes,uint64_t Address) const16*0fca6ea1SDimitry Andric Expected<bool> MCDisassembler::onSymbolStart(SymbolInfoTy &Symbol,
17*0fca6ea1SDimitry Andric uint64_t &Size,
18*0fca6ea1SDimitry Andric ArrayRef<uint8_t> Bytes,
19*0fca6ea1SDimitry Andric uint64_t Address) const {
20*0fca6ea1SDimitry Andric return false;
210b57cec5SDimitry Andric }
220b57cec5SDimitry Andric
suggestBytesToSkip(ArrayRef<uint8_t> Bytes,uint64_t Address) const23972a253aSDimitry Andric uint64_t MCDisassembler::suggestBytesToSkip(ArrayRef<uint8_t> Bytes,
24972a253aSDimitry Andric uint64_t Address) const {
25972a253aSDimitry Andric return 1;
26972a253aSDimitry Andric }
27972a253aSDimitry Andric
tryAddingSymbolicOperand(MCInst & Inst,int64_t Value,uint64_t Address,bool IsBranch,uint64_t Offset,uint64_t OpSize,uint64_t InstSize) const280b57cec5SDimitry Andric bool MCDisassembler::tryAddingSymbolicOperand(MCInst &Inst, int64_t Value,
290b57cec5SDimitry Andric uint64_t Address, bool IsBranch,
3081ad6265SDimitry Andric uint64_t Offset, uint64_t OpSize,
310b57cec5SDimitry Andric uint64_t InstSize) const {
320b57cec5SDimitry Andric if (Symbolizer)
3381ad6265SDimitry Andric return Symbolizer->tryAddingSymbolicOperand(Inst, *CommentStream, Value,
3481ad6265SDimitry Andric Address, IsBranch, Offset,
3581ad6265SDimitry Andric OpSize, InstSize);
360b57cec5SDimitry Andric return false;
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
tryAddingPcLoadReferenceComment(int64_t Value,uint64_t Address) const390b57cec5SDimitry Andric void MCDisassembler::tryAddingPcLoadReferenceComment(int64_t Value,
400b57cec5SDimitry Andric uint64_t Address) const {
410b57cec5SDimitry Andric if (Symbolizer)
42480093f4SDimitry Andric Symbolizer->tryAddingPcLoadReferenceComment(*CommentStream, Value, Address);
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric
setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer)450b57cec5SDimitry Andric void MCDisassembler::setSymbolizer(std::unique_ptr<MCSymbolizer> Symzer) {
460b57cec5SDimitry Andric Symbolizer = std::move(Symzer);
470b57cec5SDimitry Andric }
485ffd83dbSDimitry Andric
495ffd83dbSDimitry Andric #define SMC_PCASE(A, P) \
505ffd83dbSDimitry Andric case XCOFF::XMC_##A: \
515ffd83dbSDimitry Andric return P;
525ffd83dbSDimitry Andric
getSMCPriority(XCOFF::StorageMappingClass SMC)535ffd83dbSDimitry Andric static uint8_t getSMCPriority(XCOFF::StorageMappingClass SMC) {
545ffd83dbSDimitry Andric switch (SMC) {
555ffd83dbSDimitry Andric SMC_PCASE(PR, 1)
565ffd83dbSDimitry Andric SMC_PCASE(RO, 1)
575ffd83dbSDimitry Andric SMC_PCASE(DB, 1)
585ffd83dbSDimitry Andric SMC_PCASE(GL, 1)
595ffd83dbSDimitry Andric SMC_PCASE(XO, 1)
605ffd83dbSDimitry Andric SMC_PCASE(SV, 1)
615ffd83dbSDimitry Andric SMC_PCASE(SV64, 1)
625ffd83dbSDimitry Andric SMC_PCASE(SV3264, 1)
635ffd83dbSDimitry Andric SMC_PCASE(TI, 1)
645ffd83dbSDimitry Andric SMC_PCASE(TB, 1)
655ffd83dbSDimitry Andric SMC_PCASE(RW, 1)
665ffd83dbSDimitry Andric SMC_PCASE(TC0, 0)
675ffd83dbSDimitry Andric SMC_PCASE(TC, 1)
685ffd83dbSDimitry Andric SMC_PCASE(TD, 1)
695ffd83dbSDimitry Andric SMC_PCASE(DS, 1)
705ffd83dbSDimitry Andric SMC_PCASE(UA, 1)
715ffd83dbSDimitry Andric SMC_PCASE(BS, 1)
725ffd83dbSDimitry Andric SMC_PCASE(UC, 1)
735ffd83dbSDimitry Andric SMC_PCASE(TL, 1)
745ffd83dbSDimitry Andric SMC_PCASE(UL, 1)
755ffd83dbSDimitry Andric SMC_PCASE(TE, 1)
765ffd83dbSDimitry Andric #undef SMC_PCASE
775ffd83dbSDimitry Andric }
785ffd83dbSDimitry Andric return 0;
795ffd83dbSDimitry Andric }
805ffd83dbSDimitry Andric
815ffd83dbSDimitry Andric /// The function is for symbol sorting when symbols have the same address.
825ffd83dbSDimitry Andric /// The symbols in the same section are sorted in ascending order.
835ffd83dbSDimitry Andric /// llvm-objdump -D will choose the highest priority symbol to display when
845ffd83dbSDimitry Andric /// there are symbols with the same address.
operator <(const XCOFFSymbolInfoTy & SymInfo) const85bdd1243dSDimitry Andric bool XCOFFSymbolInfoTy::operator<(const XCOFFSymbolInfoTy &SymInfo) const {
865ffd83dbSDimitry Andric // Label symbols have higher priority than non-label symbols.
875ffd83dbSDimitry Andric if (IsLabel != SymInfo.IsLabel)
885ffd83dbSDimitry Andric return SymInfo.IsLabel;
895ffd83dbSDimitry Andric
905ffd83dbSDimitry Andric // Symbols with a StorageMappingClass have higher priority than those without.
9181ad6265SDimitry Andric if (StorageMappingClass.has_value() !=
9281ad6265SDimitry Andric SymInfo.StorageMappingClass.has_value())
9381ad6265SDimitry Andric return SymInfo.StorageMappingClass.has_value();
945ffd83dbSDimitry Andric
9581ad6265SDimitry Andric if (StorageMappingClass) {
96bdd1243dSDimitry Andric return getSMCPriority(*StorageMappingClass) <
97bdd1243dSDimitry Andric getSMCPriority(*SymInfo.StorageMappingClass);
985ffd83dbSDimitry Andric }
995ffd83dbSDimitry Andric
1005ffd83dbSDimitry Andric return false;
1015ffd83dbSDimitry Andric }
102