1//===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the MipsMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MipsMCCodeEmitter.h"
14#include "MCTargetDesc/MipsBaseInfo.h"
15#include "MCTargetDesc/MipsFixupKinds.h"
16#include "MCTargetDesc/MipsMCAsmInfo.h"
17#include "MCTargetDesc/MipsMCTargetDesc.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFixup.h"
24#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstrDesc.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCSubtargetInfo.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/EndianStream.h"
31#include "llvm/Support/raw_ostream.h"
32#include <cassert>
33#include <cstdint>
34
35using namespace llvm;
36
37#define DEBUG_TYPE "mccodeemitter"
38
39#define GET_INSTRMAP_INFO
40#include "MipsGenInstrInfo.inc"
41#undef GET_INSTRMAP_INFO
42
43namespace llvm {
44
45MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
46 MCContext &Ctx) {
47 return new MipsMCCodeEmitter(MCII, Ctx, false);
48}
49
50MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
51 MCContext &Ctx) {
52 return new MipsMCCodeEmitter(MCII, Ctx, true);
53}
54
55} // end namespace llvm
56
57static void addFixup(SmallVectorImpl<MCFixup> &Fixups, uint32_t Offset,
58 const MCExpr *Value, uint16_t Kind) {
59 bool PCRel = false;
60 switch (Kind) {
61 case Mips::fixup_Mips_PC16:
62 case Mips::fixup_Mips_Branch_PCRel:
63 case Mips::fixup_MIPS_PC18_S3:
64 case Mips::fixup_MIPS_PC19_S2:
65 case Mips::fixup_MIPS_PC21_S2:
66 case Mips::fixup_MIPS_PC26_S2:
67 case Mips::fixup_MIPS_PCHI16:
68 case Mips::fixup_MIPS_PCLO16:
69 case Mips::fixup_MICROMIPS_PC7_S1:
70 case Mips::fixup_MICROMIPS_PC10_S1:
71 case Mips::fixup_MICROMIPS_PC16_S1:
72 case Mips::fixup_MICROMIPS_PC26_S1:
73 case Mips::fixup_MICROMIPS_PC19_S2:
74 case Mips::fixup_MICROMIPS_PC18_S3:
75 case Mips::fixup_MICROMIPS_PC21_S1:
76 PCRel = true;
77 }
78 Fixups.push_back(Elt: MCFixup::create(Offset, Value, Kind, PCRel));
79}
80
81// If the D<shift> instruction has a shift amount that is greater
82// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
83static void LowerLargeShift(MCInst& Inst) {
84 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
85 assert(Inst.getOperand(2).isImm());
86
87 int64_t Shift = Inst.getOperand(i: 2).getImm();
88 if (Shift <= 31)
89 return; // Do nothing
90 Shift -= 32;
91
92 // saminus32
93 Inst.getOperand(i: 2).setImm(Shift);
94
95 switch (Inst.getOpcode()) {
96 default:
97 // Calling function is not synchronized
98 llvm_unreachable("Unexpected shift instruction");
99 case Mips::DSLL:
100 Inst.setOpcode(Mips::DSLL32);
101 return;
102 case Mips::DSRL:
103 Inst.setOpcode(Mips::DSRL32);
104 return;
105 case Mips::DSRA:
106 Inst.setOpcode(Mips::DSRA32);
107 return;
108 case Mips::DROTR:
109 Inst.setOpcode(Mips::DROTR32);
110 return;
111 }
112}
113
114// Fix a bad compact branch encoding for beqc/bnec.
115void MipsMCCodeEmitter::LowerCompactBranch(MCInst& Inst) const {
116 // Encoding may be illegal !(rs < rt), but this situation is
117 // easily fixed.
118 MCRegister RegOp0 = Inst.getOperand(i: 0).getReg();
119 MCRegister RegOp1 = Inst.getOperand(i: 1).getReg();
120
121 unsigned Reg0 = Ctx.getRegisterInfo()->getEncodingValue(Reg: RegOp0);
122 unsigned Reg1 = Ctx.getRegisterInfo()->getEncodingValue(Reg: RegOp1);
123
124 if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC ||
125 Inst.getOpcode() == Mips::BNEC64 || Inst.getOpcode() == Mips::BEQC64) {
126 assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
127 if (Reg0 < Reg1)
128 return;
129 } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
130 if (Reg0 >= Reg1)
131 return;
132 } else if (Inst.getOpcode() == Mips::BNVC_MMR6 ||
133 Inst.getOpcode() == Mips::BOVC_MMR6) {
134 if (Reg1 >= Reg0)
135 return;
136 } else
137 llvm_unreachable("Cannot rewrite unknown branch!");
138
139 Inst.getOperand(i: 0).setReg(RegOp1);
140 Inst.getOperand(i: 1).setReg(RegOp0);
141}
142
143bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
144 return STI.hasFeature(Feature: Mips::FeatureMicroMips);
145}
146
147bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
148 return STI.hasFeature(Feature: Mips::FeatureMips32r6);
149}
150
151bool MipsMCCodeEmitter::isNanoMips(const MCSubtargetInfo &STI) const {
152 return STI.hasFeature(Feature: Mips::FeatureNanoMips);
153}
154
155void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
156 OS << (char)C;
157}
158
159/// encodeInstruction - Emit the instruction.
160/// Size the instruction with Desc.getSize().
161void MipsMCCodeEmitter::encodeInstruction(const MCInst &MI,
162 SmallVectorImpl<char> &CB,
163 SmallVectorImpl<MCFixup> &Fixups,
164 const MCSubtargetInfo &STI) const {
165 // Non-pseudo instructions that get changed for direct object
166 // only based on operand values.
167 // If this list of instructions get much longer we will move
168 // the check to a function call. Until then, this is more efficient.
169 MCInst TmpInst = MI;
170 switch (MI.getOpcode()) {
171 // If shift amount is >= 32 it the inst needs to be lowered further
172 case Mips::DSLL:
173 case Mips::DSRL:
174 case Mips::DSRA:
175 case Mips::DROTR:
176 LowerLargeShift(Inst&: TmpInst);
177 break;
178 // Compact branches, enforce encoding restrictions.
179 case Mips::BEQC:
180 case Mips::BNEC:
181 case Mips::BEQC64:
182 case Mips::BNEC64:
183 case Mips::BOVC:
184 case Mips::BOVC_MMR6:
185 case Mips::BNVC:
186 case Mips::BNVC_MMR6:
187 LowerCompactBranch(Inst&: TmpInst);
188 }
189
190 size_t N = Fixups.size();
191 uint64_t Binary = getBinaryCodeForInstr(MI: TmpInst, Fixups, STI);
192
193 // Check for unimplemented opcodes.
194 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
195 // so we have to special check for them.
196 const unsigned Opcode = TmpInst.getOpcode();
197 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
198 (Opcode != Mips::SLL_MM) && (Opcode != Mips::SLL_MMR6) && !Binary)
199 llvm_unreachable("unimplemented opcode in encodeInstruction()");
200
201 int NewOpcode = -1;
202 if (isMicroMips(STI)) {
203 if (isMips32r6(STI)) {
204 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, inArch: Mips::Arch_micromipsr6);
205 if (NewOpcode == -1)
206 NewOpcode = Mips::Std2MicroMipsR6(Opcode, inArch: Mips::Arch_micromipsr6);
207 }
208 else
209 NewOpcode = Mips::Std2MicroMips(Opcode, inArch: Mips::Arch_micromips);
210
211 // Check whether it is Dsp instruction.
212 if (NewOpcode == -1)
213 NewOpcode = Mips::Dsp2MicroMips(Opcode, inArch: Mips::Arch_mmdsp);
214
215 if (NewOpcode != -1) {
216 if (Fixups.size() > N)
217 Fixups.pop_back();
218
219 TmpInst.setOpcode (NewOpcode);
220 Binary = getBinaryCodeForInstr(MI: TmpInst, Fixups, STI);
221 }
222
223 if (((MI.getOpcode() == Mips::MOVEP_MM) ||
224 (MI.getOpcode() == Mips::MOVEP_MMR6))) {
225 unsigned RegPair = getMovePRegPairOpValue(MI, OpNo: 0, Fixups, STI);
226 Binary = (Binary & 0xFFFFFC7F) | (RegPair << 7);
227 }
228 }
229
230 const MCInstrDesc &Desc = MCII.get(Opcode: TmpInst.getOpcode());
231
232 // Get byte count of instruction
233 unsigned Size = Desc.getSize();
234 if (!Size)
235 llvm_unreachable("Desc.getSize() returns 0");
236
237 auto Endian =
238 IsLittleEndian ? llvm::endianness::little : llvm::endianness::big;
239 if (Size == 2) {
240 support::endian::write<uint16_t>(Out&: CB, V: Binary, E: Endian);
241 } else if (IsLittleEndian && Size == 4 &&
242 (isMicroMips(STI) || isNanoMips(STI))) {
243 support::endian::write<uint16_t>(Out&: CB, V: Binary >> 16, E: Endian);
244 support::endian::write<uint16_t>(Out&: CB, V: Binary & 0xffff, E: Endian);
245 } else if (IsLittleEndian && Size == 6 && isNanoMips(STI)) {
246 support::endian::write<uint16_t>(Out&: CB, V: Binary >> 32, E: Endian);
247 support::endian::write<uint16_t>(Out&: CB, V: Binary >> 16, E: Endian);
248 support::endian::write<uint16_t>(Out&: CB, V: Binary & 0xffff, E: Endian);
249 } else {
250 support::endian::write<uint32_t>(Out&: CB, V: Binary, E: Endian);
251 }
252}
253
254/// getBranchTargetOpValue - Return binary encoding of the branch
255/// target operand. If the machine operand requires relocation,
256/// record the relocation and return zero.
257unsigned MipsMCCodeEmitter::
258getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
259 SmallVectorImpl<MCFixup> &Fixups,
260 const MCSubtargetInfo &STI) const {
261 const MCOperand &MO = MI.getOperand(i: OpNo);
262
263 // If the destination is an immediate, divide by 4.
264 if (MO.isImm()) return MO.getImm() >> 2;
265
266 assert(MO.isExpr() &&
267 "getBranchTargetOpValue expects only expressions or immediates");
268
269 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
270 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
271 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_Mips_PC16);
272 return 0;
273}
274
275/// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
276/// target operand. If the machine operand requires relocation,
277/// record the relocation and return zero.
278unsigned MipsMCCodeEmitter::
279getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
280 SmallVectorImpl<MCFixup> &Fixups,
281 const MCSubtargetInfo &STI) const {
282 const MCOperand &MO = MI.getOperand(i: OpNo);
283
284 // If the destination is an immediate, divide by 2.
285 if (MO.isImm()) return MO.getImm() >> 1;
286
287 assert(MO.isExpr() &&
288 "getBranchTargetOpValue expects only expressions or immediates");
289
290 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
291 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
292 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_Mips_PC16);
293 return 0;
294}
295
296/// getBranchTargetOpValueMMR6 - Return binary encoding of the branch
297/// target operand. If the machine operand requires relocation,
298/// record the relocation and return zero.
299unsigned MipsMCCodeEmitter::
300getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
301 SmallVectorImpl<MCFixup> &Fixups,
302 const MCSubtargetInfo &STI) const {
303 const MCOperand &MO = MI.getOperand(i: OpNo);
304
305 // If the destination is an immediate, divide by 2.
306 if (MO.isImm())
307 return MO.getImm() >> 1;
308
309 assert(MO.isExpr() &&
310 "getBranchTargetOpValueMMR6 expects only expressions or immediates");
311
312 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
313 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -2, Ctx), Ctx);
314 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_Mips_PC16);
315 return 0;
316}
317
318/// getBranchTargetOpValueLsl2MMR6 - Return binary encoding of the branch
319/// target operand. If the machine operand requires relocation,
320/// record the relocation and return zero.
321unsigned MipsMCCodeEmitter::
322getBranchTargetOpValueLsl2MMR6(const MCInst &MI, unsigned OpNo,
323 SmallVectorImpl<MCFixup> &Fixups,
324 const MCSubtargetInfo &STI) const {
325 const MCOperand &MO = MI.getOperand(i: OpNo);
326
327 // If the destination is an immediate, divide by 4.
328 if (MO.isImm())
329 return MO.getImm() >> 2;
330
331 assert(MO.isExpr() &&
332 "getBranchTargetOpValueLsl2MMR6 expects only expressions or immediates");
333
334 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
335 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
336 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_Mips_PC16);
337 return 0;
338}
339
340/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
341/// target operand. If the machine operand requires relocation,
342/// record the relocation and return zero.
343unsigned MipsMCCodeEmitter::
344getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
345 SmallVectorImpl<MCFixup> &Fixups,
346 const MCSubtargetInfo &STI) const {
347 const MCOperand &MO = MI.getOperand(i: OpNo);
348
349 // If the destination is an immediate, divide by 2.
350 if (MO.isImm()) return MO.getImm() >> 1;
351
352 assert(MO.isExpr() &&
353 "getBranchTargetOpValueMM expects only expressions or immediates");
354
355 const MCExpr *Expr = MO.getExpr();
356 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_MICROMIPS_PC7_S1);
357 return 0;
358}
359
360/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
361/// 10-bit branch target operand. If the machine operand requires relocation,
362/// record the relocation and return zero.
363unsigned MipsMCCodeEmitter::
364getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
365 SmallVectorImpl<MCFixup> &Fixups,
366 const MCSubtargetInfo &STI) const {
367 const MCOperand &MO = MI.getOperand(i: OpNo);
368
369 // If the destination is an immediate, divide by 2.
370 if (MO.isImm()) return MO.getImm() >> 1;
371
372 assert(MO.isExpr() &&
373 "getBranchTargetOpValuePC10 expects only expressions or immediates");
374
375 const MCExpr *Expr = MO.getExpr();
376 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_MICROMIPS_PC10_S1);
377 return 0;
378}
379
380/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
381/// target operand. If the machine operand requires relocation,
382/// record the relocation and return zero.
383unsigned MipsMCCodeEmitter::
384getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
385 SmallVectorImpl<MCFixup> &Fixups,
386 const MCSubtargetInfo &STI) const {
387 const MCOperand &MO = MI.getOperand(i: OpNo);
388
389 // If the destination is an immediate, divide by 2.
390 if (MO.isImm()) return MO.getImm() >> 1;
391
392 assert(MO.isExpr() &&
393 "getBranchTargetOpValueMM expects only expressions or immediates");
394
395 const MCExpr *Expr = MO.getExpr();
396 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_MICROMIPS_PC16_S1);
397 return 0;
398}
399
400/// getBranchTarget21OpValue - Return binary encoding of the branch
401/// target operand. If the machine operand requires relocation,
402/// record the relocation and return zero.
403unsigned MipsMCCodeEmitter::
404getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
405 SmallVectorImpl<MCFixup> &Fixups,
406 const MCSubtargetInfo &STI) const {
407 const MCOperand &MO = MI.getOperand(i: OpNo);
408
409 // If the destination is an immediate, divide by 4.
410 if (MO.isImm()) return MO.getImm() >> 2;
411
412 assert(MO.isExpr() &&
413 "getBranchTarget21OpValue expects only expressions or immediates");
414
415 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
416 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
417 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_MIPS_PC21_S2);
418 return 0;
419}
420
421/// getBranchTarget21OpValueMM - Return binary encoding of the branch
422/// target operand for microMIPS. If the machine operand requires
423/// relocation, record the relocation and return zero.
424unsigned MipsMCCodeEmitter::
425getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
426 SmallVectorImpl<MCFixup> &Fixups,
427 const MCSubtargetInfo &STI) const {
428 const MCOperand &MO = MI.getOperand(i: OpNo);
429
430 // If the destination is an immediate, divide by 4.
431 if (MO.isImm()) return MO.getImm() >> 2;
432
433 assert(MO.isExpr() &&
434 "getBranchTarget21OpValueMM expects only expressions or immediates");
435
436 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
437 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
438 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_MICROMIPS_PC21_S1);
439 return 0;
440}
441
442/// getBranchTarget26OpValue - Return binary encoding of the branch
443/// target operand. If the machine operand requires relocation,
444/// record the relocation and return zero.
445unsigned MipsMCCodeEmitter::
446getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
447 SmallVectorImpl<MCFixup> &Fixups,
448 const MCSubtargetInfo &STI) const {
449 const MCOperand &MO = MI.getOperand(i: OpNo);
450
451 // If the destination is an immediate, divide by 4.
452 if (MO.isImm()) return MO.getImm() >> 2;
453
454 assert(MO.isExpr() &&
455 "getBranchTarget26OpValue expects only expressions or immediates");
456
457 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
458 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
459 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_MIPS_PC26_S2);
460 return 0;
461}
462
463/// getBranchTarget26OpValueMM - Return binary encoding of the branch
464/// target operand. If the machine operand requires relocation,
465/// record the relocation and return zero.
466unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
467 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
468 const MCSubtargetInfo &STI) const {
469 const MCOperand &MO = MI.getOperand(i: OpNo);
470
471 // If the destination is an immediate, divide by 2.
472 if (MO.isImm())
473 return MO.getImm() >> 1;
474
475 assert(MO.isExpr() &&
476 "getBranchTarget26OpValueMM expects only expressions or immediates");
477
478 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
479 LHS: MO.getExpr(), RHS: MCConstantExpr::create(Value: -4, Ctx), Ctx);
480 addFixup(Fixups, Offset: 0, Value: FixupExpression, Kind: Mips::fixup_MICROMIPS_PC26_S1);
481 return 0;
482}
483
484/// getJumpOffset16OpValue - Return binary encoding of the jump
485/// target operand. If the machine operand requires relocation,
486/// record the relocation and return zero.
487unsigned MipsMCCodeEmitter::
488getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
489 SmallVectorImpl<MCFixup> &Fixups,
490 const MCSubtargetInfo &STI) const {
491 const MCOperand &MO = MI.getOperand(i: OpNo);
492
493 if (MO.isImm()) return MO.getImm();
494
495 assert(MO.isExpr() &&
496 "getJumpOffset16OpValue expects only expressions or an immediate");
497
498 const MCExpr *Expr = MO.getExpr();
499 Mips::Fixups FixupKind =
500 isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 : Mips::fixup_Mips_LO16;
501 addFixup(Fixups, Offset: 0, Value: Expr, Kind: MCFixupKind(FixupKind));
502 return 0;
503}
504
505/// getJumpTargetOpValue - Return binary encoding of the jump
506/// target operand. If the machine operand requires relocation,
507/// record the relocation and return zero.
508unsigned MipsMCCodeEmitter::
509getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
510 SmallVectorImpl<MCFixup> &Fixups,
511 const MCSubtargetInfo &STI) const {
512 const MCOperand &MO = MI.getOperand(i: OpNo);
513 // If the destination is an immediate, divide by 4.
514 if (MO.isImm()) return MO.getImm()>>2;
515
516 assert(MO.isExpr() &&
517 "getJumpTargetOpValue expects only expressions or an immediate");
518
519 const MCExpr *Expr = MO.getExpr();
520 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_Mips_26);
521 return 0;
522}
523
524unsigned MipsMCCodeEmitter::
525getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
526 SmallVectorImpl<MCFixup> &Fixups,
527 const MCSubtargetInfo &STI) const {
528 const MCOperand &MO = MI.getOperand(i: OpNo);
529 // If the destination is an immediate, divide by 2.
530 if (MO.isImm()) return MO.getImm() >> 1;
531
532 assert(MO.isExpr() &&
533 "getJumpTargetOpValueMM expects only expressions or an immediate");
534
535 const MCExpr *Expr = MO.getExpr();
536 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_MICROMIPS_26_S1);
537 return 0;
538}
539
540unsigned MipsMCCodeEmitter::
541getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
542 SmallVectorImpl<MCFixup> &Fixups,
543 const MCSubtargetInfo &STI) const {
544 const MCOperand &MO = MI.getOperand(i: OpNo);
545 if (MO.isImm()) {
546 // The immediate is encoded as 'immediate << 2'.
547 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
548 assert((Res & 3) == 0);
549 return Res >> 2;
550 }
551
552 assert(MO.isExpr() &&
553 "getUImm5Lsl2Encoding expects only expressions or an immediate");
554
555 return 0;
556}
557
558unsigned MipsMCCodeEmitter::
559getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
560 SmallVectorImpl<MCFixup> &Fixups,
561 const MCSubtargetInfo &STI) const {
562 const MCOperand &MO = MI.getOperand(i: OpNo);
563 if (MO.isImm()) {
564 int Value = MO.getImm();
565 return Value >> 2;
566 }
567
568 return 0;
569}
570
571unsigned MipsMCCodeEmitter::
572getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
573 SmallVectorImpl<MCFixup> &Fixups,
574 const MCSubtargetInfo &STI) const {
575 const MCOperand &MO = MI.getOperand(i: OpNo);
576 if (MO.isImm()) {
577 unsigned Value = MO.getImm();
578 return Value >> 2;
579 }
580
581 return 0;
582}
583
584unsigned MipsMCCodeEmitter::
585getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
586 SmallVectorImpl<MCFixup> &Fixups,
587 const MCSubtargetInfo &STI) const {
588 const MCOperand &MO = MI.getOperand(i: OpNo);
589 if (MO.isImm()) {
590 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
591 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
592 }
593
594 return 0;
595}
596
597unsigned MipsMCCodeEmitter::
598getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
599 const MCSubtargetInfo &STI) const {
600 MCExpr::ExprKind Kind = Expr->getKind();
601 if (Kind == MCExpr::Specifier) {
602 const auto *MipsExpr = cast<MCSpecifierExpr>(Val: Expr);
603
604 Mips::Fixups FixupKind = Mips::Fixups(0);
605 switch (MipsExpr->getSpecifier()) {
606 case Mips::S_None:
607 case Mips::S_Special:
608 llvm_unreachable("Unhandled fixup kind!");
609 break;
610 case Mips::S_DTPREL:
611 // MEK_DTPREL is used for marking TLS DIEExpr only
612 // and contains a regular sub-expression.
613 return getExprOpValue(Expr: MipsExpr->getSubExpr(), Fixups, STI);
614 case Mips::S_CALL_HI16:
615 FixupKind = Mips::fixup_Mips_CALL_HI16;
616 break;
617 case Mips::S_CALL_LO16:
618 FixupKind = Mips::fixup_Mips_CALL_LO16;
619 break;
620 case Mips::S_DTPREL_HI:
621 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
622 : Mips::fixup_Mips_DTPREL_HI;
623 break;
624 case Mips::S_DTPREL_LO:
625 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
626 : Mips::fixup_Mips_DTPREL_LO;
627 break;
628 case Mips::S_GOTTPREL:
629 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOTTPREL
630 : Mips::fixup_Mips_GOTTPREL;
631 break;
632 case Mips::S_GOT:
633 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
634 : Mips::fixup_Mips_GOT;
635 break;
636 case Mips::S_GOT_CALL:
637 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
638 : Mips::fixup_Mips_CALL16;
639 break;
640 case Mips::S_GOT_DISP:
641 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
642 : Mips::fixup_Mips_GOT_DISP;
643 break;
644 case Mips::S_GOT_HI16:
645 FixupKind = Mips::fixup_Mips_GOT_HI16;
646 break;
647 case Mips::S_GOT_LO16:
648 FixupKind = Mips::fixup_Mips_GOT_LO16;
649 break;
650 case Mips::S_GOT_PAGE:
651 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
652 : Mips::fixup_Mips_GOT_PAGE;
653 break;
654 case Mips::S_GOT_OFST:
655 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
656 : Mips::fixup_Mips_GOT_OFST;
657 break;
658 case Mips::S_GPREL:
659 FixupKind = Mips::fixup_Mips_GPREL16;
660 break;
661 case Mips::S_LO:
662 // Check for %lo(%neg(%gp_rel(X)))
663 if (Mips::isGpOff(E: *MipsExpr))
664 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_LO
665 : Mips::fixup_Mips_GPOFF_LO;
666 else
667 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
668 : Mips::fixup_Mips_LO16;
669 break;
670 case Mips::S_HIGHEST:
671 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHEST
672 : Mips::fixup_Mips_HIGHEST;
673 break;
674 case Mips::S_HIGHER:
675 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHER
676 : Mips::fixup_Mips_HIGHER;
677 break;
678 case Mips::S_HI:
679 // Check for %hi(%neg(%gp_rel(X)))
680 if (Mips::isGpOff(E: *MipsExpr))
681 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_HI
682 : Mips::fixup_Mips_GPOFF_HI;
683 else
684 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
685 : Mips::fixup_Mips_HI16;
686 break;
687 case Mips::S_PCREL_HI16:
688 FixupKind = Mips::fixup_MIPS_PCHI16;
689 break;
690 case Mips::S_PCREL_LO16:
691 FixupKind = Mips::fixup_MIPS_PCLO16;
692 break;
693 case Mips::S_TLSGD:
694 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
695 : Mips::fixup_Mips_TLSGD;
696 break;
697 case Mips::S_TLSLDM:
698 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
699 : Mips::fixup_Mips_TLSLDM;
700 break;
701 case Mips::S_TPREL_HI:
702 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
703 : Mips::fixup_Mips_TPREL_HI;
704 break;
705 case Mips::S_TPREL_LO:
706 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
707 : Mips::fixup_Mips_TPREL_LO;
708 break;
709 case Mips::S_NEG:
710 FixupKind =
711 isMicroMips(STI) ? Mips::fixup_MICROMIPS_SUB : Mips::fixup_Mips_SUB;
712 break;
713 }
714 addFixup(Fixups, Offset: 0, Value: MipsExpr, Kind: MCFixupKind(FixupKind));
715 return 0;
716 }
717
718 Ctx.reportError(L: Expr->getLoc(), Msg: "expected an immediate");
719 return 0;
720}
721
722/// getMachineOpValue - Return binary encoding of operand. If the machine
723/// operand requires relocation, record the relocation and return zero.
724unsigned MipsMCCodeEmitter::
725getMachineOpValue(const MCInst &MI, const MCOperand &MO,
726 SmallVectorImpl<MCFixup> &Fixups,
727 const MCSubtargetInfo &STI) const {
728 if (MO.isReg()) {
729 MCRegister Reg = MO.getReg();
730 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
731 return RegNo;
732 } else if (MO.isImm()) {
733 return static_cast<unsigned>(MO.getImm());
734 } else if (MO.isDFPImm()) {
735 return static_cast<unsigned>(bit_cast<double>(from: MO.getDFPImm()));
736 }
737 // TODO: Set EncoderMethod to "getImmOpValue" for imm Operand so that
738 // getMachineOpValue will not be called for isExpr code paths.
739 assert(MO.isExpr());
740 return getImmOpValue(MI, MO, Fixups, STI);
741}
742
743unsigned MipsMCCodeEmitter::getImmOpValue(const MCInst &MI, const MCOperand &MO,
744 SmallVectorImpl<MCFixup> &Fixups,
745 const MCSubtargetInfo &STI) const {
746 if (MO.isImm())
747 return MO.getImm();
748 assert(MO.isExpr() && "getImmOpValue expects only expressions or immediates");
749 const MCExpr *Expr = MO.getExpr();
750 int64_t Res;
751 if (Expr->evaluateAsAbsolute(Res))
752 return Res;
753 unsigned MIFrm = MipsII::getFormat(TSFlags: MCII.get(Opcode: MI.getOpcode()).TSFlags);
754 if (!isa<MCSpecifierExpr>(Val: Expr) && MIFrm == MipsII::FrmI) {
755 addFixup(Fixups, Offset: 0, Value: Expr, Kind: Mips::fixup_Mips_AnyImm16);
756 return 0;
757 }
758 return getExprOpValue(Expr, Fixups, STI);
759}
760
761template <unsigned Shift>
762unsigned
763MipsMCCodeEmitter::getScaledImmOpValue(const MCInst &MI, unsigned OpNo,
764 SmallVectorImpl<MCFixup> &Fixups,
765 const MCSubtargetInfo &STI) const {
766 return getMachineOpValue(MI, MO: MI.getOperand(i: OpNo), Fixups, STI) >> Shift;
767}
768
769template <unsigned FromEnd, unsigned Shift>
770unsigned
771MipsMCCodeEmitter::getOperandFromEndValue(const MCInst &MI, unsigned OpNo,
772 SmallVectorImpl<MCFixup> &Fixups,
773 const MCSubtargetInfo &STI) const {
774 return getScaledImmOpValue<Shift>(MI, MI.getNumOperands() - FromEnd, Fixups,
775 STI);
776}
777
778// FIXME: should be called getMSBEncoding
779//
780unsigned
781MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
782 SmallVectorImpl<MCFixup> &Fixups,
783 const MCSubtargetInfo &STI) const {
784 assert(MI.getOperand(OpNo-1).isImm());
785 assert(MI.getOperand(OpNo).isImm());
786 unsigned Position = getMachineOpValue(MI, MO: MI.getOperand(i: OpNo-1), Fixups, STI);
787 unsigned Size = getMachineOpValue(MI, MO: MI.getOperand(i: OpNo), Fixups, STI);
788
789 return Position + Size - 1;
790}
791
792template <unsigned Bits, int Offset>
793unsigned
794MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
795 SmallVectorImpl<MCFixup> &Fixups,
796 const MCSubtargetInfo &STI) const {
797 assert(MI.getOperand(OpNo).isImm());
798 unsigned Value = getMachineOpValue(MI, MO: MI.getOperand(i: OpNo), Fixups, STI);
799 Value -= Offset;
800 return Value;
801}
802
803unsigned
804MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
805 SmallVectorImpl<MCFixup> &Fixups,
806 const MCSubtargetInfo &STI) const {
807 const MCOperand &MO = MI.getOperand(i: OpNo);
808 if (MO.isImm()) {
809 // The immediate is encoded as 'immediate << 2'.
810 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
811 assert((Res & 3) == 0);
812 return Res >> 2;
813 }
814
815 assert(MO.isExpr() &&
816 "getSimm19Lsl2Encoding expects only expressions or an immediate");
817
818 const MCExpr *Expr = MO.getExpr();
819 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
820 : Mips::fixup_MIPS_PC19_S2;
821 addFixup(Fixups, Offset: 0, Value: Expr, Kind: MCFixupKind(FixupKind));
822 return 0;
823}
824
825unsigned
826MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
827 SmallVectorImpl<MCFixup> &Fixups,
828 const MCSubtargetInfo &STI) const {
829 const MCOperand &MO = MI.getOperand(i: OpNo);
830 if (MO.isImm()) {
831 // The immediate is encoded as 'immediate << 3'.
832 unsigned Res = getMachineOpValue(MI, MO: MI.getOperand(i: OpNo), Fixups, STI);
833 assert((Res & 7) == 0);
834 return Res >> 3;
835 }
836
837 assert(MO.isExpr() &&
838 "getSimm18Lsl2Encoding expects only expressions or an immediate");
839
840 const MCExpr *Expr = MO.getExpr();
841 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
842 : Mips::fixup_MIPS_PC18_S3;
843 addFixup(Fixups, Offset: 0, Value: Expr, Kind: MCFixupKind(FixupKind));
844 return 0;
845}
846
847unsigned
848MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
849 SmallVectorImpl<MCFixup> &Fixups,
850 const MCSubtargetInfo &STI) const {
851 assert(MI.getOperand(OpNo).isImm());
852 const MCOperand &MO = MI.getOperand(i: OpNo);
853 return MO.getImm() % 8;
854}
855
856unsigned
857MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
858 SmallVectorImpl<MCFixup> &Fixups,
859 const MCSubtargetInfo &STI) const {
860 assert(MI.getOperand(OpNo).isImm());
861 const MCOperand &MO = MI.getOperand(i: OpNo);
862 unsigned Value = MO.getImm();
863 switch (Value) {
864 case 128: return 0x0;
865 case 1: return 0x1;
866 case 2: return 0x2;
867 case 3: return 0x3;
868 case 4: return 0x4;
869 case 7: return 0x5;
870 case 8: return 0x6;
871 case 15: return 0x7;
872 case 16: return 0x8;
873 case 31: return 0x9;
874 case 32: return 0xa;
875 case 63: return 0xb;
876 case 64: return 0xc;
877 case 255: return 0xd;
878 case 32768: return 0xe;
879 case 65535: return 0xf;
880 }
881 llvm_unreachable("Unexpected value");
882}
883
884unsigned
885MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
886 SmallVectorImpl<MCFixup> &Fixups,
887 const MCSubtargetInfo &STI) const {
888 unsigned res = 0;
889
890 // Register list operand is always first operand of instruction and it is
891 // placed before memory operand (register + imm).
892
893 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
894 MCRegister Reg = MI.getOperand(i: I).getReg();
895 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
896 if (RegNo != 31)
897 res++;
898 else
899 res |= 0x10;
900 }
901 return res;
902}
903
904unsigned
905MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
906 SmallVectorImpl<MCFixup> &Fixups,
907 const MCSubtargetInfo &STI) const {
908 return (MI.getNumOperands() - 4);
909}
910
911unsigned
912MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
913 SmallVectorImpl<MCFixup> &Fixups,
914 const MCSubtargetInfo &STI) const {
915 unsigned res = 0;
916
917 if (MI.getOperand(i: 0).getReg() == Mips::R5 &&
918 MI.getOperand(i: 1).getReg() == Mips::R6)
919 res = 0;
920 else if (MI.getOperand(i: 0).getReg() == Mips::R5 &&
921 MI.getOperand(i: 1).getReg() == Mips::R7)
922 res = 1;
923 else if (MI.getOperand(i: 0).getReg() == Mips::R6 &&
924 MI.getOperand(i: 1).getReg() == Mips::R7)
925 res = 2;
926 else if (MI.getOperand(i: 0).getReg() == Mips::R4 &&
927 MI.getOperand(i: 1).getReg() == Mips::R21)
928 res = 3;
929 else if (MI.getOperand(i: 0).getReg() == Mips::R4 &&
930 MI.getOperand(i: 1).getReg() == Mips::R22)
931 res = 4;
932 else if (MI.getOperand(i: 0).getReg() == Mips::R4 &&
933 MI.getOperand(i: 1).getReg() == Mips::R5)
934 res = 5;
935 else if (MI.getOperand(i: 0).getReg() == Mips::R4 &&
936 MI.getOperand(i: 1).getReg() == Mips::R6)
937 res = 6;
938 else if (MI.getOperand(i: 0).getReg() == Mips::R4 &&
939 MI.getOperand(i: 1).getReg() == Mips::R7)
940 res = 7;
941
942 return res;
943}
944
945unsigned
946MipsMCCodeEmitter::getMovePRegSingleOpValue(const MCInst &MI, unsigned OpNo,
947 SmallVectorImpl<MCFixup> &Fixups,
948 const MCSubtargetInfo &STI) const {
949 assert(((OpNo == 2) || (OpNo == 3)) &&
950 "Unexpected OpNo for movep operand encoding!");
951
952 MCOperand Op = MI.getOperand(i: OpNo);
953 assert(Op.isReg() && "Operand of movep is not a register!");
954 switch (Op.getReg().id()) {
955 default:
956 llvm_unreachable("Unknown register for movep!");
957 case Mips::R0:
958 return 0;
959 case Mips::R17:
960 return 1;
961 case Mips::R2:
962 return 2;
963 case Mips::R3:
964 return 3;
965 case Mips::R16:
966 return 4;
967 case Mips::R18:
968 return 5;
969 case Mips::R19:
970 return 6;
971 case Mips::R20:
972 return 7;
973 }
974}
975
976unsigned
977MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
978 SmallVectorImpl<MCFixup> &Fixups,
979 const MCSubtargetInfo &STI) const {
980 const MCOperand &MO = MI.getOperand(i: OpNo);
981 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
982 // The immediate is encoded as 'immediate >> 2'.
983 unsigned Res = static_cast<unsigned>(MO.getImm());
984 assert((Res & 3) == 0);
985 return Res >> 2;
986}
987
988#include "MipsGenMCCodeEmitter.inc"
989