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 | |
| 36 | // Gather the set of reserved registers (depends on function's calling |
| 37 | // convention and target machine). |
| 38 | llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM); |
| 39 | |
| 40 | // Creates a temporary `void foo()` function containing the provided |
| 41 | // Instructions. Runs a set of llvm Passes to provide correct prologue and |
| 42 | // epilogue. Once the MachineFunction is ready, it is assembled for TM to |
| 43 | // AsmStream, the temporary function is eventually discarded. |
| 44 | void assembleToStream(std::unique_ptr<llvm::LLVMTargetMachine> TM, |
| 45 | llvm::ArrayRef<llvm::MCInst> Instructions, |
| 46 | llvm::raw_pwrite_stream &AsmStream); |
| 47 | |
| 48 | // Creates an ObjectFile in the format understood by the host. |
| 49 | // Note: the resulting object keeps a copy of Buffer so it can be discarded once |
| 50 | // this function returns. |
| 51 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 52 | getObjectFromBuffer(llvm::StringRef Buffer); |
| 53 | |
| 54 | // Loads the content of Filename as on ObjectFile and returns it. |
| 55 | llvm::object::OwningBinary<llvm::object::ObjectFile> |
| 56 | getObjectFromFile(llvm::StringRef Filename); |
| 57 | |
| 58 | // Consumes an ObjectFile containing a `void foo()` function and make it |
| 59 | // executable. |
| 60 | struct ExecutableFunction { |
| 61 | explicit ExecutableFunction( |
| 62 | std::unique_ptr<llvm::LLVMTargetMachine> TM, |
| 63 | llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder); |
| 64 | |
| 65 | // Retrieves the function as an array of bytes. |
| 66 | llvm::StringRef getFunctionBytes() const { return FunctionBytes; } |
| 67 | |
| 68 | // Executes the function. |
| 69 | void operator()() const { ((void (*)())(intptr_t)FunctionBytes.data())(); } |
| 70 | |
| 71 | std::unique_ptr<llvm::LLVMContext> Context; |
| 72 | std::unique_ptr<llvm::ExecutionEngine> ExecEngine; |
| 73 | llvm::StringRef FunctionBytes; |
| 74 | }; |
| 75 | |
| 76 | } // namespace exegesis |
| 77 | |
| 78 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H |