blob: 2a83344b751e597b2bb79363197955a436704d0e [file] [log] [blame]
Clement Courbet0e69e2d2018-05-17 10:52:18 +00001//===-- Assembler.h ---------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Clement Courbet0e69e2d2018-05-17 10:52:18 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines classes to assemble functions composed of a single basic block of
11/// MCInsts.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
16#define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
17
18#include <memory>
19
Clement Courbet78b2e732018-09-25 07:31:44 +000020#include "BenchmarkCode.h"
Miloš Stojanović79c7d342020-02-19 14:34:12 +010021#include "Error.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000022#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/BitVector.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/ExecutionEngine/ExecutionEngine.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/MC/MCInst.h"
30#include "llvm/Object/Binary.h"
31#include "llvm/Object/ObjectFile.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetMachine.h"
34
Fangrui Song32401af2018-10-22 17:10:47 +000035namespace llvm {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000036namespace exegesis {
37
Clement Courbeta51efc22018-06-25 13:12:02 +000038class ExegesisTarget;
39
Clement Courbet0e69e2d2018-05-17 10:52:18 +000040// Gather the set of reserved registers (depends on function's calling
41// convention and target machine).
Clement Courbet50cdd562019-10-09 11:58:42 +000042BitVector getFunctionReservedRegs(const TargetMachine &TM);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000043
Clement Courbet9431b722019-09-27 12:56:24 +000044// Helper to fill in a basic block.
45class BasicBlockFiller {
46public:
47 BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB,
48 const MCInstrInfo *MCII);
49
50 void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc());
Clement Courbet50cdd562019-10-09 11:58:42 +000051 void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc());
Clement Courbet9431b722019-09-27 12:56:24 +000052
53 void addReturn(const DebugLoc &DL = DebugLoc());
54
55 MachineFunction &MF;
56 MachineBasicBlock *const MBB;
57 const MCInstrInfo *const MCII;
58};
59
60// Helper to fill in a function.
61class FunctionFiller {
62public:
63 FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp);
64
65 // Adds a basic block to the function.
66 BasicBlockFiller addBasicBlock();
67
68 // Returns the function entry point.
69 BasicBlockFiller getEntry() { return Entry; }
70
71 MachineFunction &MF;
72 const MCInstrInfo *const MCII;
73
74 // Returns the set of registers in the snippet setup code.
75 ArrayRef<unsigned> getRegistersSetUp() const;
76
77private:
78 BasicBlockFiller Entry;
79 // The set of registers that are set up in the basic block.
80 std::vector<unsigned> RegistersSetUp;
81};
82
83// A callback that fills a function.
84using FillFunction = std::function<void(FunctionFiller &)>;
85
Guillaume Chateletfb943542018-08-01 14:41:45 +000086// Creates a temporary `void foo(char*)` function containing the provided
Clement Courbet0e69e2d2018-05-17 10:52:18 +000087// Instructions. Runs a set of llvm Passes to provide correct prologue and
88// epilogue. Once the MachineFunction is ready, it is assembled for TM to
89// AsmStream, the temporary function is eventually discarded.
Miloš Stojanović79c7d342020-02-19 14:34:12 +010090Error assembleToStream(const ExegesisTarget &ET,
91 std::unique_ptr<LLVMTargetMachine> TM,
92 ArrayRef<unsigned> LiveIns,
93 ArrayRef<RegisterValue> RegisterInitialValues,
94 const FillFunction &Fill, raw_pwrite_stream &AsmStream);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000095
96// Creates an ObjectFile in the format understood by the host.
97// Note: the resulting object keeps a copy of Buffer so it can be discarded once
98// this function returns.
Clement Courbet50cdd562019-10-09 11:58:42 +000099object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000100
101// Loads the content of Filename as on ObjectFile and returns it.
Clement Courbet50cdd562019-10-09 11:58:42 +0000102object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000103
Guillaume Chateletfb943542018-08-01 14:41:45 +0000104// Consumes an ObjectFile containing a `void foo(char*)` function and make it
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000105// executable.
106struct ExecutableFunction {
107 explicit ExecutableFunction(
Clement Courbet50cdd562019-10-09 11:58:42 +0000108 std::unique_ptr<LLVMTargetMachine> TM,
109 object::OwningBinary<object::ObjectFile> &&ObjectFileHolder);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000110
111 // Retrieves the function as an array of bytes.
Clement Courbet50cdd562019-10-09 11:58:42 +0000112 StringRef getFunctionBytes() const { return FunctionBytes; }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000113
114 // Executes the function.
Guillaume Chateletfb943542018-08-01 14:41:45 +0000115 void operator()(char *Memory) const {
116 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
117 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000118
Clement Courbet50cdd562019-10-09 11:58:42 +0000119 std::unique_ptr<LLVMContext> Context;
120 std::unique_ptr<ExecutionEngine> ExecEngine;
121 StringRef FunctionBytes;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000122};
123
Clement Courbet9431b722019-09-27 12:56:24 +0000124// Creates a void(int8*) MachineFunction.
Clement Courbet50cdd562019-10-09 11:58:42 +0000125MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID,
126 Module *Module,
127 MachineModuleInfo *MMI);
Clement Courbet9431b722019-09-27 12:56:24 +0000128
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000129} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000130} // namespace llvm
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000131
132#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H