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