1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the top-level support for creating a Just-In-Time
11 // compiler for the current architecture.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/ExecutionEngine/GenericValue.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/Target/TargetMachineImpls.h"
21 #include "Support/CommandLine.h"
23 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
24 #define NO_JITS_ENABLED
30 enum ArchName { x86, Sparc };
32 #ifndef NO_JITS_ENABLED
34 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
37 clEnumVal(x86, " IA-32 (Pentium and above)"),
39 #ifdef ENABLE_SPARC_JIT
40 clEnumValN(Sparc, "sparc", " Sparc-V9"),
43 #if defined(ENABLE_X86_JIT)
45 #elif defined(ENABLE_SPARC_JIT)
49 #endif /* NO_JITS_ENABLED */
52 /// create - Create an return a new JIT compiler if there is one available
53 /// for the current target. Otherwise, return null.
55 ExecutionEngine *VM::create(ModuleProvider *MP) {
56 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
58 // Allow a command-line switch to override what *should* be the default target
59 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
60 // our X86 machines are much faster at recompiling LLVM and linking LLI.
61 #ifndef NO_JITS_ENABLED
66 TargetMachineAllocator = allocateX86TargetMachine;
69 #ifdef ENABLE_SPARC_JIT
71 TargetMachineAllocator = allocateSparcTargetMachine;
75 assert(0 && "-march flag not supported on this host!");
81 // Allocate a target...
82 TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
83 assert(Target && "Could not allocate target machine!");
85 // Create the virtual machine object...
86 return new VM(MP, Target);
89 VM::VM(ModuleProvider *MP, TargetMachine *tm) : ExecutionEngine(MP), TM(*tm),
92 setTargetData(TM.getTargetData());
95 MCE = createEmitter(*this);
102 /// run - Start execution with the specified function and arguments.
104 GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
106 assert (F && "Function *F was null at entry to run()");
108 int (*PF)(int, char **, const char **) =
109 (int(*)(int, char **, const char **))getPointerToFunction(F);
110 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
112 // Call the function.
113 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
114 (const char **) GVTOP (ArgValues[2]));
116 // Run any atexit handlers now!
120 rv.IntVal = ExitCode;
124 } // End llvm namespace