blob: 35a735d647c5025cd380c1d69502b40165f0e8f9 [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"
Brian Gaeke97222942003-09-05 19:39:22 +00009#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000010#include "JIT/VM.h"
11#include "Interpreter/Interpreter.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012#include "llvm/DerivedTypes.h"
13#include "llvm/Constants.h"
14#include "llvm/Module.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000015#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000016#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000017#include "Support/Debug.h"
18#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000019#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000020#include "Config/dlfcn.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021
22Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
23
Brian Gaeke8e539482003-09-04 22:57:27 +000024ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000025 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000026}
27
Misha Brukman4afac182003-10-10 17:45:12 +000028/// FIXME: document
29///
Misha Brukman7b2b40f2003-10-14 21:36:31 +000030ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
31 bool ForceInterpreter,
Misha Brukman4afac182003-10-10 17:45:12 +000032 bool TraceMode) {
Brian Gaeke82d82772003-09-03 20:34:19 +000033 ExecutionEngine *EE = 0;
34
35 // If there is nothing that is forcing us to use the interpreter, make a JIT.
Brian Gaekef58815e2003-09-04 22:21:24 +000036 if (!ForceInterpreter && !TraceMode)
Misha Brukman7b2b40f2003-10-14 21:36:31 +000037 EE = VM::create(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +000038
39 // If we can't make a JIT, make an interpreter instead.
40 if (EE == 0)
Misha Brukman7b2b40f2003-10-14 21:36:31 +000041 EE = Interpreter::create(MP->releaseModule(), TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +000042 return EE;
43}
44
Misha Brukman4afac182003-10-10 17:45:12 +000045/// getPointerToGlobal - This returns the address of the specified global
46/// value. This may involve code generation if it's a function.
47///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000048void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000049 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000050 return getPointerToFunction(F);
51
52 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
53 return GlobalAddress[GV];
54}
55
Misha Brukman4afac182003-10-10 17:45:12 +000056/// FIXME: document
57///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000058GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
59 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000060
Chris Lattner9a231222003-05-14 17:51:49 +000061 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000062 switch (CE->getOpcode()) {
63 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000064 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000065 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
66 uint64_t Offset =
67 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
68
69 Result.LongVal += Offset;
70 return Result;
71 }
Chris Lattner9a231222003-05-14 17:51:49 +000072 case Instruction::Cast: {
73 // We only need to handle a few cases here. Almost all casts will
74 // automatically fold, just the ones involving pointers won't.
75 //
76 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000077
Chris Lattner9a231222003-05-14 17:51:49 +000078 // Handle cast of pointer to pointer...
79 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
80 return getConstantValue(Op);
81
Chris Lattner74cf8192003-08-18 17:23:40 +000082 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +000083 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +000084 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +000085
86 // Handle cast of long to pointer...
87 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
88 Op->getType() == Type::ULongTy))
89 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +000090 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000091 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000092
Chris Lattner9a231222003-05-14 17:51:49 +000093 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +000094 if (CE->getOperand(0)->getType() == Type::LongTy ||
95 CE->getOperand(0)->getType() == Type::ULongTy)
96 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
97 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +000098 else
99 break;
100 return Result;
101
102 default:
103 break;
104 }
105 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
106 abort();
107 }
108
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000109 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000110#define GET_CONST_VAL(TY, CLASS) \
111 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000112 GET_CONST_VAL(Bool , ConstantBool);
113 GET_CONST_VAL(UByte , ConstantUInt);
114 GET_CONST_VAL(SByte , ConstantSInt);
115 GET_CONST_VAL(UShort , ConstantUInt);
116 GET_CONST_VAL(Short , ConstantSInt);
117 GET_CONST_VAL(UInt , ConstantUInt);
118 GET_CONST_VAL(Int , ConstantSInt);
119 GET_CONST_VAL(ULong , ConstantUInt);
120 GET_CONST_VAL(Long , ConstantSInt);
121 GET_CONST_VAL(Float , ConstantFP);
122 GET_CONST_VAL(Double , ConstantFP);
123#undef GET_CONST_VAL
124 case Type::PointerTyID:
125 if (isa<ConstantPointerNull>(C)) {
126 Result.PointerVal = 0;
127 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
128 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
129
130 } else {
131 assert(0 && "Unknown constant pointer type!");
132 }
133 break;
134 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000135 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000136 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000137 }
138 return Result;
139}
140
Misha Brukman4afac182003-10-10 17:45:12 +0000141/// FIXME: document
142///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000143void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000144 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000145 if (getTargetData().isLittleEndian()) {
146 switch (Ty->getPrimitiveID()) {
147 case Type::BoolTyID:
148 case Type::UByteTyID:
149 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
150 case Type::UShortTyID:
151 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
152 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
153 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000154 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000155 case Type::FloatTyID:
156 case Type::UIntTyID:
157 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
158 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
159 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
160 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
161 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000162 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000163 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000164 case Type::DoubleTyID:
165 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000166 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000167 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
168 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
169 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
170 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
171 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
172 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
173 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
174 break;
175 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000176 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000177 }
178 } else {
179 switch (Ty->getPrimitiveID()) {
180 case Type::BoolTyID:
181 case Type::UByteTyID:
182 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
183 case Type::UShortTyID:
184 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
185 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
186 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000187 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000188 case Type::FloatTyID:
189 case Type::UIntTyID:
190 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
191 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
192 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
193 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
194 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000195 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000196 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000197 case Type::DoubleTyID:
198 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000199 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000200 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
201 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
202 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
203 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
204 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
205 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
206 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
207 break;
208 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000209 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000210 }
211 }
212}
213
Misha Brukman4afac182003-10-10 17:45:12 +0000214/// FIXME: document
215///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000216GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
217 const Type *Ty) {
218 GenericValue Result;
219 if (getTargetData().isLittleEndian()) {
220 switch (Ty->getPrimitiveID()) {
221 case Type::BoolTyID:
222 case Type::UByteTyID:
223 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
224 case Type::UShortTyID:
225 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
226 ((unsigned)Ptr->Untyped[1] << 8);
227 break;
228 Load4BytesLittleEndian:
229 case Type::FloatTyID:
230 case Type::UIntTyID:
231 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
232 ((unsigned)Ptr->Untyped[1] << 8) |
233 ((unsigned)Ptr->Untyped[2] << 16) |
234 ((unsigned)Ptr->Untyped[3] << 24);
235 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000236 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000237 goto Load4BytesLittleEndian;
238 case Type::DoubleTyID:
239 case Type::ULongTyID:
240 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
241 ((uint64_t)Ptr->Untyped[1] << 8) |
242 ((uint64_t)Ptr->Untyped[2] << 16) |
243 ((uint64_t)Ptr->Untyped[3] << 24) |
244 ((uint64_t)Ptr->Untyped[4] << 32) |
245 ((uint64_t)Ptr->Untyped[5] << 40) |
246 ((uint64_t)Ptr->Untyped[6] << 48) |
247 ((uint64_t)Ptr->Untyped[7] << 56);
248 break;
249 default:
250 std::cout << "Cannot load value of type " << *Ty << "!\n";
251 abort();
252 }
253 } else {
254 switch (Ty->getPrimitiveID()) {
255 case Type::BoolTyID:
256 case Type::UByteTyID:
257 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
258 case Type::UShortTyID:
259 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
260 ((unsigned)Ptr->Untyped[0] << 8);
261 break;
262 Load4BytesBigEndian:
263 case Type::FloatTyID:
264 case Type::UIntTyID:
265 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
266 ((unsigned)Ptr->Untyped[2] << 8) |
267 ((unsigned)Ptr->Untyped[1] << 16) |
268 ((unsigned)Ptr->Untyped[0] << 24);
269 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000270 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000271 goto Load4BytesBigEndian;
272 case Type::DoubleTyID:
273 case Type::ULongTyID:
274 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
275 ((uint64_t)Ptr->Untyped[6] << 8) |
276 ((uint64_t)Ptr->Untyped[5] << 16) |
277 ((uint64_t)Ptr->Untyped[4] << 24) |
278 ((uint64_t)Ptr->Untyped[3] << 32) |
279 ((uint64_t)Ptr->Untyped[2] << 40) |
280 ((uint64_t)Ptr->Untyped[1] << 48) |
281 ((uint64_t)Ptr->Untyped[0] << 56);
282 break;
283 default:
284 std::cout << "Cannot load value of type " << *Ty << "!\n";
285 abort();
286 }
287 }
288 return Result;
289}
290
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000291// InitializeMemory - Recursive function to apply a Constant value into the
292// specified memory location...
293//
294void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
295 if (Init->getType()->isFirstClassType()) {
296 GenericValue Val = getConstantValue(Init);
297 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
298 return;
299 }
300
301 switch (Init->getType()->getPrimitiveID()) {
302 case Type::ArrayTyID: {
303 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000304 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000305 unsigned ElementSize =
306 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
307 for (unsigned i = 0; i < Val.size(); ++i)
308 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
309 return;
310 }
311
312 case Type::StructTyID: {
313 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
314 const StructLayout *SL =
315 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000316 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000317 for (unsigned i = 0; i < Val.size(); ++i)
318 InitializeMemory(cast<Constant>(Val[i].get()),
319 (char*)Addr+SL->MemberOffsets[i]);
320 return;
321 }
322
323 default:
324 std::cerr << "Bad Type: " << Init->getType() << "\n";
325 assert(0 && "Unknown constant type to initialize memory with!");
326 }
327}
328
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000329/// EmitGlobals - Emit all of the global variables to memory, storing their
330/// addresses into GlobalAddress. This must make sure to copy the contents of
331/// their initializers into the memory.
332///
333void ExecutionEngine::emitGlobals() {
334 const TargetData &TD = getTargetData();
335
336 // Loop over all of the global variables in the program, allocating the memory
337 // to hold them.
338 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
339 I != E; ++I)
340 if (!I->isExternal()) {
341 // Get the type of the global...
342 const Type *Ty = I->getType()->getElementType();
343
344 // Allocate some memory for it!
345 unsigned Size = TD.getTypeSize(Ty);
346 GlobalAddress[I] = new char[Size];
347 NumInitBytes += Size;
348
349 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Misha Brukman4afac182003-10-10 17:45:12 +0000350 << (void*)GlobalAddress[I] << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000351 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000352 // External variable reference. Try to use the dynamic loader to
353 // get a pointer to it.
354 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000355 GlobalAddress[I] = SymAddr;
356 else {
357 std::cerr << "Could not resolve external global address: "
358 << I->getName() << "\n";
359 abort();
360 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000361 }
362
363 // Now that all of the globals are set up in memory, loop through them all and
364 // initialize their contents.
365 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
366 I != E; ++I)
367 if (!I->isExternal())
368 InitializeMemory(I->getInitializer(), GlobalAddress[I]);
369}
370