blob: 61d9629d93e0a7d782af5afd1754d57d990750dc [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
Brian Gaeke960707c2003-11-11 22:41:34 +000027namespace llvm {
28
Misha Brukman7dee4432003-07-02 17:53:19 +000029namespace {
30 enum ArchName { x86, Sparc };
31
32#ifndef NO_JITS_ENABLED
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000033 cl::opt<ArchName>
34 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
Misha Brukman7dee4432003-07-02 17:53:19 +000035 cl::values(
36#ifdef ENABLE_X86_JIT
37 clEnumVal(x86, " IA-32 (Pentium and above)"),
38#endif
39#ifdef ENABLE_SPARC_JIT
Chris Lattnercf481502003-06-17 15:54:02 +000040 clEnumValN(Sparc, "sparc", " Sparc-V9"),
Misha Brukman56d27322003-05-27 21:40:39 +000041#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000042 0),
Misha Brukman7dee4432003-07-02 17:53:19 +000043#if defined(ENABLE_X86_JIT)
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000044 cl::init(x86)
Misha Brukman7dee4432003-07-02 17:53:19 +000045#elif defined(ENABLE_SPARC_JIT)
Chris Lattnercf481502003-06-17 15:54:02 +000046 cl::init(Sparc)
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000047#endif
48 );
Misha Brukman7dee4432003-07-02 17:53:19 +000049#endif /* NO_JITS_ENABLED */
Misha Brukman56d27322003-05-27 21:40:39 +000050}
Chris Lattner996fe012002-12-24 00:01:05 +000051
Brian Gaeke4bd3bd52003-09-03 20:34:19 +000052/// create - Create an return a new JIT compiler if there is one available
53/// for the current target. Otherwise, return null.
Chris Lattner996fe012002-12-24 00:01:05 +000054///
Misha Brukman299f3e62003-10-14 21:37:41 +000055ExecutionEngine *VM::create(ModuleProvider *MP) {
Chris Lattner4d4f4242003-08-24 19:50:53 +000056 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
Chris Lattner996fe012002-12-24 00:01:05 +000057
Misha Brukman56d27322003-05-27 21:40:39 +000058 // Allow a command-line switch to override what *should* be the default target
59 // machine for this platform. This allows for debugging a Sparc JIT on X86 --
Misha Brukman7dee4432003-07-02 17:53:19 +000060 // our X86 machines are much faster at recompiling LLVM and linking LLI.
Brian Gaeke9d202002003-10-29 04:24:09 +000061#ifndef NO_JITS_ENABLED
Misha Brukman7dee4432003-07-02 17:53:19 +000062
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000063 switch (Arch) {
Misha Brukman7dee4432003-07-02 17:53:19 +000064#ifdef ENABLE_X86_JIT
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000065 case x86:
Misha Brukman56d27322003-05-27 21:40:39 +000066 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000067 break;
Misha Brukman7dee4432003-07-02 17:53:19 +000068#endif
69#ifdef ENABLE_SPARC_JIT
Chris Lattnercf481502003-06-17 15:54:02 +000070 case Sparc:
Misha Brukman6ac7fe72003-06-02 03:23:16 +000071 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000072 break;
Chris Lattnera5741ac2003-06-17 15:32:38 +000073#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000074 default:
75 assert(0 && "-march flag not supported on this host!");
Misha Brukman56d27322003-05-27 21:40:39 +000076 }
Brian Gaeke9d202002003-10-29 04:24:09 +000077#else
78 return 0;
79#endif
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000080
81 // Allocate a target...
Misha Brukman12731472003-10-16 21:19:34 +000082 TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
Chris Lattnerd9e6bfa2003-06-17 15:43:13 +000083 assert(Target && "Could not allocate target machine!");
84
85 // Create the virtual machine object...
Misha Brukman299f3e62003-10-14 21:37:41 +000086 return new VM(MP, Target);
Chris Lattner996fe012002-12-24 00:01:05 +000087}
88
Misha Brukman299f3e62003-10-14 21:37:41 +000089VM::VM(ModuleProvider *MP, TargetMachine *tm) : ExecutionEngine(MP), TM(*tm),
90 PM(MP)
91{
Chris Lattner996fe012002-12-24 00:01:05 +000092 setTargetData(TM.getTargetData());
Misha Brukman56d27322003-05-27 21:40:39 +000093
94 // Initialize MCE
Misha Brukman6ac7fe72003-06-02 03:23:16 +000095 MCE = createEmitter(*this);
Misha Brukman56d27322003-05-27 21:40:39 +000096
Chris Lattner996fe012002-12-24 00:01:05 +000097 setupPassManager();
Misha Brukman0ccdecb2003-06-06 06:59:55 +000098
Chris Lattnerb78244f2003-05-12 02:14:34 +000099 emitGlobals();
Chris Lattner996fe012002-12-24 00:01:05 +0000100}
101
Brian Gaekea7669032003-09-05 18:42:01 +0000102/// run - Start execution with the specified function and arguments.
Chris Lattnerabca19d2003-08-21 21:32:12 +0000103///
Brian Gaekea7669032003-09-05 18:42:01 +0000104GenericValue VM::run(Function *F, const std::vector<GenericValue> &ArgValues)
105{
106 assert (F && "Function *F was null at entry to run()");
Chris Lattner996fe012002-12-24 00:01:05 +0000107
Brian Gaekea7669032003-09-05 18:42:01 +0000108 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");
Chris Lattner996fe012002-12-24 00:01:05 +0000111
Brian Gaekea7669032003-09-05 18:42:01 +0000112 // Call the function.
113 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
114 (const char **) GVTOP (ArgValues[2]));
Chris Lattner2537ca32003-05-14 13:53:40 +0000115
116 // Run any atexit handlers now!
117 runAtExitHandlers();
Brian Gaekea7669032003-09-05 18:42:01 +0000118
119 GenericValue rv;
120 rv.IntVal = ExitCode;
121 return rv;
Chris Lattner996fe012002-12-24 00:01:05 +0000122}
Brian Gaeke960707c2003-11-11 22:41:34 +0000123
124} // End llvm namespace