Skip to content

Commit

Permalink
Move TableGen's parser and entry point into a library
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pcc committed Oct 1, 2011
1 parent 2e6b97b commit 7c78888
Show file tree
Hide file tree
Showing 77 changed files with 369 additions and 241 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ endif()

# Put this before tblgen. Else we have a circular dependence.
add_subdirectory(lib/Support)
add_subdirectory(lib/TableGen)

set(LLVM_TABLEGEN "tblgen" CACHE
STRING "Native TableGen executable. Saves building one when cross-compiling.")
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
LEVEL := .

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

ifeq ($(BUILD_DIRS_ONLY),1)
DIRS := lib/Support utils
DIRS := lib/Support lib/TableGen utils
OPTIONAL_DIRS :=
else
DIRS := lib/Support utils lib/VMCore lib tools/llvm-shlib \
DIRS := lib/Support lib/TableGen utils lib/VMCore lib tools/llvm-shlib \
tools/llvm-config tools runtime docs unittests
OPTIONAL_DIRS := projects bindings
endif
Expand Down
6 changes: 3 additions & 3 deletions utils/TableGen/Error.h → include/llvm/TableGen/Error.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===- Error.h - tblgen error handling helper routines ----------*- C++ -*-===//
//===- llvm/TableGen/Error.h - tblgen error handling helpers ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef ERROR_H
#define ERROR_H
#ifndef LLVM_TABLEGEN_ERROR_H
#define LLVM_TABLEGEN_ERROR_H

#include "llvm/Support/SourceMgr.h"

Expand Down
26 changes: 26 additions & 0 deletions include/llvm/TableGen/Main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===- llvm/TableGen/Main.h - tblgen entry point ----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the common entry point for tblgen tools.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TABLEGEN_MAIN_H
#define LLVM_TABLEGEN_MAIN_H

namespace llvm {

class TableGenAction;

/// Run the table generator, performing the specified Action on parsed records.
int TableGenMain(char *argv0, TableGenAction &Action);

}

#endif
7 changes: 3 additions & 4 deletions utils/TableGen/Record.h → include/llvm/TableGen/Record.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

//===- Record.h - Classes to represent Table Records ------------*- C++ -*-===//
//===- llvm/TableGen/Record.h - Classes for Table Records -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -13,8 +12,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef RECORD_H
#define RECORD_H
#ifndef LLVM_TABLEGEN_RECORD_H
#define LLVM_TABLEGEN_RECORD_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FoldingSet.h"
Expand Down
34 changes: 34 additions & 0 deletions include/llvm/TableGen/TableGenAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===- llvm/TableGen/TableGenAction.h - defines TableGenAction --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the TableGenAction base class to be derived from by
// tblgen tools.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TABLEGEN_TABLEGENACTION_H
#define LLVM_TABLEGEN_TABLEGENACTION_H

namespace llvm {

class raw_ostream;
class RecordKeeper;

class TableGenAction {
public:
virtual ~TableGenAction() {}

/// Perform the action using Records, and write output to OS.
/// @returns true on error, false otherwise
virtual bool operator()(raw_ostream &OS, RecordKeeper &Records) = 0;
};

}

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===- TableGenBackend.h - Base class for TableGen Backends -----*- C++ -*-===//
//===- llvm/TableGen/TableGenBackend.h - Backend base class -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -12,8 +12,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef TABLEGENBACKEND_H
#define TABLEGENBACKEND_H
#ifndef LLVM_TABLEGEN_TABLEGENBACKEND_H
#define LLVM_TABLEGEN_TABLEGENBACKEND_H

#include "llvm/Support/raw_ostream.h"
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `Support' library is added on the top-level CMakeLists.txt
# `Support' and `TableGen' libraries are added on the top-level CMakeLists.txt

add_subdirectory(VMCore)
add_subdirectory(CodeGen)
Expand Down
16 changes: 16 additions & 0 deletions lib/TableGen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## FIXME: This only requires RTTI because tblgen uses it. Fix that.
set(LLVM_REQUIRES_RTTI 1)
set(LLVM_REQUIRES_EH 1)

add_llvm_library(LLVMTableGen
Error.cpp
Main.cpp
Record.cpp
TableGenBackend.cpp
TGLexer.cpp
TGParser.cpp
)

add_llvm_library_dependencies(LLVMTableGen
LLVMSupport
)
2 changes: 1 addition & 1 deletion utils/TableGen/Error.cpp → lib/TableGen/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//

#include "Error.h"
#include "llvm/TableGen/Error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/raw_ostream.h"

Expand Down
124 changes: 124 additions & 0 deletions lib/TableGen/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//===- Main.cpp - Top-Level TableGen implementation -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// TableGen is a tool which can be used to build up a description of something,
// then invoke one or more "tablegen backends" to emit information about the
// description in some predefined format. In practice, this is used by the LLVM
// code generators to automate generation of a code generator through a
// high-level description of the target.
//
//===----------------------------------------------------------------------===//

#include "TGParser.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/system_error.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenAction.h"
#include <algorithm>
#include <cstdio>
using namespace llvm;

namespace {
cl::opt<std::string>
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
cl::init("-"));

cl::opt<std::string>
DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"),
cl::init(""));

cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));

cl::list<std::string>
IncludeDirs("I", cl::desc("Directory of include files"),
cl::value_desc("directory"), cl::Prefix);
}

namespace llvm {

int TableGenMain(char *argv0, TableGenAction &Action) {
RecordKeeper Records;

try {
// Parse the input file.
OwningPtr<MemoryBuffer> File;
if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
errs() << "Could not open input file '" << InputFilename << "': "
<< ec.message() <<"\n";
return 1;
}
MemoryBuffer *F = File.take();

// Tell SrcMgr about this buffer, which is what TGParser will pick up.
SrcMgr.AddNewSourceBuffer(F, SMLoc());

// Record the location of the include directory so that the lexer can find
// it later.
SrcMgr.setIncludeDirs(IncludeDirs);

TGParser Parser(SrcMgr, Records);

if (Parser.ParseFile())
return 1;

std::string Error;
tool_output_file Out(OutputFilename.c_str(), Error);
if (!Error.empty()) {
errs() << argv0 << ": error opening " << OutputFilename
<< ":" << Error << "\n";
return 1;
}
if (!DependFilename.empty()) {
if (OutputFilename == "-") {
errs() << argv0 << ": the option -d must be used together with -o\n";
return 1;
}
tool_output_file DepOut(DependFilename.c_str(), Error);
if (!Error.empty()) {
errs() << argv0 << ": error opening " << DependFilename
<< ":" << Error << "\n";
return 1;
}
DepOut.os() << OutputFilename << ":";
const std::vector<std::string> &Dependencies = Parser.getDependencies();
for (std::vector<std::string>::const_iterator I = Dependencies.begin(),
E = Dependencies.end();
I != E; ++I) {
DepOut.os() << " " << (*I);
}
DepOut.os() << "\n";
DepOut.keep();
}

if (Action(Out.os(), Records))
return 1;

// Declare success.
Out.keep();
return 0;

} catch (const TGError &Error) {
PrintError(Error);
} catch (const std::string &Error) {
PrintError(Error);
} catch (const char *Error) {
PrintError(Error);
} catch (...) {
errs() << argv0 << ": Unknown unexpected exception occurred.\n";
}

return 1;
}

}
18 changes: 18 additions & 0 deletions lib/TableGen/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
##===- lib/TableGen/Makefile -------------------------------*- Makefile -*-===##
#
# The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##

LEVEL = ../..
LIBRARYNAME = LLVMTableGen
BUILD_ARCHIVE = 1

## FIXME: This only requires RTTI because tblgen uses it. Fix that.
REQUIRES_RTTI = 1
REQUIRES_EH = 1

include $(LEVEL)/Makefile.common
4 changes: 2 additions & 2 deletions utils/TableGen/Record.cpp → lib/TableGen/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

#include "Record.h"
#include "Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/Error.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
Expand Down
2 changes: 1 addition & 1 deletion utils/TableGen/TGLexer.cpp → lib/TableGen/TGLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//===----------------------------------------------------------------------===//

#include "TGLexer.h"
#include "Error.h"
#include "llvm/TableGen/Error.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Config/config.h"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion utils/TableGen/TGParser.cpp → lib/TableGen/TGParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//===----------------------------------------------------------------------===//

#include "TGParser.h"
#include "Record.h"
#include "llvm/TableGen/Record.h"
#include "llvm/ADT/StringExtras.h"
#include <algorithm>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion utils/TableGen/TGParser.h → lib/TableGen/TGParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define TGPARSER_H

#include "TGLexer.h"
#include "Error.h"
#include "llvm/TableGen/Error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/SourceMgr.h"
#include <map>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

#include "TableGenBackend.h"
#include "Record.h"
#include "llvm/TableGen/TableGenBackend.h"
#include "llvm/TableGen/Record.h"
using namespace llvm;

void TableGenBackend::EmitSourceFileHeader(const std::string &Desc,
Expand Down
2 changes: 1 addition & 1 deletion utils/TableGen/ARMDecoderEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "ARMDecoderEmitter.h"
#include "CodeGenTarget.h"
#include "Record.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"

#include <vector>
#include <map>
Expand Down
3 changes: 1 addition & 2 deletions utils/TableGen/ARMDecoderEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
#ifndef ARMDECODEREMITTER_H
#define ARMDECODEREMITTER_H

#include "TableGenBackend.h"

#include "llvm/Support/DataTypes.h"
#include "llvm/TableGen/TableGenBackend.h"

namespace llvm {

Expand Down
Loading

0 comments on commit 7c78888

Please sign in to comment.