Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 1 | //===--- Bitcode/Writer/BitcodeWriterPass.cpp - Bitcode Writer ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // BitcodeWriterPass implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Bitcode/ReaderWriter.h" |
| 15 | #include "llvm/Pass.h" |
| 16 | using namespace llvm; |
| 17 | |
| 18 | namespace { |
| 19 | class WriteBitcodePass : public ModulePass { |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 20 | // FIXME: Kill off std::ostream |
| 21 | std::ostream *Out; |
| 22 | raw_ostream *RawOut; // raw_ostream to print on |
Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 23 | public: |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 24 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | adf3eab | 2007-11-19 15:30:20 +0000 | [diff] [blame] | 25 | explicit WriteBitcodePass(std::ostream &o) |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 26 | : ModulePass(&ID), Out(&o), RawOut(0) {} |
| 27 | explicit WriteBitcodePass(raw_ostream &o) |
| 28 | : ModulePass(&ID), Out(0), RawOut(&o) {} |
Gordon Henriksen | bd76d66 | 2007-11-04 20:28:31 +0000 | [diff] [blame] | 29 | |
| 30 | const char *getPassName() const { return "Bitcode Writer"; } |
Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 31 | |
| 32 | bool runOnModule(Module &M) { |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 33 | if (Out) { |
| 34 | WriteBitcodeToFile(&M, *Out); |
| 35 | } else { |
| 36 | WriteBitcodeToFile(&M, *RawOut); |
| 37 | } |
Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | char WriteBitcodePass::ID = 0; |
Chris Lattner | e8e1e4b | 2007-05-06 02:30:12 +0000 | [diff] [blame] | 44 | |
| 45 | /// CreateBitcodeWriterPass - Create and return a pass that writes the module |
| 46 | /// to the specified ostream. |
| 47 | ModulePass *llvm::CreateBitcodeWriterPass(std::ostream &Str) { |
| 48 | return new WriteBitcodePass(Str); |
| 49 | } |
| 50 | |
| 51 | |
Daniel Dunbar | d1ce3b4 | 2008-10-22 17:39:14 +0000 | [diff] [blame] | 52 | /// createBitcodeWriterPass - Create and return a pass that writes the module |
| 53 | /// to the specified ostream. |
| 54 | ModulePass *llvm::createBitcodeWriterPass(raw_ostream &Str) { |
| 55 | return new WriteBitcodePass(Str); |
| 56 | } |