|
| 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 | +} |
0 commit comments