Add EngineBuilder to ExecutionEngine in favor of the five optional argument EE::create().
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the common interface used by the various execution engine
11 // subclasses.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "jit"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Config/alloca.h"
22 #include "llvm/ExecutionEngine/ExecutionEngine.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MutexGuard.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/System/DynamicLibrary.h"
29 #include "llvm/System/Host.h"
30 #include "llvm/Target/TargetData.h"
31 #include <cmath>
32 #include <cstring>
33 using namespace llvm;
34
35 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
36 STATISTIC(NumGlobals  , "Number of global vars initialized");
37
38 ExecutionEngine *(*ExecutionEngine::JITCtor)(ModuleProvider *MP,
39                                              std::string *ErrorStr,
40                                              JITMemoryManager *JMM,
41                                              CodeGenOpt::Level OptLevel,
42                                              bool GVsWithCode) = 0;
43 ExecutionEngine *(*ExecutionEngine::InterpCtor)(ModuleProvider *MP,
44                                                 std::string *ErrorStr) = 0;
45 ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0;
46
47
48 ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
49   LazyCompilationDisabled = false;
50   GVCompilationDisabled   = false;
51   SymbolSearchingDisabled = false;
52   DlsymStubsEnabled       = false;
53   Modules.push_back(P);
54   assert(P && "ModuleProvider is null?");
55 }
56
57 ExecutionEngine::~ExecutionEngine() {
58   clearAllGlobalMappings();
59   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
60     delete Modules[i];
61 }
62
63 char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) {
64   const Type *ElTy = GV->getType()->getElementType();
65   size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
66   return new char[GVSize];
67 }
68
69 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
70 /// Relases the Module from the ModuleProvider, materializing it in the
71 /// process, and returns the materialized Module.
72 Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P, 
73                                               std::string *ErrInfo) {
74   for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 
75         E = Modules.end(); I != E; ++I) {
76     ModuleProvider *MP = *I;
77     if (MP == P) {
78       Modules.erase(I);
79       clearGlobalMappingsFromModule(MP->getModule());
80       return MP->releaseModule(ErrInfo);
81     }
82   }
83   return NULL;
84 }
85
86 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
87 /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
88 /// the underlying module.
89 void ExecutionEngine::deleteModuleProvider(ModuleProvider *P, 
90                                            std::string *ErrInfo) {
91   for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(), 
92       E = Modules.end(); I != E; ++I) {
93     ModuleProvider *MP = *I;
94     if (MP == P) {
95       Modules.erase(I);
96       clearGlobalMappingsFromModule(MP->getModule());
97       delete MP;
98       return;
99     }
100   }
101 }
102
103 /// FindFunctionNamed - Search all of the active modules to find the one that
104 /// defines FnName.  This is very slow operation and shouldn't be used for
105 /// general code.
106 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
107   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
108     if (Function *F = Modules[i]->getModule()->getFunction(FnName))
109       return F;
110   }
111   return 0;
112 }
113
114
115 /// addGlobalMapping - Tell the execution engine that the specified global is
116 /// at the specified location.  This is used internally as functions are JIT'd
117 /// and as global variables are laid out in memory.  It can and should also be
118 /// used by clients of the EE that want to have an LLVM global overlay
119 /// existing data in memory.
120 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
121   MutexGuard locked(lock);
122
123   DOUT << "JIT: Map \'" << GV->getNameStart() << "\' to [" << Addr << "]\n";  
124   void *&CurVal = state.getGlobalAddressMap(locked)[GV];
125   assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
126   CurVal = Addr;
127   
128   // If we are using the reverse mapping, add it too
129   if (!state.getGlobalAddressReverseMap(locked).empty()) {
130     const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
131     assert((V == 0 || GV == 0) && "GlobalMapping already established!");
132     V = GV;
133   }
134 }
135
136 /// clearAllGlobalMappings - Clear all global mappings and start over again
137 /// use in dynamic compilation scenarios when you want to move globals
138 void ExecutionEngine::clearAllGlobalMappings() {
139   MutexGuard locked(lock);
140   
141   state.getGlobalAddressMap(locked).clear();
142   state.getGlobalAddressReverseMap(locked).clear();
143 }
144
145 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
146 /// particular module, because it has been removed from the JIT.
147 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
148   MutexGuard locked(lock);
149   
150   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
151     state.getGlobalAddressMap(locked).erase(FI);
152     state.getGlobalAddressReverseMap(locked).erase(FI);
153   }
154   for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); 
155        GI != GE; ++GI) {
156     state.getGlobalAddressMap(locked).erase(GI);
157     state.getGlobalAddressReverseMap(locked).erase(GI);
158   }
159 }
160
161 /// updateGlobalMapping - Replace an existing mapping for GV with a new
162 /// address.  This updates both maps as required.  If "Addr" is null, the
163 /// entry for the global is removed from the mappings.
164 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
165   MutexGuard locked(lock);
166
167   std::map<const GlobalValue*, void *> &Map = state.getGlobalAddressMap(locked);
168
169   // Deleting from the mapping?
170   if (Addr == 0) {
171     std::map<const GlobalValue*, void *>::iterator I = Map.find(GV);
172     void *OldVal;
173     if (I == Map.end())
174       OldVal = 0;
175     else {
176       OldVal = I->second;
177       Map.erase(I); 
178     }
179     
180     if (!state.getGlobalAddressReverseMap(locked).empty())
181       state.getGlobalAddressReverseMap(locked).erase(Addr);
182     return OldVal;
183   }
184   
185   void *&CurVal = Map[GV];
186   void *OldVal = CurVal;
187
188   if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
189     state.getGlobalAddressReverseMap(locked).erase(CurVal);
190   CurVal = Addr;
191   
192   // If we are using the reverse mapping, add it too
193   if (!state.getGlobalAddressReverseMap(locked).empty()) {
194     const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr];
195     assert((V == 0 || GV == 0) && "GlobalMapping already established!");
196     V = GV;
197   }
198   return OldVal;
199 }
200
201 /// getPointerToGlobalIfAvailable - This returns the address of the specified
202 /// global value if it is has already been codegen'd, otherwise it returns null.
203 ///
204 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
205   MutexGuard locked(lock);
206   
207   std::map<const GlobalValue*, void*>::iterator I =
208   state.getGlobalAddressMap(locked).find(GV);
209   return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
210 }
211
212 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
213 /// at the specified address.
214 ///
215 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
216   MutexGuard locked(lock);
217
218   // If we haven't computed the reverse mapping yet, do so first.
219   if (state.getGlobalAddressReverseMap(locked).empty()) {
220     for (std::map<const GlobalValue*, void *>::iterator
221          I = state.getGlobalAddressMap(locked).begin(),
222          E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
223       state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
224                                                                      I->first));
225   }
226
227   std::map<void *, const GlobalValue*>::iterator I =
228     state.getGlobalAddressReverseMap(locked).find(Addr);
229   return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
230 }
231
232 // CreateArgv - Turn a vector of strings into a nice argv style array of
233 // pointers to null terminated strings.
234 //
235 static void *CreateArgv(ExecutionEngine *EE,
236                         const std::vector<std::string> &InputArgv) {
237   unsigned PtrSize = EE->getTargetData()->getPointerSize();
238   char *Result = new char[(InputArgv.size()+1)*PtrSize];
239
240   DOUT << "JIT: ARGV = " << (void*)Result << "\n";
241   const Type *SBytePtr = PointerType::getUnqual(Type::Int8Ty);
242
243   for (unsigned i = 0; i != InputArgv.size(); ++i) {
244     unsigned Size = InputArgv[i].size()+1;
245     char *Dest = new char[Size];
246     DOUT << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n";
247
248     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
249     Dest[Size-1] = 0;
250
251     // Endian safe: Result[i] = (PointerTy)Dest;
252     EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
253                            SBytePtr);
254   }
255
256   // Null terminate it
257   EE->StoreValueToMemory(PTOGV(0),
258                          (GenericValue*)(Result+InputArgv.size()*PtrSize),
259                          SBytePtr);
260   return Result;
261 }
262
263
264 /// runStaticConstructorsDestructors - This method is used to execute all of
265 /// the static constructors or destructors for a module, depending on the
266 /// value of isDtors.
267 void ExecutionEngine::runStaticConstructorsDestructors(Module *module, bool isDtors) {
268   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
269   
270   // Execute global ctors/dtors for each module in the program.
271   
272  GlobalVariable *GV = module->getNamedGlobal(Name);
273
274  // If this global has internal linkage, or if it has a use, then it must be
275  // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
276  // this is the case, don't execute any of the global ctors, __main will do
277  // it.
278  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
279  
280  // Should be an array of '{ int, void ()* }' structs.  The first value is
281  // the init priority, which we ignore.
282  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
283  if (!InitList) return;
284  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
285    if (ConstantStruct *CS = 
286        dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
287      if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
288    
289      Constant *FP = CS->getOperand(1);
290      if (FP->isNullValue())
291        break;  // Found a null terminator, exit.
292    
293      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
294        if (CE->isCast())
295          FP = CE->getOperand(0);
296      if (Function *F = dyn_cast<Function>(FP)) {
297        // Execute the ctor/dtor function!
298        runFunction(F, std::vector<GenericValue>());
299      }
300    }
301 }
302
303 /// runStaticConstructorsDestructors - This method is used to execute all of
304 /// the static constructors or destructors for a program, depending on the
305 /// value of isDtors.
306 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
307   // Execute global ctors/dtors for each module in the program.
308   for (unsigned m = 0, e = Modules.size(); m != e; ++m)
309     runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors);
310 }
311
312 #ifndef NDEBUG
313 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
314 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
315   unsigned PtrSize = EE->getTargetData()->getPointerSize();
316   for (unsigned i = 0; i < PtrSize; ++i)
317     if (*(i + (uint8_t*)Loc))
318       return false;
319   return true;
320 }
321 #endif
322
323 /// runFunctionAsMain - This is a helper function which wraps runFunction to
324 /// handle the common task of starting up main with the specified argc, argv,
325 /// and envp parameters.
326 int ExecutionEngine::runFunctionAsMain(Function *Fn,
327                                        const std::vector<std::string> &argv,
328                                        const char * const * envp) {
329   std::vector<GenericValue> GVArgs;
330   GenericValue GVArgc;
331   GVArgc.IntVal = APInt(32, argv.size());
332
333   // Check main() type
334   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
335   const FunctionType *FTy = Fn->getFunctionType();
336   const Type* PPInt8Ty = 
337     PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
338   switch (NumArgs) {
339   case 3:
340    if (FTy->getParamType(2) != PPInt8Ty) {
341      llvm_report_error("Invalid type for third argument of main() supplied");
342    }
343    // FALLS THROUGH
344   case 2:
345    if (FTy->getParamType(1) != PPInt8Ty) {
346      llvm_report_error("Invalid type for second argument of main() supplied");
347    }
348    // FALLS THROUGH
349   case 1:
350    if (FTy->getParamType(0) != Type::Int32Ty) {
351      llvm_report_error("Invalid type for first argument of main() supplied");
352    }
353    // FALLS THROUGH
354   case 0:
355    if (!isa<IntegerType>(FTy->getReturnType()) &&
356        FTy->getReturnType() != Type::VoidTy) {
357      llvm_report_error("Invalid return type of main() supplied");
358    }
359    break;
360   default:
361    llvm_report_error("Invalid number of arguments of main() supplied");
362   }
363   
364   if (NumArgs) {
365     GVArgs.push_back(GVArgc); // Arg #0 = argc.
366     if (NumArgs > 1) {
367       GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv.
368       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
369              "argv[0] was null after CreateArgv");
370       if (NumArgs > 2) {
371         std::vector<std::string> EnvVars;
372         for (unsigned i = 0; envp[i]; ++i)
373           EnvVars.push_back(envp[i]);
374         GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp.
375       }
376     }
377   }
378   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
379 }
380
381 /// If possible, create a JIT, unless the caller specifically requests an
382 /// Interpreter or there's an error. If even an Interpreter cannot be created,
383 /// NULL is returned.
384 ///
385 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
386                                          bool ForceInterpreter,
387                                          std::string *ErrorStr,
388                                          CodeGenOpt::Level OptLevel,
389                                          bool GVsWithCode) {
390   return EngineBuilder(MP)
391       .setEngineKind(ForceInterpreter
392                      ? EngineKind::Interpreter
393                      : EngineKind::JIT)
394       .setErrorStr(ErrorStr)
395       .setOptLevel(OptLevel)
396       .setAllocateGVsWithCode(GVsWithCode)
397       .create();
398 }
399
400 ExecutionEngine *ExecutionEngine::create(Module *M) {
401   return EngineBuilder(M).create();
402 }
403
404 /// EngineBuilder - Overloaded constructor that automatically creates an
405 /// ExistingModuleProvider for an existing module.
406 EngineBuilder::EngineBuilder(Module *m) : MP(new ExistingModuleProvider(m)) {
407   InitEngine();
408 }
409
410 ExecutionEngine *EngineBuilder::create() {
411   // Make sure we can resolve symbols in the program as well. The zero arg
412   // to the function tells DynamicLibrary to load the program, not a library.
413   if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
414     return 0;
415
416   // If the user specified a memory manager but didn't specify which engine to
417   // create, we assume they only want the JIT, and we fail if they only want
418   // the interpreter.
419   if (JMM) {
420     if (WhichEngine & EngineKind::JIT) {
421       WhichEngine = EngineKind::JIT;
422     } else {
423       *ErrorStr = "Cannot create an interpreter with a memory manager.";
424     }
425   }
426
427   ExecutionEngine *EE = 0;
428
429   // Unless the interpreter was explicitly selected or the JIT is not linked,
430   // try making a JIT.
431   if (WhichEngine & EngineKind::JIT && ExecutionEngine::JITCtor) {
432     EE = ExecutionEngine::JITCtor(MP, ErrorStr, JMM, OptLevel,
433                                   AllocateGVsWithCode);
434   }
435
436   // If we can't make a JIT and we didn't request one specifically, try making
437   // an interpreter instead.
438   if (WhichEngine & EngineKind::Interpreter && EE == 0 &&
439       ExecutionEngine::InterpCtor) {
440     EE = ExecutionEngine::InterpCtor(MP, ErrorStr);
441   }
442
443   return EE;
444 }
445
446 /// getPointerToGlobal - This returns the address of the specified global
447 /// value.  This may involve code generation if it's a function.
448 ///
449 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
450   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
451     return getPointerToFunction(F);
452
453   MutexGuard locked(lock);
454   void *p = state.getGlobalAddressMap(locked)[GV];
455   if (p)
456     return p;
457
458   // Global variable might have been added since interpreter started.
459   if (GlobalVariable *GVar =
460           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
461     EmitGlobalVariable(GVar);
462   else
463     llvm_unreachable("Global hasn't had an address allocated yet!");
464   return state.getGlobalAddressMap(locked)[GV];
465 }
466
467 /// This function converts a Constant* into a GenericValue. The interesting 
468 /// part is if C is a ConstantExpr.
469 /// @brief Get a GenericValue for a Constant*
470 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
471   // If its undefined, return the garbage.
472   if (isa<UndefValue>(C)) 
473     return GenericValue();
474
475   // If the value is a ConstantExpr
476   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
477     Constant *Op0 = CE->getOperand(0);
478     switch (CE->getOpcode()) {
479     case Instruction::GetElementPtr: {
480       // Compute the index 
481       GenericValue Result = getConstantValue(Op0);
482       SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
483       uint64_t Offset =
484         TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size());
485
486       char* tmp = (char*) Result.PointerVal;
487       Result = PTOGV(tmp + Offset);
488       return Result;
489     }
490     case Instruction::Trunc: {
491       GenericValue GV = getConstantValue(Op0);
492       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
493       GV.IntVal = GV.IntVal.trunc(BitWidth);
494       return GV;
495     }
496     case Instruction::ZExt: {
497       GenericValue GV = getConstantValue(Op0);
498       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
499       GV.IntVal = GV.IntVal.zext(BitWidth);
500       return GV;
501     }
502     case Instruction::SExt: {
503       GenericValue GV = getConstantValue(Op0);
504       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
505       GV.IntVal = GV.IntVal.sext(BitWidth);
506       return GV;
507     }
508     case Instruction::FPTrunc: {
509       // FIXME long double
510       GenericValue GV = getConstantValue(Op0);
511       GV.FloatVal = float(GV.DoubleVal);
512       return GV;
513     }
514     case Instruction::FPExt:{
515       // FIXME long double
516       GenericValue GV = getConstantValue(Op0);
517       GV.DoubleVal = double(GV.FloatVal);
518       return GV;
519     }
520     case Instruction::UIToFP: {
521       GenericValue GV = getConstantValue(Op0);
522       if (CE->getType() == Type::FloatTy)
523         GV.FloatVal = float(GV.IntVal.roundToDouble());
524       else if (CE->getType() == Type::DoubleTy)
525         GV.DoubleVal = GV.IntVal.roundToDouble();
526       else if (CE->getType() == Type::X86_FP80Ty) {
527         const uint64_t zero[] = {0, 0};
528         APFloat apf = APFloat(APInt(80, 2, zero));
529         (void)apf.convertFromAPInt(GV.IntVal, 
530                                    false,
531                                    APFloat::rmNearestTiesToEven);
532         GV.IntVal = apf.bitcastToAPInt();
533       }
534       return GV;
535     }
536     case Instruction::SIToFP: {
537       GenericValue GV = getConstantValue(Op0);
538       if (CE->getType() == Type::FloatTy)
539         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
540       else if (CE->getType() == Type::DoubleTy)
541         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
542       else if (CE->getType() == Type::X86_FP80Ty) {
543         const uint64_t zero[] = { 0, 0};
544         APFloat apf = APFloat(APInt(80, 2, zero));
545         (void)apf.convertFromAPInt(GV.IntVal, 
546                                    true,
547                                    APFloat::rmNearestTiesToEven);
548         GV.IntVal = apf.bitcastToAPInt();
549       }
550       return GV;
551     }
552     case Instruction::FPToUI: // double->APInt conversion handles sign
553     case Instruction::FPToSI: {
554       GenericValue GV = getConstantValue(Op0);
555       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
556       if (Op0->getType() == Type::FloatTy)
557         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
558       else if (Op0->getType() == Type::DoubleTy)
559         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
560       else if (Op0->getType() == Type::X86_FP80Ty) {
561         APFloat apf = APFloat(GV.IntVal);
562         uint64_t v;
563         bool ignored;
564         (void)apf.convertToInteger(&v, BitWidth,
565                                    CE->getOpcode()==Instruction::FPToSI, 
566                                    APFloat::rmTowardZero, &ignored);
567         GV.IntVal = v; // endian?
568       }
569       return GV;
570     }
571     case Instruction::PtrToInt: {
572       GenericValue GV = getConstantValue(Op0);
573       uint32_t PtrWidth = TD->getPointerSizeInBits();
574       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
575       return GV;
576     }
577     case Instruction::IntToPtr: {
578       GenericValue GV = getConstantValue(Op0);
579       uint32_t PtrWidth = TD->getPointerSizeInBits();
580       if (PtrWidth != GV.IntVal.getBitWidth())
581         GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
582       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
583       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
584       return GV;
585     }
586     case Instruction::BitCast: {
587       GenericValue GV = getConstantValue(Op0);
588       const Type* DestTy = CE->getType();
589       switch (Op0->getType()->getTypeID()) {
590         default: llvm_unreachable("Invalid bitcast operand");
591         case Type::IntegerTyID:
592           assert(DestTy->isFloatingPoint() && "invalid bitcast");
593           if (DestTy == Type::FloatTy)
594             GV.FloatVal = GV.IntVal.bitsToFloat();
595           else if (DestTy == Type::DoubleTy)
596             GV.DoubleVal = GV.IntVal.bitsToDouble();
597           break;
598         case Type::FloatTyID: 
599           assert(DestTy == Type::Int32Ty && "Invalid bitcast");
600           GV.IntVal.floatToBits(GV.FloatVal);
601           break;
602         case Type::DoubleTyID:
603           assert(DestTy == Type::Int64Ty && "Invalid bitcast");
604           GV.IntVal.doubleToBits(GV.DoubleVal);
605           break;
606         case Type::PointerTyID:
607           assert(isa<PointerType>(DestTy) && "Invalid bitcast");
608           break; // getConstantValue(Op0)  above already converted it
609       }
610       return GV;
611     }
612     case Instruction::Add:
613     case Instruction::FAdd:
614     case Instruction::Sub:
615     case Instruction::FSub:
616     case Instruction::Mul:
617     case Instruction::FMul:
618     case Instruction::UDiv:
619     case Instruction::SDiv:
620     case Instruction::URem:
621     case Instruction::SRem:
622     case Instruction::And:
623     case Instruction::Or:
624     case Instruction::Xor: {
625       GenericValue LHS = getConstantValue(Op0);
626       GenericValue RHS = getConstantValue(CE->getOperand(1));
627       GenericValue GV;
628       switch (CE->getOperand(0)->getType()->getTypeID()) {
629       default: llvm_unreachable("Bad add type!");
630       case Type::IntegerTyID:
631         switch (CE->getOpcode()) {
632           default: llvm_unreachable("Invalid integer opcode");
633           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
634           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
635           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
636           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
637           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
638           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
639           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
640           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
641           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
642           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
643         }
644         break;
645       case Type::FloatTyID:
646         switch (CE->getOpcode()) {
647           default: llvm_unreachable("Invalid float opcode");
648           case Instruction::FAdd:
649             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
650           case Instruction::FSub:
651             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
652           case Instruction::FMul:
653             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
654           case Instruction::FDiv: 
655             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
656           case Instruction::FRem: 
657             GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break;
658         }
659         break;
660       case Type::DoubleTyID:
661         switch (CE->getOpcode()) {
662           default: llvm_unreachable("Invalid double opcode");
663           case Instruction::FAdd:
664             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
665           case Instruction::FSub:
666             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
667           case Instruction::FMul:
668             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
669           case Instruction::FDiv: 
670             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
671           case Instruction::FRem: 
672             GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
673         }
674         break;
675       case Type::X86_FP80TyID:
676       case Type::PPC_FP128TyID:
677       case Type::FP128TyID: {
678         APFloat apfLHS = APFloat(LHS.IntVal);
679         switch (CE->getOpcode()) {
680           default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0);
681           case Instruction::FAdd:
682             apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
683             GV.IntVal = apfLHS.bitcastToAPInt();
684             break;
685           case Instruction::FSub:
686             apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
687             GV.IntVal = apfLHS.bitcastToAPInt();
688             break;
689           case Instruction::FMul:
690             apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
691             GV.IntVal = apfLHS.bitcastToAPInt();
692             break;
693           case Instruction::FDiv: 
694             apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
695             GV.IntVal = apfLHS.bitcastToAPInt();
696             break;
697           case Instruction::FRem: 
698             apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
699             GV.IntVal = apfLHS.bitcastToAPInt();
700             break;
701           }
702         }
703         break;
704       }
705       return GV;
706     }
707     default:
708       break;
709     }
710     std::string msg;
711     raw_string_ostream Msg(msg);
712     Msg << "ConstantExpr not handled: " << *CE;
713     llvm_report_error(Msg.str());
714   }
715
716   GenericValue Result;
717   switch (C->getType()->getTypeID()) {
718   case Type::FloatTyID: 
719     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 
720     break;
721   case Type::DoubleTyID:
722     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
723     break;
724   case Type::X86_FP80TyID:
725   case Type::FP128TyID:
726   case Type::PPC_FP128TyID:
727     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
728     break;
729   case Type::IntegerTyID:
730     Result.IntVal = cast<ConstantInt>(C)->getValue();
731     break;
732   case Type::PointerTyID:
733     if (isa<ConstantPointerNull>(C))
734       Result.PointerVal = 0;
735     else if (const Function *F = dyn_cast<Function>(C))
736       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
737     else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
738       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
739     else
740       llvm_unreachable("Unknown constant pointer type!");
741     break;
742   default:
743     std::string msg;
744     raw_string_ostream Msg(msg);
745     Msg << "ERROR: Constant unimplemented for type: " << *C->getType();
746     llvm_report_error(Msg.str());
747   }
748   return Result;
749 }
750
751 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
752 /// with the integer held in IntVal.
753 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
754                              unsigned StoreBytes) {
755   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
756   uint8_t *Src = (uint8_t *)IntVal.getRawData();
757
758   if (sys::isLittleEndianHost())
759     // Little-endian host - the source is ordered from LSB to MSB.  Order the
760     // destination from LSB to MSB: Do a straight copy.
761     memcpy(Dst, Src, StoreBytes);
762   else {
763     // Big-endian host - the source is an array of 64 bit words ordered from
764     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
765     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
766     while (StoreBytes > sizeof(uint64_t)) {
767       StoreBytes -= sizeof(uint64_t);
768       // May not be aligned so use memcpy.
769       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
770       Src += sizeof(uint64_t);
771     }
772
773     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
774   }
775 }
776
777 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr.  Ptr
778 /// is the address of the memory at which to store Val, cast to GenericValue *.
779 /// It is not a pointer to a GenericValue containing the address at which to
780 /// store Val.
781 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
782                                          GenericValue *Ptr, const Type *Ty) {
783   const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
784
785   switch (Ty->getTypeID()) {
786   case Type::IntegerTyID:
787     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
788     break;
789   case Type::FloatTyID:
790     *((float*)Ptr) = Val.FloatVal;
791     break;
792   case Type::DoubleTyID:
793     *((double*)Ptr) = Val.DoubleVal;
794     break;
795   case Type::X86_FP80TyID:
796     memcpy(Ptr, Val.IntVal.getRawData(), 10);
797     break;
798   case Type::PointerTyID:
799     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
800     if (StoreBytes != sizeof(PointerTy))
801       memset(Ptr, 0, StoreBytes);
802
803     *((PointerTy*)Ptr) = Val.PointerVal;
804     break;
805   default:
806     cerr << "Cannot store value of type " << *Ty << "!\n";
807   }
808
809   if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
810     // Host and target are different endian - reverse the stored bytes.
811     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
812 }
813
814 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
815 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
816 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
817   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
818   uint8_t *Dst = (uint8_t *)IntVal.getRawData();
819
820   if (sys::isLittleEndianHost())
821     // Little-endian host - the destination must be ordered from LSB to MSB.
822     // The source is ordered from LSB to MSB: Do a straight copy.
823     memcpy(Dst, Src, LoadBytes);
824   else {
825     // Big-endian - the destination is an array of 64 bit words ordered from
826     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
827     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
828     // a word.
829     while (LoadBytes > sizeof(uint64_t)) {
830       LoadBytes -= sizeof(uint64_t);
831       // May not be aligned so use memcpy.
832       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
833       Dst += sizeof(uint64_t);
834     }
835
836     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
837   }
838 }
839
840 /// FIXME: document
841 ///
842 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
843                                           GenericValue *Ptr,
844                                           const Type *Ty) {
845   const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
846
847   if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) {
848     // Host and target are different endian - reverse copy the stored
849     // bytes into a buffer, and load from that.
850     uint8_t *Src = (uint8_t*)Ptr;
851     uint8_t *Buf = (uint8_t*)alloca(LoadBytes);
852     std::reverse_copy(Src, Src + LoadBytes, Buf);
853     Ptr = (GenericValue*)Buf;
854   }
855
856   switch (Ty->getTypeID()) {
857   case Type::IntegerTyID:
858     // An APInt with all words initially zero.
859     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
860     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
861     break;
862   case Type::FloatTyID:
863     Result.FloatVal = *((float*)Ptr);
864     break;
865   case Type::DoubleTyID:
866     Result.DoubleVal = *((double*)Ptr);
867     break;
868   case Type::PointerTyID:
869     Result.PointerVal = *((PointerTy*)Ptr);
870     break;
871   case Type::X86_FP80TyID: {
872     // This is endian dependent, but it will only work on x86 anyway.
873     // FIXME: Will not trap if loading a signaling NaN.
874     uint64_t y[2];
875     memcpy(y, Ptr, 10);
876     Result.IntVal = APInt(80, 2, y);
877     break;
878   }
879   default:
880     std::string msg;
881     raw_string_ostream Msg(msg);
882     Msg << "Cannot load value of type " << *Ty << "!";
883     llvm_report_error(Msg.str());
884   }
885 }
886
887 // InitializeMemory - Recursive function to apply a Constant value into the
888 // specified memory location...
889 //
890 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
891   DOUT << "JIT: Initializing " << Addr << " ";
892   DEBUG(Init->dump());
893   if (isa<UndefValue>(Init)) {
894     return;
895   } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
896     unsigned ElementSize =
897       getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
898     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
899       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
900     return;
901   } else if (isa<ConstantAggregateZero>(Init)) {
902     memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
903     return;
904   } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
905     unsigned ElementSize =
906       getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
907     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
908       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
909     return;
910   } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
911     const StructLayout *SL =
912       getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
913     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
914       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
915     return;
916   } else if (Init->getType()->isFirstClassType()) {
917     GenericValue Val = getConstantValue(Init);
918     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
919     return;
920   }
921
922   cerr << "Bad Type: " << *Init->getType() << "\n";
923   llvm_unreachable("Unknown constant type to initialize memory with!");
924 }
925
926 /// EmitGlobals - Emit all of the global variables to memory, storing their
927 /// addresses into GlobalAddress.  This must make sure to copy the contents of
928 /// their initializers into the memory.
929 ///
930 void ExecutionEngine::emitGlobals() {
931
932   // Loop over all of the global variables in the program, allocating the memory
933   // to hold them.  If there is more than one module, do a prepass over globals
934   // to figure out how the different modules should link together.
935   //
936   std::map<std::pair<std::string, const Type*>,
937            const GlobalValue*> LinkedGlobalsMap;
938
939   if (Modules.size() != 1) {
940     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
941       Module &M = *Modules[m]->getModule();
942       for (Module::const_global_iterator I = M.global_begin(),
943            E = M.global_end(); I != E; ++I) {
944         const GlobalValue *GV = I;
945         if (GV->hasLocalLinkage() || GV->isDeclaration() ||
946             GV->hasAppendingLinkage() || !GV->hasName())
947           continue;// Ignore external globals and globals with internal linkage.
948           
949         const GlobalValue *&GVEntry = 
950           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
951
952         // If this is the first time we've seen this global, it is the canonical
953         // version.
954         if (!GVEntry) {
955           GVEntry = GV;
956           continue;
957         }
958         
959         // If the existing global is strong, never replace it.
960         if (GVEntry->hasExternalLinkage() ||
961             GVEntry->hasDLLImportLinkage() ||
962             GVEntry->hasDLLExportLinkage())
963           continue;
964         
965         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
966         // symbol.  FIXME is this right for common?
967         if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
968           GVEntry = GV;
969       }
970     }
971   }
972   
973   std::vector<const GlobalValue*> NonCanonicalGlobals;
974   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
975     Module &M = *Modules[m]->getModule();
976     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
977          I != E; ++I) {
978       // In the multi-module case, see what this global maps to.
979       if (!LinkedGlobalsMap.empty()) {
980         if (const GlobalValue *GVEntry = 
981               LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
982           // If something else is the canonical global, ignore this one.
983           if (GVEntry != &*I) {
984             NonCanonicalGlobals.push_back(I);
985             continue;
986           }
987         }
988       }
989       
990       if (!I->isDeclaration()) {
991         addGlobalMapping(I, getMemoryForGV(I));
992       } else {
993         // External variable reference. Try to use the dynamic loader to
994         // get a pointer to it.
995         if (void *SymAddr =
996             sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str()))
997           addGlobalMapping(I, SymAddr);
998         else {
999           llvm_report_error("Could not resolve external global address: "
1000                             +I->getName());
1001         }
1002       }
1003     }
1004     
1005     // If there are multiple modules, map the non-canonical globals to their
1006     // canonical location.
1007     if (!NonCanonicalGlobals.empty()) {
1008       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1009         const GlobalValue *GV = NonCanonicalGlobals[i];
1010         const GlobalValue *CGV =
1011           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1012         void *Ptr = getPointerToGlobalIfAvailable(CGV);
1013         assert(Ptr && "Canonical global wasn't codegen'd!");
1014         addGlobalMapping(GV, Ptr);
1015       }
1016     }
1017     
1018     // Now that all of the globals are set up in memory, loop through them all 
1019     // and initialize their contents.
1020     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1021          I != E; ++I) {
1022       if (!I->isDeclaration()) {
1023         if (!LinkedGlobalsMap.empty()) {
1024           if (const GlobalValue *GVEntry = 
1025                 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1026             if (GVEntry != &*I)  // Not the canonical variable.
1027               continue;
1028         }
1029         EmitGlobalVariable(I);
1030       }
1031     }
1032   }
1033 }
1034
1035 // EmitGlobalVariable - This method emits the specified global variable to the
1036 // address specified in GlobalAddresses, or allocates new memory if it's not
1037 // already in the map.
1038 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1039   void *GA = getPointerToGlobalIfAvailable(GV);
1040
1041   if (GA == 0) {
1042     // If it's not already specified, allocate memory for the global.
1043     GA = getMemoryForGV(GV);
1044     addGlobalMapping(GV, GA);
1045   }
1046   
1047   // Don't initialize if it's thread local, let the client do it.
1048   if (!GV->isThreadLocal())
1049     InitializeMemory(GV->getInitializer(), GA);
1050   
1051   const Type *ElTy = GV->getType()->getElementType();
1052   size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
1053   NumInitBytes += (unsigned)GVSize;
1054   ++NumGlobals;
1055 }