1 //===-- RISCVAsmBackend.h - RISC-V Assembler Backend ----------------------===// 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 #ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVASMBACKEND_H 10 #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVASMBACKEND_H 11 12 #include "MCTargetDesc/RISCVBaseInfo.h" 13 #include "MCTargetDesc/RISCVFixupKinds.h" 14 #include "MCTargetDesc/RISCVMCTargetDesc.h" 15 #include "llvm/MC/MCAsmBackend.h" 16 #include "llvm/MC/MCFixupKindInfo.h" 17 #include "llvm/MC/MCSubtargetInfo.h" 18 19 namespace llvm { 20 class MCAssembler; 21 class MCObjectTargetWriter; 22 class raw_ostream; 23 24 class RISCVAsmBackend : public MCAsmBackend { 25 const MCSubtargetInfo &STI; 26 uint8_t OSABI; 27 bool Is64Bit; 28 bool ForceRelocs = false; 29 const MCTargetOptions &TargetOptions; 30 31 public: 32 RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, 33 const MCTargetOptions &Options) 34 : MCAsmBackend(llvm::endianness::little, RISCV::fixup_riscv_relax), 35 STI(STI), OSABI(OSABI), Is64Bit(Is64Bit), TargetOptions(Options) { 36 RISCVFeatures::validate(STI.getTargetTriple(), STI.getFeatureBits()); 37 } 38 ~RISCVAsmBackend() override = default; 39 40 void setForceRelocs() { ForceRelocs = true; } 41 42 // Return Size with extra Nop Bytes for alignment directive in code section. 43 bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, 44 unsigned &Size) override; 45 46 // Insert target specific fixup type for alignment directive in code section. 47 bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, 48 const MCAsmLayout &Layout, 49 MCAlignFragment &AF) override; 50 51 bool evaluateTargetFixup(const MCAssembler &Asm, const MCAsmLayout &Layout, 52 const MCFixup &Fixup, const MCFragment *DF, 53 const MCValue &Target, const MCSubtargetInfo *STI, 54 uint64_t &Value, bool &WasForced) override; 55 56 bool handleAddSubRelocations(const MCAsmLayout &Layout, const MCFragment &F, 57 const MCFixup &Fixup, const MCValue &Target, 58 uint64_t &FixedValue) const override; 59 60 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, 61 const MCValue &Target, MutableArrayRef<char> Data, 62 uint64_t Value, bool IsResolved, 63 const MCSubtargetInfo *STI) const override; 64 65 std::unique_ptr<MCObjectTargetWriter> 66 createObjectTargetWriter() const override; 67 68 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, 69 const MCValue &Target, 70 const MCSubtargetInfo *STI) override; 71 72 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 73 const MCRelaxableFragment *DF, 74 const MCAsmLayout &Layout) const override { 75 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced"); 76 } 77 78 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, 79 uint64_t Value, 80 const MCRelaxableFragment *DF, 81 const MCAsmLayout &Layout, 82 const bool WasForced) const override; 83 84 unsigned getNumFixupKinds() const override { 85 return RISCV::NumTargetFixupKinds; 86 } 87 88 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override; 89 90 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; 91 92 bool mayNeedRelaxation(const MCInst &Inst, 93 const MCSubtargetInfo &STI) const override; 94 unsigned getRelaxedOpcode(unsigned Op) const; 95 96 void relaxInstruction(MCInst &Inst, 97 const MCSubtargetInfo &STI) const override; 98 99 bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout, 100 bool &WasRelaxed) const override; 101 bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout, 102 bool &WasRelaxed) const override; 103 std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, 104 int64_t &Value) const override; 105 106 bool writeNopData(raw_ostream &OS, uint64_t Count, 107 const MCSubtargetInfo *STI) const override; 108 109 const MCTargetOptions &getTargetOptions() const { return TargetOptions; } 110 }; 111 } 112 113 #endif 114