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 | |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 62 | enum OptLev { |
| 63 | None = 0, |
| 64 | One = 1, |
| 65 | Two = 2, |
| 66 | Three = 3, |
| 67 | Four = 4, |
| 68 | Five = 5 |
| 69 | }; |
| 70 | |
| 71 | static cl::opt<OptLev> OptLevel( |
| 72 | cl::desc("Choose optimization level to apply:"), |
| 73 | cl::init(One), |
| 74 | cl::values( |
| 75 | clEnumValN(None,"O0","An alias for the -O1 option"), |
| 76 | clEnumValN(One,"O1","Optimize for compilation speed"), |
| 77 | clEnumValN(Two,"O2","Perform simple optimizations to reduce code size"), |
| 78 | clEnumValN(Three,"O3","More aggressive optimizations"), |
| 79 | clEnumValN(Four,"O4","High level of optimization"), |
| 80 | clEnumValN(Five,"O5","An alias for the -O4 option"), |
| 81 | clEnumValEnd |
| 82 | )); |
| 83 | |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 84 | int main(int argc, char **argv) |
| 85 | { |
| 86 | cl::ParseCommandLineOptions(argc, argv, " stacker .st -> .bc compiler\n"); |
| 87 | |
| 88 | std::ostream *Out = 0; |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 89 | try { |
| 90 | StackerCompiler compiler; |
| 91 | try |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 92 | { |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 93 | #ifdef PARSE_DEBUG |
| 94 | { |
| 95 | extern int Stackerdebug; |
| 96 | Stackerdebug = ParseDebug; |
| 97 | } |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 98 | #endif |
| 99 | #ifdef FLEX_DEBUG |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 100 | { |
| 101 | extern int Stacker_flex_debug; |
| 102 | Stacker_flex_debug = FlexDebug; |
| 103 | } |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 104 | #endif |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 105 | // Parse the file now... |
| 106 | |
| 107 | std::auto_ptr<Module> M ( |
| 108 | compiler.compile(InputFilename,EchoSource,OptLevel,StackSize)); |
| 109 | if (M.get() == 0) { |
| 110 | throw std::string("program didn't parse correctly."); |
| 111 | } |
| 112 | |
| 113 | if (verifyModule(*M.get())) { |
| 114 | throw std::string("program parsed, but does not verify as correct!"); |
| 115 | } |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 116 | |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 117 | if (DumpAsm) |
| 118 | std::cerr << "Here's the assembly:" << M.get(); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 119 | |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 120 | if (OutputFilename != "") { // Specified an output filename? |
| 121 | if (OutputFilename != "-") { // Not stdout? |
| 122 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 123 | // If force is not specified, make sure not to overwrite a file! |
| 124 | throw std::string("error opening '") + OutputFilename + |
| 125 | "': file exists!\n" + |
| 126 | "Use -f command line argument to force output"; |
| 127 | return 1; |
| 128 | } |
| 129 | Out = new std::ofstream(OutputFilename.c_str()); |
| 130 | } else { // Specified stdout |
| 131 | Out = &std::cout; |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 133 | } else { |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 134 | if (InputFilename == "-") { |
| 135 | OutputFilename = "-"; |
| 136 | Out = &std::cout; |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 137 | } else { |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 138 | std::string IFN = InputFilename; |
| 139 | int Len = IFN.length(); |
| 140 | if (IFN[Len-3] == '.' && IFN[Len-2] == 's' && IFN[Len-1] == 't') { |
| 141 | // Source ends in .ll |
| 142 | OutputFilename = std::string(IFN.begin(), IFN.end()-3); |
| 143 | } else { |
| 144 | OutputFilename = IFN; // Append a .bc to it |
| 145 | } |
| 146 | OutputFilename += ".bc"; |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 147 | |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 148 | if (!Force && std::ifstream(OutputFilename.c_str())) { |
| 149 | // If force is not specified, make sure not to overwrite a file! |
| 150 | throw std::string("error opening '") + OutputFilename + |
| 151 | "': file exists!\n" + |
| 152 | "Use -f command line argument to force output\n"; |
| 153 | } |
| 154 | |
| 155 | Out = new std::ofstream(OutputFilename.c_str()); |
| 156 | // Make sure that the Out file gets unlinked from the disk if we get a |
| 157 | // SIGINT |
| 158 | sys::RemoveFileOnSignal(OutputFilename); |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 160 | } |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 161 | |
| 162 | if (!Out->good()) { |
| 163 | throw std::string("error opening ") + OutputFilename + "!"; |
| 164 | } |
| 165 | |
| 166 | WriteBytecodeToFile(M.get(), *Out); |
| 167 | } catch (const ParseException &E) { |
| 168 | std::cerr << argv[0] << ": " << E.getMessage() << "\n"; |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 169 | return 1; |
| 170 | } |
Reid Spencer | 2c71157 | 2004-09-04 19:05:53 +0000 | [diff] [blame] | 171 | } |
| 172 | catch (const std::string& msg ) { |
| 173 | std::cerr << argv[0] << ": " << msg << "\n"; |
Chris Lattner | 767a1e4 | 2003-11-23 18:01:26 +0000 | [diff] [blame] | 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | if (Out != &std::cout) delete Out; |
| 178 | return 0; |
| 179 | } |