blob: d25abfb63f7d9f770d1aa73b8a03869801ae3c92 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00002//
3// This file defines the common interface used by the various execution engine
4// subclasses.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattner3785fad2003-08-05 17:00:32 +00008#define DEBUG_TYPE "jit"
Chris Lattnerfd131292003-09-05 20:08:15 +00009#include "Interpreter/Interpreter.h"
Misha Brukman19684162003-10-16 21:18:05 +000010#include "JIT/VM.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000011#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000012#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000013#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000014#include "llvm/ModuleProvider.h"
15#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000017#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000018#include "Support/Debug.h"
19#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000020#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000021#include "Config/dlfcn.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000022
23Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
24
Misha Brukman19684162003-10-16 21:18:05 +000025ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
26 CurMod(*P->getModule()), MP(P) {
27 assert(P && "ModuleProvider is null?");
28}
29
30ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000031 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000032}
33
Brian Gaeke8e539482003-09-04 22:57:27 +000034ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000035 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000036}
37
Misha Brukman19684162003-10-16 21:18:05 +000038/// If possible, create a JIT, unless the caller specifically requests an
39/// Interpreter or there's an error. If even an Interpreter cannot be created,
40/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +000041///
Misha Brukman7b2b40f2003-10-14 21:36:31 +000042ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
43 bool ForceInterpreter,
Misha Brukman4afac182003-10-10 17:45:12 +000044 bool TraceMode) {
Brian Gaeke82d82772003-09-03 20:34:19 +000045 ExecutionEngine *EE = 0;
46
47 // If there is nothing that is forcing us to use the interpreter, make a JIT.
Brian Gaekef58815e2003-09-04 22:21:24 +000048 if (!ForceInterpreter && !TraceMode)
Misha Brukman7b2b40f2003-10-14 21:36:31 +000049 EE = VM::create(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +000050
51 // If we can't make a JIT, make an interpreter instead.
Misha Brukman19684162003-10-16 21:18:05 +000052 try {
53 if (EE == 0)
Misha Brukman05701572003-10-17 18:31:59 +000054 EE = Interpreter::create(MP->materializeModule(), TraceMode);
Misha Brukman19684162003-10-16 21:18:05 +000055 } catch (...) {
56 EE = 0;
57 }
Brian Gaeke82d82772003-09-03 20:34:19 +000058 return EE;
59}
60
Misha Brukman4afac182003-10-10 17:45:12 +000061/// getPointerToGlobal - This returns the address of the specified global
62/// value. This may involve code generation if it's a function.
63///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000064void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000065 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000066 return getPointerToFunction(F);
67
68 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
69 return GlobalAddress[GV];
70}
71
Misha Brukman4afac182003-10-10 17:45:12 +000072/// FIXME: document
73///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000074GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
75 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000076
Chris Lattner9a231222003-05-14 17:51:49 +000077 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000078 switch (CE->getOpcode()) {
79 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000080 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000081 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
82 uint64_t Offset =
83 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
84
85 Result.LongVal += Offset;
86 return Result;
87 }
Chris Lattner9a231222003-05-14 17:51:49 +000088 case Instruction::Cast: {
89 // We only need to handle a few cases here. Almost all casts will
90 // automatically fold, just the ones involving pointers won't.
91 //
92 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000093
Chris Lattner9a231222003-05-14 17:51:49 +000094 // Handle cast of pointer to pointer...
95 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
96 return getConstantValue(Op);
97
Chris Lattner74cf8192003-08-18 17:23:40 +000098 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +000099 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +0000100 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +0000101
102 // Handle cast of long to pointer...
103 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
104 Op->getType() == Type::ULongTy))
105 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +0000106 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000107 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000108
Chris Lattner9a231222003-05-14 17:51:49 +0000109 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000110 if (CE->getOperand(0)->getType() == Type::LongTy ||
111 CE->getOperand(0)->getType() == Type::ULongTy)
112 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
113 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000114 else
115 break;
116 return Result;
117
118 default:
119 break;
120 }
121 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
122 abort();
123 }
124
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000125 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000126#define GET_CONST_VAL(TY, CLASS) \
127 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000128 GET_CONST_VAL(Bool , ConstantBool);
129 GET_CONST_VAL(UByte , ConstantUInt);
130 GET_CONST_VAL(SByte , ConstantSInt);
131 GET_CONST_VAL(UShort , ConstantUInt);
132 GET_CONST_VAL(Short , ConstantSInt);
133 GET_CONST_VAL(UInt , ConstantUInt);
134 GET_CONST_VAL(Int , ConstantSInt);
135 GET_CONST_VAL(ULong , ConstantUInt);
136 GET_CONST_VAL(Long , ConstantSInt);
137 GET_CONST_VAL(Float , ConstantFP);
138 GET_CONST_VAL(Double , ConstantFP);
139#undef GET_CONST_VAL
140 case Type::PointerTyID:
141 if (isa<ConstantPointerNull>(C)) {
142 Result.PointerVal = 0;
143 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
144 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
145
146 } else {
147 assert(0 && "Unknown constant pointer type!");
148 }
149 break;
150 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000151 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000152 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000153 }
154 return Result;
155}
156
Misha Brukman4afac182003-10-10 17:45:12 +0000157/// FIXME: document
158///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000159void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000160 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000161 if (getTargetData().isLittleEndian()) {
162 switch (Ty->getPrimitiveID()) {
163 case Type::BoolTyID:
164 case Type::UByteTyID:
165 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
166 case Type::UShortTyID:
167 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
168 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
169 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000170 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000171 case Type::FloatTyID:
172 case Type::UIntTyID:
173 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
174 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
175 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
176 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
177 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000178 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000179 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000180 case Type::DoubleTyID:
181 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000182 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000183 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
184 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
185 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
186 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
187 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
188 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
189 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
190 break;
191 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000192 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000193 }
194 } else {
195 switch (Ty->getPrimitiveID()) {
196 case Type::BoolTyID:
197 case Type::UByteTyID:
198 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
199 case Type::UShortTyID:
200 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
201 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
202 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000203 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000204 case Type::FloatTyID:
205 case Type::UIntTyID:
206 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
207 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
208 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
209 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
210 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000211 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000212 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000213 case Type::DoubleTyID:
214 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000215 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000216 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
217 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
218 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
219 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
220 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
221 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
222 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
223 break;
224 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000225 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000226 }
227 }
228}
229
Misha Brukman4afac182003-10-10 17:45:12 +0000230/// FIXME: document
231///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000232GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
233 const Type *Ty) {
234 GenericValue Result;
235 if (getTargetData().isLittleEndian()) {
236 switch (Ty->getPrimitiveID()) {
237 case Type::BoolTyID:
238 case Type::UByteTyID:
239 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
240 case Type::UShortTyID:
241 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
242 ((unsigned)Ptr->Untyped[1] << 8);
243 break;
244 Load4BytesLittleEndian:
245 case Type::FloatTyID:
246 case Type::UIntTyID:
247 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
248 ((unsigned)Ptr->Untyped[1] << 8) |
249 ((unsigned)Ptr->Untyped[2] << 16) |
250 ((unsigned)Ptr->Untyped[3] << 24);
251 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000252 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000253 goto Load4BytesLittleEndian;
254 case Type::DoubleTyID:
255 case Type::ULongTyID:
256 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
257 ((uint64_t)Ptr->Untyped[1] << 8) |
258 ((uint64_t)Ptr->Untyped[2] << 16) |
259 ((uint64_t)Ptr->Untyped[3] << 24) |
260 ((uint64_t)Ptr->Untyped[4] << 32) |
261 ((uint64_t)Ptr->Untyped[5] << 40) |
262 ((uint64_t)Ptr->Untyped[6] << 48) |
263 ((uint64_t)Ptr->Untyped[7] << 56);
264 break;
265 default:
266 std::cout << "Cannot load value of type " << *Ty << "!\n";
267 abort();
268 }
269 } else {
270 switch (Ty->getPrimitiveID()) {
271 case Type::BoolTyID:
272 case Type::UByteTyID:
273 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
274 case Type::UShortTyID:
275 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
276 ((unsigned)Ptr->Untyped[0] << 8);
277 break;
278 Load4BytesBigEndian:
279 case Type::FloatTyID:
280 case Type::UIntTyID:
281 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
282 ((unsigned)Ptr->Untyped[2] << 8) |
283 ((unsigned)Ptr->Untyped[1] << 16) |
284 ((unsigned)Ptr->Untyped[0] << 24);
285 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000286 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000287 goto Load4BytesBigEndian;
288 case Type::DoubleTyID:
289 case Type::ULongTyID:
290 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
291 ((uint64_t)Ptr->Untyped[6] << 8) |
292 ((uint64_t)Ptr->Untyped[5] << 16) |
293 ((uint64_t)Ptr->Untyped[4] << 24) |
294 ((uint64_t)Ptr->Untyped[3] << 32) |
295 ((uint64_t)Ptr->Untyped[2] << 40) |
296 ((uint64_t)Ptr->Untyped[1] << 48) |
297 ((uint64_t)Ptr->Untyped[0] << 56);
298 break;
299 default:
300 std::cout << "Cannot load value of type " << *Ty << "!\n";
301 abort();
302 }
303 }
304 return Result;
305}
306
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000307// InitializeMemory - Recursive function to apply a Constant value into the
308// specified memory location...
309//
310void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
311 if (Init->getType()->isFirstClassType()) {
312 GenericValue Val = getConstantValue(Init);
313 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
314 return;
315 }
316
317 switch (Init->getType()->getPrimitiveID()) {
318 case Type::ArrayTyID: {
319 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000320 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000321 unsigned ElementSize =
322 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
323 for (unsigned i = 0; i < Val.size(); ++i)
324 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
325 return;
326 }
327
328 case Type::StructTyID: {
329 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
330 const StructLayout *SL =
331 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000332 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000333 for (unsigned i = 0; i < Val.size(); ++i)
334 InitializeMemory(cast<Constant>(Val[i].get()),
335 (char*)Addr+SL->MemberOffsets[i]);
336 return;
337 }
338
339 default:
340 std::cerr << "Bad Type: " << Init->getType() << "\n";
341 assert(0 && "Unknown constant type to initialize memory with!");
342 }
343}
344
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000345/// EmitGlobals - Emit all of the global variables to memory, storing their
346/// addresses into GlobalAddress. This must make sure to copy the contents of
347/// their initializers into the memory.
348///
349void ExecutionEngine::emitGlobals() {
350 const TargetData &TD = getTargetData();
351
352 // Loop over all of the global variables in the program, allocating the memory
353 // to hold them.
354 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
355 I != E; ++I)
356 if (!I->isExternal()) {
357 // Get the type of the global...
358 const Type *Ty = I->getType()->getElementType();
359
360 // Allocate some memory for it!
361 unsigned Size = TD.getTypeSize(Ty);
362 GlobalAddress[I] = new char[Size];
363 NumInitBytes += Size;
364
365 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Misha Brukman4afac182003-10-10 17:45:12 +0000366 << (void*)GlobalAddress[I] << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000367 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000368 // External variable reference. Try to use the dynamic loader to
369 // get a pointer to it.
370 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000371 GlobalAddress[I] = SymAddr;
372 else {
373 std::cerr << "Could not resolve external global address: "
374 << I->getName() << "\n";
375 abort();
376 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000377 }
378
379 // Now that all of the globals are set up in memory, loop through them all and
380 // initialize their contents.
381 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
382 I != E; ++I)
383 if (!I->isExternal())
384 InitializeMemory(I->getInitializer(), GlobalAddress[I]);
385}
386