blob: 17556a2377f244957f757029eb918c88d6b19349 [file] [log] [blame]
Peter Collingbourne84c287e2011-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
18#include "TGParser.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000019#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000020#include "llvm/Support/FileSystem.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000021#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/ToolOutputFile.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000023#include "llvm/TableGen/Error.h"
Sean Silva8c6c7de2012-10-03 21:29:18 +000024#include "llvm/TableGen/Main.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000025#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
Sean Silva0cd35362012-10-09 20:29:03 +000048/// \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 Silva2f7bf412012-10-09 20:39:28 +000052static int createDependencyFile(const TGParser &Parser, const char *argv0) {
Sean Silva0cd35362012-10-09 20:29:03 +000053 if (OutputFilename == "-") {
54 errs() << argv0 << ": the option -d must be used together with -o\n";
55 return 1;
56 }
Rafael Espindola3fd1e992014-08-25 18:16:47 +000057 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 Silva0cd35362012-10-09 20:29:03 +000062 return 1;
63 }
64 DepOut.os() << OutputFilename << ":";
Craig Topper5fcc5ab2014-12-11 07:04:54 +000065 for (const auto &Dep : Parser.getDependencies()) {
66 DepOut.os() << ' ' << Dep.first;
Sean Silva0cd35362012-10-09 20:29:03 +000067 }
68 DepOut.os() << "\n";
69 DepOut.keep();
70 return 0;
71}
72
Craig Toppercd371212015-05-26 06:48:41 +000073int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
Peter Collingbourne84c287e2011-10-01 16:41:13 +000074 RecordKeeper Records;
75
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000076 // Parse the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +000077 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 Sonnenberger635debe2012-10-25 20:33:17 +000082 return 1;
Peter Collingbourne84c287e2011-10-01 16:41:13 +000083 }
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000084
85 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
David Blaikie1961f142014-08-21 20:44:56 +000086 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000087
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 Espindola3fd1e992014-08-25 18:16:47 +000097 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 Sonnenberger635debe2012-10-25 20:33:17 +0000102 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 Olesena1214e72013-03-20 20:43:11 +0000112 if (ErrorsPrinted > 0) {
113 errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
114 return 1;
115 }
116
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000117 // Declare success.
118 Out.keep();
119 return 0;
Peter Collingbourne84c287e2011-10-01 16:41:13 +0000120}