blob: fe4e2f94c01ece04d5179a5f3342a1aa7cffefdc [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Bradbury6b2cca72016-11-01 23:47:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the RISCVMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
Alex Bradbury9d3f1252017-09-28 08:26:24 +000013#include "MCTargetDesc/RISCVFixupKinds.h"
14#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000015#include "MCTargetDesc/RISCVMCTargetDesc.h"
Ana Pazos9d6c5532018-10-04 21:50:54 +000016#include "Utils/RISCVBaseInfo.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000017#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000018#include "llvm/MC/MCAsmInfo.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Shiva Chen98f93892018-04-25 14:18:55 +000023#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000024#include "llvm/MC/MCInstrInfo.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000025#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCSymbol.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000027#include "llvm/Support/Casting.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000028#include "llvm/Support/EndianStream.h"
29#include "llvm/Support/raw_ostream.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "mccodeemitter"
34
35STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
Alex Bradbury9d3f1252017-09-28 08:26:24 +000036STATISTIC(MCNumFixups, "Number of MC fixups created");
Alex Bradbury6b2cca72016-11-01 23:47:30 +000037
38namespace {
39class RISCVMCCodeEmitter : public MCCodeEmitter {
40 RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
41 void operator=(const RISCVMCCodeEmitter &) = delete;
42 MCContext &Ctx;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000043 MCInstrInfo const &MCII;
Alex Bradbury6b2cca72016-11-01 23:47:30 +000044
45public:
Alex Bradbury9d3f1252017-09-28 08:26:24 +000046 RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
47 : Ctx(ctx), MCII(MCII) {}
Alex Bradbury6b2cca72016-11-01 23:47:30 +000048
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 Chen98f93892018-04-25 14:18:55 +000055 void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
56 SmallVectorImpl<MCFixup> &Fixups,
57 const MCSubtargetInfo &STI) const;
58
Alex Bradbury6b2cca72016-11-01 23:47:30 +000059 /// TableGen'erated function for getting the binary encoding for an
60 /// instruction.
61 uint64_t getBinaryCodeForInstr(const MCInst &MI,
62 SmallVectorImpl<MCFixup> &Fixups,
63 const MCSubtargetInfo &STI) const;
64
65 /// Return binary encoding of operand. If the machine operand requires
66 /// relocation, record the relocation and return zero.
67 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
68 SmallVectorImpl<MCFixup> &Fixups,
69 const MCSubtargetInfo &STI) const;
Alex Bradbury6758ecb2017-09-17 14:27:35 +000070
71 unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups,
73 const MCSubtargetInfo &STI) const;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000074
Alex Bradbury8ab4a962017-09-17 14:36:28 +000075 unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
Alex Bradbury6b2cca72016-11-01 23:47:30 +000078};
79} // end anonymous namespace
80
81MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
82 const MCRegisterInfo &MRI,
83 MCContext &Ctx) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +000084 return new RISCVMCCodeEmitter(Ctx, MCII);
Alex Bradbury6b2cca72016-11-01 23:47:30 +000085}
86
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +000087// Expand PseudoCALL and PseudoTAIL to AUIPC and JALR with relocation types.
88// We expand PseudoCALL and PseudoTAIL while encoding, meaning AUIPC and JALR
89// won't go through RISCV MC to MC compressed instruction transformation. This
90// is acceptable because AUIPC has no 16-bit form and C_JALR have no immediate
91// operand field. We let linker relaxation deal with it. When linker
92// relaxation enabled, AUIPC and JALR have chance relax to JAL. If C extension
93// is enabled, JAL has chance relax to C_JAL.
Shiva Chen98f93892018-04-25 14:18:55 +000094void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
95 SmallVectorImpl<MCFixup> &Fixups,
96 const MCSubtargetInfo &STI) const {
97 MCInst TmpInst;
98 MCOperand Func = MI.getOperand(0);
Mandeep Singh Grangef0ebf22018-05-17 17:31:27 +000099 unsigned Ra = (MI.getOpcode() == RISCV::PseudoTAIL) ? RISCV::X6 : RISCV::X1;
Shiva Chen98f93892018-04-25 14:18:55 +0000100 uint32_t Binary;
101
102 assert(Func.isExpr() && "Expected expression");
103
104 const MCExpr *Expr = Func.getExpr();
105
106 // Create function call expression CallExpr for AUIPC.
107 const MCExpr *CallExpr =
108 RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_CALL, Ctx);
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 Collingbournee3f65292018-05-18 19:46:24 +0000115 support::endian::write(OS, Binary, support::little);
Shiva Chen98f93892018-04-25 14:18:55 +0000116
Sameer AbuAsale01e7112018-06-21 14:37:09 +0000117 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 Chen98f93892018-04-25 14:18:55 +0000123 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000124 support::endian::write(OS, Binary, support::little);
Shiva Chen98f93892018-04-25 14:18:55 +0000125}
126
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000127void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
128 SmallVectorImpl<MCFixup> &Fixups,
129 const MCSubtargetInfo &STI) const {
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000130 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
131 // Get byte count of instruction.
132 unsigned Size = Desc.getSize();
133
Mandeep Singh Grangef0ebf22018-05-17 17:31:27 +0000134 if (MI.getOpcode() == RISCV::PseudoCALL ||
135 MI.getOpcode() == RISCV::PseudoTAIL) {
Shiva Chen98f93892018-04-25 14:18:55 +0000136 expandFunctionCall(MI, OS, Fixups, STI);
137 MCNumEmitted += 2;
138 return;
139 }
140
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000141 switch (Size) {
142 default:
143 llvm_unreachable("Unhandled encodeInstruction length!");
144 case 2: {
145 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000146 support::endian::write<uint16_t>(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000147 break;
148 }
149 case 4: {
150 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000151 support::endian::write(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000152 break;
153 }
154 }
155
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000156 ++MCNumEmitted; // Keep track of the # of mi's emitted.
157}
158
159unsigned
160RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
161 SmallVectorImpl<MCFixup> &Fixups,
162 const MCSubtargetInfo &STI) const {
163
164 if (MO.isReg())
165 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
166
167 if (MO.isImm())
168 return static_cast<unsigned>(MO.getImm());
169
170 llvm_unreachable("Unhandled expression!");
171 return 0;
172}
173
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000174unsigned
175RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
176 SmallVectorImpl<MCFixup> &Fixups,
177 const MCSubtargetInfo &STI) const {
178 const MCOperand &MO = MI.getOperand(OpNo);
179
180 if (MO.isImm()) {
181 unsigned Res = MO.getImm();
182 assert((Res & 1) == 0 && "LSB is non-zero");
183 return Res >> 1;
184 }
185
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000186 return getImmOpValue(MI, OpNo, Fixups, STI);
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000187}
188
189unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
190 SmallVectorImpl<MCFixup> &Fixups,
191 const MCSubtargetInfo &STI) const {
Shiva Chen43bfe842018-05-24 06:21:23 +0000192 bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax];
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000193 const MCOperand &MO = MI.getOperand(OpNo);
194
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000195 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
196 unsigned MIFrm = Desc.TSFlags & RISCVII::InstFormatMask;
197
Chih-Mao Chen5d94b252018-08-14 08:08:39 +0000198 // If the destination is an immediate, there is nothing to do.
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000199 if (MO.isImm())
200 return MO.getImm();
201
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000202 assert(MO.isExpr() &&
203 "getImmOpValue expects only expressions or immediates");
204 const MCExpr *Expr = MO.getExpr();
205 MCExpr::ExprKind Kind = Expr->getKind();
206 RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000207 bool RelaxCandidate = false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000208 if (Kind == MCExpr::Target) {
209 const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
210
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:
Alex Bradbury8d8d0a72018-02-22 13:24:25 +0000216 if (MIFrm == RISCVII::InstFormatI)
217 FixupKind = RISCV::fixup_riscv_lo12_i;
218 else if (MIFrm == RISCVII::InstFormatS)
219 FixupKind = RISCV::fixup_riscv_lo12_s;
220 else
221 llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
Kito Cheng5e8798f2019-01-21 05:27:09 +0000222 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000223 break;
224 case RISCVMCExpr::VK_RISCV_HI:
225 FixupKind = RISCV::fixup_riscv_hi20;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000226 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000227 break;
Ahmed Charles646ab872018-02-06 00:55:23 +0000228 case RISCVMCExpr::VK_RISCV_PCREL_LO:
Alex Bradbury8d8d0a72018-02-22 13:24:25 +0000229 if (MIFrm == RISCVII::InstFormatI)
230 FixupKind = RISCV::fixup_riscv_pcrel_lo12_i;
231 else if (MIFrm == RISCVII::InstFormatS)
232 FixupKind = RISCV::fixup_riscv_pcrel_lo12_s;
233 else
234 llvm_unreachable(
235 "VK_RISCV_PCREL_LO used with unexpected instruction format");
Kito Cheng5e8798f2019-01-21 05:27:09 +0000236 RelaxCandidate = true;
Ahmed Charles646ab872018-02-06 00:55:23 +0000237 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000238 case RISCVMCExpr::VK_RISCV_PCREL_HI:
239 FixupKind = RISCV::fixup_riscv_pcrel_hi20;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000240 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000241 break;
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000242 case RISCVMCExpr::VK_RISCV_GOT_HI:
243 FixupKind = RISCV::fixup_riscv_got_hi20;
244 break;
Shiva Chen98f93892018-04-25 14:18:55 +0000245 case RISCVMCExpr::VK_RISCV_CALL:
246 FixupKind = RISCV::fixup_riscv_call;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000247 RelaxCandidate = true;
Shiva Chen98f93892018-04-25 14:18:55 +0000248 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000249 }
250 } else if (Kind == MCExpr::SymbolRef &&
251 cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
252 if (Desc.getOpcode() == RISCV::JAL) {
253 FixupKind = RISCV::fixup_riscv_jal;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000254 } else if (MIFrm == RISCVII::InstFormatB) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000255 FixupKind = RISCV::fixup_riscv_branch;
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000256 } else if (MIFrm == RISCVII::InstFormatCJ) {
257 FixupKind = RISCV::fixup_riscv_rvc_jump;
258 } else if (MIFrm == RISCVII::InstFormatCB) {
259 FixupKind = RISCV::fixup_riscv_rvc_branch;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000260 }
261 }
262
263 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
264
265 Fixups.push_back(
266 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
267 ++MCNumFixups;
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000268
Kito Cheng5e8798f2019-01-21 05:27:09 +0000269 // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
270 // enabled and the current fixup will result in a relocation that may be
271 // relaxed.
272 if (EnableRelax && RelaxCandidate) {
273 const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
274 Fixups.push_back(
275 MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax),
276 MI.getLoc()));
277 ++MCNumFixups;
Shiva Chen43bfe842018-05-24 06:21:23 +0000278 }
279
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000280 return 0;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000281}
282
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000283#include "RISCVGenMCCodeEmitter.inc"