1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines interfaces to read and write LLVM bitcode files/streams.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BITCODE_H
15 #define LLVM_BITCODE_H
23 class BitstreamWriter;
27 /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
28 /// and prepare for lazy deserialization of function bodies. If successful,
29 /// this takes ownership of 'buffer' and returns a non-null pointer. On
30 /// error, this returns null, *does not* take ownership of Buffer, and fills
31 /// in *ErrMsg with an error description if ErrMsg is non-null.
32 Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
34 std::string *ErrMsg = 0);
36 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
37 /// If an error occurs, this returns null and fills in *ErrMsg if it is
38 /// non-null. This method *never* takes ownership of Buffer.
39 Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
40 std::string *ErrMsg = 0);
42 /// WriteBitcodeToFile - Write the specified module to the specified
43 /// raw output stream.
44 void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
46 /// WriteBitcodeToStream - Write the specified module to the specified
47 /// raw output stream.
48 void WriteBitcodeToStream(const Module *M, BitstreamWriter &Stream);
50 /// createBitcodeWriterPass - Create and return a pass that writes the module
51 /// to the specified ostream.
52 ModulePass *createBitcodeWriterPass(raw_ostream &Str);
55 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
56 /// for an LLVM IR bitcode wrapper.
58 static inline bool isBitcodeWrapper(const unsigned char *BufPtr,
59 const unsigned char *BufEnd) {
60 // See if you can find the hidden message in the magic bytes :-).
61 // (Hint: it's a little-endian encoding.)
62 return BufPtr != BufEnd &&
69 /// isRawBitcode - Return true if the given bytes are the magic bytes for
70 /// raw LLVM IR bitcode (without a wrapper).
72 static inline bool isRawBitcode(const unsigned char *BufPtr,
73 const unsigned char *BufEnd) {
74 // These bytes sort of have a hidden message, but it's not in
75 // little-endian this time, and it's a little redundant.
76 return BufPtr != BufEnd &&
83 /// isBitcode - Return true if the given bytes are the magic bytes for
84 /// LLVM IR bitcode, either with or without a wrapper.
86 static bool inline isBitcode(const unsigned char *BufPtr,
87 const unsigned char *BufEnd) {
88 return isBitcodeWrapper(BufPtr, BufEnd) ||
89 isRawBitcode(BufPtr, BufEnd);
92 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
93 /// header for padding or other reasons. The format of this header is:
95 /// struct bc_header {
96 /// uint32_t Magic; // 0x0B17C0DE
97 /// uint32_t Version; // Version, currently always 0.
98 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
99 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
100 /// ... potentially other gunk ...
103 /// This function is called when we find a file with a matching magic number.
104 /// In this case, skip down to the subsection of the file that is actually a
106 static inline bool SkipBitcodeWrapperHeader(unsigned char *&BufPtr,
107 unsigned char *&BufEnd) {
109 KnownHeaderSize = 4*4, // Size of header we read.
110 OffsetField = 2*4, // Offset in bytes to Offset field.
111 SizeField = 3*4 // Offset in bytes to Size field.
114 // Must contain the header!
115 if (BufEnd-BufPtr < KnownHeaderSize) return true;
117 unsigned Offset = ( BufPtr[OffsetField ] |
118 (BufPtr[OffsetField+1] << 8) |
119 (BufPtr[OffsetField+2] << 16) |
120 (BufPtr[OffsetField+3] << 24));
121 unsigned Size = ( BufPtr[SizeField ] |
122 (BufPtr[SizeField +1] << 8) |
123 (BufPtr[SizeField +2] << 16) |
124 (BufPtr[SizeField +3] << 24));
126 // Verify that Offset+Size fits in the file.
127 if (Offset+Size > unsigned(BufEnd-BufPtr))
130 BufEnd = BufPtr+Size;
133 } // End llvm namespace