blob: 2681790e56aadef00be51e1d64d7342e8a7c5139 [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//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "Interpreter/Interpreter.h"
Misha Brukman19684162003-10-16 21:18:05 +000017#include "JIT/VM.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ModuleProvider.h"
22#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000025#include "Support/Debug.h"
26#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000027#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000028#include "Config/dlfcn.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000031namespace {
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
33}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034
Misha Brukman19684162003-10-16 21:18:05 +000035ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
36 CurMod(*P->getModule()), MP(P) {
37 assert(P && "ModuleProvider is null?");
38}
39
40ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000041 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000042}
43
Brian Gaeke8e539482003-09-04 22:57:27 +000044ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000045 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000046}
47
Misha Brukman19684162003-10-16 21:18:05 +000048/// If possible, create a JIT, unless the caller specifically requests an
49/// Interpreter or there's an error. If even an Interpreter cannot be created,
50/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +000051///
Misha Brukman7b2b40f2003-10-14 21:36:31 +000052ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Brian Gaeke20a277e2003-10-24 19:58:38 +000053 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +000054 ExecutionEngine *EE = 0;
55
Brian Gaeke20a277e2003-10-24 19:58:38 +000056 // Unless the interpreter was explicitly selected, make a JIT.
57 if (!ForceInterpreter)
Misha Brukman7b2b40f2003-10-14 21:36:31 +000058 EE = VM::create(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +000059
60 // If we can't make a JIT, make an interpreter instead.
Misha Brukman19684162003-10-16 21:18:05 +000061 try {
62 if (EE == 0)
Brian Gaeke20a277e2003-10-24 19:58:38 +000063 EE = Interpreter::create(MP->materializeModule());
Misha Brukman19684162003-10-16 21:18:05 +000064 } catch (...) {
65 EE = 0;
66 }
Brian Gaeke82d82772003-09-03 20:34:19 +000067 return EE;
68}
69
Misha Brukman4afac182003-10-10 17:45:12 +000070/// getPointerToGlobal - This returns the address of the specified global
71/// value. This may involve code generation if it's a function.
72///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000073void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000074 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000075 return getPointerToFunction(F);
76
77 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
78 return GlobalAddress[GV];
79}
80
Misha Brukman4afac182003-10-10 17:45:12 +000081/// FIXME: document
82///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000083GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
84 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000085
Chris Lattner9a231222003-05-14 17:51:49 +000086 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000087 switch (CE->getOpcode()) {
88 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000089 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000090 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
91 uint64_t Offset =
92 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
93
94 Result.LongVal += Offset;
95 return Result;
96 }
Chris Lattner9a231222003-05-14 17:51:49 +000097 case Instruction::Cast: {
98 // We only need to handle a few cases here. Almost all casts will
99 // automatically fold, just the ones involving pointers won't.
100 //
101 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000102
Chris Lattner9a231222003-05-14 17:51:49 +0000103 // Handle cast of pointer to pointer...
104 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
105 return getConstantValue(Op);
106
Chris Lattner74cf8192003-08-18 17:23:40 +0000107 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000108 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +0000109 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +0000110
111 // Handle cast of long to pointer...
112 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
113 Op->getType() == Type::ULongTy))
114 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +0000115 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000116 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000117
Chris Lattner9a231222003-05-14 17:51:49 +0000118 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000119 if (CE->getOperand(0)->getType() == Type::LongTy ||
120 CE->getOperand(0)->getType() == Type::ULongTy)
121 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
122 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000123 else
124 break;
125 return Result;
126
127 default:
128 break;
129 }
130 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
131 abort();
132 }
133
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000134 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000135#define GET_CONST_VAL(TY, CLASS) \
136 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000137 GET_CONST_VAL(Bool , ConstantBool);
138 GET_CONST_VAL(UByte , ConstantUInt);
139 GET_CONST_VAL(SByte , ConstantSInt);
140 GET_CONST_VAL(UShort , ConstantUInt);
141 GET_CONST_VAL(Short , ConstantSInt);
142 GET_CONST_VAL(UInt , ConstantUInt);
143 GET_CONST_VAL(Int , ConstantSInt);
144 GET_CONST_VAL(ULong , ConstantUInt);
145 GET_CONST_VAL(Long , ConstantSInt);
146 GET_CONST_VAL(Float , ConstantFP);
147 GET_CONST_VAL(Double , ConstantFP);
148#undef GET_CONST_VAL
149 case Type::PointerTyID:
150 if (isa<ConstantPointerNull>(C)) {
151 Result.PointerVal = 0;
152 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
153 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
154
155 } else {
156 assert(0 && "Unknown constant pointer type!");
157 }
158 break;
159 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000160 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000161 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000162 }
163 return Result;
164}
165
Misha Brukman4afac182003-10-10 17:45:12 +0000166/// FIXME: document
167///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000168void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000169 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000170 if (getTargetData().isLittleEndian()) {
171 switch (Ty->getPrimitiveID()) {
172 case Type::BoolTyID:
173 case Type::UByteTyID:
174 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
175 case Type::UShortTyID:
176 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
177 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
178 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000179 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000180 case Type::FloatTyID:
181 case Type::UIntTyID:
182 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
183 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
184 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
185 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
186 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000187 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000188 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000189 case Type::DoubleTyID:
190 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000191 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000192 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
193 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
194 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
195 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
196 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
197 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
198 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
199 break;
200 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000201 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000202 }
203 } else {
204 switch (Ty->getPrimitiveID()) {
205 case Type::BoolTyID:
206 case Type::UByteTyID:
207 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
208 case Type::UShortTyID:
209 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
210 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
211 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000212 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000213 case Type::FloatTyID:
214 case Type::UIntTyID:
215 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
216 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
217 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
218 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
219 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000220 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000221 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000222 case Type::DoubleTyID:
223 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000224 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000225 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
226 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
227 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
228 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
229 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
230 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
231 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
232 break;
233 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000234 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000235 }
236 }
237}
238
Misha Brukman4afac182003-10-10 17:45:12 +0000239/// FIXME: document
240///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000241GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
242 const Type *Ty) {
243 GenericValue Result;
244 if (getTargetData().isLittleEndian()) {
245 switch (Ty->getPrimitiveID()) {
246 case Type::BoolTyID:
247 case Type::UByteTyID:
248 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
249 case Type::UShortTyID:
250 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
251 ((unsigned)Ptr->Untyped[1] << 8);
252 break;
253 Load4BytesLittleEndian:
254 case Type::FloatTyID:
255 case Type::UIntTyID:
256 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
257 ((unsigned)Ptr->Untyped[1] << 8) |
258 ((unsigned)Ptr->Untyped[2] << 16) |
259 ((unsigned)Ptr->Untyped[3] << 24);
260 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000261 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000262 goto Load4BytesLittleEndian;
263 case Type::DoubleTyID:
264 case Type::ULongTyID:
265 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
266 ((uint64_t)Ptr->Untyped[1] << 8) |
267 ((uint64_t)Ptr->Untyped[2] << 16) |
268 ((uint64_t)Ptr->Untyped[3] << 24) |
269 ((uint64_t)Ptr->Untyped[4] << 32) |
270 ((uint64_t)Ptr->Untyped[5] << 40) |
271 ((uint64_t)Ptr->Untyped[6] << 48) |
272 ((uint64_t)Ptr->Untyped[7] << 56);
273 break;
274 default:
275 std::cout << "Cannot load value of type " << *Ty << "!\n";
276 abort();
277 }
278 } else {
279 switch (Ty->getPrimitiveID()) {
280 case Type::BoolTyID:
281 case Type::UByteTyID:
282 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
283 case Type::UShortTyID:
284 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
285 ((unsigned)Ptr->Untyped[0] << 8);
286 break;
287 Load4BytesBigEndian:
288 case Type::FloatTyID:
289 case Type::UIntTyID:
290 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
291 ((unsigned)Ptr->Untyped[2] << 8) |
292 ((unsigned)Ptr->Untyped[1] << 16) |
293 ((unsigned)Ptr->Untyped[0] << 24);
294 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000295 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000296 goto Load4BytesBigEndian;
297 case Type::DoubleTyID:
298 case Type::ULongTyID:
299 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
300 ((uint64_t)Ptr->Untyped[6] << 8) |
301 ((uint64_t)Ptr->Untyped[5] << 16) |
302 ((uint64_t)Ptr->Untyped[4] << 24) |
303 ((uint64_t)Ptr->Untyped[3] << 32) |
304 ((uint64_t)Ptr->Untyped[2] << 40) |
305 ((uint64_t)Ptr->Untyped[1] << 48) |
306 ((uint64_t)Ptr->Untyped[0] << 56);
307 break;
308 default:
309 std::cout << "Cannot load value of type " << *Ty << "!\n";
310 abort();
311 }
312 }
313 return Result;
314}
315
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000316// InitializeMemory - Recursive function to apply a Constant value into the
317// specified memory location...
318//
319void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
320 if (Init->getType()->isFirstClassType()) {
321 GenericValue Val = getConstantValue(Init);
322 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
323 return;
324 }
325
326 switch (Init->getType()->getPrimitiveID()) {
327 case Type::ArrayTyID: {
328 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000329 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000330 unsigned ElementSize =
331 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
332 for (unsigned i = 0; i < Val.size(); ++i)
333 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
334 return;
335 }
336
337 case Type::StructTyID: {
338 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
339 const StructLayout *SL =
340 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000341 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000342 for (unsigned i = 0; i < Val.size(); ++i)
343 InitializeMemory(cast<Constant>(Val[i].get()),
344 (char*)Addr+SL->MemberOffsets[i]);
345 return;
346 }
347
348 default:
349 std::cerr << "Bad Type: " << Init->getType() << "\n";
350 assert(0 && "Unknown constant type to initialize memory with!");
351 }
352}
353
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000354/// EmitGlobals - Emit all of the global variables to memory, storing their
355/// addresses into GlobalAddress. This must make sure to copy the contents of
356/// their initializers into the memory.
357///
358void ExecutionEngine::emitGlobals() {
359 const TargetData &TD = getTargetData();
360
361 // Loop over all of the global variables in the program, allocating the memory
362 // to hold them.
363 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
364 I != E; ++I)
365 if (!I->isExternal()) {
366 // Get the type of the global...
367 const Type *Ty = I->getType()->getElementType();
368
369 // Allocate some memory for it!
370 unsigned Size = TD.getTypeSize(Ty);
371 GlobalAddress[I] = new char[Size];
372 NumInitBytes += Size;
373
374 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Misha Brukman4afac182003-10-10 17:45:12 +0000375 << (void*)GlobalAddress[I] << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000376 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000377 // External variable reference. Try to use the dynamic loader to
378 // get a pointer to it.
379 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000380 GlobalAddress[I] = SymAddr;
381 else {
382 std::cerr << "Could not resolve external global address: "
383 << I->getName() << "\n";
384 abort();
385 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000386 }
387
388 // Now that all of the globals are set up in memory, loop through them all and
389 // initialize their contents.
390 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
391 I != E; ++I)
392 if (!I->isExternal())
393 InitializeMemory(I->getInitializer(), GlobalAddress[I]);
394}