blob: 92e0020ca8fc2bc139d4d3b590e8b3e5661f80c6 [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() {
Reid Spencerd4c0e622007-03-03 18:19:18 +000048 clearAllGlobalMappings();
Chris Lattnerfe854032006-08-16 01:24:12 +000049 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
50 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000051}
52
Chris Lattnerfe854032006-08-16 01:24:12 +000053/// FindFunctionNamed - Search all of the active modules to find the one that
54/// defines FnName. This is very slow operation and shouldn't be used for
55/// general code.
56Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
57 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Reid Spencer688b0492007-02-05 21:19:13 +000058 if (Function *F = Modules[i]->getModule()->getFunction(FnName))
Chris Lattnerfe854032006-08-16 01:24:12 +000059 return F;
60 }
61 return 0;
62}
63
64
Chris Lattnere7fd5532006-05-08 22:00:52 +000065/// addGlobalMapping - Tell the execution engine that the specified global is
66/// at the specified location. This is used internally as functions are JIT'd
67/// and as global variables are laid out in memory. It can and should also be
68/// used by clients of the EE that want to have an LLVM global overlay
69/// existing data in memory.
70void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
71 MutexGuard locked(lock);
72
73 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
74 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
75 CurVal = Addr;
76
77 // If we are using the reverse mapping, add it too
78 if (!state.getGlobalAddressReverseMap(locked).empty()) {
79 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
80 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
81 V = GV;
82 }
83}
84
85/// clearAllGlobalMappings - Clear all global mappings and start over again
86/// use in dynamic compilation scenarios when you want to move globals
87void ExecutionEngine::clearAllGlobalMappings() {
88 MutexGuard locked(lock);
89
90 state.getGlobalAddressMap(locked).clear();
91 state.getGlobalAddressReverseMap(locked).clear();
92}
93
94/// updateGlobalMapping - Replace an existing mapping for GV with a new
95/// address. This updates both maps as required. If "Addr" is null, the
96/// entry for the global is removed from the mappings.
97void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
98 MutexGuard locked(lock);
99
100 // Deleting from the mapping?
101 if (Addr == 0) {
102 state.getGlobalAddressMap(locked).erase(GV);
103 if (!state.getGlobalAddressReverseMap(locked).empty())
104 state.getGlobalAddressReverseMap(locked).erase(Addr);
105 return;
106 }
107
108 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
109 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
110 state.getGlobalAddressReverseMap(locked).erase(CurVal);
111 CurVal = Addr;
112
113 // If we are using the reverse mapping, add it too
114 if (!state.getGlobalAddressReverseMap(locked).empty()) {
115 const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
116 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
117 V = GV;
118 }
119}
120
121/// getPointerToGlobalIfAvailable - This returns the address of the specified
122/// global value if it is has already been codegen'd, otherwise it returns null.
123///
124void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
125 MutexGuard locked(lock);
126
127 std::map<const GlobalValue*, void*>::iterator I =
128 state.getGlobalAddressMap(locked).find(GV);
129 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
130}
131
Chris Lattner55d86482003-12-31 20:21:04 +0000132/// getGlobalValueAtAddress - Return the LLVM global value object that starts
133/// at the specified address.
134///
135const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000136 MutexGuard locked(lock);
137
Chris Lattner55d86482003-12-31 20:21:04 +0000138 // If we haven't computed the reverse mapping yet, do so first.
Reid Spenceree448632005-07-12 15:51:55 +0000139 if (state.getGlobalAddressReverseMap(locked).empty()) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000140 for (std::map<const GlobalValue*, void *>::iterator
141 I = state.getGlobalAddressMap(locked).begin(),
142 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
143 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
144 I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000145 }
146
147 std::map<void *, const GlobalValue*>::iterator I =
Reid Spenceree448632005-07-12 15:51:55 +0000148 state.getGlobalAddressReverseMap(locked).find(Addr);
149 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000150}
Chris Lattner87f03102003-12-26 06:50:30 +0000151
152// CreateArgv - Turn a vector of strings into a nice argv style array of
153// pointers to null terminated strings.
154//
155static void *CreateArgv(ExecutionEngine *EE,
156 const std::vector<std::string> &InputArgv) {
Owen Andersona69571c2006-05-03 01:29:57 +0000157 unsigned PtrSize = EE->getTargetData()->getPointerSize();
Chris Lattner87f03102003-12-26 06:50:30 +0000158 char *Result = new char[(InputArgv.size()+1)*PtrSize];
159
Bill Wendling480f0932006-11-27 23:54:50 +0000160 DOUT << "ARGV = " << (void*)Result << "\n";
Reid Spencere49661b2006-12-31 05:51:36 +0000161 const Type *SBytePtr = PointerType::get(Type::Int8Ty);
Chris Lattner87f03102003-12-26 06:50:30 +0000162
163 for (unsigned i = 0; i != InputArgv.size(); ++i) {
164 unsigned Size = InputArgv[i].size()+1;
165 char *Dest = new char[Size];
Bill Wendling480f0932006-11-27 23:54:50 +0000166 DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +0000167
Chris Lattner87f03102003-12-26 06:50:30 +0000168 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
169 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170
Chris Lattner87f03102003-12-26 06:50:30 +0000171 // Endian safe: Result[i] = (PointerTy)Dest;
172 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
173 SBytePtr);
174 }
175
176 // Null terminate it
177 EE->StoreValueToMemory(PTOGV(0),
178 (GenericValue*)(Result+InputArgv.size()*PtrSize),
179 SBytePtr);
180 return Result;
181}
182
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000183
184/// runStaticConstructorsDestructors - This method is used to execute all of
Chris Lattnerfe854032006-08-16 01:24:12 +0000185/// the static constructors or destructors for a program, depending on the
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000186/// value of isDtors.
187void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
188 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000189
Chris Lattnerfe854032006-08-16 01:24:12 +0000190 // Execute global ctors/dtors for each module in the program.
191 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
192 GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name);
193
194 // If this global has internal linkage, or if it has a use, then it must be
195 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
196 // this is the case, don't execute any of the global ctors, __main will do
197 // it.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000198 if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) continue;
Chris Lattnerfe854032006-08-16 01:24:12 +0000199
200 // Should be an array of '{ int, void ()* }' structs. The first value is
201 // the init priority, which we ignore.
202 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
203 if (!InitList) continue;
204 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
205 if (ConstantStruct *CS =
206 dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
207 if (CS->getNumOperands() != 2) break; // Not array of 2-element structs.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000208
Chris Lattnerfe854032006-08-16 01:24:12 +0000209 Constant *FP = CS->getOperand(1);
210 if (FP->isNullValue())
211 break; // Found a null terminator, exit.
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000212
Chris Lattnerfe854032006-08-16 01:24:12 +0000213 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +0000214 if (CE->isCast())
Chris Lattnerfe854032006-08-16 01:24:12 +0000215 FP = CE->getOperand(0);
216 if (Function *F = dyn_cast<Function>(FP)) {
217 // Execute the ctor/dtor function!
218 runFunction(F, std::vector<GenericValue>());
219 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000220 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000221 }
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000222}
223
Chris Lattner87f03102003-12-26 06:50:30 +0000224/// runFunctionAsMain - This is a helper function which wraps runFunction to
225/// handle the common task of starting up main with the specified argc, argv,
226/// and envp parameters.
227int ExecutionEngine::runFunctionAsMain(Function *Fn,
228 const std::vector<std::string> &argv,
229 const char * const * envp) {
230 std::vector<GenericValue> GVArgs;
231 GenericValue GVArgc;
Reid Spencere49661b2006-12-31 05:51:36 +0000232 GVArgc.Int32Val = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000233 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
234 if (NumArgs) {
235 GVArgs.push_back(GVArgc); // Arg #0 = argc.
236 if (NumArgs > 1) {
237 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
238 assert(((char **)GVTOP(GVArgs[1]))[0] &&
239 "argv[0] was null after CreateArgv");
240 if (NumArgs > 2) {
241 std::vector<std::string> EnvVars;
242 for (unsigned i = 0; envp[i]; ++i)
243 EnvVars.push_back(envp[i]);
244 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
245 }
246 }
247 }
Reid Spencere49661b2006-12-31 05:51:36 +0000248 return runFunction(Fn, GVArgs).Int32Val;
Chris Lattner87f03102003-12-26 06:50:30 +0000249}
250
Misha Brukman19684162003-10-16 21:18:05 +0000251/// If possible, create a JIT, unless the caller specifically requests an
252/// Interpreter or there's an error. If even an Interpreter cannot be created,
Misha Brukmanedf128a2005-04-21 22:36:52 +0000253/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000254///
Misha Brukmanedf128a2005-04-21 22:36:52 +0000255ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Reid Spencerd4c0e622007-03-03 18:19:18 +0000256 bool ForceInterpreter,
257 std::string *ErrorStr) {
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)
Reid Spencerd4c0e622007-03-03 18:19:18 +0000262 EE = JITCtor(MP, ErrorStr);
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)
Reid Spencerd4c0e622007-03-03 18:19:18 +0000266 EE = InterpCtor(MP, ErrorStr);
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
Chris Lattner64f150f2007-02-14 06:20:04 +0000297 assert(0 && "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 Spencera54b7cb2007-01-12 07:05:14 +0000301/// This macro is used to handle a variety of situations involing integer
302/// values where the action should be done to one of the GenericValue members.
303/// THEINTTY is a const Type * for the integer type. ACTION1 comes before
304/// the GenericValue, ACTION2 comes after.
305#define DO_FOR_INTEGER(THEINTTY, ACTION) \
306 { \
307 unsigned BitWidth = cast<IntegerType>(THEINTTY)->getBitWidth(); \
308 if (BitWidth == 1) {\
309 ACTION(Int1Val); \
310 } else if (BitWidth <= 8) {\
311 ACTION(Int8Val); \
312 } else if (BitWidth <= 16) {\
313 ACTION(Int16Val); \
314 } else if (BitWidth <= 32) { \
315 ACTION(Int32Val); \
316 } else if (BitWidth <= 64) { \
317 ACTION(Int64Val); \
318 } else {\
319 assert(0 && "Not implemented: integer types > 64 bits"); \
320 } \
321 }
322
Reid Spencer3da59db2006-11-27 01:05:10 +0000323/// This function converts a Constant* into a GenericValue. The interesting
324/// part is if C is a ConstantExpr.
325/// @brief Get a GenericValue for a Constnat*
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000326GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000327 // Declare the result as garbage.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000328 GenericValue Result;
Reid Spencer3da59db2006-11-27 01:05:10 +0000329
330 // If its undefined, return the garbage.
Chris Lattner6f335f92004-10-26 05:35:14 +0000331 if (isa<UndefValue>(C)) return Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000332
Reid Spencer3da59db2006-11-27 01:05:10 +0000333 // If the value is a ConstantExpr
334 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000335 switch (CE->getOpcode()) {
336 case Instruction::GetElementPtr: {
Reid Spencer3da59db2006-11-27 01:05:10 +0000337 // Compute the index
Chris Lattner9a231222003-05-14 17:51:49 +0000338 Result = getConstantValue(CE->getOperand(0));
Chris Lattner829621c2007-02-10 20:35:22 +0000339 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000340 uint64_t Offset =
Chris Lattner829621c2007-02-10 20:35:22 +0000341 TD->getIndexedOffset(CE->getOperand(0)->getType(),
342 &Indices[0], Indices.size());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000343
Owen Andersona69571c2006-05-03 01:29:57 +0000344 if (getTargetData()->getPointerSize() == 4)
Reid Spencere49661b2006-12-31 05:51:36 +0000345 Result.Int32Val += Offset;
Chris Lattner3db4b622005-10-23 23:54:56 +0000346 else
Reid Spencere49661b2006-12-31 05:51:36 +0000347 Result.Int64Val += Offset;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000348 return Result;
349 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000350 case Instruction::Trunc:
351 case Instruction::ZExt:
352 case Instruction::SExt:
353 case Instruction::FPTrunc:
354 case Instruction::FPExt:
355 case Instruction::UIToFP:
356 case Instruction::SIToFP:
357 case Instruction::FPToUI:
358 case Instruction::FPToSI:
359 break;
360 case Instruction::PtrToInt: {
Chris Lattner9a231222003-05-14 17:51:49 +0000361 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000362 GenericValue GV = getConstantValue(Op);
Reid Spencer3da59db2006-11-27 01:05:10 +0000363 return GV;
364 }
365 case Instruction::BitCast: {
366 // Bit casts are no-ops but we can only return the GV of the operand if
367 // they are the same basic type (pointer->pointer, packed->packed, etc.)
368 Constant *Op = CE->getOperand(0);
369 GenericValue GV = getConstantValue(Op);
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000370 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000371 return GV;
Reid Spencer3da59db2006-11-27 01:05:10 +0000372 break;
373 }
374 case Instruction::IntToPtr: {
375 // IntToPtr casts are just so special. Cast to intptr_t first.
376 Constant *Op = CE->getOperand(0);
377 GenericValue GV = getConstantValue(Op);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000378#define INT_TO_PTR_ACTION(FIELD) \
379 return PTOGV((void*)(uintptr_t)GV.FIELD)
380 DO_FOR_INTEGER(Op->getType(), INT_TO_PTR_ACTION)
381#undef INT_TO_PTR_ACTION
Chris Lattner9a231222003-05-14 17:51:49 +0000382 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000383 }
Chris Lattner9a231222003-05-14 17:51:49 +0000384 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000385 switch (CE->getOperand(0)->getType()->getTypeID()) {
386 default: assert(0 && "Bad add type!"); abort();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000387 case Type::IntegerTyID:
388#define ADD_ACTION(FIELD) \
389 Result.FIELD = getConstantValue(CE->getOperand(0)).FIELD + \
390 getConstantValue(CE->getOperand(1)).FIELD;
391 DO_FOR_INTEGER(CE->getOperand(0)->getType(),ADD_ACTION);
392#undef ADD_ACTION
Chris Lattner5f90cb82004-07-11 08:01:11 +0000393 break;
394 case Type::FloatTyID:
395 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
396 getConstantValue(CE->getOperand(1)).FloatVal;
397 break;
398 case Type::DoubleTyID:
399 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
400 getConstantValue(CE->getOperand(1)).DoubleVal;
401 break;
402 }
Chris Lattner9a231222003-05-14 17:51:49 +0000403 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000404 default:
405 break;
406 }
Bill Wendlinge8156192006-12-07 01:30:32 +0000407 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
Chris Lattner9a231222003-05-14 17:51:49 +0000408 abort();
409 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000410
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000411 switch (C->getType()->getTypeID()) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000412#define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \
413 case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break
Reid Spencere49661b2006-12-31 05:51:36 +0000414 GET_CONST_VAL(Float , float , ConstantFP, getValue);
415 GET_CONST_VAL(Double, double , ConstantFP, getValue);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000416#undef GET_CONST_VAL
Reid Spencera54b7cb2007-01-12 07:05:14 +0000417 case Type::IntegerTyID: {
418 unsigned BitWidth = cast<IntegerType>(C->getType())->getBitWidth();
419 if (BitWidth == 1)
420 Result.Int1Val = (bool)cast<ConstantInt>(C)->getZExtValue();
421 else if (BitWidth <= 8)
422 Result.Int8Val = (uint8_t )cast<ConstantInt>(C)->getZExtValue();
423 else if (BitWidth <= 16)
424 Result.Int16Val = (uint16_t )cast<ConstantInt>(C)->getZExtValue();
425 else if (BitWidth <= 32)
426 Result.Int32Val = (uint32_t )cast<ConstantInt>(C)->getZExtValue();
427 else if (BitWidth <= 64)
428 Result.Int64Val = (uint64_t )cast<ConstantInt>(C)->getZExtValue();
429 else
Reid Spencer93a28752007-03-03 06:18:03 +0000430 Result.APIntVal = const_cast<APInt*>(&cast<ConstantInt>(C)->getValue());
Reid Spencera54b7cb2007-01-12 07:05:14 +0000431 break;
432 }
433
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000434 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000435 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000436 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000437 else if (const Function *F = dyn_cast<Function>(C))
438 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
439 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
440 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
441 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000442 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000443 break;
444 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000445 cerr << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000446 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000447 }
448 return Result;
449}
450
Nate Begeman37efe672006-04-22 18:53:45 +0000451/// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
452/// is the address of the memory at which to store Val, cast to GenericValue *.
453/// It is not a pointer to a GenericValue containing the address at which to
454/// store Val.
Misha Brukman4afac182003-10-10 17:45:12 +0000455///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000456void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000457 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000458 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000459 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000460 case Type::IntegerTyID: {
461 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc1030572007-01-19 21:13:56 +0000462 uint64_t BitMask = cast<IntegerType>(Ty)->getBitMask();
Reid Spencerf89aec62007-01-18 01:24:02 +0000463 GenericValue TmpVal = Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000464 if (BitWidth <= 8)
Reid Spencerf89aec62007-01-18 01:24:02 +0000465 Ptr->Untyped[0] = Val.Int8Val & BitMask;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000466 else if (BitWidth <= 16) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000467 TmpVal.Int16Val &= BitMask;
468 Ptr->Untyped[0] = TmpVal.Int16Val & 255;
469 Ptr->Untyped[1] = (TmpVal.Int16Val >> 8) & 255;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000470 } else if (BitWidth <= 32) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000471 TmpVal.Int32Val &= BitMask;
472 Ptr->Untyped[0] = TmpVal.Int32Val & 255;
473 Ptr->Untyped[1] = (TmpVal.Int32Val >> 8) & 255;
474 Ptr->Untyped[2] = (TmpVal.Int32Val >> 16) & 255;
475 Ptr->Untyped[3] = (TmpVal.Int32Val >> 24) & 255;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000476 } else if (BitWidth <= 64) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000477 TmpVal.Int64Val &= BitMask;
478 Ptr->Untyped[0] = (unsigned char)(TmpVal.Int64Val );
479 Ptr->Untyped[1] = (unsigned char)(TmpVal.Int64Val >> 8);
480 Ptr->Untyped[2] = (unsigned char)(TmpVal.Int64Val >> 16);
481 Ptr->Untyped[3] = (unsigned char)(TmpVal.Int64Val >> 24);
482 Ptr->Untyped[4] = (unsigned char)(TmpVal.Int64Val >> 32);
483 Ptr->Untyped[5] = (unsigned char)(TmpVal.Int64Val >> 40);
484 Ptr->Untyped[6] = (unsigned char)(TmpVal.Int64Val >> 48);
485 Ptr->Untyped[7] = (unsigned char)(TmpVal.Int64Val >> 56);
Reid Spencer93a28752007-03-03 06:18:03 +0000486 } else {
487 uint64_t *Dest = (uint64_t*)Ptr;
488 const uint64_t *Src = Val.APIntVal->getRawData();
489 for (uint32_t i = 0; i < Val.APIntVal->getNumWords(); ++i)
490 Dest[i] = Src[i];
491 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000492 break;
493 }
494Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000495 case Type::FloatTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000496 Ptr->Untyped[0] = Val.Int32Val & 255;
497 Ptr->Untyped[1] = (Val.Int32Val >> 8) & 255;
498 Ptr->Untyped[2] = (Val.Int32Val >> 16) & 255;
499 Ptr->Untyped[3] = (Val.Int32Val >> 24) & 255;
500 break;
501 case Type::PointerTyID:
502 if (getTargetData()->getPointerSize() == 4)
503 goto Store4BytesLittleEndian;
504 /* FALL THROUGH */
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000505 case Type::DoubleTyID:
Reid Spencere49661b2006-12-31 05:51:36 +0000506 Ptr->Untyped[0] = (unsigned char)(Val.Int64Val );
507 Ptr->Untyped[1] = (unsigned char)(Val.Int64Val >> 8);
508 Ptr->Untyped[2] = (unsigned char)(Val.Int64Val >> 16);
509 Ptr->Untyped[3] = (unsigned char)(Val.Int64Val >> 24);
510 Ptr->Untyped[4] = (unsigned char)(Val.Int64Val >> 32);
511 Ptr->Untyped[5] = (unsigned char)(Val.Int64Val >> 40);
512 Ptr->Untyped[6] = (unsigned char)(Val.Int64Val >> 48);
513 Ptr->Untyped[7] = (unsigned char)(Val.Int64Val >> 56);
Chris Lattner813c8152005-01-08 20:13:19 +0000514 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000515 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000516 cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000517 }
518 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000519 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000520 case Type::IntegerTyID: {
521 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencerc1030572007-01-19 21:13:56 +0000522 uint64_t BitMask = cast<IntegerType>(Ty)->getBitMask();
Reid Spencerf89aec62007-01-18 01:24:02 +0000523 GenericValue TmpVal = Val;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000524 if (BitWidth <= 8)
Reid Spencerf89aec62007-01-18 01:24:02 +0000525 Ptr->Untyped[0] = Val.Int8Val & BitMask;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000526 else if (BitWidth <= 16) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000527 TmpVal.Int16Val &= BitMask;
528 Ptr->Untyped[1] = TmpVal.Int16Val & 255;
529 Ptr->Untyped[0] = (TmpVal.Int16Val >> 8) & 255;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000530 } else if (BitWidth <= 32) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000531 TmpVal.Int32Val &= BitMask;
532 Ptr->Untyped[3] = TmpVal.Int32Val & 255;
533 Ptr->Untyped[2] = (TmpVal.Int32Val >> 8) & 255;
534 Ptr->Untyped[1] = (TmpVal.Int32Val >> 16) & 255;
535 Ptr->Untyped[0] = (TmpVal.Int32Val >> 24) & 255;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000536 } else if (BitWidth <= 64) {
Reid Spencerf89aec62007-01-18 01:24:02 +0000537 TmpVal.Int64Val &= BitMask;
538 Ptr->Untyped[7] = (unsigned char)(TmpVal.Int64Val );
539 Ptr->Untyped[6] = (unsigned char)(TmpVal.Int64Val >> 8);
540 Ptr->Untyped[5] = (unsigned char)(TmpVal.Int64Val >> 16);
541 Ptr->Untyped[4] = (unsigned char)(TmpVal.Int64Val >> 24);
542 Ptr->Untyped[3] = (unsigned char)(TmpVal.Int64Val >> 32);
543 Ptr->Untyped[2] = (unsigned char)(TmpVal.Int64Val >> 40);
544 Ptr->Untyped[1] = (unsigned char)(TmpVal.Int64Val >> 48);
545 Ptr->Untyped[0] = (unsigned char)(TmpVal.Int64Val >> 56);
Reid Spencer93a28752007-03-03 06:18:03 +0000546 } else {
547 uint64_t *Dest = (uint64_t*)Ptr;
548 const uint64_t *Src = Val.APIntVal->getRawData();
549 for (uint32_t i = 0; i < Val.APIntVal->getNumWords(); ++i)
550 Dest[i] = Src[i];
551 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000552 break;
553 }
Chris Lattner2be50792003-04-23 20:41:01 +0000554 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000555 case Type::FloatTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000556 Ptr->Untyped[3] = Val.Int32Val & 255;
557 Ptr->Untyped[2] = (Val.Int32Val >> 8) & 255;
558 Ptr->Untyped[1] = (Val.Int32Val >> 16) & 255;
559 Ptr->Untyped[0] = (Val.Int32Val >> 24) & 255;
560 break;
561 case Type::PointerTyID:
562 if (getTargetData()->getPointerSize() == 4)
563 goto Store4BytesBigEndian;
564 /* FALL THROUGH */
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000565 case Type::DoubleTyID:
Reid Spencere49661b2006-12-31 05:51:36 +0000566 Ptr->Untyped[7] = (unsigned char)(Val.Int64Val );
567 Ptr->Untyped[6] = (unsigned char)(Val.Int64Val >> 8);
568 Ptr->Untyped[5] = (unsigned char)(Val.Int64Val >> 16);
569 Ptr->Untyped[4] = (unsigned char)(Val.Int64Val >> 24);
570 Ptr->Untyped[3] = (unsigned char)(Val.Int64Val >> 32);
571 Ptr->Untyped[2] = (unsigned char)(Val.Int64Val >> 40);
572 Ptr->Untyped[1] = (unsigned char)(Val.Int64Val >> 48);
573 Ptr->Untyped[0] = (unsigned char)(Val.Int64Val >> 56);
Chris Lattner813c8152005-01-08 20:13:19 +0000574 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000575 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000576 cerr << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000577 }
578 }
579}
580
Misha Brukman4afac182003-10-10 17:45:12 +0000581/// FIXME: document
582///
Reid Spencerf0f09a92007-03-03 08:36:29 +0000583void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
584 GenericValue *Ptr,
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000585 const Type *Ty) {
Owen Andersona69571c2006-05-03 01:29:57 +0000586 if (getTargetData()->isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000587 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000588 case Type::IntegerTyID: {
589 unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
590 if (BitWidth <= 8)
591 Result.Int8Val = Ptr->Untyped[0];
592 else if (BitWidth <= 16) {
593 Result.Int16Val = (unsigned)Ptr->Untyped[0] |
594 ((unsigned)Ptr->Untyped[1] << 8);
595 } else if (BitWidth <= 32) {
596 Result.Int32Val = (unsigned)Ptr->Untyped[0] |
597 ((unsigned)Ptr->Untyped[1] << 8) |
598 ((unsigned)Ptr->Untyped[2] << 16) |
599 ((unsigned)Ptr->Untyped[3] << 24);
600 } else if (BitWidth <= 64) {
601 Result.Int64Val = (uint64_t)Ptr->Untyped[0] |
602 ((uint64_t)Ptr->Untyped[1] << 8) |
603 ((uint64_t)Ptr->Untyped[2] << 16) |
604 ((uint64_t)Ptr->Untyped[3] << 24) |
605 ((uint64_t)Ptr->Untyped[4] << 32) |
606 ((uint64_t)Ptr->Untyped[5] << 40) |
607 ((uint64_t)Ptr->Untyped[6] << 48) |
608 ((uint64_t)Ptr->Untyped[7] << 56);
609 } else
Reid Spencerf0f09a92007-03-03 08:36:29 +0000610 *(Result.APIntVal) = APInt(BitWidth, BitWidth/64, (uint64_t*)Ptr);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000611 break;
612 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000613 Load4BytesLittleEndian:
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000614 case Type::FloatTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000615 Result.Int32Val = (unsigned)Ptr->Untyped[0] |
616 ((unsigned)Ptr->Untyped[1] << 8) |
617 ((unsigned)Ptr->Untyped[2] << 16) |
618 ((unsigned)Ptr->Untyped[3] << 24);
619 break;
620 case Type::PointerTyID:
621 if (getTargetData()->getPointerSize() == 4)
622 goto Load4BytesLittleEndian;
623 /* FALL THROUGH */
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000624 case Type::DoubleTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000625 Result.Int64Val = (uint64_t)Ptr->Untyped[0] |
626 ((uint64_t)Ptr->Untyped[1] << 8) |
627 ((uint64_t)Ptr->Untyped[2] << 16) |
628 ((uint64_t)Ptr->Untyped[3] << 24) |
629 ((uint64_t)Ptr->Untyped[4] << 32) |
630 ((uint64_t)Ptr->Untyped[5] << 40) |
631 ((uint64_t)Ptr->Untyped[6] << 48) |
632 ((uint64_t)Ptr->Untyped[7] << 56);
633 break;
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000634 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000635 cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000636 abort();
637 }
638 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000639 switch (Ty->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000640 case Type::IntegerTyID: {
Reid Spencer93a28752007-03-03 06:18:03 +0000641 uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000642 if (BitWidth <= 8)
643 Result.Int8Val = Ptr->Untyped[0];
644 else if (BitWidth <= 16) {
645 Result.Int16Val = (unsigned)Ptr->Untyped[1] |
646 ((unsigned)Ptr->Untyped[0] << 8);
647 } else if (BitWidth <= 32) {
648 Result.Int32Val = (unsigned)Ptr->Untyped[3] |
649 ((unsigned)Ptr->Untyped[2] << 8) |
650 ((unsigned)Ptr->Untyped[1] << 16) |
651 ((unsigned)Ptr->Untyped[0] << 24);
652 } else if (BitWidth <= 64) {
653 Result.Int64Val = (uint64_t)Ptr->Untyped[7] |
654 ((uint64_t)Ptr->Untyped[6] << 8) |
655 ((uint64_t)Ptr->Untyped[5] << 16) |
656 ((uint64_t)Ptr->Untyped[4] << 24) |
657 ((uint64_t)Ptr->Untyped[3] << 32) |
658 ((uint64_t)Ptr->Untyped[2] << 40) |
659 ((uint64_t)Ptr->Untyped[1] << 48) |
660 ((uint64_t)Ptr->Untyped[0] << 56);
661 } else
Reid Spencerf0f09a92007-03-03 08:36:29 +0000662 *(Result.APIntVal) = APInt(BitWidth, BitWidth/64, (uint64_t*)Ptr);
Reid Spencera54b7cb2007-01-12 07:05:14 +0000663 break;
664 }
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000665 Load4BytesBigEndian:
666 case Type::FloatTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000667 Result.Int32Val = (unsigned)Ptr->Untyped[3] |
668 ((unsigned)Ptr->Untyped[2] << 8) |
669 ((unsigned)Ptr->Untyped[1] << 16) |
670 ((unsigned)Ptr->Untyped[0] << 24);
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000671 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000672 case Type::PointerTyID:
673 if (getTargetData()->getPointerSize() == 4)
674 goto Load4BytesBigEndian;
675 /* FALL THROUGH */
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000676 case Type::DoubleTyID:
Reid Spencera54b7cb2007-01-12 07:05:14 +0000677 Result.Int64Val = (uint64_t)Ptr->Untyped[7] |
678 ((uint64_t)Ptr->Untyped[6] << 8) |
679 ((uint64_t)Ptr->Untyped[5] << 16) |
680 ((uint64_t)Ptr->Untyped[4] << 24) |
681 ((uint64_t)Ptr->Untyped[3] << 32) |
682 ((uint64_t)Ptr->Untyped[2] << 40) |
683 ((uint64_t)Ptr->Untyped[1] << 48) |
684 ((uint64_t)Ptr->Untyped[0] << 56);
685 break;
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000686 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000687 cerr << "Cannot load value of type " << *Ty << "!\n";
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000688 abort();
689 }
690 }
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000691}
692
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000693// InitializeMemory - Recursive function to apply a Constant value into the
694// specified memory location...
695//
696void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000697 if (isa<UndefValue>(Init)) {
698 return;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000699 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000700 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000701 getTargetData()->getTypeSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +0000702 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
703 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
704 return;
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000705 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000706 GenericValue Val = getConstantValue(Init);
707 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
708 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000709 } else if (isa<ConstantAggregateZero>(Init)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000710 memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType()));
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000711 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000712 }
713
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000714 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000715 case Type::ArrayTyID: {
716 const ConstantArray *CPA = cast<ConstantArray>(Init);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000717 unsigned ElementSize =
Owen Andersona69571c2006-05-03 01:29:57 +0000718 getTargetData()->getTypeSize(CPA->getType()->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000719 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
720 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000721 return;
722 }
723
724 case Type::StructTyID: {
725 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
726 const StructLayout *SL =
Owen Andersona69571c2006-05-03 01:29:57 +0000727 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000728 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000729 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000730 return;
731 }
732
733 default:
Bill Wendlinge8156192006-12-07 01:30:32 +0000734 cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000735 assert(0 && "Unknown constant type to initialize memory with!");
736 }
737}
738
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000739/// EmitGlobals - Emit all of the global variables to memory, storing their
740/// addresses into GlobalAddress. This must make sure to copy the contents of
741/// their initializers into the memory.
742///
743void ExecutionEngine::emitGlobals() {
Owen Andersona69571c2006-05-03 01:29:57 +0000744 const TargetData *TD = getTargetData();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000745
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000746 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +0000747 // to hold them. If there is more than one module, do a prepass over globals
748 // to figure out how the different modules should link together.
749 //
750 std::map<std::pair<std::string, const Type*>,
751 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000752
Chris Lattnerfe854032006-08-16 01:24:12 +0000753 if (Modules.size() != 1) {
754 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
755 Module &M = *Modules[m]->getModule();
756 for (Module::const_global_iterator I = M.global_begin(),
757 E = M.global_end(); I != E; ++I) {
758 const GlobalValue *GV = I;
Reid Spencer5cbf9852007-01-30 20:08:39 +0000759 if (GV->hasInternalLinkage() || GV->isDeclaration() ||
Chris Lattnerfe854032006-08-16 01:24:12 +0000760 GV->hasAppendingLinkage() || !GV->hasName())
761 continue;// Ignore external globals and globals with internal linkage.
762
763 const GlobalValue *&GVEntry =
764 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
765
766 // If this is the first time we've seen this global, it is the canonical
767 // version.
768 if (!GVEntry) {
769 GVEntry = GV;
770 continue;
771 }
772
773 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000774 if (GVEntry->hasExternalLinkage() ||
775 GVEntry->hasDLLImportLinkage() ||
776 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000777 continue;
778
779 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
780 // symbol.
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000781 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +0000782 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000783 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000784 }
Chris Lattnerfe854032006-08-16 01:24:12 +0000785 }
786
787 std::vector<const GlobalValue*> NonCanonicalGlobals;
788 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
789 Module &M = *Modules[m]->getModule();
790 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
791 I != E; ++I) {
792 // In the multi-module case, see what this global maps to.
793 if (!LinkedGlobalsMap.empty()) {
794 if (const GlobalValue *GVEntry =
795 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
796 // If something else is the canonical global, ignore this one.
797 if (GVEntry != &*I) {
798 NonCanonicalGlobals.push_back(I);
799 continue;
800 }
801 }
802 }
803
Reid Spencer5cbf9852007-01-30 20:08:39 +0000804 if (!I->isDeclaration()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000805 // Get the type of the global.
806 const Type *Ty = I->getType()->getElementType();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000807
Chris Lattnerfe854032006-08-16 01:24:12 +0000808 // Allocate some memory for it!
809 unsigned Size = TD->getTypeSize(Ty);
810 addGlobalMapping(I, new char[Size]);
811 } else {
812 // External variable reference. Try to use the dynamic loader to
813 // get a pointer to it.
814 if (void *SymAddr =
815 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
816 addGlobalMapping(I, SymAddr);
817 else {
Bill Wendlinge8156192006-12-07 01:30:32 +0000818 cerr << "Could not resolve external global address: "
819 << I->getName() << "\n";
Chris Lattnerfe854032006-08-16 01:24:12 +0000820 abort();
821 }
822 }
823 }
824
825 // If there are multiple modules, map the non-canonical globals to their
826 // canonical location.
827 if (!NonCanonicalGlobals.empty()) {
828 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
829 const GlobalValue *GV = NonCanonicalGlobals[i];
830 const GlobalValue *CGV =
831 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
832 void *Ptr = getPointerToGlobalIfAvailable(CGV);
833 assert(Ptr && "Canonical global wasn't codegen'd!");
834 addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV));
835 }
836 }
837
Reid Spencera54b7cb2007-01-12 07:05:14 +0000838 // Now that all of the globals are set up in memory, loop through them all
839 // and initialize their contents.
Chris Lattnerfe854032006-08-16 01:24:12 +0000840 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
841 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000842 if (!I->isDeclaration()) {
Chris Lattnerfe854032006-08-16 01:24:12 +0000843 if (!LinkedGlobalsMap.empty()) {
844 if (const GlobalValue *GVEntry =
845 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
846 if (GVEntry != &*I) // Not the canonical variable.
847 continue;
848 }
849 EmitGlobalVariable(I);
850 }
851 }
852 }
Chris Lattner24b0a182003-12-20 02:45:37 +0000853}
854
855// EmitGlobalVariable - This method emits the specified global variable to the
856// address specified in GlobalAddresses, or allocates new memory if it's not
857// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000858void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000859 void *GA = getPointerToGlobalIfAvailable(GV);
Bill Wendling480f0932006-11-27 23:54:50 +0000860 DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n";
Chris Lattner23c47242004-02-08 19:33:23 +0000861
Chris Lattnerc07ed132003-12-20 03:36:47 +0000862 const Type *ElTy = GV->getType()->getElementType();
Owen Andersona69571c2006-05-03 01:29:57 +0000863 size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000864 if (GA == 0) {
865 // If it's not already specified, allocate memory for the global.
Chris Lattnera98c5452004-11-19 08:44:07 +0000866 GA = new char[GVSize];
Chris Lattner55d86482003-12-31 20:21:04 +0000867 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000868 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000869
Chris Lattner24b0a182003-12-20 02:45:37 +0000870 InitializeMemory(GV->getInitializer(), GA);
Chris Lattner813c8152005-01-08 20:13:19 +0000871 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +0000872 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000873}