379be77ac84b309b1ca229eae042bff32e78114a
[oota-llvm.git] / lib / ExecutionEngine / JIT / VM.cpp
1 //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
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"
16 #include "llvm/Function.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/CodeGen/MachineCodeEmitter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/Target/TargetMachine.h"
21
22 namespace llvm {
23
24 VM::~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 ///
32 void 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   if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
43     std::cerr << "lli: target '" << TM.getName()
44               << "' doesn't support machine code emission!\n";
45     abort();
46   }
47 }
48
49 /// runJITOnFunction - Run the FunctionPassManager full of
50 /// just-in-time compilation passes on F, hopefully filling in
51 /// GlobalAddress[F] with the address of F's machine code.
52 ///
53 void VM::runJITOnFunction(Function *F) {
54   static bool isAlreadyCodeGenerating = false;
55   assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
56
57   // JIT the function
58   isAlreadyCodeGenerating = true;
59   PM.run(*F);
60   isAlreadyCodeGenerating = false;
61 }
62
63 /// getPointerToFunction - This method is used to get the address of the
64 /// specified function, compiling it if neccesary.
65 ///
66 void *VM::getPointerToFunction(Function *F) {
67   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
68   if (Addr) return Addr;
69
70   // Make sure we read in the function if it exists in this Module
71   MP->materializeFunction(F);
72
73   if (F->isExternal())
74     return Addr = getPointerToNamedFunction(F->getName());
75
76   runJITOnFunction(F);
77   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
78   return Addr;
79 }
80
81 /// recompileAndRelinkFunction - This method is used to force a function
82 /// which has already been compiled, to be compiled again, possibly
83 /// after it has been modified. Then the entry to the old copy is overwritten
84 /// with a branch to the new copy. If there was no old copy, this acts
85 /// just like VM::getPointerToFunction().
86 ///
87 void *VM::recompileAndRelinkFunction(Function *F) {
88   void *&Addr = GlobalAddress[F];   // Check if function already code gen'd
89
90   // If it's not already compiled (this is kind of weird) there is no
91   // reason to patch it up.
92   if (!Addr) { return getPointerToFunction (F); }
93
94   void *OldAddr = Addr;
95   Addr = 0;
96   MachineFunction::destruct(F);
97   runJITOnFunction(F);
98   assert(Addr && "Code generation didn't add function to GlobalAddress table!");
99   TM.replaceMachineCodeForFunction(OldAddr, Addr);
100   return Addr;
101 }
102
103 } // End llvm namespace