blob: 9b51e3c798fb6c9ac744f97a7890a22ec82f476a [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"
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009#include "ExecutionEngine.h"
10#include "GenericValue.h"
11#include "llvm/DerivedTypes.h"
12#include "llvm/Constants.h"
13#include "llvm/Module.h"
14#include "llvm/Target/TargetData.h"
Chris Lattnerc648dab2003-08-01 22:13:59 +000015#include "Support/Debug.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000016#include "Support/Statistic.h"
John Criswell7a73b802003-06-30 21:59:07 +000017#include "Config/dlfcn.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018
19Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
20
21// getPointerToGlobal - This returns the address of the specified global
22// value. This may involve code generation if it's a function.
23//
24void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000025 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000026 return getPointerToFunction(F);
27
28 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
29 return GlobalAddress[GV];
30}
31
32
33GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
34 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000035
Chris Lattner9a231222003-05-14 17:51:49 +000036 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000037 switch (CE->getOpcode()) {
38 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000039 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000040 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
41 uint64_t Offset =
42 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
43
44 Result.LongVal += Offset;
45 return Result;
46 }
Chris Lattner9a231222003-05-14 17:51:49 +000047 case Instruction::Cast: {
48 // We only need to handle a few cases here. Almost all casts will
49 // automatically fold, just the ones involving pointers won't.
50 //
51 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000052
Chris Lattner9a231222003-05-14 17:51:49 +000053 // Handle cast of pointer to pointer...
54 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
55 return getConstantValue(Op);
56
Chris Lattner74cf8192003-08-18 17:23:40 +000057 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +000058 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +000059 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +000060
61 // Handle cast of long to pointer...
62 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
63 Op->getType() == Type::ULongTy))
64 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +000065 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000066 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000067
Chris Lattner9a231222003-05-14 17:51:49 +000068 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +000069 if (CE->getOperand(0)->getType() == Type::LongTy ||
70 CE->getOperand(0)->getType() == Type::ULongTy)
71 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
72 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +000073 else
74 break;
75 return Result;
76
77 default:
78 break;
79 }
80 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
81 abort();
82 }
83
Chris Lattnerbd199fb2002-12-24 00:01:05 +000084 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000085#define GET_CONST_VAL(TY, CLASS) \
86 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +000087 GET_CONST_VAL(Bool , ConstantBool);
88 GET_CONST_VAL(UByte , ConstantUInt);
89 GET_CONST_VAL(SByte , ConstantSInt);
90 GET_CONST_VAL(UShort , ConstantUInt);
91 GET_CONST_VAL(Short , ConstantSInt);
92 GET_CONST_VAL(UInt , ConstantUInt);
93 GET_CONST_VAL(Int , ConstantSInt);
94 GET_CONST_VAL(ULong , ConstantUInt);
95 GET_CONST_VAL(Long , ConstantSInt);
96 GET_CONST_VAL(Float , ConstantFP);
97 GET_CONST_VAL(Double , ConstantFP);
98#undef GET_CONST_VAL
99 case Type::PointerTyID:
100 if (isa<ConstantPointerNull>(C)) {
101 Result.PointerVal = 0;
102 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
103 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
104
105 } else {
106 assert(0 && "Unknown constant pointer type!");
107 }
108 break;
109 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000110 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000111 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000112 }
113 return Result;
114}
115
116void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
117 const Type *Ty) {
118 if (getTargetData().isLittleEndian()) {
119 switch (Ty->getPrimitiveID()) {
120 case Type::BoolTyID:
121 case Type::UByteTyID:
122 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
123 case Type::UShortTyID:
124 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
125 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
126 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000127 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000128 case Type::FloatTyID:
129 case Type::UIntTyID:
130 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
131 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
132 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
133 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
134 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000135 case Type::PointerTyID: if (CurMod.has32BitPointers())
136 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000137 case Type::DoubleTyID:
138 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000139 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000140 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
141 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
142 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
143 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
144 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
145 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
146 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
147 break;
148 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000149 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000150 }
151 } else {
152 switch (Ty->getPrimitiveID()) {
153 case Type::BoolTyID:
154 case Type::UByteTyID:
155 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
156 case Type::UShortTyID:
157 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
158 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
159 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000160 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000161 case Type::FloatTyID:
162 case Type::UIntTyID:
163 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
164 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
165 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
166 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
167 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000168 case Type::PointerTyID: if (CurMod.has32BitPointers())
169 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000170 case Type::DoubleTyID:
171 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000172 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000173 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
174 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
175 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
176 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
177 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
178 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
179 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
180 break;
181 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000182 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000183 }
184 }
185}
186
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000187GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
188 const Type *Ty) {
189 GenericValue Result;
190 if (getTargetData().isLittleEndian()) {
191 switch (Ty->getPrimitiveID()) {
192 case Type::BoolTyID:
193 case Type::UByteTyID:
194 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
195 case Type::UShortTyID:
196 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
197 ((unsigned)Ptr->Untyped[1] << 8);
198 break;
199 Load4BytesLittleEndian:
200 case Type::FloatTyID:
201 case Type::UIntTyID:
202 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
203 ((unsigned)Ptr->Untyped[1] << 8) |
204 ((unsigned)Ptr->Untyped[2] << 16) |
205 ((unsigned)Ptr->Untyped[3] << 24);
206 break;
207 case Type::PointerTyID: if (getModule().has32BitPointers())
208 goto Load4BytesLittleEndian;
209 case Type::DoubleTyID:
210 case Type::ULongTyID:
211 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
212 ((uint64_t)Ptr->Untyped[1] << 8) |
213 ((uint64_t)Ptr->Untyped[2] << 16) |
214 ((uint64_t)Ptr->Untyped[3] << 24) |
215 ((uint64_t)Ptr->Untyped[4] << 32) |
216 ((uint64_t)Ptr->Untyped[5] << 40) |
217 ((uint64_t)Ptr->Untyped[6] << 48) |
218 ((uint64_t)Ptr->Untyped[7] << 56);
219 break;
220 default:
221 std::cout << "Cannot load value of type " << *Ty << "!\n";
222 abort();
223 }
224 } else {
225 switch (Ty->getPrimitiveID()) {
226 case Type::BoolTyID:
227 case Type::UByteTyID:
228 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
229 case Type::UShortTyID:
230 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
231 ((unsigned)Ptr->Untyped[0] << 8);
232 break;
233 Load4BytesBigEndian:
234 case Type::FloatTyID:
235 case Type::UIntTyID:
236 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
237 ((unsigned)Ptr->Untyped[2] << 8) |
238 ((unsigned)Ptr->Untyped[1] << 16) |
239 ((unsigned)Ptr->Untyped[0] << 24);
240 break;
241 case Type::PointerTyID: if (getModule().has32BitPointers())
242 goto Load4BytesBigEndian;
243 case Type::DoubleTyID:
244 case Type::ULongTyID:
245 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
246 ((uint64_t)Ptr->Untyped[6] << 8) |
247 ((uint64_t)Ptr->Untyped[5] << 16) |
248 ((uint64_t)Ptr->Untyped[4] << 24) |
249 ((uint64_t)Ptr->Untyped[3] << 32) |
250 ((uint64_t)Ptr->Untyped[2] << 40) |
251 ((uint64_t)Ptr->Untyped[1] << 48) |
252 ((uint64_t)Ptr->Untyped[0] << 56);
253 break;
254 default:
255 std::cout << "Cannot load value of type " << *Ty << "!\n";
256 abort();
257 }
258 }
259 return Result;
260}
261
262
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000263// InitializeMemory - Recursive function to apply a Constant value into the
264// specified memory location...
265//
266void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
267 if (Init->getType()->isFirstClassType()) {
268 GenericValue Val = getConstantValue(Init);
269 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
270 return;
271 }
272
273 switch (Init->getType()->getPrimitiveID()) {
274 case Type::ArrayTyID: {
275 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000276 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000277 unsigned ElementSize =
278 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
279 for (unsigned i = 0; i < Val.size(); ++i)
280 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
281 return;
282 }
283
284 case Type::StructTyID: {
285 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
286 const StructLayout *SL =
287 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000288 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000289 for (unsigned i = 0; i < Val.size(); ++i)
290 InitializeMemory(cast<Constant>(Val[i].get()),
291 (char*)Addr+SL->MemberOffsets[i]);
292 return;
293 }
294
295 default:
296 std::cerr << "Bad Type: " << Init->getType() << "\n";
297 assert(0 && "Unknown constant type to initialize memory with!");
298 }
299}
300
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000301void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000302 if (getTargetData().getPointerSize() == 8) { // 64 bit target?
303 PointerTy *Result = new PointerTy[InputArgv.size()+1];
304 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
Chris Lattnerc648dab2003-08-01 22:13:59 +0000305
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000306 for (unsigned i = 0; i < InputArgv.size(); ++i) {
307 unsigned Size = InputArgv[i].size()+1;
308 char *Dest = new char[Size];
309 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
310
311 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
312 Dest[Size-1] = 0;
313
314 // Endian safe: Result[i] = (PointerTy)Dest;
315 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy);
316 }
317 Result[InputArgv.size()] = 0;
318 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000319
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000320 } else { // 32 bit target?
321 int *Result = new int[InputArgv.size()+1];
322 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000323
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000324 for (unsigned i = 0; i < InputArgv.size(); ++i) {
325 unsigned Size = InputArgv[i].size()+1;
326 char *Dest = new char[Size];
327 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
328
329 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
330 Dest[Size-1] = 0;
331
332 // Endian safe: Result[i] = (PointerTy)Dest;
333 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy);
334 }
335 Result[InputArgv.size()] = 0; // null terminate it
336 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000337 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000338}
339
340/// EmitGlobals - Emit all of the global variables to memory, storing their
341/// addresses into GlobalAddress. This must make sure to copy the contents of
342/// their initializers into the memory.
343///
344void ExecutionEngine::emitGlobals() {
345 const TargetData &TD = getTargetData();
346
347 // Loop over all of the global variables in the program, allocating the memory
348 // to hold them.
349 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
350 I != E; ++I)
351 if (!I->isExternal()) {
352 // Get the type of the global...
353 const Type *Ty = I->getType()->getElementType();
354
355 // Allocate some memory for it!
356 unsigned Size = TD.getTypeSize(Ty);
357 GlobalAddress[I] = new char[Size];
358 NumInitBytes += Size;
359
360 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
361 << (void*)GlobalAddress[I] << "\n");
362 } else {
Misha Brukman06dabfa2003-07-18 18:33:38 +0000363 // On Sparc, RTLD_SELF is already defined and it's not zero
Misha Brukman859e09f2003-07-15 15:58:26 +0000364 // Linux/x86 wants to use a 0, other systems may differ
Misha Brukman06dabfa2003-07-18 18:33:38 +0000365#ifndef RTLD_SELF
Misha Brukman859e09f2003-07-15 15:58:26 +0000366#define RTLD_SELF 0
367#endif
Misha Brukman06dabfa2003-07-18 18:33:38 +0000368 // External variable reference, try to use dlsym to get a pointer to it in
369 // the LLI image.
Misha Brukman291c2c52003-07-15 15:55:32 +0000370 if (void *SymAddr = dlsym(RTLD_SELF, 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