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:
LoongArchAsmBackend(const MCSubtargetInfo & STI,uint8_t OSABI,bool Is64Bit,const MCTargetOptions & Options)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) {}
~LoongArchAsmBackend()40   ~LoongArchAsmBackend() override {}
41 
42   bool handleAddSubRelocations(const MCAsmLayout &Layout, 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                                      const MCAsmLayout &Layout,
58                                      MCAlignFragment &AF) override;
59 
60   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
61                              const MCValue &Target,
62                              const MCSubtargetInfo *STI) override;
63 
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout)64   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
65                             const MCRelaxableFragment *DF,
66                             const MCAsmLayout &Layout) const override {
67     return false;
68   }
69 
getNumFixupKinds()70   unsigned getNumFixupKinds() const override {
71     return LoongArch::NumTargetFixupKinds;
72   }
73 
74   std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
75 
76   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
77 
relaxInstruction(MCInst & Inst,const MCSubtargetInfo & STI)78   void relaxInstruction(MCInst &Inst,
79                         const MCSubtargetInfo &STI) const override {}
80 
81   std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout,
82                                     int64_t &Value) const override;
83 
84   bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout,
85                           bool &WasRelaxed) const override;
86   bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout,
87                      bool &WasRelaxed) const override;
88 
89   bool writeNopData(raw_ostream &OS, uint64_t Count,
90                     const MCSubtargetInfo *STI) const override;
91 
92   std::unique_ptr<MCObjectTargetWriter>
93   createObjectTargetWriter() const override;
getTargetOptions()94   const MCTargetOptions &getTargetOptions() const { return TargetOptions; }
getSecToAlignSym()95   DenseMap<MCSection *, const MCSymbolRefExpr *> &getSecToAlignSym() {
96     return SecToAlignSym;
97   }
98 };
99 } // end namespace llvm
100 
101 #endif // LLVM_LIB_TARGET_LOONGARCH_MCTARGETDESC_LOONGARCHASMBACKEND_H
102