1226584Sdim//===- Main.cpp - Top-Level TableGen implementation -----------------------===// 2226584Sdim// 3226584Sdim// The LLVM Compiler Infrastructure 4226584Sdim// 5226584Sdim// This file is distributed under the University of Illinois Open Source 6226584Sdim// License. See LICENSE.TXT for details. 7226584Sdim// 8226584Sdim//===----------------------------------------------------------------------===// 9226584Sdim// 10226584Sdim// TableGen is a tool which can be used to build up a description of something, 11226584Sdim// then invoke one or more "tablegen backends" to emit information about the 12226584Sdim// description in some predefined format. In practice, this is used by the LLVM 13226584Sdim// code generators to automate generation of a code generator through a 14226584Sdim// high-level description of the target. 15226584Sdim// 16226584Sdim//===----------------------------------------------------------------------===// 17226584Sdim 18226584Sdim#include "TGParser.h" 19226584Sdim#include "llvm/ADT/OwningPtr.h" 20226584Sdim#include "llvm/Support/CommandLine.h" 21226584Sdim#include "llvm/Support/MemoryBuffer.h" 22226584Sdim#include "llvm/Support/ToolOutputFile.h" 23226584Sdim#include "llvm/Support/system_error.h" 24226584Sdim#include "llvm/TableGen/Error.h" 25243830Sdim#include "llvm/TableGen/Main.h" 26226584Sdim#include "llvm/TableGen/Record.h" 27226584Sdim#include <algorithm> 28226584Sdim#include <cstdio> 29226584Sdimusing namespace llvm; 30226584Sdim 31226584Sdimnamespace { 32226584Sdim cl::opt<std::string> 33226584Sdim OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), 34226584Sdim cl::init("-")); 35226584Sdim 36226584Sdim cl::opt<std::string> 37239462Sdim DependFilename("d", 38239462Sdim cl::desc("Dependency filename"), 39239462Sdim cl::value_desc("filename"), 40226584Sdim cl::init("")); 41226584Sdim 42226584Sdim cl::opt<std::string> 43226584Sdim InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-")); 44226584Sdim 45226584Sdim cl::list<std::string> 46226584Sdim IncludeDirs("I", cl::desc("Directory of include files"), 47226584Sdim cl::value_desc("directory"), cl::Prefix); 48226584Sdim} 49226584Sdim 50243830Sdim/// \brief Create a dependency file for `-d` option. 51243830Sdim/// 52243830Sdim/// This functionality is really only for the benefit of the build system. 53243830Sdim/// It is similar to GCC's `-M*` family of options. 54243830Sdimstatic int createDependencyFile(const TGParser &Parser, const char *argv0) { 55243830Sdim if (OutputFilename == "-") { 56243830Sdim errs() << argv0 << ": the option -d must be used together with -o\n"; 57243830Sdim return 1; 58243830Sdim } 59243830Sdim std::string Error; 60243830Sdim tool_output_file DepOut(DependFilename.c_str(), Error); 61243830Sdim if (!Error.empty()) { 62243830Sdim errs() << argv0 << ": error opening " << DependFilename 63243830Sdim << ":" << Error << "\n"; 64243830Sdim return 1; 65243830Sdim } 66243830Sdim DepOut.os() << OutputFilename << ":"; 67249423Sdim const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies(); 68249423Sdim for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(), 69249423Sdim E = Dependencies.end(); 70243830Sdim I != E; ++I) { 71249423Sdim DepOut.os() << " " << I->first; 72243830Sdim } 73243830Sdim DepOut.os() << "\n"; 74243830Sdim DepOut.keep(); 75243830Sdim return 0; 76243830Sdim} 77243830Sdim 78226584Sdimnamespace llvm { 79226584Sdim 80243830Sdimint TableGenMain(char *argv0, TableGenMainFn *MainFn) { 81226584Sdim RecordKeeper Records; 82226584Sdim 83243830Sdim // Parse the input file. 84243830Sdim OwningPtr<MemoryBuffer> File; 85243830Sdim if (error_code ec = 86263508Sdim MemoryBuffer::getFileOrSTDIN(InputFilename, File)) { 87243830Sdim errs() << "Could not open input file '" << InputFilename << "': " 88243830Sdim << ec.message() <<"\n"; 89243830Sdim return 1; 90243830Sdim } 91243830Sdim MemoryBuffer *F = File.take(); 92226584Sdim 93243830Sdim // Tell SrcMgr about this buffer, which is what TGParser will pick up. 94243830Sdim SrcMgr.AddNewSourceBuffer(F, SMLoc()); 95226584Sdim 96243830Sdim // Record the location of the include directory so that the lexer can find 97243830Sdim // it later. 98243830Sdim SrcMgr.setIncludeDirs(IncludeDirs); 99226584Sdim 100243830Sdim TGParser Parser(SrcMgr, Records); 101226584Sdim 102243830Sdim if (Parser.ParseFile()) 103243830Sdim return 1; 104226584Sdim 105243830Sdim std::string Error; 106243830Sdim tool_output_file Out(OutputFilename.c_str(), Error); 107243830Sdim if (!Error.empty()) { 108243830Sdim errs() << argv0 << ": error opening " << OutputFilename 109243830Sdim << ":" << Error << "\n"; 110243830Sdim return 1; 111243830Sdim } 112243830Sdim if (!DependFilename.empty()) { 113243830Sdim if (int Ret = createDependencyFile(Parser, argv0)) 114243830Sdim return Ret; 115243830Sdim } 116226584Sdim 117243830Sdim if (MainFn(Out.os(), Records)) 118243830Sdim return 1; 119226584Sdim 120249423Sdim if (ErrorsPrinted > 0) { 121249423Sdim errs() << argv0 << ": " << ErrorsPrinted << " errors.\n"; 122249423Sdim return 1; 123249423Sdim } 124249423Sdim 125243830Sdim // Declare success. 126243830Sdim Out.keep(); 127243830Sdim return 0; 128226584Sdim} 129226584Sdim 130226584Sdim} 131