32
32
#include " llvm/Bitcode/ReaderWriter.h"
33
33
#include " llvm/ExecutionEngine/GenericValue.h"
34
34
#include " llvm/ExecutionEngine/JIT.h"
35
+ #include " llvm/Target/TargetSelect.h"
35
36
#include " llvm/Support/CommandLine.h"
36
37
#include " llvm/Support/ManagedStatic.h"
37
- #include " llvm/Target/TargetSelect.h"
38
- #include < fstream>
38
+ #include " llvm/Support/raw_ostream.h"
39
39
#include < iostream>
40
+ #include < fstream>
40
41
using namespace llvm ;
41
42
42
43
// Command line options
@@ -91,58 +92,57 @@ int main(int argc, char **argv) {
91
92
LLVMContext &Context = getGlobalContext ();
92
93
93
94
if (InputFilename == " " ) {
94
- std::cerr<< " Error: You must specify the filename of the program to "
95
+ errs () << " Error: You must specify the filename of the program to "
95
96
" be compiled. Use --help to see the options.\n " ;
96
97
abort ();
97
98
}
98
99
99
100
// Get the output stream
100
- std::ostream *out = &std::cout ;
101
+ raw_ostream *out = &outs () ;
101
102
if (!JIT) {
102
103
if (OutputFilename == " " ) {
103
104
std::string base = InputFilename;
104
- if (InputFilename == " -" ) {base = " a" ;}
105
+ if (InputFilename == " -" ) { base = " a" ; }
105
106
106
- // Use default filename
107
- const char *suffix = " .bc" ;
108
- OutputFilename = base+suffix;
107
+ // Use default filename.
108
+ OutputFilename = base+" .bc" ;
109
109
}
110
110
if (OutputFilename != " -" ) {
111
- out = new std::
112
- ofstream (OutputFilename.c_str (),
113
- std::ios::out | std::ios::trunc | std::ios::binary);
111
+ std::string ErrInfo;
112
+ out = new raw_fd_ostream (OutputFilename.c_str (), ErrInfo,
113
+ raw_fd_ostream::F_Force|
114
+ raw_fd_ostream::F_Binary);
114
115
}
115
116
}
116
117
117
118
// Get the input stream
118
119
std::istream *in = &std::cin;
119
- if (InputFilename != " -" ) {
120
+ if (InputFilename != " -" )
120
121
in = new std::ifstream (InputFilename.c_str ());
121
- }
122
122
123
123
// Gather the compile flags
124
124
BrainF::CompileFlags cf = BrainF::flag_off;
125
- if (ArrayBoundsChecking) {
125
+ if (ArrayBoundsChecking)
126
126
cf = BrainF::CompileFlags (cf | BrainF::flag_arraybounds);
127
- }
128
127
129
128
// Read the BrainF program
130
129
BrainF bf;
131
130
Module *mod = bf.parse (in, 65536 , cf, Context); // 64 KiB
132
- if (in != &std::cin) {delete in;}
131
+ if (in != &std::cin)
132
+ delete in;
133
133
addMainFunction (mod);
134
134
135
135
// Verify generated code
136
136
if (verifyModule (*mod)) {
137
- std::cerr<< " Error: module failed verification. This shouldn't happen.\n " ;
137
+ errs () << " Error: module failed verification. This shouldn't happen.\n " ;
138
138
abort ();
139
139
}
140
140
141
141
// Write it out
142
142
if (JIT) {
143
143
InitializeNativeTarget ();
144
144
145
- std::cout << " ------- Running JIT -------\n " ;
145
+ outs () << " ------- Running JIT -------\n " ;
146
146
ExecutionEngine *ee = EngineBuilder (mod).create ();
147
147
std::vector<GenericValue> args;
148
148
Function *brainf_func = mod->getFunction (" brainf" );
@@ -152,7 +152,8 @@ int main(int argc, char **argv) {
152
152
}
153
153
154
154
// Clean up
155
- if (out != &std::cout) {delete out;}
155
+ if (out != &outs ())
156
+ delete out;
156
157
delete mod;
157
158
158
159
llvm_shutdown ();
0 commit comments