blob: c820f63c29fb6758f410fa40b8fccac0ba10f264 [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 Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ModuleProvider.h"
22#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000023#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000025#include "Support/Debug.h"
26#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000027#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000028#include "Config/dlfcn.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000031namespace {
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000033 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000034}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
Misha Brukman19684162003-10-16 21:18:05 +000036ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
37 CurMod(*P->getModule()), MP(P) {
38 assert(P && "ModuleProvider is null?");
39}
40
41ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000042 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000043}
44
Brian Gaeke8e539482003-09-04 22:57:27 +000045ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000046 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000047}
48
Misha Brukman19684162003-10-16 21:18:05 +000049/// If possible, create a JIT, unless the caller specifically requests an
50/// Interpreter or there's an error. If even an Interpreter cannot be created,
51/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +000052///
Misha Brukman7b2b40f2003-10-14 21:36:31 +000053ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Brian Gaeke20a277e2003-10-24 19:58:38 +000054 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +000055 ExecutionEngine *EE = 0;
56
Brian Gaeke20a277e2003-10-24 19:58:38 +000057 // Unless the interpreter was explicitly selected, make a JIT.
58 if (!ForceInterpreter)
Chris Lattner61612df2003-12-20 01:45:17 +000059 EE = JIT::create(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +000060
61 // If we can't make a JIT, make an interpreter instead.
Misha Brukman19684162003-10-16 21:18:05 +000062 try {
63 if (EE == 0)
Brian Gaeke20a277e2003-10-24 19:58:38 +000064 EE = Interpreter::create(MP->materializeModule());
Misha Brukman19684162003-10-16 21:18:05 +000065 } catch (...) {
66 EE = 0;
67 }
Brian Gaeke82d82772003-09-03 20:34:19 +000068 return EE;
69}
70
Misha Brukman4afac182003-10-10 17:45:12 +000071/// getPointerToGlobal - This returns the address of the specified global
72/// value. This may involve code generation if it's a function.
73///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000074void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +000075 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +000076 return getPointerToFunction(F);
77
78 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
79 return GlobalAddress[GV];
80}
81
Misha Brukman4afac182003-10-10 17:45:12 +000082/// FIXME: document
83///
Chris Lattnerbd199fb2002-12-24 00:01:05 +000084GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
85 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000086
Chris Lattner9a231222003-05-14 17:51:49 +000087 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000088 switch (CE->getOpcode()) {
89 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +000090 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +000091 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
92 uint64_t Offset =
93 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
94
95 Result.LongVal += Offset;
96 return Result;
97 }
Chris Lattner9a231222003-05-14 17:51:49 +000098 case Instruction::Cast: {
99 // We only need to handle a few cases here. Almost all casts will
100 // automatically fold, just the ones involving pointers won't.
101 //
102 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000103
Chris Lattner9a231222003-05-14 17:51:49 +0000104 // Handle cast of pointer to pointer...
105 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
106 return getConstantValue(Op);
107
Chris Lattner74cf8192003-08-18 17:23:40 +0000108 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000109 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +0000110 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +0000111
112 // Handle cast of long to pointer...
113 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
114 Op->getType() == Type::ULongTy))
115 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +0000116 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000117 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000118
Chris Lattner9a231222003-05-14 17:51:49 +0000119 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000120 if (CE->getOperand(0)->getType() == Type::LongTy ||
121 CE->getOperand(0)->getType() == Type::ULongTy)
122 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
123 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000124 else
125 break;
126 return Result;
127
128 default:
129 break;
130 }
131 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
132 abort();
133 }
134
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000135 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000136#define GET_CONST_VAL(TY, CLASS) \
137 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000138 GET_CONST_VAL(Bool , ConstantBool);
139 GET_CONST_VAL(UByte , ConstantUInt);
140 GET_CONST_VAL(SByte , ConstantSInt);
141 GET_CONST_VAL(UShort , ConstantUInt);
142 GET_CONST_VAL(Short , ConstantSInt);
143 GET_CONST_VAL(UInt , ConstantUInt);
144 GET_CONST_VAL(Int , ConstantSInt);
145 GET_CONST_VAL(ULong , ConstantUInt);
146 GET_CONST_VAL(Long , ConstantSInt);
147 GET_CONST_VAL(Float , ConstantFP);
148 GET_CONST_VAL(Double , ConstantFP);
149#undef GET_CONST_VAL
150 case Type::PointerTyID:
151 if (isa<ConstantPointerNull>(C)) {
152 Result.PointerVal = 0;
153 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
Chris Lattner38f0eba2003-12-08 08:22:48 +0000154 if (Function *F =
155 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
156 Result = PTOGV(getPointerToFunctionOrStub(F));
157 else
Chris Lattnerc07ed132003-12-20 03:36:47 +0000158 Result = PTOGV(getOrEmitGlobalVariable(
159 cast<GlobalVariable>(CPR->getValue())));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000160
161 } else {
162 assert(0 && "Unknown constant pointer type!");
163 }
164 break;
165 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000166 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000167 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000168 }
169 return Result;
170}
171
Misha Brukman4afac182003-10-10 17:45:12 +0000172/// FIXME: document
173///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000174void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000175 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000176 if (getTargetData().isLittleEndian()) {
177 switch (Ty->getPrimitiveID()) {
178 case Type::BoolTyID:
179 case Type::UByteTyID:
180 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
181 case Type::UShortTyID:
182 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
183 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
184 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000185 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000186 case Type::FloatTyID:
187 case Type::UIntTyID:
188 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
189 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
190 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
191 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
192 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000193 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000194 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000195 case Type::DoubleTyID:
196 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000197 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000198 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
199 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
200 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
201 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
202 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
203 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
204 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
205 break;
206 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000207 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000208 }
209 } else {
210 switch (Ty->getPrimitiveID()) {
211 case Type::BoolTyID:
212 case Type::UByteTyID:
213 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
214 case Type::UShortTyID:
215 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
216 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
217 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000218 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000219 case Type::FloatTyID:
220 case Type::UIntTyID:
221 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
222 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
223 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
224 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
225 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000226 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000227 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000228 case Type::DoubleTyID:
229 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000230 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000231 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
232 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
233 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
234 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
235 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
236 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
237 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
238 break;
239 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000240 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000241 }
242 }
243}
244
Misha Brukman4afac182003-10-10 17:45:12 +0000245/// FIXME: document
246///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000247GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
248 const Type *Ty) {
249 GenericValue Result;
250 if (getTargetData().isLittleEndian()) {
251 switch (Ty->getPrimitiveID()) {
252 case Type::BoolTyID:
253 case Type::UByteTyID:
254 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
255 case Type::UShortTyID:
256 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
257 ((unsigned)Ptr->Untyped[1] << 8);
258 break;
259 Load4BytesLittleEndian:
260 case Type::FloatTyID:
261 case Type::UIntTyID:
262 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
263 ((unsigned)Ptr->Untyped[1] << 8) |
264 ((unsigned)Ptr->Untyped[2] << 16) |
265 ((unsigned)Ptr->Untyped[3] << 24);
266 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000267 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000268 goto Load4BytesLittleEndian;
269 case Type::DoubleTyID:
270 case Type::ULongTyID:
271 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
272 ((uint64_t)Ptr->Untyped[1] << 8) |
273 ((uint64_t)Ptr->Untyped[2] << 16) |
274 ((uint64_t)Ptr->Untyped[3] << 24) |
275 ((uint64_t)Ptr->Untyped[4] << 32) |
276 ((uint64_t)Ptr->Untyped[5] << 40) |
277 ((uint64_t)Ptr->Untyped[6] << 48) |
278 ((uint64_t)Ptr->Untyped[7] << 56);
279 break;
280 default:
281 std::cout << "Cannot load value of type " << *Ty << "!\n";
282 abort();
283 }
284 } else {
285 switch (Ty->getPrimitiveID()) {
286 case Type::BoolTyID:
287 case Type::UByteTyID:
288 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
289 case Type::UShortTyID:
290 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
291 ((unsigned)Ptr->Untyped[0] << 8);
292 break;
293 Load4BytesBigEndian:
294 case Type::FloatTyID:
295 case Type::UIntTyID:
296 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
297 ((unsigned)Ptr->Untyped[2] << 8) |
298 ((unsigned)Ptr->Untyped[1] << 16) |
299 ((unsigned)Ptr->Untyped[0] << 24);
300 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000301 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000302 goto Load4BytesBigEndian;
303 case Type::DoubleTyID:
304 case Type::ULongTyID:
305 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
306 ((uint64_t)Ptr->Untyped[6] << 8) |
307 ((uint64_t)Ptr->Untyped[5] << 16) |
308 ((uint64_t)Ptr->Untyped[4] << 24) |
309 ((uint64_t)Ptr->Untyped[3] << 32) |
310 ((uint64_t)Ptr->Untyped[2] << 40) |
311 ((uint64_t)Ptr->Untyped[1] << 48) |
312 ((uint64_t)Ptr->Untyped[0] << 56);
313 break;
314 default:
315 std::cout << "Cannot load value of type " << *Ty << "!\n";
316 abort();
317 }
318 }
319 return Result;
320}
321
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000322// InitializeMemory - Recursive function to apply a Constant value into the
323// specified memory location...
324//
325void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
326 if (Init->getType()->isFirstClassType()) {
327 GenericValue Val = getConstantValue(Init);
328 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
329 return;
330 }
331
332 switch (Init->getType()->getPrimitiveID()) {
333 case Type::ArrayTyID: {
334 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000335 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000336 unsigned ElementSize =
337 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
338 for (unsigned i = 0; i < Val.size(); ++i)
339 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
340 return;
341 }
342
343 case Type::StructTyID: {
344 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
345 const StructLayout *SL =
346 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000347 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000348 for (unsigned i = 0; i < Val.size(); ++i)
349 InitializeMemory(cast<Constant>(Val[i].get()),
350 (char*)Addr+SL->MemberOffsets[i]);
351 return;
352 }
353
354 default:
355 std::cerr << "Bad Type: " << Init->getType() << "\n";
356 assert(0 && "Unknown constant type to initialize memory with!");
357 }
358}
359
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000360/// EmitGlobals - Emit all of the global variables to memory, storing their
361/// addresses into GlobalAddress. This must make sure to copy the contents of
362/// their initializers into the memory.
363///
364void ExecutionEngine::emitGlobals() {
365 const TargetData &TD = getTargetData();
366
367 // Loop over all of the global variables in the program, allocating the memory
368 // to hold them.
369 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
370 I != E; ++I)
371 if (!I->isExternal()) {
372 // Get the type of the global...
373 const Type *Ty = I->getType()->getElementType();
374
375 // Allocate some memory for it!
376 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000377 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000378
379 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Misha Brukman4afac182003-10-10 17:45:12 +0000380 << (void*)GlobalAddress[I] << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000381 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000382 // External variable reference. Try to use the dynamic loader to
383 // get a pointer to it.
384 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000385 GlobalAddress[I] = SymAddr;
386 else {
387 std::cerr << "Could not resolve external global address: "
388 << I->getName() << "\n";
389 abort();
390 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000391 }
392
393 // Now that all of the globals are set up in memory, loop through them all and
394 // initialize their contents.
395 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
396 I != E; ++I)
397 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000398 EmitGlobalVariable(I);
399}
400
401// EmitGlobalVariable - This method emits the specified global variable to the
402// address specified in GlobalAddresses, or allocates new memory if it's not
403// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000404void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner24b0a182003-12-20 02:45:37 +0000405 void *&GA = GlobalAddress[GV];
Chris Lattnerc07ed132003-12-20 03:36:47 +0000406 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000407 if (GA == 0) {
408 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000409 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner24b0a182003-12-20 02:45:37 +0000410 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000411
Chris Lattner24b0a182003-12-20 02:45:37 +0000412 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000413 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000414 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000415}