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/Target/TargetMachine.h"
10 #include "llvm/Target/TargetMachineImpls.h"
11 #include "llvm/Module.h"
12 #include "Support/CommandLine.h"
15 #include "llvm/PassManager.h"
17 #if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
18 #define NO_JITS_ENABLED
22 enum ArchName { x86, Sparc };
24 #ifndef NO_JITS_ENABLED
26 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
29 clEnumVal(x86, " IA-32 (Pentium and above)"),
31 #ifdef ENABLE_SPARC_JIT
32 clEnumValN(Sparc, "sparc", " Sparc-V9"),
35 #if defined(ENABLE_X86_JIT)
37 #elif defined(ENABLE_SPARC_JIT)
41 #endif /* NO_JITS_ENABLED */
44 /// createJIT - Create an return a new JIT compiler if there is one available
45 /// for the current target. Otherwise it returns null.
47 ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
49 TargetMachine* (*TargetMachineAllocator)(unsigned) = 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)(Config);
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 /// VM::run - This method begins the execution of a program beginning at the
105 /// specified function name. The function is called with the specified
106 /// arguments and array of environment variables (a la main()).
109 /// FnName - The name of the function as a C++ string.
110 /// Args - A vector of C++ strings containing the arguments.
111 /// envp - An array of C strings containing the environment.
114 /// 1 - An error occurred.
115 /// Otherwise, the return value from the specified function is returned.
117 int VM::run(const std::string &FnName, const std::vector<std::string> &Args,
119 Function *F = getModule().getNamedFunction(FnName);
121 std::cerr << "Could not find function '" << FnName << "' in module!\n";
125 int (*PF)(int, char**, const char**) =
126 (int(*)(int, char**, const char**))getPointerToFunction(F);
127 assert(PF != 0 && "Null pointer to function?");
129 // Build an argv vector...
130 char **Argv = (char**)CreateArgv(Args);
132 // Call the main function...
133 int Result = PF(Args.size(), Argv, envp);
135 // Run any atexit handlers now!