diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2011-10-01 16:41:13 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2011-10-01 16:41:13 +0000 |
commit | 7c788888872233748da10a8177a9a1eb176c1bc8 (patch) | |
tree | 2a813c66793364aeb39020c96c9510bb1c4f9cee | |
parent | 2e6b97bbf86d0825a060e190189fae7f884c79c9 (diff) |
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
77 files changed, 369 insertions, 241 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index e767ce0d24..874f381a21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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.") @@ -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. @@ -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 diff --git a/utils/TableGen/Error.h b/include/llvm/TableGen/Error.h index b3a0146194..c01b32b1c2 100644 --- a/utils/TableGen/Error.h +++ b/include/llvm/TableGen/Error.h @@ -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 // @@ -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" diff --git a/include/llvm/TableGen/Main.h b/include/llvm/TableGen/Main.h new file mode 100644 index 0000000000..deaef4a990 --- /dev/null +++ b/include/llvm/TableGen/Main.h @@ -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 diff --git a/utils/TableGen/Record.h b/include/llvm/TableGen/Record.h index 84313e66d5..afce760998 100644 --- a/utils/TableGen/Record.h +++ b/include/llvm/TableGen/Record.h @@ -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 // @@ -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" diff --git a/include/llvm/TableGen/TableGenAction.h b/include/llvm/TableGen/TableGenAction.h new file mode 100644 index 0000000000..9f1c23c5b4 --- /dev/null +++ b/include/llvm/TableGen/TableGenAction.h @@ -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 diff --git a/utils/TableGen/TableGenBackend.h b/include/llvm/TableGen/TableGenBackend.h index 9c2b948b0d..853f92e406 100644 --- a/utils/TableGen/TableGenBackend.h +++ b/include/llvm/TableGen/TableGenBackend.h @@ -1,4 +1,4 @@ -//===- TableGenBackend.h - Base class for TableGen Backends -----*- C++ -*-===// +//===- llvm/TableGen/TableGenBackend.h - Backend base class -----*- C++ -*-===// // // The LLVM Compiler Infrastructure // @@ -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> diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index d8a9b7365b..fb63c63f32 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -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) diff --git a/lib/TableGen/CMakeLists.txt b/lib/TableGen/CMakeLists.txt new file mode 100644 index 0000000000..0db4134691 --- /dev/null +++ b/lib/TableGen/CMakeLists.txt @@ -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 + ) diff --git a/utils/TableGen/Error.cpp b/lib/TableGen/Error.cpp index 3f6cda8977..5b2cbbfec4 100644 --- a/utils/TableGen/Error.cpp +++ b/lib/TableGen/Error.cpp @@ -12,7 +12,7 @@ // //===----------------------------------------------------------------------===// -#include "Error.h" +#include "llvm/TableGen/Error.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/raw_ostream.h" diff --git a/lib/TableGen/Main.cpp b/lib/TableGen/Main.cpp new file mode 100644 index 0000000000..01bc55e989 --- /dev/null +++ b/lib/TableGen/Main.cpp @@ -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; +} + +} diff --git a/lib/TableGen/Makefile b/lib/TableGen/Makefile new file mode 100644 index 0000000000..44724389e1 --- /dev/null +++ b/lib/TableGen/Makefile @@ -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 diff --git a/utils/TableGen/Record.cpp b/lib/TableGen/Record.cpp index 3d42a5233c..b4277973b5 100644 --- a/utils/TableGen/Record.cpp +++ b/lib/TableGen/Record.cpp @@ -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" diff --git a/utils/TableGen/TGLexer.cpp b/lib/TableGen/TGLexer.cpp index b4b90ff64e..0dc1c70136 100644 --- a/utils/TableGen/TGLexer.cpp +++ b/lib/TableGen/TGLexer.cpp @@ -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" diff --git a/utils/TableGen/TGLexer.h b/lib/TableGen/TGLexer.h index 84d328b12d..84d328b12d 100644 --- a/utils/TableGen/TGLexer.h +++ b/lib/TableGen/TGLexer.h diff --git a/utils/TableGen/TGParser.cpp b/lib/TableGen/TGParser.cpp index f734b984e1..1601a530b1 100644 --- a/utils/TableGen/TGParser.cpp +++ b/lib/TableGen/TGParser.cpp @@ -12,7 +12,7 @@ //===----------------------------------------------------------------------===// #include "TGParser.h" -#include "Record.h" +#include "llvm/TableGen/Record.h" #include "llvm/ADT/StringExtras.h" #include <algorithm> #include <sstream> diff --git a/utils/TableGen/TGParser.h b/lib/TableGen/TGParser.h index 8b56b8a329..b408c80b7a 100644 --- a/utils/TableGen/TGParser.h +++ b/lib/TableGen/TGParser.h @@ -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> diff --git a/utils/TableGen/TableGenBackend.cpp b/lib/TableGen/TableGenBackend.cpp index b3e33b5f9c..29588db324 100644 --- a/utils/TableGen/TableGenBackend.cpp +++ b/lib/TableGen/TableGenBackend.cpp @@ -11,8 +11,8 @@ |