blob: 76030ae8f008cd9d6e3e500560dc68d48a8ac303 [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
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000042// A simple object storing the value for a particular register.
43struct RegisterValue {
44 unsigned Register;
45 llvm::APInt Value;
46};
47
Guillaume Chateletfb943542018-08-01 14:41:45 +000048// Creates a temporary `void foo(char*)` function containing the provided
Clement Courbet0e69e2d2018-05-17 10:52:18 +000049// Instructions. Runs a set of llvm Passes to provide correct prologue and
50// epilogue. Once the MachineFunction is ready, it is assembled for TM to
51// AsmStream, the temporary function is eventually discarded.
Clement Courbet4860b982018-06-26 08:49:30 +000052void assembleToStream(const ExegesisTarget &ET,
Clement Courbeta51efc22018-06-25 13:12:02 +000053 std::unique_ptr<llvm::LLVMTargetMachine> TM,
Guillaume Chateletfb943542018-08-01 14:41:45 +000054 llvm::ArrayRef<unsigned> LiveIns,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000055 llvm::ArrayRef<RegisterValue> RegisterInitialValues,
Clement Courbet0e69e2d2018-05-17 10:52:18 +000056 llvm::ArrayRef<llvm::MCInst> Instructions,
57 llvm::raw_pwrite_stream &AsmStream);
58
59// Creates an ObjectFile in the format understood by the host.
60// Note: the resulting object keeps a copy of Buffer so it can be discarded once
61// this function returns.
62llvm::object::OwningBinary<llvm::object::ObjectFile>
63getObjectFromBuffer(llvm::StringRef Buffer);
64
65// Loads the content of Filename as on ObjectFile and returns it.
66llvm::object::OwningBinary<llvm::object::ObjectFile>
67getObjectFromFile(llvm::StringRef Filename);
68
Guillaume Chateletfb943542018-08-01 14:41:45 +000069// Consumes an ObjectFile containing a `void foo(char*)` function and make it
Clement Courbet0e69e2d2018-05-17 10:52:18 +000070// executable.
71struct ExecutableFunction {
72 explicit ExecutableFunction(
73 std::unique_ptr<llvm::LLVMTargetMachine> TM,
74 llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder);
75
76 // Retrieves the function as an array of bytes.
77 llvm::StringRef getFunctionBytes() const { return FunctionBytes; }
78
79 // Executes the function.
Guillaume Chateletfb943542018-08-01 14:41:45 +000080 void operator()(char *Memory) const {
81 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
82 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +000083
84 std::unique_ptr<llvm::LLVMContext> Context;
85 std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
86 llvm::StringRef FunctionBytes;
87};
88
89} // namespace exegesis
90
91#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H