blob: 3c4d9d500d3a920f153a84c64bd2d5c5afdcf6b1 [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 Lattner2fe4bb02006-03-22 06:07:50 +000027#include <iostream>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000028using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000029
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000030namespace {
31 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000032 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000033}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000034
Chris Lattner2fe4bb02006-03-22 06:07:50 +000035ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0;
36ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0;
37
Chris Lattnerfe854032006-08-16 01:24:12 +000038ExecutionEngine::ExecutionEngine(ModuleProvider *P) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000039 LazyCompilationDisabled = false;
Chris Lattnerfe854032006-08-16 01:24:12 +000040 Modules.push_back(P);
Misha Brukman19684162003-10-16 21:18:05 +000041 assert(P && "ModuleProvider is null?");
42}
43
Chris Lattnerfe854032006-08-16 01:24:12 +000044ExecutionEngine::ExecutionEngine(Module *M) {
Chris Lattner3d6e33d2006-11-09 19:31:15 +000045 LazyCompilationDisabled = false;
Misha Brukman05701572003-10-17 18:31:59 +000046 assert(M && "Module is null?");
Chris Lattnerfe854032006-08-16 01:24:12 +000047 Modules.push_back(new ExistingModuleProvider(M));
Misha Brukman19684162003-10-16 21:18:05 +000048}
49
Brian Gaeke8e539482003-09-04 22:57:27 +000050ExecutionEngine::~ExecutionEngine() {
Chris Lattnerfe854032006-08-16 01:24:12 +000051 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
52 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000053}
54
Chris Lattnerfe854032006-08-16 01:24:12 +000055/// FindFunctionNamed - Search all of the active modules to find the one that
56/// defines FnName. This is very slow operation and shouldn't be used for
57/// general code.
58Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
59 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
60 if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName))
61 return F;
62 }
63 return 0;
64}
65
66
Chris Lattnere7fd5532006-05-08 22:00:52 +000067/// addGlobalMapping - Tell the execution engine that the specified global is
68/// at the specified location. This is used internally as functions are JIT'd
69/// and as global variables are laid out in memory. It can and should also be
70/// used by clients of the EE that want to have an LLVM global overlay
71/// existing data in memory.
72void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
73 MutexGuard locked(lock);
74
75 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
76 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
77 CurVal = Addr;
78
79 // If we are using the reverse mapping, add it too
80 if (!state.getGlobalAddressReverseMap(locked).empty()) {
81 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
82 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
83 V = GV;
84 }
85}
86
87/// clearAllGlobalMappings - Clear all global mappings and start over again
88/// use in dynamic compilation scenarios when you want to move globals
89void ExecutionEngine::clearAllGlobalMappings() {
90 MutexGuard locked(lock);
91
92 state.getGlobalAddressMap(locked).clear();
93 state.getGlobalAddressReverseMap(locked).clear();
94}
95
96/// updateGlobalMapping - Replace an existing mapping for GV with a new
97/// address. This updates both maps as required. If "Addr" is null, the
98/// entry for the global is removed from the mappings.
99void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
100 MutexGuard locked(lock);
101
102 // Deleting from the mapping?
103 if (Addr == 0) {
104 state.getGlobalAddressMap(locked).erase(GV);
105 if (!state.getGlobalAddressReverseMap(locked).empty())
106 state.getGlobalAddressReverseMap(locked).erase(Addr);
107 return;
108 }
109
110 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
111 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
112 state.getGlobalAddressReverseMap(locked).erase(CurVal);
113 CurVal = Addr;
114
115 // If we are using the reverse mapping, add it too
116 if (!state.getGlobalAddressReverseMap(locked).empty()) {
117 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
118 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
119 V = GV;
120 }
121}
122
123/// getPointerToGlobalIfAvailable - This returns the address of the specified
124/// global value if it is has already been codegen'd, otherwise it returns null.
125///
126void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
127 MutexGuard locked(lock);
128
129 std::map<const GlobalValue*, void*>::iterator I =
130 state.getGlobalAddressMap(locked).find(GV);
131 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
132}
133
Chris Lattner55d86482003-12-31 20:21:04 +0000134/// getGlobalValueAtAddress - Return the LLVM global value object that starts
135/// at the specified address.
136///
137const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000138 MutexGuard locked(lock);
139
Chris Lattner55d86482003-12-31 20:21:04 +0000140 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +0000141 if (state.getGlobalAddressReverseMap(locked).empty()) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000142 for (std::map<const GlobalValue*, void *>::iterator
143 I = state.getGlobalAddressMap(locked).begin(),
144 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
145 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
146 I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000147 }
148
149 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000150 state.getGlobalAddressReverseMap(locked).find(Addr);
151 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000152}
Chris Lattner87f03102003-12-26 06:50:30 +0000153
154// CreateArgv - Turn a vector of strings into a nice argv style array of
155// pointers to null terminated strings.
156//
157static void *CreateArgv(ExecutionEngine *EE,
158 const std::vector<std::string> &InputArgv) {
Owen Andersona69571c2006-05-03 01:29:57 +0000159 unsigned PtrSize = EE->getTargetData()->getPointerSize();
Chris Lattner87f03102003-12-26 06:50:30 +0000160 char *Result = new char[(InputArgv.size()+1)*PtrSize];
161
162 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
163 const Type *SBytePtr = PointerType::get(Type::SByteTy);
164
165 for (unsigned i = 0; i != InputArgv.size(); ++i) {
166 unsigned Size = InputArgv[i].size()+1;
167 char *Dest = new char[Size];
168 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000169
Chris Lattner87f03102003-12-26 06:50:30 +0000170 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
171 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000172
Chris Lattner87f03102003-12-26 06:50:30 +0000173 // Endian safe: Result[i] = (PointerTy)Dest;
174 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
175 SBytePtr);
176 }
177
178 // Null terminate it
179 EE->StoreValueToMemory(PTOGV(0),
180 (GenericValue*)(Result+InputArgv.size()*PtrSize),
181 SBytePtr);
182 return Result;
183}
184
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000185
186/// runStaticConstructorsDestructors - This method is used to execute all of
Chris Lattnerfe854032006-08-16 01:24:12 +0000187/// the static constructors or destructors for a program, depending on the
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000188/// value of isDtors.
189void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
190 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000191
Chris Lattnerfe854032006-08-16 01:24:12 +0000192 // Execute global ctors/dtors for each module in the program.
193 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
194 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
195
196 // If this global has internal linkage, or if it has a use, then it must be
197 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
198 // this is the case, don't execute any of the global ctors, __main will do
199 // it.
200 if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue;
201
202 // Should be an array of '{ int, void ()* }' structs. The first value is
203 // the init priority, which we ignore.
204 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
205 if (!InitList) continue;
206 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
207 if (ConstantStruct *CS =
208 dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
209 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000210
Chris Lattnerfe854032006-08-16 01:24:12 +0000211 Constant *FP = CS->getOperand(1);
212 if (FP->isNullValue())
213 break; // Found a null terminator, exit.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000214
Chris Lattnerfe854032006-08-16 01:24:12 +0000215 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +0000216 if (CE->isCast())
Chris Lattnerfe854032006-08-16 01:24:12 +0000217 FP = CE->getOperand(0);
218 if (Function *F = dyn_cast<Function>(FP)) {
219 // Execute the ctor/dtor function!
220 runFunction(F, std::vector<GenericValue>());
221 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000222 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000223 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000224}
225
Chris Lattner87f03102003-12-26 06:50:30 +0000226/// runFunctionAsMain - This is a helper function which wraps runFunction to
227/// handle the common task of starting up main with the specified argc, argv,
228/// and envp parameters.
229int ExecutionEngine::runFunctionAsMain(Function *Fn,
230 const std::vector<std::string> &argv,
231 const char * const * envp) {
232 std::vector<GenericValue> GVArgs;
233 GenericValue GVArgc;
234 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000235 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
236 if (NumArgs) {
237 GVArgs.push_back(GVArgc); // Arg #0 = argc.
238 if (NumArgs > 1) {
239 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
240 assert(((char **)GVTOP(GVArgs[1]))[0] &&
241 "argv[0] was null after CreateArgv");
242 if (NumArgs > 2) {
243 std::vector<std::string> EnvVars;
244 for (unsigned i = 0; envp[i]; ++i)
245 EnvVars.push_back(envp[i]);
246 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
247 }
248 }
249 }
Chris Lattner87f03102003-12-26 06:50:30 +0000250 return runFunction(Fn, GVArgs).IntVal;
251}
252
Misha Brukman19684162003-10-16 21:18:05 +0000253/// If possible, create a JIT, unless the caller specifically requests an
254/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000255/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000256///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000257ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner726c1ef2006-03-23 05:22:51 +0000258 bool ForceInterpreter) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000259 ExecutionEngine *EE = 0;
260
Chris Lattner73011782003-12-28 09:44:37 +0000261 // Unless the interpreter was explicitly selected, try making a JIT.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000262 if (!ForceInterpreter && JITCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000263 EE = JITCtor(MP);
Brian Gaeke82d82772003-09-03 20:34:19 +0000264
265 // If we can't make a JIT, make an interpreter instead.
Chris Lattner2fe4bb02006-03-22 06:07:50 +0000266 if (EE == 0 && InterpCtor)
Chris Lattner726c1ef2006-03-23 05:22:51 +0000267 EE = InterpCtor(MP);
Chris Lattner73011782003-12-28 09:44:37 +0000268
Chris Lattner726c1ef2006-03-23 05:22:51 +0000269 if (EE) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000270 // Make sure we can resolve symbols in the program as well. The zero arg
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000271 // to the function tells DynamicLibrary to load the program, not a library.
Chris Lattner6f3ada52006-05-14 19:01:55 +0000272 try {
273 sys::DynamicLibrary::LoadLibraryPermanently(0);
274 } catch (...) {
275 }
Chris Lattner726c1ef2006-03-23 05:22:51 +0000276 }
Reid Spencerdf5a37e2004-11-29 14:11:29 +0000277
Brian Gaeke82d82772003-09-03 20:34:19 +0000278 return EE;
279}
280
Misha Brukman4afac182003-10-10 17:45:12 +0000281/// getPointerToGlobal - This returns the address of the specified global
282/// value. This may involve code generation if it's a function.
283///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000284void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000285 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000286 return getPointerToFunction(F);
287
Reid Spenceree448632005-07-12 15:51:55 +0000288 MutexGuard locked(lock);
Jeff Cohen68835dd2006-02-07 05:11:57 +0000289 void *p = state.getGlobalAddressMap(locked)[GV];
290 if (p)
291 return p;
292
293 // Global variable might have been added since interpreter started.
294 if (GlobalVariable *GVar =
295 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
296 EmitGlobalVariable(GVar);
297 else
298 assert("Global hasn't had an address allocated yet!");
Reid Spenceree448632005-07-12 15:51:55 +0000299 return state.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000300}
301
Reid Spencer3da59db2006-11-27 01:05:10 +0000302/// This function converts a Constant* into a GenericValue. The interesting
303/// part is if C is a ConstantExpr.
304/// @brief Get a GenericValue for a Constnat*
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000305GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000306 // Declare the result as garbage.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000307 GenericValue Result;
Reid Spencer3da59db2006-11-27 01:05:10 +0000308
309 // If its undefined, return the garbage.
Chris Lattner6f335f92004-10-26 05:35:14 +0000310 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000311
Reid Spencer3da59db2006-11-27 01:05:10 +0000312 // If the value is a ConstantExpr
313 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000314 switch (CE->getOpcode()) {
315 case Instruction::GetElementPtr: {
Reid Spencer3da59db2006-11-27 01:05:10 +0000316 // Compute the index
Chris Lattner9a231222003-05-14 17:51:49 +0000317 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000318 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
319 uint64_t Offset =
320 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000321
Owen Andersona69571c2006-05-03 01:29:57 +0000322 if (getTargetData()->getPointerSize() == 4)
Chris Lattner3db4b622005-10-23 23:54:56 +0000323 Result.IntVal += Offset;
324 else
325 Result.LongVal += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000326 return Result;
327 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000328 case Instruction::Trunc:
329 case Instruction::ZExt:
330 case Instruction::SExt:
331 case Instruction::FPTrunc:
332 case Instruction::FPExt:
333 case Instruction::UIToFP:
334 case Instruction::SIToFP:
335 case Instruction::FPToUI:
336 case Instruction::FPToSI:
337 break;
338 case Instruction::PtrToInt: {
Chris Lattner9a231222003-05-14 17:51:49 +0000339 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000340 GenericValue GV = getConstantValue(Op);
Reid Spencer3da59db2006-11-27 01:05:10 +0000341 return GV;
342 }
343 case Instruction::BitCast: {
344 // Bit casts are no-ops but we can only return the GV of the operand if
345 // they are the same basic type (pointer->pointer, packed->packed, etc.)
346 Constant *Op = CE->getOperand(0);
347 GenericValue GV = getConstantValue(Op);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000348 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000349 return GV;
Reid Spencer3da59db2006-11-27 01:05:10 +0000350 break;
351 }
352 case Instruction::IntToPtr: {
353 // IntToPtr casts are just so special. Cast to intptr_t first.
354 Constant *Op = CE->getOperand(0);
355 GenericValue GV = getConstantValue(Op);
356 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000357 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
358 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
359 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
360 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
361 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
362 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
363 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
364 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
365 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
366 default: assert(0 && "Unknown integral type!");
Reid Spencer3da59db2006-11-27 01:05:10 +0000367 }
Chris Lattner9a231222003-05-14 17:51:49 +0000368 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000369 }
Chris Lattner9a231222003-05-14 17:51:49 +0000370 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000371 switch (CE->getOperand(0)->getType()->getTypeID()) {
372 default: assert(0 && "Bad add type!"); abort();
373 case Type::LongTyID:
374 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000375 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
376 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000377 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000378 case Type::IntTyID:
379 case Type::UIntTyID:
380 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
381 getConstantValue(CE->getOperand(1)).IntVal;
382 break;
383 case Type::ShortTyID:
384 case Type::UShortTyID:
385 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
386 getConstantValue(CE->getOperand(1)).ShortVal;
387 break;
388 case Type::SByteTyID:
389 case Type::UByteTyID:
390 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
391 getConstantValue(CE->getOperand(1)).SByteVal;
392 break;
393 case Type::FloatTyID:
394 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
395 getConstantValue(CE->getOperand(1)).FloatVal;
396 break;
397 case Type::DoubleTyID:
398 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
399 getConstantValue(CE->getOperand(1)).DoubleVal;
400 break;
401 }
Chris Lattner9a231222003-05-14 17:51:49 +0000402 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000403 default:
404 break;
405 }
406 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
407 abort();
408 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000409
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000410 switch (C->getType()->getTypeID()) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000411#define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \
412 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break
413 GET_CONST_VAL(Bool , bool , ConstantBool, getValue);
414 GET_CONST_VAL(UByte , unsigned char , ConstantInt, getZExtValue);
415 GET_CONST_VAL(SByte , signed char , ConstantInt, getSExtValue);
416 GET_CONST_VAL(UShort , unsigned short, ConstantInt, getZExtValue);
417 GET_CONST_VAL(Short , signed short , ConstantInt, getSExtValue);
418 GET_CONST_VAL(UInt , unsigned int , ConstantInt, getZExtValue);
419 GET_CONST_VAL(Int , signed int , ConstantInt, getSExtValue);
420 GET_CONST_VAL(ULong , uint64_t , ConstantInt, getZExtValue);
421 GET_CONST_VAL(Long , int64_t , ConstantInt, getSExtValue);
422 GET_CONST_VAL(Float , float , ConstantFP, getValue);
423 GET_CONST_VAL(Double , double , ConstantFP, getValue);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000424#undef GET_CONST_VAL
425 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000426 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000427 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000428 else if (const Function *F = dyn_cast<Function>(C))
429 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
430 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
431 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
432 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000433 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000434 break;
435 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000436 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000437 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000438 }
439 return Result;
440}
441
Nate Begeman37efe672006-04-22 18:53:45 +0000442/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
443/// is the address of the memory at which to store Val, cast to GenericValue *.
444/// It is not a pointer to a GenericValue containing the address at which to
445/// store Val.
Misha Brukman4afac182003-10-10 17:45:12 +0000446///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000447void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000448 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000449 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000450 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000451 case Type::BoolTyID:
452 case Type::UByteTyID:
453 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
454 case Type::UShortTyID:
455 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
456 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
457 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000458 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000459 case Type::FloatTyID:
460 case Type::UIntTyID:
461 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
462 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
463 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
464 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
465 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000466 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000467 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000468 case Type::DoubleTyID:
469 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000470 case Type::LongTyID:
471 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal );
472 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8);
473 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16);
474 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24);
475 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32);
476 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40);
477 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48);
478 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56);
479 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000480 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000481 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000482 }
483 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000484 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000485 case Type::BoolTyID:
486 case Type::UByteTyID:
487 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
488 case Type::UShortTyID:
489 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
490 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
491 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000492 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000493 case Type::FloatTyID:
494 case Type::UIntTyID:
495 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
496 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
497 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
498 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
499 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000500 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000501 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000502 case Type::DoubleTyID:
503 case Type::ULongTyID:
Chris Lattner813c8152005-01-08 20:13:19 +0000504 case Type::LongTyID:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000505 Ptr->Untyped[7] = (unsigned char)(Val.ULongVal );
Chris Lattner813c8152005-01-08 20:13:19 +0000506 Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8);
507 Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16);
508 Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24);
509 Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32);
510 Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40);
511 Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48);
512 Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56);
513 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000514 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000515 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000516 }
517 }
518}
519
Misha Brukman4afac182003-10-10 17:45:12 +0000520/// FIXME: document
521///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000522GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
523 const Type *Ty) {
524 GenericValue Result;
Owen Andersona69571c2006-05-03 01:29:57 +0000525 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000526 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000527 case Type::BoolTyID:
528 case Type::UByteTyID:
529 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
530 case Type::UShortTyID:
531 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
532 ((unsigned)Ptr->Untyped[1] << 8);
533 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000534 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000535 case Type::FloatTyID:
536 case Type::UIntTyID:
537 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
538 ((unsigned)Ptr->Untyped[1] << 8) |
539 ((unsigned)Ptr->Untyped[2] << 16) |
540 ((unsigned)Ptr->Untyped[3] << 24);
541 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000542 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000543 goto Load4BytesLittleEndian;
544 case Type::DoubleTyID:
545 case Type::ULongTyID:
546 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
547 ((uint64_t)Ptr->Untyped[1] << 8) |
548 ((uint64_t)Ptr->Untyped[2] << 16) |
549 ((uint64_t)Ptr->Untyped[3] << 24) |
550 ((uint64_t)Ptr->Untyped[4] << 32) |
551 ((uint64_t)Ptr->Untyped[5] << 40) |
552 ((uint64_t)Ptr->Untyped[6] << 48) |
553 ((uint64_t)Ptr->Untyped[7] << 56);
554 break;
555 default:
556 std::cout << "Cannot load value of type " << *Ty << "!\n";
557 abort();
558 }
559 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000560 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000561 case Type::BoolTyID:
562 case Type::UByteTyID:
563 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
564 case Type::UShortTyID:
565 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
566 ((unsigned)Ptr->Untyped[0] << 8);
567 break;
568 Load4BytesBigEndian:
569 case Type::FloatTyID:
570 case Type::UIntTyID:
571 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
572 ((unsigned)Ptr->Untyped[2] << 8) |
573 ((unsigned)Ptr->Untyped[1] << 16) |
574 ((unsigned)Ptr->Untyped[0] << 24);
575 break;
Owen Andersona69571c2006-05-03 01:29:57 +0000576 case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000577 goto Load4BytesBigEndian;
578 case Type::DoubleTyID:
579 case Type::ULongTyID:
580 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
581 ((uint64_t)Ptr->Untyped[6] << 8) |
582 ((uint64_t)Ptr->Untyped[5] << 16) |
583 ((uint64_t)Ptr->Untyped[4] << 24) |
584 ((uint64_t)Ptr->Untyped[3] << 32) |
585 ((uint64_t)Ptr->Untyped[2] << 40) |
586 ((uint64_t)Ptr->Untyped[1] << 48) |
587 ((uint64_t)Ptr->Untyped[0] << 56);
588 break;
589 default:
590 std::cout << "Cannot load value of type " << *Ty << "!\n";
591 abort();
592 }
593 }
594 return Result;
595}
596
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000597// InitializeMemory - Recursive function to apply a Constant value into the
598// specified memory location...
599//
600void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000601 if (isa<UndefValue>(Init)) {
602 return;
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000603 } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) {
604 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000605 getTargetData()->getTypeSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000606 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
607 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
608 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000609 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000610 GenericValue Val = getConstantValue(Init);
611 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
612 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000613 } else if (isa<ConstantAggregateZero>(Init)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000614 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000615 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000616 }
617
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000618 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000619 case Type::ArrayTyID: {
620 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000621 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000622 getTargetData()->getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000623 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
624 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000625 return;
626 }
627
628 case Type::StructTyID: {
629 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
630 const StructLayout *SL =
Owen Andersona69571c2006-05-03 01:29:57 +0000631 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000632 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
633 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000634 return;
635 }
636
637 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000638 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000639 assert(0 && "Unknown constant type to initialize memory with!");
640 }
641}
642
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000643/// EmitGlobals - Emit all of the global variables to memory, storing their
644/// addresses into GlobalAddress. This must make sure to copy the contents of
645/// their initializers into the memory.
646///
647void ExecutionEngine::emitGlobals() {
Owen Andersona69571c2006-05-03 01:29:57 +0000648 const TargetData *TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000649
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000650 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +0000651 // to hold them. If there is more than one module, do a prepass over globals
652 // to figure out how the different modules should link together.
653 //
654 std::map<std::pair<std::string, const Type*>,
655 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000656
Chris Lattnerfe854032006-08-16 01:24:12 +0000657 if (Modules.size() != 1) {
658 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
659 Module &M = *Modules[m]->getModule();
660 for (Module::const_global_iterator I = M.global_begin(),
661 E = M.global_end(); I != E; ++I) {
662 const GlobalValue *GV = I;
663 if (GV->hasInternalLinkage() || GV->isExternal() ||
664 GV->hasAppendingLinkage() || !GV->hasName())
665 continue;// Ignore external globals and globals with internal linkage.
666
667 const GlobalValue *&GVEntry =
668 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
669
670 // If this is the first time we've seen this global, it is the canonical
671 // version.
672 if (!GVEntry) {
673 GVEntry = GV;
674 continue;
675 }
676
677 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000678 if (GVEntry->hasExternalLinkage() ||
679 GVEntry->hasDLLImportLinkage() ||
680 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000681 continue;
682
683 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
684 // symbol.
685 if (GV->hasExternalLinkage())
686 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000687 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000688 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000689 }
690
691 std::vector<const GlobalValue*> NonCanonicalGlobals;
692 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
693 Module &M = *Modules[m]->getModule();
694 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
695 I != E; ++I) {
696 // In the multi-module case, see what this global maps to.
697 if (!LinkedGlobalsMap.empty()) {
698 if (const GlobalValue *GVEntry =
699 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
700 // If something else is the canonical global, ignore this one.
701 if (GVEntry != &*I) {
702 NonCanonicalGlobals.push_back(I);
703 continue;
704 }
705 }
706 }
707
708 if (!I->isExternal()) {
709 // Get the type of the global.
710 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000711
Chris Lattnerfe854032006-08-16 01:24:12 +0000712 // Allocate some memory for it!
713 unsigned Size = TD->getTypeSize(Ty);
714 addGlobalMapping(I, new char[Size]);
715 } else {
716 // External variable reference. Try to use the dynamic loader to
717 // get a pointer to it.
718 if (void *SymAddr =
719 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
720 addGlobalMapping(I, SymAddr);
721 else {
722 std::cerr << "Could not resolve external global address: "
723 << I->getName() << "\n";
724 abort();
725 }
726 }
727 }
728
729 // If there are multiple modules, map the non-canonical globals to their
730 // canonical location.
731 if (!NonCanonicalGlobals.empty()) {
732 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
733 const GlobalValue *GV = NonCanonicalGlobals[i];
734 const GlobalValue *CGV =
735 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
736 void *Ptr = getPointerToGlobalIfAvailable(CGV);
737 assert(Ptr && "Canonical global wasn't codegen'd!");
738 addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
739 }
740 }
741
742 // Now that all of the globals are set up in memory, loop through them all and
743 // initialize their contents.
744 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
745 I != E; ++I) {
746 if (!I->isExternal()) {
747 if (!LinkedGlobalsMap.empty()) {
748 if (const GlobalValue *GVEntry =
749 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
750 if (GVEntry != &*I) // Not the canonical variable.
751 continue;
752 }
753 EmitGlobalVariable(I);
754 }
755 }
756 }
Chris Lattner24b0a182003-12-20 02:45:37 +0000757}
758
759// EmitGlobalVariable - This method emits the specified global variable to the
760// address specified in GlobalAddresses, or allocates new memory if it's not
761// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000762void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000763 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000764 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
765
Chris Lattnerc07ed132003-12-20 03:36:47 +0000766 const Type *ElTy = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000767 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000768 if (GA == 0) {
769 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000770 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000771 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000772 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000773
Chris Lattner24b0a182003-12-20 02:45:37 +0000774 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000775 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000776 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000777}