blob: 208f8e2e834a2b22eb43e219feec089fca291850 [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 Lattnerbd199fb2002-12-24 00:01:05 +000020#include "llvm/Module.h"
Misha Brukman19684162003-10-16 21:18:05 +000021#include "llvm/ModuleProvider.h"
Chris Lattner8f7f71b2004-06-20 07:46:18 +000022#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukman19684162003-10-16 21:18:05 +000023#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"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/Support/DynamicLinker.h"
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000029using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000030
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000031namespace {
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
Chris Lattner24b0a182003-12-20 02:45:37 +000033 Statistic<> NumGlobals ("lli", "Number of global vars initialized");
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000034}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
Misha Brukman19684162003-10-16 21:18:05 +000036ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
37 CurMod(*P->getModule()), MP(P) {
38 assert(P && "ModuleProvider is null?");
39}
40
41ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
Misha Brukman05701572003-10-17 18:31:59 +000042 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000043}
44
Brian Gaeke8e539482003-09-04 22:57:27 +000045ExecutionEngine::~ExecutionEngine() {
Misha Brukman7b2b40f2003-10-14 21:36:31 +000046 delete MP;
Brian Gaeke8e539482003-09-04 22:57:27 +000047}
48
Chris Lattner55d86482003-12-31 20:21:04 +000049/// getGlobalValueAtAddress - Return the LLVM global value object that starts
50/// at the specified address.
51///
52const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
53 // If we haven't computed the reverse mapping yet, do so first.
54 if (GlobalAddressReverseMap.empty()) {
55 for (std::map<const GlobalValue*, void *>::iterator I =
56 GlobalAddressMap.begin(), E = GlobalAddressMap.end(); I != E; ++I)
57 GlobalAddressReverseMap.insert(std::make_pair(I->second, I->first));
58 }
59
60 std::map<void *, const GlobalValue*>::iterator I =
61 GlobalAddressReverseMap.find(Addr);
62 return I != GlobalAddressReverseMap.end() ? I->second : 0;
63}
Chris Lattner87f03102003-12-26 06:50:30 +000064
65// CreateArgv - Turn a vector of strings into a nice argv style array of
66// pointers to null terminated strings.
67//
68static void *CreateArgv(ExecutionEngine *EE,
69 const std::vector<std::string> &InputArgv) {
70 unsigned PtrSize = EE->getTargetData().getPointerSize();
71 char *Result = new char[(InputArgv.size()+1)*PtrSize];
72
73 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n");
74 const Type *SBytePtr = PointerType::get(Type::SByteTy);
75
76 for (unsigned i = 0; i != InputArgv.size(); ++i) {
77 unsigned Size = InputArgv[i].size()+1;
78 char *Dest = new char[Size];
79 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n");
80
81 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
82 Dest[Size-1] = 0;
83
84 // Endian safe: Result[i] = (PointerTy)Dest;
85 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
86 SBytePtr);
87 }
88
89 // Null terminate it
90 EE->StoreValueToMemory(PTOGV(0),
91 (GenericValue*)(Result+InputArgv.size()*PtrSize),
92 SBytePtr);
93 return Result;
94}
95
96/// runFunctionAsMain - This is a helper function which wraps runFunction to
97/// handle the common task of starting up main with the specified argc, argv,
98/// and envp parameters.
99int ExecutionEngine::runFunctionAsMain(Function *Fn,
100 const std::vector<std::string> &argv,
101 const char * const * envp) {
102 std::vector<GenericValue> GVArgs;
103 GenericValue GVArgc;
104 GVArgc.IntVal = argv.size();
Chris Lattnerf24d0992004-08-16 01:05:35 +0000105 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
106 if (NumArgs) {
107 GVArgs.push_back(GVArgc); // Arg #0 = argc.
108 if (NumArgs > 1) {
109 GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
110 assert(((char **)GVTOP(GVArgs[1]))[0] &&
111 "argv[0] was null after CreateArgv");
112 if (NumArgs > 2) {
113 std::vector<std::string> EnvVars;
114 for (unsigned i = 0; envp[i]; ++i)
115 EnvVars.push_back(envp[i]);
116 GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
117 }
118 }
119 }
Chris Lattner87f03102003-12-26 06:50:30 +0000120 return runFunction(Fn, GVArgs).IntVal;
121}
122
123
124
Misha Brukman19684162003-10-16 21:18:05 +0000125/// If possible, create a JIT, unless the caller specifically requests an
126/// Interpreter or there's an error. If even an Interpreter cannot be created,
127/// NULL is returned.
Misha Brukman4afac182003-10-10 17:45:12 +0000128///
Misha Brukman7b2b40f2003-10-14 21:36:31 +0000129ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
Chris Lattner73011782003-12-28 09:44:37 +0000130 bool ForceInterpreter,
131 IntrinsicLowering *IL) {
Brian Gaeke82d82772003-09-03 20:34:19 +0000132 ExecutionEngine *EE = 0;
133
Chris Lattner73011782003-12-28 09:44:37 +0000134 // Unless the interpreter was explicitly selected, try making a JIT.
Brian Gaeke20a277e2003-10-24 19:58:38 +0000135 if (!ForceInterpreter)
Chris Lattner73011782003-12-28 09:44:37 +0000136 EE = JIT::create(MP, IL);
Brian Gaeke82d82772003-09-03 20:34:19 +0000137
138 // If we can't make a JIT, make an interpreter instead.
Chris Lattner338733f2004-02-01 01:07:25 +0000139 if (EE == 0) {
140 try {
141 Module *M = MP->materializeModule();
142 try {
143 EE = Interpreter::create(M, IL);
144 } catch (...) {
145 std::cerr << "Error creating the interpreter!\n";
146 }
Reid Spencere2947532004-07-07 21:01:38 +0000147 } catch (std::string& errmsg) {
148 std::cerr << "Error reading the bytecode file: " << errmsg << "\n";
Chris Lattner338733f2004-02-01 01:07:25 +0000149 } catch (...) {
150 std::cerr << "Error reading the bytecode file!\n";
151 }
Misha Brukman19684162003-10-16 21:18:05 +0000152 }
Chris Lattner73011782003-12-28 09:44:37 +0000153
154 if (EE == 0) delete IL;
Brian Gaeke82d82772003-09-03 20:34:19 +0000155 return EE;
156}
157
Misha Brukman4afac182003-10-10 17:45:12 +0000158/// getPointerToGlobal - This returns the address of the specified global
159/// value. This may involve code generation if it's a function.
160///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000161void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000162 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000163 return getPointerToFunction(F);
164
Chris Lattner55d86482003-12-31 20:21:04 +0000165 assert(GlobalAddressMap[GV] && "Global hasn't had an address allocated yet?");
166 return GlobalAddressMap[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000167}
168
Misha Brukman4afac182003-10-10 17:45:12 +0000169/// FIXME: document
170///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000171GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
172 GenericValue Result;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000173
Chris Lattner9a231222003-05-14 17:51:49 +0000174 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000175 switch (CE->getOpcode()) {
176 case Instruction::GetElementPtr: {
Chris Lattner9a231222003-05-14 17:51:49 +0000177 Result = getConstantValue(CE->getOperand(0));
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000178 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
179 uint64_t Offset =
180 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
181
182 Result.LongVal += Offset;
183 return Result;
184 }
Chris Lattner9a231222003-05-14 17:51:49 +0000185 case Instruction::Cast: {
186 // We only need to handle a few cases here. Almost all casts will
187 // automatically fold, just the ones involving pointers won't.
188 //
189 Constant *Op = CE->getOperand(0);
Chris Lattner7d1bd332004-03-16 08:38:56 +0000190 GenericValue GV = getConstantValue(Op);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000191
Chris Lattner9a231222003-05-14 17:51:49 +0000192 // Handle cast of pointer to pointer...
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000193 if (Op->getType()->getTypeID() == C->getType()->getTypeID())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000194 return GV;
Chris Lattner9a231222003-05-14 17:51:49 +0000195
Chris Lattner74cf8192003-08-18 17:23:40 +0000196 // Handle a cast of pointer to any integral type...
Chris Lattnerc88a4ea2003-08-18 17:33:15 +0000197 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
Chris Lattner7d1bd332004-03-16 08:38:56 +0000198 return GV;
Chris Lattner74cf8192003-08-18 17:23:40 +0000199
Chris Lattner7d1bd332004-03-16 08:38:56 +0000200 // Handle cast of integer to a pointer...
201 if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral())
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000202 switch (Op->getType()->getTypeID()) {
Chris Lattner7d1bd332004-03-16 08:38:56 +0000203 case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal);
204 case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal);
205 case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal);
206 case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal);
207 case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal);
208 case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal);
209 case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal);
210 case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal);
211 case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal);
212 default: assert(0 && "Unknown integral type!");
213 }
Chris Lattner9a231222003-05-14 17:51:49 +0000214 break;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000215 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000216
Chris Lattner9a231222003-05-14 17:51:49 +0000217 case Instruction::Add:
Chris Lattner5f90cb82004-07-11 08:01:11 +0000218 switch (CE->getOperand(0)->getType()->getTypeID()) {
219 default: assert(0 && "Bad add type!"); abort();
220 case Type::LongTyID:
221 case Type::ULongTyID:
Chris Lattner6b2125c2003-05-14 17:53:49 +0000222 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
223 getConstantValue(CE->getOperand(1)).LongVal;
Chris Lattner9a231222003-05-14 17:51:49 +0000224 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000225 case Type::IntTyID:
226 case Type::UIntTyID:
227 Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal +
228 getConstantValue(CE->getOperand(1)).IntVal;
229 break;
230 case Type::ShortTyID:
231 case Type::UShortTyID:
232 Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal +
233 getConstantValue(CE->getOperand(1)).ShortVal;
234 break;
235 case Type::SByteTyID:
236 case Type::UByteTyID:
237 Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal +
238 getConstantValue(CE->getOperand(1)).SByteVal;
239 break;
240 case Type::FloatTyID:
241 Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal +
242 getConstantValue(CE->getOperand(1)).FloatVal;
243 break;
244 case Type::DoubleTyID:
245 Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal +
246 getConstantValue(CE->getOperand(1)).DoubleVal;
247 break;
248 }
Chris Lattner9a231222003-05-14 17:51:49 +0000249 return Result;
Chris Lattner9a231222003-05-14 17:51:49 +0000250 default:
251 break;
252 }
253 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
254 abort();
255 }
256
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000257 switch (C->getType()->getTypeID()) {
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000258#define GET_CONST_VAL(TY, CLASS) \
259 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000260 GET_CONST_VAL(Bool , ConstantBool);
261 GET_CONST_VAL(UByte , ConstantUInt);
262 GET_CONST_VAL(SByte , ConstantSInt);
263 GET_CONST_VAL(UShort , ConstantUInt);
264 GET_CONST_VAL(Short , ConstantSInt);
265 GET_CONST_VAL(UInt , ConstantUInt);
266 GET_CONST_VAL(Int , ConstantSInt);
267 GET_CONST_VAL(ULong , ConstantUInt);
268 GET_CONST_VAL(Long , ConstantSInt);
269 GET_CONST_VAL(Float , ConstantFP);
270 GET_CONST_VAL(Double , ConstantFP);
271#undef GET_CONST_VAL
272 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000273 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000274 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000275 else if (const Function *F = dyn_cast<Function>(C))
276 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
277 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
278 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
279 else
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000280 assert(0 && "Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000281 break;
282 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000283 std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n";
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000284 abort();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000285 }
286 return Result;
287}
288
Misha Brukman4afac182003-10-10 17:45:12 +0000289/// FIXME: document
290///
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000291void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
Misha Brukman4afac182003-10-10 17:45:12 +0000292 const Type *Ty) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000293 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000294 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000295 case Type::BoolTyID:
296 case Type::UByteTyID:
297 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
298 case Type::UShortTyID:
299 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
300 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
301 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000302 Store4BytesLittleEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000303 case Type::FloatTyID:
304 case Type::UIntTyID:
305 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
306 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
307 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
308 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
309 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000310 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000311 goto Store4BytesLittleEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000312 case Type::DoubleTyID:
313 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000314 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000315 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
316 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
317 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
318 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
319 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
320 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
321 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
322 break;
323 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000324 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000325 }
326 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000327 switch (Ty->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000328 case Type::BoolTyID:
329 case Type::UByteTyID:
330 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
331 case Type::UShortTyID:
332 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
333 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
334 break;
Chris Lattner2be50792003-04-23 20:41:01 +0000335 Store4BytesBigEndian:
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000336 case Type::FloatTyID:
337 case Type::UIntTyID:
338 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
339 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
340 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
341 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
342 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000343 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattner2be50792003-04-23 20:41:01 +0000344 goto Store4BytesBigEndian;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000345 case Type::DoubleTyID:
346 case Type::ULongTyID:
Chris Lattner2be50792003-04-23 20:41:01 +0000347 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000348 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
349 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
350 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
351 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
352 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
353 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
354 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
355 break;
356 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000357 std::cout << "Cannot store value of type " << *Ty << "!\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000358 }
359 }
360}
361
Misha Brukman4afac182003-10-10 17:45:12 +0000362/// FIXME: document
363///
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000364GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
365 const Type *Ty) {
366 GenericValue Result;
367 if (getTargetData().isLittleEndian()) {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000368 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000369 case Type::BoolTyID:
370 case Type::UByteTyID:
371 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
372 case Type::UShortTyID:
373 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
374 ((unsigned)Ptr->Untyped[1] << 8);
375 break;
376 Load4BytesLittleEndian:
377 case Type::FloatTyID:
378 case Type::UIntTyID:
379 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
380 ((unsigned)Ptr->Untyped[1] << 8) |
381 ((unsigned)Ptr->Untyped[2] << 16) |
382 ((unsigned)Ptr->Untyped[3] << 24);
383 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000384 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000385 goto Load4BytesLittleEndian;
386 case Type::DoubleTyID:
387 case Type::ULongTyID:
388 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
389 ((uint64_t)Ptr->Untyped[1] << 8) |
390 ((uint64_t)Ptr->Untyped[2] << 16) |
391 ((uint64_t)Ptr->Untyped[3] << 24) |
392 ((uint64_t)Ptr->Untyped[4] << 32) |
393 ((uint64_t)Ptr->Untyped[5] << 40) |
394 ((uint64_t)Ptr->Untyped[6] << 48) |
395 ((uint64_t)Ptr->Untyped[7] << 56);
396 break;
397 default:
398 std::cout << "Cannot load value of type " << *Ty << "!\n";
399 abort();
400 }
401 } else {
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000402 switch (Ty->getTypeID()) {
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000403 case Type::BoolTyID:
404 case Type::UByteTyID:
405 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
406 case Type::UShortTyID:
407 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
408 ((unsigned)Ptr->Untyped[0] << 8);
409 break;
410 Load4BytesBigEndian:
411 case Type::FloatTyID:
412 case Type::UIntTyID:
413 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
414 ((unsigned)Ptr->Untyped[2] << 8) |
415 ((unsigned)Ptr->Untyped[1] << 16) |
416 ((unsigned)Ptr->Untyped[0] << 24);
417 break;
Chris Lattnerc879e8f2003-08-24 19:55:26 +0000418 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
Chris Lattnerf88b9a62003-05-08 16:52:16 +0000419 goto Load4BytesBigEndian;
420 case Type::DoubleTyID:
421 case Type::ULongTyID:
422 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
423 ((uint64_t)Ptr->Untyped[6] << 8) |
424 ((uint64_t)Ptr->Untyped[5] << 16) |
425 ((uint64_t)Ptr->Untyped[4] << 24) |
426 ((uint64_t)Ptr->Untyped[3] << 32) |
427 ((uint64_t)Ptr->Untyped[2] << 40) |
428 ((uint64_t)Ptr->Untyped[1] << 48) |
429 ((uint64_t)Ptr->Untyped[0] << 56);
430 break;
431 default:
432 std::cout << "Cannot load value of type " << *Ty << "!\n";
433 abort();
434 }
435 }
436 return Result;
437}
438
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000439// InitializeMemory - Recursive function to apply a Constant value into the
440// specified memory location...
441//
442void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000443 if (isa<UndefValue>(Init)) {
444 return;
445 } else if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000446 GenericValue Val = getConstantValue(Init);
447 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
448 return;
Chris Lattnerdd2c82a2004-02-15 05:54:06 +0000449 } else if (isa<ConstantAggregateZero>(Init)) {
450 unsigned Size = getTargetData().getTypeSize(Init->getType());
451 memset(Addr, 0, Size);
452 return;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000453 }
454
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000455 switch (Init->getType()->getTypeID()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000456 case Type::ArrayTyID: {
457 const ConstantArray *CPA = cast<ConstantArray>(Init);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000458 unsigned ElementSize =
459 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000460 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
461 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000462 return;
463 }
464
465 case Type::StructTyID: {
466 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
467 const StructLayout *SL =
468 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000469 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
470 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000471 return;
472 }
473
474 default:
Chris Lattner0a8e8e12004-07-15 02:51:31 +0000475 std::cerr << "Bad Type: " << *Init->getType() << "\n";
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000476 assert(0 && "Unknown constant type to initialize memory with!");
477 }
478}
479
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000480/// EmitGlobals - Emit all of the global variables to memory, storing their
481/// addresses into GlobalAddress. This must make sure to copy the contents of
482/// their initializers into the memory.
483///
484void ExecutionEngine::emitGlobals() {
485 const TargetData &TD = getTargetData();
486
487 // Loop over all of the global variables in the program, allocating the memory
488 // to hold them.
489 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
490 I != E; ++I)
491 if (!I->isExternal()) {
492 // Get the type of the global...
493 const Type *Ty = I->getType()->getElementType();
494
495 // Allocate some memory for it!
496 unsigned Size = TD.getTypeSize(Ty);
Chris Lattner24b0a182003-12-20 02:45:37 +0000497 addGlobalMapping(I, new char[Size]);
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000498 } else {
Brian Gaeke322cdb22003-10-10 17:02:42 +0000499 // External variable reference. Try to use the dynamic loader to
500 // get a pointer to it.
501 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
Chris Lattner55d86482003-12-31 20:21:04 +0000502 addGlobalMapping(I, SymAddr);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000503 else {
504 std::cerr << "Could not resolve external global address: "
505 << I->getName() << "\n";
506 abort();
507 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000508 }
509
510 // Now that all of the globals are set up in memory, loop through them all and
511 // initialize their contents.
512 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
513 I != E; ++I)
514 if (!I->isExternal())
Chris Lattner24b0a182003-12-20 02:45:37 +0000515 EmitGlobalVariable(I);
516}
517
518// EmitGlobalVariable - This method emits the specified global variable to the
519// address specified in GlobalAddresses, or allocates new memory if it's not
520// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000521void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +0000522 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +0000523 DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n");
524
Chris Lattnerc07ed132003-12-20 03:36:47 +0000525 const Type *ElTy = GV->getType()->getElementType();
Chris Lattner24b0a182003-12-20 02:45:37 +0000526 if (GA == 0) {
527 // If it's not already specified, allocate memory for the global.
Chris Lattnerc07ed132003-12-20 03:36:47 +0000528 GA = new char[getTargetData().getTypeSize(ElTy)];
Chris Lattner55d86482003-12-31 20:21:04 +0000529 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +0000530 }
Chris Lattnerc07ed132003-12-20 03:36:47 +0000531
Chris Lattner24b0a182003-12-20 02:45:37 +0000532 InitializeMemory(GV->getInitializer(), GA);
Chris Lattnerc07ed132003-12-20 03:36:47 +0000533 NumInitBytes += getTargetData().getTypeSize(ElTy);
Chris Lattner24b0a182003-12-20 02:45:37 +0000534 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000535}