1 //===-- JIT.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/MachineCodeEmitter.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/ExecutionEngine/GenericValue.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetJITInfo.h"
25 JIT::JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji)
26 : ExecutionEngine(MP), TM(tm), TJI(tji), PM(MP) {
27 setTargetData(TM.getTargetData());
30 MCE = createEmitter(*this);
32 // Compile LLVM Code down to machine code in the intermediate representation
33 TJI.addPassesToJITCompile(PM);
35 // Turn the machine code intermediate representation into bytes in memory that
37 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
38 std::cerr << "lli: target '" << TM.getName()
39 << "' doesn't support machine code emission!\n";
51 /// run - Start execution with the specified function and arguments.
53 GenericValue JIT::run(Function *F, const std::vector<GenericValue> &ArgValues) {
54 assert (F && "Function *F was null at entry to run()");
56 int (*PF)(int, char **, const char **) =
57 (int(*)(int, char **, const char **))getPointerToFunction(F);
58 assert(PF != 0 && "Pointer to fn's code was null after getPointerToFunction");
61 int ExitCode = PF(ArgValues[0].IntVal, (char **) GVTOP (ArgValues[1]),
62 (const char **) GVTOP (ArgValues[2]));
64 // Run any atexit handlers now!
72 /// runJITOnFunction - Run the FunctionPassManager full of
73 /// just-in-time compilation passes on F, hopefully filling in
74 /// GlobalAddress[F] with the address of F's machine code.
76 void JIT::runJITOnFunction(Function *F) {
77 static bool isAlreadyCodeGenerating = false;
78 assert(!isAlreadyCodeGenerating && "Error: Recursive compilation detected!");
81 isAlreadyCodeGenerating = true;
83 isAlreadyCodeGenerating = false;
86 /// getPointerToFunction - This method is used to get the address of the
87 /// specified function, compiling it if neccesary.
89 void *JIT::getPointerToFunction(Function *F) {
90 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
91 if (Addr) return Addr;
93 // Make sure we read in the function if it exists in this Module
94 MP->materializeFunction(F);
97 return Addr = getPointerToNamedFunction(F->getName());
100 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
104 // getPointerToFunctionOrStub - If the specified function has been
105 // code-gen'd, return a pointer to the function. If not, compile it, or use
106 // a stub to implement lazy compilation if available.
108 void *JIT::getPointerToFunctionOrStub(Function *F) {
109 // If we have already code generated the function, just return the address.
110 std::map<const GlobalValue*, void *>::iterator I = GlobalAddress.find(F);
111 if (I != GlobalAddress.end()) return I->second;
113 // If the target supports "stubs" for functions, get a stub now.
114 if (void *Ptr = TJI.getJITStubForFunction(F, *MCE))
117 // Otherwise, if the target doesn't support it, just codegen the function.
118 return getPointerToFunction(F);
121 /// recompileAndRelinkFunction - This method is used to force a function
122 /// which has already been compiled, to be compiled again, possibly
123 /// after it has been modified. Then the entry to the old copy is overwritten
124 /// with a branch to the new copy. If there was no old copy, this acts
125 /// just like JIT::getPointerToFunction().
127 void *JIT::recompileAndRelinkFunction(Function *F) {
128 void *&Addr = GlobalAddress[F]; // Check if function already code gen'd
130 // If it's not already compiled (this is kind of weird) there is no
131 // reason to patch it up.
132 if (!Addr) { return getPointerToFunction (F); }
134 void *OldAddr = Addr;
136 MachineFunction::destruct(F);
138 assert(Addr && "Code generation didn't add function to GlobalAddress table!");
139 TJI.replaceMachineCodeForFunction(OldAddr, Addr);