19f610119SJia Liu //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
2750ecec7SAkira Hatanaka //
3750ecec7SAkira Hatanaka //                     The LLVM Compiler Infrastructure
4750ecec7SAkira Hatanaka //
5750ecec7SAkira Hatanaka // This file is distributed under the University of Illinois Open Source
6750ecec7SAkira Hatanaka // License. See LICENSE.TXT for details.
7750ecec7SAkira Hatanaka //
8750ecec7SAkira Hatanaka //===----------------------------------------------------------------------===//
9750ecec7SAkira Hatanaka //
10750ecec7SAkira Hatanaka // This file implements the MipsMCCodeEmitter class.
11750ecec7SAkira Hatanaka //
12750ecec7SAkira Hatanaka //===----------------------------------------------------------------------===//
13750ecec7SAkira Hatanaka //
149e1450bcSMatheus Almeida 
159e1450bcSMatheus Almeida #include "MipsMCCodeEmitter.h"
16c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsFixupKinds.h"
17a5da588bSPetar Jovanovic #include "MCTargetDesc/MipsMCExpr.h"
18c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsMCTargetDesc.h"
19c85e3ff3SBruno Cardoso Lopes #include "llvm/ADT/APFloat.h"
209e1450bcSMatheus Almeida #include "llvm/ADT/SmallVector.h"
215d6faed1SAkira Hatanaka #include "llvm/MC/MCContext.h"
22750ecec7SAkira Hatanaka #include "llvm/MC/MCExpr.h"
23750ecec7SAkira Hatanaka #include "llvm/MC/MCInst.h"
24750ecec7SAkira Hatanaka #include "llvm/MC/MCInstrInfo.h"
259e1450bcSMatheus Almeida #include "llvm/MC/MCFixup.h"
26750ecec7SAkira Hatanaka #include "llvm/MC/MCSubtargetInfo.h"
27750ecec7SAkira Hatanaka #include "llvm/Support/raw_ostream.h"
28750ecec7SAkira Hatanaka 
2984e68b29SChandler Carruth #define DEBUG_TYPE "mccodeemitter"
3084e68b29SChandler Carruth 
31be6a818fSAkira Hatanaka #define GET_INSTRMAP_INFO
32be6a818fSAkira Hatanaka #include "MipsGenInstrInfo.inc"
339e1450bcSMatheus Almeida #undef GET_INSTRMAP_INFO
34be6a818fSAkira Hatanaka 
359e1450bcSMatheus Almeida namespace llvm {
369e1450bcSMatheus Almeida MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
37c3b04279SJim Grosbach                                          const MCRegisterInfo &MRI,
38750ecec7SAkira Hatanaka                                          const MCSubtargetInfo &STI,
399e1450bcSMatheus Almeida                                          MCContext &Ctx) {
40d2cca113SDavid Woodhouse   return new MipsMCCodeEmitter(MCII, Ctx, false);
411ee768dbSAkira Hatanaka }
421ee768dbSAkira Hatanaka 
439e1450bcSMatheus Almeida MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
44c3b04279SJim Grosbach                                          const MCRegisterInfo &MRI,
451ee768dbSAkira Hatanaka                                          const MCSubtargetInfo &STI,
469e1450bcSMatheus Almeida                                          MCContext &Ctx) {
47d2cca113SDavid Woodhouse   return new MipsMCCodeEmitter(MCII, Ctx, true);
48750ecec7SAkira Hatanaka }
499e1450bcSMatheus Almeida } // End of namespace llvm.
50f30f2cceSRafael Espindola 
51f30f2cceSRafael Espindola // If the D<shift> instruction has a shift amount that is greater
52f30f2cceSRafael Espindola // than 31 (checked in calling routine), lower it to a D<shift>32 instruction
53f30f2cceSRafael Espindola static void LowerLargeShift(MCInst& Inst) {
54f30f2cceSRafael Espindola 
55f30f2cceSRafael Espindola   assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
56f30f2cceSRafael Espindola   assert(Inst.getOperand(2).isImm());
57f30f2cceSRafael Espindola 
58f30f2cceSRafael Espindola   int64_t Shift = Inst.getOperand(2).getImm();
59f30f2cceSRafael Espindola   if (Shift <= 31)
60f30f2cceSRafael Espindola     return; // Do nothing
61f30f2cceSRafael Espindola   Shift -= 32;
62f30f2cceSRafael Espindola 
63f30f2cceSRafael Espindola   // saminus32
64f30f2cceSRafael Espindola   Inst.getOperand(2).setImm(Shift);
65f30f2cceSRafael Espindola 
66f30f2cceSRafael Espindola   switch (Inst.getOpcode()) {
67f30f2cceSRafael Espindola   default:
68f30f2cceSRafael Espindola     // Calling function is not synchronized
69f30f2cceSRafael Espindola     llvm_unreachable("Unexpected shift instruction");
70f30f2cceSRafael Espindola   case Mips::DSLL:
71f30f2cceSRafael Espindola     Inst.setOpcode(Mips::DSLL32);
72f30f2cceSRafael Espindola     return;
73f30f2cceSRafael Espindola   case Mips::DSRL:
74f30f2cceSRafael Espindola     Inst.setOpcode(Mips::DSRL32);
75f30f2cceSRafael Espindola     return;
76f30f2cceSRafael Espindola   case Mips::DSRA:
77f30f2cceSRafael Espindola     Inst.setOpcode(Mips::DSRA32);
78f30f2cceSRafael Espindola     return;
796a3fe574SAkira Hatanaka   case Mips::DROTR:
806a3fe574SAkira Hatanaka     Inst.setOpcode(Mips::DROTR32);
816a3fe574SAkira Hatanaka     return;
82f30f2cceSRafael Espindola   }
83f30f2cceSRafael Espindola }
84f30f2cceSRafael Espindola 
85f30f2cceSRafael Espindola // Pick a DEXT or DINS instruction variant based on the pos and size operands
86f30f2cceSRafael Espindola static void LowerDextDins(MCInst& InstIn) {
87f30f2cceSRafael Espindola   int Opcode = InstIn.getOpcode();
88f30f2cceSRafael Espindola 
89f30f2cceSRafael Espindola   if (Opcode == Mips::DEXT)
90f30f2cceSRafael Espindola     assert(InstIn.getNumOperands() == 4 &&
91f30f2cceSRafael Espindola            "Invalid no. of machine operands for DEXT!");
92f30f2cceSRafael Espindola   else // Only DEXT and DINS are possible
93f30f2cceSRafael Espindola     assert(InstIn.getNumOperands() == 5 &&
94f30f2cceSRafael Espindola            "Invalid no. of machine operands for DINS!");
95f30f2cceSRafael Espindola 
96f30f2cceSRafael Espindola   assert(InstIn.getOperand(2).isImm());
97f30f2cceSRafael Espindola   int64_t pos = InstIn.getOperand(2).getImm();
98f30f2cceSRafael Espindola   assert(InstIn.getOperand(3).isImm());
99f30f2cceSRafael Espindola   int64_t size = InstIn.getOperand(3).getImm();
100f30f2cceSRafael Espindola 
101f30f2cceSRafael Espindola   if (size <= 32) {
102f30f2cceSRafael Espindola     if (pos < 32)  // DEXT/DINS, do nothing
103f30f2cceSRafael Espindola       return;
104f30f2cceSRafael Espindola     // DEXTU/DINSU
105f30f2cceSRafael Espindola     InstIn.getOperand(2).setImm(pos - 32);
106f30f2cceSRafael Espindola     InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
107f30f2cceSRafael Espindola     return;
108f30f2cceSRafael Espindola   }
109f30f2cceSRafael Espindola   // DEXTM/DINSM
110f30f2cceSRafael Espindola   assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
111f30f2cceSRafael Espindola   InstIn.getOperand(3).setImm(size - 32);
112f30f2cceSRafael Espindola   InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
113f30f2cceSRafael Espindola   return;
114f30f2cceSRafael Espindola }
115f30f2cceSRafael Espindola 
1169e1450bcSMatheus Almeida bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
1179e1450bcSMatheus Almeida   return STI.getFeatureBits() & Mips::FeatureMicroMips;
1189e1450bcSMatheus Almeida }
1199e1450bcSMatheus Almeida 
1209e1450bcSMatheus Almeida void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
1219e1450bcSMatheus Almeida   OS << (char)C;
1229e1450bcSMatheus Almeida }
1239e1450bcSMatheus Almeida 
1249e1450bcSMatheus Almeida void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
1259e1450bcSMatheus Almeida                                         const MCSubtargetInfo &STI,
1269e1450bcSMatheus Almeida                                         raw_ostream &OS) const {
1279e1450bcSMatheus Almeida   // Output the instruction encoding in little endian byte order.
1289e1450bcSMatheus Almeida   // Little-endian byte ordering:
1299e1450bcSMatheus Almeida   //   mips32r2:   4 | 3 | 2 | 1
1309e1450bcSMatheus Almeida   //   microMIPS:  2 | 1 | 4 | 3
1319e1450bcSMatheus Almeida   if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
1329e1450bcSMatheus Almeida     EmitInstruction(Val >> 16, 2, STI, OS);
1339e1450bcSMatheus Almeida     EmitInstruction(Val, 2, STI, OS);
1349e1450bcSMatheus Almeida   } else {
1359e1450bcSMatheus Almeida     for (unsigned i = 0; i < Size; ++i) {
1369e1450bcSMatheus Almeida       unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
1379e1450bcSMatheus Almeida       EmitByte((Val >> Shift) & 0xff, OS);
1389e1450bcSMatheus Almeida     }
1399e1450bcSMatheus Almeida   }
1409e1450bcSMatheus Almeida }
1419e1450bcSMatheus Almeida 
142c85e3ff3SBruno Cardoso Lopes /// EncodeInstruction - Emit the instruction.
1434e07b95dSJack Carter /// Size the instruction with Desc.getSize().
144c85e3ff3SBruno Cardoso Lopes void MipsMCCodeEmitter::
145c85e3ff3SBruno Cardoso Lopes EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1469784cef3SDavid Woodhouse                   SmallVectorImpl<MCFixup> &Fixups,
1479784cef3SDavid Woodhouse                   const MCSubtargetInfo &STI) const
148c85e3ff3SBruno Cardoso Lopes {
149aa7aeaa0SJack Carter 
150aa7aeaa0SJack Carter   // Non-pseudo instructions that get changed for direct object
151aa7aeaa0SJack Carter   // only based on operand values.
152aa7aeaa0SJack Carter   // If this list of instructions get much longer we will move
153aa7aeaa0SJack Carter   // the check to a function call. Until then, this is more efficient.
154aa7aeaa0SJack Carter   MCInst TmpInst = MI;
155aa7aeaa0SJack Carter   switch (MI.getOpcode()) {
156aa7aeaa0SJack Carter   // If shift amount is >= 32 it the inst needs to be lowered further
157aa7aeaa0SJack Carter   case Mips::DSLL:
158aa7aeaa0SJack Carter   case Mips::DSRL:
159aa7aeaa0SJack Carter   case Mips::DSRA:
1606a3fe574SAkira Hatanaka   case Mips::DROTR:
161f30f2cceSRafael Espindola     LowerLargeShift(TmpInst);
162aa7aeaa0SJack Carter     break;
163aa7aeaa0SJack Carter     // Double extract instruction is chosen by pos and size operands
164aa7aeaa0SJack Carter   case Mips::DEXT:
165aa7aeaa0SJack Carter   case Mips::DINS:
166f30f2cceSRafael Espindola     LowerDextDins(TmpInst);
167aa7aeaa0SJack Carter   }
168aa7aeaa0SJack Carter 
16997700977SJack Carter   unsigned long N = Fixups.size();
1703fa98a65SDavid Woodhouse   uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
171c85e3ff3SBruno Cardoso Lopes 
172c85e3ff3SBruno Cardoso Lopes   // Check for unimplemented opcodes.
173aa7aeaa0SJack Carter   // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
174c85e3ff3SBruno Cardoso Lopes   // so we have to special check for them.
175aa7aeaa0SJack Carter   unsigned Opcode = TmpInst.getOpcode();
176c85e3ff3SBruno Cardoso Lopes   if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
177c85e3ff3SBruno Cardoso Lopes     llvm_unreachable("unimplemented opcode in EncodeInstruction()");
178c85e3ff3SBruno Cardoso Lopes 
179be6a818fSAkira Hatanaka   if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
180be6a818fSAkira Hatanaka     int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
181be6a818fSAkira Hatanaka     if (NewOpcode != -1) {
18297700977SJack Carter       if (Fixups.size() > N)
18397700977SJack Carter         Fixups.pop_back();
184be6a818fSAkira Hatanaka       Opcode = NewOpcode;
185be6a818fSAkira Hatanaka       TmpInst.setOpcode (NewOpcode);
1863fa98a65SDavid Woodhouse       Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
187be6a818fSAkira Hatanaka     }
188be6a818fSAkira Hatanaka   }
189be6a818fSAkira Hatanaka 
190aa7aeaa0SJack Carter   const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
191c85e3ff3SBruno Cardoso Lopes 
1925b5559d3SJack Carter   // Get byte count of instruction
1935b5559d3SJack Carter   unsigned Size = Desc.getSize();
1945b5559d3SJack Carter   if (!Size)
1955b5559d3SJack Carter     llvm_unreachable("Desc.getSize() returns 0");
196c85e3ff3SBruno Cardoso Lopes 
197d2cca113SDavid Woodhouse   EmitInstruction(Binary, Size, STI, OS);
198c85e3ff3SBruno Cardoso Lopes }
199c85e3ff3SBruno Cardoso Lopes 
200c85e3ff3SBruno Cardoso Lopes /// getBranchTargetOpValue - Return binary encoding of the branch
201c85e3ff3SBruno Cardoso Lopes /// target operand. If the machine operand requires relocation,
202c85e3ff3SBruno Cardoso Lopes /// record the relocation and return zero.
203c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
204c85e3ff3SBruno Cardoso Lopes getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
2053fa98a65SDavid Woodhouse                        SmallVectorImpl<MCFixup> &Fixups,
2063fa98a65SDavid Woodhouse                        const MCSubtargetInfo &STI) const {
207c85e3ff3SBruno Cardoso Lopes 
208c85e3ff3SBruno Cardoso Lopes   const MCOperand &MO = MI.getOperand(OpNo);
20971e6a749SJack Carter 
2104f69a0f2SJack Carter   // If the destination is an immediate, divide by 4.
2114f69a0f2SJack Carter   if (MO.isImm()) return MO.getImm() >> 2;
2124f69a0f2SJack Carter 
21371e6a749SJack Carter   assert(MO.isExpr() &&
21471e6a749SJack Carter          "getBranchTargetOpValue expects only expressions or immediates");
215c85e3ff3SBruno Cardoso Lopes 
216c85e3ff3SBruno Cardoso Lopes   const MCExpr *Expr = MO.getExpr();
217c85e3ff3SBruno Cardoso Lopes   Fixups.push_back(MCFixup::Create(0, Expr,
218c85e3ff3SBruno Cardoso Lopes                                    MCFixupKind(Mips::fixup_Mips_PC16)));
219c85e3ff3SBruno Cardoso Lopes   return 0;
220c85e3ff3SBruno Cardoso Lopes }
221c85e3ff3SBruno Cardoso Lopes 
2228a80aa76SZoran Jovanovic /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
2238a80aa76SZoran Jovanovic /// target operand. If the machine operand requires relocation,
2248a80aa76SZoran Jovanovic /// record the relocation and return zero.
2258a80aa76SZoran Jovanovic unsigned MipsMCCodeEmitter::
2268a80aa76SZoran Jovanovic getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
2273fa98a65SDavid Woodhouse                          SmallVectorImpl<MCFixup> &Fixups,
2283fa98a65SDavid Woodhouse                          const MCSubtargetInfo &STI) const {
2298a80aa76SZoran Jovanovic 
2308a80aa76SZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
2318a80aa76SZoran Jovanovic 
2328a80aa76SZoran Jovanovic   // If the destination is an immediate, divide by 2.
2338a80aa76SZoran Jovanovic   if (MO.isImm()) return MO.getImm() >> 1;
2348a80aa76SZoran Jovanovic 
2358a80aa76SZoran Jovanovic   assert(MO.isExpr() &&
2368a80aa76SZoran Jovanovic          "getBranchTargetOpValueMM expects only expressions or immediates");
2378a80aa76SZoran Jovanovic 
2388a80aa76SZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
2398a80aa76SZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
2408a80aa76SZoran Jovanovic                    MCFixupKind(Mips::
2418a80aa76SZoran Jovanovic                                fixup_MICROMIPS_PC16_S1)));
2428a80aa76SZoran Jovanovic   return 0;
2438a80aa76SZoran Jovanovic }
2448a80aa76SZoran Jovanovic 
2453c8869dcSZoran Jovanovic /// getBranchTarget21OpValue - Return binary encoding of the branch
2463c8869dcSZoran Jovanovic /// target operand. If the machine operand requires relocation,
2473c8869dcSZoran Jovanovic /// record the relocation and return zero.
2483c8869dcSZoran Jovanovic unsigned MipsMCCodeEmitter::
2493c8869dcSZoran Jovanovic getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
2503c8869dcSZoran Jovanovic                          SmallVectorImpl<MCFixup> &Fixups,
2513c8869dcSZoran Jovanovic                          const MCSubtargetInfo &STI) const {
2523c8869dcSZoran Jovanovic 
2533c8869dcSZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
2543c8869dcSZoran Jovanovic 
2553c8869dcSZoran Jovanovic   // If the destination is an immediate, divide by 4.
2563c8869dcSZoran Jovanovic   if (MO.isImm()) return MO.getImm() >> 2;
2573c8869dcSZoran Jovanovic 
2583c8869dcSZoran Jovanovic   assert(MO.isExpr() &&
2593c8869dcSZoran Jovanovic          "getBranchTarget21OpValue expects only expressions or immediates");
2603c8869dcSZoran Jovanovic 
26110e06da0SZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
26210e06da0SZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
26310e06da0SZoran Jovanovic                                    MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
2643c8869dcSZoran Jovanovic   return 0;
2653c8869dcSZoran Jovanovic }
2663c8869dcSZoran Jovanovic 
2673c8869dcSZoran Jovanovic /// getBranchTarget26OpValue - Return binary encoding of the branch
2683c8869dcSZoran Jovanovic /// target operand. If the machine operand requires relocation,
2693c8869dcSZoran Jovanovic /// record the relocation and return zero.
2703c8869dcSZoran Jovanovic unsigned MipsMCCodeEmitter::
2713c8869dcSZoran Jovanovic getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
2723c8869dcSZoran Jovanovic                          SmallVectorImpl<MCFixup> &Fixups,
2733c8869dcSZoran Jovanovic                          const MCSubtargetInfo &STI) const {
2743c8869dcSZoran Jovanovic 
2753c8869dcSZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
2763c8869dcSZoran Jovanovic 
2773c8869dcSZoran Jovanovic   // If the destination is an immediate, divide by 4.
2783c8869dcSZoran Jovanovic   if (MO.isImm()) return MO.getImm() >> 2;
2793c8869dcSZoran Jovanovic 
2803c8869dcSZoran Jovanovic   assert(MO.isExpr() &&
2813c8869dcSZoran Jovanovic          "getBranchTarget26OpValue expects only expressions or immediates");
2823c8869dcSZoran Jovanovic 
28310e06da0SZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
28410e06da0SZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
28510e06da0SZoran Jovanovic                                    MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
2863c8869dcSZoran Jovanovic   return 0;
2873c8869dcSZoran Jovanovic }
2883c8869dcSZoran Jovanovic 
28952c56b93SZoran Jovanovic /// getJumpOffset16OpValue - Return binary encoding of the jump
29052c56b93SZoran Jovanovic /// target operand. If the machine operand requires relocation,
29152c56b93SZoran Jovanovic /// record the relocation and return zero.
29252c56b93SZoran Jovanovic unsigned MipsMCCodeEmitter::
29352c56b93SZoran Jovanovic getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
29452c56b93SZoran Jovanovic                        SmallVectorImpl<MCFixup> &Fixups,
29552c56b93SZoran Jovanovic                        const MCSubtargetInfo &STI) const {
29652c56b93SZoran Jovanovic 
29752c56b93SZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
29852c56b93SZoran Jovanovic 
29952c56b93SZoran Jovanovic   if (MO.isImm()) return MO.getImm();
30052c56b93SZoran Jovanovic 
30152c56b93SZoran Jovanovic   assert(MO.isExpr() &&
30252c56b93SZoran Jovanovic          "getJumpOffset16OpValue expects only expressions or an immediate");
30352c56b93SZoran Jovanovic 
30452c56b93SZoran Jovanovic    // TODO: Push fixup.
30552c56b93SZoran Jovanovic    return 0;
30652c56b93SZoran Jovanovic }
30752c56b93SZoran Jovanovic 
308c85e3ff3SBruno Cardoso Lopes /// getJumpTargetOpValue - Return binary encoding of the jump
309c85e3ff3SBruno Cardoso Lopes /// target operand. If the machine operand requires relocation,
310c85e3ff3SBruno Cardoso Lopes /// record the relocation and return zero.
311c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
312c85e3ff3SBruno Cardoso Lopes getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
3133fa98a65SDavid Woodhouse                      SmallVectorImpl<MCFixup> &Fixups,
3143fa98a65SDavid Woodhouse                      const MCSubtargetInfo &STI) const {
315c85e3ff3SBruno Cardoso Lopes 
316c85e3ff3SBruno Cardoso Lopes   const MCOperand &MO = MI.getOperand(OpNo);
3174f69a0f2SJack Carter   // If the destination is an immediate, divide by 4.
3184f69a0f2SJack Carter   if (MO.isImm()) return MO.getImm()>>2;
3194f69a0f2SJack Carter 
32071e6a749SJack Carter   assert(MO.isExpr() &&
32171e6a749SJack Carter          "getJumpTargetOpValue expects only expressions or an immediate");
322c85e3ff3SBruno Cardoso Lopes 
323c85e3ff3SBruno Cardoso Lopes   const MCExpr *Expr = MO.getExpr();
324c85e3ff3SBruno Cardoso Lopes   Fixups.push_back(MCFixup::Create(0, Expr,
325c85e3ff3SBruno Cardoso Lopes                                    MCFixupKind(Mips::fixup_Mips_26)));
326c85e3ff3SBruno Cardoso Lopes   return 0;
327c85e3ff3SBruno Cardoso Lopes }
328c85e3ff3SBruno Cardoso Lopes 
329c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
330507e084aSZoran Jovanovic getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
3313fa98a65SDavid Woodhouse                        SmallVectorImpl<MCFixup> &Fixups,
3323fa98a65SDavid Woodhouse                        const MCSubtargetInfo &STI) const {
333507e084aSZoran Jovanovic 
334507e084aSZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
335507e084aSZoran Jovanovic   // If the destination is an immediate, divide by 2.
336507e084aSZoran Jovanovic   if (MO.isImm()) return MO.getImm() >> 1;
337507e084aSZoran Jovanovic 
338507e084aSZoran Jovanovic   assert(MO.isExpr() &&
339507e084aSZoran Jovanovic          "getJumpTargetOpValueMM expects only expressions or an immediate");
340507e084aSZoran Jovanovic 
341507e084aSZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
342507e084aSZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
343507e084aSZoran Jovanovic                                    MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
344507e084aSZoran Jovanovic   return 0;
345507e084aSZoran Jovanovic }
346507e084aSZoran Jovanovic 
347507e084aSZoran Jovanovic unsigned MipsMCCodeEmitter::
348*c74e3eb9SZoran Jovanovic getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
349*c74e3eb9SZoran Jovanovic                      SmallVectorImpl<MCFixup> &Fixups,
350*c74e3eb9SZoran Jovanovic                      const MCSubtargetInfo &STI) const {
351*c74e3eb9SZoran Jovanovic 
352*c74e3eb9SZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
353*c74e3eb9SZoran Jovanovic   if (MO.isImm()) {
354*c74e3eb9SZoran Jovanovic     // The immediate is encoded as 'immediate << 2'.
355*c74e3eb9SZoran Jovanovic     unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
356*c74e3eb9SZoran Jovanovic     assert((Res & 3) == 0);
357*c74e3eb9SZoran Jovanovic     return Res >> 2;
358*c74e3eb9SZoran Jovanovic   }
359*c74e3eb9SZoran Jovanovic 
360*c74e3eb9SZoran Jovanovic   assert(MO.isExpr() &&
361*c74e3eb9SZoran Jovanovic          "getUImm5Lsl2Encoding expects only expressions or an immediate");
362*c74e3eb9SZoran Jovanovic 
363*c74e3eb9SZoran Jovanovic   return 0;
364*c74e3eb9SZoran Jovanovic }
365*c74e3eb9SZoran Jovanovic 
366*c74e3eb9SZoran Jovanovic unsigned MipsMCCodeEmitter::
3673fa98a65SDavid Woodhouse getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
3683fa98a65SDavid Woodhouse                const MCSubtargetInfo &STI) const {
369b5cf5909SJack Carter   int64_t Res;
370049e9e4dSAkira Hatanaka 
371b5cf5909SJack Carter   if (Expr->EvaluateAsAbsolute(Res))
372b5cf5909SJack Carter     return Res;
373fe384a2cSAkira Hatanaka 
374fe384a2cSAkira Hatanaka   MCExpr::ExprKind Kind = Expr->getKind();
375b5cf5909SJack Carter   if (Kind == MCExpr::Constant) {
376b5cf5909SJack Carter     return cast<MCConstantExpr>(Expr)->getValue();
377b5cf5909SJack Carter   }
378fe384a2cSAkira Hatanaka 
379fe384a2cSAkira Hatanaka   if (Kind == MCExpr::Binary) {
3803fa98a65SDavid Woodhouse     unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
3813fa98a65SDavid Woodhouse     Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
382b5cf5909SJack Carter     return Res;
383fe384a2cSAkira Hatanaka   }
384a5da588bSPetar Jovanovic 
385a5da588bSPetar Jovanovic   if (Kind == MCExpr::Target) {
386a5da588bSPetar Jovanovic     const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
387a5da588bSPetar Jovanovic 
388a5da588bSPetar Jovanovic     Mips::Fixups FixupKind = Mips::Fixups(0);
389a5da588bSPetar Jovanovic     switch (MipsExpr->getKind()) {
390a5da588bSPetar Jovanovic     default: llvm_unreachable("Unsupported fixup kind for target expression!");
39106c47803SSasa Stankovic     case MipsMCExpr::VK_Mips_HIGHEST:
39206c47803SSasa Stankovic       FixupKind = Mips::fixup_Mips_HIGHEST;
39306c47803SSasa Stankovic       break;
39406c47803SSasa Stankovic     case MipsMCExpr::VK_Mips_HIGHER:
39506c47803SSasa Stankovic       FixupKind = Mips::fixup_Mips_HIGHER;
39606c47803SSasa Stankovic       break;
39706c47803SSasa Stankovic     case MipsMCExpr::VK_Mips_HI:
398a5da588bSPetar Jovanovic       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
399a5da588bSPetar Jovanovic                                    : Mips::fixup_Mips_HI16;
400a5da588bSPetar Jovanovic       break;
40106c47803SSasa Stankovic     case MipsMCExpr::VK_Mips_LO:
402a5da588bSPetar Jovanovic       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
403a5da588bSPetar Jovanovic                                    : Mips::fixup_Mips_LO16;
404a5da588bSPetar Jovanovic       break;
405a5da588bSPetar Jovanovic     }
406a5da588bSPetar Jovanovic     Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
407a5da588bSPetar Jovanovic     return 0;
408a5da588bSPetar Jovanovic   }
409a5da588bSPetar Jovanovic 
410b5cf5909SJack Carter   if (Kind == MCExpr::SymbolRef) {
411f9774c32SBill Wendling     Mips::Fixups FixupKind = Mips::Fixups(0);
412e2eed964SAkira Hatanaka 
413049e9e4dSAkira Hatanaka     switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
414b9f9de93SJack Carter     default: llvm_unreachable("Unknown fixup kind!");
415b9f9de93SJack Carter       break;
416b9f9de93SJack Carter     case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
417b9f9de93SJack Carter       FixupKind = Mips::fixup_Mips_GPOFF_HI;
418b9f9de93SJack Carter       break;
419b9f9de93SJack Carter     case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
420b9f9de93SJack Carter       FixupKind = Mips::fixup_Mips_GPOFF_LO;
421b9f9de93SJack Carter       break;
422b9f9de93SJack Carter     case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
423d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
424e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_GOT_PAGE;
425b9f9de93SJack Carter       break;
426b9f9de93SJack Carter     case MCSymbolRefExpr::VK_Mips_GOT_OFST :
427d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
428e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_GOT_OFST;
429b9f9de93SJack Carter       break;
4305ddcfda8SJack Carter     case MCSymbolRefExpr::VK_Mips_GOT_DISP :
431d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
432e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_GOT_DISP;
4335ddcfda8SJack Carter       break;
434c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_GPREL:
435c85e3ff3SBruno Cardoso Lopes       FixupKind = Mips::fixup_Mips_GPREL16;
436c85e3ff3SBruno Cardoso Lopes       break;
437c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_GOT_CALL:
438d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
439e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_CALL16;
440c85e3ff3SBruno Cardoso Lopes       break;
44161e6d987SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_GOT16:
442d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
443e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_GOT_Global;
44461e6d987SBruno Cardoso Lopes       break;
445c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_GOT:
446d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
447e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_GOT_Local;
448c85e3ff3SBruno Cardoso Lopes       break;
449c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_ABS_HI:
450d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
451e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_HI16;
452c85e3ff3SBruno Cardoso Lopes       break;
453c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_ABS_LO:
454d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
455e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_LO16;
456c85e3ff3SBruno Cardoso Lopes       break;
457c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_TLSGD:
458d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
45969be811aSZoran Jovanovic                               : Mips::fixup_Mips_TLSGD;
460c85e3ff3SBruno Cardoso Lopes       break;
461e2eed964SAkira Hatanaka     case MCSymbolRefExpr::VK_Mips_TLSLDM:
462d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
46369be811aSZoran Jovanovic                               : Mips::fixup_Mips_TLSLDM;
464e2eed964SAkira Hatanaka       break;
465e2eed964SAkira Hatanaka     case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
466d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
467e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_DTPREL_HI;
468e2eed964SAkira Hatanaka       break;
469e2eed964SAkira Hatanaka     case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
470d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
471e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_DTPREL_LO;
472e2eed964SAkira Hatanaka       break;
473c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_GOTTPREL:
474c85e3ff3SBruno Cardoso Lopes       FixupKind = Mips::fixup_Mips_GOTTPREL;
475c85e3ff3SBruno Cardoso Lopes       break;
476c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_TPREL_HI:
477d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
478e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_TPREL_HI;
479c85e3ff3SBruno Cardoso Lopes       break;
480c85e3ff3SBruno Cardoso Lopes     case MCSymbolRefExpr::VK_Mips_TPREL_LO:
481d2cca113SDavid Woodhouse       FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
482e7ae8af8SZoran Jovanovic                               : Mips::fixup_Mips_TPREL_LO;
483c85e3ff3SBruno Cardoso Lopes       break;
48484491abbSJack Carter     case MCSymbolRefExpr::VK_Mips_HIGHER:
48584491abbSJack Carter       FixupKind = Mips::fixup_Mips_HIGHER;
48684491abbSJack Carter       break;
48784491abbSJack Carter     case MCSymbolRefExpr::VK_Mips_HIGHEST:
48884491abbSJack Carter       FixupKind = Mips::fixup_Mips_HIGHEST;
48984491abbSJack Carter       break;
490b05cb67bSJack Carter     case MCSymbolRefExpr::VK_Mips_GOT_HI16:
491b05cb67bSJack Carter       FixupKind = Mips::fixup_Mips_GOT_HI16;
492b05cb67bSJack Carter       break;
493b05cb67bSJack Carter     case MCSymbolRefExpr::VK_Mips_GOT_LO16:
494b05cb67bSJack Carter       FixupKind = Mips::fixup_Mips_GOT_LO16;
495b05cb67bSJack Carter       break;
496b05cb67bSJack Carter     case MCSymbolRefExpr::VK_Mips_CALL_HI16:
497b05cb67bSJack Carter       FixupKind = Mips::fixup_Mips_CALL_HI16;
498b05cb67bSJack Carter       break;
499b05cb67bSJack Carter     case MCSymbolRefExpr::VK_Mips_CALL_LO16:
500b05cb67bSJack Carter       FixupKind = Mips::fixup_Mips_CALL_LO16;
501b05cb67bSJack Carter       break;
502b355e8f6SZoran Jovanovic     case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
503b355e8f6SZoran Jovanovic       FixupKind = Mips::fixup_MIPS_PCHI16;
504b355e8f6SZoran Jovanovic       break;
505b355e8f6SZoran Jovanovic     case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
506b355e8f6SZoran Jovanovic       FixupKind = Mips::fixup_MIPS_PCLO16;
507b355e8f6SZoran Jovanovic       break;
508c85e3ff3SBruno Cardoso Lopes     } // switch
509fe384a2cSAkira Hatanaka 
510b5cf5909SJack Carter     Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
511fe384a2cSAkira Hatanaka     return 0;
512c85e3ff3SBruno Cardoso Lopes   }
513b5cf5909SJack Carter   return 0;
514b5cf5909SJack Carter }
515b5cf5909SJack Carter 
516b5cf5909SJack Carter /// getMachineOpValue - Return binary encoding of operand. If the machine
517b5cf5909SJack Carter /// operand requires relocation, record the relocation and return zero.
518b5cf5909SJack Carter unsigned MipsMCCodeEmitter::
519b5cf5909SJack Carter getMachineOpValue(const MCInst &MI, const MCOperand &MO,
5203fa98a65SDavid Woodhouse                   SmallVectorImpl<MCFixup> &Fixups,
5213fa98a65SDavid Woodhouse                   const MCSubtargetInfo &STI) const {
522b5cf5909SJack Carter   if (MO.isReg()) {
523b5cf5909SJack Carter     unsigned Reg = MO.getReg();
524bc07a890SBill Wendling     unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
525b5cf5909SJack Carter     return RegNo;
526b5cf5909SJack Carter   } else if (MO.isImm()) {
527b5cf5909SJack Carter     return static_cast<unsigned>(MO.getImm());
528b5cf5909SJack Carter   } else if (MO.isFPImm()) {
529b5cf5909SJack Carter     return static_cast<unsigned>(APFloat(MO.getFPImm())
530b5cf5909SJack Carter         .bitcastToAPInt().getHiBits(32).getLimitedValue());
531b5cf5909SJack Carter   }
532b5cf5909SJack Carter   // MO must be an Expr.
533b5cf5909SJack Carter   assert(MO.isExpr());
5343fa98a65SDavid Woodhouse   return getExprOpValue(MO.getExpr(),Fixups, STI);
535b5cf5909SJack Carter }
536c85e3ff3SBruno Cardoso Lopes 
5376b59c449SMatheus Almeida /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
5386b59c449SMatheus Almeida /// instructions.
5396b59c449SMatheus Almeida unsigned
5406b59c449SMatheus Almeida MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
5413fa98a65SDavid Woodhouse                                      SmallVectorImpl<MCFixup> &Fixups,
5423fa98a65SDavid Woodhouse                                      const MCSubtargetInfo &STI) const {
5436b59c449SMatheus Almeida   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
5446b59c449SMatheus Almeida   assert(MI.getOperand(OpNo).isReg());
5453fa98a65SDavid Woodhouse   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
5463fa98a65SDavid Woodhouse   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
5476b59c449SMatheus Almeida 
5486b59c449SMatheus Almeida   // The immediate field of an LD/ST instruction is scaled which means it must
5496b59c449SMatheus Almeida   // be divided (when encoding) by the size (in bytes) of the instructions'
5506b59c449SMatheus Almeida   // data format.
5516b59c449SMatheus Almeida   // .b - 1 byte
5526b59c449SMatheus Almeida   // .h - 2 bytes
5536b59c449SMatheus Almeida   // .w - 4 bytes
5546b59c449SMatheus Almeida   // .d - 8 bytes
5556b59c449SMatheus Almeida   switch(MI.getOpcode())
5566b59c449SMatheus Almeida   {
5576b59c449SMatheus Almeida   default:
5586b59c449SMatheus Almeida     assert (0 && "Unexpected instruction");
5596b59c449SMatheus Almeida     break;
5606b59c449SMatheus Almeida   case Mips::LD_B:
5616b59c449SMatheus Almeida   case Mips::ST_B:
5626b59c449SMatheus Almeida     // We don't need to scale the offset in this case
5636b59c449SMatheus Almeida     break;
5646b59c449SMatheus Almeida   case Mips::LD_H:
5656b59c449SMatheus Almeida   case Mips::ST_H:
5666b59c449SMatheus Almeida     OffBits >>= 1;
5676b59c449SMatheus Almeida     break;
5686b59c449SMatheus Almeida   case Mips::LD_W:
5696b59c449SMatheus Almeida   case Mips::ST_W:
5706b59c449SMatheus Almeida     OffBits >>= 2;
5716b59c449SMatheus Almeida     break;
5726b59c449SMatheus Almeida   case Mips::LD_D:
5736b59c449SMatheus Almeida   case Mips::ST_D:
5746b59c449SMatheus Almeida     OffBits >>= 3;
5756b59c449SMatheus Almeida     break;
5766b59c449SMatheus Almeida   }
5776b59c449SMatheus Almeida 
5786b59c449SMatheus Almeida   return (OffBits & 0xFFFF) | RegBits;
5796b59c449SMatheus Almeida }
5806b59c449SMatheus Almeida 
581c85e3ff3SBruno Cardoso Lopes /// getMemEncoding - Return binary encoding of memory related operand.
582c85e3ff3SBruno Cardoso Lopes /// If the offset operand requires relocation, record the relocation.
583c85e3ff3SBruno Cardoso Lopes unsigned
584c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
5853fa98a65SDavid Woodhouse                                   SmallVectorImpl<MCFixup> &Fixups,
5863fa98a65SDavid Woodhouse                                   const MCSubtargetInfo &STI) const {
587c85e3ff3SBruno Cardoso Lopes   // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
588c85e3ff3SBruno Cardoso Lopes   assert(MI.getOperand(OpNo).isReg());
5893fa98a65SDavid Woodhouse   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
5903fa98a65SDavid Woodhouse   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
591c85e3ff3SBruno Cardoso Lopes 
592c85e3ff3SBruno Cardoso Lopes   return (OffBits & 0xFFFF) | RegBits;
593c85e3ff3SBruno Cardoso Lopes }
594c85e3ff3SBruno Cardoso Lopes 
59597700977SJack Carter unsigned MipsMCCodeEmitter::
59697700977SJack Carter getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
5973fa98a65SDavid Woodhouse                       SmallVectorImpl<MCFixup> &Fixups,
5983fa98a65SDavid Woodhouse                       const MCSubtargetInfo &STI) const {
59997700977SJack Carter   // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
60097700977SJack Carter   assert(MI.getOperand(OpNo).isReg());
6013fa98a65SDavid Woodhouse   unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
6023fa98a65SDavid Woodhouse   unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
60397700977SJack Carter 
60497700977SJack Carter   return (OffBits & 0x0FFF) | RegBits;
60597700977SJack Carter }
60697700977SJack Carter 
607c85e3ff3SBruno Cardoso Lopes unsigned
608c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
6093fa98a65SDavid Woodhouse                                       SmallVectorImpl<MCFixup> &Fixups,
6103fa98a65SDavid Woodhouse                                       const MCSubtargetInfo &STI) const {
611049e9e4dSAkira Hatanaka   assert(MI.getOperand(OpNo).isImm());
6123fa98a65SDavid Woodhouse   unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
61356b70de0SBruno Cardoso Lopes   return SizeEncoding - 1;
614c85e3ff3SBruno Cardoso Lopes }
615c85e3ff3SBruno Cardoso Lopes 
616049e9e4dSAkira Hatanaka // FIXME: should be called getMSBEncoding
617049e9e4dSAkira Hatanaka //
618c85e3ff3SBruno Cardoso Lopes unsigned
619c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
6203fa98a65SDavid Woodhouse                                       SmallVectorImpl<MCFixup> &Fixups,
6213fa98a65SDavid Woodhouse                                       const MCSubtargetInfo &STI) const {
622049e9e4dSAkira Hatanaka   assert(MI.getOperand(OpNo-1).isImm());
623049e9e4dSAkira Hatanaka   assert(MI.getOperand(OpNo).isImm());
6243fa98a65SDavid Woodhouse   unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
6253fa98a65SDavid Woodhouse   unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
626049e9e4dSAkira Hatanaka 
62756b70de0SBruno Cardoso Lopes   return Position + Size - 1;
628c85e3ff3SBruno Cardoso Lopes }
629c85e3ff3SBruno Cardoso Lopes 
630779c5937SMatheus Almeida unsigned
631779c5937SMatheus Almeida MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
6323fa98a65SDavid Woodhouse                                      SmallVectorImpl<MCFixup> &Fixups,
6333fa98a65SDavid Woodhouse                                      const MCSubtargetInfo &STI) const {
634779c5937SMatheus Almeida   assert(MI.getOperand(OpNo).isImm());
635779c5937SMatheus Almeida   // The immediate is encoded as 'immediate - 1'.
6363fa98a65SDavid Woodhouse   return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
637779c5937SMatheus Almeida }
638779c5937SMatheus Almeida 
639b59e1a41SDaniel Sanders unsigned
640b59e1a41SDaniel Sanders MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
641b59e1a41SDaniel Sanders                                          SmallVectorImpl<MCFixup> &Fixups,
642b59e1a41SDaniel Sanders                                          const MCSubtargetInfo &STI) const {
643b9c07f3bSZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
644b9c07f3bSZoran Jovanovic   if (MO.isImm()) {
645b59e1a41SDaniel Sanders     // The immediate is encoded as 'immediate << 2'.
646b9c07f3bSZoran Jovanovic     unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
647b59e1a41SDaniel Sanders     assert((Res & 3) == 0);
648b59e1a41SDaniel Sanders     return Res >> 2;
649b59e1a41SDaniel Sanders   }
650c85e3ff3SBruno Cardoso Lopes 
651b9c07f3bSZoran Jovanovic   assert(MO.isExpr() &&
652b9c07f3bSZoran Jovanovic          "getSimm19Lsl2Encoding expects only expressions or an immediate");
653b9c07f3bSZoran Jovanovic 
654b9c07f3bSZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
655b9c07f3bSZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
656b9c07f3bSZoran Jovanovic                                    MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
657b9c07f3bSZoran Jovanovic   return 0;
658b9c07f3bSZoran Jovanovic }
659b9c07f3bSZoran Jovanovic 
6602855142aSZoran Jovanovic unsigned
6612855142aSZoran Jovanovic MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
6622855142aSZoran Jovanovic                                          SmallVectorImpl<MCFixup> &Fixups,
6632855142aSZoran Jovanovic                                          const MCSubtargetInfo &STI) const {
664a5acdcf9SZoran Jovanovic   const MCOperand &MO = MI.getOperand(OpNo);
665a5acdcf9SZoran Jovanovic   if (MO.isImm()) {
6662855142aSZoran Jovanovic     // The immediate is encoded as 'immediate << 3'.
6672855142aSZoran Jovanovic     unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
6682855142aSZoran Jovanovic     assert((Res & 7) == 0);
6692855142aSZoran Jovanovic     return Res >> 3;
6702855142aSZoran Jovanovic   }
6712855142aSZoran Jovanovic 
672a5acdcf9SZoran Jovanovic   assert(MO.isExpr() &&
673a5acdcf9SZoran Jovanovic          "getSimm18Lsl2Encoding expects only expressions or an immediate");
674a5acdcf9SZoran Jovanovic 
675a5acdcf9SZoran Jovanovic   const MCExpr *Expr = MO.getExpr();
676a5acdcf9SZoran Jovanovic   Fixups.push_back(MCFixup::Create(0, Expr,
677a5acdcf9SZoran Jovanovic                                    MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
678a5acdcf9SZoran Jovanovic   return 0;
679a5acdcf9SZoran Jovanovic }
680a5acdcf9SZoran Jovanovic 
681b59e1a41SDaniel Sanders #include "MipsGenMCCodeEmitter.inc"
682