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/ADT/StringMap.h"
16#include "llvm/MC/MCAsmBackend.h"
17#include "llvm/MC/MCSubtargetInfo.h"
18
19namespace llvm {
20class MCAssembler;
21class MCObjectTargetWriter;
22class raw_ostream;
23
24class RISCVAsmBackend : public MCAsmBackend {
25protected:
26 const MCSubtargetInfo &STI;
27 uint8_t OSABI;
28 bool Is64Bit;
29 const MCTargetOptions &TargetOptions;
30 // Temporary symbol used to check whether a PC-relative fixup is resolved.
31 MCSymbol *PCRelTemp = nullptr;
32
33 bool isPCRelFixupResolved(const MCSymbol *SymA, const MCFragment &F);
34
35 StringMap<MCSymbol *> VendorSymbols;
36
37public:
38 RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit,
39 bool IsLittleEndian, const MCTargetOptions &Options);
40 ~RISCVAsmBackend() override = default;
41
42 std::optional<bool> evaluateFixup(const MCFragment &, MCFixup &, MCValue &,
43 uint64_t &) override;
44 virtual bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
45 uint64_t &FixedValue, bool IsResolved);
46
47 void maybeAddVendorReloc(const MCFragment &, const MCFixup &);
48
49 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
50 uint8_t *Data, uint64_t Value, bool IsResolved) override;
51
52 std::unique_ptr<MCObjectTargetWriter>
53 createObjectTargetWriter() const override;
54
55 bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
56 const MCValue &, uint64_t,
57 bool) const override;
58
59 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
60
61 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
62
63 bool mayNeedRelaxation(unsigned Opcode, ArrayRef<MCOperand> Operands,
64 const MCSubtargetInfo &STI) const override;
65 void relaxInstruction(MCInst &Inst,
66 const MCSubtargetInfo &STI) const override;
67
68 bool relaxAlign(MCFragment &F, unsigned &Size) override;
69 bool relaxDwarfLineAddr(MCFragment &) const override;
70 bool relaxDwarfCFA(MCFragment &) const override;
71 std::pair<bool, bool> relaxLEB128(MCFragment &LF,
72 int64_t &Value) const override;
73
74 bool writeNopData(raw_ostream &OS, uint64_t Count,
75 const MCSubtargetInfo *STI) const override;
76
77 const MCTargetOptions &getTargetOptions() const { return TargetOptions; }
78};
79}
80
81#endif
82