xref: /freebsd-src/contrib/llvm-project/llvm/utils/TableGen/TableGen.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the main function for LLVM's TableGen.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*06c3fb27SDimitry Andric #include "llvm/ADT/StringRef.h"
140b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
15e8d8bef9SDimitry Andric #include "llvm/Support/InitLLVM.h"
16*06c3fb27SDimitry Andric #include "llvm/Support/raw_ostream.h"
170b57cec5SDimitry Andric #include "llvm/TableGen/Main.h"
180b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
190b57cec5SDimitry Andric #include "llvm/TableGen/SetTheory.h"
20*06c3fb27SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
21*06c3fb27SDimitry Andric #include <cassert>
22*06c3fb27SDimitry Andric #include <string>
23*06c3fb27SDimitry Andric #include <vector>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric namespace llvm {
285ffd83dbSDimitry Andric cl::opt<bool> EmitLongStrLiterals(
295ffd83dbSDimitry Andric     "long-string-literals",
305ffd83dbSDimitry Andric     cl::desc("when emitting large string tables, prefer string literals over "
315ffd83dbSDimitry Andric              "comma-separated char literals. This can be a readability and "
325ffd83dbSDimitry Andric              "compile-time performance win, but upsets some compilers"),
335ffd83dbSDimitry Andric     cl::Hidden, cl::init(true));
340b57cec5SDimitry Andric } // end namespace llvm
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric namespace {
37*06c3fb27SDimitry Andric 
380b57cec5SDimitry Andric cl::OptionCategory PrintEnumsCat("Options for -print-enums");
398bcb0991SDimitry Andric cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
408bcb0991SDimitry Andric                            cl::value_desc("class name"),
418bcb0991SDimitry Andric                            cl::cat(PrintEnumsCat));
420b57cec5SDimitry Andric 
PrintRecords(RecordKeeper & Records,raw_ostream & OS)43*06c3fb27SDimitry Andric void PrintRecords(RecordKeeper &Records, raw_ostream &OS) {
440b57cec5SDimitry Andric   OS << Records; // No argument, dump all contents
45*06c3fb27SDimitry Andric }
46*06c3fb27SDimitry Andric 
PrintEnums(RecordKeeper & Records,raw_ostream & OS)47*06c3fb27SDimitry Andric void PrintEnums(RecordKeeper &Records, raw_ostream &OS) {
480b57cec5SDimitry Andric   for (Record *Rec : Records.getAllDerivedDefinitions(Class))
490b57cec5SDimitry Andric     OS << Rec->getName() << ", ";
500b57cec5SDimitry Andric   OS << "\n";
510b57cec5SDimitry Andric }
52*06c3fb27SDimitry Andric 
PrintSets(RecordKeeper & Records,raw_ostream & OS)53*06c3fb27SDimitry Andric void PrintSets(RecordKeeper &Records, raw_ostream &OS) {
540b57cec5SDimitry Andric   SetTheory Sets;
550b57cec5SDimitry Andric   Sets.addFieldExpander("Set", "Elements");
560b57cec5SDimitry Andric   for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
570b57cec5SDimitry Andric     OS << Rec->getName() << " = [";
580b57cec5SDimitry Andric     const std::vector<Record *> *Elts = Sets.expand(Rec);
590b57cec5SDimitry Andric     assert(Elts && "Couldn't expand Set instance");
600b57cec5SDimitry Andric     for (Record *Elt : *Elts)
610b57cec5SDimitry Andric       OS << ' ' << Elt->getName();
620b57cec5SDimitry Andric     OS << " ]\n";
630b57cec5SDimitry Andric   }
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
66*06c3fb27SDimitry Andric TableGen::Emitter::Opt X[] = {
67*06c3fb27SDimitry Andric     {"print-records", PrintRecords, "Print all records to stdout (default)",
68*06c3fb27SDimitry Andric      true},
69*06c3fb27SDimitry Andric     {"print-detailed-records", EmitDetailedRecords,
70*06c3fb27SDimitry Andric      "Print full details of all records to stdout"},
__anon185da32f0202() 71*06c3fb27SDimitry Andric     {"null-backend", [](RecordKeeper &Records, raw_ostream &OS) {},
72*06c3fb27SDimitry Andric      "Do nothing after parsing (useful for timing)"},
73*06c3fb27SDimitry Andric     {"dump-json", EmitJSON, "Dump all records as machine-readable JSON"},
74*06c3fb27SDimitry Andric     {"print-enums", PrintEnums, "Print enum values for a class"},
75*06c3fb27SDimitry Andric     {"print-sets", PrintSets, "Print expanded sets for testing DAG exprs"},
76*06c3fb27SDimitry Andric };
77*06c3fb27SDimitry Andric 
78*06c3fb27SDimitry Andric } // namespace
790b57cec5SDimitry Andric 
main(int argc,char ** argv)800b57cec5SDimitry Andric int main(int argc, char **argv) {
81e8d8bef9SDimitry Andric   InitLLVM X(argc, argv);
820b57cec5SDimitry Andric   cl::ParseCommandLineOptions(argc, argv);
830b57cec5SDimitry Andric 
84*06c3fb27SDimitry Andric   return TableGenMain(argv[0]);
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
878bcb0991SDimitry Andric #ifndef __has_feature
888bcb0991SDimitry Andric #define __has_feature(x) 0
898bcb0991SDimitry Andric #endif
908bcb0991SDimitry Andric 
911fd87a68SDimitry Andric #if __has_feature(address_sanitizer) ||                                        \
921fd87a68SDimitry Andric     (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) ||                    \
938bcb0991SDimitry Andric     __has_feature(leak_sanitizer)
948bcb0991SDimitry Andric 
950b57cec5SDimitry Andric #include <sanitizer/lsan_interface.h>
960b57cec5SDimitry Andric // Disable LeakSanitizer for this binary as it has too many leaks that are not
970b57cec5SDimitry Andric // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
__lsan_is_turned_off()980b57cec5SDimitry Andric LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
998bcb0991SDimitry Andric 
1008bcb0991SDimitry Andric #endif
101