blob: 662631bce950748a0f9248be1de83ccc8b5a7b6b [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
17#include "llvm/ModuleProvider.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;
Owen Anderson25209b42009-07-01 16:58:40 +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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047public:
Chris Lattnered490d12009-03-31 22:55:09 +000048 BitcodeReaderValueList() {}
Chris Lattner345bbb72008-08-21 02:34:16 +000049 ~BitcodeReaderValueList() {
50 assert(ResolveConstants.empty() && "Constants not resolved?");
51 }
Gabor Greif25bc3222008-05-10 08:32:32 +000052
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 // vector compatibility methods
Chris Lattnered490d12009-03-31 22:55:09 +000054 unsigned size() const { return ValuePtrs.size(); }
55 void resize(unsigned N) { ValuePtrs.resize(N); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 void push_back(Value *V) {
Chris Lattnered490d12009-03-31 22:55:09 +000057 ValuePtrs.push_back(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 }
59
60 void clear() {
Chris Lattner345bbb72008-08-21 02:34:16 +000061 assert(ResolveConstants.empty() && "Constants not resolved?");
Chris Lattnered490d12009-03-31 22:55:09 +000062 ValuePtrs.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 }
64
Chris Lattnered490d12009-03-31 22:55:09 +000065 Value *operator[](unsigned i) const {
66 assert(i < ValuePtrs.size());
67 return ValuePtrs[i];
68 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
Chris Lattnered490d12009-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(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 void shrinkTo(unsigned N) {
Chris Lattnered490d12009-03-31 22:55:09 +000074 assert(N <= size() && "Invalid shrinkTo request!");
75 ValuePtrs.resize(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
78 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
79 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
80
Chris Lattnered490d12009-03-31 22:55:09 +000081 void AssignValue(Value *V, unsigned Idx);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
Chris Lattner345bbb72008-08-21 02:34:16 +000083 /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
84 /// resolves any forward references.
85 void ResolveConstantForwardRefs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086};
Gabor Greif25bc3222008-05-10 08:32:32 +000087
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088class BitcodeReader : public ModuleProvider {
Owen Anderson92adb182009-07-01 23:13:44 +000089 LLVMContext& Context;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 MemoryBuffer *Buffer;
Chris Lattner25a6abf2009-04-26 20:59:02 +000091 BitstreamReader StreamFile;
92 BitstreamCursor Stream;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093
94 const char *ErrorString;
95
96 std::vector<PATypeHolder> TypeList;
97 BitcodeReaderValueList ValueList;
98 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
99 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
100
Devang Patelf2a4a922008-09-26 22:53:05 +0000101 /// MAttributes - The set of attributes by index. Index zero in the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 /// file is for null, and is thus not represented here. As such all indices
103 /// are off by one.
Devang Patelf2a4a922008-09-26 22:53:05 +0000104 std::vector<AttrListPtr> MAttributes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 /// FunctionBBs - While parsing a function body, this is a list of the basic
107 /// blocks for the function.
108 std::vector<BasicBlock*> FunctionBBs;
109
110 // 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 Carrutha228e392007-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
127public:
Owen Anderson92adb182009-07-01 23:13:44 +0000128 explicit BitcodeReader(MemoryBuffer *buffer, LLVMContext& C)
Owen Anderson25209b42009-07-01 16:58:40 +0000129 : Context(C), Buffer(buffer), ErrorString(0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 HasReversedFunctionsWithBodies = false;
131 }
132 ~BitcodeReader() {
133 FreeState();
134 }
135
136 void FreeState();
137
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 }
144
145 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
146 virtual Module *materializeModule(std::string *ErrInfo = 0);
147 virtual void dematerializeFunction(Function *F);
148 virtual Module *releaseModule(std::string *ErrInfo = 0);
149
150 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.
158 bool ParseBitcode();
159private:
160 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
161 Value *getFnValueByID(unsigned ID, const Type *Ty) {
162 return ValueList.getValueFwdRef(ID, Ty);
163 }
164 BasicBlock *getBasicBlock(unsigned ID) const {
165 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
166 return FunctionBBs[ID];
167 }
Devang Pateld222f862008-09-25 21:00:45 +0000168 AttrListPtr getAttributes(unsigned i) const {
Devang Patelf2a4a922008-09-26 22:53:05 +0000169 if (i-1 < MAttributes.size())
170 return MAttributes[i-1];
Devang Pateld222f862008-09-25 21:00:45 +0000171 return AttrListPtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 }
173
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;
180 unsigned ValNo = (unsigned)Record[Slot++];
181 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
190 unsigned TypeNo = (unsigned)Record[Slot++];
191 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;
197 unsigned ValNo = (unsigned)Record[Slot++];
198 ResVal = getFnValueByID(ValNo, Ty);
199 return ResVal == 0;
200 }
201
202
203 bool ParseModule(const std::string &ModuleID);
Devang Pateld222f862008-09-25 21:00:45 +0000204 bool ParseAttributeBlock();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 bool ParseTypeTable();
206 bool ParseTypeSymbolTable();
207 bool ParseValueSymbolTable();
208 bool ParseConstants();
209 bool RememberAndSkipFunctionBody();
210 bool ParseFunctionBody(Function *F);
211 bool ResolveGlobalAndAliasInits();
212};
213
214} // End llvm namespace
215
216#endif