blob: 61c5845cc175c0cbece88f0ab8c588a738660218 [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVAsmBackend.cpp - RISCV Assembler Backend ---------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Bradbury6b2cca72016-11-01 23:47:30 +00006//
7//===----------------------------------------------------------------------===//
8
Alex Bradbury9c03e4c2018-11-12 14:25:07 +00009#include "RISCVAsmBackend.h"
Alex Bradburyeb3a64a2018-12-20 14:52:15 +000010#include "RISCVMCExpr.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000011#include "llvm/ADT/APInt.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000012#include "llvm/MC/MCAssembler.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000013#include "llvm/MC/MCContext.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000014#include "llvm/MC/MCDirectives.h"
15#include "llvm/MC/MCELFObjectWriter.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/MC/MCExpr.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000017#include "llvm/MC/MCObjectWriter.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000018#include "llvm/MC/MCSymbol.h"
Shiva Chen5af037f2019-01-30 11:16:59 +000019#include "llvm/MC/MCValue.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace llvm;
24
Alex Bradburyeb3a64a2018-12-20 14:52:15 +000025// If linker relaxation is enabled, or the relax option had previously been
26// enabled, always emit relocations even if the fixup can be resolved. This is
27// necessary for correctness as offsets may change during relaxation.
28bool RISCVAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
29 const MCFixup &Fixup,
30 const MCValue &Target) {
31 bool ShouldForce = false;
32
33 switch ((unsigned)Fixup.getKind()) {
34 default:
35 break;
Alex Bradbury1c1f8f22019-08-19 13:23:02 +000036 case FK_Data_1:
37 case FK_Data_2:
38 case FK_Data_4:
39 case FK_Data_8:
40 if (Target.isAbsolute())
41 return false;
42 break;
Alex Bradbury8eb87e52019-02-15 09:43:46 +000043 case RISCV::fixup_riscv_got_hi20:
Lewis Revilldf3cb472019-04-23 14:46:13 +000044 case RISCV::fixup_riscv_tls_got_hi20:
45 case RISCV::fixup_riscv_tls_gd_hi20:
Alex Bradbury8eb87e52019-02-15 09:43:46 +000046 return true;
Alex Bradburyeb3a64a2018-12-20 14:52:15 +000047 case RISCV::fixup_riscv_pcrel_lo12_i:
48 case RISCV::fixup_riscv_pcrel_lo12_s:
49 // For pcrel_lo12, force a relocation if the target of the corresponding
50 // pcrel_hi20 is not in the same fragment.
51 const MCFixup *T = cast<RISCVMCExpr>(Fixup.getValue())->getPCRelHiFixup();
52 if (!T) {
53 Asm.getContext().reportError(Fixup.getLoc(),
54 "could not find corresponding %pcrel_hi");
55 return false;
56 }
57
58 switch ((unsigned)T->getKind()) {
59 default:
60 llvm_unreachable("Unexpected fixup kind for pcrel_lo12");
61 break;
Alex Bradbury8eb87e52019-02-15 09:43:46 +000062 case RISCV::fixup_riscv_got_hi20:
Lewis Revilldf3cb472019-04-23 14:46:13 +000063 case RISCV::fixup_riscv_tls_got_hi20:
64 case RISCV::fixup_riscv_tls_gd_hi20:
Alex Bradbury8eb87e52019-02-15 09:43:46 +000065 ShouldForce = true;
66 break;
Alex Bradburyeb3a64a2018-12-20 14:52:15 +000067 case RISCV::fixup_riscv_pcrel_hi20:
68 ShouldForce = T->getValue()->findAssociatedFragment() !=
69 Fixup.getValue()->findAssociatedFragment();
70 break;
71 }
72 break;
73 }
74
75 return ShouldForce || STI.getFeatureBits()[RISCV::FeatureRelax] ||
76 ForceRelocs;
77}
78
Shiva Chen6e07dfb2018-05-18 06:42:21 +000079bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
80 bool Resolved,
81 uint64_t Value,
82 const MCRelaxableFragment *DF,
83 const MCAsmLayout &Layout,
84 const bool WasForced) const {
85 // Return true if the symbol is actually unresolved.
86 // Resolved could be always false when shouldForceRelocation return true.
87 // We use !WasForced to indicate that the symbol is unresolved and not forced
88 // by shouldForceRelocation.
89 if (!Resolved && !WasForced)
90 return true;
91
Sameer AbuAsal2646a412018-03-02 22:04:12 +000092 int64_t Offset = int64_t(Value);
93 switch ((unsigned)Fixup.getKind()) {
94 default:
95 return false;
96 case RISCV::fixup_riscv_rvc_branch:
97 // For compressed branch instructions the immediate must be
98 // in the range [-256, 254].
99 return Offset > 254 || Offset < -256;
100 case RISCV::fixup_riscv_rvc_jump:
101 // For compressed jump instructions the immediate must be
102 // in the range [-2048, 2046].
103 return Offset > 2046 || Offset < -2048;
104 }
105}
106
107void RISCVAsmBackend::relaxInstruction(const MCInst &Inst,
108 const MCSubtargetInfo &STI,
109 MCInst &Res) const {
110 // TODO: replace this with call to auto generated uncompressinstr() function.
111 switch (Inst.getOpcode()) {
112 default:
113 llvm_unreachable("Opcode not expected!");
114 case RISCV::C_BEQZ:
115 // c.beqz $rs1, $imm -> beq $rs1, X0, $imm.
116 Res.setOpcode(RISCV::BEQ);
117 Res.addOperand(Inst.getOperand(0));
118 Res.addOperand(MCOperand::createReg(RISCV::X0));
119 Res.addOperand(Inst.getOperand(1));
120 break;
121 case RISCV::C_BNEZ:
122 // c.bnez $rs1, $imm -> bne $rs1, X0, $imm.
123 Res.setOpcode(RISCV::BNE);
124 Res.addOperand(Inst.getOperand(0));
125 Res.addOperand(MCOperand::createReg(RISCV::X0));
126 Res.addOperand(Inst.getOperand(1));
127 break;
128 case RISCV::C_J:
129 // c.j $imm -> jal X0, $imm.
130 Res.setOpcode(RISCV::JAL);
131 Res.addOperand(MCOperand::createReg(RISCV::X0));
132 Res.addOperand(Inst.getOperand(0));
133 break;
134 case RISCV::C_JAL:
135 // c.jal $imm -> jal X1, $imm.
136 Res.setOpcode(RISCV::JAL);
137 Res.addOperand(MCOperand::createReg(RISCV::X1));
138 Res.addOperand(Inst.getOperand(0));
139 break;
140 }
141}
142
143// Given a compressed control flow instruction this function returns
144// the expanded instruction.
145unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
146 switch (Op) {
147 default:
148 return Op;
149 case RISCV::C_BEQZ:
150 return RISCV::BEQ;
151 case RISCV::C_BNEZ:
152 return RISCV::BNE;
153 case RISCV::C_J:
154 case RISCV::C_JAL: // fall through.
155 return RISCV::JAL;
156 }
157}
158
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000159bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst,
160 const MCSubtargetInfo &STI) const {
Sameer AbuAsal2646a412018-03-02 22:04:12 +0000161 return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
162}
163
Peter Collingbourne571a3302018-05-21 17:57:19 +0000164bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
Alex Bradburyd93f8892018-01-17 14:17:12 +0000165 bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
166 unsigned MinNopLen = HasStdExtC ? 2 : 4;
167
168 if ((Count % MinNopLen) != 0)
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000169 return false;
170
Alex Bradburyd93f8892018-01-17 14:17:12 +0000171 // The canonical nop on RISC-V is addi x0, x0, 0.
Fangrui Song44cc4e92019-06-15 06:14:15 +0000172 for (; Count >= 4; Count -= 4)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000173 OS.write("\x13\0\0\0", 4);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000174
Alex Bradburyd93f8892018-01-17 14:17:12 +0000175 // The canonical nop on RVC is c.nop.
Fangrui Song44cc4e92019-06-15 06:14:15 +0000176 if (Count && HasStdExtC)
177 OS.write("\x01\0", 2);
Alex Bradburyd93f8892018-01-17 14:17:12 +0000178
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000179 return true;
180}
181
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000182static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
183 MCContext &Ctx) {
184 unsigned Kind = Fixup.getKind();
185 switch (Kind) {
186 default:
187 llvm_unreachable("Unknown fixup kind!");
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000188 case RISCV::fixup_riscv_got_hi20:
Lewis Revilldf3cb472019-04-23 14:46:13 +0000189 case RISCV::fixup_riscv_tls_got_hi20:
190 case RISCV::fixup_riscv_tls_gd_hi20:
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000191 llvm_unreachable("Relocation should be unconditionally forced\n");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000192 case FK_Data_1:
193 case FK_Data_2:
194 case FK_Data_4:
195 case FK_Data_8:
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000196 case FK_Data_6b:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000197 return Value;
198 case RISCV::fixup_riscv_lo12_i:
Ahmed Charles646ab872018-02-06 00:55:23 +0000199 case RISCV::fixup_riscv_pcrel_lo12_i:
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000200 case RISCV::fixup_riscv_tprel_lo12_i:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000201 return Value & 0xfff;
202 case RISCV::fixup_riscv_lo12_s:
Ahmed Charles646ab872018-02-06 00:55:23 +0000203 case RISCV::fixup_riscv_pcrel_lo12_s:
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000204 case RISCV::fixup_riscv_tprel_lo12_s:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000205 return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
206 case RISCV::fixup_riscv_hi20:
207 case RISCV::fixup_riscv_pcrel_hi20:
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000208 case RISCV::fixup_riscv_tprel_hi20:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000209 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
210 return ((Value + 0x800) >> 12) & 0xfffff;
211 case RISCV::fixup_riscv_jal: {
212 if (!isInt<21>(Value))
213 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
214 if (Value & 0x1)
215 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
216 // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
217 unsigned Sbit = (Value >> 20) & 0x1;
218 unsigned Hi8 = (Value >> 12) & 0xff;
219 unsigned Mid1 = (Value >> 11) & 0x1;
220 unsigned Lo10 = (Value >> 1) & 0x3ff;
221 // Inst{31} = Sbit;
222 // Inst{30-21} = Lo10;
223 // Inst{20} = Mid1;
224 // Inst{19-12} = Hi8;
225 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
226 return Value;
227 }
228 case RISCV::fixup_riscv_branch: {
229 if (!isInt<13>(Value))
230 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
231 if (Value & 0x1)
232 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
233 // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
234 // Value.
235 unsigned Sbit = (Value >> 12) & 0x1;
236 unsigned Hi1 = (Value >> 11) & 0x1;
237 unsigned Mid6 = (Value >> 5) & 0x3f;
238 unsigned Lo4 = (Value >> 1) & 0xf;
239 // Inst{31} = Sbit;
240 // Inst{30-25} = Mid6;
241 // Inst{11-8} = Lo4;
242 // Inst{7} = Hi1;
243 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
244 return Value;
245 }
Alex Bradburyf8078f62019-04-02 12:47:20 +0000246 case RISCV::fixup_riscv_call:
247 case RISCV::fixup_riscv_call_plt: {
Shiva Chenc3d0e892018-05-30 01:16:36 +0000248 // Jalr will add UpperImm with the sign-extended 12-bit LowerImm,
249 // we need to add 0x800ULL before extract upper bits to reflect the
250 // effect of the sign extension.
251 uint64_t UpperImm = (Value + 0x800ULL) & 0xfffff000ULL;
252 uint64_t LowerImm = Value & 0xfffULL;
253 return UpperImm | ((LowerImm << 20) << 32);
254 }
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000255 case RISCV::fixup_riscv_rvc_jump: {
256 // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
257 unsigned Bit11 = (Value >> 11) & 0x1;
258 unsigned Bit4 = (Value >> 4) & 0x1;
259 unsigned Bit9_8 = (Value >> 8) & 0x3;
260 unsigned Bit10 = (Value >> 10) & 0x1;
261 unsigned Bit6 = (Value >> 6) & 0x1;
262 unsigned Bit7 = (Value >> 7) & 0x1;
263 unsigned Bit3_1 = (Value >> 1) & 0x7;
264 unsigned Bit5 = (Value >> 5) & 0x1;
265 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
266 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
267 return Value;
268 }
269 case RISCV::fixup_riscv_rvc_branch: {
270 // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
271 unsigned Bit8 = (Value >> 8) & 0x1;
272 unsigned Bit7_6 = (Value >> 6) & 0x3;
273 unsigned Bit5 = (Value >> 5) & 0x1;
274 unsigned Bit4_3 = (Value >> 3) & 0x3;
275 unsigned Bit2_1 = (Value >> 1) & 0x3;
276 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
277 (Bit5 << 2);
278 return Value;
279 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000280
281 }
282}
283
Rafael Espindola801b42d2017-06-23 22:52:36 +0000284void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
285 const MCValue &Target,
Rafael Espindola88d9e372017-06-21 23:06:53 +0000286 MutableArrayRef<char> Data, uint64_t Value,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000287 bool IsResolved,
288 const MCSubtargetInfo *STI) const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000289 MCContext &Ctx = Asm.getContext();
Mandeep Singh Grang5f043ae2017-11-10 19:09:28 +0000290 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000291 if (!Value)
292 return; // Doesn't change encoding.
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000293 // Apply any target-specific value adjustments.
294 Value = adjustFixupValue(Fixup, Value, Ctx);
295
296 // Shift the value into position.
297 Value <<= Info.TargetOffset;
298
299 unsigned Offset = Fixup.getOffset();
Alex Bradbury1c010d02018-05-23 10:53:56 +0000300 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
Mandeep Singh Grang5f043ae2017-11-10 19:09:28 +0000301
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000302 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
303
304 // For each byte of the fragment that the fixup touches, mask in the
305 // bits from the fixup value.
Alex Bradbury1c010d02018-05-23 10:53:56 +0000306 for (unsigned i = 0; i != NumBytes; ++i) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000307 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
308 }
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000309}
310
Shiva Chen5af037f2019-01-30 11:16:59 +0000311// Linker relaxation may change code size. We have to insert Nops
312// for .align directive when linker relaxation enabled. So then Linker
313// could satisfy alignment by removing Nops.
314// The function return the total Nops Size we need to insert.
315bool RISCVAsmBackend::shouldInsertExtraNopBytesForCodeAlign(
316 const MCAlignFragment &AF, unsigned &Size) {
317 // Calculate Nops Size only when linker relaxation enabled.
318 if (!STI.getFeatureBits()[RISCV::FeatureRelax])
319 return false;
320
321 bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
322 unsigned MinNopLen = HasStdExtC ? 2 : 4;
323
Alex Bradburybb479ca2019-07-16 04:40:25 +0000324 if (AF.getAlignment() <= MinNopLen) {
325 return false;
326 } else {
327 Size = AF.getAlignment() - MinNopLen;
328 return true;
329 }
Shiva Chen5af037f2019-01-30 11:16:59 +0000330}
331
332// We need to insert R_RISCV_ALIGN relocation type to indicate the
333// position of Nops and the total bytes of the Nops have been inserted
334// when linker relaxation enabled.
335// The function insert fixup_riscv_align fixup which eventually will
336// transfer to R_RISCV_ALIGN relocation type.
337bool RISCVAsmBackend::shouldInsertFixupForCodeAlign(MCAssembler &Asm,
338 const MCAsmLayout &Layout,
339 MCAlignFragment &AF) {
340 // Insert the fixup only when linker relaxation enabled.
341 if (!STI.getFeatureBits()[RISCV::FeatureRelax])
342 return false;
343
Alex Bradburye9ad0cf2019-07-16 04:37:19 +0000344 // Calculate total Nops we need to insert. If there are none to insert
345 // then simply return.
Shiva Chen5af037f2019-01-30 11:16:59 +0000346 unsigned Count;
Alex Bradburye9ad0cf2019-07-16 04:37:19 +0000347 if (!shouldInsertExtraNopBytesForCodeAlign(AF, Count) || (Count == 0))
Shiva Chen5af037f2019-01-30 11:16:59 +0000348 return false;
349
350 MCContext &Ctx = Asm.getContext();
351 const MCExpr *Dummy = MCConstantExpr::create(0, Ctx);
352 // Create fixup_riscv_align fixup.
353 MCFixup Fixup =
354 MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_align), SMLoc());
355
356 uint64_t FixedValue = 0;
357 MCValue NopBytes = MCValue::get(Count);
358
359 Asm.getWriter().recordRelocation(Asm, Layout, &AF, Fixup, NopBytes,
360 FixedValue);
361
362 return true;
363}
364
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000365std::unique_ptr<MCObjectTargetWriter>
366RISCVAsmBackend::createObjectTargetWriter() const {
367 return createRISCVELFObjectWriter(OSABI, Is64Bit);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000368}
369
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000370MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +0000371 const MCSubtargetInfo &STI,
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000372 const MCRegisterInfo &MRI,
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000373 const MCTargetOptions &Options) {
Alex Bradburyb22f7512018-01-03 08:53:05 +0000374 const Triple &TT = STI.getTargetTriple();
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000375 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Alex Bradburyfea49572019-03-09 09:28:06 +0000376 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit(), Options);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000377}