blob: 0655a1a91c2be875e5d7da5aac18472bca7c3748 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BITCODE_READER_H
15#define BITCODE_READER_H
16
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"
23#include <vector>
24
25namespace llvm {
26 class MemoryBuffer;
27 class ParamAttrsList;
28
29class BitcodeReaderValueList : public User {
30 std::vector<Use> Uses;
31public:
32 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33
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];
39 ++NumOperands;
40 }
41
42 void clear() {
43 std::vector<Use>().swap(Uses);
44 }
45
46 Value *operator[](unsigned i) const { return getOperand(i); }
47
48 Value *back() const { return Uses.back(); }
49 void pop_back() { Uses.pop_back(); --NumOperands; }
50 bool empty() const { return NumOperands == 0; }
51 void shrinkTo(unsigned N) {
52 assert(N <= NumOperands && "Invalid shrinkTo request!");
53 Uses.resize(N);
54 NumOperands = N;
55 }
56 virtual void print(std::ostream&) const {}
57
58 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
59 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
60
61 void AssignValue(Value *V, unsigned Idx) {
62 if (Idx == size()) {
63 push_back(V);
64 } else if (Value *OldV = getOperand(Idx)) {
65 // If there was a forward reference to this value, replace it.
66 setOperand(Idx, V);
67 OldV->replaceAllUsesWith(V);
68 delete OldV;
69 } else {
70 initVal(Idx, V);
71 }
72 }
73
74private:
75 void initVal(unsigned Idx, Value *V) {
76 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
77 Uses[Idx].init(V, this);
78 }
79};
80
81
82class BitcodeReader : public ModuleProvider {
83 MemoryBuffer *Buffer;
84 BitstreamReader Stream;
85
86 const char *ErrorString;
87
88 std::vector<PATypeHolder> TypeList;
89 BitcodeReaderValueList ValueList;
90 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
91 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
92
93 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
94 /// file is for null, and is thus not represented here. As such all indices
95 /// are off by one.
96 std::vector<const ParamAttrsList*> ParamAttrs;
97
98 /// FunctionBBs - While parsing a function body, this is a list of the basic
99 /// blocks for the function.
100 std::vector<BasicBlock*> FunctionBBs;
101
102 // When reading the module header, this list is populated with functions that
103 // have bodies later in the file.
104 std::vector<Function*> FunctionsWithBodies;
Chandler Carrutha228e392007-08-04 01:51:18 +0000105
106 // When intrinsic functions are encountered which require upgrading they are
107 // stored here with their replacement function.
108 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
109 UpgradedIntrinsicMap UpgradedIntrinsics;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111 // After the module header has been read, the FunctionsWithBodies list is
112 // reversed. This keeps track of whether we've done this yet.
113 bool HasReversedFunctionsWithBodies;
114
115 /// DeferredFunctionInfo - When function bodies are initially scanned, this
116 /// map contains info about where to find deferred function body (in the
117 /// stream) and what linkage the original function had.
118 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
119public:
120 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
121 HasReversedFunctionsWithBodies = false;
122 }
123 ~BitcodeReader() {
124 FreeState();
125 }
126
127 void FreeState();
128
129 /// releaseMemoryBuffer - This causes the reader to completely forget about
130 /// the memory buffer it contains, which prevents the buffer from being
131 /// destroyed when it is deleted.
132 void releaseMemoryBuffer() {
133 Buffer = 0;
134 }
135
136 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
137 virtual Module *materializeModule(std::string *ErrInfo = 0);
138 virtual void dematerializeFunction(Function *F);
139 virtual Module *releaseModule(std::string *ErrInfo = 0);
140
141 bool Error(const char *Str) {
142 ErrorString = Str;
143 return true;
144 }
145 const char *getErrorString() const { return ErrorString; }
146
147 /// @brief Main interface to parsing a bitcode buffer.
148 /// @returns true if an error occurred.
149 bool ParseBitcode();
150private:
151 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
152 Value *getFnValueByID(unsigned ID, const Type *Ty) {
153 return ValueList.getValueFwdRef(ID, Ty);
154 }
155 BasicBlock *getBasicBlock(unsigned ID) const {
156 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
157 return FunctionBBs[ID];
158 }
159 const ParamAttrsList *getParamAttrs(unsigned i) const {
160 if (i-1 < ParamAttrs.size())
161 return ParamAttrs[i-1];
162 return 0;
163 }
164
165 /// getValueTypePair - Read a value/type pair out of the specified record from
166 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
167 /// Return true on failure.
168 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
169 unsigned InstNum, Value *&ResVal) {
170 if (Slot == Record.size()) return true;
171 unsigned ValNo = (unsigned)Record[Slot++];
172 if (ValNo < InstNum) {
173 // If this is not a forward reference, just return the value we already
174 // have.
175 ResVal = getFnValueByID(ValNo, 0);
176 return ResVal == 0;
177 } else if (Slot == Record.size()) {
178 return true;
179 }
180
181 unsigned TypeNo = (unsigned)Record[Slot++];
182 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
183 return ResVal == 0;
184 }
185 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
186 const Type *Ty, Value *&ResVal) {
187 if (Slot == Record.size()) return true;
188 unsigned ValNo = (unsigned)Record[Slot++];
189 ResVal = getFnValueByID(ValNo, Ty);
190 return ResVal == 0;
191 }
192
193
194 bool ParseModule(const std::string &ModuleID);
195 bool ParseParamAttrBlock();
196 bool ParseTypeTable();
197 bool ParseTypeSymbolTable();
198 bool ParseValueSymbolTable();
199 bool ParseConstants();
200 bool RememberAndSkipFunctionBody();
201 bool ParseFunctionBody(Function *F);
202 bool ResolveGlobalAndAliasInits();
203};
204
205} // End llvm namespace
206
207#endif