blob: 6006bb476e2ab6eb0c5bcff033926aaa0c02679d [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"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/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>
Reid Spencer321f8312004-07-04 12:22:14 +000027#include <iostream>
Chris Lattner767a1e42003-11-23 18:01:26 +000028#include <memory>
29
30using namespace llvm;
31
32static cl::opt<std::string>
33InputFilename(cl::Positional, cl::desc("<input .st file>"), cl::init("-"));
34
35static cl::opt<std::string>
36OutputFilename("o", cl::desc("Override output filename"),
37 cl::value_desc("filename"));
38
39static cl::opt<bool>
40Force("f", cl::desc("Overwrite output files"));
41
Chris Lattner767a1e42003-11-23 18:01:26 +000042static cl::opt<uint32_t>
43StackSize("s", cl::desc("Specify program maximum stack size"),
Reid Spencer6220aa82004-07-10 23:35:46 +000044 cl::init(1024), cl::value_desc("stack size"));
45
46static cl::opt<bool>
47DumpAsm("d", cl::desc("Print LLVM Assembly as parsed"), cl::Hidden);
Chris Lattner767a1e42003-11-23 18:01:26 +000048
49#ifdef PARSE_DEBUG
50static cl::opt<bool>
51ParseDebug("g", cl::desc("Turn on Bison Debugging"), cl::Hidden);
52#endif
53
54#ifdef FLEX_DEBUG
55static cl::opt<bool>
56FlexDebug("x", cl::desc("Turn on Flex Debugging"), cl::Hidden);
57#endif
58
59static cl::opt<bool>
Reid Spencer6220aa82004-07-10 23:35:46 +000060EchoSource("e", cl::desc("Print Stacker Source as parsed"), cl::Hidden);
Chris Lattner767a1e42003-11-23 18:01:26 +000061
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 (
Reid Spencer6220aa82004-07-10 23:35:46 +000085 compiler.compile(InputFilename,EchoSource, StackSize) );
Chris Lattner767a1e42003-11-23 18:01:26 +000086 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
Nate Begeman2f795cf2004-08-29 22:01:17 +0000138 sys::RemoveFileOnSignal(OutputFilename);
Chris Lattner767a1e42003-11-23 18:01:26 +0000139 }
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}