1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the common interface used by the various execution engine
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "jit"
16 #include "Interpreter/Interpreter.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/ExecutionEngine/ExecutionEngine.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/Target/TargetData.h"
25 #include "Support/Debug.h"
26 #include "Support/Statistic.h"
27 #include "Support/DynamicLinker.h"
28 #include "Config/dlfcn.h"
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
33 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
36 ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
37 CurMod(*P->getModule()), MP(P) {
38 assert(P && "ModuleProvider is null?");
41 ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
42 assert(M && "Module is null?");
45 ExecutionEngine::~ExecutionEngine() {
49 /// If possible, create a JIT, unless the caller specifically requests an
50 /// Interpreter or there's an error. If even an Interpreter cannot be created,
53 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
54 bool ForceInterpreter) {
55 ExecutionEngine *EE = 0;
57 // Unless the interpreter was explicitly selected, make a JIT.
58 if (!ForceInterpreter)
61 // If we can't make a JIT, make an interpreter instead.
64 EE = Interpreter::create(MP->materializeModule());
71 /// getPointerToGlobal - This returns the address of the specified global
72 /// value. This may involve code generation if it's a function.
74 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
75 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
76 return getPointerToFunction(F);
78 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
79 return GlobalAddress[GV];
84 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
87 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
88 switch (CE->getOpcode()) {
89 case Instruction::GetElementPtr: {
90 Result = getConstantValue(CE->getOperand(0));
91 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
93 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
95 Result.LongVal += Offset;
98 case Instruction::Cast: {
99 // We only need to handle a few cases here. Almost all casts will
100 // automatically fold, just the ones involving pointers won't.
102 Constant *Op = CE->getOperand(0);
104 // Handle cast of pointer to pointer...
105 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
106 return getConstantValue(Op);
108 // Handle a cast of pointer to any integral type...
109 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
110 return getConstantValue(Op);
112 // Handle cast of long to pointer...
113 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
114 Op->getType() == Type::ULongTy))
115 return getConstantValue(Op);
119 case Instruction::Add:
120 if (CE->getOperand(0)->getType() == Type::LongTy ||
121 CE->getOperand(0)->getType() == Type::ULongTy)
122 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
123 getConstantValue(CE->getOperand(1)).LongVal;
131 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
135 switch (C->getType()->getPrimitiveID()) {
136 #define GET_CONST_VAL(TY, CLASS) \
137 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
138 GET_CONST_VAL(Bool , ConstantBool);
139 GET_CONST_VAL(UByte , ConstantUInt);
140 GET_CONST_VAL(SByte , ConstantSInt);
141 GET_CONST_VAL(UShort , ConstantUInt);
142 GET_CONST_VAL(Short , ConstantSInt);
143 GET_CONST_VAL(UInt , ConstantUInt);
144 GET_CONST_VAL(Int , ConstantSInt);
145 GET_CONST_VAL(ULong , ConstantUInt);
146 GET_CONST_VAL(Long , ConstantSInt);
147 GET_CONST_VAL(Float , ConstantFP);
148 GET_CONST_VAL(Double , ConstantFP);
150 case Type::PointerTyID:
151 if (isa<ConstantPointerNull>(C)) {
152 Result.PointerVal = 0;
153 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
155 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
156 Result = PTOGV(getPointerToFunctionOrStub(F));
158 Result = PTOGV(getOrEmitGlobalVariable(
159 cast<GlobalVariable>(CPR->getValue())));
162 assert(0 && "Unknown constant pointer type!");
166 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
174 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
176 if (getTargetData().isLittleEndian()) {
177 switch (Ty->getPrimitiveID()) {
179 case Type::UByteTyID:
180 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
181 case Type::UShortTyID:
182 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
183 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
185 Store4BytesLittleEndian:
186 case Type::FloatTyID:
188 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
189 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
190 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
191 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
193 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
194 goto Store4BytesLittleEndian;
195 case Type::DoubleTyID:
196 case Type::ULongTyID:
197 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
198 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
199 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
200 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
201 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
202 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
203 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
204 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
207 std::cout << "Cannot store value of type " << Ty << "!\n";
210 switch (Ty->getPrimitiveID()) {
212 case Type::UByteTyID:
213 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
214 case Type::UShortTyID:
215 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
216 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
218 Store4BytesBigEndian:
219 case Type::FloatTyID:
221 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
222 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
223 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
224 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
226 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
227 goto Store4BytesBigEndian;
228 case Type::DoubleTyID:
229 case Type::ULongTyID:
230 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
231 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
232 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
233 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
234 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
235 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
236 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
237 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
240 std::cout << "Cannot store value of type " << Ty << "!\n";
247 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
250 if (getTargetData().isLittleEndian()) {
251 switch (Ty->getPrimitiveID()) {
253 case Type::UByteTyID:
254 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
255 case Type::UShortTyID:
256 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
257 ((unsigned)Ptr->Untyped[1] << 8);
259 Load4BytesLittleEndian:
260 case Type::FloatTyID:
262 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
263 ((unsigned)Ptr->Untyped[1] << 8) |
264 ((unsigned)Ptr->Untyped[2] << 16) |
265 ((unsigned)Ptr->Untyped[3] << 24);
267 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
268 goto Load4BytesLittleEndian;
269 case Type::DoubleTyID:
270 case Type::ULongTyID:
271 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
272 ((uint64_t)Ptr->Untyped[1] << 8) |
273 ((uint64_t)Ptr->Untyped[2] << 16) |
274 ((uint64_t)Ptr->Untyped[3] << 24) |
275 ((uint64_t)Ptr->Untyped[4] << 32) |
276 ((uint64_t)Ptr->Untyped[5] << 40) |
277 ((uint64_t)Ptr->Untyped[6] << 48) |
278 ((uint64_t)Ptr->Untyped[7] << 56);
281 std::cout << "Cannot load value of type " << *Ty << "!\n";
285 switch (Ty->getPrimitiveID()) {
287 case Type::UByteTyID:
288 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
289 case Type::UShortTyID:
290 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
291 ((unsigned)Ptr->Untyped[0] << 8);
294 case Type::FloatTyID:
296 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
297 ((unsigned)Ptr->Untyped[2] << 8) |
298 ((unsigned)Ptr->Untyped[1] << 16) |
299 ((unsigned)Ptr->Untyped[0] << 24);
301 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
302 goto Load4BytesBigEndian;
303 case Type::DoubleTyID:
304 case Type::ULongTyID:
305 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
306 ((uint64_t)Ptr->Untyped[6] << 8) |
307 ((uint64_t)Ptr->Untyped[5] << 16) |
308 ((uint64_t)Ptr->Untyped[4] << 24) |
309 ((uint64_t)Ptr->Untyped[3] << 32) |
310 ((uint64_t)Ptr->Untyped[2] << 40) |
311 ((uint64_t)Ptr->Untyped[1] << 48) |
312 ((uint64_t)Ptr->Untyped[0] << 56);
315 std::cout << "Cannot load value of type " << *Ty << "!\n";
322 // InitializeMemory - Recursive function to apply a Constant value into the
323 // specified memory location...
325 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
326 if (Init->getType()->isFirstClassType()) {
327 GenericValue Val = getConstantValue(Init);
328 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
332 switch (Init->getType()->getPrimitiveID()) {
333 case Type::ArrayTyID: {
334 const ConstantArray *CPA = cast<ConstantArray>(Init);
335 const std::vector<Use> &Val = CPA->getValues();
336 unsigned ElementSize =
337 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
338 for (unsigned i = 0; i < Val.size(); ++i)
339 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
343 case Type::StructTyID: {
344 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
345 const StructLayout *SL =
346 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
347 const std::vector<Use> &Val = CPS->getValues();
348 for (unsigned i = 0; i < Val.size(); ++i)
349 InitializeMemory(cast<Constant>(Val[i].get()),
350 (char*)Addr+SL->MemberOffsets[i]);
355 std::cerr << "Bad Type: " << Init->getType() << "\n";
356 assert(0 && "Unknown constant type to initialize memory with!");
360 /// EmitGlobals - Emit all of the global variables to memory, storing their
361 /// addresses into GlobalAddress. This must make sure to copy the contents of
362 /// their initializers into the memory.
364 void ExecutionEngine::emitGlobals() {
365 const TargetData &TD = getTargetData();
367 // Loop over all of the global variables in the program, allocating the memory
369 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
371 if (!I->isExternal()) {
372 // Get the type of the global...
373 const Type *Ty = I->getType()->getElementType();
375 // Allocate some memory for it!
376 unsigned Size = TD.getTypeSize(Ty);
377 addGlobalMapping(I, new char[Size]);
379 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
380 << (void*)GlobalAddress[I] << "\n");
382 // External variable reference. Try to use the dynamic loader to
383 // get a pointer to it.
384 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
385 GlobalAddress[I] = SymAddr;
387 std::cerr << "Could not resolve external global address: "
388 << I->getName() << "\n";
393 // Now that all of the globals are set up in memory, loop through them all and
394 // initialize their contents.
395 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
397 if (!I->isExternal())
398 EmitGlobalVariable(I);
401 // EmitGlobalVariable - This method emits the specified global variable to the
402 // address specified in GlobalAddresses, or allocates new memory if it's not
403 // already in the map.
404 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
405 void *&GA = GlobalAddress[GV];
406 const Type *ElTy = GV->getType()->getElementType();
408 // If it's not already specified, allocate memory for the global.
409 GA = new char[getTargetData().getTypeSize(ElTy)];
412 InitializeMemory(GV->getInitializer(), GA);
413 NumInitBytes += getTargetData().getTypeSize(ElTy);