blob: 2dda271fa0ad4018dd9c1063abba428eabda2a7b [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
22VM::~VM() {
23 delete MCE;
24 delete &TM;
25}
26
27/// setupPassManager - Initialize the VM PassManager object with all of the
28/// passes needed for the target to generate code.
29///
30void VM::setupPassManager() {
31 // Compile LLVM Code down to machine code in the intermediate representation
32 if (TM.addPassesToJITCompile(PM)) {
33 std::cerr << "lli: target '" << TM.getName()
34 << "' doesn't support JIT compilation!\n";
35 abort();
36 }
37
38 // Turn the machine code intermediate representation into bytes in memory that
39 // may be executed.
40 //
41 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
42 std::cerr << "lli: target '" << TM.getName()
43 << "' doesn't support machine code emission!\n";
44 abort();
45 }
46}
47
Brian Gaekecd64ddf2003-10-17 18:27:00 +000048/// runJITOnFunction - Run the FunctionPassManager full of
49/// just-in-time compilation passes on F, hopefully filling in
50/// GlobalAddress[F] with the address of F's machine code.
51///
52void VM::runJITOnFunction (Function *F) {
53 static bool isAlreadyCodeGenerating = false;
54 assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");
55
56 // JIT the function
57 isAlreadyCodeGenerating = true;
58 PM.run(*F);
59 isAlreadyCodeGenerating = false;
60}
61
Chris Lattnerbd199fb2002-12-24 00:01:05 +000062/// getPointerToFunction - This method is used to get the address of the
63/// specified function, compiling it if neccesary.
64///
Brian Gaeke71d84782003-08-13 18:16:34 +000065void *VM::getPointerToFunction(Function *F) {
Brian Gaekecd64ddf2003-10-17 18:27:00 +000066 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
Chris Lattnerbd199fb2002-12-24 00:01:05 +000067 if (Addr) return Addr;
68
Misha Brukman005e5e92003-10-14 21:37:41 +000069 // Make sure we read in the function if it exists in this Module
70 MP->materializeFunction(F);
71
Chris Lattner0d448c02003-01-13 01:00:48 +000072 if (F->isExternal())
73 return Addr = getPointerToNamedFunction(F->getName());
Chris Lattnerbd199fb2002-12-24 00:01:05 +000074
Brian Gaekecd64ddf2003-10-17 18:27:00 +000075 runJITOnFunction (F);
Chris Lattnerbd199fb2002-12-24 00:01:05 +000076 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
77 return Addr;
78}
Brian Gaekecd64ddf2003-10-17 18:27:00 +000079
80/// recompileAndRelinkFunction - This method is used to force a function
81/// which has already been compiled, to be compiled again, possibly
82/// after it has been modified. Then the entry to the old copy is overwritten
83/// with a branch to the new copy. If there was no old copy, this acts
84/// just like VM::getPointerToFunction().
85///
86void *VM::recompileAndRelinkFunction(Function *F) {
87 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
88
89 // If it's not already compiled (this is kind of weird) there is no
90 // reason to patch it up.
91 if (!Addr) { return getPointerToFunction (F); }
92
93 void *OldAddr = Addr;
94 Addr = 0;
Brian Gaeke242978c2003-10-24 03:55:37 +000095 MachineFunction::destruct (F);
Brian Gaekecd64ddf2003-10-17 18:27:00 +000096 runJITOnFunction (F);
97 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
98 TM.replaceMachineCodeForFunction (OldAddr, Addr);
99 return Addr;
100}