xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/WebAssemblyDisassemblerEmitter.cpp (revision 1fd87a682ad7442327078e1eeb63edc4258f9815)
10b57cec5SDimitry Andric //===- WebAssemblyDisassemblerEmitter.cpp - Disassembler tables -*- C++ -*-===//
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 // This file is part of the WebAssembly Disassembler Emitter.
100b57cec5SDimitry Andric // It contains the implementation of the disassembler tables.
110b57cec5SDimitry Andric // Documentation for the disassembler emitter in general can be found in
120b57cec5SDimitry Andric // WebAssemblyDisassemblerEmitter.h.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "WebAssemblyDisassemblerEmitter.h"
17*1fd87a68SDimitry Andric #include "CodeGenInstruction.h"
18*1fd87a68SDimitry Andric #include "llvm/ADT/STLExtras.h"
19*1fd87a68SDimitry Andric #include "llvm/Support/raw_ostream.h"
200b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace llvm {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric static constexpr int WebAssemblyInstructionTableSize = 256;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric void emitWebAssemblyDisassemblerTables(
270b57cec5SDimitry Andric     raw_ostream &OS,
280b57cec5SDimitry Andric     const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
290b57cec5SDimitry Andric   // First lets organize all opcodes by (prefix) byte. Prefix 0 is the
300b57cec5SDimitry Andric   // starting table.
310b57cec5SDimitry Andric   std::map<unsigned,
320b57cec5SDimitry Andric            std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
330b57cec5SDimitry Andric       OpcodeTable;
340b57cec5SDimitry Andric   for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
350b57cec5SDimitry Andric     auto &CGI = *NumberedInstructions[I];
360b57cec5SDimitry Andric     auto &Def = *CGI.TheDef;
370b57cec5SDimitry Andric     if (!Def.getValue("Inst"))
380b57cec5SDimitry Andric       continue;
390b57cec5SDimitry Andric     auto &Inst = *Def.getValueAsBitsInit("Inst");
400b57cec5SDimitry Andric     auto Opc = static_cast<unsigned>(
410b57cec5SDimitry Andric         reinterpret_cast<IntInit *>(Inst.convertInitializerTo(IntRecTy::get()))
420b57cec5SDimitry Andric             ->getValue());
430b57cec5SDimitry Andric     if (Opc == 0xFFFFFFFF)
440b57cec5SDimitry Andric       continue; // No opcode defined.
45e8d8bef9SDimitry Andric     assert(Opc <= 0xFFFFFF);
46e8d8bef9SDimitry Andric     unsigned Prefix;
47e8d8bef9SDimitry Andric     if (Opc <= 0xFFFF) {
48e8d8bef9SDimitry Andric       Prefix = Opc >> 8;
490b57cec5SDimitry Andric       Opc = Opc & 0xFF;
50e8d8bef9SDimitry Andric     } else {
51e8d8bef9SDimitry Andric       Prefix = Opc >> 16;
52e8d8bef9SDimitry Andric       Opc = Opc & 0xFFFF;
53e8d8bef9SDimitry Andric     }
540b57cec5SDimitry Andric     auto &CGIP = OpcodeTable[Prefix][Opc];
550b57cec5SDimitry Andric     // All wasm instructions have a StackBased field of type string, we only
560b57cec5SDimitry Andric     // want the instructions for which this is "true".
570b57cec5SDimitry Andric     auto StackString =
580b57cec5SDimitry Andric         Def.getValue("StackBased")->getValue()->getCastTo(StringRecTy::get());
590b57cec5SDimitry Andric     auto IsStackBased =
600b57cec5SDimitry Andric         StackString &&
610b57cec5SDimitry Andric         reinterpret_cast<const StringInit *>(StackString)->getValue() == "true";
620b57cec5SDimitry Andric     if (!IsStackBased)
630b57cec5SDimitry Andric       continue;
640b57cec5SDimitry Andric     if (CGIP.second) {
650b57cec5SDimitry Andric       // We already have an instruction for this slot, so decide which one
660b57cec5SDimitry Andric       // should be the canonical one. This determines which variant gets
670b57cec5SDimitry Andric       // printed in a disassembly. We want e.g. "call" not "i32.call", and
680b57cec5SDimitry Andric       // "end" when we don't know if its "end_loop" or "end_block" etc.
690b57cec5SDimitry Andric       auto IsCanonicalExisting = CGIP.second->TheDef->getValue("IsCanonical")
700b57cec5SDimitry Andric                                      ->getValue()
710b57cec5SDimitry Andric                                      ->getAsString() == "1";
720b57cec5SDimitry Andric       // We already have one marked explicitly as canonical, so keep it.
730b57cec5SDimitry Andric       if (IsCanonicalExisting)
740b57cec5SDimitry Andric         continue;
750b57cec5SDimitry Andric       auto IsCanonicalNew =
760b57cec5SDimitry Andric           Def.getValue("IsCanonical")->getValue()->getAsString() == "1";
770b57cec5SDimitry Andric       // If the new one is explicitly marked as canonical, take it.
780b57cec5SDimitry Andric       if (!IsCanonicalNew) {
790b57cec5SDimitry Andric         // Neither the existing or new instruction is canonical.
800b57cec5SDimitry Andric         // Pick the one with the shortest name as heuristic.
810b57cec5SDimitry Andric         // Though ideally IsCanonical is always defined for at least one
820b57cec5SDimitry Andric         // variant so this never has to apply.
830b57cec5SDimitry Andric         if (CGIP.second->AsmString.size() <= CGI.AsmString.size())
840b57cec5SDimitry Andric           continue;
850b57cec5SDimitry Andric       }
860b57cec5SDimitry Andric     }
870b57cec5SDimitry Andric     // Set this instruction as the one to use.
880b57cec5SDimitry Andric     CGIP = std::make_pair(I, &CGI);
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric   OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
910b57cec5SDimitry Andric   OS << "\n";
920b57cec5SDimitry Andric   OS << "namespace llvm {\n\n";
930b57cec5SDimitry Andric   OS << "static constexpr int WebAssemblyInstructionTableSize = ";
940b57cec5SDimitry Andric   OS << WebAssemblyInstructionTableSize << ";\n\n";
950b57cec5SDimitry Andric   OS << "enum EntryType : uint8_t { ";
960b57cec5SDimitry Andric   OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
970b57cec5SDimitry Andric   OS << "struct WebAssemblyInstruction {\n";
980b57cec5SDimitry Andric   OS << "  uint16_t Opcode;\n";
990b57cec5SDimitry Andric   OS << "  EntryType ET;\n";
1000b57cec5SDimitry Andric   OS << "  uint8_t NumOperands;\n";
1010b57cec5SDimitry Andric   OS << "  uint16_t OperandStart;\n";
1020b57cec5SDimitry Andric   OS << "};\n\n";
1030b57cec5SDimitry Andric   std::vector<std::string> OperandTable, CurOperandList;
1040b57cec5SDimitry Andric   // Output one table per prefix.
1050b57cec5SDimitry Andric   for (auto &PrefixPair : OpcodeTable) {
1060b57cec5SDimitry Andric     if (PrefixPair.second.empty())
1070b57cec5SDimitry Andric       continue;
1080b57cec5SDimitry Andric     OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
1090b57cec5SDimitry Andric     OS << "[] = {\n";
1100b57cec5SDimitry Andric     for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
1110b57cec5SDimitry Andric       auto InstIt = PrefixPair.second.find(I);
1120b57cec5SDimitry Andric       if (InstIt != PrefixPair.second.end()) {
1130b57cec5SDimitry Andric         // Regular instruction.
1140b57cec5SDimitry Andric         assert(InstIt->second.second);
1150b57cec5SDimitry Andric         auto &CGI = *InstIt->second.second;
1160b57cec5SDimitry Andric         OS << "  // 0x";
1170b57cec5SDimitry Andric         OS.write_hex(static_cast<unsigned long long>(I));
1180b57cec5SDimitry Andric         OS << ": " << CGI.AsmString << "\n";
1190b57cec5SDimitry Andric         OS << "  { " << InstIt->second.first << ", ET_Instruction, ";
1200b57cec5SDimitry Andric         OS << CGI.Operands.OperandList.size() << ", ";
1210b57cec5SDimitry Andric         // Collect operand types for storage in a shared list.
1220b57cec5SDimitry Andric         CurOperandList.clear();
1230b57cec5SDimitry Andric         for (auto &Op : CGI.Operands.OperandList) {
1240b57cec5SDimitry Andric           assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
1250b57cec5SDimitry Andric           CurOperandList.push_back(Op.OperandType);
1260b57cec5SDimitry Andric         }
1270b57cec5SDimitry Andric         // See if we already have stored this sequence before. This is not
1280b57cec5SDimitry Andric         // strictly necessary but makes the table really small.
1290b57cec5SDimitry Andric         size_t OperandStart = OperandTable.size();
1300b57cec5SDimitry Andric         if (CurOperandList.size() <= OperandTable.size()) {
1310b57cec5SDimitry Andric           for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
1320b57cec5SDimitry Andric                ++J) {
1330b57cec5SDimitry Andric             size_t K = 0;
1340b57cec5SDimitry Andric             for (; K < CurOperandList.size(); ++K) {
1350b57cec5SDimitry Andric               if (OperandTable[J + K] != CurOperandList[K]) break;
1360b57cec5SDimitry Andric             }
1370b57cec5SDimitry Andric             if (K == CurOperandList.size()) {
1380b57cec5SDimitry Andric               OperandStart = J;
1390b57cec5SDimitry Andric               break;
1400b57cec5SDimitry Andric             }
1410b57cec5SDimitry Andric           }
1420b57cec5SDimitry Andric         }
1430b57cec5SDimitry Andric         // Store operands if no prior occurrence.
1440b57cec5SDimitry Andric         if (OperandStart == OperandTable.size()) {
145e8d8bef9SDimitry Andric           llvm::append_range(OperandTable, CurOperandList);
1460b57cec5SDimitry Andric         }
1470b57cec5SDimitry Andric         OS << OperandStart;
1480b57cec5SDimitry Andric       } else {
1490b57cec5SDimitry Andric         auto PrefixIt = OpcodeTable.find(I);
1500b57cec5SDimitry Andric         // If we have a non-empty table for it that's not 0, this is a prefix.
1510b57cec5SDimitry Andric         if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
1520b57cec5SDimitry Andric           OS << "  { 0, ET_Prefix, 0, 0";
1530b57cec5SDimitry Andric         } else {
1540b57cec5SDimitry Andric           OS << "  { 0, ET_Unused, 0, 0";
1550b57cec5SDimitry Andric         }
1560b57cec5SDimitry Andric       }
1570b57cec5SDimitry Andric       OS << "  },\n";
1580b57cec5SDimitry Andric     }
1590b57cec5SDimitry Andric     OS << "};\n\n";
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric   // Create a table of all operands:
1620b57cec5SDimitry Andric   OS << "const uint8_t OperandTable[] = {\n";
1630b57cec5SDimitry Andric   for (auto &Op : OperandTable) {
1640b57cec5SDimitry Andric     OS << "  " << Op << ",\n";
1650b57cec5SDimitry Andric   }
1660b57cec5SDimitry Andric   OS << "};\n\n";
1670b57cec5SDimitry Andric   // Create a table of all extension tables:
1680b57cec5SDimitry Andric   OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
1690b57cec5SDimitry Andric   OS << "PrefixTable[] = {\n";
1700b57cec5SDimitry Andric   for (auto &PrefixPair : OpcodeTable) {
1710b57cec5SDimitry Andric     if (PrefixPair.second.empty() || !PrefixPair.first)
1720b57cec5SDimitry Andric       continue;
1730b57cec5SDimitry Andric     OS << "  { " << PrefixPair.first << ", InstructionTable"
1740b57cec5SDimitry Andric        << PrefixPair.first;
1750b57cec5SDimitry Andric     OS << " },\n";
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric   OS << "  { 0, nullptr }\n};\n\n";
1788bcb0991SDimitry Andric   OS << "} // end namespace llvm\n";
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric } // namespace llvm
182