summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-exegesis/lib/InMemoryAssembler.h
blob: 51b555f545373e1363ea6fbac2aaa0c815afcdd6 (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
//===-- InMemoryAssembler.h -------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Defines classes to assemble functions composed of a single basic block of
/// MCInsts.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_TOOLS_LLVM_EXEGESIS_INMEMORYASSEMBLER_H
#define LLVM_TOOLS_LLVM_EXEGESIS_INMEMORYASSEMBLER_H

#include "llvm/ADT/BitVector.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/MC/MCInst.h"
#include <cstdint>
#include <memory>
#include <vector>

namespace exegesis {

// Consumable context for JitFunction below.
// This temporary object allows for retrieving MachineFunction properties before
// assembling it.
class JitFunctionContext {
public:
  explicit JitFunctionContext(std::unique_ptr<llvm::LLVMTargetMachine> TM);
  // Movable
  JitFunctionContext(JitFunctionContext &&) = default;
  JitFunctionContext &operator=(JitFunctionContext &&) = default;
  // Non copyable
  JitFunctionContext(const JitFunctionContext &) = delete;
  JitFunctionContext &operator=(const JitFunctionContext &) = delete;

  const llvm::BitVector &getReservedRegs() const { return ReservedRegs; }

private:
  friend class JitFunction;

  std::unique_ptr<llvm::LLVMContext> Context;
  std::unique_ptr<llvm::LLVMTargetMachine> TM;
  std::unique_ptr<llvm::MachineModuleInfo> MMI;
  std::unique_ptr<llvm::Module> Module;
  llvm::MachineFunction *MF = nullptr;
  llvm::BitVector ReservedRegs;
};

// Creates a void() function from a sequence of llvm::MCInst.
class JitFunction {
public:
  // Assembles Instructions into an executable function.
  JitFunction(JitFunctionContext &&Context,
              llvm::ArrayRef<llvm::MCInst> Instructions);

  // Retrieves the function as an array of bytes.
  llvm::StringRef getFunctionBytes() const { return FunctionBytes; }

  // Retrieves the callable function.
  void operator()() const {
    char* const FnData = const_cast<char*>(FunctionBytes.data());
    ((void (*)())(intptr_t)FnData)();
  }

private:
  JitFunctionContext FunctionContext;
  std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
  llvm::StringRef FunctionBytes;
};

} // namespace exegesis

#endif // LLVM_TOOLS_LLVM_EXEGESIS_INMEMORYASSEMBLER_H
OpenPOWER on IntegriCloud