blob: d294e9e025e4c3e0b82926191a7e8081ea194ed5 [file] [log] [blame]
Chris Lattnerd6b65252001-10-24 01:15:12 +00001//===- Reader.cpp - Code to read bytecode files ---------------------------===//
Misha Brukman8a96c532005-04-21 21:44:41 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman8a96c532005-04-21 21:44:41 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/Bytecode/Reader.h
11//
Misha Brukman8a96c532005-04-21 21:44:41 +000012// Note that this library should be as fast as possible, reentrant, and
Chris Lattner00950542001-06-06 20:29:01 +000013// 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"
Chris Lattnerdee199f2005-05-06 22:34:01 +000022#include "llvm/CallingConv.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000023#include "llvm/Constants.h"
Chris Lattner3bc5a602006-01-25 23:08:15 +000024#include "llvm/InlineAsm.h"
Reid Spencer04cde2c2004-07-04 11:33:49 +000025#include "llvm/Instructions.h"
26#include "llvm/SymbolTable.h"
Chris Lattner00950542001-06-06 20:29:01 +000027#include "llvm/Bytecode/Format.h"
Chris Lattnerdee199f2005-05-06 22:34:01 +000028#include "llvm/Config/alloca.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer17f52c52004-11-06 23:17:23 +000030#include "llvm/Support/Compressor.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000031#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/StringExtras.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000033#include <sstream>
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000034#include <algorithm>
Chris Lattner29b789b2003-11-19 17:27:18 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Reid Spencer46b002c2004-07-11 17:28:43 +000037namespace {
Chris Lattnercad28bd2005-01-29 00:36:19 +000038 /// @brief A class for maintaining the slot number definition
39 /// as a placeholder for the actual definition for forward constants defs.
40 class ConstantPlaceHolder : public ConstantExpr {
41 ConstantPlaceHolder(); // DO NOT IMPLEMENT
42 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
43 public:
Chris Lattner61323322005-01-31 01:11:13 +000044 Use Op;
Misha Brukman8a96c532005-04-21 21:44:41 +000045 ConstantPlaceHolder(const Type *Ty)
Chris Lattner61323322005-01-31 01:11:13 +000046 : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1),
47 Op(UndefValue::get(Type::IntTy), this) {
48 }
Chris Lattnercad28bd2005-01-29 00:36:19 +000049 };
Reid Spencer46b002c2004-07-11 17:28:43 +000050}
Reid Spencer060d25d2004-06-29 23:29:38 +000051
Reid Spencer24399722004-07-09 22:21:33 +000052// Provide some details on error
Reid Spencer233fe722006-08-22 16:09:19 +000053inline void BytecodeReader::error(const std::string& err) {
54 ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos="
55 + itostr(At-MemStart) + ")";
56 longjmp(context,1);
Reid Spencer24399722004-07-09 22:21:33 +000057}
58
Reid Spencer060d25d2004-06-29 23:29:38 +000059//===----------------------------------------------------------------------===//
60// Bytecode Reading Methods
61//===----------------------------------------------------------------------===//
62
Reid Spencer04cde2c2004-07-04 11:33:49 +000063/// Determine if the current block being read contains any more data.
Reid Spencer060d25d2004-06-29 23:29:38 +000064inline bool BytecodeReader::moreInBlock() {
65 return At < BlockEnd;
Chris Lattner00950542001-06-06 20:29:01 +000066}
67
Reid Spencer04cde2c2004-07-04 11:33:49 +000068/// Throw an error if we've read past the end of the current block
Reid Spencer060d25d2004-06-29 23:29:38 +000069inline void BytecodeReader::checkPastBlockEnd(const char * block_name) {
Reid Spencer46b002c2004-07-11 17:28:43 +000070 if (At > BlockEnd)
Chris Lattnera79e7cc2004-10-16 18:18:16 +000071 error(std::string("Attempt to read past the end of ") + block_name +
72 " block.");
Reid Spencer060d25d2004-06-29 23:29:38 +000073}
Chris Lattner36392bc2003-10-08 21:18:57 +000074
Reid Spencer04cde2c2004-07-04 11:33:49 +000075/// Read a whole unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000076inline unsigned BytecodeReader::read_uint() {
Misha Brukman8a96c532005-04-21 21:44:41 +000077 if (At+4 > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +000078 error("Ran out of data reading uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +000079 At += 4;
80 return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24);
81}
82
Reid Spencer04cde2c2004-07-04 11:33:49 +000083/// Read a variable-bit-rate encoded unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000084inline unsigned BytecodeReader::read_vbr_uint() {
85 unsigned Shift = 0;
86 unsigned Result = 0;
87 BufPtr Save = At;
Misha Brukman8a96c532005-04-21 21:44:41 +000088
Reid Spencer060d25d2004-06-29 23:29:38 +000089 do {
Misha Brukman8a96c532005-04-21 21:44:41 +000090 if (At == BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +000091 error("Ran out of data reading vbr_uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +000092 Result |= (unsigned)((*At++) & 0x7F) << Shift;
93 Shift += 7;
94 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +000095 if (Handler) Handler->handleVBR32(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +000096 return Result;
97}
98
Reid Spencer04cde2c2004-07-04 11:33:49 +000099/// Read a variable-bit-rate encoded unsigned 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000100inline uint64_t BytecodeReader::read_vbr_uint64() {
101 unsigned Shift = 0;
102 uint64_t Result = 0;
103 BufPtr Save = At;
Misha Brukman8a96c532005-04-21 21:44:41 +0000104
Reid Spencer060d25d2004-06-29 23:29:38 +0000105 do {
Misha Brukman8a96c532005-04-21 21:44:41 +0000106 if (At == BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000107 error("Ran out of data reading vbr_uint64!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000108 Result |= (uint64_t)((*At++) & 0x7F) << Shift;
109 Shift += 7;
110 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000111 if (Handler) Handler->handleVBR64(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +0000112 return Result;
113}
114
Reid Spencer04cde2c2004-07-04 11:33:49 +0000115/// Read a variable-bit-rate encoded signed 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000116inline int64_t BytecodeReader::read_vbr_int64() {
117 uint64_t R = read_vbr_uint64();
118 if (R & 1) {
119 if (R != 1)
120 return -(int64_t)(R >> 1);
121 else // There is no such thing as -0 with integers. "-0" really means
122 // 0x8000000000000000.
123 return 1LL << 63;
124 } else
125 return (int64_t)(R >> 1);
126}
127
Reid Spencer04cde2c2004-07-04 11:33:49 +0000128/// Read a pascal-style string (length followed by text)
Reid Spencer060d25d2004-06-29 23:29:38 +0000129inline std::string BytecodeReader::read_str() {
130 unsigned Size = read_vbr_uint();
131 const unsigned char *OldAt = At;
132 At += Size;
133 if (At > BlockEnd) // Size invalid?
Reid Spencer24399722004-07-09 22:21:33 +0000134 error("Ran out of data reading a string!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000135 return std::string((char*)OldAt, Size);
136}
137
Reid Spencer04cde2c2004-07-04 11:33:49 +0000138/// Read an arbitrary block of data
Reid Spencer060d25d2004-06-29 23:29:38 +0000139inline void BytecodeReader::read_data(void *Ptr, void *End) {
140 unsigned char *Start = (unsigned char *)Ptr;
141 unsigned Amount = (unsigned char *)End - Start;
Misha Brukman8a96c532005-04-21 21:44:41 +0000142 if (At+Amount > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000143 error("Ran out of data!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000144 std::copy(At, At+Amount, Start);
145 At += Amount;
146}
147
Reid Spencer46b002c2004-07-11 17:28:43 +0000148/// Read a float value in little-endian order
149inline void BytecodeReader::read_float(float& FloatVal) {
Reid Spencerada16182004-07-25 21:36:26 +0000150 /// FIXME: This isn't optimal, it has size problems on some platforms
151 /// where FP is not IEEE.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000152 FloatVal = BitsToFloat(At[0] | (At[1] << 8) | (At[2] << 16) | (At[3] << 24));
Reid Spencerada16182004-07-25 21:36:26 +0000153 At+=sizeof(uint32_t);
Reid Spencer46b002c2004-07-11 17:28:43 +0000154}
155
156/// Read a double value in little-endian order
157inline void BytecodeReader::read_double(double& DoubleVal) {
Reid Spencerada16182004-07-25 21:36:26 +0000158 /// FIXME: This isn't optimal, it has size problems on some platforms
159 /// where FP is not IEEE.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000160 DoubleVal = BitsToDouble((uint64_t(At[0]) << 0) | (uint64_t(At[1]) << 8) |
161 (uint64_t(At[2]) << 16) | (uint64_t(At[3]) << 24) |
162 (uint64_t(At[4]) << 32) | (uint64_t(At[5]) << 40) |
163 (uint64_t(At[6]) << 48) | (uint64_t(At[7]) << 56));
Reid Spencerada16182004-07-25 21:36:26 +0000164 At+=sizeof(uint64_t);
Reid Spencer46b002c2004-07-11 17:28:43 +0000165}
166
Reid Spencer04cde2c2004-07-04 11:33:49 +0000167/// Read a block header and obtain its type and size
Reid Spencer060d25d2004-06-29 23:29:38 +0000168inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) {
Reid Spencerd798a512006-11-14 04:47:22 +0000169 Size = read_uint(); // Read the header
170 Type = Size & 0x1F; // mask low order five bits to get type
171 Size >>= 5; // high order 27 bits is the size
Reid Spencer060d25d2004-06-29 23:29:38 +0000172 BlockStart = At;
Reid Spencer46b002c2004-07-11 17:28:43 +0000173 if (At + Size > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000174 error("Attempt to size a block past end of memory");
Reid Spencer060d25d2004-06-29 23:29:38 +0000175 BlockEnd = At + Size;
Reid Spencer46b002c2004-07-11 17:28:43 +0000176 if (Handler) Handler->handleBlock(Type, BlockStart, Size);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000177}
178
Reid Spencer060d25d2004-06-29 23:29:38 +0000179//===----------------------------------------------------------------------===//
180// IR Lookup Methods
181//===----------------------------------------------------------------------===//
182
Reid Spencer04cde2c2004-07-04 11:33:49 +0000183/// Determine if a type id has an implicit null value
Reid Spencer46b002c2004-07-11 17:28:43 +0000184inline bool BytecodeReader::hasImplicitNull(unsigned TyID) {
Reid Spencerd798a512006-11-14 04:47:22 +0000185 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Reid Spencer060d25d2004-06-29 23:29:38 +0000186}
187
Reid Spencer04cde2c2004-07-04 11:33:49 +0000188/// Obtain a type given a typeid and account for things like compaction tables,
189/// function level vs module level, and the offsetting for the primitive types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000190const Type *BytecodeReader::getType(unsigned ID) {
Chris Lattner89e02532004-01-18 21:08:15 +0000191 if (ID < Type::FirstDerivedTyID)
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000192 if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
Chris Lattner927b1852003-10-09 20:22:47 +0000193 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +0000194
195 // Otherwise, derived types need offset...
Chris Lattner89e02532004-01-18 21:08:15 +0000196 ID -= Type::FirstDerivedTyID;
197
Reid Spencer060d25d2004-06-29 23:29:38 +0000198 if (!CompactionTypes.empty()) {
199 if (ID >= CompactionTypes.size())
Reid Spencer24399722004-07-09 22:21:33 +0000200 error("Type ID out of range for compaction table!");
Chris Lattner45b5dd22004-08-03 23:41:28 +0000201 return CompactionTypes[ID].first;
Chris Lattner89e02532004-01-18 21:08:15 +0000202 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000203
204 // Is it a module-level type?
Reid Spencer46b002c2004-07-11 17:28:43 +0000205 if (ID < ModuleTypes.size())
206 return ModuleTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000207
Reid Spencer46b002c2004-07-11 17:28:43 +0000208 // Nope, is it a function-level type?
209 ID -= ModuleTypes.size();
210 if (ID < FunctionTypes.size())
211 return FunctionTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000212
Reid Spencer46b002c2004-07-11 17:28:43 +0000213 error("Illegal type reference!");
214 return Type::VoidTy;
Chris Lattner00950542001-06-06 20:29:01 +0000215}
216
Reid Spencer3795ad12006-12-03 05:47:10 +0000217/// This method just saves some coding. It uses read_vbr_uint to read in a
218/// type id, errors that its not the type type, and then calls getType to
219/// return the type value.
Reid Spencerd798a512006-11-14 04:47:22 +0000220inline const Type* BytecodeReader::readType() {
221 return getType(read_vbr_uint());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000222}
223
224/// Get the slot number associated with a type accounting for primitive
225/// types, compaction tables, and function level vs module level.
Reid Spencer060d25d2004-06-29 23:29:38 +0000226unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
227 if (Ty->isPrimitiveType())
228 return Ty->getTypeID();
229
230 // Scan the compaction table for the type if needed.
231 if (!CompactionTypes.empty()) {
Chris Lattner45b5dd22004-08-03 23:41:28 +0000232 for (unsigned i = 0, e = CompactionTypes.size(); i != e; ++i)
233 if (CompactionTypes[i].first == Ty)
Misha Brukman8a96c532005-04-21 21:44:41 +0000234 return Type::FirstDerivedTyID + i;
Reid Spencer060d25d2004-06-29 23:29:38 +0000235
Chris Lattner45b5dd22004-08-03 23:41:28 +0000236 error("Couldn't find type specified in compaction table!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000237 }
238
239 // Check the function level types first...
Chris Lattnera79e7cc2004-10-16 18:18:16 +0000240 TypeListTy::iterator I = std::find(FunctionTypes.begin(),
241 FunctionTypes.end(), Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +0000242
243 if (I != FunctionTypes.end())
Misha Brukman8a96c532005-04-21 21:44:41 +0000244 return Type::FirstDerivedTyID + ModuleTypes.size() +
Reid Spencer46b002c2004-07-11 17:28:43 +0000245 (&*I - &FunctionTypes[0]);
Reid Spencer060d25d2004-06-29 23:29:38 +0000246
Chris Lattnereebac5f2005-10-03 21:26:53 +0000247 // If we don't have our cache yet, build it now.
248 if (ModuleTypeIDCache.empty()) {
249 unsigned N = 0;
250 ModuleTypeIDCache.reserve(ModuleTypes.size());
251 for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
252 I != E; ++I, ++N)
253 ModuleTypeIDCache.push_back(std::make_pair(*I, N));
254
255 std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
256 }
257
258 // Binary search the cache for the entry.
259 std::vector<std::pair<const Type*, unsigned> >::iterator IT =
260 std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
261 std::make_pair(Ty, 0U));
262 if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000263 error("Didn't find type in ModuleTypes.");
Chris Lattnereebac5f2005-10-03 21:26:53 +0000264
265 return Type::FirstDerivedTyID + IT->second;
Chris Lattner80b97342004-01-17 23:25:43 +0000266}
267
Reid Spencer04cde2c2004-07-04 11:33:49 +0000268/// This is just like getType, but when a compaction table is in use, it is
269/// ignored. It also ignores function level types.
270/// @see getType
Reid Spencer060d25d2004-06-29 23:29:38 +0000271const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
272 if (Slot < Type::FirstDerivedTyID) {
273 const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
Reid Spencer46b002c2004-07-11 17:28:43 +0000274 if (!Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000275 error("Not a primitive type ID?");
Reid Spencer060d25d2004-06-29 23:29:38 +0000276 return Ty;
277 }
278 Slot -= Type::FirstDerivedTyID;
279 if (Slot >= ModuleTypes.size())
Reid Spencer24399722004-07-09 22:21:33 +0000280 error("Illegal compaction table type reference!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000281 return ModuleTypes[Slot];
Chris Lattner52e20b02003-03-19 20:54:26 +0000282}
283
Reid Spencer04cde2c2004-07-04 11:33:49 +0000284/// This is just like getTypeSlot, but when a compaction table is in use, it
285/// is ignored. It also ignores function level types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000286unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
287 if (Ty->isPrimitiveType())
288 return Ty->getTypeID();
Chris Lattnereebac5f2005-10-03 21:26:53 +0000289
290 // If we don't have our cache yet, build it now.
291 if (ModuleTypeIDCache.empty()) {
292 unsigned N = 0;
293 ModuleTypeIDCache.reserve(ModuleTypes.size());
294 for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
295 I != E; ++I, ++N)
296 ModuleTypeIDCache.push_back(std::make_pair(*I, N));
297
298 std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
299 }
300
301 // Binary search the cache for the entry.
302 std::vector<std::pair<const Type*, unsigned> >::iterator IT =
303 std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
304 std::make_pair(Ty, 0U));
305 if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000306 error("Didn't find type in ModuleTypes.");
Chris Lattnereebac5f2005-10-03 21:26:53 +0000307
308 return Type::FirstDerivedTyID + IT->second;
Reid Spencer060d25d2004-06-29 23:29:38 +0000309}
310
Misha Brukman8a96c532005-04-21 21:44:41 +0000311/// Retrieve a value of a given type and slot number, possibly creating
312/// it if it doesn't already exist.
Reid Spencer060d25d2004-06-29 23:29:38 +0000313Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000314 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000315 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000316
Chris Lattner89e02532004-01-18 21:08:15 +0000317 // If there is a compaction table active, it defines the low-level numbers.
318 // If not, the module values define the low-level numbers.
Reid Spencer060d25d2004-06-29 23:29:38 +0000319 if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
320 if (Num < CompactionValues[type].size())
321 return CompactionValues[type][Num];
322 Num -= CompactionValues[type].size();
Chris Lattner89e02532004-01-18 21:08:15 +0000323 } else {
Reid Spencer060d25d2004-06-29 23:29:38 +0000324 // By default, the global type id is the type id passed in
Chris Lattner52f86d62004-01-20 00:54:06 +0000325 unsigned GlobalTyID = type;
Reid Spencer060d25d2004-06-29 23:29:38 +0000326
Chris Lattner45b5dd22004-08-03 23:41:28 +0000327 // If the type plane was compactified, figure out the global type ID by
328 // adding the derived type ids and the distance.
329 if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID)
330 GlobalTyID = CompactionTypes[type-Type::FirstDerivedTyID].second;
Chris Lattner00950542001-06-06 20:29:01 +0000331
Reid Spencer060d25d2004-06-29 23:29:38 +0000332 if (hasImplicitNull(GlobalTyID)) {
Chris Lattneraba5ff52005-05-05 20:57:00 +0000333 const Type *Ty = getType(type);
334 if (!isa<OpaqueType>(Ty)) {
335 if (Num == 0)
336 return Constant::getNullValue(Ty);
337 --Num;
338 }
Chris Lattner89e02532004-01-18 21:08:15 +0000339 }
340
Chris Lattner52f86d62004-01-20 00:54:06 +0000341 if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
342 if (Num < ModuleValues[GlobalTyID]->size())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000343 return ModuleValues[GlobalTyID]->getOperand(Num);
Chris Lattner52f86d62004-01-20 00:54:06 +0000344 Num -= ModuleValues[GlobalTyID]->size();
Chris Lattner89e02532004-01-18 21:08:15 +0000345 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000346 }
347
Misha Brukman8a96c532005-04-21 21:44:41 +0000348 if (FunctionValues.size() > type &&
349 FunctionValues[type] &&
Reid Spencer060d25d2004-06-29 23:29:38 +0000350 Num < FunctionValues[type]->size())
351 return FunctionValues[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000352
Chris Lattner74734132002-08-17 22:01:27 +0000353 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000354
Reid Spencer551ccae2004-09-01 22:55:40 +0000355 // Did we already create a place holder?
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000356 std::pair<unsigned,unsigned> KeyValue(type, oNum);
Reid Spencer060d25d2004-06-29 23:29:38 +0000357 ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue);
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000358 if (I != ForwardReferences.end() && I->first == KeyValue)
359 return I->second; // We have already created this placeholder
360
Reid Spencer551ccae2004-09-01 22:55:40 +0000361 // If the type exists (it should)
362 if (const Type* Ty = getType(type)) {
363 // Create the place holder
364 Value *Val = new Argument(Ty);
365 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
366 return Val;
367 }
Reid Spencer233fe722006-08-22 16:09:19 +0000368 error("Can't create placeholder for value of type slot #" + utostr(type));
369 return 0; // just silence warning, error calls longjmp
Chris Lattner00950542001-06-06 20:29:01 +0000370}
371
Misha Brukman8a96c532005-04-21 21:44:41 +0000372/// This is just like getValue, but when a compaction table is in use, it
373/// is ignored. Also, no forward references or other fancy features are
Reid Spencer04cde2c2004-07-04 11:33:49 +0000374/// supported.
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000375Value* BytecodeReader::getGlobalTableValue(unsigned TyID, unsigned SlotNo) {
376 if (SlotNo == 0)
377 return Constant::getNullValue(getType(TyID));
378
379 if (!CompactionTypes.empty() && TyID >= Type::FirstDerivedTyID) {
380 TyID -= Type::FirstDerivedTyID;
381 if (TyID >= CompactionTypes.size())
382 error("Type ID out of range for compaction table!");
383 TyID = CompactionTypes[TyID].second;
Reid Spencer060d25d2004-06-29 23:29:38 +0000384 }
385
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000386 --SlotNo;
387
Reid Spencer060d25d2004-06-29 23:29:38 +0000388 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
389 SlotNo >= ModuleValues[TyID]->size()) {
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000390 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0)
391 error("Corrupt compaction table entry!"
Misha Brukman8a96c532005-04-21 21:44:41 +0000392 + utostr(TyID) + ", " + utostr(SlotNo) + ": "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000393 + utostr(ModuleValues.size()));
Misha Brukman8a96c532005-04-21 21:44:41 +0000394 else
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000395 error("Corrupt compaction table entry!"
Misha Brukman8a96c532005-04-21 21:44:41 +0000396 + utostr(TyID) + ", " + utostr(SlotNo) + ": "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000397 + utostr(ModuleValues.size()) + ", "
Reid Spencer9a7e0c52004-08-04 22:56:46 +0000398 + utohexstr(reinterpret_cast<uint64_t>(((void*)ModuleValues[TyID])))
399 + ", "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000400 + utostr(ModuleValues[TyID]->size()));
Reid Spencer060d25d2004-06-29 23:29:38 +0000401 }
402 return ModuleValues[TyID]->getOperand(SlotNo);
403}
404
Reid Spencer04cde2c2004-07-04 11:33:49 +0000405/// Just like getValue, except that it returns a null pointer
406/// only on error. It always returns a constant (meaning that if the value is
407/// defined, but is not a constant, that is an error). If the specified
Misha Brukman8a96c532005-04-21 21:44:41 +0000408/// constant hasn't been parsed yet, a placeholder is defined and used.
Reid Spencer04cde2c2004-07-04 11:33:49 +0000409/// Later, after the real value is parsed, the placeholder is eliminated.
Reid Spencer060d25d2004-06-29 23:29:38 +0000410Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) {
411 if (Value *V = getValue(TypeSlot, Slot, false))
412 if (Constant *C = dyn_cast<Constant>(V))
413 return C; // If we already have the value parsed, just return it
Reid Spencer060d25d2004-06-29 23:29:38 +0000414 else
Misha Brukman8a96c532005-04-21 21:44:41 +0000415 error("Value for slot " + utostr(Slot) +
Reid Spencera86037e2004-07-18 00:12:03 +0000416 " is expected to be a constant!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000417
Chris Lattner389bd042004-12-09 06:19:44 +0000418 std::pair<unsigned, unsigned> Key(TypeSlot, Slot);
Reid Spencer060d25d2004-06-29 23:29:38 +0000419 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
420
421 if (I != ConstantFwdRefs.end() && I->first == Key) {
422 return I->second;
423 } else {
424 // Create a placeholder for the constant reference and
425 // keep track of the fact that we have a forward ref to recycle it
Chris Lattner389bd042004-12-09 06:19:44 +0000426 Constant *C = new ConstantPlaceHolder(getType(TypeSlot));
Misha Brukman8a96c532005-04-21 21:44:41 +0000427
Reid Spencer060d25d2004-06-29 23:29:38 +0000428 // Keep track of the fact that we have a forward ref to recycle it
429 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
430 return C;
431 }
432}
433
434//===----------------------------------------------------------------------===//
435// IR Construction Methods
436//===----------------------------------------------------------------------===//
437
Reid Spencer04cde2c2004-07-04 11:33:49 +0000438/// As values are created, they are inserted into the appropriate place
439/// with this method. The ValueTable argument must be one of ModuleValues
440/// or FunctionValues data members of this class.
Misha Brukman8a96c532005-04-21 21:44:41 +0000441unsigned BytecodeReader::insertValue(Value *Val, unsigned type,
Reid Spencer46b002c2004-07-11 17:28:43 +0000442 ValueTable &ValueTab) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000443 if (ValueTab.size() <= type)
444 ValueTab.resize(type+1);
445
446 if (!ValueTab[type]) ValueTab[type] = new ValueList();
447
448 ValueTab[type]->push_back(Val);
449
Chris Lattneraba5ff52005-05-05 20:57:00 +0000450 bool HasOffset = hasImplicitNull(type) && !isa<OpaqueType>(Val->getType());
Reid Spencer060d25d2004-06-29 23:29:38 +0000451 return ValueTab[type]->size()-1 + HasOffset;
452}
453
Reid Spencer04cde2c2004-07-04 11:33:49 +0000454/// Insert the arguments of a function as new values in the reader.
Reid Spencer46b002c2004-07-11 17:28:43 +0000455void BytecodeReader::insertArguments(Function* F) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000456 const FunctionType *FT = F->getFunctionType();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000457 Function::arg_iterator AI = F->arg_begin();
Reid Spencer060d25d2004-06-29 23:29:38 +0000458 for (FunctionType::param_iterator It = FT->param_begin();
459 It != FT->param_end(); ++It, ++AI)
460 insertValue(AI, getTypeSlot(AI->getType()), FunctionValues);
461}
462
463//===----------------------------------------------------------------------===//
464// Bytecode Parsing Methods
465//===----------------------------------------------------------------------===//
466
Reid Spencer04cde2c2004-07-04 11:33:49 +0000467/// This method parses a single instruction. The instruction is
468/// inserted at the end of the \p BB provided. The arguments of
Misha Brukman44666b12004-09-28 16:57:46 +0000469/// the instruction are provided in the \p Oprnds vector.
Reid Spencer060d25d2004-06-29 23:29:38 +0000470void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
Reid Spencer46b002c2004-07-11 17:28:43 +0000471 BasicBlock* BB) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000472 BufPtr SaveAt = At;
473
474 // Clear instruction data
475 Oprnds.clear();
476 unsigned iType = 0;
477 unsigned Opcode = 0;
478 unsigned Op = read_uint();
479
480 // bits Instruction format: Common to all formats
481 // --------------------------
482 // 01-00: Opcode type, fixed to 1.
483 // 07-02: Opcode
484 Opcode = (Op >> 2) & 63;
485 Oprnds.resize((Op >> 0) & 03);
486
487 // Extract the operands
488 switch (Oprnds.size()) {
489 case 1:
490 // bits Instruction format:
491 // --------------------------
492 // 19-08: Resulting type plane
493 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
494 //
495 iType = (Op >> 8) & 4095;
496 Oprnds[0] = (Op >> 20) & 4095;
497 if (Oprnds[0] == 4095) // Handle special encoding for 0 operands...
498 Oprnds.resize(0);
499 break;
500 case 2:
501 // bits Instruction format:
502 // --------------------------
503 // 15-08: Resulting type plane
504 // 23-16: Operand #1
Misha Brukman8a96c532005-04-21 21:44:41 +0000505 // 31-24: Operand #2
Reid Spencer060d25d2004-06-29 23:29:38 +0000506 //
507 iType = (Op >> 8) & 255;
508 Oprnds[0] = (Op >> 16) & 255;
509 Oprnds[1] = (Op >> 24) & 255;
510 break;
511 case 3:
512 // bits Instruction format:
513 // --------------------------
514 // 13-08: Resulting type plane
515 // 19-14: Operand #1
516 // 25-20: Operand #2
517 // 31-26: Operand #3
518 //
519 iType = (Op >> 8) & 63;
520 Oprnds[0] = (Op >> 14) & 63;
521 Oprnds[1] = (Op >> 20) & 63;
522 Oprnds[2] = (Op >> 26) & 63;
523 break;
524 case 0:
525 At -= 4; // Hrm, try this again...
526 Opcode = read_vbr_uint();
527 Opcode >>= 2;
528 iType = read_vbr_uint();
529
530 unsigned NumOprnds = read_vbr_uint();
531 Oprnds.resize(NumOprnds);
532
533 if (NumOprnds == 0)
Reid Spencer24399722004-07-09 22:21:33 +0000534 error("Zero-argument instruction found; this is invalid.");
Reid Spencer060d25d2004-06-29 23:29:38 +0000535
536 for (unsigned i = 0; i != NumOprnds; ++i)
537 Oprnds[i] = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +0000538 break;
539 }
540
Reid Spencerd798a512006-11-14 04:47:22 +0000541 const Type *InstTy = getType(iType);
Reid Spencer060d25d2004-06-29 23:29:38 +0000542
Reid Spencer1628cec2006-10-26 06:15:43 +0000543 // Make the necessary adjustments for dealing with backwards compatibility
544 // of opcodes.
Reid Spencer3795ad12006-12-03 05:47:10 +0000545 Instruction* Result = 0;
Reid Spencer1628cec2006-10-26 06:15:43 +0000546
Reid Spencer46b002c2004-07-11 17:28:43 +0000547 // We have enough info to inform the handler now.
Reid Spencer1628cec2006-10-26 06:15:43 +0000548 if (Handler)
549 Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
Reid Spencer060d25d2004-06-29 23:29:38 +0000550
Reid Spencer3795ad12006-12-03 05:47:10 +0000551 // First, handle the easy binary operators case
552 if (Opcode >= Instruction::BinaryOpsBegin &&
Reid Spencerc8dab492006-12-03 06:28:54 +0000553 Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2) {
Reid Spencer3795ad12006-12-03 05:47:10 +0000554 Result = BinaryOperator::create(Instruction::BinaryOps(Opcode),
555 getValue(iType, Oprnds[0]),
556 getValue(iType, Oprnds[1]));
Reid Spencerc8dab492006-12-03 06:28:54 +0000557 } else {
Reid Spencer1628cec2006-10-26 06:15:43 +0000558 // Indicate that we don't think this is a call instruction (yet).
559 // Process based on the Opcode read
560 switch (Opcode) {
561 default: // There was an error, this shouldn't happen.
562 if (Result == 0)
563 error("Illegal instruction read!");
564 break;
565 case Instruction::VAArg:
566 if (Oprnds.size() != 2)
567 error("Invalid VAArg instruction!");
568 Result = new VAArgInst(getValue(iType, Oprnds[0]),
Reid Spencerd798a512006-11-14 04:47:22 +0000569 getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000570 break;
571 case Instruction::ExtractElement: {
572 if (Oprnds.size() != 2)
573 error("Invalid extractelement instruction!");
574 Value *V1 = getValue(iType, Oprnds[0]);
575 Value *V2 = getValue(Type::UIntTyID, Oprnds[1]);
Chris Lattner59fecec2006-04-08 04:09:19 +0000576
Reid Spencer1628cec2006-10-26 06:15:43 +0000577 if (!ExtractElementInst::isValidOperands(V1, V2))
578 error("Invalid extractelement instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000579
Reid Spencer1628cec2006-10-26 06:15:43 +0000580 Result = new ExtractElementInst(V1, V2);
581 break;
Chris Lattnera65371e2006-05-26 18:42:34 +0000582 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000583 case Instruction::InsertElement: {
584 const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
585 if (!PackedTy || Oprnds.size() != 3)
586 error("Invalid insertelement instruction!");
587
588 Value *V1 = getValue(iType, Oprnds[0]);
589 Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
590 Value *V3 = getValue(Type::UIntTyID, Oprnds[2]);
591
592 if (!InsertElementInst::isValidOperands(V1, V2, V3))
593 error("Invalid insertelement instruction!");
594 Result = new InsertElementInst(V1, V2, V3);
595 break;
596 }
597 case Instruction::ShuffleVector: {
598 const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
599 if (!PackedTy || Oprnds.size() != 3)
600 error("Invalid shufflevector instruction!");
601 Value *V1 = getValue(iType, Oprnds[0]);
602 Value *V2 = getValue(iType, Oprnds[1]);
603 const PackedType *EltTy =
604 PackedType::get(Type::UIntTy, PackedTy->getNumElements());
605 Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
606 if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
607 error("Invalid shufflevector instruction!");
608 Result = new ShuffleVectorInst(V1, V2, V3);
609 break;
610 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000611 case Instruction::Trunc:
612 if (Oprnds.size() != 2)
613 error("Invalid cast instruction!");
614 Result = new TruncInst(getValue(iType, Oprnds[0]),
615 getType(Oprnds[1]));
616 break;
617 case Instruction::ZExt:
618 if (Oprnds.size() != 2)
619 error("Invalid cast instruction!");
620 Result = new ZExtInst(getValue(iType, Oprnds[0]),
621 getType(Oprnds[1]));
622 break;
623 case Instruction::SExt:
Reid Spencer1628cec2006-10-26 06:15:43 +0000624 if (Oprnds.size() != 2)
625 error("Invalid Cast instruction!");
Reid Spencer3da59db2006-11-27 01:05:10 +0000626 Result = new SExtInst(getValue(iType, Oprnds[0]),
Reid Spencerd798a512006-11-14 04:47:22 +0000627 getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000628 break;
Reid Spencer3da59db2006-11-27 01:05:10 +0000629 case Instruction::FPTrunc:
630 if (Oprnds.size() != 2)
631 error("Invalid cast instruction!");
632 Result = new FPTruncInst(getValue(iType, Oprnds[0]),
633 getType(Oprnds[1]));
634 break;
635 case Instruction::FPExt:
636 if (Oprnds.size() != 2)
637 error("Invalid cast instruction!");
638 Result = new FPExtInst(getValue(iType, Oprnds[0]),
639 getType(Oprnds[1]));
640 break;
641 case Instruction::UIToFP:
642 if (Oprnds.size() != 2)
643 error("Invalid cast instruction!");
644 Result = new UIToFPInst(getValue(iType, Oprnds[0]),
645 getType(Oprnds[1]));
646 break;
647 case Instruction::SIToFP:
648 if (Oprnds.size() != 2)
649 error("Invalid cast instruction!");
650 Result = new SIToFPInst(getValue(iType, Oprnds[0]),
651 getType(Oprnds[1]));
652 break;
653 case Instruction::FPToUI:
654 if (Oprnds.size() != 2)
655 error("Invalid cast instruction!");
656 Result = new FPToUIInst(getValue(iType, Oprnds[0]),
657 getType(Oprnds[1]));
658 break;
659 case Instruction::FPToSI:
660 if (Oprnds.size() != 2)
661 error("Invalid cast instruction!");
662 Result = new FPToSIInst(getValue(iType, Oprnds[0]),
663 getType(Oprnds[1]));
664 break;
665 case Instruction::IntToPtr:
666 if (Oprnds.size() != 2)
667 error("Invalid cast instruction!");
668 Result = new IntToPtrInst(getValue(iType, Oprnds[0]),
669 getType(Oprnds[1]));
670 break;
671 case Instruction::PtrToInt:
672 if (Oprnds.size() != 2)
673 error("Invalid cast instruction!");
674 Result = new PtrToIntInst(getValue(iType, Oprnds[0]),
675 getType(Oprnds[1]));
676 break;
677 case Instruction::BitCast:
678 if (Oprnds.size() != 2)
679 error("Invalid cast instruction!");
680 Result = new BitCastInst(getValue(iType, Oprnds[0]),
681 getType(Oprnds[1]));
682 break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000683 case Instruction::Select:
684 if (Oprnds.size() != 3)
685 error("Invalid Select instruction!");
686 Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]),
687 getValue(iType, Oprnds[1]),
688 getValue(iType, Oprnds[2]));
689 break;
690 case Instruction::PHI: {
691 if (Oprnds.size() == 0 || (Oprnds.size() & 1))
692 error("Invalid phi node encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000693
Reid Spencer1628cec2006-10-26 06:15:43 +0000694 PHINode *PN = new PHINode(InstTy);
695 PN->reserveOperandSpace(Oprnds.size());
696 for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2)
697 PN->addIncoming(
698 getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1]));
699 Result = PN;
700 break;
701 }
Reid Spencerc8dab492006-12-03 06:28:54 +0000702 case Instruction::ICmp:
703 case Instruction::FCmp:
704 // These instructions encode the comparison predicate as the 3rd operand.
705 Result = CmpInst::create(Instruction::OtherOps(Opcode),
706 static_cast<unsigned short>(Oprnds[2]),
707 getValue(iType, Oprnds[0]), getValue(iType, Oprnds[1]));
708 break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000709 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +0000710 case Instruction::LShr:
711 case Instruction::AShr:
Reid Spencer1628cec2006-10-26 06:15:43 +0000712 Result = new ShiftInst(Instruction::OtherOps(Opcode),
713 getValue(iType, Oprnds[0]),
714 getValue(Type::UByteTyID, Oprnds[1]));
715 break;
716 case Instruction::Ret:
717 if (Oprnds.size() == 0)
718 Result = new ReturnInst();
719 else if (Oprnds.size() == 1)
720 Result = new ReturnInst(getValue(iType, Oprnds[0]));
721 else
722 error("Unrecognized instruction!");
723 break;
724
725 case Instruction::Br:
726 if (Oprnds.size() == 1)
727 Result = new BranchInst(getBasicBlock(Oprnds[0]));
728 else if (Oprnds.size() == 3)
729 Result = new BranchInst(getBasicBlock(Oprnds[0]),
730 getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2]));
731 else
732 error("Invalid number of operands for a 'br' instruction!");
733 break;
734 case Instruction::Switch: {
735 if (Oprnds.size() & 1)
736 error("Switch statement with odd number of arguments!");
737
738 SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]),
739 getBasicBlock(Oprnds[1]),
740 Oprnds.size()/2-1);
741 for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2)
742 I->addCase(cast<ConstantInt>(getValue(iType, Oprnds[i])),
743 getBasicBlock(Oprnds[i+1]));
744 Result = I;
745 break;
746 }
747 case 58: // Call with extra operand for calling conv
748 case 59: // tail call, Fast CC
749 case 60: // normal call, Fast CC
750 case 61: // tail call, C Calling Conv
751 case Instruction::Call: { // Normal Call, C Calling Convention
752 if (Oprnds.size() == 0)
753 error("Invalid call instruction encountered!");
Reid Spencer1628cec2006-10-26 06:15:43 +0000754 Value *F = getValue(iType, Oprnds[0]);
755
756 unsigned CallingConv = CallingConv::C;
757 bool isTailCall = false;
758
759 if (Opcode == 61 || Opcode == 59)
760 isTailCall = true;
761
762 if (Opcode == 58) {
763 isTailCall = Oprnds.back() & 1;
764 CallingConv = Oprnds.back() >> 1;
765 Oprnds.pop_back();
766 } else if (Opcode == 59 || Opcode == 60) {
767 CallingConv = CallingConv::Fast;
768 }
769
770 // Check to make sure we have a pointer to function type
771 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
772 if (PTy == 0) error("Call to non function pointer value!");
773 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
774 if (FTy == 0) error("Call to non function pointer value!");
775
776 std::vector<Value *> Params;
777 if (!FTy->isVarArg()) {
778 FunctionType::param_iterator It = FTy->param_begin();
779
780 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
781 if (It == FTy->param_end())
782 error("Invalid call instruction!");
783 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
784 }
785 if (It != FTy->param_end())
Reid Spencer24399722004-07-09 22:21:33 +0000786 error("Invalid call instruction!");
Reid Spencer1628cec2006-10-26 06:15:43 +0000787 } else {
788 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
789
790 unsigned FirstVariableOperand;
791 if (Oprnds.size() < FTy->getNumParams())
792 error("Call instruction missing operands!");
793
794 // Read all of the fixed arguments
795 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
796 Params.push_back(
797 getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i]));
798
799 FirstVariableOperand = FTy->getNumParams();
800
801 if ((Oprnds.size()-FirstVariableOperand) & 1)
802 error("Invalid call instruction!"); // Must be pairs of type/value
803
804 for (unsigned i = FirstVariableOperand, e = Oprnds.size();
805 i != e; i += 2)
806 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000807 }
Reid Spencer060d25d2004-06-29 23:29:38 +0000808
Reid Spencer1628cec2006-10-26 06:15:43 +0000809 Result = new CallInst(F, Params);
810 if (isTailCall) cast<CallInst>(Result)->setTailCall();
811 if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv);
812 break;
Reid Spencer060d25d2004-06-29 23:29:38 +0000813 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000814 case Instruction::Invoke: { // Invoke C CC
815 if (Oprnds.size() < 3)
816 error("Invalid invoke instruction!");
817 Value *F = getValue(iType, Oprnds[0]);
Reid Spencer060d25d2004-06-29 23:29:38 +0000818
Reid Spencer1628cec2006-10-26 06:15:43 +0000819 // Check to make sure we have a pointer to function type
820 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
821 if (PTy == 0)
822 error("Invoke to non function pointer value!");
823 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
824 if (FTy == 0)
825 error("Invoke to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000826
Reid Spencer1628cec2006-10-26 06:15:43 +0000827 std::vector<Value *> Params;
828 BasicBlock *Normal, *Except;
Reid Spencer3da59db2006-11-27 01:05:10 +0000829 unsigned CallingConv = Oprnds.back();
830 Oprnds.pop_back();
Chris Lattnerdee199f2005-05-06 22:34:01 +0000831
Reid Spencer1628cec2006-10-26 06:15:43 +0000832 if (!FTy->isVarArg()) {
833 Normal = getBasicBlock(Oprnds[1]);
834 Except = getBasicBlock(Oprnds[2]);
Reid Spencer060d25d2004-06-29 23:29:38 +0000835
Reid Spencer1628cec2006-10-26 06:15:43 +0000836 FunctionType::param_iterator It = FTy->param_begin();
837 for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
838 if (It == FTy->param_end())
839 error("Invalid invoke instruction!");
840 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
841 }
842 if (It != FTy->param_end())
Reid Spencer24399722004-07-09 22:21:33 +0000843 error("Invalid invoke instruction!");
Reid Spencer1628cec2006-10-26 06:15:43 +0000844 } else {
845 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
846
847 Normal = getBasicBlock(Oprnds[0]);
848 Except = getBasicBlock(Oprnds[1]);
849
850 unsigned FirstVariableArgument = FTy->getNumParams()+2;
851 for (unsigned i = 2; i != FirstVariableArgument; ++i)
852 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
853 Oprnds[i]));
854
855 // Must be type/value pairs. If not, error out.
856 if (Oprnds.size()-FirstVariableArgument & 1)
857 error("Invalid invoke instruction!");
858
859 for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
860 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000861 }
Reid Spencer060d25d2004-06-29 23:29:38 +0000862
Reid Spencer1628cec2006-10-26 06:15:43 +0000863 Result = new InvokeInst(F, Normal, Except, Params);
864 if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
865 break;
Reid Spencer060d25d2004-06-29 23:29:38 +0000866 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000867 case Instruction::Malloc: {
868 unsigned Align = 0;
869 if (Oprnds.size() == 2)
870 Align = (1 << Oprnds[1]) >> 1;
871 else if (Oprnds.size() > 2)
872 error("Invalid malloc instruction!");
873 if (!isa<PointerType>(InstTy))
874 error("Invalid malloc instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000875
Reid Spencer1628cec2006-10-26 06:15:43 +0000876 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
877 getValue(Type::UIntTyID, Oprnds[0]), Align);
878 break;
879 }
880 case Instruction::Alloca: {
881 unsigned Align = 0;
882 if (Oprnds.size() == 2)
883 Align = (1 << Oprnds[1]) >> 1;
884 else if (Oprnds.size() > 2)
885 error("Invalid alloca instruction!");
886 if (!isa<PointerType>(InstTy))
887 error("Invalid alloca instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000888
Reid Spencer1628cec2006-10-26 06:15:43 +0000889 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
890 getValue(Type::UIntTyID, Oprnds[0]), Align);
891 break;
892 }
893 case Instruction::Free:
894 if (!isa<PointerType>(InstTy))
895 error("Invalid free instruction!");
896 Result = new FreeInst(getValue(iType, Oprnds[0]));
897 break;
898 case Instruction::GetElementPtr: {
899 if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
Misha Brukman8a96c532005-04-21 21:44:41 +0000900 error("Invalid getelementptr instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000901
Reid Spencer1628cec2006-10-26 06:15:43 +0000902 std::vector<Value*> Idx;
903
904 const Type *NextTy = InstTy;
905 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
906 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
907 if (!TopTy)
908 error("Invalid getelementptr instruction!");
909
910 unsigned ValIdx = Oprnds[i];
911 unsigned IdxTy = 0;
Reid Spencerd798a512006-11-14 04:47:22 +0000912 // Struct indices are always uints, sequential type indices can be
913 // any of the 32 or 64-bit integer types. The actual choice of
914 // type is encoded in the low two bits of the slot number.
915 if (isa<StructType>(TopTy))
916 IdxTy = Type::UIntTyID;
917 else {
918 switch (ValIdx & 3) {
919 default:
920 case 0: IdxTy = Type::UIntTyID; break;
921 case 1: IdxTy = Type::IntTyID; break;
922 case 2: IdxTy = Type::ULongTyID; break;
923 case 3: IdxTy = Type::LongTyID; break;
Reid Spencer060d25d2004-06-29 23:29:38 +0000924 }
Reid Spencerd798a512006-11-14 04:47:22 +0000925 ValIdx >>= 2;
Reid Spencer060d25d2004-06-29 23:29:38 +0000926 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000927 Idx.push_back(getValue(IdxTy, ValIdx));
Reid Spencer1628cec2006-10-26 06:15:43 +0000928 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Reid Spencer060d25d2004-06-29 23:29:38 +0000929 }
930
Reid Spencer1628cec2006-10-26 06:15:43 +0000931 Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
932 break;
Reid Spencer060d25d2004-06-29 23:29:38 +0000933 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000934 case 62: // volatile load
935 case Instruction::Load:
936 if (Oprnds.size() != 1 || !isa<PointerType>(InstTy))
937 error("Invalid load instruction!");
938 Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62);
939 break;
940 case 63: // volatile store
941 case Instruction::Store: {
942 if (!isa<PointerType>(InstTy) || Oprnds.size() != 2)
943 error("Invalid store instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000944
Reid Spencer1628cec2006-10-26 06:15:43 +0000945 Value *Ptr = getValue(iType, Oprnds[1]);
946 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
947 Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr,
948 Opcode == 63);
949 break;
950 }
951 case Instruction::Unwind:
952 if (Oprnds.size() != 0) error("Invalid unwind instruction!");
953 Result = new UnwindInst();
954 break;
955 case Instruction::Unreachable:
956 if (Oprnds.size() != 0) error("Invalid unreachable instruction!");
957 Result = new UnreachableInst();
958 break;
959 } // end switch(Opcode)
Reid Spencer3795ad12006-12-03 05:47:10 +0000960 } // end if !Result
Reid Spencer060d25d2004-06-29 23:29:38 +0000961
Reid Spencere1e96c02006-01-19 07:02:16 +0000962 BB->getInstList().push_back(Result);
963
Reid Spencer060d25d2004-06-29 23:29:38 +0000964 unsigned TypeSlot;
965 if (Result->getType() == InstTy)
966 TypeSlot = iType;
967 else
968 TypeSlot = getTypeSlot(Result->getType());
969
970 insertValue(Result, TypeSlot, FunctionValues);
Reid Spencer060d25d2004-06-29 23:29:38 +0000971}
972
Reid Spencer04cde2c2004-07-04 11:33:49 +0000973/// Get a particular numbered basic block, which might be a forward reference.
Reid Spencerd798a512006-11-14 04:47:22 +0000974/// This works together with ParseInstructionList to handle these forward
975/// references in a clean manner. This function is used when constructing
976/// phi, br, switch, and other instructions that reference basic blocks.
977/// Blocks are numbered sequentially as they appear in the function.
Reid Spencer060d25d2004-06-29 23:29:38 +0000978BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000979 // Make sure there is room in the table...
980 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
981
Reid Spencerd798a512006-11-14 04:47:22 +0000982 // First check to see if this is a backwards reference, i.e. this block
983 // has already been created, or if the forward reference has already
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000984 // been created.
985 if (ParsedBasicBlocks[ID])
986 return ParsedBasicBlocks[ID];
987
988 // Otherwise, the basic block has not yet been created. Do so and add it to
989 // the ParsedBasicBlocks list.
990 return ParsedBasicBlocks[ID] = new BasicBlock();
991}
992
Reid Spencer04cde2c2004-07-04 11:33:49 +0000993/// Parse all of the BasicBlock's & Instruction's in the body of a function.
Misha Brukman8a96c532005-04-21 21:44:41 +0000994/// In post 1.0 bytecode files, we no longer emit basic block individually,
Reid Spencer04cde2c2004-07-04 11:33:49 +0000995/// in order to avoid per-basic-block overhead.
Reid Spencerd798a512006-11-14 04:47:22 +0000996/// @returns the number of basic blocks encountered.
Reid Spencer060d25d2004-06-29 23:29:38 +0000997unsigned BytecodeReader::ParseInstructionList(Function* F) {
Chris Lattner8d1dbd22003-12-01 07:05:31 +0000998 unsigned BlockNo = 0;
999 std::vector<unsigned> Args;
1000
Reid Spencer46b002c2004-07-11 17:28:43 +00001001 while (moreInBlock()) {
1002 if (Handler) Handler->handleBasicBlockBegin(BlockNo);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001003 BasicBlock *BB;
1004 if (ParsedBasicBlocks.size() == BlockNo)
1005 ParsedBasicBlocks.push_back(BB = new BasicBlock());
1006 else if (ParsedBasicBlocks[BlockNo] == 0)
1007 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
1008 else
1009 BB = ParsedBasicBlocks[BlockNo];
1010 ++BlockNo;
1011 F->getBasicBlockList().push_back(BB);
1012
1013 // Read instructions into this basic block until we get to a terminator
Reid Spencer46b002c2004-07-11 17:28:43 +00001014 while (moreInBlock() && !BB->getTerminator())
Reid Spencer060d25d2004-06-29 23:29:38 +00001015 ParseInstruction(Args, BB);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001016
1017 if (!BB->getTerminator())
Reid Spencer24399722004-07-09 22:21:33 +00001018 error("Non-terminated basic block found!");
Reid Spencer5c15fe52004-07-05 00:57:50 +00001019
Reid Spencer46b002c2004-07-11 17:28:43 +00001020 if (Handler) Handler->handleBasicBlockEnd(BlockNo-1);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001021 }
1022
1023 return BlockNo;
1024}
1025
Reid Spencer04cde2c2004-07-04 11:33:49 +00001026/// Parse a symbol table. This works for both module level and function
1027/// level symbol tables. For function level symbol tables, the CurrentFunction
1028/// parameter must be non-zero and the ST parameter must correspond to
1029/// CurrentFunction's symbol table. For Module level symbol tables, the
1030/// CurrentFunction argument must be zero.
Reid Spencer060d25d2004-06-29 23:29:38 +00001031void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001032 SymbolTable *ST) {
1033 if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
Reid Spencer060d25d2004-06-29 23:29:38 +00001034
Chris Lattner39cacce2003-10-10 05:43:47 +00001035 // Allow efficient basic block lookup by number.
1036 std::vector<BasicBlock*> BBMap;
1037 if (CurrentFunction)
1038 for (Function::iterator I = CurrentFunction->begin(),
1039 E = CurrentFunction->end(); I != E; ++I)
1040 BBMap.push_back(I);
1041
Reid Spencerd798a512006-11-14 04:47:22 +00001042 // Symtab block header: [num entries]
1043 unsigned NumEntries = read_vbr_uint();
1044 for (unsigned i = 0; i < NumEntries; ++i) {
1045 // Symtab entry: [def slot #][name]
1046 unsigned slot = read_vbr_uint();
1047 std::string Name = read_str();
1048 const Type* T = getType(slot);
1049 ST->insert(Name, T);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001050 }
1051
Reid Spencer46b002c2004-07-11 17:28:43 +00001052 while (moreInBlock()) {
Chris Lattner00950542001-06-06 20:29:01 +00001053 // Symtab block header: [num entries][type id number]
Reid Spencer060d25d2004-06-29 23:29:38 +00001054 unsigned NumEntries = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00001055 unsigned Typ = read_vbr_uint();
Chris Lattner1d670cc2001-09-07 16:37:43 +00001056
Chris Lattner7dc3a2e2003-10-13 14:57:53 +00001057 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +00001058 // Symtab entry: [def slot #][name]
Reid Spencer060d25d2004-06-29 23:29:38 +00001059 unsigned slot = read_vbr_uint();
1060 std::string Name = read_str();
Reid Spencerd798a512006-11-14 04:47:22 +00001061 Value *V = 0;
1062 if (Typ == Type::LabelTyID) {
1063 if (slot < BBMap.size())
1064 V = BBMap[slot];
Chris Lattner39cacce2003-10-10 05:43:47 +00001065 } else {
Reid Spencerd798a512006-11-14 04:47:22 +00001066 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +00001067 }
Reid Spencerd798a512006-11-14 04:47:22 +00001068 if (V == 0)
1069 error("Failed value look-up for name '" + Name + "'");
1070 V->setName(Name);
Chris Lattner00950542001-06-06 20:29:01 +00001071 }
1072 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001073 checkPastBlockEnd("Symbol Table");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001074 if (Handler) Handler->handleSymbolTableEnd();
Chris Lattner00950542001-06-06 20:29:01 +00001075}
1076
Misha Brukman8a96c532005-04-21 21:44:41 +00001077/// Read in the types portion of a compaction table.
Reid Spencer46b002c2004-07-11 17:28:43 +00001078void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001079 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencerd798a512006-11-14 04:47:22 +00001080 unsigned TypeSlot = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001081 const Type *Typ = getGlobalTableType(TypeSlot);
Chris Lattner45b5dd22004-08-03 23:41:28 +00001082 CompactionTypes.push_back(std::make_pair(Typ, TypeSlot));
Reid Spencer46b002c2004-07-11 17:28:43 +00001083 if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001084 }
1085}
1086
1087/// Parse a compaction table.
Reid Spencer060d25d2004-06-29 23:29:38 +00001088void BytecodeReader::ParseCompactionTable() {
1089
Reid Spencer46b002c2004-07-11 17:28:43 +00001090 // Notify handler that we're beginning a compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001091 if (Handler) Handler->handleCompactionTableBegin();
1092
Reid Spencerd798a512006-11-14 04:47:22 +00001093 // Get the types for the compaction table.
1094 unsigned NumEntries = read_vbr_uint();
1095 ParseCompactionTypes(NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001096
Reid Spencer46b002c2004-07-11 17:28:43 +00001097 // Compaction tables live in separate blocks so we have to loop
1098 // until we've read the whole thing.
1099 while (moreInBlock()) {
1100 // Read the number of Value* entries in the compaction table
Reid Spencer060d25d2004-06-29 23:29:38 +00001101 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001102 unsigned Ty = 0;
Reid Spencer060d25d2004-06-29 23:29:38 +00001103
Reid Spencer46b002c2004-07-11 17:28:43 +00001104 // Decode the type from value read in. Most compaction table
1105 // planes will have one or two entries in them. If that's the
1106 // case then the length is encoded in the bottom two bits and
1107 // the higher bits encode the type. This saves another VBR value.
Reid Spencer060d25d2004-06-29 23:29:38 +00001108 if ((NumEntries & 3) == 3) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001109 // In this case, both low-order bits are set (value 3). This
1110 // is a signal that the typeid follows.
Reid Spencer060d25d2004-06-29 23:29:38 +00001111 NumEntries >>= 2;
Reid Spencerd798a512006-11-14 04:47:22 +00001112 Ty = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001113 } else {
Reid Spencer46b002c2004-07-11 17:28:43 +00001114 // In this case, the low-order bits specify the number of entries
1115 // and the high order bits specify the type.
Reid Spencer060d25d2004-06-29 23:29:38 +00001116 Ty = NumEntries >> 2;
1117 NumEntries &= 3;
1118 }
1119
Reid Spencerd798a512006-11-14 04:47:22 +00001120 // Make sure we have enough room for the plane.
1121 if (Ty >= CompactionValues.size())
1122 CompactionValues.resize(Ty+1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001123
Reid Spencerd798a512006-11-14 04:47:22 +00001124 // Make sure the plane is empty or we have some kind of error.
1125 if (!CompactionValues[Ty].empty())
1126 error("Compaction table plane contains multiple entries!");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001127
Reid Spencerd798a512006-11-14 04:47:22 +00001128 // Notify handler about the plane.
1129 if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001130
Reid Spencerd798a512006-11-14 04:47:22 +00001131 // Push the implicit zero.
1132 CompactionValues[Ty].push_back(Constant::getNullValue(getType(Ty)));
Reid Spencer46b002c2004-07-11 17:28:43 +00001133
Reid Spencerd798a512006-11-14 04:47:22 +00001134 // Read in each of the entries, put them in the compaction table
1135 // and notify the handler that we have a new compaction table value.
1136 for (unsigned i = 0; i != NumEntries; ++i) {
1137 unsigned ValSlot = read_vbr_uint();
1138 Value *V = getGlobalTableValue(Ty, ValSlot);
1139 CompactionValues[Ty].push_back(V);
1140 if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot);
Reid Spencer060d25d2004-06-29 23:29:38 +00001141 }
1142 }
Reid Spencer46b002c2004-07-11 17:28:43 +00001143 // Notify handler that the compaction table is done.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001144 if (Handler) Handler->handleCompactionTableEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001145}
Misha Brukman8a96c532005-04-21 21:44:41 +00001146
Reid Spencer46b002c2004-07-11 17:28:43 +00001147// Parse a single type. The typeid is read in first. If its a primitive type
1148// then nothing else needs to be read, we know how to instantiate it. If its
Misha Brukman8a96c532005-04-21 21:44:41 +00001149// a derived type, then additional data is read to fill out the type
Reid Spencer46b002c2004-07-11 17:28:43 +00001150// definition.
1151const Type *BytecodeReader::ParseType() {
Reid Spencerd798a512006-11-14 04:47:22 +00001152 unsigned PrimType = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001153 const Type *Result = 0;
1154 if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType)))
1155 return Result;
Misha Brukman8a96c532005-04-21 21:44:41 +00001156
Reid Spencer060d25d2004-06-29 23:29:38 +00001157 switch (PrimType) {
1158 case Type::FunctionTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001159 const Type *RetType = readType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001160
1161 unsigned NumParams = read_vbr_uint();
1162
1163 std::vector<const Type*> Params;
Misha Brukman8a96c532005-04-21 21:44:41 +00001164 while (NumParams--)
Reid Spencerd798a512006-11-14 04:47:22 +00001165 Params.push_back(readType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001166
1167 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1168 if (isVarArg) Params.pop_back();
1169
1170 Result = FunctionType::get(RetType, Params, isVarArg);
1171 break;
1172 }
1173 case Type::ArrayTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001174 const Type *ElementType = readType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001175 unsigned NumElements = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001176 Result = ArrayType::get(ElementType, NumElements);
1177 break;
1178 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001179 case Type::PackedTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001180 const Type *ElementType = readType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00001181 unsigned NumElements = read_vbr_uint();
1182 Result = PackedType::get(ElementType, NumElements);
1183 break;
1184 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001185 case Type::StructTyID: {
1186 std::vector<const Type*> Elements;
Reid Spencerd798a512006-11-14 04:47:22 +00001187 unsigned Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001188 while (Typ) { // List is terminated by void/0 typeid
1189 Elements.push_back(getType(Typ));
Reid Spencerd798a512006-11-14 04:47:22 +00001190 Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001191 }
1192
1193 Result = StructType::get(Elements);
1194 break;
1195 }
1196 case Type::PointerTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001197 Result = PointerType::get(readType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001198 break;
1199 }
1200
1201 case Type::OpaqueTyID: {
1202 Result = OpaqueType::get();
1203 break;
1204 }
1205
1206 default:
Reid Spencer24399722004-07-09 22:21:33 +00001207 error("Don't know how to deserialize primitive type " + utostr(PrimType));
Reid Spencer060d25d2004-06-29 23:29:38 +00001208 break;
1209 }
Reid Spencer46b002c2004-07-11 17:28:43 +00001210 if (Handler) Handler->handleType(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001211 return Result;
1212}
1213
Reid Spencer5b472d92004-08-21 20:49:23 +00001214// ParseTypes - We have to use this weird code to handle recursive
Reid Spencer060d25d2004-06-29 23:29:38 +00001215// types. We know that recursive types will only reference the current slab of
1216// values in the type plane, but they can forward reference types before they
1217// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
1218// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
1219// this ugly problem, we pessimistically insert an opaque type for each type we
1220// are about to read. This means that forward references will resolve to
1221// something and when we reread the type later, we can replace the opaque type
1222// with a new resolved concrete type.
1223//
Reid Spencer46b002c2004-07-11 17:28:43 +00001224void BytecodeReader::ParseTypes(TypeListTy &Tab, unsigned NumEntries){
Reid Spencer060d25d2004-06-29 23:29:38 +00001225 assert(Tab.size() == 0 && "should not have read type constants in before!");
1226
1227 // Insert a bunch of opaque types to be resolved later...
1228 Tab.reserve(NumEntries);
1229 for (unsigned i = 0; i != NumEntries; ++i)
1230 Tab.push_back(OpaqueType::get());
1231
Misha Brukman8a96c532005-04-21 21:44:41 +00001232 if (Handler)
Reid Spencer5b472d92004-08-21 20:49:23 +00001233 Handler->handleTypeList(NumEntries);
1234
Chris Lattnereebac5f2005-10-03 21:26:53 +00001235 // If we are about to resolve types, make sure the type cache is clear.
1236 if (NumEntries)
1237 ModuleTypeIDCache.clear();
1238
Reid Spencer060d25d2004-06-29 23:29:38 +00001239 // Loop through reading all of the types. Forward types will make use of the
1240 // opaque types just inserted.
1241 //
1242 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001243 const Type* NewTy = ParseType();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001244 const Type* OldTy = Tab[i].get();
Misha Brukman8a96c532005-04-21 21:44:41 +00001245 if (NewTy == 0)
Reid Spencer24399722004-07-09 22:21:33 +00001246 error("Couldn't parse type!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001247
Misha Brukman8a96c532005-04-21 21:44:41 +00001248 // Don't directly push the new type on the Tab. Instead we want to replace
Reid Spencer060d25d2004-06-29 23:29:38 +00001249 // the opaque type we previously inserted with the new concrete value. This
1250 // approach helps with forward references to types. The refinement from the
1251 // abstract (opaque) type to the new type causes all uses of the abstract
1252 // type to use the concrete type (NewTy). This will also cause the opaque
1253 // type to be deleted.
1254 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
1255
1256 // This should have replaced the old opaque type with the new type in the
1257 // value table... or with a preexisting type that was already in the system.
1258 // Let's just make sure it did.
1259 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
1260 }
1261}
1262
Reid Spencer04cde2c2004-07-04 11:33:49 +00001263/// Parse a single constant value
Chris Lattner3bc5a602006-01-25 23:08:15 +00001264Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001265 // We must check for a ConstantExpr before switching by type because
1266 // a ConstantExpr can be of any type, and has no explicit value.
Misha Brukman8a96c532005-04-21 21:44:41 +00001267 //
Reid Spencer060d25d2004-06-29 23:29:38 +00001268 // 0 if not expr; numArgs if is expr
1269 unsigned isExprNumArgs = read_vbr_uint();
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001270
Reid Spencer060d25d2004-06-29 23:29:38 +00001271 if (isExprNumArgs) {
Reid Spencerd798a512006-11-14 04:47:22 +00001272 // 'undef' is encoded with 'exprnumargs' == 1.
1273 if (isExprNumArgs == 1)
1274 return UndefValue::get(getType(TypeID));
Misha Brukman8a96c532005-04-21 21:44:41 +00001275
Reid Spencerd798a512006-11-14 04:47:22 +00001276 // Inline asm is encoded with exprnumargs == ~0U.
1277 if (isExprNumArgs == ~0U) {
1278 std::string AsmStr = read_str();
1279 std::string ConstraintStr = read_str();
1280 unsigned Flags = read_vbr_uint();
Chris Lattner3bc5a602006-01-25 23:08:15 +00001281
Reid Spencerd798a512006-11-14 04:47:22 +00001282 const PointerType *PTy = dyn_cast<PointerType>(getType(TypeID));
1283 const FunctionType *FTy =
1284 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
1285
1286 if (!FTy || !InlineAsm::Verify(FTy, ConstraintStr))
1287 error("Invalid constraints for inline asm");
1288 if (Flags & ~1U)
1289 error("Invalid flags for inline asm");
1290 bool HasSideEffects = Flags & 1;
1291 return InlineAsm::get(FTy, AsmStr, ConstraintStr, HasSideEffects);
Chris Lattner3bc5a602006-01-25 23:08:15 +00001292 }
Reid Spencerd798a512006-11-14 04:47:22 +00001293
1294 --isExprNumArgs;
Chris Lattner3bc5a602006-01-25 23:08:15 +00001295
Reid Spencer060d25d2004-06-29 23:29:38 +00001296 // FIXME: Encoding of constant exprs could be much more compact!
1297 std::vector<Constant*> ArgVec;
1298 ArgVec.reserve(isExprNumArgs);
1299 unsigned Opcode = read_vbr_uint();
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001300
Reid Spencer060d25d2004-06-29 23:29:38 +00001301 // Read the slot number and types of each of the arguments
1302 for (unsigned i = 0; i != isExprNumArgs; ++i) {
1303 unsigned ArgValSlot = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00001304 unsigned ArgTypeSlot = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001305
Reid Spencer060d25d2004-06-29 23:29:38 +00001306 // Get the arg value from its slot if it exists, otherwise a placeholder
1307 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
1308 }
Misha Brukman8a96c532005-04-21 21:44:41 +00001309
Reid Spencer060d25d2004-06-29 23:29:38 +00001310 // Construct a ConstantExpr of the appropriate kind
1311 if (isExprNumArgs == 1) { // All one-operand expressions
Reid Spencer3da59db2006-11-27 01:05:10 +00001312 if (!Instruction::isCast(Opcode))
Chris Lattner02dce162004-12-04 05:28:27 +00001313 error("Only cast instruction has one argument for ConstantExpr");
Reid Spencer46b002c2004-07-11 17:28:43 +00001314
Reid Spencer3da59db2006-11-27 01:05:10 +00001315 Constant *Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001316 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001317 return Result;
1318 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
1319 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Reid Spencer3da59db2006-11-27 01:05:10 +00001320 Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001321 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001322 return Result;
1323 } else if (Opcode == Instruction::Select) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001324 if (ArgVec.size() != 3)
1325 error("Select instruction must have three arguments.");
Misha Brukman8a96c532005-04-21 21:44:41 +00001326 Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
Reid Spencer04cde2c2004-07-04 11:33:49 +00001327 ArgVec[2]);
1328 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001329 return Result;
Robert Bocchinofee31b32006-01-10 19:04:39 +00001330 } else if (Opcode == Instruction::ExtractElement) {
Chris Lattner59fecec2006-04-08 04:09:19 +00001331 if (ArgVec.size() != 2 ||
1332 !ExtractElementInst::isValidOperands(ArgVec[0], ArgVec[1]))
1333 error("Invalid extractelement constand expr arguments");
Robert Bocchinofee31b32006-01-10 19:04:39 +00001334 Constant* Result = ConstantExpr::getExtractElement(ArgVec[0], ArgVec[1]);
1335 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1336 return Result;
Robert Bocchinob1f240b2006-01-17 20:06:35 +00001337 } else if (Opcode == Instruction::InsertElement) {
Chris Lattner59fecec2006-04-08 04:09:19 +00001338 if (ArgVec.size() != 3 ||
1339 !InsertElementInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2]))
1340 error("Invalid insertelement constand expr arguments");
1341
1342 Constant *Result =
Robert Bocchinob1f240b2006-01-17 20:06:35 +00001343 ConstantExpr::getInsertElement(ArgVec[0], ArgVec[1], ArgVec[2]);
1344 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1345 return Result;
Chris Lattner30b44b62006-04-08 01:17:59 +00001346 } else if (Opcode == Instruction::ShuffleVector) {
1347 if (ArgVec.size() != 3 ||
1348 !ShuffleVectorInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2]))
Chris Lattner59fecec2006-04-08 04:09:19 +00001349 error("Invalid shufflevector constant expr arguments.");
Chris Lattner30b44b62006-04-08 01:17:59 +00001350 Constant *Result =
1351 ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]);
1352 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1353 return Result;
Reid Spencer060d25d2004-06-29 23:29:38 +00001354 } else { // All other 2-operand expressions
1355 Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001356 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001357 return Result;
1358 }
1359 }
Misha Brukman8a96c532005-04-21 21:44:41 +00001360
Reid Spencer060d25d2004-06-29 23:29:38 +00001361 // Ok, not an ConstantExpr. We now know how to read the given type...
1362 const Type *Ty = getType(TypeID);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001363 Constant *Result = 0;
Reid Spencer060d25d2004-06-29 23:29:38 +00001364 switch (Ty->getTypeID()) {
1365 case Type::BoolTyID: {
1366 unsigned Val = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001367 if (Val != 0 && Val != 1)
Reid Spencer24399722004-07-09 22:21:33 +00001368 error("Invalid boolean value read.");
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001369 Result = ConstantBool::get(Val == 1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001370 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001371 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001372 }
1373
1374 case Type::UByteTyID: // Unsigned integer types...
1375 case Type::UShortTyID:
1376 case Type::UIntTyID: {
1377 unsigned Val = read_vbr_uint();
Reid Spencerb83eb642006-10-20 07:07:24 +00001378 if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
Reid Spencer24399722004-07-09 22:21:33 +00001379 error("Invalid unsigned byte/short/int read.");
Reid Spencerb83eb642006-10-20 07:07:24 +00001380 Result = ConstantInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001381 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001382 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001383 }
1384
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001385 case Type::ULongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +00001386 Result = ConstantInt::get(Ty, read_vbr_uint64());
Reid Spencer04cde2c2004-07-04 11:33:49 +00001387 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001388 break;
1389
Reid Spencer060d25d2004-06-29 23:29:38 +00001390 case Type::SByteTyID: // Signed integer types...
1391 case Type::ShortTyID:
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001392 case Type::IntTyID:
1393 case Type::LongTyID: {
Reid Spencer060d25d2004-06-29 23:29:38 +00001394 int64_t Val = read_vbr_int64();
Reid Spencerb83eb642006-10-20 07:07:24 +00001395 if (!ConstantInt::isValueValidForType(Ty, Val))
Reid Spencer24399722004-07-09 22:21:33 +00001396 error("Invalid signed byte/short/int/long read.");
Reid Spencerb83eb642006-10-20 07:07:24 +00001397 Result = ConstantInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001398 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001399 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001400 }
1401
1402 case Type::FloatTyID: {
Reid Spencer46b002c2004-07-11 17:28:43 +00001403 float Val;
1404 read_float(Val);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001405 Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001406 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001407 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001408 }
1409
1410 case Type::DoubleTyID: {
1411 double Val;
Reid Spencer46b002c2004-07-11 17:28:43 +00001412 read_double(Val);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001413 Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001414 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001415 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001416 }
1417
Reid Spencer060d25d2004-06-29 23:29:38 +00001418 case Type::ArrayTyID: {
1419 const ArrayType *AT = cast<ArrayType>(Ty);
1420 unsigned NumElements = AT->getNumElements();
1421 unsigned TypeSlot = getTypeSlot(AT->getElementType());
1422 std::vector<Constant*> Elements;
1423 Elements.reserve(NumElements);
1424 while (NumElements--) // Read all of the elements of the constant.
1425 Elements.push_back(getConstantValue(TypeSlot,
1426 read_vbr_uint()));
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001427 Result = ConstantArray::get(AT, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001428 if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001429 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001430 }
1431
1432 case Type::StructTyID: {
1433 const StructType *ST = cast<StructType>(Ty);
1434
1435 std::vector<Constant *> Elements;
1436 Elements.reserve(ST->getNumElements());
1437 for (unsigned i = 0; i != ST->getNumElements(); ++i)
1438 Elements.push_back(getConstantValue(ST->getElementType(i),
1439 read_vbr_uint()));
1440
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001441 Result = ConstantStruct::get(ST, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001442 if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001443 break;
Misha Brukman8a96c532005-04-21 21:44:41 +00001444 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001445
Brian Gaeke715c90b2004-08-20 06:00:58 +00001446 case Type::PackedTyID: {
1447 const PackedType *PT = cast<PackedType>(Ty);
1448 unsigned NumElements = PT->getNumElements();
1449 unsigned TypeSlot = getTypeSlot(PT->getElementType());
1450 std::vector<Constant*> Elements;
1451 Elements.reserve(NumElements);
1452 while (NumElements--) // Read all of the elements of the constant.
1453 Elements.push_back(getConstantValue(TypeSlot,
1454 read_vbr_uint()));
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001455 Result = ConstantPacked::get(PT, Elements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001456 if (Handler) Handler->handleConstantPacked(PT, Elements, TypeSlot, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001457 break;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001458 }
1459
Chris Lattner638c3812004-11-19 16:24:05 +00001460 case Type::PointerTyID: { // ConstantPointerRef value (backwards compat).
Reid Spencer060d25d2004-06-29 23:29:38 +00001461 const PointerType *PT = cast<PointerType>(Ty);
1462 unsigned Slot = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001463
Reid Spencer060d25d2004-06-29 23:29:38 +00001464 // Check to see if we have already read this global variable...
1465 Value *Val = getValue(TypeID, Slot, false);
Reid Spencer060d25d2004-06-29 23:29:38 +00001466 if (Val) {
Chris Lattnerbcb11cf2004-07-27 02:34:49 +00001467 GlobalValue *GV = dyn_cast<GlobalValue>(Val);
1468 if (!GV) error("GlobalValue not in ValueTable!");
1469 if (Handler) Handler->handleConstantPointer(PT, Slot, GV);
1470 return GV;
Reid Spencer060d25d2004-06-29 23:29:38 +00001471 } else {
Reid Spencer24399722004-07-09 22:21:33 +00001472 error("Forward references are not allowed here.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001473 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001474 }
1475
1476 default:
Reid Spencer24399722004-07-09 22:21:33 +00001477 error("Don't know how to deserialize constant value of type '" +
Reid Spencer060d25d2004-06-29 23:29:38 +00001478 Ty->getDescription());
1479 break;
1480 }
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001481
1482 // Check that we didn't read a null constant if they are implicit for this
1483 // type plane. Do not do this check for constantexprs, as they may be folded
1484 // to a null value in a way that isn't predicted when a .bc file is initially
1485 // produced.
1486 assert((!isa<Constant>(Result) || !cast<Constant>(Result)->isNullValue()) ||
1487 !hasImplicitNull(TypeID) &&
1488 "Cannot read null values from bytecode!");
1489 return Result;
Reid Spencer060d25d2004-06-29 23:29:38 +00001490}
1491
Misha Brukman8a96c532005-04-21 21:44:41 +00001492/// Resolve references for constants. This function resolves the forward
1493/// referenced constants in the ConstantFwdRefs map. It uses the
Reid Spencer04cde2c2004-07-04 11:33:49 +00001494/// replaceAllUsesWith method of Value class to substitute the placeholder
1495/// instance with the actual instance.
Chris Lattner389bd042004-12-09 06:19:44 +00001496void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Typ,
1497 unsigned Slot) {
Chris Lattner29b789b2003-11-19 17:27:18 +00001498 ConstantRefsType::iterator I =
Chris Lattner389bd042004-12-09 06:19:44 +00001499 ConstantFwdRefs.find(std::make_pair(Typ, Slot));
Chris Lattner29b789b2003-11-19 17:27:18 +00001500 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +00001501
Chris Lattner29b789b2003-11-19 17:27:18 +00001502 Value *PH = I->second; // Get the placeholder...
1503 PH->replaceAllUsesWith(NewV);
1504 delete PH; // Delete the old placeholder
1505 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +00001506}
1507
Reid Spencer04cde2c2004-07-04 11:33:49 +00001508/// Parse the constant strings section.
Reid Spencer060d25d2004-06-29 23:29:38 +00001509void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){
1510 for (; NumEntries; --NumEntries) {
Reid Spencerd798a512006-11-14 04:47:22 +00001511 unsigned Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001512 const Type *Ty = getType(Typ);
1513 if (!isa<ArrayType>(Ty))
Reid Spencer24399722004-07-09 22:21:33 +00001514 error("String constant data invalid!");
Misha Brukman8a96c532005-04-21 21:44:41 +00001515
Reid Spencer060d25d2004-06-29 23:29:38 +00001516 const ArrayType *ATy = cast<ArrayType>(Ty);
1517 if (ATy->getElementType() != Type::SByteTy &&
1518 ATy->getElementType() != Type::UByteTy)
Reid Spencer24399722004-07-09 22:21:33 +00001519 error("String constant data invalid!");
Misha Brukman8a96c532005-04-21 21:44:41 +00001520
Reid Spencer060d25d2004-06-29 23:29:38 +00001521 // Read character data. The type tells us how long the string is.
Misha Brukman8a96c532005-04-21 21:44:41 +00001522 char *Data = reinterpret_cast<char *>(alloca(ATy->getNumElements()));
Reid Spencer060d25d2004-06-29 23:29:38 +00001523 read_data(Data, Data+ATy->getNumElements());
Chris Lattner52e20b02003-03-19 20:54:26 +00001524
Reid Spencer060d25d2004-06-29 23:29:38 +00001525 std::vector<Constant*> Elements(ATy->getNumElements());
Reid Spencerb83eb642006-10-20 07:07:24 +00001526 const Type* ElemType = ATy->getElementType();
1527 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
1528 Elements[i] = ConstantInt::get(ElemType, (unsigned char)Data[i]);
Misha Brukman12c29d12003-09-22 23:38:23 +00001529
Reid Spencer060d25d2004-06-29 23:29:38 +00001530 // Create the constant, inserting it as needed.
1531 Constant *C = ConstantArray::get(ATy, Elements);
1532 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner389bd042004-12-09 06:19:44 +00001533 ResolveReferencesToConstant(C, Typ, Slot);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001534 if (Handler) Handler->handleConstantString(cast<ConstantArray>(C));
Reid Spencer060d25d2004-06-29 23:29:38 +00001535 }
Misha Brukman12c29d12003-09-22 23:38:23 +00001536}
1537
Reid Spencer04cde2c2004-07-04 11:33:49 +00001538/// Parse the constant pool.
Misha Brukman8a96c532005-04-21 21:44:41 +00001539void BytecodeReader::ParseConstantPool(ValueTable &Tab,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001540 TypeListTy &TypeTab,
Reid Spencer46b002c2004-07-11 17:28:43 +00001541 bool isFunction) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001542 if (Handler) Handler->handleGlobalConstantsBegin();
1543
1544 /// In LLVM 1.3 Type does not derive from Value so the types
1545 /// do not occupy a plane. Consequently, we read the types
1546 /// first in the constant pool.
Reid Spencerd798a512006-11-14 04:47:22 +00001547 if (isFunction) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001548 unsigned NumEntries = read_vbr_uint();
Reid Spencer46b002c2004-07-11 17:28:43 +00001549 ParseTypes(TypeTab, NumEntries);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001550 }
1551
Reid Spencer46b002c2004-07-11 17:28:43 +00001552 while (moreInBlock()) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001553 unsigned NumEntries = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00001554 unsigned Typ = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001555
Reid Spencerd798a512006-11-14 04:47:22 +00001556 if (Typ == Type::VoidTyID) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001557 /// Use of Type::VoidTyID is a misnomer. It actually means
1558 /// that the following plane is constant strings
Reid Spencer060d25d2004-06-29 23:29:38 +00001559 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
1560 ParseStringConstants(NumEntries, Tab);
1561 } else {
1562 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner3bc5a602006-01-25 23:08:15 +00001563 Value *V = ParseConstantPoolValue(Typ);
1564 assert(V && "ParseConstantPoolValue returned NULL!");
1565 unsigned Slot = insertValue(V, Typ, Tab);
Chris Lattner29b789b2003-11-19 17:27:18 +00001566
Reid Spencer060d25d2004-06-29 23:29:38 +00001567 // If we are reading a function constant table, make sure that we adjust
1568 // the slot number to be the real global constant number.
1569 //
1570 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
1571 ModuleValues[Typ])
1572 Slot += ModuleValues[Typ]->size();
Chris Lattner3bc5a602006-01-25 23:08:15 +00001573 if (Constant *C = dyn_cast<Constant>(V))
1574 ResolveReferencesToConstant(C, Typ, Slot);
Reid Spencer060d25d2004-06-29 23:29:38 +00001575 }
1576 }
1577 }
Chris Lattner02dce162004-12-04 05:28:27 +00001578
1579 // After we have finished parsing the constant pool, we had better not have
1580 // any dangling references left.
Reid Spencer3c391272004-12-04 22:19:53 +00001581 if (!ConstantFwdRefs.empty()) {
Reid Spencer3c391272004-12-04 22:19:53 +00001582 ConstantRefsType::const_iterator I = ConstantFwdRefs.begin();
Reid Spencer3c391272004-12-04 22:19:53 +00001583 Constant* missingConst = I->second;
Misha Brukman8a96c532005-04-21 21:44:41 +00001584 error(utostr(ConstantFwdRefs.size()) +
1585 " unresolved constant reference exist. First one is '" +
1586 missingConst->getName() + "' of type '" +
Chris Lattner389bd042004-12-09 06:19:44 +00001587 missingConst->getType()->getDescription() + "'.");
Reid Spencer3c391272004-12-04 22:19:53 +00001588 }
Chris Lattner02dce162004-12-04 05:28:27 +00001589
Reid Spencer060d25d2004-06-29 23:29:38 +00001590 checkPastBlockEnd("Constant Pool");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001591 if (Handler) Handler->handleGlobalConstantsEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001592}
Chris Lattner00950542001-06-06 20:29:01 +00001593
Reid Spencer04cde2c2004-07-04 11:33:49 +00001594/// Parse the contents of a function. Note that this function can be
1595/// called lazily by materializeFunction
1596/// @see materializeFunction
Reid Spencer46b002c2004-07-11 17:28:43 +00001597void BytecodeReader::ParseFunctionBody(Function* F) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001598
1599 unsigned FuncSize = BlockEnd - At;
Chris Lattnere3869c82003-04-16 21:16:05 +00001600 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
1601
Reid Spencer060d25d2004-06-29 23:29:38 +00001602 unsigned LinkageType = read_vbr_uint();
Chris Lattnerc08912f2004-01-14 16:44:44 +00001603 switch (LinkageType) {
1604 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1605 case 1: Linkage = GlobalValue::WeakLinkage; break;
1606 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1607 case 3: Linkage = GlobalValue::InternalLinkage; break;
1608 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001609 case 5: Linkage = GlobalValue::DLLImportLinkage; break;
1610 case 6: Linkage = GlobalValue::DLLExportLinkage; break;
1611 case 7: Linkage = GlobalValue::ExternalWeakLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001612 default:
Reid Spencer24399722004-07-09 22:21:33 +00001613 error("Invalid linkage type for Function.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001614 Linkage = GlobalValue::InternalLinkage;
1615 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001616 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +00001617
Reid Spencer46b002c2004-07-11 17:28:43 +00001618 F->setLinkage(Linkage);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001619 if (Handler) Handler->handleFunctionBegin(F,FuncSize);
Chris Lattner00950542001-06-06 20:29:01 +00001620
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001621 // Keep track of how many basic blocks we have read in...
1622 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +00001623 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001624
Reid Spencer060d25d2004-06-29 23:29:38 +00001625 BufPtr MyEnd = BlockEnd;
Reid Spencer46b002c2004-07-11 17:28:43 +00001626 while (At < MyEnd) {
Chris Lattner00950542001-06-06 20:29:01 +00001627 unsigned Type, Size;
Reid Spencer060d25d2004-06-29 23:29:38 +00001628 BufPtr OldAt = At;
1629 read_block(Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +00001630
1631 switch (Type) {
Reid Spencerad89bd62004-07-25 18:07:36 +00001632 case BytecodeFormat::ConstantPoolBlockID:
Chris Lattner89e02532004-01-18 21:08:15 +00001633 if (!InsertedArguments) {
1634 // Insert arguments into the value table before we parse the first basic
1635 // block in the function, but after we potentially read in the
1636 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001637 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001638 InsertedArguments = true;
1639 }
1640
Reid Spencer04cde2c2004-07-04 11:33:49 +00001641 ParseConstantPool(FunctionValues, FunctionTypes, true);
Chris Lattner00950542001-06-06 20:29:01 +00001642 break;
1643
Reid Spencerad89bd62004-07-25 18:07:36 +00001644 case BytecodeFormat::CompactionTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00001645 ParseCompactionTable();
Chris Lattner89e02532004-01-18 21:08:15 +00001646 break;
1647
Reid Spencerad89bd62004-07-25 18:07:36 +00001648 case BytecodeFormat::InstructionListBlockID: {
Chris Lattner89e02532004-01-18 21:08:15 +00001649 // Insert arguments into the value table before we parse the instruction
1650 // list for the function, but after we potentially read in the compaction
1651 // table.
1652 if (!InsertedArguments) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001653 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00001654 InsertedArguments = true;
1655 }
1656
Misha Brukman8a96c532005-04-21 21:44:41 +00001657 if (BlockNum)
Reid Spencer24399722004-07-09 22:21:33 +00001658 error("Already parsed basic blocks!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001659 BlockNum = ParseInstructionList(F);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001660 break;
1661 }
1662
Reid Spencerad89bd62004-07-25 18:07:36 +00001663 case BytecodeFormat::SymbolTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00001664 ParseSymbolTable(F, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00001665 break;
1666
1667 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00001668 At += Size;
Misha Brukman8a96c532005-04-21 21:44:41 +00001669 if (OldAt > At)
Reid Spencer24399722004-07-09 22:21:33 +00001670 error("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +00001671 break;
1672 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001673 BlockEnd = MyEnd;
Chris Lattner00950542001-06-06 20:29:01 +00001674 }
1675
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001676 // Make sure there were no references to non-existant basic blocks.
1677 if (BlockNum != ParsedBasicBlocks.size())
Reid Spencer24399722004-07-09 22:21:33 +00001678 error("Illegal basic block operand reference");
Reid Spencer060d25d2004-06-29 23:29:38 +00001679
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001680 ParsedBasicBlocks.clear();
1681
Chris Lattner97330cf2003-10-09 23:10:14 +00001682 // Resolve forward references. Replace any uses of a forward reference value
1683 // with the real value.
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001684 while (!ForwardReferences.empty()) {
Chris Lattnerc4d69162004-12-09 04:51:50 +00001685 std::map<std::pair<unsigned,unsigned>, Value*>::iterator
1686 I = ForwardReferences.begin();
1687 Value *V = getValue(I->first.first, I->first.second, false);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001688 Value *PlaceHolder = I->second;
Chris Lattnerc4d69162004-12-09 04:51:50 +00001689 PlaceHolder->replaceAllUsesWith(V);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001690 ForwardReferences.erase(I);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00001691 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +00001692 }
Chris Lattner00950542001-06-06 20:29:01 +00001693
Misha Brukman12c29d12003-09-22 23:38:23 +00001694 // Clear out function-level types...
Reid Spencer060d25d2004-06-29 23:29:38 +00001695 FunctionTypes.clear();
1696 CompactionTypes.clear();
1697 CompactionValues.clear();
1698 freeTable(FunctionValues);
1699
Reid Spencer04cde2c2004-07-04 11:33:49 +00001700 if (Handler) Handler->handleFunctionEnd(F);
Chris Lattner00950542001-06-06 20:29:01 +00001701}
1702
Reid Spencer04cde2c2004-07-04 11:33:49 +00001703/// This function parses LLVM functions lazily. It obtains the type of the
1704/// function and records where the body of the function is in the bytecode
Misha Brukman8a96c532005-04-21 21:44:41 +00001705/// buffer. The caller can then use the ParseNextFunction and
Reid Spencer04cde2c2004-07-04 11:33:49 +00001706/// ParseAllFunctionBodies to get handler events for the functions.
Reid Spencer060d25d2004-06-29 23:29:38 +00001707void BytecodeReader::ParseFunctionLazily() {
1708 if (FunctionSignatureList.empty())
Reid Spencer24399722004-07-09 22:21:33 +00001709 error("FunctionSignatureList empty!");
Chris Lattner89e02532004-01-18 21:08:15 +00001710
Reid Spencer060d25d2004-06-29 23:29:38 +00001711 Function *Func = FunctionSignatureList.back();
1712 FunctionSignatureList.pop_back();
Chris Lattner24102432004-01-18 22:35:34 +00001713
Reid Spencer060d25d2004-06-29 23:29:38 +00001714 // Save the information for future reading of the function
1715 LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd);
Chris Lattner89e02532004-01-18 21:08:15 +00001716
Misha Brukmana3e6ad62004-11-14 21:02:55 +00001717 // This function has a body but it's not loaded so it appears `External'.
1718 // Mark it as a `Ghost' instead to notify the users that it has a body.
1719 Func->setLinkage(GlobalValue::GhostLinkage);
1720
Reid Spencer060d25d2004-06-29 23:29:38 +00001721 // Pretend we've `parsed' this function
1722 At = BlockEnd;
1723}
Chris Lattner89e02532004-01-18 21:08:15 +00001724
Misha Brukman8a96c532005-04-21 21:44:41 +00001725/// The ParserFunction method lazily parses one function. Use this method to
1726/// casue the parser to parse a specific function in the module. Note that
1727/// this will remove the function from what is to be included by
Reid Spencer04cde2c2004-07-04 11:33:49 +00001728/// ParseAllFunctionBodies.
1729/// @see ParseAllFunctionBodies
1730/// @see ParseBytecode
Reid Spencer99655e12006-08-25 19:54:53 +00001731bool BytecodeReader::ParseFunction(Function* Func, std::string* ErrMsg) {
1732
1733 if (setjmp(context))
1734 return true;
1735
Reid Spencer060d25d2004-06-29 23:29:38 +00001736 // Find {start, end} pointers and slot in the map. If not there, we're done.
1737 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
Chris Lattner89e02532004-01-18 21:08:15 +00001738
Reid Spencer060d25d2004-06-29 23:29:38 +00001739 // Make sure we found it
Reid Spencer46b002c2004-07-11 17:28:43 +00001740 if (Fi == LazyFunctionLoadMap.end()) {
Reid Spencer24399722004-07-09 22:21:33 +00001741 error("Unrecognized function of type " + Func->getType()->getDescription());
Reid Spencer99655e12006-08-25 19:54:53 +00001742 return true;
Chris Lattner89e02532004-01-18 21:08:15 +00001743 }
1744
Reid Spencer060d25d2004-06-29 23:29:38 +00001745 BlockStart = At = Fi->second.Buf;
1746 BlockEnd = Fi->second.EndBuf;
Reid Spencer24399722004-07-09 22:21:33 +00001747 assert(Fi->first == Func && "Found wrong function?");
Reid Spencer060d25d2004-06-29 23:29:38 +00001748
1749 LazyFunctionLoadMap.erase(Fi);
1750
Reid Spencer46b002c2004-07-11 17:28:43 +00001751 this->ParseFunctionBody(Func);
Reid Spencer99655e12006-08-25 19:54:53 +00001752 return false;
Chris Lattner89e02532004-01-18 21:08:15 +00001753}
1754
Reid Spencer04cde2c2004-07-04 11:33:49 +00001755/// The ParseAllFunctionBodies method parses through all the previously
1756/// unparsed functions in the bytecode file. If you want to completely parse
1757/// a bytecode file, this method should be called after Parsebytecode because
1758/// Parsebytecode only records the locations in the bytecode file of where
1759/// the function definitions are located. This function uses that information
1760/// to materialize the functions.
1761/// @see ParseBytecode
Reid Spencer99655e12006-08-25 19:54:53 +00001762bool BytecodeReader::ParseAllFunctionBodies(std::string* ErrMsg) {
1763 if (setjmp(context))
1764 return true;
1765
Reid Spencer060d25d2004-06-29 23:29:38 +00001766 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
1767 LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
Chris Lattner89e02532004-01-18 21:08:15 +00001768
Reid Spencer46b002c2004-07-11 17:28:43 +00001769 while (Fi != Fe) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001770 Function* Func = Fi->first;
1771 BlockStart = At = Fi->second.Buf;
1772 BlockEnd = Fi->second.EndBuf;
Chris Lattnerb52f1c22005-02-13 17:48:18 +00001773 ParseFunctionBody(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00001774 ++Fi;
1775 }
Chris Lattnerb52f1c22005-02-13 17:48:18 +00001776 LazyFunctionLoadMap.clear();
Reid Spencer99655e12006-08-25 19:54:53 +00001777 return false;
Reid Spencer060d25d2004-06-29 23:29:38 +00001778}
Chris Lattner89e02532004-01-18 21:08:15 +00001779
Reid Spencer04cde2c2004-07-04 11:33:49 +00001780/// Parse the global type list
Reid Spencer060d25d2004-06-29 23:29:38 +00001781void BytecodeReader::ParseGlobalTypes() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001782 // Read the number of types
1783 unsigned NumEntries = read_vbr_uint();
Reid Spencer46b002c2004-07-11 17:28:43 +00001784 ParseTypes(ModuleTypes, NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001785}
1786
Reid Spencer04cde2c2004-07-04 11:33:49 +00001787/// Parse the Global info (types, global vars, constants)
Reid Spencer060d25d2004-06-29 23:29:38 +00001788void BytecodeReader::ParseModuleGlobalInfo() {
1789
Reid Spencer04cde2c2004-07-04 11:33:49 +00001790 if (Handler) Handler->handleModuleGlobalsBegin();
Chris Lattner00950542001-06-06 20:29:01 +00001791
Chris Lattner404cddf2005-11-12 01:33:40 +00001792 // SectionID - If a global has an explicit section specified, this map
1793 // remembers the ID until we can translate it into a string.
1794 std::map<GlobalValue*, unsigned> SectionID;
1795
Chris Lattner70cc3392001-09-10 07:58:01 +00001796 // Read global variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00001797 unsigned VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001798 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattner9dd87702004-04-03 23:43:42 +00001799 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
1800 // Linkage, bit4+ = slot#
1801 unsigned SlotNo = VarType >> 5;
1802 unsigned LinkageID = (VarType >> 2) & 7;
Reid Spencer060d25d2004-06-29 23:29:38 +00001803 bool isConstant = VarType & 1;
Chris Lattnerce5e04e2005-11-06 08:23:17 +00001804 bool hasInitializer = (VarType & 2) != 0;
Chris Lattner8eb52dd2005-11-06 07:11:04 +00001805 unsigned Alignment = 0;
Chris Lattner404cddf2005-11-12 01:33:40 +00001806 unsigned GlobalSectionID = 0;
Chris Lattner8eb52dd2005-11-06 07:11:04 +00001807
1808 // An extension word is present when linkage = 3 (internal) and hasinit = 0.
1809 if (LinkageID == 3 && !hasInitializer) {
1810 unsigned ExtWord = read_vbr_uint();
1811 // The extension word has this format: bit 0 = has initializer, bit 1-3 =
1812 // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
1813 hasInitializer = ExtWord & 1;
1814 LinkageID = (ExtWord >> 1) & 7;
1815 Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
Chris Lattner404cddf2005-11-12 01:33:40 +00001816
1817 if (ExtWord & (1 << 9)) // Has a section ID.
1818 GlobalSectionID = read_vbr_uint();
Chris Lattner8eb52dd2005-11-06 07:11:04 +00001819 }
Chris Lattnere3869c82003-04-16 21:16:05 +00001820
Chris Lattnerce5e04e2005-11-06 08:23:17 +00001821 GlobalValue::LinkageTypes Linkage;
Chris Lattnerc08912f2004-01-14 16:44:44 +00001822 switch (LinkageID) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00001823 case 0: Linkage = GlobalValue::ExternalLinkage; break;
1824 case 1: Linkage = GlobalValue::WeakLinkage; break;
1825 case 2: Linkage = GlobalValue::AppendingLinkage; break;
1826 case 3: Linkage = GlobalValue::InternalLinkage; break;
1827 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001828 case 5: Linkage = GlobalValue::DLLImportLinkage; break;
1829 case 6: Linkage = GlobalValue::DLLExportLinkage; break;
1830 case 7: Linkage = GlobalValue::ExternalWeakLinkage; break;
Misha Brukman8a96c532005-04-21 21:44:41 +00001831 default:
Reid Spencer24399722004-07-09 22:21:33 +00001832 error("Unknown linkage type: " + utostr(LinkageID));
Reid Spencer060d25d2004-06-29 23:29:38 +00001833 Linkage = GlobalValue::InternalLinkage;
1834 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00001835 }
1836
1837 const Type *Ty = getType(SlotNo);
Chris Lattnere73bd452005-11-06 07:43:39 +00001838 if (!Ty)
Reid Spencer24399722004-07-09 22:21:33 +00001839 error("Global has no type! SlotNo=" + utostr(SlotNo));
Reid Spencer060d25d2004-06-29 23:29:38 +00001840
Chris Lattnere73bd452005-11-06 07:43:39 +00001841 if (!isa<PointerType>(Ty))
Reid Spencer24399722004-07-09 22:21:33 +00001842 error("Global not a pointer type! Ty= " + Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +00001843
Chris Lattner52e20b02003-03-19 20:54:26 +00001844 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +00001845
Chris Lattner70cc3392001-09-10 07:58:01 +00001846 // Create the global variable...
Reid Spencer060d25d2004-06-29 23:29:38 +00001847 GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +00001848 0, "", TheModule);
Chris Lattner8eb52dd2005-11-06 07:11:04 +00001849 GV->setAlignment(Alignment);
Chris Lattner29b789b2003-11-19 17:27:18 +00001850 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +00001851
Chris Lattner404cddf2005-11-12 01:33:40 +00001852 if (GlobalSectionID != 0)
1853 SectionID[GV] = GlobalSectionID;
1854
Reid Spencer060d25d2004-06-29 23:29:38 +00001855 unsigned initSlot = 0;
Misha Brukman8a96c532005-04-21 21:44:41 +00001856 if (hasInitializer) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001857 initSlot = read_vbr_uint();
1858 GlobalInits.push_back(std::make_pair(GV, initSlot));
1859 }
1860
1861 // Notify handler about the global value.
Chris Lattner4a242b32004-10-14 01:39:18 +00001862 if (Handler)
1863 Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot);
Reid Spencer060d25d2004-06-29 23:29:38 +00001864
1865 // Get next item
1866 VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00001867 }
1868
Chris Lattner52e20b02003-03-19 20:54:26 +00001869 // Read the function objects for all of the functions that are coming
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001870 unsigned FnSignature = read_vbr_uint();
Reid Spencer24399722004-07-09 22:21:33 +00001871
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001872 // List is terminated by VoidTy.
Chris Lattnere73bd452005-11-06 07:43:39 +00001873 while (((FnSignature & (~0U >> 1)) >> 5) != Type::VoidTyID) {
1874 const Type *Ty = getType((FnSignature & (~0U >> 1)) >> 5);
Chris Lattner927b1852003-10-09 20:22:47 +00001875 if (!isa<PointerType>(Ty) ||
Reid Spencer060d25d2004-06-29 23:29:38 +00001876 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman8a96c532005-04-21 21:44:41 +00001877 error("Function not a pointer to function type! Ty = " +
Reid Spencer46b002c2004-07-11 17:28:43 +00001878 Ty->getDescription());
Reid Spencer060d25d2004-06-29 23:29:38 +00001879 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +00001880
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00001881 // We create functions by passing the underlying FunctionType to create...
Misha Brukman8a96c532005-04-21 21:44:41 +00001882 const FunctionType* FTy =
Reid Spencer060d25d2004-06-29 23:29:38 +00001883 cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +00001884
Chris Lattner18549c22004-11-15 21:43:03 +00001885 // Insert the place holder.
Chris Lattner404cddf2005-11-12 01:33:40 +00001886 Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001887 "", TheModule);
Reid Spencere1e96c02006-01-19 07:02:16 +00001888
Chris Lattnere73bd452005-11-06 07:43:39 +00001889 insertValue(Func, (FnSignature & (~0U >> 1)) >> 5, ModuleValues);
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001890
1891 // Flags are not used yet.
Chris Lattner97fbc502004-11-15 22:38:52 +00001892 unsigned Flags = FnSignature & 31;
Chris Lattner00950542001-06-06 20:29:01 +00001893
Chris Lattner97fbc502004-11-15 22:38:52 +00001894 // Save this for later so we know type of lazily instantiated functions.
1895 // Note that known-external functions do not have FunctionInfo blocks, so we
1896 // do not add them to the FunctionSignatureList.
1897 if ((Flags & (1 << 4)) == 0)
1898 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +00001899
Chris Lattnere73bd452005-11-06 07:43:39 +00001900 // Get the calling convention from the low bits.
1901 unsigned CC = Flags & 15;
1902 unsigned Alignment = 0;
1903 if (FnSignature & (1 << 31)) { // Has extension word?
1904 unsigned ExtWord = read_vbr_uint();
1905 Alignment = (1 << (ExtWord & 31)) >> 1;
1906 CC |= ((ExtWord >> 5) & 15) << 4;
Chris Lattner404cddf2005-11-12 01:33:40 +00001907
1908 if (ExtWord & (1 << 10)) // Has a section ID.
1909 SectionID[Func] = read_vbr_uint();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001910
1911 // Parse external declaration linkage
1912 switch ((ExtWord >> 11) & 3) {
1913 case 0: break;
1914 case 1: Func->setLinkage(Function::DLLImportLinkage); break;
1915 case 2: Func->setLinkage(Function::ExternalWeakLinkage); break;
1916 default: assert(0 && "Unsupported external linkage");
1917 }
Chris Lattnere73bd452005-11-06 07:43:39 +00001918 }
1919
Chris Lattner54b369e2005-11-06 07:46:13 +00001920 Func->setCallingConv(CC-1);
Chris Lattnere73bd452005-11-06 07:43:39 +00001921 Func->setAlignment(Alignment);
Chris Lattner479ffeb2005-05-06 20:42:57 +00001922
Reid Spencer04cde2c2004-07-04 11:33:49 +00001923 if (Handler) Handler->handleFunctionDeclaration(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00001924
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001925 // Get the next function signature.
1926 FnSignature = read_vbr_uint();
Chris Lattner00950542001-06-06 20:29:01 +00001927 }
1928
Misha Brukman8a96c532005-04-21 21:44:41 +00001929 // Now that the function signature list is set up, reverse it so that we can
Chris Lattner74734132002-08-17 22:01:27 +00001930 // remove elements efficiently from the back of the vector.
1931 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +00001932
Chris Lattner404cddf2005-11-12 01:33:40 +00001933 /// SectionNames - This contains the list of section names encoded in the
1934 /// moduleinfoblock. Functions and globals with an explicit section index
1935 /// into this to get their section name.
1936 std::vector<std::string> SectionNames;
1937
Reid Spencerd798a512006-11-14 04:47:22 +00001938 // Read in the dependent library information.
1939 unsigned num_dep_libs = read_vbr_uint();
1940 std::string dep_lib;
1941 while (num_dep_libs--) {
1942 dep_lib = read_str();
1943 TheModule->addLibrary(dep_lib);
Reid Spencer5b472d92004-08-21 20:49:23 +00001944 if (Handler)
Reid Spencerd798a512006-11-14 04:47:22 +00001945 Handler->handleDependentLibrary(dep_lib);
Reid Spencerad89bd62004-07-25 18:07:36 +00001946 }
1947
Reid Spencerd798a512006-11-14 04:47:22 +00001948 // Read target triple and place into the module.
1949 std::string triple = read_str();
1950 TheModule->setTargetTriple(triple);
1951 if (Handler)
1952 Handler->handleTargetTriple(triple);
1953
1954 if (At != BlockEnd) {
1955 // If the file has section info in it, read the section names now.
1956 unsigned NumSections = read_vbr_uint();
1957 while (NumSections--)
1958 SectionNames.push_back(read_str());
1959 }
1960
1961 // If the file has module-level inline asm, read it now.
1962 if (At != BlockEnd)
1963 TheModule->setModuleInlineAsm(read_str());
1964
Chris Lattner404cddf2005-11-12 01:33:40 +00001965 // If any globals are in specified sections, assign them now.
1966 for (std::map<GlobalValue*, unsigned>::iterator I = SectionID.begin(), E =
1967 SectionID.end(); I != E; ++I)
1968 if (I->second) {
1969 if (I->second > SectionID.size())
1970 error("SectionID out of range for global!");
1971 I->first->setSection(SectionNames[I->second-1]);
1972 }
Reid Spencerad89bd62004-07-25 18:07:36 +00001973
Chris Lattner00950542001-06-06 20:29:01 +00001974 // This is for future proofing... in the future extra fields may be added that
1975 // we don't understand, so we transparently ignore them.
1976 //
Reid Spencer060d25d2004-06-29 23:29:38 +00001977 At = BlockEnd;
1978
Reid Spencer04cde2c2004-07-04 11:33:49 +00001979 if (Handler) Handler->handleModuleGlobalsEnd();
Chris Lattner00950542001-06-06 20:29:01 +00001980}
1981
Reid Spencer04cde2c2004-07-04 11:33:49 +00001982/// Parse the version information and decode it by setting flags on the
1983/// Reader that enable backward compatibility of the reader.
Reid Spencer060d25d2004-06-29 23:29:38 +00001984void BytecodeReader::ParseVersionInfo() {
1985 unsigned Version = read_vbr_uint();
Chris Lattner036b8aa2003-03-06 17:55:45 +00001986
1987 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001988 Module::Endianness Endianness;
1989 Module::PointerSize PointerSize;
1990 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
1991 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
1992
1993 bool hasNoEndianness = Version & 4;
1994 bool hasNoPointerSize = Version & 8;
Misha Brukman8a96c532005-04-21 21:44:41 +00001995
Chris Lattnerd445c6b2003-08-24 13:47:36 +00001996 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +00001997
Reid Spencer3795ad12006-12-03 05:47:10 +00001998 // We don't provide backwards compatibility in the Reader any more. To
1999 // upgrade, the user should use llvm-upgrade.
2000 if (RevisionNum < 7)
2001 error("Bytecode formats < 7 are no longer supported. Use llvm-upgrade.");
Chris Lattner036b8aa2003-03-06 17:55:45 +00002002
Chris Lattnerd445c6b2003-08-24 13:47:36 +00002003 if (hasNoEndianness) Endianness = Module::AnyEndianness;
2004 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +00002005
Brian Gaekefe2102b2004-07-14 20:33:13 +00002006 TheModule->setEndianness(Endianness);
2007 TheModule->setPointerSize(PointerSize);
2008
Reid Spencer46b002c2004-07-11 17:28:43 +00002009 if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +00002010}
2011
Reid Spencer04cde2c2004-07-04 11:33:49 +00002012/// Parse a whole module.
Reid Spencer060d25d2004-06-29 23:29:38 +00002013void BytecodeReader::ParseModule() {
Chris Lattner00950542001-06-06 20:29:01 +00002014 unsigned Type, Size;
Chris Lattner00950542001-06-06 20:29:01 +00002015
Reid Spencer060d25d2004-06-29 23:29:38 +00002016 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +00002017
2018 // Read into instance variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00002019 ParseVersionInfo();
Chris Lattner00950542001-06-06 20:29:01 +00002020
Reid Spencer060d25d2004-06-29 23:29:38 +00002021 bool SeenModuleGlobalInfo = false;
2022 bool SeenGlobalTypePlane = false;
2023 BufPtr MyEnd = BlockEnd;
2024 while (At < MyEnd) {
2025 BufPtr OldAt = At;
2026 read_block(Type, Size);
2027
Chris Lattner00950542001-06-06 20:29:01 +00002028 switch (Type) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002029
Reid Spencerad89bd62004-07-25 18:07:36 +00002030 case BytecodeFormat::GlobalTypePlaneBlockID:
Reid Spencer46b002c2004-07-11 17:28:43 +00002031 if (SeenGlobalTypePlane)
Reid Spencer24399722004-07-09 22:21:33 +00002032 error("Two GlobalTypePlane Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002033
Reid Spencer5b472d92004-08-21 20:49:23 +00002034 if (Size > 0)
2035 ParseGlobalTypes();
Reid Spencer060d25d2004-06-29 23:29:38 +00002036 SeenGlobalTypePlane = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00002037 break;
2038
Misha Brukman8a96c532005-04-21 21:44:41 +00002039 case BytecodeFormat::ModuleGlobalInfoBlockID:
Reid Spencer46b002c2004-07-11 17:28:43 +00002040 if (SeenModuleGlobalInfo)
Reid Spencer24399722004-07-09 22:21:33 +00002041 error("Two ModuleGlobalInfo Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002042 ParseModuleGlobalInfo();
2043 SeenModuleGlobalInfo = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00002044 break;
2045
Reid Spencerad89bd62004-07-25 18:07:36 +00002046 case BytecodeFormat::ConstantPoolBlockID:
Reid Spencer04cde2c2004-07-04 11:33:49 +00002047 ParseConstantPool(ModuleValues, ModuleTypes,false);
Chris Lattner00950542001-06-06 20:29:01 +00002048 break;
2049
Reid Spencerad89bd62004-07-25 18:07:36 +00002050 case BytecodeFormat::FunctionBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002051 ParseFunctionLazily();
Chris Lattner00950542001-06-06 20:29:01 +00002052 break;
Chris Lattner00950542001-06-06 20:29:01 +00002053
Reid Spencerad89bd62004-07-25 18:07:36 +00002054 case BytecodeFormat::SymbolTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002055 ParseSymbolTable(0, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00002056 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00002057
Chris Lattner00950542001-06-06 20:29:01 +00002058 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00002059 At += Size;
2060 if (OldAt > At) {
Reid Spencer46b002c2004-07-11 17:28:43 +00002061 error("Unexpected Block of Type #" + utostr(Type) + " encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002062 }
Chris Lattner00950542001-06-06 20:29:01 +00002063 break;
2064 }
Reid Spencer060d25d2004-06-29 23:29:38 +00002065 BlockEnd = MyEnd;
Chris Lattner00950542001-06-06 20:29:01 +00002066 }
2067
Chris Lattner52e20b02003-03-19 20:54:26 +00002068 // After the module constant pool has been read, we can safely initialize
2069 // global variables...
2070 while (!GlobalInits.empty()) {
2071 GlobalVariable *GV = GlobalInits.back().first;
2072 unsigned Slot = GlobalInits.back().second;
2073 GlobalInits.pop_back();
2074
2075 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +00002076 // FIXME: Preserve this type ID!
Reid Spencer060d25d2004-06-29 23:29:38 +00002077
2078 const llvm::PointerType* GVType = GV->getType();
2079 unsigned TypeSlot = getTypeSlot(GVType->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +00002080 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman8a96c532005-04-21 21:44:41 +00002081 if (GV->hasInitializer())
Reid Spencer24399722004-07-09 22:21:33 +00002082 error("Global *already* has an initializer?!");
Reid Spencer04cde2c2004-07-04 11:33:49 +00002083 if (Handler) Handler->handleGlobalInitializer(GV,CV);
Chris Lattner93361992004-01-15 18:45:25 +00002084 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +00002085 } else
Reid Spencer24399722004-07-09 22:21:33 +00002086 error("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +00002087 }
2088
Chris Lattneraba5ff52005-05-05 20:57:00 +00002089 if (!ConstantFwdRefs.empty())
2090 error("Use of undefined constants in a module");
2091
Reid Spencer060d25d2004-06-29 23:29:38 +00002092 /// Make sure we pulled them all out. If we didn't then there's a declaration
2093 /// but a missing body. That's not allowed.
Misha Brukman12c29d12003-09-22 23:38:23 +00002094 if (!FunctionSignatureList.empty())
Reid Spencer24399722004-07-09 22:21:33 +00002095 error("Function declared, but bytecode stream ended before definition");
Chris Lattner00950542001-06-06 20:29:01 +00002096}
2097
Reid Spencer04cde2c2004-07-04 11:33:49 +00002098/// This function completely parses a bytecode buffer given by the \p Buf
2099/// and \p Length parameters.
Anton Korobeynikov7d515442006-09-01 20:35:17 +00002100bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
Reid Spencer233fe722006-08-22 16:09:19 +00002101 const std::string &ModuleID,
2102 std::string* ErrMsg) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +00002103
Reid Spencer233fe722006-08-22 16:09:19 +00002104 /// We handle errors by
2105 if (setjmp(context)) {
2106 // Cleanup after error
2107 if (Handler) Handler->handleError(ErrorMsg);
Reid Spencer060d25d2004-06-29 23:29:38 +00002108 freeState();
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00002109 delete TheModule;
2110 TheModule = 0;
Chris Lattner3bdad692004-11-15 21:55:33 +00002111 if (decompressedBlock != 0 ) {
Reid Spencer61aaf2e2004-11-14 21:59:21 +00002112 ::free(decompressedBlock);
Chris Lattner3bdad692004-11-15 21:55:33 +00002113 decompressedBlock = 0;
2114 }
Reid Spencer233fe722006-08-22 16:09:19 +00002115 // Set caller's error message, if requested
2116 if (ErrMsg)
2117 *ErrMsg = ErrorMsg;
2118 // Indicate an error occurred
2119 return true;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00002120 }
Reid Spencer233fe722006-08-22 16:09:19 +00002121
2122 RevisionNum = 0;
2123 At = MemStart = BlockStart = Buf;
2124 MemEnd = BlockEnd = Buf + Length;
2125
2126 // Create the module
2127 TheModule = new Module(ModuleID);
2128
2129 if (Handler) Handler->handleStart(TheModule, Length);
2130
2131 // Read the four bytes of the signature.
2132 unsigned Sig = read_uint();
2133
2134 // If this is a compressed file
2135 if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) {
2136
2137 // Invoke the decompression of the bytecode. Note that we have to skip the
2138 // file's magic number which is not part of the compressed block. Hence,
2139 // the Buf+4 and Length-4. The result goes into decompressedBlock, a data
2140 // member for retention until BytecodeReader is destructed.
2141 unsigned decompressedLength = Compressor::decompressToNewBuffer(
2142 (char*)Buf+4,Length-4,decompressedBlock);
2143
2144 // We must adjust the buffer pointers used by the bytecode reader to point
2145 // into the new decompressed block. After decompression, the
2146 // decompressedBlock will point to a contiguous memory area that has
2147 // the decompressed data.
2148 At = MemStart = BlockStart = Buf = (BufPtr) decompressedBlock;
2149 MemEnd = BlockEnd = Buf + decompressedLength;
2150
2151 // else if this isn't a regular (uncompressed) bytecode file, then its
2152 // and error, generate that now.
2153 } else if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) {
2154 error("Invalid bytecode signature: " + utohexstr(Sig));
2155 }
2156
2157 // Tell the handler we're starting a module
2158 if (Handler) Handler->handleModuleBegin(ModuleID);
2159
2160 // Get the module block and size and verify. This is handled specially
2161 // because the module block/size is always written in long format. Other
2162 // blocks are written in short format so the read_block method is used.
2163 unsigned Type, Size;
2164 Type = read_uint();
2165 Size = read_uint();
2166 if (Type != BytecodeFormat::ModuleBlockID) {
2167 error("Expected Module Block! Type:" + utostr(Type) + ", Size:"
2168 + utostr(Size));
2169 }
2170
2171 // It looks like the darwin ranlib program is broken, and adds trailing
2172 // garbage to the end of some bytecode files. This hack allows the bc
2173 // reader to ignore trailing garbage on bytecode files.
2174 if (At + Size < MemEnd)
2175 MemEnd = BlockEnd = At+Size;
2176
2177 if (At + Size != MemEnd)
2178 error("Invalid Top Level Block Length! Type:" + utostr(Type)
2179 + ", Size:" + utostr(Size));
2180
2181 // Parse the module contents
2182 this->ParseModule();
2183
2184 // Check for missing functions
2185 if (hasFunctions())
2186 error("Function expected, but bytecode stream ended!");
2187
Reid Spencer233fe722006-08-22 16:09:19 +00002188 // Tell the handler we're done with the module
2189 if (Handler)
2190 Handler->handleModuleEnd(ModuleID);
2191
2192 // Tell the handler we're finished the parse
2193 if (Handler) Handler->handleFinish();
2194
2195 return false;
2196
Chris Lattner00950542001-06-06 20:29:01 +00002197}
Reid Spencer060d25d2004-06-29 23:29:38 +00002198
2199//===----------------------------------------------------------------------===//
2200//=== Default Implementations of Handler Methods
2201//===----------------------------------------------------------------------===//
2202
2203BytecodeHandler::~BytecodeHandler() {}
Reid Spencer060d25d2004-06-29 23:29:38 +00002204