1 //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MipsMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
15 #include "MipsMCCodeEmitter.h"
16 #include "MCTargetDesc/MipsFixupKinds.h"
17 #include "MCTargetDesc/MipsMCExpr.h"
18 #include "MCTargetDesc/MipsMCTargetDesc.h"
19 #include "llvm/ADT/APFloat.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/MCInstrInfo.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "mccodeemitter"
31 #define GET_INSTRMAP_INFO
32 #include "MipsGenInstrInfo.inc"
33 #undef GET_INSTRMAP_INFO
36 MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
37 const MCRegisterInfo &MRI,
39 return new MipsMCCodeEmitter(MCII, Ctx, false);
42 MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
43 const MCRegisterInfo &MRI,
45 return new MipsMCCodeEmitter(MCII, Ctx, true);
47 } // End of namespace llvm.
49 // If the D<shift> instruction has a shift amount that is greater
50 // than 31 (checked in calling routine), lower it to a D<shift>32 instruction
51 static void LowerLargeShift(MCInst& Inst) {
53 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
54 assert(Inst.getOperand(2).isImm());
56 int64_t Shift = Inst.getOperand(2).getImm();
62 Inst.getOperand(2).setImm(Shift);
64 switch (Inst.getOpcode()) {
66 // Calling function is not synchronized
67 llvm_unreachable("Unexpected shift instruction");
69 Inst.setOpcode(Mips::DSLL32);
72 Inst.setOpcode(Mips::DSRL32);
75 Inst.setOpcode(Mips::DSRA32);
78 Inst.setOpcode(Mips::DROTR32);
83 // Pick a DEXT or DINS instruction variant based on the pos and size operands
84 static void LowerDextDins(MCInst& InstIn) {
85 int Opcode = InstIn.getOpcode();
87 if (Opcode == Mips::DEXT)
88 assert(InstIn.getNumOperands() == 4 &&
89 "Invalid no. of machine operands for DEXT!");
90 else // Only DEXT and DINS are possible
91 assert(InstIn.getNumOperands() == 5 &&
92 "Invalid no. of machine operands for DINS!");
94 assert(InstIn.getOperand(2).isImm());
95 int64_t pos = InstIn.getOperand(2).getImm();
96 assert(InstIn.getOperand(3).isImm());
97 int64_t size = InstIn.getOperand(3).getImm();
100 if (pos < 32) // DEXT/DINS, do nothing
103 InstIn.getOperand(2).setImm(pos - 32);
104 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
108 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
109 InstIn.getOperand(3).setImm(size - 32);
110 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
114 bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
115 return STI.getFeatureBits() & Mips::FeatureMicroMips;
118 void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
122 void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
123 const MCSubtargetInfo &STI,
124 raw_ostream &OS) const {
125 // Output the instruction encoding in little endian byte order.
126 // Little-endian byte ordering:
127 // mips32r2: 4 | 3 | 2 | 1
128 // microMIPS: 2 | 1 | 4 | 3
129 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
130 EmitInstruction(Val >> 16, 2, STI, OS);
131 EmitInstruction(Val, 2, STI, OS);
133 for (unsigned i = 0; i < Size; ++i) {
134 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
135 EmitByte((Val >> Shift) & 0xff, OS);
140 /// EncodeInstruction - Emit the instruction.
141 /// Size the instruction with Desc.getSize().
142 void MipsMCCodeEmitter::
143 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
144 SmallVectorImpl<MCFixup> &Fixups,
145 const MCSubtargetInfo &STI) const
148 // Non-pseudo instructions that get changed for direct object
149 // only based on operand values.
150 // If this list of instructions get much longer we will move
151 // the check to a function call. Until then, this is more efficient.
153 switch (MI.getOpcode()) {
154 // If shift amount is >= 32 it the inst needs to be lowered further
159 LowerLargeShift(TmpInst);
161 // Double extract instruction is chosen by pos and size operands
164 LowerDextDins(TmpInst);
167 unsigned long N = Fixups.size();
168 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
170 // Check for unimplemented opcodes.
171 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
172 // so we have to special check for them.
173 unsigned Opcode = TmpInst.getOpcode();
174 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
175 (Opcode != Mips::SLL_MM) && !Binary)
176 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
178 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
179 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
180 if (NewOpcode != -1) {
181 if (Fixups.size() > N)
184 TmpInst.setOpcode (NewOpcode);
185 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
189 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
191 // Get byte count of instruction
192 unsigned Size = Desc.getSize();
194 llvm_unreachable("Desc.getSize() returns 0");
196 EmitInstruction(Binary, Size, STI, OS);
199 /// getBranchTargetOpValue - Return binary encoding of the branch
200 /// target operand. If the machine operand requires relocation,
201 /// record the relocation and return zero.
202 unsigned MipsMCCodeEmitter::
203 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
204 SmallVectorImpl<MCFixup> &Fixups,
205 const MCSubtargetInfo &STI) const {
207 const MCOperand &MO = MI.getOperand(OpNo);
209 // If the destination is an immediate, divide by 4.
210 if (MO.isImm()) return MO.getImm() >> 2;
212 assert(MO.isExpr() &&
213 "getBranchTargetOpValue expects only expressions or immediates");
215 const MCExpr *Expr = MO.getExpr();
216 Fixups.push_back(MCFixup::Create(0, Expr,
217 MCFixupKind(Mips::fixup_Mips_PC16)));
221 /// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
222 /// target operand. If the machine operand requires relocation,
223 /// record the relocation and return zero.
224 unsigned MipsMCCodeEmitter::
225 getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
226 SmallVectorImpl<MCFixup> &Fixups,
227 const MCSubtargetInfo &STI) const {
229 const MCOperand &MO = MI.getOperand(OpNo);
231 // If the destination is an immediate, divide by 2.
232 if (MO.isImm()) return MO.getImm() >> 1;
234 assert(MO.isExpr() &&
235 "getBranchTargetOpValueMM expects only expressions or immediates");
237 const MCExpr *Expr = MO.getExpr();
238 Fixups.push_back(MCFixup::Create(0, Expr,
239 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
243 /// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
244 /// 10-bit branch target operand. If the machine operand requires relocation,
245 /// record the relocation and return zero.
246 unsigned MipsMCCodeEmitter::
247 getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
248 SmallVectorImpl<MCFixup> &Fixups,
249 const MCSubtargetInfo &STI) const {
251 const MCOperand &MO = MI.getOperand(OpNo);
253 // If the destination is an immediate, divide by 2.
254 if (MO.isImm()) return MO.getImm() >> 1;
256 assert(MO.isExpr() &&
257 "getBranchTargetOpValuePC10 expects only expressions or immediates");
259 const MCExpr *Expr = MO.getExpr();
260 Fixups.push_back(MCFixup::Create(0, Expr,
261 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
265 /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
266 /// target operand. If the machine operand requires relocation,
267 /// record the relocation and return zero.
268 unsigned MipsMCCodeEmitter::
269 getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
270 SmallVectorImpl<MCFixup> &Fixups,
271 const MCSubtargetInfo &STI) const {
273 const MCOperand &MO = MI.getOperand(OpNo);
275 // If the destination is an immediate, divide by 2.
276 if (MO.isImm()) return MO.getImm() >> 1;
278 assert(MO.isExpr() &&
279 "getBranchTargetOpValueMM expects only expressions or immediates");
281 const MCExpr *Expr = MO.getExpr();
282 Fixups.push_back(MCFixup::Create(0, Expr,
284 fixup_MICROMIPS_PC16_S1)));
288 /// getBranchTarget21OpValue - Return binary encoding of the branch
289 /// target operand. If the machine operand requires relocation,
290 /// record the relocation and return zero.
291 unsigned MipsMCCodeEmitter::
292 getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
293 SmallVectorImpl<MCFixup> &Fixups,
294 const MCSubtargetInfo &STI) const {
296 const MCOperand &MO = MI.getOperand(OpNo);
298 // If the destination is an immediate, divide by 4.
299 if (MO.isImm()) return MO.getImm() >> 2;
301 assert(MO.isExpr() &&
302 "getBranchTarget21OpValue expects only expressions or immediates");
304 const MCExpr *Expr = MO.getExpr();
305 Fixups.push_back(MCFixup::Create(0, Expr,
306 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
310 /// getBranchTarget26OpValue - Return binary encoding of the branch
311 /// target operand. If the machine operand requires relocation,
312 /// record the relocation and return zero.
313 unsigned MipsMCCodeEmitter::
314 getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
315 SmallVectorImpl<MCFixup> &Fixups,
316 const MCSubtargetInfo &STI) const {
318 const MCOperand &MO = MI.getOperand(OpNo);
320 // If the destination is an immediate, divide by 4.
321 if (MO.isImm()) return MO.getImm() >> 2;
323 assert(MO.isExpr() &&
324 "getBranchTarget26OpValue expects only expressions or immediates");
326 const MCExpr *Expr = MO.getExpr();
327 Fixups.push_back(MCFixup::Create(0, Expr,
328 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
332 /// getJumpOffset16OpValue - Return binary encoding of the jump
333 /// target operand. If the machine operand requires relocation,
334 /// record the relocation and return zero.
335 unsigned MipsMCCodeEmitter::
336 getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
337 SmallVectorImpl<MCFixup> &Fixups,
338 const MCSubtargetInfo &STI) const {
340 const MCOperand &MO = MI.getOperand(OpNo);
342 if (MO.isImm()) return MO.getImm();
344 assert(MO.isExpr() &&
345 "getJumpOffset16OpValue expects only expressions or an immediate");
351 /// getJumpTargetOpValue - Return binary encoding of the jump
352 /// target operand. If the machine operand requires relocation,
353 /// record the relocation and return zero.
354 unsigned MipsMCCodeEmitter::
355 getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
356 SmallVectorImpl<MCFixup> &Fixups,
357 const MCSubtargetInfo &STI) const {
359 const MCOperand &MO = MI.getOperand(OpNo);
360 // If the destination is an immediate, divide by 4.
361 if (MO.isImm()) return MO.getImm()>>2;
363 assert(MO.isExpr() &&
364 "getJumpTargetOpValue expects only expressions or an immediate");
366 const MCExpr *Expr = MO.getExpr();
367 Fixups.push_back(MCFixup::Create(0, Expr,
368 MCFixupKind(Mips::fixup_Mips_26)));
372 unsigned MipsMCCodeEmitter::
373 getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
374 SmallVectorImpl<MCFixup> &Fixups,
375 const MCSubtargetInfo &STI) const {
377 const MCOperand &MO = MI.getOperand(OpNo);
378 // If the destination is an immediate, divide by 2.
379 if (MO.isImm()) return MO.getImm() >> 1;
381 assert(MO.isExpr() &&
382 "getJumpTargetOpValueMM expects only expressions or an immediate");
384 const MCExpr *Expr = MO.getExpr();
385 Fixups.push_back(MCFixup::Create(0, Expr,
386 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
390 unsigned MipsMCCodeEmitter::
391 getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
392 SmallVectorImpl<MCFixup> &Fixups,
393 const MCSubtargetInfo &STI) const {
395 const MCOperand &MO = MI.getOperand(OpNo);
397 // The immediate is encoded as 'immediate << 2'.
398 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
399 assert((Res & 3) == 0);
403 assert(MO.isExpr() &&
404 "getUImm5Lsl2Encoding expects only expressions or an immediate");
409 unsigned MipsMCCodeEmitter::
410 getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
411 SmallVectorImpl<MCFixup> &Fixups,
412 const MCSubtargetInfo &STI) const {
414 const MCOperand &MO = MI.getOperand(OpNo);
416 int Value = MO.getImm();
423 unsigned MipsMCCodeEmitter::
424 getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
425 SmallVectorImpl<MCFixup> &Fixups,
426 const MCSubtargetInfo &STI) const {
428 const MCOperand &MO = MI.getOperand(OpNo);
430 unsigned Value = MO.getImm();
437 unsigned MipsMCCodeEmitter::
438 getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
439 SmallVectorImpl<MCFixup> &Fixups,
440 const MCSubtargetInfo &STI) const {
442 const MCOperand &MO = MI.getOperand(OpNo);
444 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
445 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
451 unsigned MipsMCCodeEmitter::
452 getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
453 const MCSubtargetInfo &STI) const {
456 if (Expr->EvaluateAsAbsolute(Res))
459 MCExpr::ExprKind Kind = Expr->getKind();
460 if (Kind == MCExpr::Constant) {
461 return cast<MCConstantExpr>(Expr)->getValue();
464 if (Kind == MCExpr::Binary) {
465 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
466 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
470 if (Kind == MCExpr::Target) {
471 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
473 Mips::Fixups FixupKind = Mips::Fixups(0);
474 switch (MipsExpr->getKind()) {
475 default: llvm_unreachable("Unsupported fixup kind for target expression!");
476 case MipsMCExpr::VK_Mips_HIGHEST:
477 FixupKind = Mips::fixup_Mips_HIGHEST;
479 case MipsMCExpr::VK_Mips_HIGHER:
480 FixupKind = Mips::fixup_Mips_HIGHER;
482 case MipsMCExpr::VK_Mips_HI:
483 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
484 : Mips::fixup_Mips_HI16;
486 case MipsMCExpr::VK_Mips_LO:
487 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
488 : Mips::fixup_Mips_LO16;
491 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
495 if (Kind == MCExpr::SymbolRef) {
496 Mips::Fixups FixupKind = Mips::Fixups(0);
498 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
499 default: llvm_unreachable("Unknown fixup kind!");
501 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
502 FixupKind = Mips::fixup_Mips_GPOFF_HI;
504 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
505 FixupKind = Mips::fixup_Mips_GPOFF_LO;
507 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
508 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
509 : Mips::fixup_Mips_GOT_PAGE;
511 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
512 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
513 : Mips::fixup_Mips_GOT_OFST;
515 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
516 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
517 : Mips::fixup_Mips_GOT_DISP;
519 case MCSymbolRefExpr::VK_Mips_GPREL:
520 FixupKind = Mips::fixup_Mips_GPREL16;
522 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
523 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
524 : Mips::fixup_Mips_CALL16;
526 case MCSymbolRefExpr::VK_Mips_GOT16:
527 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
528 : Mips::fixup_Mips_GOT_Global;
530 case MCSymbolRefExpr::VK_Mips_GOT:
531 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
532 : Mips::fixup_Mips_GOT_Local;
534 case MCSymbolRefExpr::VK_Mips_ABS_HI:
535 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
536 : Mips::fixup_Mips_HI16;
538 case MCSymbolRefExpr::VK_Mips_ABS_LO:
539 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
540 : Mips::fixup_Mips_LO16;
542 case MCSymbolRefExpr::VK_Mips_TLSGD:
543 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
544 : Mips::fixup_Mips_TLSGD;
546 case MCSymbolRefExpr::VK_Mips_TLSLDM:
547 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
548 : Mips::fixup_Mips_TLSLDM;
550 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
551 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
552 : Mips::fixup_Mips_DTPREL_HI;
554 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
555 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
556 : Mips::fixup_Mips_DTPREL_LO;
558 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
559 FixupKind = Mips::fixup_Mips_GOTTPREL;
561 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
562 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
563 : Mips::fixup_Mips_TPREL_HI;
565 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
566 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
567 : Mips::fixup_Mips_TPREL_LO;
569 case MCSymbolRefExpr::VK_Mips_HIGHER:
570 FixupKind = Mips::fixup_Mips_HIGHER;
572 case MCSymbolRefExpr::VK_Mips_HIGHEST:
573 FixupKind = Mips::fixup_Mips_HIGHEST;
575 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
576 FixupKind = Mips::fixup_Mips_GOT_HI16;
578 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
579 FixupKind = Mips::fixup_Mips_GOT_LO16;
581 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
582 FixupKind = Mips::fixup_Mips_CALL_HI16;
584 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
585 FixupKind = Mips::fixup_Mips_CALL_LO16;
587 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
588 FixupKind = Mips::fixup_MIPS_PCHI16;
590 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
591 FixupKind = Mips::fixup_MIPS_PCLO16;
595 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
601 /// getMachineOpValue - Return binary encoding of operand. If the machine
602 /// operand requires relocation, record the relocation and return zero.
603 unsigned MipsMCCodeEmitter::
604 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
605 SmallVectorImpl<MCFixup> &Fixups,
606 const MCSubtargetInfo &STI) const {
608 unsigned Reg = MO.getReg();
609 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
611 } else if (MO.isImm()) {
612 return static_cast<unsigned>(MO.getImm());
613 } else if (MO.isFPImm()) {
614 return static_cast<unsigned>(APFloat(MO.getFPImm())
615 .bitcastToAPInt().getHiBits(32).getLimitedValue());
617 // MO must be an Expr.
619 return getExprOpValue(MO.getExpr(),Fixups, STI);
622 /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
625 MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
626 SmallVectorImpl<MCFixup> &Fixups,
627 const MCSubtargetInfo &STI) const {
628 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
629 assert(MI.getOperand(OpNo).isReg());
630 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
631 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
633 // The immediate field of an LD/ST instruction is scaled which means it must
634 // be divided (when encoding) by the size (in bytes) of the instructions'
640 switch(MI.getOpcode())
643 assert (0 && "Unexpected instruction");
647 // We don't need to scale the offset in this case
663 return (OffBits & 0xFFFF) | RegBits;
666 /// getMemEncoding - Return binary encoding of memory related operand.
667 /// If the offset operand requires relocation, record the relocation.
669 MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
670 SmallVectorImpl<MCFixup> &Fixups,
671 const MCSubtargetInfo &STI) const {
672 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
673 assert(MI.getOperand(OpNo).isReg());
674 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
675 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
677 return (OffBits & 0xFFFF) | RegBits;
680 unsigned MipsMCCodeEmitter::
681 getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
682 SmallVectorImpl<MCFixup> &Fixups,
683 const MCSubtargetInfo &STI) const {
684 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
685 assert(MI.getOperand(OpNo).isReg());
686 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
688 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
691 return (OffBits & 0xF) | RegBits;
694 unsigned MipsMCCodeEmitter::
695 getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
696 SmallVectorImpl<MCFixup> &Fixups,
697 const MCSubtargetInfo &STI) const {
698 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
699 assert(MI.getOperand(OpNo).isReg());
700 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
702 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
705 return (OffBits & 0xF) | RegBits;
708 unsigned MipsMCCodeEmitter::
709 getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
710 SmallVectorImpl<MCFixup> &Fixups,
711 const MCSubtargetInfo &STI) const {
712 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
713 assert(MI.getOperand(OpNo).isReg());
714 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
716 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
719 return (OffBits & 0xF) | RegBits;
722 unsigned MipsMCCodeEmitter::
723 getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
724 SmallVectorImpl<MCFixup> &Fixups,
725 const MCSubtargetInfo &STI) const {
726 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
727 assert(MI.getOperand(OpNo).isReg() &&
728 MI.getOperand(OpNo).getReg() == Mips::SP &&
729 "Unexpected base register!");
730 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
733 return OffBits & 0x1F;
736 unsigned MipsMCCodeEmitter::
737 getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
738 SmallVectorImpl<MCFixup> &Fixups,
739 const MCSubtargetInfo &STI) const {
740 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
741 assert(MI.getOperand(OpNo).isReg() &&
742 MI.getOperand(OpNo).getReg() == Mips::GP &&
743 "Unexpected base register!");
745 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
748 return OffBits & 0x7F;
751 unsigned MipsMCCodeEmitter::
752 getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
753 SmallVectorImpl<MCFixup> &Fixups,
754 const MCSubtargetInfo &STI) const {
755 // opNum can be invalid if instruction had reglist as operand.
756 // MemOperand is always last operand of instruction (base + offset).
757 switch (MI.getOpcode()) {
762 OpNo = MI.getNumOperands() - 2;
766 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
767 assert(MI.getOperand(OpNo).isReg());
768 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
769 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
771 return (OffBits & 0x0FFF) | RegBits;
774 unsigned MipsMCCodeEmitter::
775 getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
776 SmallVectorImpl<MCFixup> &Fixups,
777 const MCSubtargetInfo &STI) const {
778 // opNum can be invalid if instruction had reglist as operand
779 // MemOperand is always last operand of instruction (base + offset)
780 switch (MI.getOpcode()) {
785 OpNo = MI.getNumOperands() - 2;
789 // Offset is encoded in bits 4-0.
790 assert(MI.getOperand(OpNo).isReg());
791 // Base register is always SP - thus it is not encoded.
792 assert(MI.getOperand(OpNo+1).isImm());
793 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
795 return ((OffBits >> 2) & 0x0F);
799 MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
800 SmallVectorImpl<MCFixup> &Fixups,
801 const MCSubtargetInfo &STI) const {
802 assert(MI.getOperand(OpNo).isImm());
803 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
804 return SizeEncoding - 1;
807 // FIXME: should be called getMSBEncoding
810 MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
811 SmallVectorImpl<MCFixup> &Fixups,
812 const MCSubtargetInfo &STI) const {
813 assert(MI.getOperand(OpNo-1).isImm());
814 assert(MI.getOperand(OpNo).isImm());
815 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
816 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
818 return Position + Size - 1;
822 MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
823 SmallVectorImpl<MCFixup> &Fixups,
824 const MCSubtargetInfo &STI) const {
825 assert(MI.getOperand(OpNo).isImm());
826 // The immediate is encoded as 'immediate - 1'.
827 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
831 MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
832 SmallVectorImpl<MCFixup> &Fixups,
833 const MCSubtargetInfo &STI) const {
834 const MCOperand &MO = MI.getOperand(OpNo);
836 // The immediate is encoded as 'immediate << 2'.
837 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
838 assert((Res & 3) == 0);
842 assert(MO.isExpr() &&
843 "getSimm19Lsl2Encoding expects only expressions or an immediate");
845 const MCExpr *Expr = MO.getExpr();
846 Fixups.push_back(MCFixup::Create(0, Expr,
847 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
852 MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
853 SmallVectorImpl<MCFixup> &Fixups,
854 const MCSubtargetInfo &STI) const {
855 const MCOperand &MO = MI.getOperand(OpNo);
857 // The immediate is encoded as 'immediate << 3'.
858 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
859 assert((Res & 7) == 0);
863 assert(MO.isExpr() &&
864 "getSimm18Lsl2Encoding expects only expressions or an immediate");
866 const MCExpr *Expr = MO.getExpr();
867 Fixups.push_back(MCFixup::Create(0, Expr,
868 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
873 MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
874 SmallVectorImpl<MCFixup> &Fixups,
875 const MCSubtargetInfo &STI) const {
876 assert(MI.getOperand(OpNo).isImm());
877 const MCOperand &MO = MI.getOperand(OpNo);
878 return MO.getImm() % 8;
882 MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
883 SmallVectorImpl<MCFixup> &Fixups,
884 const MCSubtargetInfo &STI) const {
885 assert(MI.getOperand(OpNo).isImm());
886 const MCOperand &MO = MI.getOperand(OpNo);
887 unsigned Value = MO.getImm();
889 case 128: return 0x0;
902 case 255: return 0xd;
903 case 32768: return 0xe;
904 case 65535: return 0xf;
906 llvm_unreachable("Unexpected value");
910 MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
911 SmallVectorImpl<MCFixup> &Fixups,
912 const MCSubtargetInfo &STI) const {
915 // Register list operand is always first operand of instruction and it is
916 // placed before memory operand (register + imm).
918 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
919 unsigned Reg = MI.getOperand(I).getReg();
920 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
930 MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
931 SmallVectorImpl<MCFixup> &Fixups,
932 const MCSubtargetInfo &STI) const {
933 return (MI.getNumOperands() - 4);
937 MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
938 SmallVectorImpl<MCFixup> &Fixups,
939 const MCSubtargetInfo &STI) const {
940 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
944 MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
945 SmallVectorImpl<MCFixup> &Fixups,
946 const MCSubtargetInfo &STI) const {
949 if (MI.getOperand(0).getReg() == Mips::A1 &&
950 MI.getOperand(1).getReg() == Mips::A2)
952 else if (MI.getOperand(0).getReg() == Mips::A1 &&
953 MI.getOperand(1).getReg() == Mips::A3)
955 else if (MI.getOperand(0).getReg() == Mips::A2 &&
956 MI.getOperand(1).getReg() == Mips::A3)
958 else if (MI.getOperand(0).getReg() == Mips::A0 &&
959 MI.getOperand(1).getReg() == Mips::S5)
961 else if (MI.getOperand(0).getReg() == Mips::A0 &&
962 MI.getOperand(1).getReg() == Mips::S6)
964 else if (MI.getOperand(0).getReg() == Mips::A0 &&
965 MI.getOperand(1).getReg() == Mips::A1)
967 else if (MI.getOperand(0).getReg() == Mips::A0 &&
968 MI.getOperand(1).getReg() == Mips::A2)
970 else if (MI.getOperand(0).getReg() == Mips::A0 &&
971 MI.getOperand(1).getReg() == Mips::A3)
978 MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
979 SmallVectorImpl<MCFixup> &Fixups,
980 const MCSubtargetInfo &STI) const {
981 const MCOperand &MO = MI.getOperand(OpNo);
982 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
983 // The immediate is encoded as 'immediate >> 2'.
984 unsigned Res = static_cast<unsigned>(MO.getImm());
985 assert((Res & 3) == 0);
989 #include "MipsGenMCCodeEmitter.inc"