blob: bbbe4ec43a206ff3b4bc8fa537a35a73a84d0998 [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;
Owen Anderson74a77812009-07-07 20:18:58 +000047 LLVMContext& Context;
Chris Lattner522b7b12007-04-24 05:48:56 +000048public:
Owen Anderson74a77812009-07-07 20:18:58 +000049 BitcodeReaderValueList(LLVMContext& C) : Context(C) {}
Chris Lattnerea693df2008-08-21 02:34:16 +000050 ~BitcodeReaderValueList() {
51 assert(ResolveConstants.empty() && "Constants not resolved?");
52 }
Gabor Greifefe65362008-05-10 08:32:32 +000053
Chris Lattner522b7b12007-04-24 05:48:56 +000054 // vector compatibility methods
Chris Lattner46e77402009-03-31 22:55:09 +000055 unsigned size() const { return ValuePtrs.size(); }
56 void resize(unsigned N) { ValuePtrs.resize(N); }
Chris Lattner522b7b12007-04-24 05:48:56 +000057 void push_back(Value *V) {
Chris Lattner46e77402009-03-31 22:55:09 +000058 ValuePtrs.push_back(V);
Chris Lattner522b7b12007-04-24 05:48:56 +000059 }
60
Chris Lattnerb348bb82007-05-18 04:02:46 +000061 void clear() {
Chris Lattnerea693df2008-08-21 02:34:16 +000062 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattner46e77402009-03-31 22:55:09 +000063 ValuePtrs.clear();
Chris Lattnerb348bb82007-05-18 04:02:46 +000064 }
65
Chris Lattner46e77402009-03-31 22:55:09 +000066 Value *operator[](unsigned i) const {
67 assert(i < ValuePtrs.size());
68 return ValuePtrs[i];
69 }
Chris Lattner522b7b12007-04-24 05:48:56 +000070
Chris Lattner46e77402009-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(); }
Chris Lattner198f34a2007-04-26 03:27:58 +000074 void shrinkTo(unsigned N) {
Chris Lattner46e77402009-03-31 22:55:09 +000075 assert(N <= size() && "Invalid shrinkTo request!");
76 ValuePtrs.resize(N);
Chris Lattner198f34a2007-04-26 03:27:58 +000077 }
Chris Lattner522b7b12007-04-24 05:48:56 +000078
79 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000080 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
81
Chris Lattner46e77402009-03-31 22:55:09 +000082 void AssignValue(Value *V, unsigned Idx);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000083
Chris Lattnerea693df2008-08-21 02:34:16 +000084 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
85 /// resolves any forward references.
86 void ResolveConstantForwardRefs();
Chris Lattner522b7b12007-04-24 05:48:56 +000087};
Gabor Greifefe65362008-05-10 08:32:32 +000088
Chris Lattnercaee0dc2007-04-22 06:23:29 +000089class BitcodeReader : public ModuleProvider {
Owen Anderson4434ed42009-07-01 23:13:44 +000090 LLVMContext& Context;
Chris Lattnerc453f762007-04-29 07:54:31 +000091 MemoryBuffer *Buffer;
Chris Lattner962dde32009-04-26 20:59:02 +000092 BitstreamReader StreamFile;
93 BitstreamCursor Stream;
Chris Lattner48f84872007-05-01 04:59:48 +000094
Chris Lattnercaee0dc2007-04-22 06:23:29 +000095 const char *ErrorString;
96
97 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000098 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000099 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +0000100 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +0000101
Devang Patel19c87462008-09-26 22:53:05 +0000102 /// MAttributes - The set of attributes by index. Index zero in the
Chris Lattner48c85b82007-05-04 03:30:17 +0000103 /// file is for null, and is thus not represented here. As such all indices
104 /// are off by one.
Devang Patel19c87462008-09-26 22:53:05 +0000105 std::vector<AttrListPtr> MAttributes;
Chris Lattner48c85b82007-05-04 03:30:17 +0000106
Chris Lattner980e5aa2007-05-01 05:52:21 +0000107 /// FunctionBBs - While parsing a function body, this is a list of the basic
108 /// blocks for the function.
109 std::vector<BasicBlock*> FunctionBBs;
110
Chris Lattner48f84872007-05-01 04:59:48 +0000111 // When reading the module header, this list is populated with functions that
112 // have bodies later in the file.
113 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000114
115 // When intrinsic functions are encountered which require upgrading they are
116 // stored here with their replacement function.
117 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
118 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000119
120 // After the module header has been read, the FunctionsWithBodies list is
121 // reversed. This keeps track of whether we've done this yet.
122 bool HasReversedFunctionsWithBodies;
123
124 /// DeferredFunctionInfo - When function bodies are initially scanned, this
125 /// map contains info about where to find deferred function body (in the
126 /// stream) and what linkage the original function had.
127 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000128public:
Owen Anderson4434ed42009-07-01 23:13:44 +0000129 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
Owen Anderson74a77812009-07-07 20:18:58 +0000130 : Context(C), Buffer(buffer), ErrorString(0), ValueList(C) {
Chris Lattner48f84872007-05-01 04:59:48 +0000131 HasReversedFunctionsWithBodies = false;
132 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000133 ~BitcodeReader() {
134 FreeState();
135 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000136
Chris Lattnerb348bb82007-05-18 04:02:46 +0000137 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000138
139 /// releaseMemoryBuffer - This causes the reader to completely forget about
140 /// the memory buffer it contains, which prevents the buffer from being
141 /// destroyed when it is deleted.
142 void releaseMemoryBuffer() {
143 Buffer = 0;
144 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000145
Chris Lattner48f84872007-05-01 04:59:48 +0000146 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000147 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000148 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000149 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000150
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000151 bool Error(const char *Str) {
152 ErrorString = Str;
153 return true;
154 }
155 const char *getErrorString() const { return ErrorString; }
156
157 /// @brief Main interface to parsing a bitcode buffer.
158 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000159 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000160private:
161 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000162 Value *getFnValueByID(unsigned ID, const Type *Ty) {
163 return ValueList.getValueFwdRef(ID, Ty);
164 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000165 BasicBlock *getBasicBlock(unsigned ID) const {
166 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
167 return FunctionBBs[ID];
168 }
Devang Patel05988662008-09-25 21:00:45 +0000169 AttrListPtr getAttributes(unsigned i) const {
Devang Patel19c87462008-09-26 22:53:05 +0000170 if (i-1 < MAttributes.size())
171 return MAttributes[i-1];
Devang Patel05988662008-09-25 21:00:45 +0000172 return AttrListPtr();
Chris Lattner48c85b82007-05-04 03:30:17 +0000173 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000174
175 /// getValueTypePair - Read a value/type pair out of the specified record from
176 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
177 /// Return true on failure.
178 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
179 unsigned InstNum, Value *&ResVal) {
180 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000181 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000182 if (ValNo < InstNum) {
183 // If this is not a forward reference, just return the value we already
184 // have.
185 ResVal = getFnValueByID(ValNo, 0);
186 return ResVal == 0;
187 } else if (Slot == Record.size()) {
188 return true;
189 }
190
Jeff Cohen650c9382007-05-06 03:23:14 +0000191 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000192 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
193 return ResVal == 0;
194 }
195 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
196 const Type *Ty, Value *&ResVal) {
197 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000198 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000199 ResVal = getFnValueByID(ValNo, Ty);
200 return ResVal == 0;
201 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000202
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000203
Chris Lattner86697142007-05-01 05:01:34 +0000204 bool ParseModule(const std::string &ModuleID);
Devang Patel05988662008-09-25 21:00:45 +0000205 bool ParseAttributeBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000206 bool ParseTypeTable();
207 bool ParseTypeSymbolTable();
208 bool ParseValueSymbolTable();
209 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000210 bool RememberAndSkipFunctionBody();
211 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000212 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000213};
214
215} // End llvm namespace
216
217#endif