blob: 9a501f183ae9b98a8c45b61d715f091067367958 [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"
Reid Spencer0b118202006-01-16 21:12:35 +000020#include "llvm/Assembly/AutoUpgrade.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000021#include "llvm/Bytecode/BytecodeHandler.h"
22#include "llvm/BasicBlock.h"
Chris Lattnerdee199f2005-05-06 22:34:01 +000023#include "llvm/CallingConv.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000024#include "llvm/Constants.h"
Chris Lattner3bc5a602006-01-25 23:08:15 +000025#include "llvm/InlineAsm.h"
Reid Spencer04cde2c2004-07-04 11:33:49 +000026#include "llvm/Instructions.h"
27#include "llvm/SymbolTable.h"
Chris Lattner00950542001-06-06 20:29:01 +000028#include "llvm/Bytecode/Format.h"
Chris Lattnerdee199f2005-05-06 22:34:01 +000029#include "llvm/Config/alloca.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000030#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer17f52c52004-11-06 23:17:23 +000031#include "llvm/Support/Compressor.h"
Jim Laskeycb6682f2005-08-17 19:34:49 +000032#include "llvm/Support/MathExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/StringExtras.h"
Reid Spencer060d25d2004-06-29 23:29:38 +000034#include <sstream>
Alkis Evlogimenos20aa4742004-09-03 18:19:51 +000035#include <algorithm>
Chris Lattner29b789b2003-11-19 17:27:18 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Reid Spencer46b002c2004-07-11 17:28:43 +000038namespace {
Chris Lattnercad28bd2005-01-29 00:36:19 +000039 /// @brief A class for maintaining the slot number definition
40 /// as a placeholder for the actual definition for forward constants defs.
41 class ConstantPlaceHolder : public ConstantExpr {
42 ConstantPlaceHolder(); // DO NOT IMPLEMENT
43 void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
44 public:
Chris Lattner61323322005-01-31 01:11:13 +000045 Use Op;
Misha Brukman8a96c532005-04-21 21:44:41 +000046 ConstantPlaceHolder(const Type *Ty)
Chris Lattner61323322005-01-31 01:11:13 +000047 : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1),
48 Op(UndefValue::get(Type::IntTy), this) {
49 }
Chris Lattnercad28bd2005-01-29 00:36:19 +000050 };
Reid Spencer46b002c2004-07-11 17:28:43 +000051}
Reid Spencer060d25d2004-06-29 23:29:38 +000052
Reid Spencer24399722004-07-09 22:21:33 +000053// Provide some details on error
Reid Spencer233fe722006-08-22 16:09:19 +000054inline void BytecodeReader::error(const std::string& err) {
55 ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos="
56 + itostr(At-MemStart) + ")";
57 longjmp(context,1);
Reid Spencer24399722004-07-09 22:21:33 +000058}
59
Reid Spencer060d25d2004-06-29 23:29:38 +000060//===----------------------------------------------------------------------===//
61// Bytecode Reading Methods
62//===----------------------------------------------------------------------===//
63
Reid Spencer04cde2c2004-07-04 11:33:49 +000064/// Determine if the current block being read contains any more data.
Reid Spencer060d25d2004-06-29 23:29:38 +000065inline bool BytecodeReader::moreInBlock() {
66 return At < BlockEnd;
Chris Lattner00950542001-06-06 20:29:01 +000067}
68
Reid Spencer04cde2c2004-07-04 11:33:49 +000069/// Throw an error if we've read past the end of the current block
Reid Spencer060d25d2004-06-29 23:29:38 +000070inline void BytecodeReader::checkPastBlockEnd(const char * block_name) {
Reid Spencer46b002c2004-07-11 17:28:43 +000071 if (At > BlockEnd)
Chris Lattnera79e7cc2004-10-16 18:18:16 +000072 error(std::string("Attempt to read past the end of ") + block_name +
73 " block.");
Reid Spencer060d25d2004-06-29 23:29:38 +000074}
Chris Lattner36392bc2003-10-08 21:18:57 +000075
Reid Spencer04cde2c2004-07-04 11:33:49 +000076/// Read a whole unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000077inline unsigned BytecodeReader::read_uint() {
Misha Brukman8a96c532005-04-21 21:44:41 +000078 if (At+4 > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +000079 error("Ran out of data reading uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +000080 At += 4;
81 return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24);
82}
83
Reid Spencer04cde2c2004-07-04 11:33:49 +000084/// Read a variable-bit-rate encoded unsigned integer
Reid Spencer060d25d2004-06-29 23:29:38 +000085inline unsigned BytecodeReader::read_vbr_uint() {
86 unsigned Shift = 0;
87 unsigned Result = 0;
88 BufPtr Save = At;
Misha Brukman8a96c532005-04-21 21:44:41 +000089
Reid Spencer060d25d2004-06-29 23:29:38 +000090 do {
Misha Brukman8a96c532005-04-21 21:44:41 +000091 if (At == BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +000092 error("Ran out of data reading vbr_uint!");
Reid Spencer060d25d2004-06-29 23:29:38 +000093 Result |= (unsigned)((*At++) & 0x7F) << Shift;
94 Shift += 7;
95 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +000096 if (Handler) Handler->handleVBR32(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +000097 return Result;
98}
99
Reid Spencer04cde2c2004-07-04 11:33:49 +0000100/// Read a variable-bit-rate encoded unsigned 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000101inline uint64_t BytecodeReader::read_vbr_uint64() {
102 unsigned Shift = 0;
103 uint64_t Result = 0;
104 BufPtr Save = At;
Misha Brukman8a96c532005-04-21 21:44:41 +0000105
Reid Spencer060d25d2004-06-29 23:29:38 +0000106 do {
Misha Brukman8a96c532005-04-21 21:44:41 +0000107 if (At == BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000108 error("Ran out of data reading vbr_uint64!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000109 Result |= (uint64_t)((*At++) & 0x7F) << Shift;
110 Shift += 7;
111 } while (At[-1] & 0x80);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000112 if (Handler) Handler->handleVBR64(At-Save);
Reid Spencer060d25d2004-06-29 23:29:38 +0000113 return Result;
114}
115
Reid Spencer04cde2c2004-07-04 11:33:49 +0000116/// Read a variable-bit-rate encoded signed 64-bit integer.
Reid Spencer060d25d2004-06-29 23:29:38 +0000117inline int64_t BytecodeReader::read_vbr_int64() {
118 uint64_t R = read_vbr_uint64();
119 if (R & 1) {
120 if (R != 1)
121 return -(int64_t)(R >> 1);
122 else // There is no such thing as -0 with integers. "-0" really means
123 // 0x8000000000000000.
124 return 1LL << 63;
125 } else
126 return (int64_t)(R >> 1);
127}
128
Reid Spencer04cde2c2004-07-04 11:33:49 +0000129/// Read a pascal-style string (length followed by text)
Reid Spencer060d25d2004-06-29 23:29:38 +0000130inline std::string BytecodeReader::read_str() {
131 unsigned Size = read_vbr_uint();
132 const unsigned char *OldAt = At;
133 At += Size;
134 if (At > BlockEnd) // Size invalid?
Reid Spencer24399722004-07-09 22:21:33 +0000135 error("Ran out of data reading a string!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000136 return std::string((char*)OldAt, Size);
137}
138
Reid Spencer04cde2c2004-07-04 11:33:49 +0000139/// Read an arbitrary block of data
Reid Spencer060d25d2004-06-29 23:29:38 +0000140inline void BytecodeReader::read_data(void *Ptr, void *End) {
141 unsigned char *Start = (unsigned char *)Ptr;
142 unsigned Amount = (unsigned char *)End - Start;
Misha Brukman8a96c532005-04-21 21:44:41 +0000143 if (At+Amount > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000144 error("Ran out of data!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000145 std::copy(At, At+Amount, Start);
146 At += Amount;
147}
148
Reid Spencer46b002c2004-07-11 17:28:43 +0000149/// Read a float value in little-endian order
150inline void BytecodeReader::read_float(float& FloatVal) {
Reid Spencerada16182004-07-25 21:36:26 +0000151 /// FIXME: This isn't optimal, it has size problems on some platforms
152 /// where FP is not IEEE.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000153 FloatVal = BitsToFloat(At[0] | (At[1] << 8) | (At[2] << 16) | (At[3] << 24));
Reid Spencerada16182004-07-25 21:36:26 +0000154 At+=sizeof(uint32_t);
Reid Spencer46b002c2004-07-11 17:28:43 +0000155}
156
157/// Read a double value in little-endian order
158inline void BytecodeReader::read_double(double& DoubleVal) {
Reid Spencerada16182004-07-25 21:36:26 +0000159 /// FIXME: This isn't optimal, it has size problems on some platforms
160 /// where FP is not IEEE.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000161 DoubleVal = BitsToDouble((uint64_t(At[0]) << 0) | (uint64_t(At[1]) << 8) |
162 (uint64_t(At[2]) << 16) | (uint64_t(At[3]) << 24) |
163 (uint64_t(At[4]) << 32) | (uint64_t(At[5]) << 40) |
164 (uint64_t(At[6]) << 48) | (uint64_t(At[7]) << 56));
Reid Spencerada16182004-07-25 21:36:26 +0000165 At+=sizeof(uint64_t);
Reid Spencer46b002c2004-07-11 17:28:43 +0000166}
167
Reid Spencer04cde2c2004-07-04 11:33:49 +0000168/// Read a block header and obtain its type and size
Reid Spencer060d25d2004-06-29 23:29:38 +0000169inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) {
Reid Spencerd798a512006-11-14 04:47:22 +0000170 Size = read_uint(); // Read the header
171 Type = Size & 0x1F; // mask low order five bits to get type
172 Size >>= 5; // high order 27 bits is the size
Reid Spencer060d25d2004-06-29 23:29:38 +0000173 BlockStart = At;
Reid Spencer46b002c2004-07-11 17:28:43 +0000174 if (At + Size > BlockEnd)
Reid Spencer24399722004-07-09 22:21:33 +0000175 error("Attempt to size a block past end of memory");
Reid Spencer060d25d2004-06-29 23:29:38 +0000176 BlockEnd = At + Size;
Reid Spencer46b002c2004-07-11 17:28:43 +0000177 if (Handler) Handler->handleBlock(Type, BlockStart, Size);
Reid Spencer04cde2c2004-07-04 11:33:49 +0000178}
179
Reid Spencer060d25d2004-06-29 23:29:38 +0000180//===----------------------------------------------------------------------===//
181// IR Lookup Methods
182//===----------------------------------------------------------------------===//
183
Reid Spencer04cde2c2004-07-04 11:33:49 +0000184/// Determine if a type id has an implicit null value
Reid Spencer46b002c2004-07-11 17:28:43 +0000185inline bool BytecodeReader::hasImplicitNull(unsigned TyID) {
Reid Spencerd798a512006-11-14 04:47:22 +0000186 return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
Reid Spencer060d25d2004-06-29 23:29:38 +0000187}
188
Reid Spencer04cde2c2004-07-04 11:33:49 +0000189/// Obtain a type given a typeid and account for things like compaction tables,
190/// function level vs module level, and the offsetting for the primitive types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000191const Type *BytecodeReader::getType(unsigned ID) {
Chris Lattner89e02532004-01-18 21:08:15 +0000192 if (ID < Type::FirstDerivedTyID)
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000193 if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
Chris Lattner927b1852003-10-09 20:22:47 +0000194 return T; // Asked for a primitive type...
Chris Lattner36392bc2003-10-08 21:18:57 +0000195
196 // Otherwise, derived types need offset...
Chris Lattner89e02532004-01-18 21:08:15 +0000197 ID -= Type::FirstDerivedTyID;
198
Reid Spencer060d25d2004-06-29 23:29:38 +0000199 if (!CompactionTypes.empty()) {
200 if (ID >= CompactionTypes.size())
Reid Spencer24399722004-07-09 22:21:33 +0000201 error("Type ID out of range for compaction table!");
Chris Lattner45b5dd22004-08-03 23:41:28 +0000202 return CompactionTypes[ID].first;
Chris Lattner89e02532004-01-18 21:08:15 +0000203 }
Chris Lattner36392bc2003-10-08 21:18:57 +0000204
205 // Is it a module-level type?
Reid Spencer46b002c2004-07-11 17:28:43 +0000206 if (ID < ModuleTypes.size())
207 return ModuleTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000208
Reid Spencer46b002c2004-07-11 17:28:43 +0000209 // Nope, is it a function-level type?
210 ID -= ModuleTypes.size();
211 if (ID < FunctionTypes.size())
212 return FunctionTypes[ID].get();
Chris Lattner36392bc2003-10-08 21:18:57 +0000213
Reid Spencer46b002c2004-07-11 17:28:43 +0000214 error("Illegal type reference!");
215 return Type::VoidTy;
Chris Lattner00950542001-06-06 20:29:01 +0000216}
217
Reid Spencerd798a512006-11-14 04:47:22 +0000218/// This method just saves some coding. It uses read_vbr_uint to read
Reid Spencer24399722004-07-09 22:21:33 +0000219/// in a sanitized type id, errors that its not the type type, and
Reid Spencer04cde2c2004-07-04 11:33:49 +0000220/// then calls getType to return the type value.
Reid Spencerd798a512006-11-14 04:47:22 +0000221inline const Type* BytecodeReader::readType() {
222 return getType(read_vbr_uint());
Reid Spencer04cde2c2004-07-04 11:33:49 +0000223}
224
225/// Get the slot number associated with a type accounting for primitive
226/// types, compaction tables, and function level vs module level.
Reid Spencer060d25d2004-06-29 23:29:38 +0000227unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
228 if (Ty->isPrimitiveType())
229 return Ty->getTypeID();
230
231 // Scan the compaction table for the type if needed.
232 if (!CompactionTypes.empty()) {
Chris Lattner45b5dd22004-08-03 23:41:28 +0000233 for (unsigned i = 0, e = CompactionTypes.size(); i != e; ++i)
234 if (CompactionTypes[i].first == Ty)
Misha Brukman8a96c532005-04-21 21:44:41 +0000235 return Type::FirstDerivedTyID + i;
Reid Spencer060d25d2004-06-29 23:29:38 +0000236
Chris Lattner45b5dd22004-08-03 23:41:28 +0000237 error("Couldn't find type specified in compaction table!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000238 }
239
240 // Check the function level types first...
Chris Lattnera79e7cc2004-10-16 18:18:16 +0000241 TypeListTy::iterator I = std::find(FunctionTypes.begin(),
242 FunctionTypes.end(), Ty);
Reid Spencer060d25d2004-06-29 23:29:38 +0000243
244 if (I != FunctionTypes.end())
Misha Brukman8a96c532005-04-21 21:44:41 +0000245 return Type::FirstDerivedTyID + ModuleTypes.size() +
Reid Spencer46b002c2004-07-11 17:28:43 +0000246 (&*I - &FunctionTypes[0]);
Reid Spencer060d25d2004-06-29 23:29:38 +0000247
Chris Lattnereebac5f2005-10-03 21:26:53 +0000248 // If we don't have our cache yet, build it now.
249 if (ModuleTypeIDCache.empty()) {
250 unsigned N = 0;
251 ModuleTypeIDCache.reserve(ModuleTypes.size());
252 for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
253 I != E; ++I, ++N)
254 ModuleTypeIDCache.push_back(std::make_pair(*I, N));
255
256 std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
257 }
258
259 // Binary search the cache for the entry.
260 std::vector<std::pair<const Type*, unsigned> >::iterator IT =
261 std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
262 std::make_pair(Ty, 0U));
263 if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000264 error("Didn't find type in ModuleTypes.");
Chris Lattnereebac5f2005-10-03 21:26:53 +0000265
266 return Type::FirstDerivedTyID + IT->second;
Chris Lattner80b97342004-01-17 23:25:43 +0000267}
268
Reid Spencer04cde2c2004-07-04 11:33:49 +0000269/// This is just like getType, but when a compaction table is in use, it is
270/// ignored. It also ignores function level types.
271/// @see getType
Reid Spencer060d25d2004-06-29 23:29:38 +0000272const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
273 if (Slot < Type::FirstDerivedTyID) {
274 const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
Reid Spencer46b002c2004-07-11 17:28:43 +0000275 if (!Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000276 error("Not a primitive type ID?");
Reid Spencer060d25d2004-06-29 23:29:38 +0000277 return Ty;
278 }
279 Slot -= Type::FirstDerivedTyID;
280 if (Slot >= ModuleTypes.size())
Reid Spencer24399722004-07-09 22:21:33 +0000281 error("Illegal compaction table type reference!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000282 return ModuleTypes[Slot];
Chris Lattner52e20b02003-03-19 20:54:26 +0000283}
284
Reid Spencer04cde2c2004-07-04 11:33:49 +0000285/// This is just like getTypeSlot, but when a compaction table is in use, it
286/// is ignored. It also ignores function level types.
Reid Spencer060d25d2004-06-29 23:29:38 +0000287unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
288 if (Ty->isPrimitiveType())
289 return Ty->getTypeID();
Chris Lattnereebac5f2005-10-03 21:26:53 +0000290
291 // If we don't have our cache yet, build it now.
292 if (ModuleTypeIDCache.empty()) {
293 unsigned N = 0;
294 ModuleTypeIDCache.reserve(ModuleTypes.size());
295 for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
296 I != E; ++I, ++N)
297 ModuleTypeIDCache.push_back(std::make_pair(*I, N));
298
299 std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
300 }
301
302 // Binary search the cache for the entry.
303 std::vector<std::pair<const Type*, unsigned> >::iterator IT =
304 std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
305 std::make_pair(Ty, 0U));
306 if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
Reid Spencer24399722004-07-09 22:21:33 +0000307 error("Didn't find type in ModuleTypes.");
Chris Lattnereebac5f2005-10-03 21:26:53 +0000308
309 return Type::FirstDerivedTyID + IT->second;
Reid Spencer060d25d2004-06-29 23:29:38 +0000310}
311
Misha Brukman8a96c532005-04-21 21:44:41 +0000312/// Retrieve a value of a given type and slot number, possibly creating
313/// it if it doesn't already exist.
Reid Spencer060d25d2004-06-29 23:29:38 +0000314Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000315 assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
Chris Lattner00950542001-06-06 20:29:01 +0000316 unsigned Num = oNum;
Chris Lattner00950542001-06-06 20:29:01 +0000317
Chris Lattner89e02532004-01-18 21:08:15 +0000318 // If there is a compaction table active, it defines the low-level numbers.
319 // If not, the module values define the low-level numbers.
Reid Spencer060d25d2004-06-29 23:29:38 +0000320 if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
321 if (Num < CompactionValues[type].size())
322 return CompactionValues[type][Num];
323 Num -= CompactionValues[type].size();
Chris Lattner89e02532004-01-18 21:08:15 +0000324 } else {
Reid Spencer060d25d2004-06-29 23:29:38 +0000325 // By default, the global type id is the type id passed in
Chris Lattner52f86d62004-01-20 00:54:06 +0000326 unsigned GlobalTyID = type;
Reid Spencer060d25d2004-06-29 23:29:38 +0000327
Chris Lattner45b5dd22004-08-03 23:41:28 +0000328 // If the type plane was compactified, figure out the global type ID by
329 // adding the derived type ids and the distance.
330 if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID)
331 GlobalTyID = CompactionTypes[type-Type::FirstDerivedTyID].second;
Chris Lattner00950542001-06-06 20:29:01 +0000332
Reid Spencer060d25d2004-06-29 23:29:38 +0000333 if (hasImplicitNull(GlobalTyID)) {
Chris Lattneraba5ff52005-05-05 20:57:00 +0000334 const Type *Ty = getType(type);
335 if (!isa<OpaqueType>(Ty)) {
336 if (Num == 0)
337 return Constant::getNullValue(Ty);
338 --Num;
339 }
Chris Lattner89e02532004-01-18 21:08:15 +0000340 }
341
Chris Lattner52f86d62004-01-20 00:54:06 +0000342 if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
343 if (Num < ModuleValues[GlobalTyID]->size())
Reid Spencer04cde2c2004-07-04 11:33:49 +0000344 return ModuleValues[GlobalTyID]->getOperand(Num);
Chris Lattner52f86d62004-01-20 00:54:06 +0000345 Num -= ModuleValues[GlobalTyID]->size();
Chris Lattner89e02532004-01-18 21:08:15 +0000346 }
Chris Lattner52e20b02003-03-19 20:54:26 +0000347 }
348
Misha Brukman8a96c532005-04-21 21:44:41 +0000349 if (FunctionValues.size() > type &&
350 FunctionValues[type] &&
Reid Spencer060d25d2004-06-29 23:29:38 +0000351 Num < FunctionValues[type]->size())
352 return FunctionValues[type]->getOperand(Num);
Chris Lattner00950542001-06-06 20:29:01 +0000353
Chris Lattner74734132002-08-17 22:01:27 +0000354 if (!Create) return 0; // Do not create a placeholder?
Chris Lattner00950542001-06-06 20:29:01 +0000355
Reid Spencer551ccae2004-09-01 22:55:40 +0000356 // Did we already create a place holder?
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000357 std::pair<unsigned,unsigned> KeyValue(type, oNum);
Reid Spencer060d25d2004-06-29 23:29:38 +0000358 ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue);
Chris Lattner8eb10ce2003-10-09 06:05:40 +0000359 if (I != ForwardReferences.end() && I->first == KeyValue)
360 return I->second; // We have already created this placeholder
361
Reid Spencer551ccae2004-09-01 22:55:40 +0000362 // If the type exists (it should)
363 if (const Type* Ty = getType(type)) {
364 // Create the place holder
365 Value *Val = new Argument(Ty);
366 ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
367 return Val;
368 }
Reid Spencer233fe722006-08-22 16:09:19 +0000369 error("Can't create placeholder for value of type slot #" + utostr(type));
370 return 0; // just silence warning, error calls longjmp
Chris Lattner00950542001-06-06 20:29:01 +0000371}
372
Misha Brukman8a96c532005-04-21 21:44:41 +0000373/// This is just like getValue, but when a compaction table is in use, it
374/// is ignored. Also, no forward references or other fancy features are
Reid Spencer04cde2c2004-07-04 11:33:49 +0000375/// supported.
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000376Value* BytecodeReader::getGlobalTableValue(unsigned TyID, unsigned SlotNo) {
377 if (SlotNo == 0)
378 return Constant::getNullValue(getType(TyID));
379
380 if (!CompactionTypes.empty() && TyID >= Type::FirstDerivedTyID) {
381 TyID -= Type::FirstDerivedTyID;
382 if (TyID >= CompactionTypes.size())
383 error("Type ID out of range for compaction table!");
384 TyID = CompactionTypes[TyID].second;
Reid Spencer060d25d2004-06-29 23:29:38 +0000385 }
386
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000387 --SlotNo;
388
Reid Spencer060d25d2004-06-29 23:29:38 +0000389 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
390 SlotNo >= ModuleValues[TyID]->size()) {
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000391 if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0)
392 error("Corrupt compaction table entry!"
Misha Brukman8a96c532005-04-21 21:44:41 +0000393 + utostr(TyID) + ", " + utostr(SlotNo) + ": "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000394 + utostr(ModuleValues.size()));
Misha Brukman8a96c532005-04-21 21:44:41 +0000395 else
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000396 error("Corrupt compaction table entry!"
Misha Brukman8a96c532005-04-21 21:44:41 +0000397 + utostr(TyID) + ", " + utostr(SlotNo) + ": "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000398 + utostr(ModuleValues.size()) + ", "
Reid Spencer9a7e0c52004-08-04 22:56:46 +0000399 + utohexstr(reinterpret_cast<uint64_t>(((void*)ModuleValues[TyID])))
400 + ", "
Chris Lattner2c6c14d2004-08-04 00:19:23 +0000401 + utostr(ModuleValues[TyID]->size()));
Reid Spencer060d25d2004-06-29 23:29:38 +0000402 }
403 return ModuleValues[TyID]->getOperand(SlotNo);
404}
405
Reid Spencer04cde2c2004-07-04 11:33:49 +0000406/// Just like getValue, except that it returns a null pointer
407/// only on error. It always returns a constant (meaning that if the value is
408/// defined, but is not a constant, that is an error). If the specified
Misha Brukman8a96c532005-04-21 21:44:41 +0000409/// constant hasn't been parsed yet, a placeholder is defined and used.
Reid Spencer04cde2c2004-07-04 11:33:49 +0000410/// Later, after the real value is parsed, the placeholder is eliminated.
Reid Spencer060d25d2004-06-29 23:29:38 +0000411Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) {
412 if (Value *V = getValue(TypeSlot, Slot, false))
413 if (Constant *C = dyn_cast<Constant>(V))
414 return C; // If we already have the value parsed, just return it
Reid Spencer060d25d2004-06-29 23:29:38 +0000415 else
Misha Brukman8a96c532005-04-21 21:44:41 +0000416 error("Value for slot " + utostr(Slot) +
Reid Spencera86037e2004-07-18 00:12:03 +0000417 " is expected to be a constant!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000418
Chris Lattner389bd042004-12-09 06:19:44 +0000419 std::pair<unsigned, unsigned> Key(TypeSlot, Slot);
Reid Spencer060d25d2004-06-29 23:29:38 +0000420 ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
421
422 if (I != ConstantFwdRefs.end() && I->first == Key) {
423 return I->second;
424 } else {
425 // Create a placeholder for the constant reference and
426 // keep track of the fact that we have a forward ref to recycle it
Chris Lattner389bd042004-12-09 06:19:44 +0000427 Constant *C = new ConstantPlaceHolder(getType(TypeSlot));
Misha Brukman8a96c532005-04-21 21:44:41 +0000428
Reid Spencer060d25d2004-06-29 23:29:38 +0000429 // Keep track of the fact that we have a forward ref to recycle it
430 ConstantFwdRefs.insert(I, std::make_pair(Key, C));
431 return C;
432 }
433}
434
435//===----------------------------------------------------------------------===//
436// IR Construction Methods
437//===----------------------------------------------------------------------===//
438
Reid Spencer04cde2c2004-07-04 11:33:49 +0000439/// As values are created, they are inserted into the appropriate place
440/// with this method. The ValueTable argument must be one of ModuleValues
441/// or FunctionValues data members of this class.
Misha Brukman8a96c532005-04-21 21:44:41 +0000442unsigned BytecodeReader::insertValue(Value *Val, unsigned type,
Reid Spencer46b002c2004-07-11 17:28:43 +0000443 ValueTable &ValueTab) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000444 if (ValueTab.size() <= type)
445 ValueTab.resize(type+1);
446
447 if (!ValueTab[type]) ValueTab[type] = new ValueList();
448
449 ValueTab[type]->push_back(Val);
450
Chris Lattneraba5ff52005-05-05 20:57:00 +0000451 bool HasOffset = hasImplicitNull(type) && !isa<OpaqueType>(Val->getType());
Reid Spencer060d25d2004-06-29 23:29:38 +0000452 return ValueTab[type]->size()-1 + HasOffset;
453}
454
Reid Spencer04cde2c2004-07-04 11:33:49 +0000455/// Insert the arguments of a function as new values in the reader.
Reid Spencer46b002c2004-07-11 17:28:43 +0000456void BytecodeReader::insertArguments(Function* F) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000457 const FunctionType *FT = F->getFunctionType();
Chris Lattnere4d5c442005-03-15 04:54:21 +0000458 Function::arg_iterator AI = F->arg_begin();
Reid Spencer060d25d2004-06-29 23:29:38 +0000459 for (FunctionType::param_iterator It = FT->param_begin();
460 It != FT->param_end(); ++It, ++AI)
461 insertValue(AI, getTypeSlot(AI->getType()), FunctionValues);
462}
463
Reid Spencer3da59db2006-11-27 01:05:10 +0000464/// Convert previous opcode values into the current value and/or construct
465/// the instruction. This function handles all *abnormal* cases for instruction
466/// generation based on obsolete opcode values. The normal cases are handled
467/// in ParseInstruction below. Generally this function just produces a new
468/// Opcode value (first argument). In a few cases (VAArg, VANext) the upgrade
469/// path requies that the instruction (sequence) be generated differently from
470/// the normal case in order to preserve the original semantics. In these
471/// cases the result of the function will be a non-zero Instruction pointer. In
472/// all other cases, zero will be returned indicating that the *normal*
473/// instruction generation should be used, but with the new Opcode value.
Reid Spencer1628cec2006-10-26 06:15:43 +0000474Instruction*
Reid Spencer6996feb2006-11-08 21:27:54 +0000475BytecodeReader::upgradeInstrOpcodes(
Reid Spencer1628cec2006-10-26 06:15:43 +0000476 unsigned &Opcode, ///< The old opcode, possibly updated by this function
477 std::vector<unsigned> &Oprnds, ///< The operands to the instruction
478 unsigned &iType, ///< The type code from the bytecode file
Reid Spencer3da59db2006-11-27 01:05:10 +0000479 const Type *InstTy, ///< The type of the instruction
480 BasicBlock *BB ///< The basic block to insert into, if we need to
Reid Spencer1628cec2006-10-26 06:15:43 +0000481) {
482
483 // First, short circuit this if no conversion is required. When signless
Reid Spencer6996feb2006-11-08 21:27:54 +0000484 // instructions were implemented the entire opcode sequence was revised in
485 // two stages: first Div/Rem became signed, then Shr/Cast/Setcc became
486 // signed. If all of these instructions are signed then we don't have to
487 // upgrade the opcode.
488 if (!hasSignlessDivRem && !hasSignlessShrCastSetcc)
Reid Spencer1628cec2006-10-26 06:15:43 +0000489 return 0; // The opcode is fine the way it is.
490
Reid Spencer6996feb2006-11-08 21:27:54 +0000491 // If this is bytecode version 6, that only had signed Rem and Div
492 // instructions, then we must compensate for those two instructions only.
493 // So that the switch statement below works, we're trying to turn this into
494 // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its
495 // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to
496 // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply
497 // decrement the instruction code if its beyond FRem.
498 if (!hasSignlessDivRem) {
499 // If its one of the signed Div/Rem opcodes, its fine the way it is
500 if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv
501 Opcode = 10; // Div
502 else if (Opcode >=13 && Opcode <= 15) // URem through FRem
503 Opcode = 11; // Rem
504 else if (Opcode >= 16 && Opcode <= 35) // And through Shr
505 // Adjust for new instruction codes
506 Opcode -= 4;
507 else if (Opcode >= 36 && Opcode <= 42) // Everything after Select
508 // In vers 6 bytecode we eliminated the placeholders for the obsolete
509 // VAARG and VANEXT instructions. Consequently those two slots were
510 // filled starting with Select (36) which was 34. So now we only need
511 // to subtract two. This circumvents hitting opcodes 32 and 33
512 Opcode -= 2;
513 else { // Opcode < 10 or > 42
514 // No upgrade necessary.
515 return 0;
516 }
517 }
518
Reid Spencer1628cec2006-10-26 06:15:43 +0000519 // Declare the resulting instruction we might build. In general we just
520 // change the Opcode argument but in a few cases we need to generate the
521 // Instruction here because the upgrade case is significantly different from
522 // the normal case.
523 Instruction *Result = 0;
524
Reid Spencer1628cec2006-10-26 06:15:43 +0000525 // We're dealing with an upgrade situation. For each of the opcode values,
526 // perform the necessary conversion.
527 switch (Opcode) {
528 default: // Error
529 // This switch statement provides cases for all known opcodes prior to
530 // version 6 bytecode format. We know we're in an upgrade situation so
531 // if there isn't a match in this switch, then something is horribly
532 // wrong.
533 error("Unknown obsolete opcode encountered.");
534 break;
535 case 1: // Ret
536 Opcode = Instruction::Ret;
537 break;
538 case 2: // Br
539 Opcode = Instruction::Br;
540 break;
541 case 3: // Switch
542 Opcode = Instruction::Switch;
543 break;
544 case 4: // Invoke
545 Opcode = Instruction::Invoke;
546 break;
547 case 5: // Unwind
548 Opcode = Instruction::Unwind;
549 break;
550 case 6: // Unreachable
551 Opcode = Instruction::Unreachable;
552 break;
553 case 7: // Add
554 Opcode = Instruction::Add;
555 break;
556 case 8: // Sub
557 Opcode = Instruction::Sub;
558 break;
559 case 9: // Mul
560 Opcode = Instruction::Mul;
561 break;
562 case 10: // Div
563 // The type of the instruction is based on the operands. We need to select
564 // fdiv, udiv or sdiv based on that type. The iType values are hardcoded
565 // to the values used in bytecode version 5 (and prior) because it is
566 // likely these codes will change in future versions of LLVM.
567 if (iType == 10 || iType == 11 )
568 Opcode = Instruction::FDiv;
569 else if (iType >= 2 && iType <= 9 && iType % 2 != 0)
570 Opcode = Instruction::SDiv;
571 else
572 Opcode = Instruction::UDiv;
573 break;
574
575 case 11: // Rem
Reid Spencer0a783f72006-11-02 01:53:59 +0000576 // As with "Div", make the signed/unsigned or floating point Rem
577 // instruction choice based on the type of the operands.
578 if (iType == 10 || iType == 11)
579 Opcode = Instruction::FRem;
580 else if (iType >= 2 && iType <= 9 && iType % 2 != 0)
581 Opcode = Instruction::SRem;
582 else
583 Opcode = Instruction::URem;
Reid Spencer1628cec2006-10-26 06:15:43 +0000584 break;
585 case 12: // And
586 Opcode = Instruction::And;
587 break;
588 case 13: // Or
589 Opcode = Instruction::Or;
590 break;
591 case 14: // Xor
592 Opcode = Instruction::Xor;
593 break;
594 case 15: // SetEQ
595 Opcode = Instruction::SetEQ;
596 break;
597 case 16: // SetNE
598 Opcode = Instruction::SetNE;
599 break;
600 case 17: // SetLE
601 Opcode = Instruction::SetLE;
602 break;
603 case 18: // SetGE
604 Opcode = Instruction::SetGE;
605 break;
606 case 19: // SetLT
607 Opcode = Instruction::SetLT;
608 break;
609 case 20: // SetGT
610 Opcode = Instruction::SetGT;
611 break;
612 case 21: // Malloc
613 Opcode = Instruction::Malloc;
614 break;
615 case 22: // Free
616 Opcode = Instruction::Free;
617 break;
618 case 23: // Alloca
619 Opcode = Instruction::Alloca;
620 break;
621 case 24: // Load
622 Opcode = Instruction::Load;
623 break;
624 case 25: // Store
625 Opcode = Instruction::Store;
626 break;
627 case 26: // GetElementPtr
628 Opcode = Instruction::GetElementPtr;
629 break;
630 case 27: // PHI
631 Opcode = Instruction::PHI;
632 break;
633 case 28: // Cast
Reid Spencer3da59db2006-11-27 01:05:10 +0000634 {
635 Value *Source = getValue(iType, Oprnds[0]);
636 const Type *DestTy = getType(Oprnds[1]);
637 // The previous definition of cast to bool was a compare against zero.
638 // We have to retain that semantic so we do it here.
639 if (DestTy == Type::BoolTy) { // if its a cast to bool
640 Opcode = Instruction::SetNE;
641 Result = new SetCondInst(Instruction::SetNE, Source,
642 Constant::getNullValue(Source->getType()));
643 } else if (Source->getType()->isFloatingPoint() &&
644 isa<PointerType>(DestTy)) {
645 // Upgrade what is now an illegal cast (fp -> ptr) into two casts,
646 // fp -> ui, and ui -> ptr
647 CastInst *CI = new FPToUIInst(Source, Type::ULongTy);
648 BB->getInstList().push_back(CI);
649 Result = new IntToPtrInst(CI, DestTy);
650 } else {
651 Result = CastInst::createInferredCast(Source, DestTy);
652 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000653 break;
Reid Spencer3da59db2006-11-27 01:05:10 +0000654 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000655 case 29: // Call
656 Opcode = Instruction::Call;
657 break;
658 case 30: // Shl
659 Opcode = Instruction::Shl;
660 break;
661 case 31: // Shr
Reid Spencer3822ff52006-11-08 06:47:33 +0000662 // The type of the instruction is based on the operands. We need to
663 // select ashr or lshr based on that type. The iType values are hardcoded
664 // to the values used in bytecode version 5 (and prior) because it is
665 // likely these codes will change in future versions of LLVM. This if
666 // statement says "if (integer type and signed)"
667 if (iType >= 2 && iType <= 9 && iType % 2 != 0)
668 Opcode = Instruction::AShr;
669 else
670 Opcode = Instruction::LShr;
Reid Spencer1628cec2006-10-26 06:15:43 +0000671 break;
672 case 32: { //VANext_old ( <= llvm 1.5 )
673 const Type* ArgTy = getValue(iType, Oprnds[0])->getType();
674 Function* NF = TheModule->getOrInsertFunction(
675 "llvm.va_copy", ArgTy, ArgTy, (Type *)0);
676
677 // In llvm 1.6 the VANext instruction was dropped because it was only
678 // necessary to have a VAArg instruction. The code below transforms an
679 // old vanext instruction into the equivalent code given only the
680 // availability of the new vaarg instruction. Essentially, the transform
681 // is as follows:
682 // b = vanext a, t ->
683 // foo = alloca 1 of t
684 // bar = vacopy a
685 // store bar -> foo
686 // tmp = vaarg foo, t
687 // b = load foo
688 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
689 BB->getInstList().push_back(foo);
690 CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0]));
691 BB->getInstList().push_back(bar);
692 BB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencerd798a512006-11-14 04:47:22 +0000693 Instruction* tmp = new VAArgInst(foo, getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000694 BB->getInstList().push_back(tmp);
695 Result = new LoadInst(foo);
696 break;
697 }
698 case 33: { //VAArg_old
699 const Type* ArgTy = getValue(iType, Oprnds[0])->getType();
700 Function* NF = TheModule->getOrInsertFunction(
701 "llvm.va_copy", ArgTy, ArgTy, (Type *)0);
702
703 // In llvm 1.6 the VAArg's instruction semantics were changed. The code
704 // below transforms an old vaarg instruction into the equivalent code
705 // given only the availability of the new vaarg instruction. Essentially,
706 // the transform is as follows:
707 // b = vaarg a, t ->
708 // foo = alloca 1 of t
709 // bar = vacopy a
710 // store bar -> foo
711 // b = vaarg foo, t
712 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
713 BB->getInstList().push_back(foo);
714 CallInst* bar = new CallInst(NF, getValue(iType, Oprnds[0]));
715 BB->getInstList().push_back(bar);
716 BB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencerd798a512006-11-14 04:47:22 +0000717 Result = new VAArgInst(foo, getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000718 break;
719 }
720 case 34: // Select
721 Opcode = Instruction::Select;
722 break;
723 case 35: // UserOp1
724 Opcode = Instruction::UserOp1;
725 break;
726 case 36: // UserOp2
727 Opcode = Instruction::UserOp2;
728 break;
729 case 37: // VAArg
730 Opcode = Instruction::VAArg;
731 break;
732 case 38: // ExtractElement
733 Opcode = Instruction::ExtractElement;
734 break;
735 case 39: // InsertElement
736 Opcode = Instruction::InsertElement;
737 break;
738 case 40: // ShuffleVector
739 Opcode = Instruction::ShuffleVector;
740 break;
Reid Spencer3da59db2006-11-27 01:05:10 +0000741 case 56: // Invoke with encoded CC
742 case 57: { // Invoke Fast CC
743 if (Oprnds.size() < 3)
744 error("Invalid invoke instruction!");
745 Value *F = getValue(iType, Oprnds[0]);
746
747 // Check to make sure we have a pointer to function type
748 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
749 if (PTy == 0)
750 error("Invoke to non function pointer value!");
751 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
752 if (FTy == 0)
753 error("Invoke to non function pointer value!");
754
755 std::vector<Value *> Params;
756 BasicBlock *Normal, *Except;
757 unsigned CallingConv = CallingConv::C;
758 if (Opcode == 57)
759 CallingConv = CallingConv::Fast;
760 else if (Opcode == 56) {
761 CallingConv = Oprnds.back();
762 Oprnds.pop_back();
763 }
764 Opcode = Instruction::Invoke;
765
766 if (!FTy->isVarArg()) {
767 Normal = getBasicBlock(Oprnds[1]);
768 Except = getBasicBlock(Oprnds[2]);
769
770 FunctionType::param_iterator It = FTy->param_begin();
771 for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
772 if (It == FTy->param_end())
773 error("Invalid invoke instruction!");
774 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
775 }
776 if (It != FTy->param_end())
777 error("Invalid invoke instruction!");
778 } else {
779 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
780
781 Normal = getBasicBlock(Oprnds[0]);
782 Except = getBasicBlock(Oprnds[1]);
783
784 unsigned FirstVariableArgument = FTy->getNumParams()+2;
785 for (unsigned i = 2; i != FirstVariableArgument; ++i)
786 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
787 Oprnds[i]));
788
789 // Must be type/value pairs. If not, error out.
790 if (Oprnds.size()-FirstVariableArgument & 1)
791 error("Invalid invoke instruction!");
792
793 for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
794 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
795 }
796
797 Result = new InvokeInst(F, Normal, Except, Params);
798 if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
799 break;
800 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000801 case 58: // Call with extra operand for calling conv
802 case 59: // tail call, Fast CC
803 case 60: // normal call, Fast CC
804 case 61: // tail call, C Calling Conv
805 case 62: // volatile load
806 case 63: // volatile store
807 // In all these cases, we pass the opcode through. The new version uses
808 // the same code (for now, this might change in 2.0). These are listed
809 // here to document the opcodes in use in vers 5 bytecode and to make it
810 // easier to migrate these opcodes in the future.
811 break;
812 }
813 return Result;
814}
815
Reid Spencer060d25d2004-06-29 23:29:38 +0000816//===----------------------------------------------------------------------===//
817// Bytecode Parsing Methods
818//===----------------------------------------------------------------------===//
819
Reid Spencer04cde2c2004-07-04 11:33:49 +0000820/// This method parses a single instruction. The instruction is
821/// inserted at the end of the \p BB provided. The arguments of
Misha Brukman44666b12004-09-28 16:57:46 +0000822/// the instruction are provided in the \p Oprnds vector.
Reid Spencer060d25d2004-06-29 23:29:38 +0000823void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
Reid Spencer46b002c2004-07-11 17:28:43 +0000824 BasicBlock* BB) {
Reid Spencer060d25d2004-06-29 23:29:38 +0000825 BufPtr SaveAt = At;
826
827 // Clear instruction data
828 Oprnds.clear();
829 unsigned iType = 0;
830 unsigned Opcode = 0;
831 unsigned Op = read_uint();
832
833 // bits Instruction format: Common to all formats
834 // --------------------------
835 // 01-00: Opcode type, fixed to 1.
836 // 07-02: Opcode
837 Opcode = (Op >> 2) & 63;
838 Oprnds.resize((Op >> 0) & 03);
839
840 // Extract the operands
841 switch (Oprnds.size()) {
842 case 1:
843 // bits Instruction format:
844 // --------------------------
845 // 19-08: Resulting type plane
846 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
847 //
848 iType = (Op >> 8) & 4095;
849 Oprnds[0] = (Op >> 20) & 4095;
850 if (Oprnds[0] == 4095) // Handle special encoding for 0 operands...
851 Oprnds.resize(0);
852 break;
853 case 2:
854 // bits Instruction format:
855 // --------------------------
856 // 15-08: Resulting type plane
857 // 23-16: Operand #1
Misha Brukman8a96c532005-04-21 21:44:41 +0000858 // 31-24: Operand #2
Reid Spencer060d25d2004-06-29 23:29:38 +0000859 //
860 iType = (Op >> 8) & 255;
861 Oprnds[0] = (Op >> 16) & 255;
862 Oprnds[1] = (Op >> 24) & 255;
863 break;
864 case 3:
865 // bits Instruction format:
866 // --------------------------
867 // 13-08: Resulting type plane
868 // 19-14: Operand #1
869 // 25-20: Operand #2
870 // 31-26: Operand #3
871 //
872 iType = (Op >> 8) & 63;
873 Oprnds[0] = (Op >> 14) & 63;
874 Oprnds[1] = (Op >> 20) & 63;
875 Oprnds[2] = (Op >> 26) & 63;
876 break;
877 case 0:
878 At -= 4; // Hrm, try this again...
879 Opcode = read_vbr_uint();
880 Opcode >>= 2;
881 iType = read_vbr_uint();
882
883 unsigned NumOprnds = read_vbr_uint();
884 Oprnds.resize(NumOprnds);
885
886 if (NumOprnds == 0)
Reid Spencer24399722004-07-09 22:21:33 +0000887 error("Zero-argument instruction found; this is invalid.");
Reid Spencer060d25d2004-06-29 23:29:38 +0000888
889 for (unsigned i = 0; i != NumOprnds; ++i)
890 Oprnds[i] = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +0000891 break;
892 }
893
Reid Spencerd798a512006-11-14 04:47:22 +0000894 const Type *InstTy = getType(iType);
Reid Spencer060d25d2004-06-29 23:29:38 +0000895
Reid Spencer1628cec2006-10-26 06:15:43 +0000896 // Make the necessary adjustments for dealing with backwards compatibility
897 // of opcodes.
898 Instruction* Result =
Reid Spencer6996feb2006-11-08 21:27:54 +0000899 upgradeInstrOpcodes(Opcode, Oprnds, iType, InstTy, BB);
Reid Spencer1628cec2006-10-26 06:15:43 +0000900
Reid Spencer46b002c2004-07-11 17:28:43 +0000901 // We have enough info to inform the handler now.
Reid Spencer1628cec2006-10-26 06:15:43 +0000902 if (Handler)
903 Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
Reid Spencer060d25d2004-06-29 23:29:38 +0000904
Reid Spencer1628cec2006-10-26 06:15:43 +0000905 // If the backwards compatibility code didn't produce an instruction then
906 // we do the *normal* thing ..
907 if (!Result) {
908 // First, handle the easy binary operators case
909 if (Opcode >= Instruction::BinaryOpsBegin &&
910 Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2)
911 Result = BinaryOperator::create(Instruction::BinaryOps(Opcode),
912 getValue(iType, Oprnds[0]),
913 getValue(iType, Oprnds[1]));
Reid Spencer060d25d2004-06-29 23:29:38 +0000914
Reid Spencer1628cec2006-10-26 06:15:43 +0000915 // Indicate that we don't think this is a call instruction (yet).
916 // Process based on the Opcode read
917 switch (Opcode) {
918 default: // There was an error, this shouldn't happen.
919 if (Result == 0)
920 error("Illegal instruction read!");
921 break;
922 case Instruction::VAArg:
923 if (Oprnds.size() != 2)
924 error("Invalid VAArg instruction!");
925 Result = new VAArgInst(getValue(iType, Oprnds[0]),
Reid Spencerd798a512006-11-14 04:47:22 +0000926 getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000927 break;
928 case Instruction::ExtractElement: {
929 if (Oprnds.size() != 2)
930 error("Invalid extractelement instruction!");
931 Value *V1 = getValue(iType, Oprnds[0]);
932 Value *V2 = getValue(Type::UIntTyID, Oprnds[1]);
Chris Lattner59fecec2006-04-08 04:09:19 +0000933
Reid Spencer1628cec2006-10-26 06:15:43 +0000934 if (!ExtractElementInst::isValidOperands(V1, V2))
935 error("Invalid extractelement instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +0000936
Reid Spencer1628cec2006-10-26 06:15:43 +0000937 Result = new ExtractElementInst(V1, V2);
938 break;
Chris Lattnera65371e2006-05-26 18:42:34 +0000939 }
Reid Spencer1628cec2006-10-26 06:15:43 +0000940 case Instruction::InsertElement: {
941 const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
942 if (!PackedTy || Oprnds.size() != 3)
943 error("Invalid insertelement instruction!");
944
945 Value *V1 = getValue(iType, Oprnds[0]);
946 Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
947 Value *V3 = getValue(Type::UIntTyID, Oprnds[2]);
948
949 if (!InsertElementInst::isValidOperands(V1, V2, V3))
950 error("Invalid insertelement instruction!");
951 Result = new InsertElementInst(V1, V2, V3);
952 break;
953 }
954 case Instruction::ShuffleVector: {
955 const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
956 if (!PackedTy || Oprnds.size() != 3)
957 error("Invalid shufflevector instruction!");
958 Value *V1 = getValue(iType, Oprnds[0]);
959 Value *V2 = getValue(iType, Oprnds[1]);
960 const PackedType *EltTy =
961 PackedType::get(Type::UIntTy, PackedTy->getNumElements());
962 Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
963 if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
964 error("Invalid shufflevector instruction!");
965 Result = new ShuffleVectorInst(V1, V2, V3);
966 break;
967 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000968 case Instruction::Trunc:
969 if (Oprnds.size() != 2)
970 error("Invalid cast instruction!");
971 Result = new TruncInst(getValue(iType, Oprnds[0]),
972 getType(Oprnds[1]));
973 break;
974 case Instruction::ZExt:
975 if (Oprnds.size() != 2)
976 error("Invalid cast instruction!");
977 Result = new ZExtInst(getValue(iType, Oprnds[0]),
978 getType(Oprnds[1]));
979 break;
980 case Instruction::SExt:
Reid Spencer1628cec2006-10-26 06:15:43 +0000981 if (Oprnds.size() != 2)
982 error("Invalid Cast instruction!");
Reid Spencer3da59db2006-11-27 01:05:10 +0000983 Result = new SExtInst(getValue(iType, Oprnds[0]),
Reid Spencerd798a512006-11-14 04:47:22 +0000984 getType(Oprnds[1]));
Reid Spencer1628cec2006-10-26 06:15:43 +0000985 break;
Reid Spencer3da59db2006-11-27 01:05:10 +0000986 case Instruction::FPTrunc:
987 if (Oprnds.size() != 2)
988 error("Invalid cast instruction!");
989 Result = new FPTruncInst(getValue(iType, Oprnds[0]),
990 getType(Oprnds[1]));
991 break;
992 case Instruction::FPExt:
993 if (Oprnds.size() != 2)
994 error("Invalid cast instruction!");
995 Result = new FPExtInst(getValue(iType, Oprnds[0]),
996 getType(Oprnds[1]));
997 break;
998 case Instruction::UIToFP:
999 if (Oprnds.size() != 2)
1000 error("Invalid cast instruction!");
1001 Result = new UIToFPInst(getValue(iType, Oprnds[0]),
1002 getType(Oprnds[1]));
1003 break;
1004 case Instruction::SIToFP:
1005 if (Oprnds.size() != 2)
1006 error("Invalid cast instruction!");
1007 Result = new SIToFPInst(getValue(iType, Oprnds[0]),
1008 getType(Oprnds[1]));
1009 break;
1010 case Instruction::FPToUI:
1011 if (Oprnds.size() != 2)
1012 error("Invalid cast instruction!");
1013 Result = new FPToUIInst(getValue(iType, Oprnds[0]),
1014 getType(Oprnds[1]));
1015 break;
1016 case Instruction::FPToSI:
1017 if (Oprnds.size() != 2)
1018 error("Invalid cast instruction!");
1019 Result = new FPToSIInst(getValue(iType, Oprnds[0]),
1020 getType(Oprnds[1]));
1021 break;
1022 case Instruction::IntToPtr:
1023 if (Oprnds.size() != 2)
1024 error("Invalid cast instruction!");
1025 Result = new IntToPtrInst(getValue(iType, Oprnds[0]),
1026 getType(Oprnds[1]));
1027 break;
1028 case Instruction::PtrToInt:
1029 if (Oprnds.size() != 2)
1030 error("Invalid cast instruction!");
1031 Result = new PtrToIntInst(getValue(iType, Oprnds[0]),
1032 getType(Oprnds[1]));
1033 break;
1034 case Instruction::BitCast:
1035 if (Oprnds.size() != 2)
1036 error("Invalid cast instruction!");
1037 Result = new BitCastInst(getValue(iType, Oprnds[0]),
1038 getType(Oprnds[1]));
1039 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001040 case Instruction::Select:
1041 if (Oprnds.size() != 3)
1042 error("Invalid Select instruction!");
1043 Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]),
1044 getValue(iType, Oprnds[1]),
1045 getValue(iType, Oprnds[2]));
1046 break;
1047 case Instruction::PHI: {
1048 if (Oprnds.size() == 0 || (Oprnds.size() & 1))
1049 error("Invalid phi node encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001050
Reid Spencer1628cec2006-10-26 06:15:43 +00001051 PHINode *PN = new PHINode(InstTy);
1052 PN->reserveOperandSpace(Oprnds.size());
1053 for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2)
1054 PN->addIncoming(
1055 getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1]));
1056 Result = PN;
1057 break;
1058 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001059 case Instruction::Shl:
Reid Spencer3822ff52006-11-08 06:47:33 +00001060 case Instruction::LShr:
1061 case Instruction::AShr:
Reid Spencer1628cec2006-10-26 06:15:43 +00001062 Result = new ShiftInst(Instruction::OtherOps(Opcode),
1063 getValue(iType, Oprnds[0]),
1064 getValue(Type::UByteTyID, Oprnds[1]));
1065 break;
1066 case Instruction::Ret:
1067 if (Oprnds.size() == 0)
1068 Result = new ReturnInst();
1069 else if (Oprnds.size() == 1)
1070 Result = new ReturnInst(getValue(iType, Oprnds[0]));
1071 else
1072 error("Unrecognized instruction!");
1073 break;
1074
1075 case Instruction::Br:
1076 if (Oprnds.size() == 1)
1077 Result = new BranchInst(getBasicBlock(Oprnds[0]));
1078 else if (Oprnds.size() == 3)
1079 Result = new BranchInst(getBasicBlock(Oprnds[0]),
1080 getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2]));
1081 else
1082 error("Invalid number of operands for a 'br' instruction!");
1083 break;
1084 case Instruction::Switch: {
1085 if (Oprnds.size() & 1)
1086 error("Switch statement with odd number of arguments!");
1087
1088 SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]),
1089 getBasicBlock(Oprnds[1]),
1090 Oprnds.size()/2-1);
1091 for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2)
1092 I->addCase(cast<ConstantInt>(getValue(iType, Oprnds[i])),
1093 getBasicBlock(Oprnds[i+1]));
1094 Result = I;
1095 break;
1096 }
1097 case 58: // Call with extra operand for calling conv
1098 case 59: // tail call, Fast CC
1099 case 60: // normal call, Fast CC
1100 case 61: // tail call, C Calling Conv
1101 case Instruction::Call: { // Normal Call, C Calling Convention
1102 if (Oprnds.size() == 0)
1103 error("Invalid call instruction encountered!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001104 Value *F = getValue(iType, Oprnds[0]);
1105
1106 unsigned CallingConv = CallingConv::C;
1107 bool isTailCall = false;
1108
1109 if (Opcode == 61 || Opcode == 59)
1110 isTailCall = true;
1111
1112 if (Opcode == 58) {
1113 isTailCall = Oprnds.back() & 1;
1114 CallingConv = Oprnds.back() >> 1;
1115 Oprnds.pop_back();
1116 } else if (Opcode == 59 || Opcode == 60) {
1117 CallingConv = CallingConv::Fast;
1118 }
1119
1120 // Check to make sure we have a pointer to function type
1121 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
1122 if (PTy == 0) error("Call to non function pointer value!");
1123 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
1124 if (FTy == 0) error("Call to non function pointer value!");
1125
1126 std::vector<Value *> Params;
1127 if (!FTy->isVarArg()) {
1128 FunctionType::param_iterator It = FTy->param_begin();
1129
1130 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
1131 if (It == FTy->param_end())
1132 error("Invalid call instruction!");
1133 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
1134 }
1135 if (It != FTy->param_end())
Reid Spencer24399722004-07-09 22:21:33 +00001136 error("Invalid call instruction!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001137 } else {
1138 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
1139
1140 unsigned FirstVariableOperand;
1141 if (Oprnds.size() < FTy->getNumParams())
1142 error("Call instruction missing operands!");
1143
1144 // Read all of the fixed arguments
1145 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1146 Params.push_back(
1147 getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i]));
1148
1149 FirstVariableOperand = FTy->getNumParams();
1150
1151 if ((Oprnds.size()-FirstVariableOperand) & 1)
1152 error("Invalid call instruction!"); // Must be pairs of type/value
1153
1154 for (unsigned i = FirstVariableOperand, e = Oprnds.size();
1155 i != e; i += 2)
1156 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
Reid Spencer060d25d2004-06-29 23:29:38 +00001157 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001158
Reid Spencer1628cec2006-10-26 06:15:43 +00001159 Result = new CallInst(F, Params);
1160 if (isTailCall) cast<CallInst>(Result)->setTailCall();
1161 if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv);
1162 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001163 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001164 case Instruction::Invoke: { // Invoke C CC
1165 if (Oprnds.size() < 3)
1166 error("Invalid invoke instruction!");
1167 Value *F = getValue(iType, Oprnds[0]);
Reid Spencer060d25d2004-06-29 23:29:38 +00001168
Reid Spencer1628cec2006-10-26 06:15:43 +00001169 // Check to make sure we have a pointer to function type
1170 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
1171 if (PTy == 0)
1172 error("Invoke to non function pointer value!");
1173 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
1174 if (FTy == 0)
1175 error("Invoke to non function pointer value!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001176
Reid Spencer1628cec2006-10-26 06:15:43 +00001177 std::vector<Value *> Params;
1178 BasicBlock *Normal, *Except;
Reid Spencer3da59db2006-11-27 01:05:10 +00001179 unsigned CallingConv = Oprnds.back();
1180 Oprnds.pop_back();
Chris Lattnerdee199f2005-05-06 22:34:01 +00001181
Reid Spencer1628cec2006-10-26 06:15:43 +00001182 if (!FTy->isVarArg()) {
1183 Normal = getBasicBlock(Oprnds[1]);
1184 Except = getBasicBlock(Oprnds[2]);
Reid Spencer060d25d2004-06-29 23:29:38 +00001185
Reid Spencer1628cec2006-10-26 06:15:43 +00001186 FunctionType::param_iterator It = FTy->param_begin();
1187 for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
1188 if (It == FTy->param_end())
1189 error("Invalid invoke instruction!");
1190 Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
1191 }
1192 if (It != FTy->param_end())
Reid Spencer24399722004-07-09 22:21:33 +00001193 error("Invalid invoke instruction!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001194 } else {
1195 Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
1196
1197 Normal = getBasicBlock(Oprnds[0]);
1198 Except = getBasicBlock(Oprnds[1]);
1199
1200 unsigned FirstVariableArgument = FTy->getNumParams()+2;
1201 for (unsigned i = 2; i != FirstVariableArgument; ++i)
1202 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
1203 Oprnds[i]));
1204
1205 // Must be type/value pairs. If not, error out.
1206 if (Oprnds.size()-FirstVariableArgument & 1)
1207 error("Invalid invoke instruction!");
1208
1209 for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
1210 Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
Reid Spencer060d25d2004-06-29 23:29:38 +00001211 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001212
Reid Spencer1628cec2006-10-26 06:15:43 +00001213 Result = new InvokeInst(F, Normal, Except, Params);
1214 if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
1215 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001216 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001217 case Instruction::Malloc: {
1218 unsigned Align = 0;
1219 if (Oprnds.size() == 2)
1220 Align = (1 << Oprnds[1]) >> 1;
1221 else if (Oprnds.size() > 2)
1222 error("Invalid malloc instruction!");
1223 if (!isa<PointerType>(InstTy))
1224 error("Invalid malloc instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001225
Reid Spencer1628cec2006-10-26 06:15:43 +00001226 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
1227 getValue(Type::UIntTyID, Oprnds[0]), Align);
1228 break;
1229 }
1230 case Instruction::Alloca: {
1231 unsigned Align = 0;
1232 if (Oprnds.size() == 2)
1233 Align = (1 << Oprnds[1]) >> 1;
1234 else if (Oprnds.size() > 2)
1235 error("Invalid alloca instruction!");
1236 if (!isa<PointerType>(InstTy))
1237 error("Invalid alloca instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001238
Reid Spencer1628cec2006-10-26 06:15:43 +00001239 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
1240 getValue(Type::UIntTyID, Oprnds[0]), Align);
1241 break;
1242 }
1243 case Instruction::Free:
1244 if (!isa<PointerType>(InstTy))
1245 error("Invalid free instruction!");
1246 Result = new FreeInst(getValue(iType, Oprnds[0]));
1247 break;
1248 case Instruction::GetElementPtr: {
1249 if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
Misha Brukman8a96c532005-04-21 21:44:41 +00001250 error("Invalid getelementptr instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001251
Reid Spencer1628cec2006-10-26 06:15:43 +00001252 std::vector<Value*> Idx;
1253
1254 const Type *NextTy = InstTy;
1255 for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
1256 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
1257 if (!TopTy)
1258 error("Invalid getelementptr instruction!");
1259
1260 unsigned ValIdx = Oprnds[i];
1261 unsigned IdxTy = 0;
Reid Spencerd798a512006-11-14 04:47:22 +00001262 // Struct indices are always uints, sequential type indices can be
1263 // any of the 32 or 64-bit integer types. The actual choice of
1264 // type is encoded in the low two bits of the slot number.
1265 if (isa<StructType>(TopTy))
1266 IdxTy = Type::UIntTyID;
1267 else {
1268 switch (ValIdx & 3) {
1269 default:
1270 case 0: IdxTy = Type::UIntTyID; break;
1271 case 1: IdxTy = Type::IntTyID; break;
1272 case 2: IdxTy = Type::ULongTyID; break;
1273 case 3: IdxTy = Type::LongTyID; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001274 }
Reid Spencerd798a512006-11-14 04:47:22 +00001275 ValIdx >>= 2;
Reid Spencer060d25d2004-06-29 23:29:38 +00001276 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001277 Idx.push_back(getValue(IdxTy, ValIdx));
Reid Spencer1628cec2006-10-26 06:15:43 +00001278 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Reid Spencer060d25d2004-06-29 23:29:38 +00001279 }
1280
Reid Spencer1628cec2006-10-26 06:15:43 +00001281 Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
1282 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001283 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001284 case 62: // volatile load
1285 case Instruction::Load:
1286 if (Oprnds.size() != 1 || !isa<PointerType>(InstTy))
1287 error("Invalid load instruction!");
1288 Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62);
1289 break;
1290 case 63: // volatile store
1291 case Instruction::Store: {
1292 if (!isa<PointerType>(InstTy) || Oprnds.size() != 2)
1293 error("Invalid store instruction!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001294
Reid Spencer1628cec2006-10-26 06:15:43 +00001295 Value *Ptr = getValue(iType, Oprnds[1]);
1296 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
1297 Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr,
1298 Opcode == 63);
1299 break;
1300 }
1301 case Instruction::Unwind:
1302 if (Oprnds.size() != 0) error("Invalid unwind instruction!");
1303 Result = new UnwindInst();
1304 break;
1305 case Instruction::Unreachable:
1306 if (Oprnds.size() != 0) error("Invalid unreachable instruction!");
1307 Result = new UnreachableInst();
1308 break;
1309 } // end switch(Opcode)
1310 } // end if *normal*
Reid Spencer060d25d2004-06-29 23:29:38 +00001311
Reid Spencere1e96c02006-01-19 07:02:16 +00001312 BB->getInstList().push_back(Result);
1313
Reid Spencer060d25d2004-06-29 23:29:38 +00001314 unsigned TypeSlot;
1315 if (Result->getType() == InstTy)
1316 TypeSlot = iType;
1317 else
1318 TypeSlot = getTypeSlot(Result->getType());
1319
1320 insertValue(Result, TypeSlot, FunctionValues);
Reid Spencer060d25d2004-06-29 23:29:38 +00001321}
1322
Reid Spencer04cde2c2004-07-04 11:33:49 +00001323/// Get a particular numbered basic block, which might be a forward reference.
Reid Spencerd798a512006-11-14 04:47:22 +00001324/// This works together with ParseInstructionList to handle these forward
1325/// references in a clean manner. This function is used when constructing
1326/// phi, br, switch, and other instructions that reference basic blocks.
1327/// Blocks are numbered sequentially as they appear in the function.
Reid Spencer060d25d2004-06-29 23:29:38 +00001328BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) {
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001329 // Make sure there is room in the table...
1330 if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
1331
Reid Spencerd798a512006-11-14 04:47:22 +00001332 // First check to see if this is a backwards reference, i.e. this block
1333 // has already been created, or if the forward reference has already
Chris Lattner4ee8ef22003-10-08 22:52:54 +00001334 // been created.
1335 if (ParsedBasicBlocks[ID])
1336 return ParsedBasicBlocks[ID];
1337
1338 // Otherwise, the basic block has not yet been created. Do so and add it to
1339 // the ParsedBasicBlocks list.
1340 return ParsedBasicBlocks[ID] = new BasicBlock();
1341}
1342
Reid Spencer04cde2c2004-07-04 11:33:49 +00001343/// Parse all of the BasicBlock's & Instruction's in the body of a function.
Misha Brukman8a96c532005-04-21 21:44:41 +00001344/// In post 1.0 bytecode files, we no longer emit basic block individually,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001345/// in order to avoid per-basic-block overhead.
Reid Spencerd798a512006-11-14 04:47:22 +00001346/// @returns the number of basic blocks encountered.
Reid Spencer060d25d2004-06-29 23:29:38 +00001347unsigned BytecodeReader::ParseInstructionList(Function* F) {
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001348 unsigned BlockNo = 0;
1349 std::vector<unsigned> Args;
1350
Reid Spencer46b002c2004-07-11 17:28:43 +00001351 while (moreInBlock()) {
1352 if (Handler) Handler->handleBasicBlockBegin(BlockNo);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001353 BasicBlock *BB;
1354 if (ParsedBasicBlocks.size() == BlockNo)
1355 ParsedBasicBlocks.push_back(BB = new BasicBlock());
1356 else if (ParsedBasicBlocks[BlockNo] == 0)
1357 BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
1358 else
1359 BB = ParsedBasicBlocks[BlockNo];
1360 ++BlockNo;
1361 F->getBasicBlockList().push_back(BB);
1362
1363 // Read instructions into this basic block until we get to a terminator
Reid Spencer46b002c2004-07-11 17:28:43 +00001364 while (moreInBlock() && !BB->getTerminator())
Reid Spencer060d25d2004-06-29 23:29:38 +00001365 ParseInstruction(Args, BB);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001366
1367 if (!BB->getTerminator())
Reid Spencer24399722004-07-09 22:21:33 +00001368 error("Non-terminated basic block found!");
Reid Spencer5c15fe52004-07-05 00:57:50 +00001369
Reid Spencer46b002c2004-07-11 17:28:43 +00001370 if (Handler) Handler->handleBasicBlockEnd(BlockNo-1);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00001371 }
1372
1373 return BlockNo;
1374}
1375
Reid Spencer04cde2c2004-07-04 11:33:49 +00001376/// Parse a symbol table. This works for both module level and function
1377/// level symbol tables. For function level symbol tables, the CurrentFunction
1378/// parameter must be non-zero and the ST parameter must correspond to
1379/// CurrentFunction's symbol table. For Module level symbol tables, the
1380/// CurrentFunction argument must be zero.
Reid Spencer060d25d2004-06-29 23:29:38 +00001381void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
Reid Spencer04cde2c2004-07-04 11:33:49 +00001382 SymbolTable *ST) {
1383 if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
Reid Spencer060d25d2004-06-29 23:29:38 +00001384
Chris Lattner39cacce2003-10-10 05:43:47 +00001385 // Allow efficient basic block lookup by number.
1386 std::vector<BasicBlock*> BBMap;
1387 if (CurrentFunction)
1388 for (Function::iterator I = CurrentFunction->begin(),
1389 E = CurrentFunction->end(); I != E; ++I)
1390 BBMap.push_back(I);
1391
Reid Spencerd798a512006-11-14 04:47:22 +00001392 // Symtab block header: [num entries]
1393 unsigned NumEntries = read_vbr_uint();
1394 for (unsigned i = 0; i < NumEntries; ++i) {
1395 // Symtab entry: [def slot #][name]
1396 unsigned slot = read_vbr_uint();
1397 std::string Name = read_str();
1398 const Type* T = getType(slot);
1399 ST->insert(Name, T);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001400 }
1401
Reid Spencer46b002c2004-07-11 17:28:43 +00001402 while (moreInBlock()) {
Chris Lattner00950542001-06-06 20:29:01 +00001403 // Symtab block header: [num entries][type id number]
Reid Spencer060d25d2004-06-29 23:29:38 +00001404 unsigned NumEntries = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00001405 unsigned Typ = read_vbr_uint();
Chris Lattner1d670cc2001-09-07 16:37:43 +00001406
Chris Lattner7dc3a2e2003-10-13 14:57:53 +00001407 for (unsigned i = 0; i != NumEntries; ++i) {
Chris Lattner00950542001-06-06 20:29:01 +00001408 // Symtab entry: [def slot #][name]
Reid Spencer060d25d2004-06-29 23:29:38 +00001409 unsigned slot = read_vbr_uint();
1410 std::string Name = read_str();
Reid Spencerd798a512006-11-14 04:47:22 +00001411 Value *V = 0;
1412 if (Typ == Type::LabelTyID) {
1413 if (slot < BBMap.size())
1414 V = BBMap[slot];
Chris Lattner39cacce2003-10-10 05:43:47 +00001415 } else {
Reid Spencerd798a512006-11-14 04:47:22 +00001416 V = getValue(Typ, slot, false); // Find mapping...
Chris Lattner39cacce2003-10-10 05:43:47 +00001417 }
Reid Spencerd798a512006-11-14 04:47:22 +00001418 if (V == 0)
1419 error("Failed value look-up for name '" + Name + "'");
1420 V->setName(Name);
Chris Lattner00950542001-06-06 20:29:01 +00001421 }
1422 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001423 checkPastBlockEnd("Symbol Table");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001424 if (Handler) Handler->handleSymbolTableEnd();
Chris Lattner00950542001-06-06 20:29:01 +00001425}
1426
Misha Brukman8a96c532005-04-21 21:44:41 +00001427/// Read in the types portion of a compaction table.
Reid Spencer46b002c2004-07-11 17:28:43 +00001428void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00001429 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencerd798a512006-11-14 04:47:22 +00001430 unsigned TypeSlot = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001431 const Type *Typ = getGlobalTableType(TypeSlot);
Chris Lattner45b5dd22004-08-03 23:41:28 +00001432 CompactionTypes.push_back(std::make_pair(Typ, TypeSlot));
Reid Spencer46b002c2004-07-11 17:28:43 +00001433 if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001434 }
1435}
1436
1437/// Parse a compaction table.
Reid Spencer060d25d2004-06-29 23:29:38 +00001438void BytecodeReader::ParseCompactionTable() {
1439
Reid Spencer46b002c2004-07-11 17:28:43 +00001440 // Notify handler that we're beginning a compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001441 if (Handler) Handler->handleCompactionTableBegin();
1442
Reid Spencerd798a512006-11-14 04:47:22 +00001443 // Get the types for the compaction table.
1444 unsigned NumEntries = read_vbr_uint();
1445 ParseCompactionTypes(NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00001446
Reid Spencer46b002c2004-07-11 17:28:43 +00001447 // Compaction tables live in separate blocks so we have to loop
1448 // until we've read the whole thing.
1449 while (moreInBlock()) {
1450 // Read the number of Value* entries in the compaction table
Reid Spencer060d25d2004-06-29 23:29:38 +00001451 unsigned NumEntries = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001452 unsigned Ty = 0;
Reid Spencer060d25d2004-06-29 23:29:38 +00001453
Reid Spencer46b002c2004-07-11 17:28:43 +00001454 // Decode the type from value read in. Most compaction table
1455 // planes will have one or two entries in them. If that's the
1456 // case then the length is encoded in the bottom two bits and
1457 // the higher bits encode the type. This saves another VBR value.
Reid Spencer060d25d2004-06-29 23:29:38 +00001458 if ((NumEntries & 3) == 3) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001459 // In this case, both low-order bits are set (value 3). This
1460 // is a signal that the typeid follows.
Reid Spencer060d25d2004-06-29 23:29:38 +00001461 NumEntries >>= 2;
Reid Spencerd798a512006-11-14 04:47:22 +00001462 Ty = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001463 } else {
Reid Spencer46b002c2004-07-11 17:28:43 +00001464 // In this case, the low-order bits specify the number of entries
1465 // and the high order bits specify the type.
Reid Spencer060d25d2004-06-29 23:29:38 +00001466 Ty = NumEntries >> 2;
1467 NumEntries &= 3;
1468 }
1469
Reid Spencerd798a512006-11-14 04:47:22 +00001470 // Make sure we have enough room for the plane.
1471 if (Ty >= CompactionValues.size())
1472 CompactionValues.resize(Ty+1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001473
Reid Spencerd798a512006-11-14 04:47:22 +00001474 // Make sure the plane is empty or we have some kind of error.
1475 if (!CompactionValues[Ty].empty())
1476 error("Compaction table plane contains multiple entries!");
Reid Spencer04cde2c2004-07-04 11:33:49 +00001477
Reid Spencerd798a512006-11-14 04:47:22 +00001478 // Notify handler about the plane.
1479 if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001480
Reid Spencerd798a512006-11-14 04:47:22 +00001481 // Push the implicit zero.
1482 CompactionValues[Ty].push_back(Constant::getNullValue(getType(Ty)));
Reid Spencer46b002c2004-07-11 17:28:43 +00001483
Reid Spencerd798a512006-11-14 04:47:22 +00001484 // Read in each of the entries, put them in the compaction table
1485 // and notify the handler that we have a new compaction table value.
1486 for (unsigned i = 0; i != NumEntries; ++i) {
1487 unsigned ValSlot = read_vbr_uint();
1488 Value *V = getGlobalTableValue(Ty, ValSlot);
1489 CompactionValues[Ty].push_back(V);
1490 if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot);
Reid Spencer060d25d2004-06-29 23:29:38 +00001491 }
1492 }
Reid Spencer46b002c2004-07-11 17:28:43 +00001493 // Notify handler that the compaction table is done.
Reid Spencer04cde2c2004-07-04 11:33:49 +00001494 if (Handler) Handler->handleCompactionTableEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00001495}
Misha Brukman8a96c532005-04-21 21:44:41 +00001496
Reid Spencer46b002c2004-07-11 17:28:43 +00001497// Parse a single type. The typeid is read in first. If its a primitive type
1498// then nothing else needs to be read, we know how to instantiate it. If its
Misha Brukman8a96c532005-04-21 21:44:41 +00001499// a derived type, then additional data is read to fill out the type
Reid Spencer46b002c2004-07-11 17:28:43 +00001500// definition.
1501const Type *BytecodeReader::ParseType() {
Reid Spencerd798a512006-11-14 04:47:22 +00001502 unsigned PrimType = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001503 const Type *Result = 0;
1504 if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType)))
1505 return Result;
Misha Brukman8a96c532005-04-21 21:44:41 +00001506
Reid Spencer060d25d2004-06-29 23:29:38 +00001507 switch (PrimType) {
1508 case Type::FunctionTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001509 const Type *RetType = readType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001510
1511 unsigned NumParams = read_vbr_uint();
1512
1513 std::vector<const Type*> Params;
Misha Brukman8a96c532005-04-21 21:44:41 +00001514 while (NumParams--)
Reid Spencerd798a512006-11-14 04:47:22 +00001515 Params.push_back(readType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001516
1517 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1518 if (isVarArg) Params.pop_back();
1519
1520 Result = FunctionType::get(RetType, Params, isVarArg);
1521 break;
1522 }
1523 case Type::ArrayTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001524 const Type *ElementType = readType();
Reid Spencer060d25d2004-06-29 23:29:38 +00001525 unsigned NumElements = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001526 Result = ArrayType::get(ElementType, NumElements);
1527 break;
1528 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001529 case Type::PackedTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001530 const Type *ElementType = readType();
Brian Gaeke715c90b2004-08-20 06:00:58 +00001531 unsigned NumElements = read_vbr_uint();
1532 Result = PackedType::get(ElementType, NumElements);
1533 break;
1534 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001535 case Type::StructTyID: {
1536 std::vector<const Type*> Elements;
Reid Spencerd798a512006-11-14 04:47:22 +00001537 unsigned Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001538 while (Typ) { // List is terminated by void/0 typeid
1539 Elements.push_back(getType(Typ));
Reid Spencerd798a512006-11-14 04:47:22 +00001540 Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00001541 }
1542
1543 Result = StructType::get(Elements);
1544 break;
1545 }
1546 case Type::PointerTyID: {
Reid Spencerd798a512006-11-14 04:47:22 +00001547 Result = PointerType::get(readType());
Reid Spencer060d25d2004-06-29 23:29:38 +00001548 break;
1549 }
1550
1551 case Type::OpaqueTyID: {
1552 Result = OpaqueType::get();
1553 break;
1554 }
1555
1556 default:
Reid Spencer24399722004-07-09 22:21:33 +00001557 error("Don't know how to deserialize primitive type " + utostr(PrimType));
Reid Spencer060d25d2004-06-29 23:29:38 +00001558 break;
1559 }
Reid Spencer46b002c2004-07-11 17:28:43 +00001560 if (Handler) Handler->handleType(Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001561 return Result;
1562}
1563
Reid Spencer5b472d92004-08-21 20:49:23 +00001564// ParseTypes - We have to use this weird code to handle recursive
Reid Spencer060d25d2004-06-29 23:29:38 +00001565// types. We know that recursive types will only reference the current slab of
1566// values in the type plane, but they can forward reference types before they
1567// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
1568// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
1569// this ugly problem, we pessimistically insert an opaque type for each type we
1570// are about to read. This means that forward references will resolve to
1571// something and when we reread the type later, we can replace the opaque type
1572// with a new resolved concrete type.
1573//
Reid Spencer46b002c2004-07-11 17:28:43 +00001574void BytecodeReader::ParseTypes(TypeListTy &Tab, unsigned NumEntries){
Reid Spencer060d25d2004-06-29 23:29:38 +00001575 assert(Tab.size() == 0 && "should not have read type constants in before!");
1576
1577 // Insert a bunch of opaque types to be resolved later...
1578 Tab.reserve(NumEntries);
1579 for (unsigned i = 0; i != NumEntries; ++i)
1580 Tab.push_back(OpaqueType::get());
1581
Misha Brukman8a96c532005-04-21 21:44:41 +00001582 if (Handler)
Reid Spencer5b472d92004-08-21 20:49:23 +00001583 Handler->handleTypeList(NumEntries);
1584
Chris Lattnereebac5f2005-10-03 21:26:53 +00001585 // If we are about to resolve types, make sure the type cache is clear.
1586 if (NumEntries)
1587 ModuleTypeIDCache.clear();
1588
Reid Spencer060d25d2004-06-29 23:29:38 +00001589 // Loop through reading all of the types. Forward types will make use of the
1590 // opaque types just inserted.
1591 //
1592 for (unsigned i = 0; i != NumEntries; ++i) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001593 const Type* NewTy = ParseType();
Reid Spencer04cde2c2004-07-04 11:33:49 +00001594 const Type* OldTy = Tab[i].get();
Misha Brukman8a96c532005-04-21 21:44:41 +00001595 if (NewTy == 0)
Reid Spencer24399722004-07-09 22:21:33 +00001596 error("Couldn't parse type!");
Reid Spencer060d25d2004-06-29 23:29:38 +00001597
Misha Brukman8a96c532005-04-21 21:44:41 +00001598 // Don't directly push the new type on the Tab. Instead we want to replace
Reid Spencer060d25d2004-06-29 23:29:38 +00001599 // the opaque type we previously inserted with the new concrete value. This
1600 // approach helps with forward references to types. The refinement from the
1601 // abstract (opaque) type to the new type causes all uses of the abstract
1602 // type to use the concrete type (NewTy). This will also cause the opaque
1603 // type to be deleted.
1604 cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
1605
1606 // This should have replaced the old opaque type with the new type in the
1607 // value table... or with a preexisting type that was already in the system.
1608 // Let's just make sure it did.
1609 assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
1610 }
1611}
1612
Reid Spencer1628cec2006-10-26 06:15:43 +00001613// Upgrade obsolete constant expression opcodes (ver. 5 and prior) to the new
1614// values used after ver 6. bytecode format. The operands are provided to the
1615// function so that decisions based on the operand type can be made when
1616// auto-upgrading obsolete opcodes to the new ones.
Reid Spencer6996feb2006-11-08 21:27:54 +00001617// NOTE: This code needs to be kept synchronized with upgradeInstrOpcodes.
Reid Spencer1628cec2006-10-26 06:15:43 +00001618// We can't use that function because of that functions argument requirements.
1619// This function only deals with the subset of opcodes that are applicable to
Reid Spencer6996feb2006-11-08 21:27:54 +00001620// constant expressions and is therefore simpler than upgradeInstrOpcodes.
Reid Spencer3da59db2006-11-27 01:05:10 +00001621inline Constant *BytecodeReader::upgradeCEOpcodes(
1622 unsigned &Opcode, const std::vector<Constant*> &ArgVec, unsigned TypeID
Reid Spencer1628cec2006-10-26 06:15:43 +00001623) {
Reid Spencer6996feb2006-11-08 21:27:54 +00001624 // Determine if no upgrade necessary
1625 if (!hasSignlessDivRem && !hasSignlessShrCastSetcc)
Reid Spencer3da59db2006-11-27 01:05:10 +00001626 return 0;
Reid Spencer6996feb2006-11-08 21:27:54 +00001627
Reid Spencer6996feb2006-11-08 21:27:54 +00001628 // If this is bytecode version 6, that only had signed Rem and Div
1629 // instructions, then we must compensate for those two instructions only.
1630 // So that the switch statement below works, we're trying to turn this into
1631 // a version 5 opcode. To do that we must adjust the opcode to 10 (Div) if its
1632 // any of the UDiv, SDiv or FDiv instructions; or, adjust the opcode to
1633 // 11 (Rem) if its any of the URem, SRem, or FRem instructions; or, simply
1634 // decrement the instruction code if its beyond FRem.
1635 if (!hasSignlessDivRem) {
1636 // If its one of the signed Div/Rem opcodes, its fine the way it is
1637 if (Opcode >= 10 && Opcode <= 12) // UDiv through FDiv
1638 Opcode = 10; // Div
1639 else if (Opcode >=13 && Opcode <= 15) // URem through FRem
1640 Opcode = 11; // Rem
Reid Spencer967413f2006-11-18 04:37:19 +00001641 else if (Opcode >= 16 && Opcode <= 35) // And through Shr
Reid Spencer6996feb2006-11-08 21:27:54 +00001642 // Adjust for new instruction codes
1643 Opcode -= 4;
Reid Spencer967413f2006-11-18 04:37:19 +00001644 else if (Opcode >= 36 && Opcode <= 42) // Everything after Select
1645 // In vers 6 bytecode we eliminated the placeholders for the obsolete
1646 // VAARG and VANEXT instructions. Consequently those two slots were
1647 // filled starting with Select (36) which was 34. So now we only need
1648 // to subtract two. This circumvents hitting opcodes 32 and 33
1649 Opcode -= 2;
1650 else { // Opcode < 10 or > 42
1651 // No upgrade necessary.
1652 return 0;
1653 }
Reid Spencer6996feb2006-11-08 21:27:54 +00001654 }
1655
Reid Spencer1628cec2006-10-26 06:15:43 +00001656 switch (Opcode) {
1657 default: // Pass Through
1658 // If we don't match any of the cases here then the opcode is fine the
1659 // way it is.
1660 break;
1661 case 7: // Add
1662 Opcode = Instruction::Add;
1663 break;
1664 case 8: // Sub
1665 Opcode = Instruction::Sub;
1666 break;
1667 case 9: // Mul
1668 Opcode = Instruction::Mul;
1669 break;
1670 case 10: // Div
1671 // The type of the instruction is based on the operands. We need to select
1672 // either udiv or sdiv based on that type. This expression selects the
1673 // cases where the type is floating point or signed in which case we
1674 // generated an sdiv instruction.
1675 if (ArgVec[0]->getType()->isFloatingPoint())
1676 Opcode = Instruction::FDiv;
1677 else if (ArgVec[0]->getType()->isSigned())
1678 Opcode = Instruction::SDiv;
1679 else
1680 Opcode = Instruction::UDiv;
1681 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001682 case 11: // Rem
Reid Spencer0a783f72006-11-02 01:53:59 +00001683 // As with "Div", make the signed/unsigned or floating point Rem
1684 // instruction choice based on the type of the operands.
Reid Spencer1628cec2006-10-26 06:15:43 +00001685 if (ArgVec[0]->getType()->isFloatingPoint())
Reid Spencer0a783f72006-11-02 01:53:59 +00001686 Opcode = Instruction::FRem;
Reid Spencer1628cec2006-10-26 06:15:43 +00001687 else if (ArgVec[0]->getType()->isSigned())
Reid Spencer0a783f72006-11-02 01:53:59 +00001688 Opcode = Instruction::SRem;
Reid Spencer1628cec2006-10-26 06:15:43 +00001689 else
Reid Spencer0a783f72006-11-02 01:53:59 +00001690 Opcode = Instruction::URem;
Reid Spencer1628cec2006-10-26 06:15:43 +00001691 break;
Reid Spencer1628cec2006-10-26 06:15:43 +00001692 case 12: // And
1693 Opcode = Instruction::And;
1694 break;
1695 case 13: // Or
1696 Opcode = Instruction::Or;
1697 break;
1698 case 14: // Xor
1699 Opcode = Instruction::Xor;
1700 break;
1701 case 15: // SetEQ
1702 Opcode = Instruction::SetEQ;
1703 break;
1704 case 16: // SetNE
1705 Opcode = Instruction::SetNE;
1706 break;
1707 case 17: // SetLE
1708 Opcode = Instruction::SetLE;
1709 break;
1710 case 18: // SetGE
1711 Opcode = Instruction::SetGE;
1712 break;
1713 case 19: // SetLT
1714 Opcode = Instruction::SetLT;
1715 break;
1716 case 20: // SetGT
1717 Opcode = Instruction::SetGT;
1718 break;
1719 case 26: // GetElementPtr
1720 Opcode = Instruction::GetElementPtr;
1721 break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001722 case 28: { // Cast
1723 const Type *Ty = getType(TypeID);
1724 if (Ty == Type::BoolTy) {
1725 // The previous definition of cast to bool was a compare against zero.
1726 // We have to retain that semantic so we do it here.
1727 Opcode = Instruction::SetEQ;
1728 return ConstantExpr::get(Instruction::SetEQ, ArgVec[0],
1729 Constant::getNullValue(ArgVec[0]->getType()));
1730 } else if (ArgVec[0]->getType()->isFloatingPoint() &&
1731 isa<PointerType>(Ty)) {
1732 // Upgrade what is now an illegal cast (fp -> ptr) into two casts,
1733 // fp -> ui, and ui -> ptr
1734 Constant *CE = ConstantExpr::getFPToUI(ArgVec[0], Type::ULongTy);
1735 return ConstantExpr::getIntToPtr(CE, Ty);
1736 } else {
1737 Opcode = CastInst::getCastOpcode(ArgVec[0], Ty);
1738 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001739 break;
Reid Spencer3da59db2006-11-27 01:05:10 +00001740 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001741 case 30: // Shl
1742 Opcode = Instruction::Shl;
1743 break;
1744 case 31: // Shr
Reid Spencer3822ff52006-11-08 06:47:33 +00001745 if (ArgVec[0]->getType()->isSigned())
1746 Opcode = Instruction::AShr;
1747 else
1748 Opcode = Instruction::LShr;
Reid Spencer1628cec2006-10-26 06:15:43 +00001749 break;
1750 case 34: // Select
1751 Opcode = Instruction::Select;
1752 break;
1753 case 38: // ExtractElement
1754 Opcode = Instruction::ExtractElement;
1755 break;
1756 case 39: // InsertElement
1757 Opcode = Instruction::InsertElement;
1758 break;
1759 case 40: // ShuffleVector
1760 Opcode = Instruction::ShuffleVector;
1761 break;
1762 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001763 return 0;
Reid Spencer1628cec2006-10-26 06:15:43 +00001764}
1765
Reid Spencer04cde2c2004-07-04 11:33:49 +00001766/// Parse a single constant value
Chris Lattner3bc5a602006-01-25 23:08:15 +00001767Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
Reid Spencer060d25d2004-06-29 23:29:38 +00001768 // We must check for a ConstantExpr before switching by type because
1769 // a ConstantExpr can be of any type, and has no explicit value.
Misha Brukman8a96c532005-04-21 21:44:41 +00001770 //
Reid Spencer060d25d2004-06-29 23:29:38 +00001771 // 0 if not expr; numArgs if is expr
1772 unsigned isExprNumArgs = read_vbr_uint();
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001773
Reid Spencer060d25d2004-06-29 23:29:38 +00001774 if (isExprNumArgs) {
Reid Spencerd798a512006-11-14 04:47:22 +00001775 // 'undef' is encoded with 'exprnumargs' == 1.
1776 if (isExprNumArgs == 1)
1777 return UndefValue::get(getType(TypeID));
Misha Brukman8a96c532005-04-21 21:44:41 +00001778
Reid Spencerd798a512006-11-14 04:47:22 +00001779 // Inline asm is encoded with exprnumargs == ~0U.
1780 if (isExprNumArgs == ~0U) {
1781 std::string AsmStr = read_str();
1782 std::string ConstraintStr = read_str();
1783 unsigned Flags = read_vbr_uint();
Chris Lattner3bc5a602006-01-25 23:08:15 +00001784
Reid Spencerd798a512006-11-14 04:47:22 +00001785 const PointerType *PTy = dyn_cast<PointerType>(getType(TypeID));
1786 const FunctionType *FTy =
1787 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
1788
1789 if (!FTy || !InlineAsm::Verify(FTy, ConstraintStr))
1790 error("Invalid constraints for inline asm");
1791 if (Flags & ~1U)
1792 error("Invalid flags for inline asm");
1793 bool HasSideEffects = Flags & 1;
1794 return InlineAsm::get(FTy, AsmStr, ConstraintStr, HasSideEffects);
Chris Lattner3bc5a602006-01-25 23:08:15 +00001795 }
Reid Spencerd798a512006-11-14 04:47:22 +00001796
1797 --isExprNumArgs;
Chris Lattner3bc5a602006-01-25 23:08:15 +00001798
Reid Spencer060d25d2004-06-29 23:29:38 +00001799 // FIXME: Encoding of constant exprs could be much more compact!
1800 std::vector<Constant*> ArgVec;
1801 ArgVec.reserve(isExprNumArgs);
1802 unsigned Opcode = read_vbr_uint();
Chris Lattnera79e7cc2004-10-16 18:18:16 +00001803
Reid Spencer060d25d2004-06-29 23:29:38 +00001804 // Read the slot number and types of each of the arguments
1805 for (unsigned i = 0; i != isExprNumArgs; ++i) {
1806 unsigned ArgValSlot = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00001807 unsigned ArgTypeSlot = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001808
Reid Spencer060d25d2004-06-29 23:29:38 +00001809 // Get the arg value from its slot if it exists, otherwise a placeholder
1810 ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
1811 }
Misha Brukman8a96c532005-04-21 21:44:41 +00001812
Reid Spencer1628cec2006-10-26 06:15:43 +00001813 // Handle backwards compatibility for the opcode numbers
Reid Spencer3da59db2006-11-27 01:05:10 +00001814 if (Constant *C = upgradeCEOpcodes(Opcode, ArgVec, TypeID)) {
1815 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, C);
1816 return C;
1817 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001818
Reid Spencer060d25d2004-06-29 23:29:38 +00001819 // Construct a ConstantExpr of the appropriate kind
1820 if (isExprNumArgs == 1) { // All one-operand expressions
Reid Spencer3da59db2006-11-27 01:05:10 +00001821 if (!Instruction::isCast(Opcode))
Chris Lattner02dce162004-12-04 05:28:27 +00001822 error("Only cast instruction has one argument for ConstantExpr");
Reid Spencer46b002c2004-07-11 17:28:43 +00001823
Reid Spencer3da59db2006-11-27 01:05:10 +00001824 Constant *Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
Reid Spencer04cde2c2004-07-04 11:33:49 +00001825 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001826 return Result;
1827 } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
1828 std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
Reid Spencer3da59db2006-11-27 01:05:10 +00001829 Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001830 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001831 return Result;
1832 } else if (Opcode == Instruction::Select) {
Reid Spencer46b002c2004-07-11 17:28:43 +00001833 if (ArgVec.size() != 3)
1834 error("Select instruction must have three arguments.");
Misha Brukman8a96c532005-04-21 21:44:41 +00001835 Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
Reid Spencer04cde2c2004-07-04 11:33:49 +00001836 ArgVec[2]);
1837 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001838 return Result;
Robert Bocchinofee31b32006-01-10 19:04:39 +00001839 } else if (Opcode == Instruction::ExtractElement) {
Chris Lattner59fecec2006-04-08 04:09:19 +00001840 if (ArgVec.size() != 2 ||
1841 !ExtractElementInst::isValidOperands(ArgVec[0], ArgVec[1]))
1842 error("Invalid extractelement constand expr arguments");
Robert Bocchinofee31b32006-01-10 19:04:39 +00001843 Constant* Result = ConstantExpr::getExtractElement(ArgVec[0], ArgVec[1]);
1844 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1845 return Result;
Robert Bocchinob1f240b2006-01-17 20:06:35 +00001846 } else if (Opcode == Instruction::InsertElement) {
Chris Lattner59fecec2006-04-08 04:09:19 +00001847 if (ArgVec.size() != 3 ||
1848 !InsertElementInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2]))
1849 error("Invalid insertelement constand expr arguments");
1850
1851 Constant *Result =
Robert Bocchinob1f240b2006-01-17 20:06:35 +00001852 ConstantExpr::getInsertElement(ArgVec[0], ArgVec[1], ArgVec[2]);
1853 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1854 return Result;
Chris Lattner30b44b62006-04-08 01:17:59 +00001855 } else if (Opcode == Instruction::ShuffleVector) {
1856 if (ArgVec.size() != 3 ||
1857 !ShuffleVectorInst::isValidOperands(ArgVec[0], ArgVec[1], ArgVec[2]))
Chris Lattner59fecec2006-04-08 04:09:19 +00001858 error("Invalid shufflevector constant expr arguments.");
Chris Lattner30b44b62006-04-08 01:17:59 +00001859 Constant *Result =
1860 ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]);
1861 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
1862 return Result;
Reid Spencer060d25d2004-06-29 23:29:38 +00001863 } else { // All other 2-operand expressions
1864 Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001865 if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
Reid Spencer060d25d2004-06-29 23:29:38 +00001866 return Result;
1867 }
1868 }
Misha Brukman8a96c532005-04-21 21:44:41 +00001869
Reid Spencer060d25d2004-06-29 23:29:38 +00001870 // Ok, not an ConstantExpr. We now know how to read the given type...
1871 const Type *Ty = getType(TypeID);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001872 Constant *Result = 0;
Reid Spencer060d25d2004-06-29 23:29:38 +00001873 switch (Ty->getTypeID()) {
1874 case Type::BoolTyID: {
1875 unsigned Val = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001876 if (Val != 0 && Val != 1)
Reid Spencer24399722004-07-09 22:21:33 +00001877 error("Invalid boolean value read.");
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001878 Result = ConstantBool::get(Val == 1);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001879 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001880 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001881 }
1882
1883 case Type::UByteTyID: // Unsigned integer types...
1884 case Type::UShortTyID:
1885 case Type::UIntTyID: {
1886 unsigned Val = read_vbr_uint();
Reid Spencerb83eb642006-10-20 07:07:24 +00001887 if (!ConstantInt::isValueValidForType(Ty, uint64_t(Val)))
Reid Spencer24399722004-07-09 22:21:33 +00001888 error("Invalid unsigned byte/short/int read.");
Reid Spencerb83eb642006-10-20 07:07:24 +00001889 Result = ConstantInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001890 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001891 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001892 }
1893
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001894 case Type::ULongTyID:
Reid Spencerb83eb642006-10-20 07:07:24 +00001895 Result = ConstantInt::get(Ty, read_vbr_uint64());
Reid Spencer04cde2c2004-07-04 11:33:49 +00001896 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001897 break;
1898
Reid Spencer060d25d2004-06-29 23:29:38 +00001899 case Type::SByteTyID: // Signed integer types...
1900 case Type::ShortTyID:
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001901 case Type::IntTyID:
1902 case Type::LongTyID: {
Reid Spencer060d25d2004-06-29 23:29:38 +00001903 int64_t Val = read_vbr_int64();
Reid Spencerb83eb642006-10-20 07:07:24 +00001904 if (!ConstantInt::isValueValidForType(Ty, Val))
Reid Spencer24399722004-07-09 22:21:33 +00001905 error("Invalid signed byte/short/int/long read.");
Reid Spencerb83eb642006-10-20 07:07:24 +00001906 Result = ConstantInt::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001907 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001908 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001909 }
1910
1911 case Type::FloatTyID: {
Reid Spencer46b002c2004-07-11 17:28:43 +00001912 float Val;
1913 read_float(Val);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001914 Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001915 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001916 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001917 }
1918
1919 case Type::DoubleTyID: {
1920 double Val;
Reid Spencer46b002c2004-07-11 17:28:43 +00001921 read_double(Val);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001922 Result = ConstantFP::get(Ty, Val);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001923 if (Handler) Handler->handleConstantValue(Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001924 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001925 }
1926
Reid Spencer060d25d2004-06-29 23:29:38 +00001927 case Type::ArrayTyID: {
1928 const ArrayType *AT = cast<ArrayType>(Ty);
1929 unsigned NumElements = AT->getNumElements();
1930 unsigned TypeSlot = getTypeSlot(AT->getElementType());
1931 std::vector<Constant*> Elements;
1932 Elements.reserve(NumElements);
1933 while (NumElements--) // Read all of the elements of the constant.
1934 Elements.push_back(getConstantValue(TypeSlot,
1935 read_vbr_uint()));
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001936 Result = ConstantArray::get(AT, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001937 if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001938 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00001939 }
1940
1941 case Type::StructTyID: {
1942 const StructType *ST = cast<StructType>(Ty);
1943
1944 std::vector<Constant *> Elements;
1945 Elements.reserve(ST->getNumElements());
1946 for (unsigned i = 0; i != ST->getNumElements(); ++i)
1947 Elements.push_back(getConstantValue(ST->getElementType(i),
1948 read_vbr_uint()));
1949
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001950 Result = ConstantStruct::get(ST, Elements);
Reid Spencer04cde2c2004-07-04 11:33:49 +00001951 if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001952 break;
Misha Brukman8a96c532005-04-21 21:44:41 +00001953 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001954
Brian Gaeke715c90b2004-08-20 06:00:58 +00001955 case Type::PackedTyID: {
1956 const PackedType *PT = cast<PackedType>(Ty);
1957 unsigned NumElements = PT->getNumElements();
1958 unsigned TypeSlot = getTypeSlot(PT->getElementType());
1959 std::vector<Constant*> Elements;
1960 Elements.reserve(NumElements);
1961 while (NumElements--) // Read all of the elements of the constant.
1962 Elements.push_back(getConstantValue(TypeSlot,
1963 read_vbr_uint()));
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001964 Result = ConstantPacked::get(PT, Elements);
Brian Gaeke715c90b2004-08-20 06:00:58 +00001965 if (Handler) Handler->handleConstantPacked(PT, Elements, TypeSlot, Result);
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001966 break;
Brian Gaeke715c90b2004-08-20 06:00:58 +00001967 }
1968
Chris Lattner638c3812004-11-19 16:24:05 +00001969 case Type::PointerTyID: { // ConstantPointerRef value (backwards compat).
Reid Spencer060d25d2004-06-29 23:29:38 +00001970 const PointerType *PT = cast<PointerType>(Ty);
1971 unsigned Slot = read_vbr_uint();
Misha Brukman8a96c532005-04-21 21:44:41 +00001972
Reid Spencer060d25d2004-06-29 23:29:38 +00001973 // Check to see if we have already read this global variable...
1974 Value *Val = getValue(TypeID, Slot, false);
Reid Spencer060d25d2004-06-29 23:29:38 +00001975 if (Val) {
Chris Lattnerbcb11cf2004-07-27 02:34:49 +00001976 GlobalValue *GV = dyn_cast<GlobalValue>(Val);
1977 if (!GV) error("GlobalValue not in ValueTable!");
1978 if (Handler) Handler->handleConstantPointer(PT, Slot, GV);
1979 return GV;
Reid Spencer060d25d2004-06-29 23:29:38 +00001980 } else {
Reid Spencer24399722004-07-09 22:21:33 +00001981 error("Forward references are not allowed here.");
Reid Spencer060d25d2004-06-29 23:29:38 +00001982 }
Reid Spencer060d25d2004-06-29 23:29:38 +00001983 }
1984
1985 default:
Reid Spencer24399722004-07-09 22:21:33 +00001986 error("Don't know how to deserialize constant value of type '" +
Reid Spencer060d25d2004-06-29 23:29:38 +00001987 Ty->getDescription());
1988 break;
1989 }
Chris Lattnerd2cfb7a2006-04-07 05:00:02 +00001990
1991 // Check that we didn't read a null constant if they are implicit for this
1992 // type plane. Do not do this check for constantexprs, as they may be folded
1993 // to a null value in a way that isn't predicted when a .bc file is initially
1994 // produced.
1995 assert((!isa<Constant>(Result) || !cast<Constant>(Result)->isNullValue()) ||
1996 !hasImplicitNull(TypeID) &&
1997 "Cannot read null values from bytecode!");
1998 return Result;
Reid Spencer060d25d2004-06-29 23:29:38 +00001999}
2000
Misha Brukman8a96c532005-04-21 21:44:41 +00002001/// Resolve references for constants. This function resolves the forward
2002/// referenced constants in the ConstantFwdRefs map. It uses the
Reid Spencer04cde2c2004-07-04 11:33:49 +00002003/// replaceAllUsesWith method of Value class to substitute the placeholder
2004/// instance with the actual instance.
Chris Lattner389bd042004-12-09 06:19:44 +00002005void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Typ,
2006 unsigned Slot) {
Chris Lattner29b789b2003-11-19 17:27:18 +00002007 ConstantRefsType::iterator I =
Chris Lattner389bd042004-12-09 06:19:44 +00002008 ConstantFwdRefs.find(std::make_pair(Typ, Slot));
Chris Lattner29b789b2003-11-19 17:27:18 +00002009 if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
Chris Lattner00950542001-06-06 20:29:01 +00002010
Chris Lattner29b789b2003-11-19 17:27:18 +00002011 Value *PH = I->second; // Get the placeholder...
2012 PH->replaceAllUsesWith(NewV);
2013 delete PH; // Delete the old placeholder
2014 ConstantFwdRefs.erase(I); // Remove the map entry for it
Vikram S. Advec1e4a812002-07-14 23:04:18 +00002015}
2016
Reid Spencer04cde2c2004-07-04 11:33:49 +00002017/// Parse the constant strings section.
Reid Spencer060d25d2004-06-29 23:29:38 +00002018void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){
2019 for (; NumEntries; --NumEntries) {
Reid Spencerd798a512006-11-14 04:47:22 +00002020 unsigned Typ = read_vbr_uint();
Reid Spencer060d25d2004-06-29 23:29:38 +00002021 const Type *Ty = getType(Typ);
2022 if (!isa<ArrayType>(Ty))
Reid Spencer24399722004-07-09 22:21:33 +00002023 error("String constant data invalid!");
Misha Brukman8a96c532005-04-21 21:44:41 +00002024
Reid Spencer060d25d2004-06-29 23:29:38 +00002025 const ArrayType *ATy = cast<ArrayType>(Ty);
2026 if (ATy->getElementType() != Type::SByteTy &&
2027 ATy->getElementType() != Type::UByteTy)
Reid Spencer24399722004-07-09 22:21:33 +00002028 error("String constant data invalid!");
Misha Brukman8a96c532005-04-21 21:44:41 +00002029
Reid Spencer060d25d2004-06-29 23:29:38 +00002030 // Read character data. The type tells us how long the string is.
Misha Brukman8a96c532005-04-21 21:44:41 +00002031 char *Data = reinterpret_cast<char *>(alloca(ATy->getNumElements()));
Reid Spencer060d25d2004-06-29 23:29:38 +00002032 read_data(Data, Data+ATy->getNumElements());
Chris Lattner52e20b02003-03-19 20:54:26 +00002033
Reid Spencer060d25d2004-06-29 23:29:38 +00002034 std::vector<Constant*> Elements(ATy->getNumElements());
Reid Spencerb83eb642006-10-20 07:07:24 +00002035 const Type* ElemType = ATy->getElementType();
2036 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
2037 Elements[i] = ConstantInt::get(ElemType, (unsigned char)Data[i]);
Misha Brukman12c29d12003-09-22 23:38:23 +00002038
Reid Spencer060d25d2004-06-29 23:29:38 +00002039 // Create the constant, inserting it as needed.
2040 Constant *C = ConstantArray::get(ATy, Elements);
2041 unsigned Slot = insertValue(C, Typ, Tab);
Chris Lattner389bd042004-12-09 06:19:44 +00002042 ResolveReferencesToConstant(C, Typ, Slot);
Reid Spencer04cde2c2004-07-04 11:33:49 +00002043 if (Handler) Handler->handleConstantString(cast<ConstantArray>(C));
Reid Spencer060d25d2004-06-29 23:29:38 +00002044 }
Misha Brukman12c29d12003-09-22 23:38:23 +00002045}
2046
Reid Spencer04cde2c2004-07-04 11:33:49 +00002047/// Parse the constant pool.
Misha Brukman8a96c532005-04-21 21:44:41 +00002048void BytecodeReader::ParseConstantPool(ValueTable &Tab,
Reid Spencer04cde2c2004-07-04 11:33:49 +00002049 TypeListTy &TypeTab,
Reid Spencer46b002c2004-07-11 17:28:43 +00002050 bool isFunction) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00002051 if (Handler) Handler->handleGlobalConstantsBegin();
2052
2053 /// In LLVM 1.3 Type does not derive from Value so the types
2054 /// do not occupy a plane. Consequently, we read the types
2055 /// first in the constant pool.
Reid Spencerd798a512006-11-14 04:47:22 +00002056 if (isFunction) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00002057 unsigned NumEntries = read_vbr_uint();
Reid Spencer46b002c2004-07-11 17:28:43 +00002058 ParseTypes(TypeTab, NumEntries);
Reid Spencer04cde2c2004-07-04 11:33:49 +00002059 }
2060
Reid Spencer46b002c2004-07-11 17:28:43 +00002061 while (moreInBlock()) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002062 unsigned NumEntries = read_vbr_uint();
Reid Spencerd798a512006-11-14 04:47:22 +00002063 unsigned Typ = read_vbr_uint();
Reid Spencer04cde2c2004-07-04 11:33:49 +00002064
Reid Spencerd798a512006-11-14 04:47:22 +00002065 if (Typ == Type::VoidTyID) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00002066 /// Use of Type::VoidTyID is a misnomer. It actually means
2067 /// that the following plane is constant strings
Reid Spencer060d25d2004-06-29 23:29:38 +00002068 assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
2069 ParseStringConstants(NumEntries, Tab);
2070 } else {
2071 for (unsigned i = 0; i < NumEntries; ++i) {
Chris Lattner3bc5a602006-01-25 23:08:15 +00002072 Value *V = ParseConstantPoolValue(Typ);
2073 assert(V && "ParseConstantPoolValue returned NULL!");
2074 unsigned Slot = insertValue(V, Typ, Tab);
Chris Lattner29b789b2003-11-19 17:27:18 +00002075
Reid Spencer060d25d2004-06-29 23:29:38 +00002076 // If we are reading a function constant table, make sure that we adjust
2077 // the slot number to be the real global constant number.
2078 //
2079 if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
2080 ModuleValues[Typ])
2081 Slot += ModuleValues[Typ]->size();
Chris Lattner3bc5a602006-01-25 23:08:15 +00002082 if (Constant *C = dyn_cast<Constant>(V))
2083 ResolveReferencesToConstant(C, Typ, Slot);
Reid Spencer060d25d2004-06-29 23:29:38 +00002084 }
2085 }
2086 }
Chris Lattner02dce162004-12-04 05:28:27 +00002087
2088 // After we have finished parsing the constant pool, we had better not have
2089 // any dangling references left.
Reid Spencer3c391272004-12-04 22:19:53 +00002090 if (!ConstantFwdRefs.empty()) {
Reid Spencer3c391272004-12-04 22:19:53 +00002091 ConstantRefsType::const_iterator I = ConstantFwdRefs.begin();
Reid Spencer3c391272004-12-04 22:19:53 +00002092 Constant* missingConst = I->second;
Misha Brukman8a96c532005-04-21 21:44:41 +00002093 error(utostr(ConstantFwdRefs.size()) +
2094 " unresolved constant reference exist. First one is '" +
2095 missingConst->getName() + "' of type '" +
Chris Lattner389bd042004-12-09 06:19:44 +00002096 missingConst->getType()->getDescription() + "'.");
Reid Spencer3c391272004-12-04 22:19:53 +00002097 }
Chris Lattner02dce162004-12-04 05:28:27 +00002098
Reid Spencer060d25d2004-06-29 23:29:38 +00002099 checkPastBlockEnd("Constant Pool");
Reid Spencer04cde2c2004-07-04 11:33:49 +00002100 if (Handler) Handler->handleGlobalConstantsEnd();
Reid Spencer060d25d2004-06-29 23:29:38 +00002101}
Chris Lattner00950542001-06-06 20:29:01 +00002102
Reid Spencer04cde2c2004-07-04 11:33:49 +00002103/// Parse the contents of a function. Note that this function can be
2104/// called lazily by materializeFunction
2105/// @see materializeFunction
Reid Spencer46b002c2004-07-11 17:28:43 +00002106void BytecodeReader::ParseFunctionBody(Function* F) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002107
2108 unsigned FuncSize = BlockEnd - At;
Chris Lattnere3869c82003-04-16 21:16:05 +00002109 GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
2110
Reid Spencer060d25d2004-06-29 23:29:38 +00002111 unsigned LinkageType = read_vbr_uint();
Chris Lattnerc08912f2004-01-14 16:44:44 +00002112 switch (LinkageType) {
2113 case 0: Linkage = GlobalValue::ExternalLinkage; break;
2114 case 1: Linkage = GlobalValue::WeakLinkage; break;
2115 case 2: Linkage = GlobalValue::AppendingLinkage; break;
2116 case 3: Linkage = GlobalValue::InternalLinkage; break;
2117 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002118 case 5: Linkage = GlobalValue::DLLImportLinkage; break;
2119 case 6: Linkage = GlobalValue::DLLExportLinkage; break;
2120 case 7: Linkage = GlobalValue::ExternalWeakLinkage; break;
Reid Spencer060d25d2004-06-29 23:29:38 +00002121 default:
Reid Spencer24399722004-07-09 22:21:33 +00002122 error("Invalid linkage type for Function.");
Reid Spencer060d25d2004-06-29 23:29:38 +00002123 Linkage = GlobalValue::InternalLinkage;
2124 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00002125 }
Chris Lattnerd23b1d32001-11-26 18:56:10 +00002126
Reid Spencer46b002c2004-07-11 17:28:43 +00002127 F->setLinkage(Linkage);
Reid Spencer04cde2c2004-07-04 11:33:49 +00002128 if (Handler) Handler->handleFunctionBegin(F,FuncSize);
Chris Lattner00950542001-06-06 20:29:01 +00002129
Chris Lattner4ee8ef22003-10-08 22:52:54 +00002130 // Keep track of how many basic blocks we have read in...
2131 unsigned BlockNum = 0;
Chris Lattner89e02532004-01-18 21:08:15 +00002132 bool InsertedArguments = false;
Chris Lattner4ee8ef22003-10-08 22:52:54 +00002133
Reid Spencer060d25d2004-06-29 23:29:38 +00002134 BufPtr MyEnd = BlockEnd;
Reid Spencer46b002c2004-07-11 17:28:43 +00002135 while (At < MyEnd) {
Chris Lattner00950542001-06-06 20:29:01 +00002136 unsigned Type, Size;
Reid Spencer060d25d2004-06-29 23:29:38 +00002137 BufPtr OldAt = At;
2138 read_block(Type, Size);
Chris Lattner00950542001-06-06 20:29:01 +00002139
2140 switch (Type) {
Reid Spencerad89bd62004-07-25 18:07:36 +00002141 case BytecodeFormat::ConstantPoolBlockID:
Chris Lattner89e02532004-01-18 21:08:15 +00002142 if (!InsertedArguments) {
2143 // Insert arguments into the value table before we parse the first basic
2144 // block in the function, but after we potentially read in the
2145 // compaction table.
Reid Spencer04cde2c2004-07-04 11:33:49 +00002146 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00002147 InsertedArguments = true;
2148 }
2149
Reid Spencer04cde2c2004-07-04 11:33:49 +00002150 ParseConstantPool(FunctionValues, FunctionTypes, true);
Chris Lattner00950542001-06-06 20:29:01 +00002151 break;
2152
Reid Spencerad89bd62004-07-25 18:07:36 +00002153 case BytecodeFormat::CompactionTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002154 ParseCompactionTable();
Chris Lattner89e02532004-01-18 21:08:15 +00002155 break;
2156
Reid Spencerad89bd62004-07-25 18:07:36 +00002157 case BytecodeFormat::InstructionListBlockID: {
Chris Lattner89e02532004-01-18 21:08:15 +00002158 // Insert arguments into the value table before we parse the instruction
2159 // list for the function, but after we potentially read in the compaction
2160 // table.
2161 if (!InsertedArguments) {
Reid Spencer04cde2c2004-07-04 11:33:49 +00002162 insertArguments(F);
Chris Lattner89e02532004-01-18 21:08:15 +00002163 InsertedArguments = true;
2164 }
2165
Misha Brukman8a96c532005-04-21 21:44:41 +00002166 if (BlockNum)
Reid Spencer24399722004-07-09 22:21:33 +00002167 error("Already parsed basic blocks!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002168 BlockNum = ParseInstructionList(F);
Chris Lattner8d1dbd22003-12-01 07:05:31 +00002169 break;
2170 }
2171
Reid Spencerad89bd62004-07-25 18:07:36 +00002172 case BytecodeFormat::SymbolTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002173 ParseSymbolTable(F, &F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00002174 break;
2175
2176 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00002177 At += Size;
Misha Brukman8a96c532005-04-21 21:44:41 +00002178 if (OldAt > At)
Reid Spencer24399722004-07-09 22:21:33 +00002179 error("Wrapped around reading bytecode.");
Chris Lattner00950542001-06-06 20:29:01 +00002180 break;
2181 }
Reid Spencer060d25d2004-06-29 23:29:38 +00002182 BlockEnd = MyEnd;
Chris Lattner00950542001-06-06 20:29:01 +00002183 }
2184
Chris Lattner4ee8ef22003-10-08 22:52:54 +00002185 // Make sure there were no references to non-existant basic blocks.
2186 if (BlockNum != ParsedBasicBlocks.size())
Reid Spencer24399722004-07-09 22:21:33 +00002187 error("Illegal basic block operand reference");
Reid Spencer060d25d2004-06-29 23:29:38 +00002188
Chris Lattner4ee8ef22003-10-08 22:52:54 +00002189 ParsedBasicBlocks.clear();
2190
Chris Lattner97330cf2003-10-09 23:10:14 +00002191 // Resolve forward references. Replace any uses of a forward reference value
2192 // with the real value.
Chris Lattner8eb10ce2003-10-09 06:05:40 +00002193 while (!ForwardReferences.empty()) {
Chris Lattnerc4d69162004-12-09 04:51:50 +00002194 std::map<std::pair<unsigned,unsigned>, Value*>::iterator
2195 I = ForwardReferences.begin();
2196 Value *V = getValue(I->first.first, I->first.second, false);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00002197 Value *PlaceHolder = I->second;
Chris Lattnerc4d69162004-12-09 04:51:50 +00002198 PlaceHolder->replaceAllUsesWith(V);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00002199 ForwardReferences.erase(I);
Chris Lattner8eb10ce2003-10-09 06:05:40 +00002200 delete PlaceHolder;
Chris Lattner6e448022003-10-08 21:51:46 +00002201 }
Chris Lattner00950542001-06-06 20:29:01 +00002202
Reid Spencere2a5fb02006-01-27 11:49:27 +00002203 // If upgraded intrinsic functions were detected during reading of the
2204 // module information, then we need to look for instructions that need to
2205 // be upgraded. This can't be done while the instructions are read in because
2206 // additional instructions inserted mess up the slot numbering.
2207 if (!upgradedFunctions.empty()) {
2208 for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
2209 for (BasicBlock::iterator II = BI->begin(), IE = BI->end();
Jim Laskeyf4321a32006-03-13 13:07:37 +00002210 II != IE;)
2211 if (CallInst* CI = dyn_cast<CallInst>(II++)) {
Reid Spencere2a5fb02006-01-27 11:49:27 +00002212 std::map<Function*,Function*>::iterator FI =
2213 upgradedFunctions.find(CI->getCalledFunction());
Chris Lattnerbad08002006-03-02 23:59:12 +00002214 if (FI != upgradedFunctions.end())
2215 UpgradeIntrinsicCall(CI, FI->second);
Reid Spencere2a5fb02006-01-27 11:49:27 +00002216 }
2217 }
2218
Misha Brukman12c29d12003-09-22 23:38:23 +00002219 // Clear out function-level types...
Reid Spencer060d25d2004-06-29 23:29:38 +00002220 FunctionTypes.clear();
2221 CompactionTypes.clear();
2222 CompactionValues.clear();
2223 freeTable(FunctionValues);
2224
Reid Spencer04cde2c2004-07-04 11:33:49 +00002225 if (Handler) Handler->handleFunctionEnd(F);
Chris Lattner00950542001-06-06 20:29:01 +00002226}
2227
Reid Spencer04cde2c2004-07-04 11:33:49 +00002228/// This function parses LLVM functions lazily. It obtains the type of the
2229/// function and records where the body of the function is in the bytecode
Misha Brukman8a96c532005-04-21 21:44:41 +00002230/// buffer. The caller can then use the ParseNextFunction and
Reid Spencer04cde2c2004-07-04 11:33:49 +00002231/// ParseAllFunctionBodies to get handler events for the functions.
Reid Spencer060d25d2004-06-29 23:29:38 +00002232void BytecodeReader::ParseFunctionLazily() {
2233 if (FunctionSignatureList.empty())
Reid Spencer24399722004-07-09 22:21:33 +00002234 error("FunctionSignatureList empty!");
Chris Lattner89e02532004-01-18 21:08:15 +00002235
Reid Spencer060d25d2004-06-29 23:29:38 +00002236 Function *Func = FunctionSignatureList.back();
2237 FunctionSignatureList.pop_back();
Chris Lattner24102432004-01-18 22:35:34 +00002238
Reid Spencer060d25d2004-06-29 23:29:38 +00002239 // Save the information for future reading of the function
2240 LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd);
Chris Lattner89e02532004-01-18 21:08:15 +00002241
Misha Brukmana3e6ad62004-11-14 21:02:55 +00002242 // This function has a body but it's not loaded so it appears `External'.
2243 // Mark it as a `Ghost' instead to notify the users that it has a body.
2244 Func->setLinkage(GlobalValue::GhostLinkage);
2245
Reid Spencer060d25d2004-06-29 23:29:38 +00002246 // Pretend we've `parsed' this function
2247 At = BlockEnd;
2248}
Chris Lattner89e02532004-01-18 21:08:15 +00002249
Misha Brukman8a96c532005-04-21 21:44:41 +00002250/// The ParserFunction method lazily parses one function. Use this method to
2251/// casue the parser to parse a specific function in the module. Note that
2252/// this will remove the function from what is to be included by
Reid Spencer04cde2c2004-07-04 11:33:49 +00002253/// ParseAllFunctionBodies.
2254/// @see ParseAllFunctionBodies
2255/// @see ParseBytecode
Reid Spencer99655e12006-08-25 19:54:53 +00002256bool BytecodeReader::ParseFunction(Function* Func, std::string* ErrMsg) {
2257
2258 if (setjmp(context))
2259 return true;
2260
Reid Spencer060d25d2004-06-29 23:29:38 +00002261 // Find {start, end} pointers and slot in the map. If not there, we're done.
2262 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
Chris Lattner89e02532004-01-18 21:08:15 +00002263
Reid Spencer060d25d2004-06-29 23:29:38 +00002264 // Make sure we found it
Reid Spencer46b002c2004-07-11 17:28:43 +00002265 if (Fi == LazyFunctionLoadMap.end()) {
Reid Spencer24399722004-07-09 22:21:33 +00002266 error("Unrecognized function of type " + Func->getType()->getDescription());
Reid Spencer99655e12006-08-25 19:54:53 +00002267 return true;
Chris Lattner89e02532004-01-18 21:08:15 +00002268 }
2269
Reid Spencer060d25d2004-06-29 23:29:38 +00002270 BlockStart = At = Fi->second.Buf;
2271 BlockEnd = Fi->second.EndBuf;
Reid Spencer24399722004-07-09 22:21:33 +00002272 assert(Fi->first == Func && "Found wrong function?");
Reid Spencer060d25d2004-06-29 23:29:38 +00002273
2274 LazyFunctionLoadMap.erase(Fi);
2275
Reid Spencer46b002c2004-07-11 17:28:43 +00002276 this->ParseFunctionBody(Func);
Reid Spencer99655e12006-08-25 19:54:53 +00002277 return false;
Chris Lattner89e02532004-01-18 21:08:15 +00002278}
2279
Reid Spencer04cde2c2004-07-04 11:33:49 +00002280/// The ParseAllFunctionBodies method parses through all the previously
2281/// unparsed functions in the bytecode file. If you want to completely parse
2282/// a bytecode file, this method should be called after Parsebytecode because
2283/// Parsebytecode only records the locations in the bytecode file of where
2284/// the function definitions are located. This function uses that information
2285/// to materialize the functions.
2286/// @see ParseBytecode
Reid Spencer99655e12006-08-25 19:54:53 +00002287bool BytecodeReader::ParseAllFunctionBodies(std::string* ErrMsg) {
2288 if (setjmp(context))
2289 return true;
2290
Reid Spencer060d25d2004-06-29 23:29:38 +00002291 LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
2292 LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
Chris Lattner89e02532004-01-18 21:08:15 +00002293
Reid Spencer46b002c2004-07-11 17:28:43 +00002294 while (Fi != Fe) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002295 Function* Func = Fi->first;
2296 BlockStart = At = Fi->second.Buf;
2297 BlockEnd = Fi->second.EndBuf;
Chris Lattnerb52f1c22005-02-13 17:48:18 +00002298 ParseFunctionBody(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00002299 ++Fi;
2300 }
Chris Lattnerb52f1c22005-02-13 17:48:18 +00002301 LazyFunctionLoadMap.clear();
Reid Spencer99655e12006-08-25 19:54:53 +00002302 return false;
Reid Spencer060d25d2004-06-29 23:29:38 +00002303}
Chris Lattner89e02532004-01-18 21:08:15 +00002304
Reid Spencer04cde2c2004-07-04 11:33:49 +00002305/// Parse the global type list
Reid Spencer060d25d2004-06-29 23:29:38 +00002306void BytecodeReader::ParseGlobalTypes() {
Reid Spencer04cde2c2004-07-04 11:33:49 +00002307 // Read the number of types
2308 unsigned NumEntries = read_vbr_uint();
Reid Spencer46b002c2004-07-11 17:28:43 +00002309 ParseTypes(ModuleTypes, NumEntries);
Reid Spencer060d25d2004-06-29 23:29:38 +00002310}
2311
Reid Spencer04cde2c2004-07-04 11:33:49 +00002312/// Parse the Global info (types, global vars, constants)
Reid Spencer060d25d2004-06-29 23:29:38 +00002313void BytecodeReader::ParseModuleGlobalInfo() {
2314
Reid Spencer04cde2c2004-07-04 11:33:49 +00002315 if (Handler) Handler->handleModuleGlobalsBegin();
Chris Lattner00950542001-06-06 20:29:01 +00002316
Chris Lattner404cddf2005-11-12 01:33:40 +00002317 // SectionID - If a global has an explicit section specified, this map
2318 // remembers the ID until we can translate it into a string.
2319 std::map<GlobalValue*, unsigned> SectionID;
2320
Chris Lattner70cc3392001-09-10 07:58:01 +00002321 // Read global variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00002322 unsigned VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00002323 while (VarType != Type::VoidTyID) { // List is terminated by Void
Chris Lattner9dd87702004-04-03 23:43:42 +00002324 // VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
2325 // Linkage, bit4+ = slot#
2326 unsigned SlotNo = VarType >> 5;
2327 unsigned LinkageID = (VarType >> 2) & 7;
Reid Spencer060d25d2004-06-29 23:29:38 +00002328 bool isConstant = VarType & 1;
Chris Lattnerce5e04e2005-11-06 08:23:17 +00002329 bool hasInitializer = (VarType & 2) != 0;
Chris Lattner8eb52dd2005-11-06 07:11:04 +00002330 unsigned Alignment = 0;
Chris Lattner404cddf2005-11-12 01:33:40 +00002331 unsigned GlobalSectionID = 0;
Chris Lattner8eb52dd2005-11-06 07:11:04 +00002332
2333 // An extension word is present when linkage = 3 (internal) and hasinit = 0.
2334 if (LinkageID == 3 && !hasInitializer) {
2335 unsigned ExtWord = read_vbr_uint();
2336 // The extension word has this format: bit 0 = has initializer, bit 1-3 =
2337 // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
2338 hasInitializer = ExtWord & 1;
2339 LinkageID = (ExtWord >> 1) & 7;
2340 Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
Chris Lattner404cddf2005-11-12 01:33:40 +00002341
2342 if (ExtWord & (1 << 9)) // Has a section ID.
2343 GlobalSectionID = read_vbr_uint();
Chris Lattner8eb52dd2005-11-06 07:11:04 +00002344 }
Chris Lattnere3869c82003-04-16 21:16:05 +00002345
Chris Lattnerce5e04e2005-11-06 08:23:17 +00002346 GlobalValue::LinkageTypes Linkage;
Chris Lattnerc08912f2004-01-14 16:44:44 +00002347 switch (LinkageID) {
Chris Lattnerc08912f2004-01-14 16:44:44 +00002348 case 0: Linkage = GlobalValue::ExternalLinkage; break;
2349 case 1: Linkage = GlobalValue::WeakLinkage; break;
2350 case 2: Linkage = GlobalValue::AppendingLinkage; break;
2351 case 3: Linkage = GlobalValue::InternalLinkage; break;
2352 case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002353 case 5: Linkage = GlobalValue::DLLImportLinkage; break;
2354 case 6: Linkage = GlobalValue::DLLExportLinkage; break;
2355 case 7: Linkage = GlobalValue::ExternalWeakLinkage; break;
Misha Brukman8a96c532005-04-21 21:44:41 +00002356 default:
Reid Spencer24399722004-07-09 22:21:33 +00002357 error("Unknown linkage type: " + utostr(LinkageID));
Reid Spencer060d25d2004-06-29 23:29:38 +00002358 Linkage = GlobalValue::InternalLinkage;
2359 break;
Chris Lattnere3869c82003-04-16 21:16:05 +00002360 }
2361
2362 const Type *Ty = getType(SlotNo);
Chris Lattnere73bd452005-11-06 07:43:39 +00002363 if (!Ty)
Reid Spencer24399722004-07-09 22:21:33 +00002364 error("Global has no type! SlotNo=" + utostr(SlotNo));
Reid Spencer060d25d2004-06-29 23:29:38 +00002365
Chris Lattnere73bd452005-11-06 07:43:39 +00002366 if (!isa<PointerType>(Ty))
Reid Spencer24399722004-07-09 22:21:33 +00002367 error("Global not a pointer type! Ty= " + Ty->getDescription());
Chris Lattner70cc3392001-09-10 07:58:01 +00002368
Chris Lattner52e20b02003-03-19 20:54:26 +00002369 const Type *ElTy = cast<PointerType>(Ty)->getElementType();
Chris Lattnerd70684f2001-09-18 04:01:05 +00002370
Chris Lattner70cc3392001-09-10 07:58:01 +00002371 // Create the global variable...
Reid Spencer060d25d2004-06-29 23:29:38 +00002372 GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
Chris Lattner52e20b02003-03-19 20:54:26 +00002373 0, "", TheModule);
Chris Lattner8eb52dd2005-11-06 07:11:04 +00002374 GV->setAlignment(Alignment);
Chris Lattner29b789b2003-11-19 17:27:18 +00002375 insertValue(GV, SlotNo, ModuleValues);
Chris Lattner05950c32001-10-13 06:47:01 +00002376
Chris Lattner404cddf2005-11-12 01:33:40 +00002377 if (GlobalSectionID != 0)
2378 SectionID[GV] = GlobalSectionID;
2379
Reid Spencer060d25d2004-06-29 23:29:38 +00002380 unsigned initSlot = 0;
Misha Brukman8a96c532005-04-21 21:44:41 +00002381 if (hasInitializer) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002382 initSlot = read_vbr_uint();
2383 GlobalInits.push_back(std::make_pair(GV, initSlot));
2384 }
2385
2386 // Notify handler about the global value.
Chris Lattner4a242b32004-10-14 01:39:18 +00002387 if (Handler)
2388 Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot);
Reid Spencer060d25d2004-06-29 23:29:38 +00002389
2390 // Get next item
2391 VarType = read_vbr_uint();
Chris Lattner70cc3392001-09-10 07:58:01 +00002392 }
2393
Chris Lattner52e20b02003-03-19 20:54:26 +00002394 // Read the function objects for all of the functions that are coming
Chris Lattnera79e7cc2004-10-16 18:18:16 +00002395 unsigned FnSignature = read_vbr_uint();
Reid Spencer24399722004-07-09 22:21:33 +00002396
Chris Lattnera79e7cc2004-10-16 18:18:16 +00002397 // List is terminated by VoidTy.
Chris Lattnere73bd452005-11-06 07:43:39 +00002398 while (((FnSignature & (~0U >> 1)) >> 5) != Type::VoidTyID) {
2399 const Type *Ty = getType((FnSignature & (~0U >> 1)) >> 5);
Chris Lattner927b1852003-10-09 20:22:47 +00002400 if (!isa<PointerType>(Ty) ||
Reid Spencer060d25d2004-06-29 23:29:38 +00002401 !isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
Misha Brukman8a96c532005-04-21 21:44:41 +00002402 error("Function not a pointer to function type! Ty = " +
Reid Spencer46b002c2004-07-11 17:28:43 +00002403 Ty->getDescription());
Reid Spencer060d25d2004-06-29 23:29:38 +00002404 }
Chris Lattner8cdc6b72002-10-23 00:51:54 +00002405
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00002406 // We create functions by passing the underlying FunctionType to create...
Misha Brukman8a96c532005-04-21 21:44:41 +00002407 const FunctionType* FTy =
Reid Spencer060d25d2004-06-29 23:29:38 +00002408 cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
Chris Lattner00950542001-06-06 20:29:01 +00002409
Chris Lattner18549c22004-11-15 21:43:03 +00002410 // Insert the place holder.
Chris Lattner404cddf2005-11-12 01:33:40 +00002411 Function *Func = new Function(FTy, GlobalValue::ExternalLinkage,
Reid Spencer04cde2c2004-07-04 11:33:49 +00002412 "", TheModule);
Reid Spencere1e96c02006-01-19 07:02:16 +00002413
Chris Lattnere73bd452005-11-06 07:43:39 +00002414 insertValue(Func, (FnSignature & (~0U >> 1)) >> 5, ModuleValues);
Chris Lattnera79e7cc2004-10-16 18:18:16 +00002415
2416 // Flags are not used yet.
Chris Lattner97fbc502004-11-15 22:38:52 +00002417 unsigned Flags = FnSignature & 31;
Chris Lattner00950542001-06-06 20:29:01 +00002418
Chris Lattner97fbc502004-11-15 22:38:52 +00002419 // Save this for later so we know type of lazily instantiated functions.
2420 // Note that known-external functions do not have FunctionInfo blocks, so we
2421 // do not add them to the FunctionSignatureList.
2422 if ((Flags & (1 << 4)) == 0)
2423 FunctionSignatureList.push_back(Func);
Chris Lattner52e20b02003-03-19 20:54:26 +00002424
Chris Lattnere73bd452005-11-06 07:43:39 +00002425 // Get the calling convention from the low bits.
2426 unsigned CC = Flags & 15;
2427 unsigned Alignment = 0;
2428 if (FnSignature & (1 << 31)) { // Has extension word?
2429 unsigned ExtWord = read_vbr_uint();
2430 Alignment = (1 << (ExtWord & 31)) >> 1;
2431 CC |= ((ExtWord >> 5) & 15) << 4;
Chris Lattner404cddf2005-11-12 01:33:40 +00002432
2433 if (ExtWord & (1 << 10)) // Has a section ID.
2434 SectionID[Func] = read_vbr_uint();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002435
2436 // Parse external declaration linkage
2437 switch ((ExtWord >> 11) & 3) {
2438 case 0: break;
2439 case 1: Func->setLinkage(Function::DLLImportLinkage); break;
2440 case 2: Func->setLinkage(Function::ExternalWeakLinkage); break;
2441 default: assert(0 && "Unsupported external linkage");
2442 }
Chris Lattnere73bd452005-11-06 07:43:39 +00002443 }
2444
Chris Lattner54b369e2005-11-06 07:46:13 +00002445 Func->setCallingConv(CC-1);
Chris Lattnere73bd452005-11-06 07:43:39 +00002446 Func->setAlignment(Alignment);
Chris Lattner479ffeb2005-05-06 20:42:57 +00002447
Reid Spencer04cde2c2004-07-04 11:33:49 +00002448 if (Handler) Handler->handleFunctionDeclaration(Func);
Reid Spencer060d25d2004-06-29 23:29:38 +00002449
Chris Lattnera79e7cc2004-10-16 18:18:16 +00002450 // Get the next function signature.
2451 FnSignature = read_vbr_uint();
Chris Lattner00950542001-06-06 20:29:01 +00002452 }
2453
Misha Brukman8a96c532005-04-21 21:44:41 +00002454 // Now that the function signature list is set up, reverse it so that we can
Chris Lattner74734132002-08-17 22:01:27 +00002455 // remove elements efficiently from the back of the vector.
2456 std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
Chris Lattner00950542001-06-06 20:29:01 +00002457
Chris Lattner404cddf2005-11-12 01:33:40 +00002458 /// SectionNames - This contains the list of section names encoded in the
2459 /// moduleinfoblock. Functions and globals with an explicit section index
2460 /// into this to get their section name.
2461 std::vector<std::string> SectionNames;
2462
Reid Spencerd798a512006-11-14 04:47:22 +00002463 // Read in the dependent library information.
2464 unsigned num_dep_libs = read_vbr_uint();
2465 std::string dep_lib;
2466 while (num_dep_libs--) {
2467 dep_lib = read_str();
2468 TheModule->addLibrary(dep_lib);
Reid Spencer5b472d92004-08-21 20:49:23 +00002469 if (Handler)
Reid Spencerd798a512006-11-14 04:47:22 +00002470 Handler->handleDependentLibrary(dep_lib);
Reid Spencerad89bd62004-07-25 18:07:36 +00002471 }
2472
Reid Spencerd798a512006-11-14 04:47:22 +00002473 // Read target triple and place into the module.
2474 std::string triple = read_str();
2475 TheModule->setTargetTriple(triple);
2476 if (Handler)
2477 Handler->handleTargetTriple(triple);
2478
2479 if (At != BlockEnd) {
2480 // If the file has section info in it, read the section names now.
2481 unsigned NumSections = read_vbr_uint();
2482 while (NumSections--)
2483 SectionNames.push_back(read_str());
2484 }
2485
2486 // If the file has module-level inline asm, read it now.
2487 if (At != BlockEnd)
2488 TheModule->setModuleInlineAsm(read_str());
2489
Chris Lattner404cddf2005-11-12 01:33:40 +00002490 // If any globals are in specified sections, assign them now.
2491 for (std::map<GlobalValue*, unsigned>::iterator I = SectionID.begin(), E =
2492 SectionID.end(); I != E; ++I)
2493 if (I->second) {
2494 if (I->second > SectionID.size())
2495 error("SectionID out of range for global!");
2496 I->first->setSection(SectionNames[I->second-1]);
2497 }
Reid Spencerad89bd62004-07-25 18:07:36 +00002498
Chris Lattner00950542001-06-06 20:29:01 +00002499 // This is for future proofing... in the future extra fields may be added that
2500 // we don't understand, so we transparently ignore them.
2501 //
Reid Spencer060d25d2004-06-29 23:29:38 +00002502 At = BlockEnd;
2503
Reid Spencer04cde2c2004-07-04 11:33:49 +00002504 if (Handler) Handler->handleModuleGlobalsEnd();
Chris Lattner00950542001-06-06 20:29:01 +00002505}
2506
Reid Spencer04cde2c2004-07-04 11:33:49 +00002507/// Parse the version information and decode it by setting flags on the
2508/// Reader that enable backward compatibility of the reader.
Reid Spencer060d25d2004-06-29 23:29:38 +00002509void BytecodeReader::ParseVersionInfo() {
2510 unsigned Version = read_vbr_uint();
Chris Lattner036b8aa2003-03-06 17:55:45 +00002511
2512 // Unpack version number: low four bits are for flags, top bits = version
Chris Lattnerd445c6b2003-08-24 13:47:36 +00002513 Module::Endianness Endianness;
2514 Module::PointerSize PointerSize;
2515 Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
2516 PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
2517
2518 bool hasNoEndianness = Version & 4;
2519 bool hasNoPointerSize = Version & 8;
Misha Brukman8a96c532005-04-21 21:44:41 +00002520
Chris Lattnerd445c6b2003-08-24 13:47:36 +00002521 RevisionNum = Version >> 4;
Chris Lattnere3869c82003-04-16 21:16:05 +00002522
Reid Spencerd798a512006-11-14 04:47:22 +00002523 // Default the backwards compatibility flag values for the current BC version
Reid Spencer6996feb2006-11-08 21:27:54 +00002524 hasSignlessDivRem = false;
2525 hasSignlessShrCastSetcc = false;
Chris Lattner036b8aa2003-03-06 17:55:45 +00002526
Reid Spencer1628cec2006-10-26 06:15:43 +00002527 // Determine which backwards compatibility flags to set based on the
2528 // bytecode file's version number
Chris Lattner036b8aa2003-03-06 17:55:45 +00002529 switch (RevisionNum) {
Reid Spencerd798a512006-11-14 04:47:22 +00002530 case 0: // LLVM 1.0, 1.1 (Released)
2531 case 1: // LLVM 1.2 (Released)
2532 case 2: // 1.2.5 (Not Released)
2533 case 3: // LLVM 1.3 (Released)
2534 case 4: // 1.3.1 (Not Released)
2535 error("Old bytecode formats no longer supported");
2536 break;
Reid Spencer04cde2c2004-07-04 11:33:49 +00002537
Reid Spencerd798a512006-11-14 04:47:22 +00002538 case 5: // 1.4 (Released)
Reid Spencer6996feb2006-11-08 21:27:54 +00002539 // In version 6, the Div and Rem instructions were converted to their
2540 // signed and floating point counterparts: UDiv, SDiv, FDiv, URem, SRem,
2541 // and FRem. Versions prior to 6 need to indicate that they have the
2542 // signless Div and Rem instructions.
2543 hasSignlessDivRem = true;
2544
2545 // FALL THROUGH
2546
Reid Spencerd798a512006-11-14 04:47:22 +00002547 case 6: // 1.9 (Released)
Reid Spencer1628cec2006-10-26 06:15:43 +00002548 // In version 5 and prior, instructions were signless while integer types
2549 // were signed. In version 6, instructions became signed and types became
2550 // signless. For example in version 5 we have the DIV instruction but in
2551 // version 6 we have FDIV, SDIV and UDIV to replace it. This caused a
2552 // renumbering of the instruction codes in version 6 that must be dealt with
2553 // when reading old bytecode files.
Reid Spencer6996feb2006-11-08 21:27:54 +00002554 hasSignlessShrCastSetcc = true;
Reid Spencer1628cec2006-10-26 06:15:43 +00002555
2556 // FALL THROUGH
Reid Spencer6996feb2006-11-08 21:27:54 +00002557
2558 case 7:
Chris Lattnera79e7cc2004-10-16 18:18:16 +00002559 break;
2560
Chris Lattner036b8aa2003-03-06 17:55:45 +00002561 default:
Reid Spencer24399722004-07-09 22:21:33 +00002562 error("Unknown bytecode version number: " + itostr(RevisionNum));
Chris Lattner036b8aa2003-03-06 17:55:45 +00002563 }
2564
Chris Lattnerd445c6b2003-08-24 13:47:36 +00002565 if (hasNoEndianness) Endianness = Module::AnyEndianness;
2566 if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
Chris Lattner76e38962003-04-22 18:15:10 +00002567
Brian Gaekefe2102b2004-07-14 20:33:13 +00002568 TheModule->setEndianness(Endianness);
2569 TheModule->setPointerSize(PointerSize);
2570
Reid Spencer46b002c2004-07-11 17:28:43 +00002571 if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize);
Chris Lattner036b8aa2003-03-06 17:55:45 +00002572}
2573
Reid Spencer04cde2c2004-07-04 11:33:49 +00002574/// Parse a whole module.
Reid Spencer060d25d2004-06-29 23:29:38 +00002575void BytecodeReader::ParseModule() {
Chris Lattner00950542001-06-06 20:29:01 +00002576 unsigned Type, Size;
Chris Lattner00950542001-06-06 20:29:01 +00002577
Reid Spencer060d25d2004-06-29 23:29:38 +00002578 FunctionSignatureList.clear(); // Just in case...
Chris Lattner00950542001-06-06 20:29:01 +00002579
2580 // Read into instance variables...
Reid Spencer060d25d2004-06-29 23:29:38 +00002581 ParseVersionInfo();
Chris Lattner00950542001-06-06 20:29:01 +00002582
Reid Spencer060d25d2004-06-29 23:29:38 +00002583 bool SeenModuleGlobalInfo = false;
2584 bool SeenGlobalTypePlane = false;
2585 BufPtr MyEnd = BlockEnd;
2586 while (At < MyEnd) {
2587 BufPtr OldAt = At;
2588 read_block(Type, Size);
2589
Chris Lattner00950542001-06-06 20:29:01 +00002590 switch (Type) {
Reid Spencer060d25d2004-06-29 23:29:38 +00002591
Reid Spencerad89bd62004-07-25 18:07:36 +00002592 case BytecodeFormat::GlobalTypePlaneBlockID:
Reid Spencer46b002c2004-07-11 17:28:43 +00002593 if (SeenGlobalTypePlane)
Reid Spencer24399722004-07-09 22:21:33 +00002594 error("Two GlobalTypePlane Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002595
Reid Spencer5b472d92004-08-21 20:49:23 +00002596 if (Size > 0)
2597 ParseGlobalTypes();
Reid Spencer060d25d2004-06-29 23:29:38 +00002598 SeenGlobalTypePlane = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00002599 break;
2600
Misha Brukman8a96c532005-04-21 21:44:41 +00002601 case BytecodeFormat::ModuleGlobalInfoBlockID:
Reid Spencer46b002c2004-07-11 17:28:43 +00002602 if (SeenModuleGlobalInfo)
Reid Spencer24399722004-07-09 22:21:33 +00002603 error("Two ModuleGlobalInfo Blocks Encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002604 ParseModuleGlobalInfo();
2605 SeenModuleGlobalInfo = true;
Chris Lattner52e20b02003-03-19 20:54:26 +00002606 break;
2607
Reid Spencerad89bd62004-07-25 18:07:36 +00002608 case BytecodeFormat::ConstantPoolBlockID:
Reid Spencer04cde2c2004-07-04 11:33:49 +00002609 ParseConstantPool(ModuleValues, ModuleTypes,false);
Chris Lattner00950542001-06-06 20:29:01 +00002610 break;
2611
Reid Spencerad89bd62004-07-25 18:07:36 +00002612 case BytecodeFormat::FunctionBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002613 ParseFunctionLazily();
Chris Lattner00950542001-06-06 20:29:01 +00002614 break;
Chris Lattner00950542001-06-06 20:29:01 +00002615
Reid Spencerad89bd62004-07-25 18:07:36 +00002616 case BytecodeFormat::SymbolTableBlockID:
Reid Spencer060d25d2004-06-29 23:29:38 +00002617 ParseSymbolTable(0, &TheModule->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +00002618 break;
Reid Spencer060d25d2004-06-29 23:29:38 +00002619
Chris Lattner00950542001-06-06 20:29:01 +00002620 default:
Reid Spencer060d25d2004-06-29 23:29:38 +00002621 At += Size;
2622 if (OldAt > At) {
Reid Spencer46b002c2004-07-11 17:28:43 +00002623 error("Unexpected Block of Type #" + utostr(Type) + " encountered!");
Reid Spencer060d25d2004-06-29 23:29:38 +00002624 }
Chris Lattner00950542001-06-06 20:29:01 +00002625 break;
2626 }
Reid Spencer060d25d2004-06-29 23:29:38 +00002627 BlockEnd = MyEnd;
Chris Lattner00950542001-06-06 20:29:01 +00002628 }
2629
Chris Lattner52e20b02003-03-19 20:54:26 +00002630 // After the module constant pool has been read, we can safely initialize
2631 // global variables...
2632 while (!GlobalInits.empty()) {
2633 GlobalVariable *GV = GlobalInits.back().first;
2634 unsigned Slot = GlobalInits.back().second;
2635 GlobalInits.pop_back();
2636
2637 // Look up the initializer value...
Chris Lattner29b789b2003-11-19 17:27:18 +00002638 // FIXME: Preserve this type ID!
Reid Spencer060d25d2004-06-29 23:29:38 +00002639
2640 const llvm::PointerType* GVType = GV->getType();
2641 unsigned TypeSlot = getTypeSlot(GVType->getElementType());
Chris Lattner93361992004-01-15 18:45:25 +00002642 if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
Misha Brukman8a96c532005-04-21 21:44:41 +00002643 if (GV->hasInitializer())
Reid Spencer24399722004-07-09 22:21:33 +00002644 error("Global *already* has an initializer?!");
Reid Spencer04cde2c2004-07-04 11:33:49 +00002645 if (Handler) Handler->handleGlobalInitializer(GV,CV);
Chris Lattner93361992004-01-15 18:45:25 +00002646 GV->setInitializer(CV);
Chris Lattner52e20b02003-03-19 20:54:26 +00002647 } else
Reid Spencer24399722004-07-09 22:21:33 +00002648 error("Cannot find initializer value.");
Chris Lattner52e20b02003-03-19 20:54:26 +00002649 }
2650
Chris Lattneraba5ff52005-05-05 20:57:00 +00002651 if (!ConstantFwdRefs.empty())
2652 error("Use of undefined constants in a module");
2653
Reid Spencer060d25d2004-06-29 23:29:38 +00002654 /// Make sure we pulled them all out. If we didn't then there's a declaration
2655 /// but a missing body. That's not allowed.
Misha Brukman12c29d12003-09-22 23:38:23 +00002656 if (!FunctionSignatureList.empty())
Reid Spencer24399722004-07-09 22:21:33 +00002657 error("Function declared, but bytecode stream ended before definition");
Chris Lattner00950542001-06-06 20:29:01 +00002658}
2659
Reid Spencer04cde2c2004-07-04 11:33:49 +00002660/// This function completely parses a bytecode buffer given by the \p Buf
2661/// and \p Length parameters.
Anton Korobeynikov7d515442006-09-01 20:35:17 +00002662bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
Reid Spencer233fe722006-08-22 16:09:19 +00002663 const std::string &ModuleID,
2664 std::string* ErrMsg) {
Misha Brukmane0dd0d42003-09-23 16:15:29 +00002665
Reid Spencer233fe722006-08-22 16:09:19 +00002666 /// We handle errors by
2667 if (setjmp(context)) {
2668 // Cleanup after error
2669 if (Handler) Handler->handleError(ErrorMsg);
Reid Spencer060d25d2004-06-29 23:29:38 +00002670 freeState();
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00002671 delete TheModule;
2672 TheModule = 0;
Chris Lattner3bdad692004-11-15 21:55:33 +00002673 if (decompressedBlock != 0 ) {
Reid Spencer61aaf2e2004-11-14 21:59:21 +00002674 ::free(decompressedBlock);
Chris Lattner3bdad692004-11-15 21:55:33 +00002675 decompressedBlock = 0;
2676 }
Reid Spencer233fe722006-08-22 16:09:19 +00002677 // Set caller's error message, if requested
2678 if (ErrMsg)
2679 *ErrMsg = ErrorMsg;
2680 // Indicate an error occurred
2681 return true;
Chris Lattner2a7b6ba2003-03-06 17:15:19 +00002682 }
Reid Spencer233fe722006-08-22 16:09:19 +00002683
2684 RevisionNum = 0;
2685 At = MemStart = BlockStart = Buf;
2686 MemEnd = BlockEnd = Buf + Length;
2687
2688 // Create the module
2689 TheModule = new Module(ModuleID);
2690
2691 if (Handler) Handler->handleStart(TheModule, Length);
2692
2693 // Read the four bytes of the signature.
2694 unsigned Sig = read_uint();
2695
2696 // If this is a compressed file
2697 if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) {
2698
2699 // Invoke the decompression of the bytecode. Note that we have to skip the
2700 // file's magic number which is not part of the compressed block. Hence,
2701 // the Buf+4 and Length-4. The result goes into decompressedBlock, a data
2702 // member for retention until BytecodeReader is destructed.
2703 unsigned decompressedLength = Compressor::decompressToNewBuffer(
2704 (char*)Buf+4,Length-4,decompressedBlock);
2705
2706 // We must adjust the buffer pointers used by the bytecode reader to point
2707 // into the new decompressed block. After decompression, the
2708 // decompressedBlock will point to a contiguous memory area that has
2709 // the decompressed data.
2710 At = MemStart = BlockStart = Buf = (BufPtr) decompressedBlock;
2711 MemEnd = BlockEnd = Buf + decompressedLength;
2712
2713 // else if this isn't a regular (uncompressed) bytecode file, then its
2714 // and error, generate that now.
2715 } else if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) {
2716 error("Invalid bytecode signature: " + utohexstr(Sig));
2717 }
2718
2719 // Tell the handler we're starting a module
2720 if (Handler) Handler->handleModuleBegin(ModuleID);
2721
2722 // Get the module block and size and verify. This is handled specially
2723 // because the module block/size is always written in long format. Other
2724 // blocks are written in short format so the read_block method is used.
2725 unsigned Type, Size;
2726 Type = read_uint();
2727 Size = read_uint();
2728 if (Type != BytecodeFormat::ModuleBlockID) {
2729 error("Expected Module Block! Type:" + utostr(Type) + ", Size:"
2730 + utostr(Size));
2731 }
2732
2733 // It looks like the darwin ranlib program is broken, and adds trailing
2734 // garbage to the end of some bytecode files. This hack allows the bc
2735 // reader to ignore trailing garbage on bytecode files.
2736 if (At + Size < MemEnd)
2737 MemEnd = BlockEnd = At+Size;
2738
2739 if (At + Size != MemEnd)
2740 error("Invalid Top Level Block Length! Type:" + utostr(Type)
2741 + ", Size:" + utostr(Size));
2742
2743 // Parse the module contents
2744 this->ParseModule();
2745
2746 // Check for missing functions
2747 if (hasFunctions())
2748 error("Function expected, but bytecode stream ended!");
2749
2750 // Look for intrinsic functions to upgrade, upgrade them, and save the
2751 // mapping from old function to new for use later when instructions are
2752 // converted.
2753 for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
2754 FI != FE; ++FI)
2755 if (Function* newF = UpgradeIntrinsicFunction(FI)) {
2756 upgradedFunctions.insert(std::make_pair(FI, newF));
2757 FI->setName("");
2758 }
2759
2760 // Tell the handler we're done with the module
2761 if (Handler)
2762 Handler->handleModuleEnd(ModuleID);
2763
2764 // Tell the handler we're finished the parse
2765 if (Handler) Handler->handleFinish();
2766
2767 return false;
2768
Chris Lattner00950542001-06-06 20:29:01 +00002769}
Reid Spencer060d25d2004-06-29 23:29:38 +00002770
2771//===----------------------------------------------------------------------===//
2772//=== Default Implementations of Handler Methods
2773//===----------------------------------------------------------------------===//
2774
2775BytecodeHandler::~BytecodeHandler() {}
Reid Spencer060d25d2004-06-29 23:29:38 +00002776