2001-10-24 03:15:12 +02:00
|
|
|
//===- Reader.cpp - Code to read bytecode files ---------------------------===//
|
2001-06-06 22:29:01 +02: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!!
|
|
|
|
//
|
2002-08-18 00:01:27 +02:00
|
|
|
// TODO: Return error messages to caller instead of printing them out directly.
|
2001-06-06 22:29:01 +02:00
|
|
|
// TODO: Allow passing in an option to ignore the symbol table
|
|
|
|
//
|
2001-10-24 03:15:12 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2001-12-03 19:02:31 +01:00
|
|
|
#include "ReaderInternals.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/Bytecode/Reader.h"
|
|
|
|
#include "llvm/Bytecode/Format.h"
|
2002-04-28 21:55:58 +02:00
|
|
|
#include "llvm/Constants.h"
|
2001-12-03 19:02:31 +01:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include "llvm/iOther.h"
|
2003-09-23 01:38:23 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "Support/StringExtras.h"
|
2003-06-30 23:59:07 +02:00
|
|
|
#include "Config/unistd.h"
|
2003-09-23 01:38:23 +02:00
|
|
|
#include "Config/sys/mman.h"
|
|
|
|
#include "Config/sys/stat.h"
|
|
|
|
#include "Config/sys/types.h"
|
2001-06-06 22:29:01 +02:00
|
|
|
#include <algorithm>
|
2003-09-23 01:38:23 +02:00
|
|
|
#include <memory>
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
#define CHECK_ALIGN32(begin,end) \
|
|
|
|
if (align32(begin,end)) \
|
|
|
|
throw std::string("Alignment error: Reader.cpp:" + \
|
|
|
|
utostr((unsigned)__LINE__));
|
|
|
|
|
|
|
|
void
|
|
|
|
BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
|
2001-06-06 22:29:01 +02:00
|
|
|
if (Ty->isPrimitiveType()) {
|
|
|
|
Slot = Ty->getPrimitiveID();
|
|
|
|
} else {
|
2003-03-06 18:15:19 +01:00
|
|
|
// Check the function level types first...
|
2003-03-06 18:18:14 +01:00
|
|
|
TypeValuesListTy::iterator I = find(FunctionTypeValues.begin(),
|
2003-09-23 01:38:23 +02:00
|
|
|
FunctionTypeValues.end(), Ty);
|
2003-03-06 18:18:14 +01:00
|
|
|
if (I != FunctionTypeValues.end()) {
|
2003-09-23 01:38:23 +02:00
|
|
|
Slot = FirstDerivedTyID + ModuleTypeValues.size() +
|
|
|
|
(&*I - &FunctionTypeValues[0]);
|
2001-09-07 18:37:43 +02:00
|
|
|
} else {
|
|
|
|
I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
|
2003-09-23 01:38:23 +02:00
|
|
|
if (I == ModuleTypeValues.end())
|
|
|
|
throw std::string("Didn't find type in ModuleTypeValues.");
|
2001-09-07 18:37:43 +02:00
|
|
|
Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2002-01-20 23:54:45 +01:00
|
|
|
//cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Type *BytecodeParser::getType(unsigned ID) {
|
2002-10-23 02:51:54 +02:00
|
|
|
if (ID < Type::NumPrimitiveIDs) {
|
|
|
|
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
|
|
|
|
if (T) return T;
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
//cerr << "Looking up Type ID: " << ID << "\n";
|
2002-10-23 02:51:54 +02:00
|
|
|
const Value *V = getValue(Type::TypeTy, ID, false);
|
|
|
|
return cast_or_null<Type>(V);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
int BytecodeParser::insertValue(Value *Val, ValueTable &ValueTab) {
|
|
|
|
assert((!HasImplicitZeroInitializer || !isa<Constant>(Val) ||
|
|
|
|
Val->getType()->isPrimitiveType() ||
|
|
|
|
!cast<Constant>(Val)->isNullValue()) &&
|
|
|
|
"Cannot read null values from bytecode!");
|
2001-06-06 22:29:01 +02:00
|
|
|
unsigned type;
|
2003-09-23 01:38:23 +02:00
|
|
|
getTypeSlot(Val->getType(), type);
|
2001-09-07 18:37:43 +02:00
|
|
|
assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
if (ValueTab.size() <= type) {
|
|
|
|
unsigned OldSize = ValueTab.size();
|
|
|
|
ValueTab.resize(type+1);
|
|
|
|
while (OldSize != type+1)
|
|
|
|
ValueTab[OldSize++] = new ValueList();
|
2003-03-06 18:55:45 +01:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
//cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
|
2003-09-23 01:38:23 +02:00
|
|
|
// << "] = " << Val << "\n";
|
2003-03-19 21:54:26 +01:00
|
|
|
ValueTab[type]->push_back(Val);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
bool HasOffset = HasImplicitZeroInitializer &&
|
2003-09-23 01:38:23 +02:00
|
|
|
!Val->getType()->isPrimitiveType();
|
2003-03-19 21:54:26 +01:00
|
|
|
|
|
|
|
return ValueTab[type]->size()-1 + HasOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BytecodeParser::setValueTo(ValueTable &ValueTab, unsigned Slot,
|
|
|
|
Value *Val) {
|
|
|
|
assert(&ValueTab == &ModuleValues && "Can only setValueTo on Module values!");
|
|
|
|
unsigned type;
|
2003-09-23 01:38:23 +02:00
|
|
|
getTypeSlot(Val->getType(), type);
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
assert((!HasImplicitZeroInitializer || Slot != 0) &&
|
|
|
|
"Cannot change zero init");
|
|
|
|
assert(type < ValueTab.size() && Slot <= ValueTab[type]->size());
|
|
|
|
ValueTab[type]->setOperand(Slot-HasImplicitZeroInitializer, Val);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
|
|
|
|
unsigned Num = oNum;
|
|
|
|
unsigned type; // The type plane it lives in...
|
2003-09-23 01:38:23 +02:00
|
|
|
getTypeSlot(Ty, type);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (type == Type::TypeTyID) { // The 'type' plane has implicit values
|
2001-09-07 18:37:43 +02:00
|
|
|
assert(Create == false);
|
2002-10-23 02:51:54 +02:00
|
|
|
if (Num < Type::NumPrimitiveIDs) {
|
|
|
|
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
|
|
|
|
if (T) return (Value*)T; // Asked for a primitive type...
|
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Otherwise, derived types need offset...
|
|
|
|
Num -= FirstDerivedTyID;
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
// Is it a module-level type?
|
2001-09-07 18:37:43 +02:00
|
|
|
if (Num < ModuleTypeValues.size())
|
2001-10-13 08:47:01 +02:00
|
|
|
return (Value*)ModuleTypeValues[Num].get();
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
// Nope, is it a function-level type?
|
2001-09-07 18:37:43 +02:00
|
|
|
Num -= ModuleTypeValues.size();
|
2003-03-06 18:18:14 +01:00
|
|
|
if (Num < FunctionTypeValues.size())
|
|
|
|
return (Value*)FunctionTypeValues[Num].get();
|
2001-09-07 18:37:43 +02:00
|
|
|
|
|
|
|
return 0;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
if (HasImplicitZeroInitializer && type >= FirstDerivedTyID) {
|
|
|
|
if (Num == 0)
|
|
|
|
return Constant::getNullValue(Ty);
|
|
|
|
--Num;
|
|
|
|
}
|
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
if (type < ModuleValues.size()) {
|
2003-03-19 21:54:26 +01:00
|
|
|
if (Num < ModuleValues[type]->size())
|
|
|
|
return ModuleValues[type]->getOperand(Num);
|
|
|
|
Num -= ModuleValues[type]->size();
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
if (Values.size() > type && Values[type]->size() > Num)
|
|
|
|
return Values[type]->getOperand(Num);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
if (!Create) return 0; // Do not create a placeholder?
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
Value *d = 0;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
2002-08-18 00:01:27 +02:00
|
|
|
case Type::LabelTyID:
|
|
|
|
d = new BBPHolder(Ty, oNum);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
d = new ValPHolder(Ty, oNum);
|
|
|
|
break;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(d != 0 && "How did we not make something?");
|
2002-08-18 00:01:27 +02:00
|
|
|
if (insertValue(d, LateResolveValues) == -1) return 0;
|
2001-06-06 22:29:01 +02:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2002-10-14 05:33:02 +02: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.
|
|
|
|
///
|
|
|
|
Constant *BytecodeParser::getConstantValue(const Type *Ty, unsigned Slot) {
|
|
|
|
if (Value *V = getValue(Ty, Slot, false))
|
|
|
|
return dyn_cast<Constant>(V); // If we already have the value parsed...
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
std::pair<const Type*, unsigned> Key(Ty, Slot);
|
|
|
|
GlobalRefsType::iterator I = GlobalRefs.lower_bound(Key);
|
|
|
|
|
|
|
|
if (I != GlobalRefs.end() && I->first == Key) {
|
2002-10-14 05:33:02 +02:00
|
|
|
BCR_TRACE(5, "Previous forward ref found!\n");
|
|
|
|
return cast<Constant>(I->second);
|
|
|
|
} 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-03-19 21:54:26 +01:00
|
|
|
GlobalRefs.insert(I, std::make_pair(Key, C));
|
2002-10-14 05:33:02 +02:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
void BytecodeParser::postResolveValues(ValueTable &ValTab) {
|
2003-03-19 21:54:26 +01:00
|
|
|
while (!ValTab.empty()) {
|
|
|
|
ValueList &DL = *ValTab.back();
|
|
|
|
ValTab.pop_back();
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
while (!DL.empty()) {
|
|
|
|
Value *D = DL.back();
|
|
|
|
unsigned IDNumber = getValueIDNumberFromPlaceHolder(D);
|
2001-06-06 22:29:01 +02:00
|
|
|
DL.pop_back();
|
|
|
|
|
|
|
|
Value *NewDef = getValue(D->getType(), IDNumber, false);
|
|
|
|
if (NewDef == 0) {
|
2003-09-23 01:38:23 +02:00
|
|
|
throw std::string("Unresolvable reference found: <" +
|
|
|
|
D->getType()->getName() + ">:" +utostr(IDNumber)+".");
|
2001-06-06 22:29:01 +02:00
|
|
|
} else {
|
2003-09-23 01:38:23 +02:00
|
|
|
// Fixup all of the uses of this placeholder def...
|
2001-06-06 22:29:01 +02:00
|
|
|
D->replaceAllUsesWith(NewDef);
|
|
|
|
|
|
|
|
// Now that all the uses are gone, delete the placeholder...
|
|
|
|
// If we couldn't find a def (error case), then leak a little
|
2003-09-23 01:38:23 +02:00
|
|
|
delete D; // memory, 'cause otherwise we can't remove all uses!
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
2003-03-19 21:54:26 +01:00
|
|
|
delete &DL;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
std::auto_ptr<BasicBlock>
|
|
|
|
BytecodeParser::ParseBasicBlock(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
|
|
|
std::auto_ptr<BasicBlock> BB(new BasicBlock());
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
2001-09-07 18:37:43 +02:00
|
|
|
Instruction *Inst;
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseInstruction(Buf, EndBuf, Inst);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Inst == 0) { throw std::string("Could not parse Instruction."); }
|
|
|
|
if (insertValue(Inst, Values) == -1) {
|
|
|
|
throw std::string("Could not insert value.");
|
|
|
|
}
|
2001-09-07 18:37:43 +02:00
|
|
|
|
|
|
|
BB->getInstList().push_back(Inst);
|
|
|
|
BCR_TRACE(4, Inst);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
return BB;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
void BytecodeParser::ParseSymbolTable(const unsigned char *&Buf,
|
2003-05-22 20:08:30 +02:00
|
|
|
const unsigned char *EndBuf,
|
2003-09-23 01:38:23 +02:00
|
|
|
SymbolTable *ST) {
|
2001-06-06 22:29:01 +02:00
|
|
|
while (Buf < EndBuf) {
|
|
|
|
// Symtab block header: [num entries][type id number]
|
|
|
|
unsigned NumEntries, Typ;
|
|
|
|
if (read_vbr(Buf, EndBuf, NumEntries) ||
|
2003-09-23 01:38:23 +02:00
|
|
|
read_vbr(Buf, EndBuf, Typ)) throw Error_readvbr;
|
2001-06-06 22:29:01 +02:00
|
|
|
const Type *Ty = getType(Typ);
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Ty == 0) throw std::string("Invalid type read in symbol table.");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
|
2003-09-23 01:38:23 +02:00
|
|
|
" entries\n");
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2001-06-28 01:41:11 +02:00
|
|
|
for (unsigned i = 0; i < NumEntries; ++i) {
|
2001-06-06 22:29:01 +02:00
|
|
|
// Symtab entry: [def slot #][name]
|
|
|
|
unsigned slot;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, EndBuf, slot)) throw Error_readvbr;
|
2002-01-20 23:54:45 +01:00
|
|
|
std::string Name;
|
2001-06-06 22:29:01 +02:00
|
|
|
if (read(Buf, EndBuf, Name, false)) // Not aligned...
|
2003-09-23 01:38:23 +02:00
|
|
|
throw std::string("Buffer not aligned.");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
Value *V = getValue(Ty, slot, false); // Find mapping...
|
|
|
|
if (V == 0) {
|
2003-09-23 01:38:23 +02:00
|
|
|
BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
|
|
|
|
throw std::string("Failed value look-up.");
|
2001-09-07 18:37:43 +02:00
|
|
|
}
|
2003-03-19 21:54:26 +01:00
|
|
|
BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << *V;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (!isa<Instruction>(V)) std::cerr << "\n");
|
2001-09-07 18:37:43 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
V->setName(Name, ST);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Buf > EndBuf) throw std::string("Tried to read past end of buffer.");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
void BytecodeParser::ResolveReferencesToValue(Value *NewV, unsigned Slot) {
|
2003-03-06 17:32:25 +01:00
|
|
|
GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(NewV->getType(),
|
|
|
|
Slot));
|
2002-08-18 00:01:27 +02:00
|
|
|
if (I == GlobalRefs.end()) return; // Never forward referenced?
|
2002-07-15 01:04:18 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
BCR_TRACE(3, "Mutating forward refs!\n");
|
|
|
|
Value *VPH = I->second; // Get the placeholder...
|
2002-07-15 01:04:18 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
VPH->replaceAllUsesWith(NewV);
|
|
|
|
|
|
|
|
// If this is a global variable being resolved, remove the placeholder from
|
|
|
|
// the module...
|
|
|
|
if (GlobalValue* GVal = dyn_cast<GlobalValue>(NewV))
|
|
|
|
GVal->getParent()->getGlobalList().remove(cast<GlobalVariable>(VPH));
|
2002-07-15 01:04:18 +02:00
|
|
|
|
2002-08-18 00:01:27 +02:00
|
|
|
delete VPH; // Delete the old placeholder
|
|
|
|
GlobalRefs.erase(I); // Remove the map entry for it
|
2002-07-15 01:04:18 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
void
|
|
|
|
BytecodeParser::ParseFunction(const unsigned char *&Buf,
|
|
|
|
const unsigned char *EndBuf) {
|
|
|
|
if (FunctionSignatureList.empty())
|
|
|
|
throw std::string("FunctionSignatureList empty!");
|
2003-03-19 21:54:26 +01:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
Function *F = FunctionSignatureList.back().first;
|
|
|
|
unsigned FunctionSlot = FunctionSignatureList.back().second;
|
|
|
|
FunctionSignatureList.pop_back();
|
|
|
|
|
|
|
|
// Save the information for future reading of the function
|
|
|
|
LazyFunctionInfo *LFI = new LazyFunctionInfo();
|
|
|
|
LFI->Buf = Buf; LFI->EndBuf = EndBuf; LFI->FunctionSlot = FunctionSlot;
|
|
|
|
LazyFunctionLoadMap[F] = LFI;
|
|
|
|
// 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.
|
|
|
|
std::map<Function*, LazyFunctionInfo*>::iterator Fi =
|
|
|
|
LazyFunctionLoadMap.find(F);
|
|
|
|
if (Fi == LazyFunctionLoadMap.end()) return;
|
|
|
|
|
|
|
|
LazyFunctionInfo *LFI = Fi->second;
|
|
|
|
const unsigned char *Buf = LFI->Buf;
|
|
|
|
const unsigned char *EndBuf = LFI->EndBuf;
|
|
|
|
unsigned FunctionSlot = LFI->FunctionSlot;
|
|
|
|
LazyFunctionLoadMap.erase(Fi);
|
|
|
|
delete LFI;
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-04-16 23:16:05 +02:00
|
|
|
GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
|
|
|
|
|
|
|
|
if (!hasInternalMarkerOnly) {
|
|
|
|
unsigned LinkageType;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, EndBuf, LinkageType))
|
|
|
|
throw std::string("ParseFunction: Error reading from buffer.");
|
|
|
|
if (LinkageType & ~0x3)
|
|
|
|
throw std::string("Invalid linkage type for Function.");
|
2003-04-16 23:16:05 +02:00
|
|
|
Linkage = (GlobalValue::LinkageTypes)LinkageType;
|
|
|
|
} else {
|
|
|
|
// We used to only support two linkage models: internal and external
|
|
|
|
unsigned isInternal;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, EndBuf, isInternal))
|
|
|
|
throw std::string("ParseFunction: Error reading from buffer.");
|
2003-04-16 23:16:05 +02:00
|
|
|
if (isInternal) Linkage = GlobalValue::InternalLinkage;
|
|
|
|
}
|
2001-11-26 19:56:10 +01:00
|
|
|
|
2003-04-16 23:16:05 +02:00
|
|
|
F->setLinkage(Linkage);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
const FunctionType::ParamTypes &Params =F->getFunctionType()->getParamTypes();
|
|
|
|
Function::aiterator AI = F->abegin();
|
2002-03-29 04:51:11 +01:00
|
|
|
for (FunctionType::ParamTypes::const_iterator It = Params.begin();
|
2002-10-13 22:57:00 +02:00
|
|
|
It != Params.end(); ++It, ++AI) {
|
2003-09-23 01:38:23 +02:00
|
|
|
if (insertValue(AI, Values) == -1)
|
|
|
|
throw std::string("Error reading function arguments!");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
|
|
|
unsigned Type, Size;
|
2003-03-06 18:03:28 +01:00
|
|
|
const unsigned char *OldBuf = Buf;
|
2003-09-23 01:38:23 +02:00
|
|
|
readBlock(Buf, EndBuf, Type, Size);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
switch (Type) {
|
2003-09-23 01:38:23 +02:00
|
|
|
case BytecodeFormat::ConstantPool: {
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseConstantPool(Buf, Buf+Size, Values, FunctionTypeValues);
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
case BytecodeFormat::BasicBlock: {
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
std::auto_ptr<BasicBlock> BB = ParseBasicBlock(Buf, Buf+Size);
|
|
|
|
if (!BB.get() || insertValue(BB.get(), Values) == -1)
|
|
|
|
throw std::string("Parse error: BasicBlock");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
F->getBasicBlockList().push_back(BB.release());
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
case BytecodeFormat::SymbolTable: {
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseSymbolTable(Buf, Buf+Size, &F->getSymbolTable());
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
2003-09-23 01:38:23 +02:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
default:
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(2, "BLOCK <unknown>:ignored! {\n");
|
2001-06-06 22:29:01 +02:00
|
|
|
Buf += Size;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (OldBuf > Buf)
|
|
|
|
throw std::string("Wrapped around reading bytecode.");
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(2, "} end block\n");
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
// Malformed bc file if read past end of block.
|
|
|
|
CHECK_ALIGN32(Buf, EndBuf);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
// Check for unresolvable references
|
|
|
|
postResolveValues(LateResolveValues);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
//ResolveReferencesToValue(F, FunctionSlot);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
// Clear out function-level types...
|
2003-03-06 18:18:14 +01:00
|
|
|
FunctionTypeValues.clear();
|
2001-09-15 00:03:42 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
freeTable(Values);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02: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 22:29:01 +02:00
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
// Read global variables...
|
|
|
|
unsigned VarType;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
|
2001-09-10 09:58:01 +02:00
|
|
|
while (VarType != Type::VoidTyID) { // List is terminated by Void
|
2003-04-16 23:16:05 +02:00
|
|
|
unsigned SlotNo;
|
|
|
|
GlobalValue::LinkageTypes Linkage;
|
|
|
|
|
|
|
|
if (!hasInternalMarkerOnly) {
|
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
|
|
|
|
// bit2,3 = Linkage, bit4+ = slot#
|
|
|
|
SlotNo = VarType >> 4;
|
|
|
|
Linkage = (GlobalValue::LinkageTypes)((VarType >> 2) & 3);
|
|
|
|
} else {
|
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
|
|
|
|
// bit2 = isInternal, bit3+ = slot#
|
|
|
|
SlotNo = VarType >> 3;
|
|
|
|
Linkage = (VarType & 4) ? GlobalValue::InternalLinkage :
|
|
|
|
GlobalValue::ExternalLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Type *Ty = getType(SlotNo);
|
2003-09-23 01:38:23 +02:00
|
|
|
if (!Ty || !isa<PointerType>(Ty))
|
|
|
|
throw std::string("Global not pointer type! Ty = " +
|
|
|
|
Ty->getDescription());
|
2001-09-10 09:58:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
const Type *ElTy = cast<PointerType>(Ty)->getElementType();
|
2001-09-18 06:01:05 +02:00
|
|
|
|
2001-09-10 09:58:01 +02:00
|
|
|
// Create the global variable...
|
2003-04-16 22:28:45 +02:00
|
|
|
GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, Linkage,
|
2003-03-19 21:54:26 +01:00
|
|
|
0, "", TheModule);
|
2001-10-13 08:47:01 +02:00
|
|
|
int DestSlot = insertValue(GV, ModuleValues);
|
2003-09-23 01:38:23 +02:00
|
|
|
if (DestSlot == -1) throw Error_DestSlot;
|
2003-03-19 21:54:26 +01:00
|
|
|
BCR_TRACE(2, "Global Variable of type: " << *Ty << "\n");
|
2002-08-18 00:01:27 +02:00
|
|
|
ResolveReferencesToValue(GV, (unsigned)DestSlot);
|
2001-10-13 08:47:01 +02:00
|
|
|
|
2003-09-12 00:34:13 +02:00
|
|
|
if (VarType & 2) { // Does it have an initializer?
|
2003-03-19 21:54:26 +01:00
|
|
|
unsigned InitSlot;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, End, InitSlot)) throw Error_readvbr;
|
2003-03-19 21:54:26 +01:00
|
|
|
GlobalInits.push_back(std::make_pair(GV, InitSlot));
|
|
|
|
}
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, End, VarType)) throw Error_readvbr;
|
2001-09-10 09:58:01 +02:00
|
|
|
}
|
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
// Read the function objects for all of the functions that are coming
|
2002-08-18 00:01:27 +02:00
|
|
|
unsigned FnSignature;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
|
2002-08-18 00:01:27 +02:00
|
|
|
while (FnSignature != Type::VoidTyID) { // List is terminated by Void
|
|
|
|
const Type *Ty = getType(FnSignature);
|
2001-10-03 16:53:21 +02:00
|
|
|
if (!Ty || !isa<PointerType>(Ty) ||
|
2002-03-29 04:51:11 +01:00
|
|
|
!isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
|
2003-09-23 01:38:23 +02:00
|
|
|
throw std::string("Function not ptr to func type! Ty = " +
|
|
|
|
Ty->getDescription());
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2002-10-23 02:51:54 +02:00
|
|
|
|
2003-03-06 18:15:19 +01:00
|
|
|
// We create functions by passing the underlying FunctionType to create...
|
2001-12-04 01:03:30 +01:00
|
|
|
Ty = cast<PointerType>(Ty)->getElementType();
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-06 18:15:19 +01: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 22:29:01 +02:00
|
|
|
|
|
|
|
// Insert the placeholder...
|
2003-04-16 22:28:45 +02:00
|
|
|
Function *Func = new Function(cast<FunctionType>(Ty),
|
|
|
|
GlobalValue::InternalLinkage, "", TheModule);
|
2003-03-19 21:54:26 +01:00
|
|
|
int DestSlot = insertValue(Func, ModuleValues);
|
2003-09-23 01:38:23 +02:00
|
|
|
if (DestSlot == -1) throw Error_DestSlot;
|
2003-03-19 21:54:26 +01:00
|
|
|
ResolveReferencesToValue(Func, (unsigned)DestSlot);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-03-19 21:54:26 +01:00
|
|
|
// Keep track of this information in a list that is emptied as functions are
|
|
|
|
// loaded...
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2003-03-19 21:54:26 +01:00
|
|
|
FunctionSignatureList.push_back(std::make_pair(Func, DestSlot));
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, End, FnSignature)) throw Error_readvbr;
|
2002-03-29 04:51:11 +01:00
|
|
|
BCR_TRACE(2, "Function of type: " << Ty << "\n");
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
CHECK_ALIGN32(Buf, End);
|
2002-08-18 00:01:27 +02: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 22:29:01 +02: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-23 01:38:23 +02:00
|
|
|
void BytecodeParser::ParseVersionInfo(const unsigned char *&Buf,
|
2003-05-22 20:08:30 +02:00
|
|
|
const unsigned char *EndBuf) {
|
2003-03-06 18:55:45 +01:00
|
|
|
unsigned Version;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (read_vbr(Buf, EndBuf, Version)) throw Error_readvbr;
|
2003-03-06 18:55:45 +01:00
|
|
|
|
|
|
|
// Unpack version number: low four bits are for flags, top bits = version
|
2003-08-24 15:47:36 +02: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 23:16:05 +02:00
|
|
|
|
|
|
|
// Default values for the current bytecode version
|
2003-03-06 18:55:45 +01:00
|
|
|
HasImplicitZeroInitializer = true;
|
2003-04-16 23:16:05 +02:00
|
|
|
hasInternalMarkerOnly = false;
|
|
|
|
FirstDerivedTyID = 14;
|
2003-03-06 18:55:45 +01:00
|
|
|
|
|
|
|
switch (RevisionNum) {
|
|
|
|
case 0: // Initial revision
|
2003-03-19 21:54:26 +01:00
|
|
|
// Version #0 didn't have any of the flags stored correctly, and in fact as
|
|
|
|
// only valid with a 14 in the flags values. Also, it does not support
|
|
|
|
// encoding zero initializers for arrays compactly.
|
|
|
|
//
|
2003-09-23 01:38:23 +02:00
|
|
|
if (Version != 14) throw std::string("Unknown revision 0 flags?");
|
2003-03-06 18:55:45 +01:00
|
|
|
HasImplicitZeroInitializer = false;
|
2003-08-24 15:47:36 +02:00
|
|
|
Endianness = Module::BigEndian;
|
|
|
|
PointerSize = Module::Pointer64;
|
2003-04-16 23:16:05 +02:00
|
|
|
hasInternalMarkerOnly = true;
|
2003-08-24 15:47:36 +02:00
|
|
|
hasNoEndianness = hasNoPointerSize = false;
|
2003-03-06 18:55:45 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2003-08-24 15:47:36 +02:00
|
|
|
// Version #1 has four bit fields: isBigEndian, hasLongPointers,
|
|
|
|
// hasNoEndianness, and hasNoPointerSize.
|
2003-04-16 23:16:05 +02:00
|
|
|
hasInternalMarkerOnly = true;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// Version #2 added information about all 4 linkage types instead of just
|
|
|
|
// having internal and external.
|
2003-03-06 18:55:45 +01:00
|
|
|
break;
|
|
|
|
default:
|
2003-09-23 01:38:23 +02:00
|
|
|
throw std::string("Unknown bytecode version number!");
|
2003-03-06 18:55:45 +01:00
|
|
|
}
|
|
|
|
|
2003-08-24 15:47:36 +02:00
|
|
|
if (hasNoEndianness) Endianness = Module::AnyEndianness;
|
|
|
|
if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
|
2003-04-22 20:15:10 +02:00
|
|
|
|
2003-08-24 15:47:36 +02:00
|
|
|
TheModule->setEndianness(Endianness);
|
|
|
|
TheModule->setPointerSize(PointerSize);
|
2003-03-06 18:55:45 +01:00
|
|
|
BCR_TRACE(1, "Bytecode Rev = " << (unsigned)RevisionNum << "\n");
|
2003-08-24 15:47:36 +02:00
|
|
|
BCR_TRACE(1, "Endianness/PointerSize = " << Endianness << ","
|
|
|
|
<< PointerSize << "\n");
|
2003-03-06 18:55:45 +01:00
|
|
|
BCR_TRACE(1, "HasImplicitZeroInit = " << HasImplicitZeroInitializer << "\n");
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
void BytecodeParser::ParseModule(const unsigned char *Buf,
|
2003-05-22 20:08:30 +02:00
|
|
|
const unsigned char *EndBuf) {
|
2001-06-06 22:29:01 +02:00
|
|
|
unsigned Type, Size;
|
2003-09-23 01:38:23 +02: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 22:29:01 +02:00
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
|
2002-08-18 00:01:27 +02:00
|
|
|
FunctionSignatureList.clear(); // Just in case...
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
// Read into instance variables...
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseVersionInfo(Buf, EndBuf);
|
|
|
|
CHECK_ALIGN32(Buf, EndBuf);
|
2001-06-06 22:29:01 +02:00
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
2003-03-06 18:03:28 +01:00
|
|
|
const unsigned char *OldBuf = Buf;
|
2003-09-23 01:38:23 +02:00
|
|
|
readBlock(Buf, EndBuf, Type, Size);
|
2001-06-06 22:29:01 +02:00
|
|
|
switch (Type) {
|
2003-03-19 21:54:26 +01:00
|
|
|
case BytecodeFormat::GlobalTypePlane:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::GlobalTypePlane: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseGlobalTypes(Buf, Buf+Size);
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
|
2001-09-07 18:37:43 +02:00
|
|
|
case BytecodeFormat::ModuleGlobalInfo:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::ModuleGlobalInfo: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseModuleGlobalInfo(Buf, Buf+Size);
|
2003-03-19 21:54:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BytecodeFormat::ConstantPool:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues);
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
|
2002-03-29 04:51:11 +01:00
|
|
|
case BytecodeFormat::Function: {
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::Function: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseFunction(Buf, Buf+Size);
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BytecodeFormat::SymbolTable:
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::SymbolTable: {\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
ParseSymbolTable(Buf, Buf+Size, &TheModule->getSymbolTable());
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Buf += Size;
|
2003-09-23 01:38:23 +02:00
|
|
|
if (OldBuf > Buf) throw std::string("Expected Module Block!");
|
2001-06-06 22:29:01 +02:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 18:37:43 +02:00
|
|
|
BCR_TRACE(1, "} end block\n");
|
2003-09-23 01:38:23 +02:00
|
|
|
CHECK_ALIGN32(Buf, EndBuf);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2003-03-19 21:54:26 +01: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...
|
|
|
|
if (Value *V = getValue(GV->getType()->getElementType(), Slot, false)) {
|
2003-09-23 01:38:23 +02:00
|
|
|
if (GV->hasInitializer())
|
|
|
|
throw std::string("Global *already* has an initializer?!");
|
2003-03-19 21:54:26 +01:00
|
|
|
GV->setInitializer(cast<Constant>(V));
|
|
|
|
} else
|
2003-09-23 01:38:23 +02:00
|
|
|
throw std::string("Cannot find initializer value.");
|
2003-03-19 21:54:26 +01:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
if (!FunctionSignatureList.empty())
|
|
|
|
throw std::string("Function expected, but bytecode stream ended!");
|
2001-09-07 18:37:43 +02:00
|
|
|
|
|
|
|
BCR_TRACE(0, "} end block\n\n");
|
2003-03-06 18:15:19 +01:00
|
|
|
}
|
|
|
|
|
2003-09-23 01:38:23 +02:00
|
|
|
void
|
|
|
|
BytecodeParser::ParseBytecode(const unsigned char *Buf, unsigned Length,
|
|
|
|
const std::string &ModuleID) {
|
2001-06-06 22:29:01 +02:00
|
|
|
unsigned Sig;
|
2003-09-23 01:38:23 +02:00
|
|
|
unsigned char *EndBuf = (unsigned char*)(Buf + Length);
|
2001-06-06 22:29:01 +02:00
|
|
|
// Read and check signature...
|
|
|
|
if (read(Buf, EndBuf, Sig) ||
|
2003-09-23 01:38:23 +02:00
|
|
|
Sig != ('l' | ('l' << 8) | ('v' << 16) | ('m' << 24)))
|
|
|
|
throw std::string("Invalid bytecode signature!");
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2003-04-22 20:02:52 +02:00
|
|
|
TheModule = new Module(ModuleID);
|
2003-09-23 01:38:23 +02:00
|
|
|
try {
|
|
|
|
ParseModule(Buf, EndBuf);
|
|
|
|
} catch (std::string &Error) {
|
2003-05-22 20:26:48 +02:00
|
|
|
freeState(); // Must destroy handles before deleting module!
|
2003-03-06 18:15:19 +01:00
|
|
|
delete TheModule;
|
|
|
|
TheModule = 0;
|
2003-09-23 01:38:23 +02:00
|
|
|
throw Error;
|
2003-03-06 18:15:19 +01:00
|
|
|
}
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|