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