blob: 09ac4262c02a039e44d952796c86a194ec929ee7 [file] [log] [blame]
Chris Lattner4d326fa2003-12-20 01:46:27 +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//
Chris Lattner4d326fa2003-12-20 01:46:27 +000010// This tool implements a just-in-time compiler for LLVM, allowing direct
11// execution of LLVM bytecode in an efficient manner.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner4d326fa2003-12-20 01:46:27 +000015#include "JIT.h"
16#include "llvm/Function.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000017#include "llvm/ModuleProvider.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000018#include "llvm/CodeGen/MachineCodeEmitter.h"
19#include "llvm/CodeGen/MachineFunction.h"
Brian Gaeke97222942003-09-05 19:39:22 +000020#include "llvm/ExecutionEngine/GenericValue.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattner4d326fa2003-12-20 01:46:27 +000022#include "llvm/Target/TargetJITInfo.h"
Chris Lattnerc19aade2003-12-08 08:06:28 +000023using namespace llvm;
Misha Brukmanabb027c2003-05-27 21:40:39 +000024
Chris Lattner4d326fa2003-12-20 01:46:27 +000025JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
Chris Lattner1e60a912003-12-20 01:22:19 +000026 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +000027 setTargetData(TM.getTargetData());
Misha Brukmanabb027c2003-05-27 21:40:39 +000028
29 // Initialize MCE
Misha Brukman906f5fa2003-06-02 03:23:16 +000030 MCE = createEmitter(*this);
Chris Lattner1e60a912003-12-20 01:22:19 +000031
Chris Lattner4d326fa2003-12-20 01:46:27 +000032 // Compile LLVM Code down to machine code in the intermediate representation
33 TJI.addPassesToJITCompile(PM);
34
35 // Turn the machine code intermediate representation into bytes in memory that
36 // may be executed.
37 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
38 std::cerr << "lli: target '" << TM.getName()
39 << "' doesn't support machine code emission!\n";
40 abort();
41 }
Misha Brukman4e8c9992003-06-06 06:59:55 +000042
Chris Lattner56adf152003-05-12 02:14:34 +000043 emitGlobals();
Chris Lattnerbd199fb2002-12-24 00:01:05 +000044}
45
Chris Lattner4d326fa2003-12-20 01:46:27 +000046JIT::~JIT() {
47 delete MCE;
48 delete &TM;
49}
50
Brian Gaeke70975ee2003-09-05 18:42:01 +000051/// run - Start execution with the specified function and arguments.
Chris Lattner05a1a302003-08-21 21:32:12 +000052///
Chris Lattner4d326fa2003-12-20 01:46:27 +000053GenericValue JIT::run(Function *F, const std::vector<GenericValue> &ArgValues) {
Brian Gaeke70975ee2003-09-05 18:42:01 +000054 assert (F && "Function *F was null at entry to run()");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000055
Brian Gaeke70975ee2003-09-05 18:42:01 +000056 int (*PF)(int, char **, const char **) =
57 (int(*)(int, char **, const char **))getPointerToFunction(F);
58 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000059
Brian Gaeke70975ee2003-09-05 18:42:01 +000060 // Call the function.
61 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
62 (const char **) GVTOP (ArgValues[2]));
Chris Lattner22080f92003-05-14 13:53:40 +000063
64 // Run any atexit handlers now!
65 runAtExitHandlers();
Brian Gaeke70975ee2003-09-05 18:42:01 +000066
67 GenericValue rv;
68 rv.IntVal = ExitCode;
69 return rv;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000070}
Chris Lattner4d326fa2003-12-20 01:46:27 +000071
72/// runJITOnFunction - Run the FunctionPassManager full of
73/// just-in-time compilation passes on F, hopefully filling in
74/// GlobalAddress[F] with the address of F's machine code.
75///
76void JIT::runJITOnFunction(Function *F) {
77 static bool isAlreadyCodeGenerating = false;
78 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
79
80 // JIT the function
81 isAlreadyCodeGenerating = true;
82 PM.run(*F);
83 isAlreadyCodeGenerating = false;
84}
85
86/// getPointerToFunction - This method is used to get the address of the
87/// specified function, compiling it if neccesary.
88///
89void *JIT::getPointerToFunction(Function *F) {
90 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
91 if (Addr) return Addr;
92
93 // Make sure we read in the function if it exists in this Module
94 MP->materializeFunction(F);
95
96 if (F->isExternal())
97 return Addr = getPointerToNamedFunction(F->getName());
98
99 runJITOnFunction(F);
100 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
101 return Addr;
102}
103
104// getPointerToFunctionOrStub - If the specified function has been
105// code-gen'd, return a pointer to the function. If not, compile it, or use
106// a stub to implement lazy compilation if available.
107//
108void *JIT::getPointerToFunctionOrStub(Function *F) {
109 // If we have already code generated the function, just return the address.
110 std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
111 if (I != GlobalAddress.end()) return I->second;
112
113 // If the target supports "stubs" for functions, get a stub now.
114 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
115 return Ptr;
116
117 // Otherwise, if the target doesn't support it, just codegen the function.
118 return getPointerToFunction(F);
119}
120
121/// recompileAndRelinkFunction - This method is used to force a function
122/// which has already been compiled, to be compiled again, possibly
123/// after it has been modified. Then the entry to the old copy is overwritten
124/// with a branch to the new copy. If there was no old copy, this acts
125/// just like JIT::getPointerToFunction().
126///
127void *JIT::recompileAndRelinkFunction(Function *F) {
128 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
129
130 // If it's not already compiled (this is kind of weird) there is no
131 // reason to patch it up.
132 if (!Addr) { return getPointerToFunction (F); }
133
134 void *OldAddr = Addr;
135 Addr = 0;
136 MachineFunction::destruct(F);
137 runJITOnFunction(F);
138 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
139 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
140 return Addr;
141}