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 | |
| 14 | namespace { |
| 15 | cl::opt<std::string> |
| 16 | Arch("march", cl::desc("Architecture: `x86' or `sparc'"), cl::Prefix, |
| 17 | cl::value_desc("machine architecture")); |
| 18 | |
| 19 | static std::string DefaultArch = |
| 20 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
| 21 | "x86"; |
| 22 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 23 | "sparc"; |
| 24 | #else |
| 25 | ""; |
| 26 | #endif |
| 27 | |
| 28 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 29 | |
| 30 | |
| 31 | /// createJIT - Create an return a new JIT compiler if there is one available |
| 32 | /// for the current target. Otherwise it returns null. |
| 33 | /// |
| 34 | ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) { |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame^] | 35 | |
| 36 | TargetMachine* (*TargetMachineAllocator)(unsigned) = 0; |
| 37 | if (Arch == "") |
| 38 | Arch = DefaultArch; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 39 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame^] | 40 | // Allow a command-line switch to override what *should* be the default target |
| 41 | // machine for this platform. This allows for debugging a Sparc JIT on X86 -- |
| 42 | // our X86 machines are much faster at recompiling LLVM and linking lli. |
| 43 | if (Arch == "x86") { |
| 44 | TargetMachineAllocator = allocateX86TargetMachine; |
| 45 | } else if (Arch == "sparc") { |
| 46 | TargetMachineAllocator = allocateSparcTargetMachine; |
| 47 | } |
| 48 | |
| 49 | if (TargetMachineAllocator) { |
| 50 | // Allocate a target... |
| 51 | TargetMachine *Target = (*TargetMachineAllocator)(Config); |
| 52 | assert(Target && "Could not allocate target machine!"); |
| 53 | |
| 54 | // Create the virtual machine object... |
| 55 | return new VM(M, Target); |
| 56 | } else { |
| 57 | return 0; |
| 58 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) { |
| 62 | setTargetData(TM.getTargetData()); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame^] | 63 | |
| 64 | // Initialize MCE |
| 65 | if (Arch == "x86") { |
| 66 | MCE = createX86Emitter(*this); |
| 67 | } else if (Arch == "sparc") { |
| 68 | MCE = createSparcEmitter(*this); |
| 69 | } |
| 70 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 71 | setupPassManager(); |
| 72 | registerCallback(); |
Chris Lattner | 56adf15 | 2003-05-12 02:14:34 +0000 | [diff] [blame] | 73 | emitGlobals(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int VM::run(const std::string &FnName, const std::vector<std::string> &Args) { |
| 77 | Function *F = getModule().getNamedFunction(FnName); |
| 78 | if (F == 0) { |
| 79 | std::cerr << "Could not find function '" << FnName <<"' in module!\n"; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F); |
| 84 | assert(PF != 0 && "Null pointer to function?"); |
| 85 | |
| 86 | // Build an argv vector... |
| 87 | char **Argv = (char**)CreateArgv(Args); |
| 88 | |
| 89 | // Call the main function... |
Chris Lattner | 22080f9 | 2003-05-14 13:53:40 +0000 | [diff] [blame] | 90 | int Result = PF(Args.size(), Argv); |
| 91 | |
| 92 | // Run any atexit handlers now! |
| 93 | runAtExitHandlers(); |
| 94 | return Result; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 95 | } |