blob: 5d4204e4a50ce8bf8c952838bdf1e44f61dd9d7f [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"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000021#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
Fangrui Song32401af2018-10-22 17:10:47 +000034namespace llvm {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000035namespace exegesis {
36
Clement Courbeta51efc22018-06-25 13:12:02 +000037class ExegesisTarget;
38
Clement Courbet0e69e2d2018-05-17 10:52:18 +000039// Gather the set of reserved registers (depends on function's calling
40// convention and target machine).
Clement Courbet50cdd562019-10-09 11:58:42 +000041BitVector getFunctionReservedRegs(const TargetMachine &TM);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000042
Clement Courbet9431b722019-09-27 12:56:24 +000043// Helper to fill in a basic block.
44class BasicBlockFiller {
45public:
46 BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB,
47 const MCInstrInfo *MCII);
48
49 void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc());
Clement Courbet50cdd562019-10-09 11:58:42 +000050 void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc());
Clement Courbet9431b722019-09-27 12:56:24 +000051
52 void addReturn(const DebugLoc &DL = DebugLoc());
53
54 MachineFunction &MF;
55 MachineBasicBlock *const MBB;
56 const MCInstrInfo *const MCII;
57};
58
59// Helper to fill in a function.
60class FunctionFiller {
61public:
62 FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp);
63
64 // Adds a basic block to the function.
65 BasicBlockFiller addBasicBlock();
66
67 // Returns the function entry point.
68 BasicBlockFiller getEntry() { return Entry; }
69
70 MachineFunction &MF;
71 const MCInstrInfo *const MCII;
72
73 // Returns the set of registers in the snippet setup code.
74 ArrayRef<unsigned> getRegistersSetUp() const;
75
76private:
77 BasicBlockFiller Entry;
78 // The set of registers that are set up in the basic block.
79 std::vector<unsigned> RegistersSetUp;
80};
81
82// A callback that fills a function.
83using FillFunction = std::function<void(FunctionFiller &)>;
84
Guillaume Chateletfb943542018-08-01 14:41:45 +000085// Creates a temporary `void foo(char*)` function containing the provided
Clement Courbet0e69e2d2018-05-17 10:52:18 +000086// Instructions. Runs a set of llvm Passes to provide correct prologue and
87// epilogue. Once the MachineFunction is ready, it is assembled for TM to
88// AsmStream, the temporary function is eventually discarded.
Clement Courbet4860b982018-06-26 08:49:30 +000089void assembleToStream(const ExegesisTarget &ET,
Clement Courbet50cdd562019-10-09 11:58:42 +000090 std::unique_ptr<LLVMTargetMachine> TM,
91 ArrayRef<unsigned> LiveIns,
92 ArrayRef<RegisterValue> RegisterInitialValues,
93 const FillFunction &Fill, raw_pwrite_stream &AsmStream);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000094
95// Creates an ObjectFile in the format understood by the host.
96// Note: the resulting object keeps a copy of Buffer so it can be discarded once
97// this function returns.
Clement Courbet50cdd562019-10-09 11:58:42 +000098object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer);
Clement Courbet0e69e2d2018-05-17 10:52:18 +000099
100// Loads the content of Filename as on ObjectFile and returns it.
Clement Courbet50cdd562019-10-09 11:58:42 +0000101object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000102
Guillaume Chateletfb943542018-08-01 14:41:45 +0000103// Consumes an ObjectFile containing a `void foo(char*)` function and make it
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000104// executable.
105struct ExecutableFunction {
106 explicit ExecutableFunction(
Clement Courbet50cdd562019-10-09 11:58:42 +0000107 std::unique_ptr<LLVMTargetMachine> TM,
108 object::OwningBinary<object::ObjectFile> &&ObjectFileHolder);
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000109
110 // Retrieves the function as an array of bytes.
Clement Courbet50cdd562019-10-09 11:58:42 +0000111 StringRef getFunctionBytes() const { return FunctionBytes; }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000112
113 // Executes the function.
Guillaume Chateletfb943542018-08-01 14:41:45 +0000114 void operator()(char *Memory) const {
115 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
116 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000117
Clement Courbet50cdd562019-10-09 11:58:42 +0000118 std::unique_ptr<LLVMContext> Context;
119 std::unique_ptr<ExecutionEngine> ExecEngine;
120 StringRef FunctionBytes;
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000121};
122
Clement Courbet9431b722019-09-27 12:56:24 +0000123// Creates a void(int8*) MachineFunction.
Clement Courbet50cdd562019-10-09 11:58:42 +0000124MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID,
125 Module *Module,
126 MachineModuleInfo *MMI);
Clement Courbet9431b722019-09-27 12:56:24 +0000127
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000128} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000129} // namespace llvm
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000130
131#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H