LLVM 24.0.0git
MipsMCCodeEmitter.cpp
Go to the documentation of this file.
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"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/APInt.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"
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
46 MCContext &Ctx) {
47 return new MipsMCCodeEmitter(MCII, Ctx, false);
48}
49
51 MCContext &Ctx) {
52 return new MipsMCCodeEmitter(MCII, Ctx, true);
53}
54
55} // end namespace llvm
56
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(2).getImm();
88 if (Shift <= 31)
89 return; // Do nothing
90 Shift -= 32;
91
92 // saminus32
93 Inst.getOperand(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(0).getReg();
119 MCRegister RegOp1 = Inst.getOperand(1).getReg();
120
121 unsigned Reg0 = Ctx.getRegisterInfo()->getEncodingValue(RegOp0);
122 unsigned Reg1 = Ctx.getRegisterInfo()->getEncodingValue(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(0).setReg(RegOp1);
140 Inst.getOperand(1).setReg(RegOp0);
141}
142
143bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
144 return STI.hasFeature(Mips::FeatureMicroMips);
145}
146
147bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
148 return STI.hasFeature(Mips::FeatureMips32r6);
149}
150
151bool MipsMCCodeEmitter::isNanoMips(const MCSubtargetInfo &STI) const {
152 return STI.hasFeature(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().
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(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(TmpInst);
188 }
189
190 size_t N = Fixups.size();
191 uint64_t Binary = getBinaryCodeForInstr(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, Mips::Arch_micromipsr6);
205 if (NewOpcode == -1)
206 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
207 }
208 else
209 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
210
211 // Check whether it is Dsp instruction.
212 if (NewOpcode == -1)
213 NewOpcode = Mips::Dsp2MicroMips(Opcode, 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(TmpInst, Fixups, STI);
221 }
222
223 if (((MI.getOpcode() == Mips::MOVEP_MM) ||
224 (MI.getOpcode() == Mips::MOVEP_MMR6))) {
225 unsigned RegPair = getMovePRegPairOpValue(MI, 0, Fixups, STI);
226 Binary = (Binary & 0xFFFFFC7F) | (RegPair << 7);
227 }
228 }
229
230 const MCInstrDesc &Desc = MCII.get(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 =
239 if (Size == 2) {
241 } else if (IsLittleEndian && Size == 4 &&
242 (isMicroMips(STI) || isNanoMips(STI))) {
243 support::endian::write<uint16_t>(CB, Binary >> 16, Endian);
244 support::endian::write<uint16_t>(CB, Binary & 0xffff, Endian);
245 } else if (IsLittleEndian && Size == 6 && isNanoMips(STI)) {
246 support::endian::write<uint16_t>(CB, Binary >> 32, Endian);
247 support::endian::write<uint16_t>(CB, Binary >> 16, Endian);
248 support::endian::write<uint16_t>(CB, Binary & 0xffff, Endian);
249 } else {
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.
258getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
260 const MCSubtargetInfo &STI) const {
261 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
271 addFixup(Fixups, 0, FixupExpression, 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.
279getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
281 const MCSubtargetInfo &STI) const {
282 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
292 addFixup(Fixups, 0, FixupExpression, 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.
300getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
302 const MCSubtargetInfo &STI) const {
303 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-2, Ctx), Ctx);
314 addFixup(Fixups, 0, FixupExpression, 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.
322getBranchTargetOpValueLsl2MMR6(const MCInst &MI, unsigned OpNo,
324 const MCSubtargetInfo &STI) const {
325 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
336 addFixup(Fixups, 0, FixupExpression, 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.
344getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
346 const MCSubtargetInfo &STI) const {
347 const MCOperand &MO = MI.getOperand(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, 0, Expr, 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.
364getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
366 const MCSubtargetInfo &STI) const {
367 const MCOperand &MO = MI.getOperand(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, 0, Expr, 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.
384getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
386 const MCSubtargetInfo &STI) const {
387 const MCOperand &MO = MI.getOperand(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, 0, Expr, 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.
404getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
406 const MCSubtargetInfo &STI) const {
407 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
417 addFixup(Fixups, 0, FixupExpression, 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.
425getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
427 const MCSubtargetInfo &STI) const {
428 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
438 addFixup(Fixups, 0, FixupExpression, 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.
446getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
448 const MCSubtargetInfo &STI) const {
449 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
459 addFixup(Fixups, 0, FixupExpression, 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.
467 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
468 const MCSubtargetInfo &STI) const {
469 const MCOperand &MO = MI.getOperand(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 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
480 addFixup(Fixups, 0, FixupExpression, 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.
488getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
490 const MCSubtargetInfo &STI) const {
491 const MCOperand &MO = MI.getOperand(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();
501 addFixup(Fixups, 0, Expr, 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.
509getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
511 const MCSubtargetInfo &STI) const {
512 const MCOperand &MO = MI.getOperand(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, 0, Expr, Mips::fixup_Mips_26);
521 return 0;
522}
523
525getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
527 const MCSubtargetInfo &STI) const {
528 const MCOperand &MO = MI.getOperand(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, 0, Expr, Mips::fixup_MICROMIPS_26_S1);
537 return 0;
538}
539
541getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
543 const MCSubtargetInfo &STI) const {
544 const MCOperand &MO = MI.getOperand(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
559getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
561 const MCSubtargetInfo &STI) const {
562 const MCOperand &MO = MI.getOperand(OpNo);
563 if (MO.isImm()) {
564 int Value = MO.getImm();
565 return Value >> 2;
566 }
567
568 return 0;
569}
570
572getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
574 const MCSubtargetInfo &STI) const {
575 const MCOperand &MO = MI.getOperand(OpNo);
576 if (MO.isImm()) {
577 unsigned Value = MO.getImm();
578 return Value >> 2;
579 }
580
581 return 0;
582}
583
585getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
587 const MCSubtargetInfo &STI) const {
588 const MCOperand &MO = MI.getOperand(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
599 const MCSubtargetInfo &STI) const {
600 MCExpr::ExprKind Kind = Expr->getKind();
601 if (Kind == MCExpr::Specifier) {
602 const auto *MipsExpr = cast<MCSpecifierExpr>(Expr);
603
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(MipsExpr->getSubExpr(), Fixups, STI);
616 break;
619 break;
623 break;
627 break;
628 case Mips::S_GOTTPREL:
629 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOTTPREL
631 break;
632 case Mips::S_GOT:
633 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
635 break;
636 case Mips::S_GOT_CALL:
637 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
639 break;
640 case Mips::S_GOT_DISP:
641 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
643 break;
644 case Mips::S_GOT_HI16:
646 break;
647 case Mips::S_GOT_LO16:
649 break;
650 case Mips::S_GOT_PAGE:
651 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
653 break;
654 case Mips::S_GOT_OFST:
655 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
657 break;
658 case Mips::S_GPREL:
660 break;
661 case Mips::S_LO:
662 // Check for %lo(%neg(%gp_rel(X)))
663 if (Mips::isGpOff(*MipsExpr))
664 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_LO
666 else
667 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
669 break;
670 case Mips::S_HIGHEST:
671 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHEST
673 break;
674 case Mips::S_HIGHER:
675 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHER
677 break;
678 case Mips::S_HI:
679 // Check for %hi(%neg(%gp_rel(X)))
680 if (Mips::isGpOff(*MipsExpr))
681 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_HI
683 else
684 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
686 break;
689 break;
692 break;
693 case Mips::S_TLSGD:
694 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
696 break;
697 case Mips::S_TLSLDM:
698 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
700 break;
701 case Mips::S_TPREL_HI:
704 break;
705 case Mips::S_TPREL_LO:
708 break;
709 case Mips::S_NEG:
710 FixupKind =
712 break;
713 }
714 addFixup(Fixups, 0, MipsExpr, MCFixupKind(FixupKind));
715 return 0;
716 }
717
718 Ctx.reportError(Expr->getLoc(), "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.
725getMachineOpValue(const MCInst &MI, const MCOperand &MO,
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>(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
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(MCII.get(MI.getOpcode()).TSFlags);
754 if (!isa<MCSpecifierExpr>(Expr) && MIFrm == MipsII::FrmI) {
755 addFixup(Fixups, 0, Expr, Mips::fixup_Mips_AnyImm16);
756 return 0;
757 }
758 return getExprOpValue(Expr, Fixups, STI);
759}
760
761template <unsigned Shift>
762unsigned
765 const MCSubtargetInfo &STI) const {
766 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) >> Shift;
767}
768
769template <unsigned FromEnd, unsigned Shift>
770unsigned
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
783 const MCSubtargetInfo &STI) const {
784 assert(MI.getOperand(OpNo-1).isImm());
785 assert(MI.getOperand(OpNo).isImm());
786 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
787 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
788
789 return Position + Size - 1;
790}
791
792template <unsigned Bits, int Offset>
793unsigned
796 const MCSubtargetInfo &STI) const {
797 assert(MI.getOperand(OpNo).isImm());
798 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
799 Value -= Offset;
800 return Value;
801}
802
803unsigned
806 const MCSubtargetInfo &STI) const {
807 const MCOperand &MO = MI.getOperand(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();
821 addFixup(Fixups, 0, Expr, MCFixupKind(FixupKind));
822 return 0;
823}
824
825unsigned
828 const MCSubtargetInfo &STI) const {
829 const MCOperand &MO = MI.getOperand(OpNo);
830 if (MO.isImm()) {
831 // The immediate is encoded as 'immediate << 3'.
832 unsigned Res = getMachineOpValue(MI, MI.getOperand(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();
843 addFixup(Fixups, 0, Expr, MCFixupKind(FixupKind));
844 return 0;
845}
846
847unsigned
850 const MCSubtargetInfo &STI) const {
851 assert(MI.getOperand(OpNo).isImm());
852 const MCOperand &MO = MI.getOperand(OpNo);
853 return MO.getImm() % 8;
854}
855
856unsigned
859 const MCSubtargetInfo &STI) const {
860 assert(MI.getOperand(OpNo).isImm());
861 const MCOperand &MO = MI.getOperand(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
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).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
907 const MCSubtargetInfo &STI) const {
908 return (MI.getNumOperands() - 4);
909}
910
911unsigned
914 const MCSubtargetInfo &STI) const {
915 unsigned res = 0;
916
917 if (MI.getOperand(0).getReg() == Mips::R5 &&
918 MI.getOperand(1).getReg() == Mips::R6)
919 res = 0;
920 else if (MI.getOperand(0).getReg() == Mips::R5 &&
921 MI.getOperand(1).getReg() == Mips::R7)
922 res = 1;
923 else if (MI.getOperand(0).getReg() == Mips::R6 &&
924 MI.getOperand(1).getReg() == Mips::R7)
925 res = 2;
926 else if (MI.getOperand(0).getReg() == Mips::R4 &&
927 MI.getOperand(1).getReg() == Mips::R21)
928 res = 3;
929 else if (MI.getOperand(0).getReg() == Mips::R4 &&
930 MI.getOperand(1).getReg() == Mips::R22)
931 res = 4;
932 else if (MI.getOperand(0).getReg() == Mips::R4 &&
933 MI.getOperand(1).getReg() == Mips::R5)
934 res = 5;
935 else if (MI.getOperand(0).getReg() == Mips::R4 &&
936 MI.getOperand(1).getReg() == Mips::R6)
937 res = 6;
938 else if (MI.getOperand(0).getReg() == Mips::R4 &&
939 MI.getOperand(1).getReg() == Mips::R7)
940 res = 7;
941
942 return res;
943}
944
945unsigned
948 const MCSubtargetInfo &STI) const {
949 assert(((OpNo == 2) || (OpNo == 3)) &&
950 "Unexpected OpNo for movep operand encoding!");
951
952 MCOperand Op = MI.getOperand(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
979 const MCSubtargetInfo &STI) const {
980 const MCOperand &MO = MI.getOperand(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"
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
static void LowerLargeShift(MCInst &Inst)
This file defines the SmallVector class.
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
MCCodeEmitter - Generic instruction encoding interface.
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:45
ExprKind getKind() const
Definition MCExpr.h:85
SMLoc getLoc() const
Definition MCExpr.h:86
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition MCFixup.h:90
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
unsigned getNumOperands() const
Definition MCInst.h:212
unsigned getOpcode() const
Definition MCInst.h:202
void setOpcode(unsigned Op)
Definition MCInst.h:201
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
void setImm(int64_t Val)
Definition MCInst.h:89
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
void setReg(MCRegister Reg)
Set the register number.
Definition MCInst.h:79
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
bool isDFPImm() const
Definition MCInst.h:68
const MCExpr * getExpr() const
Definition MCInst.h:118
uint64_t getDFPImm() const
Definition MCInst.h:104
bool isExpr() const
Definition MCInst.h:69
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
unsigned getBranchTargetOpValueLsl2MMR6(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValueLsl2MMR6 - Return binary encoding of the branch target operand.
unsigned getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTarget21OpValue - Return binary encoding of the branch target operand.
uint64_t getBinaryCodeForInstr(const MCInst &MI, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getMachineOpValue - Return binary encoding of operand.
unsigned getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
Subtract Offset then encode as a N-bit unsigned integer.
unsigned getOperandFromEndValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getScaledImmOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValueMMR6 - Return binary encoding of the branch target operand.
unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValue - Return binary encoding of the branch target operand.
unsigned getRegisterListOpValue16(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTarget26OpValue - Return binary encoding of the branch target operand.
unsigned getBranchTarget26OpValueMM(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTarget26OpValueMM - Return binary encoding of the branch target operand.
unsigned getImmOpValue(const MCInst &MI, const MCOperand &MO, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValue1SImm16 - Return binary encoding of the branch target operand.
unsigned getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getJumpOffset16OpValue - Return binary encoding of the jump target operand.
unsigned getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS 10-bit branch target operand.
unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getJumpTargetOpValue - Return binary encoding of the jump target operand.
unsigned getUImm4AndValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getRegisterListOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getExprOpValue(const MCExpr *Expr, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTargetOpValue - Return binary encoding of the microMIPS branch target operand.
unsigned getMovePRegSingleOpValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
void encodeInstruction(const MCInst &MI, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const override
encodeInstruction - Emit the instruction.
unsigned getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
unsigned getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTarget21OpValueMM - Return binary encoding of the branch target operand for microMIPS.
unsigned getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch target operand.
unsigned getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const
void EmitByte(unsigned char C, raw_ostream &OS) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getFormat(uint64_t TSFlags)
@ fixup_MICROMIPS_TLS_TPREL_LO16
@ fixup_MICROMIPS_TLS_TPREL_HI16
@ fixup_MICROMIPS_TLS_DTPREL_HI16
@ fixup_MICROMIPS_TLS_DTPREL_LO16
@ fixup_Mips_Branch_PCRel
bool isGpOff(const MCSpecifierExpr &E)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
MCCodeEmitter * createMipsMCCodeEmitterEL(const MCInstrInfo &MCII, MCContext &Ctx)
MCCodeEmitter * createMipsMCCodeEmitterEB(const MCInstrInfo &MCII, MCContext &Ctx)
@ Offset
Definition DWP.cpp:577
Op::Description Desc
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
static Lanai::Fixups FixupKind(const MCExpr *Expr)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
To bit_cast(const From &from) noexcept
Definition bit.h:90
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N