blob: fedb6e46b087b70351128fabef014a24b3273f12 [file] [log] [blame]
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerbd199fb2002-12-24 00:01:05 +00009//
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"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000016#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
Brian Gaeke97222942003-09-05 19:39:22 +000018#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetMachineImpls.h"
Misha Brukmanabb027c2003-05-27 21:40:39 +000021#include "Support/CommandLine.h"
22
Misha Brukman82742912003-07-02 17:53:19 +000023#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
24#define NO_JITS_ENABLED
25#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000026
Misha Brukman82742912003-07-02 17:53:19 +000027namespace {
28 enum ArchName { x86, Sparc };
29
30#ifndef NO_JITS_ENABLED
Chris Lattner97ac14f2003-06-17 15:43:13 +000031 cl::opt<ArchName>
32 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
Misha Brukman82742912003-07-02 17:53:19 +000033 cl::values(
34#ifdef ENABLE_X86_JIT
35 clEnumVal(x86, " IA-32 (Pentium and above)"),
36#endif
37#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000038 clEnumValN(Sparc, "sparc", " Sparc-V9"),
Misha Brukmanabb027c2003-05-27 21:40:39 +000039#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000040 0),
Misha Brukman82742912003-07-02 17:53:19 +000041#if defined(ENABLE_X86_JIT)
Chris Lattner97ac14f2003-06-17 15:43:13 +000042 cl::init(x86)
Misha Brukman82742912003-07-02 17:53:19 +000043#elif defined(ENABLE_SPARC_JIT)
Chris Lattnerde3209b2003-06-17 15:54:02 +000044 cl::init(Sparc)
Chris Lattner97ac14f2003-06-17 15:43:13 +000045#endif
46 );
Misha Brukman82742912003-07-02 17:53:19 +000047#endif /* NO_JITS_ENABLED */
Misha Brukmanabb027c2003-05-27 21:40:39 +000048}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000049
Brian Gaeke82d82772003-09-03 20:34:19 +000050/// create - Create an return a new JIT compiler if there is one available
51/// for the current target. Otherwise, return null.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000052///
Misha Brukman005e5e92003-10-14 21:37:41 +000053ExecutionEngine *VM::create(ModuleProvider *MP) {
Chris Lattner39c07262003-08-24 19:50:53 +000054 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000055
Misha Brukmanabb027c2003-05-27 21:40:39 +000056 // Allow a command-line switch to override what *should* be the default target
57 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
Misha Brukman82742912003-07-02 17:53:19 +000058 // our X86 machines are much faster at recompiling LLVM and linking LLI.
Brian Gaeke11911ab2003-10-29 04:24:09 +000059#ifndef NO_JITS_ENABLED
Misha Brukman82742912003-07-02 17:53:19 +000060
Chris Lattner97ac14f2003-06-17 15:43:13 +000061 switch (Arch) {
Misha Brukman82742912003-07-02 17:53:19 +000062#ifdef ENABLE_X86_JIT
Chris Lattner97ac14f2003-06-17 15:43:13 +000063 case x86:
Misha Brukmanabb027c2003-05-27 21:40:39 +000064 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000065 break;
Misha Brukman82742912003-07-02 17:53:19 +000066#endif
67#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000068 case Sparc:
Misha Brukman906f5fa2003-06-02 03:23:16 +000069 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000070 break;
Chris Lattner7aefa962003-06-17 15:32:38 +000071#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000072 default:
73 assert(0 && "-march flag not supported on this host!");
Misha Brukmanabb027c2003-05-27 21:40:39 +000074 }
Brian Gaeke11911ab2003-10-29 04:24:09 +000075#else
76 return 0;
77#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000078
79 // Allocate a target...
Misha Brukman0f4f7d92003-10-16 21:19:34 +000080 TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
Chris Lattner97ac14f2003-06-17 15:43:13 +000081 assert(Target && "Could not allocate target machine!");
82
83 // Create the virtual machine object...
Misha Brukman005e5e92003-10-14 21:37:41 +000084 return new VM(MP, Target);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000085}
86
Misha Brukman005e5e92003-10-14 21:37:41 +000087VM::VM(ModuleProvider *MP, TargetMachine *tm) : ExecutionEngine(MP), TM(*tm),
88 PM(MP)
89{
Chris Lattnerbd199fb2002-12-24 00:01:05 +000090 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000091
92 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000093 MCE = createEmitter(*this);
Misha Brukmanabb027c2003-05-27 21:40:39 +000094
Chris Lattnerbd199fb2002-12-24 00:01:05 +000095 setupPassManager();
Misha Brukman4e8c9992003-06-06 06:59:55 +000096
Chris Lattner56adf152003-05-12 02:14:34 +000097 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000098}
99
Brian Gaeke70975ee2003-09-05 18:42:01 +0000100/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000101///
Brian Gaeke70975ee2003-09-05 18:42:01 +0000102GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
103{
104 assert (F && "Function *F was null at entry to run()");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000105
Brian Gaeke70975ee2003-09-05 18:42:01 +0000106 int (*PF)(int, char **, const char **) =
107 (int(*)(int, char **, const char **))getPointerToFunction(F);
108 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000109
Brian Gaeke70975ee2003-09-05 18:42:01 +0000110 // Call the function.
111 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
112 (const char **) GVTOP (ArgValues[2]));
Chris Lattner22080f92003-05-14 13:53:40 +0000113
114 // Run any atexit handlers now!
115 runAtExitHandlers();
Brian Gaeke70975ee2003-09-05 18:42:01 +0000116
117 GenericValue rv;
118 rv.IntVal = ExitCode;
119 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000120}