Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 1 | //===--- stkrc.cpp --- The Stacker Compiler -------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and donated to the LLVM research |
| 6 | // group and is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This is the "main" program for the Stacker Compiler. It is simply a utility |
| 12 | // that invokes the StackerCompiler::compile method (see StackerCompiler.cpp) |
| 13 | // |
| 14 | // To get help using this utility, you can invoke it with: |
| 15 | // stkrc --help - Output information about command line switches |
| 16 | // |
| 17 | // |
| 18 | //===------------------------------------------------------------------------=== |
| 19 | |
| 20 | #include "../../lib/compiler/StackerCompiler.h" |
| 21 | #include "llvm/Assembly/Parser.h" |
| 22 | #include "llvm/Bytecode/Writer.h" |
| 23 | #include "llvm/Analysis/Verifier.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 6f9e607 | 2004-05-27 05:37:32 +0000 | [diff] [blame] | 25 | #include "llvm/System/Signals.h" |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 26 | #include <fstream> |
Reid Spencer | 321f831 | 2004-07-04 12:22:14 +0000 | [diff] [blame] | 27 | #include <iostream> |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 28 | #include <memory> |
| 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<std::string> |
| 33 | InputFilename(cl::Positional, cl::desc("<input .st file>"), cl::init("-")); |
| 34 | |
| 35 | static cl::opt<std::string> |
| 36 | OutputFilename("o", cl::desc("Override output filename"), |
| 37 | cl::value_desc("filename")); |
| 38 | |
| 39 | static cl::opt<bool> |
| 40 | Force("f", cl::desc("Overwrite output files")); |
| 41 | |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 42 | static cl::opt<uint32_t> |
| 43 | StackSize("s", cl::desc("Specify program maximum stack size"), |
Reid Spencer | 6220aa8 | 2004-07-10 23:35:46 +0000 | [diff] [blame] | 44 | cl::init(1024), cl::value_desc("stack size")); |
| 45 | |
| 46 | static cl::opt<bool> |
| 47 | DumpAsm("d", cl::desc("Print LLVM Assembly as parsed"), cl::Hidden); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef PARSE_DEBUG |
| 50 | static cl::opt<bool> |
| 51 | ParseDebug("g", cl::desc("Turn on Bison Debugging"), cl::Hidden); |
| 52 | #endif |
| 53 | |
| 54 | #ifdef FLEX_DEBUG |
| 55 | static cl::opt<bool> |
| 56 | FlexDebug("x", cl::desc("Turn on Flex Debugging"), cl::Hidden); |
| 57 | #endif |
| 58 | |
| 59 | static cl::opt<bool> |
Reid Spencer | 6220aa8 | 2004-07-10 23:35:46 +0000 | [diff] [blame] | 60 | EchoSource("e", cl::desc("Print Stacker Source as parsed"), cl::Hidden); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 61 | |
| 62 | int main(int argc, char **argv) |
| 63 | { |
| 64 | cl::ParseCommandLineOptions(argc, argv, " stacker .st -> .bc compiler\n"); |
| 65 | |
| 66 | std::ostream *Out = 0; |
| 67 | StackerCompiler compiler; |
| 68 | try |
| 69 | { |
| 70 | #ifdef PARSE_DEBUG |
| 71 | { |
| 72 | extern int Stackerdebug; |
| 73 | Stackerdebug = ParseDebug; |
| 74 | } |
| 75 | #endif |
| 76 | #ifdef FLEX_DEBUG |
| 77 | { |
| 78 | extern int Stacker_flex_debug; |
| 79 | Stacker_flex_debug = FlexDebug; |
| 80 | } |
| 81 | #endif |
| 82 | // Parse the file now... |
| 83 | |
| 84 | std::auto_ptr<Module> M ( |
Reid Spencer | 6220aa8 | 2004-07-10 23:35:46 +0000 | [diff] [blame] | 85 | compiler.compile(InputFilename,EchoSource, StackSize) ); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 86 | if (M.get() == 0) { |
| 87 | std::cerr << argv[0] << ": assembly didn't read correctly.\n"; |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | if (verifyModule(*M.get())) { |
| 92 | std::cerr << argv[0] |
| 93 | << ": assembly parsed, but does not verify as correct!\n"; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get(); |
| 98 | |
| 99 | if (OutputFilename != "") { // Specified an output filename? |
| 100 | if (OutputFilename != "-") { // Not stdout? |
| 101 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 102 | // If force is not specified, make sure not to overwrite a file! |
| 103 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 104 | << "': file exists!\n" |
| 105 | << "Use -f command line argument to force output\n"; |
| 106 | return 1; |
| 107 | } |
| 108 | Out = new std::ofstream(OutputFilename.c_str()); |
| 109 | } else { // Specified stdout |
| 110 | Out = &std::cout; |
| 111 | } |
| 112 | } else { |
| 113 | if (InputFilename == "-") { |
| 114 | OutputFilename = "-"; |
| 115 | Out = &std::cout; |
| 116 | } else { |
| 117 | std::string IFN = InputFilename; |
| 118 | int Len = IFN.length(); |
| 119 | if (IFN[Len-3] == '.' && IFN[Len-2] == 's' && IFN[Len-1] == 't') { |
| 120 | // Source ends in .ll |
| 121 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 122 | } else { |
| 123 | OutputFilename = IFN; // Append a .bc to it |
| 124 | } |
| 125 | OutputFilename += ".bc"; |
| 126 | |
| 127 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 128 | // If force is not specified, make sure not to overwrite a file! |
| 129 | std::cerr << argv[0] << ": error opening '" << OutputFilename |
| 130 | << "': file exists!\n" |
| 131 | << "Use -f command line argument to force output\n"; |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | Out = new std::ofstream(OutputFilename.c_str()); |
| 136 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 137 | // SIGINT |
Nate Begeman | 2f795cf | 2004-08-29 22:01:17 +0000 | [diff] [blame] | 138 | sys::RemoveFileOnSignal(OutputFilename); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | if (!Out->good()) { |
| 143 | std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | WriteBytecodeToFile(M.get(), *Out); |
| 148 | } catch (const ParseException &E) { |
| 149 | std::cerr << argv[0] << ": " << E.getMessage() << "\n"; |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | if (Out != &std::cout) delete Out; |
| 154 | return 0; |
| 155 | } |