blob: e9a65cbbe36945e34862dc1575c2f9cbe2fe3d96 [file] [log] [blame]
Clement Courbet0e69e2d2018-05-17 10:52:18 +00001//===-- Assembler.h ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// Defines classes to assemble functions composed of a single basic block of
12/// MCInsts.
13///
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
17#define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
18
19#include <memory>
20
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/BitVector.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/ExecutionEngine/ExecutionEngine.h"
26#include "llvm/IR/LLVMContext.h"
27#include "llvm/IR/Module.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/Object/Binary.h"
30#include "llvm/Object/ObjectFile.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Target/TargetMachine.h"
33
34namespace exegesis {
35
Clement Courbeta51efc22018-06-25 13:12:02 +000036class ExegesisTarget;
37
Clement Courbet0e69e2d2018-05-17 10:52:18 +000038// Gather the set of reserved registers (depends on function's calling
39// convention and target machine).
40llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM);
41
42// Creates a temporary `void foo()` function containing the provided
43// Instructions. Runs a set of llvm Passes to provide correct prologue and
44// epilogue. Once the MachineFunction is ready, it is assembled for TM to
45// AsmStream, the temporary function is eventually discarded.
Clement Courbet4860b982018-06-26 08:49:30 +000046void assembleToStream(const ExegesisTarget &ET,
Clement Courbeta51efc22018-06-25 13:12:02 +000047 std::unique_ptr<llvm::LLVMTargetMachine> TM,
48 llvm::ArrayRef<unsigned> RegsToDef,
Clement Courbet0e69e2d2018-05-17 10:52:18 +000049 llvm::ArrayRef<llvm::MCInst> Instructions,
50 llvm::raw_pwrite_stream &AsmStream);
51
52// Creates an ObjectFile in the format understood by the host.
53// Note: the resulting object keeps a copy of Buffer so it can be discarded once
54// this function returns.
55llvm::object::OwningBinary<llvm::object::ObjectFile>
56getObjectFromBuffer(llvm::StringRef Buffer);
57
58// Loads the content of Filename as on ObjectFile and returns it.
59llvm::object::OwningBinary<llvm::object::ObjectFile>
60getObjectFromFile(llvm::StringRef Filename);
61
62// Consumes an ObjectFile containing a `void foo()` function and make it
63// executable.
64struct ExecutableFunction {
65 explicit ExecutableFunction(
66 std::unique_ptr<llvm::LLVMTargetMachine> TM,
67 llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder);
68
69 // Retrieves the function as an array of bytes.
70 llvm::StringRef getFunctionBytes() const { return FunctionBytes; }
71
72 // Executes the function.
73 void operator()() const { ((void (*)())(intptr_t)FunctionBytes.data())(); }
74
75 std::unique_ptr<llvm::LLVMContext> Context;
76 std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
77 llvm::StringRef FunctionBytes;
78};
79
80} // namespace exegesis
81
82#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H