xref: /freebsd/contrib/llvm-project/llvm/utils/TableGen/TableGen.cpp (revision f9fd7337f63698f33239c58c07bf430198235a22)
1 //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===//
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 contains the main function for LLVM's TableGen.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "TableGenBackends.h" // Declares all backends.
14 #include "llvm/Support/CommandLine.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/Signals.h"
18 #include "llvm/TableGen/Main.h"
19 #include "llvm/TableGen/Record.h"
20 #include "llvm/TableGen/SetTheory.h"
21 
22 using namespace llvm;
23 
24 enum ActionType {
25   PrintRecords,
26   DumpJSON,
27   GenEmitter,
28   GenRegisterInfo,
29   GenInstrInfo,
30   GenInstrDocs,
31   GenAsmWriter,
32   GenAsmMatcher,
33   GenDisassembler,
34   GenPseudoLowering,
35   GenCompressInst,
36   GenCallingConv,
37   GenDAGISel,
38   GenDFAPacketizer,
39   GenFastISel,
40   GenSubtarget,
41   GenIntrinsicEnums,
42   GenIntrinsicImpl,
43   PrintEnums,
44   PrintSets,
45   GenOptParserDefs,
46   GenOptRST,
47   GenCTags,
48   GenAttributes,
49   GenSearchableTables,
50   GenGlobalISel,
51   GenGICombiner,
52   GenX86EVEX2VEXTables,
53   GenX86FoldTables,
54   GenRegisterBank,
55   GenExegesis,
56   GenAutomata,
57   GenDirectivesEnumDecl,
58   GenDirectivesEnumImpl,
59   GenDirectivesEnumGen,
60 };
61 
62 namespace llvm {
63 /// Storage for TimeRegionsOpt as a global so that backends aren't required to
64 /// include CommandLine.h
65 bool TimeRegions = false;
66 cl::opt<bool> EmitLongStrLiterals(
67     "long-string-literals",
68     cl::desc("when emitting large string tables, prefer string literals over "
69              "comma-separated char literals. This can be a readability and "
70              "compile-time performance win, but upsets some compilers"),
71     cl::Hidden, cl::init(true));
72 } // end namespace llvm
73 
74 namespace {
75 cl::opt<ActionType> Action(
76     cl::desc("Action to perform:"),
77     cl::values(
78         clEnumValN(PrintRecords, "print-records",
79                    "Print all records to stdout (default)"),
80         clEnumValN(DumpJSON, "dump-json",
81                    "Dump all records as machine-readable JSON"),
82         clEnumValN(GenEmitter, "gen-emitter", "Generate machine code emitter"),
83         clEnumValN(GenRegisterInfo, "gen-register-info",
84                    "Generate registers and register classes info"),
85         clEnumValN(GenInstrInfo, "gen-instr-info",
86                    "Generate instruction descriptions"),
87         clEnumValN(GenInstrDocs, "gen-instr-docs",
88                    "Generate instruction documentation"),
89         clEnumValN(GenCallingConv, "gen-callingconv",
90                    "Generate calling convention descriptions"),
91         clEnumValN(GenAsmWriter, "gen-asm-writer", "Generate assembly writer"),
92         clEnumValN(GenDisassembler, "gen-disassembler",
93                    "Generate disassembler"),
94         clEnumValN(GenPseudoLowering, "gen-pseudo-lowering",
95                    "Generate pseudo instruction lowering"),
96         clEnumValN(GenCompressInst, "gen-compress-inst-emitter",
97                    "Generate RISCV compressed instructions."),
98         clEnumValN(GenAsmMatcher, "gen-asm-matcher",
99                    "Generate assembly instruction matcher"),
100         clEnumValN(GenDAGISel, "gen-dag-isel",
101                    "Generate a DAG instruction selector"),
102         clEnumValN(GenDFAPacketizer, "gen-dfa-packetizer",
103                    "Generate DFA Packetizer for VLIW targets"),
104         clEnumValN(GenFastISel, "gen-fast-isel",
105                    "Generate a \"fast\" instruction selector"),
106         clEnumValN(GenSubtarget, "gen-subtarget",
107                    "Generate subtarget enumerations"),
108         clEnumValN(GenIntrinsicEnums, "gen-intrinsic-enums",
109                    "Generate intrinsic enums"),
110         clEnumValN(GenIntrinsicImpl, "gen-intrinsic-impl",
111                    "Generate intrinsic information"),
112         clEnumValN(PrintEnums, "print-enums", "Print enum values for a class"),
113         clEnumValN(PrintSets, "print-sets",
114                    "Print expanded sets for testing DAG exprs"),
115         clEnumValN(GenOptParserDefs, "gen-opt-parser-defs",
116                    "Generate option definitions"),
117         clEnumValN(GenOptRST, "gen-opt-rst", "Generate option RST"),
118         clEnumValN(GenCTags, "gen-ctags", "Generate ctags-compatible index"),
119         clEnumValN(GenAttributes, "gen-attrs", "Generate attributes"),
120         clEnumValN(GenSearchableTables, "gen-searchable-tables",
121                    "Generate generic binary-searchable table"),
122         clEnumValN(GenGlobalISel, "gen-global-isel",
123                    "Generate GlobalISel selector"),
124         clEnumValN(GenGICombiner, "gen-global-isel-combiner",
125                    "Generate GlobalISel combiner"),
126         clEnumValN(GenX86EVEX2VEXTables, "gen-x86-EVEX2VEX-tables",
127                    "Generate X86 EVEX to VEX compress tables"),
128         clEnumValN(GenX86FoldTables, "gen-x86-fold-tables",
129                    "Generate X86 fold tables"),
130         clEnumValN(GenRegisterBank, "gen-register-bank",
131                    "Generate registers bank descriptions"),
132         clEnumValN(GenExegesis, "gen-exegesis",
133                    "Generate llvm-exegesis tables"),
134         clEnumValN(GenAutomata, "gen-automata", "Generate generic automata"),
135         clEnumValN(GenDirectivesEnumDecl, "gen-directive-decl",
136                    "Generate directive related declaration code (header file)"),
137         clEnumValN(GenDirectivesEnumImpl, "gen-directive-impl",
138                    "Generate directive related implementation code"),
139         clEnumValN(GenDirectivesEnumGen, "gen-directive-gen",
140                    "Generate directive related implementation code part")));
141 
142 cl::OptionCategory PrintEnumsCat("Options for -print-enums");
143 cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
144                            cl::value_desc("class name"),
145                            cl::cat(PrintEnumsCat));
146 
147 cl::opt<bool, true>
148     TimeRegionsOpt("time-regions",
149                    cl::desc("Time regions of tablegens execution"),
150                    cl::location(TimeRegions));
151 
152 bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
153   switch (Action) {
154   case PrintRecords:
155     OS << Records;           // No argument, dump all contents
156     break;
157   case DumpJSON:
158     EmitJSON(Records, OS);
159     break;
160   case GenEmitter:
161     EmitCodeEmitter(Records, OS);
162     break;
163   case GenRegisterInfo:
164     EmitRegisterInfo(Records, OS);
165     break;
166   case GenInstrInfo:
167     EmitInstrInfo(Records, OS);
168     break;
169   case GenInstrDocs:
170     EmitInstrDocs(Records, OS);
171     break;
172   case GenCallingConv:
173     EmitCallingConv(Records, OS);
174     break;
175   case GenAsmWriter:
176     EmitAsmWriter(Records, OS);
177     break;
178   case GenAsmMatcher:
179     EmitAsmMatcher(Records, OS);
180     break;
181   case GenDisassembler:
182     EmitDisassembler(Records, OS);
183     break;
184   case GenPseudoLowering:
185     EmitPseudoLowering(Records, OS);
186     break;
187   case GenCompressInst:
188     EmitCompressInst(Records, OS);
189     break;
190   case GenDAGISel:
191     EmitDAGISel(Records, OS);
192     break;
193   case GenDFAPacketizer:
194     EmitDFAPacketizer(Records, OS);
195     break;
196   case GenFastISel:
197     EmitFastISel(Records, OS);
198     break;
199   case GenSubtarget:
200     EmitSubtarget(Records, OS);
201     break;
202   case GenIntrinsicEnums:
203     EmitIntrinsicEnums(Records, OS);
204     break;
205   case GenIntrinsicImpl:
206     EmitIntrinsicImpl(Records, OS);
207     break;
208   case GenOptParserDefs:
209     EmitOptParser(Records, OS);
210     break;
211   case GenOptRST:
212     EmitOptRST(Records, OS);
213     break;
214   case PrintEnums:
215   {
216     for (Record *Rec : Records.getAllDerivedDefinitions(Class))
217       OS << Rec->getName() << ", ";
218     OS << "\n";
219     break;
220   }
221   case PrintSets:
222   {
223     SetTheory Sets;
224     Sets.addFieldExpander("Set", "Elements");
225     for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
226       OS << Rec->getName() << " = [";
227       const std::vector<Record*> *Elts = Sets.expand(Rec);
228       assert(Elts && "Couldn't expand Set instance");
229       for (Record *Elt : *Elts)
230         OS << ' ' << Elt->getName();
231       OS << " ]\n";
232     }
233     break;
234   }
235   case GenCTags:
236     EmitCTags(Records, OS);
237     break;
238   case GenAttributes:
239     EmitAttributes(Records, OS);
240     break;
241   case GenSearchableTables:
242     EmitSearchableTables(Records, OS);
243     break;
244   case GenGlobalISel:
245     EmitGlobalISel(Records, OS);
246     break;
247   case GenGICombiner:
248     EmitGICombiner(Records, OS);
249     break;
250   case GenRegisterBank:
251     EmitRegisterBank(Records, OS);
252     break;
253   case GenX86EVEX2VEXTables:
254     EmitX86EVEX2VEXTables(Records, OS);
255     break;
256   case GenX86FoldTables:
257     EmitX86FoldTables(Records, OS);
258     break;
259   case GenExegesis:
260     EmitExegesis(Records, OS);
261     break;
262   case GenAutomata:
263     EmitAutomata(Records, OS);
264     break;
265   case GenDirectivesEnumDecl:
266     EmitDirectivesDecl(Records, OS);
267     break;
268   case GenDirectivesEnumImpl:
269     EmitDirectivesImpl(Records, OS);
270     break;
271   case GenDirectivesEnumGen:
272     EmitDirectivesGen(Records, OS);
273     break;
274   }
275 
276   return false;
277 }
278 }
279 
280 int main(int argc, char **argv) {
281   sys::PrintStackTraceOnErrorSignal(argv[0]);
282   PrettyStackTraceProgram X(argc, argv);
283   cl::ParseCommandLineOptions(argc, argv);
284 
285   llvm_shutdown_obj Y;
286 
287   return TableGenMain(argv[0], &LLVMTableGenMain);
288 }
289 
290 #ifndef __has_feature
291 #define __has_feature(x) 0
292 #endif
293 
294 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) ||       \
295     __has_feature(leak_sanitizer)
296 
297 #include <sanitizer/lsan_interface.h>
298 // Disable LeakSanitizer for this binary as it has too many leaks that are not
299 // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
300 LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
301 
302 #endif
303