Teach llvm-bcanalyzer to skip over the header we use on LLVM IR files.
[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 <iosfwd>
18 #include <string>
19
20 namespace llvm {
21   class Module;
22   class ModuleProvider;
23   class MemoryBuffer;
24   class ModulePass;
25   class BitstreamWriter;
26   class raw_ostream;
27   
28   /// getBitcodeModuleProvider - Read the header of the specified bitcode buffer
29   /// and prepare for lazy deserialization of function bodies.  If successful,
30   /// this takes ownership of 'buffer' and returns a non-null pointer.  On
31   /// error, this returns null, *does not* take ownership of Buffer, and fills
32   /// in *ErrMsg with an error description if ErrMsg is non-null.
33   ModuleProvider *getBitcodeModuleProvider(MemoryBuffer *Buffer,
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, std::string *ErrMsg = 0);
40
41   /// WriteBitcodeToFile - Write the specified module to the specified output
42   /// stream.
43   void WriteBitcodeToFile(const Module *M, std::ostream &Out);
44
45   /// WriteBitcodeToFile - Write the specified module to the specified
46   /// raw output stream.
47   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
48
49   /// WriteBitcodeToStream - Write the specified module to the specified
50   /// raw output stream.
51   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
52
53   /// CreateBitcodeWriterPass - Create and return a pass that writes the module
54   /// to the specified ostream.
55   ModulePass *CreateBitcodeWriterPass(std::ostream &Str);
56
57   /// createBitcodeWriterPass - Create and return a pass that writes the module
58   /// to the specified ostream.
59   ModulePass *createBitcodeWriterPass(raw_ostream &Str);
60   
61   
62   /// isBitcodeWrapper - Return true fi this is a wrapper for LLVM IR bitcode
63   /// files.
64   static bool inline isBitcodeWrapper(unsigned char *BufPtr,
65                                       unsigned char *BufEnd) {
66     return (BufPtr != BufEnd && BufPtr[0] == 0xDE && BufPtr[1] == 0xC0 && 
67             BufPtr[2] == 0x17 && BufPtr[3] == 0x0B);
68   }
69   
70   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
71   /// header for padding or other reasons.  The format of this header is:
72   ///
73   /// struct bc_header {
74   ///   uint32_t Magic;         // 0x0B17C0DE
75   ///   uint32_t Version;       // Version, currently always 0.
76   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
77   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
78   ///   ... potentially other gunk ...
79   /// };
80   /// 
81   /// This function is called when we find a file with a matching magic number.
82   /// In this case, skip down to the subsection of the file that is actually a
83   /// BC file.
84   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
85                                               unsigned char *&BufEnd) {
86     enum {
87       KnownHeaderSize = 4*4,  // Size of header we read.
88       OffsetField = 2*4,      // Offset in bytes to Offset field.
89       SizeField = 3*4         // Offset in bytes to Size field.
90     };
91     
92     // Must contain the header!
93     if (BufEnd-BufPtr < KnownHeaderSize) return true;
94     
95     unsigned Offset = ( BufPtr[OffsetField  ]        |
96                        (BufPtr[OffsetField+1] << 8)  |
97                        (BufPtr[OffsetField+2] << 16) |
98                        (BufPtr[OffsetField+3] << 24));
99     unsigned Size   = ( BufPtr[SizeField    ]        |
100                        (BufPtr[SizeField  +1] << 8)  |
101                        (BufPtr[SizeField  +2] << 16) |
102                        (BufPtr[SizeField  +3] << 24));
103     
104     // Verify that Offset+Size fits in the file.
105     if (Offset+Size > unsigned(BufEnd-BufPtr))
106       return true;
107     BufPtr += Offset;
108     BufEnd = BufPtr+Size;
109     return false;
110   }
111 } // End llvm namespace
112
113 #endif