1 //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the MipsMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 
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/MCRegisterInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Support/raw_ostream.h"
29 
30 #define DEBUG_TYPE "mccodeemitter"
31 
32 #define GET_INSTRMAP_INFO
33 #include "MipsGenInstrInfo.inc"
34 #undef GET_INSTRMAP_INFO
35 
36 namespace llvm {
37 MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
38                                          const MCRegisterInfo &MRI,
39                                          MCContext &Ctx) {
40   return new MipsMCCodeEmitter(MCII, Ctx, false);
41 }
42 
43 MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
44                                          const MCRegisterInfo &MRI,
45                                          MCContext &Ctx) {
46   return new MipsMCCodeEmitter(MCII, Ctx, true);
47 }
48 } // End of namespace llvm.
49 
50 // If the D<shift> instruction has a shift amount that is greater
51 // than 31 (checked in calling routine), lower it to a D<shift>32 instruction
52 static void LowerLargeShift(MCInst& Inst) {
53 
54   assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
55   assert(Inst.getOperand(2).isImm());
56 
57   int64_t Shift = Inst.getOperand(2).getImm();
58   if (Shift <= 31)
59     return; // Do nothing
60   Shift -= 32;
61 
62   // saminus32
63   Inst.getOperand(2).setImm(Shift);
64 
65   switch (Inst.getOpcode()) {
66   default:
67     // Calling function is not synchronized
68     llvm_unreachable("Unexpected shift instruction");
69   case Mips::DSLL:
70     Inst.setOpcode(Mips::DSLL32);
71     return;
72   case Mips::DSRL:
73     Inst.setOpcode(Mips::DSRL32);
74     return;
75   case Mips::DSRA:
76     Inst.setOpcode(Mips::DSRA32);
77     return;
78   case Mips::DROTR:
79     Inst.setOpcode(Mips::DROTR32);
80     return;
81   }
82 }
83 
84 // Pick a DINS instruction variant based on the pos and size operands
85 static void LowerDins(MCInst& InstIn) {
86   assert(InstIn.getNumOperands() == 5 &&
87          "Invalid no. of machine operands for DINS!");
88 
89   assert(InstIn.getOperand(2).isImm());
90   int64_t pos = InstIn.getOperand(2).getImm();
91   assert(InstIn.getOperand(3).isImm());
92   int64_t size = InstIn.getOperand(3).getImm();
93 
94   if (size <= 32) {
95     if (pos < 32)  // DINS, do nothing
96       return;
97     // DINSU
98     InstIn.getOperand(2).setImm(pos - 32);
99     InstIn.setOpcode(Mips::DINSU);
100     return;
101   }
102   // DINSM
103   assert(pos < 32 && "DINS cannot have both size and pos > 32");
104   InstIn.getOperand(3).setImm(size - 32);
105   InstIn.setOpcode(Mips::DINSM);
106   return;
107 }
108 
109 // Fix a bad compact branch encoding for beqc/bnec.
110 void MipsMCCodeEmitter::LowerCompactBranch(MCInst& Inst) const {
111 
112   // Encoding may be illegal !(rs < rt), but this situation is
113   // easily fixed.
114   unsigned RegOp0 = Inst.getOperand(0).getReg();
115   unsigned RegOp1 = Inst.getOperand(1).getReg();
116 
117   unsigned Reg0 =  Ctx.getRegisterInfo()->getEncodingValue(RegOp0);
118   unsigned Reg1 =  Ctx.getRegisterInfo()->getEncodingValue(RegOp1);
119 
120   if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC) {
121     assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
122     if (Reg0 < Reg1)
123       return;
124   } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
125     if (Reg0 >= Reg1)
126       return;
127   } else
128    llvm_unreachable("Cannot rewrite unknown branch!");
129 
130   Inst.getOperand(0).setReg(RegOp1);
131   Inst.getOperand(1).setReg(RegOp0);
132 
133 }
134 
135 bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
136   return STI.getFeatureBits()[Mips::FeatureMicroMips];
137 }
138 
139 bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
140   return STI.getFeatureBits()[Mips::FeatureMips32r6];
141 }
142 
143 void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
144   OS << (char)C;
145 }
146 
147 void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
148                                         const MCSubtargetInfo &STI,
149                                         raw_ostream &OS) const {
150   // Output the instruction encoding in little endian byte order.
151   // Little-endian byte ordering:
152   //   mips32r2:   4 | 3 | 2 | 1
153   //   microMIPS:  2 | 1 | 4 | 3
154   if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
155     EmitInstruction(Val >> 16, 2, STI, OS);
156     EmitInstruction(Val, 2, STI, OS);
157   } else {
158     for (unsigned i = 0; i < Size; ++i) {
159       unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
160       EmitByte((Val >> Shift) & 0xff, OS);
161     }
162   }
163 }
164 
165 /// encodeInstruction - Emit the instruction.
166 /// Size the instruction with Desc.getSize().
167 void MipsMCCodeEmitter::
168 encodeInstruction(const MCInst &MI, raw_ostream &OS,
169                   SmallVectorImpl<MCFixup> &Fixups,
170                   const MCSubtargetInfo &STI) const
171 {
172 
173   // Non-pseudo instructions that get changed for direct object
174   // only based on operand values.
175   // If this list of instructions get much longer we will move
176   // the check to a function call. Until then, this is more efficient.
177   MCInst TmpInst = MI;
178   switch (MI.getOpcode()) {
179   // If shift amount is >= 32 it the inst needs to be lowered further
180   case Mips::DSLL:
181   case Mips::DSRL:
182   case Mips::DSRA:
183   case Mips::DROTR:
184     LowerLargeShift(TmpInst);
185     break;
186     // Double extract instruction is chosen by pos and size operands
187   case Mips::DINS:
188     LowerDins(TmpInst);
189     break;
190   // Compact branches, enforce encoding restrictions.
191   case Mips::BEQC:
192   case Mips::BNEC:
193   case Mips::BOVC:
194   case Mips::BNVC:
195     LowerCompactBranch(TmpInst);
196   }
197 
198   unsigned long N = Fixups.size();
199   uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
200 
201   // Check for unimplemented opcodes.
202   // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
203   // so we have to special check for them.
204   unsigned Opcode = TmpInst.getOpcode();
205   if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
206       (Opcode != Mips::SLL_MM) && !Binary)
207     llvm_unreachable("unimplemented opcode in encodeInstruction()");
208 
209   int NewOpcode = -1;
210   if (isMicroMips(STI)) {
211     if (isMips32r6(STI)) {
212       NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
213       if (NewOpcode == -1)
214         NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
215     }
216     else
217       NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
218 
219     // Check whether it is Dsp instruction.
220     if (NewOpcode == -1)
221       NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
222 
223     if (NewOpcode != -1) {
224       if (Fixups.size() > N)
225         Fixups.pop_back();
226 
227       Opcode = NewOpcode;
228       TmpInst.setOpcode (NewOpcode);
229       Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
230     }
231   }
232 
233   const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
234 
235   // Get byte count of instruction
236   unsigned Size = Desc.getSize();
237   if (!Size)
238     llvm_unreachable("Desc.getSize() returns 0");
239 
240   EmitInstruction(Binary, Size, STI, OS);
241 }
242 
243 /// getBranchTargetOpValue - Return binary encoding of the branch
244 /// target operand. If the machine operand requires relocation,
245 /// record the relocation and return zero.
246 unsigned MipsMCCodeEmitter::
247 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
248                        SmallVectorImpl<MCFixup> &Fixups,
249                        const MCSubtargetInfo &STI) const {
250 
251   const MCOperand &MO = MI.getOperand(OpNo);
252 
253   // If the destination is an immediate, divide by 4.
254   if (MO.isImm()) return MO.getImm() >> 2;
255 
256   assert(MO.isExpr() &&
257          "getBranchTargetOpValue expects only expressions or immediates");
258 
259   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
260       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
261   Fixups.push_back(MCFixup::create(0, FixupExpression,
262                                    MCFixupKind(Mips::fixup_Mips_PC16)));
263   return 0;
264 }
265 
266 /// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
267 /// target operand. If the machine operand requires relocation,
268 /// record the relocation and return zero.
269 unsigned MipsMCCodeEmitter::
270 getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
271                               SmallVectorImpl<MCFixup> &Fixups,
272                               const MCSubtargetInfo &STI) const {
273 
274   const MCOperand &MO = MI.getOperand(OpNo);
275 
276   // If the destination is an immediate, divide by 2.
277   if (MO.isImm()) return MO.getImm() >> 1;
278 
279   assert(MO.isExpr() &&
280          "getBranchTargetOpValue expects only expressions or immediates");
281 
282   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
283       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
284   Fixups.push_back(MCFixup::create(0, FixupExpression,
285                                    MCFixupKind(Mips::fixup_Mips_PC16)));
286   return 0;
287 }
288 
289 /// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
290 /// target operand. If the machine operand requires relocation,
291 /// record the relocation and return zero.
292 unsigned MipsMCCodeEmitter::
293 getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
294                           SmallVectorImpl<MCFixup> &Fixups,
295                           const MCSubtargetInfo &STI) const {
296 
297   const MCOperand &MO = MI.getOperand(OpNo);
298 
299   // If the destination is an immediate, divide by 2.
300   if (MO.isImm()) return MO.getImm() >> 1;
301 
302   assert(MO.isExpr() &&
303          "getBranchTargetOpValueMM expects only expressions or immediates");
304 
305   const MCExpr *Expr = MO.getExpr();
306   Fixups.push_back(MCFixup::create(0, Expr,
307                                    MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
308   return 0;
309 }
310 
311 /// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
312 /// 10-bit branch target operand. If the machine operand requires relocation,
313 /// record the relocation and return zero.
314 unsigned MipsMCCodeEmitter::
315 getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
316                              SmallVectorImpl<MCFixup> &Fixups,
317                              const MCSubtargetInfo &STI) const {
318 
319   const MCOperand &MO = MI.getOperand(OpNo);
320 
321   // If the destination is an immediate, divide by 2.
322   if (MO.isImm()) return MO.getImm() >> 1;
323 
324   assert(MO.isExpr() &&
325          "getBranchTargetOpValuePC10 expects only expressions or immediates");
326 
327   const MCExpr *Expr = MO.getExpr();
328   Fixups.push_back(MCFixup::create(0, Expr,
329                    MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
330   return 0;
331 }
332 
333 /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
334 /// target operand. If the machine operand requires relocation,
335 /// record the relocation and return zero.
336 unsigned MipsMCCodeEmitter::
337 getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
338                          SmallVectorImpl<MCFixup> &Fixups,
339                          const MCSubtargetInfo &STI) const {
340 
341   const MCOperand &MO = MI.getOperand(OpNo);
342 
343   // If the destination is an immediate, divide by 2.
344   if (MO.isImm()) return MO.getImm() >> 1;
345 
346   assert(MO.isExpr() &&
347          "getBranchTargetOpValueMM expects only expressions or immediates");
348 
349   const MCExpr *Expr = MO.getExpr();
350   Fixups.push_back(MCFixup::create(0, Expr,
351                    MCFixupKind(Mips::
352                                fixup_MICROMIPS_PC16_S1)));
353   return 0;
354 }
355 
356 /// getBranchTarget21OpValue - Return binary encoding of the branch
357 /// target operand. If the machine operand requires relocation,
358 /// record the relocation and return zero.
359 unsigned MipsMCCodeEmitter::
360 getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
361                          SmallVectorImpl<MCFixup> &Fixups,
362                          const MCSubtargetInfo &STI) const {
363 
364   const MCOperand &MO = MI.getOperand(OpNo);
365 
366   // If the destination is an immediate, divide by 4.
367   if (MO.isImm()) return MO.getImm() >> 2;
368 
369   assert(MO.isExpr() &&
370          "getBranchTarget21OpValue expects only expressions or immediates");
371 
372   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
373       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
374   Fixups.push_back(MCFixup::create(0, FixupExpression,
375                                    MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
376   return 0;
377 }
378 
379 /// getBranchTarget21OpValueMM - Return binary encoding of the branch
380 /// target operand for microMIPS. If the machine operand requires
381 /// relocation, record the relocation and return zero.
382 unsigned MipsMCCodeEmitter::
383 getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
384                            SmallVectorImpl<MCFixup> &Fixups,
385                            const MCSubtargetInfo &STI) const {
386 
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     "getBranchTarget21OpValueMM expects only expressions or immediates");
394 
395   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
396       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
397   Fixups.push_back(MCFixup::create(0, FixupExpression,
398                                    MCFixupKind(Mips::fixup_MICROMIPS_PC21_S1)));
399   return 0;
400 }
401 
402 /// getBranchTarget26OpValue - Return binary encoding of the branch
403 /// target operand. If the machine operand requires relocation,
404 /// record the relocation and return zero.
405 unsigned MipsMCCodeEmitter::
406 getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
407                          SmallVectorImpl<MCFixup> &Fixups,
408                          const MCSubtargetInfo &STI) const {
409 
410   const MCOperand &MO = MI.getOperand(OpNo);
411 
412   // If the destination is an immediate, divide by 4.
413   if (MO.isImm()) return MO.getImm() >> 2;
414 
415   assert(MO.isExpr() &&
416          "getBranchTarget26OpValue expects only expressions or immediates");
417 
418   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
419       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
420   Fixups.push_back(MCFixup::create(0, FixupExpression,
421                                    MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
422   return 0;
423 }
424 
425 /// getBranchTarget26OpValueMM - Return binary encoding of the branch
426 /// target operand. If the machine operand requires relocation,
427 /// record the relocation and return zero.
428 unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
429     const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
430     const MCSubtargetInfo &STI) const {
431 
432   const MCOperand &MO = MI.getOperand(OpNo);
433 
434   // If the destination is an immediate, divide by 2.
435   if (MO.isImm())
436     return MO.getImm() >> 1;
437 
438   assert(MO.isExpr() &&
439          "getBranchTarget26OpValueMM expects only expressions or immediates");
440 
441   const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
442       MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
443   Fixups.push_back(MCFixup::create(0, FixupExpression,
444                                    MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
445   return 0;
446 }
447 
448 /// getJumpOffset16OpValue - Return binary encoding of the jump
449 /// target operand. If the machine operand requires relocation,
450 /// record the relocation and return zero.
451 unsigned MipsMCCodeEmitter::
452 getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
453                        SmallVectorImpl<MCFixup> &Fixups,
454                        const MCSubtargetInfo &STI) const {
455 
456   const MCOperand &MO = MI.getOperand(OpNo);
457 
458   if (MO.isImm()) return MO.getImm();
459 
460   assert(MO.isExpr() &&
461          "getJumpOffset16OpValue expects only expressions or an immediate");
462 
463    // TODO: Push fixup.
464    return 0;
465 }
466 
467 /// getJumpTargetOpValue - Return binary encoding of the jump
468 /// target operand. If the machine operand requires relocation,
469 /// record the relocation and return zero.
470 unsigned MipsMCCodeEmitter::
471 getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
472                      SmallVectorImpl<MCFixup> &Fixups,
473                      const MCSubtargetInfo &STI) const {
474 
475   const MCOperand &MO = MI.getOperand(OpNo);
476   // If the destination is an immediate, divide by 4.
477   if (MO.isImm()) return MO.getImm()>>2;
478 
479   assert(MO.isExpr() &&
480          "getJumpTargetOpValue expects only expressions or an immediate");
481 
482   const MCExpr *Expr = MO.getExpr();
483   Fixups.push_back(MCFixup::create(0, Expr,
484                                    MCFixupKind(Mips::fixup_Mips_26)));
485   return 0;
486 }
487 
488 unsigned MipsMCCodeEmitter::
489 getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
490                        SmallVectorImpl<MCFixup> &Fixups,
491                        const MCSubtargetInfo &STI) const {
492 
493   const MCOperand &MO = MI.getOperand(OpNo);
494   // If the destination is an immediate, divide by 2.
495   if (MO.isImm()) return MO.getImm() >> 1;
496 
497   assert(MO.isExpr() &&
498          "getJumpTargetOpValueMM expects only expressions or an immediate");
499 
500   const MCExpr *Expr = MO.getExpr();
501   Fixups.push_back(MCFixup::create(0, Expr,
502                                    MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
503   return 0;
504 }
505 
506 unsigned MipsMCCodeEmitter::
507 getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
508                      SmallVectorImpl<MCFixup> &Fixups,
509                      const MCSubtargetInfo &STI) const {
510 
511   const MCOperand &MO = MI.getOperand(OpNo);
512   if (MO.isImm()) {
513     // The immediate is encoded as 'immediate << 2'.
514     unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
515     assert((Res & 3) == 0);
516     return Res >> 2;
517   }
518 
519   assert(MO.isExpr() &&
520          "getUImm5Lsl2Encoding expects only expressions or an immediate");
521 
522   return 0;
523 }
524 
525 unsigned MipsMCCodeEmitter::
526 getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
527                   SmallVectorImpl<MCFixup> &Fixups,
528                   const MCSubtargetInfo &STI) const {
529 
530   const MCOperand &MO = MI.getOperand(OpNo);
531   if (MO.isImm()) {
532     int Value = MO.getImm();
533     return Value >> 2;
534   }
535 
536   return 0;
537 }
538 
539 unsigned MipsMCCodeEmitter::
540 getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
541                      SmallVectorImpl<MCFixup> &Fixups,
542                      const MCSubtargetInfo &STI) const {
543 
544   const MCOperand &MO = MI.getOperand(OpNo);
545   if (MO.isImm()) {
546     unsigned Value = MO.getImm();
547     return Value >> 2;
548   }
549 
550   return 0;
551 }
552 
553 unsigned MipsMCCodeEmitter::
554 getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
555                      SmallVectorImpl<MCFixup> &Fixups,
556                      const MCSubtargetInfo &STI) const {
557 
558   const MCOperand &MO = MI.getOperand(OpNo);
559   if (MO.isImm()) {
560     unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
561     return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
562   }
563 
564   return 0;
565 }
566 
567 unsigned MipsMCCodeEmitter::
568 getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
569                const MCSubtargetInfo &STI) const {
570   int64_t Res;
571 
572   if (Expr->evaluateAsAbsolute(Res))
573     return Res;
574 
575   MCExpr::ExprKind Kind = Expr->getKind();
576   if (Kind == MCExpr::Constant) {
577     return cast<MCConstantExpr>(Expr)->getValue();
578   }
579 
580   if (Kind == MCExpr::Binary) {
581     unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
582     Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
583     return Res;
584   }
585 
586   if (Kind == MCExpr::Target) {
587     const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
588 
589     Mips::Fixups FixupKind = Mips::Fixups(0);
590     switch (MipsExpr->getKind()) {
591     case MipsMCExpr::MEK_NEG:
592     case MipsMCExpr::MEK_None:
593     case MipsMCExpr::MEK_Special:
594       llvm_unreachable("Unhandled fixup kind!");
595       break;
596     case MipsMCExpr::MEK_CALL_HI16:
597       FixupKind = Mips::fixup_Mips_CALL_HI16;
598       break;
599     case MipsMCExpr::MEK_CALL_LO16:
600       FixupKind = Mips::fixup_Mips_CALL_LO16;
601       break;
602     case MipsMCExpr::MEK_DTPREL_HI:
603       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
604                                    : Mips::fixup_Mips_DTPREL_HI;
605       break;
606     case MipsMCExpr::MEK_DTPREL_LO:
607       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
608                                    : Mips::fixup_Mips_DTPREL_LO;
609       break;
610     case MipsMCExpr::MEK_GOTTPREL:
611       FixupKind = Mips::fixup_Mips_GOTTPREL;
612       break;
613     case MipsMCExpr::MEK_GOT:
614       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
615                                    : Mips::fixup_Mips_GOT;
616       break;
617     case MipsMCExpr::MEK_GOT_CALL:
618       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
619                                    : Mips::fixup_Mips_CALL16;
620       break;
621     case MipsMCExpr::MEK_GOT_DISP:
622       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
623                                    : Mips::fixup_Mips_GOT_DISP;
624       break;
625     case MipsMCExpr::MEK_GOT_HI16:
626       FixupKind = Mips::fixup_Mips_GOT_HI16;
627       break;
628     case MipsMCExpr::MEK_GOT_LO16:
629       FixupKind = Mips::fixup_Mips_GOT_LO16;
630       break;
631     case MipsMCExpr::MEK_GOT_PAGE:
632       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
633                                    : Mips::fixup_Mips_GOT_PAGE;
634       break;
635     case MipsMCExpr::MEK_GOT_OFST:
636       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
637                                    : Mips::fixup_Mips_GOT_OFST;
638       break;
639     case MipsMCExpr::MEK_GPREL:
640       FixupKind = Mips::fixup_Mips_GPREL16;
641       break;
642     case MipsMCExpr::MEK_LO: {
643       // Check for %lo(%neg(%gp_rel(X)))
644       if (MipsExpr->isGpOff()) {
645         FixupKind = Mips::fixup_Mips_GPOFF_LO;
646         break;
647       }
648       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
649                                    : Mips::fixup_Mips_LO16;
650       break;
651     }
652     case MipsMCExpr::MEK_HIGHEST:
653       FixupKind = Mips::fixup_Mips_HIGHEST;
654       break;
655     case MipsMCExpr::MEK_HIGHER:
656       FixupKind = Mips::fixup_Mips_HIGHER;
657       break;
658     case MipsMCExpr::MEK_HI:
659       // Check for %hi(%neg(%gp_rel(X)))
660       if (MipsExpr->isGpOff()) {
661         FixupKind = Mips::fixup_Mips_GPOFF_HI;
662         break;
663       }
664       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
665                                    : Mips::fixup_Mips_HI16;
666       break;
667     case MipsMCExpr::MEK_PCREL_HI16:
668       FixupKind = Mips::fixup_MIPS_PCHI16;
669       break;
670     case MipsMCExpr::MEK_PCREL_LO16:
671       FixupKind = Mips::fixup_MIPS_PCLO16;
672       break;
673     case MipsMCExpr::MEK_TLSGD:
674       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
675                                    : Mips::fixup_Mips_TLSGD;
676       break;
677     case MipsMCExpr::MEK_TLSLDM:
678       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
679                                    : Mips::fixup_Mips_TLSLDM;
680       break;
681     case MipsMCExpr::MEK_TPREL_HI:
682       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
683                                    : Mips::fixup_Mips_TPREL_HI;
684       break;
685     case MipsMCExpr::MEK_TPREL_LO:
686       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
687                                    : Mips::fixup_Mips_TPREL_LO;
688       break;
689     }
690     Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
691     return 0;
692   }
693 
694   if (Kind == MCExpr::SymbolRef) {
695     Mips::Fixups FixupKind = Mips::Fixups(0);
696 
697     switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
698     default: llvm_unreachable("Unknown fixup kind!");
699       break;
700     case MCSymbolRefExpr::VK_None:
701       FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
702       break;
703     } // switch
704 
705     Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
706     return 0;
707   }
708   return 0;
709 }
710 
711 /// getMachineOpValue - Return binary encoding of operand. If the machine
712 /// operand requires relocation, record the relocation and return zero.
713 unsigned MipsMCCodeEmitter::
714 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
715                   SmallVectorImpl<MCFixup> &Fixups,
716                   const MCSubtargetInfo &STI) const {
717   if (MO.isReg()) {
718     unsigned Reg = MO.getReg();
719     unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
720     return RegNo;
721   } else if (MO.isImm()) {
722     return static_cast<unsigned>(MO.getImm());
723   } else if (MO.isFPImm()) {
724     return static_cast<unsigned>(APFloat(MO.getFPImm())
725         .bitcastToAPInt().getHiBits(32).getLimitedValue());
726   }
727   // MO must be an Expr.
728   assert(MO.isExpr());
729   return getExprOpValue(MO.getExpr(),Fixups, STI);
730 }
731 
732 /// Return binary encoding of memory related operand.
733 /// If the offset operand requires relocation, record the relocation.
734 template <unsigned ShiftAmount>
735 unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
736                                            SmallVectorImpl<MCFixup> &Fixups,
737                                            const MCSubtargetInfo &STI) const {
738   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
739   assert(MI.getOperand(OpNo).isReg());
740   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
741   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
742 
743   // Apply the scale factor if there is one.
744   OffBits >>= ShiftAmount;
745 
746   return (OffBits & 0xFFFF) | RegBits;
747 }
748 
749 unsigned MipsMCCodeEmitter::
750 getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
751                      SmallVectorImpl<MCFixup> &Fixups,
752                      const MCSubtargetInfo &STI) const {
753   // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
754   assert(MI.getOperand(OpNo).isReg());
755   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
756                                        Fixups, STI) << 4;
757   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
758                                        Fixups, STI);
759 
760   return (OffBits & 0xF) | RegBits;
761 }
762 
763 unsigned MipsMCCodeEmitter::
764 getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
765                          SmallVectorImpl<MCFixup> &Fixups,
766                          const MCSubtargetInfo &STI) const {
767   // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
768   assert(MI.getOperand(OpNo).isReg());
769   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
770                                        Fixups, STI) << 4;
771   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
772                                        Fixups, STI) >> 1;
773 
774   return (OffBits & 0xF) | RegBits;
775 }
776 
777 unsigned MipsMCCodeEmitter::
778 getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
779                          SmallVectorImpl<MCFixup> &Fixups,
780                          const MCSubtargetInfo &STI) const {
781   // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
782   assert(MI.getOperand(OpNo).isReg());
783   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
784                                        Fixups, STI) << 4;
785   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
786                                        Fixups, STI) >> 2;
787 
788   return (OffBits & 0xF) | RegBits;
789 }
790 
791 unsigned MipsMCCodeEmitter::
792 getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
793                            SmallVectorImpl<MCFixup> &Fixups,
794                            const MCSubtargetInfo &STI) const {
795   // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
796   assert(MI.getOperand(OpNo).isReg() &&
797          (MI.getOperand(OpNo).getReg() == Mips::SP ||
798          MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
799          "Unexpected base register!");
800   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
801                                        Fixups, STI) >> 2;
802 
803   return OffBits & 0x1F;
804 }
805 
806 unsigned MipsMCCodeEmitter::
807 getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
808                            SmallVectorImpl<MCFixup> &Fixups,
809                            const MCSubtargetInfo &STI) const {
810   // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
811   assert(MI.getOperand(OpNo).isReg() &&
812          MI.getOperand(OpNo).getReg() == Mips::GP &&
813          "Unexpected base register!");
814 
815   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
816                                        Fixups, STI) >> 2;
817 
818   return OffBits & 0x7F;
819 }
820 
821 unsigned MipsMCCodeEmitter::
822 getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
823                      SmallVectorImpl<MCFixup> &Fixups,
824                      const MCSubtargetInfo &STI) const {
825   // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
826   assert(MI.getOperand(OpNo).isReg());
827   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
828                                        STI) << 16;
829   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
830 
831   return (OffBits & 0x1FF) | RegBits;
832 }
833 
834 unsigned MipsMCCodeEmitter::
835 getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
836                       SmallVectorImpl<MCFixup> &Fixups,
837                       const MCSubtargetInfo &STI) const {
838   // opNum can be invalid if instruction had reglist as operand.
839   // MemOperand is always last operand of instruction (base + offset).
840   switch (MI.getOpcode()) {
841   default:
842     break;
843   case Mips::SWM32_MM:
844   case Mips::LWM32_MM:
845     OpNo = MI.getNumOperands() - 2;
846     break;
847   }
848 
849   // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
850   assert(MI.getOperand(OpNo).isReg());
851   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
852   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
853 
854   return (OffBits & 0x0FFF) | RegBits;
855 }
856 
857 unsigned MipsMCCodeEmitter::
858 getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
859                       SmallVectorImpl<MCFixup> &Fixups,
860                       const MCSubtargetInfo &STI) const {
861   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
862   assert(MI.getOperand(OpNo).isReg());
863   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
864                                        STI) << 16;
865   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
866 
867   return (OffBits & 0xFFFF) | RegBits;
868 }
869 
870 unsigned MipsMCCodeEmitter::
871 getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
872                        SmallVectorImpl<MCFixup> &Fixups,
873                        const MCSubtargetInfo &STI) const {
874   // opNum can be invalid if instruction had reglist as operand
875   // MemOperand is always last operand of instruction (base + offset)
876   switch (MI.getOpcode()) {
877   default:
878     break;
879   case Mips::SWM16_MM:
880   case Mips::SWM16_MMR6:
881   case Mips::LWM16_MM:
882   case Mips::LWM16_MMR6:
883     OpNo = MI.getNumOperands() - 2;
884     break;
885   }
886 
887   // Offset is encoded in bits 4-0.
888   assert(MI.getOperand(OpNo).isReg());
889   // Base register is always SP - thus it is not encoded.
890   assert(MI.getOperand(OpNo+1).isImm());
891   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
892 
893   return ((OffBits >> 2) & 0x0F);
894 }
895 
896 // FIXME: should be called getMSBEncoding
897 //
898 unsigned
899 MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
900                                       SmallVectorImpl<MCFixup> &Fixups,
901                                       const MCSubtargetInfo &STI) const {
902   assert(MI.getOperand(OpNo-1).isImm());
903   assert(MI.getOperand(OpNo).isImm());
904   unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
905   unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
906 
907   return Position + Size - 1;
908 }
909 
910 template <unsigned Bits, int Offset>
911 unsigned
912 MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
913                                              SmallVectorImpl<MCFixup> &Fixups,
914                                              const MCSubtargetInfo &STI) const {
915   assert(MI.getOperand(OpNo).isImm());
916   unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
917   Value -= Offset;
918   return Value;
919 }
920 
921 unsigned
922 MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
923                                          SmallVectorImpl<MCFixup> &Fixups,
924                                          const MCSubtargetInfo &STI) const {
925   const MCOperand &MO = MI.getOperand(OpNo);
926   if (MO.isImm()) {
927     // The immediate is encoded as 'immediate << 2'.
928     unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
929     assert((Res & 3) == 0);
930     return Res >> 2;
931   }
932 
933   assert(MO.isExpr() &&
934          "getSimm19Lsl2Encoding expects only expressions or an immediate");
935 
936   const MCExpr *Expr = MO.getExpr();
937   Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
938                                             : Mips::fixup_MIPS_PC19_S2;
939   Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
940   return 0;
941 }
942 
943 unsigned
944 MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
945                                          SmallVectorImpl<MCFixup> &Fixups,
946                                          const MCSubtargetInfo &STI) const {
947   const MCOperand &MO = MI.getOperand(OpNo);
948   if (MO.isImm()) {
949     // The immediate is encoded as 'immediate << 3'.
950     unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
951     assert((Res & 7) == 0);
952     return Res >> 3;
953   }
954 
955   assert(MO.isExpr() &&
956          "getSimm18Lsl2Encoding expects only expressions or an immediate");
957 
958   const MCExpr *Expr = MO.getExpr();
959   Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
960                                             : Mips::fixup_MIPS_PC18_S3;
961   Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
962   return 0;
963 }
964 
965 unsigned
966 MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
967                                         SmallVectorImpl<MCFixup> &Fixups,
968                                         const MCSubtargetInfo &STI) const {
969   assert(MI.getOperand(OpNo).isImm());
970   const MCOperand &MO = MI.getOperand(OpNo);
971   return MO.getImm() % 8;
972 }
973 
974 unsigned
975 MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
976                                     SmallVectorImpl<MCFixup> &Fixups,
977                                     const MCSubtargetInfo &STI) const {
978   assert(MI.getOperand(OpNo).isImm());
979   const MCOperand &MO = MI.getOperand(OpNo);
980   unsigned Value = MO.getImm();
981   switch (Value) {
982     case 128:   return 0x0;
983     case 1:     return 0x1;
984     case 2:     return 0x2;
985     case 3:     return 0x3;
986     case 4:     return 0x4;
987     case 7:     return 0x5;
988     case 8:     return 0x6;
989     case 15:    return 0x7;
990     case 16:    return 0x8;
991     case 31:    return 0x9;
992     case 32:    return 0xa;
993     case 63:    return 0xb;
994     case 64:    return 0xc;
995     case 255:   return 0xd;
996     case 32768: return 0xe;
997     case 65535: return 0xf;
998   }
999   llvm_unreachable("Unexpected value");
1000 }
1001 
1002 unsigned
1003 MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
1004                                           SmallVectorImpl<MCFixup> &Fixups,
1005                                           const MCSubtargetInfo &STI) const {
1006   unsigned res = 0;
1007 
1008   // Register list operand is always first operand of instruction and it is
1009   // placed before memory operand (register + imm).
1010 
1011   for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
1012     unsigned Reg = MI.getOperand(I).getReg();
1013     unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
1014     if (RegNo != 31)
1015       res++;
1016     else
1017       res |= 0x10;
1018   }
1019   return res;
1020 }
1021 
1022 unsigned
1023 MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
1024                                             SmallVectorImpl<MCFixup> &Fixups,
1025                                             const MCSubtargetInfo &STI) const {
1026   return (MI.getNumOperands() - 4);
1027 }
1028 
1029 unsigned
1030 MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
1031                                           SmallVectorImpl<MCFixup> &Fixups,
1032                                           const MCSubtargetInfo &STI) const {
1033   return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1034 }
1035 
1036 unsigned
1037 MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1038                                           SmallVectorImpl<MCFixup> &Fixups,
1039                                           const MCSubtargetInfo &STI) const {
1040   unsigned res = 0;
1041 
1042   if (MI.getOperand(0).getReg() == Mips::A1 &&
1043       MI.getOperand(1).getReg() == Mips::A2)
1044     res = 0;
1045   else if (MI.getOperand(0).getReg() == Mips::A1 &&
1046            MI.getOperand(1).getReg() == Mips::A3)
1047     res = 1;
1048   else if (MI.getOperand(0).getReg() == Mips::A2 &&
1049            MI.getOperand(1).getReg() == Mips::A3)
1050     res = 2;
1051   else if (MI.getOperand(0).getReg() == Mips::A0 &&
1052            MI.getOperand(1).getReg() == Mips::S5)
1053     res = 3;
1054   else if (MI.getOperand(0).getReg() == Mips::A0 &&
1055            MI.getOperand(1).getReg() == Mips::S6)
1056     res = 4;
1057   else if (MI.getOperand(0).getReg() == Mips::A0 &&
1058            MI.getOperand(1).getReg() == Mips::A1)
1059     res = 5;
1060   else if (MI.getOperand(0).getReg() == Mips::A0 &&
1061            MI.getOperand(1).getReg() == Mips::A2)
1062     res = 6;
1063   else if (MI.getOperand(0).getReg() == Mips::A0 &&
1064            MI.getOperand(1).getReg() == Mips::A3)
1065     res = 7;
1066 
1067   return res;
1068 }
1069 
1070 unsigned
1071 MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1072                                          SmallVectorImpl<MCFixup> &Fixups,
1073                                          const MCSubtargetInfo &STI) const {
1074   const MCOperand &MO = MI.getOperand(OpNo);
1075   assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1076   // The immediate is encoded as 'immediate >> 2'.
1077   unsigned Res = static_cast<unsigned>(MO.getImm());
1078   assert((Res & 3) == 0);
1079   return Res >> 2;
1080 }
1081 
1082 #include "MipsGenMCCodeEmitter.inc"
1083