aboutsummaryrefslogtreecommitdiff
path: root/lib/ExecutionEngine/ExecutionEngine.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2003-04-23 19:01:49 +0000
committerChris Lattner <sabre@nondot.org>2003-04-23 19:01:49 +0000
commitd8c03bfa0a8cbcbf3ee31b0e9b52126c9dbafe2a (patch)
treebf6e82aa01fea6f386fc3228ca0689c0492351f1 /lib/ExecutionEngine/ExecutionEngine.cpp
parentc2b97d43a074445c6856d69538a95de09450c26e (diff)
* Implement access to external variables in LLI
* Implement GetElementPtr constant expressions when initializing global variables git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5880 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ExecutionEngine/ExecutionEngine.cpp')
-rw-r--r--lib/ExecutionEngine/ExecutionEngine.cpp41
1 files changed, 31 insertions, 10 deletions
diff --git a/lib/ExecutionEngine/ExecutionEngine.cpp b/lib/ExecutionEngine/ExecutionEngine.cpp
index 3d190aa0f8..336e6a7fbe 100644
--- a/lib/ExecutionEngine/ExecutionEngine.cpp
+++ b/lib/ExecutionEngine/ExecutionEngine.cpp
@@ -12,6 +12,7 @@
#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");
@@ -29,10 +30,28 @@ void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
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);
@@ -57,6 +76,7 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
break;
default:
std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
+ abort();
}
return Result;
}
@@ -213,15 +233,16 @@ void ExecutionEngine::emitGlobals() {
DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
<< (void*)GlobalAddress[I] << "\n");
- } else if (I->getName() == "stdout") {
- GlobalAddress[I] = &stdout;
- } else if (I->getName() == "stderr") {
- GlobalAddress[I] = &stderr;
- } else if (I->getName() == "stdin") {
- GlobalAddress[I] = &stdin;
} 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