aboutsummaryrefslogtreecommitdiffstats
path: root/tools/jello/VM.h
blob: 64451014c1690a571ee4238f2990becb4f9c4a8d (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
//===-- VM.h - Definitions for Virtual Machine ------------------*- C++ -*-===//
//
// This file defines the top level Virtual Machine data structure.
//
//===----------------------------------------------------------------------===//

#ifndef VM_H
#define VM_H

#include "llvm/PassManager.h"
#include <string>
#include <map>
#include <vector>

class Function;
class GlobalValue;
class Constant;
class TargetMachine;
class MachineCodeEmitter;

class VM {
  std::string ExeName;
  Module &M;               // The LLVM program we are running
  TargetMachine &TM;       // The current target we are compiling to
  PassManager PM;          // Passes to compile a function
  MachineCodeEmitter *MCE; // MCE object

  // GlobalAddress - A mapping between LLVM values and their native code
  // generated versions...
  std::map<const GlobalValue*, void *> GlobalAddress;

  // FunctionRefs - A mapping between addresses that refer to unresolved
  // functions and the LLVM function object itself.  This is used by the fault
  // handler to lazily patch up references...
  //
  std::map<void*, Function*> FunctionRefs;
public:
  VM(const std::string &name, Module &m, TargetMachine &tm)
    : ExeName(name), M(m), TM(tm) {
    MCE = createEmitter(*this);  // Initialize MCE
    setupPassManager();
    registerCallback();
    emitGlobals();
  }

  ~VM();

  int run(Function *F);

  void addGlobalMapping(const Function *F, void *Addr) {
    void *&CurVal = GlobalAddress[(const GlobalValue*)F];
    assert(CurVal == 0 && "GlobalMapping already established!");
    CurVal = Addr;
  }

  void addFunctionRef(void *Ref, Function *F) {
    FunctionRefs[Ref] = F;
  }

  const std::string &getFunctionReferencedName(void *RefAddr);

  void *resolveFunctionReference(void *RefAddr);

private:
  static MachineCodeEmitter *createEmitter(VM &V);
  void setupPassManager();
  void *getPointerToFunction(Function *F);
  void registerCallback();
  void emitGlobals();
  void emitConstantToMemory(Constant *Init, void *Addr);
};

#endif