Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 1 | //===-- Assembler.h ---------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// Defines classes to assemble functions composed of a single basic block of |
| 12 | /// MCInsts. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |
| 17 | #define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 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 | |
| 34 | namespace exegesis { |
| 35 | |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 36 | class ExegesisTarget; |
| 37 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 38 | // Gather the set of reserved registers (depends on function's calling |
| 39 | // convention and target machine). |
| 40 | llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM); |
| 41 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame^] | 42 | // Creates a temporary `void foo(char*)` function containing the provided |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 43 | // Instructions. Runs a set of llvm Passes to provide correct prologue and |
| 44 | // epilogue. Once the MachineFunction is ready, it is assembled for TM to |
| 45 | // AsmStream, the temporary function is eventually discarded. |
Clement Courbet | 4860b98 | 2018-06-26 08:49:30 +0000 | [diff] [blame] | 46 | void assembleToStream(const ExegesisTarget &ET, |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 47 | std::unique_ptr<llvm::LLVMTargetMachine> TM, |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame^] | 48 | llvm::ArrayRef<unsigned> LiveIns, |
Clement Courbet | a51efc2 | 2018-06-25 13:12:02 +0000 | [diff] [blame] | 49 | llvm::ArrayRef<unsigned> RegsToDef, |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 50 | llvm::ArrayRef<llvm::MCInst> Instructions, |
| 51 | llvm::raw_pwrite_stream &AsmStream); |
| 52 | |
| 53 | // Creates an ObjectFile in the format understood by the host. |
| 54 | // Note: the resulting object keeps a copy of Buffer so it can be discarded once |
| 55 | // this function returns. |
| 56 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 57 | getObjectFromBuffer(llvm::StringRef Buffer); |
| 58 | |
| 59 | // Loads the content of Filename as on ObjectFile and returns it. |
| 60 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 61 | getObjectFromFile(llvm::StringRef Filename); |
| 62 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame^] | 63 | // Consumes an ObjectFile containing a `void foo(char*)` function and make it |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 64 | // executable. |
| 65 | struct ExecutableFunction { |
| 66 | explicit ExecutableFunction( |
| 67 | std::unique_ptr<llvm::LLVMTargetMachine> TM, |
| 68 | llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder); |
| 69 | |
| 70 | // Retrieves the function as an array of bytes. |
| 71 | llvm::StringRef getFunctionBytes() const { return FunctionBytes; } |
| 72 | |
| 73 | // Executes the function. |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame^] | 74 | void operator()(char *Memory) const { |
| 75 | ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory); |
| 76 | } |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 77 | |
| 78 | std::unique_ptr<llvm::LLVMContext> Context; |
| 79 | std::unique_ptr<llvm::ExecutionEngine> ExecEngine; |
| 80 | llvm::StringRef FunctionBytes; |
| 81 | }; |
| 82 | |
| 83 | } // namespace exegesis |
| 84 | |
| 85 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |