blob: 9f62efec29326b3fba86563e72e2724d74305b9a [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"
Chris Lattner58d74912008-03-12 17:45:29 +000018#include "llvm/ParameterAttributes.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 Lattner522b7b12007-04-24 05:48:56 +000035public:
Gabor Greifefe65362008-05-10 08:32:32 +000036 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0)
37 , Capacity(0) {}
38
39 /// Provide fast operand accessors
40 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
41
Chris Lattner522b7b12007-04-24 05:48:56 +000042 // vector compatibility methods
43 unsigned size() const { return getNumOperands(); }
Gabor Greifefe65362008-05-10 08:32:32 +000044 void resize(unsigned);
Chris Lattner522b7b12007-04-24 05:48:56 +000045 void push_back(Value *V) {
Gabor Greifefe65362008-05-10 08:32:32 +000046 unsigned OldOps(NumOperands), NewOps(NumOperands + 1);
47 resize(NewOps);
48 NumOperands = NewOps;
49 OperandList[OldOps] = V;
Chris Lattner522b7b12007-04-24 05:48:56 +000050 }
51
Chris Lattnerb348bb82007-05-18 04:02:46 +000052 void clear() {
Gabor Greifefe65362008-05-10 08:32:32 +000053 if (OperandList) dropHungoffUses(OperandList);
54 Capacity = 0;
Chris Lattnerb348bb82007-05-18 04:02:46 +000055 }
56
Chris Lattner522b7b12007-04-24 05:48:56 +000057 Value *operator[](unsigned i) const { return getOperand(i); }
58
Gabor Greifefe65362008-05-10 08:32:32 +000059 Value *back() const { return getOperand(size() - 1); }
60 void pop_back() { setOperand(size() - 1, 0); --NumOperands; }
Chris Lattner522b7b12007-04-24 05:48:56 +000061 bool empty() const { return NumOperands == 0; }
Chris Lattner198f34a2007-04-26 03:27:58 +000062 void shrinkTo(unsigned N) {
Chris Lattner980e5aa2007-05-01 05:52:21 +000063 assert(N <= NumOperands && "Invalid shrinkTo request!");
Gabor Greifefe65362008-05-10 08:32:32 +000064 while (NumOperands > N)
65 pop_back();
Chris Lattner198f34a2007-04-26 03:27:58 +000066 }
Chris Lattner522b7b12007-04-24 05:48:56 +000067 virtual void print(std::ostream&) const {}
68
69 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000070 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
71
72 void AssignValue(Value *V, unsigned Idx) {
73 if (Idx == size()) {
74 push_back(V);
75 } else if (Value *OldV = getOperand(Idx)) {
76 // If there was a forward reference to this value, replace it.
77 setOperand(Idx, V);
78 OldV->replaceAllUsesWith(V);
79 delete OldV;
80 } else {
81 initVal(Idx, V);
82 }
83 }
84
85private:
Chris Lattner522b7b12007-04-24 05:48:56 +000086 void initVal(unsigned Idx, Value *V) {
Gabor Greifefe65362008-05-10 08:32:32 +000087 if (Idx >= size()) {
88 // Insert a bunch of null values.
89 resize(Idx * 2 + 1);
90 }
91 assert(getOperand(Idx) == 0 && "Cannot init an already init'd Use!");
Gabor Greif6c80c382008-05-26 21:33:52 +000092 OperandList[Idx] = V;
Chris Lattner522b7b12007-04-24 05:48:56 +000093 }
94};
Gabor Greifefe65362008-05-10 08:32:32 +000095
96template <>
97struct OperandTraits<BitcodeReaderValueList> : HungoffOperandTraits</*16 FIXME*/> {
98};
99
100DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value)
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000101
102class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +0000103 MemoryBuffer *Buffer;
Chris Lattner48f84872007-05-01 04:59:48 +0000104 BitstreamReader Stream;
105
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000106 const char *ErrorString;
107
108 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +0000109 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +0000110 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +0000111 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +0000112
Chris Lattner48c85b82007-05-04 03:30:17 +0000113 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
114 /// file is for null, and is thus not represented here. As such all indices
115 /// are off by one.
Chris Lattner58d74912008-03-12 17:45:29 +0000116 std::vector<PAListPtr> ParamAttrs;
Chris Lattner48c85b82007-05-04 03:30:17 +0000117
Chris Lattner980e5aa2007-05-01 05:52:21 +0000118 /// FunctionBBs - While parsing a function body, this is a list of the basic
119 /// blocks for the function.
120 std::vector<BasicBlock*> FunctionBBs;
121
Chris Lattner48f84872007-05-01 04:59:48 +0000122 // When reading the module header, this list is populated with functions that
123 // have bodies later in the file.
124 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000125
126 // When intrinsic functions are encountered which require upgrading they are
127 // stored here with their replacement function.
128 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
129 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000130
131 // After the module header has been read, the FunctionsWithBodies list is
132 // reversed. This keeps track of whether we've done this yet.
133 bool HasReversedFunctionsWithBodies;
134
135 /// DeferredFunctionInfo - When function bodies are initially scanned, this
136 /// map contains info about where to find deferred function body (in the
137 /// stream) and what linkage the original function had.
138 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000139public:
Dan Gohman950a4c42008-03-25 22:06:05 +0000140 explicit BitcodeReader(MemoryBuffer *buffer)
141 : Buffer(buffer), ErrorString(0) {
Chris Lattner48f84872007-05-01 04:59:48 +0000142 HasReversedFunctionsWithBodies = false;
143 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000144 ~BitcodeReader() {
145 FreeState();
146 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000147
Chris Lattnerb348bb82007-05-18 04:02:46 +0000148 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000149
150 /// releaseMemoryBuffer - This causes the reader to completely forget about
151 /// the memory buffer it contains, which prevents the buffer from being
152 /// destroyed when it is deleted.
153 void releaseMemoryBuffer() {
154 Buffer = 0;
155 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000156
Chris Lattner48f84872007-05-01 04:59:48 +0000157 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000158 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000159 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000160 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000161
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000162 bool Error(const char *Str) {
163 ErrorString = Str;
164 return true;
165 }
166 const char *getErrorString() const { return ErrorString; }
167
168 /// @brief Main interface to parsing a bitcode buffer.
169 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000170 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000171private:
172 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000173 Value *getFnValueByID(unsigned ID, const Type *Ty) {
174 return ValueList.getValueFwdRef(ID, Ty);
175 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000176 BasicBlock *getBasicBlock(unsigned ID) const {
177 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
178 return FunctionBBs[ID];
179 }
Chris Lattner58d74912008-03-12 17:45:29 +0000180 PAListPtr getParamAttrs(unsigned i) const {
Chris Lattner48c85b82007-05-04 03:30:17 +0000181 if (i-1 < ParamAttrs.size())
182 return ParamAttrs[i-1];
Chris Lattner58d74912008-03-12 17:45:29 +0000183 return PAListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000184 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000185
186 /// getValueTypePair - Read a value/type pair out of the specified record from
187 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
188 /// Return true on failure.
189 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
190 unsigned InstNum, Value *&ResVal) {
191 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000192 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000193 if (ValNo < InstNum) {
194 // If this is not a forward reference, just return the value we already
195 // have.
196 ResVal = getFnValueByID(ValNo, 0);
197 return ResVal == 0;
198 } else if (Slot == Record.size()) {
199 return true;
200 }
201
Jeff Cohen650c9382007-05-06 03:23:14 +0000202 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000203 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
204 return ResVal == 0;
205 }
206 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
207 const Type *Ty, Value *&ResVal) {
208 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000209 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000210 ResVal = getFnValueByID(ValNo, Ty);
211 return ResVal == 0;
212 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000213
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000214
Chris Lattner86697142007-05-01 05:01:34 +0000215 bool ParseModule(const std::string &ModuleID);
Chris Lattner48c85b82007-05-04 03:30:17 +0000216 bool ParseParamAttrBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000217 bool ParseTypeTable();
218 bool ParseTypeSymbolTable();
219 bool ParseValueSymbolTable();
220 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000221 bool RememberAndSkipFunctionBody();
222 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000223 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000224};
225
226} // End llvm namespace
227
228#endif