llvm.org GIT mirror llvm / b515d75
eliminate the std::ostream forms of the bitcode writing APIs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79840 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
9 changed file(s) with 82 addition(s) and 130 deletion(s). Raw diff Collapse all Expand all
3131 #include "llvm/Bitcode/ReaderWriter.h"
3232 #include "llvm/ExecutionEngine/GenericValue.h"
3333 #include "llvm/ExecutionEngine/JIT.h"
34 #include "llvm/Target/TargetSelect.h"
3435 #include "llvm/Support/CommandLine.h"
3536 #include "llvm/Support/ManagedStatic.h"
36 #include "llvm/Target/TargetSelect.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include
3739 #include
38 #include
3940 using namespace llvm;
4041
4142 //Command line options
9091 LLVMContext &Context = getGlobalContext();
9192
9293 if (InputFilename == "") {
93 std::cerr<<"Error: You must specify the filename of the program to "
94 errs() << "Error: You must specify the filename of the program to "
9495 "be compiled. Use --help to see the options.\n";
9596 abort();
9697 }
9798
9899 //Get the output stream
99 std::ostream *out = &std::cout;
100 raw_ostream *out = &outs();
100101 if (!JIT) {
101102 if (OutputFilename == "") {
102103 std::string base = InputFilename;
103 if (InputFilename == "-") {base = "a";}
104 if (InputFilename == "-") { base = "a"; }
104105
105 //Use default filename
106 const char *suffix = ".bc";
107 OutputFilename = base+suffix;
106 // Use default filename.
107 OutputFilename = base+".bc";
108108 }
109109 if (OutputFilename != "-") {
110 out = new std::
111 ofstream(OutputFilename.c_str(),
112 std::ios::out | std::ios::trunc | std::ios::binary);
110 std::string ErrInfo;
111 out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
112 raw_fd_ostream::F_Force|
113 raw_fd_ostream::F_Binary);
113114 }
114115 }
115116
116117 //Get the input stream
117118 std::istream *in = &std::cin;
118 if (InputFilename != "-") {
119 if (InputFilename != "-")
119120 in = new std::ifstream(InputFilename.c_str());
120 }
121121
122122 //Gather the compile flags
123123 BrainF::CompileFlags cf = BrainF::flag_off;
124 if (ArrayBoundsChecking) {
124 if (ArrayBoundsChecking)
125125 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
126 }
127126
128127 //Read the BrainF program
129128 BrainF bf;
130129 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
131 if (in != &std::cin) {delete in;}
130 if (in != &std::cin)
131 delete in;
132132 addMainFunction(mod);
133133
134134 //Verify generated code
135135 if (verifyModule(*mod)) {
136 std::cerr<<"Error: module failed verification. This shouldn't happen.\n";
136 errs() << "Error: module failed verification. This shouldn't happen.\n";
137137 abort();
138138 }
139139
141141 if (JIT) {
142142 InitializeNativeTarget();
143143
144 std::cout << "------- Running JIT -------\n";
144 outs() << "------- Running JIT -------\n";
145145 ExecutionEngine *ee = EngineBuilder(mod).create();
146146 std::vector args;
147147 Function *brainf_func = mod->getFunction("brainf");
151151 }
152152
153153 //Clean up
154 if (out != &std::cout) {delete out;}
154 if (out != &outs())
155 delete out;
155156 delete mod;
156157
157158 llvm_shutdown();
1818 #include "llvm/Constants.h"
1919 #include "llvm/Instructions.h"
2020 #include "llvm/Bitcode/ReaderWriter.h"
21 #include
21 #include "llvm/Support/raw_ostream.h"
2222 using namespace llvm;
2323
2424 int main() {
5555 BB->getInstList().push_back(ReturnInst::Create(Context, Add));
5656
5757 // Output the bitcode file to stdout
58 WriteBitcodeToFile(M, std::cout);
58 WriteBitcodeToFile(M, outs());
5959
6060 // Delete the module and all of its contents.
6161 delete M;
1313 #ifndef LLVM_BITCODE_H
1414 #define LLVM_BITCODE_H
1515
16 #include
1716 #include
1817
1918 namespace llvm {
4039 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
4140 std::string *ErrMsg = 0);
4241
43 /// WriteBitcodeToFile - Write the specified module to the specified output
44 /// stream.
45 void WriteBitcodeToFile(const Module *M, std::ostream &Out);
46
4742 /// WriteBitcodeToFile - Write the specified module to the specified
4843 /// raw output stream.
4944 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
5146 /// WriteBitcodeToStream - Write the specified module to the specified
5247 /// raw output stream.
5348 void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
54
55 /// CreateBitcodeWriterPass - Create and return a pass that writes the module
56 /// to the specified ostream.
57 ModulePass *CreateBitcodeWriterPass(std::ostream &Str);
5849
5950 /// createBitcodeWriterPass - Create and return a pass that writes the module
6051 /// to the specified ostream.
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "llvm/Bitcode/Deserialize.h"
14
15 #ifdef DEBUG_BACKPATCH
16 #include "llvm/Support/Streams.h"
17 #endif
18
14 #include "llvm/Support/raw_ostream.h"
1915 using namespace llvm;
2016
2117 Deserializer::Deserializer(BitstreamReader& stream)
356352 assert (!HasFinalPtr(E) && "Pointer already registered.");
357353
358354 #ifdef DEBUG_BACKPATCH
359 llvm::cerr << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
355 errs() << "RegisterPtr: " << PtrId << " => " << Ptr << "\n";
360356 #endif
361357
362358 SetPtr(E,Ptr);
376372 PtrRef = GetFinalPtr(E);
377373
378374 #ifdef DEBUG_BACKPATCH
379 llvm::cerr << "ReadUintPtr: " << PtrId
380 << " <-- " << (void*) GetFinalPtr(E) << '\n';
375 errs() << "ReadUintPtr: " << PtrId
376 << " <-- " << (void*) GetFinalPtr(E) << '\n';
381377 #endif
382378 }
383379 else {
385381 "Client forbids backpatching for this pointer.");
386382
387383 #ifdef DEBUG_BACKPATCH
388 llvm::cerr << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
384 errs() << "ReadUintPtr: " << PtrId << " (NO PTR YET)\n";
389385 #endif
390386
391387 // Register backpatch. Check the freelist for a BPNode.
88
99 #include "llvm-c/BitWriter.h"
1010 #include "llvm/Bitcode/ReaderWriter.h"
11 #include
12
11 #include "llvm/Support/raw_ostream.h"
1312 using namespace llvm;
1413
1514
1615 /*===-- Operations on modules ---------------------------------------------===*/
1716
1817 int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
19 std::ofstream OS(Path, std::ios_base::out|std::ios::trunc|std::ios::binary);
18 std::string ErrorInfo;
19 raw_fd_ostream OS(Path, ErrorInfo,
20 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
2021
21 if (!OS.fail())
22 WriteBitcodeToFile(unwrap(M), OS);
23
24 if (OS.fail())
22 if (!ErrorInfo.empty())
2523 return -1;
2624
25 WriteBitcodeToFile(unwrap(M), OS);
2726 return 0;
2827 }
2928
3029 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR >= 4)
3130 #include
3231
33 // FIXME: Control this with configure? Provide some portable abstraction in
34 // libSystem? As is, the user will just get a linker error if they use this on
35 // non-GCC. Some C++ stdlibs even have ofstream::ofstream(int fd).
3632 int LLVMWriteBitcodeToFileHandle(LLVMModuleRef M, int FileHandle) {
37 __gnu_cxx::stdio_filebuf Buffer(FileHandle, std::ios_base::out |
38 std::ios::trunc |
39 std::ios::binary);
40 std::ostream OS(&Buffer);
33 raw_fd_ostream OS(FileHandle, false);
4134
42 if (!OS.fail())
43 WriteBitcodeToFile(unwrap(M), OS);
44
45 if (OS.fail())
46 return -1;
47
35 WriteBitcodeToFile(unwrap(M), OS);
4836 return 0;
4937 }
5038
2525 #include "llvm/ValueSymbolTable.h"
2626 #include "llvm/Support/ErrorHandling.h"
2727 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/Streams.h"
2928 #include "llvm/Support/raw_ostream.h"
3029 #include "llvm/System/Program.h"
3130 using namespace llvm;
14671466
14681467 /// WriteBitcodeToFile - Write the specified module to the specified output
14691468 /// stream.
1470 void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
1471 raw_os_ostream RawOut(Out);
1472 // If writing to stdout, set binary mode.
1473 if (llvm::cout == Out)
1474 sys::Program::ChangeStdoutToBinary();
1475 WriteBitcodeToFile(M, RawOut);
1476 }
1477
1478 /// WriteBitcodeToFile - Write the specified module to the specified output
1479 /// stream.
14801469 void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out) {
14811470 std::vector Buffer;
14821471 BitstreamWriter Stream(Buffer);
1616
1717 namespace {
1818 class WriteBitcodePass : public ModulePass {
19 // FIXME: Kill off std::ostream
20 std::ostream *Out;
21 raw_ostream *RawOut; // raw_ostream to print on
19 raw_ostream &OS; // raw_ostream to print on
2220 public:
2321 static char ID; // Pass identification, replacement for typeid
24 explicit WriteBitcodePass(std::ostream &o)
25 : ModulePass(&ID), Out(&o), RawOut(0) {}
2622 explicit WriteBitcodePass(raw_ostream &o)
27 : ModulePass(&ID), Out(0), RawOut(&o) {}
23 : ModulePass(&ID), OS(o) {}
2824
2925 const char *getPassName() const { return "Bitcode Writer"; }
3026
3127 bool runOnModule(Module &M) {
32 if (Out) {
33 WriteBitcodeToFile(&M, *Out);
34 } else {
35 WriteBitcodeToFile(&M, *RawOut);
36 }
28 WriteBitcodeToFile(&M, OS);
3729 return false;
3830 }
3931 };
4133
4234 char WriteBitcodePass::ID = 0;
4335
44 /// CreateBitcodeWriterPass - Create and return a pass that writes the module
45 /// to the specified ostream.
46 ModulePass *llvm::CreateBitcodeWriterPass(std::ostream &Str) {
47 return new WriteBitcodePass(Str);
48 }
49
50
5136 /// createBitcodeWriterPass - Create and return a pass that writes the module
5237 /// to the specified ostream.
5338 ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str) {
5050 ///
5151 bool BugDriver::writeProgramToFile(const std::string &Filename,
5252 Module *M) const {
53 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
54 std::ios::binary;
55 std::ofstream Out(Filename.c_str(), io_mode);
56 if (!Out.good()) return true;
53 std::string ErrInfo;
54 raw_fd_ostream Out(Filename.c_str(), ErrInfo,
55 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
56 if (!ErrInfo.empty()) return true;
5757
5858 WriteBitcodeToFile(M ? M : Program, Out);
5959 return false;
8282 }
8383
8484 int BugDriver::runPassesAsChild(const std::vector &Passes) {
85
86 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
87 std::ios::binary;
88 std::ofstream OutFile(ChildOutput.c_str(), io_mode);
89 if (!OutFile.good()) {
85 std::string ErrInfo;
86 raw_fd_ostream OutFile(ChildOutput.c_str(), ErrInfo,
87 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
88 if (!ErrInfo.empty()) {
9089 errs() << "Error opening bitcode file: " << ChildOutput << "\n";
9190 return 1;
9291 }
105104 PM.add(createVerifierPass());
106105
107106 // Write bitcode out to disk as the last step...
108 PM.add(CreateBitcodeWriterPass(OutFile));
107 PM.add(createBitcodeWriterPass(OutFile));
109108
110109 // Run all queued passes.
111110 PM.run(*Program);
145144 << ErrMsg << "\n";
146145 return(1);
147146 }
148 std::ios::openmode io_mode = std::ios::out | std::ios::trunc |
149 std::ios::binary;
150 std::ofstream InFile(inputFilename.c_str(), io_mode);
151 if (!InFile.good()) {
147
148 std::string ErrInfo;
149 raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo,
150 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
151
152
153 if (!ErrInfo.empty()) {
152154 errs() << "Error opening bitcode file: " << inputFilename << "\n";
153 return(1);
155 return 1;
154156 }
155157 WriteBitcodeToFile(Program, InFile);
156158 InFile.close();
4646 #include "llvm/Transforms/IPO.h"
4747 #include "llvm/Transforms/Scalar.h"
4848 #include "llvm/Config/config.h"
49
50
5149 #include
52 #include
5350 #include
5451 #include
5552
138135 }
139136
140137
141 bool LTOCodeGenerator::writeMergedModules(const char* path, std::string& errMsg)
142 {
143 if ( this->determineTarget(errMsg) )
144 return true;
145
146 // mark which symbols can not be internalized
147 this->applyScopeRestrictions();
148
149 // create output file
150 std::ofstream out(path, std::ios_base::out|std::ios::trunc|std::ios::binary);
151 if ( out.fail() ) {
152 errMsg = "could not open bitcode file for writing: ";
153 errMsg += path;
154 return true;
155 }
138 bool LTOCodeGenerator::writeMergedModules(const char *path,
139 std::string &errMsg) {
140 if (determineTarget(errMsg))
141 return true;
142
143 // mark which symbols can not be internalized
144 applyScopeRestrictions();
145
146 // create output file
147 std::string ErrInfo;
148 raw_fd_ostream Out(path, ErrInfo,
149 raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
150 if (!ErrInfo.empty()) {
151 errMsg = "could not open bitcode file for writing: ";
152 errMsg += path;
153 return true;
154 }
156155
157 // write bitcode to it
158 WriteBitcodeToFile(_linker.getModule(), out);
159 if ( out.fail() ) {
160 errMsg = "could not write bitcode file: ";
161 errMsg += path;
162 return true;
163 }
164
165 return false;
156 // write bitcode to it
157 WriteBitcodeToFile(_linker.getModule(), Out);
158
159 if (Out.has_error()) {
160 errMsg = "could not write bitcode file: ";
161 errMsg += path;
162 return true;
163 }
164
165 return false;
166166 }
167167
168168