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 }; 58 59 namespace llvm { 60 /// Storage for TimeRegionsOpt as a global so that backends aren't required to 61 /// include CommandLine.h 62 bool TimeRegions = false; 63 } // end namespace llvm 64 65 namespace { 66 cl::opt<ActionType> Action( 67 cl::desc("Action to perform:"), 68 cl::values( 69 clEnumValN(PrintRecords, "print-records", 70 "Print all records to stdout (default)"), 71 clEnumValN(DumpJSON, "dump-json", 72 "Dump all records as machine-readable JSON"), 73 clEnumValN(GenEmitter, "gen-emitter", "Generate machine code emitter"), 74 clEnumValN(GenRegisterInfo, "gen-register-info", 75 "Generate registers and register classes info"), 76 clEnumValN(GenInstrInfo, "gen-instr-info", 77 "Generate instruction descriptions"), 78 clEnumValN(GenInstrDocs, "gen-instr-docs", 79 "Generate instruction documentation"), 80 clEnumValN(GenCallingConv, "gen-callingconv", 81 "Generate calling convention descriptions"), 82 clEnumValN(GenAsmWriter, "gen-asm-writer", "Generate assembly writer"), 83 clEnumValN(GenDisassembler, "gen-disassembler", 84 "Generate disassembler"), 85 clEnumValN(GenPseudoLowering, "gen-pseudo-lowering", 86 "Generate pseudo instruction lowering"), 87 clEnumValN(GenCompressInst, "gen-compress-inst-emitter", 88 "Generate RISCV compressed instructions."), 89 clEnumValN(GenAsmMatcher, "gen-asm-matcher", 90 "Generate assembly instruction matcher"), 91 clEnumValN(GenDAGISel, "gen-dag-isel", 92 "Generate a DAG instruction selector"), 93 clEnumValN(GenDFAPacketizer, "gen-dfa-packetizer", 94 "Generate DFA Packetizer for VLIW targets"), 95 clEnumValN(GenFastISel, "gen-fast-isel", 96 "Generate a \"fast\" instruction selector"), 97 clEnumValN(GenSubtarget, "gen-subtarget", 98 "Generate subtarget enumerations"), 99 clEnumValN(GenIntrinsicEnums, "gen-intrinsic-enums", 100 "Generate intrinsic enums"), 101 clEnumValN(GenIntrinsicImpl, "gen-intrinsic-impl", 102 "Generate intrinsic information"), 103 clEnumValN(PrintEnums, "print-enums", "Print enum values for a class"), 104 clEnumValN(PrintSets, "print-sets", 105 "Print expanded sets for testing DAG exprs"), 106 clEnumValN(GenOptParserDefs, "gen-opt-parser-defs", 107 "Generate option definitions"), 108 clEnumValN(GenOptRST, "gen-opt-rst", "Generate option RST"), 109 clEnumValN(GenCTags, "gen-ctags", "Generate ctags-compatible index"), 110 clEnumValN(GenAttributes, "gen-attrs", "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(GenGICombiner, "gen-global-isel-combiner", 116 "Generate GlobalISel combiner"), 117 clEnumValN(GenX86EVEX2VEXTables, "gen-x86-EVEX2VEX-tables", 118 "Generate X86 EVEX to VEX compress tables"), 119 clEnumValN(GenX86FoldTables, "gen-x86-fold-tables", 120 "Generate X86 fold tables"), 121 clEnumValN(GenRegisterBank, "gen-register-bank", 122 "Generate registers bank descriptions"), 123 clEnumValN(GenExegesis, "gen-exegesis", 124 "Generate llvm-exegesis tables"), 125 clEnumValN(GenAutomata, "gen-automata", "Generate generic automata"))); 126 127 cl::OptionCategory PrintEnumsCat("Options for -print-enums"); 128 cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"), 129 cl::value_desc("class name"), 130 cl::cat(PrintEnumsCat)); 131 132 cl::opt<bool, true> 133 TimeRegionsOpt("time-regions", 134 cl::desc("Time regions of tablegens execution"), 135 cl::location(TimeRegions)); 136 137 bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) { 138 switch (Action) { 139 case PrintRecords: 140 OS << Records; // No argument, dump all contents 141 break; 142 case DumpJSON: 143 EmitJSON(Records, OS); 144 break; 145 case GenEmitter: 146 EmitCodeEmitter(Records, OS); 147 break; 148 case GenRegisterInfo: 149 EmitRegisterInfo(Records, OS); 150 break; 151 case GenInstrInfo: 152 EmitInstrInfo(Records, OS); 153 break; 154 case GenInstrDocs: 155 EmitInstrDocs(Records, OS); 156 break; 157 case GenCallingConv: 158 EmitCallingConv(Records, OS); 159 break; 160 case GenAsmWriter: 161 EmitAsmWriter(Records, OS); 162 break; 163 case GenAsmMatcher: 164 EmitAsmMatcher(Records, OS); 165 break; 166 case GenDisassembler: 167 EmitDisassembler(Records, OS); 168 break; 169 case GenPseudoLowering: 170 EmitPseudoLowering(Records, OS); 171 break; 172 case GenCompressInst: 173 EmitCompressInst(Records, OS); 174 break; 175 case GenDAGISel: 176 EmitDAGISel(Records, OS); 177 break; 178 case GenDFAPacketizer: 179 EmitDFAPacketizer(Records, OS); 180 break; 181 case GenFastISel: 182 EmitFastISel(Records, OS); 183 break; 184 case GenSubtarget: 185 EmitSubtarget(Records, OS); 186 break; 187 case GenIntrinsicEnums: 188 EmitIntrinsicEnums(Records, OS); 189 break; 190 case GenIntrinsicImpl: 191 EmitIntrinsicImpl(Records, OS); 192 break; 193 case GenOptParserDefs: 194 EmitOptParser(Records, OS); 195 break; 196 case GenOptRST: 197 EmitOptRST(Records, OS); 198 break; 199 case PrintEnums: 200 { 201 for (Record *Rec : Records.getAllDerivedDefinitions(Class)) 202 OS << Rec->getName() << ", "; 203 OS << "\n"; 204 break; 205 } 206 case PrintSets: 207 { 208 SetTheory Sets; 209 Sets.addFieldExpander("Set", "Elements"); 210 for (Record *Rec : Records.getAllDerivedDefinitions("Set")) { 211 OS << Rec->getName() << " = ["; 212 const std::vector<Record*> *Elts = Sets.expand(Rec); 213 assert(Elts && "Couldn't expand Set instance"); 214 for (Record *Elt : *Elts) 215 OS << ' ' << Elt->getName(); 216 OS << " ]\n"; 217 } 218 break; 219 } 220 case GenCTags: 221 EmitCTags(Records, OS); 222 break; 223 case GenAttributes: 224 EmitAttributes(Records, OS); 225 break; 226 case GenSearchableTables: 227 EmitSearchableTables(Records, OS); 228 break; 229 case GenGlobalISel: 230 EmitGlobalISel(Records, OS); 231 break; 232 case GenGICombiner: 233 EmitGICombiner(Records, OS); 234 break; 235 case GenRegisterBank: 236 EmitRegisterBank(Records, OS); 237 break; 238 case GenX86EVEX2VEXTables: 239 EmitX86EVEX2VEXTables(Records, OS); 240 break; 241 case GenX86FoldTables: 242 EmitX86FoldTables(Records, OS); 243 break; 244 case GenExegesis: 245 EmitExegesis(Records, OS); 246 break; 247 case GenAutomata: 248 EmitAutomata(Records, OS); 249 break; 250 } 251 252 return false; 253 } 254 } 255 256 int main(int argc, char **argv) { 257 sys::PrintStackTraceOnErrorSignal(argv[0]); 258 PrettyStackTraceProgram X(argc, argv); 259 cl::ParseCommandLineOptions(argc, argv); 260 261 llvm_shutdown_obj Y; 262 263 return TableGenMain(argv[0], &LLVMTableGenMain); 264 } 265 266 #ifndef __has_feature 267 #define __has_feature(x) 0 268 #endif 269 270 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) || \ 271 __has_feature(leak_sanitizer) 272 273 #include <sanitizer/lsan_interface.h> 274 // Disable LeakSanitizer for this binary as it has too many leaks that are not 275 // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h . 276 LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; } 277 278 #endif 279