blob: fb8bbaaa052c09dcb2b9d5622a9720105afce8a8 [file] [log] [blame]
Chris Lattner767a1e42003-11-23 18:01:26 +00001//===--- 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 "Support/CommandLine.h"
Chris Lattner6f9e6072004-05-27 05:37:32 +000025#include "llvm/System/Signals.h"
Chris Lattner767a1e42003-11-23 18:01:26 +000026#include <fstream>
27#include <memory>
28
29using namespace llvm;
30
31static cl::opt<std::string>
32InputFilename(cl::Positional, cl::desc("<input .st file>"), cl::init("-"));
33
34static cl::opt<std::string>
35OutputFilename("o", cl::desc("Override output filename"),
36 cl::value_desc("filename"));
37
38static cl::opt<bool>
39Force("f", cl::desc("Overwrite output files"));
40
41static cl::opt<bool>
42DumpAsm("d", cl::desc("Print LLVM Assembly as parsed"), cl::Hidden);
43
44static cl::opt<uint32_t>
45StackSize("s", cl::desc("Specify program maximum stack size"),
46 cl::value_desc("stacksize"));
47
48#ifdef PARSE_DEBUG
49static cl::opt<bool>
50ParseDebug("g", cl::desc("Turn on Bison Debugging"), cl::Hidden);
51#endif
52
53#ifdef FLEX_DEBUG
54static cl::opt<bool>
55FlexDebug("x", cl::desc("Turn on Flex Debugging"), cl::Hidden);
56#endif
57
58static cl::opt<bool>
59EchoSource("e", cl::desc("Print Stacker Source as parsed"),
60 cl::value_desc("echo"));
61
62int 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 (
85 compiler.compile(InputFilename,EchoSource, 1024) );
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
138 RemoveFileOnSignal(OutputFilename);
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}