blob: 02698416609f0f27cd9ee691b9fa05944f00160e [file] [log] [blame]
Peter Collingbourne7c788882011-10-01 16:41:13 +00001//===- 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 Toppercfacb302015-05-26 06:48:46 +000018#include "llvm/TableGen/Main.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000019#include "TGParser.h"
Davide Italianofb77f462016-12-05 22:58:01 +000020#include "llvm/ADT/StringExtras.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000021#include "llvm/Support/CommandLine.h"
Benjamin Kramer7259f142014-04-29 23:26:49 +000022#include "llvm/Support/FileSystem.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/ToolOutputFile.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000025#include "llvm/TableGen/Error.h"
26#include "llvm/TableGen/Record.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000027#include <algorithm>
28#include <cstdio>
Rafael Espindolad5132f92014-06-12 17:38:55 +000029#include <system_error>
Peter Collingbourne7c788882011-10-01 16:41:13 +000030using namespace llvm;
31
Craig Topperef92f292015-05-26 06:48:38 +000032static cl::opt<std::string>
33OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
34 cl::init("-"));
Peter Collingbourne7c788882011-10-01 16:41:13 +000035
Craig Topperef92f292015-05-26 06:48:38 +000036static cl::opt<std::string>
37DependFilename("d",
38 cl::desc("Dependency filename"),
39 cl::value_desc("filename"),
40 cl::init(""));
Peter Collingbourne7c788882011-10-01 16:41:13 +000041
Craig Topperef92f292015-05-26 06:48:38 +000042static cl::opt<std::string>
43InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
Peter Collingbourne7c788882011-10-01 16:41:13 +000044
Craig Topperef92f292015-05-26 06:48:38 +000045static cl::list<std::string>
46IncludeDirs("I", cl::desc("Directory of include files"),
47 cl::value_desc("directory"), cl::Prefix);
Peter Collingbourne7c788882011-10-01 16:41:13 +000048
Vyacheslav Zakharin6c99d2b2018-11-27 18:57:43 +000049static cl::list<std::string>
50MacroNames("D", cl::desc("Name of the macro to be defined"),
51 cl::value_desc("macro name"), cl::Prefix);
52
Davide Italianofb77f462016-12-05 22:58:01 +000053static int reportError(const char *ProgName, Twine Msg) {
54 errs() << ProgName << ": " << Msg;
55 errs().flush();
56 return 1;
57}
58
Adrian Prantl26b584c2018-05-01 15:54:18 +000059/// Create a dependency file for `-d` option.
Sean Silva88dbc5e2012-10-09 20:29:03 +000060///
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 Silva1ed3b422012-10-09 20:39:28 +000063static int createDependencyFile(const TGParser &Parser, const char *argv0) {
Davide Italianofb77f462016-12-05 22:58:01 +000064 if (OutputFilename == "-")
65 return reportError(argv0, "the option -d must be used together with -o\n");
66
Rafael Espindola8c968622014-08-25 18:16:47 +000067 std::error_code EC;
Reid Kleckner97ca9642017-09-23 01:03:17 +000068 ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text);
Davide Italianofb77f462016-12-05 22:58:01 +000069 if (EC)
70 return reportError(argv0, "error opening " + DependFilename + ":" +
71 EC.message() + "\n");
Sean Silva88dbc5e2012-10-09 20:29:03 +000072 DepOut.os() << OutputFilename << ":";
Craig Topper34905582014-12-11 07:04:54 +000073 for (const auto &Dep : Parser.getDependencies()) {
74 DepOut.os() << ' ' << Dep.first;
Sean Silva88dbc5e2012-10-09 20:29:03 +000075 }
76 DepOut.os() << "\n";
77 DepOut.keep();
78 return 0;
79}
80
Craig Toppera274efb2015-05-26 06:48:41 +000081int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
Peter Collingbourne7c788882011-10-01 16:41:13 +000082 RecordKeeper Records;
83
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000084 // Parse the input file.
Rafael Espindola7cba2a92014-07-06 17:43:13 +000085 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
86 MemoryBuffer::getFileOrSTDIN(InputFilename);
Davide Italianofb77f462016-12-05 22:58:01 +000087 if (std::error_code EC = FileOrErr.getError())
88 return reportError(argv0, "Could not open input file '" + InputFilename +
89 "': " + EC.message() + "\n");
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000090
91 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
David Blaikie95ca0fb2014-08-21 20:44:56 +000092 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000093
94 // Record the location of the include directory so that the lexer can find
95 // it later.
96 SrcMgr.setIncludeDirs(IncludeDirs);
97
Vyacheslav Zakharin6c99d2b2018-11-27 18:57:43 +000098 TGParser Parser(SrcMgr, MacroNames, Records);
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +000099
100 if (Parser.ParseFile())
101 return 1;
102
Nico Weber0bc70d32018-12-19 13:35:53 +0000103 // Write output to memory.
104 std::string OutString;
105 raw_string_ostream Out(OutString);
106 if (MainFn(Out, Records))
107 return 1;
108
109 // Always write the depfile, even if the main output hasn't changed.
110 // If it's missing, Ninja considers the output dirty. If this was below
111 // the early exit below and someone deleted the .inc.d file but not the .inc
112 // file, tablegen would never write the depfile.
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000113 if (!DependFilename.empty()) {
114 if (int Ret = createDependencyFile(Parser, argv0))
115 return Ret;
116 }
Chandler Carruth5d61ed92018-05-07 23:41:48 +0000117
Nico Weber0bc70d32018-12-19 13:35:53 +0000118 // Only updates the real output file if there are any differences.
119 // This prevents recompilation of all the files depending on it if there
120 // aren't any.
121 if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename))
122 if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
123 return 0;
124
125 std::error_code EC;
126 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::F_Text);
127 if (EC)
128 return reportError(argv0, "error opening " + OutputFilename + ":" +
129 EC.message() + "\n");
130 OutFile.os() << Out.str();
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000131
Davide Italianofb77f462016-12-05 22:58:01 +0000132 if (ErrorsPrinted > 0)
Benjamin Kramerca5092a2017-12-28 16:58:54 +0000133 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
Jakob Stoklund Olesenf8ea5a52013-03-20 20:43:11 +0000134
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000135 // Declare success.
Nico Weber0bc70d32018-12-19 13:35:53 +0000136 OutFile.keep();
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000137 return 0;
Peter Collingbourne7c788882011-10-01 16:41:13 +0000138}