blob: d87d175502357098af63f82ad65e1db068e97236 [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
18#include "TGParser.h"
19#include "llvm/ADT/OwningPtr.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/ToolOutputFile.h"
23#include "llvm/Support/system_error.h"
24#include "llvm/TableGen/Error.h"
Sean Silvaf42a6742012-10-03 21:29:18 +000025#include "llvm/TableGen/Main.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000026#include "llvm/TableGen/Record.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000027#include <algorithm>
28#include <cstdio>
29using namespace llvm;
30
31namespace {
32 cl::opt<std::string>
33 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
34 cl::init("-"));
35
36 cl::opt<std::string>
Michael J. Spencercdd6b2d2012-06-01 00:58:41 +000037 DependFilename("d",
38 cl::desc("Dependency filename"),
39 cl::value_desc("filename"),
Peter Collingbourne7c788882011-10-01 16:41:13 +000040 cl::init(""));
41
42 cl::opt<std::string>
43 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
44
45 cl::list<std::string>
46 IncludeDirs("I", cl::desc("Directory of include files"),
47 cl::value_desc("directory"), cl::Prefix);
48}
49
Sean Silva88dbc5e2012-10-09 20:29:03 +000050/// \brief Create a dependency file for `-d` option.
51///
52/// This functionality is really only for the benefit of the build system.
53/// It is similar to GCC's `-M*` family of options.
Sean Silva1ed3b422012-10-09 20:39:28 +000054static int createDependencyFile(const TGParser &Parser, const char *argv0) {
Sean Silva88dbc5e2012-10-09 20:29:03 +000055 if (OutputFilename == "-") {
56 errs() << argv0 << ": the option -d must be used together with -o\n";
57 return 1;
58 }
59 std::string Error;
60 tool_output_file DepOut(DependFilename.c_str(), Error);
61 if (!Error.empty()) {
62 errs() << argv0 << ": error opening " << DependFilename
63 << ":" << Error << "\n";
64 return 1;
65 }
66 DepOut.os() << OutputFilename << ":";
67 const std::vector<std::string> &Dependencies = Parser.getDependencies();
68 for (std::vector<std::string>::const_iterator I = Dependencies.begin(),
69 E = Dependencies.end();
70 I != E; ++I) {
71 DepOut.os() << " " << (*I);
72 }
73 DepOut.os() << "\n";
74 DepOut.keep();
75 return 0;
76}
77
Peter Collingbourne7c788882011-10-01 16:41:13 +000078namespace llvm {
79
Sean Silvaf42a6742012-10-03 21:29:18 +000080int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
Peter Collingbourne7c788882011-10-01 16:41:13 +000081 RecordKeeper Records;
82
83 try {
84 // Parse the input file.
85 OwningPtr<MemoryBuffer> File;
Michael J. Spencercdd6b2d2012-06-01 00:58:41 +000086 if (error_code ec =
87 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
Peter Collingbourne7c788882011-10-01 16:41:13 +000088 errs() << "Could not open input file '" << InputFilename << "': "
89 << ec.message() <<"\n";
90 return 1;
91 }
92 MemoryBuffer *F = File.take();
93
94 // Tell SrcMgr about this buffer, which is what TGParser will pick up.
95 SrcMgr.AddNewSourceBuffer(F, SMLoc());
96
97 // Record the location of the include directory so that the lexer can find
98 // it later.
99 SrcMgr.setIncludeDirs(IncludeDirs);
100
Peter Collingbourne7c788882011-10-01 16:41:13 +0000101 TGParser Parser(SrcMgr, Records);
102
103 if (Parser.ParseFile())
104 return 1;
105
106 std::string Error;
107 tool_output_file Out(OutputFilename.c_str(), Error);
108 if (!Error.empty()) {
109 errs() << argv0 << ": error opening " << OutputFilename
110 << ":" << Error << "\n";
111 return 1;
112 }
Sean Silva88dbc5e2012-10-09 20:29:03 +0000113 if (!DependFilename.empty())
Sean Silva1ed3b422012-10-09 20:39:28 +0000114 if (int Ret = createDependencyFile(Parser, argv0))
Sean Silva88dbc5e2012-10-09 20:29:03 +0000115 return Ret;
Peter Collingbourne7c788882011-10-01 16:41:13 +0000116
Sean Silva3c096282012-10-03 21:31:08 +0000117 if (MainFn(Out.os(), Records))
Peter Collingbourne7c788882011-10-01 16:41:13 +0000118 return 1;
119
120 // Declare success.
121 Out.keep();
122 return 0;
123
124 } catch (const TGError &Error) {
125 PrintError(Error);
126 } catch (const std::string &Error) {
127 PrintError(Error);
128 } catch (const char *Error) {
129 PrintError(Error);
130 } catch (...) {
131 errs() << argv0 << ": Unknown unexpected exception occurred.\n";
132 }
133
134 return 1;
135}
136
137}