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 "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);
62 int main(int argc, char **argv)
64 cl::ParseCommandLineOptions(argc, argv, " stacker .st -> .bc compiler\n");
66 std::ostream *Out = 0;
67 StackerCompiler compiler;
72 extern int Stackerdebug;
73 Stackerdebug = ParseDebug;
78 extern int Stacker_flex_debug;
79 Stacker_flex_debug = FlexDebug;
82 // Parse the file now...
84 std::auto_ptr<Module> M (
85 compiler.compile(InputFilename,EchoSource, StackSize) );
87 std::cerr << argv[0] << ": assembly didn't read correctly.\n";
91 if (verifyModule(*M.get())) {
93 << ": assembly parsed, but does not verify as correct!\n";
97 if (DumpAsm) std::cerr << "Here's the assembly:\n" << M.get();
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";
108 Out = new std::ofstream(OutputFilename.c_str());
109 } else { // Specified stdout
113 if (InputFilename == "-") {
114 OutputFilename = "-";
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);
123 OutputFilename = IFN; // Append a .bc to it
125 OutputFilename += ".bc";
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";
135 Out = new std::ofstream(OutputFilename.c_str());
136 // Make sure that the Out file gets unlinked from the disk if we get a
138 sys::RemoveFileOnSignal(OutputFilename);
143 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
147 WriteBytecodeToFile(M.get(), *Out);
148 } catch (const ParseException &E) {
149 std::cerr << argv[0] << ": " << E.getMessage() << "\n";
153 if (Out != &std::cout) delete Out;