1 //===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===//
3 // This tool implements a just-in-time compiler for LLVM, allowing direct
4 // execution of LLVM bytecode in an efficient manner.
6 //===----------------------------------------------------------------------===//
9 #include "llvm/Target/TargetMachine.h"
10 #include "llvm/CodeGen/MachineCodeEmitter.h"
11 #include "llvm/Function.h"
18 /// setupPassManager - Initialize the VM PassManager object with all of the
19 /// passes needed for the target to generate code.
21 void VM::setupPassManager() {
22 // Compile LLVM Code down to machine code in the intermediate representation
23 if (TM.addPassesToJITCompile(PM)) {
24 std::cerr << "lli: target '" << TM.getName()
25 << "' doesn't support JIT compilation!\n";
29 // Turn the machine code intermediate representation into bytes in memory that
32 if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
33 std::cerr << "lli: target '" << TM.getName()
34 << "' doesn't support machine code emission!\n";
39 /// getPointerToFunction - This method is used to get the address of the
40 /// specified function, compiling it if neccesary.
42 void *VM::getPointerToFunction(Function *F) {
43 void *&Addr = GlobalAddress[F]; // Function already code gen'd
44 if (Addr) return Addr;
47 return Addr = getPointerToNamedFunction(F->getName());
49 static bool isAlreadyCodeGenerating = false;
50 assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");
53 isAlreadyCodeGenerating = true;
55 isAlreadyCodeGenerating = false;
57 assert(Addr && "Code generation didn't add function to GlobalAddress table!");