Per discussion on the list, remove BitcodeVerify pass to reimplement as a free function.
[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   /// getBitcodeTargetTriple - Read the header of the specified bitcode
37   /// buffer and extract just the triple information. If successful,
38   /// this returns a string and *does not* take ownership
39   /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
40   /// if ErrMsg is non-null.
41   std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
42                                      LLVMContext& Context,
43                                      std::string *ErrMsg = 0);
44
45   /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
46   /// If an error occurs, this returns null and fills in *ErrMsg if it is
47   /// non-null.  This method *never* takes ownership of Buffer.
48   Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
49                            std::string *ErrMsg = 0);
50
51   /// WriteBitcodeToFile - Write the specified module to the specified
52   /// raw output stream.  For streams where it matters, the given stream
53   /// should be in "binary" mode.
54   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
55
56   /// WriteBitcodeToStream - Write the specified module to the specified
57   /// raw output stream.
58   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
59
60   /// createBitcodeWriterPass - Create and return a pass that writes the module
61   /// to the specified ostream.
62   ModulePass *createBitcodeWriterPass(raw_ostream &Str);
63   
64   
65   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
66   /// for an LLVM IR bitcode wrapper.
67   ///
68   static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
69                                       const unsigned char *BufEnd) {
70     // See if you can find the hidden message in the magic bytes :-).
71     // (Hint: it's a little-endian encoding.)
72     return BufPtr != BufEnd &&
73            BufPtr[0] == 0xDE &&
74            BufPtr[1] == 0xC0 &&
75            BufPtr[2] == 0x17 &&
76            BufPtr[3] == 0x0B;
77   }
78
79   /// isRawBitcode - Return true if the given bytes are the magic bytes for
80   /// raw LLVM IR bitcode (without a wrapper).
81   ///
82   static inline bool isRawBitcode(const unsigned char *BufPtr,
83                                   const unsigned char *BufEnd) {
84     // These bytes sort of have a hidden message, but it's not in
85     // little-endian this time, and it's a little redundant.
86     return BufPtr != BufEnd &&
87            BufPtr[0] == 'B' &&
88            BufPtr[1] == 'C' &&
89            BufPtr[2] == 0xc0 &&
90            BufPtr[3] == 0xde;
91   }
92
93   /// isBitcode - Return true if the given bytes are the magic bytes for
94   /// LLVM IR bitcode, either with or without a wrapper.
95   ///
96   static bool inline isBitcode(const unsigned char *BufPtr,
97                                const unsigned char *BufEnd) {
98     return isBitcodeWrapper(BufPtr, BufEnd) ||
99            isRawBitcode(BufPtr, BufEnd);
100   }
101
102   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
103   /// header for padding or other reasons.  The format of this header is:
104   ///
105   /// struct bc_header {
106   ///   uint32_t Magic;         // 0x0B17C0DE
107   ///   uint32_t Version;       // Version, currently always 0.
108   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
109   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
110   ///   ... potentially other gunk ...
111   /// };
112   /// 
113   /// This function is called when we find a file with a matching magic number.
114   /// In this case, skip down to the subsection of the file that is actually a
115   /// BC file.
116   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
117                                               unsigned char *&BufEnd) {
118     enum {
119       KnownHeaderSize = 4*4,  // Size of header we read.
120       OffsetField = 2*4,      // Offset in bytes to Offset field.
121       SizeField = 3*4         // Offset in bytes to Size field.
122     };
123     
124     // Must contain the header!
125     if (BufEnd-BufPtr < KnownHeaderSize) return true;
126     
127     unsigned Offset = ( BufPtr[OffsetField  ]        |
128                        (BufPtr[OffsetField+1] << 8)  |
129                        (BufPtr[OffsetField+2] << 16) |
130                        (BufPtr[OffsetField+3] << 24));
131     unsigned Size   = ( BufPtr[SizeField    ]        |
132                        (BufPtr[SizeField  +1] << 8)  |
133                        (BufPtr[SizeField  +2] << 16) |
134                        (BufPtr[SizeField  +3] << 24));
135     
136     // Verify that Offset+Size fits in the file.
137     if (Offset+Size > unsigned(BufEnd-BufPtr))
138       return true;
139     BufPtr += Offset;
140     BufEnd = BufPtr+Size;
141     return false;
142   }
143 } // End llvm namespace
144
145 #endif