2001-10-24 01:15:12 +00:00
|
|
|
//===- Reader.cpp - Code to read bytecode files ---------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This library implements the functionality defined in llvm/Bytecode/Reader.h
|
|
|
|
//
|
|
|
|
// Note that this library should be as fast as possible, reentrant, and
|
|
|
|
// threadsafe!!
|
|
|
|
//
|
|
|
|
// TODO: Allow passing in an option to ignore the symbol table
|
|
|
|
//
|
2001-10-24 01:15:12 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
#include "Reader.h"
|
|
|
|
#include "llvm/Bytecode/BytecodeHandler.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Constants.h"
|
2004-07-04 11:33:49 +00:00
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Bytecode/Format.h"
|
2004-06-29 23:29:38 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2003-09-22 23:38:23 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2004-06-29 23:29:38 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
namespace {
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
/// @brief A class for maintaining the slot number definition
|
2004-07-11 17:28:43 +00:00
|
|
|
/// as a placeholder for the actual definition for forward constants defs.
|
|
|
|
class ConstantPlaceHolder : public ConstantExpr {
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned ID;
|
2004-07-11 17:28:43 +00:00
|
|
|
ConstantPlaceHolder(); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT
|
2004-06-29 23:29:38 +00:00
|
|
|
public:
|
2004-07-11 17:28:43 +00:00
|
|
|
ConstantPlaceHolder(const Type *Ty, unsigned id)
|
|
|
|
: ConstantExpr(Instruction::UserOp1, Constant::getNullValue(Ty), Ty),
|
|
|
|
ID(id) {}
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned getID() { return ID; }
|
|
|
|
};
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
}
|
2003-10-04 20:00:03 +00:00
|
|
|
|
2004-07-09 22:21:33 +00:00
|
|
|
// Provide some details on error
|
|
|
|
inline void BytecodeReader::error(std::string err) {
|
|
|
|
err += " (Vers=" ;
|
|
|
|
err += itostr(RevisionNum) ;
|
|
|
|
err += ", Pos=" ;
|
|
|
|
err += itostr(At-MemStart);
|
|
|
|
err += ")";
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bytecode Reading Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Determine if the current block being read contains any more data.
|
2004-06-29 23:29:38 +00:00
|
|
|
inline bool BytecodeReader::moreInBlock() {
|
|
|
|
return At < BlockEnd;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Throw an error if we've read past the end of the current block
|
2004-06-29 23:29:38 +00:00
|
|
|
inline void BytecodeReader::checkPastBlockEnd(const char * block_name) {
|
2004-07-11 17:28:43 +00:00
|
|
|
if (At > BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error(std::string("Attempt to read past the end of ") + block_name + " block.");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Align the buffer position to a 32 bit boundary
|
2004-06-29 23:29:38 +00:00
|
|
|
inline void BytecodeReader::align32() {
|
|
|
|
BufPtr Save = At;
|
|
|
|
At = (const unsigned char *)((unsigned long)(At+3) & (~3UL));
|
2004-07-11 17:28:43 +00:00
|
|
|
if (At > Save)
|
|
|
|
if (Handler) Handler->handleAlignment(At - Save);
|
2004-06-29 23:29:38 +00:00
|
|
|
if (At > BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data while aligning!");
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2003-10-08 21:18:57 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a whole unsigned integer
|
2004-06-29 23:29:38 +00:00
|
|
|
inline unsigned BytecodeReader::read_uint() {
|
|
|
|
if (At+4 > BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data reading uint!");
|
2004-06-29 23:29:38 +00:00
|
|
|
At += 4;
|
|
|
|
return At[-4] | (At[-3] << 8) | (At[-2] << 16) | (At[-1] << 24);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a variable-bit-rate encoded unsigned integer
|
2004-06-29 23:29:38 +00:00
|
|
|
inline unsigned BytecodeReader::read_vbr_uint() {
|
|
|
|
unsigned Shift = 0;
|
|
|
|
unsigned Result = 0;
|
|
|
|
BufPtr Save = At;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (At == BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data reading vbr_uint!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Result |= (unsigned)((*At++) & 0x7F) << Shift;
|
|
|
|
Shift += 7;
|
|
|
|
} while (At[-1] & 0x80);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleVBR32(At-Save);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a variable-bit-rate encoded unsigned 64-bit integer.
|
2004-06-29 23:29:38 +00:00
|
|
|
inline uint64_t BytecodeReader::read_vbr_uint64() {
|
|
|
|
unsigned Shift = 0;
|
|
|
|
uint64_t Result = 0;
|
|
|
|
BufPtr Save = At;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (At == BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data reading vbr_uint64!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Result |= (uint64_t)((*At++) & 0x7F) << Shift;
|
|
|
|
Shift += 7;
|
|
|
|
} while (At[-1] & 0x80);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleVBR64(At-Save);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a variable-bit-rate encoded signed 64-bit integer.
|
2004-06-29 23:29:38 +00:00
|
|
|
inline int64_t BytecodeReader::read_vbr_int64() {
|
|
|
|
uint64_t R = read_vbr_uint64();
|
|
|
|
if (R & 1) {
|
|
|
|
if (R != 1)
|
|
|
|
return -(int64_t)(R >> 1);
|
|
|
|
else // There is no such thing as -0 with integers. "-0" really means
|
|
|
|
// 0x8000000000000000.
|
|
|
|
return 1LL << 63;
|
|
|
|
} else
|
|
|
|
return (int64_t)(R >> 1);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a pascal-style string (length followed by text)
|
2004-06-29 23:29:38 +00:00
|
|
|
inline std::string BytecodeReader::read_str() {
|
|
|
|
unsigned Size = read_vbr_uint();
|
|
|
|
const unsigned char *OldAt = At;
|
|
|
|
At += Size;
|
|
|
|
if (At > BlockEnd) // Size invalid?
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data reading a string!");
|
2004-06-29 23:29:38 +00:00
|
|
|
return std::string((char*)OldAt, Size);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read an arbitrary block of data
|
2004-06-29 23:29:38 +00:00
|
|
|
inline void BytecodeReader::read_data(void *Ptr, void *End) {
|
|
|
|
unsigned char *Start = (unsigned char *)Ptr;
|
|
|
|
unsigned Amount = (unsigned char *)End - Start;
|
|
|
|
if (At+Amount > BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Ran out of data!");
|
2004-06-29 23:29:38 +00:00
|
|
|
std::copy(At, At+Amount, Start);
|
|
|
|
At += Amount;
|
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
/// Read a float value in little-endian order
|
|
|
|
inline void BytecodeReader::read_float(float& FloatVal) {
|
|
|
|
/// FIXME: This is a broken implementation! It reads
|
|
|
|
/// it in a platform-specific endianess. Need to make
|
|
|
|
/// it little endian always.
|
|
|
|
read_data(&FloatVal, &FloatVal+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a double value in little-endian order
|
|
|
|
inline void BytecodeReader::read_double(double& DoubleVal) {
|
|
|
|
/// FIXME: This is a broken implementation! It reads
|
|
|
|
/// it in a platform-specific endianess. Need to make
|
|
|
|
/// it little endian always.
|
|
|
|
read_data(&DoubleVal, &DoubleVal+1);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Read a block header and obtain its type and size
|
2004-06-29 23:29:38 +00:00
|
|
|
inline void BytecodeReader::read_block(unsigned &Type, unsigned &Size) {
|
|
|
|
Type = read_uint();
|
|
|
|
Size = read_uint();
|
|
|
|
BlockStart = At;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (At + Size > BlockEnd)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Attempt to size a block past end of memory");
|
2004-06-29 23:29:38 +00:00
|
|
|
BlockEnd = At + Size;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleBlock(Type, BlockStart, Size);
|
2004-07-04 11:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// In LLVM 1.2 and before, Types were derived from Value and so they were
|
|
|
|
/// written as part of the type planes along with any other Value. In LLVM
|
|
|
|
/// 1.3 this changed so that Type does not derive from Value. Consequently,
|
|
|
|
/// the BytecodeReader's containers for Values can't contain Types because
|
|
|
|
/// there's no inheritance relationship. This means that the "Type Type"
|
|
|
|
/// plane is defunct along with the Type::TypeTyID TypeID. In LLVM 1.3
|
|
|
|
/// whenever a bytecode construct must have both types and values together,
|
|
|
|
/// the types are always read/written first and then the Values. Furthermore
|
|
|
|
/// since Type::TypeTyID no longer exists, its value (12) now corresponds to
|
|
|
|
/// Type::LabelTyID. In order to overcome this we must "sanitize" all the
|
|
|
|
/// type TypeIDs we encounter. For LLVM 1.3 bytecode files, there's no change.
|
|
|
|
/// For LLVM 1.2 and before, this function will decrement the type id by
|
|
|
|
/// one to account for the missing Type::TypeTyID enumerator if the value is
|
|
|
|
/// larger than 12 (Type::LabelTyID). If the value is exactly 12, then this
|
|
|
|
/// function returns true, otherwise false. This helps detect situations
|
|
|
|
/// where the pre 1.3 bytecode is indicating that what follows is a type.
|
|
|
|
/// @returns true iff type id corresponds to pre 1.3 "type type"
|
2004-07-11 17:28:43 +00:00
|
|
|
inline bool BytecodeReader::sanitizeTypeId(unsigned &TypeId) {
|
|
|
|
if (hasTypeDerivedFromValue) { /// do nothing if 1.3 or later
|
|
|
|
if (TypeId == Type::LabelTyID) {
|
2004-07-04 11:33:49 +00:00
|
|
|
TypeId = Type::VoidTyID; // sanitize it
|
|
|
|
return true; // indicate we got TypeTyID in pre 1.3 bytecode
|
2004-07-11 17:28:43 +00:00
|
|
|
} else if (TypeId > Type::LabelTyID)
|
2004-07-04 11:33:49 +00:00
|
|
|
--TypeId; // shift all planes down because type type plane is missing
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads a vbr uint to read in a type id and does the necessary
|
|
|
|
/// conversion on it by calling sanitizeTypeId.
|
|
|
|
/// @returns true iff \p TypeId read corresponds to a pre 1.3 "type type"
|
|
|
|
/// @see sanitizeTypeId
|
|
|
|
inline bool BytecodeReader::read_typeid(unsigned &TypeId) {
|
|
|
|
TypeId = read_vbr_uint();
|
|
|
|
return sanitizeTypeId(TypeId);
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IR Lookup Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Determine if a type id has an implicit null value
|
2004-07-11 17:28:43 +00:00
|
|
|
inline bool BytecodeReader::hasImplicitNull(unsigned TyID) {
|
2004-06-29 23:29:38 +00:00
|
|
|
if (!hasExplicitPrimitiveZeros)
|
2004-07-04 11:33:49 +00:00
|
|
|
return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
|
2004-06-29 23:29:38 +00:00
|
|
|
return TyID >= Type::FirstDerivedTyID;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Obtain a type given a typeid and account for things like compaction tables,
|
|
|
|
/// function level vs module level, and the offsetting for the primitive types.
|
2004-06-29 23:29:38 +00:00
|
|
|
const Type *BytecodeReader::getType(unsigned ID) {
|
2004-01-18 21:08:15 +00:00
|
|
|
if (ID < Type::FirstDerivedTyID)
|
2004-06-17 18:19:28 +00:00
|
|
|
if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
|
2003-10-09 20:22:47 +00:00
|
|
|
return T; // Asked for a primitive type...
|
2003-10-08 21:18:57 +00:00
|
|
|
|
|
|
|
// Otherwise, derived types need offset...
|
2004-01-18 21:08:15 +00:00
|
|
|
ID -= Type::FirstDerivedTyID;
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (!CompactionTypes.empty()) {
|
|
|
|
if (ID >= CompactionTypes.size())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Type ID out of range for compaction table!");
|
2004-06-29 23:29:38 +00:00
|
|
|
return CompactionTypes[ID];
|
2004-01-18 21:08:15 +00:00
|
|
|
}
|
2003-10-08 21:18:57 +00:00
|
|
|
|
|
|
|
// Is it a module-level type?
|
2004-07-11 17:28:43 +00:00
|
|
|
if (ID < ModuleTypes.size())
|
|
|
|
return ModuleTypes[ID].get();
|
2003-10-08 21:18:57 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Nope, is it a function-level type?
|
|
|
|
ID -= ModuleTypes.size();
|
|
|
|
if (ID < FunctionTypes.size())
|
|
|
|
return FunctionTypes[ID].get();
|
2003-10-08 21:18:57 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
error("Illegal type reference!");
|
|
|
|
return Type::VoidTy;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Get a sanitized type id. This just makes sure that the \p ID
|
|
|
|
/// is both sanitized and not the "type type" of pre-1.3 bytecode.
|
|
|
|
/// @see sanitizeTypeId
|
|
|
|
inline const Type* BytecodeReader::getSanitizedType(unsigned& ID) {
|
2004-07-11 17:28:43 +00:00
|
|
|
if (sanitizeTypeId(ID))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid type id encountered");
|
2004-07-04 11:33:49 +00:00
|
|
|
return getType(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method just saves some coding. It uses read_typeid to read
|
2004-07-09 22:21:33 +00:00
|
|
|
/// in a sanitized type id, errors that its not the type type, and
|
2004-07-04 11:33:49 +00:00
|
|
|
/// then calls getType to return the type value.
|
|
|
|
inline const Type* BytecodeReader::readSanitizedType() {
|
|
|
|
unsigned ID;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(ID))
|
|
|
|
error("Invalid type id encountered");
|
2004-07-04 11:33:49 +00:00
|
|
|
return getType(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the slot number associated with a type accounting for primitive
|
|
|
|
/// types, compaction tables, and function level vs module level.
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
|
|
|
|
if (Ty->isPrimitiveType())
|
|
|
|
return Ty->getTypeID();
|
2004-01-17 23:25:43 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Scan the compaction table for the type if needed.
|
|
|
|
if (!CompactionTypes.empty()) {
|
2004-07-11 17:28:43 +00:00
|
|
|
std::vector<const Type*>::const_iterator I =
|
|
|
|
find(CompactionTypes.begin(), CompactionTypes.end(), Ty);
|
2003-11-19 17:27:18 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
if (I == CompactionTypes.end())
|
|
|
|
error("Couldn't find type specified in compaction table!");
|
|
|
|
return Type::FirstDerivedTyID + (&*I - &CompactionTypes[0]);
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Check the function level types first...
|
|
|
|
TypeListTy::iterator I = find(FunctionTypes.begin(), FunctionTypes.end(), Ty);
|
2003-11-19 17:27:18 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (I != FunctionTypes.end())
|
2004-07-11 17:28:43 +00:00
|
|
|
return Type::FirstDerivedTyID + ModuleTypes.size() +
|
|
|
|
(&*I - &FunctionTypes[0]);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Check the module level types now...
|
|
|
|
I = find(ModuleTypes.begin(), ModuleTypes.end(), Ty);
|
|
|
|
if (I == ModuleTypes.end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Didn't find type in ModuleTypes.");
|
2004-06-29 23:29:38 +00:00
|
|
|
return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
|
2003-03-19 20:54:26 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This is just like getType, but when a compaction table is in use, it is
|
|
|
|
/// ignored. It also ignores function level types.
|
|
|
|
/// @see getType
|
2004-06-29 23:29:38 +00:00
|
|
|
const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
|
|
|
|
if (Slot < Type::FirstDerivedTyID) {
|
|
|
|
const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
|
2004-07-11 17:28:43 +00:00
|
|
|
if (!Ty)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Not a primitive type ID?");
|
2004-06-29 23:29:38 +00:00
|
|
|
return Ty;
|
|
|
|
}
|
|
|
|
Slot -= Type::FirstDerivedTyID;
|
|
|
|
if (Slot >= ModuleTypes.size())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Illegal compaction table type reference!");
|
2004-06-29 23:29:38 +00:00
|
|
|
return ModuleTypes[Slot];
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This is just like getTypeSlot, but when a compaction table is in use, it
|
|
|
|
/// is ignored. It also ignores function level types.
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
|
|
|
|
if (Ty->isPrimitiveType())
|
|
|
|
return Ty->getTypeID();
|
|
|
|
TypeListTy::iterator I = find(ModuleTypes.begin(),
|
2004-07-04 11:33:49 +00:00
|
|
|
ModuleTypes.end(), Ty);
|
2004-06-29 23:29:38 +00:00
|
|
|
if (I == ModuleTypes.end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Didn't find type in ModuleTypes.");
|
2004-06-29 23:29:38 +00:00
|
|
|
return Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Retrieve a value of a given type and slot number, possibly creating
|
|
|
|
/// it if it doesn't already exist.
|
2004-06-29 23:29:38 +00:00
|
|
|
Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
|
2003-10-08 22:52:54 +00:00
|
|
|
assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
|
2003-10-08 21:18:57 +00:00
|
|
|
unsigned Num = oNum;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
// If there is a compaction table active, it defines the low-level numbers.
|
|
|
|
// If not, the module values define the low-level numbers.
|
2004-06-29 23:29:38 +00:00
|
|
|
if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
|
|
|
|
if (Num < CompactionValues[type].size())
|
|
|
|
return CompactionValues[type][Num];
|
|
|
|
Num -= CompactionValues[type].size();
|
2004-01-18 21:08:15 +00:00
|
|
|
} else {
|
2004-06-29 23:29:38 +00:00
|
|
|
// By default, the global type id is the type id passed in
|
2004-01-20 00:54:06 +00:00
|
|
|
unsigned GlobalTyID = type;
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// If the type plane was compactified, figure out the global type ID
|
|
|
|
// by adding the derived type ids and the distance.
|
2004-07-04 11:33:49 +00:00
|
|
|
if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID) {
|
2004-06-29 23:29:38 +00:00
|
|
|
const Type *Ty = CompactionTypes[type-Type::FirstDerivedTyID];
|
|
|
|
TypeListTy::iterator I =
|
2004-07-04 11:33:49 +00:00
|
|
|
find(ModuleTypes.begin(), ModuleTypes.end(), Ty);
|
2004-06-29 23:29:38 +00:00
|
|
|
assert(I != ModuleTypes.end());
|
|
|
|
GlobalTyID = Type::FirstDerivedTyID + (&*I - &ModuleTypes[0]);
|
2004-01-20 00:54:06 +00:00
|
|
|
}
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (hasImplicitNull(GlobalTyID)) {
|
2004-01-18 21:08:15 +00:00
|
|
|
if (Num == 0)
|
2004-07-04 11:33:49 +00:00
|
|
|
return Constant::getNullValue(getType(type));
|
2004-01-18 21:08:15 +00:00
|
|
|
--Num;
|
|
|
|
}
|
|
|
|
|
2004-01-20 00:54:06 +00:00
|
|
|
if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
|
|
|
|
if (Num < ModuleValues[GlobalTyID]->size())
|
2004-07-04 11:33:49 +00:00
|
|
|
return ModuleValues[GlobalTyID]->getOperand(Num);
|
2004-01-20 00:54:06 +00:00
|
|
|
Num -= ModuleValues[GlobalTyID]->size();
|
2004-01-18 21:08:15 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (FunctionValues.size() > type &&
|
|
|
|
FunctionValues[type] &&
|
|
|
|
Num < FunctionValues[type]->size())
|
|
|
|
return FunctionValues[type]->getOperand(Num);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-08-17 22:01:27 +00:00
|
|
|
if (!Create) return 0; // Do not create a placeholder?
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-09 06:05:40 +00:00
|
|
|
std::pair<unsigned,unsigned> KeyValue(type, oNum);
|
2004-06-29 23:29:38 +00:00
|
|
|
ForwardReferenceMap::iterator I = ForwardReferences.lower_bound(KeyValue);
|
2003-10-09 06:05:40 +00:00
|
|
|
if (I != ForwardReferences.end() && I->first == KeyValue)
|
|
|
|
return I->second; // We have already created this placeholder
|
|
|
|
|
2003-10-09 06:14:26 +00:00
|
|
|
Value *Val = new Argument(getType(type));
|
2003-10-09 06:05:40 +00:00
|
|
|
ForwardReferences.insert(I, std::make_pair(KeyValue, Val));
|
2003-10-08 21:18:57 +00:00
|
|
|
return Val;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This is just like getValue, but when a compaction table is in use, it
|
|
|
|
/// is ignored. Also, no forward references or other fancy features are
|
|
|
|
/// supported.
|
2004-06-29 23:29:38 +00:00
|
|
|
Value* BytecodeReader::getGlobalTableValue(const Type *Ty, unsigned SlotNo) {
|
|
|
|
// FIXME: getTypeSlot is inefficient!
|
|
|
|
unsigned TyID = getGlobalTableTypeSlot(Ty);
|
|
|
|
|
|
|
|
if (TyID != Type::LabelTyID) {
|
|
|
|
if (SlotNo == 0)
|
|
|
|
return Constant::getNullValue(Ty);
|
|
|
|
--SlotNo;
|
|
|
|
}
|
2003-10-08 22:52:54 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
|
|
|
|
SlotNo >= ModuleValues[TyID]->size()) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Corrupt compaction table entry!"
|
|
|
|
+ utostr(TyID) + ", " + utostr(SlotNo) + ": "
|
2004-07-11 17:28:43 +00:00
|
|
|
+ utostr(ModuleValues.size()) + ", "
|
2004-07-13 07:37:43 +00:00
|
|
|
+ utohexstr(intptr_t((void*)ModuleValues[TyID])) + ", "
|
2004-07-11 17:28:43 +00:00
|
|
|
+ utostr(ModuleValues[TyID]->size()));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
return ModuleValues[TyID]->getOperand(SlotNo);
|
2003-10-08 22:52:54 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Just like getValue, except that it returns a null pointer
|
|
|
|
/// only on error. It always returns a constant (meaning that if the value is
|
|
|
|
/// defined, but is not a constant, that is an error). If the specified
|
|
|
|
/// constant hasn't been parsed yet, a placeholder is defined and used.
|
|
|
|
/// Later, after the real value is parsed, the placeholder is eliminated.
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) {
|
2003-11-19 06:01:12 +00:00
|
|
|
if (Value *V = getValue(TypeSlot, Slot, false))
|
2003-10-09 20:41:16 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(V))
|
|
|
|
return C; // If we already have the value parsed, just return it
|
|
|
|
else
|
2004-07-18 00:12:03 +00:00
|
|
|
error("Value for slot " + utostr(Slot) +
|
|
|
|
" is expected to be a constant!");
|
2002-10-14 03:33:02 +00:00
|
|
|
|
2003-11-19 06:01:12 +00:00
|
|
|
const Type *Ty = getType(TypeSlot);
|
2003-03-19 20:54:26 +00:00
|
|
|
std::pair<const Type*, unsigned> Key(Ty, Slot);
|
2003-11-19 17:27:18 +00:00
|
|
|
ConstantRefsType::iterator I = ConstantFwdRefs.lower_bound(Key);
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
if (I != ConstantFwdRefs.end() && I->first == Key) {
|
|
|
|
return I->second;
|
2002-10-14 03:33:02 +00:00
|
|
|
} else {
|
|
|
|
// Create a placeholder for the constant reference and
|
|
|
|
// keep track of the fact that we have a forward ref to recycle it
|
2004-07-11 17:28:43 +00:00
|
|
|
Constant *C = new ConstantPlaceHolder(Ty, Slot);
|
2002-10-14 03:33:02 +00:00
|
|
|
|
|
|
|
// Keep track of the fact that we have a forward ref to recycle it
|
2003-11-19 17:27:18 +00:00
|
|
|
ConstantFwdRefs.insert(I, std::make_pair(Key, C));
|
2002-10-14 03:33:02 +00:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IR Construction Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// As values are created, they are inserted into the appropriate place
|
|
|
|
/// with this method. The ValueTable argument must be one of ModuleValues
|
|
|
|
/// or FunctionValues data members of this class.
|
2004-07-11 17:28:43 +00:00
|
|
|
unsigned BytecodeReader::insertValue(Value *Val, unsigned type,
|
|
|
|
ValueTable &ValueTab) {
|
2004-06-29 23:29:38 +00:00
|
|
|
assert((!isa<Constant>(Val) || !cast<Constant>(Val)->isNullValue()) ||
|
2004-07-04 11:33:49 +00:00
|
|
|
!hasImplicitNull(type) &&
|
|
|
|
"Cannot read null values from bytecode!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
if (ValueTab.size() <= type)
|
|
|
|
ValueTab.resize(type+1);
|
|
|
|
|
|
|
|
if (!ValueTab[type]) ValueTab[type] = new ValueList();
|
|
|
|
|
|
|
|
ValueTab[type]->push_back(Val);
|
|
|
|
|
|
|
|
bool HasOffset = hasImplicitNull(type);
|
|
|
|
return ValueTab[type]->size()-1 + HasOffset;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Insert the arguments of a function as new values in the reader.
|
2004-07-11 17:28:43 +00:00
|
|
|
void BytecodeReader::insertArguments(Function* F) {
|
2004-06-29 23:29:38 +00:00
|
|
|
const FunctionType *FT = F->getFunctionType();
|
|
|
|
Function::aiterator AI = F->abegin();
|
|
|
|
for (FunctionType::param_iterator It = FT->param_begin();
|
|
|
|
It != FT->param_end(); ++It, ++AI)
|
|
|
|
insertValue(AI, getTypeSlot(AI->getType()), FunctionValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Bytecode Parsing Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This method parses a single instruction. The instruction is
|
|
|
|
/// inserted at the end of the \p BB provided. The arguments of
|
|
|
|
/// the instruction are provided in the \p Args vector.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
|
2004-07-11 17:28:43 +00:00
|
|
|
BasicBlock* BB) {
|
2004-06-29 23:29:38 +00:00
|
|
|
BufPtr SaveAt = At;
|
|
|
|
|
|
|
|
// Clear instruction data
|
|
|
|
Oprnds.clear();
|
|
|
|
unsigned iType = 0;
|
|
|
|
unsigned Opcode = 0;
|
|
|
|
unsigned Op = read_uint();
|
|
|
|
|
|
|
|
// bits Instruction format: Common to all formats
|
|
|
|
// --------------------------
|
|
|
|
// 01-00: Opcode type, fixed to 1.
|
|
|
|
// 07-02: Opcode
|
|
|
|
Opcode = (Op >> 2) & 63;
|
|
|
|
Oprnds.resize((Op >> 0) & 03);
|
|
|
|
|
|
|
|
// Extract the operands
|
|
|
|
switch (Oprnds.size()) {
|
|
|
|
case 1:
|
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 19-08: Resulting type plane
|
|
|
|
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
|
|
|
|
//
|
|
|
|
iType = (Op >> 8) & 4095;
|
|
|
|
Oprnds[0] = (Op >> 20) & 4095;
|
|
|
|
if (Oprnds[0] == 4095) // Handle special encoding for 0 operands...
|
|
|
|
Oprnds.resize(0);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 15-08: Resulting type plane
|
|
|
|
// 23-16: Operand #1
|
|
|
|
// 31-24: Operand #2
|
|
|
|
//
|
|
|
|
iType = (Op >> 8) & 255;
|
|
|
|
Oprnds[0] = (Op >> 16) & 255;
|
|
|
|
Oprnds[1] = (Op >> 24) & 255;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// bits Instruction format:
|
|
|
|
// --------------------------
|
|
|
|
// 13-08: Resulting type plane
|
|
|
|
// 19-14: Operand #1
|
|
|
|
// 25-20: Operand #2
|
|
|
|
// 31-26: Operand #3
|
|
|
|
//
|
|
|
|
iType = (Op >> 8) & 63;
|
|
|
|
Oprnds[0] = (Op >> 14) & 63;
|
|
|
|
Oprnds[1] = (Op >> 20) & 63;
|
|
|
|
Oprnds[2] = (Op >> 26) & 63;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
At -= 4; // Hrm, try this again...
|
|
|
|
Opcode = read_vbr_uint();
|
|
|
|
Opcode >>= 2;
|
|
|
|
iType = read_vbr_uint();
|
|
|
|
|
|
|
|
unsigned NumOprnds = read_vbr_uint();
|
|
|
|
Oprnds.resize(NumOprnds);
|
|
|
|
|
|
|
|
if (NumOprnds == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Zero-argument instruction found; this is invalid.");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumOprnds; ++i)
|
|
|
|
Oprnds[i] = read_vbr_uint();
|
|
|
|
align32();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
const Type *InstTy = getSanitizedType(iType);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// We have enough info to inform the handler now.
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Declare the resulting instruction we'll build.
|
|
|
|
Instruction *Result = 0;
|
|
|
|
|
|
|
|
// Handle binary operators
|
|
|
|
if (Opcode >= Instruction::BinaryOpsBegin &&
|
|
|
|
Opcode < Instruction::BinaryOpsEnd && Oprnds.size() == 2)
|
|
|
|
Result = BinaryOperator::create((Instruction::BinaryOps)Opcode,
|
|
|
|
getValue(iType, Oprnds[0]),
|
|
|
|
getValue(iType, Oprnds[1]));
|
|
|
|
|
|
|
|
switch (Opcode) {
|
|
|
|
default:
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Result == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Illegal instruction read!");
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::VAArg:
|
2004-07-04 11:33:49 +00:00
|
|
|
Result = new VAArgInst(getValue(iType, Oprnds[0]),
|
2004-07-11 17:28:43 +00:00
|
|
|
getSanitizedType(Oprnds[1]));
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::VANext:
|
2004-07-04 11:33:49 +00:00
|
|
|
Result = new VANextInst(getValue(iType, Oprnds[0]),
|
2004-07-11 17:28:43 +00:00
|
|
|
getSanitizedType(Oprnds[1]));
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Cast:
|
2004-07-04 11:33:49 +00:00
|
|
|
Result = new CastInst(getValue(iType, Oprnds[0]),
|
2004-07-11 17:28:43 +00:00
|
|
|
getSanitizedType(Oprnds[1]));
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Select:
|
|
|
|
Result = new SelectInst(getValue(Type::BoolTyID, Oprnds[0]),
|
|
|
|
getValue(iType, Oprnds[1]),
|
|
|
|
getValue(iType, Oprnds[2]));
|
|
|
|
break;
|
|
|
|
case Instruction::PHI: {
|
|
|
|
if (Oprnds.size() == 0 || (Oprnds.size() & 1))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid phi node encountered!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
PHINode *PN = new PHINode(InstTy);
|
|
|
|
PN->op_reserve(Oprnds.size());
|
|
|
|
for (unsigned i = 0, e = Oprnds.size(); i != e; i += 2)
|
|
|
|
PN->addIncoming(getValue(iType, Oprnds[i]), getBasicBlock(Oprnds[i+1]));
|
|
|
|
Result = PN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::Shr:
|
|
|
|
Result = new ShiftInst((Instruction::OtherOps)Opcode,
|
|
|
|
getValue(iType, Oprnds[0]),
|
|
|
|
getValue(Type::UByteTyID, Oprnds[1]));
|
|
|
|
break;
|
|
|
|
case Instruction::Ret:
|
|
|
|
if (Oprnds.size() == 0)
|
|
|
|
Result = new ReturnInst();
|
|
|
|
else if (Oprnds.size() == 1)
|
|
|
|
Result = new ReturnInst(getValue(iType, Oprnds[0]));
|
|
|
|
else
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Unrecognized instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Br:
|
|
|
|
if (Oprnds.size() == 1)
|
|
|
|
Result = new BranchInst(getBasicBlock(Oprnds[0]));
|
|
|
|
else if (Oprnds.size() == 3)
|
|
|
|
Result = new BranchInst(getBasicBlock(Oprnds[0]),
|
2004-07-04 11:33:49 +00:00
|
|
|
getBasicBlock(Oprnds[1]), getValue(Type::BoolTyID , Oprnds[2]));
|
2004-06-29 23:29:38 +00:00
|
|
|
else
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid number of operands for a 'br' instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Switch: {
|
|
|
|
if (Oprnds.size() & 1)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Switch statement with odd number of arguments!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
SwitchInst *I = new SwitchInst(getValue(iType, Oprnds[0]),
|
|
|
|
getBasicBlock(Oprnds[1]));
|
|
|
|
for (unsigned i = 2, e = Oprnds.size(); i != e; i += 2)
|
|
|
|
I->addCase(cast<Constant>(getValue(iType, Oprnds[i])),
|
|
|
|
getBasicBlock(Oprnds[i+1]));
|
|
|
|
Result = I;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Call: {
|
|
|
|
if (Oprnds.size() == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid call instruction encountered!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
Value *F = getValue(iType, Oprnds[0]);
|
|
|
|
|
|
|
|
// Check to make sure we have a pointer to function type
|
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(F->getType());
|
2004-07-09 22:21:33 +00:00
|
|
|
if (PTy == 0) error("Call to non function pointer value!");
|
2004-06-29 23:29:38 +00:00
|
|
|
const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
|
2004-07-09 22:21:33 +00:00
|
|
|
if (FTy == 0) error("Call to non function pointer value!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
std::vector<Value *> Params;
|
|
|
|
if (!FTy->isVarArg()) {
|
|
|
|
FunctionType::param_iterator It = FTy->param_begin();
|
|
|
|
|
|
|
|
for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
|
|
|
|
if (It == FTy->param_end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid call instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
|
|
|
|
}
|
|
|
|
if (It != FTy->param_end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid call instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
} else {
|
|
|
|
Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
|
|
|
|
|
|
|
|
unsigned FirstVariableOperand;
|
|
|
|
if (Oprnds.size() < FTy->getNumParams())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Call instruction missing operands!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Read all of the fixed arguments
|
|
|
|
for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
|
|
|
|
Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Oprnds[i]));
|
|
|
|
|
|
|
|
FirstVariableOperand = FTy->getNumParams();
|
|
|
|
|
|
|
|
if ((Oprnds.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid call instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
for (unsigned i = FirstVariableOperand, e = Oprnds.size();
|
2004-07-04 11:33:49 +00:00
|
|
|
i != e; i += 2)
|
2004-06-29 23:29:38 +00:00
|
|
|
Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = new CallInst(F, Params);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Invoke: {
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Oprnds.size() < 3)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid invoke instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Value *F = getValue(iType, Oprnds[0]);
|
|
|
|
|
|
|
|
// Check to make sure we have a pointer to function type
|
|
|
|
const PointerType *PTy = dyn_cast<PointerType>(F->getType());
|
2004-07-04 11:33:49 +00:00
|
|
|
if (PTy == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invoke to non function pointer value!");
|
2004-06-29 23:29:38 +00:00
|
|
|
const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
|
2004-07-04 11:33:49 +00:00
|
|
|
if (FTy == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invoke to non function pointer value!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
std::vector<Value *> Params;
|
|
|
|
BasicBlock *Normal, *Except;
|
|
|
|
|
|
|
|
if (!FTy->isVarArg()) {
|
|
|
|
Normal = getBasicBlock(Oprnds[1]);
|
|
|
|
Except = getBasicBlock(Oprnds[2]);
|
|
|
|
|
|
|
|
FunctionType::param_iterator It = FTy->param_begin();
|
|
|
|
for (unsigned i = 3, e = Oprnds.size(); i != e; ++i) {
|
|
|
|
if (It == FTy->param_end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid invoke instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Params.push_back(getValue(getTypeSlot(*It++), Oprnds[i]));
|
|
|
|
}
|
|
|
|
if (It != FTy->param_end())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid invoke instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
} else {
|
|
|
|
Oprnds.erase(Oprnds.begin(), Oprnds.begin()+1);
|
|
|
|
|
|
|
|
Normal = getBasicBlock(Oprnds[0]);
|
|
|
|
Except = getBasicBlock(Oprnds[1]);
|
|
|
|
|
|
|
|
unsigned FirstVariableArgument = FTy->getNumParams()+2;
|
|
|
|
for (unsigned i = 2; i != FirstVariableArgument; ++i)
|
|
|
|
Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
|
|
|
|
Oprnds[i]));
|
|
|
|
|
|
|
|
if (Oprnds.size()-FirstVariableArgument & 1) // Must be type/value pairs
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid invoke instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
for (unsigned i = FirstVariableArgument; i < Oprnds.size(); i += 2)
|
|
|
|
Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = new InvokeInst(F, Normal, Except, Params);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Malloc:
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Oprnds.size() > 2)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid malloc instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
if (!isa<PointerType>(InstTy))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid malloc instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
|
|
|
|
Oprnds.size() ? getValue(Type::UIntTyID,
|
|
|
|
Oprnds[0]) : 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Alloca:
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Oprnds.size() > 2)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid alloca instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
if (!isa<PointerType>(InstTy))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid alloca instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
|
|
|
|
Oprnds.size() ? getValue(Type::UIntTyID,
|
2004-07-04 11:33:49 +00:00
|
|
|
Oprnds[0]) :0);
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Free:
|
|
|
|
if (!isa<PointerType>(InstTy))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid free instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Result = new FreeInst(getValue(iType, Oprnds[0]));
|
|
|
|
break;
|
|
|
|
case Instruction::GetElementPtr: {
|
|
|
|
if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid getelementptr instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
std::vector<Value*> Idx;
|
|
|
|
|
|
|
|
const Type *NextTy = InstTy;
|
|
|
|
for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
|
|
|
|
const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (!TopTy)
|
2004-07-11 17:28:43 +00:00
|
|
|
error("Invalid getelementptr instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
unsigned ValIdx = Oprnds[i];
|
|
|
|
unsigned IdxTy = 0;
|
|
|
|
if (!hasRestrictedGEPTypes) {
|
|
|
|
// Struct indices are always uints, sequential type indices can be any
|
|
|
|
// of the 32 or 64-bit integer types. The actual choice of type is
|
|
|
|
// encoded in the low two bits of the slot number.
|
|
|
|
if (isa<StructType>(TopTy))
|
|
|
|
IdxTy = Type::UIntTyID;
|
|
|
|
else {
|
|
|
|
switch (ValIdx & 3) {
|
|
|
|
default:
|
|
|
|
case 0: IdxTy = Type::UIntTyID; break;
|
|
|
|
case 1: IdxTy = Type::IntTyID; break;
|
|
|
|
case 2: IdxTy = Type::ULongTyID; break;
|
|
|
|
case 3: IdxTy = Type::LongTyID; break;
|
|
|
|
}
|
|
|
|
ValIdx >>= 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IdxTy = isa<StructType>(TopTy) ? Type::UByteTyID : Type::LongTyID;
|
|
|
|
}
|
|
|
|
|
|
|
|
Idx.push_back(getValue(IdxTy, ValIdx));
|
|
|
|
|
|
|
|
// Convert ubyte struct indices into uint struct indices.
|
|
|
|
if (isa<StructType>(TopTy) && hasRestrictedGEPTypes)
|
|
|
|
if (ConstantUInt *C = dyn_cast<ConstantUInt>(Idx.back()))
|
|
|
|
Idx[Idx.size()-1] = ConstantExpr::getCast(C, Type::UIntTy);
|
|
|
|
|
|
|
|
NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 62: // volatile load
|
|
|
|
case Instruction::Load:
|
|
|
|
if (Oprnds.size() != 1 || !isa<PointerType>(InstTy))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid load instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Result = new LoadInst(getValue(iType, Oprnds[0]), "", Opcode == 62);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 63: // volatile store
|
|
|
|
case Instruction::Store: {
|
|
|
|
if (!isa<PointerType>(InstTy) || Oprnds.size() != 2)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid store instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
Value *Ptr = getValue(iType, Oprnds[1]);
|
|
|
|
const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
|
|
|
|
Result = new StoreInst(getValue(getTypeSlot(ValTy), Oprnds[0]), Ptr,
|
|
|
|
Opcode == 63);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Instruction::Unwind:
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Oprnds.size() != 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid unwind instruction!");
|
2004-06-29 23:29:38 +00:00
|
|
|
Result = new UnwindInst();
|
|
|
|
break;
|
|
|
|
} // end switch(Opcode)
|
|
|
|
|
|
|
|
unsigned TypeSlot;
|
|
|
|
if (Result->getType() == InstTy)
|
|
|
|
TypeSlot = iType;
|
|
|
|
else
|
|
|
|
TypeSlot = getTypeSlot(Result->getType());
|
|
|
|
|
|
|
|
insertValue(Result, TypeSlot, FunctionValues);
|
|
|
|
BB->getInstList().push_back(Result);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Get a particular numbered basic block, which might be a forward reference.
|
|
|
|
/// This works together with ParseBasicBlock to handle these forward references
|
|
|
|
/// in a clean manner. This function is used when constructing phi, br, switch,
|
|
|
|
/// and other instructions that reference basic blocks. Blocks are numbered
|
|
|
|
/// sequentially as they appear in the function.
|
2004-06-29 23:29:38 +00:00
|
|
|
BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) {
|
|
|
|
// Make sure there is room in the table...
|
|
|
|
if (ParsedBasicBlocks.size() <= ID) ParsedBasicBlocks.resize(ID+1);
|
|
|
|
|
|
|
|
// First check to see if this is a backwards reference, i.e., ParseBasicBlock
|
|
|
|
// has already created this block, or if the forward reference has already
|
|
|
|
// been created.
|
|
|
|
if (ParsedBasicBlocks[ID])
|
|
|
|
return ParsedBasicBlocks[ID];
|
|
|
|
|
|
|
|
// Otherwise, the basic block has not yet been created. Do so and add it to
|
|
|
|
// the ParsedBasicBlocks list.
|
|
|
|
return ParsedBasicBlocks[ID] = new BasicBlock();
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// In LLVM 1.0 bytecode files, we used to output one basicblock at a time.
|
|
|
|
/// This method reads in one of the basicblock packets. This method is not used
|
|
|
|
/// for bytecode files after LLVM 1.0
|
|
|
|
/// @returns The basic block constructed.
|
2004-07-11 17:28:43 +00:00
|
|
|
BasicBlock *BytecodeReader::ParseBasicBlock(unsigned BlockNo) {
|
|
|
|
if (Handler) Handler->handleBasicBlockBegin(BlockNo);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
BasicBlock *BB = 0;
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
if (ParsedBasicBlocks.size() == BlockNo)
|
|
|
|
ParsedBasicBlocks.push_back(BB = new BasicBlock());
|
|
|
|
else if (ParsedBasicBlocks[BlockNo] == 0)
|
|
|
|
BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
|
|
|
|
else
|
|
|
|
BB = ParsedBasicBlocks[BlockNo];
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
std::vector<unsigned> Operands;
|
2004-07-11 17:28:43 +00:00
|
|
|
while (moreInBlock())
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseInstruction(Operands, BB);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleBasicBlockEnd(BlockNo);
|
2003-09-22 23:38:23 +00:00
|
|
|
return BB;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse all of the BasicBlock's & Instruction's in the body of a function.
|
|
|
|
/// In post 1.0 bytecode files, we no longer emit basic block individually,
|
|
|
|
/// in order to avoid per-basic-block overhead.
|
|
|
|
/// @returns Rhe number of basic blocks encountered.
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned BytecodeReader::ParseInstructionList(Function* F) {
|
2003-12-01 07:05:31 +00:00
|
|
|
unsigned BlockNo = 0;
|
|
|
|
std::vector<unsigned> Args;
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
while (moreInBlock()) {
|
|
|
|
if (Handler) Handler->handleBasicBlockBegin(BlockNo);
|
2003-12-01 07:05:31 +00:00
|
|
|
BasicBlock *BB;
|
|
|
|
if (ParsedBasicBlocks.size() == BlockNo)
|
|
|
|
ParsedBasicBlocks.push_back(BB = new BasicBlock());
|
|
|
|
else if (ParsedBasicBlocks[BlockNo] == 0)
|
|
|
|
BB = ParsedBasicBlocks[BlockNo] = new BasicBlock();
|
|
|
|
else
|
|
|
|
BB = ParsedBasicBlocks[BlockNo];
|
|
|
|
++BlockNo;
|
|
|
|
F->getBasicBlockList().push_back(BB);
|
|
|
|
|
|
|
|
// Read instructions into this basic block until we get to a terminator
|
2004-07-11 17:28:43 +00:00
|
|
|
while (moreInBlock() && !BB->getTerminator())
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseInstruction(Args, BB);
|
2003-12-01 07:05:31 +00:00
|
|
|
|
|
|
|
if (!BB->getTerminator())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Non-terminated basic block found!");
|
2004-07-05 00:57:50 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleBasicBlockEnd(BlockNo-1);
|
2003-12-01 07:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return BlockNo;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse a symbol table. This works for both module level and function
|
|
|
|
/// level symbol tables. For function level symbol tables, the CurrentFunction
|
|
|
|
/// parameter must be non-zero and the ST parameter must correspond to
|
|
|
|
/// CurrentFunction's symbol table. For Module level symbol tables, the
|
|
|
|
/// CurrentFunction argument must be zero.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseSymbolTable(Function *CurrentFunction,
|
2004-07-04 11:33:49 +00:00
|
|
|
SymbolTable *ST) {
|
|
|
|
if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2003-10-10 05:43:47 +00:00
|
|
|
// Allow efficient basic block lookup by number.
|
|
|
|
std::vector<BasicBlock*> BBMap;
|
|
|
|
if (CurrentFunction)
|
|
|
|
for (Function::iterator I = CurrentFunction->begin(),
|
|
|
|
E = CurrentFunction->end(); I != E; ++I)
|
|
|
|
BBMap.push_back(I);
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// In LLVM 1.3 we write types separately from values so
|
|
|
|
/// The types are always first in the symbol table. This is
|
|
|
|
/// because Type no longer derives from Value.
|
2004-07-11 17:28:43 +00:00
|
|
|
if (!hasTypeDerivedFromValue) {
|
2004-07-04 11:33:49 +00:00
|
|
|
// Symtab block header: [num entries]
|
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-11 17:28:43 +00:00
|
|
|
for (unsigned i = 0; i < NumEntries; ++i) {
|
2004-07-04 11:33:49 +00:00
|
|
|
// Symtab entry: [def slot #][name]
|
|
|
|
unsigned slot = read_vbr_uint();
|
|
|
|
std::string Name = read_str();
|
|
|
|
const Type* T = getType(slot);
|
|
|
|
ST->insert(Name, T);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
while (moreInBlock()) {
|
2001-06-06 20:29:01 +00:00
|
|
|
// Symtab block header: [num entries][type id number]
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned Typ = 0;
|
|
|
|
bool isTypeType = read_typeid(Typ);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2003-10-13 14:57:53 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2001-06-06 20:29:01 +00:00
|
|
|
// Symtab entry: [def slot #][name]
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned slot = read_vbr_uint();
|
|
|
|
std::string Name = read_str();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
// if we're reading a pre 1.3 bytecode file and the type plane
|
|
|
|
// is the "type type", handle it here
|
2004-07-11 17:28:43 +00:00
|
|
|
if (isTypeType) {
|
|
|
|
const Type* T = getType(slot);
|
|
|
|
if (T == 0)
|
|
|
|
error("Failed type look-up for name '" + Name + "'");
|
|
|
|
ST->insert(Name, T);
|
|
|
|
continue; // code below must be short circuited
|
2003-10-10 05:43:47 +00:00
|
|
|
} else {
|
2004-07-11 17:28:43 +00:00
|
|
|
Value *V = 0;
|
|
|
|
if (Typ == Type::LabelTyID) {
|
|
|
|
if (slot < BBMap.size())
|
|
|
|
V = BBMap[slot];
|
|
|
|
} else {
|
|
|
|
V = getValue(Typ, slot, false); // Find mapping...
|
|
|
|
}
|
|
|
|
if (V == 0)
|
|
|
|
error("Failed value look-up for name '" + Name + "'");
|
|
|
|
V->setName(Name, ST);
|
2003-10-10 05:43:47 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
checkPastBlockEnd("Symbol Table");
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleSymbolTableEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read in the types portion of a compaction table.
|
2004-07-11 17:28:43 +00:00
|
|
|
void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) {
|
2004-07-04 11:33:49 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
|
|
|
unsigned TypeSlot = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(TypeSlot))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid type in compaction table: type type");
|
2004-07-04 11:33:49 +00:00
|
|
|
const Type *Typ = getGlobalTableType(TypeSlot);
|
|
|
|
CompactionTypes.push_back(Typ);
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ);
|
2004-07-04 11:33:49 +00:00
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse a compaction table.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseCompactionTable() {
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Notify handler that we're beginning a compaction table.
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleCompactionTableBegin();
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// In LLVM 1.3 Type no longer derives from Value. So,
|
|
|
|
// we always write them first in the compaction table
|
|
|
|
// because they can't occupy a "type plane" where the
|
|
|
|
// Values reside.
|
|
|
|
if (! hasTypeDerivedFromValue) {
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-11 17:28:43 +00:00
|
|
|
ParseCompactionTypes(NumEntries);
|
2004-07-04 11:33:49 +00:00
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Compaction tables live in separate blocks so we have to loop
|
|
|
|
// until we've read the whole thing.
|
|
|
|
while (moreInBlock()) {
|
|
|
|
// Read the number of Value* entries in the compaction table
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned Ty = 0;
|
|
|
|
unsigned isTypeType = false;
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Decode the type from value read in. Most compaction table
|
|
|
|
// planes will have one or two entries in them. If that's the
|
|
|
|
// case then the length is encoded in the bottom two bits and
|
|
|
|
// the higher bits encode the type. This saves another VBR value.
|
2004-06-29 23:29:38 +00:00
|
|
|
if ((NumEntries & 3) == 3) {
|
2004-07-11 17:28:43 +00:00
|
|
|
// In this case, both low-order bits are set (value 3). This
|
|
|
|
// is a signal that the typeid follows.
|
2004-06-29 23:29:38 +00:00
|
|
|
NumEntries >>= 2;
|
2004-07-04 11:33:49 +00:00
|
|
|
isTypeType = read_typeid(Ty);
|
2004-06-29 23:29:38 +00:00
|
|
|
} else {
|
2004-07-11 17:28:43 +00:00
|
|
|
// In this case, the low-order bits specify the number of entries
|
|
|
|
// and the high order bits specify the type.
|
2004-06-29 23:29:38 +00:00
|
|
|
Ty = NumEntries >> 2;
|
2004-07-04 11:33:49 +00:00
|
|
|
isTypeType = sanitizeTypeId(Ty);
|
2004-06-29 23:29:38 +00:00
|
|
|
NumEntries &= 3;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
// if we're reading a pre 1.3 bytecode file and the type plane
|
|
|
|
// is the "type type", handle it here
|
2004-07-11 17:28:43 +00:00
|
|
|
if (isTypeType) {
|
2004-07-04 11:33:49 +00:00
|
|
|
ParseCompactionTypes(NumEntries);
|
|
|
|
} else {
|
2004-07-11 17:28:43 +00:00
|
|
|
// Make sure we have enough room for the plane
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Ty >= CompactionValues.size())
|
2004-07-11 17:28:43 +00:00
|
|
|
CompactionValues.resize(Ty+1);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Make sure the plane is empty or we have some kind of error
|
2004-07-04 11:33:49 +00:00
|
|
|
if (!CompactionValues[Ty].empty())
|
2004-07-11 17:28:43 +00:00
|
|
|
error("Compaction table plane contains multiple entries!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Notify handler about the plane
|
|
|
|
if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Convert the type slot to a type
|
2004-06-29 23:29:38 +00:00
|
|
|
const Type *Typ = getType(Ty);
|
2004-07-11 17:28:43 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Push the implicit zero
|
|
|
|
CompactionValues[Ty].push_back(Constant::getNullValue(Typ));
|
2004-07-11 17:28:43 +00:00
|
|
|
|
|
|
|
// Read in each of the entries, put them in the compaction table
|
|
|
|
// and notify the handler that we have a new compaction table value.
|
2004-06-29 23:29:38 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2004-07-11 17:28:43 +00:00
|
|
|
unsigned ValSlot = read_vbr_uint();
|
|
|
|
Value *V = getGlobalTableValue(Typ, ValSlot);
|
|
|
|
CompactionValues[Ty].push_back(V);
|
|
|
|
if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot, Typ);
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-07-11 17:28:43 +00:00
|
|
|
// Notify handler that the compaction table is done.
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleCompactionTableEnd();
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// Parse a single type. The typeid is read in first. If its a primitive type
|
|
|
|
// then nothing else needs to be read, we know how to instantiate it. If its
|
|
|
|
// a derived type, then additional data is read to fill out the type
|
|
|
|
// definition.
|
|
|
|
const Type *BytecodeReader::ParseType() {
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned PrimType = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(PrimType))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid type (type type) in type constants!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
const Type *Result = 0;
|
|
|
|
if ((Result = Type::getPrimitiveType((Type::TypeID)PrimType)))
|
|
|
|
return Result;
|
|
|
|
|
|
|
|
switch (PrimType) {
|
|
|
|
case Type::FunctionTyID: {
|
2004-07-04 11:33:49 +00:00
|
|
|
const Type *RetType = readSanitizedType();
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
unsigned NumParams = read_vbr_uint();
|
|
|
|
|
|
|
|
std::vector<const Type*> Params;
|
2004-07-04 11:33:49 +00:00
|
|
|
while (NumParams--)
|
|
|
|
Params.push_back(readSanitizedType());
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
|
|
|
|
if (isVarArg) Params.pop_back();
|
|
|
|
|
|
|
|
Result = FunctionType::get(RetType, Params, isVarArg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::ArrayTyID: {
|
2004-07-04 11:33:49 +00:00
|
|
|
const Type *ElementType = readSanitizedType();
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned NumElements = read_vbr_uint();
|
|
|
|
Result = ArrayType::get(ElementType, NumElements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::StructTyID: {
|
|
|
|
std::vector<const Type*> Elements;
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned Typ = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(Typ))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid element type (type type) for structure!");
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
while (Typ) { // List is terminated by void/0 typeid
|
|
|
|
Elements.push_back(getType(Typ));
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(Typ))
|
|
|
|
error("Invalid element type (type type) for structure!");
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result = StructType::get(Elements);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Type::PointerTyID: {
|
2004-07-04 11:33:49 +00:00
|
|
|
Result = PointerType::get(readSanitizedType());
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::OpaqueTyID: {
|
|
|
|
Result = OpaqueType::get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Don't know how to deserialize primitive type " + utostr(PrimType));
|
2004-06-29 23:29:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleType(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
// ParseType - We have to use this weird code to handle recursive
|
2004-06-29 23:29:38 +00:00
|
|
|
// types. We know that recursive types will only reference the current slab of
|
|
|
|
// values in the type plane, but they can forward reference types before they
|
|
|
|
// have been read. For example, Type #0 might be '{ Ty#1 }' and Type #1 might
|
|
|
|
// be 'Ty#0*'. When reading Type #0, type number one doesn't exist. To fix
|
|
|
|
// this ugly problem, we pessimistically insert an opaque type for each type we
|
|
|
|
// are about to read. This means that forward references will resolve to
|
|
|
|
// something and when we reread the type later, we can replace the opaque type
|
|
|
|
// with a new resolved concrete type.
|
|
|
|
//
|
2004-07-11 17:28:43 +00:00
|
|
|
void BytecodeReader::ParseTypes(TypeListTy &Tab, unsigned NumEntries){
|
2004-06-29 23:29:38 +00:00
|
|
|
assert(Tab.size() == 0 && "should not have read type constants in before!");
|
|
|
|
|
|
|
|
// Insert a bunch of opaque types to be resolved later...
|
|
|
|
Tab.reserve(NumEntries);
|
|
|
|
for (unsigned i = 0; i != NumEntries; ++i)
|
|
|
|
Tab.push_back(OpaqueType::get());
|
|
|
|
|
|
|
|
// Loop through reading all of the types. Forward types will make use of the
|
|
|
|
// opaque types just inserted.
|
|
|
|
//
|
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2004-07-11 17:28:43 +00:00
|
|
|
const Type* NewTy = ParseType();
|
2004-07-04 11:33:49 +00:00
|
|
|
const Type* OldTy = Tab[i].get();
|
|
|
|
if (NewTy == 0)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Couldn't parse type!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Don't directly push the new type on the Tab. Instead we want to replace
|
|
|
|
// the opaque type we previously inserted with the new concrete value. This
|
|
|
|
// approach helps with forward references to types. The refinement from the
|
|
|
|
// abstract (opaque) type to the new type causes all uses of the abstract
|
|
|
|
// type to use the concrete type (NewTy). This will also cause the opaque
|
|
|
|
// type to be deleted.
|
|
|
|
cast<DerivedType>(const_cast<Type*>(OldTy))->refineAbstractTypeTo(NewTy);
|
|
|
|
|
|
|
|
// This should have replaced the old opaque type with the new type in the
|
|
|
|
// value table... or with a preexisting type that was already in the system.
|
|
|
|
// Let's just make sure it did.
|
|
|
|
assert(Tab[i] != OldTy && "refineAbstractType didn't work!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse a single constant value
|
2004-07-11 17:28:43 +00:00
|
|
|
Constant *BytecodeReader::ParseConstantValue(unsigned TypeID) {
|
2004-06-29 23:29:38 +00:00
|
|
|
// We must check for a ConstantExpr before switching by type because
|
|
|
|
// a ConstantExpr can be of any type, and has no explicit value.
|
|
|
|
//
|
|
|
|
// 0 if not expr; numArgs if is expr
|
|
|
|
unsigned isExprNumArgs = read_vbr_uint();
|
|
|
|
|
|
|
|
if (isExprNumArgs) {
|
|
|
|
// FIXME: Encoding of constant exprs could be much more compact!
|
|
|
|
std::vector<Constant*> ArgVec;
|
|
|
|
ArgVec.reserve(isExprNumArgs);
|
|
|
|
unsigned Opcode = read_vbr_uint();
|
|
|
|
|
|
|
|
// Read the slot number and types of each of the arguments
|
|
|
|
for (unsigned i = 0; i != isExprNumArgs; ++i) {
|
|
|
|
unsigned ArgValSlot = read_vbr_uint();
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned ArgTypeSlot = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(ArgTypeSlot))
|
|
|
|
error("Invalid argument type (type type) for constant value");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Get the arg value from its slot if it exists, otherwise a placeholder
|
|
|
|
ArgVec.push_back(getConstantValue(ArgTypeSlot, ArgValSlot));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a ConstantExpr of the appropriate kind
|
|
|
|
if (isExprNumArgs == 1) { // All one-operand expressions
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Opcode != Instruction::Cast)
|
|
|
|
error("Only Cast instruction has one argument for ConstantExpr");
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantExpr::getCast(ArgVec[0], getType(TypeID));
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
} else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
|
|
|
|
std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
|
|
|
|
|
|
|
|
if (hasRestrictedGEPTypes) {
|
|
|
|
const Type *BaseTy = ArgVec[0]->getType();
|
|
|
|
generic_gep_type_iterator<std::vector<Constant*>::iterator>
|
|
|
|
GTI = gep_type_begin(BaseTy, IdxList.begin(), IdxList.end()),
|
|
|
|
E = gep_type_end(BaseTy, IdxList.begin(), IdxList.end());
|
|
|
|
for (unsigned i = 0; GTI != E; ++GTI, ++i)
|
|
|
|
if (isa<StructType>(*GTI)) {
|
|
|
|
if (IdxList[i]->getType() != Type::UByteTy)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid index for getelementptr!");
|
2004-06-29 23:29:38 +00:00
|
|
|
IdxList[i] = ConstantExpr::getCast(IdxList[i], Type::UIntTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant* Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
} else if (Opcode == Instruction::Select) {
|
2004-07-11 17:28:43 +00:00
|
|
|
if (ArgVec.size() != 3)
|
|
|
|
error("Select instruction must have three arguments.");
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
|
2004-07-04 11:33:49 +00:00
|
|
|
ArgVec[2]);
|
|
|
|
if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
} else { // All other 2-operand expressions
|
|
|
|
Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, not an ConstantExpr. We now know how to read the given type...
|
|
|
|
const Type *Ty = getType(TypeID);
|
|
|
|
switch (Ty->getTypeID()) {
|
|
|
|
case Type::BoolTyID: {
|
|
|
|
unsigned Val = read_vbr_uint();
|
|
|
|
if (Val != 0 && Val != 1)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid boolean value read.");
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantBool::get(Val == 1);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::UByteTyID: // Unsigned integer types...
|
|
|
|
case Type::UShortTyID:
|
|
|
|
case Type::UIntTyID: {
|
|
|
|
unsigned Val = read_vbr_uint();
|
|
|
|
if (!ConstantUInt::isValueValidForType(Ty, Val))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid unsigned byte/short/int read.");
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantUInt::get(Ty, Val);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ULongTyID: {
|
|
|
|
Constant* Result = ConstantUInt::get(Ty, read_vbr_uint64());
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::SByteTyID: // Signed integer types...
|
|
|
|
case Type::ShortTyID:
|
|
|
|
case Type::IntTyID: {
|
|
|
|
case Type::LongTyID:
|
|
|
|
int64_t Val = read_vbr_int64();
|
|
|
|
if (!ConstantSInt::isValueValidForType(Ty, Val))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid signed byte/short/int/long read.");
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantSInt::get(Ty, Val);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::FloatTyID: {
|
2004-07-11 17:28:43 +00:00
|
|
|
float Val;
|
|
|
|
read_float(Val);
|
|
|
|
Constant* Result = ConstantFP::get(Ty, Val);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::DoubleTyID: {
|
|
|
|
double Val;
|
2004-07-11 17:28:43 +00:00
|
|
|
read_double(Val);
|
2004-06-29 23:29:38 +00:00
|
|
|
Constant* Result = ConstantFP::get(Ty, Val);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantValue(Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::ArrayTyID: {
|
|
|
|
const ArrayType *AT = cast<ArrayType>(Ty);
|
|
|
|
unsigned NumElements = AT->getNumElements();
|
|
|
|
unsigned TypeSlot = getTypeSlot(AT->getElementType());
|
|
|
|
std::vector<Constant*> Elements;
|
|
|
|
Elements.reserve(NumElements);
|
|
|
|
while (NumElements--) // Read all of the elements of the constant.
|
|
|
|
Elements.push_back(getConstantValue(TypeSlot,
|
|
|
|
read_vbr_uint()));
|
|
|
|
Constant* Result = ConstantArray::get(AT, Elements);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::StructTyID: {
|
|
|
|
const StructType *ST = cast<StructType>(Ty);
|
|
|
|
|
|
|
|
std::vector<Constant *> Elements;
|
|
|
|
Elements.reserve(ST->getNumElements());
|
|
|
|
for (unsigned i = 0; i != ST->getNumElements(); ++i)
|
|
|
|
Elements.push_back(getConstantValue(ST->getElementType(i),
|
|
|
|
read_vbr_uint()));
|
|
|
|
|
|
|
|
Constant* Result = ConstantStruct::get(ST, Elements);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
|
2004-06-29 23:29:38 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::PointerTyID: { // ConstantPointerRef value...
|
|
|
|
const PointerType *PT = cast<PointerType>(Ty);
|
|
|
|
unsigned Slot = read_vbr_uint();
|
|
|
|
|
|
|
|
// Check to see if we have already read this global variable...
|
|
|
|
Value *Val = getValue(TypeID, Slot, false);
|
|
|
|
GlobalValue *GV;
|
|
|
|
if (Val) {
|
|
|
|
if (!(GV = dyn_cast<GlobalValue>(Val)))
|
2004-07-18 00:12:03 +00:00
|
|
|
error("GlobalValue not in ValueTable!");
|
2004-06-29 23:29:38 +00:00
|
|
|
} else {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Forward references are not allowed here.");
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-18 00:12:03 +00:00
|
|
|
if (Handler) Handler->handleConstantPointer(PT, Slot, GV );
|
|
|
|
return GV;
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Don't know how to deserialize constant value of type '" +
|
2004-06-29 23:29:38 +00:00
|
|
|
Ty->getDescription());
|
|
|
|
break;
|
|
|
|
}
|
2004-07-09 22:21:33 +00:00
|
|
|
return 0;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Resolve references for constants. This function resolves the forward
|
|
|
|
/// referenced constants in the ConstantFwdRefs map. It uses the
|
|
|
|
/// replaceAllUsesWith method of Value class to substitute the placeholder
|
|
|
|
/// instance with the actual instance.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
|
2003-11-19 17:27:18 +00:00
|
|
|
ConstantRefsType::iterator I =
|
|
|
|
ConstantFwdRefs.find(std::make_pair(NewV->getType(), Slot));
|
|
|
|
if (I == ConstantFwdRefs.end()) return; // Never forward referenced?
|
2002-07-14 23:04:18 +00:00
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
Value *PH = I->second; // Get the placeholder...
|
|
|
|
PH->replaceAllUsesWith(NewV);
|
|
|
|
delete PH; // Delete the old placeholder
|
|
|
|
ConstantFwdRefs.erase(I); // Remove the map entry for it
|
2002-07-14 23:04:18 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the constant strings section.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseStringConstants(unsigned NumEntries, ValueTable &Tab){
|
|
|
|
for (; NumEntries; --NumEntries) {
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned Typ = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(Typ))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid type (type type) for string constant");
|
2004-06-29 23:29:38 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
|
|
|
if (!isa<ArrayType>(Ty))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("String constant data invalid!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
const ArrayType *ATy = cast<ArrayType>(Ty);
|
|
|
|
if (ATy->getElementType() != Type::SByteTy &&
|
|
|
|
ATy->getElementType() != Type::UByteTy)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("String constant data invalid!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Read character data. The type tells us how long the string is.
|
|
|
|
char Data[ATy->getNumElements()];
|
|
|
|
read_data(Data, Data+ATy->getNumElements());
|
|
|
|
|
|
|
|
std::vector<Constant*> Elements(ATy->getNumElements());
|
|
|
|
if (ATy->getElementType() == Type::SByteTy)
|
|
|
|
for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
|
|
|
|
Elements[i] = ConstantSInt::get(Type::SByteTy, (signed char)Data[i]);
|
|
|
|
else
|
|
|
|
for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
|
|
|
|
Elements[i] = ConstantUInt::get(Type::UByteTy, (unsigned char)Data[i]);
|
|
|
|
|
|
|
|
// Create the constant, inserting it as needed.
|
|
|
|
Constant *C = ConstantArray::get(ATy, Elements);
|
|
|
|
unsigned Slot = insertValue(C, Typ, Tab);
|
|
|
|
ResolveReferencesToConstant(C, Slot);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleConstantString(cast<ConstantArray>(C));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2003-09-22 23:38:23 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the constant pool.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseConstantPool(ValueTable &Tab,
|
2004-07-04 11:33:49 +00:00
|
|
|
TypeListTy &TypeTab,
|
2004-07-11 17:28:43 +00:00
|
|
|
bool isFunction) {
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleGlobalConstantsBegin();
|
|
|
|
|
|
|
|
/// In LLVM 1.3 Type does not derive from Value so the types
|
|
|
|
/// do not occupy a plane. Consequently, we read the types
|
|
|
|
/// first in the constant pool.
|
2004-07-11 17:28:43 +00:00
|
|
|
if (isFunction && !hasTypeDerivedFromValue) {
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-11 17:28:43 +00:00
|
|
|
ParseTypes(TypeTab, NumEntries);
|
2004-07-04 11:33:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
while (moreInBlock()) {
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned Typ = 0;
|
|
|
|
bool isTypeType = read_typeid(Typ);
|
|
|
|
|
|
|
|
/// In LLVM 1.2 and before, Types were written to the
|
|
|
|
/// bytecode file in the "Type Type" plane (#12).
|
|
|
|
/// In 1.3 plane 12 is now the label plane. Handle this here.
|
2004-07-11 17:28:43 +00:00
|
|
|
if (isTypeType) {
|
|
|
|
ParseTypes(TypeTab, NumEntries);
|
2004-06-29 23:29:38 +00:00
|
|
|
} else if (Typ == Type::VoidTyID) {
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Use of Type::VoidTyID is a misnomer. It actually means
|
|
|
|
/// that the following plane is constant strings
|
2004-06-29 23:29:38 +00:00
|
|
|
assert(&Tab == &ModuleValues && "Cannot read strings in functions!");
|
|
|
|
ParseStringConstants(NumEntries, Tab);
|
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i < NumEntries; ++i) {
|
|
|
|
Constant *C = ParseConstantValue(Typ);
|
|
|
|
assert(C && "ParseConstantValue returned NULL!");
|
|
|
|
unsigned Slot = insertValue(C, Typ, Tab);
|
|
|
|
|
|
|
|
// If we are reading a function constant table, make sure that we adjust
|
|
|
|
// the slot number to be the real global constant number.
|
|
|
|
//
|
|
|
|
if (&Tab != &ModuleValues && Typ < ModuleValues.size() &&
|
|
|
|
ModuleValues[Typ])
|
|
|
|
Slot += ModuleValues[Typ]->size();
|
|
|
|
ResolveReferencesToConstant(C, Slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkPastBlockEnd("Constant Pool");
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleGlobalConstantsEnd();
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2003-11-19 17:27:18 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the contents of a function. Note that this function can be
|
|
|
|
/// called lazily by materializeFunction
|
|
|
|
/// @see materializeFunction
|
2004-07-11 17:28:43 +00:00
|
|
|
void BytecodeReader::ParseFunctionBody(Function* F) {
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned FuncSize = BlockEnd - At;
|
2003-04-16 21:16:05 +00:00
|
|
|
GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned LinkageType = read_vbr_uint();
|
2004-01-14 16:44:44 +00:00
|
|
|
switch (LinkageType) {
|
|
|
|
case 0: Linkage = GlobalValue::ExternalLinkage; break;
|
|
|
|
case 1: Linkage = GlobalValue::WeakLinkage; break;
|
|
|
|
case 2: Linkage = GlobalValue::AppendingLinkage; break;
|
|
|
|
case 3: Linkage = GlobalValue::InternalLinkage; break;
|
|
|
|
case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
|
2004-06-29 23:29:38 +00:00
|
|
|
default:
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid linkage type for Function.");
|
2004-06-29 23:29:38 +00:00
|
|
|
Linkage = GlobalValue::InternalLinkage;
|
|
|
|
break;
|
2003-04-16 21:16:05 +00:00
|
|
|
}
|
2001-11-26 18:56:10 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
F->setLinkage(Linkage);
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleFunctionBegin(F,FuncSize);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
// Keep track of how many basic blocks we have read in...
|
|
|
|
unsigned BlockNum = 0;
|
2004-01-18 21:08:15 +00:00
|
|
|
bool InsertedArguments = false;
|
2003-10-08 22:52:54 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
BufPtr MyEnd = BlockEnd;
|
2004-07-11 17:28:43 +00:00
|
|
|
while (At < MyEnd) {
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned Type, Size;
|
2004-06-29 23:29:38 +00:00
|
|
|
BufPtr OldAt = At;
|
|
|
|
read_block(Type, Size);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
switch (Type) {
|
2003-11-19 17:27:18 +00:00
|
|
|
case BytecodeFormat::ConstantPool:
|
2004-01-18 21:08:15 +00:00
|
|
|
if (!InsertedArguments) {
|
|
|
|
// Insert arguments into the value table before we parse the first basic
|
|
|
|
// block in the function, but after we potentially read in the
|
|
|
|
// compaction table.
|
2004-07-04 11:33:49 +00:00
|
|
|
insertArguments(F);
|
2004-01-18 21:08:15 +00:00
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
ParseConstantPool(FunctionValues, FunctionTypes, true);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
case BytecodeFormat::CompactionTable:
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseCompactionTable();
|
2004-01-18 21:08:15 +00:00
|
|
|
break;
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
case BytecodeFormat::BasicBlock: {
|
2004-01-18 21:08:15 +00:00
|
|
|
if (!InsertedArguments) {
|
|
|
|
// Insert arguments into the value table before we parse the first basic
|
|
|
|
// block in the function, but after we potentially read in the
|
|
|
|
// compaction table.
|
2004-07-04 11:33:49 +00:00
|
|
|
insertArguments(F);
|
2004-01-18 21:08:15 +00:00
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
BasicBlock *BB = ParseBasicBlock(BlockNum++);
|
2003-10-08 22:52:54 +00:00
|
|
|
F->getBasicBlockList().push_back(BB);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-12-01 07:05:31 +00:00
|
|
|
case BytecodeFormat::InstructionList: {
|
2004-01-18 21:08:15 +00:00
|
|
|
// Insert arguments into the value table before we parse the instruction
|
|
|
|
// list for the function, but after we potentially read in the compaction
|
|
|
|
// table.
|
|
|
|
if (!InsertedArguments) {
|
2004-07-04 11:33:49 +00:00
|
|
|
insertArguments(F);
|
2004-01-18 21:08:15 +00:00
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
if (BlockNum)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Already parsed basic blocks!");
|
2004-06-29 23:29:38 +00:00
|
|
|
BlockNum = ParseInstructionList(F);
|
2003-12-01 07:05:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
case BytecodeFormat::SymbolTable:
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseSymbolTable(F, &F->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-06-29 23:29:38 +00:00
|
|
|
At += Size;
|
|
|
|
if (OldAt > At)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Wrapped around reading bytecode.");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
BlockEnd = MyEnd;
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
// Malformed bc file if read past end of block.
|
2004-06-29 23:29:38 +00:00
|
|
|
align32();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
// Make sure there were no references to non-existant basic blocks.
|
|
|
|
if (BlockNum != ParsedBasicBlocks.size())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Illegal basic block operand reference");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
ParsedBasicBlocks.clear();
|
|
|
|
|
2003-10-09 23:10:14 +00:00
|
|
|
// Resolve forward references. Replace any uses of a forward reference value
|
|
|
|
// with the real value.
|
|
|
|
|
|
|
|
// replaceAllUsesWith is very inefficient for instructions which have a LARGE
|
|
|
|
// number of operands. PHI nodes often have forward references, and can also
|
|
|
|
// often have a very large number of operands.
|
2004-01-18 21:08:15 +00:00
|
|
|
//
|
|
|
|
// FIXME: REEVALUATE. replaceAllUsesWith is _much_ faster now, and this code
|
|
|
|
// should be simplified back to using it!
|
|
|
|
//
|
2003-10-09 23:10:14 +00:00
|
|
|
std::map<Value*, Value*> ForwardRefMapping;
|
|
|
|
for (std::map<std::pair<unsigned,unsigned>, Value*>::iterator
|
|
|
|
I = ForwardReferences.begin(), E = ForwardReferences.end();
|
|
|
|
I != E; ++I)
|
|
|
|
ForwardRefMapping[I->second] = getValue(I->first.first, I->first.second,
|
|
|
|
false);
|
|
|
|
|
|
|
|
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
|
|
|
if (Argument *A = dyn_cast<Argument>(I->getOperand(i))) {
|
|
|
|
std::map<Value*, Value*>::iterator It = ForwardRefMapping.find(A);
|
|
|
|
if (It != ForwardRefMapping.end()) I->setOperand(i, It->second);
|
|
|
|
}
|
2003-10-08 22:52:54 +00:00
|
|
|
|
2003-10-09 06:05:40 +00:00
|
|
|
while (!ForwardReferences.empty()) {
|
2003-10-09 22:39:30 +00:00
|
|
|
std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
|
|
|
|
ForwardReferences.begin();
|
2003-10-09 06:05:40 +00:00
|
|
|
Value *PlaceHolder = I->second;
|
|
|
|
ForwardReferences.erase(I);
|
2003-10-08 21:51:46 +00:00
|
|
|
|
2003-10-09 06:05:40 +00:00
|
|
|
// Now that all the uses are gone, delete the placeholder...
|
|
|
|
// If we couldn't find a def (error case), then leak a little
|
|
|
|
// memory, because otherwise we can't remove all uses!
|
|
|
|
delete PlaceHolder;
|
2003-10-08 21:51:46 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
// Clear out function-level types...
|
2004-06-29 23:29:38 +00:00
|
|
|
FunctionTypes.clear();
|
|
|
|
CompactionTypes.clear();
|
|
|
|
CompactionValues.clear();
|
|
|
|
freeTable(FunctionValues);
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleFunctionEnd(F);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This function parses LLVM functions lazily. It obtains the type of the
|
|
|
|
/// function and records where the body of the function is in the bytecode
|
|
|
|
/// buffer. The caller can then use the ParseNextFunction and
|
|
|
|
/// ParseAllFunctionBodies to get handler events for the functions.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseFunctionLazily() {
|
|
|
|
if (FunctionSignatureList.empty())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("FunctionSignatureList empty!");
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
Function *Func = FunctionSignatureList.back();
|
|
|
|
FunctionSignatureList.pop_back();
|
2004-01-18 22:35:34 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Save the information for future reading of the function
|
|
|
|
LazyFunctionLoadMap[Func] = LazyFunctionInfo(BlockStart, BlockEnd);
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Pretend we've `parsed' this function
|
|
|
|
At = BlockEnd;
|
|
|
|
}
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// The ParserFunction method lazily parses one function. Use this method to
|
|
|
|
/// casue the parser to parse a specific function in the module. Note that
|
|
|
|
/// this will remove the function from what is to be included by
|
|
|
|
/// ParseAllFunctionBodies.
|
|
|
|
/// @see ParseAllFunctionBodies
|
|
|
|
/// @see ParseBytecode
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseFunction(Function* Func) {
|
|
|
|
// Find {start, end} pointers and slot in the map. If not there, we're done.
|
|
|
|
LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.find(Func);
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Make sure we found it
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Fi == LazyFunctionLoadMap.end()) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Unrecognized function of type " + Func->getType()->getDescription());
|
2004-06-29 23:29:38 +00:00
|
|
|
return;
|
2004-01-18 21:08:15 +00:00
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
BlockStart = At = Fi->second.Buf;
|
|
|
|
BlockEnd = Fi->second.EndBuf;
|
2004-07-09 22:21:33 +00:00
|
|
|
assert(Fi->first == Func && "Found wrong function?");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
LazyFunctionLoadMap.erase(Fi);
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
this->ParseFunctionBody(Func);
|
2004-01-18 21:08:15 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// The ParseAllFunctionBodies method parses through all the previously
|
|
|
|
/// unparsed functions in the bytecode file. If you want to completely parse
|
|
|
|
/// a bytecode file, this method should be called after Parsebytecode because
|
|
|
|
/// Parsebytecode only records the locations in the bytecode file of where
|
|
|
|
/// the function definitions are located. This function uses that information
|
|
|
|
/// to materialize the functions.
|
|
|
|
/// @see ParseBytecode
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseAllFunctionBodies() {
|
|
|
|
LazyFunctionMap::iterator Fi = LazyFunctionLoadMap.begin();
|
|
|
|
LazyFunctionMap::iterator Fe = LazyFunctionLoadMap.end();
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
while (Fi != Fe) {
|
2004-06-29 23:29:38 +00:00
|
|
|
Function* Func = Fi->first;
|
|
|
|
BlockStart = At = Fi->second.Buf;
|
|
|
|
BlockEnd = Fi->second.EndBuf;
|
|
|
|
this->ParseFunctionBody(Func);
|
|
|
|
++Fi;
|
|
|
|
}
|
|
|
|
}
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the global type list
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseGlobalTypes() {
|
2004-07-04 11:33:49 +00:00
|
|
|
// Read the number of types
|
|
|
|
unsigned NumEntries = read_vbr_uint();
|
2004-07-09 21:13:53 +00:00
|
|
|
|
|
|
|
// Ignore the type plane identifier for types if the bc file is pre 1.3
|
|
|
|
if (hasTypeDerivedFromValue)
|
|
|
|
read_vbr_uint();
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
ParseTypes(ModuleTypes, NumEntries);
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2004-01-18 21:08:15 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the Global info (types, global vars, constants)
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseModuleGlobalInfo() {
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleModuleGlobalsBegin();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Read global variables...
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned VarType = read_vbr_uint();
|
2001-09-10 07:58:01 +00:00
|
|
|
while (VarType != Type::VoidTyID) { // List is terminated by Void
|
2004-04-03 23:43:42 +00:00
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer, bit2,3,4 =
|
|
|
|
// Linkage, bit4+ = slot#
|
|
|
|
unsigned SlotNo = VarType >> 5;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (sanitizeTypeId(SlotNo))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid type (type type) for global var!");
|
2004-04-03 23:43:42 +00:00
|
|
|
unsigned LinkageID = (VarType >> 2) & 7;
|
2004-06-29 23:29:38 +00:00
|
|
|
bool isConstant = VarType & 1;
|
|
|
|
bool hasInitializer = VarType & 2;
|
2003-04-16 21:16:05 +00:00
|
|
|
GlobalValue::LinkageTypes Linkage;
|
|
|
|
|
2004-01-14 16:44:44 +00:00
|
|
|
switch (LinkageID) {
|
|
|
|
case 0: Linkage = GlobalValue::ExternalLinkage; break;
|
|
|
|
case 1: Linkage = GlobalValue::WeakLinkage; break;
|
|
|
|
case 2: Linkage = GlobalValue::AppendingLinkage; break;
|
|
|
|
case 3: Linkage = GlobalValue::InternalLinkage; break;
|
|
|
|
case 4: Linkage = GlobalValue::LinkOnceLinkage; break;
|
2004-06-29 23:29:38 +00:00
|
|
|
default:
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Unknown linkage type: " + utostr(LinkageID));
|
2004-06-29 23:29:38 +00:00
|
|
|
Linkage = GlobalValue::InternalLinkage;
|
|
|
|
break;
|
2003-04-16 21:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *Ty = getType(SlotNo);
|
2004-07-11 17:28:43 +00:00
|
|
|
if (!Ty) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Global has no type! SlotNo=" + utostr(SlotNo));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
if (!isa<PointerType>(Ty)) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Global not a pointer type! Ty= " + Ty->getDescription());
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
const Type *ElTy = cast<PointerType>(Ty)->getElementType();
|
2001-09-18 04:01:05 +00:00
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Create the global variable...
|
2004-06-29 23:29:38 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
|
2003-03-19 20:54:26 +00:00
|
|
|
0, "", TheModule);
|
2003-11-19 17:27:18 +00:00
|
|
|
insertValue(GV, SlotNo, ModuleValues);
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
unsigned initSlot = 0;
|
|
|
|
if (hasInitializer) {
|
|
|
|
initSlot = read_vbr_uint();
|
|
|
|
GlobalInits.push_back(std::make_pair(GV, initSlot));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify handler about the global value.
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo, initSlot);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Get next item
|
|
|
|
VarType = read_vbr_uint();
|
2001-09-10 07:58:01 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
// Read the function objects for all of the functions that are coming
|
2004-07-04 11:33:49 +00:00
|
|
|
unsigned FnSignature = 0;
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(FnSignature))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid function type (type type) found");
|
|
|
|
|
2002-08-17 22:01:27 +00:00
|
|
|
while (FnSignature != Type::VoidTyID) { // List is terminated by Void
|
|
|
|
const Type *Ty = getType(FnSignature);
|
2003-10-09 20:22:47 +00:00
|
|
|
if (!isa<PointerType>(Ty) ||
|
2004-06-29 23:29:38 +00:00
|
|
|
!isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Function not a pointer to function type! Ty = " +
|
2004-07-11 17:28:43 +00:00
|
|
|
Ty->getDescription());
|
2004-06-29 23:29:38 +00:00
|
|
|
// FIXME: what should Ty be if handler continues?
|
|
|
|
}
|
2002-10-23 00:51:54 +00:00
|
|
|
|
2003-03-06 17:15:19 +00:00
|
|
|
// We create functions by passing the underlying FunctionType to create...
|
2004-06-29 23:29:38 +00:00
|
|
|
const FunctionType* FTy =
|
|
|
|
cast<FunctionType>(cast<PointerType>(Ty)->getElementType());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Insert the place hodler
|
|
|
|
Function* Func = new Function(FTy, GlobalValue::InternalLinkage,
|
2004-07-04 11:33:49 +00:00
|
|
|
"", TheModule);
|
2003-11-19 17:27:18 +00:00
|
|
|
insertValue(Func, FnSignature, ModuleValues);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Save this for later so we know type of lazily instantiated functions
|
2003-11-19 17:27:18 +00:00
|
|
|
FunctionSignatureList.push_back(Func);
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleFunctionDeclaration(Func);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Get Next function signature
|
2004-07-11 17:28:43 +00:00
|
|
|
if (read_typeid(FnSignature))
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid function type (type type) found");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-01-15 17:55:01 +00:00
|
|
|
if (hasInconsistentModuleGlobalInfo)
|
2004-06-29 23:29:38 +00:00
|
|
|
align32();
|
2002-08-17 22:01:27 +00:00
|
|
|
|
|
|
|
// Now that the function signature list is set up, reverse it so that we can
|
|
|
|
// remove elements efficiently from the back of the vector.
|
|
|
|
std::reverse(FunctionSignatureList.begin(), FunctionSignatureList.end());
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// This is for future proofing... in the future extra fields may be added that
|
|
|
|
// we don't understand, so we transparently ignore them.
|
|
|
|
//
|
2004-06-29 23:29:38 +00:00
|
|
|
At = BlockEnd;
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleModuleGlobalsEnd();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse the version information and decode it by setting flags on the
|
|
|
|
/// Reader that enable backward compatibility of the reader.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseVersionInfo() {
|
|
|
|
unsigned Version = read_vbr_uint();
|
2003-03-06 17:55:45 +00:00
|
|
|
|
|
|
|
// Unpack version number: low four bits are for flags, top bits = version
|
2003-08-24 13:47:36 +00:00
|
|
|
Module::Endianness Endianness;
|
|
|
|
Module::PointerSize PointerSize;
|
|
|
|
Endianness = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
|
|
|
|
PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
|
|
|
|
|
|
|
|
bool hasNoEndianness = Version & 4;
|
|
|
|
bool hasNoPointerSize = Version & 8;
|
|
|
|
|
|
|
|
RevisionNum = Version >> 4;
|
2003-04-16 21:16:05 +00:00
|
|
|
|
|
|
|
// Default values for the current bytecode version
|
2004-01-15 17:55:01 +00:00
|
|
|
hasInconsistentModuleGlobalInfo = false;
|
2004-01-17 23:25:43 +00:00
|
|
|
hasExplicitPrimitiveZeros = false;
|
2004-04-05 01:27:26 +00:00
|
|
|
hasRestrictedGEPTypes = false;
|
2004-07-04 11:33:49 +00:00
|
|
|
hasTypeDerivedFromValue = false;
|
2003-03-06 17:55:45 +00:00
|
|
|
|
|
|
|
switch (RevisionNum) {
|
2004-01-14 16:44:44 +00:00
|
|
|
case 0: // LLVM 1.0, 1.1 release version
|
2004-01-14 23:35:21 +00:00
|
|
|
// Base LLVM 1.0 bytecode format.
|
2004-01-15 17:55:01 +00:00
|
|
|
hasInconsistentModuleGlobalInfo = true;
|
2004-01-17 23:25:43 +00:00
|
|
|
hasExplicitPrimitiveZeros = true;
|
2004-07-04 11:33:49 +00:00
|
|
|
|
2004-01-17 23:25:43 +00:00
|
|
|
// FALL THROUGH
|
2004-01-14 16:44:44 +00:00
|
|
|
case 1: // LLVM 1.2 release version
|
2004-01-14 23:35:21 +00:00
|
|
|
// LLVM 1.2 added explicit support for emitting strings efficiently.
|
2004-01-15 17:55:01 +00:00
|
|
|
|
|
|
|
// Also, it fixed the problem where the size of the ModuleGlobalInfo block
|
|
|
|
// included the size for the alignment at the end, where the rest of the
|
|
|
|
// blocks did not.
|
2004-04-05 01:27:26 +00:00
|
|
|
|
|
|
|
// LLVM 1.2 and before required that GEP indices be ubyte constants for
|
|
|
|
// structures and longs for sequential types.
|
|
|
|
hasRestrictedGEPTypes = true;
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
// LLVM 1.2 and before had the Type class derive from Value class. This
|
|
|
|
// changed in release 1.3 and consequently LLVM 1.3 bytecode files are
|
|
|
|
// written differently because Types can no longer be part of the
|
|
|
|
// type planes for Values.
|
|
|
|
hasTypeDerivedFromValue = true;
|
|
|
|
|
2004-04-05 01:27:26 +00:00
|
|
|
// FALL THROUGH
|
|
|
|
case 2: // LLVM 1.3 release version
|
2004-01-14 16:44:44 +00:00
|
|
|
break;
|
|
|
|
|
2003-03-06 17:55:45 +00:00
|
|
|
default:
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Unknown bytecode version number: " + itostr(RevisionNum));
|
2003-03-06 17:55:45 +00:00
|
|
|
}
|
|
|
|
|
2003-08-24 13:47:36 +00:00
|
|
|
if (hasNoEndianness) Endianness = Module::AnyEndianness;
|
|
|
|
if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
|
2003-04-22 18:15:10 +00:00
|
|
|
|
2004-07-14 20:33:13 +00:00
|
|
|
TheModule->setEndianness(Endianness);
|
|
|
|
TheModule->setPointerSize(PointerSize);
|
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize);
|
2003-03-06 17:55:45 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// Parse a whole module.
|
2004-06-29 23:29:38 +00:00
|
|
|
void BytecodeReader::ParseModule() {
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned Type, Size;
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
FunctionSignatureList.clear(); // Just in case...
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Read into instance variables...
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseVersionInfo();
|
|
|
|
align32(); /// FIXME: Is this redundant? VI is first and 4 bytes!
|
|
|
|
|
|
|
|
bool SeenModuleGlobalInfo = false;
|
|
|
|
bool SeenGlobalTypePlane = false;
|
|
|
|
BufPtr MyEnd = BlockEnd;
|
|
|
|
while (At < MyEnd) {
|
|
|
|
BufPtr OldAt = At;
|
|
|
|
read_block(Type, Size);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
switch (Type) {
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
case BytecodeFormat::GlobalTypePlane:
|
2004-07-11 17:28:43 +00:00
|
|
|
if (SeenGlobalTypePlane)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Two GlobalTypePlane Blocks Encountered!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
ParseGlobalTypes();
|
|
|
|
SeenGlobalTypePlane = true;
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
case BytecodeFormat::ModuleGlobalInfo:
|
2004-07-11 17:28:43 +00:00
|
|
|
if (SeenModuleGlobalInfo)
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Two ModuleGlobalInfo Blocks Encountered!");
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseModuleGlobalInfo();
|
|
|
|
SeenModuleGlobalInfo = true;
|
2003-03-19 20:54:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BytecodeFormat::ConstantPool:
|
2004-07-04 11:33:49 +00:00
|
|
|
ParseConstantPool(ModuleValues, ModuleTypes,false);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
case BytecodeFormat::Function:
|
|
|
|
ParseFunctionLazily();
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BytecodeFormat::SymbolTable:
|
2004-06-29 23:29:38 +00:00
|
|
|
ParseSymbolTable(0, &TheModule->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
default:
|
2004-06-29 23:29:38 +00:00
|
|
|
At += Size;
|
|
|
|
if (OldAt > At) {
|
2004-07-11 17:28:43 +00:00
|
|
|
error("Unexpected Block of Type #" + utostr(Type) + " encountered!");
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
BlockEnd = MyEnd;
|
|
|
|
align32();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
// After the module constant pool has been read, we can safely initialize
|
|
|
|
// global variables...
|
|
|
|
while (!GlobalInits.empty()) {
|
|
|
|
GlobalVariable *GV = GlobalInits.back().first;
|
|
|
|
unsigned Slot = GlobalInits.back().second;
|
|
|
|
GlobalInits.pop_back();
|
|
|
|
|
|
|
|
// Look up the initializer value...
|
2003-11-19 17:27:18 +00:00
|
|
|
// FIXME: Preserve this type ID!
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
const llvm::PointerType* GVType = GV->getType();
|
|
|
|
unsigned TypeSlot = getTypeSlot(GVType->getElementType());
|
2004-01-15 18:45:25 +00:00
|
|
|
if (Constant *CV = getConstantValue(TypeSlot, Slot)) {
|
2003-09-22 23:38:23 +00:00
|
|
|
if (GV->hasInitializer())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Global *already* has an initializer?!");
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleGlobalInitializer(GV,CV);
|
2004-01-15 18:45:25 +00:00
|
|
|
GV->setInitializer(CV);
|
2003-03-19 20:54:26 +00:00
|
|
|
} else
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Cannot find initializer value.");
|
2003-03-19 20:54:26 +00:00
|
|
|
}
|
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
/// Make sure we pulled them all out. If we didn't then there's a declaration
|
|
|
|
/// but a missing body. That's not allowed.
|
2003-09-22 23:38:23 +00:00
|
|
|
if (!FunctionSignatureList.empty())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Function declared, but bytecode stream ended before definition");
|
2003-03-06 17:15:19 +00:00
|
|
|
}
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
/// This function completely parses a bytecode buffer given by the \p Buf
|
|
|
|
/// and \p Length parameters.
|
2004-07-11 17:28:43 +00:00
|
|
|
void BytecodeReader::ParseBytecode(BufPtr Buf, unsigned Length,
|
|
|
|
const std::string &ModuleID,
|
|
|
|
bool processFunctions) {
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
At = MemStart = BlockStart = Buf;
|
|
|
|
MemEnd = BlockEnd = Buf + Length;
|
|
|
|
|
|
|
|
// Create the module
|
|
|
|
TheModule = new Module(ModuleID);
|
|
|
|
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleStart(TheModule, Length);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Read and check signature...
|
|
|
|
unsigned Sig = read_uint();
|
|
|
|
if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24))) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid bytecode signature: " + utostr(Sig));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Tell the handler we're starting a module
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleModuleBegin(ModuleID);
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
// Get the module block and size and verify
|
|
|
|
unsigned Type, Size;
|
|
|
|
read_block(Type, Size);
|
2004-07-11 17:28:43 +00:00
|
|
|
if (Type != BytecodeFormat::Module) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Expected Module Block! Type:" + utostr(Type) + ", Size:"
|
2004-07-11 17:28:43 +00:00
|
|
|
+ utostr(Size));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2004-07-11 17:28:43 +00:00
|
|
|
if (At + Size != MemEnd) {
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Invalid Top Level Block Length! Type:" + utostr(Type)
|
2004-07-11 17:28:43 +00:00
|
|
|
+ ", Size:" + utostr(Size));
|
2004-06-29 23:29:38 +00:00
|
|
|
}
|
2003-09-23 16:15:29 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Parse the module contents
|
|
|
|
this->ParseModule();
|
2003-09-23 16:15:29 +00:00
|
|
|
|
2004-06-29 23:29:38 +00:00
|
|
|
// Check for missing functions
|
2004-07-11 17:28:43 +00:00
|
|
|
if (hasFunctions())
|
2004-07-09 22:21:33 +00:00
|
|
|
error("Function expected, but bytecode stream ended!");
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-05 00:57:50 +00:00
|
|
|
// Process all the function bodies now, if requested
|
2004-07-11 17:28:43 +00:00
|
|
|
if (processFunctions)
|
2004-07-05 00:57:50 +00:00
|
|
|
ParseAllFunctionBodies();
|
|
|
|
|
|
|
|
// Tell the handler we're done with the module
|
|
|
|
if (Handler)
|
|
|
|
Handler->handleModuleEnd(ModuleID);
|
|
|
|
|
|
|
|
// Tell the handler we're finished the parse
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleFinish();
|
2004-06-29 23:29:38 +00:00
|
|
|
|
2004-07-11 17:28:43 +00:00
|
|
|
} catch (std::string& errstr) {
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleError(errstr);
|
2004-06-29 23:29:38 +00:00
|
|
|
freeState();
|
2003-03-06 17:15:19 +00:00
|
|
|
delete TheModule;
|
|
|
|
TheModule = 0;
|
2003-09-26 14:44:52 +00:00
|
|
|
throw;
|
2004-06-29 23:29:38 +00:00
|
|
|
} catch (...) {
|
|
|
|
std::string msg("Unknown Exception Occurred");
|
2004-07-04 11:33:49 +00:00
|
|
|
if (Handler) Handler->handleError(msg);
|
2004-06-29 23:29:38 +00:00
|
|
|
freeState();
|
|
|
|
delete TheModule;
|
|
|
|
TheModule = 0;
|
|
|
|
throw msg;
|
2003-03-06 17:15:19 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-06-29 23:29:38 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== Default Implementations of Handler Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
BytecodeHandler::~BytecodeHandler() {}
|
|
|
|
|
|
|
|
// vim: sw=2
|