blob: d7df1a1b02989908b8a8328bdd3e152ab73ea085 [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 Lattner522b7b12007-04-24 05:48:56 +000018#include "llvm/Type.h"
19#include "llvm/User.h"
Chris Lattner48f84872007-05-01 04:59:48 +000020#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner47f96bf2007-04-23 01:01:37 +000021#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattner48f84872007-05-01 04:59:48 +000022#include "llvm/ADT/DenseMap.h"
Chris Lattnercaee0dc2007-04-22 06:23:29 +000023#include <vector>
24
25namespace llvm {
Chris Lattnerc453f762007-04-29 07:54:31 +000026 class MemoryBuffer;
Chris Lattner48c85b82007-05-04 03:30:17 +000027 class ParamAttrsList;
Chris Lattner522b7b12007-04-24 05:48:56 +000028
29class BitcodeReaderValueList : public User {
30 std::vector<Use> Uses;
31public:
32 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33
34 // vector compatibility methods
35 unsigned size() const { return getNumOperands(); }
36 void push_back(Value *V) {
37 Uses.push_back(Use(V, this));
38 OperandList = &Uses[0];
39 ++NumOperands;
40 }
41
Chris Lattnerb348bb82007-05-18 04:02:46 +000042 void clear() {
43 std::vector<Use>().swap(Uses);
44 }
45
Chris Lattner522b7b12007-04-24 05:48:56 +000046 Value *operator[](unsigned i) const { return getOperand(i); }
47
48 Value *back() const { return Uses.back(); }
49 void pop_back() { Uses.pop_back(); --NumOperands; }
50 bool empty() const { return NumOperands == 0; }
Chris Lattner198f34a2007-04-26 03:27:58 +000051 void shrinkTo(unsigned N) {
Chris Lattner980e5aa2007-05-01 05:52:21 +000052 assert(N <= NumOperands && "Invalid shrinkTo request!");
Chris Lattner198f34a2007-04-26 03:27:58 +000053 Uses.resize(N);
54 NumOperands = N;
55 }
Chris Lattner522b7b12007-04-24 05:48:56 +000056 virtual void print(std::ostream&) const {}
57
58 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000059 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
60
61 void AssignValue(Value *V, unsigned Idx) {
62 if (Idx == size()) {
63 push_back(V);
64 } else if (Value *OldV = getOperand(Idx)) {
65 // If there was a forward reference to this value, replace it.
66 setOperand(Idx, V);
67 OldV->replaceAllUsesWith(V);
68 delete OldV;
69 } else {
70 initVal(Idx, V);
71 }
72 }
73
74private:
Chris Lattner522b7b12007-04-24 05:48:56 +000075 void initVal(unsigned Idx, Value *V) {
76 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
77 Uses[Idx].init(V, this);
78 }
79};
80
Chris Lattnercaee0dc2007-04-22 06:23:29 +000081
82class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +000083 MemoryBuffer *Buffer;
Chris Lattner48f84872007-05-01 04:59:48 +000084 BitstreamReader Stream;
85
Chris Lattnercaee0dc2007-04-22 06:23:29 +000086 const char *ErrorString;
87
88 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000089 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000090 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +000091 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +000092
Chris Lattner48c85b82007-05-04 03:30:17 +000093 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
94 /// file is for null, and is thus not represented here. As such all indices
95 /// are off by one.
96 std::vector<const ParamAttrsList*> ParamAttrs;
97
Chris Lattner980e5aa2007-05-01 05:52:21 +000098 /// FunctionBBs - While parsing a function body, this is a list of the basic
99 /// blocks for the function.
100 std::vector<BasicBlock*> FunctionBBs;
101
Chris Lattner48f84872007-05-01 04:59:48 +0000102 // When reading the module header, this list is populated with functions that
103 // have bodies later in the file.
104 std::vector<Function*> FunctionsWithBodies;
Chandler Carruth69940402007-08-04 01:51:18 +0000105
106 // When intrinsic functions are encountered which require upgrading they are
107 // stored here with their replacement function.
108 typedef std::vector<std::pair<Function*, Function*> > UpgradedIntrinsicMap;
109 UpgradedIntrinsicMap UpgradedIntrinsics;
Chris Lattner48f84872007-05-01 04:59:48 +0000110
111 // After the module header has been read, the FunctionsWithBodies list is
112 // reversed. This keeps track of whether we've done this yet.
113 bool HasReversedFunctionsWithBodies;
114
115 /// DeferredFunctionInfo - When function bodies are initially scanned, this
116 /// map contains info about where to find deferred function body (in the
117 /// stream) and what linkage the original function had.
118 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000119public:
Chris Lattner48f84872007-05-01 04:59:48 +0000120 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
121 HasReversedFunctionsWithBodies = false;
122 }
Chris Lattnerb348bb82007-05-18 04:02:46 +0000123 ~BitcodeReader() {
124 FreeState();
125 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000126
Chris Lattnerb348bb82007-05-18 04:02:46 +0000127 void FreeState();
Chris Lattnerc453f762007-04-29 07:54:31 +0000128
129 /// releaseMemoryBuffer - This causes the reader to completely forget about
130 /// the memory buffer it contains, which prevents the buffer from being
131 /// destroyed when it is deleted.
132 void releaseMemoryBuffer() {
133 Buffer = 0;
134 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000135
Chris Lattner48f84872007-05-01 04:59:48 +0000136 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000137 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000138 virtual void dematerializeFunction(Function *F);
Chris Lattnerb348bb82007-05-18 04:02:46 +0000139 virtual Module *releaseModule(std::string *ErrInfo = 0);
Chris Lattnerd67c6322007-05-15 06:29:44 +0000140
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000141 bool Error(const char *Str) {
142 ErrorString = Str;
143 return true;
144 }
145 const char *getErrorString() const { return ErrorString; }
146
147 /// @brief Main interface to parsing a bitcode buffer.
148 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000149 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000150private:
151 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000152 Value *getFnValueByID(unsigned ID, const Type *Ty) {
153 return ValueList.getValueFwdRef(ID, Ty);
154 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000155 BasicBlock *getBasicBlock(unsigned ID) const {
156 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
157 return FunctionBBs[ID];
158 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000159 const ParamAttrsList *getParamAttrs(unsigned i) const {
160 if (i-1 < ParamAttrs.size())
161 return ParamAttrs[i-1];
162 return 0;
163 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000164
165 /// getValueTypePair - Read a value/type pair out of the specified record from
166 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
167 /// Return true on failure.
168 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
169 unsigned InstNum, Value *&ResVal) {
170 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000171 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000172 if (ValNo < InstNum) {
173 // If this is not a forward reference, just return the value we already
174 // have.
175 ResVal = getFnValueByID(ValNo, 0);
176 return ResVal == 0;
177 } else if (Slot == Record.size()) {
178 return true;
179 }
180
Jeff Cohen650c9382007-05-06 03:23:14 +0000181 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000182 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
183 return ResVal == 0;
184 }
185 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
186 const Type *Ty, Value *&ResVal) {
187 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000188 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000189 ResVal = getFnValueByID(ValNo, Ty);
190 return ResVal == 0;
191 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000192
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000193
Chris Lattner86697142007-05-01 05:01:34 +0000194 bool ParseModule(const std::string &ModuleID);
Chris Lattner48c85b82007-05-04 03:30:17 +0000195 bool ParseParamAttrBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000196 bool ParseTypeTable();
197 bool ParseTypeSymbolTable();
198 bool ParseValueSymbolTable();
199 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000200 bool RememberAndSkipFunctionBody();
201 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000202 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000203};
204
205} // End llvm namespace
206
207#endif