blob: 683b4c75176583ddc491c9ec1ee278d0a60c6132 [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 Lattnerc2ee9b92003-11-19 21:08:57 +000029namespace {
30 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000031 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000032}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000033
Chris Lattner2fe4bb02006-03-22 06:07:50 +000034ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
35ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
36
Chris Lattnerfe854032006-08-16 01:24:12 +000037ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000038 LazyCompilationDisabled = false;
Chris Lattnerfe854032006-08-16 01:24:12 +000039 Modules.push_back(P);
Misha Brukman19684162003-10-16 21:18:05 +000040 assert(P && "ModuleProvider is null?");
41}
42
Chris Lattnerfe854032006-08-16 01:24:12 +000043ExecutionEngine::ExecutionEngine(Module *M) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000044 LazyCompilationDisabled = false;
Misha Brukman05701572003-10-17 18:31:59 +000045 assert(M && "Module is null?");
Chris Lattnerfe854032006-08-16 01:24:12 +000046 Modules.push_back(new ExistingModuleProvider(M));
Misha Brukman19684162003-10-16 21:18:05 +000047}
48
Brian Gaeke8e539482003-09-04 22:57:27 +000049ExecutionEngine::~ExecutionEngine() {
Chris Lattnerfe854032006-08-16 01:24:12 +000050 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
51 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000052}
53
Chris Lattnerfe854032006-08-16 01:24:12 +000054/// FindFunctionNamed - Search all of the active modules to find the one that
55/// defines FnName. This is very slow operation and shouldn't be used for
56/// general code.
57Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
58 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
59 if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName))
60 return F;
61 }
62 return 0;
63}
64
65
Chris Lattnere7fd5532006-05-08 22:00:52 +000066/// addGlobalMapping - Tell the execution engine that the specified global is
67/// at the specified location. This is used internally as functions are JIT'd
68/// and as global variables are laid out in memory. It can and should also be
69/// used by clients of the EE that want to have an LLVM global overlay
70/// existing data in memory.
71void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
72 MutexGuard locked(lock);
73
74 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
75 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
76 CurVal = Addr;
77
78 // If we are using the reverse mapping, add it too
79 if (!state.getGlobalAddressReverseMap(locked).empty()) {
80 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
81 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
82 V = GV;
83 }
84}
85
86/// clearAllGlobalMappings - Clear all global mappings and start over again
87/// use in dynamic compilation scenarios when you want to move globals
88void ExecutionEngine::clearAllGlobalMappings() {
89 MutexGuard locked(lock);
90
91 state.getGlobalAddressMap(locked).clear();
92 state.getGlobalAddressReverseMap(locked).clear();
93}
94
95/// updateGlobalMapping - Replace an existing mapping for GV with a new
96/// address. This updates both maps as required. If "Addr" is null, the
97/// entry for the global is removed from the mappings.
98void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
99 MutexGuard locked(lock);
100
101 // Deleting from the mapping?
102 if (Addr == 0) {
103 state.getGlobalAddressMap(locked).erase(GV);
104 if (!state.getGlobalAddressReverseMap(locked).empty())
105 state.getGlobalAddressReverseMap(locked).erase(Addr);
106 return;
107 }
108
109 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
110 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
111 state.getGlobalAddressReverseMap(locked).erase(CurVal);
112 CurVal = Addr;
113
114 // If we are using the reverse mapping, add it too
115 if (!state.getGlobalAddressReverseMap(locked).empty()) {
116 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
117 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
118 V = GV;
119 }
120}
121
122/// getPointerToGlobalIfAvailable - This returns the address of the specified
123/// global value if it is has already been codegen'd, otherwise it returns null.
124///
125void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
126 MutexGuard locked(lock);
127
128 std::map<const GlobalValue*, void*>::iterator I =
129 state.getGlobalAddressMap(locked).find(GV);
130 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
131}
132
Chris Lattner55d86482003-12-31 20:21:04 +0000133/// getGlobalValueAtAddress - Return the LLVM global value object that starts
134/// at the specified address.
135///
136const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000137 MutexGuard locked(lock);
138
Chris Lattner55d86482003-12-31 20:21:04 +0000139 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +0000140 if (state.getGlobalAddressReverseMap(locked).empty()) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000141 for (std::map<const GlobalValue*, void *>::iterator
142 I = state.getGlobalAddressMap(locked).begin(),
143 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
144 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
145 I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000146 }
147
148 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000149 state.getGlobalAddressReverseMap(locked).find(Addr);
150 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000151}
Chris Lattner87f03102003-12-26 06:50:30 +0000152
153// CreateArgv - Turn a vector of strings into a nice argv style array of
154// pointers to null terminated strings.
155//
156static void *CreateArgv(ExecutionEngine *EE,
157 const std::vector<std::string> &InputArgv) {
Owen Andersona69571c2006-05-03 01:29:57 +0000158 unsigned PtrSize = EE->getTargetData()->getPointerSize();
Chris Lattner87f03102003-12-26 06:50:30 +0000159 char *Result = new char[(InputArgv.size()+1)*PtrSize];
160
Bill Wendling480f0932006-11-27 23:54:50 +0000161 DOUT << "ARGV = " << (void*)Result << "\n";
Chris Lattner87f03102003-12-26 06:50:30 +0000162 const Type *SBytePtr = PointerType::get(Type::SByteTy);
163
164 for (unsigned i = 0; i != InputArgv.size(); ++i) {
165 unsigned Size = InputArgv[i].size()+1;
166 char *Dest = new char[Size];
Bill Wendling480f0932006-11-27 23:54:50 +0000167 DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000168
Chris Lattner87f03102003-12-26 06:50:30 +0000169 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
170 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000171
Chris Lattner87f03102003-12-26 06:50:30 +0000172 // Endian safe: Result[i] = (PointerTy)Dest;
173 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
174 SBytePtr);
175 }
176
177 // Null terminate it
178 EE->StoreValueToMemory(PTOGV(0),
179 (GenericValue*)(Result+InputArgv.size()*PtrSize),
180 SBytePtr);
181 return Result;
182}
183
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000184
185/// runStaticConstructorsDestructors - This method is used to execute all of
Chris Lattnerfe854032006-08-16 01:24:12 +0000186/// the static constructors or destructors for a program, depending on the
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000187/// value of isDtors.
188void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
189 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000190
Chris Lattnerfe854032006-08-16 01:24:12 +0000191 // Execute global ctors/dtors for each module in the program.
192 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
193 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
194
195 // If this global has internal linkage, or if it has a use, then it must be
196 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
197 // this is the case, don't execute any of the global ctors, __main will do
198 // it.
199 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue;
200
201 // Should be an array of '{ int, void ()* }' structs. The first value is
202 // the init priority, which we ignore.
203 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
204 if (!InitList) continue;
205 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
206 if (ConstantStruct *CS =
207 dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
208 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000209
Chris Lattnerfe854032006-08-16 01:24:12 +0000210 Constant *FP = CS->getOperand(1);
211 if (FP->isNullValue())
212 break; // Found a null terminator, exit.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000213
Chris Lattnerfe854032006-08-16 01:24:12 +0000214 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +0000215 if (CE->isCast())
Chris Lattnerfe854032006-08-16 01:24:12 +0000216 FP = CE->getOperand(0);
217 if (Function *F = dyn_cast<Function>(FP)) {
218 // Execute the ctor/dtor function!
219 runFunction(F, std::vector<GenericValue>());
220 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000221 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000222 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000223}
224
Chris Lattner87f03102003-12-26 06:50:30 +0000225/// runFunctionAsMain - This is a helper function which wraps runFunction to
226/// handle the common task of starting up main with the specified argc, argv,
227/// and envp parameters.
228int ExecutionEngine::runFunctionAsMain(Function *Fn,
229 const std::vector<std::string> &argv,
230 const char * const * envp) {
231 std::vector<GenericValue> GVArgs;
232 GenericValue GVArgc;
233 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000234 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
235 if (NumArgs) {
236 GVArgs.push_back(GVArgc); // Arg #0 = argc.
237 if (NumArgs > 1) {
238 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
239 assert(((char **)GVTOP(GVArgs[1]))[0] &&
240 "argv[0] was null after CreateArgv");
241 if (NumArgs > 2) {
242 std::vector<std::string> EnvVars;
243 for (unsigned i = 0; envp[i]; ++i)
244 EnvVars.push_back(envp[i]);
245 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
246 }
247 }
248 }
Chris Lattner87f03102003-12-26 06:50:30 +0000249 return runFunction(Fn, GVArgs).IntVal;
250}
251
Misha Brukman19684162003-10-16 21:18:05 +0000252/// If possible, create a JIT, unless the caller specifically requests an
253/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000254/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000255///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000256ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000257 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000258 ExecutionEngine *EE = 0;
259
Chris Lattner73011782003-12-28 09:44:37 +0000260 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000261 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000262 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000263
264 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000265 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000266 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000267
Chris Lattner726c1ef2006-03-23 05:22:51 +0000268 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000270 // to the function tells DynamicLibrary to load the program, not a library.
Chris Lattner6f3ada52006-05-14 19:01:55 +0000271 try {
272 sys::DynamicLibrary::LoadLibraryPermanently(0);
273 } catch (...) {
274 }
Chris Lattner726c1ef2006-03-23 05:22:51 +0000275 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000276
Brian Gaeke82d82772003-09-03 20:34:19 +0000277 return EE;
278}
279
Misha Brukman4afac182003-10-10 17:45:12 +0000280/// getPointerToGlobal - This returns the address of the specified global
281/// value. This may involve code generation if it's a function.
282///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000283void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000284 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000285 return getPointerToFunction(F);
286
Reid Spenceree448632005-07-12 15:51:55 +0000287 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000288 void *p = state.getGlobalAddressMap(locked)[GV];
289 if (p)
290 return p;
291
292 // Global variable might have been added since interpreter started.
293 if (GlobalVariable *GVar =
294 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
295 EmitGlobalVariable(GVar);
296 else
297 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000298 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000299}
300
Reid Spencer3da59db2006-11-27 01:05:10 +0000301/// This function converts a Constant* into a GenericValue. The interesting
302/// part is if C is a ConstantExpr.
303/// @brief Get a GenericValue for a Constnat*
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000304GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000305 // Declare the result as garbage.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000306 GenericValue Result;
Reid Spencer3da59db2006-11-27 01:05:10 +0000307
308 // If its undefined, return the garbage.
Chris Lattner6f335f92004-10-26 05:35:14 +0000309 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000310
Reid Spencer3da59db2006-11-27 01:05:10 +0000311 // If the value is a ConstantExpr
312 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000313 switch (CE->getOpcode()) {
314 case Instruction::GetElementPtr: {
Reid Spencer3da59db2006-11-27 01:05:10 +0000315 // Compute the index
Chris Lattner9a231222003-05-14 17:51:49 +0000316 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000317 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
318 uint64_t Offset =
319 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000320
Owen Andersona69571c2006-05-03 01:29:57 +0000321 if (getTargetData()->getPointerSize() == 4)
Chris Lattner3db4b622005-10-23 23:54:56 +0000322 Result.IntVal += Offset;
323 else
324 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000325 return Result;
326 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000327 case Instruction::Trunc:
328 case Instruction::ZExt:
329 case Instruction::SExt:
330 case Instruction::FPTrunc:
331 case Instruction::FPExt:
332 case Instruction::UIToFP:
333 case Instruction::SIToFP:
334 case Instruction::FPToUI:
335 case Instruction::FPToSI:
336 break;
337 case Instruction::PtrToInt: {
Chris Lattner9a231222003-05-14 17:51:49 +0000338 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000339 GenericValue GV = getConstantValue(Op);
Reid Spencer3da59db2006-11-27 01:05:10 +0000340 return GV;
341 }
342 case Instruction::BitCast: {
343 // Bit casts are no-ops but we can only return the GV of the operand if
344 // they are the same basic type (pointer->pointer, packed->packed, etc.)
345 Constant *Op = CE->getOperand(0);
346 GenericValue GV = getConstantValue(Op);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000347 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000348 return GV;
Reid Spencer3da59db2006-11-27 01:05:10 +0000349 break;
350 }
351 case Instruction::IntToPtr: {
352 // IntToPtr casts are just so special. Cast to intptr_t first.
353 Constant *Op = CE->getOperand(0);
354 GenericValue GV = getConstantValue(Op);
355 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000356 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
357 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
358 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
359 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
360 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
361 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
362 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
363 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
364 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
365 default: assert(0 && "Unknown integral type!");
Reid Spencer3da59db2006-11-27 01:05:10 +0000366 }
Chris Lattner9a231222003-05-14 17:51:49 +0000367 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000368 }
Chris Lattner9a231222003-05-14 17:51:49 +0000369 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000370 switch (CE->getOperand(0)->getType()->getTypeID()) {
371 default: assert(0 && "Bad add type!"); abort();
372 case Type::LongTyID:
373 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000374 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
375 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000376 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000377 case Type::IntTyID:
378 case Type::UIntTyID:
379 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
380 getConstantValue(CE->getOperand(1)).IntVal;
381 break;
382 case Type::ShortTyID:
383 case Type::UShortTyID:
384 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
385 getConstantValue(CE->getOperand(1)).ShortVal;
386 break;
387 case Type::SByteTyID:
388 case Type::UByteTyID:
389 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
390 getConstantValue(CE->getOperand(1)).SByteVal;
391 break;
392 case Type::FloatTyID:
393 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
394 getConstantValue(CE->getOperand(1)).FloatVal;
395 break;
396 case Type::DoubleTyID:
397 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
398 getConstantValue(CE->getOperand(1)).DoubleVal;
399 break;
400 }
Chris Lattner9a231222003-05-14 17:51:49 +0000401 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000402 default:
403 break;
404 }
Bill Wendling480f0932006-11-27 23:54:50 +0000405 llvm_cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
Chris Lattner9a231222003-05-14 17:51:49 +0000406 abort();
407 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000408
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000409 switch (C->getType()->getTypeID()) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000410#define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \
411 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break
412 GET_CONST_VAL(Bool , bool , ConstantBool, getValue);
413 GET_CONST_VAL(UByte , unsigned char , ConstantInt, getZExtValue);
414 GET_CONST_VAL(SByte , signed char , ConstantInt, getSExtValue);
415 GET_CONST_VAL(UShort , unsigned short, ConstantInt, getZExtValue);
416 GET_CONST_VAL(Short , signed short , ConstantInt, getSExtValue);
417 GET_CONST_VAL(UInt , unsigned int , ConstantInt, getZExtValue);
418 GET_CONST_VAL(Int , signed int , ConstantInt, getSExtValue);
419 GET_CONST_VAL(ULong , uint64_t , ConstantInt, getZExtValue);
420 GET_CONST_VAL(Long , int64_t , ConstantInt, getSExtValue);
421 GET_CONST_VAL(Float , float , ConstantFP, getValue);
422 GET_CONST_VAL(Double , double , ConstantFP, getValue);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000423#undef GET_CONST_VAL
424 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000425 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000426 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000427 else if (const Function *F = dyn_cast<Function>(C))
428 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
429 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
430 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
431 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000432 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000433 break;
434 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000435 llvm_cerr << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000436 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000437 }
438 return Result;
439}
440
Nate Begeman37efe672006-04-22 18:53:45 +0000441/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
442/// is the address of the memory at which to store Val, cast to GenericValue *.
443/// It is not a pointer to a GenericValue containing the address at which to
444/// store Val.
Misha Brukman4afac182003-10-10 17:45:12 +0000445///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000446void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000447 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000448 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000449 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000450 case Type::BoolTyID:
451 case Type::UByteTyID:
452 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
453 case Type::UShortTyID:
454 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
455 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
456 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000457 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000458 case Type::FloatTyID:
459 case Type::UIntTyID:
460 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
461 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
462 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
463 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
464 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000465 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000466 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000467 case Type::DoubleTyID:
468 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000469 case Type::LongTyID:
470 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
471 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
472 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
473 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
474 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
475 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
476 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
477 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
478 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000479 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000480 llvm_cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000481 }
482 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000483 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000484 case Type::BoolTyID:
485 case Type::UByteTyID:
486 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
487 case Type::UShortTyID:
488 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
489 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
490 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000491 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000492 case Type::FloatTyID:
493 case Type::UIntTyID:
494 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
495 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
496 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
497 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
498 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000499 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000500 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000501 case Type::DoubleTyID:
502 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000503 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000505 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
506 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
507 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
508 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
509 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
510 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
511 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
512 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000513 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000514 llvm_cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000515 }
516 }
517}
518
Misha Brukman4afac182003-10-10 17:45:12 +0000519/// FIXME: document
520///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000521GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
522 const Type *Ty) {
523 GenericValue Result;
Owen Andersona69571c2006-05-03 01:29:57 +0000524 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000525 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000526 case Type::BoolTyID:
527 case Type::UByteTyID:
528 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
529 case Type::UShortTyID:
530 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
531 ((unsigned)Ptr->Untyped[1] << 8);
532 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000533 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000534 case Type::FloatTyID:
535 case Type::UIntTyID:
536 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
537 ((unsigned)Ptr->Untyped[1] << 8) |
538 ((unsigned)Ptr->Untyped[2] << 16) |
539 ((unsigned)Ptr->Untyped[3] << 24);
540 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000541 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000542 goto Load4BytesLittleEndian;
543 case Type::DoubleTyID:
544 case Type::ULongTyID:
545 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
546 ((uint64_t)Ptr->Untyped[1] << 8) |
547 ((uint64_t)Ptr->Untyped[2] << 16) |
548 ((uint64_t)Ptr->Untyped[3] << 24) |
549 ((uint64_t)Ptr->Untyped[4] << 32) |
550 ((uint64_t)Ptr->Untyped[5] << 40) |
551 ((uint64_t)Ptr->Untyped[6] << 48) |
552 ((uint64_t)Ptr->Untyped[7] << 56);
553 break;
554 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000555 llvm_cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000556 abort();
557 }
558 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000559 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000560 case Type::BoolTyID:
561 case Type::UByteTyID:
562 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
563 case Type::UShortTyID:
564 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
565 ((unsigned)Ptr->Untyped[0] << 8);
566 break;
567 Load4BytesBigEndian:
568 case Type::FloatTyID:
569 case Type::UIntTyID:
570 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
571 ((unsigned)Ptr->Untyped[2] << 8) |
572 ((unsigned)Ptr->Untyped[1] << 16) |
573 ((unsigned)Ptr->Untyped[0] << 24);
574 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000575 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000576 goto Load4BytesBigEndian;
577 case Type::DoubleTyID:
578 case Type::ULongTyID:
579 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
580 ((uint64_t)Ptr->Untyped[6] << 8) |
581 ((uint64_t)Ptr->Untyped[5] << 16) |
582 ((uint64_t)Ptr->Untyped[4] << 24) |
583 ((uint64_t)Ptr->Untyped[3] << 32) |
584 ((uint64_t)Ptr->Untyped[2] << 40) |
585 ((uint64_t)Ptr->Untyped[1] << 48) |
586 ((uint64_t)Ptr->Untyped[0] << 56);
587 break;
588 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000589 llvm_cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000590 abort();
591 }
592 }
593 return Result;
594}
595
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000596// InitializeMemory - Recursive function to apply a Constant value into the
597// specified memory location...
598//
599void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000600 if (isa<UndefValue>(Init)) {
601 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000602 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
603 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000604 getTargetData()->getTypeSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000605 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
606 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
607 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000608 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000609 GenericValue Val = getConstantValue(Init);
610 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
611 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000612 } else if (isa<ConstantAggregateZero>(Init)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000613 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000614 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000615 }
616
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000617 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000618 case Type::ArrayTyID: {
619 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000620 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000621 getTargetData()->getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000622 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
623 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000624 return;
625 }
626
627 case Type::StructTyID: {
628 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
629 const StructLayout *SL =
Owen Andersona69571c2006-05-03 01:29:57 +0000630 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000631 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
632 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000633 return;
634 }
635
636 default:
Bill Wendling480f0932006-11-27 23:54:50 +0000637 llvm_cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000638 assert(0 && "Unknown constant type to initialize memory with!");
639 }
640}
641
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000642/// EmitGlobals - Emit all of the global variables to memory, storing their
643/// addresses into GlobalAddress. This must make sure to copy the contents of
644/// their initializers into the memory.
645///
646void ExecutionEngine::emitGlobals() {
Owen Andersona69571c2006-05-03 01:29:57 +0000647 const TargetData *TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000648
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000649 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +0000650 // to hold them. If there is more than one module, do a prepass over globals
651 // to figure out how the different modules should link together.
652 //
653 std::map<std::pair<std::string, const Type*>,
654 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000655
Chris Lattnerfe854032006-08-16 01:24:12 +0000656 if (Modules.size() != 1) {
657 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
658 Module &M = *Modules[m]->getModule();
659 for (Module::const_global_iterator I = M.global_begin(),
660 E = M.global_end(); I != E; ++I) {
661 const GlobalValue *GV = I;
662 if (GV->hasInternalLinkage() || GV->isExternal() ||
663 GV->hasAppendingLinkage() || !GV->hasName())
664 continue;// Ignore external globals and globals with internal linkage.
665
666 const GlobalValue *&GVEntry =
667 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
668
669 // If this is the first time we've seen this global, it is the canonical
670 // version.
671 if (!GVEntry) {
672 GVEntry = GV;
673 continue;
674 }
675
676 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000677 if (GVEntry->hasExternalLinkage() ||
678 GVEntry->hasDLLImportLinkage() ||
679 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000680 continue;
681
682 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
683 // symbol.
684 if (GV->hasExternalLinkage())
685 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000686 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000687 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000688 }
689
690 std::vector<const GlobalValue*> NonCanonicalGlobals;
691 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
692 Module &M = *Modules[m]->getModule();
693 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
694 I != E; ++I) {
695 // In the multi-module case, see what this global maps to.
696 if (!LinkedGlobalsMap.empty()) {
697 if (const GlobalValue *GVEntry =
698 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
699 // If something else is the canonical global, ignore this one.
700 if (GVEntry != &*I) {
701 NonCanonicalGlobals.push_back(I);
702 continue;
703 }
704 }
705 }
706
707 if (!I->isExternal()) {
708 // Get the type of the global.
709 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000710
Chris Lattnerfe854032006-08-16 01:24:12 +0000711 // Allocate some memory for it!
712 unsigned Size = TD->getTypeSize(Ty);
713 addGlobalMapping(I, new char[Size]);
714 } else {
715 // External variable reference. Try to use the dynamic loader to
716 // get a pointer to it.
717 if (void *SymAddr =
718 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
719 addGlobalMapping(I, SymAddr);
720 else {
Bill Wendling480f0932006-11-27 23:54:50 +0000721 llvm_cerr << "Could not resolve external global address: "
Chris Lattnerfe854032006-08-16 01:24:12 +0000722 << I->getName() << "\n";
723 abort();
724 }
725 }
726 }
727
728 // If there are multiple modules, map the non-canonical globals to their
729 // canonical location.
730 if (!NonCanonicalGlobals.empty()) {
731 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
732 const GlobalValue *GV = NonCanonicalGlobals[i];
733 const GlobalValue *CGV =
734 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
735 void *Ptr = getPointerToGlobalIfAvailable(CGV);
736 assert(Ptr && "Canonical global wasn't codegen'd!");
737 addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
738 }
739 }
740
741 // Now that all of the globals are set up in memory, loop through them all and
742 // initialize their contents.
743 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
744 I != E; ++I) {
745 if (!I->isExternal()) {
746 if (!LinkedGlobalsMap.empty()) {
747 if (const GlobalValue *GVEntry =
748 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
749 if (GVEntry != &*I) // Not the canonical variable.
750 continue;
751 }
752 EmitGlobalVariable(I);
753 }
754 }
755 }
Chris Lattner24b0a182003-12-20 02:45:37 +0000756}
757
758// EmitGlobalVariable - This method emits the specified global variable to the
759// address specified in GlobalAddresses, or allocates new memory if it's not
760// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000761void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000762 void *GA = getPointerToGlobalIfAvailable(GV);
Bill Wendling480f0932006-11-27 23:54:50 +0000763 DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
Chris Lattner23c47242004-02-08 19:33:23 +0000764
Chris Lattnerc07ed132003-12-20 03:36:47 +0000765 const Type *ElTy = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000766 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000767 if (GA == 0) {
768 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000769 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000770 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000771 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000772
Chris Lattner24b0a182003-12-20 02:45:37 +0000773 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000774 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000775 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000776}