1 //===-- RISCVAsmBackend.cpp - RISCV Assembler Backend ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/RISCVFixupKinds.h"
11 #include "MCTargetDesc/RISCVMCTargetDesc.h"
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCELFObjectWriter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixupKindInfo.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
29 class RISCVAsmBackend : public MCAsmBackend {
30 const MCSubtargetInfo &STI;
35 RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit)
36 : MCAsmBackend(support::little), STI(STI), OSABI(OSABI),
38 ~RISCVAsmBackend() override {}
40 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
41 const MCValue &Target, MutableArrayRef<char> Data,
42 uint64_t Value, bool IsResolved) const override;
44 std::unique_ptr<MCObjectWriter>
45 createObjectWriter(raw_pwrite_stream &OS) const override;
47 // If linker relaxation is enabled, always emit relocations even if the fixup
48 // can be resolved. This is necessary for correctness as offsets may change
50 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
51 const MCValue &Target) override {
52 return STI.getFeatureBits()[RISCV::FeatureRelax];
55 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
56 const MCRelaxableFragment *DF,
57 const MCAsmLayout &Layout) const override {
58 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
61 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
63 const MCRelaxableFragment *DF,
64 const MCAsmLayout &Layout,
65 const bool WasForced) const override;
67 unsigned getNumFixupKinds() const override {
68 return RISCV::NumTargetFixupKinds;
71 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
72 const static MCFixupKindInfo Infos[RISCV::NumTargetFixupKinds] = {
73 // This table *must* be in the order that the fixup_* kinds are defined in
76 // name offset bits flags
77 { "fixup_riscv_hi20", 12, 20, 0 },
78 { "fixup_riscv_lo12_i", 20, 12, 0 },
79 { "fixup_riscv_lo12_s", 0, 32, 0 },
80 { "fixup_riscv_pcrel_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
81 { "fixup_riscv_pcrel_lo12_i", 20, 12, MCFixupKindInfo::FKF_IsPCRel },
82 { "fixup_riscv_pcrel_lo12_s", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
83 { "fixup_riscv_jal", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
84 { "fixup_riscv_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
85 { "fixup_riscv_rvc_jump", 2, 11, MCFixupKindInfo::FKF_IsPCRel },
86 { "fixup_riscv_rvc_branch", 0, 16, MCFixupKindInfo::FKF_IsPCRel }
89 if (Kind < FirstTargetFixupKind)
90 return MCAsmBackend::getFixupKindInfo(Kind);
92 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
94 return Infos[Kind - FirstTargetFixupKind];
97 bool mayNeedRelaxation(const MCInst &Inst) const override;
98 unsigned getRelaxedOpcode(unsigned Op) const;
100 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
101 MCInst &Res) const override;
104 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
108 bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
111 const MCRelaxableFragment *DF,
112 const MCAsmLayout &Layout,
113 const bool WasForced) const {
114 // Return true if the symbol is actually unresolved.
115 // Resolved could be always false when shouldForceRelocation return true.
116 // We use !WasForced to indicate that the symbol is unresolved and not forced
117 // by shouldForceRelocation.
118 if (!Resolved && !WasForced)
121 int64_t Offset = int64_t(Value);
122 switch ((unsigned)Fixup.getKind()) {
125 case RISCV::fixup_riscv_rvc_branch:
126 // For compressed branch instructions the immediate must be
127 // in the range [-256, 254].
128 return Offset > 254 || Offset < -256;
129 case RISCV::fixup_riscv_rvc_jump:
130 // For compressed jump instructions the immediate must be
131 // in the range [-2048, 2046].
132 return Offset > 2046 || Offset < -2048;
136 void RISCVAsmBackend::relaxInstruction(const MCInst &Inst,
137 const MCSubtargetInfo &STI,
139 // TODO: replace this with call to auto generated uncompressinstr() function.
140 switch (Inst.getOpcode()) {
142 llvm_unreachable("Opcode not expected!");
144 // c.beqz $rs1, $imm -> beq $rs1, X0, $imm.
145 Res.setOpcode(RISCV::BEQ);
146 Res.addOperand(Inst.getOperand(0));
147 Res.addOperand(MCOperand::createReg(RISCV::X0));
148 Res.addOperand(Inst.getOperand(1));
151 // c.bnez $rs1, $imm -> bne $rs1, X0, $imm.
152 Res.setOpcode(RISCV::BNE);
153 Res.addOperand(Inst.getOperand(0));
154 Res.addOperand(MCOperand::createReg(RISCV::X0));
155 Res.addOperand(Inst.getOperand(1));
158 // c.j $imm -> jal X0, $imm.
159 Res.setOpcode(RISCV::JAL);
160 Res.addOperand(MCOperand::createReg(RISCV::X0));
161 Res.addOperand(Inst.getOperand(0));
164 // c.jal $imm -> jal X1, $imm.
165 Res.setOpcode(RISCV::JAL);
166 Res.addOperand(MCOperand::createReg(RISCV::X1));
167 Res.addOperand(Inst.getOperand(0));
172 // Given a compressed control flow instruction this function returns
173 // the expanded instruction.
174 unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
183 case RISCV::C_JAL: // fall through.
188 bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
189 return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
192 bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
193 bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
194 unsigned MinNopLen = HasStdExtC ? 2 : 4;
196 if ((Count % MinNopLen) != 0)
199 // The canonical nop on RISC-V is addi x0, x0, 0.
200 uint64_t Nop32Count = Count / 4;
201 for (uint64_t i = Nop32Count; i != 0; --i)
202 OS.write("\x13\0\0\0", 4);
204 // The canonical nop on RVC is c.nop.
206 uint64_t Nop16Count = (Count - Nop32Count * 4) / 2;
207 for (uint64_t i = Nop16Count; i != 0; --i)
208 OS.write("\x01\0", 2);
214 static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
216 unsigned Kind = Fixup.getKind();
219 llvm_unreachable("Unknown fixup kind!");
225 case RISCV::fixup_riscv_lo12_i:
226 case RISCV::fixup_riscv_pcrel_lo12_i:
227 return Value & 0xfff;
228 case RISCV::fixup_riscv_lo12_s:
229 case RISCV::fixup_riscv_pcrel_lo12_s:
230 return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
231 case RISCV::fixup_riscv_hi20:
232 case RISCV::fixup_riscv_pcrel_hi20:
233 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
234 return ((Value + 0x800) >> 12) & 0xfffff;
235 case RISCV::fixup_riscv_jal: {
236 if (!isInt<21>(Value))
237 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
239 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
240 // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
241 unsigned Sbit = (Value >> 20) & 0x1;
242 unsigned Hi8 = (Value >> 12) & 0xff;
243 unsigned Mid1 = (Value >> 11) & 0x1;
244 unsigned Lo10 = (Value >> 1) & 0x3ff;
246 // Inst{30-21} = Lo10;
248 // Inst{19-12} = Hi8;
249 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
252 case RISCV::fixup_riscv_branch: {
253 if (!isInt<13>(Value))
254 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
256 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
257 // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
259 unsigned Sbit = (Value >> 12) & 0x1;
260 unsigned Hi1 = (Value >> 11) & 0x1;
261 unsigned Mid6 = (Value >> 5) & 0x3f;
262 unsigned Lo4 = (Value >> 1) & 0xf;
264 // Inst{30-25} = Mid6;
267 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
270 case RISCV::fixup_riscv_rvc_jump: {
271 // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
272 unsigned Bit11 = (Value >> 11) & 0x1;
273 unsigned Bit4 = (Value >> 4) & 0x1;
274 unsigned Bit9_8 = (Value >> 8) & 0x3;
275 unsigned Bit10 = (Value >> 10) & 0x1;
276 unsigned Bit6 = (Value >> 6) & 0x1;
277 unsigned Bit7 = (Value >> 7) & 0x1;
278 unsigned Bit3_1 = (Value >> 1) & 0x7;
279 unsigned Bit5 = (Value >> 5) & 0x1;
280 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
281 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
284 case RISCV::fixup_riscv_rvc_branch: {
285 // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
286 unsigned Bit8 = (Value >> 8) & 0x1;
287 unsigned Bit7_6 = (Value >> 6) & 0x3;
288 unsigned Bit5 = (Value >> 5) & 0x1;
289 unsigned Bit4_3 = (Value >> 3) & 0x3;
290 unsigned Bit2_1 = (Value >> 1) & 0x3;
291 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
299 static unsigned getSize(unsigned Kind) {
303 case RISCV::fixup_riscv_rvc_jump:
304 case RISCV::fixup_riscv_rvc_branch:
309 void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
310 const MCValue &Target,
311 MutableArrayRef<char> Data, uint64_t Value,
312 bool IsResolved) const {
313 MCContext &Ctx = Asm.getContext();
314 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
316 return; // Doesn't change encoding.
317 // Apply any target-specific value adjustments.
318 Value = adjustFixupValue(Fixup, Value, Ctx);
320 // Shift the value into position.
321 Value <<= Info.TargetOffset;
323 unsigned Offset = Fixup.getOffset();
324 unsigned FullSize = getSize(Fixup.getKind());
327 unsigned NumBytes = (Info.TargetSize + 7) / 8;
328 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
331 // For each byte of the fragment that the fixup touches, mask in the
332 // bits from the fixup value.
333 for (unsigned i = 0; i != FullSize; ++i) {
334 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
338 std::unique_ptr<MCObjectWriter>
339 RISCVAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
340 return createRISCVELFObjectWriter(OS, OSABI, Is64Bit);
343 } // end anonymous namespace
345 MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
346 const MCSubtargetInfo &STI,
347 const MCRegisterInfo &MRI,
348 const MCTargetOptions &Options) {
349 const Triple &TT = STI.getTargetTriple();
350 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
351 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit());