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" |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 34 | namespace llvm { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 35 | namespace exegesis { |
| 36 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 37 | class ExegesisTarget; |
| 38 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 39 | // Gather the set of reserved registers (depends on function's calling |
| 40 | // convention and target machine). |
| 41 | llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM); |
| 42 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 43 | // Creates a temporary `void foo(char*)` function containing the provided |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 44 | // Instructions. Runs a set of llvm Passes to provide correct prologue and |
| 45 | // epilogue. Once the MachineFunction is ready, it is assembled for TM to |
| 46 | // AsmStream, the temporary function is eventually discarded. |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 47 | void assembleToStream(const ExegesisTarget &ET, |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 48 | std::unique_ptr<llvm::LLVMTargetMachine> TM, |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 49 | llvm::ArrayRef<unsigned> LiveIns, |
Guillaume Chatelet | c96a97b | 2018-09-20 12:22:18 +0000 | [diff] [blame] | 50 | llvm::ArrayRef<RegisterValue> RegisterInitialValues, |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 51 | llvm::ArrayRef<llvm::MCInst> Instructions, |
| 52 | llvm::raw_pwrite_stream &AsmStream); |
| 53 | |
| 54 | // Creates an ObjectFile in the format understood by the host. |
| 55 | // Note: the resulting object keeps a copy of Buffer so it can be discarded once |
| 56 | // this function returns. |
| 57 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 58 | getObjectFromBuffer(llvm::StringRef Buffer); |
| 59 | |
| 60 | // Loads the content of Filename as on ObjectFile and returns it. |
| 61 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 62 | getObjectFromFile(llvm::StringRef Filename); |
| 63 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 64 | // Consumes an ObjectFile containing a `void foo(char*)` function and make it |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 65 | // executable. |
| 66 | struct ExecutableFunction { |
| 67 | explicit ExecutableFunction( |
| 68 | std::unique_ptr<llvm::LLVMTargetMachine> TM, |
| 69 | llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder); |
| 70 | |
| 71 | // Retrieves the function as an array of bytes. |
| 72 | llvm::StringRef getFunctionBytes() const { return FunctionBytes; } |
| 73 | |
| 74 | // Executes the function. |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 75 | void operator()(char *Memory) const { |
| 76 | ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory); |
| 77 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 78 | |
| 79 | std::unique_ptr<llvm::LLVMContext> Context; |
| 80 | std::unique_ptr<llvm::ExecutionEngine> ExecEngine; |
| 81 | llvm::StringRef FunctionBytes; |
| 82 | }; |
| 83 | |
| 84 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 85 | } // namespace llvm |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 86 | |
| 87 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |