blob: 5937de9f053e1e4147a6c03942af7d1f043e54c1 [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).
41llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM);
42
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());
50 void addInstructions(ArrayRef<llvm::MCInst> Insts,
51 const DebugLoc &DL = DebugLoc());
52
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.
Clement Courbet4860b982018-06-26 08:49:30 +000090void assembleToStream(const ExegesisTarget &ET,
Clement Courbeta51efc22018-06-25 13:12:02 +000091 std::unique_ptr<llvm::LLVMTargetMachine> TM,
Guillaume Chateletfb943542018-08-01 14:41:45 +000092 llvm::ArrayRef<unsigned> LiveIns,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000093 llvm::ArrayRef<RegisterValue> RegisterInitialValues,
Clement Courbet9431b722019-09-27 12:56:24 +000094 const FillFunction &Fill,
Clement Courbet0e69e2d2018-05-17 10:52:18 +000095 llvm::raw_pwrite_stream &AsmStream);
96
97// Creates an ObjectFile in the format understood by the host.
98// Note: the resulting object keeps a copy of Buffer so it can be discarded once
99// this function returns.
100llvm::object::OwningBinary<llvm::object::ObjectFile>
101getObjectFromBuffer(llvm::StringRef Buffer);
102
103// Loads the content of Filename as on ObjectFile and returns it.
104llvm::object::OwningBinary<llvm::object::ObjectFile>
105getObjectFromFile(llvm::StringRef Filename);
106
Guillaume Chateletfb943542018-08-01 14:41:45 +0000107// Consumes an ObjectFile containing a `void foo(char*)` function and make it
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000108// executable.
109struct ExecutableFunction {
110 explicit ExecutableFunction(
111 std::unique_ptr<llvm::LLVMTargetMachine> TM,
112 llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder);
113
114 // Retrieves the function as an array of bytes.
115 llvm::StringRef getFunctionBytes() const { return FunctionBytes; }
116
117 // Executes the function.
Guillaume Chateletfb943542018-08-01 14:41:45 +0000118 void operator()(char *Memory) const {
119 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
120 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000121
122 std::unique_ptr<llvm::LLVMContext> Context;
123 std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
124 llvm::StringRef FunctionBytes;
125};
126
Clement Courbet9431b722019-09-27 12:56:24 +0000127// Creates a void(int8*) MachineFunction.
128llvm::MachineFunction &
129createVoidVoidPtrMachineFunction(llvm::StringRef FunctionID,
130 llvm::Module *Module,
131 llvm::MachineModuleInfo *MMI);
132
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000133} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000134} // namespace llvm
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000135
136#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H