1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/User.h"
20 #include "llvm/Bitcode/BitstreamReader.h"
21 #include "llvm/Bitcode/LLVMBitCodes.h"
22 #include "llvm/ADT/DenseMap.h"
26 class BitstreamReader;
29 class BitcodeReaderValueList : public User {
30 std::vector<Use> Uses;
32 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
34 // vector compatibility methods
35 unsigned size() const { return getNumOperands(); }
36 void push_back(Value *V) {
37 Uses.push_back(Use(V, this));
38 OperandList = &Uses[0];
42 Value *operator[](unsigned i) const { return getOperand(i); }
44 Value *back() const { return Uses.back(); }
45 void pop_back() { Uses.pop_back(); --NumOperands; }
46 bool empty() const { return NumOperands == 0; }
47 void shrinkTo(unsigned N) {
48 assert(N < NumOperands && "Invalid shrinkTo request!");
52 virtual void print(std::ostream&) const {}
54 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
55 void initVal(unsigned Idx, Value *V) {
56 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
57 Uses[Idx].init(V, this);
62 class BitcodeReader : public ModuleProvider {
64 BitstreamReader Stream;
66 const char *ErrorString;
68 std::vector<PATypeHolder> TypeList;
69 BitcodeReaderValueList ValueList;
70 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
71 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
73 // When reading the module header, this list is populated with functions that
74 // have bodies later in the file.
75 std::vector<Function*> FunctionsWithBodies;
77 // After the module header has been read, the FunctionsWithBodies list is
78 // reversed. This keeps track of whether we've done this yet.
79 bool HasReversedFunctionsWithBodies;
81 /// DeferredFunctionInfo - When function bodies are initially scanned, this
82 /// map contains info about where to find deferred function body (in the
83 /// stream) and what linkage the original function had.
84 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
86 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
87 HasReversedFunctionsWithBodies = false;
92 /// releaseMemoryBuffer - This causes the reader to completely forget about
93 /// the memory buffer it contains, which prevents the buffer from being
94 /// destroyed when it is deleted.
95 void releaseMemoryBuffer() {
99 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
101 virtual Module *materializeModule(std::string *ErrInfo = 0) {
103 //if (ParseAllFunctionBodies(ErrMsg))
108 bool Error(const char *Str) {
112 const char *getErrorString() const { return ErrorString; }
114 /// @brief Main interface to parsing a bitcode buffer.
115 /// @returns true if an error occurred.
118 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
120 bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
121 bool ParseTypeTable(BitstreamReader &Stream);
122 bool ParseTypeSymbolTable(BitstreamReader &Stream);
123 bool ParseValueSymbolTable(BitstreamReader &Stream);
124 bool ParseConstants(BitstreamReader &Stream);
125 bool ParseFunction(BitstreamReader &Stream);
126 bool ResolveGlobalAndAliasInits();
129 } // End llvm namespace