1193323Sed//===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This header defines interfaces to read and write LLVM bitcode files/streams.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14249423Sdim#ifndef LLVM_BITCODE_READERWRITER_H
15249423Sdim#define LLVM_BITCODE_READERWRITER_H
16193323Sed
17193323Sed#include <string>
18193323Sed
19193323Sednamespace llvm {
20234353Sdim  class BitstreamWriter;
21234353Sdim  class MemoryBuffer;
22234353Sdim  class DataStreamer;
23234353Sdim  class LLVMContext;
24193323Sed  class Module;
25193323Sed  class ModulePass;
26193323Sed  class raw_ostream;
27234353Sdim
28203954Srdivacky  /// getLazyBitcodeModule - Read the header of the specified bitcode buffer
29193323Sed  /// and prepare for lazy deserialization of function bodies.  If successful,
30193323Sed  /// this takes ownership of 'buffer' and returns a non-null pointer.  On
31193323Sed  /// error, this returns null, *does not* take ownership of Buffer, and fills
32193323Sed  /// in *ErrMsg with an error description if ErrMsg is non-null.
33203954Srdivacky  Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
34234353Sdim                               LLVMContext &Context,
35203954Srdivacky                               std::string *ErrMsg = 0);
36193323Sed
37234353Sdim  /// getStreamedBitcodeModule - Read the header of the specified stream
38234353Sdim  /// and prepare for lazy deserialization and streaming of function bodies.
39234353Sdim  /// On error, this returns null, and fills in *ErrMsg with an error
40234353Sdim  /// description if ErrMsg is non-null.
41234353Sdim  Module *getStreamedBitcodeModule(const std::string &name,
42234353Sdim                                   DataStreamer *streamer,
43234353Sdim                                   LLVMContext &Context,
44234353Sdim                                   std::string *ErrMsg = 0);
45234353Sdim
46218893Sdim  /// getBitcodeTargetTriple - Read the header of the specified bitcode
47218893Sdim  /// buffer and extract just the triple information. If successful,
48218893Sdim  /// this returns a string and *does not* take ownership
49218893Sdim  /// of 'buffer'. On error, this returns "", and fills in *ErrMsg
50218893Sdim  /// if ErrMsg is non-null.
51218893Sdim  std::string getBitcodeTargetTriple(MemoryBuffer *Buffer,
52234353Sdim                                     LLVMContext &Context,
53218893Sdim                                     std::string *ErrMsg = 0);
54218893Sdim
55193323Sed  /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
56193323Sed  /// If an error occurs, this returns null and fills in *ErrMsg if it is
57193323Sed  /// non-null.  This method *never* takes ownership of Buffer.
58234353Sdim  Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context,
59195340Sed                           std::string *ErrMsg = 0);
60193323Sed
61193323Sed  /// WriteBitcodeToFile - Write the specified module to the specified
62210299Sed  /// raw output stream.  For streams where it matters, the given stream
63210299Sed  /// should be in "binary" mode.
64193323Sed  void WriteBitcodeToFile(const Module *M, raw_ostream &Out);
65193323Sed
66193323Sed  /// createBitcodeWriterPass - Create and return a pass that writes the module
67193323Sed  /// to the specified ostream.
68193323Sed  ModulePass *createBitcodeWriterPass(raw_ostream &Str);
69234353Sdim
70234353Sdim
71198090Srdivacky  /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
72198090Srdivacky  /// for an LLVM IR bitcode wrapper.
73198090Srdivacky  ///
74239462Sdim  inline bool isBitcodeWrapper(const unsigned char *BufPtr,
75239462Sdim                               const unsigned char *BufEnd) {
76198090Srdivacky    // See if you can find the hidden message in the magic bytes :-).
77198090Srdivacky    // (Hint: it's a little-endian encoding.)
78198090Srdivacky    return BufPtr != BufEnd &&
79198090Srdivacky           BufPtr[0] == 0xDE &&
80198090Srdivacky           BufPtr[1] == 0xC0 &&
81198090Srdivacky           BufPtr[2] == 0x17 &&
82198090Srdivacky           BufPtr[3] == 0x0B;
83193323Sed  }
84198090Srdivacky
85198090Srdivacky  /// isRawBitcode - Return true if the given bytes are the magic bytes for
86198090Srdivacky  /// raw LLVM IR bitcode (without a wrapper).
87198090Srdivacky  ///
88239462Sdim  inline bool isRawBitcode(const unsigned char *BufPtr,
89239462Sdim                           const unsigned char *BufEnd) {
90198090Srdivacky    // These bytes sort of have a hidden message, but it's not in
91198090Srdivacky    // little-endian this time, and it's a little redundant.
92198090Srdivacky    return BufPtr != BufEnd &&
93198090Srdivacky           BufPtr[0] == 'B' &&
94198090Srdivacky           BufPtr[1] == 'C' &&
95198090Srdivacky           BufPtr[2] == 0xc0 &&
96198090Srdivacky           BufPtr[3] == 0xde;
97198090Srdivacky  }
98198090Srdivacky
99198090Srdivacky  /// isBitcode - Return true if the given bytes are the magic bytes for
100198090Srdivacky  /// LLVM IR bitcode, either with or without a wrapper.
101198090Srdivacky  ///
102239462Sdim  inline bool isBitcode(const unsigned char *BufPtr,
103239462Sdim                        const unsigned char *BufEnd) {
104198090Srdivacky    return isBitcodeWrapper(BufPtr, BufEnd) ||
105198090Srdivacky           isRawBitcode(BufPtr, BufEnd);
106198090Srdivacky  }
107198090Srdivacky
108193323Sed  /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
109193323Sed  /// header for padding or other reasons.  The format of this header is:
110193323Sed  ///
111193323Sed  /// struct bc_header {
112193323Sed  ///   uint32_t Magic;         // 0x0B17C0DE
113193323Sed  ///   uint32_t Version;       // Version, currently always 0.
114193323Sed  ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
115193323Sed  ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
116193323Sed  ///   ... potentially other gunk ...
117193323Sed  /// };
118234353Sdim  ///
119193323Sed  /// This function is called when we find a file with a matching magic number.
120193323Sed  /// In this case, skip down to the subsection of the file that is actually a
121193323Sed  /// BC file.
122234353Sdim  /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
123234353Sdim  /// contain the whole bitcode file.
124239462Sdim  inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
125239462Sdim                                       const unsigned char *&BufEnd,
126239462Sdim                                       bool VerifyBufferSize) {
127193323Sed    enum {
128193323Sed      KnownHeaderSize = 4*4,  // Size of header we read.
129193323Sed      OffsetField = 2*4,      // Offset in bytes to Offset field.
130193323Sed      SizeField = 3*4         // Offset in bytes to Size field.
131193323Sed    };
132234353Sdim
133193323Sed    // Must contain the header!
134193323Sed    if (BufEnd-BufPtr < KnownHeaderSize) return true;
135234353Sdim
136193323Sed    unsigned Offset = ( BufPtr[OffsetField  ]        |
137193323Sed                       (BufPtr[OffsetField+1] << 8)  |
138193323Sed                       (BufPtr[OffsetField+2] << 16) |
139193323Sed                       (BufPtr[OffsetField+3] << 24));
140193323Sed    unsigned Size   = ( BufPtr[SizeField    ]        |
141193323Sed                       (BufPtr[SizeField  +1] << 8)  |
142193323Sed                       (BufPtr[SizeField  +2] << 16) |
143193323Sed                       (BufPtr[SizeField  +3] << 24));
144234353Sdim
145193323Sed    // Verify that Offset+Size fits in the file.
146234353Sdim    if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
147193323Sed      return true;
148193323Sed    BufPtr += Offset;
149193323Sed    BufEnd = BufPtr+Size;
150193323Sed    return false;
151193323Sed  }
152193323Sed} // End llvm namespace
153193323Sed
154193323Sed#endif
155