blob: 8dc1d5a84baa1879f41f21f78d01d687d054f469 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EE's --------===//
2//
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 "Support/Debug.h"
10#include "Support/Statistic.h"
11#include "llvm/ExecutionEngine/ExecutionEngine.h"
12#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000013#include "llvm/DerivedTypes.h"
14#include "llvm/Constants.h"
15#include "llvm/Module.h"
16#include "llvm/Target/TargetData.h"
John Criswell7a73b802003-06-30 21:59:07 +000017#include "Config/dlfcn.h"
Brian Gaeke82d82772003-09-03 20:34:19 +000018#include "JIT/VM.h"
19#include "Interpreter/Interpreter.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000020
21Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
22
Brian Gaeke8e539482003-09-04 22:57:27 +000023ExecutionEngine::~ExecutionEngine() {
24 delete &CurMod;
25}
26
Brian Gaeke82d82772003-09-03 20:34:19 +000027ExecutionEngine *ExecutionEngine::create (Module *M, bool ForceInterpreter,
Brian Gaekef58815e2003-09-04 22:21:24 +000028 bool TraceMode) {
Brian Gaeke82d82772003-09-03 20:34:19 +000029 ExecutionEngine *EE = 0;
30
31 // If there is nothing that is forcing us to use the interpreter, make a JIT.
Brian Gaekef58815e2003-09-04 22:21:24 +000032 if (!ForceInterpreter && !TraceMode)
Brian Gaeke82d82772003-09-03 20:34:19 +000033 EE = VM::create(M);
34
35 // If we can't make a JIT, make an interpreter instead.
36 if (EE == 0)
Brian Gaekef58815e2003-09-04 22:21:24 +000037 EE = Interpreter::create(M, TraceMode);
Brian Gaeke82d82772003-09-03 20:34:19 +000038 return EE;
39}
40
Chris Lattnerbd199fb2002-12-24 00:01:05 +000041// getPointerToGlobal - This returns the address of the specified global
42// value. This may involve code generation if it's a function.
43//
44void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000045 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000046 return getPointerToFunction(F);
47
48 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
49 return GlobalAddress[GV];
50}
51
Chris Lattnerbd199fb2002-12-24 00:01:05 +000052GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
53 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000054
Chris Lattner9a231222003-05-14 17:51:49 +000055 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000056 switch (CE->getOpcode()) {
57 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000058 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000059 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
60 uint64_t Offset =
61 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
62
63 Result.LongVal += Offset;
64 return Result;
65 }
Chris Lattner9a231222003-05-14 17:51:49 +000066 case Instruction::Cast: {
67 // We only need to handle a few cases here. Almost all casts will
68 // automatically fold, just the ones involving pointers won't.
69 //
70 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000071
Chris Lattner9a231222003-05-14 17:51:49 +000072 // Handle cast of pointer to pointer...
73 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
74 return getConstantValue(Op);
75
Chris Lattner74cf8192003-08-18 17:23:40 +000076 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +000077 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +000078 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +000079
80 // Handle cast of long to pointer...
81 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
82 Op->getType() == Type::ULongTy))
83 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +000084 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000085 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000086
Chris Lattner9a231222003-05-14 17:51:49 +000087 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +000088 if (CE->getOperand(0)->getType() == Type::LongTy ||
89 CE->getOperand(0)->getType() == Type::ULongTy)
90 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
91 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +000092 else
93 break;
94 return Result;
95
96 default:
97 break;
98 }
99 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
100 abort();
101 }
102
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000103 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000104#define GET_CONST_VAL(TY, CLASS) \
105 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000106 GET_CONST_VAL(Bool , ConstantBool);
107 GET_CONST_VAL(UByte , ConstantUInt);
108 GET_CONST_VAL(SByte , ConstantSInt);
109 GET_CONST_VAL(UShort , ConstantUInt);
110 GET_CONST_VAL(Short , ConstantSInt);
111 GET_CONST_VAL(UInt , ConstantUInt);
112 GET_CONST_VAL(Int , ConstantSInt);
113 GET_CONST_VAL(ULong , ConstantUInt);
114 GET_CONST_VAL(Long , ConstantSInt);
115 GET_CONST_VAL(Float , ConstantFP);
116 GET_CONST_VAL(Double , ConstantFP);
117#undef GET_CONST_VAL
118 case Type::PointerTyID:
119 if (isa<ConstantPointerNull>(C)) {
120 Result.PointerVal = 0;
121 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
122 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
123
124 } else {
125 assert(0 && "Unknown constant pointer type!");
126 }
127 break;
128 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000129 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000130 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000131 }
132 return Result;
133}
134
135void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
136 const Type *Ty) {
137 if (getTargetData().isLittleEndian()) {
138 switch (Ty->getPrimitiveID()) {
139 case Type::BoolTyID:
140 case Type::UByteTyID:
141 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
142 case Type::UShortTyID:
143 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
144 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
145 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000146 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000147 case Type::FloatTyID:
148 case Type::UIntTyID:
149 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
150 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
151 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
152 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
153 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000154 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000155 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000156 case Type::DoubleTyID:
157 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000158 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000159 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
160 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
161 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
162 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
163 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
164 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
165 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
166 break;
167 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000168 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000169 }
170 } else {
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[1] = Val.UShortVal & 255;
177 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
178 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000179 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000180 case Type::FloatTyID:
181 case Type::UIntTyID:
182 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
183 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
184 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
185 Ptr->Untyped[0] = (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 Store4BytesBigEndian;
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[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000192 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
193 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
194 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
195 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
196 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
197 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
198 Ptr->Untyped[0] = (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 }
204}
205
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000206GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
207 const Type *Ty) {
208 GenericValue Result;
209 if (getTargetData().isLittleEndian()) {
210 switch (Ty->getPrimitiveID()) {
211 case Type::BoolTyID:
212 case Type::UByteTyID:
213 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
214 case Type::UShortTyID:
215 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
216 ((unsigned)Ptr->Untyped[1] << 8);
217 break;
218 Load4BytesLittleEndian:
219 case Type::FloatTyID:
220 case Type::UIntTyID:
221 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
222 ((unsigned)Ptr->Untyped[1] << 8) |
223 ((unsigned)Ptr->Untyped[2] << 16) |
224 ((unsigned)Ptr->Untyped[3] << 24);
225 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000226 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000227 goto Load4BytesLittleEndian;
228 case Type::DoubleTyID:
229 case Type::ULongTyID:
230 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
231 ((uint64_t)Ptr->Untyped[1] << 8) |
232 ((uint64_t)Ptr->Untyped[2] << 16) |
233 ((uint64_t)Ptr->Untyped[3] << 24) |
234 ((uint64_t)Ptr->Untyped[4] << 32) |
235 ((uint64_t)Ptr->Untyped[5] << 40) |
236 ((uint64_t)Ptr->Untyped[6] << 48) |
237 ((uint64_t)Ptr->Untyped[7] << 56);
238 break;
239 default:
240 std::cout << "Cannot load value of type " << *Ty << "!\n";
241 abort();
242 }
243 } else {
244 switch (Ty->getPrimitiveID()) {
245 case Type::BoolTyID:
246 case Type::UByteTyID:
247 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
248 case Type::UShortTyID:
249 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
250 ((unsigned)Ptr->Untyped[0] << 8);
251 break;
252 Load4BytesBigEndian:
253 case Type::FloatTyID:
254 case Type::UIntTyID:
255 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
256 ((unsigned)Ptr->Untyped[2] << 8) |
257 ((unsigned)Ptr->Untyped[1] << 16) |
258 ((unsigned)Ptr->Untyped[0] << 24);
259 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000260 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000261 goto Load4BytesBigEndian;
262 case Type::DoubleTyID:
263 case Type::ULongTyID:
264 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
265 ((uint64_t)Ptr->Untyped[6] << 8) |
266 ((uint64_t)Ptr->Untyped[5] << 16) |
267 ((uint64_t)Ptr->Untyped[4] << 24) |
268 ((uint64_t)Ptr->Untyped[3] << 32) |
269 ((uint64_t)Ptr->Untyped[2] << 40) |
270 ((uint64_t)Ptr->Untyped[1] << 48) |
271 ((uint64_t)Ptr->Untyped[0] << 56);
272 break;
273 default:
274 std::cout << "Cannot load value of type " << *Ty << "!\n";
275 abort();
276 }
277 }
278 return Result;
279}
280
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000281// InitializeMemory - Recursive function to apply a Constant value into the
282// specified memory location...
283//
284void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
285 if (Init->getType()->isFirstClassType()) {
286 GenericValue Val = getConstantValue(Init);
287 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
288 return;
289 }
290
291 switch (Init->getType()->getPrimitiveID()) {
292 case Type::ArrayTyID: {
293 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000294 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000295 unsigned ElementSize =
296 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
297 for (unsigned i = 0; i < Val.size(); ++i)
298 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
299 return;
300 }
301
302 case Type::StructTyID: {
303 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
304 const StructLayout *SL =
305 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000306 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000307 for (unsigned i = 0; i < Val.size(); ++i)
308 InitializeMemory(cast<Constant>(Val[i].get()),
309 (char*)Addr+SL->MemberOffsets[i]);
310 return;
311 }
312
313 default:
314 std::cerr << "Bad Type: " << Init->getType() << "\n";
315 assert(0 && "Unknown constant type to initialize memory with!");
316 }
317}
318
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000319/// EmitGlobals - Emit all of the global variables to memory, storing their
320/// addresses into GlobalAddress. This must make sure to copy the contents of
321/// their initializers into the memory.
322///
323void ExecutionEngine::emitGlobals() {
324 const TargetData &TD = getTargetData();
325
326 // Loop over all of the global variables in the program, allocating the memory
327 // to hold them.
328 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
329 I != E; ++I)
330 if (!I->isExternal()) {
331 // Get the type of the global...
332 const Type *Ty = I->getType()->getElementType();
333
334 // Allocate some memory for it!
335 unsigned Size = TD.getTypeSize(Ty);
336 GlobalAddress[I] = new char[Size];
337 NumInitBytes += Size;
338
339 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
340 << (void*)GlobalAddress[I] << "\n");
341 } else {
Misha Brukman06dabfa2003-07-18 18:33:38 +0000342 // On Sparc, RTLD_SELF is already defined and it's not zero
Misha Brukman859e09f2003-07-15 15:58:26 +0000343 // Linux/x86 wants to use a 0, other systems may differ
Misha Brukman06dabfa2003-07-18 18:33:38 +0000344#ifndef RTLD_SELF
Misha Brukman859e09f2003-07-15 15:58:26 +0000345#define RTLD_SELF 0
346#endif
Misha Brukman06dabfa2003-07-18 18:33:38 +0000347 // External variable reference, try to use dlsym to get a pointer to it in
348 // the LLI image.
Misha Brukman291c2c52003-07-15 15:55:32 +0000349 if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000350 GlobalAddress[I] = SymAddr;
351 else {
352 std::cerr << "Could not resolve external global address: "
353 << I->getName() << "\n";
354 abort();
355 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000356 }
357
358 // Now that all of the globals are set up in memory, loop through them all and
359 // initialize their contents.
360 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
361 I != E; ++I)
362 if (!I->isExternal())
363 InitializeMemory(I->getInitializer(), GlobalAddress[I]);
364}
365