Remove BitcodeReader::setBufferOwned.
[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_READERWRITER_H
15 #define LLVM_BITCODE_READERWRITER_H
16
17 #include "llvm/Support/ErrorOr.h"
18 #include <string>
19
20 namespace llvm {
21   class BitstreamWriter;
22   class MemoryBuffer;
23   class DataStreamer;
24   class LLVMContext;
25   class Module;
26   class ModulePass;
27   class raw_ostream;
28
29   /// Read the header of the specified bitcode buffer and prepare for lazy
30   /// deserialization of function bodies.  If successful, this takes ownership
31   /// of 'buffer. On error, this *does not* take ownership of Buffer.
32   ErrorOr<Module *> getLazyBitcodeModule(MemoryBuffer *Buffer,
33                                          LLVMContext &Context,
34                                          bool BufferOwned = true);
35
36   /// getStreamedBitcodeModule - Read the header of the specified stream
37   /// and prepare for lazy deserialization and streaming of function bodies.
38   /// On error, this returns null, and fills in *ErrMsg with an error
39   /// description if ErrMsg is non-null.
40   Module *getStreamedBitcodeModule(const std::string &name,
41                                    DataStreamer *streamer,
42                                    LLVMContext &Context,
43                                    std::string *ErrMsg = nullptr);
44
45   /// getBitcodeTargetTriple - Read the header of the specified bitcode
46   /// buffer and extract just the triple information. If successful,
47   /// this returns a string and *does not* take ownership
48   /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
49   /// if ErrMsg is non-null.
50   std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
51                                      LLVMContext &Context,
52                                      std::string *ErrMsg = nullptr);
53
54   /// Read the specified bitcode file, returning the module.
55   /// This method *never* takes ownership of Buffer.
56   ErrorOr<Module *> parseBitcodeFile(MemoryBuffer *Buffer,
57                                      LLVMContext &Context);
58
59   /// WriteBitcodeToFile - Write the specified module to the specified
60   /// raw output stream.  For streams where it matters, the given stream
61   /// should be in "binary" mode.
62   void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
63
64
65   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
66   /// for an LLVM IR bitcode wrapper.
67   ///
68   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   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   inline bool 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   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
117   /// contain the whole bitcode file.
118   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
119                                        const unsigned char *&BufEnd,
120                                        bool VerifyBufferSize) {
121     enum {
122       KnownHeaderSize = 4*4,  // Size of header we read.
123       OffsetField = 2*4,      // Offset in bytes to Offset field.
124       SizeField = 3*4         // Offset in bytes to Size field.
125     };
126
127     // Must contain the header!
128     if (BufEnd-BufPtr < KnownHeaderSize) return true;
129
130     unsigned Offset = ( BufPtr[OffsetField  ]        |
131                        (BufPtr[OffsetField+1] << 8)  |
132                        (BufPtr[OffsetField+2] << 16) |
133                        (BufPtr[OffsetField+3] << 24));
134     unsigned Size   = ( BufPtr[SizeField    ]        |
135                        (BufPtr[SizeField  +1] << 8)  |
136                        (BufPtr[SizeField  +2] << 16) |
137                        (BufPtr[SizeField  +3] << 24));
138
139     // Verify that Offset+Size fits in the file.
140     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
141       return true;
142     BufPtr += Offset;
143     BufEnd = BufPtr+Size;
144     return false;
145   }
146 } // End llvm namespace
147
148 #endif