TableGen.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #include "TableGenBackends.h" // Declares all backends.
  13. #include "llvm/Support/CommandLine.h"
  14. #include "llvm/Support/InitLLVM.h"
  15. #include "llvm/TableGen/Main.h"
  16. #include "llvm/TableGen/Record.h"
  17. #include "llvm/TableGen/SetTheory.h"
  18. using namespace llvm;
  19. enum ActionType {
  20. PrintRecords,
  21. PrintDetailedRecords,
  22. NullBackend,
  23. DumpJSON,
  24. GenEmitter,
  25. GenRegisterInfo,
  26. GenInstrInfo,
  27. GenInstrDocs,
  28. GenAsmWriter,
  29. GenAsmMatcher,
  30. GenDisassembler,
  31. GenPseudoLowering,
  32. GenCompressInst,
  33. GenCallingConv,
  34. GenDAGISel,
  35. GenDFAPacketizer,
  36. GenFastISel,
  37. GenSubtarget,
  38. GenIntrinsicEnums,
  39. GenIntrinsicImpl,
  40. PrintEnums,
  41. PrintSets,
  42. GenOptParserDefs,
  43. GenOptRST,
  44. GenCTags,
  45. GenAttributes,
  46. GenSearchableTables,
  47. GenGlobalISel,
  48. GenGICombiner,
  49. GenX86EVEX2VEXTables,
  50. GenX86FoldTables,
  51. GenX86MnemonicTables,
  52. GenRegisterBank,
  53. GenExegesis,
  54. GenAutomata,
  55. GenDirectivesEnumDecl,
  56. GenDirectivesEnumImpl,
  57. GenDXILOperation,
  58. GenRISCVTargetDef,
  59. };
  60. namespace llvm {
  61. cl::opt<bool> EmitLongStrLiterals(
  62. "long-string-literals",
  63. cl::desc("when emitting large string tables, prefer string literals over "
  64. "comma-separated char literals. This can be a readability and "
  65. "compile-time performance win, but upsets some compilers"),
  66. cl::Hidden, cl::init(true));
  67. } // end namespace llvm
  68. namespace {
  69. cl::opt<ActionType> Action(
  70. cl::desc("Action to perform:"),
  71. cl::values(
  72. clEnumValN(PrintRecords, "print-records",
  73. "Print all records to stdout (default)"),
  74. clEnumValN(PrintDetailedRecords, "print-detailed-records",
  75. "Print full details of all records to stdout"),
  76. clEnumValN(NullBackend, "null-backend",
  77. "Do nothing after parsing (useful for timing)"),
  78. clEnumValN(DumpJSON, "dump-json",
  79. "Dump all records as machine-readable JSON"),
  80. clEnumValN(GenEmitter, "gen-emitter", "Generate machine code emitter"),
  81. clEnumValN(GenRegisterInfo, "gen-register-info",
  82. "Generate registers and register classes info"),
  83. clEnumValN(GenInstrInfo, "gen-instr-info",
  84. "Generate instruction descriptions"),
  85. clEnumValN(GenInstrDocs, "gen-instr-docs",
  86. "Generate instruction documentation"),
  87. clEnumValN(GenCallingConv, "gen-callingconv",
  88. "Generate calling convention descriptions"),
  89. clEnumValN(GenAsmWriter, "gen-asm-writer", "Generate assembly writer"),
  90. clEnumValN(GenDisassembler, "gen-disassembler",
  91. "Generate disassembler"),
  92. clEnumValN(GenPseudoLowering, "gen-pseudo-lowering",
  93. "Generate pseudo instruction lowering"),
  94. clEnumValN(GenCompressInst, "gen-compress-inst-emitter",
  95. "Generate RISCV compressed instructions."),
  96. clEnumValN(GenAsmMatcher, "gen-asm-matcher",
  97. "Generate assembly instruction matcher"),
  98. clEnumValN(GenDAGISel, "gen-dag-isel",
  99. "Generate a DAG instruction selector"),
  100. clEnumValN(GenDFAPacketizer, "gen-dfa-packetizer",
  101. "Generate DFA Packetizer for VLIW targets"),
  102. clEnumValN(GenFastISel, "gen-fast-isel",
  103. "Generate a \"fast\" instruction selector"),
  104. clEnumValN(GenSubtarget, "gen-subtarget",
  105. "Generate subtarget enumerations"),
  106. clEnumValN(GenIntrinsicEnums, "gen-intrinsic-enums",
  107. "Generate intrinsic enums"),
  108. clEnumValN(GenIntrinsicImpl, "gen-intrinsic-impl",
  109. "Generate intrinsic information"),
  110. clEnumValN(PrintEnums, "print-enums", "Print enum values for a class"),
  111. clEnumValN(PrintSets, "print-sets",
  112. "Print expanded sets for testing DAG exprs"),
  113. clEnumValN(GenOptParserDefs, "gen-opt-parser-defs",
  114. "Generate option definitions"),
  115. clEnumValN(GenOptRST, "gen-opt-rst", "Generate option RST"),
  116. clEnumValN(GenCTags, "gen-ctags", "Generate ctags-compatible index"),
  117. clEnumValN(GenAttributes, "gen-attrs", "Generate attributes"),
  118. clEnumValN(GenSearchableTables, "gen-searchable-tables",
  119. "Generate generic binary-searchable table"),
  120. clEnumValN(GenGlobalISel, "gen-global-isel",
  121. "Generate GlobalISel selector"),
  122. clEnumValN(GenGICombiner, "gen-global-isel-combiner",
  123. "Generate GlobalISel combiner"),
  124. clEnumValN(GenX86EVEX2VEXTables, "gen-x86-EVEX2VEX-tables",
  125. "Generate X86 EVEX to VEX compress tables"),
  126. clEnumValN(GenX86FoldTables, "gen-x86-fold-tables",
  127. "Generate X86 fold tables"),
  128. clEnumValN(GenX86MnemonicTables, "gen-x86-mnemonic-tables",
  129. "Generate X86 mnemonic 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(GenDXILOperation, "gen-dxil-operation",
  140. "Generate DXIL operation information"),
  141. clEnumValN(GenRISCVTargetDef, "gen-riscv-target-def",
  142. "Generate the list of CPU for RISCV")));
  143. cl::OptionCategory PrintEnumsCat("Options for -print-enums");
  144. cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
  145. cl::value_desc("class name"),
  146. cl::cat(PrintEnumsCat));
  147. bool LLVMTableGenMain(raw_ostream &OS, RecordKeeper &Records) {
  148. switch (Action) {
  149. case PrintRecords:
  150. OS << Records; // No argument, dump all contents
  151. break;
  152. case PrintDetailedRecords:
  153. EmitDetailedRecords(Records, OS);
  154. break;
  155. case NullBackend: // No backend at all.
  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 GenX86MnemonicTables:
  257. EmitX86MnemonicTables(Records, OS);
  258. break;
  259. case GenX86FoldTables:
  260. EmitX86FoldTables(Records, OS);
  261. break;
  262. case GenExegesis:
  263. EmitExegesis(Records, OS);
  264. break;
  265. case GenAutomata:
  266. EmitAutomata(Records, OS);
  267. break;
  268. case GenDirectivesEnumDecl:
  269. EmitDirectivesDecl(Records, OS);
  270. break;
  271. case GenDirectivesEnumImpl:
  272. EmitDirectivesImpl(Records, OS);
  273. break;
  274. case GenDXILOperation:
  275. EmitDXILOperation(Records, OS);
  276. break;
  277. case GenRISCVTargetDef:
  278. EmitRISCVTargetDef(Records, OS);
  279. break;
  280. }
  281. return false;
  282. }
  283. }
  284. int main(int argc, char **argv) {
  285. InitLLVM X(argc, argv);
  286. cl::ParseCommandLineOptions(argc, argv);
  287. return TableGenMain(argv[0], &LLVMTableGenMain);
  288. }
  289. #ifndef __has_feature
  290. #define __has_feature(x) 0
  291. #endif
  292. #if __has_feature(address_sanitizer) || \
  293. (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) || \
  294. __has_feature(leak_sanitizer)
  295. #include <sanitizer/lsan_interface.h>
  296. // Disable LeakSanitizer for this binary as it has too many leaks that are not
  297. // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
  298. LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
  299. #endif