DisassemblerEmitter.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===- DisassemblerEmitter.cpp - Generate a disassembler ------------------===//
  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 "CodeGenTarget.h"
  9. #include "WebAssemblyDisassemblerEmitter.h"
  10. #include "X86DisassemblerTables.h"
  11. #include "X86RecognizableInstr.h"
  12. #include "llvm/TableGen/Error.h"
  13. #include "llvm/TableGen/Record.h"
  14. #include "llvm/TableGen/TableGenBackend.h"
  15. using namespace llvm;
  16. using namespace llvm::X86Disassembler;
  17. /// DisassemblerEmitter - Contains disassembler table emitters for various
  18. /// architectures.
  19. /// X86 Disassembler Emitter
  20. ///
  21. /// *** IF YOU'RE HERE TO RESOLVE A "Primary decode conflict", LOOK DOWN NEAR
  22. /// THE END OF THIS COMMENT!
  23. ///
  24. /// The X86 disassembler emitter is part of the X86 Disassembler, which is
  25. /// documented in lib/Target/X86/X86Disassembler.h.
  26. ///
  27. /// The emitter produces the tables that the disassembler uses to translate
  28. /// instructions. The emitter generates the following tables:
  29. ///
  30. /// - One table (CONTEXTS_SYM) that contains a mapping of attribute masks to
  31. /// instruction contexts. Although for each attribute there are cases where
  32. /// that attribute determines decoding, in the majority of cases decoding is
  33. /// the same whether or not an attribute is present. For example, a 64-bit
  34. /// instruction with an OPSIZE prefix and an XS prefix decodes the same way in
  35. /// all cases as a 64-bit instruction with only OPSIZE set. (The XS prefix
  36. /// may have effects on its execution, but does not change the instruction
  37. /// returned.) This allows considerable space savings in other tables.
  38. /// - Six tables (ONEBYTE_SYM, TWOBYTE_SYM, THREEBYTE38_SYM, THREEBYTE3A_SYM,
  39. /// THREEBYTEA6_SYM, and THREEBYTEA7_SYM contain the hierarchy that the
  40. /// decoder traverses while decoding an instruction. At the lowest level of
  41. /// this hierarchy are instruction UIDs, 16-bit integers that can be used to
  42. /// uniquely identify the instruction and correspond exactly to its position
  43. /// in the list of CodeGenInstructions for the target.
  44. /// - One table (INSTRUCTIONS_SYM) contains information about the operands of
  45. /// each instruction and how to decode them.
  46. ///
  47. /// During table generation, there may be conflicts between instructions that
  48. /// occupy the same space in the decode tables. These conflicts are resolved as
  49. /// follows in setTableFields() (X86DisassemblerTables.cpp)
  50. ///
  51. /// - If the current context is the native context for one of the instructions
  52. /// (that is, the attributes specified for it in the LLVM tables specify
  53. /// precisely the current context), then it has priority.
  54. /// - If the current context isn't native for either of the instructions, then
  55. /// the higher-priority context wins (that is, the one that is more specific).
  56. /// That hierarchy is determined by outranks() (X86DisassemblerTables.cpp)
  57. /// - If the current context is native for both instructions, then the table
  58. /// emitter reports a conflict and dies.
  59. ///
  60. /// *** RESOLUTION FOR "Primary decode conflict"S
  61. ///
  62. /// If two instructions collide, typically the solution is (in order of
  63. /// likelihood):
  64. ///
  65. /// (1) to filter out one of the instructions by editing filter()
  66. /// (X86RecognizableInstr.cpp). This is the most common resolution, but
  67. /// check the Intel manuals first to make sure that (2) and (3) are not the
  68. /// problem.
  69. /// (2) to fix the tables (X86.td and its subsidiaries) so the opcodes are
  70. /// accurate. Sometimes they are not.
  71. /// (3) to fix the tables to reflect the actual context (for example, required
  72. /// prefixes), and possibly to add a new context by editing
  73. /// include/llvm/Support/X86DisassemblerDecoderCommon.h. This is unlikely
  74. /// to be the cause.
  75. ///
  76. /// DisassemblerEmitter.cpp contains the implementation for the emitter,
  77. /// which simply pulls out instructions from the CodeGenTarget and pushes them
  78. /// into X86DisassemblerTables.
  79. /// X86DisassemblerTables.h contains the interface for the instruction tables,
  80. /// which manage and emit the structures discussed above.
  81. /// X86DisassemblerTables.cpp contains the implementation for the instruction
  82. /// tables.
  83. /// X86ModRMFilters.h contains filters that can be used to determine which
  84. /// ModR/M values are valid for a particular instruction. These are used to
  85. /// populate ModRMDecisions.
  86. /// X86RecognizableInstr.h contains the interface for a single instruction,
  87. /// which knows how to translate itself from a CodeGenInstruction and provide
  88. /// the information necessary for integration into the tables.
  89. /// X86RecognizableInstr.cpp contains the implementation for a single
  90. /// instruction.
  91. namespace llvm {
  92. extern void EmitFixedLenDecoder(RecordKeeper &RK, raw_ostream &OS,
  93. const std::string &PredicateNamespace,
  94. const std::string &GPrefix,
  95. const std::string &GPostfix,
  96. const std::string &ROK,
  97. const std::string &RFail, const std::string &L);
  98. void EmitDisassembler(RecordKeeper &Records, raw_ostream &OS) {
  99. CodeGenTarget Target(Records);
  100. emitSourceFileHeader(" * " + Target.getName().str() + " Disassembler", OS);
  101. // X86 uses a custom disassembler.
  102. if (Target.getName() == "X86") {
  103. DisassemblerTables Tables;
  104. ArrayRef<const CodeGenInstruction*> numberedInstructions =
  105. Target.getInstructionsByEnumValue();
  106. for (unsigned i = 0, e = numberedInstructions.size(); i != e; ++i)
  107. RecognizableInstr::processInstr(Tables, *numberedInstructions[i], i);
  108. if (Tables.hasConflicts()) {
  109. PrintError(Target.getTargetRecord()->getLoc(), "Primary decode conflict");
  110. return;
  111. }
  112. Tables.emit(OS);
  113. return;
  114. }
  115. // WebAssembly has variable length opcodes, so can't use EmitFixedLenDecoder
  116. // below (which depends on a Size table-gen Record), and also uses a custom
  117. // disassembler.
  118. if (Target.getName() == "WebAssembly") {
  119. emitWebAssemblyDisassemblerTables(OS, Target.getInstructionsByEnumValue());
  120. return;
  121. }
  122. // ARM and Thumb have a CHECK() macro to deal with DecodeStatuses.
  123. if (Target.getName() == "ARM" || Target.getName() == "Thumb" ||
  124. Target.getName() == "AArch64" || Target.getName() == "ARM64") {
  125. std::string PredicateNamespace = std::string(Target.getName());
  126. if (PredicateNamespace == "Thumb")
  127. PredicateNamespace = "ARM";
  128. EmitFixedLenDecoder(Records, OS, PredicateNamespace,
  129. "if (!Check(S, ", "))",
  130. "S", "MCDisassembler::Fail",
  131. " MCDisassembler::DecodeStatus S = "
  132. "MCDisassembler::Success;\n(void)S;");
  133. return;
  134. }
  135. EmitFixedLenDecoder(Records, OS, std::string(Target.getName()), "if (",
  136. " == MCDisassembler::Fail)", "MCDisassembler::Success",
  137. "MCDisassembler::Fail", "");
  138. }
  139. } // end namespace llvm