blob: 49239ac9099ee2d8c9c060b92e0fe1c8ae1055f4 [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVAsmBackend.cpp - 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
Alex Bradbury9c03e4c2018-11-12 14:25:07 +000010#include "RISCVAsmBackend.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"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace llvm;
23
Shiva Chen6e07dfb2018-05-18 06:42:21 +000024bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
25 bool Resolved,
26 uint64_t Value,
27 const MCRelaxableFragment *DF,
28 const MCAsmLayout &Layout,
29 const bool WasForced) const {
30 // Return true if the symbol is actually unresolved.
31 // Resolved could be always false when shouldForceRelocation return true.
32 // We use !WasForced to indicate that the symbol is unresolved and not forced
33 // by shouldForceRelocation.
34 if (!Resolved && !WasForced)
35 return true;
36
Sameer AbuAsal2646a412018-03-02 22:04:12 +000037 int64_t Offset = int64_t(Value);
38 switch ((unsigned)Fixup.getKind()) {
39 default:
40 return false;
41 case RISCV::fixup_riscv_rvc_branch:
42 // For compressed branch instructions the immediate must be
43 // in the range [-256, 254].
44 return Offset > 254 || Offset < -256;
45 case RISCV::fixup_riscv_rvc_jump:
46 // For compressed jump instructions the immediate must be
47 // in the range [-2048, 2046].
48 return Offset > 2046 || Offset < -2048;
49 }
50}
51
52void RISCVAsmBackend::relaxInstruction(const MCInst &Inst,
53 const MCSubtargetInfo &STI,
54 MCInst &Res) const {
55 // TODO: replace this with call to auto generated uncompressinstr() function.
56 switch (Inst.getOpcode()) {
57 default:
58 llvm_unreachable("Opcode not expected!");
59 case RISCV::C_BEQZ:
60 // c.beqz $rs1, $imm -> beq $rs1, X0, $imm.
61 Res.setOpcode(RISCV::BEQ);
62 Res.addOperand(Inst.getOperand(0));
63 Res.addOperand(MCOperand::createReg(RISCV::X0));
64 Res.addOperand(Inst.getOperand(1));
65 break;
66 case RISCV::C_BNEZ:
67 // c.bnez $rs1, $imm -> bne $rs1, X0, $imm.
68 Res.setOpcode(RISCV::BNE);
69 Res.addOperand(Inst.getOperand(0));
70 Res.addOperand(MCOperand::createReg(RISCV::X0));
71 Res.addOperand(Inst.getOperand(1));
72 break;
73 case RISCV::C_J:
74 // c.j $imm -> jal X0, $imm.
75 Res.setOpcode(RISCV::JAL);
76 Res.addOperand(MCOperand::createReg(RISCV::X0));
77 Res.addOperand(Inst.getOperand(0));
78 break;
79 case RISCV::C_JAL:
80 // c.jal $imm -> jal X1, $imm.
81 Res.setOpcode(RISCV::JAL);
82 Res.addOperand(MCOperand::createReg(RISCV::X1));
83 Res.addOperand(Inst.getOperand(0));
84 break;
85 }
86}
87
88// Given a compressed control flow instruction this function returns
89// the expanded instruction.
90unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
91 switch (Op) {
92 default:
93 return Op;
94 case RISCV::C_BEQZ:
95 return RISCV::BEQ;
96 case RISCV::C_BNEZ:
97 return RISCV::BNE;
98 case RISCV::C_J:
99 case RISCV::C_JAL: // fall through.
100 return RISCV::JAL;
101 }
102}
103
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000104bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst,
105 const MCSubtargetInfo &STI) const {
Sameer AbuAsal2646a412018-03-02 22:04:12 +0000106 return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
107}
108
Peter Collingbourne571a3302018-05-21 17:57:19 +0000109bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
Alex Bradburyd93f8892018-01-17 14:17:12 +0000110 bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
111 unsigned MinNopLen = HasStdExtC ? 2 : 4;
112
113 if ((Count % MinNopLen) != 0)
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000114 return false;
115
Alex Bradburyd93f8892018-01-17 14:17:12 +0000116 // The canonical nop on RISC-V is addi x0, x0, 0.
117 uint64_t Nop32Count = Count / 4;
118 for (uint64_t i = Nop32Count; i != 0; --i)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000119 OS.write("\x13\0\0\0", 4);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000120
Alex Bradburyd93f8892018-01-17 14:17:12 +0000121 // The canonical nop on RVC is c.nop.
122 if (HasStdExtC) {
123 uint64_t Nop16Count = (Count - Nop32Count * 4) / 2;
124 for (uint64_t i = Nop16Count; i != 0; --i)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000125 OS.write("\x01\0", 2);
Alex Bradburyd93f8892018-01-17 14:17:12 +0000126 }
127
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000128 return true;
129}
130
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000131static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
132 MCContext &Ctx) {
133 unsigned Kind = Fixup.getKind();
134 switch (Kind) {
135 default:
136 llvm_unreachable("Unknown fixup kind!");
137 case FK_Data_1:
138 case FK_Data_2:
139 case FK_Data_4:
140 case FK_Data_8:
141 return Value;
142 case RISCV::fixup_riscv_lo12_i:
Ahmed Charles646ab872018-02-06 00:55:23 +0000143 case RISCV::fixup_riscv_pcrel_lo12_i:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000144 return Value & 0xfff;
145 case RISCV::fixup_riscv_lo12_s:
Ahmed Charles646ab872018-02-06 00:55:23 +0000146 case RISCV::fixup_riscv_pcrel_lo12_s:
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000147 return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
148 case RISCV::fixup_riscv_hi20:
149 case RISCV::fixup_riscv_pcrel_hi20:
150 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
151 return ((Value + 0x800) >> 12) & 0xfffff;
152 case RISCV::fixup_riscv_jal: {
153 if (!isInt<21>(Value))
154 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
155 if (Value & 0x1)
156 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
157 // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
158 unsigned Sbit = (Value >> 20) & 0x1;
159 unsigned Hi8 = (Value >> 12) & 0xff;
160 unsigned Mid1 = (Value >> 11) & 0x1;
161 unsigned Lo10 = (Value >> 1) & 0x3ff;
162 // Inst{31} = Sbit;
163 // Inst{30-21} = Lo10;
164 // Inst{20} = Mid1;
165 // Inst{19-12} = Hi8;
166 Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
167 return Value;
168 }
169 case RISCV::fixup_riscv_branch: {
170 if (!isInt<13>(Value))
171 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
172 if (Value & 0x1)
173 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
174 // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
175 // Value.
176 unsigned Sbit = (Value >> 12) & 0x1;
177 unsigned Hi1 = (Value >> 11) & 0x1;
178 unsigned Mid6 = (Value >> 5) & 0x3f;
179 unsigned Lo4 = (Value >> 1) & 0xf;
180 // Inst{31} = Sbit;
181 // Inst{30-25} = Mid6;
182 // Inst{11-8} = Lo4;
183 // Inst{7} = Hi1;
184 Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
185 return Value;
186 }
Shiva Chenc3d0e892018-05-30 01:16:36 +0000187 case RISCV::fixup_riscv_call: {
188 // Jalr will add UpperImm with the sign-extended 12-bit LowerImm,
189 // we need to add 0x800ULL before extract upper bits to reflect the
190 // effect of the sign extension.
191 uint64_t UpperImm = (Value + 0x800ULL) & 0xfffff000ULL;
192 uint64_t LowerImm = Value & 0xfffULL;
193 return UpperImm | ((LowerImm << 20) << 32);
194 }
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000195 case RISCV::fixup_riscv_rvc_jump: {
196 // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
197 unsigned Bit11 = (Value >> 11) & 0x1;
198 unsigned Bit4 = (Value >> 4) & 0x1;
199 unsigned Bit9_8 = (Value >> 8) & 0x3;
200 unsigned Bit10 = (Value >> 10) & 0x1;
201 unsigned Bit6 = (Value >> 6) & 0x1;
202 unsigned Bit7 = (Value >> 7) & 0x1;
203 unsigned Bit3_1 = (Value >> 1) & 0x7;
204 unsigned Bit5 = (Value >> 5) & 0x1;
205 Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
206 (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
207 return Value;
208 }
209 case RISCV::fixup_riscv_rvc_branch: {
210 // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
211 unsigned Bit8 = (Value >> 8) & 0x1;
212 unsigned Bit7_6 = (Value >> 6) & 0x3;
213 unsigned Bit5 = (Value >> 5) & 0x1;
214 unsigned Bit4_3 = (Value >> 3) & 0x3;
215 unsigned Bit2_1 = (Value >> 1) & 0x3;
216 Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
217 (Bit5 << 2);
218 return Value;
219 }
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000220
221 }
222}
223
Rafael Espindola801b42d2017-06-23 22:52:36 +0000224void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
225 const MCValue &Target,
Rafael Espindola88d9e372017-06-21 23:06:53 +0000226 MutableArrayRef<char> Data, uint64_t Value,
Ilya Biryukov3c9c1062018-06-06 10:57:50 +0000227 bool IsResolved,
228 const MCSubtargetInfo *STI) const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000229 MCContext &Ctx = Asm.getContext();
Mandeep Singh Grang5f043ae2017-11-10 19:09:28 +0000230 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000231 if (!Value)
232 return; // Doesn't change encoding.
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000233 // Apply any target-specific value adjustments.
234 Value = adjustFixupValue(Fixup, Value, Ctx);
235
236 // Shift the value into position.
237 Value <<= Info.TargetOffset;
238
239 unsigned Offset = Fixup.getOffset();
Alex Bradbury1c010d02018-05-23 10:53:56 +0000240 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
Mandeep Singh Grang5f043ae2017-11-10 19:09:28 +0000241
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000242 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
243
244 // For each byte of the fragment that the fixup touches, mask in the
245 // bits from the fixup value.
Alex Bradbury1c010d02018-05-23 10:53:56 +0000246 for (unsigned i = 0; i != NumBytes; ++i) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000247 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
248 }
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000249}
250
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000251std::unique_ptr<MCObjectTargetWriter>
252RISCVAsmBackend::createObjectTargetWriter() const {
253 return createRISCVELFObjectWriter(OSABI, Is64Bit);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000254}
255
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000256MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +0000257 const MCSubtargetInfo &STI,
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000258 const MCRegisterInfo &MRI,
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000259 const MCTargetOptions &Options) {
Alex Bradburyb22f7512018-01-03 08:53:05 +0000260 const Triple &TT = STI.getTargetTriple();
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000261 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Alex Bradburyd93f8892018-01-17 14:17:12 +0000262 return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit());
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000263}