//
// 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.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include "llvm/Module.h"
#include "llvm/ModuleProvider.h"
#include "llvm/ADT/Statistic.h"
+#include "llvm/Config/alloca.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/MutexGuard.h"
#include "llvm/System/DynamicLibrary.h"
+#include "llvm/System/Host.h"
#include "llvm/Target/TargetData.h"
#include <math.h>
using namespace llvm;
ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
-ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
+ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
LazyCompilationDisabled = false;
Modules.push_back(P);
assert(P && "ModuleProvider is null?");
}
-ExecutionEngine::ExecutionEngine(Module *M) {
- LazyCompilationDisabled = false;
- assert(M && "Module is null?");
- Modules.push_back(new ExistingModuleProvider(M));
-}
-
ExecutionEngine::~ExecutionEngine() {
clearAllGlobalMappings();
for (unsigned i = 0, e = Modules.size(); i != e; ++i)
delete Modules[i];
}
+/// removeModuleProvider - Remove a ModuleProvider from the list of modules.
+/// Release module from ModuleProvider.
+Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P,
+ std::string *ErrInfo) {
+ for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(),
+ E = Modules.end(); I != E; ++I) {
+ ModuleProvider *MP = *I;
+ if (MP == P) {
+ Modules.erase(I);
+ return MP->releaseModule(ErrInfo);
+ }
+ }
+ return NULL;
+}
+
/// FindFunctionNamed - Search all of the active modules to find the one that
/// defines FnName. This is very slow operation and shouldn't be used for
/// general code.
char *Result = new char[(InputArgv.size()+1)*PtrSize];
DOUT << "ARGV = " << (void*)Result << "\n";
- const Type *SBytePtr = PointerType::get(Type::Int8Ty);
+ const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
for (unsigned i = 0; i != InputArgv.size(); ++i) {
unsigned Size = InputArgv[i].size()+1;
}
}
+/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
+static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
+ unsigned PtrSize = EE->getTargetData()->getPointerSize();
+ for (unsigned i = 0; i < PtrSize; ++i)
+ if (*(i + (uint8_t*)Loc))
+ return false;
+ return true;
+}
+
/// runFunctionAsMain - This is a helper function which wraps runFunction to
/// handle the common task of starting up main with the specified argc, argv,
/// and envp parameters.
// Check main() type
unsigned NumArgs = Fn->getFunctionType()->getNumParams();
const FunctionType *FTy = Fn->getFunctionType();
- const Type* PPInt8Ty = PointerType::get(PointerType::get(Type::Int8Ty));
+ const Type* PPInt8Ty =
+ PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
switch (NumArgs) {
case 3:
if (FTy->getParamType(2) != PPInt8Ty) {
GVArgs.push_back(GVArgc); // Arg #0 = argc.
if (NumArgs > 1) {
GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
- assert(((char **)GVTOP(GVArgs[1]))[0] &&
+ assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
"argv[0] was null after CreateArgv");
if (NumArgs > 2) {
std::vector<std::string> EnvVars;
if (EE) {
// Make sure we can resolve symbols in the program as well. The zero arg
// to the function tells DynamicLibrary to load the program, not a library.
- try {
- sys::DynamicLibrary::LoadLibraryPermanently(0);
- } catch (...) {
+ if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) {
+ delete EE;
+ return 0;
}
}
return EE;
}
+ExecutionEngine *ExecutionEngine::create(Module *M) {
+ return create(new ExistingModuleProvider(M));
+}
+
/// getPointerToGlobal - This returns the address of the specified global
/// value. This may involve code generation if it's a function.
///
return GV;
}
case Instruction::FPTrunc: {
+ // FIXME long double
GenericValue GV = getConstantValue(Op0);
GV.FloatVal = float(GV.DoubleVal);
return GV;
}
case Instruction::FPExt:{
+ // FIXME long double
GenericValue GV = getConstantValue(Op0);
GV.DoubleVal = double(GV.FloatVal);
return GV;
GenericValue GV = getConstantValue(Op0);
if (CE->getType() == Type::FloatTy)
GV.FloatVal = float(GV.IntVal.roundToDouble());
- else
+ else if (CE->getType() == Type::DoubleTy)
GV.DoubleVal = GV.IntVal.roundToDouble();
+ else if (CE->getType() == Type::X86_FP80Ty) {
+ const uint64_t zero[] = {0, 0};
+ APFloat apf = APFloat(APInt(80, 2, zero));
+ (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(),
+ GV.IntVal.getBitWidth(), false,
+ APFloat::rmNearestTiesToEven);
+ GV.IntVal = apf.convertToAPInt();
+ }
return GV;
}
case Instruction::SIToFP: {
GenericValue GV = getConstantValue(Op0);
if (CE->getType() == Type::FloatTy)
GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
- else
+ else if (CE->getType() == Type::DoubleTy)
GV.DoubleVal = GV.IntVal.signedRoundToDouble();
+ else if (CE->getType() == Type::X86_FP80Ty) {
+ const uint64_t zero[] = { 0, 0};
+ APFloat apf = APFloat(APInt(80, 2, zero));
+ (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(),
+ GV.IntVal.getBitWidth(), true,
+ APFloat::rmNearestTiesToEven);
+ GV.IntVal = apf.convertToAPInt();
+ }
return GV;
}
case Instruction::FPToUI: // double->APInt conversion handles sign
uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
if (Op0->getType() == Type::FloatTy)
GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
- else
+ else if (Op0->getType() == Type::DoubleTy)
GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
+ else if (Op0->getType() == Type::X86_FP80Ty) {
+ APFloat apf = APFloat(GV.IntVal);
+ uint64_t v;
+ (void)apf.convertToInteger(&v, BitWidth,
+ CE->getOpcode()==Instruction::FPToSI,
+ APFloat::rmTowardZero);
+ GV.IntVal = v; // endian?
+ }
return GV;
}
case Instruction::PtrToInt: {
GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
}
break;
+ case Type::X86_FP80TyID:
+ case Type::PPC_FP128TyID:
+ case Type::FP128TyID: {
+ APFloat apfLHS = APFloat(LHS.IntVal);
+ switch (CE->getOpcode()) {
+ default: assert(0 && "Invalid long double opcode"); abort();
+ case Instruction::Add:
+ apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+ GV.IntVal = apfLHS.convertToAPInt();
+ break;
+ case Instruction::Sub:
+ apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+ GV.IntVal = apfLHS.convertToAPInt();
+ break;
+ case Instruction::Mul:
+ apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+ GV.IntVal = apfLHS.convertToAPInt();
+ break;
+ case Instruction::FDiv:
+ apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+ GV.IntVal = apfLHS.convertToAPInt();
+ break;
+ case Instruction::FRem:
+ apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+ GV.IntVal = apfLHS.convertToAPInt();
+ break;
+ }
+ }
+ break;
}
return GV;
}
case Type::DoubleTyID:
Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
break;
+ case Type::X86_FP80TyID:
+ case Type::FP128TyID:
+ case Type::PPC_FP128TyID:
+ Result.IntVal = cast <ConstantFP>(C)->getValueAPF().convertToAPInt();
+ break;
case Type::IntegerTyID:
Result.IntVal = cast<ConstantInt>(C)->getValue();
break;
return Result;
}
+/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
+/// with the integer held in IntVal.
+static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
+ unsigned StoreBytes) {
+ assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
+ uint8_t *Src = (uint8_t *)IntVal.getRawData();
+
+ if (sys::littleEndianHost())
+ // Little-endian host - the source is ordered from LSB to MSB. Order the
+ // destination from LSB to MSB: Do a straight copy.
+ memcpy(Dst, Src, StoreBytes);
+ else {
+ // Big-endian host - the source is an array of 64 bit words ordered from
+ // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
+ // from MSB to LSB: Reverse the word order, but not the bytes in a word.
+ while (StoreBytes > sizeof(uint64_t)) {
+ StoreBytes -= sizeof(uint64_t);
+ // May not be aligned so use memcpy.
+ memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
+ Src += sizeof(uint64_t);
+ }
+
+ memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
+ }
+}
+
/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
/// is the address of the memory at which to store Val, cast to GenericValue *.
/// It is not a pointer to a GenericValue containing the address at which to
/// store Val.
-///
void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr,
const Type *Ty) {
+ const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
+
switch (Ty->getTypeID()) {
- case Type::IntegerTyID: {
- unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- GenericValue TmpVal = Val;
- if (BitWidth <= 8)
- *((uint8_t*)Ptr) = uint8_t(Val.IntVal.getZExtValue());
- else if (BitWidth <= 16) {
- *((uint16_t*)Ptr) = uint16_t(Val.IntVal.getZExtValue());
- } else if (BitWidth <= 32) {
- *((uint32_t*)Ptr) = uint32_t(Val.IntVal.getZExtValue());
- } else if (BitWidth <= 64) {
- *((uint64_t*)Ptr) = uint64_t(Val.IntVal.getZExtValue());
- } else {
- uint64_t *Dest = (uint64_t*)Ptr;
- const uint64_t *Src = Val.IntVal.getRawData();
- for (uint32_t i = 0; i < Val.IntVal.getNumWords(); ++i)
- Dest[i] = Src[i];
- }
+ case Type::IntegerTyID:
+ StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
break;
- }
case Type::FloatTyID:
*((float*)Ptr) = Val.FloatVal;
break;
case Type::DoubleTyID:
*((double*)Ptr) = Val.DoubleVal;
break;
- case Type::PointerTyID:
+ case Type::X86_FP80TyID: {
+ uint16_t *Dest = (uint16_t*)Ptr;
+ const uint16_t *Src = (uint16_t*)Val.IntVal.getRawData();
+ // This is endian dependent, but it will only work on x86 anyway.
+ Dest[0] = Src[4];
+ Dest[1] = Src[0];
+ Dest[2] = Src[1];
+ Dest[3] = Src[2];
+ Dest[4] = Src[3];
+ break;
+ }
+ case Type::PointerTyID:
+ // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
+ if (StoreBytes != sizeof(PointerTy))
+ memset(Ptr, 0, StoreBytes);
+
*((PointerTy*)Ptr) = Val.PointerVal;
break;
default:
cerr << "Cannot store value of type " << *Ty << "!\n";
}
+
+ if (sys::littleEndianHost() != getTargetData()->isLittleEndian())
+ // Host and target are different endian - reverse the stored bytes.
+ std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
+}
+
+/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
+/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
+static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
+ assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
+ uint8_t *Dst = (uint8_t *)IntVal.getRawData();
+
+ if (sys::littleEndianHost())
+ // Little-endian host - the destination must be ordered from LSB to MSB.
+ // The source is ordered from LSB to MSB: Do a straight copy.
+ memcpy(Dst, Src, LoadBytes);
+ else {
+ // Big-endian - the destination is an array of 64 bit words ordered from
+ // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
+ // ordered from MSB to LSB: Reverse the word order, but not the bytes in
+ // a word.
+ while (LoadBytes > sizeof(uint64_t)) {
+ LoadBytes -= sizeof(uint64_t);
+ // May not be aligned so use memcpy.
+ memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
+ Dst += sizeof(uint64_t);
+ }
+
+ memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
+ }
}
/// FIXME: document
///
-void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
+void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
GenericValue *Ptr,
const Type *Ty) {
+ const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
+
+ if (sys::littleEndianHost() != getTargetData()->isLittleEndian()) {
+ // Host and target are different endian - reverse copy the stored
+ // bytes into a buffer, and load from that.
+ uint8_t *Src = (uint8_t*)Ptr;
+ uint8_t *Buf = (uint8_t*)alloca(LoadBytes);
+ std::reverse_copy(Src, Src + LoadBytes, Buf);
+ Ptr = (GenericValue*)Buf;
+ }
+
switch (Ty->getTypeID()) {
- case Type::IntegerTyID: {
- unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- if (BitWidth <= 8)
- Result.IntVal = APInt(BitWidth, *((uint8_t*)Ptr));
- else if (BitWidth <= 16) {
- Result.IntVal = APInt(BitWidth, *((uint16_t*)Ptr));
- } else if (BitWidth <= 32) {
- Result.IntVal = APInt(BitWidth, *((uint32_t*)Ptr));
- } else if (BitWidth <= 64) {
- Result.IntVal = APInt(BitWidth, *((uint64_t*)Ptr));
- } else
- Result.IntVal = APInt(BitWidth, (BitWidth+63)/64, (uint64_t*)Ptr);
+ case Type::IntegerTyID:
+ // An APInt with all words initially zero.
+ Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
+ LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
break;
- }
case Type::FloatTyID:
Result.FloatVal = *((float*)Ptr);
break;
case Type::DoubleTyID:
- Result.DoubleVal = *((double*)Ptr);
+ Result.DoubleVal = *((double*)Ptr);
break;
- case Type::PointerTyID:
+ case Type::PointerTyID:
Result.PointerVal = *((PointerTy*)Ptr);
break;
+ case Type::X86_FP80TyID: {
+ // This is endian dependent, but it will only work on x86 anyway.
+ // FIXME: Will not trap if loading a signaling NaN.
+ uint16_t *p = (uint16_t*)Ptr;
+ union {
+ uint16_t x[8];
+ uint64_t y[2];
+ };
+ x[0] = p[1];
+ x[1] = p[2];
+ x[2] = p[3];
+ x[3] = p[4];
+ x[4] = p[0];
+ Result.IntVal = APInt(80, 2, y);
+ break;
+ }
default:
cerr << "Cannot load value of type " << *Ty << "!\n";
abort();
return;
} else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
unsigned ElementSize =
- getTargetData()->getTypeSize(CP->getType()->getElementType());
+ getTargetData()->getABITypeSize(CP->getType()->getElementType());
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
return;
StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
return;
} else if (isa<ConstantAggregateZero>(Init)) {
- memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
+ memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType()));
return;
}
case Type::ArrayTyID: {
const ConstantArray *CPA = cast<ConstantArray>(Init);
unsigned ElementSize =
- getTargetData()->getTypeSize(CPA->getType()->getElementType());
+ getTargetData()->getABITypeSize(CPA->getType()->getElementType());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
return;
const Type *Ty = I->getType()->getElementType();
// Allocate some memory for it!
- unsigned Size = TD->getTypeSize(Ty);
+ unsigned Size = TD->getABITypeSize(Ty);
addGlobalMapping(I, new char[Size]);
} else {
// External variable reference. Try to use the dynamic loader to
DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
const Type *ElTy = GV->getType()->getElementType();
- size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
+ size_t GVSize = (size_t)getTargetData()->getABITypeSize(ElTy);
if (GA == 0) {
// If it's not already specified, allocate memory for the global.
GA = new char[GVSize];