1 //===--- stkrc.cpp --- The Stacker Compiler -------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
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)
14 // To get help using this utility, you can invoke it with:
15 // stkrc --help - Output information about command line switches
18 //===------------------------------------------------------------------------===
20 #include "../../lib/compiler/StackerCompiler.h"
21 #include "llvm/Assembly/Parser.h"
22 #include "llvm/Bytecode/Writer.h"
23 #include "llvm/Analysis/Verifier.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/System/Signals.h"
32 static cl::opt<std::string>
33 InputFilename(cl::Positional, cl::desc("<input .st file>"), cl::init("-"));
35 static cl::opt<std::string>
36 OutputFilename("o", cl::desc("Override output filename"),
37 cl::value_desc("filename"));
40 Force("f", cl::desc("Overwrite output files"));
42 static cl::opt<uint32_t>
43 StackSize("s", cl::desc("Specify program maximum stack size"),
44 cl::init(1024), cl::value_desc("stack size"));
47 DumpAsm("d", cl::desc("Print LLVM Assembly as parsed"), cl::Hidden);
51 ParseDebug("g", cl::desc("Turn on Bison Debugging"), cl::Hidden);
56 FlexDebug("x", cl::desc("Turn on Flex Debugging"), cl::Hidden);
60 EchoSource("e", cl::desc("Print Stacker Source as parsed"), cl::Hidden);
71 static cl::opt<OptLev> OptLevel(
72 cl::desc("Choose optimization level to apply:"),
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"),
84 int main(int argc, char **argv)
86 cl::ParseCommandLineOptions(argc, argv, " stacker .st -> .bc compiler\n");
88 std::ostream *Out = 0;
90 StackerCompiler compiler;
95 extern int Stackerdebug;
96 Stackerdebug = ParseDebug;
101 extern int Stacker_flex_debug;
102 Stacker_flex_debug = FlexDebug;
105 // Parse the file now...
107 std::auto_ptr<Module> M (
108 compiler.compile(InputFilename,EchoSource,OptLevel,StackSize));
110 throw std::string("program didn't parse correctly.");
113 if (verifyModule(*M.get())) {
114 throw std::string("program parsed, but does not verify as correct!");
118 std::cerr << "Here's the assembly:" << M.get();
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";
129 Out = new std::ofstream(OutputFilename.c_str());
130 } else { // Specified stdout
134 if (InputFilename == "-") {
135 OutputFilename = "-";
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);
144 OutputFilename = IFN; // Append a .bc to it
146 OutputFilename += ".bc";
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";
155 Out = new std::ofstream(OutputFilename.c_str());
156 // Make sure that the Out file gets unlinked from the disk if we get a
158 sys::RemoveFileOnSignal(sys::Path(OutputFilename));
163 throw std::string("error opening ") + OutputFilename + "!";
166 WriteBytecodeToFile(M.get(), *Out);
167 } catch (const ParseException &E) {
168 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
172 catch (const std::string& msg ) {
173 std::cerr << argv[0] << ": " << msg << "\n";
177 if (Out != &std::cout) delete Out;