blob: 5dffa5c9136e99aa2c70b72e84bae9fdac018989 [file] [log] [blame]
Chris Lattner6701a862003-05-14 13:26:47 +00001//===-- VM.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 tool implements a just-in-time compiler for LLVM, allowing direct
11// execution of LLVM bytecode in an efficient manner.
12//
13//===----------------------------------------------------------------------===//
14
15#include "VM.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000016#include "llvm/Function.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000017#include "llvm/ModuleProvider.h"
18#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukman0ca042d2003-11-17 20:37:02 +000019#include "llvm/CodeGen/MachineFunction.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000020#include "llvm/Target/TargetMachine.h"
Chris Lattner1e60a912003-12-20 01:22:19 +000021#include "llvm/Target/TargetJITInfo.h"
Chris Lattnerc19aade2003-12-08 08:06:28 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattnerbd199fb2002-12-24 00:01:05 +000024VM::~VM() {
25 delete MCE;
26 delete &TM;
27}
28
29/// setupPassManager - Initialize the VM PassManager object with all of the
30/// passes needed for the target to generate code.
31///
32void VM::setupPassManager() {
33 // Compile LLVM Code down to machine code in the intermediate representation
Chris Lattner1e60a912003-12-20 01:22:19 +000034 TJI.addPassesToJITCompile(PM);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000035
36 // Turn the machine code intermediate representation into bytes in memory that
37 // may be executed.
Chris Lattnerbd199fb2002-12-24 00:01:05 +000038 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
39 std::cerr << "lli: target '" << TM.getName()
40 << "' doesn't support machine code emission!\n";
41 abort();
42 }
43}
44
Brian Gaekecd64ddf2003-10-17 18:27:00 +000045/// runJITOnFunction - Run the FunctionPassManager full of
46/// just-in-time compilation passes on F, hopefully filling in
47/// GlobalAddress[F] with the address of F's machine code.
48///
Misha Brukman0ca042d2003-11-17 20:37:02 +000049void VM::runJITOnFunction(Function *F) {
Brian Gaekecd64ddf2003-10-17 18:27:00 +000050 static bool isAlreadyCodeGenerating = false;
Misha Brukman0ca042d2003-11-17 20:37:02 +000051 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
Brian Gaekecd64ddf2003-10-17 18:27:00 +000052
53 // JIT the function
54 isAlreadyCodeGenerating = true;
55 PM.run(*F);
56 isAlreadyCodeGenerating = false;
57}
58
Chris Lattnerbd199fb2002-12-24 00:01:05 +000059/// getPointerToFunction - This method is used to get the address of the
60/// specified function, compiling it if neccesary.
61///
Brian Gaeke71d84782003-08-13 18:16:34 +000062void *VM::getPointerToFunction(Function *F) {
Brian Gaekecd64ddf2003-10-17 18:27:00 +000063 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
Chris Lattnerbd199fb2002-12-24 00:01:05 +000064 if (Addr) return Addr;
65
Misha Brukman005e5e92003-10-14 21:37:41 +000066 // Make sure we read in the function if it exists in this Module
67 MP->materializeFunction(F);
68
Chris Lattner0d448c02003-01-13 01:00:48 +000069 if (F->isExternal())
70 return Addr = getPointerToNamedFunction(F->getName());
Chris Lattnerbd199fb2002-12-24 00:01:05 +000071
Misha Brukman0ca042d2003-11-17 20:37:02 +000072 runJITOnFunction(F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000073 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
74 return Addr;
75}
Brian Gaekecd64ddf2003-10-17 18:27:00 +000076
Chris Lattner993bdce2003-12-12 07:12:02 +000077// getPointerToFunctionOrStub - If the specified function has been
78// code-gen'd, return a pointer to the function. If not, compile it, or use
79// a stub to implement lazy compilation if available.
80//
81void *VM::getPointerToFunctionOrStub(Function *F) {
82 // If we have already code generated the function, just return the address.
83 std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
84 if (I != GlobalAddress.end()) return I->second;
85
86 // If the target supports "stubs" for functions, get a stub now.
Chris Lattner1e60a912003-12-20 01:22:19 +000087 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
Chris Lattner993bdce2003-12-12 07:12:02 +000088 return Ptr;
89
90 // Otherwise, if the target doesn't support it, just codegen the function.
91 return getPointerToFunction(F);
92}
93
Brian Gaekecd64ddf2003-10-17 18:27:00 +000094/// recompileAndRelinkFunction - This method is used to force a function
95/// which has already been compiled, to be compiled again, possibly
96/// after it has been modified. Then the entry to the old copy is overwritten
97/// with a branch to the new copy. If there was no old copy, this acts
98/// just like VM::getPointerToFunction().
99///
100void *VM::recompileAndRelinkFunction(Function *F) {
101 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
102
103 // If it's not already compiled (this is kind of weird) there is no
104 // reason to patch it up.
105 if (!Addr) { return getPointerToFunction (F); }
106
107 void *OldAddr = Addr;
108 Addr = 0;
Misha Brukman0ca042d2003-11-17 20:37:02 +0000109 MachineFunction::destruct(F);
110 runJITOnFunction(F);
Brian Gaekecd64ddf2003-10-17 18:27:00 +0000111 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
Chris Lattner1e60a912003-12-20 01:22:19 +0000112 TJI.replaceMachineCodeForFunction(OldAddr, Addr);
Brian Gaekecd64ddf2003-10-17 18:27:00 +0000113 return Addr;
114}