MCDisassembler.cpp 3.2 KB

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