#include "llvm/System/DynamicLibrary.h"
#include "llvm/System/Host.h"
#include "llvm/Target/TargetData.h"
-#include <math.h>
+#include <cmath>
+#include <cstring>
using namespace llvm;
STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
ModuleProvider *MP = *I;
if (MP == P) {
Modules.erase(I);
+ clearGlobalMappingsFromModule(MP->getModule());
return MP->releaseModule(ErrInfo);
}
}
state.getGlobalAddressReverseMap(locked).clear();
}
+/// clearGlobalMappingsFromModule - Clear all global mappings that came from a
+/// particular module, because it has been removed from the JIT.
+void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
+ MutexGuard locked(lock);
+
+ for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
+ state.getGlobalAddressMap(locked).erase(FI);
+ state.getGlobalAddressReverseMap(locked).erase(FI);
+ }
+ for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
+ GI != GE; ++GI) {
+ state.getGlobalAddressMap(locked).erase(GI);
+ state.getGlobalAddressReverseMap(locked).erase(GI);
+ }
+}
+
/// updateGlobalMapping - Replace an existing mapping for GV with a new
/// address. This updates both maps as required. If "Addr" is null, the
/// entry for the global is removed from the mappings.
-void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
+void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
-
+
+ std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
+
// Deleting from the mapping?
if (Addr == 0) {
- state.getGlobalAddressMap(locked).erase(GV);
+ std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
+ void *OldVal;
+ if (I == Map.end())
+ OldVal = 0;
+ else {
+ OldVal = I->second;
+ Map.erase(I);
+ }
+
if (!state.getGlobalAddressReverseMap(locked).empty())
state.getGlobalAddressReverseMap(locked).erase(Addr);
- return;
+ return OldVal;
}
- void *&CurVal = state.getGlobalAddressMap(locked)[GV];
+ void *&CurVal = Map[GV];
+ void *OldVal = CurVal;
+
if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
state.getGlobalAddressReverseMap(locked).erase(CurVal);
CurVal = Addr;
assert((V == 0 || GV == 0) && "GlobalMapping already established!");
V = GV;
}
+ return OldVal;
}
/// getPointerToGlobalIfAvailable - This returns the address of the specified
std::string *ErrorStr) {
ExecutionEngine *EE = 0;
+ // 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.
+ if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
+ return 0;
+
// Unless the interpreter was explicitly selected, try making a JIT.
if (!ForceInterpreter && JITCtor)
EE = JITCtor(MP, ErrorStr);
if (EE == 0 && InterpCtor)
EE = InterpCtor(MP, ErrorStr);
- 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.
- if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) {
- delete EE;
- return 0;
- }
- }
-
return EE;
}
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);
+ (void)apf.convertFromAPInt(GV.IntVal,
+ false,
+ APFloat::rmNearestTiesToEven);
GV.IntVal = apf.convertToAPInt();
}
return GV;
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);
+ (void)apf.convertFromAPInt(GV.IntVal,
+ true,
+ APFloat::rmNearestTiesToEven);
GV.IntVal = apf.convertToAPInt();
}
return GV;
/// FIXME: document
///
void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
- GenericValue *Ptr,
- const Type *Ty) {
+ GenericValue *Ptr,
+ const Type *Ty) {
const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
if (sys::littleEndianHost() != getTargetData()->isLittleEndian()) {
for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
return;
- } else if (Init->getType()->isFirstClassType()) {
- GenericValue Val = getConstantValue(Init);
- StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
- return;
} else if (isa<ConstantAggregateZero>(Init)) {
memset(Addr, 0, (size_t)getTargetData()->getABITypeSize(Init->getType()));
return;
- }
-
- switch (Init->getType()->getTypeID()) {
- case Type::ArrayTyID: {
- const ConstantArray *CPA = cast<ConstantArray>(Init);
+ } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
unsigned ElementSize =
getTargetData()->getABITypeSize(CPA->getType()->getElementType());
for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
return;
- }
-
- case Type::StructTyID: {
- const ConstantStruct *CPS = cast<ConstantStruct>(Init);
+ } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
const StructLayout *SL =
getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
return;
+ } else if (Init->getType()->isFirstClassType()) {
+ GenericValue Val = getConstantValue(Init);
+ StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
+ return;
}
- default:
- cerr << "Bad Type: " << *Init->getType() << "\n";
- assert(0 && "Unknown constant type to initialize memory with!");
- }
+ cerr << "Bad Type: " << *Init->getType() << "\n";
+ assert(0 && "Unknown constant type to initialize memory with!");
}
/// EmitGlobals - Emit all of the global variables to memory, storing their
continue;
// Otherwise, we know it's linkonce/weak, replace it if this is a strong
- // symbol.
+ // symbol. FIXME is this right for common?
if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
GVEntry = GV;
}