| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 1 | //===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the RISCVMCCodeEmitter class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/RISCVFixupKinds.h" |
| 14 | #include "MCTargetDesc/RISCVMCExpr.h" |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/RISCVMCTargetDesc.h" |
| Ana Pazos | 9d6c553 | 2018-10-04 21:50:54 +0000 | [diff] [blame] | 16 | #include "Utils/RISCVBaseInfo.h" |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Statistic.h" |
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmInfo.h" |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCCodeEmitter.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCInst.h" |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCInstBuilder.h" |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCRegisterInfo.h" |
| 26 | #include "llvm/MC/MCSymbol.h" |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Casting.h" |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 28 | #include "llvm/Support/EndianStream.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "mccodeemitter" |
| 34 | |
| 35 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 36 | STATISTIC(MCNumFixups, "Number of MC fixups created"); |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | class RISCVMCCodeEmitter : public MCCodeEmitter { |
| 40 | RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete; |
| 41 | void operator=(const RISCVMCCodeEmitter &) = delete; |
| 42 | MCContext &Ctx; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 43 | MCInstrInfo const &MCII; |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 46 | RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII) |
| 47 | : Ctx(ctx), MCII(MCII) {} |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 48 | |
| 49 | ~RISCVMCCodeEmitter() override {} |
| 50 | |
| 51 | void encodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 52 | SmallVectorImpl<MCFixup> &Fixups, |
| 53 | const MCSubtargetInfo &STI) const override; |
| 54 | |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 55 | void expandFunctionCall(const MCInst &MI, raw_ostream &OS, |
| 56 | SmallVectorImpl<MCFixup> &Fixups, |
| 57 | const MCSubtargetInfo &STI) const; |
| 58 | |
| Lewis Revill | aa79a3f | 2019-04-04 14:13:37 +0000 | [diff] [blame] | 59 | void expandAddTPRel(const MCInst &MI, raw_ostream &OS, |
| 60 | SmallVectorImpl<MCFixup> &Fixups, |
| 61 | const MCSubtargetInfo &STI) const; |
| 62 | |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 63 | /// TableGen'erated function for getting the binary encoding for an |
| 64 | /// instruction. |
| 65 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
| 66 | SmallVectorImpl<MCFixup> &Fixups, |
| 67 | const MCSubtargetInfo &STI) const; |
| 68 | |
| 69 | /// Return binary encoding of operand. If the machine operand requires |
| 70 | /// relocation, record the relocation and return zero. |
| 71 | unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 72 | SmallVectorImpl<MCFixup> &Fixups, |
| 73 | const MCSubtargetInfo &STI) const; |
| Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 74 | |
| 75 | unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo, |
| 76 | SmallVectorImpl<MCFixup> &Fixups, |
| 77 | const MCSubtargetInfo &STI) const; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 78 | |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 79 | unsigned getImmOpValue(const MCInst &MI, unsigned OpNo, |
| 80 | SmallVectorImpl<MCFixup> &Fixups, |
| 81 | const MCSubtargetInfo &STI) const; |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 82 | }; |
| 83 | } // end anonymous namespace |
| 84 | |
| 85 | MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII, |
| 86 | const MCRegisterInfo &MRI, |
| 87 | MCContext &Ctx) { |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 88 | return new RISCVMCCodeEmitter(Ctx, MCII); |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| Mandeep Singh Grang | ddcb956 | 2018-05-23 22:44:08 +0000 | [diff] [blame] | 91 | // Expand PseudoCALL and PseudoTAIL to AUIPC and JALR with relocation types. |
| 92 | // We expand PseudoCALL and PseudoTAIL while encoding, meaning AUIPC and JALR |
| 93 | // won't go through RISCV MC to MC compressed instruction transformation. This |
| 94 | // is acceptable because AUIPC has no 16-bit form and C_JALR have no immediate |
| 95 | // operand field. We let linker relaxation deal with it. When linker |
| 96 | // relaxation enabled, AUIPC and JALR have chance relax to JAL. If C extension |
| 97 | // is enabled, JAL has chance relax to C_JAL. |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 98 | void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS, |
| 99 | SmallVectorImpl<MCFixup> &Fixups, |
| 100 | const MCSubtargetInfo &STI) const { |
| 101 | MCInst TmpInst; |
| 102 | MCOperand Func = MI.getOperand(0); |
| Mandeep Singh Grang | ef0ebf2 | 2018-05-17 17:31:27 +0000 | [diff] [blame] | 103 | unsigned Ra = (MI.getOpcode() == RISCV::PseudoTAIL) ? RISCV::X6 : RISCV::X1; |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 104 | uint32_t Binary; |
| 105 | |
| 106 | assert(Func.isExpr() && "Expected expression"); |
| 107 | |
| Alex Bradbury | 44668ae | 2019-04-01 14:53:17 +0000 | [diff] [blame] | 108 | const MCExpr *CallExpr = Func.getExpr(); |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 109 | |
| 110 | // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type. |
| 111 | TmpInst = MCInstBuilder(RISCV::AUIPC) |
| 112 | .addReg(Ra) |
| 113 | .addOperand(MCOperand::createExpr(CallExpr)); |
| 114 | Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI); |
| Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 115 | support::endian::write(OS, Binary, support::little); |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 116 | |
| Sameer AbuAsal | e01e711 | 2018-06-21 14:37:09 +0000 | [diff] [blame] | 117 | if (MI.getOpcode() == RISCV::PseudoTAIL) |
| 118 | // Emit JALR X0, X6, 0 |
| 119 | TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0); |
| 120 | else |
| 121 | // Emit JALR X1, X1, 0 |
| 122 | TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0); |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 123 | Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI); |
| Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 124 | support::endian::write(OS, Binary, support::little); |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| Lewis Revill | aa79a3f | 2019-04-04 14:13:37 +0000 | [diff] [blame] | 127 | // Expand PseudoAddTPRel to a simple ADD with the correct relocation. |
| 128 | void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI, raw_ostream &OS, |
| 129 | SmallVectorImpl<MCFixup> &Fixups, |
| 130 | const MCSubtargetInfo &STI) const { |
| 131 | MCOperand DestReg = MI.getOperand(0); |
| 132 | MCOperand SrcReg = MI.getOperand(1); |
| 133 | MCOperand TPReg = MI.getOperand(2); |
| 134 | assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 && |
| 135 | "Expected thread pointer as second input to TP-relative add"); |
| 136 | |
| 137 | MCOperand SrcSymbol = MI.getOperand(3); |
| 138 | assert(SrcSymbol.isExpr() && |
| 139 | "Expected expression as third input to TP-relative add"); |
| 140 | |
| 141 | const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr()); |
| 142 | assert(Expr && Expr->getKind() == RISCVMCExpr::VK_RISCV_TPREL_ADD && |
| 143 | "Expected tprel_add relocation on TP-relative symbol"); |
| 144 | |
| 145 | // Emit the correct tprel_add relocation for the symbol. |
| 146 | Fixups.push_back(MCFixup::create( |
| 147 | 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc())); |
| 148 | |
| 149 | // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled. |
| 150 | if (STI.getFeatureBits()[RISCV::FeatureRelax]) { |
| 151 | const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx); |
| 152 | Fixups.push_back(MCFixup::create( |
| 153 | 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc())); |
| 154 | } |
| 155 | |
| 156 | // Emit a normal ADD instruction with the given operands. |
| 157 | MCInst TmpInst = MCInstBuilder(RISCV::ADD) |
| 158 | .addOperand(DestReg) |
| 159 | .addOperand(SrcReg) |
| 160 | .addOperand(TPReg); |
| 161 | uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI); |
| 162 | support::endian::write(OS, Binary, support::little); |
| 163 | } |
| 164 | |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 165 | void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 166 | SmallVectorImpl<MCFixup> &Fixups, |
| 167 | const MCSubtargetInfo &STI) const { |
| Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 168 | const MCInstrDesc &Desc = MCII.get(MI.getOpcode()); |
| 169 | // Get byte count of instruction. |
| 170 | unsigned Size = Desc.getSize(); |
| 171 | |
| Mandeep Singh Grang | ef0ebf2 | 2018-05-17 17:31:27 +0000 | [diff] [blame] | 172 | if (MI.getOpcode() == RISCV::PseudoCALL || |
| 173 | MI.getOpcode() == RISCV::PseudoTAIL) { |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 174 | expandFunctionCall(MI, OS, Fixups, STI); |
| 175 | MCNumEmitted += 2; |
| 176 | return; |
| 177 | } |
| 178 | |
| Lewis Revill | aa79a3f | 2019-04-04 14:13:37 +0000 | [diff] [blame] | 179 | if (MI.getOpcode() == RISCV::PseudoAddTPRel) { |
| 180 | expandAddTPRel(MI, OS, Fixups, STI); |
| 181 | MCNumEmitted += 1; |
| 182 | return; |
| 183 | } |
| 184 | |
| Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 185 | switch (Size) { |
| 186 | default: |
| 187 | llvm_unreachable("Unhandled encodeInstruction length!"); |
| 188 | case 2: { |
| 189 | uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); |
| Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 190 | support::endian::write<uint16_t>(OS, Bits, support::little); |
| Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | case 4: { |
| 194 | uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); |
| Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 195 | support::endian::write(OS, Bits, support::little); |
| Alex Bradbury | 9f6aec4 | 2017-12-07 12:50:32 +0000 | [diff] [blame] | 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 200 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
| 201 | } |
| 202 | |
| 203 | unsigned |
| 204 | RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 205 | SmallVectorImpl<MCFixup> &Fixups, |
| 206 | const MCSubtargetInfo &STI) const { |
| 207 | |
| 208 | if (MO.isReg()) |
| 209 | return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg()); |
| 210 | |
| 211 | if (MO.isImm()) |
| 212 | return static_cast<unsigned>(MO.getImm()); |
| 213 | |
| 214 | llvm_unreachable("Unhandled expression!"); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 218 | unsigned |
| 219 | RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo, |
| 220 | SmallVectorImpl<MCFixup> &Fixups, |
| 221 | const MCSubtargetInfo &STI) const { |
| 222 | const MCOperand &MO = MI.getOperand(OpNo); |
| 223 | |
| 224 | if (MO.isImm()) { |
| 225 | unsigned Res = MO.getImm(); |
| 226 | assert((Res & 1) == 0 && "LSB is non-zero"); |
| 227 | return Res >> 1; |
| 228 | } |
| 229 | |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 230 | return getImmOpValue(MI, OpNo, Fixups, STI); |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo, |
| 234 | SmallVectorImpl<MCFixup> &Fixups, |
| 235 | const MCSubtargetInfo &STI) const { |
| Shiva Chen | 43bfe84 | 2018-05-24 06:21:23 +0000 | [diff] [blame] | 236 | bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax]; |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 237 | const MCOperand &MO = MI.getOperand(OpNo); |
| 238 | |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 239 | MCInstrDesc const &Desc = MCII.get(MI.getOpcode()); |
| 240 | unsigned MIFrm = Desc.TSFlags & RISCVII::InstFormatMask; |
| 241 | |
| Chih-Mao Chen | 5d94b25 | 2018-08-14 08:08:39 +0000 | [diff] [blame] | 242 | // If the destination is an immediate, there is nothing to do. |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 243 | if (MO.isImm()) |
| 244 | return MO.getImm(); |
| 245 | |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 246 | assert(MO.isExpr() && |
| 247 | "getImmOpValue expects only expressions or immediates"); |
| 248 | const MCExpr *Expr = MO.getExpr(); |
| 249 | MCExpr::ExprKind Kind = Expr->getKind(); |
| 250 | RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid; |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 251 | bool RelaxCandidate = false; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 252 | if (Kind == MCExpr::Target) { |
| 253 | const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr); |
| 254 | |
| 255 | switch (RVExpr->getKind()) { |
| 256 | case RISCVMCExpr::VK_RISCV_None: |
| 257 | case RISCVMCExpr::VK_RISCV_Invalid: |
| 258 | llvm_unreachable("Unhandled fixup kind!"); |
| Lewis Revill | aa79a3f | 2019-04-04 14:13:37 +0000 | [diff] [blame] | 259 | case RISCVMCExpr::VK_RISCV_TPREL_ADD: |
| 260 | // tprel_add is only used to indicate that a relocation should be emitted |
| 261 | // for an add instruction used in TP-relative addressing. It should not be |
| 262 | // expanded as if representing an actual instruction operand and so to |
| 263 | // encounter it here is an error. |
| 264 | llvm_unreachable( |
| 265 | "VK_RISCV_TPREL_ADD should not represent an instruction operand"); |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 266 | case RISCVMCExpr::VK_RISCV_LO: |
| Alex Bradbury | 8d8d0a7 | 2018-02-22 13:24:25 +0000 | [diff] [blame] | 267 | if (MIFrm == RISCVII::InstFormatI) |
| 268 | FixupKind = RISCV::fixup_riscv_lo12_i; |
| 269 | else if (MIFrm == RISCVII::InstFormatS) |
| 270 | FixupKind = RISCV::fixup_riscv_lo12_s; |
| 271 | else |
| 272 | llvm_unreachable("VK_RISCV_LO used with unexpected instruction format"); |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 273 | RelaxCandidate = true; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 274 | break; |
| 275 | case RISCVMCExpr::VK_RISCV_HI: |
| 276 | FixupKind = RISCV::fixup_riscv_hi20; |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 277 | RelaxCandidate = true; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 278 | break; |
| Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 279 | case RISCVMCExpr::VK_RISCV_PCREL_LO: |
| Alex Bradbury | 8d8d0a7 | 2018-02-22 13:24:25 +0000 | [diff] [blame] | 280 | if (MIFrm == RISCVII::InstFormatI) |
| 281 | FixupKind = RISCV::fixup_riscv_pcrel_lo12_i; |
| 282 | else if (MIFrm == RISCVII::InstFormatS) |
| 283 | FixupKind = RISCV::fixup_riscv_pcrel_lo12_s; |
| 284 | else |
| 285 | llvm_unreachable( |
| 286 | "VK_RISCV_PCREL_LO used with unexpected instruction format"); |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 287 | RelaxCandidate = true; |
| Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 288 | break; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 289 | case RISCVMCExpr::VK_RISCV_PCREL_HI: |
| 290 | FixupKind = RISCV::fixup_riscv_pcrel_hi20; |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 291 | RelaxCandidate = true; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 292 | break; |
| Alex Bradbury | 8eb87e5 | 2019-02-15 09:43:46 +0000 | [diff] [blame] | 293 | case RISCVMCExpr::VK_RISCV_GOT_HI: |
| 294 | FixupKind = RISCV::fixup_riscv_got_hi20; |
| 295 | break; |
| Lewis Revill | aa79a3f | 2019-04-04 14:13:37 +0000 | [diff] [blame] | 296 | case RISCVMCExpr::VK_RISCV_TPREL_LO: |
| 297 | if (MIFrm == RISCVII::InstFormatI) |
| 298 | FixupKind = RISCV::fixup_riscv_tprel_lo12_i; |
| 299 | else if (MIFrm == RISCVII::InstFormatS) |
| 300 | FixupKind = RISCV::fixup_riscv_tprel_lo12_s; |
| 301 | else |
| 302 | llvm_unreachable( |
| 303 | "VK_RISCV_TPREL_LO used with unexpected instruction format"); |
| 304 | RelaxCandidate = true; |
| 305 | break; |
| 306 | case RISCVMCExpr::VK_RISCV_TPREL_HI: |
| 307 | FixupKind = RISCV::fixup_riscv_tprel_hi20; |
| 308 | RelaxCandidate = true; |
| 309 | break; |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 310 | case RISCVMCExpr::VK_RISCV_CALL: |
| 311 | FixupKind = RISCV::fixup_riscv_call; |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 312 | RelaxCandidate = true; |
| Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 313 | break; |
| Alex Bradbury | f8078f6 | 2019-04-02 12:47:20 +0000 | [diff] [blame] | 314 | case RISCVMCExpr::VK_RISCV_CALL_PLT: |
| 315 | FixupKind = RISCV::fixup_riscv_call_plt; |
| 316 | RelaxCandidate = true; |
| 317 | break; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 318 | } |
| 319 | } else if (Kind == MCExpr::SymbolRef && |
| 320 | cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) { |
| 321 | if (Desc.getOpcode() == RISCV::JAL) { |
| 322 | FixupKind = RISCV::fixup_riscv_jal; |
| Alex Bradbury | ee7c7ec | 2017-10-19 14:29:03 +0000 | [diff] [blame] | 323 | } else if (MIFrm == RISCVII::InstFormatB) { |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 324 | FixupKind = RISCV::fixup_riscv_branch; |
| Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 325 | } else if (MIFrm == RISCVII::InstFormatCJ) { |
| 326 | FixupKind = RISCV::fixup_riscv_rvc_jump; |
| 327 | } else if (MIFrm == RISCVII::InstFormatCB) { |
| 328 | FixupKind = RISCV::fixup_riscv_rvc_branch; |
| Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!"); |
| 333 | |
| 334 | Fixups.push_back( |
| 335 | MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc())); |
| 336 | ++MCNumFixups; |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 337 | |
| Kito Cheng | 5e8798f | 2019-01-21 05:27:09 +0000 | [diff] [blame] | 338 | // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is |
| 339 | // enabled and the current fixup will result in a relocation that may be |
| 340 | // relaxed. |
| 341 | if (EnableRelax && RelaxCandidate) { |
| 342 | const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx); |
| 343 | Fixups.push_back( |
| 344 | MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), |
| 345 | MI.getLoc())); |
| 346 | ++MCNumFixups; |
| Shiva Chen | 43bfe84 | 2018-05-24 06:21:23 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| Alex Bradbury | 8ab4a96 | 2017-09-17 14:36:28 +0000 | [diff] [blame] | 349 | return 0; |
| Alex Bradbury | 6758ecb | 2017-09-17 14:27:35 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 352 | #include "RISCVGenMCCodeEmitter.inc" |