blob: 278b567fb22099d305bc7f674f507dd737e2278d [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
Craig Topper1407b172015-05-26 06:48:46 +000018#include "llvm/TableGen/Main.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000019#include "TGParser.h"
Davide Italianoebd53502016-12-05 22:58:01 +000020#include "llvm/ADT/StringExtras.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000021#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000022#include "llvm/Support/FileSystem.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/ToolOutputFile.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000025#include "llvm/TableGen/Error.h"
26#include "llvm/TableGen/Record.h"
Peter Collingbourne84c287e2011-10-01 16:41:13 +000027#include <algorithm>
28#include <cstdio>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000029#include <system_error>
Peter Collingbourne84c287e2011-10-01 16:41:13 +000030using namespace llvm;
31
Craig Topperb7644fd2015-05-26 06:48:38 +000032static cl::opt<std::string>
33OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
34 cl::init("-"));
Peter Collingbourne84c287e2011-10-01 16:41:13 +000035
Craig Topperb7644fd2015-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 Collingbourne84c287e2011-10-01 16:41:13 +000041
Craig Topperb7644fd2015-05-26 06:48:38 +000042static cl::opt<std::string>
43InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
Peter Collingbourne84c287e2011-10-01 16:41:13 +000044
Craig Topperb7644fd2015-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 Collingbourne84c287e2011-10-01 16:41:13 +000048
Davide Italianoebd53502016-12-05 22:58:01 +000049static int reportError(const char *ProgName, Twine Msg) {
50 errs() << ProgName << ": " << Msg;
51 errs().flush();
52 return 1;
53}
54
Sean Silva0cd35362012-10-09 20:29:03 +000055/// \brief Create a dependency file for `-d` option.
56///
57/// This functionality is really only for the benefit of the build system.
58/// It is similar to GCC's `-M*` family of options.
Sean Silva2f7bf412012-10-09 20:39:28 +000059static int createDependencyFile(const TGParser &Parser, const char *argv0) {
Davide Italianoebd53502016-12-05 22:58:01 +000060 if (OutputFilename == "-")
61 return reportError(argv0, "the option -d must be used together with -o\n");
62
Rafael Espindola3fd1e992014-08-25 18:16:47 +000063 std::error_code EC;
64 tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
Davide Italianoebd53502016-12-05 22:58:01 +000065 if (EC)
66 return reportError(argv0, "error opening " + DependFilename + ":" +
67 EC.message() + "\n");
Sean Silva0cd35362012-10-09 20:29:03 +000068 DepOut.os() << OutputFilename << ":";
Craig Topper5fcc5ab2014-12-11 07:04:54 +000069 for (const auto &Dep : Parser.getDependencies()) {
70 DepOut.os() << ' ' << Dep.first;
Sean Silva0cd35362012-10-09 20:29:03 +000071 }
72 DepOut.os() << "\n";
73 DepOut.keep();
74 return 0;
75}
76
Craig Toppercd371212015-05-26 06:48:41 +000077int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
Peter Collingbourne84c287e2011-10-01 16:41:13 +000078 RecordKeeper Records;
79
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000080 // Parse the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +000081 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
82 MemoryBuffer::getFileOrSTDIN(InputFilename);
Davide Italianoebd53502016-12-05 22:58:01 +000083 if (std::error_code EC = FileOrErr.getError())
84 return reportError(argv0, "Could not open input file '" + InputFilename +
85 "': " + EC.message() + "\n");
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000086
87 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
David Blaikie1961f142014-08-21 20:44:56 +000088 SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
Joerg Sonnenberger635debe2012-10-25 20:33:17 +000089
90 // Record the location of the include directory so that the lexer can find
91 // it later.
92 SrcMgr.setIncludeDirs(IncludeDirs);
93
94 TGParser Parser(SrcMgr, Records);
95
96 if (Parser.ParseFile())
97 return 1;
98
Rafael Espindola3fd1e992014-08-25 18:16:47 +000099 std::error_code EC;
100 tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
Davide Italianoebd53502016-12-05 22:58:01 +0000101 if (EC)
102 return reportError(argv0, "error opening " + OutputFilename + ":" +
103 EC.message() + "\n");
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000104 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
Davide Italianoebd53502016-12-05 22:58:01 +0000112 if (ErrorsPrinted > 0)
113 return reportError(argv0, utostr(ErrorsPrinted) + " errors.\n");
Jakob Stoklund Olesena1214e72013-03-20 20:43:11 +0000114
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000115 // Declare success.
116 Out.keep();
117 return 0;
Peter Collingbourne84c287e2011-10-01 16:41:13 +0000118}