blob: 8d0df020ec31b1eaf803f7c61b1946efc7d08c3f [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];
Reid Spencer04cde2c2004-07-04 11:33:49 +0000897 if (Handler) Handler->handleBasicBlockEnd( BlockNo );
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000898 ++BlockNo;
899 F->getBasicBlockList().push_back(BB);
900
901 // Read instructions into this basic block until we get to a terminator
Reid Spencer060d25d2004-06-29 23:29:38 +0000902 while ( moreInBlock() && !BB->getTerminator())
903 ParseInstruction(Args, BB);
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000904
905 if (!BB->getTerminator())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000906 throw std::string("Non-terminated basic block found!");
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000907 }
908
909 return BlockNo;
910}
911
Reid Spencer04cde2c2004-07-04 11:33:49 +0000912/// Parse a symbol table. This works for both module level and function
913/// level symbol tables. For function level symbol tables, the CurrentFunction
914/// parameter must be non-zero and the ST parameter must correspond to
915/// CurrentFunction's symbol table. For Module level symbol tables, the
916/// CurrentFunction argument must be zero.
Reid Spencer060d25d2004-06-29 23:29:38 +0000917void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
Reid Spencer04cde2c2004-07-04 11:33:49 +0000918 SymbolTable *ST) {
919 if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
Reid Spencer060d25d2004-06-29 23:29:38 +0000920
Chris Lattner39cacce2003-10-10 05:43:47 +0000921 // Allow efficient basic block lookup by number.
922 std::vector<BasicBlock*> BBMap;
923 if (CurrentFunction)
924 for (Function::iterator I = CurrentFunction->begin(),
925 E = CurrentFunction->end(); I != E; ++I)
926 BBMap.push_back(I);
927
Reid Spencer04cde2c2004-07-04 11:33:49 +0000928 /// In LLVM 1.3 we write types separately from values so
929 /// The types are always first in the symbol table. This is
930 /// because Type no longer derives from Value.
931 if ( ! hasTypeDerivedFromValue ) {
932 // Symtab block header: [num entries]
933 unsigned NumEntries = read_vbr_uint();
934 for ( unsigned i = 0; i < NumEntries; ++i ) {
935 // Symtab entry: [def slot #][name]
936 unsigned slot = read_vbr_uint();
937 std::string Name = read_str();
938 const Type* T = getType(slot);
939 ST->insert(Name, T);
940 }
941 }
942
Reid Spencer060d25d2004-06-29 23:29:38 +0000943 while ( moreInBlock() ) {
Chris Lattner00950542001-06-06 20:29:01 +0000944 // Symtab block header: [num entries][type id number]
Reid Spencer060d25d2004-06-29 23:29:38 +0000945 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +0000946 unsigned Typ = 0;
947 bool isTypeType = read_typeid(Typ);
Chris Lattner00950542001-06-06 20:29:01 +0000948 const Type *Ty = getType(Typ);
Chris Lattner1d670cc2001-09-07 16:37:43 +0000949
Chris Lattner7dc3a2e2003-10-13 14:57:53 +0000950 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +0000951 // Symtab entry: [def slot #][name]
Reid Spencer060d25d2004-06-29 23:29:38 +0000952 unsigned slot = read_vbr_uint();
953 std::string Name = read_str();
Chris Lattner00950542001-06-06 20:29:01 +0000954
Reid Spencer04cde2c2004-07-04 11:33:49 +0000955 // if we're reading a pre 1.3 bytecode file and the type plane
956 // is the "type type", handle it here
957 if ( isTypeType ) {
958 const Type* T = getType(slot);
959 if ( T == 0 )
960 PARSE_ERROR("Failed type look-up for name '" << Name << "'");
961 ST->insert(Name, T);
962 continue; // code below must be short circuited
Chris Lattner39cacce2003-10-10 05:43:47 +0000963 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +0000964 Value *V = 0;
965 if (Typ == Type::LabelTyID) {
966 if (slot < BBMap.size())
967 V = BBMap[slot];
968 } else {
969 V = getValue(Typ, slot, false); // Find mapping...
970 }
971 if (V == 0)
972 PARSE_ERROR("Failed value look-up for name '" << Name << "'");
973 V->setName(Name, ST);
Chris Lattner39cacce2003-10-10 05:43:47 +0000974 }
Chris Lattner00950542001-06-06 20:29:01 +0000975 }
976 }
Reid Spencer060d25d2004-06-29 23:29:38 +0000977 checkPastBlockEnd("Symbol Table");
Reid Spencer04cde2c2004-07-04 11:33:49 +0000978 if (Handler) Handler->handleSymbolTableEnd();
Chris Lattner00950542001-06-06 20:29:01 +0000979}
980
Reid Spencer04cde2c2004-07-04 11:33:49 +0000981/// Read in the types portion of a compaction table.
982void BytecodeReader::ParseCompactionTypes( unsigned NumEntries ) {
983 for (unsigned i = 0; i != NumEntries; ++i) {
984 unsigned TypeSlot = 0;
985 bool isTypeType = read_typeid(TypeSlot);
986 assert(!isTypeType && "Invalid type in compaction table: type type");
987 const Type *Typ = getGlobalTableType(TypeSlot);
988 CompactionTypes.push_back(Typ);
989 if (Handler) Handler->handleCompactionTableType( i, TypeSlot, Typ );
990 }
991}
992
993/// Parse a compaction table.
Reid Spencer060d25d2004-06-29 23:29:38 +0000994void BytecodeReader::ParseCompactionTable() {
995
Reid Spencer04cde2c2004-07-04 11:33:49 +0000996 if (Handler) Handler->handleCompactionTableBegin();
997
998 /// In LLVM 1.3 Type no longer derives from Value. So,
999 /// we always write them first in the compaction table
1000 /// because they can't occupy a "type plane" where the
1001 /// Values reside.
1002 if ( ! hasTypeDerivedFromValue ) {
1003 unsigned NumEntries = read_vbr_uint();
1004 ParseCompactionTypes( NumEntries );
1005 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001006
1007 while ( moreInBlock() ) {
1008 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001009 unsigned Ty = 0;
1010 unsigned isTypeType = false;
Reid Spencer060d25d2004-06-29 23:29:38 +00001011
1012 if ((NumEntries & 3) == 3) {
1013 NumEntries >>= 2;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001014 isTypeType = read_typeid(Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +00001015 } else {
1016 Ty = NumEntries >> 2;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001017 isTypeType = sanitizeTypeId(Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +00001018 NumEntries &= 3;
1019 }
1020
Reid Spencer04cde2c2004-07-04 11:33:49 +00001021 // if we're reading a pre 1.3 bytecode file and the type plane
1022 // is the "type type", handle it here
1023 if ( isTypeType ) {
1024 ParseCompactionTypes(NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001025 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001026 if (Ty >= CompactionValues.size())
1027 CompactionValues.resize(Ty+1);
1028
1029 if (!CompactionValues[Ty].empty())
1030 throw std::string("Compaction table plane contains multiple entries!");
1031
1032 if (Handler) Handler->handleCompactionTablePlane( Ty, NumEntries );
1033
Reid Spencer060d25d2004-06-29 23:29:38 +00001034 const Type *Typ = getType(Ty);
1035 // Push the implicit zero
1036 CompactionValues[Ty].push_back(Constant::getNullValue(Typ));
1037 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001038 unsigned ValSlot = read_vbr_uint();
1039 Value *V = getGlobalTableValue(Typ, ValSlot);
1040 CompactionValues[Ty].push_back(V);
1041 if (Handler) Handler->handleCompactionTableValue( i, Ty, ValSlot, Typ );
Reid Spencer060d25d2004-06-29 23:29:38 +00001042 }
1043 }
1044 }
Reid Spencer04cde2c2004-07-04 11:33:49 +00001045 if (Handler) Handler->handleCompactionTableEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001046}
1047
1048// Parse a single type constant.
1049const Type *BytecodeReader::ParseTypeConstant() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001050 unsigned PrimType = 0;
1051 bool isTypeType = read_typeid(PrimType);
1052 assert(!isTypeType && "Invalid type (type type) in type constants!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001053
1054 const Type *Result = 0;
1055 if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType)))
1056 return Result;
1057
1058 switch (PrimType) {
1059 case Type::FunctionTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001060 const Type *RetType = readSanitizedType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001061
1062 unsigned NumParams = read_vbr_uint();
1063
1064 std::vector<const Type*> Params;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001065 while (NumParams--)
1066 Params.push_back(readSanitizedType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001067
1068 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1069 if (isVarArg) Params.pop_back();
1070
1071 Result = FunctionType::get(RetType, Params, isVarArg);
1072 break;
1073 }
1074 case Type::ArrayTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001075 const Type *ElementType = readSanitizedType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001076 unsigned NumElements = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001077 Result = ArrayType::get(ElementType, NumElements);
1078 break;
1079 }
1080 case Type::StructTyID: {
1081 std::vector<const Type*> Elements;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001082 unsigned Typ = 0;
1083 bool isTypeType = read_typeid(Typ);
1084 assert(!isTypeType && "Invalid element type (type type) for structure!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001085 while (Typ) { // List is terminated by void/0 typeid
1086 Elements.push_back(getType(Typ));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001087 bool isTypeType = read_typeid(Typ);
1088 assert(!isTypeType && "Invalid element type (type type) for structure!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001089 }
1090
1091 Result = StructType::get(Elements);
1092 break;
1093 }
1094 case Type::PointerTyID: {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001095 Result = PointerType::get(readSanitizedType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001096 break;
1097 }
1098
1099 case Type::OpaqueTyID: {
1100 Result = OpaqueType::get();
1101 break;
1102 }
1103
1104 default:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001105 PARSE_ERROR("Don't know how to deserialize primitive type"
1106 << PrimType << "\n");
Reid Spencer060d25d2004-06-29 23:29:38 +00001107 break;
1108 }
Reid Spencer04cde2c2004-07-04 11:33:49 +00001109 if (Handler) Handler->handleType( Result );
Reid Spencer060d25d2004-06-29 23:29:38 +00001110 return Result;
1111}
1112
1113// ParseTypeConstants - We have to use this weird code to handle recursive
1114// types. We know that recursive types will only reference the current slab of
1115// values in the type plane, but they can forward reference types before they
1116// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
1117// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
1118// this ugly problem, we pessimistically insert an opaque type for each type we
1119// are about to read. This means that forward references will resolve to
1120// something and when we reread the type later, we can replace the opaque type
1121// with a new resolved concrete type.
1122//
1123void BytecodeReader::ParseTypeConstants(TypeListTy &Tab, unsigned NumEntries){
1124 assert(Tab.size() == 0 && "should not have read type constants in before!");
1125
1126 // Insert a bunch of opaque types to be resolved later...
1127 Tab.reserve(NumEntries);
1128 for (unsigned i = 0; i != NumEntries; ++i)
1129 Tab.push_back(OpaqueType::get());
1130
1131 // Loop through reading all of the types. Forward types will make use of the
1132 // opaque types just inserted.
1133 //
1134 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001135 const Type* NewTy = ParseTypeConstant();
1136 const Type* OldTy = Tab[i].get();
1137 if (NewTy == 0)
1138 throw std::string("Couldn't parse type!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001139
1140 // Don't directly push the new type on the Tab. Instead we want to replace
1141 // the opaque type we previously inserted with the new concrete value. This
1142 // approach helps with forward references to types. The refinement from the
1143 // abstract (opaque) type to the new type causes all uses of the abstract
1144 // type to use the concrete type (NewTy). This will also cause the opaque
1145 // type to be deleted.
1146 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
1147
1148 // This should have replaced the old opaque type with the new type in the
1149 // value table... or with a preexisting type that was already in the system.
1150 // Let's just make sure it did.
1151 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
1152 }
1153}
1154
Reid Spencer04cde2c2004-07-04 11:33:49 +00001155/// Parse a single constant value
Reid Spencer060d25d2004-06-29 23:29:38 +00001156Constant *BytecodeReader::ParseConstantValue( unsigned TypeID) {
1157 // We must check for a ConstantExpr before switching by type because
1158 // a ConstantExpr can be of any type, and has no explicit value.
1159 //
1160 // 0 if not expr; numArgs if is expr
1161 unsigned isExprNumArgs = read_vbr_uint();
1162
1163 if (isExprNumArgs) {
1164 // FIXME: Encoding of constant exprs could be much more compact!
1165 std::vector<Constant*> ArgVec;
1166 ArgVec.reserve(isExprNumArgs);
1167 unsigned Opcode = read_vbr_uint();
1168
1169 // Read the slot number and types of each of the arguments
1170 for (unsigned i = 0; i != isExprNumArgs; ++i) {
1171 unsigned ArgValSlot = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001172 unsigned ArgTypeSlot = 0;
1173 bool isTypeType = read_typeid(ArgTypeSlot);
1174 assert(!isTypeType && "Invalid argument type (type type) for constant value");
Reid Spencer060d25d2004-06-29 23:29:38 +00001175
1176 // Get the arg value from its slot if it exists, otherwise a placeholder
1177 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
1178 }
1179
1180 // Construct a ConstantExpr of the appropriate kind
1181 if (isExprNumArgs == 1) { // All one-operand expressions
1182 assert(Opcode == Instruction::Cast);
1183 Constant* Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001184 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001185 return Result;
1186 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
1187 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
1188
1189 if (hasRestrictedGEPTypes) {
1190 const Type *BaseTy = ArgVec[0]->getType();
1191 generic_gep_type_iterator<std::vector<Constant*>::iterator>
1192 GTI = gep_type_begin(BaseTy, IdxList.begin(), IdxList.end()),
1193 E = gep_type_end(BaseTy, IdxList.begin(), IdxList.end());
1194 for (unsigned i = 0; GTI != E; ++GTI, ++i)
1195 if (isa<StructType>(*GTI)) {
1196 if (IdxList[i]->getType() != Type::UByteTy)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001197 throw std::string("Invalid index for getelementptr!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001198 IdxList[i] = ConstantExpr::getCast(IdxList[i], Type::UIntTy);
1199 }
1200 }
1201
1202 Constant* Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001203 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001204 return Result;
1205 } else if (Opcode == Instruction::Select) {
1206 assert(ArgVec.size() == 3);
1207 Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
Reid Spencer04cde2c2004-07-04 11:33:49 +00001208 ArgVec[2]);
1209 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001210 return Result;
1211 } else { // All other 2-operand expressions
1212 Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001213 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001214 return Result;
1215 }
1216 }
1217
1218 // Ok, not an ConstantExpr. We now know how to read the given type...
1219 const Type *Ty = getType(TypeID);
1220 switch (Ty->getTypeID()) {
1221 case Type::BoolTyID: {
1222 unsigned Val = read_vbr_uint();
1223 if (Val != 0 && Val != 1)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001224 throw std::string("Invalid boolean value read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001225 Constant* Result = ConstantBool::get(Val == 1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001226 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001227 return Result;
1228 }
1229
1230 case Type::UByteTyID: // Unsigned integer types...
1231 case Type::UShortTyID:
1232 case Type::UIntTyID: {
1233 unsigned Val = read_vbr_uint();
1234 if (!ConstantUInt::isValueValidForType(Ty, Val))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001235 throw std::string("Invalid unsigned byte/short/int read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001236 Constant* Result = ConstantUInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001237 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001238 return Result;
1239 }
1240
1241 case Type::ULongTyID: {
1242 Constant* Result = ConstantUInt::get(Ty, read_vbr_uint64());
Reid Spencer04cde2c2004-07-04 11:33:49 +00001243 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001244 return Result;
1245 }
1246
1247 case Type::SByteTyID: // Signed integer types...
1248 case Type::ShortTyID:
1249 case Type::IntTyID: {
1250 case Type::LongTyID:
1251 int64_t Val = read_vbr_int64();
1252 if (!ConstantSInt::isValueValidForType(Ty, Val))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001253 throw std::string("Invalid signed byte/short/int/long read.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001254 Constant* Result = ConstantSInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001255 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001256 return Result;
1257 }
1258
1259 case Type::FloatTyID: {
1260 float F;
1261 read_data(&F, &F+1);
1262 Constant* Result = ConstantFP::get(Ty, F);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001263 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001264 return Result;
1265 }
1266
1267 case Type::DoubleTyID: {
1268 double Val;
1269 read_data(&Val, &Val+1);
1270 Constant* Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001271 if (Handler) Handler->handleConstantValue(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001272 return Result;
1273 }
1274
Reid Spencer060d25d2004-06-29 23:29:38 +00001275 case Type::ArrayTyID: {
1276 const ArrayType *AT = cast<ArrayType>(Ty);
1277 unsigned NumElements = AT->getNumElements();
1278 unsigned TypeSlot = getTypeSlot(AT->getElementType());
1279 std::vector<Constant*> Elements;
1280 Elements.reserve(NumElements);
1281 while (NumElements--) // Read all of the elements of the constant.
1282 Elements.push_back(getConstantValue(TypeSlot,
1283 read_vbr_uint()));
1284 Constant* Result = ConstantArray::get(AT, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001285 if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001286 return Result;
1287 }
1288
1289 case Type::StructTyID: {
1290 const StructType *ST = cast<StructType>(Ty);
1291
1292 std::vector<Constant *> Elements;
1293 Elements.reserve(ST->getNumElements());
1294 for (unsigned i = 0; i != ST->getNumElements(); ++i)
1295 Elements.push_back(getConstantValue(ST->getElementType(i),
1296 read_vbr_uint()));
1297
1298 Constant* Result = ConstantStruct::get(ST, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001299 if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001300 return Result;
1301 }
1302
1303 case Type::PointerTyID: { // ConstantPointerRef value...
1304 const PointerType *PT = cast<PointerType>(Ty);
1305 unsigned Slot = read_vbr_uint();
1306
1307 // Check to see if we have already read this global variable...
1308 Value *Val = getValue(TypeID, Slot, false);
1309 GlobalValue *GV;
1310 if (Val) {
1311 if (!(GV = dyn_cast<GlobalValue>(Val)))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001312 throw std::string("Value of ConstantPointerRef not in ValueTable!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001313 } else {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001314 throw std::string("Forward references are not allowed here.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001315 }
1316
1317 Constant* Result = ConstantPointerRef::get(GV);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001318 if (Handler) Handler->handleConstantPointer(PT, Slot, GV, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001319 return Result;
1320 }
1321
1322 default:
1323 PARSE_ERROR("Don't know how to deserialize constant value of type '"+
1324 Ty->getDescription());
1325 break;
1326 }
1327}
1328
Reid Spencer04cde2c2004-07-04 11:33:49 +00001329/// Resolve references for constants. This function resolves the forward
1330/// referenced constants in the ConstantFwdRefs map. It uses the
1331/// replaceAllUsesWith method of Value class to substitute the placeholder
1332/// instance with the actual instance.
Reid Spencer060d25d2004-06-29 23:29:38 +00001333void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
Chris Lattner29b789b2003-11-19 17:27:18 +00001334 ConstantRefsType::iterator I =
1335 ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
1336 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +00001337
Chris Lattner29b789b2003-11-19 17:27:18 +00001338 Value *PH = I->second; // Get the placeholder...
1339 PH->replaceAllUsesWith(NewV);
1340 delete PH; // Delete the old placeholder
1341 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +00001342}
1343
Reid Spencer04cde2c2004-07-04 11:33:49 +00001344/// Parse the constant strings section.
Reid Spencer060d25d2004-06-29 23:29:38 +00001345void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){
1346 for (; NumEntries; --NumEntries) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001347 unsigned Typ = 0;
1348 bool isTypeType = read_typeid(Typ);
1349 assert(!isTypeType && "Invalid type (type type) for string constant");
Reid Spencer060d25d2004-06-29 23:29:38 +00001350 const Type *Ty = getType(Typ);
1351 if (!isa<ArrayType>(Ty))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001352 throw std::string("String constant data invalid!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001353
1354 const ArrayType *ATy = cast<ArrayType>(Ty);
1355 if (ATy->getElementType() != Type::SByteTy &&
1356 ATy->getElementType() != Type::UByteTy)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001357 throw std::string("String constant data invalid!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001358
1359 // Read character data. The type tells us how long the string is.
1360 char Data[ATy->getNumElements()];
1361 read_data(Data, Data+ATy->getNumElements());
Chris Lattner52e20b02003-03-19 20:54:26 +00001362
Reid Spencer060d25d2004-06-29 23:29:38 +00001363 std::vector<Constant*> Elements(ATy->getNumElements());
1364 if (ATy->getElementType() == Type::SByteTy)
1365 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1366 Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
1367 else
1368 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1369 Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
Misha Brukman12c29d12003-09-22 23:38:23 +00001370
Reid Spencer060d25d2004-06-29 23:29:38 +00001371 // Create the constant, inserting it as needed.
1372 Constant *C = ConstantArray::get(ATy, Elements);
1373 unsigned Slot = insertValue(C, Typ, Tab);
1374 ResolveReferencesToConstant(C, Slot);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001375 if (Handler) Handler->handleConstantString(cast<ConstantArray>(C));
Reid Spencer060d25d2004-06-29 23:29:38 +00001376 }
Misha Brukman12c29d12003-09-22 23:38:23 +00001377}
1378
Reid Spencer04cde2c2004-07-04 11:33:49 +00001379/// Parse the constant pool.
Reid Spencer060d25d2004-06-29 23:29:38 +00001380void BytecodeReader::ParseConstantPool(ValueTable &Tab,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001381 TypeListTy &TypeTab,
1382 bool isFunction) {
1383 if (Handler) Handler->handleGlobalConstantsBegin();
1384
1385 /// In LLVM 1.3 Type does not derive from Value so the types
1386 /// do not occupy a plane. Consequently, we read the types
1387 /// first in the constant pool.
1388 if ( isFunction && !hasTypeDerivedFromValue ) {
1389 unsigned NumEntries = read_vbr_uint();
1390 ParseTypeConstants(TypeTab, NumEntries);
1391 }
1392
Reid Spencer060d25d2004-06-29 23:29:38 +00001393 while ( moreInBlock() ) {
1394 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001395 unsigned Typ = 0;
1396 bool isTypeType = read_typeid(Typ);
1397
1398 /// In LLVM 1.2 and before, Types were written to the
1399 /// bytecode file in the "Type Type" plane (#12).
1400 /// In 1.3 plane 12 is now the label plane. Handle this here.
1401 if ( isTypeType ) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001402 ParseTypeConstants(TypeTab, NumEntries);
1403 } else if (Typ == Type::VoidTyID) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001404 /// Use of Type::VoidTyID is a misnomer. It actually means
1405 /// that the following plane is constant strings
Reid Spencer060d25d2004-06-29 23:29:38 +00001406 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
1407 ParseStringConstants(NumEntries, Tab);
1408 } else {
1409 for (unsigned i = 0; i < NumEntries; ++i) {
1410 Constant *C = ParseConstantValue(Typ);
1411 assert(C && "ParseConstantValue returned NULL!");
1412 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner29b789b2003-11-19 17:27:18 +00001413
Reid Spencer060d25d2004-06-29 23:29:38 +00001414 // If we are reading a function constant table, make sure that we adjust
1415 // the slot number to be the real global constant number.
1416 //
1417 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
1418 ModuleValues[Typ])
1419 Slot += ModuleValues[Typ]->size();
1420 ResolveReferencesToConstant(C, Slot);
1421 }
1422 }
1423 }
1424 checkPastBlockEnd("Constant Pool");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001425 if (Handler) Handler->handleGlobalConstantsEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001426}
Chris Lattner00950542001-06-06 20:29:01 +00001427
Reid Spencer04cde2c2004-07-04 11:33:49 +00001428/// Parse the contents of a function. Note that this function can be
1429/// called lazily by materializeFunction
1430/// @see materializeFunction
Reid Spencer060d25d2004-06-29 23:29:38 +00001431void BytecodeReader::ParseFunctionBody(Function* F ) {
1432
1433 unsigned FuncSize = BlockEnd - At;
Chris Lattnere3869c82003-04-16 21:16:05 +00001434 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
1435
Reid Spencer060d25d2004-06-29 23:29:38 +00001436 unsigned LinkageType = read_vbr_uint();
Chris Lattnerc08912f2004-01-14 16:44:44 +00001437 switch (LinkageType) {
1438 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1439 case 1: Linkage = GlobalValue::WeakLinkage; break;
1440 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1441 case 3: Linkage = GlobalValue::InternalLinkage; break;
1442 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001443 default:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001444 throw std::string("Invalid linkage type for Function.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001445 Linkage = GlobalValue::InternalLinkage;
1446 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001447 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +00001448
Reid Spencer060d25d2004-06-29 23:29:38 +00001449 F->setLinkage( Linkage );
Reid Spencer04cde2c2004-07-04 11:33:49 +00001450 if (Handler) Handler->handleFunctionBegin(F,FuncSize);
Chris Lattner00950542001-06-06 20:29:01 +00001451
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001452 // Keep track of how many basic blocks we have read in...
1453 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +00001454 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001455
Reid Spencer060d25d2004-06-29 23:29:38 +00001456 BufPtr MyEnd = BlockEnd;
1457 while ( At < MyEnd ) {
Chris Lattner00950542001-06-06 20:29:01 +00001458 unsigned Type, Size;
Reid Spencer060d25d2004-06-29 23:29:38 +00001459 BufPtr OldAt = At;
1460 read_block(Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +00001461
1462 switch (Type) {
Chris Lattner29b789b2003-11-19 17:27:18 +00001463 case BytecodeFormat::ConstantPool:
Chris Lattner89e02532004-01-18 21:08:15 +00001464 if (!InsertedArguments) {
1465 // Insert arguments into the value table before we parse the first basic
1466 // block in the function, but after we potentially read in the
1467 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001468 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001469 InsertedArguments = true;
1470 }
1471
Reid Spencer04cde2c2004-07-04 11:33:49 +00001472 ParseConstantPool(FunctionValues, FunctionTypes, true);
Chris Lattner00950542001-06-06 20:29:01 +00001473 break;
1474
Chris Lattner89e02532004-01-18 21:08:15 +00001475 case BytecodeFormat::CompactionTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001476 ParseCompactionTable();
Chris Lattner89e02532004-01-18 21:08:15 +00001477 break;
1478
Chris Lattner00950542001-06-06 20:29:01 +00001479 case BytecodeFormat::BasicBlock: {
Chris Lattner89e02532004-01-18 21:08:15 +00001480 if (!InsertedArguments) {
1481 // Insert arguments into the value table before we parse the first basic
1482 // block in the function, but after we potentially read in the
1483 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001484 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001485 InsertedArguments = true;
1486 }
1487
Reid Spencer060d25d2004-06-29 23:29:38 +00001488 BasicBlock *BB = ParseBasicBlock(BlockNum++);
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001489 F->getBasicBlockList().push_back(BB);
Chris Lattner00950542001-06-06 20:29:01 +00001490 break;
1491 }
1492
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001493 case BytecodeFormat::InstructionList: {
Chris Lattner89e02532004-01-18 21:08:15 +00001494 // Insert arguments into the value table before we parse the instruction
1495 // list for the function, but after we potentially read in the compaction
1496 // table.
1497 if (!InsertedArguments) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001498 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001499 InsertedArguments = true;
1500 }
1501
Reid Spencer060d25d2004-06-29 23:29:38 +00001502 if (BlockNum)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001503 throw std::string("Already parsed basic blocks!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001504 BlockNum = ParseInstructionList(F);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001505 break;
1506 }
1507
Chris Lattner29b789b2003-11-19 17:27:18 +00001508 case BytecodeFormat::SymbolTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001509 ParseSymbolTable(F, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00001510 break;
1511
1512 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001513 At += Size;
1514 if (OldAt > At)
Reid Spencer04cde2c2004-07-04 11:33:49 +00001515 throw std::string("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +00001516 break;
1517 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001518 BlockEnd = MyEnd;
Chris Lattner1d670cc2001-09-07 16:37:43 +00001519
Misha Brukman12c29d12003-09-22 23:38:23 +00001520 // Malformed bc file if read past end of block.
Reid Spencer060d25d2004-06-29 23:29:38 +00001521 align32();
Chris Lattner00950542001-06-06 20:29:01 +00001522 }
1523
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001524 // Make sure there were no references to non-existant basic blocks.
1525 if (BlockNum != ParsedBasicBlocks.size())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001526 throw std::string("Illegal basic block operand reference");
Reid Spencer060d25d2004-06-29 23:29:38 +00001527
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001528 ParsedBasicBlocks.clear();
1529
Chris Lattner97330cf2003-10-09 23:10:14 +00001530 // Resolve forward references. Replace any uses of a forward reference value
1531 // with the real value.
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001532
Chris Lattner97330cf2003-10-09 23:10:14 +00001533 // replaceAllUsesWith is very inefficient for instructions which have a LARGE
1534 // number of operands. PHI nodes often have forward references, and can also
1535 // often have a very large number of operands.
Chris Lattner89e02532004-01-18 21:08:15 +00001536 //
1537 // FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code
1538 // should be simplified back to using it!
1539 //
Chris Lattner97330cf2003-10-09 23:10:14 +00001540 std::map<Value*, Value*> ForwardRefMapping;
1541 for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
1542 I = ForwardReferences.begin(), E = ForwardReferences.end();
1543 I != E; ++I)
1544 ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
1545 false);
1546
1547 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
1548 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1549 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1550 if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
1551 std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
1552 if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
1553 }
1554
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001555 while (!ForwardReferences.empty()) {
Chris Lattner35d2ca62003-10-09 22:39:30 +00001556 std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
1557 ForwardReferences.begin();
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001558 Value *PlaceHolder = I->second;
1559 ForwardReferences.erase(I);
Chris Lattner00950542001-06-06 20:29:01 +00001560
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001561 // Now that all the uses are gone, delete the placeholder...
1562 // If we couldn't find a def (error case), then leak a little
1563 // memory, because otherwise we can't remove all uses!
1564 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +00001565 }
Chris Lattner00950542001-06-06 20:29:01 +00001566
Misha Brukman12c29d12003-09-22 23:38:23 +00001567 // Clear out function-level types...
Reid Spencer060d25d2004-06-29 23:29:38 +00001568 FunctionTypes.clear();
1569 CompactionTypes.clear();
1570 CompactionValues.clear();
1571 freeTable(FunctionValues);
1572
Reid Spencer04cde2c2004-07-04 11:33:49 +00001573 if (Handler) Handler->handleFunctionEnd(F);
Chris Lattner00950542001-06-06 20:29:01 +00001574}
1575
Reid Spencer04cde2c2004-07-04 11:33:49 +00001576/// This function parses LLVM functions lazily. It obtains the type of the
1577/// function and records where the body of the function is in the bytecode
1578/// buffer. The caller can then use the ParseNextFunction and
1579/// ParseAllFunctionBodies to get handler events for the functions.
Reid Spencer060d25d2004-06-29 23:29:38 +00001580void BytecodeReader::ParseFunctionLazily() {
1581 if (FunctionSignatureList.empty())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001582 throw std::string("FunctionSignatureList empty!");
Chris Lattner89e02532004-01-18 21:08:15 +00001583
Reid Spencer060d25d2004-06-29 23:29:38 +00001584 Function *Func = FunctionSignatureList.back();
1585 FunctionSignatureList.pop_back();
Chris Lattner24102432004-01-18 22:35:34 +00001586
Reid Spencer060d25d2004-06-29 23:29:38 +00001587 // Save the information for future reading of the function
1588 LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd);
Chris Lattner89e02532004-01-18 21:08:15 +00001589
Reid Spencer060d25d2004-06-29 23:29:38 +00001590 // Pretend we've `parsed' this function
1591 At = BlockEnd;
1592}
Chris Lattner89e02532004-01-18 21:08:15 +00001593
Reid Spencer04cde2c2004-07-04 11:33:49 +00001594/// The ParserFunction method lazily parses one function. Use this method to
1595/// casue the parser to parse a specific function in the module. Note that
1596/// this will remove the function from what is to be included by
1597/// ParseAllFunctionBodies.
1598/// @see ParseAllFunctionBodies
1599/// @see ParseBytecode
Reid Spencer060d25d2004-06-29 23:29:38 +00001600void BytecodeReader::ParseFunction(Function* Func) {
1601 // Find {start, end} pointers and slot in the map. If not there, we're done.
1602 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
Chris Lattner89e02532004-01-18 21:08:15 +00001603
Reid Spencer060d25d2004-06-29 23:29:38 +00001604 // Make sure we found it
1605 if ( Fi == LazyFunctionLoadMap.end() ) {
1606 PARSE_ERROR("Unrecognized function of type " << Func->getType()->getDescription());
1607 return;
Chris Lattner89e02532004-01-18 21:08:15 +00001608 }
1609
Reid Spencer060d25d2004-06-29 23:29:38 +00001610 BlockStart = At = Fi->second.Buf;
1611 BlockEnd = Fi->second.EndBuf;
1612 assert(Fi->first == Func);
1613
1614 LazyFunctionLoadMap.erase(Fi);
1615
1616 this->ParseFunctionBody( Func );
Chris Lattner89e02532004-01-18 21:08:15 +00001617}
1618
Reid Spencer04cde2c2004-07-04 11:33:49 +00001619/// The ParseAllFunctionBodies method parses through all the previously
1620/// unparsed functions in the bytecode file. If you want to completely parse
1621/// a bytecode file, this method should be called after Parsebytecode because
1622/// Parsebytecode only records the locations in the bytecode file of where
1623/// the function definitions are located. This function uses that information
1624/// to materialize the functions.
1625/// @see ParseBytecode
Reid Spencer060d25d2004-06-29 23:29:38 +00001626void BytecodeReader::ParseAllFunctionBodies() {
1627 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
1628 LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
Chris Lattner89e02532004-01-18 21:08:15 +00001629
Reid Spencer060d25d2004-06-29 23:29:38 +00001630 while ( Fi != Fe ) {
1631 Function* Func = Fi->first;
1632 BlockStart = At = Fi->second.Buf;
1633 BlockEnd = Fi->second.EndBuf;
1634 this->ParseFunctionBody(Func);
1635 ++Fi;
1636 }
1637}
Chris Lattner89e02532004-01-18 21:08:15 +00001638
Reid Spencer04cde2c2004-07-04 11:33:49 +00001639/// Parse the global type list
Reid Spencer060d25d2004-06-29 23:29:38 +00001640void BytecodeReader::ParseGlobalTypes() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001641 // Read the number of types
1642 unsigned NumEntries = read_vbr_uint();
1643 ParseTypeConstants(ModuleTypes, NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001644}
1645
Reid Spencer04cde2c2004-07-04 11:33:49 +00001646/// Parse the Global info (types, global vars, constants)
Reid Spencer060d25d2004-06-29 23:29:38 +00001647void BytecodeReader::ParseModuleGlobalInfo() {
1648
Reid Spencer04cde2c2004-07-04 11:33:49 +00001649 if (Handler) Handler->handleModuleGlobalsBegin();
Chris Lattner00950542001-06-06 20:29:01 +00001650
Chris Lattner70cc3392001-09-10 07:58:01 +00001651 // Read global variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00001652 unsigned VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001653 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattner9dd87702004-04-03 23:43:42 +00001654 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
1655 // Linkage, bit4+ = slot#
1656 unsigned SlotNo = VarType >> 5;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001657 bool isTypeType = sanitizeTypeId(SlotNo);
1658 assert(!isTypeType && "Invalid type (type type) for global var!");
Chris Lattner9dd87702004-04-03 23:43:42 +00001659 unsigned LinkageID = (VarType >> 2) & 7;
Reid Spencer060d25d2004-06-29 23:29:38 +00001660 bool isConstant = VarType & 1;
1661 bool hasInitializer = VarType & 2;
Chris Lattnere3869c82003-04-16 21:16:05 +00001662 GlobalValue::LinkageTypes Linkage;
1663
Chris Lattnerc08912f2004-01-14 16:44:44 +00001664 switch (LinkageID) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00001665 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1666 case 1: Linkage = GlobalValue::WeakLinkage; break;
1667 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1668 case 3: Linkage = GlobalValue::InternalLinkage; break;
1669 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001670 default:
1671 PARSE_ERROR("Unknown linkage type: " << LinkageID);
1672 Linkage = GlobalValue::InternalLinkage;
1673 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001674 }
1675
1676 const Type *Ty = getType(SlotNo);
Reid Spencer060d25d2004-06-29 23:29:38 +00001677 if ( !Ty ) {
1678 PARSE_ERROR("Global has no type! SlotNo=" << SlotNo);
1679 }
1680
1681 if ( !isa<PointerType>(Ty)) {
1682 PARSE_ERROR("Global not a pointer type! Ty= " << Ty->getDescription());
1683 }
Chris Lattner70cc3392001-09-10 07:58:01 +00001684
Chris Lattner52e20b02003-03-19 20:54:26 +00001685 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +00001686
Chris Lattner70cc3392001-09-10 07:58:01 +00001687 // Create the global variable...
Reid Spencer060d25d2004-06-29 23:29:38 +00001688 GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +00001689 0, "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +00001690 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +00001691
Reid Spencer060d25d2004-06-29 23:29:38 +00001692 unsigned initSlot = 0;
1693 if (hasInitializer) {
1694 initSlot = read_vbr_uint();
1695 GlobalInits.push_back(std::make_pair(GV, initSlot));
1696 }
1697
1698 // Notify handler about the global value.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001699 if (Handler) Handler->handleGlobalVariable( ElTy, isConstant, Linkage, SlotNo, initSlot );
Reid Spencer060d25d2004-06-29 23:29:38 +00001700
1701 // Get next item
1702 VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001703 }
1704
Chris Lattner52e20b02003-03-19 20:54:26 +00001705 // Read the function objects for all of the functions that are coming
Reid Spencer04cde2c2004-07-04 11:33:49 +00001706 unsigned FnSignature = 0;
1707 bool isTypeType = read_typeid(FnSignature);
1708 assert(!isTypeType && "Invalid function type (type type) found");
Chris Lattner74734132002-08-17 22:01:27 +00001709 while (FnSignature != Type::VoidTyID) { // List is terminated by Void
1710 const Type *Ty = getType(FnSignature);
Chris Lattner927b1852003-10-09 20:22:47 +00001711 if (!isa<PointerType>(Ty) ||
Reid Spencer060d25d2004-06-29 23:29:38 +00001712 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
1713 PARSE_ERROR( "Function not a pointer to function type! Ty = " +
Misha Brukman12c29d12003-09-22 23:38:23 +00001714 Ty->getDescription());
Reid Spencer060d25d2004-06-29 23:29:38 +00001715 // FIXME: what should Ty be if handler continues?
1716 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +00001717
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001718 // We create functions by passing the underlying FunctionType to create...
Reid Spencer060d25d2004-06-29 23:29:38 +00001719 const FunctionType* FTy =
1720 cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +00001721
Reid Spencer060d25d2004-06-29 23:29:38 +00001722 // Insert the place hodler
1723 Function* Func = new Function(FTy, GlobalValue::InternalLinkage,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001724 "", TheModule);
Chris Lattner29b789b2003-11-19 17:27:18 +00001725 insertValue(Func, FnSignature, ModuleValues);
Chris Lattner00950542001-06-06 20:29:01 +00001726
Reid Spencer060d25d2004-06-29 23:29:38 +00001727 // Save this for later so we know type of lazily instantiated functions
Chris Lattner29b789b2003-11-19 17:27:18 +00001728 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +00001729
Reid Spencer04cde2c2004-07-04 11:33:49 +00001730 if (Handler) Handler->handleFunctionDeclaration(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00001731
1732 // Get Next function signature
Reid Spencer04cde2c2004-07-04 11:33:49 +00001733 isTypeType = read_typeid(FnSignature);
1734 assert(!isTypeType && "Invalid function type (type type) found");
Chris Lattner00950542001-06-06 20:29:01 +00001735 }
1736
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001737 if (hasInconsistentModuleGlobalInfo)
Reid Spencer060d25d2004-06-29 23:29:38 +00001738 align32();
Chris Lattner74734132002-08-17 22:01:27 +00001739
1740 // Now that the function signature list is set up, reverse it so that we can
1741 // remove elements efficiently from the back of the vector.
1742 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +00001743
1744 // This is for future proofing... in the future extra fields may be added that
1745 // we don't understand, so we transparently ignore them.
1746 //
Reid Spencer060d25d2004-06-29 23:29:38 +00001747 At = BlockEnd;
1748
Reid Spencer04cde2c2004-07-04 11:33:49 +00001749 if (Handler) Handler->handleModuleGlobalsEnd();
Chris Lattner00950542001-06-06 20:29:01 +00001750}
1751
Reid Spencer04cde2c2004-07-04 11:33:49 +00001752/// Parse the version information and decode it by setting flags on the
1753/// Reader that enable backward compatibility of the reader.
Reid Spencer060d25d2004-06-29 23:29:38 +00001754void BytecodeReader::ParseVersionInfo() {
1755 unsigned Version = read_vbr_uint();
Chris Lattner036b8aa2003-03-06 17:55:45 +00001756
1757 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001758 Module::Endianness Endianness;
1759 Module::PointerSize PointerSize;
1760 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
1761 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
1762
1763 bool hasNoEndianness = Version & 4;
1764 bool hasNoPointerSize = Version & 8;
1765
1766 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +00001767
1768 // Default values for the current bytecode version
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001769 hasInconsistentModuleGlobalInfo = false;
Chris Lattner80b97342004-01-17 23:25:43 +00001770 hasExplicitPrimitiveZeros = false;
Chris Lattner5fa428f2004-04-05 01:27:26 +00001771 hasRestrictedGEPTypes = false;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001772 hasTypeDerivedFromValue = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +00001773
1774 switch (RevisionNum) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00001775 case 0: // LLVM 1.0, 1.1 release version
Chris Lattner9e893e82004-01-14 23:35:21 +00001776 // Base LLVM 1.0 bytecode format.
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001777 hasInconsistentModuleGlobalInfo = true;
Chris Lattner80b97342004-01-17 23:25:43 +00001778 hasExplicitPrimitiveZeros = true;
Reid Spencer04cde2c2004-07-04 11:33:49 +00001779
Chris Lattner80b97342004-01-17 23:25:43 +00001780 // FALL THROUGH
Chris Lattnerc08912f2004-01-14 16:44:44 +00001781 case 1: // LLVM 1.2 release version
Chris Lattner9e893e82004-01-14 23:35:21 +00001782 // LLVM 1.2 added explicit support for emitting strings efficiently.
Chris Lattner44d0eeb2004-01-15 17:55:01 +00001783
1784 // Also, it fixed the problem where the size of the ModuleGlobalInfo block
1785 // included the size for the alignment at the end, where the rest of the
1786 // blocks did not.
Chris Lattner5fa428f2004-04-05 01:27:26 +00001787
1788 // LLVM 1.2 and before required that GEP indices be ubyte constants for
1789 // structures and longs for sequential types.
1790 hasRestrictedGEPTypes = true;
1791
Reid Spencer04cde2c2004-07-04 11:33:49 +00001792 // LLVM 1.2 and before had the Type class derive from Value class. This
1793 // changed in release 1.3 and consequently LLVM 1.3 bytecode files are
1794 // written differently because Types can no longer be part of the
1795 // type planes for Values.
1796 hasTypeDerivedFromValue = true;
1797
Chris Lattner5fa428f2004-04-05 01:27:26 +00001798 // FALL THROUGH
1799 case 2: // LLVM 1.3 release version
Chris Lattnerc08912f2004-01-14 16:44:44 +00001800 break;
1801
Chris Lattner036b8aa2003-03-06 17:55:45 +00001802 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001803 PARSE_ERROR("Unknown bytecode version number: " << RevisionNum);
Chris Lattner036b8aa2003-03-06 17:55:45 +00001804 }
1805
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001806 if (hasNoEndianness) Endianness = Module::AnyEndianness;
1807 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +00001808
Reid Spencer04cde2c2004-07-04 11:33:49 +00001809 if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize );
Chris Lattner036b8aa2003-03-06 17:55:45 +00001810}
1811
Reid Spencer04cde2c2004-07-04 11:33:49 +00001812/// Parse a whole module.
Reid Spencer060d25d2004-06-29 23:29:38 +00001813void BytecodeReader::ParseModule() {
Chris Lattner00950542001-06-06 20:29:01 +00001814 unsigned Type, Size;
Chris Lattner00950542001-06-06 20:29:01 +00001815
Reid Spencer060d25d2004-06-29 23:29:38 +00001816 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +00001817
1818 // Read into instance variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00001819 ParseVersionInfo();
1820 align32(); /// FIXME: Is this redundant? VI is first and 4 bytes!
Chris Lattner00950542001-06-06 20:29:01 +00001821
Reid Spencer060d25d2004-06-29 23:29:38 +00001822 bool SeenModuleGlobalInfo = false;
1823 bool SeenGlobalTypePlane = false;
1824 BufPtr MyEnd = BlockEnd;
1825 while (At < MyEnd) {
1826 BufPtr OldAt = At;
1827 read_block(Type, Size);
1828
Chris Lattner00950542001-06-06 20:29:01 +00001829 switch (Type) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001830
Chris Lattner52e20b02003-03-19 20:54:26 +00001831 case BytecodeFormat::GlobalTypePlane:
Reid Spencer060d25d2004-06-29 23:29:38 +00001832 if ( SeenGlobalTypePlane )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001833 throw std::string("Two GlobalTypePlane Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001834
1835 ParseGlobalTypes();
1836 SeenGlobalTypePlane = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00001837 break;
1838
Reid Spencer060d25d2004-06-29 23:29:38 +00001839 case BytecodeFormat::ModuleGlobalInfo:
1840 if ( SeenModuleGlobalInfo )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001841 throw std::string("Two ModuleGlobalInfo Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001842 ParseModuleGlobalInfo();
1843 SeenModuleGlobalInfo = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00001844 break;
1845
Chris Lattner1d670cc2001-09-07 16:37:43 +00001846 case BytecodeFormat::ConstantPool:
Reid Spencer04cde2c2004-07-04 11:33:49 +00001847 ParseConstantPool(ModuleValues, ModuleTypes,false);
Chris Lattner00950542001-06-06 20:29:01 +00001848 break;
1849
Reid Spencer060d25d2004-06-29 23:29:38 +00001850 case BytecodeFormat::Function:
1851 ParseFunctionLazily();
Chris Lattner00950542001-06-06 20:29:01 +00001852 break;
Chris Lattner00950542001-06-06 20:29:01 +00001853
1854 case BytecodeFormat::SymbolTable:
Reid Spencer060d25d2004-06-29 23:29:38 +00001855 ParseSymbolTable(0, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00001856 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001857
Chris Lattner00950542001-06-06 20:29:01 +00001858 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001859 At += Size;
1860 if (OldAt > At) {
1861 PARSE_ERROR("Unexpected Block of Type" << Type << "encountered!" );
1862 }
Chris Lattner00950542001-06-06 20:29:01 +00001863 break;
1864 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001865 BlockEnd = MyEnd;
1866 align32();
Chris Lattner00950542001-06-06 20:29:01 +00001867 }
1868
Chris Lattner52e20b02003-03-19 20:54:26 +00001869 // After the module constant pool has been read, we can safely initialize
1870 // global variables...
1871 while (!GlobalInits.empty()) {
1872 GlobalVariable *GV = GlobalInits.back().first;
1873 unsigned Slot = GlobalInits.back().second;
1874 GlobalInits.pop_back();
1875
1876 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +00001877 // FIXME: Preserve this type ID!
Reid Spencer060d25d2004-06-29 23:29:38 +00001878
1879 const llvm::PointerType* GVType = GV->getType();
1880 unsigned TypeSlot = getTypeSlot(GVType->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +00001881 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman12c29d12003-09-22 23:38:23 +00001882 if (GV->hasInitializer())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001883 throw std::string("Global *already* has an initializer?!");
1884 if (Handler) Handler->handleGlobalInitializer(GV,CV);
Chris Lattner93361992004-01-15 18:45:25 +00001885 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +00001886 } else
Reid Spencer04cde2c2004-07-04 11:33:49 +00001887 throw std::string("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +00001888 }
1889
Reid Spencer060d25d2004-06-29 23:29:38 +00001890 /// Make sure we pulled them all out. If we didn't then there's a declaration
1891 /// but a missing body. That's not allowed.
Misha Brukman12c29d12003-09-22 23:38:23 +00001892 if (!FunctionSignatureList.empty())
Reid Spencer04cde2c2004-07-04 11:33:49 +00001893 throw std::string(
Reid Spencer060d25d2004-06-29 23:29:38 +00001894 "Function declared, but bytecode stream ended before definition");
Chris Lattner00950542001-06-06 20:29:01 +00001895}
1896
Reid Spencer04cde2c2004-07-04 11:33:49 +00001897/// This function completely parses a bytecode buffer given by the \p Buf
1898/// and \p Length parameters.
Reid Spencer060d25d2004-06-29 23:29:38 +00001899void BytecodeReader::ParseBytecode(
1900 BufPtr Buf, unsigned Length,
1901 const std::string &ModuleID) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +00001902
Reid Spencer060d25d2004-06-29 23:29:38 +00001903 try {
1904 At = MemStart = BlockStart = Buf;
1905 MemEnd = BlockEnd = Buf + Length;
Misha Brukmane0dd0d42003-09-23 16:15:29 +00001906
Reid Spencer060d25d2004-06-29 23:29:38 +00001907 // Create the module
1908 TheModule = new Module(ModuleID);
Chris Lattner00950542001-06-06 20:29:01 +00001909
Reid Spencer04cde2c2004-07-04 11:33:49 +00001910 if (Handler) Handler->handleStart(TheModule, Length);
Reid Spencer060d25d2004-06-29 23:29:38 +00001911
1912 // Read and check signature...
1913 unsigned Sig = read_uint();
1914 if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) {
1915 PARSE_ERROR("Invalid bytecode signature: " << Sig);
1916 }
1917
1918
1919 // Tell the handler we're starting a module
Reid Spencer04cde2c2004-07-04 11:33:49 +00001920 if (Handler) Handler->handleModuleBegin(ModuleID);
Reid Spencer060d25d2004-06-29 23:29:38 +00001921
1922 // Get the module block and size and verify
1923 unsigned Type, Size;
1924 read_block(Type, Size);
1925 if ( Type != BytecodeFormat::Module ) {
1926 PARSE_ERROR("Expected Module Block! At: " << unsigned(intptr_t(At))
Reid Spencer04cde2c2004-07-04 11:33:49 +00001927 << ", Type:" << Type << ", Size:" << Size);
Reid Spencer060d25d2004-06-29 23:29:38 +00001928 }
1929 if ( At + Size != MemEnd ) {
1930 PARSE_ERROR("Invalid Top Level Block Length! At: "
Reid Spencer04cde2c2004-07-04 11:33:49 +00001931 << unsigned(intptr_t(At)) << ", Type:" << Type << ", Size:" << Size);
Reid Spencer060d25d2004-06-29 23:29:38 +00001932 }
1933
1934 // Parse the module contents
1935 this->ParseModule();
1936
1937 // Tell the handler we're done
Reid Spencer04cde2c2004-07-04 11:33:49 +00001938 if (Handler) Handler->handleModuleEnd(ModuleID);
Reid Spencer060d25d2004-06-29 23:29:38 +00001939
1940 // Check for missing functions
1941 if ( hasFunctions() )
Reid Spencer04cde2c2004-07-04 11:33:49 +00001942 throw std::string("Function expected, but bytecode stream ended!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001943
1944 // Tell the handler we're
Reid Spencer04cde2c2004-07-04 11:33:49 +00001945 if (Handler) Handler->handleFinish();
Reid Spencer060d25d2004-06-29 23:29:38 +00001946
1947 } catch (std::string& errstr ) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001948 if (Handler) Handler->handleError(errstr);
Reid Spencer060d25d2004-06-29 23:29:38 +00001949 freeState();
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001950 delete TheModule;
1951 TheModule = 0;
Chris Lattnerb0b7c0d2003-09-26 14:44:52 +00001952 throw;
Reid Spencer060d25d2004-06-29 23:29:38 +00001953 } catch (...) {
1954 std::string msg("Unknown Exception Occurred");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001955 if (Handler) Handler->handleError(msg);
Reid Spencer060d25d2004-06-29 23:29:38 +00001956 freeState();
1957 delete TheModule;
1958 TheModule = 0;
1959 throw msg;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001960 }
Chris Lattner00950542001-06-06 20:29:01 +00001961}
Reid Spencer060d25d2004-06-29 23:29:38 +00001962
1963//===----------------------------------------------------------------------===//
1964//=== Default Implementations of Handler Methods
1965//===----------------------------------------------------------------------===//
1966
1967BytecodeHandler::~BytecodeHandler() {}
Reid Spencer060d25d2004-06-29 23:29:38 +00001968
1969// vim: sw=2