blob: 83e94828b7ae61dd44c6ee0b12b5a55fc9e20829 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
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 Lattnerbd199fb2002-12-24 00:01:05 +000016#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000017#include "llvm/DerivedTypes.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/ModuleProvider.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000020#include "llvm/ADT/Statistic.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000022#include "llvm/ExecutionEngine/GenericValue.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000024#include "llvm/Support/MutexGuard.h"
Reid Spencerdf5a37e2004-11-29 14:11:29 +000025#include "llvm/System/DynamicLibrary.h"
26#include "llvm/Target/TargetData.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000027using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000028
Chris Lattner36343732006-12-19 22:43:32 +000029STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
30STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000031
Chris Lattner2fe4bb02006-03-22 06:07:50 +000032ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
33ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
34
Chris Lattnerfe854032006-08-16 01:24:12 +000035ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000036 LazyCompilationDisabled = false;
Chris Lattnerfe854032006-08-16 01:24:12 +000037 Modules.push_back(P);
Misha Brukman19684162003-10-16 21:18:05 +000038 assert(P && "ModuleProvider is null?");
39}
40
Chris Lattnerfe854032006-08-16 01:24:12 +000041ExecutionEngine::ExecutionEngine(Module *M) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000042 LazyCompilationDisabled = false;
Misha Brukman05701572003-10-17 18:31:59 +000043 assert(M && "Module is null?");
Chris Lattnerfe854032006-08-16 01:24:12 +000044 Modules.push_back(new ExistingModuleProvider(M));
Misha Brukman19684162003-10-16 21:18:05 +000045}
46
Brian Gaeke8e539482003-09-04 22:57:27 +000047ExecutionEngine::~ExecutionEngine() {
Chris Lattnerfe854032006-08-16 01:24:12 +000048 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
49 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000050}
51
Chris Lattnerfe854032006-08-16 01:24:12 +000052/// FindFunctionNamed - Search all of the active modules to find the one that
53/// defines FnName. This is very slow operation and shouldn't be used for
54/// general code.
55Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
56 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
57 if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName))
58 return F;
59 }
60 return 0;
61}
62
63
Chris Lattnere7fd5532006-05-08 22:00:52 +000064/// addGlobalMapping - Tell the execution engine that the specified global is
65/// at the specified location. This is used internally as functions are JIT'd
66/// and as global variables are laid out in memory. It can and should also be
67/// used by clients of the EE that want to have an LLVM global overlay
68/// existing data in memory.
69void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
70 MutexGuard locked(lock);
71
72 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
73 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
74 CurVal = Addr;
75
76 // If we are using the reverse mapping, add it too
77 if (!state.getGlobalAddressReverseMap(locked).empty()) {
78 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
79 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
80 V = GV;
81 }
82}
83
84/// clearAllGlobalMappings - Clear all global mappings and start over again
85/// use in dynamic compilation scenarios when you want to move globals
86void ExecutionEngine::clearAllGlobalMappings() {
87 MutexGuard locked(lock);
88
89 state.getGlobalAddressMap(locked).clear();
90 state.getGlobalAddressReverseMap(locked).clear();
91}
92
93/// updateGlobalMapping - Replace an existing mapping for GV with a new
94/// address. This updates both maps as required. If "Addr" is null, the
95/// entry for the global is removed from the mappings.
96void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
97 MutexGuard locked(lock);
98
99 // Deleting from the mapping?
100 if (Addr == 0) {
101 state.getGlobalAddressMap(locked).erase(GV);
102 if (!state.getGlobalAddressReverseMap(locked).empty())
103 state.getGlobalAddressReverseMap(locked).erase(Addr);
104 return;
105 }
106
107 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
108 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
109 state.getGlobalAddressReverseMap(locked).erase(CurVal);
110 CurVal = Addr;
111
112 // If we are using the reverse mapping, add it too
113 if (!state.getGlobalAddressReverseMap(locked).empty()) {
114 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
115 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
116 V = GV;
117 }
118}
119
120/// getPointerToGlobalIfAvailable - This returns the address of the specified
121/// global value if it is has already been codegen'd, otherwise it returns null.
122///
123void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
124 MutexGuard locked(lock);
125
126 std::map<const GlobalValue*, void*>::iterator I =
127 state.getGlobalAddressMap(locked).find(GV);
128 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
129}
130
Chris Lattner55d86482003-12-31 20:21:04 +0000131/// getGlobalValueAtAddress - Return the LLVM global value object that starts
132/// at the specified address.
133///
134const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000135 MutexGuard locked(lock);
136
Chris Lattner55d86482003-12-31 20:21:04 +0000137 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +0000138 if (state.getGlobalAddressReverseMap(locked).empty()) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000139 for (std::map<const GlobalValue*, void *>::iterator
140 I = state.getGlobalAddressMap(locked).begin(),
141 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
142 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
143 I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000144 }
145
146 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000147 state.getGlobalAddressReverseMap(locked).find(Addr);
148 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000149}
Chris Lattner87f03102003-12-26 06:50:30 +0000150
151// CreateArgv - Turn a vector of strings into a nice argv style array of
152// pointers to null terminated strings.
153//
154static void *CreateArgv(ExecutionEngine *EE,
155 const std::vector<std::string> &InputArgv) {
Owen Andersona69571c2006-05-03 01:29:57 +0000156 unsigned PtrSize = EE->getTargetData()->getPointerSize();
Chris Lattner87f03102003-12-26 06:50:30 +0000157 char *Result = new char[(InputArgv.size()+1)*PtrSize];
158
Bill Wendling480f0932006-11-27 23:54:50 +0000159 DOUT << "ARGV = " << (void*)Result << "\n";
Chris Lattner87f03102003-12-26 06:50:30 +0000160 const Type *SBytePtr = PointerType::get(Type::SByteTy);
161
162 for (unsigned i = 0; i != InputArgv.size(); ++i) {
163 unsigned Size = InputArgv[i].size()+1;
164 char *Dest = new char[Size];
Bill Wendling480f0932006-11-27 23:54:50 +0000165 DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000166
Chris Lattner87f03102003-12-26 06:50:30 +0000167 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
168 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000169
Chris Lattner87f03102003-12-26 06:50:30 +0000170 // Endian safe: Result[i] = (PointerTy)Dest;
171 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
172 SBytePtr);
173 }
174
175 // Null terminate it
176 EE->StoreValueToMemory(PTOGV(0),
177 (GenericValue*)(Result+InputArgv.size()*PtrSize),
178 SBytePtr);
179 return Result;
180}
181
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000182
183/// runStaticConstructorsDestructors - This method is used to execute all of
Chris Lattnerfe854032006-08-16 01:24:12 +0000184/// the static constructors or destructors for a program, depending on the
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000185/// value of isDtors.
186void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
187 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000188
Chris Lattnerfe854032006-08-16 01:24:12 +0000189 // Execute global ctors/dtors for each module in the program.
190 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
191 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
192
193 // If this global has internal linkage, or if it has a use, then it must be
194 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
195 // this is the case, don't execute any of the global ctors, __main will do
196 // it.
197 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue;
198
199 // Should be an array of '{ int, void ()* }' structs. The first value is
200 // the init priority, which we ignore.
201 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
202 if (!InitList) continue;
203 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
204 if (ConstantStruct *CS =
205 dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
206 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000207
Chris Lattnerfe854032006-08-16 01:24:12 +0000208 Constant *FP = CS->getOperand(1);
209 if (FP->isNullValue())
210 break; // Found a null terminator, exit.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000211
Chris Lattnerfe854032006-08-16 01:24:12 +0000212 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +0000213 if (CE->isCast())
Chris Lattnerfe854032006-08-16 01:24:12 +0000214 FP = CE->getOperand(0);
215 if (Function *F = dyn_cast<Function>(FP)) {
216 // Execute the ctor/dtor function!
217 runFunction(F, std::vector<GenericValue>());
218 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000219 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000220 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000221}
222
Chris Lattner87f03102003-12-26 06:50:30 +0000223/// runFunctionAsMain - This is a helper function which wraps runFunction to
224/// handle the common task of starting up main with the specified argc, argv,
225/// and envp parameters.
226int ExecutionEngine::runFunctionAsMain(Function *Fn,
227 const std::vector<std::string> &argv,
228 const char * const * envp) {
229 std::vector<GenericValue> GVArgs;
230 GenericValue GVArgc;
231 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000232 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
233 if (NumArgs) {
234 GVArgs.push_back(GVArgc); // Arg #0 = argc.
235 if (NumArgs > 1) {
236 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
237 assert(((char **)GVTOP(GVArgs[1]))[0] &&
238 "argv[0] was null after CreateArgv");
239 if (NumArgs > 2) {
240 std::vector<std::string> EnvVars;
241 for (unsigned i = 0; envp[i]; ++i)
242 EnvVars.push_back(envp[i]);
243 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
244 }
245 }
246 }
Chris Lattner87f03102003-12-26 06:50:30 +0000247 return runFunction(Fn, GVArgs).IntVal;
248}
249
Misha Brukman19684162003-10-16 21:18:05 +0000250/// If possible, create a JIT, unless the caller specifically requests an
251/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000252/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000253///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000254ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000255 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000256 ExecutionEngine *EE = 0;
257
Chris Lattner73011782003-12-28 09:44:37 +0000258 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000259 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000260 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000261
262 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000263 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000264 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000265
Chris Lattner726c1ef2006-03-23 05:22:51 +0000266 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000267 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000268 // to the function tells DynamicLibrary to load the program, not a library.
Chris Lattner6f3ada52006-05-14 19:01:55 +0000269 try {
270 sys::DynamicLibrary::LoadLibraryPermanently(0);
271 } catch (...) {
272 }
Chris Lattner726c1ef2006-03-23 05:22:51 +0000273 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000274
Brian Gaeke82d82772003-09-03 20:34:19 +0000275 return EE;
276}
277
Misha Brukman4afac182003-10-10 17:45:12 +0000278/// getPointerToGlobal - This returns the address of the specified global
279/// value. This may involve code generation if it's a function.
280///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000281void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000282 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000283 return getPointerToFunction(F);
284
Reid Spenceree448632005-07-12 15:51:55 +0000285 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000286 void *p = state.getGlobalAddressMap(locked)[GV];
287 if (p)
288 return p;
289
290 // Global variable might have been added since interpreter started.
291 if (GlobalVariable *GVar =
292 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
293 EmitGlobalVariable(GVar);
294 else
295 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000296 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000297}
298
Reid Spencer3da59db2006-11-27 01:05:10 +0000299/// This function converts a Constant* into a GenericValue. The interesting
300/// part is if C is a ConstantExpr.
301/// @brief Get a GenericValue for a Constnat*
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000302GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000303 // Declare the result as garbage.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000304 GenericValue Result;
Reid Spencer3da59db2006-11-27 01:05:10 +0000305
306 // If its undefined, return the garbage.
Chris Lattner6f335f92004-10-26 05:35:14 +0000307 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000308
Reid Spencer3da59db2006-11-27 01:05:10 +0000309 // If the value is a ConstantExpr
310 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000311 switch (CE->getOpcode()) {
312 case Instruction::GetElementPtr: {
Reid Spencer3da59db2006-11-27 01:05:10 +0000313 // Compute the index
Chris Lattner9a231222003-05-14 17:51:49 +0000314 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000315 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
316 uint64_t Offset =
317 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000318
Owen Andersona69571c2006-05-03 01:29:57 +0000319 if (getTargetData()->getPointerSize() == 4)
Chris Lattner3db4b622005-10-23 23:54:56 +0000320 Result.IntVal += Offset;
321 else
322 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000323 return Result;
324 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000325 case Instruction::Trunc:
326 case Instruction::ZExt:
327 case Instruction::SExt:
328 case Instruction::FPTrunc:
329 case Instruction::FPExt:
330 case Instruction::UIToFP:
331 case Instruction::SIToFP:
332 case Instruction::FPToUI:
333 case Instruction::FPToSI:
334 break;
335 case Instruction::PtrToInt: {
Chris Lattner9a231222003-05-14 17:51:49 +0000336 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000337 GenericValue GV = getConstantValue(Op);
Reid Spencer3da59db2006-11-27 01:05:10 +0000338 return GV;
339 }
340 case Instruction::BitCast: {
341 // Bit casts are no-ops but we can only return the GV of the operand if
342 // they are the same basic type (pointer->pointer, packed->packed, etc.)
343 Constant *Op = CE->getOperand(0);
344 GenericValue GV = getConstantValue(Op);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000345 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000346 return GV;
Reid Spencer3da59db2006-11-27 01:05:10 +0000347 break;
348 }
349 case Instruction::IntToPtr: {
350 // IntToPtr casts are just so special. Cast to intptr_t first.
351 Constant *Op = CE->getOperand(0);
352 GenericValue GV = getConstantValue(Op);
353 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000354 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
355 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
356 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
357 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
358 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
359 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
360 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
361 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
362 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
363 default: assert(0 && "Unknown integral type!");
Reid Spencer3da59db2006-11-27 01:05:10 +0000364 }
Chris Lattner9a231222003-05-14 17:51:49 +0000365 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000366 }
Chris Lattner9a231222003-05-14 17:51:49 +0000367 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000368 switch (CE->getOperand(0)->getType()->getTypeID()) {
369 default: assert(0 && "Bad add type!"); abort();
370 case Type::LongTyID:
371 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000372 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
373 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000374 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000375 case Type::IntTyID:
376 case Type::UIntTyID:
377 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
378 getConstantValue(CE->getOperand(1)).IntVal;
379 break;
380 case Type::ShortTyID:
381 case Type::UShortTyID:
382 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
383 getConstantValue(CE->getOperand(1)).ShortVal;
384 break;
385 case Type::SByteTyID:
386 case Type::UByteTyID:
387 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
388 getConstantValue(CE->getOperand(1)).SByteVal;
389 break;
390 case Type::FloatTyID:
391 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
392 getConstantValue(CE->getOperand(1)).FloatVal;
393 break;
394 case Type::DoubleTyID:
395 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
396 getConstantValue(CE->getOperand(1)).DoubleVal;
397 break;
398 }
Chris Lattner9a231222003-05-14 17:51:49 +0000399 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000400 default:
401 break;
402 }
Bill Wendlinge8156192006-12-07 01:30:32 +0000403 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
Chris Lattner9a231222003-05-14 17:51:49 +0000404 abort();
405 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000406
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000407 switch (C->getType()->getTypeID()) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000408#define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \
409 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break
410 GET_CONST_VAL(Bool , bool , ConstantBool, getValue);
411 GET_CONST_VAL(UByte , unsigned char , ConstantInt, getZExtValue);
412 GET_CONST_VAL(SByte , signed char , ConstantInt, getSExtValue);
413 GET_CONST_VAL(UShort , unsigned short, ConstantInt, getZExtValue);
414 GET_CONST_VAL(Short , signed short , ConstantInt, getSExtValue);
415 GET_CONST_VAL(UInt , unsigned int , ConstantInt, getZExtValue);
416 GET_CONST_VAL(Int , signed int , ConstantInt, getSExtValue);
417 GET_CONST_VAL(ULong , uint64_t , ConstantInt, getZExtValue);
418 GET_CONST_VAL(Long , int64_t , ConstantInt, getSExtValue);
419 GET_CONST_VAL(Float , float , ConstantFP, getValue);
420 GET_CONST_VAL(Double , double , ConstantFP, getValue);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000421#undef GET_CONST_VAL
422 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000423 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000424 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000425 else if (const Function *F = dyn_cast<Function>(C))
426 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
427 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
428 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
429 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000430 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000431 break;
432 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000433 cerr << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000434 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000435 }
436 return Result;
437}
438
Nate Begeman37efe672006-04-22 18:53:45 +0000439/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
440/// is the address of the memory at which to store Val, cast to GenericValue *.
441/// It is not a pointer to a GenericValue containing the address at which to
442/// store Val.
Misha Brukman4afac182003-10-10 17:45:12 +0000443///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000444void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000445 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000446 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000447 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000448 case Type::BoolTyID:
449 case Type::UByteTyID:
450 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
451 case Type::UShortTyID:
452 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
453 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
454 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000455 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000456 case Type::FloatTyID:
457 case Type::UIntTyID:
458 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
459 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
460 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
461 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
462 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000463 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000464 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000465 case Type::DoubleTyID:
466 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000467 case Type::LongTyID:
468 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
469 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
470 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
471 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
472 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
473 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
474 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
475 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
476 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000477 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000478 cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000479 }
480 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000481 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000482 case Type::BoolTyID:
483 case Type::UByteTyID:
484 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
485 case Type::UShortTyID:
486 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
487 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
488 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000489 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000490 case Type::FloatTyID:
491 case Type::UIntTyID:
492 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
493 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
494 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
495 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
496 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000497 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000498 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000499 case Type::DoubleTyID:
500 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000501 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000502 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000503 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
504 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
505 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
506 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
507 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
508 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
509 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
510 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000511 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000512 cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000513 }
514 }
515}
516
Misha Brukman4afac182003-10-10 17:45:12 +0000517/// FIXME: document
518///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000519GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
520 const Type *Ty) {
521 GenericValue Result;
Owen Andersona69571c2006-05-03 01:29:57 +0000522 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000523 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000524 case Type::BoolTyID:
525 case Type::UByteTyID:
526 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
527 case Type::UShortTyID:
528 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
529 ((unsigned)Ptr->Untyped[1] << 8);
530 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000531 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000532 case Type::FloatTyID:
533 case Type::UIntTyID:
534 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
535 ((unsigned)Ptr->Untyped[1] << 8) |
536 ((unsigned)Ptr->Untyped[2] << 16) |
537 ((unsigned)Ptr->Untyped[3] << 24);
538 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000539 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000540 goto Load4BytesLittleEndian;
541 case Type::DoubleTyID:
542 case Type::ULongTyID:
543 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
544 ((uint64_t)Ptr->Untyped[1] << 8) |
545 ((uint64_t)Ptr->Untyped[2] << 16) |
546 ((uint64_t)Ptr->Untyped[3] << 24) |
547 ((uint64_t)Ptr->Untyped[4] << 32) |
548 ((uint64_t)Ptr->Untyped[5] << 40) |
549 ((uint64_t)Ptr->Untyped[6] << 48) |
550 ((uint64_t)Ptr->Untyped[7] << 56);
551 break;
552 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000553 cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000554 abort();
555 }
556 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000557 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000558 case Type::BoolTyID:
559 case Type::UByteTyID:
560 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
561 case Type::UShortTyID:
562 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
563 ((unsigned)Ptr->Untyped[0] << 8);
564 break;
565 Load4BytesBigEndian:
566 case Type::FloatTyID:
567 case Type::UIntTyID:
568 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
569 ((unsigned)Ptr->Untyped[2] << 8) |
570 ((unsigned)Ptr->Untyped[1] << 16) |
571 ((unsigned)Ptr->Untyped[0] << 24);
572 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000573 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000574 goto Load4BytesBigEndian;
575 case Type::DoubleTyID:
576 case Type::ULongTyID:
577 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
578 ((uint64_t)Ptr->Untyped[6] << 8) |
579 ((uint64_t)Ptr->Untyped[5] << 16) |
580 ((uint64_t)Ptr->Untyped[4] << 24) |
581 ((uint64_t)Ptr->Untyped[3] << 32) |
582 ((uint64_t)Ptr->Untyped[2] << 40) |
583 ((uint64_t)Ptr->Untyped[1] << 48) |
584 ((uint64_t)Ptr->Untyped[0] << 56);
585 break;
586 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000587 cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000588 abort();
589 }
590 }
591 return Result;
592}
593
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000594// InitializeMemory - Recursive function to apply a Constant value into the
595// specified memory location...
596//
597void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000598 if (isa<UndefValue>(Init)) {
599 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000600 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
601 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000602 getTargetData()->getTypeSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000603 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
604 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
605 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000606 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000607 GenericValue Val = getConstantValue(Init);
608 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
609 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000610 } else if (isa<ConstantAggregateZero>(Init)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000611 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000612 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000613 }
614
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000615 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000616 case Type::ArrayTyID: {
617 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000618 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000619 getTargetData()->getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000620 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
621 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000622 return;
623 }
624
625 case Type::StructTyID: {
626 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
627 const StructLayout *SL =
Owen Andersona69571c2006-05-03 01:29:57 +0000628 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000629 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
630 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000631 return;
632 }
633
634 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000635 cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000636 assert(0 && "Unknown constant type to initialize memory with!");
637 }
638}
639
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000640/// EmitGlobals - Emit all of the global variables to memory, storing their
641/// addresses into GlobalAddress. This must make sure to copy the contents of
642/// their initializers into the memory.
643///
644void ExecutionEngine::emitGlobals() {
Owen Andersona69571c2006-05-03 01:29:57 +0000645 const TargetData *TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000646
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000647 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +0000648 // to hold them. If there is more than one module, do a prepass over globals
649 // to figure out how the different modules should link together.
650 //
651 std::map<std::pair<std::string, const Type*>,
652 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000653
Chris Lattnerfe854032006-08-16 01:24:12 +0000654 if (Modules.size() != 1) {
655 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
656 Module &M = *Modules[m]->getModule();
657 for (Module::const_global_iterator I = M.global_begin(),
658 E = M.global_end(); I != E; ++I) {
659 const GlobalValue *GV = I;
660 if (GV->hasInternalLinkage() || GV->isExternal() ||
661 GV->hasAppendingLinkage() || !GV->hasName())
662 continue;// Ignore external globals and globals with internal linkage.
663
664 const GlobalValue *&GVEntry =
665 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
666
667 // If this is the first time we've seen this global, it is the canonical
668 // version.
669 if (!GVEntry) {
670 GVEntry = GV;
671 continue;
672 }
673
674 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000675 if (GVEntry->hasExternalLinkage() ||
676 GVEntry->hasDLLImportLinkage() ||
677 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000678 continue;
679
680 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
681 // symbol.
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000682 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000683 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000684 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000685 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000686 }
687
688 std::vector<const GlobalValue*> NonCanonicalGlobals;
689 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
690 Module &M = *Modules[m]->getModule();
691 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
692 I != E; ++I) {
693 // In the multi-module case, see what this global maps to.
694 if (!LinkedGlobalsMap.empty()) {
695 if (const GlobalValue *GVEntry =
696 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
697 // If something else is the canonical global, ignore this one.
698 if (GVEntry != &*I) {
699 NonCanonicalGlobals.push_back(I);
700 continue;
701 }
702 }
703 }
704
705 if (!I->isExternal()) {
706 // Get the type of the global.
707 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000708
Chris Lattnerfe854032006-08-16 01:24:12 +0000709 // Allocate some memory for it!
710 unsigned Size = TD->getTypeSize(Ty);
711 addGlobalMapping(I, new char[Size]);
712 } else {
713 // External variable reference. Try to use the dynamic loader to
714 // get a pointer to it.
715 if (void *SymAddr =
716 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
717 addGlobalMapping(I, SymAddr);
718 else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000719 cerr << "Could not resolve external global address: "
720 << I->getName() << "\n";
Chris Lattnerfe854032006-08-16 01:24:12 +0000721 abort();
722 }
723 }
724 }
725
726 // If there are multiple modules, map the non-canonical globals to their
727 // canonical location.
728 if (!NonCanonicalGlobals.empty()) {
729 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
730 const GlobalValue *GV = NonCanonicalGlobals[i];
731 const GlobalValue *CGV =
732 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
733 void *Ptr = getPointerToGlobalIfAvailable(CGV);
734 assert(Ptr && "Canonical global wasn't codegen'd!");
735 addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
736 }
737 }
738
739 // Now that all of the globals are set up in memory, loop through them all and
740 // initialize their contents.
741 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
742 I != E; ++I) {
743 if (!I->isExternal()) {
744 if (!LinkedGlobalsMap.empty()) {
745 if (const GlobalValue *GVEntry =
746 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
747 if (GVEntry != &*I) // Not the canonical variable.
748 continue;
749 }
750 EmitGlobalVariable(I);
751 }
752 }
753 }
Chris Lattner24b0a182003-12-20 02:45:37 +0000754}
755
756// EmitGlobalVariable - This method emits the specified global variable to the
757// address specified in GlobalAddresses, or allocates new memory if it's not
758// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000759void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000760 void *GA = getPointerToGlobalIfAvailable(GV);
Bill Wendling480f0932006-11-27 23:54:50 +0000761 DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
Chris Lattner23c47242004-02-08 19:33:23 +0000762
Chris Lattnerc07ed132003-12-20 03:36:47 +0000763 const Type *ElTy = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000764 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000765 if (GA == 0) {
766 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000767 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000768 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000769 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000770
Chris Lattner24b0a182003-12-20 02:45:37 +0000771 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000772 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000773 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000774}