JIT.cpp revision fd7d99120b394e7aa4cf0b670f7b783a61dec3a1
1//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
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 tool implements a just-in-time compiler for LLVM, allowing direct
11// execution of LLVM bitcode in an efficient manner.
12//
13//===----------------------------------------------------------------------===//
14
15#include "JIT.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/GlobalVariable.h"
20#include "llvm/Instructions.h"
21#include "llvm/ModuleProvider.h"
22#include "llvm/CodeGen/MachineCodeEmitter.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/ExecutionEngine/GenericValue.h"
25#include "llvm/Support/MutexGuard.h"
26#include "llvm/System/DynamicLibrary.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetJITInfo.h"
30
31#include "llvm/Config/config.h"
32
33using namespace llvm;
34
35#ifdef __APPLE__
36// Apple gcc defaults to -fuse-cxa-atexit (i.e. calls __cxa_atexit instead
37// of atexit). It passes the address of linker generated symbol __dso_handle
38// to the function.
39// This configuration change happened at version 5330.
40# include <AvailabilityMacros.h>
41# if defined(MAC_OS_X_VERSION_10_4) && \
42     ((MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_4) || \
43      (MAC_OS_X_VERSION_MIN_REQUIRED == MAC_OS_X_VERSION_10_4 && \
44       __APPLE_CC__ >= 5330))
45#  ifndef HAVE___DSO_HANDLE
46#   define HAVE___DSO_HANDLE 1
47#  endif
48# endif
49#endif
50
51#if HAVE___DSO_HANDLE
52extern void *__dso_handle __attribute__ ((__visibility__ ("hidden")));
53#endif
54
55static struct RegisterJIT {
56  RegisterJIT() { JIT::Register(); }
57} JITRegistrator;
58
59namespace llvm {
60  void LinkInJIT() {
61  }
62}
63
64#if defined (__GNUC__)
65extern "C" void __register_frame(void*);
66#endif
67
68/// createJIT - This is the factory method for creating a JIT for the current
69/// machine, it does not fall back to the interpreter.  This takes ownership
70/// of the module provider.
71ExecutionEngine *ExecutionEngine::createJIT(ModuleProvider *MP,
72                                            std::string *ErrorStr,
73                                            JITMemoryManager *JMM) {
74  ExecutionEngine *EE = JIT::createJIT(MP, ErrorStr, JMM);
75  if (!EE) return 0;
76
77  // Register routine for informing unwinding runtime about new EH frames
78#if defined(__GNUC__)
79  EE->InstallExceptionTableRegister(__register_frame);
80#endif
81
82  // Make sure we can resolve symbols in the program as well. The zero arg
83  // to the function tells DynamicLibrary to load the program, not a library.
84  sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr);
85  return EE;
86}
87
88JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji,
89         JITMemoryManager *JMM)
90  : ExecutionEngine(MP), TM(tm), TJI(tji), jitstate(MP) {
91  setTargetData(TM.getTargetData());
92
93  // Initialize MCE
94  MCE = createEmitter(*this, JMM);
95
96  // Add target data
97  MutexGuard locked(lock);
98  FunctionPassManager &PM = jitstate.getPM(locked);
99  PM.add(new TargetData(*TM.getTargetData()));
100
101  // Turn the machine code intermediate representation into bytes in memory that
102  // may be executed.
103  if (TM.addPassesToEmitMachineCode(PM, *MCE, false /*fast*/)) {
104    cerr << "Target does not support machine code emission!\n";
105    abort();
106  }
107
108  // Initialize passes.
109  PM.doInitialization();
110}
111
112JIT::~JIT() {
113  delete MCE;
114  delete &TM;
115}
116
117/// run - Start execution with the specified function and arguments.
118///
119GenericValue JIT::runFunction(Function *F,
120                              const std::vector<GenericValue> &ArgValues) {
121  assert(F && "Function *F was null at entry to run()");
122
123  void *FPtr = getPointerToFunction(F);
124  assert(FPtr && "Pointer to fn's code was null after getPointerToFunction");
125  const FunctionType *FTy = F->getFunctionType();
126  const Type *RetTy = FTy->getReturnType();
127
128  assert((FTy->getNumParams() <= ArgValues.size() || FTy->isVarArg()) &&
129         "Too many arguments passed into function!");
130  assert(FTy->getNumParams() == ArgValues.size() &&
131         "This doesn't support passing arguments through varargs (yet)!");
132
133  // Handle some common cases first.  These cases correspond to common `main'
134  // prototypes.
135  if (RetTy == Type::Int32Ty || RetTy == Type::VoidTy) {
136    switch (ArgValues.size()) {
137    case 3:
138      if (FTy->getParamType(0) == Type::Int32Ty &&
139          isa<PointerType>(FTy->getParamType(1)) &&
140          isa<PointerType>(FTy->getParamType(2))) {
141        int (*PF)(int, char **, const char **) =
142          (int(*)(int, char **, const char **))(intptr_t)FPtr;
143
144        // Call the function.
145        GenericValue rv;
146        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
147                                 (char **)GVTOP(ArgValues[1]),
148                                 (const char **)GVTOP(ArgValues[2])));
149        return rv;
150      }
151      break;
152    case 2:
153      if (FTy->getParamType(0) == Type::Int32Ty &&
154          isa<PointerType>(FTy->getParamType(1))) {
155        int (*PF)(int, char **) = (int(*)(int, char **))(intptr_t)FPtr;
156
157        // Call the function.
158        GenericValue rv;
159        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue(),
160                                 (char **)GVTOP(ArgValues[1])));
161        return rv;
162      }
163      break;
164    case 1:
165      if (FTy->getNumParams() == 1 &&
166          FTy->getParamType(0) == Type::Int32Ty) {
167        GenericValue rv;
168        int (*PF)(int) = (int(*)(int))(intptr_t)FPtr;
169        rv.IntVal = APInt(32, PF(ArgValues[0].IntVal.getZExtValue()));
170        return rv;
171      }
172      break;
173    }
174  }
175
176  // Handle cases where no arguments are passed first.
177  if (ArgValues.empty()) {
178    GenericValue rv;
179    switch (RetTy->getTypeID()) {
180    default: assert(0 && "Unknown return type for function call!");
181    case Type::IntegerTyID: {
182      unsigned BitWidth = cast<IntegerType>(RetTy)->getBitWidth();
183      if (BitWidth == 1)
184        rv.IntVal = APInt(BitWidth, ((bool(*)())(intptr_t)FPtr)());
185      else if (BitWidth <= 8)
186        rv.IntVal = APInt(BitWidth, ((char(*)())(intptr_t)FPtr)());
187      else if (BitWidth <= 16)
188        rv.IntVal = APInt(BitWidth, ((short(*)())(intptr_t)FPtr)());
189      else if (BitWidth <= 32)
190        rv.IntVal = APInt(BitWidth, ((int(*)())(intptr_t)FPtr)());
191      else if (BitWidth <= 64)
192        rv.IntVal = APInt(BitWidth, ((int64_t(*)())(intptr_t)FPtr)());
193      else
194        assert(0 && "Integer types > 64 bits not supported");
195      return rv;
196    }
197    case Type::VoidTyID:
198      rv.IntVal = APInt(32, ((int(*)())(intptr_t)FPtr)());
199      return rv;
200    case Type::FloatTyID:
201      rv.FloatVal = ((float(*)())(intptr_t)FPtr)();
202      return rv;
203    case Type::DoubleTyID:
204      rv.DoubleVal = ((double(*)())(intptr_t)FPtr)();
205      return rv;
206    case Type::X86_FP80TyID:
207    case Type::FP128TyID:
208    case Type::PPC_FP128TyID:
209      assert(0 && "long double not supported yet");
210      return rv;
211    case Type::PointerTyID:
212      return PTOGV(((void*(*)())(intptr_t)FPtr)());
213    }
214  }
215
216  // Okay, this is not one of our quick and easy cases.  Because we don't have a
217  // full FFI, we have to codegen a nullary stub function that just calls the
218  // function we are interested in, passing in constants for all of the
219  // arguments.  Make this function and return.
220
221  // First, create the function.
222  FunctionType *STy=FunctionType::get(RetTy, std::vector<const Type*>(), false);
223  Function *Stub = Function::Create(STy, Function::InternalLinkage, "",
224                                    F->getParent());
225
226  // Insert a basic block.
227  BasicBlock *StubBB = BasicBlock::Create("", Stub);
228
229  // Convert all of the GenericValue arguments over to constants.  Note that we
230  // currently don't support varargs.
231  SmallVector<Value*, 8> Args;
232  for (unsigned i = 0, e = ArgValues.size(); i != e; ++i) {
233    Constant *C = 0;
234    const Type *ArgTy = FTy->getParamType(i);
235    const GenericValue &AV = ArgValues[i];
236    switch (ArgTy->getTypeID()) {
237    default: assert(0 && "Unknown argument type for function call!");
238    case Type::IntegerTyID:
239        C = ConstantInt::get(AV.IntVal);
240        break;
241    case Type::FloatTyID:
242        C = ConstantFP::get(APFloat(AV.FloatVal));
243        break;
244    case Type::DoubleTyID:
245        C = ConstantFP::get(APFloat(AV.DoubleVal));
246        break;
247    case Type::PPC_FP128TyID:
248    case Type::X86_FP80TyID:
249    case Type::FP128TyID:
250        C = ConstantFP::get(APFloat(AV.IntVal));
251        break;
252    case Type::PointerTyID:
253      void *ArgPtr = GVTOP(AV);
254      if (sizeof(void*) == 4)
255        C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
256      else
257        C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
258      C = ConstantExpr::getIntToPtr(C, ArgTy);  // Cast the integer to pointer
259      break;
260    }
261    Args.push_back(C);
262  }
263
264  CallInst *TheCall = CallInst::Create(F, Args.begin(), Args.end(), "", StubBB);
265  TheCall->setTailCall();
266  if (TheCall->getType() != Type::VoidTy)
267    ReturnInst::Create(TheCall, StubBB);             // Return result of the call.
268  else
269    ReturnInst::Create(StubBB);                      // Just return void.
270
271  // Finally, return the value returned by our nullary stub function.
272  return runFunction(Stub, std::vector<GenericValue>());
273}
274
275/// runJITOnFunction - Run the FunctionPassManager full of
276/// just-in-time compilation passes on F, hopefully filling in
277/// GlobalAddress[F] with the address of F's machine code.
278///
279void JIT::runJITOnFunction(Function *F) {
280  static bool isAlreadyCodeGenerating = false;
281
282  MutexGuard locked(lock);
283  assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
284
285  // JIT the function
286  isAlreadyCodeGenerating = true;
287  jitstate.getPM(locked).run(*F);
288  isAlreadyCodeGenerating = false;
289
290  // If the function referred to a global variable that had not yet been
291  // emitted, it allocates memory for the global, but doesn't emit it yet.  Emit
292  // all of these globals now.
293  while (!jitstate.getPendingGlobals(locked).empty()) {
294    const GlobalVariable *GV = jitstate.getPendingGlobals(locked).back();
295    jitstate.getPendingGlobals(locked).pop_back();
296    EmitGlobalVariable(GV);
297  }
298}
299
300/// getPointerToFunction - This method is used to get the address of the
301/// specified function, compiling it if neccesary.
302///
303void *JIT::getPointerToFunction(Function *F) {
304
305  if (void *Addr = getPointerToGlobalIfAvailable(F))
306    return Addr;   // Check if function already code gen'd
307
308  // Make sure we read in the function if it exists in this Module.
309  if (F->hasNotBeenReadFromBitcode()) {
310    // Determine the module provider this function is provided by.
311    Module *M = F->getParent();
312    ModuleProvider *MP = 0;
313    for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
314      if (Modules[i]->getModule() == M) {
315        MP = Modules[i];
316        break;
317      }
318    }
319    assert(MP && "Function isn't in a module we know about!");
320
321    std::string ErrorMsg;
322    if (MP->materializeFunction(F, &ErrorMsg)) {
323      cerr << "Error reading function '" << F->getName()
324           << "' from bitcode file: " << ErrorMsg << "\n";
325      abort();
326    }
327  }
328
329  if (void *Addr = getPointerToGlobalIfAvailable(F)) {
330    return Addr;
331  }
332
333  MutexGuard locked(lock);
334
335  if (F->isDeclaration()) {
336    void *Addr = getPointerToNamedFunction(F->getName());
337    addGlobalMapping(F, Addr);
338    return Addr;
339  }
340
341  runJITOnFunction(F);
342
343  void *Addr = getPointerToGlobalIfAvailable(F);
344  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
345  return Addr;
346}
347
348/// getOrEmitGlobalVariable - Return the address of the specified global
349/// variable, possibly emitting it to memory if needed.  This is used by the
350/// Emitter.
351void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
352  MutexGuard locked(lock);
353
354  void *Ptr = getPointerToGlobalIfAvailable(GV);
355  if (Ptr) return Ptr;
356
357  // If the global is external, just remember the address.
358  if (GV->isDeclaration()) {
359#if HAVE___DSO_HANDLE
360    if (GV->getName() == "__dso_handle")
361      return (void*)&__dso_handle;
362#endif
363    Ptr = sys::DynamicLibrary::SearchForAddressOfSymbol(GV->getName().c_str());
364    if (Ptr == 0) {
365      cerr << "Could not resolve external global address: "
366           << GV->getName() << "\n";
367      abort();
368    }
369  } else {
370    // If the global hasn't been emitted to memory yet, allocate space.  We will
371    // actually initialize the global after current function has finished
372    // compilation.
373    const Type *GlobalType = GV->getType()->getElementType();
374    size_t S = getTargetData()->getABITypeSize(GlobalType);
375    size_t A = getTargetData()->getPreferredAlignment(GV);
376    if (A <= 8) {
377      Ptr = malloc(S);
378    } else {
379      // Allocate S+A bytes of memory, then use an aligned pointer within that
380      // space.
381      Ptr = malloc(S+A);
382      unsigned MisAligned = ((intptr_t)Ptr & (A-1));
383      Ptr = (char*)Ptr + (MisAligned ? (A-MisAligned) : 0);
384    }
385    jitstate.getPendingGlobals(locked).push_back(GV);
386  }
387  addGlobalMapping(GV, Ptr);
388  return Ptr;
389}
390
391
392/// recompileAndRelinkFunction - This method is used to force a function
393/// which has already been compiled, to be compiled again, possibly
394/// after it has been modified. Then the entry to the old copy is overwritten
395/// with a branch to the new copy. If there was no old copy, this acts
396/// just like JIT::getPointerToFunction().
397///
398void *JIT::recompileAndRelinkFunction(Function *F) {
399  void *OldAddr = getPointerToGlobalIfAvailable(F);
400
401  // If it's not already compiled there is no reason to patch it up.
402  if (OldAddr == 0) { return getPointerToFunction(F); }
403
404  // Delete the old function mapping.
405  addGlobalMapping(F, 0);
406
407  // Recodegen the function
408  runJITOnFunction(F);
409
410  // Update state, forward the old function to the new function.
411  void *Addr = getPointerToGlobalIfAvailable(F);
412  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
413  TJI.replaceMachineCodeForFunction(OldAddr, Addr);
414  return Addr;
415}
416
417