19f610119SJia Liu //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
2750ecec7SAkira Hatanaka //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6750ecec7SAkira Hatanaka //
7750ecec7SAkira Hatanaka //===----------------------------------------------------------------------===//
8750ecec7SAkira Hatanaka //
9750ecec7SAkira Hatanaka // This file implements the MipsMCCodeEmitter class.
10750ecec7SAkira Hatanaka //
11750ecec7SAkira Hatanaka //===----------------------------------------------------------------------===//
129e1450bcSMatheus Almeida
136bda14b3SChandler Carruth #include "MipsMCCodeEmitter.h"
14c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsFixupKinds.h"
15a5da588bSPetar Jovanovic #include "MCTargetDesc/MipsMCExpr.h"
16c85e3ff3SBruno Cardoso Lopes #include "MCTargetDesc/MipsMCTargetDesc.h"
17c85e3ff3SBruno Cardoso Lopes #include "llvm/ADT/APFloat.h"
18926883e1SEugene Zelenko #include "llvm/ADT/APInt.h"
199e1450bcSMatheus Almeida #include "llvm/ADT/SmallVector.h"
205d6faed1SAkira Hatanaka #include "llvm/MC/MCContext.h"
21750ecec7SAkira Hatanaka #include "llvm/MC/MCExpr.h"
22d9903888SChandler Carruth #include "llvm/MC/MCFixup.h"
23750ecec7SAkira Hatanaka #include "llvm/MC/MCInst.h"
24926883e1SEugene Zelenko #include "llvm/MC/MCInstrDesc.h"
25750ecec7SAkira Hatanaka #include "llvm/MC/MCInstrInfo.h"
263de83e40SPete Cooper #include "llvm/MC/MCRegisterInfo.h"
27750ecec7SAkira Hatanaka #include "llvm/MC/MCSubtargetInfo.h"
28926883e1SEugene Zelenko #include "llvm/Support/Casting.h"
29926883e1SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
30750ecec7SAkira Hatanaka #include "llvm/Support/raw_ostream.h"
31926883e1SEugene Zelenko #include <cassert>
32926883e1SEugene Zelenko #include <cstdint>
33926883e1SEugene Zelenko
34926883e1SEugene Zelenko using namespace llvm;
35750ecec7SAkira Hatanaka
3684e68b29SChandler Carruth #define DEBUG_TYPE "mccodeemitter"
3784e68b29SChandler Carruth
38be6a818fSAkira Hatanaka #define GET_INSTRMAP_INFO
39be6a818fSAkira Hatanaka #include "MipsGenInstrInfo.inc"
409e1450bcSMatheus Almeida #undef GET_INSTRMAP_INFO
41be6a818fSAkira Hatanaka
429e1450bcSMatheus Almeida namespace llvm {
43926883e1SEugene Zelenko
createMipsMCCodeEmitterEB(const MCInstrInfo & MCII,MCContext & Ctx)449e1450bcSMatheus Almeida MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
459e1450bcSMatheus Almeida MCContext &Ctx) {
46d2cca113SDavid Woodhouse return new MipsMCCodeEmitter(MCII, Ctx, false);
471ee768dbSAkira Hatanaka }
481ee768dbSAkira Hatanaka
createMipsMCCodeEmitterEL(const MCInstrInfo & MCII,MCContext & Ctx)499e1450bcSMatheus Almeida MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
509e1450bcSMatheus Almeida MCContext &Ctx) {
51d2cca113SDavid Woodhouse return new MipsMCCodeEmitter(MCII, Ctx, true);
52750ecec7SAkira Hatanaka }
53926883e1SEugene Zelenko
54926883e1SEugene Zelenko } // end namespace llvm
55f30f2cceSRafael Espindola
56f30f2cceSRafael Espindola // If the D<shift> instruction has a shift amount that is greater
57f30f2cceSRafael Espindola // than 31 (checked in calling routine), lower it to a D<shift>32 instruction
LowerLargeShift(MCInst & Inst)58f30f2cceSRafael Espindola static void LowerLargeShift(MCInst& Inst) {
59f30f2cceSRafael Espindola assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
60f30f2cceSRafael Espindola assert(Inst.getOperand(2).isImm());
61f30f2cceSRafael Espindola
62f30f2cceSRafael Espindola int64_t Shift = Inst.getOperand(2).getImm();
63f30f2cceSRafael Espindola if (Shift <= 31)
64f30f2cceSRafael Espindola return; // Do nothing
65f30f2cceSRafael Espindola Shift -= 32;
66f30f2cceSRafael Espindola
67f30f2cceSRafael Espindola // saminus32
68f30f2cceSRafael Espindola Inst.getOperand(2).setImm(Shift);
69f30f2cceSRafael Espindola
70f30f2cceSRafael Espindola switch (Inst.getOpcode()) {
71f30f2cceSRafael Espindola default:
72f30f2cceSRafael Espindola // Calling function is not synchronized
73f30f2cceSRafael Espindola llvm_unreachable("Unexpected shift instruction");
74f30f2cceSRafael Espindola case Mips::DSLL:
75f30f2cceSRafael Espindola Inst.setOpcode(Mips::DSLL32);
76f30f2cceSRafael Espindola return;
77f30f2cceSRafael Espindola case Mips::DSRL:
78f30f2cceSRafael Espindola Inst.setOpcode(Mips::DSRL32);
79f30f2cceSRafael Espindola return;
80f30f2cceSRafael Espindola case Mips::DSRA:
81f30f2cceSRafael Espindola Inst.setOpcode(Mips::DSRA32);
82f30f2cceSRafael Espindola return;
836a3fe574SAkira Hatanaka case Mips::DROTR:
846a3fe574SAkira Hatanaka Inst.setOpcode(Mips::DROTR32);
856a3fe574SAkira Hatanaka return;
86f30f2cceSRafael Espindola }
87f30f2cceSRafael Espindola }
88f30f2cceSRafael Espindola
89669d8dd8SSimon Dardis // Fix a bad compact branch encoding for beqc/bnec.
LowerCompactBranch(MCInst & Inst) const90669d8dd8SSimon Dardis void MipsMCCodeEmitter::LowerCompactBranch(MCInst& Inst) const {
91669d8dd8SSimon Dardis // Encoding may be illegal !(rs < rt), but this situation is
92669d8dd8SSimon Dardis // easily fixed.
93669d8dd8SSimon Dardis unsigned RegOp0 = Inst.getOperand(0).getReg();
94669d8dd8SSimon Dardis unsigned RegOp1 = Inst.getOperand(1).getReg();
95669d8dd8SSimon Dardis
96669d8dd8SSimon Dardis unsigned Reg0 = Ctx.getRegisterInfo()->getEncodingValue(RegOp0);
97669d8dd8SSimon Dardis unsigned Reg1 = Ctx.getRegisterInfo()->getEncodingValue(RegOp1);
98669d8dd8SSimon Dardis
9968a204ddSSimon Dardis if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC ||
10068a204ddSSimon Dardis Inst.getOpcode() == Mips::BNEC64 || Inst.getOpcode() == Mips::BEQC64) {
101669d8dd8SSimon Dardis assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
102669d8dd8SSimon Dardis if (Reg0 < Reg1)
103669d8dd8SSimon Dardis return;
104b60833c0SSimon Dardis } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
105b60833c0SSimon Dardis if (Reg0 >= Reg1)
106b60833c0SSimon Dardis return;
107c962c493SHrvoje Varga } else if (Inst.getOpcode() == Mips::BNVC_MMR6 ||
108c962c493SHrvoje Varga Inst.getOpcode() == Mips::BOVC_MMR6) {
109c962c493SHrvoje Varga if (Reg1 >= Reg0)
110c962c493SHrvoje Varga return;
111b60833c0SSimon Dardis } else
112b60833c0SSimon Dardis llvm_unreachable("Cannot rewrite unknown branch!");
113669d8dd8SSimon Dardis
114669d8dd8SSimon Dardis Inst.getOperand(0).setReg(RegOp1);
115669d8dd8SSimon Dardis Inst.getOperand(1).setReg(RegOp0);
116669d8dd8SSimon Dardis }
117669d8dd8SSimon Dardis
isMicroMips(const MCSubtargetInfo & STI) const1189e1450bcSMatheus Almeida bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
119db0712f9SMichael Kuperstein return STI.getFeatureBits()[Mips::FeatureMicroMips];
1209e1450bcSMatheus Almeida }
1219e1450bcSMatheus Almeida
isMips32r6(const MCSubtargetInfo & STI) const122c22555d9SJozef Kolek bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
123db0712f9SMichael Kuperstein return STI.getFeatureBits()[Mips::FeatureMips32r6];
124c22555d9SJozef Kolek }
125c22555d9SJozef Kolek
EmitByte(unsigned char C,raw_ostream & OS) const1269e1450bcSMatheus Almeida void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
1279e1450bcSMatheus Almeida OS << (char)C;
1289e1450bcSMatheus Almeida }
1299e1450bcSMatheus Almeida
emitInstruction(uint64_t Val,unsigned Size,const MCSubtargetInfo & STI,raw_ostream & OS) const130bcd24b2dSFangrui Song void MipsMCCodeEmitter::emitInstruction(uint64_t Val, unsigned Size,
1319e1450bcSMatheus Almeida const MCSubtargetInfo &STI,
1329e1450bcSMatheus Almeida raw_ostream &OS) const {
1339e1450bcSMatheus Almeida // Output the instruction encoding in little endian byte order.
1349e1450bcSMatheus Almeida // Little-endian byte ordering:
1359e1450bcSMatheus Almeida // mips32r2: 4 | 3 | 2 | 1
1369e1450bcSMatheus Almeida // microMIPS: 2 | 1 | 4 | 3
1379e1450bcSMatheus Almeida if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
138bcd24b2dSFangrui Song emitInstruction(Val >> 16, 2, STI, OS);
139bcd24b2dSFangrui Song emitInstruction(Val, 2, STI, OS);
1409e1450bcSMatheus Almeida } else {
1419e1450bcSMatheus Almeida for (unsigned i = 0; i < Size; ++i) {
1429e1450bcSMatheus Almeida unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
1439e1450bcSMatheus Almeida EmitByte((Val >> Shift) & 0xff, OS);
1449e1450bcSMatheus Almeida }
1459e1450bcSMatheus Almeida }
1469e1450bcSMatheus Almeida }
1479e1450bcSMatheus Almeida
14891df21f7SJim Grosbach /// encodeInstruction - Emit the instruction.
1494e07b95dSJack Carter /// Size the instruction with Desc.getSize().
150c85e3ff3SBruno Cardoso Lopes void MipsMCCodeEmitter::
encodeInstruction(const MCInst & MI,raw_ostream & OS,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const15191df21f7SJim Grosbach encodeInstruction(const MCInst &MI, raw_ostream &OS,
1529784cef3SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
1539784cef3SDavid Woodhouse const MCSubtargetInfo &STI) const
154c85e3ff3SBruno Cardoso Lopes {
155aa7aeaa0SJack Carter // Non-pseudo instructions that get changed for direct object
156aa7aeaa0SJack Carter // only based on operand values.
157aa7aeaa0SJack Carter // If this list of instructions get much longer we will move
158aa7aeaa0SJack Carter // the check to a function call. Until then, this is more efficient.
159aa7aeaa0SJack Carter MCInst TmpInst = MI;
160aa7aeaa0SJack Carter switch (MI.getOpcode()) {
161aa7aeaa0SJack Carter // If shift amount is >= 32 it the inst needs to be lowered further
162aa7aeaa0SJack Carter case Mips::DSLL:
163aa7aeaa0SJack Carter case Mips::DSRL:
164aa7aeaa0SJack Carter case Mips::DSRA:
1656a3fe574SAkira Hatanaka case Mips::DROTR:
166f30f2cceSRafael Espindola LowerLargeShift(TmpInst);
167aa7aeaa0SJack Carter break;
168b60833c0SSimon Dardis // Compact branches, enforce encoding restrictions.
169669d8dd8SSimon Dardis case Mips::BEQC:
170669d8dd8SSimon Dardis case Mips::BNEC:
17168a204ddSSimon Dardis case Mips::BEQC64:
17268a204ddSSimon Dardis case Mips::BNEC64:
173b60833c0SSimon Dardis case Mips::BOVC:
174c962c493SHrvoje Varga case Mips::BOVC_MMR6:
175b60833c0SSimon Dardis case Mips::BNVC:
176c962c493SHrvoje Varga case Mips::BNVC_MMR6:
177669d8dd8SSimon Dardis LowerCompactBranch(TmpInst);
178aa7aeaa0SJack Carter }
179aa7aeaa0SJack Carter
18097700977SJack Carter unsigned long N = Fixups.size();
1813fa98a65SDavid Woodhouse uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
182c85e3ff3SBruno Cardoso Lopes
183c85e3ff3SBruno Cardoso Lopes // Check for unimplemented opcodes.
184aa7aeaa0SJack Carter // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
185c85e3ff3SBruno Cardoso Lopes // so we have to special check for them.
186b251cc0dSFangrui Song const unsigned Opcode = TmpInst.getOpcode();
187c7e220f6SJozef Kolek if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
1881ade566cSSimon Dardis (Opcode != Mips::SLL_MM) && (Opcode != Mips::SLL_MMR6) && !Binary)
18991df21f7SJim Grosbach llvm_unreachable("unimplemented opcode in encodeInstruction()");
190c85e3ff3SBruno Cardoso Lopes
191b59a5419SZoran Jovanovic int NewOpcode = -1;
1926ca13eafSJozef Kolek if (isMicroMips(STI)) {
193b59a5419SZoran Jovanovic if (isMips32r6(STI)) {
194b59a5419SZoran Jovanovic NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
195b59a5419SZoran Jovanovic if (NewOpcode == -1)
196b59a5419SZoran Jovanovic NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
197b59a5419SZoran Jovanovic }
198b59a5419SZoran Jovanovic else
199b59a5419SZoran Jovanovic NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
200b59a5419SZoran Jovanovic
2012e386d3dSZoran Jovanovic // Check whether it is Dsp instruction.
2022e386d3dSZoran Jovanovic if (NewOpcode == -1)
2032e386d3dSZoran Jovanovic NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
2042e386d3dSZoran Jovanovic
205be6a818fSAkira Hatanaka if (NewOpcode != -1) {
20697700977SJack Carter if (Fixups.size() > N)
20797700977SJack Carter Fixups.pop_back();
208b59a5419SZoran Jovanovic
209be6a818fSAkira Hatanaka TmpInst.setOpcode (NewOpcode);
2103fa98a65SDavid Woodhouse Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
211be6a818fSAkira Hatanaka }
212852dd83bSSimon Atanasyan
213852dd83bSSimon Atanasyan if (((MI.getOpcode() == Mips::MOVEP_MM) ||
214852dd83bSSimon Atanasyan (MI.getOpcode() == Mips::MOVEP_MMR6))) {
215852dd83bSSimon Atanasyan unsigned RegPair = getMovePRegPairOpValue(MI, 0, Fixups, STI);
216852dd83bSSimon Atanasyan Binary = (Binary & 0xFFFFFC7F) | (RegPair << 7);
217852dd83bSSimon Atanasyan }
218be6a818fSAkira Hatanaka }
219be6a818fSAkira Hatanaka
220aa7aeaa0SJack Carter const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
221c85e3ff3SBruno Cardoso Lopes
2225b5559d3SJack Carter // Get byte count of instruction
2235b5559d3SJack Carter unsigned Size = Desc.getSize();
2245b5559d3SJack Carter if (!Size)
2255b5559d3SJack Carter llvm_unreachable("Desc.getSize() returns 0");
226c85e3ff3SBruno Cardoso Lopes
227bcd24b2dSFangrui Song emitInstruction(Binary, Size, STI, OS);
228c85e3ff3SBruno Cardoso Lopes }
229c85e3ff3SBruno Cardoso Lopes
230c85e3ff3SBruno Cardoso Lopes /// getBranchTargetOpValue - Return binary encoding of the branch
231c85e3ff3SBruno Cardoso Lopes /// target operand. If the machine operand requires relocation,
232c85e3ff3SBruno Cardoso Lopes /// record the relocation and return zero.
233c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
getBranchTargetOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const234c85e3ff3SBruno Cardoso Lopes getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
2353fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
2363fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
237c85e3ff3SBruno Cardoso Lopes const MCOperand &MO = MI.getOperand(OpNo);
23871e6a749SJack Carter
2394f69a0f2SJack Carter // If the destination is an immediate, divide by 4.
2404f69a0f2SJack Carter if (MO.isImm()) return MO.getImm() >> 2;
2414f69a0f2SJack Carter
24271e6a749SJack Carter assert(MO.isExpr() &&
24371e6a749SJack Carter "getBranchTargetOpValue expects only expressions or immediates");
244c85e3ff3SBruno Cardoso Lopes
245b7915a1fSPetar Jovanovic const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
246b7915a1fSPetar Jovanovic MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
247b7915a1fSPetar Jovanovic Fixups.push_back(MCFixup::create(0, FixupExpression,
248c85e3ff3SBruno Cardoso Lopes MCFixupKind(Mips::fixup_Mips_PC16)));
249c85e3ff3SBruno Cardoso Lopes return 0;
250c85e3ff3SBruno Cardoso Lopes }
251c85e3ff3SBruno Cardoso Lopes
2526f09cdfdSHrvoje Varga /// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
2536f09cdfdSHrvoje Varga /// target operand. If the machine operand requires relocation,
2546f09cdfdSHrvoje Varga /// record the relocation and return zero.
2556f09cdfdSHrvoje Varga unsigned MipsMCCodeEmitter::
getBranchTargetOpValue1SImm16(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const2566f09cdfdSHrvoje Varga getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
2576f09cdfdSHrvoje Varga SmallVectorImpl<MCFixup> &Fixups,
2586f09cdfdSHrvoje Varga const MCSubtargetInfo &STI) const {
2596f09cdfdSHrvoje Varga const MCOperand &MO = MI.getOperand(OpNo);
2606f09cdfdSHrvoje Varga
2616f09cdfdSHrvoje Varga // If the destination is an immediate, divide by 2.
2626f09cdfdSHrvoje Varga if (MO.isImm()) return MO.getImm() >> 1;
2636f09cdfdSHrvoje Varga
2646f09cdfdSHrvoje Varga assert(MO.isExpr() &&
2656f09cdfdSHrvoje Varga "getBranchTargetOpValue expects only expressions or immediates");
2666f09cdfdSHrvoje Varga
2676f09cdfdSHrvoje Varga const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
2686f09cdfdSHrvoje Varga MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
2696f09cdfdSHrvoje Varga Fixups.push_back(MCFixup::create(0, FixupExpression,
2706f09cdfdSHrvoje Varga MCFixupKind(Mips::fixup_Mips_PC16)));
2716f09cdfdSHrvoje Varga return 0;
2726f09cdfdSHrvoje Varga }
2736f09cdfdSHrvoje Varga
274c962c493SHrvoje Varga /// getBranchTargetOpValueMMR6 - Return binary encoding of the branch
275c962c493SHrvoje Varga /// target operand. If the machine operand requires relocation,
276c962c493SHrvoje Varga /// record the relocation and return zero.
277c962c493SHrvoje Varga unsigned MipsMCCodeEmitter::
getBranchTargetOpValueMMR6(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const278c962c493SHrvoje Varga getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
279c962c493SHrvoje Varga SmallVectorImpl<MCFixup> &Fixups,
280c962c493SHrvoje Varga const MCSubtargetInfo &STI) const {
281c962c493SHrvoje Varga const MCOperand &MO = MI.getOperand(OpNo);
282c962c493SHrvoje Varga
283c962c493SHrvoje Varga // If the destination is an immediate, divide by 2.
284c962c493SHrvoje Varga if (MO.isImm())
285c962c493SHrvoje Varga return MO.getImm() >> 1;
286c962c493SHrvoje Varga
287c962c493SHrvoje Varga assert(MO.isExpr() &&
288c962c493SHrvoje Varga "getBranchTargetOpValueMMR6 expects only expressions or immediates");
289c962c493SHrvoje Varga
290c962c493SHrvoje Varga const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
291c962c493SHrvoje Varga MO.getExpr(), MCConstantExpr::create(-2, Ctx), Ctx);
292c962c493SHrvoje Varga Fixups.push_back(MCFixup::create(0, FixupExpression,
293c962c493SHrvoje Varga MCFixupKind(Mips::fixup_Mips_PC16)));
294c962c493SHrvoje Varga return 0;
295c962c493SHrvoje Varga }
296c962c493SHrvoje Varga
297f0ed16eaSHrvoje Varga /// getBranchTargetOpValueLsl2MMR6 - Return binary encoding of the branch
298f0ed16eaSHrvoje Varga /// target operand. If the machine operand requires relocation,
299f0ed16eaSHrvoje Varga /// record the relocation and return zero.
300f0ed16eaSHrvoje Varga unsigned MipsMCCodeEmitter::
getBranchTargetOpValueLsl2MMR6(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const301f0ed16eaSHrvoje Varga getBranchTargetOpValueLsl2MMR6(const MCInst &MI, unsigned OpNo,
302f0ed16eaSHrvoje Varga SmallVectorImpl<MCFixup> &Fixups,
303f0ed16eaSHrvoje Varga const MCSubtargetInfo &STI) const {
304f0ed16eaSHrvoje Varga const MCOperand &MO = MI.getOperand(OpNo);
305f0ed16eaSHrvoje Varga
306f0ed16eaSHrvoje Varga // If the destination is an immediate, divide by 4.
307f0ed16eaSHrvoje Varga if (MO.isImm())
308f0ed16eaSHrvoje Varga return MO.getImm() >> 2;
309f0ed16eaSHrvoje Varga
310f0ed16eaSHrvoje Varga assert(MO.isExpr() &&
311f0ed16eaSHrvoje Varga "getBranchTargetOpValueLsl2MMR6 expects only expressions or immediates");
312f0ed16eaSHrvoje Varga
313f0ed16eaSHrvoje Varga const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
314f0ed16eaSHrvoje Varga MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
315f0ed16eaSHrvoje Varga Fixups.push_back(MCFixup::create(0, FixupExpression,
316f0ed16eaSHrvoje Varga MCFixupKind(Mips::fixup_Mips_PC16)));
317f0ed16eaSHrvoje Varga return 0;
318f0ed16eaSHrvoje Varga }
319f0ed16eaSHrvoje Varga
3209761e96bSJozef Kolek /// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
3219761e96bSJozef Kolek /// target operand. If the machine operand requires relocation,
3229761e96bSJozef Kolek /// record the relocation and return zero.
3239761e96bSJozef Kolek unsigned MipsMCCodeEmitter::
getBranchTarget7OpValueMM(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3249761e96bSJozef Kolek getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
3259761e96bSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
3269761e96bSJozef Kolek const MCSubtargetInfo &STI) const {
3279761e96bSJozef Kolek const MCOperand &MO = MI.getOperand(OpNo);
3289761e96bSJozef Kolek
3299761e96bSJozef Kolek // If the destination is an immediate, divide by 2.
3309761e96bSJozef Kolek if (MO.isImm()) return MO.getImm() >> 1;
3319761e96bSJozef Kolek
3329761e96bSJozef Kolek assert(MO.isExpr() &&
3339761e96bSJozef Kolek "getBranchTargetOpValueMM expects only expressions or immediates");
3349761e96bSJozef Kolek
3359761e96bSJozef Kolek const MCExpr *Expr = MO.getExpr();
33663661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, Expr,
3379761e96bSJozef Kolek MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
3389761e96bSJozef Kolek return 0;
3399761e96bSJozef Kolek }
3409761e96bSJozef Kolek
3415cfebddeSJozef Kolek /// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
3425cfebddeSJozef Kolek /// 10-bit branch target operand. If the machine operand requires relocation,
3435cfebddeSJozef Kolek /// record the relocation and return zero.
3445cfebddeSJozef Kolek unsigned MipsMCCodeEmitter::
getBranchTargetOpValueMMPC10(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3455cfebddeSJozef Kolek getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
3465cfebddeSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
3475cfebddeSJozef Kolek const MCSubtargetInfo &STI) const {
3485cfebddeSJozef Kolek const MCOperand &MO = MI.getOperand(OpNo);
3495cfebddeSJozef Kolek
3505cfebddeSJozef Kolek // If the destination is an immediate, divide by 2.
3515cfebddeSJozef Kolek if (MO.isImm()) return MO.getImm() >> 1;
3525cfebddeSJozef Kolek
3535cfebddeSJozef Kolek assert(MO.isExpr() &&
3545cfebddeSJozef Kolek "getBranchTargetOpValuePC10 expects only expressions or immediates");
3555cfebddeSJozef Kolek
3565cfebddeSJozef Kolek const MCExpr *Expr = MO.getExpr();
35763661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, Expr,
3585cfebddeSJozef Kolek MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
3595cfebddeSJozef Kolek return 0;
3605cfebddeSJozef Kolek }
3615cfebddeSJozef Kolek
3628a80aa76SZoran Jovanovic /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
3638a80aa76SZoran Jovanovic /// target operand. If the machine operand requires relocation,
3648a80aa76SZoran Jovanovic /// record the relocation and return zero.
3658a80aa76SZoran Jovanovic unsigned MipsMCCodeEmitter::
getBranchTargetOpValueMM(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3668a80aa76SZoran Jovanovic getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
3673fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
3683fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
3698a80aa76SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
3708a80aa76SZoran Jovanovic
3718a80aa76SZoran Jovanovic // If the destination is an immediate, divide by 2.
3728a80aa76SZoran Jovanovic if (MO.isImm()) return MO.getImm() >> 1;
3738a80aa76SZoran Jovanovic
3748a80aa76SZoran Jovanovic assert(MO.isExpr() &&
3758a80aa76SZoran Jovanovic "getBranchTargetOpValueMM expects only expressions or immediates");
3768a80aa76SZoran Jovanovic
3778a80aa76SZoran Jovanovic const MCExpr *Expr = MO.getExpr();
37863661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, Expr,
3798a80aa76SZoran Jovanovic MCFixupKind(Mips::
3808a80aa76SZoran Jovanovic fixup_MICROMIPS_PC16_S1)));
3818a80aa76SZoran Jovanovic return 0;
3828a80aa76SZoran Jovanovic }
3838a80aa76SZoran Jovanovic
3843c8869dcSZoran Jovanovic /// getBranchTarget21OpValue - Return binary encoding of the branch
3853c8869dcSZoran Jovanovic /// target operand. If the machine operand requires relocation,
3863c8869dcSZoran Jovanovic /// record the relocation and return zero.
3873c8869dcSZoran Jovanovic unsigned MipsMCCodeEmitter::
getBranchTarget21OpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const3883c8869dcSZoran Jovanovic getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
3893c8869dcSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
3903c8869dcSZoran Jovanovic const MCSubtargetInfo &STI) const {
3913c8869dcSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
3923c8869dcSZoran Jovanovic
3933c8869dcSZoran Jovanovic // If the destination is an immediate, divide by 4.
3943c8869dcSZoran Jovanovic if (MO.isImm()) return MO.getImm() >> 2;
3953c8869dcSZoran Jovanovic
3963c8869dcSZoran Jovanovic assert(MO.isExpr() &&
3973c8869dcSZoran Jovanovic "getBranchTarget21OpValue expects only expressions or immediates");
3983c8869dcSZoran Jovanovic
399b7915a1fSPetar Jovanovic const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
400b7915a1fSPetar Jovanovic MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
401b7915a1fSPetar Jovanovic Fixups.push_back(MCFixup::create(0, FixupExpression,
40210e06da0SZoran Jovanovic MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
4033c8869dcSZoran Jovanovic return 0;
4043c8869dcSZoran Jovanovic }
4053c8869dcSZoran Jovanovic
40684e4d59eSZoran Jovanovic /// getBranchTarget21OpValueMM - Return binary encoding of the branch
40784e4d59eSZoran Jovanovic /// target operand for microMIPS. If the machine operand requires
40884e4d59eSZoran Jovanovic /// relocation, record the relocation and return zero.
40984e4d59eSZoran Jovanovic unsigned MipsMCCodeEmitter::
getBranchTarget21OpValueMM(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const41084e4d59eSZoran Jovanovic getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
41184e4d59eSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
41284e4d59eSZoran Jovanovic const MCSubtargetInfo &STI) const {
41384e4d59eSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
41484e4d59eSZoran Jovanovic
415f0ed16eaSHrvoje Varga // If the destination is an immediate, divide by 4.
416f0ed16eaSHrvoje Varga if (MO.isImm()) return MO.getImm() >> 2;
41784e4d59eSZoran Jovanovic
41884e4d59eSZoran Jovanovic assert(MO.isExpr() &&
41984e4d59eSZoran Jovanovic "getBranchTarget21OpValueMM expects only expressions or immediates");
42084e4d59eSZoran Jovanovic
4215f94cedeSZoran Jovanovic const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
4225f94cedeSZoran Jovanovic MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
4235f94cedeSZoran Jovanovic Fixups.push_back(MCFixup::create(0, FixupExpression,
4245f94cedeSZoran Jovanovic MCFixupKind(Mips::fixup_MICROMIPS_PC21_S1)));
42584e4d59eSZoran Jovanovic return 0;
42684e4d59eSZoran Jovanovic }
42784e4d59eSZoran Jovanovic
4283c8869dcSZoran Jovanovic /// getBranchTarget26OpValue - Return binary encoding of the branch
4293c8869dcSZoran Jovanovic /// target operand. If the machine operand requires relocation,
4303c8869dcSZoran Jovanovic /// record the relocation and return zero.
4313c8869dcSZoran Jovanovic unsigned MipsMCCodeEmitter::
getBranchTarget26OpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const4323c8869dcSZoran Jovanovic getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
4333c8869dcSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
4343c8869dcSZoran Jovanovic const MCSubtargetInfo &STI) const {
4353c8869dcSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
4363c8869dcSZoran Jovanovic
4373c8869dcSZoran Jovanovic // If the destination is an immediate, divide by 4.
4383c8869dcSZoran Jovanovic if (MO.isImm()) return MO.getImm() >> 2;
4393c8869dcSZoran Jovanovic
4403c8869dcSZoran Jovanovic assert(MO.isExpr() &&
4413c8869dcSZoran Jovanovic "getBranchTarget26OpValue expects only expressions or immediates");
4423c8869dcSZoran Jovanovic
443b7915a1fSPetar Jovanovic const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
444b7915a1fSPetar Jovanovic MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
445b7915a1fSPetar Jovanovic Fixups.push_back(MCFixup::create(0, FixupExpression,
44610e06da0SZoran Jovanovic MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
4473c8869dcSZoran Jovanovic return 0;
4483c8869dcSZoran Jovanovic }
4493c8869dcSZoran Jovanovic
450a887b361SZoran Jovanovic /// getBranchTarget26OpValueMM - Return binary encoding of the branch
451a887b361SZoran Jovanovic /// target operand. If the machine operand requires relocation,
452a887b361SZoran Jovanovic /// record the relocation and return zero.
getBranchTarget26OpValueMM(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const453a887b361SZoran Jovanovic unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
454a887b361SZoran Jovanovic const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
455a887b361SZoran Jovanovic const MCSubtargetInfo &STI) const {
456a887b361SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
457a887b361SZoran Jovanovic
458a887b361SZoran Jovanovic // If the destination is an immediate, divide by 2.
459a887b361SZoran Jovanovic if (MO.isImm())
460a887b361SZoran Jovanovic return MO.getImm() >> 1;
461a887b361SZoran Jovanovic
46202b70030SZoran Jovanovic assert(MO.isExpr() &&
46302b70030SZoran Jovanovic "getBranchTarget26OpValueMM expects only expressions or immediates");
46402b70030SZoran Jovanovic
46502b70030SZoran Jovanovic const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
46602b70030SZoran Jovanovic MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
46702b70030SZoran Jovanovic Fixups.push_back(MCFixup::create(0, FixupExpression,
46802b70030SZoran Jovanovic MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
469a887b361SZoran Jovanovic return 0;
470a887b361SZoran Jovanovic }
471a887b361SZoran Jovanovic
47252c56b93SZoran Jovanovic /// getJumpOffset16OpValue - Return binary encoding of the jump
47352c56b93SZoran Jovanovic /// target operand. If the machine operand requires relocation,
47452c56b93SZoran Jovanovic /// record the relocation and return zero.
47552c56b93SZoran Jovanovic unsigned MipsMCCodeEmitter::
getJumpOffset16OpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const47652c56b93SZoran Jovanovic getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
47752c56b93SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
47852c56b93SZoran Jovanovic const MCSubtargetInfo &STI) const {
47952c56b93SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
48052c56b93SZoran Jovanovic
48152c56b93SZoran Jovanovic if (MO.isImm()) return MO.getImm();
48252c56b93SZoran Jovanovic
48352c56b93SZoran Jovanovic assert(MO.isExpr() &&
48452c56b93SZoran Jovanovic "getJumpOffset16OpValue expects only expressions or an immediate");
48552c56b93SZoran Jovanovic
486f6551ddfSSimon Atanasyan const MCExpr *Expr = MO.getExpr();
487f6551ddfSSimon Atanasyan Mips::Fixups FixupKind =
488f6551ddfSSimon Atanasyan isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 : Mips::fixup_Mips_LO16;
489f6551ddfSSimon Atanasyan Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
49052c56b93SZoran Jovanovic return 0;
49152c56b93SZoran Jovanovic }
49252c56b93SZoran Jovanovic
493c85e3ff3SBruno Cardoso Lopes /// getJumpTargetOpValue - Return binary encoding of the jump
494c85e3ff3SBruno Cardoso Lopes /// target operand. If the machine operand requires relocation,
495c85e3ff3SBruno Cardoso Lopes /// record the relocation and return zero.
496c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
getJumpTargetOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const497c85e3ff3SBruno Cardoso Lopes getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
4983fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
4993fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
500c85e3ff3SBruno Cardoso Lopes const MCOperand &MO = MI.getOperand(OpNo);
5014f69a0f2SJack Carter // If the destination is an immediate, divide by 4.
5024f69a0f2SJack Carter if (MO.isImm()) return MO.getImm()>>2;
5034f69a0f2SJack Carter
50471e6a749SJack Carter assert(MO.isExpr() &&
50571e6a749SJack Carter "getJumpTargetOpValue expects only expressions or an immediate");
506c85e3ff3SBruno Cardoso Lopes
507c85e3ff3SBruno Cardoso Lopes const MCExpr *Expr = MO.getExpr();
50863661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, Expr,
509c85e3ff3SBruno Cardoso Lopes MCFixupKind(Mips::fixup_Mips_26)));
510c85e3ff3SBruno Cardoso Lopes return 0;
511c85e3ff3SBruno Cardoso Lopes }
512c85e3ff3SBruno Cardoso Lopes
513c85e3ff3SBruno Cardoso Lopes unsigned MipsMCCodeEmitter::
getJumpTargetOpValueMM(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const514507e084aSZoran Jovanovic getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
5153fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
5163fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
517507e084aSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
518507e084aSZoran Jovanovic // If the destination is an immediate, divide by 2.
519507e084aSZoran Jovanovic if (MO.isImm()) return MO.getImm() >> 1;
520507e084aSZoran Jovanovic
521507e084aSZoran Jovanovic assert(MO.isExpr() &&
522507e084aSZoran Jovanovic "getJumpTargetOpValueMM expects only expressions or an immediate");
523507e084aSZoran Jovanovic
524507e084aSZoran Jovanovic const MCExpr *Expr = MO.getExpr();
52563661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, Expr,
526507e084aSZoran Jovanovic MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
527507e084aSZoran Jovanovic return 0;
528507e084aSZoran Jovanovic }
529507e084aSZoran Jovanovic
530507e084aSZoran Jovanovic unsigned MipsMCCodeEmitter::
getUImm5Lsl2Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const531c74e3eb9SZoran Jovanovic getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
532c74e3eb9SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
533c74e3eb9SZoran Jovanovic const MCSubtargetInfo &STI) const {
534c74e3eb9SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
535c74e3eb9SZoran Jovanovic if (MO.isImm()) {
536c74e3eb9SZoran Jovanovic // The immediate is encoded as 'immediate << 2'.
537c74e3eb9SZoran Jovanovic unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
538c74e3eb9SZoran Jovanovic assert((Res & 3) == 0);
539c74e3eb9SZoran Jovanovic return Res >> 2;
540c74e3eb9SZoran Jovanovic }
541c74e3eb9SZoran Jovanovic
542c74e3eb9SZoran Jovanovic assert(MO.isExpr() &&
543c74e3eb9SZoran Jovanovic "getUImm5Lsl2Encoding expects only expressions or an immediate");
544c74e3eb9SZoran Jovanovic
545c74e3eb9SZoran Jovanovic return 0;
546c74e3eb9SZoran Jovanovic }
547c74e3eb9SZoran Jovanovic
548c74e3eb9SZoran Jovanovic unsigned MipsMCCodeEmitter::
getSImm3Lsa2Value(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const549bac3619bSZoran Jovanovic getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
550bac3619bSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
551bac3619bSZoran Jovanovic const MCSubtargetInfo &STI) const {
552bac3619bSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
553bac3619bSZoran Jovanovic if (MO.isImm()) {
554bac3619bSZoran Jovanovic int Value = MO.getImm();
555bac3619bSZoran Jovanovic return Value >> 2;
556bac3619bSZoran Jovanovic }
557bac3619bSZoran Jovanovic
558bac3619bSZoran Jovanovic return 0;
559bac3619bSZoran Jovanovic }
560bac3619bSZoran Jovanovic
561bac3619bSZoran Jovanovic unsigned MipsMCCodeEmitter::
getUImm6Lsl2Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const56242b84443SZoran Jovanovic getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
56342b84443SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
56442b84443SZoran Jovanovic const MCSubtargetInfo &STI) const {
56542b84443SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
56642b84443SZoran Jovanovic if (MO.isImm()) {
56742b84443SZoran Jovanovic unsigned Value = MO.getImm();
56842b84443SZoran Jovanovic return Value >> 2;
56942b84443SZoran Jovanovic }
57042b84443SZoran Jovanovic
57142b84443SZoran Jovanovic return 0;
57242b84443SZoran Jovanovic }
57342b84443SZoran Jovanovic
57442b84443SZoran Jovanovic unsigned MipsMCCodeEmitter::
getSImm9AddiuspValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const57598bd58caSZoran Jovanovic getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
57698bd58caSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
57798bd58caSZoran Jovanovic const MCSubtargetInfo &STI) const {
57898bd58caSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
57998bd58caSZoran Jovanovic if (MO.isImm()) {
58098bd58caSZoran Jovanovic unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
58198bd58caSZoran Jovanovic return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
58298bd58caSZoran Jovanovic }
58398bd58caSZoran Jovanovic
58498bd58caSZoran Jovanovic return 0;
58598bd58caSZoran Jovanovic }
58698bd58caSZoran Jovanovic
58798bd58caSZoran Jovanovic unsigned MipsMCCodeEmitter::
getExprOpValue(const MCExpr * Expr,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const5883fa98a65SDavid Woodhouse getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
5893fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
590b5cf5909SJack Carter int64_t Res;
591049e9e4dSAkira Hatanaka
59213760bd1SJim Grosbach if (Expr->evaluateAsAbsolute(Res))
593b5cf5909SJack Carter return Res;
594fe384a2cSAkira Hatanaka
595fe384a2cSAkira Hatanaka MCExpr::ExprKind Kind = Expr->getKind();
596b5cf5909SJack Carter if (Kind == MCExpr::Constant) {
597b5cf5909SJack Carter return cast<MCConstantExpr>(Expr)->getValue();
598b5cf5909SJack Carter }
599fe384a2cSAkira Hatanaka
600fe384a2cSAkira Hatanaka if (Kind == MCExpr::Binary) {
6010bd82a96SSimon Atanasyan unsigned Res =
6020bd82a96SSimon Atanasyan getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
6033fa98a65SDavid Woodhouse Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
604b5cf5909SJack Carter return Res;
605fe384a2cSAkira Hatanaka }
606a5da588bSPetar Jovanovic
607a5da588bSPetar Jovanovic if (Kind == MCExpr::Target) {
608a5da588bSPetar Jovanovic const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
609a5da588bSPetar Jovanovic
610a5da588bSPetar Jovanovic Mips::Fixups FixupKind = Mips::Fixups(0);
611a5da588bSPetar Jovanovic switch (MipsExpr->getKind()) {
612fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_None:
613fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_Special:
614fe98b2f5SDaniel Sanders llvm_unreachable("Unhandled fixup kind!");
615fe98b2f5SDaniel Sanders break;
616f76884b0SSimon Atanasyan case MipsMCExpr::MEK_DTPREL:
617b6d3c50aSSimon Atanasyan // MEK_DTPREL is used for marking TLS DIEExpr only
618b6d3c50aSSimon Atanasyan // and contains a regular sub-expression.
619b6d3c50aSSimon Atanasyan return getExprOpValue(MipsExpr->getSubExpr(), Fixups, STI);
620fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_CALL_HI16:
621fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_Mips_CALL_HI16;
622fe98b2f5SDaniel Sanders break;
623fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_CALL_LO16:
624fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_Mips_CALL_LO16;
625fe98b2f5SDaniel Sanders break;
626fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_DTPREL_HI:
627fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
628fe98b2f5SDaniel Sanders : Mips::fixup_Mips_DTPREL_HI;
629fe98b2f5SDaniel Sanders break;
630fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_DTPREL_LO:
631fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
632fe98b2f5SDaniel Sanders : Mips::fixup_Mips_DTPREL_LO;
633fe98b2f5SDaniel Sanders break;
634fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOTTPREL:
6353979f438SSimon Atanasyan FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOTTPREL
6363979f438SSimon Atanasyan : Mips::fixup_Mips_GOTTPREL;
637fe98b2f5SDaniel Sanders break;
638fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT:
639fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
640fe98b2f5SDaniel Sanders : Mips::fixup_Mips_GOT;
641fe98b2f5SDaniel Sanders break;
642fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_CALL:
643fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
644fe98b2f5SDaniel Sanders : Mips::fixup_Mips_CALL16;
645fe98b2f5SDaniel Sanders break;
646fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_DISP:
647fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
648fe98b2f5SDaniel Sanders : Mips::fixup_Mips_GOT_DISP;
649fe98b2f5SDaniel Sanders break;
650fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_HI16:
651fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_Mips_GOT_HI16;
652fe98b2f5SDaniel Sanders break;
653fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_LO16:
654fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_Mips_GOT_LO16;
655fe98b2f5SDaniel Sanders break;
656fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_PAGE:
657fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
658fe98b2f5SDaniel Sanders : Mips::fixup_Mips_GOT_PAGE;
659fe98b2f5SDaniel Sanders break;
660fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GOT_OFST:
661fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
662fe98b2f5SDaniel Sanders : Mips::fixup_Mips_GOT_OFST;
663fe98b2f5SDaniel Sanders break;
664fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_GPREL:
665fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_Mips_GPREL16;
666fe98b2f5SDaniel Sanders break;
667926883e1SEugene Zelenko case MipsMCExpr::MEK_LO:
668fe98b2f5SDaniel Sanders // Check for %lo(%neg(%gp_rel(X)))
669a1d69f9eSSimon Atanasyan if (MipsExpr->isGpOff())
670a1d69f9eSSimon Atanasyan FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_LO
671a1d69f9eSSimon Atanasyan : Mips::fixup_Mips_GPOFF_LO;
672a1d69f9eSSimon Atanasyan else
673fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
674fe98b2f5SDaniel Sanders : Mips::fixup_Mips_LO16;
675fe98b2f5SDaniel Sanders break;
676fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_HIGHEST:
6776be87bceSSimon Atanasyan FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHEST
6786be87bceSSimon Atanasyan : Mips::fixup_Mips_HIGHEST;
67906c47803SSasa Stankovic break;
680fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_HIGHER:
6816be87bceSSimon Atanasyan FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HIGHER
6826be87bceSSimon Atanasyan : Mips::fixup_Mips_HIGHER;
68306c47803SSasa Stankovic break;
684fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_HI:
685fe98b2f5SDaniel Sanders // Check for %hi(%neg(%gp_rel(X)))
686a1d69f9eSSimon Atanasyan if (MipsExpr->isGpOff())
687a1d69f9eSSimon Atanasyan FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GPOFF_HI
688a1d69f9eSSimon Atanasyan : Mips::fixup_Mips_GPOFF_HI;
689a1d69f9eSSimon Atanasyan else
690a5da588bSPetar Jovanovic FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
691a5da588bSPetar Jovanovic : Mips::fixup_Mips_HI16;
692a5da588bSPetar Jovanovic break;
693fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_PCREL_HI16:
694fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_MIPS_PCHI16;
695fe98b2f5SDaniel Sanders break;
696fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_PCREL_LO16:
697fe98b2f5SDaniel Sanders FixupKind = Mips::fixup_MIPS_PCLO16;
698fe98b2f5SDaniel Sanders break;
699fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_TLSGD:
700fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
701fe98b2f5SDaniel Sanders : Mips::fixup_Mips_TLSGD;
702fe98b2f5SDaniel Sanders break;
703fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_TLSLDM:
704fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
705fe98b2f5SDaniel Sanders : Mips::fixup_Mips_TLSLDM;
706fe98b2f5SDaniel Sanders break;
707fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_TPREL_HI:
708fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
709fe98b2f5SDaniel Sanders : Mips::fixup_Mips_TPREL_HI;
710fe98b2f5SDaniel Sanders break;
711fe98b2f5SDaniel Sanders case MipsMCExpr::MEK_TPREL_LO:
712fe98b2f5SDaniel Sanders FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
713fe98b2f5SDaniel Sanders : Mips::fixup_Mips_TPREL_LO;
714a5da588bSPetar Jovanovic break;
7153feeb9c8SDaniel Sanders case MipsMCExpr::MEK_NEG:
7163feeb9c8SDaniel Sanders FixupKind =
7173feeb9c8SDaniel Sanders isMicroMips(STI) ? Mips::fixup_MICROMIPS_SUB : Mips::fixup_Mips_SUB;
7183feeb9c8SDaniel Sanders break;
719a5da588bSPetar Jovanovic }
72063661f8dSJim Grosbach Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
721a5da588bSPetar Jovanovic return 0;
722a5da588bSPetar Jovanovic }
723a5da588bSPetar Jovanovic
724dd48c57dSWang Rui if (Kind == MCExpr::SymbolRef)
725dd48c57dSWang Rui Ctx.reportError(Expr->getLoc(), "expected an immediate");
726b5cf5909SJack Carter return 0;
727b5cf5909SJack Carter }
728b5cf5909SJack Carter
729b5cf5909SJack Carter /// getMachineOpValue - Return binary encoding of operand. If the machine
730b5cf5909SJack Carter /// operand requires relocation, record the relocation and return zero.
731b5cf5909SJack Carter unsigned MipsMCCodeEmitter::
getMachineOpValue(const MCInst & MI,const MCOperand & MO,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const732b5cf5909SJack Carter getMachineOpValue(const MCInst &MI, const MCOperand &MO,
7333fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
7343fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
735b5cf5909SJack Carter if (MO.isReg()) {
736b5cf5909SJack Carter unsigned Reg = MO.getReg();
737bc07a890SBill Wendling unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
738b5cf5909SJack Carter return RegNo;
739b5cf5909SJack Carter } else if (MO.isImm()) {
740b5cf5909SJack Carter return static_cast<unsigned>(MO.getImm());
741*698c6b0aSDan Gohman } else if (MO.isDFPImm()) {
742*698c6b0aSDan Gohman return static_cast<unsigned>(bit_cast<double>(MO.getDFPImm()));
743b5cf5909SJack Carter }
744b5cf5909SJack Carter // MO must be an Expr.
745b5cf5909SJack Carter assert(MO.isExpr());
7463fa98a65SDavid Woodhouse return getExprOpValue(MO.getExpr(),Fixups, STI);
747b5cf5909SJack Carter }
748c85e3ff3SBruno Cardoso Lopes
749dc0602a2SDaniel Sanders /// Return binary encoding of memory related operand.
750c85e3ff3SBruno Cardoso Lopes /// If the offset operand requires relocation, record the relocation.
751dc0602a2SDaniel Sanders template <unsigned ShiftAmount>
getMemEncoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const752dc0602a2SDaniel Sanders unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
7533fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
7543fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
755c85e3ff3SBruno Cardoso Lopes // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
756c85e3ff3SBruno Cardoso Lopes assert(MI.getOperand(OpNo).isReg());
7570bd82a96SSimon Atanasyan unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI)
7580bd82a96SSimon Atanasyan << 16;
7593fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
760c85e3ff3SBruno Cardoso Lopes
761dc0602a2SDaniel Sanders // Apply the scale factor if there is one.
762dc0602a2SDaniel Sanders OffBits >>= ShiftAmount;
763dc0602a2SDaniel Sanders
764c85e3ff3SBruno Cardoso Lopes return (OffBits & 0xFFFF) | RegBits;
765c85e3ff3SBruno Cardoso Lopes }
766c85e3ff3SBruno Cardoso Lopes
76797700977SJack Carter unsigned MipsMCCodeEmitter::
getMemEncodingMMImm4(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const768e8c9d1eaSJozef Kolek getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
769e8c9d1eaSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
770e8c9d1eaSJozef Kolek const MCSubtargetInfo &STI) const {
771e8c9d1eaSJozef Kolek // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
772e8c9d1eaSJozef Kolek assert(MI.getOperand(OpNo).isReg());
773e8c9d1eaSJozef Kolek unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
774e8c9d1eaSJozef Kolek Fixups, STI) << 4;
775e8c9d1eaSJozef Kolek unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
776e8c9d1eaSJozef Kolek Fixups, STI);
777e8c9d1eaSJozef Kolek
778e8c9d1eaSJozef Kolek return (OffBits & 0xF) | RegBits;
779e8c9d1eaSJozef Kolek }
780e8c9d1eaSJozef Kolek
781e8c9d1eaSJozef Kolek unsigned MipsMCCodeEmitter::
getMemEncodingMMImm4Lsl1(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const782e8c9d1eaSJozef Kolek getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
783e8c9d1eaSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
784e8c9d1eaSJozef Kolek const MCSubtargetInfo &STI) const {
785e8c9d1eaSJozef Kolek // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
786e8c9d1eaSJozef Kolek assert(MI.getOperand(OpNo).isReg());
787e8c9d1eaSJozef Kolek unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
788e8c9d1eaSJozef Kolek Fixups, STI) << 4;
789e8c9d1eaSJozef Kolek unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
790e8c9d1eaSJozef Kolek Fixups, STI) >> 1;
791e8c9d1eaSJozef Kolek
792e8c9d1eaSJozef Kolek return (OffBits & 0xF) | RegBits;
793e8c9d1eaSJozef Kolek }
794e8c9d1eaSJozef Kolek
795e8c9d1eaSJozef Kolek unsigned MipsMCCodeEmitter::
getMemEncodingMMImm4Lsl2(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const796e8c9d1eaSJozef Kolek getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
797e8c9d1eaSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
798e8c9d1eaSJozef Kolek const MCSubtargetInfo &STI) const {
799e8c9d1eaSJozef Kolek // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
800e8c9d1eaSJozef Kolek assert(MI.getOperand(OpNo).isReg());
801e8c9d1eaSJozef Kolek unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
802e8c9d1eaSJozef Kolek Fixups, STI) << 4;
803e8c9d1eaSJozef Kolek unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
804e8c9d1eaSJozef Kolek Fixups, STI) >> 2;
805e8c9d1eaSJozef Kolek
806e8c9d1eaSJozef Kolek return (OffBits & 0xF) | RegBits;
807e8c9d1eaSJozef Kolek }
808e8c9d1eaSJozef Kolek
809e8c9d1eaSJozef Kolek unsigned MipsMCCodeEmitter::
getMemEncodingMMSPImm5Lsl2(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const81012c6982bSJozef Kolek getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
81112c6982bSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
81212c6982bSJozef Kolek const MCSubtargetInfo &STI) const {
81312c6982bSJozef Kolek // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
81412c6982bSJozef Kolek assert(MI.getOperand(OpNo).isReg() &&
81568be5f21SZoran Jovanovic (MI.getOperand(OpNo).getReg() == Mips::SP ||
81668be5f21SZoran Jovanovic MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
81712c6982bSJozef Kolek "Unexpected base register!");
81812c6982bSJozef Kolek unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
81912c6982bSJozef Kolek Fixups, STI) >> 2;
82012c6982bSJozef Kolek
82112c6982bSJozef Kolek return OffBits & 0x1F;
82212c6982bSJozef Kolek }
82312c6982bSJozef Kolek
82412c6982bSJozef Kolek unsigned MipsMCCodeEmitter::
getMemEncodingMMGPImm7Lsl2(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const825e10a02ecSJozef Kolek getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
826e10a02ecSJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
827e10a02ecSJozef Kolek const MCSubtargetInfo &STI) const {
828e10a02ecSJozef Kolek // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
829e10a02ecSJozef Kolek assert(MI.getOperand(OpNo).isReg() &&
830e10a02ecSJozef Kolek MI.getOperand(OpNo).getReg() == Mips::GP &&
831e10a02ecSJozef Kolek "Unexpected base register!");
832e10a02ecSJozef Kolek
833e10a02ecSJozef Kolek unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
834e10a02ecSJozef Kolek Fixups, STI) >> 2;
835e10a02ecSJozef Kolek
836e10a02ecSJozef Kolek return OffBits & 0x7F;
837e10a02ecSJozef Kolek }
838e10a02ecSJozef Kolek
8399eaa30d2SZoran Jovanovic unsigned MipsMCCodeEmitter::
getMemEncodingMMImm9(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const8409eaa30d2SZoran Jovanovic getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
8419eaa30d2SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
8429eaa30d2SZoran Jovanovic const MCSubtargetInfo &STI) const {
8439eaa30d2SZoran Jovanovic // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
8449eaa30d2SZoran Jovanovic assert(MI.getOperand(OpNo).isReg());
8459eaa30d2SZoran Jovanovic unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
8469eaa30d2SZoran Jovanovic STI) << 16;
8470bd82a96SSimon Atanasyan unsigned OffBits =
8480bd82a96SSimon Atanasyan getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
8499eaa30d2SZoran Jovanovic
8509eaa30d2SZoran Jovanovic return (OffBits & 0x1FF) | RegBits;
8519eaa30d2SZoran Jovanovic }
8529eaa30d2SZoran Jovanovic
853e10a02ecSJozef Kolek unsigned MipsMCCodeEmitter::
getMemEncodingMMImm11(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const854cba9f80bSZlatko Buljan getMemEncodingMMImm11(const MCInst &MI, unsigned OpNo,
855cba9f80bSZlatko Buljan SmallVectorImpl<MCFixup> &Fixups,
856cba9f80bSZlatko Buljan const MCSubtargetInfo &STI) const {
857cba9f80bSZlatko Buljan // Base register is encoded in bits 20-16, offset is encoded in bits 10-0.
858cba9f80bSZlatko Buljan assert(MI.getOperand(OpNo).isReg());
859cba9f80bSZlatko Buljan unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
860cba9f80bSZlatko Buljan STI) << 16;
861cba9f80bSZlatko Buljan unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
862cba9f80bSZlatko Buljan
863cba9f80bSZlatko Buljan return (OffBits & 0x07FF) | RegBits;
864cba9f80bSZlatko Buljan }
865cba9f80bSZlatko Buljan
866cba9f80bSZlatko Buljan unsigned MipsMCCodeEmitter::
getMemEncodingMMImm12(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const86797700977SJack Carter getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
8683fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
8693fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
870a4c4b5fcSZoran Jovanovic // opNum can be invalid if instruction had reglist as operand.
871a4c4b5fcSZoran Jovanovic // MemOperand is always last operand of instruction (base + offset).
872a4c4b5fcSZoran Jovanovic switch (MI.getOpcode()) {
873a4c4b5fcSZoran Jovanovic default:
874a4c4b5fcSZoran Jovanovic break;
875a4c4b5fcSZoran Jovanovic case Mips::SWM32_MM:
876a4c4b5fcSZoran Jovanovic case Mips::LWM32_MM:
877a4c4b5fcSZoran Jovanovic OpNo = MI.getNumOperands() - 2;
878a4c4b5fcSZoran Jovanovic break;
879a4c4b5fcSZoran Jovanovic }
880a4c4b5fcSZoran Jovanovic
88197700977SJack Carter // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
88297700977SJack Carter assert(MI.getOperand(OpNo).isReg());
8830bd82a96SSimon Atanasyan unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI)
8840bd82a96SSimon Atanasyan << 16;
8853fa98a65SDavid Woodhouse unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
88697700977SJack Carter
88797700977SJack Carter return (OffBits & 0x0FFF) | RegBits;
8883c88fbd3SHrvoje Varga }
8893c88fbd3SHrvoje Varga
8903c88fbd3SHrvoje Varga unsigned MipsMCCodeEmitter::
getMemEncodingMMImm16(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const8913c88fbd3SHrvoje Varga getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
8923c88fbd3SHrvoje Varga SmallVectorImpl<MCFixup> &Fixups,
8933c88fbd3SHrvoje Varga const MCSubtargetInfo &STI) const {
8943c88fbd3SHrvoje Varga // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
8953c88fbd3SHrvoje Varga assert(MI.getOperand(OpNo).isReg());
8963c88fbd3SHrvoje Varga unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
8973c88fbd3SHrvoje Varga STI) << 16;
8983c88fbd3SHrvoje Varga unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
8993c88fbd3SHrvoje Varga
9003c88fbd3SHrvoje Varga return (OffBits & 0xFFFF) | RegBits;
90197700977SJack Carter }
90297700977SJack Carter
903f9a02500SZoran Jovanovic unsigned MipsMCCodeEmitter::
getMemEncodingMMImm4sp(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const904f9a02500SZoran Jovanovic getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
905f9a02500SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
906f9a02500SZoran Jovanovic const MCSubtargetInfo &STI) const {
907f9a02500SZoran Jovanovic // opNum can be invalid if instruction had reglist as operand
908f9a02500SZoran Jovanovic // MemOperand is always last operand of instruction (base + offset)
909f9a02500SZoran Jovanovic switch (MI.getOpcode()) {
910f9a02500SZoran Jovanovic default:
911f9a02500SZoran Jovanovic break;
912f9a02500SZoran Jovanovic case Mips::SWM16_MM:
913797c2aecSZlatko Buljan case Mips::SWM16_MMR6:
914f9a02500SZoran Jovanovic case Mips::LWM16_MM:
915797c2aecSZlatko Buljan case Mips::LWM16_MMR6:
916f9a02500SZoran Jovanovic OpNo = MI.getNumOperands() - 2;
917f9a02500SZoran Jovanovic break;
918f9a02500SZoran Jovanovic }
919f9a02500SZoran Jovanovic
920f9a02500SZoran Jovanovic // Offset is encoded in bits 4-0.
921f9a02500SZoran Jovanovic assert(MI.getOperand(OpNo).isReg());
922f9a02500SZoran Jovanovic // Base register is always SP - thus it is not encoded.
923f9a02500SZoran Jovanovic assert(MI.getOperand(OpNo+1).isImm());
924f9a02500SZoran Jovanovic unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
925f9a02500SZoran Jovanovic
926f9a02500SZoran Jovanovic return ((OffBits >> 2) & 0x0F);
927f9a02500SZoran Jovanovic }
928f9a02500SZoran Jovanovic
929049e9e4dSAkira Hatanaka // FIXME: should be called getMSBEncoding
930049e9e4dSAkira Hatanaka //
931c85e3ff3SBruno Cardoso Lopes unsigned
getSizeInsEncoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const932c85e3ff3SBruno Cardoso Lopes MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
9333fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
9343fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
935049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo-1).isImm());
936049e9e4dSAkira Hatanaka assert(MI.getOperand(OpNo).isImm());
9373fa98a65SDavid Woodhouse unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
9383fa98a65SDavid Woodhouse unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
939049e9e4dSAkira Hatanaka
94056b70de0SBruno Cardoso Lopes return Position + Size - 1;
941c85e3ff3SBruno Cardoso Lopes }
942c85e3ff3SBruno Cardoso Lopes
943ea4f653dSDaniel Sanders template <unsigned Bits, int Offset>
944779c5937SMatheus Almeida unsigned
getUImmWithOffsetEncoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const945ea4f653dSDaniel Sanders MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
9463fa98a65SDavid Woodhouse SmallVectorImpl<MCFixup> &Fixups,
9473fa98a65SDavid Woodhouse const MCSubtargetInfo &STI) const {
948779c5937SMatheus Almeida assert(MI.getOperand(OpNo).isImm());
949ea4f653dSDaniel Sanders unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
950ea4f653dSDaniel Sanders Value -= Offset;
951ea4f653dSDaniel Sanders return Value;
952779c5937SMatheus Almeida }
953779c5937SMatheus Almeida
954b59e1a41SDaniel Sanders unsigned
getSimm19Lsl2Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const955b59e1a41SDaniel Sanders MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
956b59e1a41SDaniel Sanders SmallVectorImpl<MCFixup> &Fixups,
957b59e1a41SDaniel Sanders const MCSubtargetInfo &STI) const {
958b9c07f3bSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
959b9c07f3bSZoran Jovanovic if (MO.isImm()) {
960b59e1a41SDaniel Sanders // The immediate is encoded as 'immediate << 2'.
961b9c07f3bSZoran Jovanovic unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
962b59e1a41SDaniel Sanders assert((Res & 3) == 0);
963b59e1a41SDaniel Sanders return Res >> 2;
964b59e1a41SDaniel Sanders }
965c85e3ff3SBruno Cardoso Lopes
966b9c07f3bSZoran Jovanovic assert(MO.isExpr() &&
967b9c07f3bSZoran Jovanovic "getSimm19Lsl2Encoding expects only expressions or an immediate");
968b9c07f3bSZoran Jovanovic
969b9c07f3bSZoran Jovanovic const MCExpr *Expr = MO.getExpr();
9706764fa78SZoran Jovanovic Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
9716764fa78SZoran Jovanovic : Mips::fixup_MIPS_PC19_S2;
9726764fa78SZoran Jovanovic Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
973b9c07f3bSZoran Jovanovic return 0;
974b9c07f3bSZoran Jovanovic }
975b9c07f3bSZoran Jovanovic
9762855142aSZoran Jovanovic unsigned
getSimm18Lsl3Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const9772855142aSZoran Jovanovic MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
9782855142aSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
9792855142aSZoran Jovanovic const MCSubtargetInfo &STI) const {
980a5acdcf9SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
981a5acdcf9SZoran Jovanovic if (MO.isImm()) {
9822855142aSZoran Jovanovic // The immediate is encoded as 'immediate << 3'.
9832855142aSZoran Jovanovic unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
9842855142aSZoran Jovanovic assert((Res & 7) == 0);
9852855142aSZoran Jovanovic return Res >> 3;
9862855142aSZoran Jovanovic }
9872855142aSZoran Jovanovic
988a5acdcf9SZoran Jovanovic assert(MO.isExpr() &&
989a5acdcf9SZoran Jovanovic "getSimm18Lsl2Encoding expects only expressions or an immediate");
990a5acdcf9SZoran Jovanovic
991a5acdcf9SZoran Jovanovic const MCExpr *Expr = MO.getExpr();
9928e366822SZoran Jovanovic Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
9938e366822SZoran Jovanovic : Mips::fixup_MIPS_PC18_S3;
9948e366822SZoran Jovanovic Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
995a5acdcf9SZoran Jovanovic return 0;
996a5acdcf9SZoran Jovanovic }
997a5acdcf9SZoran Jovanovic
9984a00fdc2SZoran Jovanovic unsigned
getUImm3Mod8Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const9994a00fdc2SZoran Jovanovic MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
10004a00fdc2SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
10014a00fdc2SZoran Jovanovic const MCSubtargetInfo &STI) const {
10024a00fdc2SZoran Jovanovic assert(MI.getOperand(OpNo).isImm());
10034a00fdc2SZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
10044a00fdc2SZoran Jovanovic return MO.getImm() % 8;
10054a00fdc2SZoran Jovanovic }
10064a00fdc2SZoran Jovanovic
10078853171bSZoran Jovanovic unsigned
getUImm4AndValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const10088853171bSZoran Jovanovic MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
10098853171bSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
10108853171bSZoran Jovanovic const MCSubtargetInfo &STI) const {
10118853171bSZoran Jovanovic assert(MI.getOperand(OpNo).isImm());
10128853171bSZoran Jovanovic const MCOperand &MO = MI.getOperand(OpNo);
10138853171bSZoran Jovanovic unsigned Value = MO.getImm();
10148853171bSZoran Jovanovic switch (Value) {
10158853171bSZoran Jovanovic case 128: return 0x0;
10168853171bSZoran Jovanovic case 1: return 0x1;
10178853171bSZoran Jovanovic case 2: return 0x2;
10188853171bSZoran Jovanovic case 3: return 0x3;
10198853171bSZoran Jovanovic case 4: return 0x4;
10208853171bSZoran Jovanovic case 7: return 0x5;
10218853171bSZoran Jovanovic case 8: return 0x6;
10228853171bSZoran Jovanovic case 15: return 0x7;
10238853171bSZoran Jovanovic case 16: return 0x8;
10248853171bSZoran Jovanovic case 31: return 0x9;
10258853171bSZoran Jovanovic case 32: return 0xa;
10268853171bSZoran Jovanovic case 63: return 0xb;
10278853171bSZoran Jovanovic case 64: return 0xc;
10288853171bSZoran Jovanovic case 255: return 0xd;
10298853171bSZoran Jovanovic case 32768: return 0xe;
10308853171bSZoran Jovanovic case 65535: return 0xf;
10318853171bSZoran Jovanovic }
10328853171bSZoran Jovanovic llvm_unreachable("Unexpected value");
10338853171bSZoran Jovanovic }
10348853171bSZoran Jovanovic
1035a4c4b5fcSZoran Jovanovic unsigned
getRegisterListOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1036a4c4b5fcSZoran Jovanovic MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
1037a4c4b5fcSZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
1038a4c4b5fcSZoran Jovanovic const MCSubtargetInfo &STI) const {
1039a4c4b5fcSZoran Jovanovic unsigned res = 0;
1040a4c4b5fcSZoran Jovanovic
1041a4c4b5fcSZoran Jovanovic // Register list operand is always first operand of instruction and it is
1042a4c4b5fcSZoran Jovanovic // placed before memory operand (register + imm).
1043a4c4b5fcSZoran Jovanovic
1044a4c4b5fcSZoran Jovanovic for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
1045a4c4b5fcSZoran Jovanovic unsigned Reg = MI.getOperand(I).getReg();
1046a4c4b5fcSZoran Jovanovic unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
1047a4c4b5fcSZoran Jovanovic if (RegNo != 31)
1048a4c4b5fcSZoran Jovanovic res++;
1049a4c4b5fcSZoran Jovanovic else
1050a4c4b5fcSZoran Jovanovic res |= 0x10;
1051a4c4b5fcSZoran Jovanovic }
1052a4c4b5fcSZoran Jovanovic return res;
1053a4c4b5fcSZoran Jovanovic }
1054a4c4b5fcSZoran Jovanovic
1055f9a02500SZoran Jovanovic unsigned
getRegisterListOpValue16(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1056f9a02500SZoran Jovanovic MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
1057f9a02500SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
1058f9a02500SZoran Jovanovic const MCSubtargetInfo &STI) const {
1059f9a02500SZoran Jovanovic return (MI.getNumOperands() - 4);
1060f9a02500SZoran Jovanovic }
1061f9a02500SZoran Jovanovic
10622deca348SZoran Jovanovic unsigned
getMovePRegPairOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const106341688679SZoran Jovanovic MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
106441688679SZoran Jovanovic SmallVectorImpl<MCFixup> &Fixups,
106541688679SZoran Jovanovic const MCSubtargetInfo &STI) const {
106641688679SZoran Jovanovic unsigned res = 0;
106741688679SZoran Jovanovic
106841688679SZoran Jovanovic if (MI.getOperand(0).getReg() == Mips::A1 &&
106941688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A2)
107041688679SZoran Jovanovic res = 0;
107141688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A1 &&
107241688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A3)
107341688679SZoran Jovanovic res = 1;
107441688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A2 &&
107541688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A3)
107641688679SZoran Jovanovic res = 2;
107741688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A0 &&
107841688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::S5)
107941688679SZoran Jovanovic res = 3;
108041688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A0 &&
108141688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::S6)
108241688679SZoran Jovanovic res = 4;
108341688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A0 &&
108441688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A1)
108541688679SZoran Jovanovic res = 5;
108641688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A0 &&
108741688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A2)
108841688679SZoran Jovanovic res = 6;
108941688679SZoran Jovanovic else if (MI.getOperand(0).getReg() == Mips::A0 &&
109041688679SZoran Jovanovic MI.getOperand(1).getReg() == Mips::A3)
109141688679SZoran Jovanovic res = 7;
109241688679SZoran Jovanovic
109341688679SZoran Jovanovic return res;
109441688679SZoran Jovanovic }
109541688679SZoran Jovanovic
109641688679SZoran Jovanovic unsigned
getMovePRegSingleOpValue(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const1097169df4e2SSimon Dardis MipsMCCodeEmitter::getMovePRegSingleOpValue(const MCInst &MI, unsigned OpNo,
1098169df4e2SSimon Dardis SmallVectorImpl<MCFixup> &Fixups,
1099169df4e2SSimon Dardis const MCSubtargetInfo &STI) const {
1100169df4e2SSimon Dardis assert(((OpNo == 2) || (OpNo == 3)) &&
1101169df4e2SSimon Dardis "Unexpected OpNo for movep operand encoding!");
1102169df4e2SSimon Dardis
1103169df4e2SSimon Dardis MCOperand Op = MI.getOperand(OpNo);
1104169df4e2SSimon Dardis assert(Op.isReg() && "Operand of movep is not a register!");
1105169df4e2SSimon Dardis switch (Op.getReg()) {
1106169df4e2SSimon Dardis default:
1107169df4e2SSimon Dardis llvm_unreachable("Unknown register for movep!");
1108169df4e2SSimon Dardis case Mips::ZERO: return 0;
1109169df4e2SSimon Dardis case Mips::S1: return 1;
1110169df4e2SSimon Dardis case Mips::V0: return 2;
1111169df4e2SSimon Dardis case Mips::V1: return 3;
1112169df4e2SSimon Dardis case Mips::S0: return 4;
1113169df4e2SSimon Dardis case Mips::S2: return 5;
1114169df4e2SSimon Dardis case Mips::S3: return 6;
1115169df4e2SSimon Dardis case Mips::S4: return 7;
1116169df4e2SSimon Dardis }
1117169df4e2SSimon Dardis }
1118169df4e2SSimon Dardis
1119169df4e2SSimon Dardis unsigned
getSimm23Lsl2Encoding(const MCInst & MI,unsigned OpNo,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI) const11202c6d7320SJozef Kolek MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
11212c6d7320SJozef Kolek SmallVectorImpl<MCFixup> &Fixups,
11222c6d7320SJozef Kolek const MCSubtargetInfo &STI) const {
11232c6d7320SJozef Kolek const MCOperand &MO = MI.getOperand(OpNo);
11242c6d7320SJozef Kolek assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
11252c6d7320SJozef Kolek // The immediate is encoded as 'immediate >> 2'.
11262c6d7320SJozef Kolek unsigned Res = static_cast<unsigned>(MO.getImm());
11272c6d7320SJozef Kolek assert((Res & 3) == 0);
11282c6d7320SJozef Kolek return Res >> 2;
11292c6d7320SJozef Kolek }
11302c6d7320SJozef Kolek
1131b59e1a41SDaniel Sanders #include "MipsGenMCCodeEmitter.inc"
1132