Replacing std::iostreams with llvm iostreams. Some of these changes involve
[oota-llvm.git] / projects / Stacker / tools / stkrc / stkrc.cpp
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"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Streams.h"
26 #include "llvm/System/Signals.h"
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
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
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"));
45
46 static cl::opt<bool>
47 DumpAsm("d", cl::desc("Print LLVM Assembly as parsed"), cl::Hidden);
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>
60 EchoSource("e", cl::desc("Print Stacker Source as parsed"), cl::Hidden);
61
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
84 int main(int argc, char **argv)
85 {
86   cl::ParseCommandLineOptions(argc, argv, " stacker .st -> .bc compiler\n");
87
88   std::ostream *Out = 0;
89   try {
90     StackerCompiler compiler;
91     try
92     {
93 #ifdef PARSE_DEBUG
94       {
95           extern int Stackerdebug;
96           Stackerdebug = ParseDebug;
97       }
98 #endif
99 #ifdef FLEX_DEBUG
100       {
101           extern int Stacker_flex_debug;
102           Stacker_flex_debug = FlexDebug;
103       }
104 #endif
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       }
116
117       if (DumpAsm)
118         llvm_cerr << "Here's the assembly:" << M.get();
119
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;
132         }
133       } else {
134         if (InputFilename == "-") {
135           OutputFilename = "-";
136           Out = &std::cout;
137         } else {
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";
147
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(sys::Path(OutputFilename));
159         }
160       }
161
162       if (!Out->good()) {
163         throw std::string("error opening ") + OutputFilename + "!";
164       }
165
166       llvm_ostream L(*Out);
167       WriteBytecodeToFile(M.get(), L);
168     } catch (const ParseError &E) {
169       llvm_cerr << argv[0] << ": " << E.getMessage() << "\n";
170       return 1;
171     }
172   }
173   catch (const std::string& msg ) {
174     llvm_cerr << argv[0] << ": " << msg << "\n";
175     return 1;
176   }
177
178   if (Out != &std::cout) delete Out;
179   return 0;
180 }