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