Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 1 | //===-- 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 Bradbury | 9c03e4c | 2018-11-12 14:25:07 +0000 | [diff] [blame^] | 10 | #include "RISCVAsmBackend.h" |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/APInt.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAssembler.h" |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCDirectives.h" |
| 15 | #include "llvm/MC/MCELFObjectWriter.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCExpr.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectWriter.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCSymbol.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
Shiva Chen | 6e07dfb | 2018-05-18 06:42:21 +0000 | [diff] [blame] | 24 | bool 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 AbuAsal | 2646a41 | 2018-03-02 22:04:12 +0000 | [diff] [blame] | 37 | 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 | |
| 52 | void 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. |
| 90 | unsigned 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 Biryukov | 3c9c106 | 2018-06-06 10:57:50 +0000 | [diff] [blame] | 104 | bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst, |
| 105 | const MCSubtargetInfo &STI) const { |
Sameer AbuAsal | 2646a41 | 2018-03-02 22:04:12 +0000 | [diff] [blame] | 106 | return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode(); |
| 107 | } |
| 108 | |
Peter Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 109 | bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const { |
Alex Bradbury | d93f889 | 2018-01-17 14:17:12 +0000 | [diff] [blame] | 110 | bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC]; |
| 111 | unsigned MinNopLen = HasStdExtC ? 2 : 4; |
| 112 | |
| 113 | if ((Count % MinNopLen) != 0) |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 114 | return false; |
| 115 | |
Alex Bradbury | d93f889 | 2018-01-17 14:17:12 +0000 | [diff] [blame] | 116 | // 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 Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 119 | OS.write("\x13\0\0\0", 4); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 120 | |
Alex Bradbury | d93f889 | 2018-01-17 14:17:12 +0000 | [diff] [blame] | 121 | // 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 Collingbourne | 571a330 | 2018-05-21 17:57:19 +0000 | [diff] [blame] | 125 | OS.write("\x01\0", 2); |
Alex Bradbury | d93f889 | 2018-01-17 14:17:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 131 | static 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 Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 143 | case RISCV::fixup_riscv_pcrel_lo12_i: |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 144 | return Value & 0xfff; |
| 145 | case RISCV::fixup_riscv_lo12_s: |
Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 146 | case RISCV::fixup_riscv_pcrel_lo12_s: |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 147 | 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 Chen | c3d0e89 | 2018-05-30 01:16:36 +0000 | [diff] [blame] | 187 | 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 Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 195 | 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 Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 220 | |
| 221 | } |
| 222 | } |
| 223 | |
Rafael Espindola | 801b42d | 2017-06-23 22:52:36 +0000 | [diff] [blame] | 224 | void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
| 225 | const MCValue &Target, |
Rafael Espindola | 88d9e37 | 2017-06-21 23:06:53 +0000 | [diff] [blame] | 226 | MutableArrayRef<char> Data, uint64_t Value, |
Ilya Biryukov | 3c9c106 | 2018-06-06 10:57:50 +0000 | [diff] [blame] | 227 | bool IsResolved, |
| 228 | const MCSubtargetInfo *STI) const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 229 | MCContext &Ctx = Asm.getContext(); |
Mandeep Singh Grang | 5f043ae | 2017-11-10 19:09:28 +0000 | [diff] [blame] | 230 | MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind()); |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 231 | if (!Value) |
| 232 | return; // Doesn't change encoding. |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 233 | // 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 Bradbury | 1c010d0 | 2018-05-23 10:53:56 +0000 | [diff] [blame] | 240 | unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8; |
Mandeep Singh Grang | 5f043ae | 2017-11-10 19:09:28 +0000 | [diff] [blame] | 241 | |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 242 | 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 Bradbury | 1c010d0 | 2018-05-23 10:53:56 +0000 | [diff] [blame] | 246 | for (unsigned i = 0; i != NumBytes; ++i) { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 247 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
| 248 | } |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 251 | std::unique_ptr<MCObjectTargetWriter> |
| 252 | RISCVAsmBackend::createObjectTargetWriter() const { |
| 253 | return createRISCVELFObjectWriter(OSABI, Is64Bit); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 256 | MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T, |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 257 | const MCSubtargetInfo &STI, |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 258 | const MCRegisterInfo &MRI, |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 259 | const MCTargetOptions &Options) { |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 260 | const Triple &TT = STI.getTargetTriple(); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 261 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS()); |
Alex Bradbury | d93f889 | 2018-01-17 14:17:12 +0000 | [diff] [blame] | 262 | return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit()); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 263 | } |