Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 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" |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 12 | #include "Support/CommandLine.h" |
| 13 | |
Misha Brukman | 4e8c999 | 2003-06-06 06:59:55 +0000 | [diff] [blame] | 14 | // FIXME: REMOVE THIS |
| 15 | #include "llvm/PassManager.h" |
| 16 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 17 | namespace { |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 18 | enum ArchName { nojit, x86, sparc }; |
| 19 | |
| 20 | cl::opt<ArchName> |
| 21 | Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix, |
| 22 | cl::values(clEnumVal(x86, " IA-32 (pentium and above)"), |
| 23 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 24 | clEnumVal(sparc, " Sparc-V9"), |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 25 | #endif |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 26 | 0), |
| 27 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
| 28 | cl::init(x86) |
| 29 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 30 | cl::init(sparc) |
| 31 | #else |
| 32 | cl::init(nojit) |
| 33 | #endif |
| 34 | ); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 35 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 36 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 37 | /// createJIT - Create an return a new JIT compiler if there is one available |
| 38 | /// for the current target. Otherwise it returns null. |
| 39 | /// |
| 40 | ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) { |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 41 | |
| 42 | TargetMachine* (*TargetMachineAllocator)(unsigned) = 0; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 43 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 44 | // Allow a command-line switch to override what *should* be the default target |
| 45 | // machine for this platform. This allows for debugging a Sparc JIT on X86 -- |
| 46 | // our X86 machines are much faster at recompiling LLVM and linking lli. |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 47 | switch (Arch) { |
| 48 | case x86: |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 49 | TargetMachineAllocator = allocateX86TargetMachine; |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 50 | break; |
Chris Lattner | 7aefa96 | 2003-06-17 15:32:38 +0000 | [diff] [blame] | 51 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 52 | case sparc: |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 53 | TargetMachineAllocator = allocateSparcTargetMachine; |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 54 | break; |
Chris Lattner | 7aefa96 | 2003-06-17 15:32:38 +0000 | [diff] [blame] | 55 | #endif |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 56 | default: |
| 57 | assert(0 && "-march flag not supported on this host!"); |
| 58 | case nojit: |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
Chris Lattner | 97ac14f | 2003-06-17 15:43:13 +0000 | [diff] [blame^] | 61 | |
| 62 | // Allocate a target... |
| 63 | TargetMachine *Target = (*TargetMachineAllocator)(Config); |
| 64 | assert(Target && "Could not allocate target machine!"); |
| 65 | |
| 66 | // Create the virtual machine object... |
| 67 | return new VM(M, Target); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) { |
| 71 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 72 | |
| 73 | // Initialize MCE |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 74 | MCE = createEmitter(*this); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 75 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 76 | setupPassManager(); |
Misha Brukman | 4e8c999 | 2003-06-06 06:59:55 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 7aefa96 | 2003-06-17 15:32:38 +0000 | [diff] [blame] | 78 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Misha Brukman | 4e8c999 | 2003-06-06 06:59:55 +0000 | [diff] [blame] | 79 | // THIS GOES BEYOND UGLY HACKS |
| 80 | if (TM.getName() == "UltraSparc-Native") { |
| 81 | extern Pass *createPreSelectionPass(TargetMachine &TM); |
| 82 | PassManager PM; |
| 83 | // Specialize LLVM code for this target machine and then |
| 84 | // run basic dataflow optimizations on LLVM code. |
| 85 | PM.add(createPreSelectionPass(TM)); |
| 86 | PM.run(*M); |
| 87 | } |
Chris Lattner | 7aefa96 | 2003-06-17 15:32:38 +0000 | [diff] [blame] | 88 | #endif |
Misha Brukman | 4e8c999 | 2003-06-06 06:59:55 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 90 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | int VM::run(const std::string &FnName, const std::vector<std::string> &Args) { |
| 94 | Function *F = getModule().getNamedFunction(FnName); |
| 95 | if (F == 0) { |
| 96 | std::cerr << "Could not find function '" << FnName <<"' in module!\n"; |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F); |
| 101 | assert(PF != 0 && "Null pointer to function?"); |
| 102 | |
| 103 | // Build an argv vector... |
| 104 | char **Argv = (char**)CreateArgv(Args); |
| 105 | |
| 106 | // Call the main function... |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 107 | int Result = PF(Args.size(), Argv); |
| 108 | |
| 109 | // Run any atexit handlers now! |
| 110 | runAtExitHandlers(); |
| 111 | return Result; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 112 | } |