blob: bcd39584e450ec71f3e87e76c0853ac3b765fc88 [file] [log] [blame]
Peter Collingbourne84c287e2011-10-01 16:41:13 +00001//===- Main.cpp - Top-Level TableGen implementation -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Peter Collingbourne84c287e2011-10-01 16:41:13 +00006//
7//===----------------------------------------------------------------------===//
8//
9// TableGen is a tool which can be used to build up a description of something,
10// then invoke one or more "tablegen backends" to emit information about the
11// description in some predefined format. In practice, this is used by the LLVM
12// code generators to automate generation of a code generator through a
13// high-level description of the target.
14//
15//===----------------------------------------------------------------------===//
16
Craig Topper1407b172015-05-26 06:48:46 +000017#include "llvm/TableGen/Main.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000018#include "TGParser.h"
Davide Italianoebd53502016-12-05 22:58:01 +000019#include "llvm/ADT/StringExtras.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000020#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000021#include "llvm/Support/FileSystem.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000022#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/ToolOutputFile.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000024#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000026#include <algorithm>
27#include <cstdio>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000028#include <system_error>
Peter Collingbourne84c287e2011-10-01 16:41:13 +000029using namespace llvm;
30
Craig Topperb7644fd2015-05-26 06:48:38 +000031static cl::opt<std::string>
32OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
33 cl::init("-"));
Peter Collingbourne84c287e2011-10-01 16:41:13 +000034
Craig Topperb7644fd2015-05-26 06:48:38 +000035static cl::opt<std::string>
36DependFilename("d",
37 cl::desc("Dependency filename"),
38 cl::value_desc("filename"),
39 cl::init(""));
Peter Collingbourne84c287e2011-10-01 16:41:13 +000040
Craig Topperb7644fd2015-05-26 06:48:38 +000041static cl::opt<std::string>
42InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
Peter Collingbourne84c287e2011-10-01 16:41:13 +000043
Craig Topperb7644fd2015-05-26 06:48:38 +000044static cl::list<std::string>
45IncludeDirs("I", cl::desc("Directory of include files"),
46 cl::value_desc("directory"), cl::Prefix);
Peter Collingbourne84c287e2011-10-01 16:41:13 +000047
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000048static cl::list<std::string>
49MacroNames("D", cl::desc("Name of the macro to be defined"),
50 cl::value_desc("macro name"), cl::Prefix);
51
Davide Italianoebd53502016-12-05 22:58:01 +000052static int reportError(const char *ProgName, Twine Msg) {
53 errs() << ProgName << ": " << Msg;
54 errs().flush();
55 return 1;
56}
57
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000058/// Create a dependency file for `-d` option.
Sean Silva0cd35362012-10-09 20:29:03 +000059///
60/// This functionality is really only for the benefit of the build system.
61/// It is similar to GCC's `-M*` family of options.
Sean Silva2f7bf412012-10-09 20:39:28 +000062static int createDependencyFile(const TGParser &Parser, const char *argv0) {
Davide Italianoebd53502016-12-05 22:58:01 +000063 if (OutputFilename == "-")
64 return reportError(argv0, "the option -d must be used together with -o\n");
65
Rafael Espindola3fd1e992014-08-25 18:16:47 +000066 std::error_code EC;
Reid Kleckner3fc649c2017-09-23 01:03:17 +000067 ToolOutputFile DepOut(DependFilename, EC, sys::fs::F_Text);
Davide Italianoebd53502016-12-05 22:58:01 +000068 if (EC)
69 return reportError(argv0, "error opening " + DependFilename + ":" +
70 EC.message() + "\n");
Sean Silva0cd35362012-10-09 20:29:03 +000071 DepOut.os() << OutputFilename << ":";
Craig Topper5fcc5ab2014-12-11 07:04:54 +000072 for (const auto &Dep : Parser.getDependencies()) {
73 DepOut.os() << ' ' << Dep.first;
Sean Silva0cd35362012-10-09 20:29:03 +000074 }
75 DepOut.os() << "\n";
76 DepOut.keep();
77 return 0;
78}
79
Craig Toppercd371212015-05-26 06:48:41 +000080int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
Peter Collingbourne84c287e2011-10-01 16:41:13 +000081 RecordKeeper Records;
82
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000083 // Parse the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +000084 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
85 MemoryBuffer::getFileOrSTDIN(InputFilename);
Davide Italianoebd53502016-12-05 22:58:01 +000086 if (std::error_code EC = FileOrErr.getError())
87 return reportError(argv0, "Could not open input file '" + InputFilename +
88 "': " + EC.message() + "\n");
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000089
90 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
David Blaikie1961f142014-08-21 20:44:56 +000091 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000092
93 // Record the location of the include directory so that the lexer can find
94 // it later.
95 SrcMgr.setIncludeDirs(IncludeDirs);
96
Vyacheslav Zakharinf7d079e2018-11-27 18:57:43 +000097 TGParser Parser(SrcMgr, MacroNames, Records);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000098
99 if (Parser.ParseFile())
100 return 1;
101
Nico Weberf7cf1a12018-12-19 13:35:53 +0000102 // Write output to memory.
103 std::string OutString;
104 raw_string_ostream Out(OutString);
105 if (MainFn(Out, Records))
106 return 1;
107
108 // Always write the depfile, even if the main output hasn't changed.
109 // If it's missing, Ninja considers the output dirty. If this was below
110 // the early exit below and someone deleted the .inc.d file but not the .inc
111 // file, tablegen would never write the depfile.
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000112 if (!DependFilename.empty()) {
113 if (int Ret = createDependencyFile(Parser, argv0))
114 return Ret;
115 }
Chandler Carruth029cea92018-05-07 23:41:48 +0000116
Nico Weberf7cf1a12018-12-19 13:35:53 +0000117 // Only updates the real output file if there are any differences.
118 // This prevents recompilation of all the files depending on it if there
119 // aren't any.
120 if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename))
121 if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
122 return 0;
123
124 std::error_code EC;
125 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::F_Text);
126 if (EC)
127 return reportError(argv0, "error opening " + OutputFilename + ":" +
128 EC.message() + "\n");
129 OutFile.os() << Out.str();
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000130
Davide Italianoebd53502016-12-05 22:58:01 +0000131 if (ErrorsPrinted > 0)
Benjamin Kramer3a13ed62017-12-28 16:58:54 +0000132 return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
Jakob Stoklund Olesena1214e72013-03-20 20:43:11 +0000133
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000134 // Declare success.
Nico Weberf7cf1a12018-12-19 13:35:53 +0000135 OutFile.keep();
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000136 return 0;
Peter Collingbourne84c287e2011-10-01 16:41:13 +0000137}