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:: 348c74e3eb9SZoran Jovanovic getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo, 349c74e3eb9SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 350c74e3eb9SZoran Jovanovic const MCSubtargetInfo &STI) const { 351c74e3eb9SZoran Jovanovic 352c74e3eb9SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 353c74e3eb9SZoran Jovanovic if (MO.isImm()) { 354c74e3eb9SZoran Jovanovic // The immediate is encoded as 'immediate << 2'. 355c74e3eb9SZoran Jovanovic unsigned Res = getMachineOpValue(MI, MO, Fixups, STI); 356c74e3eb9SZoran Jovanovic assert((Res & 3) == 0); 357c74e3eb9SZoran Jovanovic return Res >> 2; 358c74e3eb9SZoran Jovanovic } 359c74e3eb9SZoran Jovanovic 360c74e3eb9SZoran Jovanovic assert(MO.isExpr() && 361c74e3eb9SZoran Jovanovic "getUImm5Lsl2Encoding expects only expressions or an immediate"); 362c74e3eb9SZoran Jovanovic 363c74e3eb9SZoran Jovanovic return 0; 364c74e3eb9SZoran Jovanovic } 365c74e3eb9SZoran Jovanovic 366c74e3eb9SZoran Jovanovic unsigned MipsMCCodeEmitter:: 367bac3619bSZoran Jovanovic getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo, 368bac3619bSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 369bac3619bSZoran Jovanovic const MCSubtargetInfo &STI) const { 370bac3619bSZoran Jovanovic 371bac3619bSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 372bac3619bSZoran Jovanovic if (MO.isImm()) { 373bac3619bSZoran Jovanovic int Value = MO.getImm(); 374bac3619bSZoran Jovanovic return Value >> 2; 375bac3619bSZoran Jovanovic } 376bac3619bSZoran Jovanovic 377bac3619bSZoran Jovanovic return 0; 378bac3619bSZoran Jovanovic } 379bac3619bSZoran Jovanovic 380bac3619bSZoran Jovanovic unsigned MipsMCCodeEmitter:: 381*42b84443SZoran Jovanovic getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo, 382*42b84443SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 383*42b84443SZoran Jovanovic const MCSubtargetInfo &STI) const { 384*42b84443SZoran Jovanovic 385*42b84443SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 386*42b84443SZoran Jovanovic if (MO.isImm()) { 387*42b84443SZoran Jovanovic unsigned Value = MO.getImm(); 388*42b84443SZoran Jovanovic return Value >> 2; 389*42b84443SZoran Jovanovic } 390*42b84443SZoran Jovanovic 391*42b84443SZoran Jovanovic return 0; 392*42b84443SZoran Jovanovic } 393*42b84443SZoran Jovanovic 394*42b84443SZoran Jovanovic unsigned MipsMCCodeEmitter:: 39598bd58caSZoran Jovanovic getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo, 39698bd58caSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 39798bd58caSZoran Jovanovic const MCSubtargetInfo &STI) const { 39898bd58caSZoran Jovanovic 39998bd58caSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 40098bd58caSZoran Jovanovic if (MO.isImm()) { 40198bd58caSZoran Jovanovic unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff; 40298bd58caSZoran Jovanovic return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff)); 40398bd58caSZoran Jovanovic } 40498bd58caSZoran Jovanovic 40598bd58caSZoran Jovanovic return 0; 40698bd58caSZoran Jovanovic } 40798bd58caSZoran Jovanovic 40898bd58caSZoran Jovanovic unsigned MipsMCCodeEmitter:: 4093fa98a65SDavid Woodhouse getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups, 4103fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 411b5cf5909SJack Carter int64_t Res; 412049e9e4dSAkira Hatanaka 413b5cf5909SJack Carter if (Expr->EvaluateAsAbsolute(Res)) 414b5cf5909SJack Carter return Res; 415fe384a2cSAkira Hatanaka 416fe384a2cSAkira Hatanaka MCExpr::ExprKind Kind = Expr->getKind(); 417b5cf5909SJack Carter if (Kind == MCExpr::Constant) { 418b5cf5909SJack Carter return cast<MCConstantExpr>(Expr)->getValue(); 419b5cf5909SJack Carter } 420fe384a2cSAkira Hatanaka 421fe384a2cSAkira Hatanaka if (Kind == MCExpr::Binary) { 4223fa98a65SDavid Woodhouse unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI); 4233fa98a65SDavid Woodhouse Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI); 424b5cf5909SJack Carter return Res; 425fe384a2cSAkira Hatanaka } 426a5da588bSPetar Jovanovic 427a5da588bSPetar Jovanovic if (Kind == MCExpr::Target) { 428a5da588bSPetar Jovanovic const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr); 429a5da588bSPetar Jovanovic 430a5da588bSPetar Jovanovic Mips::Fixups FixupKind = Mips::Fixups(0); 431a5da588bSPetar Jovanovic switch (MipsExpr->getKind()) { 432a5da588bSPetar Jovanovic default: llvm_unreachable("Unsupported fixup kind for target expression!"); 43306c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HIGHEST: 43406c47803SSasa Stankovic FixupKind = Mips::fixup_Mips_HIGHEST; 43506c47803SSasa Stankovic break; 43606c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HIGHER: 43706c47803SSasa Stankovic FixupKind = Mips::fixup_Mips_HIGHER; 43806c47803SSasa Stankovic break; 43906c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HI: 440a5da588bSPetar Jovanovic FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 441a5da588bSPetar Jovanovic : Mips::fixup_Mips_HI16; 442a5da588bSPetar Jovanovic break; 44306c47803SSasa Stankovic case MipsMCExpr::VK_Mips_LO: 444a5da588bSPetar Jovanovic FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 445a5da588bSPetar Jovanovic : Mips::fixup_Mips_LO16; 446a5da588bSPetar Jovanovic break; 447a5da588bSPetar Jovanovic } 448a5da588bSPetar Jovanovic Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind))); 449a5da588bSPetar Jovanovic return 0; 450a5da588bSPetar Jovanovic } 451a5da588bSPetar Jovanovic 452b5cf5909SJack Carter if (Kind == MCExpr::SymbolRef) { 453f9774c32SBill Wendling Mips::Fixups FixupKind = Mips::Fixups(0); 454e2eed964SAkira Hatanaka 455049e9e4dSAkira Hatanaka switch(cast<MCSymbolRefExpr>(Expr)->getKind()) { 456b9f9de93SJack Carter default: llvm_unreachable("Unknown fixup kind!"); 457b9f9de93SJack Carter break; 458b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GPOFF_HI : 459b9f9de93SJack Carter FixupKind = Mips::fixup_Mips_GPOFF_HI; 460b9f9de93SJack Carter break; 461b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GPOFF_LO : 462b9f9de93SJack Carter FixupKind = Mips::fixup_Mips_GPOFF_LO; 463b9f9de93SJack Carter break; 464b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_PAGE : 465d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE 466e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_PAGE; 467b9f9de93SJack Carter break; 468b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_OFST : 469d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST 470e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_OFST; 471b9f9de93SJack Carter break; 4725ddcfda8SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_DISP : 473d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP 474e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_DISP; 4755ddcfda8SJack Carter break; 476c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GPREL: 477c85e3ff3SBruno Cardoso Lopes FixupKind = Mips::fixup_Mips_GPREL16; 478c85e3ff3SBruno Cardoso Lopes break; 479c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT_CALL: 480d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16 481e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_CALL16; 482c85e3ff3SBruno Cardoso Lopes break; 48361e6d987SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT16: 484d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 485e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_Global; 48661e6d987SBruno Cardoso Lopes break; 487c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT: 488d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 489e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_Local; 490c85e3ff3SBruno Cardoso Lopes break; 491c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_ABS_HI: 492d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 493e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_HI16; 494c85e3ff3SBruno Cardoso Lopes break; 495c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_ABS_LO: 496d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 497e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_LO16; 498c85e3ff3SBruno Cardoso Lopes break; 499c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TLSGD: 500d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD 50169be811aSZoran Jovanovic : Mips::fixup_Mips_TLSGD; 502c85e3ff3SBruno Cardoso Lopes break; 503e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_TLSLDM: 504d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM 50569be811aSZoran Jovanovic : Mips::fixup_Mips_TLSLDM; 506e2eed964SAkira Hatanaka break; 507e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_DTPREL_HI: 508d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16 509e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_DTPREL_HI; 510e2eed964SAkira Hatanaka break; 511e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_DTPREL_LO: 512d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16 513e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_DTPREL_LO; 514e2eed964SAkira Hatanaka break; 515c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOTTPREL: 516c85e3ff3SBruno Cardoso Lopes FixupKind = Mips::fixup_Mips_GOTTPREL; 517c85e3ff3SBruno Cardoso Lopes break; 518c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TPREL_HI: 519d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16 520e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_TPREL_HI; 521c85e3ff3SBruno Cardoso Lopes break; 522c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TPREL_LO: 523d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16 524e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_TPREL_LO; 525c85e3ff3SBruno Cardoso Lopes break; 52684491abbSJack Carter case MCSymbolRefExpr::VK_Mips_HIGHER: 52784491abbSJack Carter FixupKind = Mips::fixup_Mips_HIGHER; 52884491abbSJack Carter break; 52984491abbSJack Carter case MCSymbolRefExpr::VK_Mips_HIGHEST: 53084491abbSJack Carter FixupKind = Mips::fixup_Mips_HIGHEST; 53184491abbSJack Carter break; 532b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_GOT_HI16: 533b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_GOT_HI16; 534b05cb67bSJack Carter break; 535b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_GOT_LO16: 536b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_GOT_LO16; 537b05cb67bSJack Carter break; 538b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_CALL_HI16: 539b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_CALL_HI16; 540b05cb67bSJack Carter break; 541b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_CALL_LO16: 542b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_CALL_LO16; 543b05cb67bSJack Carter break; 544b355e8f6SZoran Jovanovic case MCSymbolRefExpr::VK_Mips_PCREL_HI16: 545b355e8f6SZoran Jovanovic FixupKind = Mips::fixup_MIPS_PCHI16; 546b355e8f6SZoran Jovanovic break; 547b355e8f6SZoran Jovanovic case MCSymbolRefExpr::VK_Mips_PCREL_LO16: 548b355e8f6SZoran Jovanovic FixupKind = Mips::fixup_MIPS_PCLO16; 549b355e8f6SZoran Jovanovic break; 550c85e3ff3SBruno Cardoso Lopes } // switch 551fe384a2cSAkira Hatanaka 552b5cf5909SJack Carter Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind))); 553fe384a2cSAkira Hatanaka return 0; 554c85e3ff3SBruno Cardoso Lopes } 555b5cf5909SJack Carter return 0; 556b5cf5909SJack Carter } 557b5cf5909SJack Carter 558b5cf5909SJack Carter /// getMachineOpValue - Return binary encoding of operand. If the machine 559b5cf5909SJack Carter /// operand requires relocation, record the relocation and return zero. 560b5cf5909SJack Carter unsigned MipsMCCodeEmitter:: 561b5cf5909SJack Carter getMachineOpValue(const MCInst &MI, const MCOperand &MO, 5623fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5633fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 564b5cf5909SJack Carter if (MO.isReg()) { 565b5cf5909SJack Carter unsigned Reg = MO.getReg(); 566bc07a890SBill Wendling unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg); 567b5cf5909SJack Carter return RegNo; 568b5cf5909SJack Carter } else if (MO.isImm()) { 569b5cf5909SJack Carter return static_cast<unsigned>(MO.getImm()); 570b5cf5909SJack Carter } else if (MO.isFPImm()) { 571b5cf5909SJack Carter return static_cast<unsigned>(APFloat(MO.getFPImm()) 572b5cf5909SJack Carter .bitcastToAPInt().getHiBits(32).getLimitedValue()); 573b5cf5909SJack Carter } 574b5cf5909SJack Carter // MO must be an Expr. 575b5cf5909SJack Carter assert(MO.isExpr()); 5763fa98a65SDavid Woodhouse return getExprOpValue(MO.getExpr(),Fixups, STI); 577b5cf5909SJack Carter } 578c85e3ff3SBruno Cardoso Lopes 5796b59c449SMatheus Almeida /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST 5806b59c449SMatheus Almeida /// instructions. 5816b59c449SMatheus Almeida unsigned 5826b59c449SMatheus Almeida MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo, 5833fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5843fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 5856b59c449SMatheus Almeida // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. 5866b59c449SMatheus Almeida assert(MI.getOperand(OpNo).isReg()); 5873fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; 5883fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 5896b59c449SMatheus Almeida 5906b59c449SMatheus Almeida // The immediate field of an LD/ST instruction is scaled which means it must 5916b59c449SMatheus Almeida // be divided (when encoding) by the size (in bytes) of the instructions' 5926b59c449SMatheus Almeida // data format. 5936b59c449SMatheus Almeida // .b - 1 byte 5946b59c449SMatheus Almeida // .h - 2 bytes 5956b59c449SMatheus Almeida // .w - 4 bytes 5966b59c449SMatheus Almeida // .d - 8 bytes 5976b59c449SMatheus Almeida switch(MI.getOpcode()) 5986b59c449SMatheus Almeida { 5996b59c449SMatheus Almeida default: 6006b59c449SMatheus Almeida assert (0 && "Unexpected instruction"); 6016b59c449SMatheus Almeida break; 6026b59c449SMatheus Almeida case Mips::LD_B: 6036b59c449SMatheus Almeida case Mips::ST_B: 6046b59c449SMatheus Almeida // We don't need to scale the offset in this case 6056b59c449SMatheus Almeida break; 6066b59c449SMatheus Almeida case Mips::LD_H: 6076b59c449SMatheus Almeida case Mips::ST_H: 6086b59c449SMatheus Almeida OffBits >>= 1; 6096b59c449SMatheus Almeida break; 6106b59c449SMatheus Almeida case Mips::LD_W: 6116b59c449SMatheus Almeida case Mips::ST_W: 6126b59c449SMatheus Almeida OffBits >>= 2; 6136b59c449SMatheus Almeida break; 6146b59c449SMatheus Almeida case Mips::LD_D: 6156b59c449SMatheus Almeida case Mips::ST_D: 6166b59c449SMatheus Almeida OffBits >>= 3; 6176b59c449SMatheus Almeida break; 6186b59c449SMatheus Almeida } 6196b59c449SMatheus Almeida 6206b59c449SMatheus Almeida return (OffBits & 0xFFFF) | RegBits; 6216b59c449SMatheus Almeida } 6226b59c449SMatheus Almeida 623c85e3ff3SBruno Cardoso Lopes /// getMemEncoding - Return binary encoding of memory related operand. 624c85e3ff3SBruno Cardoso Lopes /// If the offset operand requires relocation, record the relocation. 625c85e3ff3SBruno Cardoso Lopes unsigned 626c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo, 6273fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 6283fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 629c85e3ff3SBruno Cardoso Lopes // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. 630c85e3ff3SBruno Cardoso Lopes assert(MI.getOperand(OpNo).isReg()); 6313fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; 6323fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 633c85e3ff3SBruno Cardoso Lopes 634c85e3ff3SBruno Cardoso Lopes return (OffBits & 0xFFFF) | RegBits; 635c85e3ff3SBruno Cardoso Lopes } 636c85e3ff3SBruno Cardoso Lopes 63797700977SJack Carter unsigned MipsMCCodeEmitter:: 63897700977SJack Carter getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo, 6393fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 6403fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 64197700977SJack Carter // Base register is encoded in bits 20-16, offset is encoded in bits 11-0. 64297700977SJack Carter assert(MI.getOperand(OpNo).isReg()); 6433fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16; 6443fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 64597700977SJack Carter 64697700977SJack Carter return (OffBits & 0x0FFF) | RegBits; 64797700977SJack Carter } 64897700977SJack Carter 649c85e3ff3SBruno Cardoso Lopes unsigned 650c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo, 6513fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 6523fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 653049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo).isImm()); 6543fa98a65SDavid Woodhouse unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); 65556b70de0SBruno Cardoso Lopes return SizeEncoding - 1; 656c85e3ff3SBruno Cardoso Lopes } 657c85e3ff3SBruno Cardoso Lopes 658049e9e4dSAkira Hatanaka // FIXME: should be called getMSBEncoding 659049e9e4dSAkira Hatanaka // 660c85e3ff3SBruno Cardoso Lopes unsigned 661c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo, 6623fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 6633fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 664049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo-1).isImm()); 665049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo).isImm()); 6663fa98a65SDavid Woodhouse unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI); 6673fa98a65SDavid Woodhouse unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); 668049e9e4dSAkira Hatanaka 66956b70de0SBruno Cardoso Lopes return Position + Size - 1; 670c85e3ff3SBruno Cardoso Lopes } 671c85e3ff3SBruno Cardoso Lopes 672779c5937SMatheus Almeida unsigned 673779c5937SMatheus Almeida MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo, 6743fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 6753fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 676779c5937SMatheus Almeida assert(MI.getOperand(OpNo).isImm()); 677779c5937SMatheus Almeida // The immediate is encoded as 'immediate - 1'. 6783fa98a65SDavid Woodhouse return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1; 679779c5937SMatheus Almeida } 680779c5937SMatheus Almeida 681b59e1a41SDaniel Sanders unsigned 682b59e1a41SDaniel Sanders MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo, 683b59e1a41SDaniel Sanders SmallVectorImpl<MCFixup> &Fixups, 684b59e1a41SDaniel Sanders const MCSubtargetInfo &STI) const { 685b9c07f3bSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 686b9c07f3bSZoran Jovanovic if (MO.isImm()) { 687b59e1a41SDaniel Sanders // The immediate is encoded as 'immediate << 2'. 688b9c07f3bSZoran Jovanovic unsigned Res = getMachineOpValue(MI, MO, Fixups, STI); 689b59e1a41SDaniel Sanders assert((Res & 3) == 0); 690b59e1a41SDaniel Sanders return Res >> 2; 691b59e1a41SDaniel Sanders } 692c85e3ff3SBruno Cardoso Lopes 693b9c07f3bSZoran Jovanovic assert(MO.isExpr() && 694b9c07f3bSZoran Jovanovic "getSimm19Lsl2Encoding expects only expressions or an immediate"); 695b9c07f3bSZoran Jovanovic 696b9c07f3bSZoran Jovanovic const MCExpr *Expr = MO.getExpr(); 697b9c07f3bSZoran Jovanovic Fixups.push_back(MCFixup::Create(0, Expr, 698b9c07f3bSZoran Jovanovic MCFixupKind(Mips::fixup_MIPS_PC19_S2))); 699b9c07f3bSZoran Jovanovic return 0; 700b9c07f3bSZoran Jovanovic } 701b9c07f3bSZoran Jovanovic 7022855142aSZoran Jovanovic unsigned 7032855142aSZoran Jovanovic MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo, 7042855142aSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 7052855142aSZoran Jovanovic const MCSubtargetInfo &STI) const { 706a5acdcf9SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 707a5acdcf9SZoran Jovanovic if (MO.isImm()) { 7082855142aSZoran Jovanovic // The immediate is encoded as 'immediate << 3'. 7092855142aSZoran Jovanovic unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); 7102855142aSZoran Jovanovic assert((Res & 7) == 0); 7112855142aSZoran Jovanovic return Res >> 3; 7122855142aSZoran Jovanovic } 7132855142aSZoran Jovanovic 714a5acdcf9SZoran Jovanovic assert(MO.isExpr() && 715a5acdcf9SZoran Jovanovic "getSimm18Lsl2Encoding expects only expressions or an immediate"); 716a5acdcf9SZoran Jovanovic 717a5acdcf9SZoran Jovanovic const MCExpr *Expr = MO.getExpr(); 718a5acdcf9SZoran Jovanovic Fixups.push_back(MCFixup::Create(0, Expr, 719a5acdcf9SZoran Jovanovic MCFixupKind(Mips::fixup_MIPS_PC18_S3))); 720a5acdcf9SZoran Jovanovic return 0; 721a5acdcf9SZoran Jovanovic } 722a5acdcf9SZoran Jovanovic 7234a00fdc2SZoran Jovanovic unsigned 7244a00fdc2SZoran Jovanovic MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo, 7254a00fdc2SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups, 7264a00fdc2SZoran Jovanovic const MCSubtargetInfo &STI) const { 7274a00fdc2SZoran Jovanovic assert(MI.getOperand(OpNo).isImm()); 7284a00fdc2SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 7294a00fdc2SZoran Jovanovic return MO.getImm() % 8; 7304a00fdc2SZoran Jovanovic } 7314a00fdc2SZoran Jovanovic 732b59e1a41SDaniel Sanders #include "MipsGenMCCodeEmitter.inc" 733