1 //===-- LoongArchAsmBackend.h - LoongArch Assembler Backend ---*- 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 defines the LoongArchAsmBackend class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_LOONGARCH_MCTARGETDESC_LOONGARCHASMBACKEND_H 14 #define LLVM_LIB_TARGET_LOONGARCH_MCTARGETDESC_LOONGARCHASMBACKEND_H 15 16 #include "MCTargetDesc/LoongArchBaseInfo.h" 17 #include "MCTargetDesc/LoongArchFixupKinds.h" 18 #include "MCTargetDesc/LoongArchMCTargetDesc.h" 19 #include "llvm/MC/MCAsmBackend.h" 20 #include "llvm/MC/MCExpr.h" 21 #include "llvm/MC/MCFixupKindInfo.h" 22 #include "llvm/MC/MCSection.h" 23 #include "llvm/MC/MCSubtargetInfo.h" 24 25 namespace llvm { 26 27 class LoongArchAsmBackend : public MCAsmBackend { 28 const MCSubtargetInfo &STI; 29 uint8_t OSABI; 30 bool Is64Bit; 31 const MCTargetOptions &TargetOptions; 32 DenseMap<MCSection *, const MCSymbolRefExpr *> SecToAlignSym; 33 34 public: 35 LoongArchAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit, 36 const MCTargetOptions &Options) 37 : MCAsmBackend(llvm::endianness::little, 38 LoongArch::fixup_loongarch_relax), 39 STI(STI), OSABI(OSABI), Is64Bit(Is64Bit), TargetOptions(Options) {} 40 ~LoongArchAsmBackend() override {} 41 42 bool handleAddSubRelocations(const MCAssembler &Asm, const MCFragment &F, 43 const MCFixup &Fixup, const MCValue &Target, 44 uint64_t &FixedValue) const override; 45 46 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, 47 const MCValue &Target, MutableArrayRef<char> Data, 48 uint64_t Value, bool IsResolved, 49 const MCSubtargetInfo *STI) const override; 50 51 // Return Size with extra Nop Bytes for alignment directive in code section. 52 bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, 53 unsigned &Size) override; 54 55 // Insert target specific fixup type for alignment directive in code section. 56 bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, 57 MCAlignFragment &AF) override; 58 59 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, 60 const MCValue &Target, 61 const MCSubtargetInfo *STI) override; 62 63 unsigned getNumFixupKinds() const override { 64 return LoongArch::NumTargetFixupKinds; 65 } 66 67 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override; 68 69 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override; 70 71 void relaxInstruction(MCInst &Inst, 72 const MCSubtargetInfo &STI) const override {} 73 74 std::pair<bool, bool> relaxLEB128(const MCAssembler &Asm, MCLEBFragment &LF, 75 int64_t &Value) const override; 76 77 bool relaxDwarfLineAddr(const MCAssembler &Asm, MCDwarfLineAddrFragment &DF, 78 bool &WasRelaxed) const override; 79 bool relaxDwarfCFA(const MCAssembler &Asm, MCDwarfCallFrameFragment &DF, 80 bool &WasRelaxed) const override; 81 82 bool writeNopData(raw_ostream &OS, uint64_t Count, 83 const MCSubtargetInfo *STI) const override; 84 85 std::unique_ptr<MCObjectTargetWriter> 86 createObjectTargetWriter() const override; 87 const MCTargetOptions &getTargetOptions() const { return TargetOptions; } 88 DenseMap<MCSection *, const MCSymbolRefExpr *> &getSecToAlignSym() { 89 return SecToAlignSym; 90 } 91 }; 92 } // end namespace llvm 93 94 #endif // LLVM_LIB_TARGET_LOONGARCH_MCTARGETDESC_LOONGARCHASMBACKEND_H 95