1 //===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the RISCVMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/RISCVFixupKinds.h"
15 #include "MCTargetDesc/RISCVMCExpr.h"
16 #include "MCTargetDesc/RISCVMCTargetDesc.h"
17 #include "Utils/RISCVBaseInfo.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstBuilder.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/EndianStream.h"
30 #include "llvm/Support/raw_ostream.h"
34 #define DEBUG_TYPE "mccodeemitter"
36 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
37 STATISTIC(MCNumFixups, "Number of MC fixups created");
40 class RISCVMCCodeEmitter : public MCCodeEmitter {
41 RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
42 void operator=(const RISCVMCCodeEmitter &) = delete;
44 MCInstrInfo const &MCII;
47 RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
48 : Ctx(ctx), MCII(MCII) {}
50 ~RISCVMCCodeEmitter() override {}
52 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const override;
56 void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
57 SmallVectorImpl<MCFixup> &Fixups,
58 const MCSubtargetInfo &STI) const;
60 /// TableGen'erated function for getting the binary encoding for an
62 uint64_t getBinaryCodeForInstr(const MCInst &MI,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const;
66 /// Return binary encoding of operand. If the machine operand requires
67 /// relocation, record the relocation and return zero.
68 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
69 SmallVectorImpl<MCFixup> &Fixups,
70 const MCSubtargetInfo &STI) const;
72 unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
76 unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
77 SmallVectorImpl<MCFixup> &Fixups,
78 const MCSubtargetInfo &STI) const;
80 } // end anonymous namespace
82 MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
83 const MCRegisterInfo &MRI,
85 return new RISCVMCCodeEmitter(Ctx, MCII);
88 // Expand PseudoCALL and PseudoTAIL to AUIPC and JALR with relocation types.
89 // We expand PseudoCALL and PseudoTAIL while encoding, meaning AUIPC and JALR
90 // won't go through RISCV MC to MC compressed instruction transformation. This
91 // is acceptable because AUIPC has no 16-bit form and C_JALR have no immediate
92 // operand field. We let linker relaxation deal with it. When linker
93 // relaxation enabled, AUIPC and JALR have chance relax to JAL. If C extension
94 // is enabled, JAL has chance relax to C_JAL.
95 void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
96 SmallVectorImpl<MCFixup> &Fixups,
97 const MCSubtargetInfo &STI) const {
99 MCOperand Func = MI.getOperand(0);
100 unsigned Ra = (MI.getOpcode() == RISCV::PseudoTAIL) ? RISCV::X6 : RISCV::X1;
103 assert(Func.isExpr() && "Expected expression");
105 const MCExpr *Expr = Func.getExpr();
107 // Create function call expression CallExpr for AUIPC.
108 const MCExpr *CallExpr =
109 RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_CALL, Ctx);
111 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
112 TmpInst = MCInstBuilder(RISCV::AUIPC)
114 .addOperand(MCOperand::createExpr(CallExpr));
115 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
116 support::endian::write(OS, Binary, support::little);
118 if (MI.getOpcode() == RISCV::PseudoTAIL)
119 // Emit JALR X0, X6, 0
120 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
122 // Emit JALR X1, X1, 0
123 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
124 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
125 support::endian::write(OS, Binary, support::little);
128 void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
129 SmallVectorImpl<MCFixup> &Fixups,
130 const MCSubtargetInfo &STI) const {
131 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
132 // Get byte count of instruction.
133 unsigned Size = Desc.getSize();
135 if (MI.getOpcode() == RISCV::PseudoCALL ||
136 MI.getOpcode() == RISCV::PseudoTAIL) {
137 expandFunctionCall(MI, OS, Fixups, STI);
144 llvm_unreachable("Unhandled encodeInstruction length!");
146 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
147 support::endian::write<uint16_t>(OS, Bits, support::little);
151 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
152 support::endian::write(OS, Bits, support::little);
157 ++MCNumEmitted; // Keep track of the # of mi's emitted.
161 RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
162 SmallVectorImpl<MCFixup> &Fixups,
163 const MCSubtargetInfo &STI) const {
166 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
169 return static_cast<unsigned>(MO.getImm());
171 llvm_unreachable("Unhandled expression!");
176 RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
177 SmallVectorImpl<MCFixup> &Fixups,
178 const MCSubtargetInfo &STI) const {
179 const MCOperand &MO = MI.getOperand(OpNo);
182 unsigned Res = MO.getImm();
183 assert((Res & 1) == 0 && "LSB is non-zero");
187 return getImmOpValue(MI, OpNo, Fixups, STI);
190 unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
191 SmallVectorImpl<MCFixup> &Fixups,
192 const MCSubtargetInfo &STI) const {
193 bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax];
194 const MCOperand &MO = MI.getOperand(OpNo);
196 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
197 unsigned MIFrm = Desc.TSFlags & RISCVII::InstFormatMask;
199 // If the destination is an immediate, there is nothing to do.
203 assert(MO.isExpr() &&
204 "getImmOpValue expects only expressions or immediates");
205 const MCExpr *Expr = MO.getExpr();
206 MCExpr::ExprKind Kind = Expr->getKind();
207 RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid;
208 if (Kind == MCExpr::Target) {
209 const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
211 switch (RVExpr->getKind()) {
212 case RISCVMCExpr::VK_RISCV_None:
213 case RISCVMCExpr::VK_RISCV_Invalid:
214 llvm_unreachable("Unhandled fixup kind!");
215 case RISCVMCExpr::VK_RISCV_LO:
216 if (MIFrm == RISCVII::InstFormatI)
217 FixupKind = RISCV::fixup_riscv_lo12_i;
218 else if (MIFrm == RISCVII::InstFormatS)
219 FixupKind = RISCV::fixup_riscv_lo12_s;
221 llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
223 case RISCVMCExpr::VK_RISCV_HI:
224 FixupKind = RISCV::fixup_riscv_hi20;
226 case RISCVMCExpr::VK_RISCV_PCREL_LO:
227 if (MIFrm == RISCVII::InstFormatI)
228 FixupKind = RISCV::fixup_riscv_pcrel_lo12_i;
229 else if (MIFrm == RISCVII::InstFormatS)
230 FixupKind = RISCV::fixup_riscv_pcrel_lo12_s;
233 "VK_RISCV_PCREL_LO used with unexpected instruction format");
235 case RISCVMCExpr::VK_RISCV_PCREL_HI:
236 FixupKind = RISCV::fixup_riscv_pcrel_hi20;
238 case RISCVMCExpr::VK_RISCV_CALL:
239 FixupKind = RISCV::fixup_riscv_call;
242 } else if (Kind == MCExpr::SymbolRef &&
243 cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
244 if (Desc.getOpcode() == RISCV::JAL) {
245 FixupKind = RISCV::fixup_riscv_jal;
246 } else if (MIFrm == RISCVII::InstFormatB) {
247 FixupKind = RISCV::fixup_riscv_branch;
248 } else if (MIFrm == RISCVII::InstFormatCJ) {
249 FixupKind = RISCV::fixup_riscv_rvc_jump;
250 } else if (MIFrm == RISCVII::InstFormatCB) {
251 FixupKind = RISCV::fixup_riscv_rvc_branch;
255 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
258 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
262 if (FixupKind == RISCV::fixup_riscv_call) {
264 MCFixup::create(0, Expr, MCFixupKind(RISCV::fixup_riscv_relax),
273 #include "RISCVGenMCCodeEmitter.inc"