blob: a5ab5d29e917150f4e4184fc268dcf6a8af2114b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This header defines the BitcodeReader class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef BITCODE_READER_H
15#define BITCODE_READER_H
16
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +000017#include "llvm/GVMaterializer.h"
Devang Patele480dfa2008-09-23 23:03:40 +000018#include "llvm/Attributes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Type.h"
Gabor Greif25bc3222008-05-10 08:32:32 +000020#include "llvm/OperandTraits.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Bitcode/BitstreamReader.h"
22#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnered490d12009-03-31 22:55:09 +000023#include "llvm/Support/ValueHandle.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/ADT/DenseMap.h"
25#include <vector>
26
27namespace llvm {
28 class MemoryBuffer;
Benjamin Krameraca6b322009-08-11 17:45:13 +000029 class LLVMContext;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
Gabor Greif25bc3222008-05-10 08:32:32 +000031//===----------------------------------------------------------------------===//
32// BitcodeReaderValueList Class
33//===----------------------------------------------------------------------===//
34
Chris Lattnered490d12009-03-31 22:55:09 +000035class BitcodeReaderValueList {
36 std::vector<WeakVH> ValuePtrs;
Chris Lattner345bbb72008-08-21 02:34:16 +000037
38 /// ResolveConstants - As we resolve forward-referenced constants, we add
39 /// information about them to this vector. This allows us to resolve them in
40 /// bulk instead of resolving each reference at a time. See the code in
41 /// ResolveConstantForwardRefs for more information about this.
42 ///
43 /// The key of this vector is the placeholder constant, the value is the slot
44 /// number that holds the resolved value.
45 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
46 ResolveConstantsTy ResolveConstants;
Owen Anderson4956cde2009-07-07 20:18:58 +000047 LLVMContext& Context;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048public:
Owen Anderson4956cde2009-07-07 20:18:58 +000049 BitcodeReaderValueList(LLVMContext& C) : Context(C) {}
Chris Lattner345bbb72008-08-21 02:34:16 +000050 ~BitcodeReaderValueList() {
51 assert(ResolveConstants.empty() && "Constants not resolved?");
52 }
Gabor Greif25bc3222008-05-10 08:32:32 +000053
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 // vector compatibility methods
Chris Lattnered490d12009-03-31 22:55:09 +000055 unsigned size() const { return ValuePtrs.size(); }
56 void resize(unsigned N) { ValuePtrs.resize(N); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 void push_back(Value *V) {
Chris Lattnered490d12009-03-31 22:55:09 +000058 ValuePtrs.push_back(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 }
60
61 void clear() {
Chris Lattner345bbb72008-08-21 02:34:16 +000062 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattnered490d12009-03-31 22:55:09 +000063 ValuePtrs.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 }
65
Chris Lattnered490d12009-03-31 22:55:09 +000066 Value *operator[](unsigned i) const {
67 assert(i < ValuePtrs.size());
68 return ValuePtrs[i];
69 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Chris Lattnered490d12009-03-31 22:55:09 +000071 Value *back() const { return ValuePtrs.back(); }
72 void pop_back() { ValuePtrs.pop_back(); }
73 bool empty() const { return ValuePtrs.empty(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 void shrinkTo(unsigned N) {
Chris Lattnered490d12009-03-31 22:55:09 +000075 assert(N <= size() && "Invalid shrinkTo request!");
76 ValuePtrs.resize(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
80 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
81
Chris Lattnered490d12009-03-31 22:55:09 +000082 void AssignValue(Value *V, unsigned Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
Chris Lattner345bbb72008-08-21 02:34:16 +000084 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
85 /// resolves any forward references.
86 void ResolveConstantForwardRefs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087};
Gabor Greif25bc3222008-05-10 08:32:32 +000088
Devang Patelea27c232009-08-04 06:00:18 +000089
90//===----------------------------------------------------------------------===//
91// BitcodeReaderMDValueList Class
92//===----------------------------------------------------------------------===//
93
94class BitcodeReaderMDValueList {
95 std::vector<WeakVH> MDValuePtrs;
96
Chris Lattner1bd1d9e2009-10-28 05:53:48 +000097 LLVMContext &Context;
Devang Patelea27c232009-08-04 06:00:18 +000098public:
99 BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
100
101 // vector compatibility methods
102 unsigned size() const { return MDValuePtrs.size(); }
103 void resize(unsigned N) { MDValuePtrs.resize(N); }
104 void push_back(Value *V) { MDValuePtrs.push_back(V); }
105 void clear() { MDValuePtrs.clear(); }
106 Value *back() const { return MDValuePtrs.back(); }
107 void pop_back() { MDValuePtrs.pop_back(); }
108 bool empty() const { return MDValuePtrs.empty(); }
109
110 Value *operator[](unsigned i) const {
111 assert(i < MDValuePtrs.size());
112 return MDValuePtrs[i];
113 }
114
115 void shrinkTo(unsigned N) {
116 assert(N <= size() && "Invalid shrinkTo request!");
117 MDValuePtrs.resize(N);
118 }
119
120 Value *getValueFwdRef(unsigned Idx);
121 void AssignValue(Value *V, unsigned Idx);
122};
123
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000124class BitcodeReader : public GVMaterializer {
Chris Lattner1bd1d9e2009-10-28 05:53:48 +0000125 LLVMContext &Context;
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000126 Module *TheModule;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 MemoryBuffer *Buffer;
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000128 bool BufferOwned;
Chris Lattner25a6abf2009-04-26 20:59:02 +0000129 BitstreamReader StreamFile;
130 BitstreamCursor Stream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 const char *ErrorString;
133
134 std::vector<PATypeHolder> TypeList;
135 BitcodeReaderValueList ValueList;
Devang Patelea27c232009-08-04 06:00:18 +0000136 BitcodeReaderMDValueList MDValueList;
Devang Pateladc37612009-09-18 19:26:43 +0000137 SmallVector<Instruction *, 64> InstructionList;
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
140 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
141
Devang Patelf2a4a922008-09-26 22:53:05 +0000142 /// MAttributes - The set of attributes by index. Index zero in the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 /// file is for null, and is thus not represented here. As such all indices
144 /// are off by one.
Devang Patelf2a4a922008-09-26 22:53:05 +0000145 std::vector<AttrListPtr> MAttributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 /// FunctionBBs - While parsing a function body, this is a list of the basic
148 /// blocks for the function.
149 std::vector<BasicBlock*> FunctionBBs;
150
151 // When reading the module header, this list is populated with functions that
152 // have bodies later in the file.
153 std::vector<Function*> FunctionsWithBodies;
Chandler Carrutha228e392007-08-04 01:51:18 +0000154
155 // When intrinsic functions are encountered which require upgrading they are
156 // stored here with their replacement function.
157 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
158 UpgradedIntrinsicMap UpgradedIntrinsics;
Dan Gohman85302f32010-07-20 21:42:28 +0000159
160 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
161 DenseMap<unsigned, unsigned> MDKindMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
163 // After the module header has been read, the FunctionsWithBodies list is
164 // reversed. This keeps track of whether we've done this yet.
165 bool HasReversedFunctionsWithBodies;
166
167 /// DeferredFunctionInfo - When function bodies are initially scanned, this
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000168 /// map contains info about where to find deferred function body in the
169 /// stream.
170 DenseMap<Function*, uint64_t> DeferredFunctionInfo;
Chris Lattner1bd1d9e2009-10-28 05:53:48 +0000171
172 /// BlockAddrFwdRefs - These are blockaddr references to basic blocks. These
173 /// are resolved lazily when functions are loaded.
174 typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
175 DenseMap<Function*, std::vector<BlockAddrRefTy> > BlockAddrFwdRefs;
176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177public:
Chris Lattnera0d451f2009-12-29 09:01:33 +0000178 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext &C)
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000179 : Context(C), TheModule(0), Buffer(buffer), BufferOwned(false),
180 ErrorString(0), ValueList(C), MDValueList(C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 HasReversedFunctionsWithBodies = false;
182 }
183 ~BitcodeReader() {
184 FreeState();
185 }
186
187 void FreeState();
188
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000189 /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
190 /// when the reader is destroyed.
191 void setBufferOwned(bool Owned) { BufferOwned = Owned; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000193 virtual bool isMaterializable(const GlobalValue *GV) const;
194 virtual bool isDematerializable(const GlobalValue *GV) const;
195 virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
196 virtual bool MaterializeModule(Module *M, std::string *ErrInfo = 0);
197 virtual void Dematerialize(GlobalValue *GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198
199 bool Error(const char *Str) {
200 ErrorString = Str;
201 return true;
202 }
203 const char *getErrorString() const { return ErrorString; }
204
205 /// @brief Main interface to parsing a bitcode buffer.
206 /// @returns true if an error occurred.
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000207 bool ParseBitcodeInto(Module *M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208private:
209 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
210 Value *getFnValueByID(unsigned ID, const Type *Ty) {
Owen Anderson35b47072009-08-13 21:58:54 +0000211 if (Ty == Type::getMetadataTy(Context))
Devang Patelea27c232009-08-04 06:00:18 +0000212 return MDValueList.getValueFwdRef(ID);
213 else
214 return ValueList.getValueFwdRef(ID, Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 }
216 BasicBlock *getBasicBlock(unsigned ID) const {
217 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
218 return FunctionBBs[ID];
219 }
Devang Pateld222f862008-09-25 21:00:45 +0000220 AttrListPtr getAttributes(unsigned i) const {
Devang Patelf2a4a922008-09-26 22:53:05 +0000221 if (i-1 < MAttributes.size())
222 return MAttributes[i-1];
Devang Pateld222f862008-09-25 21:00:45 +0000223 return AttrListPtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
225
226 /// getValueTypePair - Read a value/type pair out of the specified record from
227 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
228 /// Return true on failure.
229 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
230 unsigned InstNum, Value *&ResVal) {
231 if (Slot == Record.size()) return true;
232 unsigned ValNo = (unsigned)Record[Slot++];
233 if (ValNo < InstNum) {
234 // If this is not a forward reference, just return the value we already
235 // have.
236 ResVal = getFnValueByID(ValNo, 0);
237 return ResVal == 0;
238 } else if (Slot == Record.size()) {
239 return true;
240 }
241
242 unsigned TypeNo = (unsigned)Record[Slot++];
243 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
244 return ResVal == 0;
245 }
246 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
247 const Type *Ty, Value *&ResVal) {
248 if (Slot == Record.size()) return true;
249 unsigned ValNo = (unsigned)Record[Slot++];
250 ResVal = getFnValueByID(ValNo, Ty);
251 return ResVal == 0;
252 }
253
254
Jeffrey Yasskin62de4e72010-01-27 20:34:15 +0000255 bool ParseModule();
Devang Pateld222f862008-09-25 21:00:45 +0000256 bool ParseAttributeBlock();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 bool ParseTypeTable();
258 bool ParseTypeSymbolTable();
259 bool ParseValueSymbolTable();
260 bool ParseConstants();
261 bool RememberAndSkipFunctionBody();
262 bool ParseFunctionBody(Function *F);
263 bool ResolveGlobalAndAliasInits();
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000264 bool ParseMetadata();
Devang Pateladc37612009-09-18 19:26:43 +0000265 bool ParseMetadataAttachment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266};
267
268} // End llvm namespace
269
270#endif