mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-01 00:11:00 +00:00
71d8478117
Run passes on single function (hey, just-in-time compilation!) instead of the entire module that contains it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7819 91177308-0d34-0410-b5e6-96231b3b80d8
60 lines
1.8 KiB
C++
60 lines
1.8 KiB
C++
//===-- VM.cpp - LLVM Just in Time Compiler -------------------------------===//
|
|
//
|
|
// This tool implements a just-in-time compiler for LLVM, allowing direct
|
|
// execution of LLVM bytecode in an efficient manner.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "VM.h"
|
|
#include "llvm/Target/TargetMachine.h"
|
|
#include "llvm/CodeGen/MachineCodeEmitter.h"
|
|
#include "llvm/Function.h"
|
|
|
|
VM::~VM() {
|
|
delete MCE;
|
|
delete &TM;
|
|
}
|
|
|
|
/// setupPassManager - Initialize the VM PassManager object with all of the
|
|
/// passes needed for the target to generate code.
|
|
///
|
|
void VM::setupPassManager() {
|
|
// Compile LLVM Code down to machine code in the intermediate representation
|
|
if (TM.addPassesToJITCompile(PM)) {
|
|
std::cerr << "lli: target '" << TM.getName()
|
|
<< "' doesn't support JIT compilation!\n";
|
|
abort();
|
|
}
|
|
|
|
// Turn the machine code intermediate representation into bytes in memory that
|
|
// may be executed.
|
|
//
|
|
if (TM.addPassesToEmitMachineCode(PM, *MCE)) {
|
|
std::cerr << "lli: target '" << TM.getName()
|
|
<< "' doesn't support machine code emission!\n";
|
|
abort();
|
|
}
|
|
}
|
|
|
|
/// getPointerToFunction - This method is used to get the address of the
|
|
/// specified function, compiling it if neccesary.
|
|
///
|
|
void *VM::getPointerToFunction(Function *F) {
|
|
void *&Addr = GlobalAddress[F]; // Function already code gen'd
|
|
if (Addr) return Addr;
|
|
|
|
if (F->isExternal())
|
|
return Addr = getPointerToNamedFunction(F->getName());
|
|
|
|
static bool isAlreadyCodeGenerating = false;
|
|
assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");
|
|
|
|
// JIT the function
|
|
isAlreadyCodeGenerating = true;
|
|
PM.run(*F);
|
|
isAlreadyCodeGenerating = false;
|
|
|
|
assert(Addr && "Code generation didn't add function to GlobalAddress table!");
|
|
return Addr;
|
|
}
|