JIT.cpp revision 1e60a9165dc4d6ce5650dacc026f2942696af920
1//===-- JIT.cpp - LLVM Just-In-Time Compiler ------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the top-level support for creating a Just-In-Time 11// compiler for the current architecture. 12// 13//===----------------------------------------------------------------------===// 14 15#include "VM.h" 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" 22using namespace llvm; 23 24#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT) 25#define NO_JITS_ENABLED 26#endif 27 28namespace { 29 enum ArchName { x86, Sparc }; 30 31#ifndef NO_JITS_ENABLED 32 cl::opt<ArchName> 33 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix, 34 cl::values( 35#ifdef ENABLE_X86_JIT 36 clEnumVal(x86, " IA-32 (Pentium and above)"), 37#endif 38#ifdef ENABLE_SPARC_JIT 39 clEnumValN(Sparc, "sparc", " Sparc-V9"), 40#endif 41 0), 42#if defined(ENABLE_X86_JIT) 43 cl::init(x86) 44#elif defined(ENABLE_SPARC_JIT) 45 cl::init(Sparc) 46#endif 47 ); 48#endif /* NO_JITS_ENABLED */ 49} 50 51/// create - Create an return a new JIT compiler if there is one available 52/// for the current target. Otherwise, return null. 53/// 54ExecutionEngine *VM::create(ModuleProvider *MP) { 55 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0; 56 57 // Allow a command-line switch to override what *should* be the default target 58 // machine for this platform. This allows for debugging a Sparc JIT on X86 -- 59 // our X86 machines are much faster at recompiling LLVM and linking LLI. 60#ifndef NO_JITS_ENABLED 61 62 switch (Arch) { 63#ifdef ENABLE_X86_JIT 64 case x86: 65 TargetMachineAllocator = allocateX86TargetMachine; 66 break; 67#endif 68#ifdef ENABLE_SPARC_JIT 69 case Sparc: 70 TargetMachineAllocator = allocateSparcTargetMachine; 71 break; 72#endif 73 default: 74 assert(0 && "-march flag not supported on this host!"); 75 } 76#else 77 return 0; 78#endif 79 80 // Allocate a target... 81 TargetMachine *Target = TargetMachineAllocator(*MP->getModule()); 82 assert(Target && "Could not allocate target machine!"); 83 84 // If the target supports JIT code generation, return a new JIT now. 85 if (TargetJITInfo *TJ = Target->getJITInfo()) 86 return new VM(MP, *Target, *TJ); 87 return 0; 88} 89 90VM::VM(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji) 91 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) { 92 setTargetData(TM.getTargetData()); 93 94 // Initialize MCE 95 MCE = createEmitter(*this); 96 97 setupPassManager(); 98 99 emitGlobals(); 100} 101 102/// run - Start execution with the specified function and arguments. 103/// 104GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues) 105{ 106 assert (F && "Function *F was null at entry to run()"); 107 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"); 111 112 // Call the function. 113 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]), 114 (const char **) GVTOP (ArgValues[2])); 115 116 // Run any atexit handlers now! 117 runAtExitHandlers(); 118 119 GenericValue rv; 120 rv.IntVal = ExitCode; 121 return rv; 122} 123