1 //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the main function for LLVM's TableGen.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "TableGenBackends.h" // Declares all backends.
15 #include "llvm/Support/CommandLine.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/PrettyStackTrace.h"
18 #include "llvm/Support/Signals.h"
19 #include "llvm/TableGen/Main.h"
20 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/SetTheory.h"
22
23 using namespace llvm;
24
25 enum ActionType {
26 PrintRecords,
27 DumpJSON,
28 GenEmitter,
29 GenRegisterInfo,
30 GenInstrInfo,
31 GenInstrDocs,
32 GenAsmWriter,
33 GenAsmMatcher,
34 GenDisassembler,
35 GenPseudoLowering,
36 GenCompressInst,
37 GenCallingConv,
38 GenDAGISel,
39 GenDFAPacketizer,
40 GenFastISel,
41 GenSubtarget,
42 GenIntrinsicEnums,
43 GenIntrinsicImpl,
44 GenTgtIntrinsicEnums,
45 GenTgtIntrinsicImpl,
46 PrintEnums,
47 PrintSets,
48 GenOptParserDefs,
49 GenCTags,
50 GenAttributes,
51 GenSearchableTables,
52 GenGlobalISel,
53 GenX86EVEX2VEXTables,
54 GenX86FoldTables,
55 GenRegisterBank,
56 };
57
58 namespace {
59 cl::opt<ActionType>
60 Action(cl::desc("Action to perform:"),
61 cl::values(clEnumValN(PrintRecords, "print-records",
62 "Print all records to stdout (default)"),
63 clEnumValN(DumpJSON, "dump-json",
64 "Dump all records as machine-readable JSON"),
65 clEnumValN(GenEmitter, "gen-emitter",
66 "Generate machine code emitter"),
67 clEnumValN(GenRegisterInfo, "gen-register-info",
68 "Generate registers and register classes info"),
69 clEnumValN(GenInstrInfo, "gen-instr-info",
70 "Generate instruction descriptions"),
71 clEnumValN(GenInstrDocs, "gen-instr-docs",
72 "Generate instruction documentation"),
73 clEnumValN(GenCallingConv, "gen-callingconv",
74 "Generate calling convention descriptions"),
75 clEnumValN(GenAsmWriter, "gen-asm-writer",
76 "Generate assembly writer"),
77 clEnumValN(GenDisassembler, "gen-disassembler",
78 "Generate disassembler"),
79 clEnumValN(GenPseudoLowering, "gen-pseudo-lowering",
80 "Generate pseudo instruction lowering"),
81 clEnumValN(GenCompressInst, "gen-compress-inst-emitter",
82 "Generate RISCV compressed instructions."),
83 clEnumValN(GenAsmMatcher, "gen-asm-matcher",
84 "Generate assembly instruction matcher"),
85 clEnumValN(GenDAGISel, "gen-dag-isel",
86 "Generate a DAG instruction selector"),
87 clEnumValN(GenDFAPacketizer, "gen-dfa-packetizer",
88 "Generate DFA Packetizer for VLIW targets"),
89 clEnumValN(GenFastISel, "gen-fast-isel",
90 "Generate a \"fast\" instruction selector"),
91 clEnumValN(GenSubtarget, "gen-subtarget",
92 "Generate subtarget enumerations"),
93 clEnumValN(GenIntrinsicEnums, "gen-intrinsic-enums",
94 "Generate intrinsic enums"),
95 clEnumValN(GenIntrinsicImpl, "gen-intrinsic-impl",
96 "Generate intrinsic information"),
97 clEnumValN(GenTgtIntrinsicEnums, "gen-tgt-intrinsic-enums",
98 "Generate target intrinsic enums"),
99 clEnumValN(GenTgtIntrinsicImpl, "gen-tgt-intrinsic-impl",
100 "Generate target intrinsic information"),
101 clEnumValN(PrintEnums, "print-enums",
102 "Print enum values for a class"),
103 clEnumValN(PrintSets, "print-sets",
104 "Print expanded sets for testing DAG exprs"),
105 clEnumValN(GenOptParserDefs, "gen-opt-parser-defs",
106 "Generate option definitions"),
107 clEnumValN(GenCTags, "gen-ctags",
108 "Generate ctags-compatible index"),
109 clEnumValN(GenAttributes, "gen-attrs",
110 "Generate attributes"),
111 clEnumValN(GenSearchableTables, "gen-searchable-tables",
112 "Generate generic binary-searchable table"),
113 clEnumValN(GenGlobalISel, "gen-global-isel",
114 "Generate GlobalISel selector"),
115 clEnumValN(GenX86EVEX2VEXTables, "gen-x86-EVEX2VEX-tables",
116 "Generate X86 EVEX to VEX compress tables"),
117 clEnumValN(GenX86FoldTables, "gen-x86-fold-tables",
118 "Generate X86 fold tables"),
119 clEnumValN(GenRegisterBank, "gen-register-bank",
120 "Generate registers bank descriptions")));
121
122 cl::OptionCategory PrintEnumsCat("Options for -print-enums");
123 cl::opt<std::string>
124 Class("class", cl::desc("Print Enum list for this class"),
125 cl::value_desc("class name"), cl::cat(PrintEnumsCat));
126
LLVMTableGenMain(raw_ostream & OS,RecordKeeper & Records)127 bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
128 switch (Action) {
129 case PrintRecords:
130 OS << Records; // No argument, dump all contents
131 break;
132 case DumpJSON:
133 EmitJSON(Records, OS);
134 break;
135 case GenEmitter:
136 EmitCodeEmitter(Records, OS);
137 break;
138 case GenRegisterInfo:
139 EmitRegisterInfo(Records, OS);
140 break;
141 case GenInstrInfo:
142 EmitInstrInfo(Records, OS);
143 break;
144 case GenInstrDocs:
145 EmitInstrDocs(Records, OS);
146 break;
147 case GenCallingConv:
148 EmitCallingConv(Records, OS);
149 break;
150 case GenAsmWriter:
151 EmitAsmWriter(Records, OS);
152 break;
153 case GenAsmMatcher:
154 EmitAsmMatcher(Records, OS);
155 break;
156 case GenDisassembler:
157 EmitDisassembler(Records, OS);
158 break;
159 case GenPseudoLowering:
160 EmitPseudoLowering(Records, OS);
161 break;
162 case GenCompressInst:
163 EmitCompressInst(Records, OS);
164 break;
165 case GenDAGISel:
166 EmitDAGISel(Records, OS);
167 break;
168 case GenDFAPacketizer:
169 EmitDFAPacketizer(Records, OS);
170 break;
171 case GenFastISel:
172 EmitFastISel(Records, OS);
173 break;
174 case GenSubtarget:
175 EmitSubtarget(Records, OS);
176 break;
177 case GenIntrinsicEnums:
178 EmitIntrinsicEnums(Records, OS);
179 break;
180 case GenIntrinsicImpl:
181 EmitIntrinsicImpl(Records, OS);
182 break;
183 case GenTgtIntrinsicEnums:
184 EmitIntrinsicEnums(Records, OS, true);
185 break;
186 case GenTgtIntrinsicImpl:
187 EmitIntrinsicImpl(Records, OS, true);
188 break;
189 case GenOptParserDefs:
190 EmitOptParser(Records, OS);
191 break;
192 case PrintEnums:
193 {
194 for (Record *Rec : Records.getAllDerivedDefinitions(Class))
195 OS << Rec->getName() << ", ";
196 OS << "\n";
197 break;
198 }
199 case PrintSets:
200 {
201 SetTheory Sets;
202 Sets.addFieldExpander("Set", "Elements");
203 for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
204 OS << Rec->getName() << " = [";
205 const std::vector<Record*> *Elts = Sets.expand(Rec);
206 assert(Elts && "Couldn't expand Set instance");
207 for (Record *Elt : *Elts)
208 OS << ' ' << Elt->getName();
209 OS << " ]\n";
210 }
211 break;
212 }
213 case GenCTags:
214 EmitCTags(Records, OS);
215 break;
216 case GenAttributes:
217 EmitAttributes(Records, OS);
218 break;
219 case GenSearchableTables:
220 EmitSearchableTables(Records, OS);
221 break;
222 case GenGlobalISel:
223 EmitGlobalISel(Records, OS);
224 break;
225 case GenRegisterBank:
226 EmitRegisterBank(Records, OS);
227 break;
228 case GenX86EVEX2VEXTables:
229 EmitX86EVEX2VEXTables(Records, OS);
230 break;
231 case GenX86FoldTables:
232 EmitX86FoldTables(Records, OS);
233 break;
234 }
235
236 return false;
237 }
238 }
239
main(int argc,char ** argv)240 int main(int argc, char **argv) {
241 sys::PrintStackTraceOnErrorSignal(argv[0]);
242 PrettyStackTraceProgram X(argc, argv);
243 cl::ParseCommandLineOptions(argc, argv);
244
245 llvm_shutdown_obj Y;
246
247 return TableGenMain(argv[0], &LLVMTableGenMain);
248 }
249
250 #ifdef __has_feature
251 #if __has_feature(address_sanitizer)
252 #include <sanitizer/lsan_interface.h>
253 // Disable LeakSanitizer for this binary as it has too many leaks that are not
254 // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
__lsan_is_turned_off()255 LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
256 #endif // __has_feature(address_sanitizer)
257 #endif // defined(__has_feature)
258