Don't special-case stdout in llvm::WriteBitcodeToFile; just consider
[oota-llvm.git] / include / llvm / Bitcode / ReaderWriter.h
1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines interfaces to read and write LLVM bitcode files/streams.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_BITCODE_H
15 #define LLVM_BITCODE_H
16
17 #include <string>
18
19 namespace llvm {
20   class Module;
21   class MemoryBuffer;
22   class ModulePass;
23   class BitstreamWriter;
24   class LLVMContext;
25   class raw_ostream;
26   
27   /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
28   /// and prepare for lazy deserialization of function bodies.  If successful,
29   /// this takes ownership of 'buffer' and returns a non-null pointer.  On
30   /// error, this returns null, *does not* take ownership of Buffer, and fills
31   /// in *ErrMsg with an error description if ErrMsg is non-null.
32   Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
33                                LLVMContext& Context,
34                                std::string *ErrMsg = 0);
35
36   /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
37   /// If an error occurs, this returns null and fills in *ErrMsg if it is
38   /// non-null.  This method *never* takes ownership of Buffer.
39   Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
40                            std::string *ErrMsg = 0);
41
42   /// WriteBitcodeToFile - Write the specified module to the specified
43   /// raw output stream.  For streams where it matters, the given stream
44   /// should be in "binary" mode.
45   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
46
47   /// WriteBitcodeToStream - Write the specified module to the specified
48   /// raw output stream.
49   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
50
51   /// createBitcodeWriterPass - Create and return a pass that writes the module
52   /// to the specified ostream.
53   ModulePass *createBitcodeWriterPass(raw_ostream &Str);
54   
55   
56   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
57   /// for an LLVM IR bitcode wrapper.
58   ///
59   static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
60                                       const unsigned char *BufEnd) {
61     // See if you can find the hidden message in the magic bytes :-).
62     // (Hint: it's a little-endian encoding.)
63     return BufPtr != BufEnd &&
64            BufPtr[0] == 0xDE &&
65            BufPtr[1] == 0xC0 &&
66            BufPtr[2] == 0x17 &&
67            BufPtr[3] == 0x0B;
68   }
69
70   /// isRawBitcode - Return true if the given bytes are the magic bytes for
71   /// raw LLVM IR bitcode (without a wrapper).
72   ///
73   static inline bool isRawBitcode(const unsigned char *BufPtr,
74                                   const unsigned char *BufEnd) {
75     // These bytes sort of have a hidden message, but it's not in
76     // little-endian this time, and it's a little redundant.
77     return BufPtr != BufEnd &&
78            BufPtr[0] == 'B' &&
79            BufPtr[1] == 'C' &&
80            BufPtr[2] == 0xc0 &&
81            BufPtr[3] == 0xde;
82   }
83
84   /// isBitcode - Return true if the given bytes are the magic bytes for
85   /// LLVM IR bitcode, either with or without a wrapper.
86   ///
87   static bool inline isBitcode(const unsigned char *BufPtr,
88                                const unsigned char *BufEnd) {
89     return isBitcodeWrapper(BufPtr, BufEnd) ||
90            isRawBitcode(BufPtr, BufEnd);
91   }
92
93   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
94   /// header for padding or other reasons.  The format of this header is:
95   ///
96   /// struct bc_header {
97   ///   uint32_t Magic;         // 0x0B17C0DE
98   ///   uint32_t Version;       // Version, currently always 0.
99   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
100   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
101   ///   ... potentially other gunk ...
102   /// };
103   /// 
104   /// This function is called when we find a file with a matching magic number.
105   /// In this case, skip down to the subsection of the file that is actually a
106   /// BC file.
107   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
108                                               unsigned char *&BufEnd) {
109     enum {
110       KnownHeaderSize = 4*4,  // Size of header we read.
111       OffsetField = 2*4,      // Offset in bytes to Offset field.
112       SizeField = 3*4         // Offset in bytes to Size field.
113     };
114     
115     // Must contain the header!
116     if (BufEnd-BufPtr < KnownHeaderSize) return true;
117     
118     unsigned Offset = ( BufPtr[OffsetField  ]        |
119                        (BufPtr[OffsetField+1] << 8)  |
120                        (BufPtr[OffsetField+2] << 16) |
121                        (BufPtr[OffsetField+3] << 24));
122     unsigned Size   = ( BufPtr[SizeField    ]        |
123                        (BufPtr[SizeField  +1] << 8)  |
124                        (BufPtr[SizeField  +2] << 16) |
125                        (BufPtr[SizeField  +3] << 24));
126     
127     // Verify that Offset+Size fits in the file.
128     if (Offset+Size > unsigned(BufEnd-BufPtr))
129       return true;
130     BufPtr += Offset;
131     BufEnd = BufPtr+Size;
132     return false;
133   }
134 } // End llvm namespace
135
136 #endif