MCDisassembler.cpp 3.4 KB

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