blob: ba21f40ffac9be24298acd13eff399ca425250f1 [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"
Chris Lattner61612df2003-12-20 01:45:17 +000017#include "JIT/JIT.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 Lattner73011782003-12-28 09:44:37 +000020#include "llvm/IntrinsicLowering.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000022#include "llvm/ModuleProvider.h"
23#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000024#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000026#include "Support/Debug.h"
27#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000028#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000029#include "Config/dlfcn.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000030using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000031
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000032namespace {
33 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000034 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000035}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000036
Misha Brukman19684162003-10-16 21:18:05 +000037ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
38 CurMod(*P->getModule()), MP(P) {
39 assert(P && "ModuleProvider is null?");
40}
41
42ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000043 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000044}
45
Brian Gaeke8e539482003-09-04 22:57:27 +000046ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000047 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000048}
49
Chris Lattner55d86482003-12-31 20:21:04 +000050/// getGlobalValueAtAddress - Return the LLVM global value object that starts
51/// at the specified address.
52///
53const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
54 // If we haven't computed the reverse mapping yet, do so first.
55 if (GlobalAddressReverseMap.empty()) {
56 for (std::map<const GlobalValue*, void *>::iterator I =
57 GlobalAddressMap.begin(), E = GlobalAddressMap.end(); I != E; ++I)
58 GlobalAddressReverseMap.insert(std::make_pair(I->second, I->first));
59 }
60
61 std::map<void *, const GlobalValue*>::iterator I =
62 GlobalAddressReverseMap.find(Addr);
63 return I != GlobalAddressReverseMap.end() ? I->second : 0;
64}
Chris Lattner87f03102003-12-26 06:50:30 +000065
66// CreateArgv - Turn a vector of strings into a nice argv style array of
67// pointers to null terminated strings.
68//
69static void *CreateArgv(ExecutionEngine *EE,
70 const std::vector<std::string> &InputArgv) {
71 unsigned PtrSize = EE->getTargetData().getPointerSize();
72 char *Result = new char[(InputArgv.size()+1)*PtrSize];
73
74 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
75 const Type *SBytePtr = PointerType::get(Type::SByteTy);
76
77 for (unsigned i = 0; i != InputArgv.size(); ++i) {
78 unsigned Size = InputArgv[i].size()+1;
79 char *Dest = new char[Size];
80 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
81
82 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
83 Dest[Size-1] = 0;
84
85 // Endian safe: Result[i] = (PointerTy)Dest;
86 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
87 SBytePtr);
88 }
89
90 // Null terminate it
91 EE->StoreValueToMemory(PTOGV(0),
92 (GenericValue*)(Result+InputArgv.size()*PtrSize),
93 SBytePtr);
94 return Result;
95}
96
97/// runFunctionAsMain - This is a helper function which wraps runFunction to
98/// handle the common task of starting up main with the specified argc, argv,
99/// and envp parameters.
100int ExecutionEngine::runFunctionAsMain(Function *Fn,
101 const std::vector<std::string> &argv,
102 const char * const * envp) {
103 std::vector<GenericValue> GVArgs;
104 GenericValue GVArgc;
105 GVArgc.IntVal = argv.size();
106 GVArgs.push_back(GVArgc); // Arg #0 = argc.
107 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
108 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
109
110 std::vector<std::string> EnvVars;
111 for (unsigned i = 0; envp[i]; ++i)
112 EnvVars.push_back(envp[i]);
113 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
114 return runFunction(Fn, GVArgs).IntVal;
115}
116
117
118
Misha Brukman19684162003-10-16 21:18:05 +0000119/// If possible, create a JIT, unless the caller specifically requests an
120/// Interpreter or there's an error. If even an Interpreter cannot be created,
121/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000122///
Misha Brukman7b2b40f2003-10-14 21:36:31 +0000123ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000124 bool ForceInterpreter,
125 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000126 ExecutionEngine *EE = 0;
127
Chris Lattner73011782003-12-28 09:44:37 +0000128 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000129 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000130 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000131
132 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000133 if (EE == 0) {
134 try {
135 Module *M = MP->materializeModule();
136 try {
137 EE = Interpreter::create(M, IL);
138 } catch (...) {
139 std::cerr << "Error creating the interpreter!\n";
140 }
141 } catch (...) {
142 std::cerr << "Error reading the bytecode file!\n";
143 }
Misha Brukman19684162003-10-16 21:18:05 +0000144 }
Chris Lattner73011782003-12-28 09:44:37 +0000145
146 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000147 return EE;
148}
149
Misha Brukman4afac182003-10-10 17:45:12 +0000150/// getPointerToGlobal - This returns the address of the specified global
151/// value. This may involve code generation if it's a function.
152///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000153void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000154 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000155 return getPointerToFunction(F);
156
Chris Lattner55d86482003-12-31 20:21:04 +0000157 assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?");
158 return GlobalAddressMap[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000159}
160
Misha Brukman4afac182003-10-10 17:45:12 +0000161/// FIXME: document
162///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000163GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
164 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000165
Chris Lattner9a231222003-05-14 17:51:49 +0000166 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000167 switch (CE->getOpcode()) {
168 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000169 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000170 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
171 uint64_t Offset =
172 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
173
174 Result.LongVal += Offset;
175 return Result;
176 }
Chris Lattner9a231222003-05-14 17:51:49 +0000177 case Instruction::Cast: {
178 // We only need to handle a few cases here. Almost all casts will
179 // automatically fold, just the ones involving pointers won't.
180 //
181 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000182 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000183
Chris Lattner9a231222003-05-14 17:51:49 +0000184 // Handle cast of pointer to pointer...
185 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000186 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000187
Chris Lattner74cf8192003-08-18 17:23:40 +0000188 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000189 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000190 return GV;
Chris Lattner74cf8192003-08-18 17:23:40 +0000191
Chris Lattner7d1bd332004-03-16 08:38:56 +0000192 // Handle cast of integer to a pointer...
193 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
194 switch (Op->getType()->getPrimitiveID()) {
195 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
196 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
197 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
198 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
199 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
200 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
201 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
202 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
203 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
204 default: assert(0 && "Unknown integral type!");
205 }
Chris Lattner9a231222003-05-14 17:51:49 +0000206 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000207 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000208
Chris Lattner9a231222003-05-14 17:51:49 +0000209 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000210 if (CE->getOperand(0)->getType() == Type::LongTy ||
211 CE->getOperand(0)->getType() == Type::ULongTy)
212 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
213 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000214 else
215 break;
216 return Result;
217
218 default:
219 break;
220 }
221 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
222 abort();
223 }
224
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000225 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000226#define GET_CONST_VAL(TY, CLASS) \
227 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000228 GET_CONST_VAL(Bool , ConstantBool);
229 GET_CONST_VAL(UByte , ConstantUInt);
230 GET_CONST_VAL(SByte , ConstantSInt);
231 GET_CONST_VAL(UShort , ConstantUInt);
232 GET_CONST_VAL(Short , ConstantSInt);
233 GET_CONST_VAL(UInt , ConstantUInt);
234 GET_CONST_VAL(Int , ConstantSInt);
235 GET_CONST_VAL(ULong , ConstantUInt);
236 GET_CONST_VAL(Long , ConstantSInt);
237 GET_CONST_VAL(Float , ConstantFP);
238 GET_CONST_VAL(Double , ConstantFP);
239#undef GET_CONST_VAL
240 case Type::PointerTyID:
241 if (isa<ConstantPointerNull>(C)) {
242 Result.PointerVal = 0;
243 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
Chris Lattner38f0eba2003-12-08 08:22:48 +0000244 if (Function *F =
245 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
246 Result = PTOGV(getPointerToFunctionOrStub(F));
247 else
Chris Lattnerc07ed132003-12-20 03:36:47 +0000248 Result = PTOGV(getOrEmitGlobalVariable(
249 cast<GlobalVariable>(CPR->getValue())));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000250
251 } else {
252 assert(0 && "Unknown constant pointer type!");
253 }
254 break;
255 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000256 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000257 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000258 }
259 return Result;
260}
261
Misha Brukman4afac182003-10-10 17:45:12 +0000262/// FIXME: document
263///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000264void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000265 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000266 if (getTargetData().isLittleEndian()) {
267 switch (Ty->getPrimitiveID()) {
268 case Type::BoolTyID:
269 case Type::UByteTyID:
270 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
271 case Type::UShortTyID:
272 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
273 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
274 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000275 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000276 case Type::FloatTyID:
277 case Type::UIntTyID:
278 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
279 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
280 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
281 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
282 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000283 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000284 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000285 case Type::DoubleTyID:
286 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000287 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000288 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
289 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
290 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
291 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
292 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
293 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
294 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
295 break;
296 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000297 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000298 }
299 } else {
300 switch (Ty->getPrimitiveID()) {
301 case Type::BoolTyID:
302 case Type::UByteTyID:
303 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
304 case Type::UShortTyID:
305 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
306 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
307 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000308 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000309 case Type::FloatTyID:
310 case Type::UIntTyID:
311 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
312 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
313 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
314 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
315 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000316 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000317 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000318 case Type::DoubleTyID:
319 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000320 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000321 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
322 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
323 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
324 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
325 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
326 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
327 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
328 break;
329 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000330 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000331 }
332 }
333}
334
Misha Brukman4afac182003-10-10 17:45:12 +0000335/// FIXME: document
336///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000337GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
338 const Type *Ty) {
339 GenericValue Result;
340 if (getTargetData().isLittleEndian()) {
341 switch (Ty->getPrimitiveID()) {
342 case Type::BoolTyID:
343 case Type::UByteTyID:
344 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
345 case Type::UShortTyID:
346 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
347 ((unsigned)Ptr->Untyped[1] << 8);
348 break;
349 Load4BytesLittleEndian:
350 case Type::FloatTyID:
351 case Type::UIntTyID:
352 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
353 ((unsigned)Ptr->Untyped[1] << 8) |
354 ((unsigned)Ptr->Untyped[2] << 16) |
355 ((unsigned)Ptr->Untyped[3] << 24);
356 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000357 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000358 goto Load4BytesLittleEndian;
359 case Type::DoubleTyID:
360 case Type::ULongTyID:
361 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
362 ((uint64_t)Ptr->Untyped[1] << 8) |
363 ((uint64_t)Ptr->Untyped[2] << 16) |
364 ((uint64_t)Ptr->Untyped[3] << 24) |
365 ((uint64_t)Ptr->Untyped[4] << 32) |
366 ((uint64_t)Ptr->Untyped[5] << 40) |
367 ((uint64_t)Ptr->Untyped[6] << 48) |
368 ((uint64_t)Ptr->Untyped[7] << 56);
369 break;
370 default:
371 std::cout << "Cannot load value of type " << *Ty << "!\n";
372 abort();
373 }
374 } else {
375 switch (Ty->getPrimitiveID()) {
376 case Type::BoolTyID:
377 case Type::UByteTyID:
378 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
379 case Type::UShortTyID:
380 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
381 ((unsigned)Ptr->Untyped[0] << 8);
382 break;
383 Load4BytesBigEndian:
384 case Type::FloatTyID:
385 case Type::UIntTyID:
386 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
387 ((unsigned)Ptr->Untyped[2] << 8) |
388 ((unsigned)Ptr->Untyped[1] << 16) |
389 ((unsigned)Ptr->Untyped[0] << 24);
390 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000391 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000392 goto Load4BytesBigEndian;
393 case Type::DoubleTyID:
394 case Type::ULongTyID:
395 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
396 ((uint64_t)Ptr->Untyped[6] << 8) |
397 ((uint64_t)Ptr->Untyped[5] << 16) |
398 ((uint64_t)Ptr->Untyped[4] << 24) |
399 ((uint64_t)Ptr->Untyped[3] << 32) |
400 ((uint64_t)Ptr->Untyped[2] << 40) |
401 ((uint64_t)Ptr->Untyped[1] << 48) |
402 ((uint64_t)Ptr->Untyped[0] << 56);
403 break;
404 default:
405 std::cout << "Cannot load value of type " << *Ty << "!\n";
406 abort();
407 }
408 }
409 return Result;
410}
411
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000412// InitializeMemory - Recursive function to apply a Constant value into the
413// specified memory location...
414//
415void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
416 if (Init->getType()->isFirstClassType()) {
417 GenericValue Val = getConstantValue(Init);
418 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
419 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000420 } else if (isa<ConstantAggregateZero>(Init)) {
421 unsigned Size = getTargetData().getTypeSize(Init->getType());
422 memset(Addr, 0, Size);
423 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000424 }
425
426 switch (Init->getType()->getPrimitiveID()) {
427 case Type::ArrayTyID: {
428 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000429 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000430 unsigned ElementSize =
431 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
432 for (unsigned i = 0; i < Val.size(); ++i)
433 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
434 return;
435 }
436
437 case Type::StructTyID: {
438 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
439 const StructLayout *SL =
440 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000441 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000442 for (unsigned i = 0; i < Val.size(); ++i)
443 InitializeMemory(cast<Constant>(Val[i].get()),
444 (char*)Addr+SL->MemberOffsets[i]);
445 return;
446 }
447
448 default:
449 std::cerr << "Bad Type: " << Init->getType() << "\n";
450 assert(0 && "Unknown constant type to initialize memory with!");
451 }
452}
453
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000454/// EmitGlobals - Emit all of the global variables to memory, storing their
455/// addresses into GlobalAddress. This must make sure to copy the contents of
456/// their initializers into the memory.
457///
458void ExecutionEngine::emitGlobals() {
459 const TargetData &TD = getTargetData();
460
461 // Loop over all of the global variables in the program, allocating the memory
462 // to hold them.
463 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
464 I != E; ++I)
465 if (!I->isExternal()) {
466 // Get the type of the global...
467 const Type *Ty = I->getType()->getElementType();
468
469 // Allocate some memory for it!
470 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000471 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000472 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000473 // External variable reference. Try to use the dynamic loader to
474 // get a pointer to it.
475 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000476 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000477 else {
478 std::cerr << "Could not resolve external global address: "
479 << I->getName() << "\n";
480 abort();
481 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000482 }
483
484 // Now that all of the globals are set up in memory, loop through them all and
485 // initialize their contents.
486 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
487 I != E; ++I)
488 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000489 EmitGlobalVariable(I);
490}
491
492// EmitGlobalVariable - This method emits the specified global variable to the
493// address specified in GlobalAddresses, or allocates new memory if it's not
494// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000495void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000496 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000497 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
498
Chris Lattnerc07ed132003-12-20 03:36:47 +0000499 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000500 if (GA == 0) {
501 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000502 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner55d86482003-12-31 20:21:04 +0000503 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000504 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000505
Chris Lattner24b0a182003-12-20 02:45:37 +0000506 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000507 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000508 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000509}