blob: 662631bce950748a0f9248be1de83ccc8b5a7b6b [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;
Owen Anderson8b477ed2009-07-01 16:58:40 +000029 class LLVMContext;
Chris Lattner522b7b12007-04-24 05:48:56 +000030
Gabor Greifefe65362008-05-10 08:32:32 +000031//===----------------------------------------------------------------------===//
32// BitcodeReaderValueList Class
33//===----------------------------------------------------------------------===//
34
Chris Lattner46e77402009-03-31 22:55:09 +000035class BitcodeReaderValueList {
36 std::vector<WeakVH> ValuePtrs;
Chris Lattnerea693df2008-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;
Chris Lattner522b7b12007-04-24 05:48:56 +000047public:
Chris Lattner46e77402009-03-31 22:55:09 +000048 BitcodeReaderValueList() {}
Chris Lattnerea693df2008-08-21 02:34:16 +000049 ~BitcodeReaderValueList() {
50 assert(ResolveConstants.empty() && "Constants not resolved?");
51 }
Gabor Greifefe65362008-05-10 08:32:32 +000052
Chris Lattner522b7b12007-04-24 05:48:56 +000053 // vector compatibility methods
Chris Lattner46e77402009-03-31 22:55:09 +000054 unsigned size() const { return ValuePtrs.size(); }
55 void resize(unsigned N) { ValuePtrs.resize(N); }
Chris Lattner522b7b12007-04-24 05:48:56 +000056 void push_back(Value *V) {
Chris Lattner46e77402009-03-31 22:55:09 +000057 ValuePtrs.push_back(V);
Chris Lattner522b7b12007-04-24 05:48:56 +000058 }
59
Chris Lattnerb348bb82007-05-18 04:02:46 +000060 void clear() {
Chris Lattnerea693df2008-08-21 02:34:16 +000061 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattner46e77402009-03-31 22:55:09 +000062 ValuePtrs.clear();
Chris Lattnerb348bb82007-05-18 04:02:46 +000063 }
64
Chris Lattner46e77402009-03-31 22:55:09 +000065 Value *operator[](unsigned i) const {
66 assert(i < ValuePtrs.size());
67 return ValuePtrs[i];
68 }
Chris Lattner522b7b12007-04-24 05:48:56 +000069
Chris Lattner46e77402009-03-31 22:55:09 +000070 Value *back() const { return ValuePtrs.back(); }
71 void pop_back() { ValuePtrs.pop_back(); }
72 bool empty() const { return ValuePtrs.empty(); }
Chris Lattner198f34a2007-04-26 03:27:58 +000073 void shrinkTo(unsigned N) {
Chris Lattner46e77402009-03-31 22:55:09 +000074 assert(N <= size() && "Invalid shrinkTo request!");
75 ValuePtrs.resize(N);
Chris Lattner198f34a2007-04-26 03:27:58 +000076 }
Chris Lattner522b7b12007-04-24 05:48:56 +000077
78 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000079 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
80
Chris Lattner46e77402009-03-31 22:55:09 +000081 void AssignValue(Value *V, unsigned Idx);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000082
Chris Lattnerea693df2008-08-21 02:34:16 +000083 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
84 /// resolves any forward references.
85 void ResolveConstantForwardRefs();
Chris Lattner522b7b12007-04-24 05:48:56 +000086};
Gabor Greifefe65362008-05-10 08:32:32 +000087
Chris Lattnercaee0dc2007-04-22 06:23:29 +000088class BitcodeReader : public ModuleProvider {
Owen Anderson4434ed42009-07-01 23:13:44 +000089 LLVMContext& Context;
Chris Lattnerc453f762007-04-29 07:54:31 +000090 MemoryBuffer *Buffer;
Chris Lattner962dde32009-04-26 20:59:02 +000091 BitstreamReader StreamFile;
92 BitstreamCursor Stream;
Chris Lattner48f84872007-05-01 04:59:48 +000093
Chris Lattnercaee0dc2007-04-22 06:23:29 +000094 const char *ErrorString;
95
96 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000097 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000098 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +000099 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +0000100
Devang Patel19c87462008-09-26 22:53:05 +0000101 /// MAttributes - The set of attributes by index. Index zero in the
Chris Lattner48c85b82007-05-04 03:30:17 +0000102 /// file is for null, and is thus not represented here. As such all indices
103 /// are off by one.
Devang Patel19c87462008-09-26 22:53:05 +0000104 std::vector<AttrListPtr> MAttributes;
Chris Lattner48c85b82007-05-04 03:30:17 +0000105
Chris Lattner980e5aa2007-05-01 05:52:21 +0000106 /// FunctionBBs - While parsing a function body, this is a list of the basic
107 /// blocks for the function.
108 std::vector<BasicBlock*> FunctionBBs;
109
Chris Lattner48f84872007-05-01 04:59:48 +0000110 // When reading the module header, this list is populated with functions that
111 // have bodies later in the file.
112 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000113
114 // When intrinsic functions are encountered which require upgrading they are
115 // stored here with their replacement function.
116 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
117 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000118
119 // After the module header has been read, the FunctionsWithBodies list is
120 // reversed. This keeps track of whether we've done this yet.
121 bool HasReversedFunctionsWithBodies;
122
123 /// DeferredFunctionInfo - When function bodies are initially scanned, this
124 /// map contains info about where to find deferred function body (in the
125 /// stream) and what linkage the original function had.
126 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000127public:
Owen Anderson4434ed42009-07-01 23:13:44 +0000128 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
Owen Anderson8b477ed2009-07-01 16:58:40 +0000129 : Context(C), Buffer(buffer), ErrorString(0) {
Chris Lattner48f84872007-05-01 04:59:48 +0000130 HasReversedFunctionsWithBodies = false;
131 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000132 ~BitcodeReader() {
133 FreeState();
134 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000135
Chris Lattnerb348bb82007-05-18 04:02:46 +0000136 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000137
138 /// releaseMemoryBuffer - This causes the reader to completely forget about
139 /// the memory buffer it contains, which prevents the buffer from being
140 /// destroyed when it is deleted.
141 void releaseMemoryBuffer() {
142 Buffer = 0;
143 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000144
Chris Lattner48f84872007-05-01 04:59:48 +0000145 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000146 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000147 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000148 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000149
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000150 bool Error(const char *Str) {
151 ErrorString = Str;
152 return true;
153 }
154 const char *getErrorString() const { return ErrorString; }
155
156 /// @brief Main interface to parsing a bitcode buffer.
157 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000158 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000159private:
160 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000161 Value *getFnValueByID(unsigned ID, const Type *Ty) {
162 return ValueList.getValueFwdRef(ID, Ty);
163 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000164 BasicBlock *getBasicBlock(unsigned ID) const {
165 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
166 return FunctionBBs[ID];
167 }
Devang Patel05988662008-09-25 21:00:45 +0000168 AttrListPtr getAttributes(unsigned i) const {
Devang Patel19c87462008-09-26 22:53:05 +0000169 if (i-1 < MAttributes.size())
170 return MAttributes[i-1];
Devang Patel05988662008-09-25 21:00:45 +0000171 return AttrListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000172 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000173
174 /// getValueTypePair - Read a value/type pair out of the specified record from
175 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
176 /// Return true on failure.
177 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
178 unsigned InstNum, Value *&ResVal) {
179 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000180 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000181 if (ValNo < InstNum) {
182 // If this is not a forward reference, just return the value we already
183 // have.
184 ResVal = getFnValueByID(ValNo, 0);
185 return ResVal == 0;
186 } else if (Slot == Record.size()) {
187 return true;
188 }
189
Jeff Cohen650c9382007-05-06 03:23:14 +0000190 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000191 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
192 return ResVal == 0;
193 }
194 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
195 const Type *Ty, Value *&ResVal) {
196 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000197 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000198 ResVal = getFnValueByID(ValNo, Ty);
199 return ResVal == 0;
200 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000201
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000202
Chris Lattner86697142007-05-01 05:01:34 +0000203 bool ParseModule(const std::string &ModuleID);
Devang Patel05988662008-09-25 21:00:45 +0000204 bool ParseAttributeBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000205 bool ParseTypeTable();
206 bool ParseTypeSymbolTable();
207 bool ParseValueSymbolTable();
208 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000209 bool RememberAndSkipFunctionBody();
210 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000211 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000212};
213
214} // End llvm namespace
215
216#endif