blob: 5892d169e78827701b5a14fc0e8417180283eb10 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Chris Lattnerfd131292003-09-05 20:08:15 +000016#include "Interpreter/Interpreter.h"
Chris Lattner61612df2003-12-20 01:45:17 +000017#include "JIT/JIT.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000018#include "llvm/Constants.h"
Misha Brukman19684162003-10-16 21:18:05 +000019#include "llvm/DerivedTypes.h"
Chris Lattner73011782003-12-28 09:44:37 +000020#include "llvm/IntrinsicLowering.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000022#include "llvm/ModuleProvider.h"
23#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000024#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000025#include "llvm/Target/TargetData.h"
Chris Lattnerfd131292003-09-05 20:08:15 +000026#include "Support/Debug.h"
27#include "Support/Statistic.h"
Brian Gaeke322cdb22003-10-10 17:02:42 +000028#include "Support/DynamicLinker.h"
John Criswell7a73b802003-06-30 21:59:07 +000029#include "Config/dlfcn.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000030using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000031
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000032namespace {
33 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000034 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000035}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000036
Misha Brukman19684162003-10-16 21:18:05 +000037ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
38 CurMod(*P->getModule()), MP(P) {
39 assert(P && "ModuleProvider is null?");
40}
41
42ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000043 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000044}
45
Brian Gaeke8e539482003-09-04 22:57:27 +000046ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000047 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000048}
49
Chris Lattner55d86482003-12-31 20:21:04 +000050/// getGlobalValueAtAddress - Return the LLVM global value object that starts
51/// at the specified address.
52///
53const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
54 // If we haven't computed the reverse mapping yet, do so first.
55 if (GlobalAddressReverseMap.empty()) {
56 for (std::map<const GlobalValue*, void *>::iterator I =
57 GlobalAddressMap.begin(), E = GlobalAddressMap.end(); I != E; ++I)
58 GlobalAddressReverseMap.insert(std::make_pair(I->second, I->first));
59 }
60
61 std::map<void *, const GlobalValue*>::iterator I =
62 GlobalAddressReverseMap.find(Addr);
63 return I != GlobalAddressReverseMap.end() ? I->second : 0;
64}
Chris Lattner87f03102003-12-26 06:50:30 +000065
66// CreateArgv - Turn a vector of strings into a nice argv style array of
67// pointers to null terminated strings.
68//
69static void *CreateArgv(ExecutionEngine *EE,
70 const std::vector<std::string> &InputArgv) {
71 unsigned PtrSize = EE->getTargetData().getPointerSize();
72 char *Result = new char[(InputArgv.size()+1)*PtrSize];
73
74 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
75 const Type *SBytePtr = PointerType::get(Type::SByteTy);
76
77 for (unsigned i = 0; i != InputArgv.size(); ++i) {
78 unsigned Size = InputArgv[i].size()+1;
79 char *Dest = new char[Size];
80 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
81
82 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
83 Dest[Size-1] = 0;
84
85 // Endian safe: Result[i] = (PointerTy)Dest;
86 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
87 SBytePtr);
88 }
89
90 // Null terminate it
91 EE->StoreValueToMemory(PTOGV(0),
92 (GenericValue*)(Result+InputArgv.size()*PtrSize),
93 SBytePtr);
94 return Result;
95}
96
97/// runFunctionAsMain - This is a helper function which wraps runFunction to
98/// handle the common task of starting up main with the specified argc, argv,
99/// and envp parameters.
100int ExecutionEngine::runFunctionAsMain(Function *Fn,
101 const std::vector<std::string> &argv,
102 const char * const * envp) {
103 std::vector<GenericValue> GVArgs;
104 GenericValue GVArgc;
105 GVArgc.IntVal = argv.size();
106 GVArgs.push_back(GVArgc); // Arg #0 = argc.
107 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
108 assert(((char **)GVTOP(GVArgs[1]))[0] && "argv[0] was null after CreateArgv");
109
110 std::vector<std::string> EnvVars;
111 for (unsigned i = 0; envp[i]; ++i)
112 EnvVars.push_back(envp[i]);
113 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
114 return runFunction(Fn, GVArgs).IntVal;
115}
116
117
118
Misha Brukman19684162003-10-16 21:18:05 +0000119/// If possible, create a JIT, unless the caller specifically requests an
120/// Interpreter or there's an error. If even an Interpreter cannot be created,
121/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000122///
Misha Brukman7b2b40f2003-10-14 21:36:31 +0000123ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000124 bool ForceInterpreter,
125 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000126 ExecutionEngine *EE = 0;
127
Chris Lattner73011782003-12-28 09:44:37 +0000128 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000129 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000130 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000131
132 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000133 if (EE == 0) {
134 try {
135 Module *M = MP->materializeModule();
136 try {
137 EE = Interpreter::create(M, IL);
138 } catch (...) {
139 std::cerr << "Error creating the interpreter!\n";
140 }
141 } catch (...) {
142 std::cerr << "Error reading the bytecode file!\n";
143 }
Misha Brukman19684162003-10-16 21:18:05 +0000144 }
Chris Lattner73011782003-12-28 09:44:37 +0000145
146 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000147 return EE;
148}
149
Misha Brukman4afac182003-10-10 17:45:12 +0000150/// getPointerToGlobal - This returns the address of the specified global
151/// value. This may involve code generation if it's a function.
152///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000153void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000154 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000155 return getPointerToFunction(F);
156
Chris Lattner55d86482003-12-31 20:21:04 +0000157 assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?");
158 return GlobalAddressMap[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000159}
160
Misha Brukman4afac182003-10-10 17:45:12 +0000161/// FIXME: document
162///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000163GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
164 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000165
Chris Lattner9a231222003-05-14 17:51:49 +0000166 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000167 switch (CE->getOpcode()) {
168 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000169 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000170 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
171 uint64_t Offset =
172 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
173
174 Result.LongVal += Offset;
175 return Result;
176 }
Chris Lattner9a231222003-05-14 17:51:49 +0000177 case Instruction::Cast: {
178 // We only need to handle a few cases here. Almost all casts will
179 // automatically fold, just the ones involving pointers won't.
180 //
181 Constant *Op = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000182
Chris Lattner9a231222003-05-14 17:51:49 +0000183 // Handle cast of pointer to pointer...
184 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
185 return getConstantValue(Op);
186
Chris Lattner74cf8192003-08-18 17:23:40 +0000187 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000188 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner9a231222003-05-14 17:51:49 +0000189 return getConstantValue(Op);
Chris Lattner74cf8192003-08-18 17:23:40 +0000190
191 // Handle cast of long to pointer...
192 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
193 Op->getType() == Type::ULongTy))
194 return getConstantValue(Op);
Chris Lattner9a231222003-05-14 17:51:49 +0000195 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000196 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000197
Chris Lattner9a231222003-05-14 17:51:49 +0000198 case Instruction::Add:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000199 if (CE->getOperand(0)->getType() == Type::LongTy ||
200 CE->getOperand(0)->getType() == Type::ULongTy)
201 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
202 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000203 else
204 break;
205 return Result;
206
207 default:
208 break;
209 }
210 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
211 abort();
212 }
213
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000214 switch (C->getType()->getPrimitiveID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000215#define GET_CONST_VAL(TY, CLASS) \
216 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000217 GET_CONST_VAL(Bool , ConstantBool);
218 GET_CONST_VAL(UByte , ConstantUInt);
219 GET_CONST_VAL(SByte , ConstantSInt);
220 GET_CONST_VAL(UShort , ConstantUInt);
221 GET_CONST_VAL(Short , ConstantSInt);
222 GET_CONST_VAL(UInt , ConstantUInt);
223 GET_CONST_VAL(Int , ConstantSInt);
224 GET_CONST_VAL(ULong , ConstantUInt);
225 GET_CONST_VAL(Long , ConstantSInt);
226 GET_CONST_VAL(Float , ConstantFP);
227 GET_CONST_VAL(Double , ConstantFP);
228#undef GET_CONST_VAL
229 case Type::PointerTyID:
230 if (isa<ConstantPointerNull>(C)) {
231 Result.PointerVal = 0;
232 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
Chris Lattner38f0eba2003-12-08 08:22:48 +0000233 if (Function *F =
234 const_cast<Function*>(dyn_cast<Function>(CPR->getValue())))
235 Result = PTOGV(getPointerToFunctionOrStub(F));
236 else
Chris Lattnerc07ed132003-12-20 03:36:47 +0000237 Result = PTOGV(getOrEmitGlobalVariable(
238 cast<GlobalVariable>(CPR->getValue())));
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000239
240 } else {
241 assert(0 && "Unknown constant pointer type!");
242 }
243 break;
244 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000245 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000246 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000247 }
248 return Result;
249}
250
Misha Brukman4afac182003-10-10 17:45:12 +0000251/// FIXME: document
252///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000253void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000254 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000255 if (getTargetData().isLittleEndian()) {
256 switch (Ty->getPrimitiveID()) {
257 case Type::BoolTyID:
258 case Type::UByteTyID:
259 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
260 case Type::UShortTyID:
261 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
262 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
263 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000264 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000265 case Type::FloatTyID:
266 case Type::UIntTyID:
267 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
268 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
269 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
270 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
271 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000272 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000273 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000274 case Type::DoubleTyID:
275 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000276 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000277 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
278 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
279 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
280 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
281 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
282 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
283 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
284 break;
285 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000286 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000287 }
288 } else {
289 switch (Ty->getPrimitiveID()) {
290 case Type::BoolTyID:
291 case Type::UByteTyID:
292 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
293 case Type::UShortTyID:
294 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
295 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
296 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000297 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000298 case Type::FloatTyID:
299 case Type::UIntTyID:
300 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
301 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
302 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
303 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
304 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000305 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000306 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000307 case Type::DoubleTyID:
308 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000309 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000310 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
311 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
312 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
313 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
314 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
315 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
316 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
317 break;
318 default:
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000319 std::cout << "Cannot store value of type " << Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000320 }
321 }
322}
323
Misha Brukman4afac182003-10-10 17:45:12 +0000324/// FIXME: document
325///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000326GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
327 const Type *Ty) {
328 GenericValue Result;
329 if (getTargetData().isLittleEndian()) {
330 switch (Ty->getPrimitiveID()) {
331 case Type::BoolTyID:
332 case Type::UByteTyID:
333 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
334 case Type::UShortTyID:
335 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
336 ((unsigned)Ptr->Untyped[1] << 8);
337 break;
338 Load4BytesLittleEndian:
339 case Type::FloatTyID:
340 case Type::UIntTyID:
341 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
342 ((unsigned)Ptr->Untyped[1] << 8) |
343 ((unsigned)Ptr->Untyped[2] << 16) |
344 ((unsigned)Ptr->Untyped[3] << 24);
345 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000346 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000347 goto Load4BytesLittleEndian;
348 case Type::DoubleTyID:
349 case Type::ULongTyID:
350 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
351 ((uint64_t)Ptr->Untyped[1] << 8) |
352 ((uint64_t)Ptr->Untyped[2] << 16) |
353 ((uint64_t)Ptr->Untyped[3] << 24) |
354 ((uint64_t)Ptr->Untyped[4] << 32) |
355 ((uint64_t)Ptr->Untyped[5] << 40) |
356 ((uint64_t)Ptr->Untyped[6] << 48) |
357 ((uint64_t)Ptr->Untyped[7] << 56);
358 break;
359 default:
360 std::cout << "Cannot load value of type " << *Ty << "!\n";
361 abort();
362 }
363 } else {
364 switch (Ty->getPrimitiveID()) {
365 case Type::BoolTyID:
366 case Type::UByteTyID:
367 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
368 case Type::UShortTyID:
369 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
370 ((unsigned)Ptr->Untyped[0] << 8);
371 break;
372 Load4BytesBigEndian:
373 case Type::FloatTyID:
374 case Type::UIntTyID:
375 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
376 ((unsigned)Ptr->Untyped[2] << 8) |
377 ((unsigned)Ptr->Untyped[1] << 16) |
378 ((unsigned)Ptr->Untyped[0] << 24);
379 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000380 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000381 goto Load4BytesBigEndian;
382 case Type::DoubleTyID:
383 case Type::ULongTyID:
384 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
385 ((uint64_t)Ptr->Untyped[6] << 8) |
386 ((uint64_t)Ptr->Untyped[5] << 16) |
387 ((uint64_t)Ptr->Untyped[4] << 24) |
388 ((uint64_t)Ptr->Untyped[3] << 32) |
389 ((uint64_t)Ptr->Untyped[2] << 40) |
390 ((uint64_t)Ptr->Untyped[1] << 48) |
391 ((uint64_t)Ptr->Untyped[0] << 56);
392 break;
393 default:
394 std::cout << "Cannot load value of type " << *Ty << "!\n";
395 abort();
396 }
397 }
398 return Result;
399}
400
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000401// InitializeMemory - Recursive function to apply a Constant value into the
402// specified memory location...
403//
404void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
405 if (Init->getType()->isFirstClassType()) {
406 GenericValue Val = getConstantValue(Init);
407 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
408 return;
409 }
410
411 switch (Init->getType()->getPrimitiveID()) {
412 case Type::ArrayTyID: {
413 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000414 const std::vector<Use> &Val = CPA->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000415 unsigned ElementSize =
416 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
417 for (unsigned i = 0; i < Val.size(); ++i)
418 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
419 return;
420 }
421
422 case Type::StructTyID: {
423 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
424 const StructLayout *SL =
425 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Chris Lattnerd6840ac2002-12-24 00:39:16 +0000426 const std::vector<Use> &Val = CPS->getValues();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000427 for (unsigned i = 0; i < Val.size(); ++i)
428 InitializeMemory(cast<Constant>(Val[i].get()),
429 (char*)Addr+SL->MemberOffsets[i]);
430 return;
431 }
432
433 default:
434 std::cerr << "Bad Type: " << Init->getType() << "\n";
435 assert(0 && "Unknown constant type to initialize memory with!");
436 }
437}
438
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000439/// EmitGlobals - Emit all of the global variables to memory, storing their
440/// addresses into GlobalAddress. This must make sure to copy the contents of
441/// their initializers into the memory.
442///
443void ExecutionEngine::emitGlobals() {
444 const TargetData &TD = getTargetData();
445
446 // Loop over all of the global variables in the program, allocating the memory
447 // to hold them.
448 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
449 I != E; ++I)
450 if (!I->isExternal()) {
451 // Get the type of the global...
452 const Type *Ty = I->getType()->getElementType();
453
454 // Allocate some memory for it!
455 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000456 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000457
458 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
Chris Lattner55d86482003-12-31 20:21:04 +0000459 << getPointerToGlobal(I) << "\n");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000460 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000461 // External variable reference. Try to use the dynamic loader to
462 // get a pointer to it.
463 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000464 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000465 else {
466 std::cerr << "Could not resolve external global address: "
467 << I->getName() << "\n";
468 abort();
469 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000470 }
471
472 // Now that all of the globals are set up in memory, loop through them all and
473 // initialize their contents.
474 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
475 I != E; ++I)
476 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000477 EmitGlobalVariable(I);
478}
479
480// EmitGlobalVariable - This method emits the specified global variable to the
481// address specified in GlobalAddresses, or allocates new memory if it's not
482// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000483void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000484 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000485 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000486 if (GA == 0) {
487 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000488 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner55d86482003-12-31 20:21:04 +0000489 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000490 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000491
Chris Lattner24b0a182003-12-20 02:45:37 +0000492 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000493 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000494 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000495}