LLVM API Documentation
00001 //===- Main.cpp - Top-Level TableGen implementation -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // TableGen is a tool which can be used to build up a description of something, 00011 // then invoke one or more "tablegen backends" to emit information about the 00012 // description in some predefined format. In practice, this is used by the LLVM 00013 // code generators to automate generation of a code generator through a 00014 // high-level description of the target. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #include "TGParser.h" 00019 #include "llvm/Support/CommandLine.h" 00020 #include "llvm/Support/FileSystem.h" 00021 #include "llvm/Support/MemoryBuffer.h" 00022 #include "llvm/Support/ToolOutputFile.h" 00023 #include "llvm/TableGen/Error.h" 00024 #include "llvm/TableGen/Main.h" 00025 #include "llvm/TableGen/Record.h" 00026 #include <algorithm> 00027 #include <cstdio> 00028 #include <system_error> 00029 using namespace llvm; 00030 00031 namespace { 00032 cl::opt<std::string> 00033 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), 00034 cl::init("-")); 00035 00036 cl::opt<std::string> 00037 DependFilename("d", 00038 cl::desc("Dependency filename"), 00039 cl::value_desc("filename"), 00040 cl::init("")); 00041 00042 cl::opt<std::string> 00043 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); 00044 00045 cl::list<std::string> 00046 IncludeDirs("I", cl::desc("Directory of include files"), 00047 cl::value_desc("directory"), cl::Prefix); 00048 } 00049 00050 /// \brief Create a dependency file for `-d` option. 00051 /// 00052 /// This functionality is really only for the benefit of the build system. 00053 /// It is similar to GCC's `-M*` family of options. 00054 static int createDependencyFile(const TGParser &Parser, const char *argv0) { 00055 if (OutputFilename == "-") { 00056 errs() << argv0 << ": the option -d must be used together with -o\n"; 00057 return 1; 00058 } 00059 std::error_code EC; 00060 tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text); 00061 if (EC) { 00062 errs() << argv0 << ": error opening " << DependFilename << ":" 00063 << EC.message() << "\n"; 00064 return 1; 00065 } 00066 DepOut.os() << OutputFilename << ":"; 00067 const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies(); 00068 for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(), 00069 E = Dependencies.end(); 00070 I != E; ++I) { 00071 DepOut.os() << " " << I->first; 00072 } 00073 DepOut.os() << "\n"; 00074 DepOut.keep(); 00075 return 0; 00076 } 00077 00078 namespace llvm { 00079 00080 int TableGenMain(char *argv0, TableGenMainFn *MainFn) { 00081 RecordKeeper Records; 00082 00083 // Parse the input file. 00084 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 00085 MemoryBuffer::getFileOrSTDIN(InputFilename); 00086 if (std::error_code EC = FileOrErr.getError()) { 00087 errs() << "Could not open input file '" << InputFilename 00088 << "': " << EC.message() << "\n"; 00089 return 1; 00090 } 00091 00092 // Tell SrcMgr about this buffer, which is what TGParser will pick up. 00093 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc()); 00094 00095 // Record the location of the include directory so that the lexer can find 00096 // it later. 00097 SrcMgr.setIncludeDirs(IncludeDirs); 00098 00099 TGParser Parser(SrcMgr, Records); 00100 00101 if (Parser.ParseFile()) 00102 return 1; 00103 00104 std::error_code EC; 00105 tool_output_file Out(OutputFilename, EC, sys::fs::F_Text); 00106 if (EC) { 00107 errs() << argv0 << ": error opening " << OutputFilename << ":" 00108 << EC.message() << "\n"; 00109 return 1; 00110 } 00111 if (!DependFilename.empty()) { 00112 if (int Ret = createDependencyFile(Parser, argv0)) 00113 return Ret; 00114 } 00115 00116 if (MainFn(Out.os(), Records)) 00117 return 1; 00118 00119 if (ErrorsPrinted > 0) { 00120 errs() << argv0 << ": " << ErrorsPrinted << " errors.\n"; 00121 return 1; 00122 } 00123 00124 // Declare success. 00125 Out.keep(); 00126 return 0; 00127 } 00128 00129 }