//
//===----------------------------------------------------------------------===//
-#include "ExecutionEngine.h"
-#include "GenericValue.h"
+#define DEBUG_TYPE "jit"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "JIT/VM.h"
+#include "Interpreter/Interpreter.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/Module.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Target/TargetData.h"
#include "Support/Debug.h"
#include "Support/Statistic.h"
+#include "Support/DynamicLinker.h"
#include "Config/dlfcn.h"
Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
+ExecutionEngine::~ExecutionEngine() {
+ delete &CurMod;
+}
+
+ExecutionEngine *ExecutionEngine::create (Module *M, bool ForceInterpreter,
+ bool TraceMode) {
+ ExecutionEngine *EE = 0;
+
+ // If there is nothing that is forcing us to use the interpreter, make a JIT.
+ if (!ForceInterpreter && !TraceMode)
+ EE = VM::create(M);
+
+ // If we can't make a JIT, make an interpreter instead.
+ if (EE == 0)
+ EE = Interpreter::create(M, TraceMode);
+ return EE;
+}
+
// getPointerToGlobal - This returns the address of the specified global
// value. This may involve code generation if it's a function.
//
void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
- if (const Function *F = dyn_cast<Function>(GV))
+ if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
return getPointerToFunction(F);
assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
return GlobalAddress[GV];
}
-
GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
GenericValue Result;
if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
return getConstantValue(Op);
- // Handle cast of long to pointer or pointer to long...
- if ((isa<PointerType>(Op->getType()) && (C->getType() == Type::LongTy ||
- C->getType() == Type::ULongTy))||
- (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
- Op->getType() == Type::ULongTy))){
+ // Handle a cast of pointer to any integral type...
+ if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
+ return getConstantValue(Op);
+
+ // Handle cast of long to pointer...
+ if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
+ Op->getType() == Type::ULongTy))
return getConstantValue(Op);
- }
break;
}
Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
break;
- case Type::PointerTyID: if (CurMod.has32BitPointers())
+ case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
goto Store4BytesLittleEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
break;
- case Type::PointerTyID: if (CurMod.has32BitPointers())
+ case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
goto Store4BytesBigEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
((unsigned)Ptr->Untyped[2] << 16) |
((unsigned)Ptr->Untyped[3] << 24);
break;
- case Type::PointerTyID: if (getModule().has32BitPointers())
+ case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
goto Load4BytesLittleEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
((unsigned)Ptr->Untyped[1] << 16) |
((unsigned)Ptr->Untyped[0] << 24);
break;
- case Type::PointerTyID: if (getModule().has32BitPointers())
+ case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
goto Load4BytesBigEndian;
case Type::DoubleTyID:
case Type::ULongTyID:
return Result;
}
-
// InitializeMemory - Recursive function to apply a Constant value into the
// specified memory location...
//
}
}
-void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
- if (getTargetData().getPointerSize() == 8) { // 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) {
- unsigned Size = InputArgv[i].size()+1;
- char *Dest = new char[Size];
- DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
-
- copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
- Dest[Size-1] = 0;
-
- // Endian safe: Result[i] = (PointerTy)Dest;
- StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy);
- }
- Result[InputArgv.size()] = 0;
- return Result;
-
- } else { // 32 bit target?
- int *Result = new int[InputArgv.size()+1];
- DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
-
- for (unsigned i = 0; i < InputArgv.size(); ++i) {
- unsigned Size = InputArgv[i].size()+1;
- char *Dest = new char[Size];
- DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
-
- copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
- Dest[Size-1] = 0;
-
- // Endian safe: Result[i] = (PointerTy)Dest;
- StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy);
- }
- Result[InputArgv.size()] = 0; // null terminate it
- return Result;
- }
-}
-
/// EmitGlobals - Emit all of the global variables to memory, storing their
/// addresses into GlobalAddress. This must make sure to copy the contents of
/// their initializers into the memory.
DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
<< (void*)GlobalAddress[I] << "\n");
} else {
- // On Sparc, RTLD_SELF is already defined and it's not zero
- // Linux/x86 wants to use a 0, other systems may differ
-#ifndef RTLD_SELF
-#define RTLD_SELF 0
-#endif
- // External variable reference, try to use dlsym to get a pointer to it in
- // the LLI image.
- if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str()))
+ // External variable reference. Try to use the dynamic loader to
+ // get a pointer to it.
+ if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
GlobalAddress[I] = SymAddr;
else {
std::cerr << "Could not resolve external global address: "