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