xref: /src/contrib/llvm-project/llvm/utils/TableGen/TableGen.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
130815c53SDimitry Andric //===- TableGen.cpp - Top-Level TableGen implementation for LLVM ----------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
930815c53SDimitry Andric // This file contains the main function for LLVM's TableGen.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten 
137fa27ce4SDimitry Andric #include "llvm/ADT/StringRef.h"
1459850d08SRoman Divacky #include "llvm/Support/CommandLine.h"
15b60736ecSDimitry Andric #include "llvm/Support/InitLLVM.h"
167fa27ce4SDimitry Andric #include "llvm/Support/raw_ostream.h"
1730815c53SDimitry Andric #include "llvm/TableGen/Main.h"
1830815c53SDimitry Andric #include "llvm/TableGen/Record.h"
195ca98fd9SDimitry Andric #include "llvm/TableGen/SetTheory.h"
207fa27ce4SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
217fa27ce4SDimitry Andric #include <cassert>
227fa27ce4SDimitry Andric #include <string>
237fa27ce4SDimitry Andric #include <vector>
2430815c53SDimitry Andric 
25009b1c42SEd Schouten using namespace llvm;
26009b1c42SEd Schouten 
27e6d15924SDimitry Andric namespace llvm {
28cfca06d7SDimitry Andric cl::opt<bool> EmitLongStrLiterals(
29cfca06d7SDimitry Andric     "long-string-literals",
30cfca06d7SDimitry Andric     cl::desc("when emitting large string tables, prefer string literals over "
31cfca06d7SDimitry Andric              "comma-separated char literals. This can be a readability and "
32cfca06d7SDimitry Andric              "compile-time performance win, but upsets some compilers"),
33cfca06d7SDimitry Andric     cl::Hidden, cl::init(true));
34e6d15924SDimitry Andric } // end namespace llvm
35e6d15924SDimitry Andric 
36009b1c42SEd Schouten namespace {
377fa27ce4SDimitry Andric 
3871d5a254SDimitry Andric cl::OptionCategory PrintEnumsCat("Options for -print-enums");
391d5ae102SDimitry Andric cl::opt<std::string> Class("class", cl::desc("Print Enum list for this class"),
401d5ae102SDimitry Andric                            cl::value_desc("class name"),
411d5ae102SDimitry Andric                            cl::cat(PrintEnumsCat));
42009b1c42SEd Schouten 
PrintRecords(RecordKeeper & Records,raw_ostream & OS)437fa27ce4SDimitry Andric void PrintRecords(RecordKeeper &Records, raw_ostream &OS) {
4430815c53SDimitry Andric   OS << Records; // No argument, dump all contents
457fa27ce4SDimitry Andric }
467fa27ce4SDimitry Andric 
PrintEnums(RecordKeeper & Records,raw_ostream & OS)477fa27ce4SDimitry Andric void PrintEnums(RecordKeeper &Records, raw_ostream &OS) {
4867c32a98SDimitry Andric   for (Record *Rec : Records.getAllDerivedDefinitions(Class))
4967c32a98SDimitry Andric     OS << Rec->getName() << ", ";
5030815c53SDimitry Andric   OS << "\n";
51009b1c42SEd Schouten }
527fa27ce4SDimitry Andric 
PrintSets(RecordKeeper & Records,raw_ostream & OS)537fa27ce4SDimitry Andric void PrintSets(RecordKeeper &Records, raw_ostream &OS) {
5456fe8f14SDimitry Andric   SetTheory Sets;
5556fe8f14SDimitry Andric   Sets.addFieldExpander("Set", "Elements");
5667c32a98SDimitry Andric   for (Record *Rec : Records.getAllDerivedDefinitions("Set")) {
5767c32a98SDimitry Andric     OS << Rec->getName() << " = [";
5867c32a98SDimitry Andric     const std::vector<Record *> *Elts = Sets.expand(Rec);
5956fe8f14SDimitry Andric     assert(Elts && "Couldn't expand Set instance");
6067c32a98SDimitry Andric     for (Record *Elt : *Elts)
6167c32a98SDimitry Andric       OS << ' ' << Elt->getName();
6230815c53SDimitry Andric     OS << " ]\n";
6356fe8f14SDimitry Andric   }
64009b1c42SEd Schouten }
65009b1c42SEd Schouten 
667fa27ce4SDimitry Andric TableGen::Emitter::Opt X[] = {
677fa27ce4SDimitry Andric     {"print-records", PrintRecords, "Print all records to stdout (default)",
687fa27ce4SDimitry Andric      true},
697fa27ce4SDimitry Andric     {"print-detailed-records", EmitDetailedRecords,
707fa27ce4SDimitry Andric      "Print full details of all records to stdout"},
__anon98730d120202() 717fa27ce4SDimitry Andric     {"null-backend", [](RecordKeeper &Records, raw_ostream &OS) {},
727fa27ce4SDimitry Andric      "Do nothing after parsing (useful for timing)"},
737fa27ce4SDimitry Andric     {"dump-json", EmitJSON, "Dump all records as machine-readable JSON"},
747fa27ce4SDimitry Andric     {"print-enums", PrintEnums, "Print enum values for a class"},
757fa27ce4SDimitry Andric     {"print-sets", PrintSets, "Print expanded sets for testing DAG exprs"},
767fa27ce4SDimitry Andric };
777fa27ce4SDimitry Andric 
787fa27ce4SDimitry Andric } // namespace
79009b1c42SEd Schouten 
main(int argc,char ** argv)8030815c53SDimitry Andric int main(int argc, char **argv) {
81b60736ecSDimitry Andric   InitLLVM X(argc, argv);
8230815c53SDimitry Andric   cl::ParseCommandLineOptions(argc, argv);
8330815c53SDimitry Andric 
847fa27ce4SDimitry Andric   return TableGenMain(argv[0]);
85009b1c42SEd Schouten }
865ca98fd9SDimitry Andric 
871d5ae102SDimitry Andric #ifndef __has_feature
881d5ae102SDimitry Andric #define __has_feature(x) 0
891d5ae102SDimitry Andric #endif
901d5ae102SDimitry Andric 
91ecbca9f5SDimitry Andric #if __has_feature(address_sanitizer) ||                                        \
92ecbca9f5SDimitry Andric     (defined(__SANITIZE_ADDRESS__) && defined(__GNUC__)) ||                    \
931d5ae102SDimitry Andric     __has_feature(leak_sanitizer)
941d5ae102SDimitry Andric 
955ca98fd9SDimitry Andric #include <sanitizer/lsan_interface.h>
965ca98fd9SDimitry Andric // Disable LeakSanitizer for this binary as it has too many leaks that are not
975ca98fd9SDimitry Andric // very interesting to fix. See compiler-rt/include/sanitizer/lsan_interface.h .
__lsan_is_turned_off()98044eb2f6SDimitry Andric LLVM_ATTRIBUTE_USED int __lsan_is_turned_off() { return 1; }
991d5ae102SDimitry Andric 
1001d5ae102SDimitry Andric #endif
101