WebAssemblyDisassemblerEmitter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
  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. // This file is part of the WebAssembly Disassembler Emitter.
  10. // It contains the implementation of the disassembler tables.
  11. // Documentation for the disassembler emitter in general can be found in
  12. // WebAssemblyDisassemblerEmitter.h.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "WebAssemblyDisassemblerEmitter.h"
  16. #include "llvm/TableGen/Record.h"
  17. namespace llvm {
  18. static constexpr int WebAssemblyInstructionTableSize = 256;
  19. void emitWebAssemblyDisassemblerTables(
  20. raw_ostream &OS,
  21. const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
  22. // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
  23. // starting table.
  24. std::map<unsigned,
  25. std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
  26. OpcodeTable;
  27. for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
  28. auto &CGI = *NumberedInstructions[I];
  29. auto &Def = *CGI.TheDef;
  30. if (!Def.getValue("Inst"))
  31. continue;
  32. auto &Inst = *Def.getValueAsBitsInit("Inst");
  33. auto Opc = static_cast<unsigned>(
  34. reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get()))
  35. ->getValue());
  36. if (Opc == 0xFFFFFFFF)
  37. continue; // No opcode defined.
  38. assert(Opc <= 0xFFFFFF);
  39. unsigned Prefix;
  40. if (Opc <= 0xFFFF) {
  41. Prefix = Opc >> 8;
  42. Opc = Opc & 0xFF;
  43. } else {
  44. Prefix = Opc >> 16;
  45. Opc = Opc & 0xFFFF;
  46. }
  47. auto &CGIP = OpcodeTable[Prefix][Opc];
  48. // All wasm instructions have a StackBased field of type string, we only
  49. // want the instructions for which this is "true".
  50. auto StackString =
  51. Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
  52. auto IsStackBased =
  53. StackString &&
  54. reinterpret_cast<const StringInit *>(StackString)->getValue() == "true";
  55. if (!IsStackBased)
  56. continue;
  57. if (CGIP.second) {
  58. // We already have an instruction for this slot, so decide which one
  59. // should be the canonical one. This determines which variant gets
  60. // printed in a disassembly. We want e.g. "call" not "i32.call", and
  61. // "end" when we don't know if its "end_loop" or "end_block" etc.
  62. auto IsCanonicalExisting = CGIP.second->TheDef->getValue("IsCanonical")
  63. ->getValue()
  64. ->getAsString() == "1";
  65. // We already have one marked explicitly as canonical, so keep it.
  66. if (IsCanonicalExisting)
  67. continue;
  68. auto IsCanonicalNew =
  69. Def.getValue("IsCanonical")->getValue()->getAsString() == "1";
  70. // If the new one is explicitly marked as canonical, take it.
  71. if (!IsCanonicalNew) {
  72. // Neither the existing or new instruction is canonical.
  73. // Pick the one with the shortest name as heuristic.
  74. // Though ideally IsCanonical is always defined for at least one
  75. // variant so this never has to apply.
  76. if (CGIP.second->AsmString.size() <= CGI.AsmString.size())
  77. continue;
  78. }
  79. }
  80. // Set this instruction as the one to use.
  81. CGIP = std::make_pair(I, &CGI);
  82. }
  83. OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
  84. OS << "\n";
  85. OS << "namespace llvm {\n\n";
  86. OS << "static constexpr int WebAssemblyInstructionTableSize = ";
  87. OS << WebAssemblyInstructionTableSize << ";\n\n";
  88. OS << "enum EntryType : uint8_t { ";
  89. OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
  90. OS << "struct WebAssemblyInstruction {\n";
  91. OS << " uint16_t Opcode;\n";
  92. OS << " EntryType ET;\n";
  93. OS << " uint8_t NumOperands;\n";
  94. OS << " uint16_t OperandStart;\n";
  95. OS << "};\n\n";
  96. std::vector<std::string> OperandTable, CurOperandList;
  97. // Output one table per prefix.
  98. for (auto &PrefixPair : OpcodeTable) {
  99. if (PrefixPair.second.empty())
  100. continue;
  101. OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
  102. OS << "[] = {\n";
  103. for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
  104. auto InstIt = PrefixPair.second.find(I);
  105. if (InstIt != PrefixPair.second.end()) {
  106. // Regular instruction.
  107. assert(InstIt->second.second);
  108. auto &CGI = *InstIt->second.second;
  109. OS << " // 0x";
  110. OS.write_hex(static_cast<unsigned long long>(I));
  111. OS << ": " << CGI.AsmString << "\n";
  112. OS << " { " << InstIt->second.first << ", ET_Instruction, ";
  113. OS << CGI.Operands.OperandList.size() << ", ";
  114. // Collect operand types for storage in a shared list.
  115. CurOperandList.clear();
  116. for (auto &Op : CGI.Operands.OperandList) {
  117. assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
  118. CurOperandList.push_back(Op.OperandType);
  119. }
  120. // See if we already have stored this sequence before. This is not
  121. // strictly necessary but makes the table really small.
  122. size_t OperandStart = OperandTable.size();
  123. if (CurOperandList.size() <= OperandTable.size()) {
  124. for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
  125. ++J) {
  126. size_t K = 0;
  127. for (; K < CurOperandList.size(); ++K) {
  128. if (OperandTable[J + K] != CurOperandList[K]) break;
  129. }
  130. if (K == CurOperandList.size()) {
  131. OperandStart = J;
  132. break;
  133. }
  134. }
  135. }
  136. // Store operands if no prior occurrence.
  137. if (OperandStart == OperandTable.size()) {
  138. llvm::append_range(OperandTable, CurOperandList);
  139. }
  140. OS << OperandStart;
  141. } else {
  142. auto PrefixIt = OpcodeTable.find(I);
  143. // If we have a non-empty table for it that's not 0, this is a prefix.
  144. if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
  145. OS << " { 0, ET_Prefix, 0, 0";
  146. } else {
  147. OS << " { 0, ET_Unused, 0, 0";
  148. }
  149. }
  150. OS << " },\n";
  151. }
  152. OS << "};\n\n";
  153. }
  154. // Create a table of all operands:
  155. OS << "const uint8_t OperandTable[] = {\n";
  156. for (auto &Op : OperandTable) {
  157. OS << " " << Op << ",\n";
  158. }
  159. OS << "};\n\n";
  160. // Create a table of all extension tables:
  161. OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
  162. OS << "PrefixTable[] = {\n";
  163. for (auto &PrefixPair : OpcodeTable) {
  164. if (PrefixPair.second.empty() || !PrefixPair.first)
  165. continue;
  166. OS << " { " << PrefixPair.first << ", InstructionTable"
  167. << PrefixPair.first;
  168. OS << " },\n";
  169. }
  170. OS << " { 0, nullptr }\n};\n\n";
  171. OS << "} // end namespace llvm\n";
  172. }
  173. } // namespace llvm