blob: ef4788499709d35fd10657a58a700096aa98d8eb [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- 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 Brukmanabb027c2003-05-27 21:40:39 +000012#include "Support/CommandLine.h"
13
Misha Brukman4e8c9992003-06-06 06:59:55 +000014// FIXME: REMOVE THIS
15#include "llvm/PassManager.h"
16
Misha Brukman82742912003-07-02 17:53:19 +000017#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
18#define NO_JITS_ENABLED
19#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000020
Misha Brukman82742912003-07-02 17:53:19 +000021namespace {
22 enum ArchName { x86, Sparc };
23
24#ifndef NO_JITS_ENABLED
Chris Lattner97ac14f2003-06-17 15:43:13 +000025 cl::opt<ArchName>
26 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
Misha Brukman82742912003-07-02 17:53:19 +000027 cl::values(
28#ifdef ENABLE_X86_JIT
29 clEnumVal(x86, " IA-32 (Pentium and above)"),
30#endif
31#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000032 clEnumValN(Sparc, "sparc", " Sparc-V9"),
Misha Brukmanabb027c2003-05-27 21:40:39 +000033#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000034 0),
Misha Brukman82742912003-07-02 17:53:19 +000035#if defined(ENABLE_X86_JIT)
Chris Lattner97ac14f2003-06-17 15:43:13 +000036 cl::init(x86)
Misha Brukman82742912003-07-02 17:53:19 +000037#elif defined(ENABLE_SPARC_JIT)
Chris Lattnerde3209b2003-06-17 15:54:02 +000038 cl::init(Sparc)
Chris Lattner97ac14f2003-06-17 15:43:13 +000039#endif
40 );
Misha Brukman82742912003-07-02 17:53:19 +000041#endif /* NO_JITS_ENABLED */
Misha Brukmanabb027c2003-05-27 21:40:39 +000042}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000043
Chris Lattnerbd199fb2002-12-24 00:01:05 +000044/// createJIT - Create an return a new JIT compiler if there is one available
45/// for the current target. Otherwise it returns null.
46///
47ExecutionEngine *ExecutionEngine::createJIT(Module *M, unsigned Config) {
Misha Brukmanabb027c2003-05-27 21:40:39 +000048
49 TargetMachine* (*TargetMachineAllocator)(unsigned) = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000050
Misha Brukmanabb027c2003-05-27 21:40:39 +000051 // 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 --
Misha Brukman82742912003-07-02 17:53:19 +000053 // our X86 machines are much faster at recompiling LLVM and linking LLI.
54#ifdef NO_JITS_ENABLED
55 return 0;
56#endif
57
Chris Lattner97ac14f2003-06-17 15:43:13 +000058 switch (Arch) {
Misha Brukman82742912003-07-02 17:53:19 +000059#ifdef ENABLE_X86_JIT
Chris Lattner97ac14f2003-06-17 15:43:13 +000060 case x86:
Misha Brukmanabb027c2003-05-27 21:40:39 +000061 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000062 break;
Misha Brukman82742912003-07-02 17:53:19 +000063#endif
64#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000065 case Sparc:
Misha Brukman906f5fa2003-06-02 03:23:16 +000066 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000067 break;
Chris Lattner7aefa962003-06-17 15:32:38 +000068#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000069 default:
70 assert(0 && "-march flag not supported on this host!");
Misha Brukmanabb027c2003-05-27 21:40:39 +000071 }
Chris Lattner97ac14f2003-06-17 15:43:13 +000072
73 // Allocate a target...
74 TargetMachine *Target = (*TargetMachineAllocator)(Config);
75 assert(Target && "Could not allocate target machine!");
76
77 // Create the virtual machine object...
78 return new VM(M, Target);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000079}
80
81VM::VM(Module *M, TargetMachine *tm) : ExecutionEngine(M), TM(*tm) {
82 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000083
84 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000085 MCE = createEmitter(*this);
Misha Brukmanabb027c2003-05-27 21:40:39 +000086
Chris Lattnerbd199fb2002-12-24 00:01:05 +000087 setupPassManager();
Misha Brukman4e8c9992003-06-06 06:59:55 +000088
Misha Brukman82742912003-07-02 17:53:19 +000089#ifdef ENABLE_SPARC_JIT
Misha Brukman4e8c9992003-06-06 06:59:55 +000090 // THIS GOES BEYOND UGLY HACKS
91 if (TM.getName() == "UltraSparc-Native") {
92 extern Pass *createPreSelectionPass(TargetMachine &TM);
93 PassManager PM;
94 // Specialize LLVM code for this target machine and then
95 // run basic dataflow optimizations on LLVM code.
96 PM.add(createPreSelectionPass(TM));
97 PM.run(*M);
98 }
Chris Lattner7aefa962003-06-17 15:32:38 +000099#endif
Misha Brukman4e8c9992003-06-06 06:59:55 +0000100
Chris Lattner56adf152003-05-12 02:14:34 +0000101 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000102}
103
104int VM::run(const std::string &FnName, const std::vector<std::string> &Args) {
105 Function *F = getModule().getNamedFunction(FnName);
106 if (F == 0) {
107 std::cerr << "Could not find function '" << FnName <<"' in module!\n";
108 return 1;
109 }
110
111 int(*PF)(int, char**) = (int(*)(int, char**))getPointerToFunction(F);
112 assert(PF != 0 && "Null pointer to function?");
113
114 // Build an argv vector...
115 char **Argv = (char**)CreateArgv(Args);
116
117 // Call the main function...
Chris Lattner22080f92003-05-14 13:53:40 +0000118 int Result = PF(Args.size(), Argv);
119
120 // Run any atexit handlers now!
121 runAtExitHandlers();
122 return Result;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000123}