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