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
25namespace llvm {
26
27class 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 // Temporary symbol used to check whether a PC-relative fixup is resolved.
34 MCSymbol *PCRelTemp = nullptr;
35
36 bool isPCRelFixupResolved(const MCSymbol *SymA, const MCFragment &F);
37
38public:
39 LoongArchAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit,
40 const MCTargetOptions &Options);
41
42 bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
43 uint64_t &FixedValue, bool IsResolved);
44
45 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
46 MutableArrayRef<char> Data, uint64_t Value,
47 bool IsResolved) override;
48
49 // Return Size with extra Nop Bytes for alignment directive in code section.
50 bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
51 unsigned &Size) override;
52
53 // Insert target specific fixup type for alignment directive in code section.
54 bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
55 MCAlignFragment &AF) override;
56
57 bool shouldForceRelocation(const MCFixup &Fixup, const MCValue &Target);
58
59 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
60
61 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
62
63 void relaxInstruction(MCInst &Inst,
64 const MCSubtargetInfo &STI) const override {}
65
66 bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,
67 bool &WasRelaxed) const override;
68 bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF,
69 bool &WasRelaxed) const override;
70 std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF,
71 int64_t &Value) const override;
72
73 bool writeNopData(raw_ostream &OS, uint64_t Count,
74 const MCSubtargetInfo *STI) const override;
75
76 std::unique_ptr<MCObjectTargetWriter>
77 createObjectTargetWriter() const override;
78 const MCTargetOptions &getTargetOptions() const { return TargetOptions; }
79 DenseMap<MCSection *, const MCSymbolRefExpr *> &getSecToAlignSym() {
80 return SecToAlignSym;
81 }
82};
83} // end namespace llvm
84
85#endif // LLVM_LIB_TARGET_LOONGARCH_MCTARGETDESC_LOONGARCHASMBACKEND_H
86