blob: d7e76859788912dcbfbc845956543b5b74c8bd37 [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"
Brian Gaeke242978c2003-10-24 03:55:37 +000018#include "llvm/CodeGen/MachineFunction.h"
Misha Brukman0f4f7d92003-10-16 21:19:34 +000019#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/Target/TargetMachine.h"
Chris Lattnerbd199fb2002-12-24 00:01:05 +000021
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
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
34 if (TM.addPassesToJITCompile(PM)) {
35 std::cerr << "lli: target '" << TM.getName()
36 << "' doesn't support JIT compilation!\n";
37 abort();
38 }
39
40 // Turn the machine code intermediate representation into bytes in memory that
41 // may be executed.
42 //
43 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
44 std::cerr << "lli: target '" << TM.getName()
45 << "' doesn't support machine code emission!\n";
46 abort();
47 }
48}
49
Brian Gaekecd64ddf2003-10-17 18:27:00 +000050/// runJITOnFunction - Run the FunctionPassManager full of
51/// just-in-time compilation passes on F, hopefully filling in
52/// GlobalAddress[F] with the address of F's machine code.
53///
54void VM::runJITOnFunction (Function *F) {
55 static bool isAlreadyCodeGenerating = false;
56 assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");
57
58 // JIT the function
59 isAlreadyCodeGenerating = true;
60 PM.run(*F);
61 isAlreadyCodeGenerating = false;
62}
63
Chris Lattnerbd199fb2002-12-24 00:01:05 +000064/// getPointerToFunction - This method is used to get the address of the
65/// specified function, compiling it if neccesary.
66///
Brian Gaeke71d84782003-08-13 18:16:34 +000067void *VM::getPointerToFunction(Function *F) {
Brian Gaekecd64ddf2003-10-17 18:27:00 +000068 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
Chris Lattnerbd199fb2002-12-24 00:01:05 +000069 if (Addr) return Addr;
70
Misha Brukman005e5e92003-10-14 21:37:41 +000071 // Make sure we read in the function if it exists in this Module
72 MP->materializeFunction(F);
73
Chris Lattner0d448c02003-01-13 01:00:48 +000074 if (F->isExternal())
75 return Addr = getPointerToNamedFunction(F->getName());
Chris Lattnerbd199fb2002-12-24 00:01:05 +000076
Brian Gaekecd64ddf2003-10-17 18:27:00 +000077 runJITOnFunction (F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000078 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
79 return Addr;
80}
Brian Gaekecd64ddf2003-10-17 18:27:00 +000081
82/// recompileAndRelinkFunction - This method is used to force a function
83/// which has already been compiled, to be compiled again, possibly
84/// after it has been modified. Then the entry to the old copy is overwritten
85/// with a branch to the new copy. If there was no old copy, this acts
86/// just like VM::getPointerToFunction().
87///
88void *VM::recompileAndRelinkFunction(Function *F) {
89 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
90
91 // If it's not already compiled (this is kind of weird) there is no
92 // reason to patch it up.
93 if (!Addr) { return getPointerToFunction (F); }
94
95 void *OldAddr = Addr;
96 Addr = 0;
Brian Gaeke242978c2003-10-24 03:55:37 +000097 MachineFunction::destruct (F);
Brian Gaekecd64ddf2003-10-17 18:27:00 +000098 runJITOnFunction (F);
99 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
100 TM.replaceMachineCodeForFunction (OldAddr, Addr);
101 return Addr;
102}
Brian Gaeked0fde302003-11-11 22:41:34 +0000103
104} // End llvm namespace