[llvm-exegesis][NFC] moving code around.
[llvm-complete.git] / tools / llvm-exegesis / lib / Assembler.h
blobf2a77168cb7fb50135408f3b6357d31dbaf3006f
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 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
17 #define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
19 #include <memory>
21 #include "BenchmarkCode.h"
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"
35 namespace exegesis {
37 class ExegesisTarget;
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);
43 // Creates a temporary `void foo(char*)` function containing the provided
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.
47 void assembleToStream(const ExegesisTarget &ET,
48 std::unique_ptr<llvm::LLVMTargetMachine> TM,
49 llvm::ArrayRef<unsigned> LiveIns,
50 llvm::ArrayRef<RegisterValue> RegisterInitialValues,
51 llvm::ArrayRef<llvm::MCInst> Instructions,
52 llvm::raw_pwrite_stream &AsmStream);
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);
60 // Loads the content of Filename as on ObjectFile and returns it.
61 llvm::object::OwningBinary<llvm::object::ObjectFile>
62 getObjectFromFile(llvm::StringRef Filename);
64 // Consumes an ObjectFile containing a `void foo(char*)` function and make it
65 // executable.
66 struct ExecutableFunction {
67 explicit ExecutableFunction(
68 std::unique_ptr<llvm::LLVMTargetMachine> TM,
69 llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder);
71 // Retrieves the function as an array of bytes.
72 llvm::StringRef getFunctionBytes() const { return FunctionBytes; }
74 // Executes the function.
75 void operator()(char *Memory) const {
76 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
79 std::unique_ptr<llvm::LLVMContext> Context;
80 std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
81 llvm::StringRef FunctionBytes;
84 } // namespace exegesis
86 #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H