blob: 8a796a014b33ab56905da3bef9ab32826a48e4b2 [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVMCCodeEmitter.cpp - Convert RISCV code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the RISCVMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Alex Bradbury9d3f1252017-09-28 08:26:24 +000014#include "MCTargetDesc/RISCVBaseInfo.h"
15#include "MCTargetDesc/RISCVFixupKinds.h"
16#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000017#include "MCTargetDesc/RISCVMCTargetDesc.h"
18#include "llvm/ADT/Statistic.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/MC/MCAsmInfo.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCInst.h"
Shiva Chen98f93892018-04-25 14:18:55 +000024#include "llvm/MC/MCInstBuilder.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000025#include "llvm/MC/MCInstrInfo.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000026#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCSymbol.h"
Alex Bradbury9d3f1252017-09-28 08:26:24 +000028#include "llvm/Support/Casting.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000029#include "llvm/Support/EndianStream.h"
30#include "llvm/Support/raw_ostream.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "mccodeemitter"
35
36STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
Alex Bradbury9d3f1252017-09-28 08:26:24 +000037STATISTIC(MCNumFixups, "Number of MC fixups created");
Alex Bradbury6b2cca72016-11-01 23:47:30 +000038
39namespace {
40class RISCVMCCodeEmitter : public MCCodeEmitter {
41 RISCVMCCodeEmitter(const RISCVMCCodeEmitter &) = delete;
42 void operator=(const RISCVMCCodeEmitter &) = delete;
43 MCContext &Ctx;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000044 MCInstrInfo const &MCII;
Alex Bradbury6b2cca72016-11-01 23:47:30 +000045
46public:
Alex Bradbury9d3f1252017-09-28 08:26:24 +000047 RISCVMCCodeEmitter(MCContext &ctx, MCInstrInfo const &MCII)
48 : Ctx(ctx), MCII(MCII) {}
Alex Bradbury6b2cca72016-11-01 23:47:30 +000049
50 ~RISCVMCCodeEmitter() override {}
51
52 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
53 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const override;
55
Shiva Chen98f93892018-04-25 14:18:55 +000056 void expandFunctionCall(const MCInst &MI, raw_ostream &OS,
57 SmallVectorImpl<MCFixup> &Fixups,
58 const MCSubtargetInfo &STI) const;
59
Alex Bradbury6b2cca72016-11-01 23:47:30 +000060 /// TableGen'erated function for getting the binary encoding for an
61 /// instruction.
62 uint64_t getBinaryCodeForInstr(const MCInst &MI,
63 SmallVectorImpl<MCFixup> &Fixups,
64 const MCSubtargetInfo &STI) const;
65
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;
Alex Bradbury6758ecb2017-09-17 14:27:35 +000071
72 unsigned getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000075
Alex Bradbury8ab4a962017-09-17 14:36:28 +000076 unsigned getImmOpValue(const MCInst &MI, unsigned OpNo,
77 SmallVectorImpl<MCFixup> &Fixups,
78 const MCSubtargetInfo &STI) const;
Alex Bradbury6b2cca72016-11-01 23:47:30 +000079};
80} // end anonymous namespace
81
82MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
83 const MCRegisterInfo &MRI,
84 MCContext &Ctx) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +000085 return new RISCVMCCodeEmitter(Ctx, MCII);
Alex Bradbury6b2cca72016-11-01 23:47:30 +000086}
87
Mandeep Singh Grangddcb9562018-05-23 22:44:08 +000088// 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.
Shiva Chen98f93892018-04-25 14:18:55 +000095void RISCVMCCodeEmitter::expandFunctionCall(const MCInst &MI, raw_ostream &OS,
96 SmallVectorImpl<MCFixup> &Fixups,
97 const MCSubtargetInfo &STI) const {
98 MCInst TmpInst;
99 MCOperand Func = MI.getOperand(0);
Mandeep Singh Grangef0ebf22018-05-17 17:31:27 +0000100 unsigned Ra = (MI.getOpcode() == RISCV::PseudoTAIL) ? RISCV::X6 : RISCV::X1;
Shiva Chen98f93892018-04-25 14:18:55 +0000101 uint32_t Binary;
102
103 assert(Func.isExpr() && "Expected expression");
104
105 const MCExpr *Expr = Func.getExpr();
106
107 // Create function call expression CallExpr for AUIPC.
108 const MCExpr *CallExpr =
109 RISCVMCExpr::create(Expr, RISCVMCExpr::VK_RISCV_CALL, Ctx);
110
111 // Emit AUIPC Ra, Func with R_RISCV_CALL relocation type.
112 TmpInst = MCInstBuilder(RISCV::AUIPC)
113 .addReg(Ra)
114 .addOperand(MCOperand::createExpr(CallExpr));
115 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000116 support::endian::write(OS, Binary, support::little);
Shiva Chen98f93892018-04-25 14:18:55 +0000117
Sameer AbuAsale01e7112018-06-21 14:37:09 +0000118 if (MI.getOpcode() == RISCV::PseudoTAIL)
119 // Emit JALR X0, X6, 0
120 TmpInst = MCInstBuilder(RISCV::JALR).addReg(RISCV::X0).addReg(Ra).addImm(0);
121 else
122 // Emit JALR X1, X1, 0
123 TmpInst = MCInstBuilder(RISCV::JALR).addReg(Ra).addReg(Ra).addImm(0);
Shiva Chen98f93892018-04-25 14:18:55 +0000124 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}
127
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000128void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
129 SmallVectorImpl<MCFixup> &Fixups,
130 const MCSubtargetInfo &STI) const {
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000131 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
132 // Get byte count of instruction.
133 unsigned Size = Desc.getSize();
134
Mandeep Singh Grangef0ebf22018-05-17 17:31:27 +0000135 if (MI.getOpcode() == RISCV::PseudoCALL ||
136 MI.getOpcode() == RISCV::PseudoTAIL) {
Shiva Chen98f93892018-04-25 14:18:55 +0000137 expandFunctionCall(MI, OS, Fixups, STI);
138 MCNumEmitted += 2;
139 return;
140 }
141
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000142 switch (Size) {
143 default:
144 llvm_unreachable("Unhandled encodeInstruction length!");
145 case 2: {
146 uint16_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000147 support::endian::write<uint16_t>(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000148 break;
149 }
150 case 4: {
151 uint32_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Peter Collingbournee3f65292018-05-18 19:46:24 +0000152 support::endian::write(OS, Bits, support::little);
Alex Bradbury9f6aec42017-12-07 12:50:32 +0000153 break;
154 }
155 }
156
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000157 ++MCNumEmitted; // Keep track of the # of mi's emitted.
158}
159
160unsigned
161RISCVMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
162 SmallVectorImpl<MCFixup> &Fixups,
163 const MCSubtargetInfo &STI) const {
164
165 if (MO.isReg())
166 return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
167
168 if (MO.isImm())
169 return static_cast<unsigned>(MO.getImm());
170
171 llvm_unreachable("Unhandled expression!");
172 return 0;
173}
174
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000175unsigned
176RISCVMCCodeEmitter::getImmOpValueAsr1(const MCInst &MI, unsigned OpNo,
177 SmallVectorImpl<MCFixup> &Fixups,
178 const MCSubtargetInfo &STI) const {
179 const MCOperand &MO = MI.getOperand(OpNo);
180
181 if (MO.isImm()) {
182 unsigned Res = MO.getImm();
183 assert((Res & 1) == 0 && "LSB is non-zero");
184 return Res >> 1;
185 }
186
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000187 return getImmOpValue(MI, OpNo, Fixups, STI);
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000188}
189
190unsigned RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
191 SmallVectorImpl<MCFixup> &Fixups,
192 const MCSubtargetInfo &STI) const {
Shiva Chen43bfe842018-05-24 06:21:23 +0000193 bool EnableRelax = STI.getFeatureBits()[RISCV::FeatureRelax];
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000194 const MCOperand &MO = MI.getOperand(OpNo);
195
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000196 MCInstrDesc const &Desc = MCII.get(MI.getOpcode());
197 unsigned MIFrm = Desc.TSFlags & RISCVII::InstFormatMask;
198
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000199 // If the destination is an immediate, there is nothing to do
200 if (MO.isImm())
201 return MO.getImm();
202
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000203 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);
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");
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000222 break;
223 case RISCVMCExpr::VK_RISCV_HI:
224 FixupKind = RISCV::fixup_riscv_hi20;
225 break;
Ahmed Charles646ab872018-02-06 00:55:23 +0000226 case RISCVMCExpr::VK_RISCV_PCREL_LO:
Alex Bradbury8d8d0a72018-02-22 13:24:25 +0000227 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;
231 else
232 llvm_unreachable(
233 "VK_RISCV_PCREL_LO used with unexpected instruction format");
Ahmed Charles646ab872018-02-06 00:55:23 +0000234 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000235 case RISCVMCExpr::VK_RISCV_PCREL_HI:
236 FixupKind = RISCV::fixup_riscv_pcrel_hi20;
237 break;
Shiva Chen98f93892018-04-25 14:18:55 +0000238 case RISCVMCExpr::VK_RISCV_CALL:
239 FixupKind = RISCV::fixup_riscv_call;
240 break;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000241 }
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;
Alex Bradburyee7c7ec2017-10-19 14:29:03 +0000246 } else if (MIFrm == RISCVII::InstFormatB) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000247 FixupKind = RISCV::fixup_riscv_branch;
Alex Bradburyf8f4b902017-12-07 13:19:57 +0000248 } 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;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000252 }
253 }
254
255 assert(FixupKind != RISCV::fixup_riscv_invalid && "Unhandled expression!");
256
257 Fixups.push_back(
258 MCFixup::create(0, Expr, MCFixupKind(FixupKind), MI.getLoc()));
259 ++MCNumFixups;
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000260
Shiva Chen43bfe842018-05-24 06:21:23 +0000261 if (EnableRelax) {
262 if (FixupKind == RISCV::fixup_riscv_call) {
263 Fixups.push_back(
264 MCFixup::create(0, Expr, MCFixupKind(RISCV::fixup_riscv_relax),
265 MI.getLoc()));
266 ++MCNumFixups;
267 }
268 }
269
Alex Bradbury8ab4a962017-09-17 14:36:28 +0000270 return 0;
Alex Bradbury6758ecb2017-09-17 14:27:35 +0000271}
272
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000273#include "RISCVGenMCCodeEmitter.inc"