| 1 | //===- X86MCLFIRewriter.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 | // This file declares the X86MCLFIRewriter class, the X86 specific |
| 10 | // subclass of MCLFIRewriter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef LLVM_LIB_TARGET_X86_MCTARGETDESC_X86MCLFIREWRITER_H |
| 14 | #define LLVM_LIB_TARGET_X86_MCTARGETDESC_X86MCLFIREWRITER_H |
| 15 | |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/MC/MCInst.h" |
| 18 | #include "llvm/MC/MCInstrInfo.h" |
| 19 | #include "llvm/MC/MCLFIRewriter.h" |
| 20 | #include "llvm/MC/MCRegisterInfo.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | class MCContext; |
| 24 | class MCStreamer; |
| 25 | class MCSubtargetInfo; |
| 26 | |
| 27 | namespace X86 { |
| 28 | |
| 29 | constexpr unsigned LFIBundleSize = 32; |
| 30 | |
| 31 | class X86MCLFIRewriter : public MCLFIRewriter { |
| 32 | public: |
| 33 | X86MCLFIRewriter(MCContext &Ctx, std::unique_ptr<MCRegisterInfo> &&RI, |
| 34 | std::unique_ptr<MCInstrInfo> &&II) |
| 35 | : MCLFIRewriter(Ctx, std::move(RI), std::move(II)) {} |
| 36 | |
| 37 | bool rewriteInst(const MCInst &Inst, MCStreamer &Out, |
| 38 | const MCSubtargetInfo &STI) override; |
| 39 | |
| 40 | void onLabel(const MCSymbol *Symbol, MCStreamer &Out) override; |
| 41 | void finish(MCStreamer &Out) override; |
| 42 | |
| 43 | private: |
| 44 | /// Recursion guard to prevent infinite loops when emitting instructions. |
| 45 | bool Guard = false; |
| 46 | |
| 47 | /// Prefixes written on their own line, held back so that they can be emitted |
| 48 | /// immediately before the instruction they apply to. |
| 49 | SmallVector<MCInst, 2> Prefixes; |
| 50 | |
| 51 | void doRewriteInst(const MCInst &Inst, MCStreamer &Out, |
| 52 | const MCSubtargetInfo &STI); |
| 53 | |
| 54 | /// Emit the prefixes that were held back, followed by Inst. |
| 55 | void emitWithPrefixes(const MCInst &Inst, MCStreamer &Out, |
| 56 | const MCSubtargetInfo &STI); |
| 57 | |
| 58 | /// Report and drop the prefixes that were held back, for when the |
| 59 | /// instruction they would apply to does not take them. |
| 60 | void discardPrefixes(); |
| 61 | |
| 62 | /// Return the 32-bit subregister of Reg, or Reg itself if it already |
| 63 | /// is a 32-bit register. |
| 64 | MCRegister getReg32(MCRegister Reg) const; |
| 65 | |
| 66 | void rewriteSyscall(const MCInst &Inst, MCStreamer &Out, |
| 67 | const MCSubtargetInfo &STI); |
| 68 | |
| 69 | /// Emit the mask sequence that turns an arbitrary value in Reg into a |
| 70 | /// bundle-aligned address inside the sandbox. |
| 71 | void emitSandboxBranchReg(MCRegister Reg, MCStreamer &Out, |
| 72 | const MCSubtargetInfo &STI); |
| 73 | |
| 74 | void rewriteIndirectBranch(const MCInst &Inst, MCStreamer &Out, |
| 75 | const MCSubtargetInfo &STI); |
| 76 | void rewriteDirectCall(const MCInst &Inst, MCStreamer &Out, |
| 77 | const MCSubtargetInfo &STI); |
| 78 | void rewriteReturn(const MCInst &Inst, MCStreamer &Out, |
| 79 | const MCSubtargetInfo &STI); |
| 80 | |
| 81 | bool isFSAccess(const MCInst &Inst); |
| 82 | void rewriteFSAccess(const MCInst &Inst, MCStreamer &Out, |
| 83 | const MCSubtargetInfo &STI); |
| 84 | |
| 85 | /// Rewrite the addressing mode of the memory operand at MemIdx so that |
| 86 | /// the access it performs stays inside the sandbox. |
| 87 | void sandboxMemOperand(MCInst &Inst, int MemIdx) const; |
| 88 | |
| 89 | /// Apply sandboxMemOperand to the memory operand of Inst, if it has one |
| 90 | /// that it dereferences. |
| 91 | void sandboxMemAccess(MCInst &Inst) const; |
| 92 | |
| 93 | void rewriteMemAccess(const MCInst &Inst, MCStreamer &Out, |
| 94 | const MCSubtargetInfo &STI); |
| 95 | void rewriteStringOperation(const MCInst &Inst, MCStreamer &Out, |
| 96 | const MCSubtargetInfo &STI); |
| 97 | |
| 98 | /// Return the stack pointer register that Inst writes as an explicit |
| 99 | /// operand, or NoRegister if it does not write one. |
| 100 | MCRegister getWrittenStackReg(const MCInst &Inst) const; |
| 101 | |
| 102 | void rewriteStackModification(MCRegister StackReg, const MCInst &Inst, |
| 103 | MCStreamer &Out, const MCSubtargetInfo &STI); |
| 104 | void rewriteLeave(const MCInst &Inst, MCStreamer &Out, |
| 105 | const MCSubtargetInfo &STI); |
| 106 | }; |
| 107 | |
| 108 | } // namespace X86 |
| 109 | } // namespace llvm |
| 110 | #endif // LLVM_LIB_TARGET_X86_MCTARGETDESC_X86MCLFIREWRITER_H |
| 111 | |