1 //===-- Assembler.h ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines classes to assemble functions composed of a single basic block of
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
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"
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
);
42 // A simple object storing the value for a particular register.
43 struct RegisterValue
{
48 // Creates a temporary `void foo(char*)` function containing the provided
49 // Instructions. Runs a set of llvm Passes to provide correct prologue and
50 // epilogue. Once the MachineFunction is ready, it is assembled for TM to
51 // AsmStream, the temporary function is eventually discarded.
52 void assembleToStream(const ExegesisTarget
&ET
,
53 std::unique_ptr
<llvm::LLVMTargetMachine
> TM
,
54 llvm::ArrayRef
<unsigned> LiveIns
,
55 llvm::ArrayRef
<RegisterValue
> RegisterInitialValues
,
56 llvm::ArrayRef
<llvm::MCInst
> Instructions
,
57 llvm::raw_pwrite_stream
&AsmStream
);
59 // Creates an ObjectFile in the format understood by the host.
60 // Note: the resulting object keeps a copy of Buffer so it can be discarded once
61 // this function returns.
62 llvm::object::OwningBinary
<llvm::object::ObjectFile
>
63 getObjectFromBuffer(llvm::StringRef Buffer
);
65 // Loads the content of Filename as on ObjectFile and returns it.
66 llvm::object::OwningBinary
<llvm::object::ObjectFile
>
67 getObjectFromFile(llvm::StringRef Filename
);
69 // Consumes an ObjectFile containing a `void foo(char*)` function and make it
71 struct ExecutableFunction
{
72 explicit ExecutableFunction(
73 std::unique_ptr
<llvm::LLVMTargetMachine
> TM
,
74 llvm::object::OwningBinary
<llvm::object::ObjectFile
> &&ObjectFileHolder
);
76 // Retrieves the function as an array of bytes.
77 llvm::StringRef
getFunctionBytes() const { return FunctionBytes
; }
79 // Executes the function.
80 void operator()(char *Memory
) const {
81 ((void (*)(char *))(intptr_t)FunctionBytes
.data())(Memory
);
84 std::unique_ptr
<llvm::LLVMContext
> Context
;
85 std::unique_ptr
<llvm::ExecutionEngine
> ExecEngine
;
86 llvm::StringRef FunctionBytes
;
89 } // namespace exegesis
91 #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H