Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 1 | //===-- Assembler.h ---------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 6 | // |
| 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 Courbet | 78b2e73 | 2018-09-25 07:31:44 +0000 | [diff] [blame] | 20 | #include "BenchmarkCode.h" |
Miloš Stojanović | 79c7d34 | 2020-02-19 14:34:12 +0100 | [diff] [blame] | 21 | #include "Error.h" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 22 | #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 Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 35 | namespace llvm { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 36 | namespace exegesis { |
| 37 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 38 | class ExegesisTarget; |
| 39 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 40 | // Gather the set of reserved registers (depends on function's calling |
| 41 | // convention and target machine). |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 42 | BitVector getFunctionReservedRegs(const TargetMachine &TM); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 43 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 44 | // Helper to fill in a basic block. |
| 45 | class BasicBlockFiller { |
| 46 | public: |
| 47 | BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB, |
| 48 | const MCInstrInfo *MCII); |
| 49 | |
| 50 | void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc()); |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 51 | void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc()); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 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. |
| 61 | class FunctionFiller { |
| 62 | public: |
| 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 | |
| 77 | private: |
| 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. |
| 84 | using FillFunction = std::function<void(FunctionFiller &)>; |
| 85 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 86 | // Creates a temporary `void foo(char*)` function containing the provided |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 87 | // 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ć | 79c7d34 | 2020-02-19 14:34:12 +0100 | [diff] [blame] | 90 | Error 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 Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 95 | |
| 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 Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 99 | object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 100 | |
| 101 | // Loads the content of Filename as on ObjectFile and returns it. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 102 | object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 103 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 104 | // Consumes an ObjectFile containing a `void foo(char*)` function and make it |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 105 | // executable. |
| 106 | struct ExecutableFunction { |
| 107 | explicit ExecutableFunction( |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 108 | std::unique_ptr<LLVMTargetMachine> TM, |
| 109 | object::OwningBinary<object::ObjectFile> &&ObjectFileHolder); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 110 | |
| 111 | // Retrieves the function as an array of bytes. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 112 | StringRef getFunctionBytes() const { return FunctionBytes; } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 113 | |
| 114 | // Executes the function. |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 115 | void operator()(char *Memory) const { |
| 116 | ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory); |
| 117 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 118 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 119 | std::unique_ptr<LLVMContext> Context; |
| 120 | std::unique_ptr<ExecutionEngine> ExecEngine; |
| 121 | StringRef FunctionBytes; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 124 | // Creates a void(int8*) MachineFunction. |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame] | 125 | MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID, |
| 126 | Module *Module, |
| 127 | MachineModuleInfo *MMI); |
Clement Courbet | 9431b72 | 2019-09-27 12:56:24 +0000 | [diff] [blame] | 128 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 129 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 130 | } // namespace llvm |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 131 | |
| 132 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |