Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 7c78888

Browse files
committed
Move TableGen's parser and entry point into a library
This is the first step towards splitting LLVM and Clang's tblgen executables. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140951 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2e6b97b commit 7c78888

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+369
-241
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ endif()
195195

196196
# Put this before tblgen. Else we have a circular dependence.
197197
add_subdirectory(lib/Support)
198+
add_subdirectory(lib/TableGen)
198199

199200
set(LLVM_TABLEGEN "tblgen" CACHE
200201
STRING "Native TableGen executable. Saves building one when cross-compiling.")

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
LEVEL := .
1111

1212
# Top-Level LLVM Build Stages:
13-
# 1. Build lib/Support, which is used by utils (tblgen).
13+
# 1. Build lib/Support and lib/TableGen, which are used by utils (tblgen).
1414
# 2. Build utils, which is used by VMCore.
1515
# 3. Build VMCore, which builds the Intrinsics.inc file used by libs.
1616
# 4. Build libs, which are needed by llvm-config.
@@ -27,10 +27,10 @@ LEVEL := .
2727
ifneq ($(findstring llvmCore, $(RC_ProjectName)),llvmCore) # Normal build (not "Apple-style").
2828

2929
ifeq ($(BUILD_DIRS_ONLY),1)
30-
DIRS := lib/Support utils
30+
DIRS := lib/Support lib/TableGen utils
3131
OPTIONAL_DIRS :=
3232
else
33-
DIRS := lib/Support utils lib/VMCore lib tools/llvm-shlib \
33+
DIRS := lib/Support lib/TableGen utils lib/VMCore lib tools/llvm-shlib \
3434
tools/llvm-config tools runtime docs unittests
3535
OPTIONAL_DIRS := projects bindings
3636
endif

utils/TableGen/Error.h include/llvm/TableGen/Error.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- Error.h - tblgen error handling helper routines ----------*- C++ -*-===//
1+
//===- llvm/TableGen/Error.h - tblgen error handling helpers ----*- C++ -*-===//
22
//
33
// The LLVM Compiler Infrastructure
44
//
@@ -12,8 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
#ifndef ERROR_H
16-
#define ERROR_H
15+
#ifndef LLVM_TABLEGEN_ERROR_H
16+
#define LLVM_TABLEGEN_ERROR_H
1717

1818
#include "llvm/Support/SourceMgr.h"
1919

include/llvm/TableGen/Main.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- llvm/TableGen/Main.h - tblgen entry point ----------------*- C++ -*-===//
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+
// This file declares the common entry point for tblgen tools.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TABLEGEN_MAIN_H
15+
#define LLVM_TABLEGEN_MAIN_H
16+
17+
namespace llvm {
18+
19+
class TableGenAction;
20+
21+
/// Run the table generator, performing the specified Action on parsed records.
22+
int TableGenMain(char *argv0, TableGenAction &Action);
23+
24+
}
25+
26+
#endif

utils/TableGen/Record.h include/llvm/TableGen/Record.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
//===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
1+
//===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
32
//
43
// The LLVM Compiler Infrastructure
54
//
@@ -13,8 +12,8 @@
1312
//
1413
//===----------------------------------------------------------------------===//
1514

16-
#ifndef RECORD_H
17-
#define RECORD_H
15+
#ifndef LLVM_TABLEGEN_RECORD_H
16+
#define LLVM_TABLEGEN_RECORD_H
1817

1918
#include "llvm/ADT/ArrayRef.h"
2019
#include "llvm/ADT/FoldingSet.h"
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===- llvm/TableGen/TableGenAction.h - defines TableGenAction --*- C++ -*-===//
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+
// This file defines the TableGenAction base class to be derived from by
11+
// tblgen tools.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_TABLEGEN_TABLEGENACTION_H
16+
#define LLVM_TABLEGEN_TABLEGENACTION_H
17+
18+
namespace llvm {
19+
20+
class raw_ostream;
21+
class RecordKeeper;
22+
23+
class TableGenAction {
24+
public:
25+
virtual ~TableGenAction() {}
26+
27+
/// Perform the action using Records, and write output to OS.
28+
/// @returns true on error, false otherwise
29+
virtual bool operator()(raw_ostream &OS, RecordKeeper &Records) = 0;
30+
};
31+
32+
}
33+
34+
#endif

utils/TableGen/TableGenBackend.h include/llvm/TableGen/TableGenBackend.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- TableGenBackend.h - Base class for TableGen Backends -----*- C++ -*-===//
1+
//===- llvm/TableGen/TableGenBackend.h - Backend base class -----*- C++ -*-===//
22
//
33
// The LLVM Compiler Infrastructure
44
//
@@ -12,8 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
#ifndef TABLEGENBACKEND_H
16-
#define TABLEGENBACKEND_H
15+
#ifndef LLVM_TABLEGEN_TABLEGENBACKEND_H
16+
#define LLVM_TABLEGEN_TABLEGENBACKEND_H
1717

1818
#include "llvm/Support/raw_ostream.h"
1919
#include <string>

lib/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `Support' library is added on the top-level CMakeLists.txt
1+
# `Support' and `TableGen' libraries are added on the top-level CMakeLists.txt
22

33
add_subdirectory(VMCore)
44
add_subdirectory(CodeGen)

lib/TableGen/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## FIXME: This only requires RTTI because tblgen uses it. Fix that.
2+
set(LLVM_REQUIRES_RTTI 1)
3+
set(LLVM_REQUIRES_EH 1)
4+
5+
add_llvm_library(LLVMTableGen
6+
Error.cpp
7+
Main.cpp
8+
Record.cpp
9+
TableGenBackend.cpp
10+
TGLexer.cpp
11+
TGParser.cpp
12+
)
13+
14+
add_llvm_library_dependencies(LLVMTableGen
15+
LLVMSupport
16+
)

utils/TableGen/Error.cpp lib/TableGen/Error.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15-
#include "Error.h"
15+
#include "llvm/TableGen/Error.h"
1616
#include "llvm/ADT/Twine.h"
1717
#include "llvm/Support/raw_ostream.h"
1818

lib/TableGen/Main.cpp

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//===- 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"
25+
#include "llvm/TableGen/Record.h"
26+
#include "llvm/TableGen/TableGenAction.h"
27+
#include <algorithm>
28+
#include <cstdio>
29+
using namespace llvm;
30+
31+
namespace {
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>
37+
DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"),
38+
cl::init(""));
39+
40+
cl::opt<std::string>
41+
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
42+
43+
cl::list<std::string>
44+
IncludeDirs("I", cl::desc("Directory of include files"),
45+
cl::value_desc("directory"), cl::Prefix);
46+
}
47+
48+
namespace llvm {
49+
50+
int TableGenMain(char *argv0, TableGenAction &Action) {
51+
RecordKeeper Records;
52+
53+
try {
54+
// Parse the input file.
55+
OwningPtr<MemoryBuffer> File;
56+
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
57+
errs() << "Could not open input file '" << InputFilename << "': "
58+
<< ec.message() <<"\n";
59+
return 1;
60+
}
61+
MemoryBuffer *F = File.take();
62+
63+
// Tell SrcMgr about this buffer, which is what TGParser will pick up.
64+
SrcMgr.AddNewSourceBuffer(F, SMLoc());
65+
66+
// Record the location of the include directory so that the lexer can find
67+
// it later.
68+
SrcMgr.setIncludeDirs(IncludeDirs);
69+
70+
TGParser Parser(SrcMgr, Records);
71+
72+
if (Parser.ParseFile())
73+
return 1;
74+
75+
std::string Error;
76+
tool_output_file Out(OutputFilename.c_str(), Error);
77+
if (!Error.empty()) {
78+
errs() << argv0 << ": error opening " << OutputFilename
79+
<< ":" << Error << "\n";
80+
return 1;
81+
}
82+
if (!DependFilename.empty()) {
83+
if (OutputFilename == "-") {
84+
errs() << argv0 << ": the option -d must be used together with -o\n";
85+
return 1;
86+
}
87+
tool_output_file DepOut(DependFilename.c_str(), Error);
88+
if (!Error.empty()) {
89+
errs() << argv0 << ": error opening " << DependFilename
90+
<< ":" << Error << "\n";
91+
return 1;
92+
}
93+
DepOut.os() << OutputFilename << ":";
94+
const std::vector<std::string> &Dependencies = Parser.getDependencies();
95+
for (std::vector<std::string>::const_iterator I = Dependencies.begin(),
96+
E = Dependencies.end();
97+
I != E; ++I) {
98+
DepOut.os() << " " << (*I);
99+
}
100+
DepOut.os() << "\n";
101+
DepOut.keep();
102+
}
103+
104+
if (Action(Out.os(), Records))
105+
return 1;
106+
107+
// Declare success.
108+
Out.keep();
109+
return 0;
110+
111+
} catch (const TGError &Error) {
112+
PrintError(Error);
113+
} catch (const std::string &Error) {
114+
PrintError(Error);
115+
} catch (const char *Error) {
116+
PrintError(Error);
117+
} catch (...) {
118+
errs() << argv0 << ": Unknown unexpected exception occurred.\n";
119+
}
120+
121+
return 1;
122+
}
123+
124+
}

lib/TableGen/Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##===- lib/TableGen/Makefile -------------------------------*- Makefile -*-===##
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+
LEVEL = ../..
11+
LIBRARYNAME = LLVMTableGen
12+
BUILD_ARCHIVE = 1
13+
14+
## FIXME: This only requires RTTI because tblgen uses it. Fix that.
15+
REQUIRES_RTTI = 1
16+
REQUIRES_EH = 1
17+
18+
include $(LEVEL)/Makefile.common

utils/TableGen/Record.cpp lib/TableGen/Record.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "Record.h"
15-
#include "Error.h"
14+
#include "llvm/TableGen/Record.h"
15+
#include "llvm/TableGen/Error.h"
1616
#include "llvm/Support/DataTypes.h"
1717
#include "llvm/Support/ErrorHandling.h"
1818
#include "llvm/Support/Format.h"

utils/TableGen/TGLexer.cpp lib/TableGen/TGLexer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "TGLexer.h"
15-
#include "Error.h"
15+
#include "llvm/TableGen/Error.h"
1616
#include "llvm/Support/SourceMgr.h"
1717
#include "llvm/Support/MemoryBuffer.h"
1818
#include "llvm/Config/config.h"
File renamed without changes.

utils/TableGen/TGParser.cpp lib/TableGen/TGParser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "TGParser.h"
15-
#include "Record.h"
15+
#include "llvm/TableGen/Record.h"
1616
#include "llvm/ADT/StringExtras.h"
1717
#include <algorithm>
1818
#include <sstream>

utils/TableGen/TGParser.h lib/TableGen/TGParser.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define TGPARSER_H
1616

1717
#include "TGLexer.h"
18-
#include "Error.h"
18+
#include "llvm/TableGen/Error.h"
1919
#include "llvm/ADT/Twine.h"
2020
#include "llvm/Support/SourceMgr.h"
2121
#include <map>

utils/TableGen/TableGenBackend.cpp lib/TableGen/TableGenBackend.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "TableGenBackend.h"
15-
#include "Record.h"
14+
#include "llvm/TableGen/TableGenBackend.h"
15+
#include "llvm/TableGen/Record.h"
1616
using namespace llvm;
1717

1818
void TableGenBackend::EmitSourceFileHeader(const std::string &Desc,

utils/TableGen/ARMDecoderEmitter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
#include "ARMDecoderEmitter.h"
2020
#include "CodeGenTarget.h"
21-
#include "Record.h"
2221
#include "llvm/ADT/StringExtras.h"
2322
#include "llvm/Support/Debug.h"
2423
#include "llvm/Support/raw_ostream.h"
24+
#include "llvm/TableGen/Record.h"
2525

2626
#include <vector>
2727
#include <map>

utils/TableGen/ARMDecoderEmitter.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
#ifndef ARMDECODEREMITTER_H
1616
#define ARMDECODEREMITTER_H
1717

18-
#include "TableGenBackend.h"
19-
2018
#include "llvm/Support/DataTypes.h"
19+
#include "llvm/TableGen/TableGenBackend.h"
2120

2221
namespace llvm {
2322

0 commit comments

Comments
 (0)