blob: fedb6e46b087b70351128fabef014a24b3273f12 [file] [log] [blame]
Chris Lattner996fe012002-12-24 00:01:05 +00001//===-- JIT.cpp - LLVM Just in Time Compiler ------------------------------===//
John Criswell482202a2003-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 Lattner996fe012002-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 Brukman12731472003-10-16 21:19:34 +000016#include "llvm/Module.h"
17#include "llvm/ModuleProvider.h"
Brian Gaekef3a300d2003-09-05 19:39:22 +000018#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattner996fe012002-12-24 00:01:05 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/Target/TargetMachineImpls.h"
Misha Brukman56d27322003-05-27 21:40:39 +000021#include "Support/CommandLine.h"
22
Misha Brukman7dee4432003-07-02 17:53:19 +000023#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
24#define NO_JITS_ENABLED
25#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000026
Misha Brukman7dee4432003-07-02 17:53:19 +000027namespace {
28 enum ArchName { x86, Sparc };
29
30#ifndef NO_JITS_ENABLED
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000031 cl::opt<ArchName>
32 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
Misha Brukman7dee4432003-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 Lattnercf481502003-06-17 15:54:02 +000038 clEnumValN(Sparc, "sparc", " Sparc-V9"),
Misha Brukman56d27322003-05-27 21:40:39 +000039#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000040 0),
Misha Brukman7dee4432003-07-02 17:53:19 +000041#if defined(ENABLE_X86_JIT)
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000042 cl::init(x86)
Misha Brukman7dee4432003-07-02 17:53:19 +000043#elif defined(ENABLE_SPARC_JIT)
Chris Lattnercf481502003-06-17 15:54:02 +000044 cl::init(Sparc)
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000045#endif
46 );
Misha Brukman7dee4432003-07-02 17:53:19 +000047#endif /* NO_JITS_ENABLED */
Misha Brukman56d27322003-05-27 21:40:39 +000048}
Chris Lattner996fe012002-12-24 00:01:05 +000049
Brian Gaeke4bd3bd52003-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 Lattner996fe012002-12-24 00:01:05 +000052///
Misha Brukman299f3e62003-10-14 21:37:41 +000053ExecutionEngine *VM::create(ModuleProvider *MP) {
Chris Lattner4d4f4242003-08-24 19:50:53 +000054 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
Chris Lattner996fe012002-12-24 00:01:05 +000055
Misha Brukman56d27322003-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 Brukman7dee4432003-07-02 17:53:19 +000058 // our X86 machines are much faster at recompiling LLVM and linking LLI.
Brian Gaeke9d202002003-10-29 04:24:09 +000059#ifndef NO_JITS_ENABLED
Misha Brukman7dee4432003-07-02 17:53:19 +000060
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000061 switch (Arch) {
Misha Brukman7dee4432003-07-02 17:53:19 +000062#ifdef ENABLE_X86_JIT
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000063 case x86:
Misha Brukman56d27322003-05-27 21:40:39 +000064 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000065 break;
Misha Brukman7dee4432003-07-02 17:53:19 +000066#endif
67#ifdef ENABLE_SPARC_JIT
Chris Lattnercf481502003-06-17 15:54:02 +000068 case Sparc:
Misha Brukman6ac7fe72003-06-02 03:23:16 +000069 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000070 break;
Chris Lattnera5741ac2003-06-17 15:32:38 +000071#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000072 default:
73 assert(0 && "-march flag not supported on this host!");
Misha Brukman56d27322003-05-27 21:40:39 +000074 }
Brian Gaeke9d202002003-10-29 04:24:09 +000075#else
76 return 0;
77#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000078
79 // Allocate a target...
Misha Brukman12731472003-10-16 21:19:34 +000080 TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000081 assert(Target && "Could not allocate target machine!");
82
83 // Create the virtual machine object...
Misha Brukman299f3e62003-10-14 21:37:41 +000084 return new VM(MP, Target);
Chris Lattner996fe012002-12-24 00:01:05 +000085}
86
Misha Brukman299f3e62003-10-14 21:37:41 +000087VM::VM(ModuleProvider *MP, TargetMachine *tm) : ExecutionEngine(MP), TM(*tm),
88 PM(MP)
89{
Chris Lattner996fe012002-12-24 00:01:05 +000090 setTargetData(TM.getTargetData());
Misha Brukman56d27322003-05-27 21:40:39 +000091
92 // Initialize MCE
Misha Brukman6ac7fe72003-06-02 03:23:16 +000093 MCE = createEmitter(*this);
Misha Brukman56d27322003-05-27 21:40:39 +000094
Chris Lattner996fe012002-12-24 00:01:05 +000095 setupPassManager();
Misha Brukman0ccdecb2003-06-06 06:59:55 +000096
Chris Lattnerb78244f2003-05-12 02:14:34 +000097 emitGlobals();
Chris Lattner996fe012002-12-24 00:01:05 +000098}
99
Brian Gaekea7669032003-09-05 18:42:01 +0000100/// run - Start execution with the specified function and arguments.
Chris Lattnerabca19d2003-08-21 21:32:12 +0000101///
Brian Gaekea7669032003-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 Lattner996fe012002-12-24 00:01:05 +0000105
Brian Gaekea7669032003-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 Lattner996fe012002-12-24 00:01:05 +0000109
Brian Gaekea7669032003-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 Lattner2537ca32003-05-14 13:53:40 +0000113
114 // Run any atexit handlers now!
115 runAtExitHandlers();
Brian Gaekea7669032003-09-05 18:42:01 +0000116
117 GenericValue rv;
118 rv.IntVal = ExitCode;
119 return rv;
Chris Lattner996fe012002-12-24 00:01:05 +0000120}