Kill ModuleProvider and ghost linkage by inverting the relationship between
[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.
44   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
45
46   /// WriteBitcodeToStream - Write the specified module to the specified
47   /// raw output stream.
48   void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
49
50   /// createBitcodeWriterPass - Create and return a pass that writes the module
51   /// to the specified ostream.
52   ModulePass *createBitcodeWriterPass(raw_ostream &Str);
53   
54   
55   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
56   /// for an LLVM IR bitcode wrapper.
57   ///
58   static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
59                                       const unsigned char *BufEnd) {
60     // See if you can find the hidden message in the magic bytes :-).
61     // (Hint: it's a little-endian encoding.)
62     return BufPtr != BufEnd &&
63            BufPtr[0] == 0xDE &&
64            BufPtr[1] == 0xC0 &&
65            BufPtr[2] == 0x17 &&
66            BufPtr[3] == 0x0B;
67   }
68
69   /// isRawBitcode - Return true if the given bytes are the magic bytes for
70   /// raw LLVM IR bitcode (without a wrapper).
71   ///
72   static inline bool isRawBitcode(const unsigned char *BufPtr,
73                                   const unsigned char *BufEnd) {
74     // These bytes sort of have a hidden message, but it's not in
75     // little-endian this time, and it's a little redundant.
76     return BufPtr != BufEnd &&
77            BufPtr[0] == 'B' &&
78            BufPtr[1] == 'C' &&
79            BufPtr[2] == 0xc0 &&
80            BufPtr[3] == 0xde;
81   }
82
83   /// isBitcode - Return true if the given bytes are the magic bytes for
84   /// LLVM IR bitcode, either with or without a wrapper.
85   ///
86   static bool inline isBitcode(const unsigned char *BufPtr,
87                                const unsigned char *BufEnd) {
88     return isBitcodeWrapper(BufPtr, BufEnd) ||
89            isRawBitcode(BufPtr, BufEnd);
90   }
91
92   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
93   /// header for padding or other reasons.  The format of this header is:
94   ///
95   /// struct bc_header {
96   ///   uint32_t Magic;         // 0x0B17C0DE
97   ///   uint32_t Version;       // Version, currently always 0.
98   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
99   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
100   ///   ... potentially other gunk ...
101   /// };
102   /// 
103   /// This function is called when we find a file with a matching magic number.
104   /// In this case, skip down to the subsection of the file that is actually a
105   /// BC file.
106   static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
107                                               unsigned char *&BufEnd) {
108     enum {
109       KnownHeaderSize = 4*4,  // Size of header we read.
110       OffsetField = 2*4,      // Offset in bytes to Offset field.
111       SizeField = 3*4         // Offset in bytes to Size field.
112     };
113     
114     // Must contain the header!
115     if (BufEnd-BufPtr < KnownHeaderSize) return true;
116     
117     unsigned Offset = ( BufPtr[OffsetField  ]        |
118                        (BufPtr[OffsetField+1] << 8)  |
119                        (BufPtr[OffsetField+2] << 16) |
120                        (BufPtr[OffsetField+3] << 24));
121     unsigned Size   = ( BufPtr[SizeField    ]        |
122                        (BufPtr[SizeField  +1] << 8)  |
123                        (BufPtr[SizeField  +2] << 16) |
124                        (BufPtr[SizeField  +3] << 24));
125     
126     // Verify that Offset+Size fits in the file.
127     if (Offset+Size > unsigned(BufEnd-BufPtr))
128       return true;
129     BufPtr += Offset;
130     BufEnd = BufPtr+Size;
131     return false;
132   }
133 } // End llvm namespace
134
135 #endif