blob: 9eac14fd99e7be40a2b01fddefc354617084a70e [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnercaee0dc2007-04-22 06:23:29 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BITCODE_READER_H
15#define BITCODE_READER_H
16
Chris Lattnercaee0dc2007-04-22 06:23:29 +000017#include "llvm/ModuleProvider.h"
Devang Pateleaf42ab2008-09-23 23:03:40 +000018#include "llvm/Attributes.h"
Chris Lattner522b7b12007-04-24 05:48:56 +000019#include "llvm/Type.h"
Gabor Greifefe65362008-05-10 08:32:32 +000020#include "llvm/OperandTraits.h"
Chris Lattner48f84872007-05-01 04:59:48 +000021#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000022#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattner48f84872007-05-01 04:59:48 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000024#include <vector>
25
26namespace llvm {
Chris Lattnerc453f762007-04-29 07:54:31 +000027 class MemoryBuffer;
Chris Lattner522b7b12007-04-24 05:48:56 +000028
Gabor Greifefe65362008-05-10 08:32:32 +000029//===----------------------------------------------------------------------===//
30// BitcodeReaderValueList Class
31//===----------------------------------------------------------------------===//
32
Chris Lattner522b7b12007-04-24 05:48:56 +000033class BitcodeReaderValueList : public User {
Gabor Greifefe65362008-05-10 08:32:32 +000034 unsigned Capacity;
Chris Lattnerea693df2008-08-21 02:34:16 +000035
36 /// ResolveConstants - As we resolve forward-referenced constants, we add
37 /// information about them to this vector. This allows us to resolve them in
38 /// bulk instead of resolving each reference at a time. See the code in
39 /// ResolveConstantForwardRefs for more information about this.
40 ///
41 /// The key of this vector is the placeholder constant, the value is the slot
42 /// number that holds the resolved value.
43 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
44 ResolveConstantsTy ResolveConstants;
Chris Lattner522b7b12007-04-24 05:48:56 +000045public:
Gabor Greifefe65362008-05-10 08:32:32 +000046 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
47 , Capacity(0) {}
Chris Lattnerea693df2008-08-21 02:34:16 +000048 ~BitcodeReaderValueList() {
49 assert(ResolveConstants.empty() && "Constants not resolved?");
50 }
Gabor Greifefe65362008-05-10 08:32:32 +000051
52 /// Provide fast operand accessors
53 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
54
Chris Lattner522b7b12007-04-24 05:48:56 +000055 // vector compatibility methods
56 unsigned size() const { return getNumOperands(); }
Gabor Greifefe65362008-05-10 08:32:32 +000057 void resize(unsigned);
Chris Lattner522b7b12007-04-24 05:48:56 +000058 void push_back(Value *V) {
Gabor Greifefe65362008-05-10 08:32:32 +000059 unsigned OldOps(NumOperands), NewOps(NumOperands + 1);
60 resize(NewOps);
61 NumOperands = NewOps;
62 OperandList[OldOps] = V;
Chris Lattner522b7b12007-04-24 05:48:56 +000063 }
64
Chris Lattnerb348bb82007-05-18 04:02:46 +000065 void clear() {
Chris Lattnerea693df2008-08-21 02:34:16 +000066 assert(ResolveConstants.empty() && "Constants not resolved?");
Gabor Greifefe65362008-05-10 08:32:32 +000067 if (OperandList) dropHungoffUses(OperandList);
68 Capacity = 0;
Chris Lattnerb348bb82007-05-18 04:02:46 +000069 }
70
Chris Lattner522b7b12007-04-24 05:48:56 +000071 Value *operator[](unsigned i) const { return getOperand(i); }
72
Gabor Greifefe65362008-05-10 08:32:32 +000073 Value *back() const { return getOperand(size() - 1); }
74 void pop_back() { setOperand(size() - 1, 0); --NumOperands; }
Chris Lattner522b7b12007-04-24 05:48:56 +000075 bool empty() const { return NumOperands == 0; }
Chris Lattner198f34a2007-04-26 03:27:58 +000076 void shrinkTo(unsigned N) {
Chris Lattner980e5aa2007-05-01 05:52:21 +000077 assert(N <= NumOperands && "Invalid shrinkTo request!");
Gabor Greifefe65362008-05-10 08:32:32 +000078 while (NumOperands > N)
79 pop_back();
Chris Lattner198f34a2007-04-26 03:27:58 +000080 }
Chris Lattner522b7b12007-04-24 05:48:56 +000081 virtual void print(std::ostream&) const {}
82
83 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000084 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
85
86 void AssignValue(Value *V, unsigned Idx) {
87 if (Idx == size()) {
88 push_back(V);
89 } else if (Value *OldV = getOperand(Idx)) {
Chris Lattnerea693df2008-08-21 02:34:16 +000090 // Handle constants and non-constants (e.g. instrs) differently for
91 // efficiency.
92 if (Constant *PHC = dyn_cast<Constant>(OldV)) {
93 ResolveConstants.push_back(std::make_pair(PHC, Idx));
94 setOperand(Idx, V);
95 } else {
96 // If there was a forward reference to this value, replace it.
97 setOperand(Idx, V);
98 OldV->replaceAllUsesWith(V);
99 delete OldV;
100 }
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000101 } else {
102 initVal(Idx, V);
103 }
104 }
105
Chris Lattnerea693df2008-08-21 02:34:16 +0000106 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
107 /// resolves any forward references.
108 void ResolveConstantForwardRefs();
109
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000110private:
Chris Lattner522b7b12007-04-24 05:48:56 +0000111 void initVal(unsigned Idx, Value *V) {
Gabor Greifefe65362008-05-10 08:32:32 +0000112 if (Idx >= size()) {
113 // Insert a bunch of null values.
114 resize(Idx * 2 + 1);
115 }
116 assert(getOperand(Idx) == 0 && "Cannot init an already init'd Use!");
Gabor Greif6c80c382008-05-26 21:33:52 +0000117 OperandList[Idx] = V;
Chris Lattner522b7b12007-04-24 05:48:56 +0000118 }
119};
Gabor Greifefe65362008-05-10 08:32:32 +0000120
121template <>
Chris Lattnerea693df2008-08-21 02:34:16 +0000122struct OperandTraits<BitcodeReaderValueList>
123 : HungoffOperandTraits</*16 FIXME*/> {
Gabor Greifefe65362008-05-10 08:32:32 +0000124};
125
126DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000127
128class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +0000129 MemoryBuffer *Buffer;
Chris Lattner48f84872007-05-01 04:59:48 +0000130 BitstreamReader Stream;
131
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000132 const char *ErrorString;
133
134 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +0000135 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +0000136 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +0000137 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +0000138
Chris Lattner48c85b82007-05-04 03:30:17 +0000139 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
140 /// file is for null, and is thus not represented here. As such all indices
141 /// are off by one.
Chris Lattner58d74912008-03-12 17:45:29 +0000142 std::vector<PAListPtr> ParamAttrs;
Chris Lattner48c85b82007-05-04 03:30:17 +0000143
Chris Lattner980e5aa2007-05-01 05:52:21 +0000144 /// FunctionBBs - While parsing a function body, this is a list of the basic
145 /// blocks for the function.
146 std::vector<BasicBlock*> FunctionBBs;
147
Chris Lattner48f84872007-05-01 04:59:48 +0000148 // When reading the module header, this list is populated with functions that
149 // have bodies later in the file.
150 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000151
152 // When intrinsic functions are encountered which require upgrading they are
153 // stored here with their replacement function.
154 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
155 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000156
157 // After the module header has been read, the FunctionsWithBodies list is
158 // reversed. This keeps track of whether we've done this yet.
159 bool HasReversedFunctionsWithBodies;
160
161 /// DeferredFunctionInfo - When function bodies are initially scanned, this
162 /// map contains info about where to find deferred function body (in the
163 /// stream) and what linkage the original function had.
164 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000165public:
Dan Gohman950a4c42008-03-25 22:06:05 +0000166 explicit BitcodeReader(MemoryBuffer *buffer)
167 : Buffer(buffer), ErrorString(0) {
Chris Lattner48f84872007-05-01 04:59:48 +0000168 HasReversedFunctionsWithBodies = false;
169 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000170 ~BitcodeReader() {
171 FreeState();
172 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000173
Chris Lattnerb348bb82007-05-18 04:02:46 +0000174 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000175
176 /// releaseMemoryBuffer - This causes the reader to completely forget about
177 /// the memory buffer it contains, which prevents the buffer from being
178 /// destroyed when it is deleted.
179 void releaseMemoryBuffer() {
180 Buffer = 0;
181 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000182
Chris Lattner48f84872007-05-01 04:59:48 +0000183 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000184 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000185 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000186 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000187
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000188 bool Error(const char *Str) {
189 ErrorString = Str;
190 return true;
191 }
192 const char *getErrorString() const { return ErrorString; }
193
194 /// @brief Main interface to parsing a bitcode buffer.
195 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000196 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000197private:
198 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000199 Value *getFnValueByID(unsigned ID, const Type *Ty) {
200 return ValueList.getValueFwdRef(ID, Ty);
201 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000202 BasicBlock *getBasicBlock(unsigned ID) const {
203 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
204 return FunctionBBs[ID];
205 }
Chris Lattner58d74912008-03-12 17:45:29 +0000206 PAListPtr getParamAttrs(unsigned i) const {
Chris Lattner48c85b82007-05-04 03:30:17 +0000207 if (i-1 < ParamAttrs.size())
208 return ParamAttrs[i-1];
Chris Lattner58d74912008-03-12 17:45:29 +0000209 return PAListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000210 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000211
212 /// getValueTypePair - Read a value/type pair out of the specified record from
213 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
214 /// Return true on failure.
215 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
216 unsigned InstNum, Value *&ResVal) {
217 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000218 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000219 if (ValNo < InstNum) {
220 // If this is not a forward reference, just return the value we already
221 // have.
222 ResVal = getFnValueByID(ValNo, 0);
223 return ResVal == 0;
224 } else if (Slot == Record.size()) {
225 return true;
226 }
227
Jeff Cohen650c9382007-05-06 03:23:14 +0000228 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000229 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
230 return ResVal == 0;
231 }
232 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
233 const Type *Ty, Value *&ResVal) {
234 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000235 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000236 ResVal = getFnValueByID(ValNo, Ty);
237 return ResVal == 0;
238 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000239
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000240
Chris Lattner86697142007-05-01 05:01:34 +0000241 bool ParseModule(const std::string &ModuleID);
Chris Lattner48c85b82007-05-04 03:30:17 +0000242 bool ParseParamAttrBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000243 bool ParseTypeTable();
244 bool ParseTypeSymbolTable();
245 bool ParseValueSymbolTable();
246 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000247 bool RememberAndSkipFunctionBody();
248 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000249 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000250};
251
252} // End llvm namespace
253
254#endif