106c3fb27SDimitry Andric //===-- RISCVMCCodeEmitter.cpp - Convert RISC-V code to machine code ------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the RISCVMCCodeEmitter class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
13e8d8bef9SDimitry Andric #include "MCTargetDesc/RISCVBaseInfo.h"
140b57cec5SDimitry Andric #include "MCTargetDesc/RISCVFixupKinds.h"
150b57cec5SDimitry Andric #include "MCTargetDesc/RISCVMCExpr.h"
160b57cec5SDimitry Andric #include "MCTargetDesc/RISCVMCTargetDesc.h"
170b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
180b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
190b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
200b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCInstBuilder.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
2681ad6265SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
280b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
290b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h"
300b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric using namespace llvm;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric #define DEBUG_TYPE "mccodeemitter"
350b57cec5SDimitry Andric
360b57cec5SDimitry Andric STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
370b57cec5SDimitry Andric STATISTIC(MCNumFixups, "Number of MC fixups created");
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric namespace {
400b57cec5SDimitry Andric class RISCVMCCodeEmitter : public MCCodeEmitter {
410b57cec5SDimitry Andric RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
420b57cec5SDimitry Andric void operator=(const RISCVMCCodeEmitter &) = delete;
430b57cec5SDimitry Andric MCContext &Ctx;
440b57cec5SDimitry Andric MCInstrInfo const &MCII;
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric public:
RISCVMCCodeEmitter(MCContext & ctx,MCInstrInfo const & MCII)470b57cec5SDimitry Andric RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
480b57cec5SDimitry Andric : Ctx(ctx), MCII(MCII) {}
490b57cec5SDimitry Andric
5081ad6265SDimitry Andric ~RISCVMCCodeEmitter() override = default;
510b57cec5SDimitry Andric
5206c3fb27SDimitry Andric void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
530b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
540b57cec5SDimitry Andric const MCSubtargetInfo &STI) const override;
550b57cec5SDimitry Andric
5606c3fb27SDimitry Andric void expandFunctionCall(const MCInst &MI, SmallVectorImpl<char> &CB,
570b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
580b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
590b57cec5SDimitry Andric
607a6dacacSDimitry Andric void expandTLSDESCCall(const MCInst &MI, SmallVectorImpl<char> &CB,
617a6dacacSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
627a6dacacSDimitry Andric const MCSubtargetInfo &STI) const;
637a6dacacSDimitry Andric
6406c3fb27SDimitry Andric void expandAddTPRel(const MCInst &MI, SmallVectorImpl<char> &CB,
6506c3fb27SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
6606c3fb27SDimitry Andric const MCSubtargetInfo &STI) const;
6706c3fb27SDimitry Andric
6806c3fb27SDimitry Andric void expandLongCondBr(const MCInst &MI, SmallVectorImpl<char> &CB,
690b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
700b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric /// TableGen'erated function for getting the binary encoding for an
730b57cec5SDimitry Andric /// instruction.
740b57cec5SDimitry Andric uint64_t getBinaryCodeForInstr(const MCInst &MI,
750b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
760b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric /// Return binary encoding of operand. If the machine operand requires
790b57cec5SDimitry Andric /// relocation, record the relocation and return zero.
800b57cec5SDimitry Andric unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
810b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
820b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
850b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
860b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
890b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
900b57cec5SDimitry Andric const MCSubtargetInfo &STI) const;
915ffd83dbSDimitry Andric
925ffd83dbSDimitry Andric unsigned getVMaskReg(const MCInst &MI, unsigned OpNo,
935ffd83dbSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
945ffd83dbSDimitry Andric const MCSubtargetInfo &STI) const;
9506c3fb27SDimitry Andric
9606c3fb27SDimitry Andric unsigned getRlistOpValue(const MCInst &MI, unsigned OpNo,
9706c3fb27SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
9806c3fb27SDimitry Andric const MCSubtargetInfo &STI) const;
995f757f3fSDimitry Andric
1005f757f3fSDimitry Andric unsigned getRegReg(const MCInst &MI, unsigned OpNo,
1015f757f3fSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1025f757f3fSDimitry Andric const MCSubtargetInfo &STI) const;
1030b57cec5SDimitry Andric };
1040b57cec5SDimitry Andric } // end anonymous namespace
1050b57cec5SDimitry Andric
createRISCVMCCodeEmitter(const MCInstrInfo & MCII,MCContext & Ctx)1060b57cec5SDimitry Andric MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
1070b57cec5SDimitry Andric MCContext &Ctx) {
1080b57cec5SDimitry Andric return new RISCVMCCodeEmitter(Ctx, MCII);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric
1115ffd83dbSDimitry Andric // Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
1125ffd83dbSDimitry Andric // relocation types. We expand those pseudo-instructions while encoding them,
11306c3fb27SDimitry Andric // meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
1145ffd83dbSDimitry Andric // instruction transformation. This is acceptable because AUIPC has no 16-bit
1155ffd83dbSDimitry Andric // form and C_JALR has no immediate operand field. We let linker relaxation
1165ffd83dbSDimitry Andric // deal with it. When linker relaxation is enabled, AUIPC and JALR have a
1175ffd83dbSDimitry Andric // chance to relax to JAL.
1185ffd83dbSDimitry Andric // If the C extension is enabled, JAL has a chance relax to C_JAL.
expandFunctionCall(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const11906c3fb27SDimitry Andric void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI,
12006c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
1210b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1220b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1230b57cec5SDimitry Andric MCInst TmpInst;
1240b57cec5SDimitry Andric MCOperand Func;
125e8d8bef9SDimitry Andric MCRegister Ra;
1260b57cec5SDimitry Andric if (MI.getOpcode() == RISCV::PseudoTAIL) {
1270b57cec5SDimitry Andric Func = MI.getOperand(0);
1280b57cec5SDimitry Andric Ra = RISCV::X6;
1290fca6ea1SDimitry Andric // For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
1300fca6ea1SDimitry Andric // It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
1310fca6ea1SDimitry Andric if (STI.hasFeature(RISCV::FeatureStdExtZicfilp))
1320fca6ea1SDimitry Andric Ra = RISCV::X7;
1330b57cec5SDimitry Andric } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
1340b57cec5SDimitry Andric Func = MI.getOperand(1);
1350b57cec5SDimitry Andric Ra = MI.getOperand(0).getReg();
1365ffd83dbSDimitry Andric } else if (MI.getOpcode() == RISCV::PseudoCALL) {
1370b57cec5SDimitry Andric Func = MI.getOperand(0);
1380b57cec5SDimitry Andric Ra = RISCV::X1;
1395ffd83dbSDimitry Andric } else if (MI.getOpcode() == RISCV::PseudoJump) {
1405ffd83dbSDimitry Andric Func = MI.getOperand(1);
1415ffd83dbSDimitry Andric Ra = MI.getOperand(0).getReg();
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric uint32_t Binary;
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric assert(Func.isExpr() && "Expected expression");
1460b57cec5SDimitry Andric
1470b57cec5SDimitry Andric const MCExpr *CallExpr = Func.getExpr();
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
15081ad6265SDimitry Andric TmpInst = MCInstBuilder(RISCV::AUIPC).addReg(Ra).addExpr(CallExpr);
1510b57cec5SDimitry Andric Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
1525f757f3fSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
1530b57cec5SDimitry Andric
1545ffd83dbSDimitry Andric if (MI.getOpcode() == RISCV::PseudoTAIL ||
1555ffd83dbSDimitry Andric MI.getOpcode() == RISCV::PseudoJump)
1565ffd83dbSDimitry Andric // Emit JALR X0, Ra, 0
1570b57cec5SDimitry Andric TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
1580b57cec5SDimitry Andric else
1590b57cec5SDimitry Andric // Emit JALR Ra, Ra, 0
1600b57cec5SDimitry Andric TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
1610b57cec5SDimitry Andric Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
1625f757f3fSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
expandTLSDESCCall(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1657a6dacacSDimitry Andric void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
1667a6dacacSDimitry Andric SmallVectorImpl<char> &CB,
1677a6dacacSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1687a6dacacSDimitry Andric const MCSubtargetInfo &STI) const {
1697a6dacacSDimitry Andric MCOperand SrcSymbol = MI.getOperand(3);
1707a6dacacSDimitry Andric assert(SrcSymbol.isExpr() &&
1717a6dacacSDimitry Andric "Expected expression as first input to TLSDESCCALL");
1727a6dacacSDimitry Andric const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
1737a6dacacSDimitry Andric MCRegister Link = MI.getOperand(0).getReg();
1747a6dacacSDimitry Andric MCRegister Dest = MI.getOperand(1).getReg();
1757a6dacacSDimitry Andric MCRegister Imm = MI.getOperand(2).getImm();
1767a6dacacSDimitry Andric Fixups.push_back(MCFixup::create(
1777a6dacacSDimitry Andric 0, Expr, MCFixupKind(RISCV::fixup_riscv_tlsdesc_call), MI.getLoc()));
1787a6dacacSDimitry Andric MCInst Call =
1797a6dacacSDimitry Andric MCInstBuilder(RISCV::JALR).addReg(Link).addReg(Dest).addImm(Imm);
1807a6dacacSDimitry Andric
1817a6dacacSDimitry Andric uint32_t Binary = getBinaryCodeForInstr(Call, Fixups, STI);
1827a6dacacSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
1837a6dacacSDimitry Andric }
1847a6dacacSDimitry Andric
1850b57cec5SDimitry Andric // Expand PseudoAddTPRel to a simple ADD with the correct relocation.
expandAddTPRel(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const18606c3fb27SDimitry Andric void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
18706c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
1880b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
1890b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
1900b57cec5SDimitry Andric MCOperand DestReg = MI.getOperand(0);
1910b57cec5SDimitry Andric MCOperand SrcReg = MI.getOperand(1);
1920b57cec5SDimitry Andric MCOperand TPReg = MI.getOperand(2);
1930b57cec5SDimitry Andric assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
1940b57cec5SDimitry Andric "Expected thread pointer as second input to TP-relative add");
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric MCOperand SrcSymbol = MI.getOperand(3);
1970b57cec5SDimitry Andric assert(SrcSymbol.isExpr() &&
1980b57cec5SDimitry Andric "Expected expression as third input to TP-relative add");
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andric const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
2010b57cec5SDimitry Andric assert(Expr && Expr->getKind() == RISCVMCExpr::VK_RISCV_TPREL_ADD &&
2020b57cec5SDimitry Andric "Expected tprel_add relocation on TP-relative symbol");
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric // Emit the correct tprel_add relocation for the symbol.
2050b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(
2060b57cec5SDimitry Andric 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc()));
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled.
20906c3fb27SDimitry Andric if (STI.hasFeature(RISCV::FeatureRelax)) {
2100b57cec5SDimitry Andric const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
2110b57cec5SDimitry Andric Fixups.push_back(MCFixup::create(
2120b57cec5SDimitry Andric 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc()));
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric // Emit a normal ADD instruction with the given operands.
2160b57cec5SDimitry Andric MCInst TmpInst = MCInstBuilder(RISCV::ADD)
2170b57cec5SDimitry Andric .addOperand(DestReg)
2180b57cec5SDimitry Andric .addOperand(SrcReg)
2190b57cec5SDimitry Andric .addOperand(TPReg);
2200b57cec5SDimitry Andric uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
2215f757f3fSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric
getInvertedBranchOp(unsigned BrOp)22406c3fb27SDimitry Andric static unsigned getInvertedBranchOp(unsigned BrOp) {
22506c3fb27SDimitry Andric switch (BrOp) {
22606c3fb27SDimitry Andric default:
22706c3fb27SDimitry Andric llvm_unreachable("Unexpected branch opcode!");
22806c3fb27SDimitry Andric case RISCV::PseudoLongBEQ:
22906c3fb27SDimitry Andric return RISCV::BNE;
23006c3fb27SDimitry Andric case RISCV::PseudoLongBNE:
23106c3fb27SDimitry Andric return RISCV::BEQ;
23206c3fb27SDimitry Andric case RISCV::PseudoLongBLT:
23306c3fb27SDimitry Andric return RISCV::BGE;
23406c3fb27SDimitry Andric case RISCV::PseudoLongBGE:
23506c3fb27SDimitry Andric return RISCV::BLT;
23606c3fb27SDimitry Andric case RISCV::PseudoLongBLTU:
23706c3fb27SDimitry Andric return RISCV::BGEU;
23806c3fb27SDimitry Andric case RISCV::PseudoLongBGEU:
23906c3fb27SDimitry Andric return RISCV::BLTU;
24006c3fb27SDimitry Andric }
24106c3fb27SDimitry Andric }
24206c3fb27SDimitry Andric
24306c3fb27SDimitry Andric // Expand PseudoLongBxx to an inverted conditional branch and an unconditional
24406c3fb27SDimitry Andric // jump.
expandLongCondBr(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const24506c3fb27SDimitry Andric void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
24606c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
24706c3fb27SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
24806c3fb27SDimitry Andric const MCSubtargetInfo &STI) const {
24906c3fb27SDimitry Andric MCRegister SrcReg1 = MI.getOperand(0).getReg();
25006c3fb27SDimitry Andric MCRegister SrcReg2 = MI.getOperand(1).getReg();
25106c3fb27SDimitry Andric MCOperand SrcSymbol = MI.getOperand(2);
25206c3fb27SDimitry Andric unsigned Opcode = MI.getOpcode();
25306c3fb27SDimitry Andric bool IsEqTest =
25406c3fb27SDimitry Andric Opcode == RISCV::PseudoLongBNE || Opcode == RISCV::PseudoLongBEQ;
25506c3fb27SDimitry Andric
25606c3fb27SDimitry Andric bool UseCompressedBr = false;
25706c3fb27SDimitry Andric if (IsEqTest && (STI.hasFeature(RISCV::FeatureStdExtC) ||
25806c3fb27SDimitry Andric STI.hasFeature(RISCV::FeatureStdExtZca))) {
25906c3fb27SDimitry Andric if (RISCV::X8 <= SrcReg1.id() && SrcReg1.id() <= RISCV::X15 &&
26006c3fb27SDimitry Andric SrcReg2.id() == RISCV::X0) {
26106c3fb27SDimitry Andric UseCompressedBr = true;
26206c3fb27SDimitry Andric } else if (RISCV::X8 <= SrcReg2.id() && SrcReg2.id() <= RISCV::X15 &&
26306c3fb27SDimitry Andric SrcReg1.id() == RISCV::X0) {
26406c3fb27SDimitry Andric std::swap(SrcReg1, SrcReg2);
26506c3fb27SDimitry Andric UseCompressedBr = true;
26606c3fb27SDimitry Andric }
26706c3fb27SDimitry Andric }
26806c3fb27SDimitry Andric
26906c3fb27SDimitry Andric uint32_t Offset;
27006c3fb27SDimitry Andric if (UseCompressedBr) {
27106c3fb27SDimitry Andric unsigned InvOpc =
27206c3fb27SDimitry Andric Opcode == RISCV::PseudoLongBNE ? RISCV::C_BEQZ : RISCV::C_BNEZ;
27306c3fb27SDimitry Andric MCInst TmpInst = MCInstBuilder(InvOpc).addReg(SrcReg1).addImm(6);
27406c3fb27SDimitry Andric uint16_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
2755f757f3fSDimitry Andric support::endian::write<uint16_t>(CB, Binary, llvm::endianness::little);
27606c3fb27SDimitry Andric Offset = 2;
27706c3fb27SDimitry Andric } else {
27806c3fb27SDimitry Andric unsigned InvOpc = getInvertedBranchOp(Opcode);
27906c3fb27SDimitry Andric MCInst TmpInst =
28006c3fb27SDimitry Andric MCInstBuilder(InvOpc).addReg(SrcReg1).addReg(SrcReg2).addImm(8);
28106c3fb27SDimitry Andric uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
2825f757f3fSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
28306c3fb27SDimitry Andric Offset = 4;
28406c3fb27SDimitry Andric }
28506c3fb27SDimitry Andric
286*415efcecSDimitry Andric // Save the number fixups.
287*415efcecSDimitry Andric size_t FixupStartIndex = Fixups.size();
288*415efcecSDimitry Andric
28906c3fb27SDimitry Andric // Emit an unconditional jump to the destination.
29006c3fb27SDimitry Andric MCInst TmpInst =
29106c3fb27SDimitry Andric MCInstBuilder(RISCV::JAL).addReg(RISCV::X0).addOperand(SrcSymbol);
29206c3fb27SDimitry Andric uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
2935f757f3fSDimitry Andric support::endian::write(CB, Binary, llvm::endianness::little);
29406c3fb27SDimitry Andric
295*415efcecSDimitry Andric // Drop any fixup added so we can add the correct one.
296*415efcecSDimitry Andric Fixups.resize(FixupStartIndex);
297*415efcecSDimitry Andric
29806c3fb27SDimitry Andric if (SrcSymbol.isExpr()) {
29906c3fb27SDimitry Andric Fixups.push_back(MCFixup::create(Offset, SrcSymbol.getExpr(),
30006c3fb27SDimitry Andric MCFixupKind(RISCV::fixup_riscv_jal),
30106c3fb27SDimitry Andric MI.getLoc()));
30206c3fb27SDimitry Andric }
30306c3fb27SDimitry Andric }
30406c3fb27SDimitry Andric
encodeInstruction(const MCInst & MI,SmallVectorImpl<char> & CB,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const30506c3fb27SDimitry Andric void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
30606c3fb27SDimitry Andric SmallVectorImpl<char> &CB,
3070b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
3080b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
3090b57cec5SDimitry Andric const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
3100b57cec5SDimitry Andric // Get byte count of instruction.
3110b57cec5SDimitry Andric unsigned Size = Desc.getSize();
3120b57cec5SDimitry Andric
3131fd87a68SDimitry Andric // RISCVInstrInfo::getInstSizeInBytes expects that the total size of the
3141fd87a68SDimitry Andric // expanded instructions for each pseudo is correct in the Size field of the
3151fd87a68SDimitry Andric // tablegen definition for the pseudo.
31606c3fb27SDimitry Andric switch (MI.getOpcode()) {
31706c3fb27SDimitry Andric default:
31806c3fb27SDimitry Andric break;
31906c3fb27SDimitry Andric case RISCV::PseudoCALLReg:
32006c3fb27SDimitry Andric case RISCV::PseudoCALL:
32106c3fb27SDimitry Andric case RISCV::PseudoTAIL:
32206c3fb27SDimitry Andric case RISCV::PseudoJump:
32306c3fb27SDimitry Andric expandFunctionCall(MI, CB, Fixups, STI);
3240b57cec5SDimitry Andric MCNumEmitted += 2;
3250b57cec5SDimitry Andric return;
32606c3fb27SDimitry Andric case RISCV::PseudoAddTPRel:
32706c3fb27SDimitry Andric expandAddTPRel(MI, CB, Fixups, STI);
3280b57cec5SDimitry Andric MCNumEmitted += 1;
3290b57cec5SDimitry Andric return;
33006c3fb27SDimitry Andric case RISCV::PseudoLongBEQ:
33106c3fb27SDimitry Andric case RISCV::PseudoLongBNE:
33206c3fb27SDimitry Andric case RISCV::PseudoLongBLT:
33306c3fb27SDimitry Andric case RISCV::PseudoLongBGE:
33406c3fb27SDimitry Andric case RISCV::PseudoLongBLTU:
33506c3fb27SDimitry Andric case RISCV::PseudoLongBGEU:
33606c3fb27SDimitry Andric expandLongCondBr(MI, CB, Fixups, STI);
33706c3fb27SDimitry Andric MCNumEmitted += 2;
33806c3fb27SDimitry Andric return;
3397a6dacacSDimitry Andric case RISCV::PseudoTLSDESCCall:
3407a6dacacSDimitry Andric expandTLSDESCCall(MI, CB, Fixups, STI);
3417a6dacacSDimitry Andric MCNumEmitted += 1;
3427a6dacacSDimitry Andric return;
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric switch (Size) {
3460b57cec5SDimitry Andric default:
3470b57cec5SDimitry Andric llvm_unreachable("Unhandled encodeInstruction length!");
3480b57cec5SDimitry Andric case 2: {
3490b57cec5SDimitry Andric uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
3505f757f3fSDimitry Andric support::endian::write<uint16_t>(CB, Bits, llvm::endianness::little);
3510b57cec5SDimitry Andric break;
3520b57cec5SDimitry Andric }
3530b57cec5SDimitry Andric case 4: {
3540b57cec5SDimitry Andric uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
3555f757f3fSDimitry Andric support::endian::write(CB, Bits, llvm::endianness::little);
3560b57cec5SDimitry Andric break;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric }
3590b57cec5SDimitry Andric
3600b57cec5SDimitry Andric ++MCNumEmitted; // Keep track of the # of mi's emitted.
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric
3630b57cec5SDimitry Andric unsigned
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3640b57cec5SDimitry Andric RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
3650b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
3660b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric if (MO.isReg())
3690b57cec5SDimitry Andric return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric if (MO.isImm())
3720b57cec5SDimitry Andric return static_cast<unsigned>(MO.getImm());
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric llvm_unreachable("Unhandled expression!");
3750b57cec5SDimitry Andric return 0;
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andric unsigned
getImmOpValueAsr1(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3790b57cec5SDimitry Andric RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
3800b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
3810b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
3820b57cec5SDimitry Andric const MCOperand &MO = MI.getOperand(OpNo);
3830b57cec5SDimitry Andric
3840b57cec5SDimitry Andric if (MO.isImm()) {
3850b57cec5SDimitry Andric unsigned Res = MO.getImm();
3860b57cec5SDimitry Andric assert((Res & 1) == 0 && "LSB is non-zero");
3870b57cec5SDimitry Andric return Res >> 1;
3880b57cec5SDimitry Andric }
3890b57cec5SDimitry Andric
3900b57cec5SDimitry Andric return getImmOpValue(MI, OpNo, Fixups, STI);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric
getImmOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3930b57cec5SDimitry Andric unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
3940b57cec5SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
3950b57cec5SDimitry Andric const MCSubtargetInfo &STI) const {
39606c3fb27SDimitry Andric bool EnableRelax = STI.hasFeature(RISCV::FeatureRelax);
3970b57cec5SDimitry Andric const MCOperand &MO = MI.getOperand(OpNo);
3980b57cec5SDimitry Andric
3990b57cec5SDimitry Andric MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
400fe6060f1SDimitry Andric unsigned MIFrm = RISCVII::getFormat(Desc.TSFlags);
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andric // If the destination is an immediate, there is nothing to do.
4030b57cec5SDimitry Andric if (MO.isImm())
4040b57cec5SDimitry Andric return MO.getImm();
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric assert(MO.isExpr() &&
4070b57cec5SDimitry Andric "getImmOpValue expects only expressions or immediates");
4080b57cec5SDimitry Andric const MCExpr *Expr = MO.getExpr();
4090b57cec5SDimitry Andric MCExpr::ExprKind Kind = Expr->getKind();
4100b57cec5SDimitry Andric RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid;
4110b57cec5SDimitry Andric bool RelaxCandidate = false;
4120b57cec5SDimitry Andric if (Kind == MCExpr::Target) {
4130b57cec5SDimitry Andric const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
4140b57cec5SDimitry Andric
4150b57cec5SDimitry Andric switch (RVExpr->getKind()) {
4160b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_None:
4170b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_Invalid:
4180b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_32_PCREL:
4190b57cec5SDimitry Andric llvm_unreachable("Unhandled fixup kind!");
4200b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_TPREL_ADD:
4210b57cec5SDimitry Andric // tprel_add is only used to indicate that a relocation should be emitted
4220b57cec5SDimitry Andric // for an add instruction used in TP-relative addressing. It should not be
4230b57cec5SDimitry Andric // expanded as if representing an actual instruction operand and so to
4240b57cec5SDimitry Andric // encounter it here is an error.
4250b57cec5SDimitry Andric llvm_unreachable(
4260b57cec5SDimitry Andric "VK_RISCV_TPREL_ADD should not represent an instruction operand");
4270b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_LO:
4280b57cec5SDimitry Andric if (MIFrm == RISCVII::InstFormatI)
4290b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_lo12_i;
4300b57cec5SDimitry Andric else if (MIFrm == RISCVII::InstFormatS)
4310b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_lo12_s;
4320b57cec5SDimitry Andric else
4330b57cec5SDimitry Andric llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
4340b57cec5SDimitry Andric RelaxCandidate = true;
4350b57cec5SDimitry Andric break;
4360b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_HI:
4370b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_hi20;
4380b57cec5SDimitry Andric RelaxCandidate = true;
4390b57cec5SDimitry Andric break;
4400b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_PCREL_LO:
4410b57cec5SDimitry Andric if (MIFrm == RISCVII::InstFormatI)
4420b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_pcrel_lo12_i;
4430b57cec5SDimitry Andric else if (MIFrm == RISCVII::InstFormatS)
4440b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_pcrel_lo12_s;
4450b57cec5SDimitry Andric else
4460b57cec5SDimitry Andric llvm_unreachable(
4470b57cec5SDimitry Andric "VK_RISCV_PCREL_LO used with unexpected instruction format");
4480b57cec5SDimitry Andric RelaxCandidate = true;
4490b57cec5SDimitry Andric break;
4500b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_PCREL_HI:
4510b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_pcrel_hi20;
4520b57cec5SDimitry Andric RelaxCandidate = true;
4530b57cec5SDimitry Andric break;
4540b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_GOT_HI:
4550b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_got_hi20;
4560b57cec5SDimitry Andric break;
4570b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_TPREL_LO:
4580b57cec5SDimitry Andric if (MIFrm == RISCVII::InstFormatI)
4590b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_tprel_lo12_i;
4600b57cec5SDimitry Andric else if (MIFrm == RISCVII::InstFormatS)
4610b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_tprel_lo12_s;
4620b57cec5SDimitry Andric else
4630b57cec5SDimitry Andric llvm_unreachable(
4640b57cec5SDimitry Andric "VK_RISCV_TPREL_LO used with unexpected instruction format");
4650b57cec5SDimitry Andric RelaxCandidate = true;
4660b57cec5SDimitry Andric break;
4670b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_TPREL_HI:
4680b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_tprel_hi20;
4690b57cec5SDimitry Andric RelaxCandidate = true;
4700b57cec5SDimitry Andric break;
4710b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_TLS_GOT_HI:
4720b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_tls_got_hi20;
4730b57cec5SDimitry Andric break;
4740b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_TLS_GD_HI:
4750b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_tls_gd_hi20;
4760b57cec5SDimitry Andric break;
4770b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_CALL:
4780b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_call;
4790b57cec5SDimitry Andric RelaxCandidate = true;
4800b57cec5SDimitry Andric break;
4810b57cec5SDimitry Andric case RISCVMCExpr::VK_RISCV_CALL_PLT:
4820b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_call_plt;
4830b57cec5SDimitry Andric RelaxCandidate = true;
4840b57cec5SDimitry Andric break;
4857a6dacacSDimitry Andric case RISCVMCExpr::VK_RISCV_TLSDESC_HI:
4867a6dacacSDimitry Andric FixupKind = RISCV::fixup_riscv_tlsdesc_hi20;
4877a6dacacSDimitry Andric break;
4887a6dacacSDimitry Andric case RISCVMCExpr::VK_RISCV_TLSDESC_LOAD_LO:
4897a6dacacSDimitry Andric FixupKind = RISCV::fixup_riscv_tlsdesc_load_lo12;
4907a6dacacSDimitry Andric break;
4917a6dacacSDimitry Andric case RISCVMCExpr::VK_RISCV_TLSDESC_ADD_LO:
4927a6dacacSDimitry Andric FixupKind = RISCV::fixup_riscv_tlsdesc_add_lo12;
4937a6dacacSDimitry Andric break;
4947a6dacacSDimitry Andric case RISCVMCExpr::VK_RISCV_TLSDESC_CALL:
4957a6dacacSDimitry Andric FixupKind = RISCV::fixup_riscv_tlsdesc_call;
4967a6dacacSDimitry Andric break;
4970b57cec5SDimitry Andric }
4985f757f3fSDimitry Andric } else if ((Kind == MCExpr::SymbolRef &&
4995f757f3fSDimitry Andric cast<MCSymbolRefExpr>(Expr)->getKind() ==
5005f757f3fSDimitry Andric MCSymbolRefExpr::VK_None) ||
5015f757f3fSDimitry Andric Kind == MCExpr::Binary) {
5025f757f3fSDimitry Andric // FIXME: Sub kind binary exprs have chance of underflow.
503349cc55cSDimitry Andric if (MIFrm == RISCVII::InstFormatJ) {
5040b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_jal;
5050b57cec5SDimitry Andric } else if (MIFrm == RISCVII::InstFormatB) {
5060b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_branch;
5070b57cec5SDimitry Andric } else if (MIFrm == RISCVII::InstFormatCJ) {
5080b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_rvc_jump;
5090b57cec5SDimitry Andric } else if (MIFrm == RISCVII::InstFormatCB) {
5100b57cec5SDimitry Andric FixupKind = RISCV::fixup_riscv_rvc_branch;
51106c3fb27SDimitry Andric } else if (MIFrm == RISCVII::InstFormatI) {
51206c3fb27SDimitry Andric FixupKind = RISCV::fixup_riscv_12_i;
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric }
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
5170b57cec5SDimitry Andric
5180b57cec5SDimitry Andric Fixups.push_back(
5190b57cec5SDimitry Andric MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
5200b57cec5SDimitry Andric ++MCNumFixups;
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
5230b57cec5SDimitry Andric // enabled and the current fixup will result in a relocation that may be
5240b57cec5SDimitry Andric // relaxed.
5250b57cec5SDimitry Andric if (EnableRelax && RelaxCandidate) {
5260b57cec5SDimitry Andric const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
5270b57cec5SDimitry Andric Fixups.push_back(
5280b57cec5SDimitry Andric MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax),
5290b57cec5SDimitry Andric MI.getLoc()));
5300b57cec5SDimitry Andric ++MCNumFixups;
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric
5330b57cec5SDimitry Andric return 0;
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric
getVMaskReg(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const5365ffd83dbSDimitry Andric unsigned RISCVMCCodeEmitter::getVMaskReg(const MCInst &MI, unsigned OpNo,
5375ffd83dbSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
5385ffd83dbSDimitry Andric const MCSubtargetInfo &STI) const {
5395ffd83dbSDimitry Andric MCOperand MO = MI.getOperand(OpNo);
5405ffd83dbSDimitry Andric assert(MO.isReg() && "Expected a register.");
5415ffd83dbSDimitry Andric
5425ffd83dbSDimitry Andric switch (MO.getReg()) {
5435ffd83dbSDimitry Andric default:
5445ffd83dbSDimitry Andric llvm_unreachable("Invalid mask register.");
5455ffd83dbSDimitry Andric case RISCV::V0:
5465ffd83dbSDimitry Andric return 0;
5475ffd83dbSDimitry Andric case RISCV::NoRegister:
5485ffd83dbSDimitry Andric return 1;
5495ffd83dbSDimitry Andric }
5505ffd83dbSDimitry Andric }
5515ffd83dbSDimitry Andric
getRlistOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const55206c3fb27SDimitry Andric unsigned RISCVMCCodeEmitter::getRlistOpValue(const MCInst &MI, unsigned OpNo,
55306c3fb27SDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
55406c3fb27SDimitry Andric const MCSubtargetInfo &STI) const {
55506c3fb27SDimitry Andric const MCOperand &MO = MI.getOperand(OpNo);
55606c3fb27SDimitry Andric assert(MO.isImm() && "Rlist operand must be immediate");
55706c3fb27SDimitry Andric auto Imm = MO.getImm();
55806c3fb27SDimitry Andric assert(Imm >= 4 && "EABI is currently not implemented");
55906c3fb27SDimitry Andric return Imm;
56006c3fb27SDimitry Andric }
56106c3fb27SDimitry Andric
getRegReg(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const5625f757f3fSDimitry Andric unsigned RISCVMCCodeEmitter::getRegReg(const MCInst &MI, unsigned OpNo,
5635f757f3fSDimitry Andric SmallVectorImpl<MCFixup> &Fixups,
5645f757f3fSDimitry Andric const MCSubtargetInfo &STI) const {
5655f757f3fSDimitry Andric const MCOperand &MO = MI.getOperand(OpNo);
5665f757f3fSDimitry Andric const MCOperand &MO1 = MI.getOperand(OpNo + 1);
5675f757f3fSDimitry Andric assert(MO.isReg() && MO1.isReg() && "Expected registers.");
5685f757f3fSDimitry Andric
5695f757f3fSDimitry Andric unsigned Op = Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
5705f757f3fSDimitry Andric unsigned Op1 = Ctx.getRegisterInfo()->getEncodingValue(MO1.getReg());
5715f757f3fSDimitry Andric
5725f757f3fSDimitry Andric return Op | Op1 << 5;
5735f757f3fSDimitry Andric }
5745f757f3fSDimitry Andric
5750b57cec5SDimitry Andric #include "RISCVGenMCCodeEmitter.inc"
576