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"
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 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
57 void AssignValue(Value *V, unsigned Idx) {
60 } else if (Value *OldV = getOperand(Idx)) {
61 // If there was a forward reference to this value, replace it.
63 OldV->replaceAllUsesWith(V);
71 void initVal(unsigned Idx, Value *V) {
72 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
73 Uses[Idx].init(V, this);
78 class BitcodeReader : public ModuleProvider {
80 BitstreamReader Stream;
82 const char *ErrorString;
84 std::vector<PATypeHolder> TypeList;
85 BitcodeReaderValueList ValueList;
86 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
87 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
89 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
90 /// file is for null, and is thus not represented here. As such all indices
92 std::vector<const ParamAttrsList*> ParamAttrs;
94 /// FunctionBBs - While parsing a function body, this is a list of the basic
95 /// blocks for the function.
96 std::vector<BasicBlock*> FunctionBBs;
98 // When reading the module header, this list is populated with functions that
99 // have bodies later in the file.
100 std::vector<Function*> FunctionsWithBodies;
102 // After the module header has been read, the FunctionsWithBodies list is
103 // reversed. This keeps track of whether we've done this yet.
104 bool HasReversedFunctionsWithBodies;
106 /// DeferredFunctionInfo - When function bodies are initially scanned, this
107 /// map contains info about where to find deferred function body (in the
108 /// stream) and what linkage the original function had.
109 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
111 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
112 HasReversedFunctionsWithBodies = false;
117 /// releaseMemoryBuffer - This causes the reader to completely forget about
118 /// the memory buffer it contains, which prevents the buffer from being
119 /// destroyed when it is deleted.
120 void releaseMemoryBuffer() {
124 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
125 virtual Module *materializeModule(std::string *ErrInfo = 0);
127 bool Error(const char *Str) {
131 const char *getErrorString() const { return ErrorString; }
133 /// @brief Main interface to parsing a bitcode buffer.
134 /// @returns true if an error occurred.
137 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
138 Value *getFnValueByID(unsigned ID, const Type *Ty) {
139 return ValueList.getValueFwdRef(ID, Ty);
141 BasicBlock *getBasicBlock(unsigned ID) const {
142 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
143 return FunctionBBs[ID];
145 const ParamAttrsList *getParamAttrs(unsigned i) const {
146 if (i-1 < ParamAttrs.size())
147 return ParamAttrs[i-1];
151 /// getValueTypePair - Read a value/type pair out of the specified record from
152 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
153 /// Return true on failure.
154 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
155 unsigned InstNum, Value *&ResVal) {
156 if (Slot == Record.size()) return true;
157 unsigned ValNo = (unsigned)Record[Slot++];
158 if (ValNo < InstNum) {
159 // If this is not a forward reference, just return the value we already
161 ResVal = getFnValueByID(ValNo, 0);
163 } else if (Slot == Record.size()) {
167 unsigned TypeNo = (unsigned)Record[Slot++];
168 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
171 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
172 const Type *Ty, Value *&ResVal) {
173 if (Slot == Record.size()) return true;
174 unsigned ValNo = (unsigned)Record[Slot++];
175 ResVal = getFnValueByID(ValNo, Ty);
180 bool ParseModule(const std::string &ModuleID);
181 bool ParseParamAttrBlock();
182 bool ParseTypeTable();
183 bool ParseTypeSymbolTable();
184 bool ParseValueSymbolTable();
185 bool ParseConstants();
186 bool RememberAndSkipFunctionBody();
187 bool ParseFunctionBody(Function *F);
188 bool ResolveGlobalAndAliasInits();
191 } // End llvm namespace