blob: 5601f07f9267828c92f0d4e5a6651f840c4815ac [file] [log] [blame]
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00001//===-- RISCVAsmBackend.h - RISCV Assembler Backend -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVASMBACKEND_H
11#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVASMBACKEND_H
12
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
19namespace llvm {
20class MCAssembler;
21class MCObjectTargetWriter;
22class raw_ostream;
23
24class RISCVAsmBackend : public MCAsmBackend {
25 const MCSubtargetInfo &STI;
26 uint8_t OSABI;
27 bool Is64Bit;
28 bool ForceRelocs = false;
29
30public:
31 RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit)
32 : MCAsmBackend(support::little), STI(STI), OSABI(OSABI),
33 Is64Bit(Is64Bit) {}
34 ~RISCVAsmBackend() override {}
35
36 void setForceRelocs() { ForceRelocs = true; }
37
38 // Generate diff expression relocations if the relax feature is enabled or had
39 // previously been enabled, otherwise it is safe for the assembler to
40 // calculate these internally.
41 bool requiresDiffExpressionRelocations() const override {
42 return STI.getFeatureBits()[RISCV::FeatureRelax] || ForceRelocs;
43 }
44 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
45 const MCValue &Target, MutableArrayRef<char> Data,
46 uint64_t Value, bool IsResolved,
47 const MCSubtargetInfo *STI) const override;
48
49 std::unique_ptr<MCObjectTargetWriter>
50 createObjectTargetWriter() const override;
51
52 // If linker relaxation is enabled, or the relax option had previously been
53 // enabled, always emit relocations even if the fixup can be resolved. This is
54 // necessary for correctness as offsets may change during relaxation.
55 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
56 const MCValue &Target) override {
57 return STI.getFeatureBits()[RISCV::FeatureRelax] || ForceRelocs;
58 }
59
60 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
61 const MCRelaxableFragment *DF,
62 const MCAsmLayout &Layout) const override {
63 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
64 }
65
66 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
67 uint64_t Value,
68 const MCRelaxableFragment *DF,
69 const MCAsmLayout &Layout,
70 const bool WasForced) const override;
71
72 unsigned getNumFixupKinds() const override {
73 return RISCV::NumTargetFixupKinds;
74 }
75
76 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
77 const static MCFixupKindInfo Infos[] = {
78 // This table *must* be in the order that the fixup_* kinds are defined in
79 // RISCVFixupKinds.h.
80 //
81 // name offset bits flags
82 { "fixup_riscv_hi20", 12, 20, 0 },
83 { "fixup_riscv_lo12_i", 20, 12, 0 },
84 { "fixup_riscv_lo12_s", 0, 32, 0 },
85 { "fixup_riscv_pcrel_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
86 { "fixup_riscv_pcrel_lo12_i", 20, 12, MCFixupKindInfo::FKF_IsPCRel },
87 { "fixup_riscv_pcrel_lo12_s", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
88 { "fixup_riscv_jal", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
89 { "fixup_riscv_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
90 { "fixup_riscv_rvc_jump", 2, 11, MCFixupKindInfo::FKF_IsPCRel },
91 { "fixup_riscv_rvc_branch", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
92 { "fixup_riscv_call", 0, 64, MCFixupKindInfo::FKF_IsPCRel },
93 { "fixup_riscv_relax", 0, 0, 0 }
94 };
95 static_assert((array_lengthof(Infos)) == RISCV::NumTargetFixupKinds,
96 "Not all fixup kinds added to Infos array");
97
98 if (Kind < FirstTargetFixupKind)
99 return MCAsmBackend::getFixupKindInfo(Kind);
100
101 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
102 "Invalid kind!");
103 return Infos[Kind - FirstTargetFixupKind];
104 }
105
106 bool mayNeedRelaxation(const MCInst &Inst,
107 const MCSubtargetInfo &STI) const override;
108 unsigned getRelaxedOpcode(unsigned Op) const;
109
110 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
111 MCInst &Res) const override;
112
113
114 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
115};
116}
117
118#endif