blob: dc9f7a14ffe32a4b6e780b40453e47768c755a76 [file] [log] [blame]
Chris Lattnerc19aade2003-12-08 08:06:28 +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"
Chris Lattnerc19aade2003-12-08 08:06:28 +000022using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000023
Misha Brukman82742912003-07-02 17:53:19 +000024#if !defined(ENABLE_X86_JIT) && !defined(ENABLE_SPARC_JIT)
25#define NO_JITS_ENABLED
26#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000027
Misha Brukman82742912003-07-02 17:53:19 +000028namespace {
29 enum ArchName { x86, Sparc };
30
31#ifndef NO_JITS_ENABLED
Chris Lattner97ac14f2003-06-17 15:43:13 +000032 cl::opt<ArchName>
33 Arch("march", cl::desc("Architecture to JIT to:"), cl::Prefix,
Misha Brukman82742912003-07-02 17:53:19 +000034 cl::values(
35#ifdef ENABLE_X86_JIT
36 clEnumVal(x86, " IA-32 (Pentium and above)"),
37#endif
38#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000039 clEnumValN(Sparc, "sparc", " Sparc-V9"),
Misha Brukmanabb027c2003-05-27 21:40:39 +000040#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000041 0),
Misha Brukman82742912003-07-02 17:53:19 +000042#if defined(ENABLE_X86_JIT)
Chris Lattner97ac14f2003-06-17 15:43:13 +000043 cl::init(x86)
Misha Brukman82742912003-07-02 17:53:19 +000044#elif defined(ENABLE_SPARC_JIT)
Chris Lattnerde3209b2003-06-17 15:54:02 +000045 cl::init(Sparc)
Chris Lattner97ac14f2003-06-17 15:43:13 +000046#endif
47 );
Misha Brukman82742912003-07-02 17:53:19 +000048#endif /* NO_JITS_ENABLED */
Misha Brukmanabb027c2003-05-27 21:40:39 +000049}
Chris Lattnerbd199fb2002-12-24 00:01:05 +000050
Brian Gaeke82d82772003-09-03 20:34:19 +000051/// create - Create an return a new JIT compiler if there is one available
52/// for the current target. Otherwise, return null.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000053///
Misha Brukman005e5e92003-10-14 21:37:41 +000054ExecutionEngine *VM::create(ModuleProvider *MP) {
Chris Lattner39c07262003-08-24 19:50:53 +000055 TargetMachine* (*TargetMachineAllocator)(const Module &) = 0;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000056
Misha Brukmanabb027c2003-05-27 21:40:39 +000057 // 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 --
Misha Brukman82742912003-07-02 17:53:19 +000059 // our X86 machines are much faster at recompiling LLVM and linking LLI.
Brian Gaeke11911ab2003-10-29 04:24:09 +000060#ifndef NO_JITS_ENABLED
Misha Brukman82742912003-07-02 17:53:19 +000061
Chris Lattner97ac14f2003-06-17 15:43:13 +000062 switch (Arch) {
Misha Brukman82742912003-07-02 17:53:19 +000063#ifdef ENABLE_X86_JIT
Chris Lattner97ac14f2003-06-17 15:43:13 +000064 case x86:
Misha Brukmanabb027c2003-05-27 21:40:39 +000065 TargetMachineAllocator = allocateX86TargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000066 break;
Misha Brukman82742912003-07-02 17:53:19 +000067#endif
68#ifdef ENABLE_SPARC_JIT
Chris Lattnerde3209b2003-06-17 15:54:02 +000069 case Sparc:
Misha Brukman906f5fa2003-06-02 03:23:16 +000070 TargetMachineAllocator = allocateSparcTargetMachine;
Chris Lattner97ac14f2003-06-17 15:43:13 +000071 break;
Chris Lattner7aefa962003-06-17 15:32:38 +000072#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000073 default:
74 assert(0 && "-march flag not supported on this host!");
Misha Brukmanabb027c2003-05-27 21:40:39 +000075 }
Brian Gaeke11911ab2003-10-29 04:24:09 +000076#else
77 return 0;
78#endif
Chris Lattner97ac14f2003-06-17 15:43:13 +000079
80 // Allocate a target...
Misha Brukman0f4f7d92003-10-16 21:19:34 +000081 TargetMachine *Target = TargetMachineAllocator(*MP->getModule());
Chris Lattner97ac14f2003-06-17 15:43:13 +000082 assert(Target && "Could not allocate target machine!");
Chris Lattner1e60a912003-12-20 01:22:19 +000083
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;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000088}
89
Chris Lattner1e60a912003-12-20 01:22:19 +000090VM::VM(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
91 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000092 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000093
94 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000095 MCE = createEmitter(*this);
Chris Lattner1e60a912003-12-20 01:22:19 +000096
Chris Lattnerbd199fb2002-12-24 00:01:05 +000097 setupPassManager();
Misha Brukman4e8c9992003-06-06 06:59:55 +000098
Chris Lattner56adf152003-05-12 02:14:34 +000099 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000100}
101
Brian Gaeke70975ee2003-09-05 18:42:01 +0000102/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +0000103///
Brian Gaeke70975ee2003-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 Lattnerbd199fb2002-12-24 00:01:05 +0000107
Brian Gaeke70975ee2003-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 Lattnerbd199fb2002-12-24 00:01:05 +0000111
Brian Gaeke70975ee2003-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 Lattner22080f92003-05-14 13:53:40 +0000115
116 // Run any atexit handlers now!
117 runAtExitHandlers();
Brian Gaeke70975ee2003-09-05 18:42:01 +0000118
119 GenericValue rv;
120 rv.IntVal = ExitCode;
121 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000122}