blob: ee6bc86f3788487f12c70ca8ae17536dd5eaf889 [file] [log] [blame]
Clement Courbet0e69e2d2018-05-17 10:52:18 +00001//===-- 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
Clement Courbet78b2e732018-09-25 07:31:44 +000021#include "BenchmarkCode.h"
Clement Courbet0e69e2d2018-05-17 10:52:18 +000022#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"
34
Fangrui Song32401af2018-10-22 17:10:47 +000035namespace llvm {
Clement Courbet0e69e2d2018-05-17 10:52:18 +000036namespace exegesis {
37
Clement Courbeta51efc22018-06-25 13:12:02 +000038class ExegesisTarget;
39
Clement Courbet0e69e2d2018-05-17 10:52:18 +000040// Gather the set of reserved registers (depends on function's calling
41// convention and target machine).
42llvm::BitVector getFunctionReservedRegs(const llvm::TargetMachine &TM);
43
Guillaume Chateletfb943542018-08-01 14:41:45 +000044// Creates a temporary `void foo(char*)` function containing the provided
Clement Courbet0e69e2d2018-05-17 10:52:18 +000045// Instructions. Runs a set of llvm Passes to provide correct prologue and
46// epilogue. Once the MachineFunction is ready, it is assembled for TM to
47// AsmStream, the temporary function is eventually discarded.
Clement Courbet4860b982018-06-26 08:49:30 +000048void assembleToStream(const ExegesisTarget &ET,
Clement Courbeta51efc22018-06-25 13:12:02 +000049 std::unique_ptr<llvm::LLVMTargetMachine> TM,
Guillaume Chateletfb943542018-08-01 14:41:45 +000050 llvm::ArrayRef<unsigned> LiveIns,
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000051 llvm::ArrayRef<RegisterValue> RegisterInitialValues,
Clement Courbet0e69e2d2018-05-17 10:52:18 +000052 llvm::ArrayRef<llvm::MCInst> Instructions,
53 llvm::raw_pwrite_stream &AsmStream);
54
55// Creates an ObjectFile in the format understood by the host.
56// Note: the resulting object keeps a copy of Buffer so it can be discarded once
57// this function returns.
58llvm::object::OwningBinary<llvm::object::ObjectFile>
59getObjectFromBuffer(llvm::StringRef Buffer);
60
61// Loads the content of Filename as on ObjectFile and returns it.
62llvm::object::OwningBinary<llvm::object::ObjectFile>
63getObjectFromFile(llvm::StringRef Filename);
64
Guillaume Chateletfb943542018-08-01 14:41:45 +000065// Consumes an ObjectFile containing a `void foo(char*)` function and make it
Clement Courbet0e69e2d2018-05-17 10:52:18 +000066// executable.
67struct ExecutableFunction {
68 explicit ExecutableFunction(
69 std::unique_ptr<llvm::LLVMTargetMachine> TM,
70 llvm::object::OwningBinary<llvm::object::ObjectFile> &&ObjectFileHolder);
71
72 // Retrieves the function as an array of bytes.
73 llvm::StringRef getFunctionBytes() const { return FunctionBytes; }
74
75 // Executes the function.
Guillaume Chateletfb943542018-08-01 14:41:45 +000076 void operator()(char *Memory) const {
77 ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
78 }
Clement Courbet0e69e2d2018-05-17 10:52:18 +000079
80 std::unique_ptr<llvm::LLVMContext> Context;
81 std::unique_ptr<llvm::ExecutionEngine> ExecEngine;
82 llvm::StringRef FunctionBytes;
83};
84
85} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +000086} // namespace llvm
Clement Courbet0e69e2d2018-05-17 10:52:18 +000087
88#endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H