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