LLVM API Documentation

Main.cpp
Go to the documentation of this file.
00001 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // TableGen is a tool which can be used to build up a description of something,
00011 // then invoke one or more "tablegen backends" to emit information about the
00012 // description in some predefined format.  In practice, this is used by the LLVM
00013 // code generators to automate generation of a code generator through a
00014 // high-level description of the target.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "TGParser.h"
00019 #include "llvm/ADT/OwningPtr.h"
00020 #include "llvm/Support/CommandLine.h"
00021 #include "llvm/Support/MemoryBuffer.h"
00022 #include "llvm/Support/ToolOutputFile.h"
00023 #include "llvm/Support/system_error.h"
00024 #include "llvm/TableGen/Error.h"
00025 #include "llvm/TableGen/Main.h"
00026 #include "llvm/TableGen/Record.h"
00027 #include <algorithm>
00028 #include <cstdio>
00029 using namespace llvm;
00030 
00031 namespace {
00032   cl::opt<std::string>
00033   OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
00034                  cl::init("-"));
00035 
00036   cl::opt<std::string>
00037   DependFilename("d",
00038                  cl::desc("Dependency filename"),
00039                  cl::value_desc("filename"),
00040                  cl::init(""));
00041 
00042   cl::opt<std::string>
00043   InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
00044 
00045   cl::list<std::string>
00046   IncludeDirs("I", cl::desc("Directory of include files"),
00047               cl::value_desc("directory"), cl::Prefix);
00048 }
00049 
00050 /// \brief Create a dependency file for `-d` option.
00051 ///
00052 /// This functionality is really only for the benefit of the build system.
00053 /// It is similar to GCC's `-M*` family of options.
00054 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
00055   if (OutputFilename == "-") {
00056     errs() << argv0 << ": the option -d must be used together with -o\n";
00057     return 1;
00058   }
00059   std::string Error;
00060   tool_output_file DepOut(DependFilename.c_str(), Error);
00061   if (!Error.empty()) {
00062     errs() << argv0 << ": error opening " << DependFilename
00063       << ":" << Error << "\n";
00064     return 1;
00065   }
00066   DepOut.os() << OutputFilename << ":";
00067   const TGLexer::DependenciesMapTy &Dependencies = Parser.getDependencies();
00068   for (TGLexer::DependenciesMapTy::const_iterator I = Dependencies.begin(),
00069                                                   E = Dependencies.end();
00070        I != E; ++I) {
00071     DepOut.os() << " " << I->first;
00072   }
00073   DepOut.os() << "\n";
00074   DepOut.keep();
00075   return 0;
00076 }
00077 
00078 namespace llvm {
00079 
00080 int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
00081   RecordKeeper Records;
00082 
00083   // Parse the input file.
00084   OwningPtr<MemoryBuffer> File;
00085   if (error_code ec =
00086         MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), File)) {
00087     errs() << "Could not open input file '" << InputFilename << "': "
00088            << ec.message() <<"\n";
00089     return 1;
00090   }
00091   MemoryBuffer *F = File.take();
00092 
00093   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
00094   SrcMgr.AddNewSourceBuffer(F, SMLoc());
00095 
00096   // Record the location of the include directory so that the lexer can find
00097   // it later.
00098   SrcMgr.setIncludeDirs(IncludeDirs);
00099 
00100   TGParser Parser(SrcMgr, Records);
00101 
00102   if (Parser.ParseFile())
00103     return 1;
00104 
00105   std::string Error;
00106   tool_output_file Out(OutputFilename.c_str(), Error);
00107   if (!Error.empty()) {
00108     errs() << argv0 << ": error opening " << OutputFilename
00109       << ":" << Error << "\n";
00110     return 1;
00111   }
00112   if (!DependFilename.empty()) {
00113     if (int Ret = createDependencyFile(Parser, argv0))
00114       return Ret;
00115   }
00116 
00117   if (MainFn(Out.os(), Records))
00118     return 1;
00119 
00120   if (ErrorsPrinted > 0) {
00121     errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
00122     return 1;
00123   }
00124 
00125   // Declare success.
00126   Out.keep();
00127   return 0;
00128 }
00129 
00130 }