Assembler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //===-- Assembler.h ---------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Defines classes to assemble functions composed of a single basic block of
  11. /// MCInsts.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
  15. #define LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H
  16. #include <memory>
  17. #include "BenchmarkCode.h"
  18. #include "Error.h"
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/BitVector.h"
  21. #include "llvm/CodeGen/MachineFunction.h"
  22. #include "llvm/CodeGen/MachineModuleInfo.h"
  23. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  24. #include "llvm/IR/LLVMContext.h"
  25. #include "llvm/IR/Module.h"
  26. #include "llvm/MC/MCInst.h"
  27. #include "llvm/Object/Binary.h"
  28. #include "llvm/Object/ObjectFile.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. #include "llvm/Target/TargetMachine.h"
  31. namespace llvm {
  32. namespace exegesis {
  33. class ExegesisTarget;
  34. // Gather the set of reserved registers (depends on function's calling
  35. // convention and target machine).
  36. BitVector getFunctionReservedRegs(const TargetMachine &TM);
  37. // Helper to fill in a basic block.
  38. class BasicBlockFiller {
  39. public:
  40. BasicBlockFiller(MachineFunction &MF, MachineBasicBlock *MBB,
  41. const MCInstrInfo *MCII);
  42. void addInstruction(const MCInst &Inst, const DebugLoc &DL = DebugLoc());
  43. void addInstructions(ArrayRef<MCInst> Insts, const DebugLoc &DL = DebugLoc());
  44. void addReturn(const DebugLoc &DL = DebugLoc());
  45. MachineFunction &MF;
  46. MachineBasicBlock *const MBB;
  47. const MCInstrInfo *const MCII;
  48. };
  49. // Helper to fill in a function.
  50. class FunctionFiller {
  51. public:
  52. FunctionFiller(MachineFunction &MF, std::vector<unsigned> RegistersSetUp);
  53. // Adds a basic block to the function.
  54. BasicBlockFiller addBasicBlock();
  55. // Returns the function entry point.
  56. BasicBlockFiller getEntry() { return Entry; }
  57. MachineFunction &MF;
  58. const MCInstrInfo *const MCII;
  59. // Returns the set of registers in the snippet setup code.
  60. ArrayRef<unsigned> getRegistersSetUp() const;
  61. private:
  62. BasicBlockFiller Entry;
  63. // The set of registers that are set up in the basic block.
  64. std::vector<unsigned> RegistersSetUp;
  65. };
  66. // A callback that fills a function.
  67. using FillFunction = std::function<void(FunctionFiller &)>;
  68. // Creates a temporary `void foo(char*)` function containing the provided
  69. // Instructions. Runs a set of llvm Passes to provide correct prologue and
  70. // epilogue. Once the MachineFunction is ready, it is assembled for TM to
  71. // AsmStream, the temporary function is eventually discarded.
  72. Error assembleToStream(const ExegesisTarget &ET,
  73. std::unique_ptr<LLVMTargetMachine> TM,
  74. ArrayRef<unsigned> LiveIns,
  75. ArrayRef<RegisterValue> RegisterInitialValues,
  76. const FillFunction &Fill, raw_pwrite_stream &AsmStream);
  77. // Creates an ObjectFile in the format understood by the host.
  78. // Note: the resulting object keeps a copy of Buffer so it can be discarded once
  79. // this function returns.
  80. object::OwningBinary<object::ObjectFile> getObjectFromBuffer(StringRef Buffer);
  81. // Loads the content of Filename as on ObjectFile and returns it.
  82. object::OwningBinary<object::ObjectFile> getObjectFromFile(StringRef Filename);
  83. // Consumes an ObjectFile containing a `void foo(char*)` function and make it
  84. // executable.
  85. struct ExecutableFunction {
  86. explicit ExecutableFunction(
  87. std::unique_ptr<LLVMTargetMachine> TM,
  88. object::OwningBinary<object::ObjectFile> &&ObjectFileHolder);
  89. // Retrieves the function as an array of bytes.
  90. StringRef getFunctionBytes() const { return FunctionBytes; }
  91. // Executes the function.
  92. void operator()(char *Memory) const {
  93. ((void (*)(char *))(intptr_t)FunctionBytes.data())(Memory);
  94. }
  95. std::unique_ptr<LLVMContext> Context;
  96. std::unique_ptr<ExecutionEngine> ExecEngine;
  97. StringRef FunctionBytes;
  98. };
  99. // Creates a void(int8*) MachineFunction.
  100. MachineFunction &createVoidVoidPtrMachineFunction(StringRef FunctionID,
  101. Module *Module,
  102. MachineModuleInfo *MMI);
  103. } // namespace exegesis
  104. } // namespace llvm
  105. #endif // LLVM_TOOLS_LLVM_EXEGESIS_ASSEMBLER_H