aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ExecutionEngine/JIT/VM.cpp
blob: 6fd366ede518228840c12e963bb41cfbb942dbf6 (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
92
93
94
95
96
97
98
99
100
101
//===-- jello.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"
#include <dlfcn.h>    // dlsym access


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();
  }
}

void *VM::resolveFunctionReference(void *RefAddr) {
  Function *F = FunctionRefs[RefAddr];
  assert(F && "Reference address not known!");

  void *Addr = getPointerToFunction(F);
  assert(Addr && "Pointer to function unknown!");

  FunctionRefs.erase(RefAddr);
  return Addr;
}

const std::string &VM::getFunctionReferencedName(void *RefAddr) {
  return FunctionRefs[RefAddr]->getName();
}

static void NoopFn() {}

/// getPointerToNamedFunction - This method returns the address of the specified
/// function by using the dlsym function call.  As such it is only useful for
/// resolving library symbols, not code generated symbols.
///
void *VM::getPointerToNamedFunction(const std::string &Name) {
  // If it's an external function, look it up in the process image...
  void *Ptr = dlsym(0, Name.c_str());
  if (Ptr == 0) {
    std::cerr << "WARNING: Cannot resolve fn '" << Name
	      << "' using a dummy noop function instead!\n";
    Ptr = (void*)NoopFn;
  }
  
  return Ptr;
}

/// getPointerToFunction - This method is used to get the address of the
/// specified function, compiling it if neccesary.
///
void *VM::getPointerToFunction(const 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;
  if (isAlreadyCodeGenerating) {
    // Generate a function stub instead of reentering...
    void *SAddr = emitStubForFunction(*F);
    assert(SAddr && "Target machine doesn't support function stub generation!");
    return SAddr;
  }

  // FIXME: JIT all of the functions in the module.  Eventually this will JIT
  // functions on demand.  This has the effect of populating all of the
  // non-external functions into the GlobalAddress table.
  isAlreadyCodeGenerating = true;
  PM.run(getModule());
  isAlreadyCodeGenerating = false;

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