1 //===-- RISCVAsmBackend.h - RISCV 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/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 
19 namespace llvm {
20 class MCAssembler;
21 class MCObjectTargetWriter;
22 class raw_ostream;
23 
24 class RISCVAsmBackend : public MCAsmBackend {
25   const MCSubtargetInfo &STI;
26   uint8_t OSABI;
27   bool Is64Bit;
28   bool ForceRelocs = false;
29   const MCTargetOptions &TargetOptions;
30 
31 public:
32   RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit,
33                   const MCTargetOptions &Options)
34       : MCAsmBackend(support::little), STI(STI), OSABI(OSABI), Is64Bit(Is64Bit),
35         TargetOptions(Options) {
36     RISCVFeatures::validate(STI.getTargetTriple(), STI.getFeatureBits());
37   }
38   ~RISCVAsmBackend() override = default;
39 
40   void setForceRelocs() { ForceRelocs = true; }
41 
42   // Return Size with extra Nop Bytes for alignment directive in code section.
43   bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
44                                              unsigned &Size) override;
45 
46   // Insert target specific fixup type for alignment directive in code section.
47   bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
48                                      const MCAsmLayout &Layout,
49                                      MCAlignFragment &AF) override;
50 
51   bool evaluateTargetFixup(const MCAssembler &Asm, const MCAsmLayout &Layout,
52                            const MCFixup &Fixup, const MCFragment *DF,
53                            const MCValue &Target, uint64_t &Value,
54                            bool &WasForced) override;
55 
56   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
57                   const MCValue &Target, MutableArrayRef<char> Data,
58                   uint64_t Value, bool IsResolved,
59                   const MCSubtargetInfo *STI) const override;
60 
61   std::unique_ptr<MCObjectTargetWriter>
62   createObjectTargetWriter() const override;
63 
64   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
65                              const MCValue &Target) override;
66 
67   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
68                             const MCRelaxableFragment *DF,
69                             const MCAsmLayout &Layout) const override {
70     llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
71   }
72 
73   bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
74                                     uint64_t Value,
75                                     const MCRelaxableFragment *DF,
76                                     const MCAsmLayout &Layout,
77                                     const bool WasForced) const override;
78 
79   unsigned getNumFixupKinds() const override {
80     return RISCV::NumTargetFixupKinds;
81   }
82 
83   Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
84 
85   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
86 
87   bool mayNeedRelaxation(const MCInst &Inst,
88                          const MCSubtargetInfo &STI) const override;
89   unsigned getRelaxedOpcode(unsigned Op) const;
90 
91   void relaxInstruction(MCInst &Inst,
92                         const MCSubtargetInfo &STI) const override;
93 
94   bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF, MCAsmLayout &Layout,
95                           bool &WasRelaxed) const override;
96   bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout,
97                      bool &WasRelaxed) const override;
98 
99   bool writeNopData(raw_ostream &OS, uint64_t Count,
100                     const MCSubtargetInfo *STI) const override;
101 
102   const MCTargetOptions &getTargetOptions() const { return TargetOptions; }
103 };
104 }
105 
106 #endif
107