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