blob: 0fc775f63ed45c28e2fc0149c715f93cb7bffe74 [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
Lewis Revillaa79a3f2019-04-04 14:13:37 +000059 void expandAddTPRel(const MCInst &MI, raw_ostream &OS,
60 SmallVectorImpl<MCFixup> &Fixups,
61 const MCSubtargetInfo &STI) const;
62
Alex Bradbury6b2cca72016-11-01 23:47:30 +000063 /// TableGen'erated function for getting the binary encoding for an
64 /// instruction.
65 uint64_t getBinaryCodeForInstr(const MCInst &MI,
66 SmallVectorImpl<MCFixup> &Fixups,
67 const MCSubtargetInfo &STI) const;
68
69 /// Return binary encoding of operand. If the machine operand requires
70 /// relocation, record the relocation and return zero.
71 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
72 SmallVectorImpl<MCFixup> &Fixups,
73 const MCSubtargetInfo &STI) const;
Alex Bradbury6758ecb2017-09-17 14:27:35 +000074
75 unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000078
Alex Bradbury8ab4a962017-09-17 14:36:28 +000079 unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
80 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
Alex Bradbury6b2cca72016-11-01 23:47:30 +000082};
83} // end anonymous namespace
84
85MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
86 const MCRegisterInfo &MRI,
87 MCContext &Ctx) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +000088 return new RISCVMCCodeEmitter(Ctx, MCII);
Alex Bradbury6b2cca72016-11-01 23:47:30 +000089}
90
Lewis Revillcf748812019-06-26 10:35:58 +000091// Expand PseudoCALL(Reg) and PseudoTAIL to AUIPC and JALR with relocation
92// types. We expand PseudoCALL(Reg) and PseudoTAIL while encoding, meaning AUIPC
93// and JALR won't go through RISCV MC to MC compressed instruction
94// transformation. This is acceptable because AUIPC has no 16-bit form and
95// C_JALR have no immediate operand field. We let linker relaxation deal with
96// it. When linker relaxation enabled, AUIPC and JALR have chance relax to JAL.
97// If C extension is enabled, JAL has chance relax to C_JAL.
Shiva Chen98f93892018-04-25 14:18:55 +000098void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
99 SmallVectorImpl<MCFixup> &Fixups,
100 const MCSubtargetInfo &STI) const {
101 MCInst TmpInst;
Lewis Revillcf748812019-06-26 10:35:58 +0000102 MCOperand Func;
103 unsigned Ra;
104 if (MI.getOpcode() == RISCV::PseudoTAIL) {
105 Func = MI.getOperand(0);
106 Ra = RISCV::X6;
107 } else if (MI.getOpcode() == RISCV::PseudoCALLReg) {
108 Func = MI.getOperand(1);
109 Ra = MI.getOperand(0).getReg();
110 } else {
111 Func = MI.getOperand(0);
112 Ra = RISCV::X1;
113 }
Shiva Chen98f93892018-04-25 14:18:55 +0000114 uint32_t Binary;
115
116 assert(Func.isExpr() && "Expected expression");
117
Alex Bradbury44668ae2019-04-01 14:53:17 +0000118 const MCExpr *CallExpr = Func.getExpr();
Shiva Chen98f93892018-04-25 14:18:55 +0000119
120 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
121 TmpInst = MCInstBuilder(RISCV::AUIPC)
122 .addReg(Ra)
123 .addOperand(MCOperand::createExpr(CallExpr));
124 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000125 support::endian::write(OS, Binary, support::little);
Shiva Chen98f93892018-04-25 14:18:55 +0000126
Sameer AbuAsale01e7112018-06-21 14:37:09 +0000127 if (MI.getOpcode() == RISCV::PseudoTAIL)
128 // Emit JALR X0, X6, 0
129 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
130 else
Lewis Revillcf748812019-06-26 10:35:58 +0000131 // Emit JALR Ra, Ra, 0
Sameer AbuAsale01e7112018-06-21 14:37:09 +0000132 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
Shiva Chen98f93892018-04-25 14:18:55 +0000133 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000134 support::endian::write(OS, Binary, support::little);
Shiva Chen98f93892018-04-25 14:18:55 +0000135}
136
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000137// Expand PseudoAddTPRel to a simple ADD with the correct relocation.
138void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI, raw_ostream &OS,
139 SmallVectorImpl<MCFixup> &Fixups,
140 const MCSubtargetInfo &STI) const {
141 MCOperand DestReg = MI.getOperand(0);
142 MCOperand SrcReg = MI.getOperand(1);
143 MCOperand TPReg = MI.getOperand(2);
144 assert(TPReg.isReg() && TPReg.getReg() == RISCV::X4 &&
145 "Expected thread pointer as second input to TP-relative add");
146
147 MCOperand SrcSymbol = MI.getOperand(3);
148 assert(SrcSymbol.isExpr() &&
149 "Expected expression as third input to TP-relative add");
150
151 const RISCVMCExpr *Expr = dyn_cast<RISCVMCExpr>(SrcSymbol.getExpr());
152 assert(Expr && Expr->getKind() == RISCVMCExpr::VK_RISCV_TPREL_ADD &&
153 "Expected tprel_add relocation on TP-relative symbol");
154
155 // Emit the correct tprel_add relocation for the symbol.
156 Fixups.push_back(MCFixup::create(
157 0, Expr, MCFixupKind(RISCV::fixup_riscv_tprel_add), MI.getLoc()));
158
159 // Emit fixup_riscv_relax for tprel_add where the relax feature is enabled.
160 if (STI.getFeatureBits()[RISCV::FeatureRelax]) {
161 const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
162 Fixups.push_back(MCFixup::create(
163 0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax), MI.getLoc()));
164 }
165
166 // Emit a normal ADD instruction with the given operands.
167 MCInst TmpInst = MCInstBuilder(RISCV::ADD)
168 .addOperand(DestReg)
169 .addOperand(SrcReg)
170 .addOperand(TPReg);
171 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
172 support::endian::write(OS, Binary, support::little);
173}
174
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000175void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
176 SmallVectorImpl<MCFixup> &Fixups,
177 const MCSubtargetInfo &STI) const {
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000178 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
179 // Get byte count of instruction.
180 unsigned Size = Desc.getSize();
181
Lewis Revillcf748812019-06-26 10:35:58 +0000182 if (MI.getOpcode() == RISCV::PseudoCALLReg ||
183 MI.getOpcode() == RISCV::PseudoCALL ||
Mandeep Singh Grangef0ebf22018-05-17 17:31:27 +0000184 MI.getOpcode() == RISCV::PseudoTAIL) {
Shiva Chen98f93892018-04-25 14:18:55 +0000185 expandFunctionCall(MI, OS, Fixups, STI);
186 MCNumEmitted += 2;
187 return;
188 }
189
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000190 if (MI.getOpcode() == RISCV::PseudoAddTPRel) {
191 expandAddTPRel(MI, OS, Fixups, STI);
192 MCNumEmitted += 1;
193 return;
194 }
195
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000196 switch (Size) {
197 default:
198 llvm_unreachable("Unhandled encodeInstruction length!");
199 case 2: {
200 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000201 support::endian::write<uint16_t>(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000202 break;
203 }
204 case 4: {
205 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000206 support::endian::write(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000207 break;
208 }
209 }
210
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000211 ++MCNumEmitted; // Keep track of the # of mi's emitted.
212}
213
214unsigned
215RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
216 SmallVectorImpl<MCFixup> &Fixups,
217 const MCSubtargetInfo &STI) const {
218
219 if (MO.isReg())
220 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
221
222 if (MO.isImm())
223 return static_cast<unsigned>(MO.getImm());
224
225 llvm_unreachable("Unhandled expression!");
226 return 0;
227}
228
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000229unsigned
230RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
231 SmallVectorImpl<MCFixup> &Fixups,
232 const MCSubtargetInfo &STI) const {
233 const MCOperand &MO = MI.getOperand(OpNo);
234
235 if (MO.isImm()) {
236 unsigned Res = MO.getImm();
237 assert((Res & 1) == 0 && "LSB is non-zero");
238 return Res >> 1;
239 }
240
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000241 return getImmOpValue(MI, OpNo, Fixups, STI);
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000242}
243
244unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
245 SmallVectorImpl<MCFixup> &Fixups,
246 const MCSubtargetInfo &STI) const {
Shiva Chen43bfe842018-05-24 06:21:23 +0000247 bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax];
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000248 const MCOperand &MO = MI.getOperand(OpNo);
249
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000250 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
251 unsigned MIFrm = Desc.TSFlags & RISCVII::InstFormatMask;
252
Chih-Mao Chen5d94b252018-08-14 08:08:39 +0000253 // If the destination is an immediate, there is nothing to do.
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000254 if (MO.isImm())
255 return MO.getImm();
256
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000257 assert(MO.isExpr() &&
258 "getImmOpValue expects only expressions or immediates");
259 const MCExpr *Expr = MO.getExpr();
260 MCExpr::ExprKind Kind = Expr->getKind();
261 RISCV::Fixups FixupKind = RISCV::fixup_riscv_invalid;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000262 bool RelaxCandidate = false;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000263 if (Kind == MCExpr::Target) {
264 const RISCVMCExpr *RVExpr = cast<RISCVMCExpr>(Expr);
265
266 switch (RVExpr->getKind()) {
267 case RISCVMCExpr::VK_RISCV_None:
268 case RISCVMCExpr::VK_RISCV_Invalid:
269 llvm_unreachable("Unhandled fixup kind!");
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000270 case RISCVMCExpr::VK_RISCV_TPREL_ADD:
271 // tprel_add is only used to indicate that a relocation should be emitted
272 // for an add instruction used in TP-relative addressing. It should not be
273 // expanded as if representing an actual instruction operand and so to
274 // encounter it here is an error.
275 llvm_unreachable(
276 "VK_RISCV_TPREL_ADD should not represent an instruction operand");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000277 case RISCVMCExpr::VK_RISCV_LO:
Alex Bradbury8d8d0a72018-02-22 13:24:25 +0000278 if (MIFrm == RISCVII::InstFormatI)
279 FixupKind = RISCV::fixup_riscv_lo12_i;
280 else if (MIFrm == RISCVII::InstFormatS)
281 FixupKind = RISCV::fixup_riscv_lo12_s;
282 else
283 llvm_unreachable("VK_RISCV_LO used with unexpected instruction format");
Kito Cheng5e8798f2019-01-21 05:27:09 +0000284 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000285 break;
286 case RISCVMCExpr::VK_RISCV_HI:
287 FixupKind = RISCV::fixup_riscv_hi20;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000288 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000289 break;
Ahmed Charles646ab872018-02-06 00:55:23 +0000290 case RISCVMCExpr::VK_RISCV_PCREL_LO:
Alex Bradbury8d8d0a72018-02-22 13:24:25 +0000291 if (MIFrm == RISCVII::InstFormatI)
292 FixupKind = RISCV::fixup_riscv_pcrel_lo12_i;
293 else if (MIFrm == RISCVII::InstFormatS)
294 FixupKind = RISCV::fixup_riscv_pcrel_lo12_s;
295 else
296 llvm_unreachable(
297 "VK_RISCV_PCREL_LO used with unexpected instruction format");
Kito Cheng5e8798f2019-01-21 05:27:09 +0000298 RelaxCandidate = true;
Ahmed Charles646ab872018-02-06 00:55:23 +0000299 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000300 case RISCVMCExpr::VK_RISCV_PCREL_HI:
301 FixupKind = RISCV::fixup_riscv_pcrel_hi20;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000302 RelaxCandidate = true;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000303 break;
Alex Bradbury8eb87e52019-02-15 09:43:46 +0000304 case RISCVMCExpr::VK_RISCV_GOT_HI:
305 FixupKind = RISCV::fixup_riscv_got_hi20;
306 break;
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000307 case RISCVMCExpr::VK_RISCV_TPREL_LO:
308 if (MIFrm == RISCVII::InstFormatI)
309 FixupKind = RISCV::fixup_riscv_tprel_lo12_i;
310 else if (MIFrm == RISCVII::InstFormatS)
311 FixupKind = RISCV::fixup_riscv_tprel_lo12_s;
312 else
313 llvm_unreachable(
314 "VK_RISCV_TPREL_LO used with unexpected instruction format");
315 RelaxCandidate = true;
316 break;
317 case RISCVMCExpr::VK_RISCV_TPREL_HI:
318 FixupKind = RISCV::fixup_riscv_tprel_hi20;
319 RelaxCandidate = true;
320 break;
Lewis Revilldf3cb472019-04-23 14:46:13 +0000321 case RISCVMCExpr::VK_RISCV_TLS_GOT_HI:
322 FixupKind = RISCV::fixup_riscv_tls_got_hi20;
323 break;
324 case RISCVMCExpr::VK_RISCV_TLS_GD_HI:
325 FixupKind = RISCV::fixup_riscv_tls_gd_hi20;
326 break;
Shiva Chen98f93892018-04-25 14:18:55 +0000327 case RISCVMCExpr::VK_RISCV_CALL:
328 FixupKind = RISCV::fixup_riscv_call;
Kito Cheng5e8798f2019-01-21 05:27:09 +0000329 RelaxCandidate = true;
Shiva Chen98f93892018-04-25 14:18:55 +0000330 break;
Alex Bradburyf8078f62019-04-02 12:47:20 +0000331 case RISCVMCExpr::VK_RISCV_CALL_PLT:
332 FixupKind = RISCV::fixup_riscv_call_plt;
333 RelaxCandidate = true;
334 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000335 }
336 } else if (Kind == MCExpr::SymbolRef &&
337 cast<MCSymbolRefExpr>(Expr)->getKind() == MCSymbolRefExpr::VK_None) {
338 if (Desc.getOpcode() == RISCV::JAL) {
339 FixupKind = RISCV::fixup_riscv_jal;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000340 } else if (MIFrm == RISCVII::InstFormatB) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000341 FixupKind = RISCV::fixup_riscv_branch;
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000342 } else if (MIFrm == RISCVII::InstFormatCJ) {
343 FixupKind = RISCV::fixup_riscv_rvc_jump;
344 } else if (MIFrm == RISCVII::InstFormatCB) {
345 FixupKind = RISCV::fixup_riscv_rvc_branch;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000346 }
347 }
348
349 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
350
351 Fixups.push_back(
352 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
353 ++MCNumFixups;
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000354
Kito Cheng5e8798f2019-01-21 05:27:09 +0000355 // Ensure an R_RISCV_RELAX relocation will be emitted if linker relaxation is
356 // enabled and the current fixup will result in a relocation that may be
357 // relaxed.
358 if (EnableRelax && RelaxCandidate) {
359 const MCConstantExpr *Dummy = MCConstantExpr::create(0, Ctx);
360 Fixups.push_back(
361 MCFixup::create(0, Dummy, MCFixupKind(RISCV::fixup_riscv_relax),
362 MI.getLoc()));
363 ++MCNumFixups;
Shiva Chen43bfe842018-05-24 06:21:23 +0000364 }
365
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000366 return 0;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000367}
368
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000369#include "RISCVGenMCCodeEmitter.inc"