summaryrefslogtreecommitdiff
path: root/lib/ExecutionEngine/JIT/VM.cpp
blob: ba1ecdf31a04d22cb36766fec212076b6d576b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//===-- 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/Function.h"
#include "llvm/ModuleProvider.h"
#include "llvm/CodeGen/MachineCodeEmitter.h"
#include "llvm/Target/TargetMachine.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();
  }
}

/// runJITOnFunction - Run the FunctionPassManager full of
/// just-in-time compilation passes on F, hopefully filling in
/// GlobalAddress[F] with the address of F's machine code.
///
void VM::runJITOnFunction (Function *F) {
  static bool isAlreadyCodeGenerating = false;
  assert(!isAlreadyCodeGenerating && "ERROR: RECURSIVE COMPILATION DETECTED!");

  // JIT the function
  isAlreadyCodeGenerating = true;
  PM.run(*F);
  isAlreadyCodeGenerating = false;
}

/// 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];   // Check if function already code gen'd
  if (Addr) return Addr;

  // Make sure we read in the function if it exists in this Module
  MP->materializeFunction(F);

  if (F->isExternal())
    return Addr = getPointerToNamedFunction(F->getName());

  runJITOnFunction (F);
  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
  return Addr;
}

/// recompileAndRelinkFunction - This method is used to force a function
/// which has already been compiled, to be compiled again, possibly
/// after it has been modified. Then the entry to the old copy is overwritten
/// with a branch to the new copy. If there was no old copy, this acts
/// just like VM::getPointerToFunction().
///
void *VM::recompileAndRelinkFunction(Function *F) {
  void *&Addr = GlobalAddress[F];   // Check if function already code gen'd

  // If it's not already compiled (this is kind of weird) there is no
  // reason to patch it up.
  if (!Addr) { return getPointerToFunction (F); }

  void *OldAddr = Addr;
  Addr = 0;
  runJITOnFunction (F);
  assert(Addr && "Code generation didn't add function to GlobalAddress table!");
  TM.replaceMachineCodeForFunction (OldAddr, Addr);
  return Addr;
}