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 bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
119 return STI.getFeatureBits() & Mips::FeatureMips32r6;
122 void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
126 void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
127 const MCSubtargetInfo &STI,
128 raw_ostream &OS) const {
129 // Output the instruction encoding in little endian byte order.
130 // Little-endian byte ordering:
131 // mips32r2: 4 | 3 | 2 | 1
132 // microMIPS: 2 | 1 | 4 | 3
133 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
134 EmitInstruction(Val >> 16, 2, STI, OS);
135 EmitInstruction(Val, 2, STI, OS);
137 for (unsigned i = 0; i < Size; ++i) {
138 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
139 EmitByte((Val >> Shift) & 0xff, OS);
144 /// EncodeInstruction - Emit the instruction.
145 /// Size the instruction with Desc.getSize().
146 void MipsMCCodeEmitter::
147 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const
152 // Non-pseudo instructions that get changed for direct object
153 // only based on operand values.
154 // If this list of instructions get much longer we will move
155 // the check to a function call. Until then, this is more efficient.
157 switch (MI.getOpcode()) {
158 // If shift amount is >= 32 it the inst needs to be lowered further
163 LowerLargeShift(TmpInst);
165 // Double extract instruction is chosen by pos and size operands
168 LowerDextDins(TmpInst);
171 unsigned long N = Fixups.size();
172 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
174 // Check for unimplemented opcodes.
175 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
176 // so we have to special check for them.
177 unsigned Opcode = TmpInst.getOpcode();
178 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
179 (Opcode != Mips::SLL_MM) && !Binary)
180 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
182 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
183 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
184 if (NewOpcode != -1) {
185 if (Fixups.size() > N)
188 TmpInst.setOpcode (NewOpcode);
189 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
193 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
195 // Get byte count of instruction
196 unsigned Size = Desc.getSize();
198 llvm_unreachable("Desc.getSize() returns 0");
200 EmitInstruction(Binary, Size, STI, OS);
203 /// getBranchTargetOpValue - Return binary encoding of the branch
204 /// target operand. If the machine operand requires relocation,
205 /// record the relocation and return zero.
206 unsigned MipsMCCodeEmitter::
207 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
208 SmallVectorImpl<MCFixup> &Fixups,
209 const MCSubtargetInfo &STI) const {
211 const MCOperand &MO = MI.getOperand(OpNo);
213 // If the destination is an immediate, divide by 4.
214 if (MO.isImm()) return MO.getImm() >> 2;
216 assert(MO.isExpr() &&
217 "getBranchTargetOpValue expects only expressions or immediates");
219 const MCExpr *Expr = MO.getExpr();
220 Fixups.push_back(MCFixup::Create(0, Expr,
221 MCFixupKind(Mips::fixup_Mips_PC16)));
225 /// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
226 /// target operand. If the machine operand requires relocation,
227 /// record the relocation and return zero.
228 unsigned MipsMCCodeEmitter::
229 getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
230 SmallVectorImpl<MCFixup> &Fixups,
231 const MCSubtargetInfo &STI) const {
233 const MCOperand &MO = MI.getOperand(OpNo);
235 // If the destination is an immediate, divide by 2.
236 if (MO.isImm()) return MO.getImm() >> 1;
238 assert(MO.isExpr() &&
239 "getBranchTargetOpValueMM expects only expressions or immediates");
241 const MCExpr *Expr = MO.getExpr();
242 Fixups.push_back(MCFixup::Create(0, Expr,
243 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
247 /// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
248 /// 10-bit branch target operand. If the machine operand requires relocation,
249 /// record the relocation and return zero.
250 unsigned MipsMCCodeEmitter::
251 getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
252 SmallVectorImpl<MCFixup> &Fixups,
253 const MCSubtargetInfo &STI) const {
255 const MCOperand &MO = MI.getOperand(OpNo);
257 // If the destination is an immediate, divide by 2.
258 if (MO.isImm()) return MO.getImm() >> 1;
260 assert(MO.isExpr() &&
261 "getBranchTargetOpValuePC10 expects only expressions or immediates");
263 const MCExpr *Expr = MO.getExpr();
264 Fixups.push_back(MCFixup::Create(0, Expr,
265 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
269 /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
270 /// target operand. If the machine operand requires relocation,
271 /// record the relocation and return zero.
272 unsigned MipsMCCodeEmitter::
273 getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
274 SmallVectorImpl<MCFixup> &Fixups,
275 const MCSubtargetInfo &STI) const {
277 const MCOperand &MO = MI.getOperand(OpNo);
279 // If the destination is an immediate, divide by 2.
280 if (MO.isImm()) return MO.getImm() >> 1;
282 assert(MO.isExpr() &&
283 "getBranchTargetOpValueMM expects only expressions or immediates");
285 const MCExpr *Expr = MO.getExpr();
286 Fixups.push_back(MCFixup::Create(0, Expr,
288 fixup_MICROMIPS_PC16_S1)));
292 /// getBranchTarget21OpValue - Return binary encoding of the branch
293 /// target operand. If the machine operand requires relocation,
294 /// record the relocation and return zero.
295 unsigned MipsMCCodeEmitter::
296 getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
297 SmallVectorImpl<MCFixup> &Fixups,
298 const MCSubtargetInfo &STI) const {
300 const MCOperand &MO = MI.getOperand(OpNo);
302 // If the destination is an immediate, divide by 4.
303 if (MO.isImm()) return MO.getImm() >> 2;
305 assert(MO.isExpr() &&
306 "getBranchTarget21OpValue expects only expressions or immediates");
308 const MCExpr *Expr = MO.getExpr();
309 Fixups.push_back(MCFixup::Create(0, Expr,
310 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
314 /// getBranchTarget26OpValue - Return binary encoding of the branch
315 /// target operand. If the machine operand requires relocation,
316 /// record the relocation and return zero.
317 unsigned MipsMCCodeEmitter::
318 getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
319 SmallVectorImpl<MCFixup> &Fixups,
320 const MCSubtargetInfo &STI) const {
322 const MCOperand &MO = MI.getOperand(OpNo);
324 // If the destination is an immediate, divide by 4.
325 if (MO.isImm()) return MO.getImm() >> 2;
327 assert(MO.isExpr() &&
328 "getBranchTarget26OpValue expects only expressions or immediates");
330 const MCExpr *Expr = MO.getExpr();
331 Fixups.push_back(MCFixup::Create(0, Expr,
332 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
336 /// getJumpOffset16OpValue - Return binary encoding of the jump
337 /// target operand. If the machine operand requires relocation,
338 /// record the relocation and return zero.
339 unsigned MipsMCCodeEmitter::
340 getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
341 SmallVectorImpl<MCFixup> &Fixups,
342 const MCSubtargetInfo &STI) const {
344 const MCOperand &MO = MI.getOperand(OpNo);
346 if (MO.isImm()) return MO.getImm();
348 assert(MO.isExpr() &&
349 "getJumpOffset16OpValue expects only expressions or an immediate");
355 /// getJumpTargetOpValue - Return binary encoding of the jump
356 /// target operand. If the machine operand requires relocation,
357 /// record the relocation and return zero.
358 unsigned MipsMCCodeEmitter::
359 getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
360 SmallVectorImpl<MCFixup> &Fixups,
361 const MCSubtargetInfo &STI) const {
363 const MCOperand &MO = MI.getOperand(OpNo);
364 // If the destination is an immediate, divide by 4.
365 if (MO.isImm()) return MO.getImm()>>2;
367 assert(MO.isExpr() &&
368 "getJumpTargetOpValue expects only expressions or an immediate");
370 const MCExpr *Expr = MO.getExpr();
371 Fixups.push_back(MCFixup::Create(0, Expr,
372 MCFixupKind(Mips::fixup_Mips_26)));
376 unsigned MipsMCCodeEmitter::
377 getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
378 SmallVectorImpl<MCFixup> &Fixups,
379 const MCSubtargetInfo &STI) const {
381 const MCOperand &MO = MI.getOperand(OpNo);
382 // If the destination is an immediate, divide by 2.
383 if (MO.isImm()) return MO.getImm() >> 1;
385 assert(MO.isExpr() &&
386 "getJumpTargetOpValueMM expects only expressions or an immediate");
388 const MCExpr *Expr = MO.getExpr();
389 Fixups.push_back(MCFixup::Create(0, Expr,
390 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
394 unsigned MipsMCCodeEmitter::
395 getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
396 SmallVectorImpl<MCFixup> &Fixups,
397 const MCSubtargetInfo &STI) const {
399 const MCOperand &MO = MI.getOperand(OpNo);
401 // The immediate is encoded as 'immediate << 2'.
402 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
403 assert((Res & 3) == 0);
407 assert(MO.isExpr() &&
408 "getUImm5Lsl2Encoding expects only expressions or an immediate");
413 unsigned MipsMCCodeEmitter::
414 getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
415 SmallVectorImpl<MCFixup> &Fixups,
416 const MCSubtargetInfo &STI) const {
418 const MCOperand &MO = MI.getOperand(OpNo);
420 int Value = MO.getImm();
427 unsigned MipsMCCodeEmitter::
428 getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
429 SmallVectorImpl<MCFixup> &Fixups,
430 const MCSubtargetInfo &STI) const {
432 const MCOperand &MO = MI.getOperand(OpNo);
434 unsigned Value = MO.getImm();
441 unsigned MipsMCCodeEmitter::
442 getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
443 SmallVectorImpl<MCFixup> &Fixups,
444 const MCSubtargetInfo &STI) const {
446 const MCOperand &MO = MI.getOperand(OpNo);
448 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
449 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
455 unsigned MipsMCCodeEmitter::
456 getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
457 const MCSubtargetInfo &STI) const {
460 if (Expr->EvaluateAsAbsolute(Res))
463 MCExpr::ExprKind Kind = Expr->getKind();
464 if (Kind == MCExpr::Constant) {
465 return cast<MCConstantExpr>(Expr)->getValue();
468 if (Kind == MCExpr::Binary) {
469 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
470 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
474 if (Kind == MCExpr::Target) {
475 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
477 Mips::Fixups FixupKind = Mips::Fixups(0);
478 switch (MipsExpr->getKind()) {
479 default: llvm_unreachable("Unsupported fixup kind for target expression!");
480 case MipsMCExpr::VK_Mips_HIGHEST:
481 FixupKind = Mips::fixup_Mips_HIGHEST;
483 case MipsMCExpr::VK_Mips_HIGHER:
484 FixupKind = Mips::fixup_Mips_HIGHER;
486 case MipsMCExpr::VK_Mips_HI:
487 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
488 : Mips::fixup_Mips_HI16;
490 case MipsMCExpr::VK_Mips_LO:
491 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
492 : Mips::fixup_Mips_LO16;
495 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
499 if (Kind == MCExpr::SymbolRef) {
500 Mips::Fixups FixupKind = Mips::Fixups(0);
502 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
503 default: llvm_unreachable("Unknown fixup kind!");
505 case MCSymbolRefExpr::VK_None:
506 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
508 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
509 FixupKind = Mips::fixup_Mips_GPOFF_HI;
511 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
512 FixupKind = Mips::fixup_Mips_GPOFF_LO;
514 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
515 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
516 : Mips::fixup_Mips_GOT_PAGE;
518 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
519 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
520 : Mips::fixup_Mips_GOT_OFST;
522 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
523 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
524 : Mips::fixup_Mips_GOT_DISP;
526 case MCSymbolRefExpr::VK_Mips_GPREL:
527 FixupKind = Mips::fixup_Mips_GPREL16;
529 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
530 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
531 : Mips::fixup_Mips_CALL16;
533 case MCSymbolRefExpr::VK_Mips_GOT16:
534 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
535 : Mips::fixup_Mips_GOT_Global;
537 case MCSymbolRefExpr::VK_Mips_GOT:
538 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
539 : Mips::fixup_Mips_GOT_Local;
541 case MCSymbolRefExpr::VK_Mips_ABS_HI:
542 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
543 : Mips::fixup_Mips_HI16;
545 case MCSymbolRefExpr::VK_Mips_ABS_LO:
546 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
547 : Mips::fixup_Mips_LO16;
549 case MCSymbolRefExpr::VK_Mips_TLSGD:
550 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
551 : Mips::fixup_Mips_TLSGD;
553 case MCSymbolRefExpr::VK_Mips_TLSLDM:
554 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
555 : Mips::fixup_Mips_TLSLDM;
557 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
558 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
559 : Mips::fixup_Mips_DTPREL_HI;
561 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
562 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
563 : Mips::fixup_Mips_DTPREL_LO;
565 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
566 FixupKind = Mips::fixup_Mips_GOTTPREL;
568 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
569 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
570 : Mips::fixup_Mips_TPREL_HI;
572 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
573 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
574 : Mips::fixup_Mips_TPREL_LO;
576 case MCSymbolRefExpr::VK_Mips_HIGHER:
577 FixupKind = Mips::fixup_Mips_HIGHER;
579 case MCSymbolRefExpr::VK_Mips_HIGHEST:
580 FixupKind = Mips::fixup_Mips_HIGHEST;
582 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
583 FixupKind = Mips::fixup_Mips_GOT_HI16;
585 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
586 FixupKind = Mips::fixup_Mips_GOT_LO16;
588 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
589 FixupKind = Mips::fixup_Mips_CALL_HI16;
591 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
592 FixupKind = Mips::fixup_Mips_CALL_LO16;
594 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
595 FixupKind = Mips::fixup_MIPS_PCHI16;
597 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
598 FixupKind = Mips::fixup_MIPS_PCLO16;
602 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
608 /// getMachineOpValue - Return binary encoding of operand. If the machine
609 /// operand requires relocation, record the relocation and return zero.
610 unsigned MipsMCCodeEmitter::
611 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
612 SmallVectorImpl<MCFixup> &Fixups,
613 const MCSubtargetInfo &STI) const {
615 unsigned Reg = MO.getReg();
616 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
618 } else if (MO.isImm()) {
619 return static_cast<unsigned>(MO.getImm());
620 } else if (MO.isFPImm()) {
621 return static_cast<unsigned>(APFloat(MO.getFPImm())
622 .bitcastToAPInt().getHiBits(32).getLimitedValue());
624 // MO must be an Expr.
626 return getExprOpValue(MO.getExpr(),Fixups, STI);
629 /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
632 MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
633 SmallVectorImpl<MCFixup> &Fixups,
634 const MCSubtargetInfo &STI) const {
635 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
636 assert(MI.getOperand(OpNo).isReg());
637 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
638 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
640 // The immediate field of an LD/ST instruction is scaled which means it must
641 // be divided (when encoding) by the size (in bytes) of the instructions'
647 switch(MI.getOpcode())
650 assert (0 && "Unexpected instruction");
654 // We don't need to scale the offset in this case
670 return (OffBits & 0xFFFF) | RegBits;
673 /// getMemEncoding - Return binary encoding of memory related operand.
674 /// If the offset operand requires relocation, record the relocation.
676 MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
677 SmallVectorImpl<MCFixup> &Fixups,
678 const MCSubtargetInfo &STI) const {
679 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
680 assert(MI.getOperand(OpNo).isReg());
681 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
682 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
684 return (OffBits & 0xFFFF) | RegBits;
687 unsigned MipsMCCodeEmitter::
688 getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
689 SmallVectorImpl<MCFixup> &Fixups,
690 const MCSubtargetInfo &STI) const {
691 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
692 assert(MI.getOperand(OpNo).isReg());
693 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
695 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
698 return (OffBits & 0xF) | RegBits;
701 unsigned MipsMCCodeEmitter::
702 getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
703 SmallVectorImpl<MCFixup> &Fixups,
704 const MCSubtargetInfo &STI) const {
705 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
706 assert(MI.getOperand(OpNo).isReg());
707 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
709 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
712 return (OffBits & 0xF) | RegBits;
715 unsigned MipsMCCodeEmitter::
716 getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
717 SmallVectorImpl<MCFixup> &Fixups,
718 const MCSubtargetInfo &STI) const {
719 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
720 assert(MI.getOperand(OpNo).isReg());
721 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
723 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
726 return (OffBits & 0xF) | RegBits;
729 unsigned MipsMCCodeEmitter::
730 getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
731 SmallVectorImpl<MCFixup> &Fixups,
732 const MCSubtargetInfo &STI) const {
733 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
734 assert(MI.getOperand(OpNo).isReg() &&
735 MI.getOperand(OpNo).getReg() == Mips::SP &&
736 "Unexpected base register!");
737 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
740 return OffBits & 0x1F;
743 unsigned MipsMCCodeEmitter::
744 getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
745 SmallVectorImpl<MCFixup> &Fixups,
746 const MCSubtargetInfo &STI) const {
747 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
748 assert(MI.getOperand(OpNo).isReg() &&
749 MI.getOperand(OpNo).getReg() == Mips::GP &&
750 "Unexpected base register!");
752 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
755 return OffBits & 0x7F;
758 unsigned MipsMCCodeEmitter::
759 getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
760 SmallVectorImpl<MCFixup> &Fixups,
761 const MCSubtargetInfo &STI) const {
762 // opNum can be invalid if instruction had reglist as operand.
763 // MemOperand is always last operand of instruction (base + offset).
764 switch (MI.getOpcode()) {
769 OpNo = MI.getNumOperands() - 2;
773 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
774 assert(MI.getOperand(OpNo).isReg());
775 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
776 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
778 return (OffBits & 0x0FFF) | RegBits;
781 unsigned MipsMCCodeEmitter::
782 getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
783 SmallVectorImpl<MCFixup> &Fixups,
784 const MCSubtargetInfo &STI) const {
785 // opNum can be invalid if instruction had reglist as operand
786 // MemOperand is always last operand of instruction (base + offset)
787 switch (MI.getOpcode()) {
792 OpNo = MI.getNumOperands() - 2;
796 // Offset is encoded in bits 4-0.
797 assert(MI.getOperand(OpNo).isReg());
798 // Base register is always SP - thus it is not encoded.
799 assert(MI.getOperand(OpNo+1).isImm());
800 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
802 return ((OffBits >> 2) & 0x0F);
806 MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
807 SmallVectorImpl<MCFixup> &Fixups,
808 const MCSubtargetInfo &STI) const {
809 assert(MI.getOperand(OpNo).isImm());
810 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
811 return SizeEncoding - 1;
814 // FIXME: should be called getMSBEncoding
817 MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
818 SmallVectorImpl<MCFixup> &Fixups,
819 const MCSubtargetInfo &STI) const {
820 assert(MI.getOperand(OpNo-1).isImm());
821 assert(MI.getOperand(OpNo).isImm());
822 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
823 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
825 return Position + Size - 1;
829 MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
830 SmallVectorImpl<MCFixup> &Fixups,
831 const MCSubtargetInfo &STI) const {
832 assert(MI.getOperand(OpNo).isImm());
833 // The immediate is encoded as 'immediate - 1'.
834 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
838 MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
839 SmallVectorImpl<MCFixup> &Fixups,
840 const MCSubtargetInfo &STI) const {
841 const MCOperand &MO = MI.getOperand(OpNo);
843 // The immediate is encoded as 'immediate << 2'.
844 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
845 assert((Res & 3) == 0);
849 assert(MO.isExpr() &&
850 "getSimm19Lsl2Encoding expects only expressions or an immediate");
852 const MCExpr *Expr = MO.getExpr();
853 Fixups.push_back(MCFixup::Create(0, Expr,
854 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
859 MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
860 SmallVectorImpl<MCFixup> &Fixups,
861 const MCSubtargetInfo &STI) const {
862 const MCOperand &MO = MI.getOperand(OpNo);
864 // The immediate is encoded as 'immediate << 3'.
865 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
866 assert((Res & 7) == 0);
870 assert(MO.isExpr() &&
871 "getSimm18Lsl2Encoding expects only expressions or an immediate");
873 const MCExpr *Expr = MO.getExpr();
874 Fixups.push_back(MCFixup::Create(0, Expr,
875 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
880 MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
881 SmallVectorImpl<MCFixup> &Fixups,
882 const MCSubtargetInfo &STI) const {
883 assert(MI.getOperand(OpNo).isImm());
884 const MCOperand &MO = MI.getOperand(OpNo);
885 return MO.getImm() % 8;
889 MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
890 SmallVectorImpl<MCFixup> &Fixups,
891 const MCSubtargetInfo &STI) const {
892 assert(MI.getOperand(OpNo).isImm());
893 const MCOperand &MO = MI.getOperand(OpNo);
894 unsigned Value = MO.getImm();
896 case 128: return 0x0;
909 case 255: return 0xd;
910 case 32768: return 0xe;
911 case 65535: return 0xf;
913 llvm_unreachable("Unexpected value");
917 MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
918 SmallVectorImpl<MCFixup> &Fixups,
919 const MCSubtargetInfo &STI) const {
922 // Register list operand is always first operand of instruction and it is
923 // placed before memory operand (register + imm).
925 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
926 unsigned Reg = MI.getOperand(I).getReg();
927 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
937 MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
938 SmallVectorImpl<MCFixup> &Fixups,
939 const MCSubtargetInfo &STI) const {
940 return (MI.getNumOperands() - 4);
944 MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
945 SmallVectorImpl<MCFixup> &Fixups,
946 const MCSubtargetInfo &STI) const {
947 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
951 MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
952 SmallVectorImpl<MCFixup> &Fixups,
953 const MCSubtargetInfo &STI) const {
956 if (MI.getOperand(0).getReg() == Mips::A1 &&
957 MI.getOperand(1).getReg() == Mips::A2)
959 else if (MI.getOperand(0).getReg() == Mips::A1 &&
960 MI.getOperand(1).getReg() == Mips::A3)
962 else if (MI.getOperand(0).getReg() == Mips::A2 &&
963 MI.getOperand(1).getReg() == Mips::A3)
965 else if (MI.getOperand(0).getReg() == Mips::A0 &&
966 MI.getOperand(1).getReg() == Mips::S5)
968 else if (MI.getOperand(0).getReg() == Mips::A0 &&
969 MI.getOperand(1).getReg() == Mips::S6)
971 else if (MI.getOperand(0).getReg() == Mips::A0 &&
972 MI.getOperand(1).getReg() == Mips::A1)
974 else if (MI.getOperand(0).getReg() == Mips::A0 &&
975 MI.getOperand(1).getReg() == Mips::A2)
977 else if (MI.getOperand(0).getReg() == Mips::A0 &&
978 MI.getOperand(1).getReg() == Mips::A3)
985 MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
986 SmallVectorImpl<MCFixup> &Fixups,
987 const MCSubtargetInfo &STI) const {
988 const MCOperand &MO = MI.getOperand(OpNo);
989 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
990 // The immediate is encoded as 'immediate >> 2'.
991 unsigned Res = static_cast<unsigned>(MO.getImm());
992 assert((Res & 3) == 0);
996 #include "MipsGenMCCodeEmitter.inc"