blob: b70a99c05761992954bcee12c3ee473048e7a670 [file] [log] [blame]
Chris Lattnercaee0dc2007-04-22 06:23:29 +00001//===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
42 Value *operator[](unsigned i) const { return getOperand(i); }
43
44 Value *back() const { return Uses.back(); }
45 void pop_back() { Uses.pop_back(); --NumOperands; }
46 bool empty() const { return NumOperands == 0; }
Chris Lattner198f34a2007-04-26 03:27:58 +000047 void shrinkTo(unsigned N) {
Chris Lattner980e5aa2007-05-01 05:52:21 +000048 assert(N <= NumOperands && "Invalid shrinkTo request!");
Chris Lattner198f34a2007-04-26 03:27:58 +000049 Uses.resize(N);
50 NumOperands = N;
51 }
Chris Lattner522b7b12007-04-24 05:48:56 +000052 virtual void print(std::ostream&) const {}
53
54 Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
Chris Lattnera7c49aa2007-05-01 07:01:57 +000055 Value *getValueFwdRef(unsigned Idx, const Type *Ty);
56
57 void AssignValue(Value *V, unsigned Idx) {
58 if (Idx == size()) {
59 push_back(V);
60 } else if (Value *OldV = getOperand(Idx)) {
61 // If there was a forward reference to this value, replace it.
62 setOperand(Idx, V);
63 OldV->replaceAllUsesWith(V);
64 delete OldV;
65 } else {
66 initVal(Idx, V);
67 }
68 }
69
70private:
Chris Lattner522b7b12007-04-24 05:48:56 +000071 void initVal(unsigned Idx, Value *V) {
72 assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
73 Uses[Idx].init(V, this);
74 }
75};
76
Chris Lattnercaee0dc2007-04-22 06:23:29 +000077
78class BitcodeReader : public ModuleProvider {
Chris Lattnerc453f762007-04-29 07:54:31 +000079 MemoryBuffer *Buffer;
Chris Lattner48f84872007-05-01 04:59:48 +000080 BitstreamReader Stream;
81
Chris Lattnercaee0dc2007-04-22 06:23:29 +000082 const char *ErrorString;
83
84 std::vector<PATypeHolder> TypeList;
Chris Lattner522b7b12007-04-24 05:48:56 +000085 BitcodeReaderValueList ValueList;
Chris Lattnere16504e2007-04-24 03:30:34 +000086 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
Chris Lattner07d98b42007-04-26 02:46:40 +000087 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
Chris Lattner48f84872007-05-01 04:59:48 +000088
Chris Lattner48c85b82007-05-04 03:30:17 +000089 /// ParamAttrs - The set of parameter attributes by index. Index zero in the
90 /// file is for null, and is thus not represented here. As such all indices
91 /// are off by one.
92 std::vector<const ParamAttrsList*> ParamAttrs;
93
Chris Lattner980e5aa2007-05-01 05:52:21 +000094 /// FunctionBBs - While parsing a function body, this is a list of the basic
95 /// blocks for the function.
96 std::vector<BasicBlock*> FunctionBBs;
97
Chris Lattner48f84872007-05-01 04:59:48 +000098 // When reading the module header, this list is populated with functions that
99 // have bodies later in the file.
100 std::vector<Function*> FunctionsWithBodies;
101
102 // After the module header has been read, the FunctionsWithBodies list is
103 // reversed. This keeps track of whether we've done this yet.
104 bool HasReversedFunctionsWithBodies;
105
106 /// DeferredFunctionInfo - When function bodies are initially scanned, this
107 /// map contains info about where to find deferred function body (in the
108 /// stream) and what linkage the original function had.
109 DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000110public:
Chris Lattner48f84872007-05-01 04:59:48 +0000111 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
112 HasReversedFunctionsWithBodies = false;
113 }
Chris Lattnerc453f762007-04-29 07:54:31 +0000114 ~BitcodeReader();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000115
Chris Lattnerc453f762007-04-29 07:54:31 +0000116
117 /// releaseMemoryBuffer - This causes the reader to completely forget about
118 /// the memory buffer it contains, which prevents the buffer from being
119 /// destroyed when it is deleted.
120 void releaseMemoryBuffer() {
121 Buffer = 0;
122 }
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000123
Chris Lattner48f84872007-05-01 04:59:48 +0000124 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
Chris Lattner980e5aa2007-05-01 05:52:21 +0000125 virtual Module *materializeModule(std::string *ErrInfo = 0);
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000126
127 bool Error(const char *Str) {
128 ErrorString = Str;
129 return true;
130 }
131 const char *getErrorString() const { return ErrorString; }
132
133 /// @brief Main interface to parsing a bitcode buffer.
134 /// @returns true if an error occurred.
Chris Lattnerc453f762007-04-29 07:54:31 +0000135 bool ParseBitcode();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000136private:
137 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
Chris Lattnera7c49aa2007-05-01 07:01:57 +0000138 Value *getFnValueByID(unsigned ID, const Type *Ty) {
139 return ValueList.getValueFwdRef(ID, Ty);
140 }
Chris Lattnerf4c8e522007-05-02 05:46:45 +0000141 BasicBlock *getBasicBlock(unsigned ID) const {
142 if (ID >= FunctionBBs.size()) return 0; // Invalid ID
143 return FunctionBBs[ID];
144 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000145 const ParamAttrsList *getParamAttrs(unsigned i) const {
146 if (i-1 < ParamAttrs.size())
147 return ParamAttrs[i-1];
148 return 0;
149 }
Chris Lattner7337ab92007-05-06 00:00:00 +0000150
151 /// getValueTypePair - Read a value/type pair out of the specified record from
152 /// slot 'Slot'. Increment Slot past the number of slots used in the record.
153 /// Return true on failure.
154 bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
155 unsigned InstNum, Value *&ResVal) {
156 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000157 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000158 if (ValNo < InstNum) {
159 // If this is not a forward reference, just return the value we already
160 // have.
161 ResVal = getFnValueByID(ValNo, 0);
162 return ResVal == 0;
163 } else if (Slot == Record.size()) {
164 return true;
165 }
166
Jeff Cohen650c9382007-05-06 03:23:14 +0000167 unsigned TypeNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000168 ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
169 return ResVal == 0;
170 }
171 bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
172 const Type *Ty, Value *&ResVal) {
173 if (Slot == Record.size()) return true;
Jeff Cohen650c9382007-05-06 03:23:14 +0000174 unsigned ValNo = (unsigned)Record[Slot++];
Chris Lattner7337ab92007-05-06 00:00:00 +0000175 ResVal = getFnValueByID(ValNo, Ty);
176 return ResVal == 0;
177 }
Chris Lattner48c85b82007-05-04 03:30:17 +0000178
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000179
Chris Lattner86697142007-05-01 05:01:34 +0000180 bool ParseModule(const std::string &ModuleID);
Chris Lattner48c85b82007-05-04 03:30:17 +0000181 bool ParseParamAttrBlock();
Chris Lattner86697142007-05-01 05:01:34 +0000182 bool ParseTypeTable();
183 bool ParseTypeSymbolTable();
184 bool ParseValueSymbolTable();
185 bool ParseConstants();
Chris Lattner980e5aa2007-05-01 05:52:21 +0000186 bool RememberAndSkipFunctionBody();
187 bool ParseFunctionBody(Function *F);
Chris Lattner07d98b42007-04-26 02:46:40 +0000188 bool ResolveGlobalAndAliasInits();
Chris Lattnercaee0dc2007-04-22 06:23:29 +0000189};
190
191} // End llvm namespace
192
193#endif