1 //===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
3 // This file implements the top-level support for creating a Just-In-Time
4 // compiler for the current architecture.
6 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/GenericValue.h"
10 #include "llvm/Target/TargetMachine.h"
11 #include "llvm/Target/TargetMachineImpls.h"
12 #include "llvm/Module.h"
13 #include "Support/CommandLine.h"
16 #include "llvm/PassManager.h"
18 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
19 #define NO_JITS_ENABLED
23 enum ArchName { x86, Sparc };
25 #ifndef NO_JITS_ENABLED
27 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
30 clEnumVal(x86, " IA-32 (Pentium and above)"),
32 #ifdef ENABLE_SPARC_JIT
33 clEnumValN(Sparc, "sparc", " Sparc-V9"),
36 #if defined(ENABLE_X86_JIT)
38 #elif defined(ENABLE_SPARC_JIT)
42 #endif /* NO_JITS_ENABLED */
45 /// create - Create an return a new JIT compiler if there is one available
46 /// for the current target. Otherwise, return null.
48 ExecutionEngine *VM::create(Module *M) {
49 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
51 // Allow a command-line switch to override what *should* be the default target
52 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
53 // our X86 machines are much faster at recompiling LLVM and linking LLI.
54 #ifdef NO_JITS_ENABLED
61 TargetMachineAllocator = allocateX86TargetMachine;
64 #ifdef ENABLE_SPARC_JIT
66 TargetMachineAllocator = allocateSparcTargetMachine;
70 assert(0 && "-march flag not supported on this host!");
73 // Allocate a target...
74 TargetMachine *Target = TargetMachineAllocator(*M);
75 assert(Target && "Could not allocate target machine!");
77 // Create the virtual machine object...
78 return new VM(M, Target);
81 VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
82 setTargetData(TM.getTargetData());
85 MCE = createEmitter(*this);
89 #ifdef ENABLE_SPARC_JIT
90 // THIS GOES BEYOND UGLY HACKS
91 if (TM.getName() == "UltraSparc-Native") {
92 extern Pass *createPreSelectionPass(TargetMachine &TM);
94 // Specialize LLVM code for this target machine and then
95 // run basic dataflow optimizations on LLVM code.
96 PM.add(createPreSelectionPass(TM));
104 /// run - Start execution with the specified function and arguments.
106 GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
108 assert (F && "Function *F was null at entry to run()");
110 int (*PF)(int, char **, const char **) =
111 (int(*)(int, char **, const char **))getPointerToFunction(F);
112 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
114 // Call the function.
115 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
116 (const char **) GVTOP (ArgValues[2]));
118 // Run any atexit handlers now!
122 rv.IntVal = ExitCode;