JIT.cpp revision bd199fb1148b9e16c4e6f3d0ee386c2505a55b71
1//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
2//
3// This file implements the top-level support for creating a Just-In-Time
4// compiler for the current architecture.
5//
6//===----------------------------------------------------------------------===//
7
8#include "VM.h"
9#include "llvm/Target/TargetMachine.h"
10#include "llvm/Target/TargetMachineImpls.h"
11#include "llvm/Module.h"
12
13
14/// createJIT - Create an return a new JIT compiler if there is one available
15/// for the current target.  Otherwise it returns null.
16///
17ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
18  // FIXME: This should be controlled by which subdirectory gets linked in!
19#if !defined(i386) && !defined(__i386__) && !defined(__x86__)
20  return 0;
21#endif
22  // Allocate a target... in the future this will be controllable on the
23  // command line.
24  TargetMachine *Target = allocateX86TargetMachine(Config);
25  assert(Target && "Could not allocate X86 target machine!");
26
27  // Create the virtual machine object...
28  return new VM(M, Target);
29}
30
31VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
32  setTargetData(TM.getTargetData());
33  MCE = createEmitter(*this);  // Initialize MCE
34  setupPassManager();
35  registerCallback();
36}
37
38int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
39  Function *F = getModule().getNamedFunction(FnName);
40  if (F == 0) {
41    std::cerr << "Could not find function '" << FnName <<"' in module!\n";
42    return 1;
43  }
44
45  int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
46  assert(PF != 0 && "Null pointer to function?");
47
48  // Build an argv vector...
49  char **Argv = (char**)CreateArgv(Args);
50
51  // Call the main function...
52  return PF(Args.size(), Argv);
53}
54