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 15750ecec7SAkira Hatanaka #define DEBUG_TYPE "mccodeemitter" 169e1450bcSMatheus Almeida 179e1450bcSMatheus Almeida #include "MipsMCCodeEmitter.h" 18c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsFixupKinds.h" 19a5da588bSPetar Jovanovic #include "MCTargetDesc/MipsMCExpr.h" 20c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsMCTargetDesc.h" 21c85e3ff3SBruno Cardoso Lopes #include "llvm/ADT/APFloat.h" 229e1450bcSMatheus Almeida #include "llvm/ADT/SmallVector.h" 235d6faed1SAkira Hatanaka #include "llvm/MC/MCContext.h" 24750ecec7SAkira Hatanaka #include "llvm/MC/MCExpr.h" 25750ecec7SAkira Hatanaka #include "llvm/MC/MCInst.h" 26750ecec7SAkira Hatanaka #include "llvm/MC/MCInstrInfo.h" 279e1450bcSMatheus Almeida #include "llvm/MC/MCFixup.h" 28750ecec7SAkira Hatanaka #include "llvm/MC/MCSubtargetInfo.h" 29750ecec7SAkira Hatanaka #include "llvm/Support/raw_ostream.h" 30750ecec7SAkira Hatanaka 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 245c85e3ff3SBruno Cardoso Lopes /// getJumpTargetOpValue - Return binary encoding of the jump 246c85e3ff3SBruno Cardoso Lopes /// target operand. If the machine operand requires relocation, 247c85e3ff3SBruno Cardoso Lopes /// record the relocation and return zero. 248c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter:: 249c85e3ff3SBruno Cardoso Lopes getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, 2503fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 2513fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 252c85e3ff3SBruno Cardoso Lopes 253c85e3ff3SBruno Cardoso Lopes const MCOperand &MO = MI.getOperand(OpNo); 2544f69a0f2SJack Carter // If the destination is an immediate, divide by 4. 2554f69a0f2SJack Carter if (MO.isImm()) return MO.getImm()>>2; 2564f69a0f2SJack Carter 25771e6a749SJack Carter assert(MO.isExpr() && 25871e6a749SJack Carter "getJumpTargetOpValue expects only expressions or an immediate"); 259c85e3ff3SBruno Cardoso Lopes 260c85e3ff3SBruno Cardoso Lopes const MCExpr *Expr = MO.getExpr(); 261c85e3ff3SBruno Cardoso Lopes Fixups.push_back(MCFixup::Create(0, Expr, 262c85e3ff3SBruno Cardoso Lopes MCFixupKind(Mips::fixup_Mips_26))); 263c85e3ff3SBruno Cardoso Lopes return 0; 264c85e3ff3SBruno Cardoso Lopes } 265c85e3ff3SBruno Cardoso Lopes 266c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter:: 267507e084aSZoran Jovanovic getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo, 2683fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 2693fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 270507e084aSZoran Jovanovic 271507e084aSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo); 272507e084aSZoran Jovanovic // If the destination is an immediate, divide by 2. 273507e084aSZoran Jovanovic if (MO.isImm()) return MO.getImm() >> 1; 274507e084aSZoran Jovanovic 275507e084aSZoran Jovanovic assert(MO.isExpr() && 276507e084aSZoran Jovanovic "getJumpTargetOpValueMM expects only expressions or an immediate"); 277507e084aSZoran Jovanovic 278507e084aSZoran Jovanovic const MCExpr *Expr = MO.getExpr(); 279507e084aSZoran Jovanovic Fixups.push_back(MCFixup::Create(0, Expr, 280507e084aSZoran Jovanovic MCFixupKind(Mips::fixup_MICROMIPS_26_S1))); 281507e084aSZoran Jovanovic return 0; 282507e084aSZoran Jovanovic } 283507e084aSZoran Jovanovic 284507e084aSZoran Jovanovic unsigned MipsMCCodeEmitter:: 2853fa98a65SDavid Woodhouse getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups, 2863fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 287b5cf5909SJack Carter int64_t Res; 288049e9e4dSAkira Hatanaka 289b5cf5909SJack Carter if (Expr->EvaluateAsAbsolute(Res)) 290b5cf5909SJack Carter return Res; 291fe384a2cSAkira Hatanaka 292fe384a2cSAkira Hatanaka MCExpr::ExprKind Kind = Expr->getKind(); 293b5cf5909SJack Carter if (Kind == MCExpr::Constant) { 294b5cf5909SJack Carter return cast<MCConstantExpr>(Expr)->getValue(); 295b5cf5909SJack Carter } 296fe384a2cSAkira Hatanaka 297fe384a2cSAkira Hatanaka if (Kind == MCExpr::Binary) { 2983fa98a65SDavid Woodhouse unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI); 2993fa98a65SDavid Woodhouse Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI); 300b5cf5909SJack Carter return Res; 301fe384a2cSAkira Hatanaka } 302a5da588bSPetar Jovanovic 303a5da588bSPetar Jovanovic if (Kind == MCExpr::Target) { 304a5da588bSPetar Jovanovic const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr); 305a5da588bSPetar Jovanovic 306a5da588bSPetar Jovanovic Mips::Fixups FixupKind = Mips::Fixups(0); 307a5da588bSPetar Jovanovic switch (MipsExpr->getKind()) { 308a5da588bSPetar Jovanovic default: llvm_unreachable("Unsupported fixup kind for target expression!"); 309*06c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HIGHEST: 310*06c47803SSasa Stankovic FixupKind = Mips::fixup_Mips_HIGHEST; 311*06c47803SSasa Stankovic break; 312*06c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HIGHER: 313*06c47803SSasa Stankovic FixupKind = Mips::fixup_Mips_HIGHER; 314*06c47803SSasa Stankovic break; 315*06c47803SSasa Stankovic case MipsMCExpr::VK_Mips_HI: 316a5da588bSPetar Jovanovic FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 317a5da588bSPetar Jovanovic : Mips::fixup_Mips_HI16; 318a5da588bSPetar Jovanovic break; 319*06c47803SSasa Stankovic case MipsMCExpr::VK_Mips_LO: 320a5da588bSPetar Jovanovic FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 321a5da588bSPetar Jovanovic : Mips::fixup_Mips_LO16; 322a5da588bSPetar Jovanovic break; 323a5da588bSPetar Jovanovic } 324a5da588bSPetar Jovanovic Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind))); 325a5da588bSPetar Jovanovic return 0; 326a5da588bSPetar Jovanovic } 327a5da588bSPetar Jovanovic 328b5cf5909SJack Carter if (Kind == MCExpr::SymbolRef) { 329f9774c32SBill Wendling Mips::Fixups FixupKind = Mips::Fixups(0); 330e2eed964SAkira Hatanaka 331049e9e4dSAkira Hatanaka switch(cast<MCSymbolRefExpr>(Expr)->getKind()) { 332b9f9de93SJack Carter default: llvm_unreachable("Unknown fixup kind!"); 333b9f9de93SJack Carter break; 334b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GPOFF_HI : 335b9f9de93SJack Carter FixupKind = Mips::fixup_Mips_GPOFF_HI; 336b9f9de93SJack Carter break; 337b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GPOFF_LO : 338b9f9de93SJack Carter FixupKind = Mips::fixup_Mips_GPOFF_LO; 339b9f9de93SJack Carter break; 340b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_PAGE : 341d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE 342e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_PAGE; 343b9f9de93SJack Carter break; 344b9f9de93SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_OFST : 345d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST 346e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_OFST; 347b9f9de93SJack Carter break; 3485ddcfda8SJack Carter case MCSymbolRefExpr::VK_Mips_GOT_DISP : 349d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP 350e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_DISP; 3515ddcfda8SJack Carter break; 352c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GPREL: 353c85e3ff3SBruno Cardoso Lopes FixupKind = Mips::fixup_Mips_GPREL16; 354c85e3ff3SBruno Cardoso Lopes break; 355c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT_CALL: 356d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16 357e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_CALL16; 358c85e3ff3SBruno Cardoso Lopes break; 35961e6d987SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT16: 360d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 361e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_Global; 36261e6d987SBruno Cardoso Lopes break; 363c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOT: 364d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 365e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_GOT_Local; 366c85e3ff3SBruno Cardoso Lopes break; 367c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_ABS_HI: 368d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 369e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_HI16; 370c85e3ff3SBruno Cardoso Lopes break; 371c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_ABS_LO: 372d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 373e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_LO16; 374c85e3ff3SBruno Cardoso Lopes break; 375c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TLSGD: 376d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD 37769be811aSZoran Jovanovic : Mips::fixup_Mips_TLSGD; 378c85e3ff3SBruno Cardoso Lopes break; 379e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_TLSLDM: 380d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM 38169be811aSZoran Jovanovic : Mips::fixup_Mips_TLSLDM; 382e2eed964SAkira Hatanaka break; 383e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_DTPREL_HI: 384d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16 385e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_DTPREL_HI; 386e2eed964SAkira Hatanaka break; 387e2eed964SAkira Hatanaka case MCSymbolRefExpr::VK_Mips_DTPREL_LO: 388d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16 389e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_DTPREL_LO; 390e2eed964SAkira Hatanaka break; 391c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_GOTTPREL: 392c85e3ff3SBruno Cardoso Lopes FixupKind = Mips::fixup_Mips_GOTTPREL; 393c85e3ff3SBruno Cardoso Lopes break; 394c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TPREL_HI: 395d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16 396e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_TPREL_HI; 397c85e3ff3SBruno Cardoso Lopes break; 398c85e3ff3SBruno Cardoso Lopes case MCSymbolRefExpr::VK_Mips_TPREL_LO: 399d2cca113SDavid Woodhouse FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16 400e7ae8af8SZoran Jovanovic : Mips::fixup_Mips_TPREL_LO; 401c85e3ff3SBruno Cardoso Lopes break; 40284491abbSJack Carter case MCSymbolRefExpr::VK_Mips_HIGHER: 40384491abbSJack Carter FixupKind = Mips::fixup_Mips_HIGHER; 40484491abbSJack Carter break; 40584491abbSJack Carter case MCSymbolRefExpr::VK_Mips_HIGHEST: 40684491abbSJack Carter FixupKind = Mips::fixup_Mips_HIGHEST; 40784491abbSJack Carter break; 408b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_GOT_HI16: 409b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_GOT_HI16; 410b05cb67bSJack Carter break; 411b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_GOT_LO16: 412b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_GOT_LO16; 413b05cb67bSJack Carter break; 414b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_CALL_HI16: 415b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_CALL_HI16; 416b05cb67bSJack Carter break; 417b05cb67bSJack Carter case MCSymbolRefExpr::VK_Mips_CALL_LO16: 418b05cb67bSJack Carter FixupKind = Mips::fixup_Mips_CALL_LO16; 419b05cb67bSJack Carter break; 420c85e3ff3SBruno Cardoso Lopes } // switch 421fe384a2cSAkira Hatanaka 422b5cf5909SJack Carter Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind))); 423fe384a2cSAkira Hatanaka return 0; 424c85e3ff3SBruno Cardoso Lopes } 425b5cf5909SJack Carter return 0; 426b5cf5909SJack Carter } 427b5cf5909SJack Carter 428b5cf5909SJack Carter /// getMachineOpValue - Return binary encoding of operand. If the machine 429b5cf5909SJack Carter /// operand requires relocation, record the relocation and return zero. 430b5cf5909SJack Carter unsigned MipsMCCodeEmitter:: 431b5cf5909SJack Carter getMachineOpValue(const MCInst &MI, const MCOperand &MO, 4323fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 4333fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 434b5cf5909SJack Carter if (MO.isReg()) { 435b5cf5909SJack Carter unsigned Reg = MO.getReg(); 436bc07a890SBill Wendling unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg); 437b5cf5909SJack Carter return RegNo; 438b5cf5909SJack Carter } else if (MO.isImm()) { 439b5cf5909SJack Carter return static_cast<unsigned>(MO.getImm()); 440b5cf5909SJack Carter } else if (MO.isFPImm()) { 441b5cf5909SJack Carter return static_cast<unsigned>(APFloat(MO.getFPImm()) 442b5cf5909SJack Carter .bitcastToAPInt().getHiBits(32).getLimitedValue()); 443b5cf5909SJack Carter } 444b5cf5909SJack Carter // MO must be an Expr. 445b5cf5909SJack Carter assert(MO.isExpr()); 4463fa98a65SDavid Woodhouse return getExprOpValue(MO.getExpr(),Fixups, STI); 447b5cf5909SJack Carter } 448c85e3ff3SBruno Cardoso Lopes 4496b59c449SMatheus Almeida /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST 4506b59c449SMatheus Almeida /// instructions. 4516b59c449SMatheus Almeida unsigned 4526b59c449SMatheus Almeida MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo, 4533fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 4543fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 4556b59c449SMatheus Almeida // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. 4566b59c449SMatheus Almeida assert(MI.getOperand(OpNo).isReg()); 4573fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; 4583fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 4596b59c449SMatheus Almeida 4606b59c449SMatheus Almeida // The immediate field of an LD/ST instruction is scaled which means it must 4616b59c449SMatheus Almeida // be divided (when encoding) by the size (in bytes) of the instructions' 4626b59c449SMatheus Almeida // data format. 4636b59c449SMatheus Almeida // .b - 1 byte 4646b59c449SMatheus Almeida // .h - 2 bytes 4656b59c449SMatheus Almeida // .w - 4 bytes 4666b59c449SMatheus Almeida // .d - 8 bytes 4676b59c449SMatheus Almeida switch(MI.getOpcode()) 4686b59c449SMatheus Almeida { 4696b59c449SMatheus Almeida default: 4706b59c449SMatheus Almeida assert (0 && "Unexpected instruction"); 4716b59c449SMatheus Almeida break; 4726b59c449SMatheus Almeida case Mips::LD_B: 4736b59c449SMatheus Almeida case Mips::ST_B: 4746b59c449SMatheus Almeida // We don't need to scale the offset in this case 4756b59c449SMatheus Almeida break; 4766b59c449SMatheus Almeida case Mips::LD_H: 4776b59c449SMatheus Almeida case Mips::ST_H: 4786b59c449SMatheus Almeida OffBits >>= 1; 4796b59c449SMatheus Almeida break; 4806b59c449SMatheus Almeida case Mips::LD_W: 4816b59c449SMatheus Almeida case Mips::ST_W: 4826b59c449SMatheus Almeida OffBits >>= 2; 4836b59c449SMatheus Almeida break; 4846b59c449SMatheus Almeida case Mips::LD_D: 4856b59c449SMatheus Almeida case Mips::ST_D: 4866b59c449SMatheus Almeida OffBits >>= 3; 4876b59c449SMatheus Almeida break; 4886b59c449SMatheus Almeida } 4896b59c449SMatheus Almeida 4906b59c449SMatheus Almeida return (OffBits & 0xFFFF) | RegBits; 4916b59c449SMatheus Almeida } 4926b59c449SMatheus Almeida 493c85e3ff3SBruno Cardoso Lopes /// getMemEncoding - Return binary encoding of memory related operand. 494c85e3ff3SBruno Cardoso Lopes /// If the offset operand requires relocation, record the relocation. 495c85e3ff3SBruno Cardoso Lopes unsigned 496c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo, 4973fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 4983fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 499c85e3ff3SBruno Cardoso Lopes // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. 500c85e3ff3SBruno Cardoso Lopes assert(MI.getOperand(OpNo).isReg()); 5013fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; 5023fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 503c85e3ff3SBruno Cardoso Lopes 504c85e3ff3SBruno Cardoso Lopes return (OffBits & 0xFFFF) | RegBits; 505c85e3ff3SBruno Cardoso Lopes } 506c85e3ff3SBruno Cardoso Lopes 50797700977SJack Carter unsigned MipsMCCodeEmitter:: 50897700977SJack Carter getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo, 5093fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5103fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 51197700977SJack Carter // Base register is encoded in bits 20-16, offset is encoded in bits 11-0. 51297700977SJack Carter assert(MI.getOperand(OpNo).isReg()); 5133fa98a65SDavid Woodhouse unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16; 5143fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); 51597700977SJack Carter 51697700977SJack Carter return (OffBits & 0x0FFF) | RegBits; 51797700977SJack Carter } 51897700977SJack Carter 519c85e3ff3SBruno Cardoso Lopes unsigned 520c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo, 5213fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5223fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 523049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo).isImm()); 5243fa98a65SDavid Woodhouse unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); 52556b70de0SBruno Cardoso Lopes return SizeEncoding - 1; 526c85e3ff3SBruno Cardoso Lopes } 527c85e3ff3SBruno Cardoso Lopes 528049e9e4dSAkira Hatanaka // FIXME: should be called getMSBEncoding 529049e9e4dSAkira Hatanaka // 530c85e3ff3SBruno Cardoso Lopes unsigned 531c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo, 5323fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5333fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 534049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo-1).isImm()); 535049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo).isImm()); 5363fa98a65SDavid Woodhouse unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI); 5373fa98a65SDavid Woodhouse unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); 538049e9e4dSAkira Hatanaka 53956b70de0SBruno Cardoso Lopes return Position + Size - 1; 540c85e3ff3SBruno Cardoso Lopes } 541c85e3ff3SBruno Cardoso Lopes 542779c5937SMatheus Almeida unsigned 543779c5937SMatheus Almeida MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo, 5443fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups, 5453fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const { 546779c5937SMatheus Almeida assert(MI.getOperand(OpNo).isImm()); 547779c5937SMatheus Almeida // The immediate is encoded as 'immediate - 1'. 5483fa98a65SDavid Woodhouse return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1; 549779c5937SMatheus Almeida } 550779c5937SMatheus Almeida 551c85e3ff3SBruno Cardoso Lopes #include "MipsGenMCCodeEmitter.inc" 552c85e3ff3SBruno Cardoso Lopes 553