blob: 0dc470b24a239a06d3b773ace9e3b6ce1e0ee047 [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 Lattner46e77402009-03-31 22:55:09 +000023#include "llvm/Support/ValueHandle.h"
Chris Lattner48f84872007-05-01 04:59:48 +000024#include "llvm/ADT/DenseMap.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000025#include <vector>
26
27namespace llvm {
Chris Lattnerc453f762007-04-29 07:54:31 +000028 class MemoryBuffer;
Chris Lattner522b7b12007-04-24 05:48:56 +000029
Gabor Greifefe65362008-05-10 08:32:32 +000030//===----------------------------------------------------------------------===//
31// BitcodeReaderValueList Class
32//===----------------------------------------------------------------------===//
33
Chris Lattner46e77402009-03-31 22:55:09 +000034class BitcodeReaderValueList {
35 std::vector<WeakVH> ValuePtrs;
Chris Lattnerea693df2008-08-21 02:34:16 +000036
37 /// ResolveConstants - As we resolve forward-referenced constants, we add
38 /// information about them to this vector. This allows us to resolve them in
39 /// bulk instead of resolving each reference at a time. See the code in
40 /// ResolveConstantForwardRefs for more information about this.
41 ///
42 /// The key of this vector is the placeholder constant, the value is the slot
43 /// number that holds the resolved value.
44 typedef std::vector<std::pair<Constant*, unsigned> > ResolveConstantsTy;
45 ResolveConstantsTy ResolveConstants;
Chris Lattner522b7b12007-04-24 05:48:56 +000046public:
Chris Lattner46e77402009-03-31 22:55:09 +000047 BitcodeReaderValueList() {}
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
Chris Lattner522b7b12007-04-24 05:48:56 +000052 // vector compatibility methods
Chris Lattner46e77402009-03-31 22:55:09 +000053 unsigned size() const { return ValuePtrs.size(); }
54 void resize(unsigned N) { ValuePtrs.resize(N); }
Chris Lattner522b7b12007-04-24 05:48:56 +000055 void push_back(Value *V) {
Chris Lattner46e77402009-03-31 22:55:09 +000056 ValuePtrs.push_back(V);
Chris Lattner522b7b12007-04-24 05:48:56 +000057 }
58
Chris Lattnerb348bb82007-05-18 04:02:46 +000059 void clear() {
Chris Lattnerea693df2008-08-21 02:34:16 +000060 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattner46e77402009-03-31 22:55:09 +000061 ValuePtrs.clear();
Chris Lattnerb348bb82007-05-18 04:02:46 +000062 }
63
Chris Lattner46e77402009-03-31 22:55:09 +000064 Value *operator[](unsigned i) const {
65 assert(i < ValuePtrs.size());
66 return ValuePtrs[i];
67 }
Chris Lattner522b7b12007-04-24 05:48:56 +000068
Chris Lattner46e77402009-03-31 22:55:09 +000069 Value *back() const { return ValuePtrs.back(); }
70 void pop_back() { ValuePtrs.pop_back(); }
71 bool empty() const { return ValuePtrs.empty(); }
Chris Lattner198f34a2007-04-26 03:27:58 +000072 void shrinkTo(unsigned N) {
Chris Lattner46e77402009-03-31 22:55:09 +000073 assert(N <= size() && "Invalid shrinkTo request!");
74 ValuePtrs.resize(N);
Chris Lattner198f34a2007-04-26 03:27:58 +000075 }
Chris Lattner522b7b12007-04-24 05:48:56 +000076
77 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000078 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
79
Chris Lattner46e77402009-03-31 22:55:09 +000080 void AssignValue(Value *V, unsigned Idx);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000081
Chris Lattnerea693df2008-08-21 02:34:16 +000082 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
83 /// resolves any forward references.
84 void ResolveConstantForwardRefs();
Chris Lattner522b7b12007-04-24 05:48:56 +000085};
Gabor Greifefe65362008-05-10 08:32:32 +000086
Chris Lattnercaee0dc2007-04-22 06:23:29 +000087class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +000088 MemoryBuffer *Buffer;
Chris Lattner962dde32009-04-26 20:59:02 +000089 BitstreamReader StreamFile;
90 BitstreamCursor Stream;
Chris Lattner48f84872007-05-01 04:59:48 +000091
Chris Lattnercaee0dc2007-04-22 06:23:29 +000092 const char *ErrorString;
93
94 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000095 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000096 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +000097 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +000098
Devang Patel19c87462008-09-26 22:53:05 +000099 /// MAttributes - The set of attributes by index. Index zero in the
Chris Lattner48c85b82007-05-04 03:30:17 +0000100 /// file is for null, and is thus not represented here. As such all indices
101 /// are off by one.
Devang Patel19c87462008-09-26 22:53:05 +0000102 std::vector<AttrListPtr> MAttributes;
Chris Lattner48c85b82007-05-04 03:30:17 +0000103
Chris Lattner980e5aa2007-05-01 05:52:21 +0000104 /// FunctionBBs - While parsing a function body, this is a list of the basic
105 /// blocks for the function.
106 std::vector<BasicBlock*> FunctionBBs;
107
Chris Lattner48f84872007-05-01 04:59:48 +0000108 // When reading the module header, this list is populated with functions that
109 // have bodies later in the file.
110 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000111
112 // When intrinsic functions are encountered which require upgrading they are
113 // stored here with their replacement function.
114 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
115 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000116
117 // After the module header has been read, the FunctionsWithBodies list is
118 // reversed. This keeps track of whether we've done this yet.
119 bool HasReversedFunctionsWithBodies;
120
121 /// DeferredFunctionInfo - When function bodies are initially scanned, this
122 /// map contains info about where to find deferred function body (in the
123 /// stream) and what linkage the original function had.
124 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000125public:
Dan Gohman950a4c42008-03-25 22:06:05 +0000126 explicit BitcodeReader(MemoryBuffer *buffer)
127 : Buffer(buffer), ErrorString(0) {
Chris Lattner48f84872007-05-01 04:59:48 +0000128 HasReversedFunctionsWithBodies = false;
129 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000130 ~BitcodeReader() {
131 FreeState();
132 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000133
Chris Lattnerb348bb82007-05-18 04:02:46 +0000134 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000135
136 /// releaseMemoryBuffer - This causes the reader to completely forget about
137 /// the memory buffer it contains, which prevents the buffer from being
138 /// destroyed when it is deleted.
139 void releaseMemoryBuffer() {
140 Buffer = 0;
141 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000142
Chris Lattner48f84872007-05-01 04:59:48 +0000143 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000144 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000145 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000146 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000147
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000148 bool Error(const char *Str) {
149 ErrorString = Str;
150 return true;
151 }
152 const char *getErrorString() const { return ErrorString; }
153
154 /// @brief Main interface to parsing a bitcode buffer.
155 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000156 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000157private:
158 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000159 Value *getFnValueByID(unsigned ID, const Type *Ty) {
160 return ValueList.getValueFwdRef(ID, Ty);
161 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000162 BasicBlock *getBasicBlock(unsigned ID) const {
163 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
164 return FunctionBBs[ID];
165 }
Devang Patel05988662008-09-25 21:00:45 +0000166 AttrListPtr getAttributes(unsigned i) const {
Devang Patel19c87462008-09-26 22:53:05 +0000167 if (i-1 < MAttributes.size())
168 return MAttributes[i-1];
Devang Patel05988662008-09-25 21:00:45 +0000169 return AttrListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000170 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000171
172 /// getValueTypePair - Read a value/type pair out of the specified record from
173 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
174 /// Return true on failure.
175 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
176 unsigned InstNum, Value *&ResVal) {
177 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000178 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000179 if (ValNo < InstNum) {
180 // If this is not a forward reference, just return the value we already
181 // have.
182 ResVal = getFnValueByID(ValNo, 0);
183 return ResVal == 0;
184 } else if (Slot == Record.size()) {
185 return true;
186 }
187
Jeff Cohen650c9382007-05-06 03:23:14 +0000188 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000189 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
190 return ResVal == 0;
191 }
192 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
193 const Type *Ty, Value *&ResVal) {
194 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000195 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000196 ResVal = getFnValueByID(ValNo, Ty);
197 return ResVal == 0;
198 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000199
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000200
Chris Lattner86697142007-05-01 05:01:34 +0000201 bool ParseModule(const std::string &ModuleID);
Devang Patel05988662008-09-25 21:00:45 +0000202 bool ParseAttributeBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000203 bool ParseTypeTable();
204 bool ParseTypeSymbolTable();
205 bool ParseValueSymbolTable();
206 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000207 bool RememberAndSkipFunctionBody();
208 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000209 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000210};
211
212} // End llvm namespace
213
214#endif