blob: 45643abca9d06fdc510de4efbc10c18c75d53adc [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 Lattner87f03102003-12-26 06:50:30 +000050
51// CreateArgv - Turn a vector of strings into a nice argv style array of
52// pointers to null terminated strings.
53//
54static void *CreateArgv(ExecutionEngine *EE,
55 const std::vector<std::string> &InputArgv) {
56 unsigned PtrSize = EE->getTargetData().getPointerSize();
57 char *Result = new char[(InputArgv.size()+1)*PtrSize];
58
59 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
60 const Type *SBytePtr = PointerType::get(Type::SByteTy);
61
62 for (unsigned i = 0; i != InputArgv.size(); ++i) {
63 unsigned Size = InputArgv[i].size()+1;
64 char *Dest = new char[Size];
65 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
66
67 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
68 Dest[Size-1] = 0;
69
70 // Endian safe: Result[i] = (PointerTy)Dest;
71 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
72 SBytePtr);
73 }
74
75 // Null terminate it
76 EE->StoreValueToMemory(PTOGV(0),
77 (GenericValue*)(Result+InputArgv.size()*PtrSize),
78 SBytePtr);
79 return Result;
80}
81
82/// runFunctionAsMain - This is a helper function which wraps runFunction to
83/// handle the common task of starting up main with the specified argc, argv,
84/// and envp parameters.
85int ExecutionEngine::runFunctionAsMain(Function *Fn,
86 const std::vector<std::string> &argv,
87 const char * const * envp) {
88 std::vector<GenericValue> GVArgs;
89 GenericValue GVArgc;
90 GVArgc.IntVal = argv.size();
91 GVArgs.push_back(GVArgc); // Arg #0 = argc.
92 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
93 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
94
95 std::vector<std::string> EnvVars;
96 for (unsigned i = 0; envp[i]; ++i)
97 EnvVars.push_back(envp[i]);
98 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
99 return runFunction(Fn, GVArgs).IntVal;
100}
101
102
103
Misha Brukman19684162003-10-16 21:18:05 +0000104/// If possible, create a JIT, unless the caller specifically requests an
105/// Interpreter or there's an error. If even an Interpreter cannot be created,
106/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000107///
Misha Brukman7b2b40f2003-10-14 21:36:31 +0000108ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000109 bool ForceInterpreter,
110 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000111 ExecutionEngine *EE = 0;
112
Chris Lattner73011782003-12-28 09:44:37 +0000113 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000114 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000115 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000116
117 // If we can't make a JIT, make an interpreter instead.
Misha Brukman19684162003-10-16 21:18:05 +0000118 try {
119 if (EE == 0)
Chris Lattner73011782003-12-28 09:44:37 +0000120 EE = Interpreter::create(MP->materializeModule(), IL);
Misha Brukman19684162003-10-16 21:18:05 +0000121 } catch (...) {
122 EE = 0;
123 }
Chris Lattner73011782003-12-28 09:44:37 +0000124
125 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000126 return EE;
127}
128
Misha Brukman4afac182003-10-10 17:45:12 +0000129/// getPointerToGlobal - This returns the address of the specified global
130/// value. This may involve code generation if it's a function.
131///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000132void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000133 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000134 return getPointerToFunction(F);
135
136 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
137 return GlobalAddress[GV];
138}
139
Misha Brukman4afac182003-10-10 17:45:12 +0000140/// FIXME: document
141///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000142GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
143 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000144
Chris Lattner9a231222003-05-14 17:51:49 +0000145 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000146 switch (CE->getOpcode()) {
147 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000148 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000149 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
150 uint64_t Offset =
151 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
152
153 Result.LongVal += Offset;
154 return Result;
155 }
Chris Lattner9a231222003-05-14 17:51:49 +0000156 case Instruction::Cast: {
157 // We only need to handle a few cases here. Almost all casts will
158 // automatically fold, just the ones involving pointers won't.
159 //
160 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000161
Chris Lattner9a231222003-05-14 17:51:49 +0000162 // Handle cast of pointer to pointer...
163 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
164 return getConstantValue(Op);
165
Chris Lattner74cf8192003-08-18 17:23:40 +0000166 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000167 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +0000168 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +0000169
170 // Handle cast of long to pointer...
171 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
172 Op->getType() == Type::ULongTy))
173 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +0000174 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000175 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176
Chris Lattner9a231222003-05-14 17:51:49 +0000177 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000178 if (CE->getOperand(0)->getType() == Type::LongTy ||
179 CE->getOperand(0)->getType() == Type::ULongTy)
180 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
181 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000182 else
183 break;
184 return Result;
185
186 default:
187 break;
188 }
189 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
190 abort();
191 }
192
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000193 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000194#define GET_CONST_VAL(TY, CLASS) \
195 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000196 GET_CONST_VAL(Bool , ConstantBool);
197 GET_CONST_VAL(UByte , ConstantUInt);
198 GET_CONST_VAL(SByte , ConstantSInt);
199 GET_CONST_VAL(UShort , ConstantUInt);
200 GET_CONST_VAL(Short , ConstantSInt);
201 GET_CONST_VAL(UInt , ConstantUInt);
202 GET_CONST_VAL(Int , ConstantSInt);
203 GET_CONST_VAL(ULong , ConstantUInt);
204 GET_CONST_VAL(Long , ConstantSInt);
205 GET_CONST_VAL(Float , ConstantFP);
206 GET_CONST_VAL(Double , ConstantFP);
207#undef GET_CONST_VAL
208 case Type::PointerTyID:
209 if (isa<ConstantPointerNull>(C)) {
210 Result.PointerVal = 0;
211 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
Chris Lattner38f0eba2003-12-08 08:22:48 +0000212 if (Function *F =
213 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
214 Result = PTOGV(getPointerToFunctionOrStub(F));
215 else
Chris Lattnerc07ed132003-12-20 03:36:47 +0000216 Result = PTOGV(getOrEmitGlobalVariable(
217 cast<GlobalVariable>(CPR->getValue())));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000218
219 } else {
220 assert(0 && "Unknown constant pointer type!");
221 }
222 break;
223 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000224 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000225 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000226 }
227 return Result;
228}
229
Misha Brukman4afac182003-10-10 17:45:12 +0000230/// FIXME: document
231///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000232void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000233 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000234 if (getTargetData().isLittleEndian()) {
235 switch (Ty->getPrimitiveID()) {
236 case Type::BoolTyID:
237 case Type::UByteTyID:
238 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
239 case Type::UShortTyID:
240 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
241 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
242 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000243 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000244 case Type::FloatTyID:
245 case Type::UIntTyID:
246 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
247 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
248 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
249 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
250 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000251 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000252 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000253 case Type::DoubleTyID:
254 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000255 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000256 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
257 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
258 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
259 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
260 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
261 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
262 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
263 break;
264 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000265 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000266 }
267 } else {
268 switch (Ty->getPrimitiveID()) {
269 case Type::BoolTyID:
270 case Type::UByteTyID:
271 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
272 case Type::UShortTyID:
273 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
274 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
275 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000276 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000277 case Type::FloatTyID:
278 case Type::UIntTyID:
279 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
280 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
281 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
282 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
283 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000284 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000285 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000286 case Type::DoubleTyID:
287 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000288 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000289 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
290 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
291 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
292 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
293 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
294 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
295 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
296 break;
297 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000298 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000299 }
300 }
301}
302
Misha Brukman4afac182003-10-10 17:45:12 +0000303/// FIXME: document
304///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000305GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
306 const Type *Ty) {
307 GenericValue Result;
308 if (getTargetData().isLittleEndian()) {
309 switch (Ty->getPrimitiveID()) {
310 case Type::BoolTyID:
311 case Type::UByteTyID:
312 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
313 case Type::UShortTyID:
314 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
315 ((unsigned)Ptr->Untyped[1] << 8);
316 break;
317 Load4BytesLittleEndian:
318 case Type::FloatTyID:
319 case Type::UIntTyID:
320 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
321 ((unsigned)Ptr->Untyped[1] << 8) |
322 ((unsigned)Ptr->Untyped[2] << 16) |
323 ((unsigned)Ptr->Untyped[3] << 24);
324 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000325 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000326 goto Load4BytesLittleEndian;
327 case Type::DoubleTyID:
328 case Type::ULongTyID:
329 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
330 ((uint64_t)Ptr->Untyped[1] << 8) |
331 ((uint64_t)Ptr->Untyped[2] << 16) |
332 ((uint64_t)Ptr->Untyped[3] << 24) |
333 ((uint64_t)Ptr->Untyped[4] << 32) |
334 ((uint64_t)Ptr->Untyped[5] << 40) |
335 ((uint64_t)Ptr->Untyped[6] << 48) |
336 ((uint64_t)Ptr->Untyped[7] << 56);
337 break;
338 default:
339 std::cout << "Cannot load value of type " << *Ty << "!\n";
340 abort();
341 }
342 } else {
343 switch (Ty->getPrimitiveID()) {
344 case Type::BoolTyID:
345 case Type::UByteTyID:
346 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
347 case Type::UShortTyID:
348 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
349 ((unsigned)Ptr->Untyped[0] << 8);
350 break;
351 Load4BytesBigEndian:
352 case Type::FloatTyID:
353 case Type::UIntTyID:
354 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
355 ((unsigned)Ptr->Untyped[2] << 8) |
356 ((unsigned)Ptr->Untyped[1] << 16) |
357 ((unsigned)Ptr->Untyped[0] << 24);
358 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000359 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000360 goto Load4BytesBigEndian;
361 case Type::DoubleTyID:
362 case Type::ULongTyID:
363 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
364 ((uint64_t)Ptr->Untyped[6] << 8) |
365 ((uint64_t)Ptr->Untyped[5] << 16) |
366 ((uint64_t)Ptr->Untyped[4] << 24) |
367 ((uint64_t)Ptr->Untyped[3] << 32) |
368 ((uint64_t)Ptr->Untyped[2] << 40) |
369 ((uint64_t)Ptr->Untyped[1] << 48) |
370 ((uint64_t)Ptr->Untyped[0] << 56);
371 break;
372 default:
373 std::cout << "Cannot load value of type " << *Ty << "!\n";
374 abort();
375 }
376 }
377 return Result;
378}
379
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000380// InitializeMemory - Recursive function to apply a Constant value into the
381// specified memory location...
382//
383void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
384 if (Init->getType()->isFirstClassType()) {
385 GenericValue Val = getConstantValue(Init);
386 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
387 return;
388 }
389
390 switch (Init->getType()->getPrimitiveID()) {
391 case Type::ArrayTyID: {
392 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000393 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000394 unsigned ElementSize =
395 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
396 for (unsigned i = 0; i < Val.size(); ++i)
397 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
398 return;
399 }
400
401 case Type::StructTyID: {
402 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
403 const StructLayout *SL =
404 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000405 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000406 for (unsigned i = 0; i < Val.size(); ++i)
407 InitializeMemory(cast<Constant>(Val[i].get()),
408 (char*)Addr+SL->MemberOffsets[i]);
409 return;
410 }
411
412 default:
413 std::cerr << "Bad Type: " << Init->getType() << "\n";
414 assert(0 && "Unknown constant type to initialize memory with!");
415 }
416}
417
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000418/// EmitGlobals - Emit all of the global variables to memory, storing their
419/// addresses into GlobalAddress. This must make sure to copy the contents of
420/// their initializers into the memory.
421///
422void ExecutionEngine::emitGlobals() {
423 const TargetData &TD = getTargetData();
424
425 // Loop over all of the global variables in the program, allocating the memory
426 // to hold them.
427 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
428 I != E; ++I)
429 if (!I->isExternal()) {
430 // Get the type of the global...
431 const Type *Ty = I->getType()->getElementType();
432
433 // Allocate some memory for it!
434 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000435 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000436
437 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Misha Brukman4afac182003-10-10 17:45:12 +0000438 << (void*)GlobalAddress[I] << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000439 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000440 // External variable reference. Try to use the dynamic loader to
441 // get a pointer to it.
442 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000443 GlobalAddress[I] = SymAddr;
444 else {
445 std::cerr << "Could not resolve external global address: "
446 << I->getName() << "\n";
447 abort();
448 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000449 }
450
451 // Now that all of the globals are set up in memory, loop through them all and
452 // initialize their contents.
453 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
454 I != E; ++I)
455 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000456 EmitGlobalVariable(I);
457}
458
459// EmitGlobalVariable - This method emits the specified global variable to the
460// address specified in GlobalAddresses, or allocates new memory if it's not
461// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000462void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner24b0a182003-12-20 02:45:37 +0000463 void *&GA = GlobalAddress[GV];
Chris Lattnerc07ed132003-12-20 03:36:47 +0000464 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000465 if (GA == 0) {
466 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000467 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner24b0a182003-12-20 02:45:37 +0000468 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000469
Chris Lattner24b0a182003-12-20 02:45:37 +0000470 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000471 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000472 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000473}