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