#include "WebAssemblyDisassemblerEmitter.h"
#include "CodeGenInstruction.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"
namespace llvm {
static constexpr int WebAssemblyInstructionTableSize = 256;
void emitWebAssemblyDisassemblerTables(
raw_ostream &OS,
const ArrayRef<const CodeGenInstruction *> &NumberedInstructions) {
std::map<unsigned,
std::map<unsigned, std::pair<unsigned, const CodeGenInstruction *>>>
OpcodeTable;
for (unsigned I = 0; I != NumberedInstructions.size(); ++I) {
auto &CGI = *NumberedInstructions[I];
auto &Def = *CGI.TheDef;
if (!Def.getValue("Inst"))
continue;
auto &Inst = *Def.getValueAsBitsInit("Inst");
RecordKeeper &RK = Inst.getRecordKeeper();
unsigned Opc = static_cast<unsigned>(
cast<IntInit>(Inst.convertInitializerTo(IntRecTy::get(RK)))
->getValue());
if (Opc == 0xFFFFFFFF)
continue; assert(Opc <= 0xFFFFFF);
unsigned Prefix;
if (Opc <= 0xFFFF) {
Prefix = Opc >> 8;
Opc = Opc & 0xFF;
} else {
Prefix = Opc >> 16;
Opc = Opc & 0xFFFF;
}
auto &CGIP = OpcodeTable[Prefix][Opc];
bool IsStackBased = Def.getValueAsBit("StackBased");
if (!IsStackBased)
continue;
if (CGIP.second) {
bool IsCanonicalExisting = CGIP.second->TheDef->getValueAsBit("IsCanonical");
if (IsCanonicalExisting)
continue;
bool IsCanonicalNew = Def.getValueAsBit("IsCanonical");
if (!IsCanonicalNew) {
if (CGIP.second->AsmString.size() <= CGI.AsmString.size())
continue;
}
}
CGIP = std::make_pair(I, &CGI);
}
OS << "#include \"MCTargetDesc/WebAssemblyMCTargetDesc.h\"\n";
OS << "\n";
OS << "namespace llvm {\n\n";
OS << "static constexpr int WebAssemblyInstructionTableSize = ";
OS << WebAssemblyInstructionTableSize << ";\n\n";
OS << "enum EntryType : uint8_t { ";
OS << "ET_Unused, ET_Prefix, ET_Instruction };\n\n";
OS << "struct WebAssemblyInstruction {\n";
OS << " uint16_t Opcode;\n";
OS << " EntryType ET;\n";
OS << " uint8_t NumOperands;\n";
OS << " uint16_t OperandStart;\n";
OS << "};\n\n";
std::vector<std::string> OperandTable, CurOperandList;
for (auto &PrefixPair : OpcodeTable) {
if (PrefixPair.second.empty())
continue;
OS << "WebAssemblyInstruction InstructionTable" << PrefixPair.first;
OS << "[] = {\n";
for (unsigned I = 0; I < WebAssemblyInstructionTableSize; I++) {
auto InstIt = PrefixPair.second.find(I);
if (InstIt != PrefixPair.second.end()) {
assert(InstIt->second.second);
auto &CGI = *InstIt->second.second;
OS << " // 0x";
OS.write_hex(static_cast<unsigned long long>(I));
OS << ": " << CGI.AsmString << "\n";
OS << " { " << InstIt->second.first << ", ET_Instruction, ";
OS << CGI.Operands.OperandList.size() << ", ";
CurOperandList.clear();
for (auto &Op : CGI.Operands.OperandList) {
assert(Op.OperandType != "MCOI::OPERAND_UNKNOWN");
CurOperandList.push_back(Op.OperandType);
}
size_t OperandStart = OperandTable.size();
if (CurOperandList.size() <= OperandTable.size()) {
for (size_t J = 0; J <= OperandTable.size() - CurOperandList.size();
++J) {
size_t K = 0;
for (; K < CurOperandList.size(); ++K) {
if (OperandTable[J + K] != CurOperandList[K]) break;
}
if (K == CurOperandList.size()) {
OperandStart = J;
break;
}
}
}
if (OperandStart == OperandTable.size()) {
llvm::append_range(OperandTable, CurOperandList);
}
OS << OperandStart;
} else {
auto PrefixIt = OpcodeTable.find(I);
if (PrefixIt != OpcodeTable.end() && I && !PrefixPair.first) {
OS << " { 0, ET_Prefix, 0, 0";
} else {
OS << " { 0, ET_Unused, 0, 0";
}
}
OS << " },\n";
}
OS << "};\n\n";
}
OS << "const uint8_t OperandTable[] = {\n";
for (auto &Op : OperandTable) {
OS << " " << Op << ",\n";
}
OS << "};\n\n";
OS << "struct { uint8_t Prefix; const WebAssemblyInstruction *Table; }\n";
OS << "PrefixTable[] = {\n";
for (auto &PrefixPair : OpcodeTable) {
if (PrefixPair.second.empty() || !PrefixPair.first)
continue;
OS << " { " << PrefixPair.first << ", InstructionTable"
<< PrefixPair.first;
OS << " },\n";
}
OS << " { 0, nullptr }\n};\n\n";
OS << "} // end namespace llvm\n";
}
}