1 //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This tool implements a just-in-time compiler for LLVM, allowing direct
11 // execution of LLVM bytecode in an efficient manner.
13 //===----------------------------------------------------------------------===//
16 #include "llvm/Function.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/Target/TargetMachine.h"
27 /// setupPassManager - Initialize the VM PassManager object with all of the
28 /// passes needed for the target to generate code.
30 void 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";
38 // Turn the machine code intermediate representation into bytes in memory that
41 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
42 std::cerr << "lli: target '" << TM.getName()
43 << "' doesn't support machine code emission!\n";
48 /// 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.
52 void VM::runJITOnFunction (Function *F) {
53 static bool isAlreadyCodeGenerating = false;
54 assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");
57 isAlreadyCodeGenerating = true;
59 isAlreadyCodeGenerating = false;
62 /// getPointerToFunction - This method is used to get the address of the
63 /// specified function, compiling it if neccesary.
65 void *VM::getPointerToFunction(Function *F) {
66 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
67 if (Addr) return Addr;
69 // Make sure we read in the function if it exists in this Module
70 MP->materializeFunction(F);
73 return Addr = getPointerToNamedFunction(F->getName());
76 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
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().
86 void *VM::recompileAndRelinkFunction(Function *F) {
87 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
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); }
95 MachineFunction::destruct (F);
97 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
98 TM.replaceMachineCodeForFunction (OldAddr, Addr);