blob: 1b11f22e5cff5156764691270308b9f71f6887dd [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Bytecode/Reader.h
11//
12// Note that this library should be as fast as possible, reentrant, and
13// threadsafe!!
14//
Chris Lattner00950542001-06-06 20:29:01 +000015// TODO: Allow passing in an option to ignore the symbol table
16//
Chris Lattnerd6b65252001-10-24 01:15:12 +000017//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000018
Reid Spencer060d25d2004-06-29 23:29:38 +000019#include "Reader.h"
20#include "llvm/Bytecode/BytecodeHandler.h"
21#include "llvm/BasicBlock.h"
22#include "llvm/Constants.h"
Reid Spencer04cde2c2004-07-04 11:33:49 +000023#include "llvm/Instructions.h"
24#include "llvm/SymbolTable.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/Bytecode/Format.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000026#include "llvm/Support/GetElementPtrTypeIterator.h"
Misha Brukman12c29d12003-09-22 23:38:23 +000027#include "Support/StringExtras.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000028#include <sstream>
29
Chris Lattner29b789b2003-11-19 17:27:18 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Reid Spencer060d25d2004-06-29 23:29:38 +000032/// A convenience macro for handling parsing errors.
33#define PARSE_ERROR(inserters) { \
34 std::ostringstream errormsg; \
35 errormsg << inserters; \
36 throw std::string(errormsg.str()); \
Chris Lattner89e02532004-01-18 21:08:15 +000037 }
38
Reid Spencer060d25d2004-06-29 23:29:38 +000039/// @brief A class for maintaining the slot number definition
40/// as a placeholder for the actual definition.
41template<class SuperType>
42class PlaceholderDef : public SuperType {
43 unsigned ID;
44 PlaceholderDef(); // DO NOT IMPLEMENT
45 void operator=(const PlaceholderDef &); // DO NOT IMPLEMENT
46public:
47 PlaceholderDef(const Type *Ty, unsigned id) : SuperType(Ty), ID(id) {}
48 unsigned getID() { return ID; }
49};
Chris Lattner9e460f22003-10-04 20:00:03 +000050
Reid Spencer060d25d2004-06-29 23:29:38 +000051struct ConstantPlaceHolderHelper : public ConstantExpr {
52 ConstantPlaceHolderHelper(const Type *Ty)
53 : ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty) {}
54};
55
56typedef PlaceholderDef<ConstantPlaceHolderHelper> ConstPHolder;
57
58//===----------------------------------------------------------------------===//
59// Bytecode Reading Methods
60//===----------------------------------------------------------------------===//
61
Reid Spencer04cde2c2004-07-04 11:33:49 +000062/// Determine if the current block being read contains any more data.
Reid Spencer060d25d2004-06-29 23:29:38 +000063inline bool BytecodeReader::moreInBlock() {
64 return At < BlockEnd;
Chris Lattner00950542001-06-06 20:29:01 +000065}
66
Reid Spencer04cde2c2004-07-04 11:33:49 +000067/// Throw an error if we've read past the end of the current block
Reid Spencer060d25d2004-06-29 23:29:38 +000068inline void BytecodeReader::checkPastBlockEnd(const char * block_name) {
69 if ( At > BlockEnd )
70 PARSE_ERROR("Attempt to read past the end of " << block_name << " block.");
71}
Chris Lattner36392bc2003-10-08 21:18:57 +000072
Reid Spencer04cde2c2004-07-04 11:33:49 +000073/// Align the buffer position to a 32 bit boundary
Reid Spencer060d25d2004-06-29 23:29:38 +000074inline void BytecodeReader::align32() {
75 BufPtr Save = At;
76 At = (const unsigned char *)((unsigned long)(At+3) & (~3UL));
77 if ( At > Save )
Reid Spencer04cde2c2004-07-04 11:33:49 +000078 if (Handler) Handler->handleAlignment( At - Save );
Reid Spencer060d25d2004-06-29 23:29:38 +000079 if (At > BlockEnd)
Reid Spencer04cde2c2004-07-04 11:33:49 +000080 throw std::string("Ran out of data while aligning!");
Reid Spencer060d25d2004-06-29 23:29:38 +000081}
82
Reid Spencer04cde2c2004-07-04 11:33:49 +000083/// Read a whole unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000084inline unsigned BytecodeReader::read_uint() {
85 if (At+4 > BlockEnd)
Reid Spencer04cde2c2004-07-04 11:33:49 +000086 throw std::string("Ran out of data reading uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +000087 At += 4;
88 return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24);
89}
90
Reid Spencer04cde2c2004-07-04 11:33:49 +000091/// Read a variable-bit-rate encoded unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000092inline unsigned BytecodeReader::read_vbr_uint() {
93 unsigned Shift = 0;
94 unsigned Result = 0;
95 BufPtr Save = At;
96
97 do {
98 if (At == BlockEnd)
Reid Spencer04cde2c2004-07-04 11:33:49 +000099 throw std::string("Ran out of data reading vbr_uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000100 Result |= (unsigned)((*At++) & 0x7F) << Shift;
101 Shift += 7;
102 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000103 if (Handler) Handler->handleVBR32(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +0000104 return Result;
105}
106
Reid Spencer04cde2c2004-07-04 11:33:49 +0000107/// Read a variable-bit-rate encoded unsigned 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000108inline uint64_t BytecodeReader::read_vbr_uint64() {
109 unsigned Shift = 0;
110 uint64_t Result = 0;
111 BufPtr Save = At;
112
113 do {
114 if (At == BlockEnd)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000115 throw std::string("Ran out of data reading vbr_uint64!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000116 Result |= (uint64_t)((*At++) & 0x7F) << Shift;
117 Shift += 7;
118 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000119 if (Handler) Handler->handleVBR64(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +0000120 return Result;
121}
122
Reid Spencer04cde2c2004-07-04 11:33:49 +0000123/// Read a variable-bit-rate encoded signed 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000124inline int64_t BytecodeReader::read_vbr_int64() {
125 uint64_t R = read_vbr_uint64();
126 if (R & 1) {
127 if (R != 1)
128 return -(int64_t)(R >> 1);
129 else // There is no such thing as -0 with integers. "-0" really means
130 // 0x8000000000000000.
131 return 1LL << 63;
132 } else
133 return (int64_t)(R >> 1);
134}
135
Reid Spencer04cde2c2004-07-04 11:33:49 +0000136/// Read a pascal-style string (length followed by text)
Reid Spencer060d25d2004-06-29 23:29:38 +0000137inline std::string BytecodeReader::read_str() {
138 unsigned Size = read_vbr_uint();
139 const unsigned char *OldAt = At;
140 At += Size;
141 if (At > BlockEnd) // Size invalid?
Reid Spencer04cde2c2004-07-04 11:33:49 +0000142 throw std::string("Ran out of data reading a string!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000143 return std::string((char*)OldAt, Size);
144}
145
Reid Spencer04cde2c2004-07-04 11:33:49 +0000146/// Read an arbitrary block of data
Reid Spencer060d25d2004-06-29 23:29:38 +0000147inline void BytecodeReader::read_data(void *Ptr, void *End) {
148 unsigned char *Start = (unsigned char *)Ptr;
149 unsigned Amount = (unsigned char *)End - Start;
150 if (At+Amount > BlockEnd)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000151 throw std::string("Ran out of data!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000152 std::copy(At, At+Amount, Start);
153 At += Amount;
154}
155
Reid Spencer04cde2c2004-07-04 11:33:49 +0000156/// Read a block header and obtain its type and size
Reid Spencer060d25d2004-06-29 23:29:38 +0000157inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) {
158 Type = read_uint();
159 Size = read_uint();
160 BlockStart = At;
161 if ( At + Size > BlockEnd )
Reid Spencer04cde2c2004-07-04 11:33:49 +0000162 throw std::string("Attempt to size a block past end of memory");
Reid Spencer060d25d2004-06-29 23:29:38 +0000163 BlockEnd = At + Size;
Reid Spencer04cde2c2004-07-04 11:33:49 +0000164 if (Handler) Handler->handleBlock( Type, BlockStart, Size );
165}
166
167
168/// In LLVM 1.2 and before, Types were derived from Value and so they were
169/// written as part of the type planes along with any other Value. In LLVM
170/// 1.3 this changed so that Type does not derive from Value. Consequently,
171/// the BytecodeReader's containers for Values can't contain Types because
172/// there's no inheritance relationship. This means that the "Type Type"
173/// plane is defunct along with the Type::TypeTyID TypeID. In LLVM 1.3
174/// whenever a bytecode construct must have both types and values together,
175/// the types are always read/written first and then the Values. Furthermore
176/// since Type::TypeTyID no longer exists, its value (12) now corresponds to
177/// Type::LabelTyID. In order to overcome this we must "sanitize" all the
178/// type TypeIDs we encounter. For LLVM 1.3 bytecode files, there's no change.
179/// For LLVM 1.2 and before, this function will decrement the type id by
180/// one to account for the missing Type::TypeTyID enumerator if the value is
181/// larger than 12 (Type::LabelTyID). If the value is exactly 12, then this
182/// function returns true, otherwise false. This helps detect situations
183/// where the pre 1.3 bytecode is indicating that what follows is a type.
184/// @returns true iff type id corresponds to pre 1.3 "type type"
185inline bool BytecodeReader::sanitizeTypeId(unsigned &TypeId ) {
186 if ( hasTypeDerivedFromValue ) { /// do nothing if 1.3 or later
187 if ( TypeId == Type::LabelTyID ) {
188 TypeId = Type::VoidTyID; // sanitize it
189 return true; // indicate we got TypeTyID in pre 1.3 bytecode
190 } else if ( TypeId > Type::LabelTyID )
191 --TypeId; // shift all planes down because type type plane is missing
192 }
193 return false;
194}
195
196/// Reads a vbr uint to read in a type id and does the necessary
197/// conversion on it by calling sanitizeTypeId.
198/// @returns true iff \p TypeId read corresponds to a pre 1.3 "type type"
199/// @see sanitizeTypeId
200inline bool BytecodeReader::read_typeid(unsigned &TypeId) {
201 TypeId = read_vbr_uint();
202 return sanitizeTypeId(TypeId);
Reid Spencer060d25d2004-06-29 23:29:38 +0000203}
204
205//===----------------------------------------------------------------------===//
206// IR Lookup Methods
207//===----------------------------------------------------------------------===//
208
Reid Spencer04cde2c2004-07-04 11:33:49 +0000209/// Determine if a type id has an implicit null value
Reid Spencer060d25d2004-06-29 23:29:38 +0000210inline bool BytecodeReader::hasImplicitNull(unsigned TyID ) {
211 if (!hasExplicitPrimitiveZeros)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000212 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Reid Spencer060d25d2004-06-29 23:29:38 +0000213 return TyID >= Type::FirstDerivedTyID;
214}
215
Reid Spencer04cde2c2004-07-04 11:33:49 +0000216/// Obtain a type given a typeid and account for things like compaction tables,
217/// function level vs module level, and the offsetting for the primitive types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000218const Type *BytecodeReader::getType(unsigned ID) {
Chris Lattner89e02532004-01-18 21:08:15 +0000219 if (ID < Type::FirstDerivedTyID)
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000220 if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
Chris Lattner927b1852003-10-09 20:22:47 +0000221 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +0000222
223 // Otherwise, derived types need offset...
Chris Lattner89e02532004-01-18 21:08:15 +0000224 ID -= Type::FirstDerivedTyID;
225
Reid Spencer060d25d2004-06-29 23:29:38 +0000226 if (!CompactionTypes.empty()) {
227 if (ID >= CompactionTypes.size())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000228 throw std::string("Type ID out of range for compaction table!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000229 return CompactionTypes[ID];
Chris Lattner89e02532004-01-18 21:08:15 +0000230 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000231
232 // Is it a module-level type?
Reid Spencer060d25d2004-06-29 23:29:38 +0000233 if (ID < ModuleTypes.size())
234 return ModuleTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000235
Reid Spencer060d25d2004-06-29 23:29:38 +0000236 // Nope, is it a function-level type?
237 ID -= ModuleTypes.size();
238 if (ID < FunctionTypes.size())
239 return FunctionTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000240
Reid Spencer04cde2c2004-07-04 11:33:49 +0000241 throw std::string("Illegal type reference!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000242 return Type::VoidTy;
Chris Lattner00950542001-06-06 20:29:01 +0000243}
244
Reid Spencer04cde2c2004-07-04 11:33:49 +0000245/// Get a sanitized type id. This just makes sure that the \p ID
246/// is both sanitized and not the "type type" of pre-1.3 bytecode.
247/// @see sanitizeTypeId
248inline const Type* BytecodeReader::getSanitizedType(unsigned& ID) {
249 bool isTypeType = sanitizeTypeId(ID);
250 assert(!isTypeType && "Invalid type id occurred");
251 return getType(ID);
252}
253
254/// This method just saves some coding. It uses read_typeid to read
255/// in a sanitized type id, asserts that its not the type type, and
256/// then calls getType to return the type value.
257inline const Type* BytecodeReader::readSanitizedType() {
258 unsigned ID;
259 bool isTypeType = read_typeid(ID);
260 assert(!isTypeType && "Invalid type id occurred");
261 return getType(ID);
262}
263
264/// Get the slot number associated with a type accounting for primitive
265/// types, compaction tables, and function level vs module level.
Reid Spencer060d25d2004-06-29 23:29:38 +0000266unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
267 if (Ty->isPrimitiveType())
268 return Ty->getTypeID();
269
270 // Scan the compaction table for the type if needed.
271 if (!CompactionTypes.empty()) {
272 std::vector<const Type*>::const_iterator I =
Reid Spencer04cde2c2004-07-04 11:33:49 +0000273 find(CompactionTypes.begin(), CompactionTypes.end(), Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +0000274
275 if (I == CompactionTypes.end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000276 throw std::string("Couldn't find type specified in compaction table!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000277 return Type::FirstDerivedTyID + (&*I - &CompactionTypes[0]);
278 }
279
280 // Check the function level types first...
281 TypeListTy::iterator I = find(FunctionTypes.begin(), FunctionTypes.end(), Ty);
282
283 if (I != FunctionTypes.end())
284 return Type::FirstDerivedTyID + ModuleTypes.size() +
Reid Spencer04cde2c2004-07-04 11:33:49 +0000285 (&*I - &FunctionTypes[0]);
Reid Spencer060d25d2004-06-29 23:29:38 +0000286
287 // Check the module level types now...
288 I = find(ModuleTypes.begin(), ModuleTypes.end(), Ty);
289 if (I == ModuleTypes.end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000290 throw std::string("Didn't find type in ModuleTypes.");
Reid Spencer060d25d2004-06-29 23:29:38 +0000291 return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
Chris Lattner80b97342004-01-17 23:25:43 +0000292}
293
Reid Spencer04cde2c2004-07-04 11:33:49 +0000294/// This is just like getType, but when a compaction table is in use, it is
295/// ignored. It also ignores function level types.
296/// @see getType
Reid Spencer060d25d2004-06-29 23:29:38 +0000297const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
298 if (Slot < Type::FirstDerivedTyID) {
299 const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
300 assert(Ty && "Not a primitive type ID?");
301 return Ty;
302 }
303 Slot -= Type::FirstDerivedTyID;
304 if (Slot >= ModuleTypes.size())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000305 throw std::string("Illegal compaction table type reference!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000306 return ModuleTypes[Slot];
Chris Lattner52e20b02003-03-19 20:54:26 +0000307}
308
Reid Spencer04cde2c2004-07-04 11:33:49 +0000309/// This is just like getTypeSlot, but when a compaction table is in use, it
310/// is ignored. It also ignores function level types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000311unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
312 if (Ty->isPrimitiveType())
313 return Ty->getTypeID();
314 TypeListTy::iterator I = find(ModuleTypes.begin(),
Reid Spencer04cde2c2004-07-04 11:33:49 +0000315 ModuleTypes.end(), Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +0000316 if (I == ModuleTypes.end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000317 throw std::string("Didn't find type in ModuleTypes.");
Reid Spencer060d25d2004-06-29 23:29:38 +0000318 return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
319}
320
Reid Spencer04cde2c2004-07-04 11:33:49 +0000321/// Retrieve a value of a given type and slot number, possibly creating
322/// it if it doesn't already exist.
Reid Spencer060d25d2004-06-29 23:29:38 +0000323Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000324 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000325 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000326
Chris Lattner89e02532004-01-18 21:08:15 +0000327 // If there is a compaction table active, it defines the low-level numbers.
328 // If not, the module values define the low-level numbers.
Reid Spencer060d25d2004-06-29 23:29:38 +0000329 if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
330 if (Num < CompactionValues[type].size())
331 return CompactionValues[type][Num];
332 Num -= CompactionValues[type].size();
Chris Lattner89e02532004-01-18 21:08:15 +0000333 } else {
Reid Spencer060d25d2004-06-29 23:29:38 +0000334 // By default, the global type id is the type id passed in
Chris Lattner52f86d62004-01-20 00:54:06 +0000335 unsigned GlobalTyID = type;
Reid Spencer060d25d2004-06-29 23:29:38 +0000336
337 // If the type plane was compactified, figure out the global type ID
338 // by adding the derived type ids and the distance.
Reid Spencer04cde2c2004-07-04 11:33:49 +0000339 if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000340 const Type *Ty = CompactionTypes[type-Type::FirstDerivedTyID];
341 TypeListTy::iterator I =
Reid Spencer04cde2c2004-07-04 11:33:49 +0000342 find(ModuleTypes.begin(), ModuleTypes.end(), Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +0000343 assert(I != ModuleTypes.end());
344 GlobalTyID = Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
Chris Lattner52f86d62004-01-20 00:54:06 +0000345 }
Chris Lattner00950542001-06-06 20:29:01 +0000346
Reid Spencer060d25d2004-06-29 23:29:38 +0000347 if (hasImplicitNull(GlobalTyID)) {
Chris Lattner89e02532004-01-18 21:08:15 +0000348 if (Num == 0)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000349 return Constant::getNullValue(getType(type));
Chris Lattner89e02532004-01-18 21:08:15 +0000350 --Num;
351 }
352
Chris Lattner52f86d62004-01-20 00:54:06 +0000353 if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
354 if (Num < ModuleValues[GlobalTyID]->size())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000355 return ModuleValues[GlobalTyID]->getOperand(Num);
Chris Lattner52f86d62004-01-20 00:54:06 +0000356 Num -= ModuleValues[GlobalTyID]->size();
Chris Lattner89e02532004-01-18 21:08:15 +0000357 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000358 }
359
Reid Spencer060d25d2004-06-29 23:29:38 +0000360 if (FunctionValues.size() > type &&
361 FunctionValues[type] &&
362 Num < FunctionValues[type]->size())
363 return FunctionValues[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000364
Chris Lattner74734132002-08-17 22:01:27 +0000365 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000366
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000367 std::pair<unsigned,unsigned> KeyValue(type, oNum);
Reid Spencer060d25d2004-06-29 23:29:38 +0000368 ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue);
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000369 if (I != ForwardReferences.end() && I->first == KeyValue)
370 return I->second; // We have already created this placeholder
371
Chris Lattnerbf43ac62003-10-09 06:14:26 +0000372 Value *Val = new Argument(getType(type));
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000373 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
Chris Lattner36392bc2003-10-08 21:18:57 +0000374 return Val;
Chris Lattner00950542001-06-06 20:29:01 +0000375}
376
Reid Spencer04cde2c2004-07-04 11:33:49 +0000377/// This is just like getValue, but when a compaction table is in use, it
378/// is ignored. Also, no forward references or other fancy features are
379/// supported.
Reid Spencer060d25d2004-06-29 23:29:38 +0000380Value* BytecodeReader::getGlobalTableValue(const Type *Ty, unsigned SlotNo) {
381 // FIXME: getTypeSlot is inefficient!
382 unsigned TyID = getGlobalTableTypeSlot(Ty);
383
384 if (TyID != Type::LabelTyID) {
385 if (SlotNo == 0)
386 return Constant::getNullValue(Ty);
387 --SlotNo;
388 }
389
390 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
391 SlotNo >= ModuleValues[TyID]->size()) {
392 PARSE_ERROR("Corrupt compaction table entry!"
Reid Spencer04cde2c2004-07-04 11:33:49 +0000393 << TyID << ", " << SlotNo << ": " << ModuleValues.size() << ", "
394 << (void*)ModuleValues[TyID] << ", "
395 << ModuleValues[TyID]->size() << "\n");
Reid Spencer060d25d2004-06-29 23:29:38 +0000396 }
397 return ModuleValues[TyID]->getOperand(SlotNo);
398}
399
Reid Spencer04cde2c2004-07-04 11:33:49 +0000400/// Just like getValue, except that it returns a null pointer
401/// only on error. It always returns a constant (meaning that if the value is
402/// defined, but is not a constant, that is an error). If the specified
403/// constant hasn't been parsed yet, a placeholder is defined and used.
404/// Later, after the real value is parsed, the placeholder is eliminated.
Reid Spencer060d25d2004-06-29 23:29:38 +0000405Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) {
406 if (Value *V = getValue(TypeSlot, Slot, false))
407 if (Constant *C = dyn_cast<Constant>(V))
408 return C; // If we already have the value parsed, just return it
409 else if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
410 // ConstantPointerRef's are an abomination, but at least they don't have
411 // to infest bytecode files.
412 return ConstantPointerRef::get(GV);
413 else
Reid Spencer04cde2c2004-07-04 11:33:49 +0000414 throw std::string("Reference of a value is expected to be a constant!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000415
416 const Type *Ty = getType(TypeSlot);
417 std::pair<const Type*, unsigned> Key(Ty, Slot);
418 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
419
420 if (I != ConstantFwdRefs.end() && I->first == Key) {
421 return I->second;
422 } else {
423 // Create a placeholder for the constant reference and
424 // keep track of the fact that we have a forward ref to recycle it
425 Constant *C = new ConstPHolder(Ty, Slot);
426
427 // Keep track of the fact that we have a forward ref to recycle it
428 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
429 return C;
430 }
431}
432
433//===----------------------------------------------------------------------===//
434// IR Construction Methods
435//===----------------------------------------------------------------------===//
436
Reid Spencer04cde2c2004-07-04 11:33:49 +0000437/// As values are created, they are inserted into the appropriate place
438/// with this method. The ValueTable argument must be one of ModuleValues
439/// or FunctionValues data members of this class.
Reid Spencer060d25d2004-06-29 23:29:38 +0000440unsigned BytecodeReader::insertValue(
441 Value *Val, unsigned type, ValueTable &ValueTab) {
442 assert((!isa<Constant>(Val) || !cast<Constant>(Val)->isNullValue()) ||
Reid Spencer04cde2c2004-07-04 11:33:49 +0000443 !hasImplicitNull(type) &&
444 "Cannot read null values from bytecode!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000445
446 if (ValueTab.size() <= type)
447 ValueTab.resize(type+1);
448
449 if (!ValueTab[type]) ValueTab[type] = new ValueList();
450
451 ValueTab[type]->push_back(Val);
452
453 bool HasOffset = hasImplicitNull(type);
454 return ValueTab[type]->size()-1 + HasOffset;
455}
456
Reid Spencer04cde2c2004-07-04 11:33:49 +0000457/// Insert the arguments of a function as new values in the reader.
Reid Spencer060d25d2004-06-29 23:29:38 +0000458void BytecodeReader::insertArguments(Function* F ) {
459 const FunctionType *FT = F->getFunctionType();
460 Function::aiterator AI = F->abegin();
461 for (FunctionType::param_iterator It = FT->param_begin();
462 It != FT->param_end(); ++It, ++AI)
463 insertValue(AI, getTypeSlot(AI->getType()), FunctionValues);
464}
465
466//===----------------------------------------------------------------------===//
467// Bytecode Parsing Methods
468//===----------------------------------------------------------------------===//
469
Reid Spencer04cde2c2004-07-04 11:33:49 +0000470/// This method parses a single instruction. The instruction is
471/// inserted at the end of the \p BB provided. The arguments of
472/// the instruction are provided in the \p Args vector.
Reid Spencer060d25d2004-06-29 23:29:38 +0000473void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
474 BasicBlock* BB) {
475 BufPtr SaveAt = At;
476
477 // Clear instruction data
478 Oprnds.clear();
479 unsigned iType = 0;
480 unsigned Opcode = 0;
481 unsigned Op = read_uint();
482
483 // bits Instruction format: Common to all formats
484 // --------------------------
485 // 01-00: Opcode type, fixed to 1.
486 // 07-02: Opcode
487 Opcode = (Op >> 2) & 63;
488 Oprnds.resize((Op >> 0) & 03);
489
490 // Extract the operands
491 switch (Oprnds.size()) {
492 case 1:
493 // bits Instruction format:
494 // --------------------------
495 // 19-08: Resulting type plane
496 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
497 //
498 iType = (Op >> 8) & 4095;
499 Oprnds[0] = (Op >> 20) & 4095;
500 if (Oprnds[0] == 4095) // Handle special encoding for 0 operands...
501 Oprnds.resize(0);
502 break;
503 case 2:
504 // bits Instruction format:
505 // --------------------------
506 // 15-08: Resulting type plane
507 // 23-16: Operand #1
508 // 31-24: Operand #2
509 //
510 iType = (Op >> 8) & 255;
511 Oprnds[0] = (Op >> 16) & 255;
512 Oprnds[1] = (Op >> 24) & 255;
513 break;
514 case 3:
515 // bits Instruction format:
516 // --------------------------
517 // 13-08: Resulting type plane
518 // 19-14: Operand #1
519 // 25-20: Operand #2
520 // 31-26: Operand #3
521 //
522 iType = (Op >> 8) & 63;
523 Oprnds[0] = (Op >> 14) & 63;
524 Oprnds[1] = (Op >> 20) & 63;
525 Oprnds[2] = (Op >> 26) & 63;
526 break;
527 case 0:
528 At -= 4; // Hrm, try this again...
529 Opcode = read_vbr_uint();
530 Opcode >>= 2;
531 iType = read_vbr_uint();
532
533 unsigned NumOprnds = read_vbr_uint();
534 Oprnds.resize(NumOprnds);
535
536 if (NumOprnds == 0)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000537 throw std::string("Zero-argument instruction found; this is invalid.");
Reid Spencer060d25d2004-06-29 23:29:38 +0000538
539 for (unsigned i = 0; i != NumOprnds; ++i)
540 Oprnds[i] = read_vbr_uint();
541 align32();
542 break;
543 }
544
Reid Spencer04cde2c2004-07-04 11:33:49 +0000545 const Type *InstTy = getSanitizedType(iType);
Reid Spencer060d25d2004-06-29 23:29:38 +0000546
547 // Hae enough to inform the handler now
Reid Spencer04cde2c2004-07-04 11:33:49 +0000548 if (Handler) Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
Reid Spencer060d25d2004-06-29 23:29:38 +0000549
550 // Declare the resulting instruction we'll build.
551 Instruction *Result = 0;
552
553 // Handle binary operators
554 if (Opcode >= Instruction::BinaryOpsBegin &&
555 Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2)
556 Result = BinaryOperator::create((Instruction::BinaryOps)Opcode,
557 getValue(iType, Oprnds[0]),
558 getValue(iType, Oprnds[1]));
559
560 switch (Opcode) {
561 default:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000562 if (Result == 0)
563 throw std::string("Illegal instruction read!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000564 break;
565 case Instruction::VAArg:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000566 Result = new VAArgInst(getValue(iType, Oprnds[0]),
567 getSanitizedType(Oprnds[1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000568 break;
569 case Instruction::VANext:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000570 Result = new VANextInst(getValue(iType, Oprnds[0]),
571 getSanitizedType(Oprnds[1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000572 break;
573 case Instruction::Cast:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000574 Result = new CastInst(getValue(iType, Oprnds[0]),
575 getSanitizedType(Oprnds[1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000576 break;
577 case Instruction::Select:
578 Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]),
579 getValue(iType, Oprnds[1]),
580 getValue(iType, Oprnds[2]));
581 break;
582 case Instruction::PHI: {
583 if (Oprnds.size() == 0 || (Oprnds.size() & 1))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000584 throw std::string("Invalid phi node encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000585
586 PHINode *PN = new PHINode(InstTy);
587 PN->op_reserve(Oprnds.size());
588 for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2)
589 PN->addIncoming(getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1]));
590 Result = PN;
591 break;
592 }
593
594 case Instruction::Shl:
595 case Instruction::Shr:
596 Result = new ShiftInst((Instruction::OtherOps)Opcode,
597 getValue(iType, Oprnds[0]),
598 getValue(Type::UByteTyID, Oprnds[1]));
599 break;
600 case Instruction::Ret:
601 if (Oprnds.size() == 0)
602 Result = new ReturnInst();
603 else if (Oprnds.size() == 1)
604 Result = new ReturnInst(getValue(iType, Oprnds[0]));
605 else
Reid Spencer04cde2c2004-07-04 11:33:49 +0000606 throw std::string("Unrecognized instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000607 break;
608
609 case Instruction::Br:
610 if (Oprnds.size() == 1)
611 Result = new BranchInst(getBasicBlock(Oprnds[0]));
612 else if (Oprnds.size() == 3)
613 Result = new BranchInst(getBasicBlock(Oprnds[0]),
Reid Spencer04cde2c2004-07-04 11:33:49 +0000614 getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000615 else
Reid Spencer04cde2c2004-07-04 11:33:49 +0000616 throw std::string("Invalid number of operands for a 'br' instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000617 break;
618 case Instruction::Switch: {
619 if (Oprnds.size() & 1)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000620 throw std::string("Switch statement with odd number of arguments!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000621
622 SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]),
623 getBasicBlock(Oprnds[1]));
624 for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2)
625 I->addCase(cast<Constant>(getValue(iType, Oprnds[i])),
626 getBasicBlock(Oprnds[i+1]));
627 Result = I;
628 break;
629 }
630
631 case Instruction::Call: {
632 if (Oprnds.size() == 0)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000633 throw std::string("Invalid call instruction encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000634
635 Value *F = getValue(iType, Oprnds[0]);
636
637 // Check to make sure we have a pointer to function type
638 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000639 if (PTy == 0) throw std::string("Call to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000640 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000641 if (FTy == 0) throw std::string("Call to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000642
643 std::vector<Value *> Params;
644 if (!FTy->isVarArg()) {
645 FunctionType::param_iterator It = FTy->param_begin();
646
647 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
648 if (It == FTy->param_end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000649 throw std::string("Invalid call instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000650 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
651 }
652 if (It != FTy->param_end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000653 throw std::string("Invalid call instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000654 } else {
655 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
656
657 unsigned FirstVariableOperand;
658 if (Oprnds.size() < FTy->getNumParams())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000659 throw std::string("Call instruction missing operands!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000660
661 // Read all of the fixed arguments
662 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
663 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i]));
664
665 FirstVariableOperand = FTy->getNumParams();
666
667 if ((Oprnds.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
Reid Spencer04cde2c2004-07-04 11:33:49 +0000668 throw std::string("Invalid call instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000669
670 for (unsigned i = FirstVariableOperand, e = Oprnds.size();
Reid Spencer04cde2c2004-07-04 11:33:49 +0000671 i != e; i += 2)
Reid Spencer060d25d2004-06-29 23:29:38 +0000672 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
673 }
674
675 Result = new CallInst(F, Params);
676 break;
677 }
678 case Instruction::Invoke: {
Reid Spencer04cde2c2004-07-04 11:33:49 +0000679 if (Oprnds.size() < 3)
680 throw std::string("Invalid invoke instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000681 Value *F = getValue(iType, Oprnds[0]);
682
683 // Check to make sure we have a pointer to function type
684 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000685 if (PTy == 0)
686 throw std::string("Invoke to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000687 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000688 if (FTy == 0)
689 throw std::string("Invoke to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000690
691 std::vector<Value *> Params;
692 BasicBlock *Normal, *Except;
693
694 if (!FTy->isVarArg()) {
695 Normal = getBasicBlock(Oprnds[1]);
696 Except = getBasicBlock(Oprnds[2]);
697
698 FunctionType::param_iterator It = FTy->param_begin();
699 for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
700 if (It == FTy->param_end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000701 throw std::string("Invalid invoke instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000702 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
703 }
704 if (It != FTy->param_end())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000705 throw std::string("Invalid invoke instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000706 } else {
707 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
708
709 Normal = getBasicBlock(Oprnds[0]);
710 Except = getBasicBlock(Oprnds[1]);
711
712 unsigned FirstVariableArgument = FTy->getNumParams()+2;
713 for (unsigned i = 2; i != FirstVariableArgument; ++i)
714 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
715 Oprnds[i]));
716
717 if (Oprnds.size()-FirstVariableArgument & 1) // Must be type/value pairs
Reid Spencer04cde2c2004-07-04 11:33:49 +0000718 throw std::string("Invalid invoke instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000719
720 for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
721 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
722 }
723
724 Result = new InvokeInst(F, Normal, Except, Params);
725 break;
726 }
727 case Instruction::Malloc:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000728 if (Oprnds.size() > 2)
729 throw std::string("Invalid malloc instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000730 if (!isa<PointerType>(InstTy))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000731 throw std::string("Invalid malloc instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000732
733 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
734 Oprnds.size() ? getValue(Type::UIntTyID,
735 Oprnds[0]) : 0);
736 break;
737
738 case Instruction::Alloca:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000739 if (Oprnds.size() > 2)
740 throw std::string("Invalid alloca instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000741 if (!isa<PointerType>(InstTy))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000742 throw std::string("Invalid alloca instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000743
744 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
745 Oprnds.size() ? getValue(Type::UIntTyID,
Reid Spencer04cde2c2004-07-04 11:33:49 +0000746 Oprnds[0]) :0);
Reid Spencer060d25d2004-06-29 23:29:38 +0000747 break;
748 case Instruction::Free:
749 if (!isa<PointerType>(InstTy))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000750 throw std::string("Invalid free instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000751 Result = new FreeInst(getValue(iType, Oprnds[0]));
752 break;
753 case Instruction::GetElementPtr: {
754 if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000755 throw std::string("Invalid getelementptr instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000756
757 std::vector<Value*> Idx;
758
759 const Type *NextTy = InstTy;
760 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
761 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000762 if (!TopTy)
763 throw std::string("Invalid getelementptr instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000764
765 unsigned ValIdx = Oprnds[i];
766 unsigned IdxTy = 0;
767 if (!hasRestrictedGEPTypes) {
768 // Struct indices are always uints, sequential type indices can be any
769 // of the 32 or 64-bit integer types. The actual choice of type is
770 // encoded in the low two bits of the slot number.
771 if (isa<StructType>(TopTy))
772 IdxTy = Type::UIntTyID;
773 else {
774 switch (ValIdx & 3) {
775 default:
776 case 0: IdxTy = Type::UIntTyID; break;
777 case 1: IdxTy = Type::IntTyID; break;
778 case 2: IdxTy = Type::ULongTyID; break;
779 case 3: IdxTy = Type::LongTyID; break;
780 }
781 ValIdx >>= 2;
782 }
783 } else {
784 IdxTy = isa<StructType>(TopTy) ? Type::UByteTyID : Type::LongTyID;
785 }
786
787 Idx.push_back(getValue(IdxTy, ValIdx));
788
789 // Convert ubyte struct indices into uint struct indices.
790 if (isa<StructType>(TopTy) && hasRestrictedGEPTypes)
791 if (ConstantUInt *C = dyn_cast<ConstantUInt>(Idx.back()))
792 Idx[Idx.size()-1] = ConstantExpr::getCast(C, Type::UIntTy);
793
794 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
795 }
796
797 Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
798 break;
799 }
800
801 case 62: // volatile load
802 case Instruction::Load:
803 if (Oprnds.size() != 1 || !isa<PointerType>(InstTy))
Reid Spencer04cde2c2004-07-04 11:33:49 +0000804 throw std::string("Invalid load instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000805 Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62);
806 break;
807
808 case 63: // volatile store
809 case Instruction::Store: {
810 if (!isa<PointerType>(InstTy) || Oprnds.size() != 2)
Reid Spencer04cde2c2004-07-04 11:33:49 +0000811 throw std::string("Invalid store instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000812
813 Value *Ptr = getValue(iType, Oprnds[1]);
814 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
815 Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr,
816 Opcode == 63);
817 break;
818 }
819 case Instruction::Unwind:
Reid Spencer04cde2c2004-07-04 11:33:49 +0000820 if (Oprnds.size() != 0)
821 throw std::string("Invalid unwind instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000822 Result = new UnwindInst();
823 break;
824 } // end switch(Opcode)
825
826 unsigned TypeSlot;
827 if (Result->getType() == InstTy)
828 TypeSlot = iType;
829 else
830 TypeSlot = getTypeSlot(Result->getType());
831
832 insertValue(Result, TypeSlot, FunctionValues);
833 BB->getInstList().push_back(Result);
834}
835
Reid Spencer04cde2c2004-07-04 11:33:49 +0000836/// Get a particular numbered basic block, which might be a forward reference.
837/// This works together with ParseBasicBlock to handle these forward references
838/// in a clean manner. This function is used when constructing phi, br, switch,
839/// and other instructions that reference basic blocks. Blocks are numbered
840/// sequentially as they appear in the function.
Reid Spencer060d25d2004-06-29 23:29:38 +0000841BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000842 // Make sure there is room in the table...
843 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
844
845 // First check to see if this is a backwards reference, i.e., ParseBasicBlock
846 // has already created this block, or if the forward reference has already
847 // been created.
848 if (ParsedBasicBlocks[ID])
849 return ParsedBasicBlocks[ID];
850
851 // Otherwise, the basic block has not yet been created. Do so and add it to
852 // the ParsedBasicBlocks list.
853 return ParsedBasicBlocks[ID] = new BasicBlock();
854}
855
Reid Spencer04cde2c2004-07-04 11:33:49 +0000856/// In LLVM 1.0 bytecode files, we used to output one basicblock at a time.
857/// This method reads in one of the basicblock packets. This method is not used
858/// for bytecode files after LLVM 1.0
859/// @returns The basic block constructed.
Reid Spencer060d25d2004-06-29 23:29:38 +0000860BasicBlock *BytecodeReader::ParseBasicBlock( unsigned BlockNo) {
Reid Spencer04cde2c2004-07-04 11:33:49 +0000861 if (Handler) Handler->handleBasicBlockBegin( BlockNo );
Reid Spencer060d25d2004-06-29 23:29:38 +0000862
863 BasicBlock *BB = 0;
864
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000865 if (ParsedBasicBlocks.size() == BlockNo)
866 ParsedBasicBlocks.push_back(BB = new BasicBlock());
867 else if (ParsedBasicBlocks[BlockNo] == 0)
868 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
869 else
870 BB = ParsedBasicBlocks[BlockNo];
Chris Lattner00950542001-06-06 20:29:01 +0000871
Reid Spencer060d25d2004-06-29 23:29:38 +0000872 std::vector<unsigned> Operands;
873 while ( moreInBlock() )
874 ParseInstruction(Operands, BB);
Chris Lattner00950542001-06-06 20:29:01 +0000875
Reid Spencer04cde2c2004-07-04 11:33:49 +0000876 if (Handler) Handler->handleBasicBlockEnd( BlockNo );
Misha Brukman12c29d12003-09-22 23:38:23 +0000877 return BB;
Chris Lattner00950542001-06-06 20:29:01 +0000878}
879
Reid Spencer04cde2c2004-07-04 11:33:49 +0000880/// Parse all of the BasicBlock's & Instruction's in the body of a function.
881/// In post 1.0 bytecode files, we no longer emit basic block individually,
882/// in order to avoid per-basic-block overhead.
883/// @returns Rhe number of basic blocks encountered.
Reid Spencer060d25d2004-06-29 23:29:38 +0000884unsigned BytecodeReader::ParseInstructionList(Function* F) {
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000885 unsigned BlockNo = 0;
886 std::vector<unsigned> Args;
887
Reid Spencer060d25d2004-06-29 23:29:38 +0000888 while ( moreInBlock() ) {
Reid Spencer04cde2c2004-07-04 11:33:49 +0000889 if (Handler) Handler->handleBasicBlockBegin( BlockNo );
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000890 BasicBlock *BB;
891 if (ParsedBasicBlocks.size() == BlockNo)
892 ParsedBasicBlocks.push_back(BB = new BasicBlock());
893 else if (ParsedBasicBlocks[BlockNo] == 0)
894 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
895 else
896 BB = ParsedBasicBlocks[BlockNo];
897 ++BlockNo;
898 F->getBasicBlockList().push_back(BB);
899
900 // Read instructions into this basic block until we get to a terminator
Reid Spencer060d25d2004-06-29 23:29:38 +0000901 while ( moreInBlock() && !BB->getTerminator())
902 ParseInstruction(Args, BB);
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000903
904 if (!BB->getTerminator())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000905 throw std::string("Non-terminated basic block found!");
Reid Spencer5c15fe52004-07-05 00:57:50 +0000906
907 if (Handler) Handler->handleBasicBlockEnd( BlockNo-1 );
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000908 }
909
910 return BlockNo;
911}
912
Reid Spencer04cde2c2004-07-04 11:33:49 +0000913/// Parse a symbol table. This works for both module level and function
914/// level symbol tables. For function level symbol tables, the CurrentFunction
915/// parameter must be non-zero and the ST parameter must correspond to
916/// CurrentFunction's symbol table. For Module level symbol tables, the
917/// CurrentFunction argument must be zero.
Reid Spencer060d25d2004-06-29 23:29:38 +0000918void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
Reid Spencer04cde2c2004-07-04 11:33:49 +0000919 SymbolTable *ST) {
920 if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
Reid Spencer060d25d2004-06-29 23:29:38 +0000921
Chris Lattner39cacce2003-10-10 05:43:47 +0000922 // Allow efficient basic block lookup by number.
923 std::vector<BasicBlock*> BBMap;
924 if (CurrentFunction)
925 for (Function::iterator I = CurrentFunction->begin(),
926 E = CurrentFunction->end(); I != E; ++I)
927 BBMap.push_back(I);
928
Reid Spencer04cde2c2004-07-04 11:33:49 +0000929 /// In LLVM 1.3 we write types separately from values so
930 /// The types are always first in the symbol table. This is
931 /// because Type no longer derives from Value.
932 if ( ! hasTypeDerivedFromValue ) {
933 // Symtab block header: [num entries]
934 unsigned NumEntries = read_vbr_uint();
935 for ( unsigned i = 0; i < NumEntries; ++i ) {
936 // Symtab entry: [def slot #][name]
937 unsigned slot = read_vbr_uint();
938 std::string Name = read_str();
939 const Type* T = getType(slot);
940 ST->insert(Name, T);
941 }
942 }
943
Reid Spencer060d25d2004-06-29 23:29:38 +0000944 while ( moreInBlock() ) {
Chris Lattner00950542001-06-06 20:29:01 +0000945 // Symtab block header: [num entries][type id number]
Reid Spencer060d25d2004-06-29 23:29:38 +0000946 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +0000947 unsigned Typ = 0;
948 bool isTypeType = read_typeid(Typ);
Chris Lattner00950542001-06-06 20:29:01 +0000949 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000950
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000951 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000952 // Symtab entry: [def slot #][name]
Reid Spencer060d25d2004-06-29 23:29:38 +0000953 unsigned slot = read_vbr_uint();
954 std::string Name = read_str();
Chris Lattner00950542001-06-06 20:29:01 +0000955
Reid Spencer04cde2c2004-07-04 11:33:49 +0000956 // if we're reading a pre 1.3 bytecode file and the type plane
957 // is the "type type", handle it here
958 if ( isTypeType ) {
959 const Type* T = getType(slot);
960 if ( T == 0 )
961 PARSE_ERROR("Failed type look-up for name '" << Name << "'");
962 ST->insert(Name, T);
963 continue; // code below must be short circuited
Chris Lattner39cacce2003-10-10 05:43:47 +0000964 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +0000965 Value *V = 0;
966 if (Typ == Type::LabelTyID) {
967 if (slot < BBMap.size())
968 V = BBMap[slot];
969 } else {
970 V = getValue(Typ, slot, false); // Find mapping...
971 }
972 if (V == 0)
973 PARSE_ERROR("Failed value look-up for name '" << Name << "'");
974 V->setName(Name, ST);
Chris Lattner39cacce2003-10-10 05:43:47 +0000975 }
Chris Lattner00950542001-06-06 20:29:01 +0000976 }
977 }
Reid Spencer060d25d2004-06-29 23:29:38 +0000978 checkPastBlockEnd("Symbol Table");
Reid Spencer04cde2c2004-07-04 11:33:49 +0000979 if (Handler) Handler->handleSymbolTableEnd();
Chris Lattner00950542001-06-06 20:29:01 +0000980}
981
Reid Spencer04cde2c2004-07-04 11:33:49 +0000982/// Read in the types portion of a compaction table.
983void BytecodeReader::ParseCompactionTypes( unsigned NumEntries ) {
984 for (unsigned i = 0; i != NumEntries; ++i) {
985 unsigned TypeSlot = 0;
986 bool isTypeType = read_typeid(TypeSlot);
987 assert(!isTypeType && "Invalid type in compaction table: type type");
988 const Type *Typ = getGlobalTableType(TypeSlot);
989 CompactionTypes.push_back(Typ);
990 if (Handler) Handler->handleCompactionTableType( i, TypeSlot, Typ );
991 }
992}
993
994/// Parse a compaction table.
Reid Spencer060d25d2004-06-29 23:29:38 +0000995void BytecodeReader::ParseCompactionTable() {
996
Reid Spencer04cde2c2004-07-04 11:33:49 +0000997 if (Handler) Handler->handleCompactionTableBegin();
998
999 /// In LLVM 1.3 Type no longer derives from Value. So,
1000 /// we always write them first in the compaction table
1001 /// because they can't occupy a "type plane" where the
1002 /// Values reside.
1003 if ( ! hasTypeDerivedFromValue ) {
1004 unsigned NumEntries = read_vbr_uint();
1005 ParseCompactionTypes( NumEntries );
1006 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001007
1008 while ( moreInBlock() ) {
1009 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001010 unsigned Ty = 0;
1011 unsigned isTypeType = false;
Reid Spencer060d25d2004-06-29 23:29:38 +00001012
1013 if ((NumEntries & 3) == 3) {
1014 NumEntries >>= 2;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001015 isTypeType = read_typeid(Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +00001016 } else {
1017 Ty = NumEntries >> 2;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001018 isTypeType = sanitizeTypeId(Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +00001019 NumEntries &= 3;
1020 }
1021
Reid Spencer04cde2c2004-07-04 11:33:49 +00001022 // if we're reading a pre 1.3 bytecode file and the type plane
1023 // is the "type type", handle it here
1024 if ( isTypeType ) {
1025 ParseCompactionTypes(NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001026 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001027 if (Ty >= CompactionValues.size())
1028 CompactionValues.resize(Ty+1);
1029
1030 if (!CompactionValues[Ty].empty())
1031 throw std::string("Compaction table plane contains multiple entries!");
1032
1033 if (Handler) Handler->handleCompactionTablePlane( Ty, NumEntries );
1034
Reid Spencer060d25d2004-06-29 23:29:38 +00001035 const Type *Typ = getType(Ty);
1036 // Push the implicit zero
1037 CompactionValues[Ty].push_back(Constant::getNullValue(Typ));
1038 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001039 unsigned ValSlot = read_vbr_uint();
1040 Value *V = getGlobalTableValue(Typ, ValSlot);
1041 CompactionValues[Ty].push_back(V);
1042 if (Handler) Handler->handleCompactionTableValue( i, Ty, ValSlot, Typ );
Reid Spencer060d25d2004-06-29 23:29:38 +00001043 }
1044 }
1045 }
Reid Spencer04cde2c2004-07-04 11:33:49 +00001046 if (Handler) Handler->handleCompactionTableEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001047}
1048
1049// Parse a single type constant.
1050const Type *BytecodeReader::ParseTypeConstant() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001051 unsigned PrimType = 0;
1052 bool isTypeType = read_typeid(PrimType);
1053 assert(!isTypeType && "Invalid type (type type) in type constants!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001054
1055 const Type *Result = 0;
1056 if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType)))
1057 return Result;
1058
1059 switch (PrimType) {
1060 case Type::FunctionTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001061 const Type *RetType = readSanitizedType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001062
1063 unsigned NumParams = read_vbr_uint();
1064
1065 std::vector<const Type*> Params;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001066 while (NumParams--)
1067 Params.push_back(readSanitizedType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001068
1069 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1070 if (isVarArg) Params.pop_back();
1071
1072 Result = FunctionType::get(RetType, Params, isVarArg);
1073 break;
1074 }
1075 case Type::ArrayTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001076 const Type *ElementType = readSanitizedType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001077 unsigned NumElements = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001078 Result = ArrayType::get(ElementType, NumElements);
1079 break;
1080 }
1081 case Type::StructTyID: {
1082 std::vector<const Type*> Elements;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001083 unsigned Typ = 0;
1084 bool isTypeType = read_typeid(Typ);
1085 assert(!isTypeType && "Invalid element type (type type) for structure!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001086 while (Typ) { // List is terminated by void/0 typeid
1087 Elements.push_back(getType(Typ));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001088 bool isTypeType = read_typeid(Typ);
1089 assert(!isTypeType && "Invalid element type (type type) for structure!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001090 }
1091
1092 Result = StructType::get(Elements);
1093 break;
1094 }
1095 case Type::PointerTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001096 Result = PointerType::get(readSanitizedType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001097 break;
1098 }
1099
1100 case Type::OpaqueTyID: {
1101 Result = OpaqueType::get();
1102 break;
1103 }
1104
1105 default:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001106 PARSE_ERROR("Don't know how to deserialize primitive type"
1107 << PrimType << "\n");
Reid Spencer060d25d2004-06-29 23:29:38 +00001108 break;
1109 }
Reid Spencer04cde2c2004-07-04 11:33:49 +00001110 if (Handler) Handler->handleType( Result );
Reid Spencer060d25d2004-06-29 23:29:38 +00001111 return Result;
1112}
1113
1114// ParseTypeConstants - We have to use this weird code to handle recursive
1115// types. We know that recursive types will only reference the current slab of
1116// values in the type plane, but they can forward reference types before they
1117// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
1118// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
1119// this ugly problem, we pessimistically insert an opaque type for each type we
1120// are about to read. This means that forward references will resolve to
1121// something and when we reread the type later, we can replace the opaque type
1122// with a new resolved concrete type.
1123//
1124void BytecodeReader::ParseTypeConstants(TypeListTy &Tab, unsigned NumEntries){
1125 assert(Tab.size() == 0 && "should not have read type constants in before!");
1126
1127 // Insert a bunch of opaque types to be resolved later...
1128 Tab.reserve(NumEntries);
1129 for (unsigned i = 0; i != NumEntries; ++i)
1130 Tab.push_back(OpaqueType::get());
1131
1132 // Loop through reading all of the types. Forward types will make use of the
1133 // opaque types just inserted.
1134 //
1135 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001136 const Type* NewTy = ParseTypeConstant();
1137 const Type* OldTy = Tab[i].get();
1138 if (NewTy == 0)
1139 throw std::string("Couldn't parse type!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001140
1141 // Don't directly push the new type on the Tab. Instead we want to replace
1142 // the opaque type we previously inserted with the new concrete value. This
1143 // approach helps with forward references to types. The refinement from the
1144 // abstract (opaque) type to the new type causes all uses of the abstract
1145 // type to use the concrete type (NewTy). This will also cause the opaque
1146 // type to be deleted.
1147 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
1148
1149 // This should have replaced the old opaque type with the new type in the
1150 // value table... or with a preexisting type that was already in the system.
1151 // Let's just make sure it did.
1152 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
1153 }
1154}
1155
Reid Spencer04cde2c2004-07-04 11:33:49 +00001156/// Parse a single constant value
Reid Spencer060d25d2004-06-29 23:29:38 +00001157Constant *BytecodeReader::ParseConstantValue( unsigned TypeID) {
1158 // We must check for a ConstantExpr before switching by type because
1159 // a ConstantExpr can be of any type, and has no explicit value.
1160 //
1161 // 0 if not expr; numArgs if is expr
1162 unsigned isExprNumArgs = read_vbr_uint();
1163
1164 if (isExprNumArgs) {
1165 // FIXME: Encoding of constant exprs could be much more compact!
1166 std::vector<Constant*> ArgVec;
1167 ArgVec.reserve(isExprNumArgs);
1168 unsigned Opcode = read_vbr_uint();
1169
1170 // Read the slot number and types of each of the arguments
1171 for (unsigned i = 0; i != isExprNumArgs; ++i) {
1172 unsigned ArgValSlot = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001173 unsigned ArgTypeSlot = 0;
1174 bool isTypeType = read_typeid(ArgTypeSlot);
1175 assert(!isTypeType && "Invalid argument type (type type) for constant value");
Reid Spencer060d25d2004-06-29 23:29:38 +00001176
1177 // Get the arg value from its slot if it exists, otherwise a placeholder
1178 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
1179 }
1180
1181 // Construct a ConstantExpr of the appropriate kind
1182 if (isExprNumArgs == 1) { // All one-operand expressions
1183 assert(Opcode == Instruction::Cast);
1184 Constant* Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001185 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001186 return Result;
1187 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
1188 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
1189
1190 if (hasRestrictedGEPTypes) {
1191 const Type *BaseTy = ArgVec[0]->getType();
1192 generic_gep_type_iterator<std::vector<Constant*>::iterator>
1193 GTI = gep_type_begin(BaseTy, IdxList.begin(), IdxList.end()),
1194 E = gep_type_end(BaseTy, IdxList.begin(), IdxList.end());
1195 for (unsigned i = 0; GTI != E; ++GTI, ++i)
1196 if (isa<StructType>(*GTI)) {
1197 if (IdxList[i]->getType() != Type::UByteTy)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001198 throw std::string("Invalid index for getelementptr!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001199 IdxList[i] = ConstantExpr::getCast(IdxList[i], Type::UIntTy);
1200 }
1201 }
1202
1203 Constant* Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001204 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001205 return Result;
1206 } else if (Opcode == Instruction::Select) {
1207 assert(ArgVec.size() == 3);
1208 Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
Reid Spencer04cde2c2004-07-04 11:33:49 +00001209 ArgVec[2]);
1210 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001211 return Result;
1212 } else { // All other 2-operand expressions
1213 Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001214 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001215 return Result;
1216 }
1217 }
1218
1219 // Ok, not an ConstantExpr. We now know how to read the given type...
1220 const Type *Ty = getType(TypeID);
1221 switch (Ty->getTypeID()) {
1222 case Type::BoolTyID: {
1223 unsigned Val = read_vbr_uint();
1224 if (Val != 0 && Val != 1)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001225 throw std::string("Invalid boolean value read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001226 Constant* Result = ConstantBool::get(Val == 1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001227 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001228 return Result;
1229 }
1230
1231 case Type::UByteTyID: // Unsigned integer types...
1232 case Type::UShortTyID:
1233 case Type::UIntTyID: {
1234 unsigned Val = read_vbr_uint();
1235 if (!ConstantUInt::isValueValidForType(Ty, Val))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001236 throw std::string("Invalid unsigned byte/short/int read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001237 Constant* Result = ConstantUInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001238 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001239 return Result;
1240 }
1241
1242 case Type::ULongTyID: {
1243 Constant* Result = ConstantUInt::get(Ty, read_vbr_uint64());
Reid Spencer04cde2c2004-07-04 11:33:49 +00001244 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001245 return Result;
1246 }
1247
1248 case Type::SByteTyID: // Signed integer types...
1249 case Type::ShortTyID:
1250 case Type::IntTyID: {
1251 case Type::LongTyID:
1252 int64_t Val = read_vbr_int64();
1253 if (!ConstantSInt::isValueValidForType(Ty, Val))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001254 throw std::string("Invalid signed byte/short/int/long read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001255 Constant* Result = ConstantSInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001256 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001257 return Result;
1258 }
1259
1260 case Type::FloatTyID: {
1261 float F;
1262 read_data(&F, &F+1);
1263 Constant* Result = ConstantFP::get(Ty, F);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001264 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001265 return Result;
1266 }
1267
1268 case Type::DoubleTyID: {
1269 double Val;
1270 read_data(&Val, &Val+1);
1271 Constant* Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001272 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001273 return Result;
1274 }
1275
Reid Spencer060d25d2004-06-29 23:29:38 +00001276 case Type::ArrayTyID: {
1277 const ArrayType *AT = cast<ArrayType>(Ty);
1278 unsigned NumElements = AT->getNumElements();
1279 unsigned TypeSlot = getTypeSlot(AT->getElementType());
1280 std::vector<Constant*> Elements;
1281 Elements.reserve(NumElements);
1282 while (NumElements--) // Read all of the elements of the constant.
1283 Elements.push_back(getConstantValue(TypeSlot,
1284 read_vbr_uint()));
1285 Constant* Result = ConstantArray::get(AT, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001286 if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001287 return Result;
1288 }
1289
1290 case Type::StructTyID: {
1291 const StructType *ST = cast<StructType>(Ty);
1292
1293 std::vector<Constant *> Elements;
1294 Elements.reserve(ST->getNumElements());
1295 for (unsigned i = 0; i != ST->getNumElements(); ++i)
1296 Elements.push_back(getConstantValue(ST->getElementType(i),
1297 read_vbr_uint()));
1298
1299 Constant* Result = ConstantStruct::get(ST, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001300 if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001301 return Result;
1302 }
1303
1304 case Type::PointerTyID: { // ConstantPointerRef value...
1305 const PointerType *PT = cast<PointerType>(Ty);
1306 unsigned Slot = read_vbr_uint();
1307
1308 // Check to see if we have already read this global variable...
1309 Value *Val = getValue(TypeID, Slot, false);
1310 GlobalValue *GV;
1311 if (Val) {
1312 if (!(GV = dyn_cast<GlobalValue>(Val)))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001313 throw std::string("Value of ConstantPointerRef not in ValueTable!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001314 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001315 throw std::string("Forward references are not allowed here.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001316 }
1317
1318 Constant* Result = ConstantPointerRef::get(GV);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001319 if (Handler) Handler->handleConstantPointer(PT, Slot, GV, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001320 return Result;
1321 }
1322
1323 default:
1324 PARSE_ERROR("Don't know how to deserialize constant value of type '"+
1325 Ty->getDescription());
1326 break;
1327 }
1328}
1329
Reid Spencer04cde2c2004-07-04 11:33:49 +00001330/// Resolve references for constants. This function resolves the forward
1331/// referenced constants in the ConstantFwdRefs map. It uses the
1332/// replaceAllUsesWith method of Value class to substitute the placeholder
1333/// instance with the actual instance.
Reid Spencer060d25d2004-06-29 23:29:38 +00001334void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
Chris Lattner29b789b2003-11-19 17:27:18 +00001335 ConstantRefsType::iterator I =
1336 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
1337 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +00001338
Chris Lattner29b789b2003-11-19 17:27:18 +00001339 Value *PH = I->second; // Get the placeholder...
1340 PH->replaceAllUsesWith(NewV);
1341 delete PH; // Delete the old placeholder
1342 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +00001343}
1344
Reid Spencer04cde2c2004-07-04 11:33:49 +00001345/// Parse the constant strings section.
Reid Spencer060d25d2004-06-29 23:29:38 +00001346void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){
1347 for (; NumEntries; --NumEntries) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001348 unsigned Typ = 0;
1349 bool isTypeType = read_typeid(Typ);
1350 assert(!isTypeType && "Invalid type (type type) for string constant");
Reid Spencer060d25d2004-06-29 23:29:38 +00001351 const Type *Ty = getType(Typ);
1352 if (!isa<ArrayType>(Ty))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001353 throw std::string("String constant data invalid!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001354
1355 const ArrayType *ATy = cast<ArrayType>(Ty);
1356 if (ATy->getElementType() != Type::SByteTy &&
1357 ATy->getElementType() != Type::UByteTy)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001358 throw std::string("String constant data invalid!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001359
1360 // Read character data. The type tells us how long the string is.
1361 char Data[ATy->getNumElements()];
1362 read_data(Data, Data+ATy->getNumElements());
Chris Lattner52e20b02003-03-19 20:54:26 +00001363
Reid Spencer060d25d2004-06-29 23:29:38 +00001364 std::vector<Constant*> Elements(ATy->getNumElements());
1365 if (ATy->getElementType() == Type::SByteTy)
1366 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1367 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
1368 else
1369 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1370 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
Misha Brukman12c29d12003-09-22 23:38:23 +00001371
Reid Spencer060d25d2004-06-29 23:29:38 +00001372 // Create the constant, inserting it as needed.
1373 Constant *C = ConstantArray::get(ATy, Elements);
1374 unsigned Slot = insertValue(C, Typ, Tab);
1375 ResolveReferencesToConstant(C, Slot);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001376 if (Handler) Handler->handleConstantString(cast<ConstantArray>(C));
Reid Spencer060d25d2004-06-29 23:29:38 +00001377 }
Misha Brukman12c29d12003-09-22 23:38:23 +00001378}
1379
Reid Spencer04cde2c2004-07-04 11:33:49 +00001380/// Parse the constant pool.
Reid Spencer060d25d2004-06-29 23:29:38 +00001381void BytecodeReader::ParseConstantPool(ValueTable &Tab,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001382 TypeListTy &TypeTab,
1383 bool isFunction) {
1384 if (Handler) Handler->handleGlobalConstantsBegin();
1385
1386 /// In LLVM 1.3 Type does not derive from Value so the types
1387 /// do not occupy a plane. Consequently, we read the types
1388 /// first in the constant pool.
1389 if ( isFunction && !hasTypeDerivedFromValue ) {
1390 unsigned NumEntries = read_vbr_uint();
1391 ParseTypeConstants(TypeTab, NumEntries);
1392 }
1393
Reid Spencer060d25d2004-06-29 23:29:38 +00001394 while ( moreInBlock() ) {
1395 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001396 unsigned Typ = 0;
1397 bool isTypeType = read_typeid(Typ);
1398
1399 /// In LLVM 1.2 and before, Types were written to the
1400 /// bytecode file in the "Type Type" plane (#12).
1401 /// In 1.3 plane 12 is now the label plane. Handle this here.
1402 if ( isTypeType ) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001403 ParseTypeConstants(TypeTab, NumEntries);
1404 } else if (Typ == Type::VoidTyID) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001405 /// Use of Type::VoidTyID is a misnomer. It actually means
1406 /// that the following plane is constant strings
Reid Spencer060d25d2004-06-29 23:29:38 +00001407 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
1408 ParseStringConstants(NumEntries, Tab);
1409 } else {
1410 for (unsigned i = 0; i < NumEntries; ++i) {
1411 Constant *C = ParseConstantValue(Typ);
1412 assert(C && "ParseConstantValue returned NULL!");
1413 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner29b789b2003-11-19 17:27:18 +00001414
Reid Spencer060d25d2004-06-29 23:29:38 +00001415 // If we are reading a function constant table, make sure that we adjust
1416 // the slot number to be the real global constant number.
1417 //
1418 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
1419 ModuleValues[Typ])
1420 Slot += ModuleValues[Typ]->size();
1421 ResolveReferencesToConstant(C, Slot);
1422 }
1423 }
1424 }
1425 checkPastBlockEnd("Constant Pool");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001426 if (Handler) Handler->handleGlobalConstantsEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001427}
Chris Lattner00950542001-06-06 20:29:01 +00001428
Reid Spencer04cde2c2004-07-04 11:33:49 +00001429/// Parse the contents of a function. Note that this function can be
1430/// called lazily by materializeFunction
1431/// @see materializeFunction
Reid Spencer060d25d2004-06-29 23:29:38 +00001432void BytecodeReader::ParseFunctionBody(Function* F ) {
1433
1434 unsigned FuncSize = BlockEnd - At;
Chris Lattnere3869c82003-04-16 21:16:05 +00001435 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
1436
Reid Spencer060d25d2004-06-29 23:29:38 +00001437 unsigned LinkageType = read_vbr_uint();
Chris Lattnerc08912f2004-01-14 16:44:44 +00001438 switch (LinkageType) {
1439 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1440 case 1: Linkage = GlobalValue::WeakLinkage; break;
1441 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1442 case 3: Linkage = GlobalValue::InternalLinkage; break;
1443 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001444 default:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001445 throw std::string("Invalid linkage type for Function.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001446 Linkage = GlobalValue::InternalLinkage;
1447 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001448 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +00001449
Reid Spencer060d25d2004-06-29 23:29:38 +00001450 F->setLinkage( Linkage );
Reid Spencer04cde2c2004-07-04 11:33:49 +00001451 if (Handler) Handler->handleFunctionBegin(F,FuncSize);
Chris Lattner00950542001-06-06 20:29:01 +00001452
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001453 // Keep track of how many basic blocks we have read in...
1454 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +00001455 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001456
Reid Spencer060d25d2004-06-29 23:29:38 +00001457 BufPtr MyEnd = BlockEnd;
1458 while ( At < MyEnd ) {
Chris Lattner00950542001-06-06 20:29:01 +00001459 unsigned Type, Size;
Reid Spencer060d25d2004-06-29 23:29:38 +00001460 BufPtr OldAt = At;
1461 read_block(Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +00001462
1463 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +00001464 case BytecodeFormat::ConstantPool:
Chris Lattner89e02532004-01-18 21:08:15 +00001465 if (!InsertedArguments) {
1466 // Insert arguments into the value table before we parse the first basic
1467 // block in the function, but after we potentially read in the
1468 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001469 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001470 InsertedArguments = true;
1471 }
1472
Reid Spencer04cde2c2004-07-04 11:33:49 +00001473 ParseConstantPool(FunctionValues, FunctionTypes, true);
Chris Lattner00950542001-06-06 20:29:01 +00001474 break;
1475
Chris Lattner89e02532004-01-18 21:08:15 +00001476 case BytecodeFormat::CompactionTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001477 ParseCompactionTable();
Chris Lattner89e02532004-01-18 21:08:15 +00001478 break;
1479
Chris Lattner00950542001-06-06 20:29:01 +00001480 case BytecodeFormat::BasicBlock: {
Chris Lattner89e02532004-01-18 21:08:15 +00001481 if (!InsertedArguments) {
1482 // Insert arguments into the value table before we parse the first basic
1483 // block in the function, but after we potentially read in the
1484 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001485 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001486 InsertedArguments = true;
1487 }
1488
Reid Spencer060d25d2004-06-29 23:29:38 +00001489 BasicBlock *BB = ParseBasicBlock(BlockNum++);
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001490 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +00001491 break;
1492 }
1493
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001494 case BytecodeFormat::InstructionList: {
Chris Lattner89e02532004-01-18 21:08:15 +00001495 // Insert arguments into the value table before we parse the instruction
1496 // list for the function, but after we potentially read in the compaction
1497 // table.
1498 if (!InsertedArguments) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001499 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001500 InsertedArguments = true;
1501 }
1502
Reid Spencer060d25d2004-06-29 23:29:38 +00001503 if (BlockNum)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001504 throw std::string("Already parsed basic blocks!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001505 BlockNum = ParseInstructionList(F);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001506 break;
1507 }
1508
Chris Lattner29b789b2003-11-19 17:27:18 +00001509 case BytecodeFormat::SymbolTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001510 ParseSymbolTable(F, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00001511 break;
1512
1513 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001514 At += Size;
1515 if (OldAt > At)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001516 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +00001517 break;
1518 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001519 BlockEnd = MyEnd;
Chris Lattner1d670cc2001-09-07 16:37:43 +00001520
Misha Brukman12c29d12003-09-22 23:38:23 +00001521 // Malformed bc file if read past end of block.
Reid Spencer060d25d2004-06-29 23:29:38 +00001522 align32();
Chris Lattner00950542001-06-06 20:29:01 +00001523 }
1524
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001525 // Make sure there were no references to non-existant basic blocks.
1526 if (BlockNum != ParsedBasicBlocks.size())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001527 throw std::string("Illegal basic block operand reference");
Reid Spencer060d25d2004-06-29 23:29:38 +00001528
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001529 ParsedBasicBlocks.clear();
1530
Chris Lattner97330cf2003-10-09 23:10:14 +00001531 // Resolve forward references. Replace any uses of a forward reference value
1532 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001533
Chris Lattner97330cf2003-10-09 23:10:14 +00001534 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
1535 // number of operands. PHI nodes often have forward references, and can also
1536 // often have a very large number of operands.
Chris Lattner89e02532004-01-18 21:08:15 +00001537 //
1538 // FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code
1539 // should be simplified back to using it!
1540 //
Chris Lattner97330cf2003-10-09 23:10:14 +00001541 std::map<Value*, Value*> ForwardRefMapping;
1542 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
1543 I = ForwardReferences.begin(), E = ForwardReferences.end();
1544 I != E; ++I)
1545 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
1546 false);
1547
1548 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1549 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1550 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1551 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
1552 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
1553 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
1554 }
1555
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001556 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +00001557 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
1558 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001559 Value *PlaceHolder = I->second;
1560 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +00001561
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001562 // Now that all the uses are gone, delete the placeholder...
1563 // If we couldn't find a def (error case), then leak a little
1564 // memory, because otherwise we can't remove all uses!
1565 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +00001566 }
Chris Lattner00950542001-06-06 20:29:01 +00001567
Misha Brukman12c29d12003-09-22 23:38:23 +00001568 // Clear out function-level types...
Reid Spencer060d25d2004-06-29 23:29:38 +00001569 FunctionTypes.clear();
1570 CompactionTypes.clear();
1571 CompactionValues.clear();
1572 freeTable(FunctionValues);
1573
Reid Spencer04cde2c2004-07-04 11:33:49 +00001574 if (Handler) Handler->handleFunctionEnd(F);
Chris Lattner00950542001-06-06 20:29:01 +00001575}
1576
Reid Spencer04cde2c2004-07-04 11:33:49 +00001577/// This function parses LLVM functions lazily. It obtains the type of the
1578/// function and records where the body of the function is in the bytecode
1579/// buffer. The caller can then use the ParseNextFunction and
1580/// ParseAllFunctionBodies to get handler events for the functions.
Reid Spencer060d25d2004-06-29 23:29:38 +00001581void BytecodeReader::ParseFunctionLazily() {
1582 if (FunctionSignatureList.empty())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001583 throw std::string("FunctionSignatureList empty!");
Chris Lattner89e02532004-01-18 21:08:15 +00001584
Reid Spencer060d25d2004-06-29 23:29:38 +00001585 Function *Func = FunctionSignatureList.back();
1586 FunctionSignatureList.pop_back();
Chris Lattner24102432004-01-18 22:35:34 +00001587
Reid Spencer060d25d2004-06-29 23:29:38 +00001588 // Save the information for future reading of the function
1589 LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd);
Chris Lattner89e02532004-01-18 21:08:15 +00001590
Reid Spencer060d25d2004-06-29 23:29:38 +00001591 // Pretend we've `parsed' this function
1592 At = BlockEnd;
1593}
Chris Lattner89e02532004-01-18 21:08:15 +00001594
Reid Spencer04cde2c2004-07-04 11:33:49 +00001595/// The ParserFunction method lazily parses one function. Use this method to
1596/// casue the parser to parse a specific function in the module. Note that
1597/// this will remove the function from what is to be included by
1598/// ParseAllFunctionBodies.
1599/// @see ParseAllFunctionBodies
1600/// @see ParseBytecode
Reid Spencer060d25d2004-06-29 23:29:38 +00001601void BytecodeReader::ParseFunction(Function* Func) {
1602 // Find {start, end} pointers and slot in the map. If not there, we're done.
1603 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
Chris Lattner89e02532004-01-18 21:08:15 +00001604
Reid Spencer060d25d2004-06-29 23:29:38 +00001605 // Make sure we found it
1606 if ( Fi == LazyFunctionLoadMap.end() ) {
1607 PARSE_ERROR("Unrecognized function of type " << Func->getType()->getDescription());
1608 return;
Chris Lattner89e02532004-01-18 21:08:15 +00001609 }
1610
Reid Spencer060d25d2004-06-29 23:29:38 +00001611 BlockStart = At = Fi->second.Buf;
1612 BlockEnd = Fi->second.EndBuf;
1613 assert(Fi->first == Func);
1614
1615 LazyFunctionLoadMap.erase(Fi);
1616
1617 this->ParseFunctionBody( Func );
Chris Lattner89e02532004-01-18 21:08:15 +00001618}
1619
Reid Spencer04cde2c2004-07-04 11:33:49 +00001620/// The ParseAllFunctionBodies method parses through all the previously
1621/// unparsed functions in the bytecode file. If you want to completely parse
1622/// a bytecode file, this method should be called after Parsebytecode because
1623/// Parsebytecode only records the locations in the bytecode file of where
1624/// the function definitions are located. This function uses that information
1625/// to materialize the functions.
1626/// @see ParseBytecode
Reid Spencer060d25d2004-06-29 23:29:38 +00001627void BytecodeReader::ParseAllFunctionBodies() {
1628 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
1629 LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
Chris Lattner89e02532004-01-18 21:08:15 +00001630
Reid Spencer060d25d2004-06-29 23:29:38 +00001631 while ( Fi != Fe ) {
1632 Function* Func = Fi->first;
1633 BlockStart = At = Fi->second.Buf;
1634 BlockEnd = Fi->second.EndBuf;
1635 this->ParseFunctionBody(Func);
1636 ++Fi;
1637 }
1638}
Chris Lattner89e02532004-01-18 21:08:15 +00001639
Reid Spencer04cde2c2004-07-04 11:33:49 +00001640/// Parse the global type list
Reid Spencer060d25d2004-06-29 23:29:38 +00001641void BytecodeReader::ParseGlobalTypes() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001642 // Read the number of types
1643 unsigned NumEntries = read_vbr_uint();
Reid Spencer011bed52004-07-09 21:13:53 +00001644
1645 // Ignore the type plane identifier for types if the bc file is pre 1.3
1646 if (hasTypeDerivedFromValue)
1647 read_vbr_uint();
1648
Reid Spencer04cde2c2004-07-04 11:33:49 +00001649 ParseTypeConstants(ModuleTypes, NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001650}
1651
Reid Spencer04cde2c2004-07-04 11:33:49 +00001652/// Parse the Global info (types, global vars, constants)
Reid Spencer060d25d2004-06-29 23:29:38 +00001653void BytecodeReader::ParseModuleGlobalInfo() {
1654
Reid Spencer04cde2c2004-07-04 11:33:49 +00001655 if (Handler) Handler->handleModuleGlobalsBegin();
Chris Lattner00950542001-06-06 20:29:01 +00001656
Chris Lattner70cc3392001-09-10 07:58:01 +00001657 // Read global variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00001658 unsigned VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001659 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattner9dd87702004-04-03 23:43:42 +00001660 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
1661 // Linkage, bit4+ = slot#
1662 unsigned SlotNo = VarType >> 5;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001663 bool isTypeType = sanitizeTypeId(SlotNo);
1664 assert(!isTypeType && "Invalid type (type type) for global var!");
Chris Lattner9dd87702004-04-03 23:43:42 +00001665 unsigned LinkageID = (VarType >> 2) & 7;
Reid Spencer060d25d2004-06-29 23:29:38 +00001666 bool isConstant = VarType & 1;
1667 bool hasInitializer = VarType & 2;
Chris Lattnere3869c82003-04-16 21:16:05 +00001668 GlobalValue::LinkageTypes Linkage;
1669
Chris Lattnerc08912f2004-01-14 16:44:44 +00001670 switch (LinkageID) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00001671 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1672 case 1: Linkage = GlobalValue::WeakLinkage; break;
1673 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1674 case 3: Linkage = GlobalValue::InternalLinkage; break;
1675 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001676 default:
1677 PARSE_ERROR("Unknown linkage type: " << LinkageID);
1678 Linkage = GlobalValue::InternalLinkage;
1679 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001680 }
1681
1682 const Type *Ty = getType(SlotNo);
Reid Spencer060d25d2004-06-29 23:29:38 +00001683 if ( !Ty ) {
1684 PARSE_ERROR("Global has no type! SlotNo=" << SlotNo);
1685 }
1686
1687 if ( !isa<PointerType>(Ty)) {
1688 PARSE_ERROR("Global not a pointer type! Ty= " << Ty->getDescription());
1689 }
Chris Lattner70cc3392001-09-10 07:58:01 +00001690
Chris Lattner52e20b02003-03-19 20:54:26 +00001691 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +00001692
Chris Lattner70cc3392001-09-10 07:58:01 +00001693 // Create the global variable...
Reid Spencer060d25d2004-06-29 23:29:38 +00001694 GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +00001695 0, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +00001696 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +00001697
Reid Spencer060d25d2004-06-29 23:29:38 +00001698 unsigned initSlot = 0;
1699 if (hasInitializer) {
1700 initSlot = read_vbr_uint();
1701 GlobalInits.push_back(std::make_pair(GV, initSlot));
1702 }
1703
1704 // Notify handler about the global value.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001705 if (Handler) Handler->handleGlobalVariable( ElTy, isConstant, Linkage, SlotNo, initSlot );
Reid Spencer060d25d2004-06-29 23:29:38 +00001706
1707 // Get next item
1708 VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001709 }
1710
Chris Lattner52e20b02003-03-19 20:54:26 +00001711 // Read the function objects for all of the functions that are coming
Reid Spencer04cde2c2004-07-04 11:33:49 +00001712 unsigned FnSignature = 0;
1713 bool isTypeType = read_typeid(FnSignature);
1714 assert(!isTypeType && "Invalid function type (type type) found");
Chris Lattner74734132002-08-17 22:01:27 +00001715 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
1716 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +00001717 if (!isa<PointerType>(Ty) ||
Reid Spencer060d25d2004-06-29 23:29:38 +00001718 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
1719 PARSE_ERROR( "Function not a pointer to function type! Ty = " +
Misha Brukman12c29d12003-09-22 23:38:23 +00001720 Ty->getDescription());
Reid Spencer060d25d2004-06-29 23:29:38 +00001721 // FIXME: what should Ty be if handler continues?
1722 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +00001723
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001724 // We create functions by passing the underlying FunctionType to create...
Reid Spencer060d25d2004-06-29 23:29:38 +00001725 const FunctionType* FTy =
1726 cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +00001727
Reid Spencer060d25d2004-06-29 23:29:38 +00001728 // Insert the place hodler
1729 Function* Func = new Function(FTy, GlobalValue::InternalLinkage,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001730 "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +00001731 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +00001732
Reid Spencer060d25d2004-06-29 23:29:38 +00001733 // Save this for later so we know type of lazily instantiated functions
Chris Lattner29b789b2003-11-19 17:27:18 +00001734 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +00001735
Reid Spencer04cde2c2004-07-04 11:33:49 +00001736 if (Handler) Handler->handleFunctionDeclaration(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00001737
1738 // Get Next function signature
Reid Spencer04cde2c2004-07-04 11:33:49 +00001739 isTypeType = read_typeid(FnSignature);
1740 assert(!isTypeType && "Invalid function type (type type) found");
Chris Lattner00950542001-06-06 20:29:01 +00001741 }
1742
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001743 if (hasInconsistentModuleGlobalInfo)
Reid Spencer060d25d2004-06-29 23:29:38 +00001744 align32();
Chris Lattner74734132002-08-17 22:01:27 +00001745
1746 // Now that the function signature list is set up, reverse it so that we can
1747 // remove elements efficiently from the back of the vector.
1748 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +00001749
1750 // This is for future proofing... in the future extra fields may be added that
1751 // we don't understand, so we transparently ignore them.
1752 //
Reid Spencer060d25d2004-06-29 23:29:38 +00001753 At = BlockEnd;
1754
Reid Spencer04cde2c2004-07-04 11:33:49 +00001755 if (Handler) Handler->handleModuleGlobalsEnd();
Chris Lattner00950542001-06-06 20:29:01 +00001756}
1757
Reid Spencer04cde2c2004-07-04 11:33:49 +00001758/// Parse the version information and decode it by setting flags on the
1759/// Reader that enable backward compatibility of the reader.
Reid Spencer060d25d2004-06-29 23:29:38 +00001760void BytecodeReader::ParseVersionInfo() {
1761 unsigned Version = read_vbr_uint();
Chris Lattner036b8aa2003-03-06 17:55:45 +00001762
1763 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001764 Module::Endianness Endianness;
1765 Module::PointerSize PointerSize;
1766 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
1767 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
1768
1769 bool hasNoEndianness = Version & 4;
1770 bool hasNoPointerSize = Version & 8;
1771
1772 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +00001773
1774 // Default values for the current bytecode version
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001775 hasInconsistentModuleGlobalInfo = false;
Chris Lattner80b97342004-01-17 23:25:43 +00001776 hasExplicitPrimitiveZeros = false;
Chris Lattner5fa428f2004-04-05 01:27:26 +00001777 hasRestrictedGEPTypes = false;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001778 hasTypeDerivedFromValue = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +00001779
1780 switch (RevisionNum) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00001781 case 0: // LLVM 1.0, 1.1 release version
Chris Lattner9e893e82004-01-14 23:35:21 +00001782 // Base LLVM 1.0 bytecode format.
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001783 hasInconsistentModuleGlobalInfo = true;
Chris Lattner80b97342004-01-17 23:25:43 +00001784 hasExplicitPrimitiveZeros = true;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001785
Chris Lattner80b97342004-01-17 23:25:43 +00001786 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +00001787 case 1: // LLVM 1.2 release version
Chris Lattner9e893e82004-01-14 23:35:21 +00001788 // LLVM 1.2 added explicit support for emitting strings efficiently.
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001789
1790 // Also, it fixed the problem where the size of the ModuleGlobalInfo block
1791 // included the size for the alignment at the end, where the rest of the
1792 // blocks did not.
Chris Lattner5fa428f2004-04-05 01:27:26 +00001793
1794 // LLVM 1.2 and before required that GEP indices be ubyte constants for
1795 // structures and longs for sequential types.
1796 hasRestrictedGEPTypes = true;
1797
Reid Spencer04cde2c2004-07-04 11:33:49 +00001798 // LLVM 1.2 and before had the Type class derive from Value class. This
1799 // changed in release 1.3 and consequently LLVM 1.3 bytecode files are
1800 // written differently because Types can no longer be part of the
1801 // type planes for Values.
1802 hasTypeDerivedFromValue = true;
1803
Chris Lattner5fa428f2004-04-05 01:27:26 +00001804 // FALL THROUGH
1805 case 2: // LLVM 1.3 release version
Chris Lattnerc08912f2004-01-14 16:44:44 +00001806 break;
1807
Chris Lattner036b8aa2003-03-06 17:55:45 +00001808 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001809 PARSE_ERROR("Unknown bytecode version number: " << RevisionNum);
Chris Lattner036b8aa2003-03-06 17:55:45 +00001810 }
1811
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001812 if (hasNoEndianness) Endianness = Module::AnyEndianness;
1813 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +00001814
Reid Spencer04cde2c2004-07-04 11:33:49 +00001815 if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize );
Chris Lattner036b8aa2003-03-06 17:55:45 +00001816}
1817
Reid Spencer04cde2c2004-07-04 11:33:49 +00001818/// Parse a whole module.
Reid Spencer060d25d2004-06-29 23:29:38 +00001819void BytecodeReader::ParseModule() {
Chris Lattner00950542001-06-06 20:29:01 +00001820 unsigned Type, Size;
Chris Lattner00950542001-06-06 20:29:01 +00001821
Reid Spencer060d25d2004-06-29 23:29:38 +00001822 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +00001823
1824 // Read into instance variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00001825 ParseVersionInfo();
1826 align32(); /// FIXME: Is this redundant? VI is first and 4 bytes!
Chris Lattner00950542001-06-06 20:29:01 +00001827
Reid Spencer060d25d2004-06-29 23:29:38 +00001828 bool SeenModuleGlobalInfo = false;
1829 bool SeenGlobalTypePlane = false;
1830 BufPtr MyEnd = BlockEnd;
1831 while (At < MyEnd) {
1832 BufPtr OldAt = At;
1833 read_block(Type, Size);
1834
Chris Lattner00950542001-06-06 20:29:01 +00001835 switch (Type) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001836
Chris Lattner52e20b02003-03-19 20:54:26 +00001837 case BytecodeFormat::GlobalTypePlane:
Reid Spencer060d25d2004-06-29 23:29:38 +00001838 if ( SeenGlobalTypePlane )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001839 throw std::string("Two GlobalTypePlane Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001840
1841 ParseGlobalTypes();
1842 SeenGlobalTypePlane = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00001843 break;
1844
Reid Spencer060d25d2004-06-29 23:29:38 +00001845 case BytecodeFormat::ModuleGlobalInfo:
1846 if ( SeenModuleGlobalInfo )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001847 throw std::string("Two ModuleGlobalInfo Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001848 ParseModuleGlobalInfo();
1849 SeenModuleGlobalInfo = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00001850 break;
1851
Chris Lattner1d670cc2001-09-07 16:37:43 +00001852 case BytecodeFormat::ConstantPool:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001853 ParseConstantPool(ModuleValues, ModuleTypes,false);
Chris Lattner00950542001-06-06 20:29:01 +00001854 break;
1855
Reid Spencer060d25d2004-06-29 23:29:38 +00001856 case BytecodeFormat::Function:
1857 ParseFunctionLazily();
Chris Lattner00950542001-06-06 20:29:01 +00001858 break;
Chris Lattner00950542001-06-06 20:29:01 +00001859
1860 case BytecodeFormat::SymbolTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001861 ParseSymbolTable(0, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00001862 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001863
Chris Lattner00950542001-06-06 20:29:01 +00001864 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001865 At += Size;
1866 if (OldAt > At) {
1867 PARSE_ERROR("Unexpected Block of Type" << Type << "encountered!" );
1868 }
Chris Lattner00950542001-06-06 20:29:01 +00001869 break;
1870 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001871 BlockEnd = MyEnd;
1872 align32();
Chris Lattner00950542001-06-06 20:29:01 +00001873 }
1874
Chris Lattner52e20b02003-03-19 20:54:26 +00001875 // After the module constant pool has been read, we can safely initialize
1876 // global variables...
1877 while (!GlobalInits.empty()) {
1878 GlobalVariable *GV = GlobalInits.back().first;
1879 unsigned Slot = GlobalInits.back().second;
1880 GlobalInits.pop_back();
1881
1882 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +00001883 // FIXME: Preserve this type ID!
Reid Spencer060d25d2004-06-29 23:29:38 +00001884
1885 const llvm::PointerType* GVType = GV->getType();
1886 unsigned TypeSlot = getTypeSlot(GVType->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +00001887 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman12c29d12003-09-22 23:38:23 +00001888 if (GV->hasInitializer())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001889 throw std::string("Global *already* has an initializer?!");
1890 if (Handler) Handler->handleGlobalInitializer(GV,CV);
Chris Lattner93361992004-01-15 18:45:25 +00001891 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +00001892 } else
Reid Spencer04cde2c2004-07-04 11:33:49 +00001893 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +00001894 }
1895
Reid Spencer060d25d2004-06-29 23:29:38 +00001896 /// Make sure we pulled them all out. If we didn't then there's a declaration
1897 /// but a missing body. That's not allowed.
Misha Brukman12c29d12003-09-22 23:38:23 +00001898 if (!FunctionSignatureList.empty())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001899 throw std::string(
Reid Spencer060d25d2004-06-29 23:29:38 +00001900 "Function declared, but bytecode stream ended before definition");
Chris Lattner00950542001-06-06 20:29:01 +00001901}
1902
Reid Spencer04cde2c2004-07-04 11:33:49 +00001903/// This function completely parses a bytecode buffer given by the \p Buf
1904/// and \p Length parameters.
Reid Spencer060d25d2004-06-29 23:29:38 +00001905void BytecodeReader::ParseBytecode(
1906 BufPtr Buf, unsigned Length,
Reid Spencer5c15fe52004-07-05 00:57:50 +00001907 const std::string &ModuleID,
1908 bool processFunctions) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +00001909
Reid Spencer060d25d2004-06-29 23:29:38 +00001910 try {
1911 At = MemStart = BlockStart = Buf;
1912 MemEnd = BlockEnd = Buf + Length;
Misha Brukmane0dd0d42003-09-23 16:15:29 +00001913
Reid Spencer060d25d2004-06-29 23:29:38 +00001914 // Create the module
1915 TheModule = new Module(ModuleID);
Chris Lattner00950542001-06-06 20:29:01 +00001916
Reid Spencer04cde2c2004-07-04 11:33:49 +00001917 if (Handler) Handler->handleStart(TheModule, Length);
Reid Spencer060d25d2004-06-29 23:29:38 +00001918
1919 // Read and check signature...
1920 unsigned Sig = read_uint();
1921 if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) {
1922 PARSE_ERROR("Invalid bytecode signature: " << Sig);
1923 }
1924
1925
1926 // Tell the handler we're starting a module
Reid Spencer04cde2c2004-07-04 11:33:49 +00001927 if (Handler) Handler->handleModuleBegin(ModuleID);
Reid Spencer060d25d2004-06-29 23:29:38 +00001928
1929 // Get the module block and size and verify
1930 unsigned Type, Size;
1931 read_block(Type, Size);
1932 if ( Type != BytecodeFormat::Module ) {
1933 PARSE_ERROR("Expected Module Block! At: " << unsigned(intptr_t(At))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001934 << ", Type:" << Type << ", Size:" << Size);
Reid Spencer060d25d2004-06-29 23:29:38 +00001935 }
1936 if ( At + Size != MemEnd ) {
1937 PARSE_ERROR("Invalid Top Level Block Length! At: "
Reid Spencer04cde2c2004-07-04 11:33:49 +00001938 << unsigned(intptr_t(At)) << ", Type:" << Type << ", Size:" << Size);
Reid Spencer060d25d2004-06-29 23:29:38 +00001939 }
1940
1941 // Parse the module contents
1942 this->ParseModule();
1943
Reid Spencer060d25d2004-06-29 23:29:38 +00001944 // Check for missing functions
1945 if ( hasFunctions() )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001946 throw std::string("Function expected, but bytecode stream ended!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001947
Reid Spencer5c15fe52004-07-05 00:57:50 +00001948 // Process all the function bodies now, if requested
1949 if ( processFunctions )
1950 ParseAllFunctionBodies();
1951
1952 // Tell the handler we're done with the module
1953 if (Handler)
1954 Handler->handleModuleEnd(ModuleID);
1955
1956 // Tell the handler we're finished the parse
Reid Spencer04cde2c2004-07-04 11:33:49 +00001957 if (Handler) Handler->handleFinish();
Reid Spencer060d25d2004-06-29 23:29:38 +00001958
1959 } catch (std::string& errstr ) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001960 if (Handler) Handler->handleError(errstr);
Reid Spencer060d25d2004-06-29 23:29:38 +00001961 freeState();
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001962 delete TheModule;
1963 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +00001964 throw;
Reid Spencer060d25d2004-06-29 23:29:38 +00001965 } catch (...) {
1966 std::string msg("Unknown Exception Occurred");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001967 if (Handler) Handler->handleError(msg);
Reid Spencer060d25d2004-06-29 23:29:38 +00001968 freeState();
1969 delete TheModule;
1970 TheModule = 0;
1971 throw msg;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001972 }
Chris Lattner00950542001-06-06 20:29:01 +00001973}
Reid Spencer060d25d2004-06-29 23:29:38 +00001974
1975//===----------------------------------------------------------------------===//
1976//=== Default Implementations of Handler Methods
1977//===----------------------------------------------------------------------===//
1978
1979BytecodeHandler::~BytecodeHandler() {}
Reid Spencer060d25d2004-06-29 23:29:38 +00001980
1981// vim: sw=2