1//===- SystemZInsnDirectiveEmitter.cpp - Generate .insn match table -------===//
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 tablegen backend emits a match table for SystemZ .insn directives.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/TableGen/Error.h"
16#include "llvm/TableGen/Record.h"
17#include "llvm/TableGen/TableGenBackend.h"
18#include <string>
19
20using namespace llvm;
21
22namespace {
23
24struct InsnMatchEntry {
25 std::string Format;
26 std::string Opcode;
27 unsigned NumOptionalOperands;
28 SmallVector<std::string, 8> OperandKinds;
29};
30
31static StringRef getFormatName(const Record &Def) {
32 StringRef AsmString = Def.getValueAsString(FieldName: "AsmString");
33 if (!AsmString.consume_front(Prefix: ".insn "))
34 PrintFatalError(Rec: &Def, Msg: "expected .insn asm string");
35 return AsmString.take_until(F: [](char C) { return C == ','; });
36}
37
38static std::string getMatchClassKind(const Record &Def, const Init *Arg,
39 unsigned OperandIndex) {
40
41 // Obtain record of operand.
42 const Record *OpRec;
43 if (const DefInit *DefOp = dyn_cast<DefInit>(Val: Arg))
44 OpRec = DefOp->getDef();
45 else if (const DagInit *DagOp = dyn_cast<DagInit>(Val: Arg))
46 OpRec = DagOp->getOperatorAsDef(Loc: Def.getLoc());
47 else
48 PrintFatalError(Rec: &Def, Msg: "Unexpected Init Type (neither def nor dag) in .insn "
49 "directive operand");
50
51 // Get name of ParserMatchClass associated with operand.
52 StringRef MC =
53 OpRec->getValueAsDef(FieldName: "ParserMatchClass")->getValueAsString(FieldName: "Name");
54
55 // Prepend MCK_ and return.
56 return "MCK_" + std::string(MC);
57}
58
59static InsnMatchEntry buildInsnMatchEntry(const Record &Def, unsigned &MaxNum) {
60 const DagInit *OutOperands = Def.getValueAsDag(FieldName: "OutOperandList");
61 const DagInit *InOperands = Def.getValueAsDag(FieldName: "InOperandList");
62 if (OutOperands->getNumArgs() > 0)
63 PrintFatalError(Rec: &Def, Msg: ".insn directive may not have output operands");
64 unsigned InOps = InOperands->getNumArgs();
65 if (InOps == 0)
66 PrintFatalError(Rec: &Def, Msg: ".insn directive missing encoding operand");
67
68 InsnMatchEntry Entry;
69 Entry.Format = getFormatName(Def).str();
70 Entry.Opcode = ("SystemZ::" + Def.getName()).str();
71
72 for (unsigned I = 0; I < InOps; ++I)
73 Entry.OperandKinds.push_back(
74 Elt: getMatchClassKind(Def, Arg: InOperands->getArg(Num: I), OperandIndex: I));
75
76 Entry.NumOptionalOperands =
77 (unsigned)Def.getValueAsInt(FieldName: "NumOptionalOperands");
78 if (Entry.NumOptionalOperands > InOps)
79 PrintFatalError(Rec: &Def, Msg: "NumOptionalOperands exceeds operand count");
80
81 if (MaxNum < InOps)
82 MaxNum = InOps;
83
84 return Entry;
85}
86
87static void emitInsnDirectiveMatchTable(const RecordKeeper &RK,
88 raw_ostream &OS) {
89 emitSourceFileHeader(Desc: "Match Table for SystemZ .insn directive operand types",
90 OS);
91 unsigned MaxOperandCount = 0;
92 // This will hold all .insn directive definitions (~100 plus margin).
93 SmallVector<InsnMatchEntry, 128> Entries;
94 // Collect all InstSystemZ records that have IsInsnDirective set to 1.
95 for (const Record *Def : RK.getAllDerivedDefinitions(ClassName: "InstSystemZ"))
96 if (Def->getValueAsBit(FieldName: "IsInsnDirective"))
97 Entries.push_back(Elt: buildInsnMatchEntry(Def: *Def, MaxNum&: MaxOperandCount));
98
99 // Sort entries by format name.
100 llvm::sort(C&: Entries, Comp: [](const InsnMatchEntry &LHS, const InsnMatchEntry &RHS) {
101 return LHS.Format < RHS.Format;
102 });
103
104 OS << "constexpr unsigned MAX_INSN_OPERANDNUM = " << MaxOperandCount << ";\n";
105 OS << "\n";
106 OS << "struct InsnMatchEntry {\n";
107 OS << " StringRef Format;\n";
108 OS << " uint64_t Opcode;\n";
109 OS << " uint32_t NumOperands;\n";
110 OS << " uint32_t NumOptionalOperands;\n";
111 OS << " MatchClassKind OperandKinds[MAX_INSN_OPERANDNUM];\n";
112 OS << "};\n";
113 OS << "\n";
114 OS << "struct CompareInsn {\n";
115 OS << " bool operator()(const InsnMatchEntry &LHS, StringRef RHS) {\n";
116 OS << " return LHS.Format < RHS;\n";
117 OS << " }\n";
118 OS << " bool operator()(StringRef LHS, const InsnMatchEntry &RHS) {\n";
119 OS << " return LHS < RHS.Format;\n";
120 OS << " }\n";
121 OS << " bool operator()(const InsnMatchEntry &LHS,\n";
122 OS << " const InsnMatchEntry &RHS) {\n";
123 OS << " return LHS.Format < RHS.Format;\n";
124 OS << " }\n";
125 OS << "};\n";
126 OS << "\n";
127 OS << "/* Format, Opcode, NumOperands, NumOptionalOperands, OperandKinds "
128 "*/\n";
129 OS << "static InsnMatchEntry InsnMatchTable[] = {\n";
130 for (const InsnMatchEntry &Entry : Entries) {
131 OS << " {\"" << Entry.Format << "\", " << Entry.Opcode << ", "
132 << Entry.OperandKinds.size() << ", " << Entry.NumOptionalOperands
133 << ", {";
134 for (unsigned I = 0; I < Entry.OperandKinds.size(); ++I) {
135 if (I != 0)
136 OS << ", ";
137 OS << Entry.OperandKinds[I];
138 }
139 OS << "}},\n";
140 }
141 OS << "};\n";
142}
143} // namespace
144
145static TableGen::Emitter::Opt X("gen-insn-directive-match-table",
146 emitInsnDirectiveMatchTable,
147 "Generate SystemZ .insn match table.");
148