blob: e37d56f2cca0b5334070ba090fad778c8166f1ce [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...
58 if (isa<PointerType>(Op->getType()) &&
59 (C->getType() == Type::LongTy || C->getType() == Type::ULongTy))
Chris Lattner9a231222003-05-14 17:51:49 +000060 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +000061
62 // Handle cast of long to pointer...
63 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
64 Op->getType() == Type::ULongTy))
65 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +000066 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000067 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +000068
Chris Lattner9a231222003-05-14 17:51:49 +000069 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +000070 if (CE->getOperand(0)->getType() == Type::LongTy ||
71 CE->getOperand(0)->getType() == Type::ULongTy)
72 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
73 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +000074 else
75 break;
76 return Result;
77
78 default:
79 break;
80 }
81 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
82 abort();
83 }
84
Chris Lattnerbd199fb2002-12-24 00:01:05 +000085 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000086#define GET_CONST_VAL(TY, CLASS) \
87 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +000088 GET_CONST_VAL(Bool , ConstantBool);
89 GET_CONST_VAL(UByte , ConstantUInt);
90 GET_CONST_VAL(SByte , ConstantSInt);
91 GET_CONST_VAL(UShort , ConstantUInt);
92 GET_CONST_VAL(Short , ConstantSInt);
93 GET_CONST_VAL(UInt , ConstantUInt);
94 GET_CONST_VAL(Int , ConstantSInt);
95 GET_CONST_VAL(ULong , ConstantUInt);
96 GET_CONST_VAL(Long , ConstantSInt);
97 GET_CONST_VAL(Float , ConstantFP);
98 GET_CONST_VAL(Double , ConstantFP);
99#undef GET_CONST_VAL
100 case Type::PointerTyID:
101 if (isa<ConstantPointerNull>(C)) {
102 Result.PointerVal = 0;
103 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
104 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
105
106 } else {
107 assert(0 && "Unknown constant pointer type!");
108 }
109 break;
110 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000111 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000112 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000113 }
114 return Result;
115}
116
117void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
118 const Type *Ty) {
119 if (getTargetData().isLittleEndian()) {
120 switch (Ty->getPrimitiveID()) {
121 case Type::BoolTyID:
122 case Type::UByteTyID:
123 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
124 case Type::UShortTyID:
125 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
126 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
127 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000128 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000129 case Type::FloatTyID:
130 case Type::UIntTyID:
131 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
132 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
133 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
134 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
135 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000136 case Type::PointerTyID: if (CurMod.has32BitPointers())
137 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000138 case Type::DoubleTyID:
139 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000140 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000141 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
142 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
143 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
144 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
145 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
146 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
147 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
148 break;
149 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000150 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000151 }
152 } else {
153 switch (Ty->getPrimitiveID()) {
154 case Type::BoolTyID:
155 case Type::UByteTyID:
156 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
157 case Type::UShortTyID:
158 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
159 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
160 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000161 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000162 case Type::FloatTyID:
163 case Type::UIntTyID:
164 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
165 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
166 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
167 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
168 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000169 case Type::PointerTyID: if (CurMod.has32BitPointers())
170 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000171 case Type::DoubleTyID:
172 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000173 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000174 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
175 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
176 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
177 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
178 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
179 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
180 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
181 break;
182 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000183 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000184 }
185 }
186}
187
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000188GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
189 const Type *Ty) {
190 GenericValue Result;
191 if (getTargetData().isLittleEndian()) {
192 switch (Ty->getPrimitiveID()) {
193 case Type::BoolTyID:
194 case Type::UByteTyID:
195 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
196 case Type::UShortTyID:
197 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
198 ((unsigned)Ptr->Untyped[1] << 8);
199 break;
200 Load4BytesLittleEndian:
201 case Type::FloatTyID:
202 case Type::UIntTyID:
203 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
204 ((unsigned)Ptr->Untyped[1] << 8) |
205 ((unsigned)Ptr->Untyped[2] << 16) |
206 ((unsigned)Ptr->Untyped[3] << 24);
207 break;
208 case Type::PointerTyID: if (getModule().has32BitPointers())
209 goto Load4BytesLittleEndian;
210 case Type::DoubleTyID:
211 case Type::ULongTyID:
212 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
213 ((uint64_t)Ptr->Untyped[1] << 8) |
214 ((uint64_t)Ptr->Untyped[2] << 16) |
215 ((uint64_t)Ptr->Untyped[3] << 24) |
216 ((uint64_t)Ptr->Untyped[4] << 32) |
217 ((uint64_t)Ptr->Untyped[5] << 40) |
218 ((uint64_t)Ptr->Untyped[6] << 48) |
219 ((uint64_t)Ptr->Untyped[7] << 56);
220 break;
221 default:
222 std::cout << "Cannot load value of type " << *Ty << "!\n";
223 abort();
224 }
225 } else {
226 switch (Ty->getPrimitiveID()) {
227 case Type::BoolTyID:
228 case Type::UByteTyID:
229 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
230 case Type::UShortTyID:
231 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
232 ((unsigned)Ptr->Untyped[0] << 8);
233 break;
234 Load4BytesBigEndian:
235 case Type::FloatTyID:
236 case Type::UIntTyID:
237 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
238 ((unsigned)Ptr->Untyped[2] << 8) |
239 ((unsigned)Ptr->Untyped[1] << 16) |
240 ((unsigned)Ptr->Untyped[0] << 24);
241 break;
242 case Type::PointerTyID: if (getModule().has32BitPointers())
243 goto Load4BytesBigEndian;
244 case Type::DoubleTyID:
245 case Type::ULongTyID:
246 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
247 ((uint64_t)Ptr->Untyped[6] << 8) |
248 ((uint64_t)Ptr->Untyped[5] << 16) |
249 ((uint64_t)Ptr->Untyped[4] << 24) |
250 ((uint64_t)Ptr->Untyped[3] << 32) |
251 ((uint64_t)Ptr->Untyped[2] << 40) |
252 ((uint64_t)Ptr->Untyped[1] << 48) |
253 ((uint64_t)Ptr->Untyped[0] << 56);
254 break;
255 default:
256 std::cout << "Cannot load value of type " << *Ty << "!\n";
257 abort();
258 }
259 }
260 return Result;
261}
262
263
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000264// InitializeMemory - Recursive function to apply a Constant value into the
265// specified memory location...
266//
267void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
268 if (Init->getType()->isFirstClassType()) {
269 GenericValue Val = getConstantValue(Init);
270 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
271 return;
272 }
273
274 switch (Init->getType()->getPrimitiveID()) {
275 case Type::ArrayTyID: {
276 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000277 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000278 unsigned ElementSize =
279 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
280 for (unsigned i = 0; i < Val.size(); ++i)
281 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
282 return;
283 }
284
285 case Type::StructTyID: {
286 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
287 const StructLayout *SL =
288 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000289 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000290 for (unsigned i = 0; i < Val.size(); ++i)
291 InitializeMemory(cast<Constant>(Val[i].get()),
292 (char*)Addr+SL->MemberOffsets[i]);
293 return;
294 }
295
296 default:
297 std::cerr << "Bad Type: " << Init->getType() << "\n";
298 assert(0 && "Unknown constant type to initialize memory with!");
299 }
300}
301
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000302void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) {
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000303 if (getTargetData().getPointerSize() == 8) { // 64 bit target?
304 PointerTy *Result = new PointerTy[InputArgv.size()+1];
305 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
Chris Lattnerc648dab2003-08-01 22:13:59 +0000306
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000307 for (unsigned i = 0; i < InputArgv.size(); ++i) {
308 unsigned Size = InputArgv[i].size()+1;
309 char *Dest = new char[Size];
310 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
311
312 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
313 Dest[Size-1] = 0;
314
315 // Endian safe: Result[i] = (PointerTy)Dest;
316 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy);
317 }
318 Result[InputArgv.size()] = 0;
319 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000320
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000321 } else { // 32 bit target?
322 int *Result = new int[InputArgv.size()+1];
323 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000324
Chris Lattnerb2abb5a2003-05-07 20:31:37 +0000325 for (unsigned i = 0; i < InputArgv.size(); ++i) {
326 unsigned Size = InputArgv[i].size()+1;
327 char *Dest = new char[Size];
328 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
329
330 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
331 Dest[Size-1] = 0;
332
333 // Endian safe: Result[i] = (PointerTy)Dest;
334 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy);
335 }
336 Result[InputArgv.size()] = 0; // null terminate it
337 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000338 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000339}
340
341/// EmitGlobals - Emit all of the global variables to memory, storing their
342/// addresses into GlobalAddress. This must make sure to copy the contents of
343/// their initializers into the memory.
344///
345void ExecutionEngine::emitGlobals() {
346 const TargetData &TD = getTargetData();
347
348 // Loop over all of the global variables in the program, allocating the memory
349 // to hold them.
350 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
351 I != E; ++I)
352 if (!I->isExternal()) {
353 // Get the type of the global...
354 const Type *Ty = I->getType()->getElementType();
355
356 // Allocate some memory for it!
357 unsigned Size = TD.getTypeSize(Ty);
358 GlobalAddress[I] = new char[Size];
359 NumInitBytes += Size;
360
361 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
362 << (void*)GlobalAddress[I] << "\n");
363 } else {
Misha Brukman06dabfa2003-07-18 18:33:38 +0000364 // On Sparc, RTLD_SELF is already defined and it's not zero
Misha Brukman859e09f2003-07-15 15:58:26 +0000365 // Linux/x86 wants to use a 0, other systems may differ
Misha Brukman06dabfa2003-07-18 18:33:38 +0000366#ifndef RTLD_SELF
Misha Brukman859e09f2003-07-15 15:58:26 +0000367#define RTLD_SELF 0
368#endif
Misha Brukman06dabfa2003-07-18 18:33:38 +0000369 // External variable reference, try to use dlsym to get a pointer to it in
370 // the LLI image.
Misha Brukman291c2c52003-07-15 15:55:32 +0000371 if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000372 GlobalAddress[I] = SymAddr;
373 else {
374 std::cerr << "Could not resolve external global address: "
375 << I->getName() << "\n";
376 abort();
377 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000378 }
379
380 // Now that all of the globals are set up in memory, loop through them all and
381 // initialize their contents.
382 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
383 I != E; ++I)
384 if (!I->isExternal())
385 InitializeMemory(I->getInitializer(), GlobalAddress[I]);
386}
387