ReaderWriter_2_9.h revision 4cc499d6e5ec602309501873449c938af61170b2
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_2_9_H
15#define LLVM_BITCODE_2_9_H
16
17#include <string>
18
19namespace llvm {
20  class Module;
21  class MemoryBuffer;
22  class ModulePass;
23  class BitstreamWriter;
24  class LLVMContext;
25  class raw_ostream;
26} // End llvm namespace
27
28namespace llvm_2_9 {
29  /// getLazyBitcodeModule - 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  llvm::Module *getLazyBitcodeModule(llvm::MemoryBuffer *Buffer,
35                               llvm::LLVMContext& Context,
36                               std::string *ErrMsg = 0);
37
38  /// getBitcodeTargetTriple - Read the header of the specified bitcode
39  /// buffer and extract just the triple information. If successful,
40  /// this returns a string and *does not* take ownership
41  /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
42  /// if ErrMsg is non-null.
43  std::string getBitcodeTargetTriple(llvm::MemoryBuffer *Buffer,
44                                     llvm::LLVMContext& Context,
45                                     std::string *ErrMsg = 0);
46
47  /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
48  /// If an error occurs, this returns null and fills in *ErrMsg if it is
49  /// non-null.  This method *never* takes ownership of Buffer.
50  llvm::Module *ParseBitcodeFile(llvm::MemoryBuffer *Buffer, llvm::LLVMContext& Context,
51                           std::string *ErrMsg = 0);
52
53  /// WriteBitcodeToFile - Write the specified module to the specified
54  /// raw output stream.  For streams where it matters, the given stream
55  /// should be in "binary" mode.
56  void WriteBitcodeToFile(const llvm::Module *M, llvm::raw_ostream &Out);
57
58  /// WriteBitcodeToStream - Write the specified module to the specified
59  /// raw output stream.
60  void WriteBitcodeToStream(const llvm::Module *M, llvm::BitstreamWriter &Stream);
61
62  /// createBitcodeWriterPass - Create and return a pass that writes the module
63  /// to the specified ostream.
64  llvm::ModulePass *createBitcodeWriterPass(llvm::raw_ostream &Str);
65
66
67  /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
68  /// for an LLVM IR bitcode wrapper.
69  ///
70  static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
71                                      const unsigned char *BufEnd) {
72    // See if you can find the hidden message in the magic bytes :-).
73    // (Hint: it's a little-endian encoding.)
74    return BufPtr != BufEnd &&
75           BufPtr[0] == 0xDE &&
76           BufPtr[1] == 0xC0 &&
77           BufPtr[2] == 0x17 &&
78           BufPtr[3] == 0x0B;
79  }
80
81  /// isRawBitcode - Return true if the given bytes are the magic bytes for
82  /// raw LLVM IR bitcode (without a wrapper).
83  ///
84  static inline bool isRawBitcode(const unsigned char *BufPtr,
85                                  const unsigned char *BufEnd) {
86    // These bytes sort of have a hidden message, but it's not in
87    // little-endian this time, and it's a little redundant.
88    return BufPtr != BufEnd &&
89           BufPtr[0] == 'B' &&
90           BufPtr[1] == 'C' &&
91           BufPtr[2] == 0xc0 &&
92           BufPtr[3] == 0xde;
93  }
94
95  /// isBitcode - Return true if the given bytes are the magic bytes for
96  /// LLVM IR bitcode, either with or without a wrapper.
97  ///
98  static bool inline isBitcode(const unsigned char *BufPtr,
99                               const unsigned char *BufEnd) {
100    return isBitcodeWrapper(BufPtr, BufEnd) ||
101           isRawBitcode(BufPtr, BufEnd);
102  }
103
104  /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
105  /// header for padding or other reasons.  The format of this header is:
106  ///
107  /// struct bc_header {
108  ///   uint32_t Magic;         // 0x0B17C0DE
109  ///   uint32_t Version;       // Version, currently always 0.
110  ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
111  ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
112  ///   ... potentially other gunk ...
113  /// };
114  ///
115  /// This function is called when we find a file with a matching magic number.
116  /// In this case, skip down to the subsection of the file that is actually a
117  /// BC file.
118  static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
119                                              unsigned char *&BufEnd) {
120    enum {
121      KnownHeaderSize = 4*4,  // Size of header we read.
122      OffsetField = 2*4,      // Offset in bytes to Offset field.
123      SizeField = 3*4         // Offset in bytes to Size field.
124    };
125
126    // Must contain the header!
127    if (BufEnd-BufPtr < KnownHeaderSize) return true;
128
129    unsigned Offset = ( BufPtr[OffsetField  ]        |
130                       (BufPtr[OffsetField+1] << 8)  |
131                       (BufPtr[OffsetField+2] << 16) |
132                       (BufPtr[OffsetField+3] << 24));
133    unsigned Size   = ( BufPtr[SizeField    ]        |
134                       (BufPtr[SizeField  +1] << 8)  |
135                       (BufPtr[SizeField  +2] << 16) |
136                       (BufPtr[SizeField  +3] << 24));
137
138    // Verify that Offset+Size fits in the file.
139    if (Offset+Size > unsigned(BufEnd-BufPtr))
140      return true;
141    BufPtr += Offset;
142    BufEnd = BufPtr+Size;
143    return false;
144  }
145} // End llvm_2_9 namespace
146
147#endif
148