Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 1 | //===- Main.cpp - Top-Level TableGen implementation -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // TableGen is a tool which can be used to build up a description of something, |
| 11 | // then invoke one or more "tablegen backends" to emit information about the |
| 12 | // description in some predefined format. In practice, this is used by the LLVM |
| 13 | // code generators to automate generation of a code generator through a |
| 14 | // high-level description of the target. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "TGParser.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/ToolOutputFile.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 23 | #include "llvm/TableGen/Error.h" |
Sean Silva | 8c6c7de | 2012-10-03 21:29:18 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/Main.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 25 | #include "llvm/TableGen/Record.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <cstdio> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 28 | #include <system_error> |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | cl::opt<std::string> |
| 33 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 34 | cl::init("-")); |
| 35 | |
| 36 | cl::opt<std::string> |
Michael J. Spencer | 5c50281 | 2012-06-01 00:58:41 +0000 | [diff] [blame] | 37 | DependFilename("d", |
| 38 | cl::desc("Dependency filename"), |
| 39 | cl::value_desc("filename"), |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 40 | cl::init("")); |
| 41 | |
| 42 | cl::opt<std::string> |
| 43 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
| 44 | |
| 45 | cl::list<std::string> |
| 46 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 47 | cl::value_desc("directory"), cl::Prefix); |
| 48 | } |
| 49 | |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 50 | /// \brief Create a dependency file for `-d` option. |
| 51 | /// |
| 52 | /// This functionality is really only for the benefit of the build system. |
| 53 | /// It is similar to GCC's `-M*` family of options. |
Sean Silva | 2f7bf41 | 2012-10-09 20:39:28 +0000 | [diff] [blame] | 54 | static int createDependencyFile(const TGParser &Parser, const char *argv0) { |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 55 | if (OutputFilename == "-") { |
| 56 | errs() << argv0 << ": the option -d must be used together with -o\n"; |
| 57 | return 1; |
| 58 | } |
| 59 | std::string Error; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 60 | tool_output_file DepOut(DependFilename.c_str(), Error, sys::fs::F_Text); |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 61 | if (!Error.empty()) { |
| 62 | errs() << argv0 << ": error opening " << DependFilename |
| 63 | << ":" << Error << "\n"; |
| 64 | return 1; |
| 65 | } |
| 66 | DepOut.os() << OutputFilename << ":"; |
Sean Silva | 3b96424 | 2013-02-07 04:30:39 +0000 | [diff] [blame] | 67 | const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies(); |
| 68 | for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(), |
| 69 | E = Dependencies.end(); |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 70 | I != E; ++I) { |
Sean Silva | 3b96424 | 2013-02-07 04:30:39 +0000 | [diff] [blame] | 71 | DepOut.os() << " " << I->first; |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 72 | } |
| 73 | DepOut.os() << "\n"; |
| 74 | DepOut.keep(); |
| 75 | return 0; |
| 76 | } |
| 77 | |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 78 | namespace llvm { |
| 79 | |
Sean Silva | 8c6c7de | 2012-10-03 21:29:18 +0000 | [diff] [blame] | 80 | int TableGenMain(char *argv0, TableGenMainFn *MainFn) { |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 81 | RecordKeeper Records; |
| 82 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 83 | // Parse the input file. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 84 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 85 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 86 | if (std::error_code EC = FileOrErr.getError()) { |
| 87 | errs() << "Could not open input file '" << InputFilename |
| 88 | << "': " << EC.message() << "\n"; |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 89 | return 1; |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 90 | } |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame^] | 91 | MemoryBuffer *F = FileOrErr.get().release(); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 92 | |
| 93 | // Tell SrcMgr about this buffer, which is what TGParser will pick up. |
| 94 | SrcMgr.AddNewSourceBuffer(F, SMLoc()); |
| 95 | |
| 96 | // Record the location of the include directory so that the lexer can find |
| 97 | // it later. |
| 98 | SrcMgr.setIncludeDirs(IncludeDirs); |
| 99 | |
| 100 | TGParser Parser(SrcMgr, Records); |
| 101 | |
| 102 | if (Parser.ParseFile()) |
| 103 | return 1; |
| 104 | |
| 105 | std::string Error; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 106 | tool_output_file Out(OutputFilename.c_str(), Error, sys::fs::F_Text); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 107 | if (!Error.empty()) { |
| 108 | errs() << argv0 << ": error opening " << OutputFilename |
| 109 | << ":" << Error << "\n"; |
| 110 | return 1; |
| 111 | } |
| 112 | if (!DependFilename.empty()) { |
| 113 | if (int Ret = createDependencyFile(Parser, argv0)) |
| 114 | return Ret; |
| 115 | } |
| 116 | |
| 117 | if (MainFn(Out.os(), Records)) |
| 118 | return 1; |
| 119 | |
Jakob Stoklund Olesen | a1214e7 | 2013-03-20 20:43:11 +0000 | [diff] [blame] | 120 | if (ErrorsPrinted > 0) { |
| 121 | errs() << argv0 << ": " << ErrorsPrinted << " errors.\n"; |
| 122 | return 1; |
| 123 | } |
| 124 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 125 | // Declare success. |
| 126 | Out.keep(); |
| 127 | return 0; |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } |