16b2cca7fSAlex Bradbury //===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===//
26b2cca7fSAlex Bradbury //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66b2cca7fSAlex Bradbury //
76b2cca7fSAlex Bradbury //===----------------------------------------------------------------------===//
86b2cca7fSAlex Bradbury //
96b2cca7fSAlex Bradbury // This file implements the RISCVMCCodeEmitter class.
106b2cca7fSAlex Bradbury //
116b2cca7fSAlex Bradbury //===----------------------------------------------------------------------===//
126b2cca7fSAlex Bradbury
13387d3c24SCraig Topper #include "MCTargetDesc/RISCVBaseInfo.h"
149d3f1250SAlex Bradbury #include "MCTargetDesc/RISCVFixupKinds.h"
159d3f1250SAlex Bradbury #include "MCTargetDesc/RISCVMCExpr.h"
166b2cca7fSAlex Bradbury #include "MCTargetDesc/RISCVMCTargetDesc.h"
176b2cca7fSAlex Bradbury #include "llvm/ADT/Statistic.h"
186bda14b3SChandler Carruth #include "llvm/MC/MCAsmInfo.h"
196b2cca7fSAlex Bradbury #include "llvm/MC/MCCodeEmitter.h"
206b2cca7fSAlex Bradbury #include "llvm/MC/MCContext.h"
216b2cca7fSAlex Bradbury #include "llvm/MC/MCExpr.h"
226b2cca7fSAlex Bradbury #include "llvm/MC/MCInst.h"
2398f9389fSShiva Chen #include "llvm/MC/MCInstBuilder.h"
249d3f1250SAlex Bradbury #include "llvm/MC/MCInstrInfo.h"
256b2cca7fSAlex Bradbury #include "llvm/MC/MCRegisterInfo.h"
26ef736a1cSserge-sans-paille #include "llvm/MC/MCSubtargetInfo.h"
276b2cca7fSAlex Bradbury #include "llvm/MC/MCSymbol.h"
289d3f1250SAlex Bradbury #include "llvm/Support/Casting.h"
296b2cca7fSAlex Bradbury #include "llvm/Support/EndianStream.h"
306b2cca7fSAlex Bradbury #include "llvm/Support/raw_ostream.h"
316b2cca7fSAlex Bradbury
326b2cca7fSAlex Bradbury using namespace llvm;
336b2cca7fSAlex Bradbury
346b2cca7fSAlex Bradbury #define DEBUG_TYPE "mccodeemitter"
356b2cca7fSAlex Bradbury
366b2cca7fSAlex Bradbury STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
379d3f1250SAlex Bradbury STATISTIC(MCNumFixups, "Number of MC fixups created");
386b2cca7fSAlex Bradbury
396b2cca7fSAlex Bradbury namespace {
406b2cca7fSAlex Bradbury class RISCVMCCodeEmitter : public MCCodeEmitter {
416b2cca7fSAlex Bradbury RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
426b2cca7fSAlex Bradbury void operator=(const RISCVMCCodeEmitter &) = delete;
436b2cca7fSAlex Bradbury MCContext &Ctx;
449d3f1250SAlex Bradbury MCInstrInfo const &MCII;
456b2cca7fSAlex Bradbury
466b2cca7fSAlex Bradbury public:
RISCVMCCodeEmitter(MCContext & ctx,MCInstrInfo const & MCII)479d3f1250SAlex Bradbury RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
489d3f1250SAlex Bradbury : Ctx(ctx), MCII(MCII) {}
496b2cca7fSAlex Bradbury
503a3cb929SKazu Hirata ~RISCVMCCodeEmitter() override = default;
516b2cca7fSAlex Bradbury
526b2cca7fSAlex Bradbury void encodeInstruction(const MCInst &MI, raw_ostream &OS,
536b2cca7fSAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
546b2cca7fSAlex Bradbury const MCSubtargetInfo &STI) const override;
556b2cca7fSAlex Bradbury
5698f9389fSShiva Chen void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
5798f9389fSShiva Chen SmallVectorImpl<MCFixup> &Fixups,
5898f9389fSShiva Chen const MCSubtargetInfo &STI) const;
5998f9389fSShiva Chen
60aa79a3feSLewis Revill void expandAddTPRel(const MCInst &MI, raw_ostream &OS,
61aa79a3feSLewis Revill SmallVectorImpl<MCFixup> &Fixups,
62aa79a3feSLewis Revill const MCSubtargetInfo &STI) const;
63aa79a3feSLewis Revill
646b2cca7fSAlex Bradbury /// TableGen'erated function for getting the binary encoding for an
656b2cca7fSAlex Bradbury /// instruction.
666b2cca7fSAlex Bradbury uint64_t getBinaryCodeForInstr(const MCInst &MI,
676b2cca7fSAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
686b2cca7fSAlex Bradbury const MCSubtargetInfo &STI) const;
696b2cca7fSAlex Bradbury
706b2cca7fSAlex Bradbury /// Return binary encoding of operand. If the machine operand requires
716b2cca7fSAlex Bradbury /// relocation, record the relocation and return zero.
726b2cca7fSAlex Bradbury unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
736b2cca7fSAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
746b2cca7fSAlex Bradbury const MCSubtargetInfo &STI) const;
756758ecb9SAlex Bradbury
766758ecb9SAlex Bradbury unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
776758ecb9SAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
786758ecb9SAlex Bradbury const MCSubtargetInfo &STI) const;
799d3f1250SAlex Bradbury
808ab4a969SAlex Bradbury unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
818ab4a969SAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
828ab4a969SAlex Bradbury const MCSubtargetInfo &STI) const;
8366da87dcSHsiangkai Wang
8466da87dcSHsiangkai Wang unsigned getVMaskReg(const MCInst &MI, unsigned OpNo,
8566da87dcSHsiangkai Wang SmallVectorImpl<MCFixup> &Fixups,
8666da87dcSHsiangkai Wang const MCSubtargetInfo &STI) const;
876b2cca7fSAlex Bradbury };
886b2cca7fSAlex Bradbury } // end anonymous namespace
896b2cca7fSAlex Bradbury
createRISCVMCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)906b2cca7fSAlex Bradbury MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
916b2cca7fSAlex Bradbury MCContext &Ctx) {
929d3f1250SAlex Bradbury return new RISCVMCCodeEmitter(Ctx, MCII);
936b2cca7fSAlex Bradbury }
946b2cca7fSAlex Bradbury
9524cba331SLuís Marques // Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
96e5ba52dcSNate Voorhies // relocation types. We expand those pseudo-instructions while encoding them,
97e5ba52dcSNate Voorhies // meaning AUIPC and JALR won't go through RISCV MC to MC compressed
98e5ba52dcSNate Voorhies // instruction transformation. This is acceptable because AUIPC has no 16-bit
99e5ba52dcSNate Voorhies // form and C_JALR has no immediate operand field. We let linker relaxation
100e5ba52dcSNate Voorhies // deal with it. When linker relaxation is enabled, AUIPC and JALR have a
101e5ba52dcSNate Voorhies // chance to relax to JAL.
102e5ba52dcSNate Voorhies // If the C extension is enabled, JAL has a chance relax to C_JAL.
expandFunctionCall(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const10398f9389fSShiva Chen void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
10498f9389fSShiva Chen SmallVectorImpl<MCFixup> &Fixups,
10598f9389fSShiva Chen const MCSubtargetInfo &STI) const {
10698f9389fSShiva Chen MCInst TmpInst;
107cf748813SLewis Revill MCOperand Func;
1080add5f91SCraig Topper MCRegister Ra;
109cf748813SLewis Revill if (MI.getOpcode() == RISCV::PseudoTAIL) {
110cf748813SLewis Revill Func = MI.getOperand(0);
111cf748813SLewis Revill Ra = RISCV::X6;
112cf748813SLewis Revill } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
113cf748813SLewis Revill Func = MI.getOperand(1);
114cf748813SLewis Revill Ra = MI.getOperand(0).getReg();
11524cba331SLuís Marques } else if (MI.getOpcode() == RISCV::PseudoCALL) {
116cf748813SLewis Revill Func = MI.getOperand(0);
117cf748813SLewis Revill Ra = RISCV::X1;
11824cba331SLuís Marques } else if (MI.getOpcode() == RISCV::PseudoJump) {
11924cba331SLuís Marques Func = MI.getOperand(1);
12024cba331SLuís Marques Ra = MI.getOperand(0).getReg();
121cf748813SLewis Revill }
12298f9389fSShiva Chen uint32_t Binary;
12398f9389fSShiva Chen
12498f9389fSShiva Chen assert(Func.isExpr() && "Expected expression");
12598f9389fSShiva Chen
12644668ae7SAlex Bradbury const MCExpr *CallExpr = Func.getExpr();
12798f9389fSShiva Chen
12898f9389fSShiva Chen // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
129*d0828c5aSwangpc TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
13098f9389fSShiva Chen Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
131e3f65297SPeter Collingbourne support::endian::write(OS, Binary, support::little);
13298f9389fSShiva Chen
13324cba331SLuís Marques if (MI.getOpcode() == RISCV::PseudoTAIL ||
13424cba331SLuís Marques MI.getOpcode() == RISCV::PseudoJump)
13524cba331SLuís Marques // Emit JALR X0, Ra, 0
136e01e711cSSameer AbuAsal TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
137e01e711cSSameer AbuAsal else
138cf748813SLewis Revill // Emit JALR Ra, Ra, 0
13998f9389fSShiva Chen TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
14098f9389fSShiva Chen Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
141e3f65297SPeter Collingbourne support::endian::write(OS, Binary, support::little);
14298f9389fSShiva Chen }
14398f9389fSShiva Chen
144aa79a3feSLewis Revill // Expand PseudoAddTPRel to a simple ADD with the correct relocation.
expandAddTPRel(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const145aa79a3feSLewis Revill void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI, raw_ostream &OS,
146aa79a3feSLewis Revill SmallVectorImpl<MCFixup> &Fixups,
147aa79a3feSLewis Revill const MCSubtargetInfo &STI) const {
148aa79a3feSLewis Revill MCOperand DestReg = MI.getOperand(0);
149aa79a3feSLewis Revill MCOperand SrcReg = MI.getOperand(1);
150aa79a3feSLewis Revill MCOperand TPReg = MI.getOperand(2);
151aa79a3feSLewis Revill assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
152aa79a3feSLewis Revill "Expected thread pointer as second input to TP-relative add");
153aa79a3feSLewis Revill
154aa79a3feSLewis Revill MCOperand SrcSymbol = MI.getOperand(3);
155aa79a3feSLewis Revill assert(SrcSymbol.isExpr() &&
156aa79a3feSLewis Revill "Expected expression as third input to TP-relative add");
157aa79a3feSLewis Revill
158aa79a3feSLewis Revill const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
159aa79a3feSLewis Revill assert(Expr && Expr->getKind() == RISCVMCExpr::VK_RISCV_TPREL_ADD &&
160aa79a3feSLewis Revill "Expected tprel_add relocation on TP-relative symbol");
161aa79a3feSLewis Revill
162aa79a3feSLewis Revill // Emit the correct tprel_add relocation for the symbol.
163aa79a3feSLewis Revill Fixups.push_back(MCFixup::create(
164aa79a3feSLewis Revill 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc()));
165aa79a3feSLewis Revill
166aa79a3feSLewis Revill // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled.
167aa79a3feSLewis Revill if (STI.getFeatureBits()[RISCV::FeatureRelax]) {
168aa79a3feSLewis Revill const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
169aa79a3feSLewis Revill Fixups.push_back(MCFixup::create(
170aa79a3feSLewis Revill 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc()));
171aa79a3feSLewis Revill }
172aa79a3feSLewis Revill
173aa79a3feSLewis Revill // Emit a normal ADD instruction with the given operands.
174aa79a3feSLewis Revill MCInst TmpInst = MCInstBuilder(RISCV::ADD)
175aa79a3feSLewis Revill .addOperand(DestReg)
176aa79a3feSLewis Revill .addOperand(SrcReg)
177aa79a3feSLewis Revill .addOperand(TPReg);
178aa79a3feSLewis Revill uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
179aa79a3feSLewis Revill support::endian::write(OS, Binary, support::little);
180aa79a3feSLewis Revill }
181aa79a3feSLewis Revill
encodeInstruction(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1826b2cca7fSAlex Bradbury void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
1836b2cca7fSAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
1846b2cca7fSAlex Bradbury const MCSubtargetInfo &STI) const {
1859f6aec4bSAlex Bradbury const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1869f6aec4bSAlex Bradbury // Get byte count of instruction.
1879f6aec4bSAlex Bradbury unsigned Size = Desc.getSize();
1889f6aec4bSAlex Bradbury
18906bd56d4SCraig Topper // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
19006bd56d4SCraig Topper // expanded instructions for each pseudo is correct in the Size field of the
19106bd56d4SCraig Topper // tablegen definition for the pseudo.
192cf748813SLewis Revill if (MI.getOpcode() == RISCV::PseudoCALLReg ||
193cf748813SLewis Revill MI.getOpcode() == RISCV::PseudoCALL ||
19424cba331SLuís Marques MI.getOpcode() == RISCV::PseudoTAIL ||
19524cba331SLuís Marques MI.getOpcode() == RISCV::PseudoJump) {
19698f9389fSShiva Chen expandFunctionCall(MI, OS, Fixups, STI);
19798f9389fSShiva Chen MCNumEmitted += 2;
19898f9389fSShiva Chen return;
19998f9389fSShiva Chen }
20098f9389fSShiva Chen
201aa79a3feSLewis Revill if (MI.getOpcode() == RISCV::PseudoAddTPRel) {
202aa79a3feSLewis Revill expandAddTPRel(MI, OS, Fixups, STI);
203aa79a3feSLewis Revill MCNumEmitted += 1;
204aa79a3feSLewis Revill return;
205aa79a3feSLewis Revill }
206aa79a3feSLewis Revill
2079f6aec4bSAlex Bradbury switch (Size) {
2089f6aec4bSAlex Bradbury default:
2099f6aec4bSAlex Bradbury llvm_unreachable("Unhandled encodeInstruction length!");
2109f6aec4bSAlex Bradbury case 2: {
2119f6aec4bSAlex Bradbury uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
212e3f65297SPeter Collingbourne support::endian::write<uint16_t>(OS, Bits, support::little);
2139f6aec4bSAlex Bradbury break;
2149f6aec4bSAlex Bradbury }
2159f6aec4bSAlex Bradbury case 4: {
2166b2cca7fSAlex Bradbury uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
217e3f65297SPeter Collingbourne support::endian::write(OS, Bits, support::little);
2189f6aec4bSAlex Bradbury break;
2199f6aec4bSAlex Bradbury }
2209f6aec4bSAlex Bradbury }
2219f6aec4bSAlex Bradbury
2226b2cca7fSAlex Bradbury ++MCNumEmitted; // Keep track of the # of mi's emitted.
2236b2cca7fSAlex Bradbury }
2246b2cca7fSAlex Bradbury
2256b2cca7fSAlex Bradbury unsigned
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const2266b2cca7fSAlex Bradbury RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
2276b2cca7fSAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
2286b2cca7fSAlex Bradbury const MCSubtargetInfo &STI) const {
2296b2cca7fSAlex Bradbury
2306b2cca7fSAlex Bradbury if (MO.isReg())
2316b2cca7fSAlex Bradbury return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
2326b2cca7fSAlex Bradbury
2336b2cca7fSAlex Bradbury if (MO.isImm())
2346b2cca7fSAlex Bradbury return static_cast<unsigned>(MO.getImm());
2356b2cca7fSAlex Bradbury
2366b2cca7fSAlex Bradbury llvm_unreachable("Unhandled expression!");
2376b2cca7fSAlex Bradbury return 0;
2386b2cca7fSAlex Bradbury }
2396b2cca7fSAlex Bradbury
2406758ecb9SAlex Bradbury unsigned
getImmOpValueAsr1(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const2416758ecb9SAlex Bradbury RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
2426758ecb9SAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
2436758ecb9SAlex Bradbury const MCSubtargetInfo &STI) const {
2446758ecb9SAlex Bradbury const MCOperand &MO = MI.getOperand(OpNo);
2456758ecb9SAlex Bradbury
2466758ecb9SAlex Bradbury if (MO.isImm()) {
2476758ecb9SAlex Bradbury unsigned Res = MO.getImm();
2486758ecb9SAlex Bradbury assert((Res & 1) == 0 && "LSB is non-zero");
2496758ecb9SAlex Bradbury return Res >> 1;
2506758ecb9SAlex Bradbury }
2516758ecb9SAlex Bradbury
2529d3f1250SAlex Bradbury return getImmOpValue(MI, OpNo, Fixups, STI);
2538ab4a969SAlex Bradbury }
2548ab4a969SAlex Bradbury
getImmOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const2558ab4a969SAlex Bradbury unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
2568ab4a969SAlex Bradbury SmallVectorImpl<MCFixup> &Fixups,
2578ab4a969SAlex Bradbury const MCSubtargetInfo &STI) const {
25843bfe844SShiva Chen bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax];
2598ab4a969SAlex Bradbury const MCOperand &MO = MI.getOperand(OpNo);
2608ab4a969SAlex Bradbury
2619d3f1250SAlex Bradbury MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
2623a64b708SEvandro Menezes unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
2639d3f1250SAlex Bradbury
2645d94b25fSChih-Mao Chen // If the destination is an immediate, there is nothing to do.
2658ab4a969SAlex Bradbury if (MO.isImm())
2668ab4a969SAlex Bradbury return MO.getImm();
2678ab4a969SAlex Bradbury
2689d3f1250SAlex Bradbury assert(MO.isExpr() &&
2699d3f1250SAlex Bradbury "getImmOpValue expects only expressions or immediates");
2709d3f1250SAlex Bradbury const MCExpr *Expr = MO.getExpr();
2719d3f1250SAlex Bradbury MCExpr::ExprKind Kind = Expr->getKind();
2729d3f1250SAlex Bradbury RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid;
2735e8798f9SKito Cheng bool RelaxCandidate = false;
2749d3f1250SAlex Bradbury if (Kind == MCExpr::Target) {
2759d3f1250SAlex Bradbury const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
2769d3f1250SAlex Bradbury
2779d3f1250SAlex Bradbury switch (RVExpr->getKind()) {
2789d3f1250SAlex Bradbury case RISCVMCExpr::VK_RISCV_None:
2799d3f1250SAlex Bradbury case RISCVMCExpr::VK_RISCV_Invalid:
2807cb3cd34SAlex Bradbury case RISCVMCExpr::VK_RISCV_32_PCREL:
2819d3f1250SAlex Bradbury llvm_unreachable("Unhandled fixup kind!");
282aa79a3feSLewis Revill case RISCVMCExpr::VK_RISCV_TPREL_ADD:
283aa79a3feSLewis Revill // tprel_add is only used to indicate that a relocation should be emitted
284aa79a3feSLewis Revill // for an add instruction used in TP-relative addressing. It should not be
285aa79a3feSLewis Revill // expanded as if representing an actual instruction operand and so to
286aa79a3feSLewis Revill // encounter it here is an error.
287aa79a3feSLewis Revill llvm_unreachable(
288aa79a3feSLewis Revill "VK_RISCV_TPREL_ADD should not represent an instruction operand");
2899d3f1250SAlex Bradbury case RISCVMCExpr::VK_RISCV_LO:
2908d8d0a73SAlex Bradbury if (MIFrm == RISCVII::InstFormatI)
2918d8d0a73SAlex Bradbury FixupKind = RISCV::fixup_riscv_lo12_i;
2928d8d0a73SAlex Bradbury else if (MIFrm == RISCVII::InstFormatS)
2938d8d0a73SAlex Bradbury FixupKind = RISCV::fixup_riscv_lo12_s;
2948d8d0a73SAlex Bradbury else
2958d8d0a73SAlex Bradbury llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
2965e8798f9SKito Cheng RelaxCandidate = true;
2979d3f1250SAlex Bradbury break;
2989d3f1250SAlex Bradbury case RISCVMCExpr::VK_RISCV_HI:
2999d3f1250SAlex Bradbury FixupKind = RISCV::fixup_riscv_hi20;
3005e8798f9SKito Cheng RelaxCandidate = true;
3019d3f1250SAlex Bradbury break;
302646ab87bSAhmed Charles case RISCVMCExpr::VK_RISCV_PCREL_LO:
3038d8d0a73SAlex Bradbury if (MIFrm == RISCVII::InstFormatI)
3048d8d0a73SAlex Bradbury FixupKind = RISCV::fixup_riscv_pcrel_lo12_i;
3058d8d0a73SAlex Bradbury else if (MIFrm == RISCVII::InstFormatS)
3068d8d0a73SAlex Bradbury FixupKind = RISCV::fixup_riscv_pcrel_lo12_s;
3078d8d0a73SAlex Bradbury else
3088d8d0a73SAlex Bradbury llvm_unreachable(
3098d8d0a73SAlex Bradbury "VK_RISCV_PCREL_LO used with unexpected instruction format");
3105e8798f9SKito Cheng RelaxCandidate = true;
311646ab87bSAhmed Charles break;
3129d3f1250SAlex Bradbury case RISCVMCExpr::VK_RISCV_PCREL_HI:
3139d3f1250SAlex Bradbury FixupKind = RISCV::fixup_riscv_pcrel_hi20;
3145e8798f9SKito Cheng RelaxCandidate = true;
3159d3f1250SAlex Bradbury break;
3168eb87e59SAlex Bradbury case RISCVMCExpr::VK_RISCV_GOT_HI:
3178eb87e59SAlex Bradbury FixupKind = RISCV::fixup_riscv_got_hi20;
3188eb87e59SAlex Bradbury break;
319aa79a3feSLewis Revill case RISCVMCExpr::VK_RISCV_TPREL_LO:
320aa79a3feSLewis Revill if (MIFrm == RISCVII::InstFormatI)
321aa79a3feSLewis Revill FixupKind = RISCV::fixup_riscv_tprel_lo12_i;
322aa79a3feSLewis Revill else if (MIFrm == RISCVII::InstFormatS)
323aa79a3feSLewis Revill FixupKind = RISCV::fixup_riscv_tprel_lo12_s;
324aa79a3feSLewis Revill else
325aa79a3feSLewis Revill llvm_unreachable(
326aa79a3feSLewis Revill "VK_RISCV_TPREL_LO used with unexpected instruction format");
327aa79a3feSLewis Revill RelaxCandidate = true;
328aa79a3feSLewis Revill break;
329aa79a3feSLewis Revill case RISCVMCExpr::VK_RISCV_TPREL_HI:
330aa79a3feSLewis Revill FixupKind = RISCV::fixup_riscv_tprel_hi20;
331aa79a3feSLewis Revill RelaxCandidate = true;
332aa79a3feSLewis Revill break;
333df3cb477SLewis Revill case RISCVMCExpr::VK_RISCV_TLS_GOT_HI:
334df3cb477SLewis Revill FixupKind = RISCV::fixup_riscv_tls_got_hi20;
335df3cb477SLewis Revill break;
336df3cb477SLewis Revill case RISCVMCExpr::VK_RISCV_TLS_GD_HI:
337df3cb477SLewis Revill FixupKind = RISCV::fixup_riscv_tls_gd_hi20;
338df3cb477SLewis Revill break;
33998f9389fSShiva Chen case RISCVMCExpr::VK_RISCV_CALL:
34098f9389fSShiva Chen FixupKind = RISCV::fixup_riscv_call;
3415e8798f9SKito Cheng RelaxCandidate = true;
34298f9389fSShiva Chen break;
343f8078f6bSAlex Bradbury case RISCVMCExpr::VK_RISCV_CALL_PLT:
344f8078f6bSAlex Bradbury FixupKind = RISCV::fixup_riscv_call_plt;
345f8078f6bSAlex Bradbury RelaxCandidate = true;
346f8078f6bSAlex Bradbury break;
3479d3f1250SAlex Bradbury }
3489d3f1250SAlex Bradbury } else if (Kind == MCExpr::SymbolRef &&
3499d3f1250SAlex Bradbury cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
35028387979SCraig Topper if (MIFrm == RISCVII::InstFormatJ) {
3519d3f1250SAlex Bradbury FixupKind = RISCV::fixup_riscv_jal;
352ee7c7ecdSAlex Bradbury } else if (MIFrm == RISCVII::InstFormatB) {
3539d3f1250SAlex Bradbury FixupKind = RISCV::fixup_riscv_branch;
354f8f4b905SAlex Bradbury } else if (MIFrm == RISCVII::InstFormatCJ) {
355f8f4b905SAlex Bradbury FixupKind = RISCV::fixup_riscv_rvc_jump;
356f8f4b905SAlex Bradbury } else if (MIFrm == RISCVII::InstFormatCB) {
357f8f4b905SAlex Bradbury FixupKind = RISCV::fixup_riscv_rvc_branch;
3589d3f1250SAlex Bradbury }
3599d3f1250SAlex Bradbury }
3609d3f1250SAlex Bradbury
3619d3f1250SAlex Bradbury assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
3629d3f1250SAlex Bradbury
3639d3f1250SAlex Bradbury Fixups.push_back(
3649d3f1250SAlex Bradbury MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
3659d3f1250SAlex Bradbury ++MCNumFixups;
3668ab4a969SAlex Bradbury
3675e8798f9SKito Cheng // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
3685e8798f9SKito Cheng // enabled and the current fixup will result in a relocation that may be
3695e8798f9SKito Cheng // relaxed.
3705e8798f9SKito Cheng if (EnableRelax && RelaxCandidate) {
3715e8798f9SKito Cheng const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
37243bfe844SShiva Chen Fixups.push_back(
3735e8798f9SKito Cheng MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax),
37443bfe844SShiva Chen MI.getLoc()));
37543bfe844SShiva Chen ++MCNumFixups;
37643bfe844SShiva Chen }
37743bfe844SShiva Chen
3788ab4a969SAlex Bradbury return 0;
3796758ecb9SAlex Bradbury }
3806758ecb9SAlex Bradbury
getVMaskReg(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const38166da87dcSHsiangkai Wang unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
38266da87dcSHsiangkai Wang SmallVectorImpl<MCFixup> &Fixups,
38366da87dcSHsiangkai Wang const MCSubtargetInfo &STI) const {
38466da87dcSHsiangkai Wang MCOperand MO = MI.getOperand(OpNo);
38566da87dcSHsiangkai Wang assert(MO.isReg() && "Expected a register.");
38666da87dcSHsiangkai Wang
38766da87dcSHsiangkai Wang switch (MO.getReg()) {
38866da87dcSHsiangkai Wang default:
38966da87dcSHsiangkai Wang llvm_unreachable("Invalid mask register.");
39066da87dcSHsiangkai Wang case RISCV::V0:
39166da87dcSHsiangkai Wang return 0;
39266da87dcSHsiangkai Wang case RISCV::NoRegister:
39366da87dcSHsiangkai Wang return 1;
39466da87dcSHsiangkai Wang }
39566da87dcSHsiangkai Wang }
39666da87dcSHsiangkai Wang
3976b2cca7fSAlex Bradbury #include "RISCVGenMCCodeEmitter.inc"
398