1*27f30029SMichael Kruse //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===// 2*27f30029SMichael Kruse // 3*27f30029SMichael Kruse // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*27f30029SMichael Kruse // See https://llvm.org/LICENSE.txt for license information. 5*27f30029SMichael Kruse // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*27f30029SMichael Kruse // 7*27f30029SMichael Kruse //===----------------------------------------------------------------------===// 8*27f30029SMichael Kruse // 9*27f30029SMichael Kruse // This file contains the global defintions (mostly command line parameters) 10*27f30029SMichael Kruse // shared between llvm-tblgen and llvm-min-tblgen. 11*27f30029SMichael Kruse // 12*27f30029SMichael Kruse //===----------------------------------------------------------------------===// 13*27f30029SMichael Kruse 14*27f30029SMichael Kruse #include "TableGen.h" 15*27f30029SMichael Kruse #include "llvm/ADT/StringRef.h" 16*27f30029SMichael Kruse #include "llvm/Support/CommandLine.h" 17*27f30029SMichael Kruse #include "llvm/Support/InitLLVM.h" 18*27f30029SMichael Kruse #include "llvm/Support/raw_ostream.h" 19*27f30029SMichael Kruse #include "llvm/TableGen/Main.h" 20*27f30029SMichael Kruse #include "llvm/TableGen/Record.h" 21*27f30029SMichael Kruse #include "llvm/TableGen/SetTheory.h" 22*27f30029SMichael Kruse #include "llvm/TableGen/TableGenBackend.h" 23*27f30029SMichael Kruse #include <cassert> 24*27f30029SMichael Kruse #include <string> 25*27f30029SMichael Kruse #include <vector> 26*27f30029SMichael Kruse 27*27f30029SMichael Kruse using namespace llvm; 28*27f30029SMichael Kruse 29*27f30029SMichael Kruse namespace llvm { 30*27f30029SMichael Kruse cl::opt<bool> EmitLongStrLiterals( 31*27f30029SMichael Kruse "long-string-literals", 32*27f30029SMichael Kruse cl::desc("when emitting large string tables, prefer string literals over " 33*27f30029SMichael Kruse "comma-separated char literals. This can be a readability and " 34*27f30029SMichael Kruse "compile-time performance win, but upsets some compilers"), 35*27f30029SMichael Kruse cl::Hidden, cl::init(true)); 36*27f30029SMichael Kruse } // end namespace llvm 37*27f30029SMichael Kruse 38*27f30029SMichael Kruse static cl::OptionCategory PrintEnumsCat("Options for -print-enums"); 39*27f30029SMichael Kruse static cl::opt<std::string> Class("class", 40*27f30029SMichael Kruse cl::desc("Print Enum list for this class"), 41*27f30029SMichael Kruse cl::value_desc("class name"), 42*27f30029SMichael Kruse cl::cat(PrintEnumsCat)); 43*27f30029SMichael Kruse 44*27f30029SMichael Kruse static void printRecords(const RecordKeeper &Records, raw_ostream &OS) { 45*27f30029SMichael Kruse OS << Records; // No argument, dump all contents 46*27f30029SMichael Kruse } 47*27f30029SMichael Kruse 48*27f30029SMichael Kruse static void printEnums(const RecordKeeper &Records, raw_ostream &OS) { 49*27f30029SMichael Kruse for (const Record *Rec : Records.getAllDerivedDefinitions(Class)) 50*27f30029SMichael Kruse OS << Rec->getName() << ", "; 51*27f30029SMichael Kruse OS << "\n"; 52*27f30029SMichael Kruse } 53*27f30029SMichael Kruse 54*27f30029SMichael Kruse static void printSets(const RecordKeeper &Records, raw_ostream &OS) { 55*27f30029SMichael Kruse SetTheory Sets; 56*27f30029SMichael Kruse Sets.addFieldExpander("Set", "Elements"); 57*27f30029SMichael Kruse for (const Record *Rec : Records.getAllDerivedDefinitions("Set")) { 58*27f30029SMichael Kruse OS << Rec->getName() << " = ["; 59*27f30029SMichael Kruse const std::vector<const Record *> *Elts = Sets.expand(Rec); 60*27f30029SMichael Kruse assert(Elts && "Couldn't expand Set instance"); 61*27f30029SMichael Kruse for (const Record *Elt : *Elts) 62*27f30029SMichael Kruse OS << ' ' << Elt->getName(); 63*27f30029SMichael Kruse OS << " ]\n"; 64*27f30029SMichael Kruse } 65*27f30029SMichael Kruse } 66*27f30029SMichael Kruse 67*27f30029SMichael Kruse static TableGen::Emitter::Opt X[] = { 68*27f30029SMichael Kruse {"print-records", printRecords, "Print all records to stdout (default)", 69*27f30029SMichael Kruse true}, 70*27f30029SMichael Kruse {"print-detailed-records", EmitDetailedRecords, 71*27f30029SMichael Kruse "Print full details of all records to stdout"}, 72*27f30029SMichael Kruse {"null-backend", [](const RecordKeeper &Records, raw_ostream &OS) {}, 73*27f30029SMichael Kruse "Do nothing after parsing (useful for timing)"}, 74*27f30029SMichael Kruse {"dump-json", EmitJSON, "Dump all records as machine-readable JSON"}, 75*27f30029SMichael Kruse {"print-enums", printEnums, "Print enum values for a class"}, 76*27f30029SMichael Kruse {"print-sets", printSets, "Print expanded sets for testing DAG exprs"}, 77*27f30029SMichael Kruse }; 78*27f30029SMichael Kruse 79*27f30029SMichael Kruse int tblgen_main(int argc, char **argv) { 80*27f30029SMichael Kruse InitLLVM X(argc, argv); 81*27f30029SMichael Kruse cl::ParseCommandLineOptions(argc, argv); 82*27f30029SMichael Kruse 83*27f30029SMichael Kruse return TableGenMain(argv[0]); 84*27f30029SMichael Kruse } 85*27f30029SMichael Kruse 86*27f30029SMichael Kruse #ifndef __has_feature 87*27f30029SMichael Kruse #define __has_feature(x) 0 88*27f30029SMichael Kruse #endif 89*27f30029SMichael Kruse 90*27f30029SMichael Kruse #if __has_feature(address_sanitizer) || \ 91*27f30029SMichael Kruse (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) || \ 92*27f30029SMichael Kruse __has_feature(leak_sanitizer) 93*27f30029SMichael Kruse 94*27f30029SMichael Kruse #include <sanitizer/lsan_interface.h> 95*27f30029SMichael Kruse // Disable LeakSanitizer for this binary as it has too many leaks that are not 96*27f30029SMichael Kruse // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h . 97*27f30029SMichael Kruse LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; } 98*27f30029SMichael Kruse 99*27f30029SMichael Kruse #endif 100