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