Begin sketching out a bitcode verifier pass. Idea is to emit a .bc file and
[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   /// createBitcodeVerifierPass - Create a pass that writes a module to disk and
65   /// then reads the module back in to verify bitcode serialization and
66   /// deserialization.
67   ModulePass *createBitcodeVerifierPass(raw_ostream &Str);
68
69   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
70   /// for an LLVM IR bitcode wrapper.
71   ///
72   static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
73                                       const unsigned char *BufEnd) {
74     // See if you can find the hidden message in the magic bytes :-).
75     // (Hint: it's a little-endian encoding.)
76     return BufPtr != BufEnd &&
77            BufPtr[0] == 0xDE &&
78            BufPtr[1] == 0xC0 &&
79            BufPtr[2] == 0x17 &&
80            BufPtr[3] == 0x0B;
81   }
82
83   /// isRawBitcode - Return true if the given bytes are the magic bytes for
84   /// raw LLVM IR bitcode (without a wrapper).
85   ///
86   static inline bool isRawBitcode(const unsigned char *BufPtr,
87                                   const unsigned char *BufEnd) {
88     // These bytes sort of have a hidden message, but it's not in
89     // little-endian this time, and it's a little redundant.
90     return BufPtr != BufEnd &&
91            BufPtr[0] == 'B' &&
92            BufPtr[1] == 'C' &&
93            BufPtr[2] == 0xc0 &&
94            BufPtr[3] == 0xde;
95   }
96
97   /// isBitcode - Return true if the given bytes are the magic bytes for
98   /// LLVM IR bitcode, either with or without a wrapper.
99   ///
100   static bool inline isBitcode(const unsigned char *BufPtr,
101                                const unsigned char *BufEnd) {
102     return isBitcodeWrapper(BufPtr, BufEnd) ||
103            isRawBitcode(BufPtr, BufEnd);
104   }
105
106   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
107   /// header for padding or other reasons.  The format of this header is:
108   ///
109   /// struct bc_header {
110   ///   uint32_t Magic;         // 0x0B17C0DE
111   ///   uint32_t Version;       // Version, currently always 0.
112   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
113   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
114   ///   ... potentially other gunk ...
115   /// };
116   /// 
117   /// This function is called when we find a file with a matching magic number.
118   /// In this case, skip down to the subsection of the file that is actually a
119   /// BC file.
120   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
121                                               unsigned char *&BufEnd) {
122     enum {
123       KnownHeaderSize = 4*4,  // Size of header we read.
124       OffsetField = 2*4,      // Offset in bytes to Offset field.
125       SizeField = 3*4         // Offset in bytes to Size field.
126     };
127     
128     // Must contain the header!
129     if (BufEnd-BufPtr < KnownHeaderSize) return true;
130     
131     unsigned Offset = ( BufPtr[OffsetField  ]        |
132                        (BufPtr[OffsetField+1] << 8)  |
133                        (BufPtr[OffsetField+2] << 16) |
134                        (BufPtr[OffsetField+3] << 24));
135     unsigned Size   = ( BufPtr[SizeField    ]        |
136                        (BufPtr[SizeField  +1] << 8)  |
137                        (BufPtr[SizeField  +2] << 16) |
138                        (BufPtr[SizeField  +3] << 24));
139     
140     // Verify that Offset+Size fits in the file.
141     if (Offset+Size > unsigned(BufEnd-BufPtr))
142       return true;
143     BufPtr += Offset;
144     BufEnd = BufPtr+Size;
145     return false;
146   }
147 } // End llvm namespace
148
149 #endif