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