#include "llvm/Module.h"
#include "llvm/Target/TargetData.h"
#include "Support/Statistic.h"
+#include <dlfcn.h>
Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
GenericValue Result;
-#define GET_CONST_VAL(TY, CLASS) \
- case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
+
+ if (ConstantExpr *CE = (ConstantExpr*)dyn_cast<ConstantExpr>(C))
+ switch (CE->getOpcode()) {
+ case Instruction::GetElementPtr: {
+ Result = getConstantValue(cast<Constant>(CE->getOperand(0)));
+ std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
+ uint64_t Offset =
+ TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
+
+ Result.LongVal += Offset;
+ return Result;
+ }
+
+ default:
+ std::cerr << "ConstantExpr not handled as global var init: " << *CE
+ << "\n";
+ abort();
+ }
switch (C->getType()->getPrimitiveID()) {
+#define GET_CONST_VAL(TY, CLASS) \
+ case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
GET_CONST_VAL(Bool , ConstantBool);
GET_CONST_VAL(UByte , ConstantUInt);
GET_CONST_VAL(SByte , ConstantSInt);
break;
default:
std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
+ abort();
}
return Result;
}
case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
break;
+ Store4BytesLittleEndian:
case Type::FloatTyID:
case Type::UIntTyID:
case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
break;
+ case Type::PointerTyID: if (CurMod.has32BitPointers())
+ goto Store4BytesLittleEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
- case Type::LongTyID:
- case Type::PointerTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
+ case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
break;
+ Store4BytesBigEndian:
case Type::FloatTyID:
case Type::UIntTyID:
case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
break;
+ case Type::PointerTyID: if (CurMod.has32BitPointers())
+ goto Store4BytesBigEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
- case Type::LongTyID:
- case Type::PointerTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
+ case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
// Pointers are 64 bits...
- PointerTy *Result = new PointerTy[InputArgv.size()+1]; // 64 bit assumption
+ // FIXME: Assumes 64 bit target
+ PointerTy *Result = new PointerTy[InputArgv.size()+1];
DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
for (unsigned i = 0; i < InputArgv.size(); ++i) {
DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
<< (void*)GlobalAddress[I] << "\n");
} else {
- std::cerr << "Global: " << I->getName() << "\n";
- assert(0 && "References to external globals not handled yet!");
+ // External variable reference, try to use dlsym to get a pointer to it in
+ // the LLI image.
+ if (void *SymAddr = dlsym(0, I->getName().c_str()))
+ GlobalAddress[I] = SymAddr;
+ else {
+ std::cerr << "Could not resolve external global address: "
+ << I->getName() << "\n";
+ abort();
+ }
}
// Now that all of the globals are set up in memory, loop through them all and