2001-10-24 01:15:12 +00:00
|
|
|
//===- Reader.cpp - Code to read bytecode files ---------------------------===//
|
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: Make error message outputs be configurable depending on an option?
|
|
|
|
// 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"
|
2001-09-10 07:58:01 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
2001-12-03 22:26:30 +00:00
|
|
|
#include "llvm/ConstantVals.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/iOther.h"
|
2002-04-09 19:48:49 +00:00
|
|
|
#include "llvm/Argument.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <sys/mman.h>
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <algorithm>
|
2002-01-20 22:54:45 +00:00
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
|
|
|
using std::make_pair;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
bool BytecodeParser::getTypeSlot(const Type *Ty, unsigned &Slot) {
|
|
|
|
if (Ty->isPrimitiveType()) {
|
|
|
|
Slot = Ty->getPrimitiveID();
|
|
|
|
} else {
|
2001-09-07 16:37:43 +00:00
|
|
|
// Check the method level types first...
|
|
|
|
TypeValuesListTy::iterator I = find(MethodTypeValues.begin(),
|
|
|
|
MethodTypeValues.end(), Ty);
|
|
|
|
if (I != MethodTypeValues.end()) {
|
|
|
|
Slot = FirstDerivedTyID+ModuleTypeValues.size()+
|
|
|
|
(&*I - &MethodTypeValues[0]);
|
|
|
|
} else {
|
|
|
|
I = find(ModuleTypeValues.begin(), ModuleTypeValues.end(), Ty);
|
|
|
|
if (I == ModuleTypeValues.end()) return true; // Didn't find type!
|
|
|
|
Slot = FirstDerivedTyID + (&*I - &ModuleTypeValues[0]);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-01-20 22:54:45 +00:00
|
|
|
//cerr << "getTypeSlot '" << Ty->getName() << "' = " << Slot << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Type *BytecodeParser::getType(unsigned ID) {
|
|
|
|
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)ID);
|
|
|
|
if (T) return T;
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
//cerr << "Looking up Type ID: " << ID << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
const Value *D = getValue(Type::TypeTy, ID, false);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (D == 0) return failure<const Type*>(0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-01 18:26:53 +00:00
|
|
|
return cast<Type>(D);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
int BytecodeParser::insertValue(Value *Val, std::vector<ValueList> &ValueTab) {
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned type;
|
2001-10-13 06:47:01 +00:00
|
|
|
if (getTypeSlot(Val->getType(), type)) return failure<int>(-1);
|
2001-09-07 16:37:43 +00:00
|
|
|
assert(type != Type::TypeTyID && "Types should never be insertValue'd!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
if (ValueTab.size() <= type)
|
|
|
|
ValueTab.resize(type+1, ValueList());
|
|
|
|
|
|
|
|
//cerr << "insertValue Values[" << type << "][" << ValueTab[type].size()
|
2002-01-20 22:54:45 +00:00
|
|
|
// << "] = " << Val << "\n";
|
2001-09-07 16:37:43 +00:00
|
|
|
ValueTab[type].push_back(Val);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-13 06:47:01 +00:00
|
|
|
return ValueTab[type].size()-1;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *BytecodeParser::getValue(const Type *Ty, unsigned oNum, bool Create) {
|
|
|
|
unsigned Num = oNum;
|
|
|
|
unsigned type; // The type plane it lives in...
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (getTypeSlot(Ty, type)) return failure<Value*>(0); // TODO: true
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
if (type == Type::TypeTyID) { // The 'type' plane has implicit values
|
2001-09-07 16:37:43 +00:00
|
|
|
assert(Create == false);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *T = Type::getPrimitiveType((Type::PrimitiveID)Num);
|
|
|
|
if (T) return (Value*)T; // Asked for a primitive type...
|
|
|
|
|
|
|
|
// Otherwise, derived types need offset...
|
|
|
|
Num -= FirstDerivedTyID;
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
// Is it a module level type?
|
|
|
|
if (Num < ModuleTypeValues.size())
|
2001-10-13 06:47:01 +00:00
|
|
|
return (Value*)ModuleTypeValues[Num].get();
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
// Nope, is it a method level type?
|
|
|
|
Num -= ModuleTypeValues.size();
|
|
|
|
if (Num < MethodTypeValues.size())
|
2001-10-13 06:47:01 +00:00
|
|
|
return (Value*)MethodTypeValues[Num].get();
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
return 0;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
if (type < ModuleValues.size()) {
|
|
|
|
if (Num < ModuleValues[type].size())
|
2001-06-06 20:29:01 +00:00
|
|
|
return ModuleValues[type][Num];
|
|
|
|
Num -= ModuleValues[type].size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Values.size() > type && Values[type].size() > Num)
|
|
|
|
return Values[type][Num];
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (!Create) return failure<Value*>(0); // Do not create a placeholder?
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
Value *d = 0;
|
|
|
|
switch (Ty->getPrimitiveID()) {
|
|
|
|
case Type::LabelTyID: d = new BBPHolder(Ty, oNum); break;
|
2002-03-29 03:51:11 +00:00
|
|
|
case Type::FunctionTyID:
|
2001-06-06 20:29:01 +00:00
|
|
|
cerr << "Creating method pholder! : " << type << ":" << oNum << " "
|
2002-01-20 22:54:45 +00:00
|
|
|
<< Ty->getName() << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
d = new MethPHolder(Ty, oNum);
|
2001-10-13 06:47:01 +00:00
|
|
|
if (insertValue(d, LateResolveModuleValues) ==-1) return failure<Value*>(0);
|
2001-06-06 20:29:01 +00:00
|
|
|
return d;
|
|
|
|
default: d = new DefPHolder(Ty, oNum); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(d != 0 && "How did we not make something?");
|
2001-10-13 06:47:01 +00:00
|
|
|
if (insertValue(d, LateResolveValues) == -1) return failure<Value*>(0);
|
2001-06-06 20:29:01 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::postResolveValues(ValueTable &ValTab) {
|
|
|
|
bool Error = false;
|
2001-06-27 23:41:11 +00:00
|
|
|
for (unsigned ty = 0; ty < ValTab.size(); ++ty) {
|
2001-06-06 20:29:01 +00:00
|
|
|
ValueList &DL = ValTab[ty];
|
|
|
|
unsigned Size;
|
|
|
|
while ((Size = DL.size())) {
|
|
|
|
unsigned IDNumber = getValueIDNumberFromPlaceHolder(DL[Size-1]);
|
|
|
|
|
|
|
|
Value *D = DL[Size-1];
|
|
|
|
DL.pop_back();
|
|
|
|
|
|
|
|
Value *NewDef = getValue(D->getType(), IDNumber, false);
|
|
|
|
if (NewDef == 0) {
|
|
|
|
Error = true; // Unresolved thinger
|
2001-10-13 06:47:01 +00:00
|
|
|
cerr << "Unresolvable reference found: <"
|
|
|
|
<< D->getType()->getDescription() << ">:" << IDNumber << "!\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
} else {
|
|
|
|
// Fixup all of the uses of this placeholder def...
|
|
|
|
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
|
|
|
|
delete D; // memory, 'cause otherwise we can't remove all uses!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::ParseBasicBlock(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
BasicBlock *&BB) {
|
|
|
|
BB = new BasicBlock();
|
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
2001-09-07 16:37:43 +00:00
|
|
|
Instruction *Inst;
|
|
|
|
if (ParseInstruction(Buf, EndBuf, Inst)) {
|
2001-06-06 20:29:01 +00:00
|
|
|
delete BB;
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
if (Inst == 0) { delete BB; return failure(true); }
|
2001-10-13 06:47:01 +00:00
|
|
|
if (insertValue(Inst, Values) == -1) { delete BB; return failure(true); }
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
BB->getInstList().push_back(Inst);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(4, Inst);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
bool BytecodeParser::ParseSymbolTable(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
SymbolTable *ST) {
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Buf < EndBuf) {
|
|
|
|
// Symtab block header: [num entries][type id number]
|
|
|
|
unsigned NumEntries, Typ;
|
|
|
|
if (read_vbr(Buf, EndBuf, NumEntries) ||
|
2001-07-28 17:50:18 +00:00
|
|
|
read_vbr(Buf, EndBuf, Typ)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
const Type *Ty = getType(Typ);
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Ty == 0) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(3, "Plane Type: '" << Ty << "' with " << NumEntries <<
|
|
|
|
" entries\n");
|
|
|
|
|
2001-06-27 23:41:11 +00:00
|
|
|
for (unsigned i = 0; i < NumEntries; ++i) {
|
2001-06-06 20:29:01 +00:00
|
|
|
// Symtab entry: [def slot #][name]
|
|
|
|
unsigned slot;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, slot)) return failure(true);
|
2002-01-20 22:54:45 +00:00
|
|
|
std::string Name;
|
2001-06-06 20:29:01 +00:00
|
|
|
if (read(Buf, EndBuf, Name, false)) // Not aligned...
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
Value *D = getValue(Ty, slot, false); // Find mapping...
|
2001-09-07 16:37:43 +00:00
|
|
|
if (D == 0) {
|
2002-01-20 22:54:45 +00:00
|
|
|
BCR_TRACE(3, "FAILED LOOKUP: Slot #" << slot << "\n");
|
2001-09-07 16:37:43 +00:00
|
|
|
return failure(true);
|
|
|
|
}
|
|
|
|
BCR_TRACE(4, "Map: '" << Name << "' to #" << slot << ":" << D;
|
2002-01-20 22:54:45 +00:00
|
|
|
if (!isa<Instruction>(D)) cerr << "\n");
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
D->setName(Name, ST);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (Buf > EndBuf) return failure(true);
|
|
|
|
return false;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-10-13 06:47:01 +00:00
|
|
|
// DeclareNewGlobalValue - Patch up forward references to global values in the
|
2001-12-03 22:26:30 +00:00
|
|
|
// form of ConstantPointerRef.
|
2001-10-13 06:47:01 +00:00
|
|
|
//
|
|
|
|
void BytecodeParser::DeclareNewGlobalValue(GlobalValue *GV, unsigned Slot) {
|
|
|
|
// Check to see if there is a forward reference to this global variable...
|
|
|
|
// if there is, eliminate it and patch the reference to use the new def'n.
|
|
|
|
GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), Slot));
|
|
|
|
|
|
|
|
if (I != GlobalRefs.end()) {
|
|
|
|
GlobalVariable *OldGV = I->second; // Get the placeholder...
|
|
|
|
BCR_TRACE(3, "Mutating CPPR Forward Ref!\n");
|
|
|
|
|
|
|
|
// Loop over all of the uses of the GlobalValue. The only thing they are
|
2001-12-03 22:26:30 +00:00
|
|
|
// allowed to be at this point is ConstantPointerRef's.
|
2001-10-13 06:47:01 +00:00
|
|
|
assert(OldGV->use_size() == 1 && "Only one reference should exist!");
|
|
|
|
while (!OldGV->use_empty()) {
|
2001-12-03 22:26:30 +00:00
|
|
|
User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
|
|
|
|
ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
|
2001-10-13 06:47:01 +00:00
|
|
|
assert(CPPR->getValue() == OldGV && "Something isn't happy");
|
|
|
|
|
|
|
|
BCR_TRACE(4, "Mutating Forward Ref!\n");
|
|
|
|
|
|
|
|
// Change the const pool reference to point to the real global variable
|
|
|
|
// now. This should drop a use from the OldGV.
|
|
|
|
CPPR->mutateReference(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove GV from the module...
|
|
|
|
GV->getParent()->getGlobalList().remove(OldGV);
|
|
|
|
delete OldGV; // Delete the old placeholder
|
|
|
|
|
|
|
|
// Remove the map entry for the global now that it has been created...
|
|
|
|
GlobalRefs.erase(I);
|
|
|
|
}
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
bool BytecodeParser::ParseMethod(const uchar *&Buf, const uchar *EndBuf,
|
|
|
|
Module *C) {
|
|
|
|
// Clear out the local values table...
|
|
|
|
Values.clear();
|
2001-10-24 01:15:12 +00:00
|
|
|
if (MethodSignatureList.empty()) {
|
2002-03-29 03:51:11 +00:00
|
|
|
Error = "Function found, but FunctionSignatureList empty!";
|
2001-10-24 01:15:12 +00:00
|
|
|
return failure(true); // Unexpected method!
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
const PointerType *PMTy = MethodSignatureList.front().first; // PtrMeth
|
2002-03-29 03:51:11 +00:00
|
|
|
const FunctionType *MTy = dyn_cast<FunctionType>(PMTy->getElementType());
|
2001-10-03 14:53:21 +00:00
|
|
|
if (MTy == 0) return failure(true); // Not ptr to method!
|
|
|
|
|
2001-11-26 18:56:10 +00:00
|
|
|
unsigned isInternal;
|
|
|
|
if (read_vbr(Buf, EndBuf, isInternal)) return failure(true);
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
unsigned MethSlot = MethodSignatureList.front().second;
|
|
|
|
MethodSignatureList.pop_front();
|
2002-03-29 03:51:11 +00:00
|
|
|
Function *M = new Function(MTy, isInternal != 0);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
BCR_TRACE(2, "METHOD TYPE: " << MTy << "\n");
|
2001-09-07 16:37:43 +00:00
|
|
|
|
2002-03-29 03:51:11 +00:00
|
|
|
const FunctionType::ParamTypes &Params = MTy->getParamTypes();
|
|
|
|
for (FunctionType::ParamTypes::const_iterator It = Params.begin();
|
2001-06-27 23:41:11 +00:00
|
|
|
It != Params.end(); ++It) {
|
2002-04-09 19:48:49 +00:00
|
|
|
Argument *FA = new Argument(*It);
|
2002-03-26 18:01:55 +00:00
|
|
|
if (insertValue(FA, Values) == -1) {
|
2001-10-24 01:15:12 +00:00
|
|
|
Error = "Error reading method arguments!\n";
|
|
|
|
delete M; return failure(true);
|
|
|
|
}
|
2002-03-26 18:01:55 +00:00
|
|
|
M->getArgumentList().push_back(FA);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (Buf < EndBuf) {
|
|
|
|
unsigned Type, Size;
|
|
|
|
const uchar *OldBuf = Buf;
|
2001-10-24 01:15:12 +00:00
|
|
|
if (readBlock(Buf, EndBuf, Type, Size)) {
|
2002-03-29 03:51:11 +00:00
|
|
|
Error = "Error reading Function level block!";
|
2001-10-24 01:15:12 +00:00
|
|
|
delete M; return failure(true);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
switch (Type) {
|
|
|
|
case BytecodeFormat::ConstantPool:
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
|
|
|
if (ParseConstantPool(Buf, Buf+Size, Values, MethodTypeValues)) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete M; return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BytecodeFormat::BasicBlock: {
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::BasicBlock: {\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
BasicBlock *BB;
|
|
|
|
if (ParseBasicBlock(Buf, Buf+Size, BB) ||
|
2001-10-13 06:47:01 +00:00
|
|
|
insertValue(BB, Values) == -1) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete M; return failure(true); // Parse error... :(
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
M->getBasicBlocks().push_back(BB);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BytecodeFormat::SymbolTable:
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "BLOCK BytecodeFormat::SymbolTable: {\n");
|
|
|
|
if (ParseSymbolTable(Buf, Buf+Size, M->getSymbolTableSure())) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete M; return failure(true);
|
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;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (OldBuf > Buf) return failure(true); // Wrap around!
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(2, "} end block\n");
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (align32(Buf, EndBuf)) {
|
2002-03-29 03:51:11 +00:00
|
|
|
Error = "Error aligning Function level block!";
|
2001-06-06 20:29:01 +00:00
|
|
|
delete M; // Malformed bc file, read past end of block.
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postResolveValues(LateResolveValues) ||
|
|
|
|
postResolveValues(LateResolveModuleValues)) {
|
2001-10-24 01:15:12 +00:00
|
|
|
Error = "Error resolving method values!";
|
2001-07-28 17:50:18 +00:00
|
|
|
delete M; return failure(true); // Unresolvable references!
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
Value *MethPHolder = getValue(PMTy, MethSlot, false);
|
2001-06-06 20:29:01 +00:00
|
|
|
assert(MethPHolder && "Something is broken no placeholder found!");
|
2002-03-29 03:51:11 +00:00
|
|
|
assert(isa<Function>(MethPHolder) && "Not a function?");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
unsigned type; // Type slot
|
|
|
|
assert(!getTypeSlot(MTy, type) && "How can meth type not exist?");
|
2001-10-03 14:53:21 +00:00
|
|
|
getTypeSlot(PMTy, type);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
C->getFunctionList().push_back(M);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Replace placeholder with the real method pointer...
|
|
|
|
ModuleValues[type][MethSlot] = M;
|
|
|
|
|
2001-09-14 22:03:42 +00:00
|
|
|
// Clear out method level types...
|
|
|
|
MethodTypeValues.clear();
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// If anyone is using the placeholder make them use the real method instead
|
|
|
|
MethPHolder->replaceAllUsesWith(M);
|
|
|
|
|
|
|
|
// We don't need the placeholder anymore!
|
|
|
|
delete MethPHolder;
|
|
|
|
|
2001-10-14 23:28:41 +00:00
|
|
|
// If the method is empty, we don't need the method argument entries...
|
|
|
|
if (M->isExternal())
|
|
|
|
M->getArgumentList().delete_all();
|
|
|
|
|
2001-10-13 06:47:01 +00:00
|
|
|
DeclareNewGlobalValue(M, MethSlot);
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::ParseModuleGlobalInfo(const uchar *&Buf, const uchar *End,
|
2001-10-13 06:47:01 +00:00
|
|
|
Module *Mod) {
|
2001-10-24 01:15:12 +00:00
|
|
|
if (!MethodSignatureList.empty()) {
|
|
|
|
Error = "Two ModuleGlobalInfo packets found!";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true); // Two ModuleGlobal blocks?
|
2001-10-24 01:15:12 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Read global variables...
|
|
|
|
unsigned VarType;
|
|
|
|
if (read_vbr(Buf, End, VarType)) return failure(true);
|
|
|
|
while (VarType != Type::VoidTyID) { // List is terminated by Void
|
2001-11-26 18:56:10 +00:00
|
|
|
// VarType Fields: bit0 = isConstant, bit1 = hasInitializer,
|
|
|
|
// bit2 = isInternal, bit3+ = slot#
|
|
|
|
const Type *Ty = getType(VarType >> 3);
|
2001-09-10 07:58:01 +00:00
|
|
|
if (!Ty || !Ty->isPointerType()) {
|
2001-10-24 01:15:12 +00:00
|
|
|
Error = "Global not pointer type! Ty = " + Ty->getDescription();
|
2001-09-10 07:58:01 +00:00
|
|
|
return failure(true);
|
|
|
|
}
|
|
|
|
|
2001-10-03 14:53:21 +00:00
|
|
|
const PointerType *PTy = cast<const PointerType>(Ty);
|
2001-12-04 00:03:30 +00:00
|
|
|
const Type *ElTy = PTy->getElementType();
|
2001-10-03 14:53:21 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
Constant *Initializer = 0;
|
2001-09-18 04:01:05 +00:00
|
|
|
if (VarType & 2) { // Does it have an initalizer?
|
|
|
|
// Do not improvise... values must have been stored in the constant pool,
|
|
|
|
// which should have been read before now.
|
|
|
|
//
|
|
|
|
unsigned InitSlot;
|
|
|
|
if (read_vbr(Buf, End, InitSlot)) return failure(true);
|
|
|
|
|
2001-10-13 06:47:01 +00:00
|
|
|
Value *V = getValue(ElTy, InitSlot, false);
|
2001-09-18 04:01:05 +00:00
|
|
|
if (V == 0) return failure(true);
|
2001-12-03 22:26:30 +00:00
|
|
|
Initializer = cast<Constant>(V);
|
2001-09-18 04:01:05 +00:00
|
|
|
}
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
// Create the global variable...
|
2001-11-26 18:56:10 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(ElTy, VarType & 1, VarType & 4,
|
|
|
|
Initializer);
|
2001-10-13 06:47:01 +00:00
|
|
|
int DestSlot = insertValue(GV, ModuleValues);
|
|
|
|
if (DestSlot == -1) return failure(true);
|
|
|
|
|
|
|
|
Mod->getGlobalList().push_back(GV);
|
|
|
|
|
|
|
|
DeclareNewGlobalValue(GV, unsigned(DestSlot));
|
|
|
|
|
|
|
|
BCR_TRACE(2, "Global Variable of type: " << PTy->getDescription()
|
2002-01-20 22:54:45 +00:00
|
|
|
<< " into slot #" << DestSlot << "\n");
|
2001-09-10 07:58:01 +00:00
|
|
|
|
|
|
|
if (read_vbr(Buf, End, VarType)) return failure(true);
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// Read the method signatures for all of the methods that are coming, and
|
|
|
|
// create fillers in the Value tables.
|
|
|
|
unsigned MethSignature;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, End, MethSignature)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
while (MethSignature != Type::VoidTyID) { // List is terminated by Void
|
|
|
|
const Type *Ty = getType(MethSignature);
|
2001-10-03 14:53:21 +00:00
|
|
|
if (!Ty || !isa<PointerType>(Ty) ||
|
2002-03-29 03:51:11 +00:00
|
|
|
!isa<FunctionType>(cast<PointerType>(Ty)->getElementType())) {
|
|
|
|
Error = "Function not ptr to func type! Ty = " + Ty->getDescription();
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-10-03 14:53:21 +00:00
|
|
|
|
2002-03-29 03:51:11 +00:00
|
|
|
// We create methods 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
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
// When the ModuleGlobalInfo section is read, we load the type of each
|
|
|
|
// method and the 'ModuleValues' slot that it lands in. We then load a
|
|
|
|
// placeholder into its slot to reserve it. When the method is loaded, this
|
|
|
|
// placeholder is replaced.
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Insert the placeholder...
|
2001-10-03 14:53:21 +00:00
|
|
|
Value *Val = new MethPHolder(Ty, 0);
|
2001-10-13 06:47:01 +00:00
|
|
|
if (insertValue(Val, ModuleValues) == -1) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Figure out which entry of its typeslot it went into...
|
|
|
|
unsigned TypeSlot;
|
2001-10-03 14:53:21 +00:00
|
|
|
if (getTypeSlot(Val->getType(), TypeSlot)) return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
unsigned SlotNo = ModuleValues[TypeSlot].size()-1;
|
|
|
|
|
|
|
|
// Keep track of this information in a linked list that is emptied as
|
|
|
|
// methods are loaded...
|
|
|
|
//
|
2001-10-03 14:53:21 +00:00
|
|
|
MethodSignatureList.push_back(
|
|
|
|
make_pair(cast<const PointerType>(Val->getType()), SlotNo));
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, End, MethSignature)) return failure(true);
|
2002-03-29 03:51:11 +00:00
|
|
|
BCR_TRACE(2, "Function of type: " << Ty << "\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (align32(Buf, End)) return failure(true);
|
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;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BytecodeParser::ParseModule(const uchar *Buf, const uchar *EndBuf,
|
|
|
|
Module *&C) {
|
|
|
|
|
|
|
|
unsigned Type, Size;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (readBlock(Buf, EndBuf, Type, Size)) return failure(true);
|
2001-10-24 01:15:12 +00:00
|
|
|
if (Type != BytecodeFormat::Module || Buf+Size != EndBuf) {
|
|
|
|
Error = "Expected Module packet!";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true); // Hrm, not a class?
|
2001-10-24 01:15:12 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(0, "BLOCK BytecodeFormat::Module: {\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
MethodSignatureList.clear(); // Just in case...
|
|
|
|
|
|
|
|
// Read into instance variables...
|
2001-07-28 17:50:18 +00:00
|
|
|
if (read_vbr(Buf, EndBuf, FirstDerivedTyID)) return failure(true);
|
|
|
|
if (align32(Buf, EndBuf)) return failure(true);
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(1, "FirstDerivedTyID = " << FirstDerivedTyID << "\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-13 06:47:01 +00:00
|
|
|
TheModule = C = new Module();
|
2001-06-06 20:29:01 +00:00
|
|
|
while (Buf < EndBuf) {
|
|
|
|
const uchar *OldBuf = Buf;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (readBlock(Buf, EndBuf, Type, Size)) { delete C; return failure(true); }
|
2001-06-06 20:29:01 +00:00
|
|
|
switch (Type) {
|
2001-09-07 16:37:43 +00:00
|
|
|
case BytecodeFormat::ConstantPool:
|
|
|
|
BCR_TRACE(1, "BLOCK BytecodeFormat::ConstantPool: {\n");
|
|
|
|
if (ParseConstantPool(Buf, Buf+Size, ModuleValues, ModuleTypeValues)) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete C; return failure(true);
|
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");
|
|
|
|
|
|
|
|
if (ParseModuleGlobalInfo(Buf, Buf+Size, C)) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete C; return failure(true);
|
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");
|
2001-06-06 20:29:01 +00:00
|
|
|
if (ParseMethod(Buf, Buf+Size, C)) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete C; return failure(true); // Error parsing method
|
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");
|
|
|
|
if (ParseSymbolTable(Buf, Buf+Size, C->getSymbolTableSure())) {
|
2001-07-28 17:50:18 +00:00
|
|
|
delete C; return failure(true);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-10-24 01:15:12 +00:00
|
|
|
Error = "Expected Module Block!";
|
2001-06-06 20:29:01 +00:00
|
|
|
Buf += Size;
|
2001-07-28 17:50:18 +00:00
|
|
|
if (OldBuf > Buf) return failure(true); // Wrap around!
|
2001-06-06 20:29:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-09-07 16:37:43 +00:00
|
|
|
BCR_TRACE(1, "} end block\n");
|
2001-07-28 17:50:18 +00:00
|
|
|
if (align32(Buf, EndBuf)) { delete C; return failure(true); }
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-10-24 01:15:12 +00:00
|
|
|
if (!MethodSignatureList.empty()) { // Expected more methods!
|
2002-03-29 03:51:11 +00:00
|
|
|
Error = "Function expected, but bytecode stream at end!";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure(true);
|
2001-10-24 01:15:12 +00:00
|
|
|
}
|
2001-09-07 16:37:43 +00:00
|
|
|
|
|
|
|
BCR_TRACE(0, "} end block\n\n");
|
2001-06-06 20:29:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Module *BytecodeParser::ParseBytecode(const uchar *Buf, const uchar *EndBuf) {
|
|
|
|
LateResolveValues.clear();
|
|
|
|
unsigned Sig;
|
|
|
|
// Read and check signature...
|
|
|
|
if (read(Buf, EndBuf, Sig) ||
|
2001-10-24 01:15:12 +00:00
|
|
|
Sig != ('l' | ('l' << 8) | ('v' << 16) | 'm' << 24)) {
|
|
|
|
Error = "Invalid bytecode signature!";
|
2001-07-28 17:50:18 +00:00
|
|
|
return failure<Module*>(0); // Invalid signature!
|
2001-10-24 01:15:12 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
Module *Result;
|
|
|
|
if (ParseModule(Buf, EndBuf, Result)) return 0;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Module *ParseBytecodeBuffer(const uchar *Buffer, unsigned Length) {
|
|
|
|
BytecodeParser Parser;
|
|
|
|
return Parser.ParseBytecode(Buffer, Buffer+Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse and return a class file...
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
Module *ParseBytecodeFile(const std::string &Filename, std::string *ErrorStr) {
|
2001-06-06 20:29:01 +00:00
|
|
|
struct stat StatBuf;
|
|
|
|
Module *Result = 0;
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
if (Filename != std::string("-")) { // Read from a file...
|
2001-07-23 18:51:23 +00:00
|
|
|
int FD = open(Filename.c_str(), O_RDONLY);
|
2001-10-24 01:15:12 +00:00
|
|
|
if (FD == -1) {
|
|
|
|
if (ErrorStr) *ErrorStr = "Error opening file!";
|
|
|
|
return failure<Module*>(0);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-07-28 17:50:18 +00:00
|
|
|
if (fstat(FD, &StatBuf) == -1) { close(FD); return failure<Module*>(0); }
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
int Length = StatBuf.st_size;
|
2001-10-24 01:15:12 +00:00
|
|
|
if (Length == 0) {
|
|
|
|
if (ErrorStr) *ErrorStr = "Error stat'ing file!";
|
|
|
|
close(FD); return failure<Module*>(0);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
uchar *Buffer = (uchar*)mmap(0, Length, PROT_READ,
|
|
|
|
MAP_PRIVATE, FD, 0);
|
2001-10-24 01:15:12 +00:00
|
|
|
if (Buffer == (uchar*)-1) {
|
|
|
|
if (ErrorStr) *ErrorStr = "Error mmapping file!";
|
|
|
|
close(FD); return failure<Module*>(0);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
BytecodeParser Parser;
|
|
|
|
Result = Parser.ParseBytecode(Buffer, Buffer+Length);
|
|
|
|
|
|
|
|
munmap((char*)Buffer, Length);
|
|
|
|
close(FD);
|
2001-10-24 01:15:12 +00:00
|
|
|
if (ErrorStr) *ErrorStr = Parser.getError();
|
2001-06-06 20:29:01 +00:00
|
|
|
} else { // Read from stdin
|
|
|
|
size_t FileSize = 0;
|
|
|
|
int BlockSize;
|
|
|
|
uchar Buffer[4096], *FileData = 0;
|
|
|
|
while ((BlockSize = read(0, Buffer, 4))) {
|
2001-07-28 17:50:18 +00:00
|
|
|
if (BlockSize == -1) { free(FileData); return failure<Module*>(0); }
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
FileData = (uchar*)realloc(FileData, FileSize+BlockSize);
|
|
|
|
memcpy(FileData+FileSize, Buffer, BlockSize);
|
|
|
|
FileSize += BlockSize;
|
|
|
|
}
|
|
|
|
|
2001-10-24 01:15:12 +00:00
|
|
|
if (FileSize == 0) {
|
|
|
|
if (ErrorStr) *ErrorStr = "Standard Input empty!";
|
|
|
|
free(FileData); return failure<Module*>(0);
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
#define ALIGN_PTRS 1
|
|
|
|
#if ALIGN_PTRS
|
|
|
|
uchar *Buf = (uchar*)mmap(0, FileSize, PROT_READ|PROT_WRITE,
|
|
|
|
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
|
|
|
|
assert((Buf != (uchar*)-1) && "mmap returned error!");
|
|
|
|
memcpy(Buf, FileData, FileSize);
|
2001-11-12 20:30:12 +00:00
|
|
|
free(FileData);
|
2001-06-06 20:29:01 +00:00
|
|
|
#else
|
|
|
|
uchar *Buf = FileData;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
BytecodeParser Parser;
|
|
|
|
Result = Parser.ParseBytecode(Buf, Buf+FileSize);
|
|
|
|
|
|
|
|
#if ALIGN_PTRS
|
|
|
|
munmap((char*)Buf, FileSize); // Free mmap'd data area
|
|
|
|
#else
|
|
|
|
free(FileData); // Free realloc'd block of memory
|
|
|
|
#endif
|
2001-10-24 01:15:12 +00:00
|
|
|
|
|
|
|
if (ErrorStr) *ErrorStr = Parser.getError();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|