1 //===-- ExecutionEngine.cpp - Common Implementation shared by EE's --------===//
3 // This file defines the common interface used by the various execution engine
6 //===----------------------------------------------------------------------===//
8 #include "ExecutionEngine.h"
9 #include "GenericValue.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/Constants.h"
12 #include "llvm/Module.h"
13 #include "llvm/Target/TargetData.h"
14 #include "Support/Statistic.h"
17 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
19 // getPointerToGlobal - This returns the address of the specified global
20 // value. This may involve code generation if it's a function.
22 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
23 if (const Function *F = dyn_cast<Function>(GV))
24 return getPointerToFunction(F);
26 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
27 return GlobalAddress[GV];
31 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
34 if (ConstantExpr *CE = (ConstantExpr*)dyn_cast<ConstantExpr>(C))
35 switch (CE->getOpcode()) {
36 case Instruction::GetElementPtr: {
37 Result = getConstantValue(cast<Constant>(CE->getOperand(0)));
38 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
40 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
42 Result.LongVal += Offset;
47 std::cerr << "ConstantExpr not handled as global var init: " << *CE
52 switch (C->getType()->getPrimitiveID()) {
53 #define GET_CONST_VAL(TY, CLASS) \
54 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
55 GET_CONST_VAL(Bool , ConstantBool);
56 GET_CONST_VAL(UByte , ConstantUInt);
57 GET_CONST_VAL(SByte , ConstantSInt);
58 GET_CONST_VAL(UShort , ConstantUInt);
59 GET_CONST_VAL(Short , ConstantSInt);
60 GET_CONST_VAL(UInt , ConstantUInt);
61 GET_CONST_VAL(Int , ConstantSInt);
62 GET_CONST_VAL(ULong , ConstantUInt);
63 GET_CONST_VAL(Long , ConstantSInt);
64 GET_CONST_VAL(Float , ConstantFP);
65 GET_CONST_VAL(Double , ConstantFP);
67 case Type::PointerTyID:
68 if (isa<ConstantPointerNull>(C)) {
69 Result.PointerVal = 0;
70 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
71 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
74 assert(0 && "Unknown constant pointer type!");
78 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
84 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
86 if (getTargetData().isLittleEndian()) {
87 switch (Ty->getPrimitiveID()) {
90 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
91 case Type::UShortTyID:
92 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
93 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
97 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
98 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
99 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
100 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
102 case Type::DoubleTyID:
103 case Type::ULongTyID:
105 case Type::PointerTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
106 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
107 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
108 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
109 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
110 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
111 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
112 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
115 std::cout << "Cannot store value of type " << Ty << "!\n";
118 switch (Ty->getPrimitiveID()) {
120 case Type::UByteTyID:
121 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
122 case Type::UShortTyID:
123 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
124 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
126 case Type::FloatTyID:
128 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
129 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
130 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
131 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
133 case Type::DoubleTyID:
134 case Type::ULongTyID:
136 case Type::PointerTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
137 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
138 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
139 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
140 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
141 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
142 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
143 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
146 std::cout << "Cannot store value of type " << Ty << "!\n";
151 // InitializeMemory - Recursive function to apply a Constant value into the
152 // specified memory location...
154 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
155 if (Init->getType()->isFirstClassType()) {
156 GenericValue Val = getConstantValue(Init);
157 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
161 switch (Init->getType()->getPrimitiveID()) {
162 case Type::ArrayTyID: {
163 const ConstantArray *CPA = cast<ConstantArray>(Init);
164 const std::vector<Use> &Val = CPA->getValues();
165 unsigned ElementSize =
166 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
167 for (unsigned i = 0; i < Val.size(); ++i)
168 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
172 case Type::StructTyID: {
173 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
174 const StructLayout *SL =
175 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
176 const std::vector<Use> &Val = CPS->getValues();
177 for (unsigned i = 0; i < Val.size(); ++i)
178 InitializeMemory(cast<Constant>(Val[i].get()),
179 (char*)Addr+SL->MemberOffsets[i]);
184 std::cerr << "Bad Type: " << Init->getType() << "\n";
185 assert(0 && "Unknown constant type to initialize memory with!");
191 void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
192 // Pointers are 64 bits...
193 // FIXME: Assumes 64 bit target
194 PointerTy *Result = new PointerTy[InputArgv.size()+1];
195 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
197 for (unsigned i = 0; i < InputArgv.size(); ++i) {
198 unsigned Size = InputArgv[i].size()+1;
199 char *Dest = new char[Size];
200 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
202 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
205 // Endian safe: Result[i] = (PointerTy)Dest;
206 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i),
207 Type::LongTy); // 64 bit assumption
210 Result[InputArgv.size()] = 0;
214 /// EmitGlobals - Emit all of the global variables to memory, storing their
215 /// addresses into GlobalAddress. This must make sure to copy the contents of
216 /// their initializers into the memory.
218 void ExecutionEngine::emitGlobals() {
219 const TargetData &TD = getTargetData();
221 // Loop over all of the global variables in the program, allocating the memory
223 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
225 if (!I->isExternal()) {
226 // Get the type of the global...
227 const Type *Ty = I->getType()->getElementType();
229 // Allocate some memory for it!
230 unsigned Size = TD.getTypeSize(Ty);
231 GlobalAddress[I] = new char[Size];
232 NumInitBytes += Size;
234 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
235 << (void*)GlobalAddress[I] << "\n");
237 // External variable reference, try to use dlsym to get a pointer to it in
239 if (void *SymAddr = dlsym(0, I->getName().c_str()))
240 GlobalAddress[I] = SymAddr;
242 std::cerr << "Could not resolve external global address: "
243 << I->getName() << "\n";
248 // Now that all of the globals are set up in memory, loop through them all and
249 // initialize their contents.
250 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
252 if (!I->isExternal())
253 InitializeMemory(I->getInitializer(), GlobalAddress[I]);