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
|
|
|
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "ReaderInternals.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Format.h"
|
2003-09-22 23:38:23 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "Support/StringExtras.h"
|
2003-11-19 17:27:18 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2003-10-04 20:00:03 +00:00
|
|
|
unsigned BytecodeParser::getTypeSlot(const Type *Ty) {
|
|
|
|
if (Ty->isPrimitiveType())
|
|
|
|
return Ty->getPrimitiveID();
|
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
// Scan the compaction table for the type if needed.
|
|
|
|
if (CompactionTable.size() > Type::TypeTyID) {
|
|
|
|
std::vector<Value*> &Plane = CompactionTable[Type::TypeTyID];
|
|
|
|
if (!Plane.empty()) {
|
|
|
|
std::vector<Value*>::iterator I = find(Plane.begin(), Plane.end(),
|
|
|
|
const_cast<Type*>(Ty));
|
|
|
|
if (I == Plane.end())
|
|
|
|
throw std::string("Couldn't find type specified in compaction table!");
|
|
|
|
return Type::FirstDerivedTyID + (&*I - &Plane[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-04 20:00:03 +00:00
|
|
|
// Check the function level types first...
|
|
|
|
TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
|
|
|
|
FunctionTypeValues.end(), Ty);
|
|
|
|
if (I != FunctionTypeValues.end())
|
2004-01-18 21:08:15 +00:00
|
|
|
return Type::FirstDerivedTyID + ModuleTypeValues.size() +
|
2003-10-04 20:00:03 +00:00
|
|
|
(&*I - &FunctionTypeValues[0]);
|
|
|
|
|
|
|
|
I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
|
|
|
|
if (I == ModuleTypeValues.end())
|
|
|
|
throw std::string("Didn't find type in ModuleTypeValues.");
|
2004-01-18 21:08:15 +00:00
|
|
|
return Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *BytecodeParser::getType(unsigned ID) {
|
2002-01-20 22:54:45 +00:00
|
|
|
//cerr << "Looking up Type ID: " << ID << "\n";
|
2003-10-08 21:18:57 +00:00
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
if (ID < Type::FirstDerivedTyID)
|
2003-10-09 20:22:47 +00:00
|
|
|
if (const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID))
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (CompactionTable.size() > Type::TypeTyID &&
|
|
|
|
!CompactionTable[Type::TypeTyID].empty()) {
|
|
|
|
if (ID >= CompactionTable[Type::TypeTyID].size())
|
|
|
|
throw std::string("Type ID out of range for compaction table!");
|
|
|
|
return cast<Type>(CompactionTable[Type::TypeTyID][ID]);
|
|
|
|
}
|
2003-10-08 21:18:57 +00:00
|
|
|
|
|
|
|
// Is it a module-level type?
|
|
|
|
if (ID < ModuleTypeValues.size())
|
|
|
|
return ModuleTypeValues[ID].get();
|
|
|
|
|
|
|
|
// Nope, is it a function-level type?
|
|
|
|
ID -= ModuleTypeValues.size();
|
|
|
|
if (ID < FunctionTypeValues.size())
|
|
|
|
return FunctionTypeValues[ID].get();
|
|
|
|
|
2003-10-09 20:22:47 +00:00
|
|
|
throw std::string("Illegal type reference!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-01-17 23:25:43 +00:00
|
|
|
static inline bool hasImplicitNull(unsigned TyID, bool EncodesPrimitiveZeros) {
|
|
|
|
if (!EncodesPrimitiveZeros)
|
|
|
|
return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
|
|
|
|
TyID != Type::VoidTyID;
|
|
|
|
return TyID >= Type::FirstDerivedTyID;
|
|
|
|
}
|
|
|
|
|
2003-10-13 14:34:59 +00:00
|
|
|
unsigned BytecodeParser::insertValue(Value *Val, unsigned type,
|
|
|
|
ValueTable &ValueTab) {
|
2004-01-17 23:25:43 +00:00
|
|
|
assert((!isa<Constant>(Val) || !cast<Constant>(Val)->isNullValue()) ||
|
|
|
|
!hasImplicitNull(type, hasExplicitPrimitiveZeros) &&
|
2003-03-19 20:54:26 +00:00
|
|
|
"Cannot read null values from bytecode!");
|
2001-09-07 16:37:43 +00:00
|
|
|
assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
|
2003-11-19 17:27:18 +00:00
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
if (ValueTab.size() <= type)
|
2003-03-19 20:54:26 +00:00
|
|
|
ValueTab.resize(type+1);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
if (!ValueTab[type]) ValueTab[type] = new ValueList();
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
|
2003-09-22 23:38:23 +00:00
|
|
|
// << "] = " << Val << "\n";
|
2003-03-19 20:54:26 +00:00
|
|
|
ValueTab[type]->push_back(Val);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
bool HasOffset = hasImplicitNull(type, hasExplicitPrimitiveZeros);
|
2003-03-19 20:54:26 +00:00
|
|
|
return ValueTab[type]->size()-1 + HasOffset;
|
|
|
|
}
|
|
|
|
|
2003-10-08 21:18:57 +00:00
|
|
|
Value *BytecodeParser::getValue(unsigned type, unsigned oNum, bool Create) {
|
|
|
|
assert(type != Type::TypeTyID && "getValue() cannot get types!");
|
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.
|
|
|
|
if (CompactionTable.size() > type && !CompactionTable[type].empty()) {
|
|
|
|
if (Num < CompactionTable[type].size())
|
|
|
|
return CompactionTable[type][Num];
|
|
|
|
Num -= CompactionTable[type].size();
|
|
|
|
} else {
|
2004-01-20 00:54:06 +00:00
|
|
|
// If the type plane was compactified, figure out the global type ID.
|
|
|
|
unsigned GlobalTyID = type;
|
|
|
|
if (CompactionTable.size() > Type::TypeTyID &&
|
|
|
|
!CompactionTable[Type::TypeTyID].empty() &&
|
|
|
|
type >= Type::FirstDerivedTyID) {
|
|
|
|
std::vector<Value*> &TypePlane = CompactionTable[Type::TypeTyID];
|
|
|
|
const Type *Ty = cast<Type>(TypePlane[type-Type::FirstDerivedTyID]);
|
|
|
|
TypeValuesListTy::iterator I =
|
|
|
|
find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
|
|
|
|
assert(I != ModuleTypeValues.end());
|
|
|
|
GlobalTyID = Type::FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
|
|
|
|
}
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2004-01-20 00:54:06 +00:00
|
|
|
if (hasImplicitNull(GlobalTyID, hasExplicitPrimitiveZeros)) {
|
2004-01-18 21:08:15 +00:00
|
|
|
if (Num == 0)
|
|
|
|
return Constant::getNullValue(getType(type));
|
|
|
|
--Num;
|
|
|
|
}
|
|
|
|
|
2004-01-20 00:54:06 +00:00
|
|
|
if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
|
|
|
|
if (Num < ModuleValues[GlobalTyID]->size())
|
|
|
|
return ModuleValues[GlobalTyID]->getOperand(Num);
|
|
|
|
Num -= ModuleValues[GlobalTyID]->size();
|
2004-01-18 21:08:15 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
if (Values.size() > type && Values[type] && Num < Values[type]->size())
|
2003-03-19 20:54:26 +00:00
|
|
|
return Values[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);
|
|
|
|
std::map<std::pair<unsigned,unsigned>, Value*>::iterator I =
|
|
|
|
ForwardReferences.lower_bound(KeyValue);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
/// getBasicBlock - 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.
|
|
|
|
///
|
|
|
|
BasicBlock *BytecodeParser::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();
|
|
|
|
}
|
|
|
|
|
2002-10-14 03:33:02 +00:00
|
|
|
/// getConstantValue - 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.
|
|
|
|
///
|
2003-11-19 06:01:12 +00:00
|
|
|
Constant *BytecodeParser::getConstantValue(unsigned TypeSlot, unsigned Slot) {
|
|
|
|
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
|
2004-01-15 18:45:25 +00:00
|
|
|
else if (GlobalValue *GV = dyn_cast<GlobalValue>(V))
|
|
|
|
// ConstantPointerRef's are an abomination, but at least they don't have
|
|
|
|
// to infest bytecode files.
|
|
|
|
return ConstantPointerRef::get(GV);
|
2003-10-09 20:41:16 +00:00
|
|
|
else
|
|
|
|
throw std::string("Reference of a value 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) {
|
2002-10-14 03:33:02 +00:00
|
|
|
BCR_TRACE(5, "Previous forward ref found!\n");
|
2003-11-19 17:27:18 +00:00
|
|
|
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
|
|
|
|
BCR_TRACE(5, "Creating new forward ref to a constant!\n");
|
|
|
|
Constant *C = new ConstPHolder(Ty, Slot);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-01 07:05:31 +00:00
|
|
|
/// ParseBasicBlock - In LLVM 1.0 bytecode files, we used to output one
|
|
|
|
/// basicblock at a time. This method reads in one of the basicblock packets.
|
2003-10-08 22:52:54 +00:00
|
|
|
BasicBlock *BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf,
|
|
|
|
unsigned BlockNo) {
|
|
|
|
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];
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
std::vector<unsigned> Args;
|
|
|
|
while (Buf < EndBuf)
|
|
|
|
ParseInstruction(Buf, EndBuf, Args, BB);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
return BB;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-12-01 07:05:31 +00:00
|
|
|
|
|
|
|
/// ParseInstructionList - 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.
|
|
|
|
unsigned BytecodeParser::ParseInstructionList(Function *F,
|
|
|
|
const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
|
|
|
unsigned BlockNo = 0;
|
|
|
|
std::vector<unsigned> Args;
|
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
|
|
|
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
|
|
|
|
while (Buf < EndBuf && !BB->getTerminator())
|
|
|
|
ParseInstruction(Buf, EndBuf, Args, BB);
|
|
|
|
|
|
|
|
if (!BB->getTerminator())
|
|
|
|
throw std::string("Non-terminated basic block found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return BlockNo;
|
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
|
2003-05-22 18:08:30 +00:00
|
|
|
const unsigned char *EndBuf,
|
2003-10-08 22:52:54 +00:00
|
|
|
SymbolTable *ST,
|
|
|
|
Function *CurrentFunction) {
|
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);
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Buf < EndBuf) {
|
|
|
|
// Symtab block header: [num entries][type id number]
|
2004-01-15 06:13:09 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint(Buf, EndBuf);
|
|
|
|
unsigned Typ = read_vbr_uint(Buf, EndBuf);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
2003-10-09 20:22:47 +00:00
|
|
|
BCR_TRACE(3, "Plane Type: '" << *Ty << "' with " << NumEntries <<
|
2003-09-22 23:38:23 +00:00
|
|
|
" entries\n");
|
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-01-15 06:13:09 +00:00
|
|
|
unsigned slot = read_vbr_uint(Buf, EndBuf);
|
|
|
|
std::string Name = read_str(Buf, EndBuf);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
Value *V = 0;
|
2003-10-08 21:18:57 +00:00
|
|
|
if (Typ == Type::TypeTyID)
|
|
|
|
V = (Value*)getType(slot);
|
2003-10-08 22:52:54 +00:00
|
|
|
else if (Typ == Type::LabelTyID) {
|
2003-10-10 05:43:47 +00:00
|
|
|
if (slot < BBMap.size())
|
|
|
|
V = BBMap[slot];
|
|
|
|
} else {
|
2003-10-08 21:18:57 +00:00
|
|
|
V = getValue(Typ, slot, false); // Find mapping...
|
2003-10-10 05:43:47 +00:00
|
|
|
}
|
2004-01-20 00:54:06 +00:00
|
|
|
if (V == 0)
|
|
|
|
throw std::string("Failed value look-up.");
|
2003-03-19 20:54:26 +00:00
|
|
|
BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
|
2003-09-22 23:38:23 +00:00
|
|
|
if (!isa<Instruction>(V)) std::cerr << "\n");
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
V->setName(Name, ST);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
void BytecodeParser::ResolveReferencesToConstant(Constant *NewV, unsigned Slot){
|
|
|
|
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
|
|
|
|
2002-08-17 22:01:27 +00:00
|
|
|
BCR_TRACE(3, "Mutating forward refs!\n");
|
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
|
|
|
}
|
|
|
|
|
2003-10-08 22:52:54 +00:00
|
|
|
void BytecodeParser::ParseFunction(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
2003-09-22 23:38:23 +00:00
|
|
|
if (FunctionSignatureList.empty())
|
|
|
|
throw std::string("FunctionSignatureList empty!");
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
Function *F = FunctionSignatureList.back();
|
2003-09-22 23:38:23 +00:00
|
|
|
FunctionSignatureList.pop_back();
|
|
|
|
|
|
|
|
// Save the information for future reading of the function
|
2003-11-19 17:27:18 +00:00
|
|
|
LazyFunctionLoadMap[F] = LazyFunctionInfo(Buf, EndBuf);
|
2003-09-22 23:38:23 +00:00
|
|
|
// Pretend we've `parsed' this function
|
|
|
|
Buf = EndBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BytecodeParser::materializeFunction(Function* F) {
|
|
|
|
// Find {start, end} pointers and slot in the map. If not there, we're done.
|
2003-11-19 17:27:18 +00:00
|
|
|
std::map<Function*, LazyFunctionInfo>::iterator Fi =
|
2003-09-22 23:38:23 +00:00
|
|
|
LazyFunctionLoadMap.find(F);
|
|
|
|
if (Fi == LazyFunctionLoadMap.end()) return;
|
2003-11-19 17:27:18 +00:00
|
|
|
|
|
|
|
const unsigned char *Buf = Fi->second.Buf;
|
|
|
|
const unsigned char *EndBuf = Fi->second.EndBuf;
|
2003-09-22 23:38:23 +00:00
|
|
|
LazyFunctionLoadMap.erase(Fi);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-04-16 21:16:05 +00:00
|
|
|
GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
|
|
|
|
|
2004-01-15 06:13:09 +00:00
|
|
|
unsigned LinkageType = read_vbr_uint(Buf, EndBuf);
|
2004-01-14 16:44:44 +00:00
|
|
|
if ((!hasExtendedLinkageSpecs && LinkageType > 3) ||
|
|
|
|
( hasExtendedLinkageSpecs && LinkageType > 4))
|
|
|
|
throw std::string("Invalid linkage type for Function.");
|
|
|
|
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;
|
2003-04-16 21:16:05 +00:00
|
|
|
}
|
2001-11-26 18:56:10 +00:00
|
|
|
|
2003-04-16 21:16:05 +00:00
|
|
|
F->setLinkage(Linkage);
|
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
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Buf < EndBuf) {
|
|
|
|
unsigned Type, Size;
|
2003-03-06 17:03:28 +00:00
|
|
|
const unsigned char *OldBuf = Buf;
|
2003-09-22 23:38:23 +00:00
|
|
|
readBlock(Buf, EndBuf, 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-02-09 04:14:01 +00:00
|
|
|
const FunctionType *FT = F->getFunctionType();
|
2004-01-18 21:08:15 +00:00
|
|
|
Function::aiterator AI = F->abegin();
|
2004-02-09 04:14:01 +00:00
|
|
|
for (FunctionType::param_iterator It = FT->param_begin();
|
|
|
|
It != FT->param_end(); ++It, ++AI)
|
2004-01-18 21:08:15 +00:00
|
|
|
insertValue(AI, getTypeSlot(AI->getType()), Values);
|
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
case BytecodeFormat::CompactionTable:
|
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::CompactionTable: {\n");
|
|
|
|
ParseCompactionTable(Buf, Buf+Size);
|
|
|
|
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-02-09 04:14:01 +00:00
|
|
|
const FunctionType *FT = F->getFunctionType();
|
2004-01-18 21:08:15 +00:00
|
|
|
Function::aiterator AI = F->abegin();
|
2004-02-09 04:14:01 +00:00
|
|
|
for (FunctionType::param_iterator It = FT->param_begin();
|
|
|
|
It != FT->param_end(); ++It, ++AI)
|
2004-01-18 21:08:15 +00:00
|
|
|
insertValue(AI, getTypeSlot(AI->getType()), Values);
|
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
|
2003-10-08 22:52:54 +00:00
|
|
|
BasicBlock *BB = ParseBasicBlock(Buf, Buf+Size, BlockNum++);
|
|
|
|
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-02-09 04:14:01 +00:00
|
|
|
const FunctionType *FT = F->getFunctionType();
|
2004-01-18 21:08:15 +00:00
|
|
|
Function::aiterator AI = F->abegin();
|
2004-02-09 04:14:01 +00:00
|
|
|
for (FunctionType::param_iterator It = FT->param_begin();
|
|
|
|
It != FT->param_end(); ++It, ++AI)
|
2004-01-18 21:08:15 +00:00
|
|
|
insertValue(AI, getTypeSlot(AI->getType()), Values);
|
|
|
|
InsertedArguments = true;
|
|
|
|
}
|
|
|
|
|
2003-12-01 07:05:31 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::InstructionList: {\n");
|
|
|
|
if (BlockNum) throw std::string("Already parsed basic blocks!");
|
|
|
|
BlockNum = ParseInstructionList(F, Buf, Buf+Size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-19 17:27:18 +00:00
|
|
|
case BytecodeFormat::SymbolTable:
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
|
2003-10-08 22:52:54 +00:00
|
|
|
ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable(), F);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
Buf += Size;
|
2003-09-22 23:38:23 +00:00
|
|
|
if (OldBuf > Buf)
|
|
|
|
throw std::string("Wrapped around reading bytecode.");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "} end block\n");
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
// Malformed bc file if read past end of block.
|
2004-01-15 06:13:09 +00:00
|
|
|
align32(Buf, EndBuf);
|
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())
|
|
|
|
throw std::string("Illegal basic block operand reference");
|
|
|
|
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...
|
2003-03-06 17:18:14 +00:00
|
|
|
FunctionTypeValues.clear();
|
2004-01-18 21:08:15 +00:00
|
|
|
CompactionTable.clear();
|
2003-03-19 20:54:26 +00:00
|
|
|
freeTable(Values);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-01-18 21:08:15 +00:00
|
|
|
void BytecodeParser::ParseCompactionTable(const unsigned char *&Buf,
|
|
|
|
const unsigned char *End) {
|
|
|
|
|
|
|
|
while (Buf != End) {
|
2004-01-20 17:06:29 +00:00
|
|
|
unsigned NumEntries = read_vbr_uint(Buf, End);
|
2004-01-18 22:35:34 +00:00
|
|
|
unsigned Ty;
|
|
|
|
|
2004-01-20 17:06:29 +00:00
|
|
|
if ((NumEntries & 3) == 3) {
|
2004-01-18 22:35:34 +00:00
|
|
|
NumEntries >>= 2;
|
|
|
|
Ty = read_vbr_uint(Buf, End);
|
2004-01-20 17:06:29 +00:00
|
|
|
} else {
|
|
|
|
Ty = NumEntries >> 2;
|
|
|
|
NumEntries &= 3;
|
2004-01-18 22:35:34 +00:00
|
|
|
}
|
2004-01-18 21:08:15 +00:00
|
|
|
|
|
|
|
if (Ty >= CompactionTable.size())
|
|
|
|
CompactionTable.resize(Ty+1);
|
|
|
|
|
|
|
|
if (!CompactionTable[Ty].empty())
|
|
|
|
throw std::string("Compaction table plane contains multiple entries!");
|
|
|
|
|
|
|
|
if (Ty == Type::TypeTyID) {
|
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
|
|
|
const Type *Typ = getGlobalTableType(read_vbr_uint(Buf, End));
|
|
|
|
CompactionTable[Type::TypeTyID].push_back(const_cast<Type*>(Typ));
|
|
|
|
}
|
|
|
|
|
|
|
|
CompactionTable.resize(NumEntries+Type::FirstDerivedTyID);
|
|
|
|
} else {
|
|
|
|
const Type *Typ = getType(Ty);
|
|
|
|
// Push the implicit zero
|
|
|
|
CompactionTable[Ty].push_back(Constant::getNullValue(Typ));
|
2004-01-18 22:35:34 +00:00
|
|
|
for (unsigned i = 0; i != NumEntries; ++i) {
|
2004-01-18 21:08:15 +00:00
|
|
|
Value *V = getGlobalTableValue(Typ, read_vbr_uint(Buf, End));
|
|
|
|
CompactionTable[Ty].push_back(V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void BytecodeParser::ParseModuleGlobalInfo(const unsigned char *&Buf,
|
|
|
|
const unsigned char *End) {
|
|
|
|
if (!FunctionSignatureList.empty())
|
|
|
|
throw std::string("Two ModuleGlobalInfo packets found!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Read global variables...
|
2004-01-15 06:13:09 +00:00
|
|
|
unsigned VarType = read_vbr_uint(Buf, End);
|
2001-09-10 07:58:01 +00:00
|
|
|
while (VarType != Type::VoidTyID) { // List is terminated by Void
|
2003-04-16 21:16:05 +00:00
|
|
|
unsigned SlotNo;
|
|
|
|
GlobalValue::LinkageTypes Linkage;
|
|
|
|
|
2004-01-14 16:44:44 +00:00
|
|
|
unsigned LinkageID;
|
|
|
|
if (hasExtendedLinkageSpecs) {
|
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
|
|
|
|
// bit2,3,4 = Linkage, bit4+ = slot#
|
|
|
|
SlotNo = VarType >> 5;
|
|
|
|
LinkageID = (VarType >> 2) & 7;
|
2003-04-16 21:16:05 +00:00
|
|
|
} else {
|
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
|
2004-01-14 16:44:44 +00:00
|
|
|
// bit2,3 = Linkage, bit4+ = slot#
|
|
|
|
SlotNo = VarType >> 4;
|
|
|
|
LinkageID = (VarType >> 2) & 3;
|
|
|
|
}
|
|
|
|
switch (LinkageID) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
|
|
|
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;
|
2003-04-16 21:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *Ty = getType(SlotNo);
|
2003-10-09 20:22:47 +00:00
|
|
|
if (!isa<PointerType>(Ty))
|
2003-09-22 23:38:23 +00:00
|
|
|
throw std::string("Global not pointer type! Ty = " +
|
|
|
|
Ty->getDescription());
|
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...
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
|
2003-03-19 20:54:26 +00:00
|
|
|
0, "", TheModule);
|
|
|
|
BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
|
2003-11-19 17:27:18 +00:00
|
|
|
insertValue(GV, SlotNo, ModuleValues);
|
2001-10-13 06:47:01 +00:00
|
|
|
|
2004-01-15 06:13:09 +00:00
|
|
|
if (VarType & 2) // Does it have an initializer?
|
|
|
|
GlobalInits.push_back(std::make_pair(GV, read_vbr_uint(Buf, End)));
|
|
|
|
VarType = read_vbr_uint(Buf, End);
|
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-01-15 06:13:09 +00:00
|
|
|
unsigned FnSignature = read_vbr_uint(Buf, End);
|
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) ||
|
|
|
|
!isa<FunctionType>(cast<PointerType>(Ty)->getElementType()))
|
2003-09-22 23:38:23 +00:00
|
|
|
throw std::string("Function not ptr to func type! Ty = " +
|
|
|
|
Ty->getDescription());
|
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...
|
2001-12-04 00:03:30 +00:00
|
|
|
Ty = cast<PointerType>(Ty)->getElementType();
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-03-06 17:15:19 +00:00
|
|
|
// When the ModuleGlobalInfo section is read, we load the type of each
|
|
|
|
// function and the 'ModuleValues' slot that it lands in. We then load a
|
|
|
|
// placeholder into its slot to reserve it. When the function is loaded,
|
|
|
|
// this placeholder is replaced.
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Insert the placeholder...
|
2003-04-16 20:28:45 +00:00
|
|
|
Function *Func = new Function(cast<FunctionType>(Ty),
|
|
|
|
GlobalValue::InternalLinkage, "", TheModule);
|
2003-11-19 17:27:18 +00:00
|
|
|
insertValue(Func, FnSignature, ModuleValues);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-03-19 20:54:26 +00:00
|
|
|
// Keep track of this information in a list that is emptied as functions are
|
|
|
|
// loaded...
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2003-11-19 17:27:18 +00:00
|
|
|
FunctionSignatureList.push_back(Func);
|
2003-03-19 20:54:26 +00:00
|
|
|
|
2004-01-15 06:13:09 +00:00
|
|
|
FnSignature = read_vbr_uint(Buf, End);
|
2002-03-29 03:51:11 +00:00
|
|
|
BCR_TRACE(2, "Function of type: " << Ty << "\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-01-15 17:55:01 +00:00
|
|
|
if (hasInconsistentModuleGlobalInfo)
|
|
|
|
align32(Buf, End);
|
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.
|
|
|
|
//
|
|
|
|
Buf = End;
|
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
|
2003-05-22 18:08:30 +00:00
|
|
|
const unsigned char *EndBuf) {
|
2004-01-15 06:13:09 +00:00
|
|
|
unsigned Version = read_vbr_uint(Buf, EndBuf);
|
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
|
2003-10-18 05:54:18 +00:00
|
|
|
hasExtendedLinkageSpecs = true;
|
|
|
|
hasOldStyleVarargs = false;
|
|
|
|
hasVarArgCallPadding = false;
|
2004-01-15 17:55:01 +00:00
|
|
|
hasInconsistentModuleGlobalInfo = false;
|
2004-01-17 23:25:43 +00:00
|
|
|
hasExplicitPrimitiveZeros = false;
|
2003-03-06 17:55:45 +00:00
|
|
|
|
|
|
|
switch (RevisionNum) {
|
2004-01-14 16:44:44 +00:00
|
|
|
case 2: // LLVM pre-1.0 release: will be deleted on the next rev
|
2004-01-14 23:35:21 +00:00
|
|
|
// Version #2 only supported 4 linkage types. It didn't support weak
|
|
|
|
// linkage.
|
2003-10-18 05:54:18 +00:00
|
|
|
hasExtendedLinkageSpecs = false;
|
|
|
|
hasOldStyleVarargs = true;
|
|
|
|
hasVarArgCallPadding = true;
|
2004-01-17 23:25:43 +00:00
|
|
|
// FALL THROUGH
|
2004-01-14 16:44:44 +00:00
|
|
|
case 0: // LLVM 1.0, 1.1 release version
|
2003-10-18 05:54:18 +00:00
|
|
|
// Compared to rev #2, we added support for weak linkage, a more dense
|
|
|
|
// encoding, and better varargs support.
|
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;
|
|
|
|
// 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-01-14 16:44:44 +00:00
|
|
|
break;
|
|
|
|
|
2003-03-06 17:55:45 +00:00
|
|
|
default:
|
2003-09-22 23:38:23 +00:00
|
|
|
throw std::string("Unknown bytecode version number!");
|
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
|
|
|
|
2003-08-24 13:47:36 +00:00
|
|
|
TheModule->setEndianness(Endianness);
|
|
|
|
TheModule->setPointerSize(PointerSize);
|
2003-03-06 17:55:45 +00:00
|
|
|
BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
|
2003-08-24 13:47:36 +00:00
|
|
|
BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
|
|
|
|
<< PointerSize << "\n");
|
2003-03-06 17:55:45 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
void BytecodeParser::ParseModule(const unsigned char *Buf,
|
2003-05-22 18:08:30 +00:00
|
|
|
const unsigned char *EndBuf) {
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned Type, Size;
|
2003-09-22 23:38:23 +00:00
|
|
|
readBlock(Buf, EndBuf, Type, Size);
|
|
|
|
if (Type != BytecodeFormat::Module || Buf+Size != EndBuf)
|
|
|
|
throw std::string("Expected Module packet! B: "+
|
|
|
|
utostr((unsigned)(intptr_t)Buf) + ", S: "+utostr(Size)+
|
|
|
|
" E: "+utostr((unsigned)(intptr_t)EndBuf)); // Hrm, not a class?
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
|
2002-08-17 22:01:27 +00:00
|
|
|
FunctionSignatureList.clear(); // Just in case...
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Read into instance variables...
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseVersionInfo(Buf, EndBuf);
|
2004-01-15 06:13:09 +00:00
|
|
|
align32(Buf, EndBuf);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
2003-03-06 17:03:28 +00:00
|
|
|
const unsigned char *OldBuf = Buf;
|
2003-09-22 23:38:23 +00:00
|
|
|
readBlock(Buf, EndBuf, Type, Size);
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Type) {
|
2003-03-19 20:54:26 +00:00
|
|
|
case BytecodeFormat::GlobalTypePlane:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseGlobalTypes(Buf, Buf+Size);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
case BytecodeFormat::ModuleGlobalInfo:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseModuleGlobalInfo(Buf, Buf+Size);
|
2003-03-19 20:54:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BytecodeFormat::ConstantPool:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
|
2002-03-29 03:51:11 +00:00
|
|
|
case BytecodeFormat::Function: {
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseFunction(Buf, Buf+Size);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BytecodeFormat::SymbolTable:
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
|
2003-10-08 22:52:54 +00:00
|
|
|
ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable(), 0);
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Buf += Size;
|
2003-09-22 23:38:23 +00:00
|
|
|
if (OldBuf > Buf) throw std::string("Expected Module Block!");
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(1, "} end block\n");
|
2004-01-15 06:13:09 +00:00
|
|
|
align32(Buf, EndBuf);
|
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!
|
|
|
|
unsigned TypeSlot = getTypeSlot(GV->getType()->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())
|
|
|
|
throw std::string("Global *already* has an initializer?!");
|
2004-01-15 18:45:25 +00:00
|
|
|
GV->setInitializer(CV);
|
2003-03-19 20:54:26 +00:00
|
|
|
} else
|
2003-09-22 23:38:23 +00:00
|
|
|
throw std::string("Cannot find initializer value.");
|
2003-03-19 20:54:26 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
if (!FunctionSignatureList.empty())
|
|
|
|
throw std::string("Function expected, but bytecode stream ended!");
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
BCR_TRACE(0, "} end block\n\n");
|
2003-03-06 17:15:19 +00:00
|
|
|
}
|
|
|
|
|
2003-10-18 05:54:18 +00:00
|
|
|
void BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
|
|
|
|
const std::string &ModuleID) {
|
2003-09-23 16:15:29 +00:00
|
|
|
|
2003-09-22 23:38:23 +00:00
|
|
|
unsigned char *EndBuf = (unsigned char*)(Buf + Length);
|
2003-09-23 16:15:29 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// Read and check signature...
|
2004-01-15 06:13:09 +00:00
|
|
|
unsigned Sig = read(Buf, EndBuf);
|
|
|
|
if (Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
|
2003-09-22 23:38:23 +00:00
|
|
|
throw std::string("Invalid bytecode signature!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2003-04-22 18:02:52 +00:00
|
|
|
TheModule = new Module(ModuleID);
|
2003-09-22 23:38:23 +00:00
|
|
|
try {
|
2003-10-18 05:54:18 +00:00
|
|
|
usesOldStyleVarargs = false;
|
2003-09-22 23:38:23 +00:00
|
|
|
ParseModule(Buf, EndBuf);
|
|
|
|
} catch (std::string &Error) {
|
2003-05-22 18:26:48 +00:00
|
|
|
freeState(); // Must destroy handles before deleting module!
|
2003-03-06 17:15:19 +00:00
|
|
|
delete TheModule;
|
|
|
|
TheModule = 0;
|
2003-09-26 14:44:52 +00:00
|
|
|
throw;
|
2003-03-06 17:15:19 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|