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 | |
Craig Topper | 1407b17 | 2015-05-26 06:48:46 +0000 | [diff] [blame^] | 18 | #include "llvm/TableGen/Main.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 19 | #include "TGParser.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/ToolOutputFile.h" |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 24 | #include "llvm/TableGen/Error.h" |
| 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 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 31 | static cl::opt<std::string> |
| 32 | OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), |
| 33 | cl::init("-")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 34 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 35 | static cl::opt<std::string> |
| 36 | DependFilename("d", |
| 37 | cl::desc("Dependency filename"), |
| 38 | cl::value_desc("filename"), |
| 39 | cl::init("")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 40 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 41 | static cl::opt<std::string> |
| 42 | InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 43 | |
Craig Topper | b7644fd | 2015-05-26 06:48:38 +0000 | [diff] [blame] | 44 | static cl::list<std::string> |
| 45 | IncludeDirs("I", cl::desc("Directory of include files"), |
| 46 | cl::value_desc("directory"), cl::Prefix); |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 47 | |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 48 | /// \brief Create a dependency file for `-d` option. |
| 49 | /// |
| 50 | /// This functionality is really only for the benefit of the build system. |
| 51 | /// It is similar to GCC's `-M*` family of options. |
Sean Silva | 2f7bf41 | 2012-10-09 20:39:28 +0000 | [diff] [blame] | 52 | static int createDependencyFile(const TGParser &Parser, const char *argv0) { |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 53 | if (OutputFilename == "-") { |
| 54 | errs() << argv0 << ": the option -d must be used together with -o\n"; |
| 55 | return 1; |
| 56 | } |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 57 | std::error_code EC; |
| 58 | tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text); |
| 59 | if (EC) { |
| 60 | errs() << argv0 << ": error opening " << DependFilename << ":" |
| 61 | << EC.message() << "\n"; |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | DepOut.os() << OutputFilename << ":"; |
Craig Topper | 5fcc5ab | 2014-12-11 07:04:54 +0000 | [diff] [blame] | 65 | for (const auto &Dep : Parser.getDependencies()) { |
| 66 | DepOut.os() << ' ' << Dep.first; |
Sean Silva | 0cd3536 | 2012-10-09 20:29:03 +0000 | [diff] [blame] | 67 | } |
| 68 | DepOut.os() << "\n"; |
| 69 | DepOut.keep(); |
| 70 | return 0; |
| 71 | } |
| 72 | |
Craig Topper | cd37121 | 2015-05-26 06:48:41 +0000 | [diff] [blame] | 73 | int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) { |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 74 | RecordKeeper Records; |
| 75 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 76 | // Parse the input file. |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 77 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 78 | MemoryBuffer::getFileOrSTDIN(InputFilename); |
| 79 | if (std::error_code EC = FileOrErr.getError()) { |
| 80 | errs() << "Could not open input file '" << InputFilename |
| 81 | << "': " << EC.message() << "\n"; |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 82 | return 1; |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 83 | } |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 84 | |
| 85 | // Tell SrcMgr about this buffer, which is what TGParser will pick up. |
David Blaikie | 1961f14 | 2014-08-21 20:44:56 +0000 | [diff] [blame] | 86 | SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc()); |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 87 | |
| 88 | // Record the location of the include directory so that the lexer can find |
| 89 | // it later. |
| 90 | SrcMgr.setIncludeDirs(IncludeDirs); |
| 91 | |
| 92 | TGParser Parser(SrcMgr, Records); |
| 93 | |
| 94 | if (Parser.ParseFile()) |
| 95 | return 1; |
| 96 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 97 | std::error_code EC; |
| 98 | tool_output_file Out(OutputFilename, EC, sys::fs::F_Text); |
| 99 | if (EC) { |
| 100 | errs() << argv0 << ": error opening " << OutputFilename << ":" |
| 101 | << EC.message() << "\n"; |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 102 | return 1; |
| 103 | } |
| 104 | if (!DependFilename.empty()) { |
| 105 | if (int Ret = createDependencyFile(Parser, argv0)) |
| 106 | return Ret; |
| 107 | } |
| 108 | |
| 109 | if (MainFn(Out.os(), Records)) |
| 110 | return 1; |
| 111 | |
Jakob Stoklund Olesen | a1214e7 | 2013-03-20 20:43:11 +0000 | [diff] [blame] | 112 | if (ErrorsPrinted > 0) { |
| 113 | errs() << argv0 << ": " << ErrorsPrinted << " errors.\n"; |
| 114 | return 1; |
| 115 | } |
| 116 | |
Joerg Sonnenberger | 635debe | 2012-10-25 20:33:17 +0000 | [diff] [blame] | 117 | // Declare success. |
| 118 | Out.keep(); |
| 119 | return 0; |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 120 | } |