blob: 4b030ebfce8c5aa259beec56ab3d5502561b4c20 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
Akira Hatanaka750ecec2011-09-30 20:40:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MipsMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13//
Matheus Almeida9e1450b2014-03-20 09:29:54 +000014
Matheus Almeida9e1450b2014-03-20 09:29:54 +000015#include "MipsMCCodeEmitter.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000016#include "MCTargetDesc/MipsFixupKinds.h"
Petar Jovanovica5da5882014-02-04 18:41:57 +000017#include "MCTargetDesc/MipsMCExpr.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000018#include "MCTargetDesc/MipsMCTargetDesc.h"
19#include "llvm/ADT/APFloat.h"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000020#include "llvm/ADT/SmallVector.h"
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000021#include "llvm/MC/MCContext.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000022#include "llvm/MC/MCExpr.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "llvm/MC/MCFixup.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000024#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstrInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000026#include "llvm/MC/MCRegisterInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000027#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000028#include "llvm/Support/raw_ostream.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000029
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "mccodeemitter"
31
Akira Hatanakabe6a8182013-04-19 19:03:11 +000032#define GET_INSTRMAP_INFO
33#include "MipsGenInstrInfo.inc"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000034#undef GET_INSTRMAP_INFO
Akira Hatanakabe6a8182013-04-19 19:03:11 +000035
Matheus Almeida9e1450b2014-03-20 09:29:54 +000036namespace llvm {
37MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
38 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000039 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000040 return new MipsMCCodeEmitter(MCII, Ctx, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000041}
42
Matheus Almeida9e1450b2014-03-20 09:29:54 +000043MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
44 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000045 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000046 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +000047}
Matheus Almeida9e1450b2014-03-20 09:29:54 +000048} // End of namespace llvm.
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000049
50// If the D<shift> instruction has a shift amount that is greater
51// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
52static void LowerLargeShift(MCInst& Inst) {
53
54 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
55 assert(Inst.getOperand(2).isImm());
56
57 int64_t Shift = Inst.getOperand(2).getImm();
58 if (Shift <= 31)
59 return; // Do nothing
60 Shift -= 32;
61
62 // saminus32
63 Inst.getOperand(2).setImm(Shift);
64
65 switch (Inst.getOpcode()) {
66 default:
67 // Calling function is not synchronized
68 llvm_unreachable("Unexpected shift instruction");
69 case Mips::DSLL:
70 Inst.setOpcode(Mips::DSLL32);
71 return;
72 case Mips::DSRL:
73 Inst.setOpcode(Mips::DSRL32);
74 return;
75 case Mips::DSRA:
76 Inst.setOpcode(Mips::DSRA32);
77 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +000078 case Mips::DROTR:
79 Inst.setOpcode(Mips::DROTR32);
80 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000081 }
82}
83
84// Pick a DEXT or DINS instruction variant based on the pos and size operands
85static void LowerDextDins(MCInst& InstIn) {
86 int Opcode = InstIn.getOpcode();
87
88 if (Opcode == Mips::DEXT)
89 assert(InstIn.getNumOperands() == 4 &&
90 "Invalid no. of machine operands for DEXT!");
91 else // Only DEXT and DINS are possible
92 assert(InstIn.getNumOperands() == 5 &&
93 "Invalid no. of machine operands for DINS!");
94
95 assert(InstIn.getOperand(2).isImm());
96 int64_t pos = InstIn.getOperand(2).getImm();
97 assert(InstIn.getOperand(3).isImm());
98 int64_t size = InstIn.getOperand(3).getImm();
99
100 if (size <= 32) {
101 if (pos < 32) // DEXT/DINS, do nothing
102 return;
103 // DEXTU/DINSU
104 InstIn.getOperand(2).setImm(pos - 32);
105 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
106 return;
107 }
108 // DEXTM/DINSM
109 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
110 InstIn.getOperand(3).setImm(size - 32);
111 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
112 return;
113}
114
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000115bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000116 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000117}
118
Jozef Kolekc22555d2015-04-20 12:23:06 +0000119bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000120 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000121}
122
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000123void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
124 OS << (char)C;
125}
126
127void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
128 const MCSubtargetInfo &STI,
129 raw_ostream &OS) const {
130 // Output the instruction encoding in little endian byte order.
131 // Little-endian byte ordering:
132 // mips32r2: 4 | 3 | 2 | 1
133 // microMIPS: 2 | 1 | 4 | 3
134 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
135 EmitInstruction(Val >> 16, 2, STI, OS);
136 EmitInstruction(Val, 2, STI, OS);
137 } else {
138 for (unsigned i = 0; i < Size; ++i) {
139 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
140 EmitByte((Val >> Shift) & 0xff, OS);
141 }
142 }
143}
144
Jim Grosbach91df21f2015-05-15 19:13:16 +0000145/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000146/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000147void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000148encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000149 SmallVectorImpl<MCFixup> &Fixups,
150 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000151{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000152
153 // Non-pseudo instructions that get changed for direct object
154 // only based on operand values.
155 // If this list of instructions get much longer we will move
156 // the check to a function call. Until then, this is more efficient.
157 MCInst TmpInst = MI;
158 switch (MI.getOpcode()) {
159 // If shift amount is >= 32 it the inst needs to be lowered further
160 case Mips::DSLL:
161 case Mips::DSRL:
162 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000163 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000164 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000165 break;
166 // Double extract instruction is chosen by pos and size operands
167 case Mips::DEXT:
168 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000169 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000170 }
171
Jack Carter97700972013-08-13 20:19:16 +0000172 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000173 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000174
175 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000176 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000177 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000178 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000179 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
180 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000181 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000182
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000183 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000184 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000185 if (isMips32r6(STI)) {
186 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
187 if (NewOpcode == -1)
188 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
189 }
190 else
191 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
192
Zoran Jovanovic2e386d32015-10-12 16:07:25 +0000193 // Check whether it is Dsp instruction.
194 if (NewOpcode == -1)
195 NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
196
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000197 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000198 if (Fixups.size() > N)
199 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000200
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000201 Opcode = NewOpcode;
202 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000203 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000204 }
205 }
206
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000207 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208
Jack Carter5b5559d2012-10-03 21:58:54 +0000209 // Get byte count of instruction
210 unsigned Size = Desc.getSize();
211 if (!Size)
212 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000213
David Woodhoused2cca112014-01-28 23:13:25 +0000214 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000215}
216
217/// getBranchTargetOpValue - Return binary encoding of the branch
218/// target operand. If the machine operand requires relocation,
219/// record the relocation and return zero.
220unsigned MipsMCCodeEmitter::
221getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000222 SmallVectorImpl<MCFixup> &Fixups,
223 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000224
225 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000226
Jack Carter4f69a0f2013-03-22 00:29:10 +0000227 // If the destination is an immediate, divide by 4.
228 if (MO.isImm()) return MO.getImm() >> 2;
229
Jack Carter71e6a742012-09-06 00:43:26 +0000230 assert(MO.isExpr() &&
231 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000232
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000233 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
234 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
235 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000236 MCFixupKind(Mips::fixup_Mips_PC16)));
237 return 0;
238}
239
Jozef Kolek9761e962015-01-12 12:03:34 +0000240/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
241/// target operand. If the machine operand requires relocation,
242/// record the relocation and return zero.
243unsigned MipsMCCodeEmitter::
244getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
245 SmallVectorImpl<MCFixup> &Fixups,
246 const MCSubtargetInfo &STI) const {
247
248 const MCOperand &MO = MI.getOperand(OpNo);
249
250 // If the destination is an immediate, divide by 2.
251 if (MO.isImm()) return MO.getImm() >> 1;
252
253 assert(MO.isExpr() &&
254 "getBranchTargetOpValueMM expects only expressions or immediates");
255
256 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000257 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000258 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
259 return 0;
260}
261
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000262/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
263/// 10-bit branch target operand. If the machine operand requires relocation,
264/// record the relocation and return zero.
265unsigned MipsMCCodeEmitter::
266getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
267 SmallVectorImpl<MCFixup> &Fixups,
268 const MCSubtargetInfo &STI) const {
269
270 const MCOperand &MO = MI.getOperand(OpNo);
271
272 // If the destination is an immediate, divide by 2.
273 if (MO.isImm()) return MO.getImm() >> 1;
274
275 assert(MO.isExpr() &&
276 "getBranchTargetOpValuePC10 expects only expressions or immediates");
277
278 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000279 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000280 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
281 return 0;
282}
283
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000284/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
285/// target operand. If the machine operand requires relocation,
286/// record the relocation and return zero.
287unsigned MipsMCCodeEmitter::
288getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000289 SmallVectorImpl<MCFixup> &Fixups,
290 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000291
292 const MCOperand &MO = MI.getOperand(OpNo);
293
294 // If the destination is an immediate, divide by 2.
295 if (MO.isImm()) return MO.getImm() >> 1;
296
297 assert(MO.isExpr() &&
298 "getBranchTargetOpValueMM expects only expressions or immediates");
299
300 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000301 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000302 MCFixupKind(Mips::
303 fixup_MICROMIPS_PC16_S1)));
304 return 0;
305}
306
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000307/// getBranchTarget21OpValue - Return binary encoding of the branch
308/// target operand. If the machine operand requires relocation,
309/// record the relocation and return zero.
310unsigned MipsMCCodeEmitter::
311getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
312 SmallVectorImpl<MCFixup> &Fixups,
313 const MCSubtargetInfo &STI) const {
314
315 const MCOperand &MO = MI.getOperand(OpNo);
316
317 // If the destination is an immediate, divide by 4.
318 if (MO.isImm()) return MO.getImm() >> 2;
319
320 assert(MO.isExpr() &&
321 "getBranchTarget21OpValue expects only expressions or immediates");
322
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000323 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
324 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
325 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000326 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000327 return 0;
328}
329
330/// getBranchTarget26OpValue - Return binary encoding of the branch
331/// target operand. If the machine operand requires relocation,
332/// record the relocation and return zero.
333unsigned MipsMCCodeEmitter::
334getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
335 SmallVectorImpl<MCFixup> &Fixups,
336 const MCSubtargetInfo &STI) const {
337
338 const MCOperand &MO = MI.getOperand(OpNo);
339
340 // If the destination is an immediate, divide by 4.
341 if (MO.isImm()) return MO.getImm() >> 2;
342
343 assert(MO.isExpr() &&
344 "getBranchTarget26OpValue expects only expressions or immediates");
345
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000346 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
347 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
348 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000349 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000350 return 0;
351}
352
Zoran Jovanovica887b362015-11-30 12:56:18 +0000353/// getBranchTarget26OpValueMM - Return binary encoding of the branch
354/// target operand. If the machine operand requires relocation,
355/// record the relocation and return zero.
356unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
357 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
358 const MCSubtargetInfo &STI) const {
359
360 const MCOperand &MO = MI.getOperand(OpNo);
361
362 // If the destination is an immediate, divide by 2.
363 if (MO.isImm())
364 return MO.getImm() >> 1;
365
366 // TODO: Push 26 PC fixup.
367 return 0;
368}
369
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000370/// getJumpOffset16OpValue - Return binary encoding of the jump
371/// target operand. If the machine operand requires relocation,
372/// record the relocation and return zero.
373unsigned MipsMCCodeEmitter::
374getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
375 SmallVectorImpl<MCFixup> &Fixups,
376 const MCSubtargetInfo &STI) const {
377
378 const MCOperand &MO = MI.getOperand(OpNo);
379
380 if (MO.isImm()) return MO.getImm();
381
382 assert(MO.isExpr() &&
383 "getJumpOffset16OpValue expects only expressions or an immediate");
384
385 // TODO: Push fixup.
386 return 0;
387}
388
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000389/// getJumpTargetOpValue - Return binary encoding of the jump
390/// target operand. If the machine operand requires relocation,
391/// record the relocation and return zero.
392unsigned MipsMCCodeEmitter::
393getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000394 SmallVectorImpl<MCFixup> &Fixups,
395 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000396
397 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000398 // If the destination is an immediate, divide by 4.
399 if (MO.isImm()) return MO.getImm()>>2;
400
Jack Carter71e6a742012-09-06 00:43:26 +0000401 assert(MO.isExpr() &&
402 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000403
404 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000405 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000406 MCFixupKind(Mips::fixup_Mips_26)));
407 return 0;
408}
409
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000410unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000411getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000412 SmallVectorImpl<MCFixup> &Fixups,
413 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000414
415 const MCOperand &MO = MI.getOperand(OpNo);
416 // If the destination is an immediate, divide by 2.
417 if (MO.isImm()) return MO.getImm() >> 1;
418
419 assert(MO.isExpr() &&
420 "getJumpTargetOpValueMM expects only expressions or an immediate");
421
422 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000423 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000424 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
425 return 0;
426}
427
428unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000429getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
430 SmallVectorImpl<MCFixup> &Fixups,
431 const MCSubtargetInfo &STI) const {
432
433 const MCOperand &MO = MI.getOperand(OpNo);
434 if (MO.isImm()) {
435 // The immediate is encoded as 'immediate << 2'.
436 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
437 assert((Res & 3) == 0);
438 return Res >> 2;
439 }
440
441 assert(MO.isExpr() &&
442 "getUImm5Lsl2Encoding expects only expressions or an immediate");
443
444 return 0;
445}
446
447unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000448getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
449 SmallVectorImpl<MCFixup> &Fixups,
450 const MCSubtargetInfo &STI) const {
451
452 const MCOperand &MO = MI.getOperand(OpNo);
453 if (MO.isImm()) {
454 int Value = MO.getImm();
455 return Value >> 2;
456 }
457
458 return 0;
459}
460
461unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000462getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
463 SmallVectorImpl<MCFixup> &Fixups,
464 const MCSubtargetInfo &STI) const {
465
466 const MCOperand &MO = MI.getOperand(OpNo);
467 if (MO.isImm()) {
468 unsigned Value = MO.getImm();
469 return Value >> 2;
470 }
471
472 return 0;
473}
474
475unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000476getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
477 SmallVectorImpl<MCFixup> &Fixups,
478 const MCSubtargetInfo &STI) const {
479
480 const MCOperand &MO = MI.getOperand(OpNo);
481 if (MO.isImm()) {
482 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
483 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
484 }
485
486 return 0;
487}
488
489unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000490getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000491 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000492 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000493
Jim Grosbach13760bd2015-05-30 01:25:56 +0000494 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000495 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000496
Akira Hatanakafe384a22012-03-27 02:33:05 +0000497 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000498 if (Kind == MCExpr::Constant) {
499 return cast<MCConstantExpr>(Expr)->getValue();
500 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000501
Akira Hatanakafe384a22012-03-27 02:33:05 +0000502 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000503 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
504 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000505 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000506 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000507
508 if (Kind == MCExpr::Target) {
509 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
510
511 Mips::Fixups FixupKind = Mips::Fixups(0);
512 switch (MipsExpr->getKind()) {
513 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000514 case MipsMCExpr::VK_Mips_HIGHEST:
515 FixupKind = Mips::fixup_Mips_HIGHEST;
516 break;
517 case MipsMCExpr::VK_Mips_HIGHER:
518 FixupKind = Mips::fixup_Mips_HIGHER;
519 break;
520 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000521 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
522 : Mips::fixup_Mips_HI16;
523 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000524 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000525 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
526 : Mips::fixup_Mips_LO16;
527 break;
528 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000529 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000530 return 0;
531 }
532
Jack Carterb5cf5902013-04-17 00:18:04 +0000533 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000534 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000535
Mark Seabornc3bd1772013-12-31 13:05:15 +0000536 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
537 default: llvm_unreachable("Unknown fixup kind!");
538 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000539 case MCSymbolRefExpr::VK_None:
540 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
541 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000542 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
543 FixupKind = Mips::fixup_Mips_GPOFF_HI;
544 break;
545 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
546 FixupKind = Mips::fixup_Mips_GPOFF_LO;
547 break;
548 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000549 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000550 : Mips::fixup_Mips_GOT_PAGE;
551 break;
552 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000553 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000554 : Mips::fixup_Mips_GOT_OFST;
555 break;
556 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000557 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000558 : Mips::fixup_Mips_GOT_DISP;
559 break;
560 case MCSymbolRefExpr::VK_Mips_GPREL:
561 FixupKind = Mips::fixup_Mips_GPREL16;
562 break;
563 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000564 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000565 : Mips::fixup_Mips_CALL16;
566 break;
567 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000568 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000569 : Mips::fixup_Mips_GOT_Global;
570 break;
571 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000572 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000573 : Mips::fixup_Mips_GOT_Local;
574 break;
575 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000576 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000577 : Mips::fixup_Mips_HI16;
578 break;
579 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000580 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000581 : Mips::fixup_Mips_LO16;
582 break;
583 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000584 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000585 : Mips::fixup_Mips_TLSGD;
586 break;
587 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000588 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000589 : Mips::fixup_Mips_TLSLDM;
590 break;
591 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000592 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000593 : Mips::fixup_Mips_DTPREL_HI;
594 break;
595 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000596 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000597 : Mips::fixup_Mips_DTPREL_LO;
598 break;
599 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
600 FixupKind = Mips::fixup_Mips_GOTTPREL;
601 break;
602 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000603 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000604 : Mips::fixup_Mips_TPREL_HI;
605 break;
606 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000607 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000608 : Mips::fixup_Mips_TPREL_LO;
609 break;
610 case MCSymbolRefExpr::VK_Mips_HIGHER:
611 FixupKind = Mips::fixup_Mips_HIGHER;
612 break;
613 case MCSymbolRefExpr::VK_Mips_HIGHEST:
614 FixupKind = Mips::fixup_Mips_HIGHEST;
615 break;
616 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
617 FixupKind = Mips::fixup_Mips_GOT_HI16;
618 break;
619 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
620 FixupKind = Mips::fixup_Mips_GOT_LO16;
621 break;
622 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
623 FixupKind = Mips::fixup_Mips_CALL_HI16;
624 break;
625 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
626 FixupKind = Mips::fixup_Mips_CALL_LO16;
627 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000628 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
629 FixupKind = Mips::fixup_MIPS_PCHI16;
630 break;
631 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
632 FixupKind = Mips::fixup_MIPS_PCLO16;
633 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000634 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000635
Jim Grosbach63661f82015-05-15 19:13:05 +0000636 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000637 return 0;
638 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000639 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000640}
641
Jack Carterb5cf5902013-04-17 00:18:04 +0000642/// getMachineOpValue - Return binary encoding of operand. If the machine
643/// operand requires relocation, record the relocation and return zero.
644unsigned MipsMCCodeEmitter::
645getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000646 SmallVectorImpl<MCFixup> &Fixups,
647 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000648 if (MO.isReg()) {
649 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000650 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000651 return RegNo;
652 } else if (MO.isImm()) {
653 return static_cast<unsigned>(MO.getImm());
654 } else if (MO.isFPImm()) {
655 return static_cast<unsigned>(APFloat(MO.getFPImm())
656 .bitcastToAPInt().getHiBits(32).getLimitedValue());
657 }
658 // MO must be an Expr.
659 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000660 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000661}
662
Matheus Almeida6b59c442013-12-05 11:06:22 +0000663/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
664/// instructions.
665unsigned
666MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000667 SmallVectorImpl<MCFixup> &Fixups,
668 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000669 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
670 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000671 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
672 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000673
674 // The immediate field of an LD/ST instruction is scaled which means it must
675 // be divided (when encoding) by the size (in bytes) of the instructions'
676 // data format.
677 // .b - 1 byte
678 // .h - 2 bytes
679 // .w - 4 bytes
680 // .d - 8 bytes
681 switch(MI.getOpcode())
682 {
683 default:
684 assert (0 && "Unexpected instruction");
685 break;
686 case Mips::LD_B:
687 case Mips::ST_B:
688 // We don't need to scale the offset in this case
689 break;
690 case Mips::LD_H:
691 case Mips::ST_H:
692 OffBits >>= 1;
693 break;
694 case Mips::LD_W:
695 case Mips::ST_W:
696 OffBits >>= 2;
697 break;
698 case Mips::LD_D:
699 case Mips::ST_D:
700 OffBits >>= 3;
701 break;
702 }
703
704 return (OffBits & 0xFFFF) | RegBits;
705}
706
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000707/// getMemEncoding - Return binary encoding of memory related operand.
708/// If the offset operand requires relocation, record the relocation.
709unsigned
710MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000711 SmallVectorImpl<MCFixup> &Fixups,
712 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000713 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
714 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000715 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
716 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000717
718 return (OffBits & 0xFFFF) | RegBits;
719}
720
Jack Carter97700972013-08-13 20:19:16 +0000721unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000722getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
723 SmallVectorImpl<MCFixup> &Fixups,
724 const MCSubtargetInfo &STI) const {
725 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
726 assert(MI.getOperand(OpNo).isReg());
727 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
728 Fixups, STI) << 4;
729 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
730 Fixups, STI);
731
732 return (OffBits & 0xF) | RegBits;
733}
734
735unsigned MipsMCCodeEmitter::
736getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
737 SmallVectorImpl<MCFixup> &Fixups,
738 const MCSubtargetInfo &STI) const {
739 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
740 assert(MI.getOperand(OpNo).isReg());
741 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
742 Fixups, STI) << 4;
743 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
744 Fixups, STI) >> 1;
745
746 return (OffBits & 0xF) | RegBits;
747}
748
749unsigned MipsMCCodeEmitter::
750getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
751 SmallVectorImpl<MCFixup> &Fixups,
752 const MCSubtargetInfo &STI) const {
753 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
754 assert(MI.getOperand(OpNo).isReg());
755 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
756 Fixups, STI) << 4;
757 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
758 Fixups, STI) >> 2;
759
760 return (OffBits & 0xF) | RegBits;
761}
762
763unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000764getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
765 SmallVectorImpl<MCFixup> &Fixups,
766 const MCSubtargetInfo &STI) const {
767 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
768 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000769 (MI.getOperand(OpNo).getReg() == Mips::SP ||
770 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000771 "Unexpected base register!");
772 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
773 Fixups, STI) >> 2;
774
775 return OffBits & 0x1F;
776}
777
778unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000779getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
780 SmallVectorImpl<MCFixup> &Fixups,
781 const MCSubtargetInfo &STI) const {
782 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
783 assert(MI.getOperand(OpNo).isReg() &&
784 MI.getOperand(OpNo).getReg() == Mips::GP &&
785 "Unexpected base register!");
786
787 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
788 Fixups, STI) >> 2;
789
790 return OffBits & 0x7F;
791}
792
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000793unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000794getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
795 SmallVectorImpl<MCFixup> &Fixups,
796 const MCSubtargetInfo &STI) const {
797 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
798 assert(MI.getOperand(OpNo).isReg());
799 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
800 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000801 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000802
803 return (OffBits & 0x1FF) | RegBits;
804}
805
Jozef Koleke10a02e2015-01-28 17:27:26 +0000806unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000807getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000808 SmallVectorImpl<MCFixup> &Fixups,
809 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000810 // opNum can be invalid if instruction had reglist as operand.
811 // MemOperand is always last operand of instruction (base + offset).
812 switch (MI.getOpcode()) {
813 default:
814 break;
815 case Mips::SWM32_MM:
816 case Mips::LWM32_MM:
817 OpNo = MI.getNumOperands() - 2;
818 break;
819 }
820
Jack Carter97700972013-08-13 20:19:16 +0000821 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
822 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000823 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
824 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000825
826 return (OffBits & 0x0FFF) | RegBits;
827}
828
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000829unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000830getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
831 SmallVectorImpl<MCFixup> &Fixups,
832 const MCSubtargetInfo &STI) const {
833 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
834 assert(MI.getOperand(OpNo).isReg());
835 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
836 STI) << 16;
837 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
838
839 return (OffBits & 0xFFFF) | RegBits;
840}
841
842unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000843getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
844 SmallVectorImpl<MCFixup> &Fixups,
845 const MCSubtargetInfo &STI) const {
846 // opNum can be invalid if instruction had reglist as operand
847 // MemOperand is always last operand of instruction (base + offset)
848 switch (MI.getOpcode()) {
849 default:
850 break;
851 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000852 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000853 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000854 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000855 OpNo = MI.getNumOperands() - 2;
856 break;
857 }
858
859 // Offset is encoded in bits 4-0.
860 assert(MI.getOperand(OpNo).isReg());
861 // Base register is always SP - thus it is not encoded.
862 assert(MI.getOperand(OpNo+1).isImm());
863 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
864
865 return ((OffBits >> 2) & 0x0F);
866}
867
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000868// FIXME: should be called getMSBEncoding
869//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000870unsigned
871MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000872 SmallVectorImpl<MCFixup> &Fixups,
873 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000874 assert(MI.getOperand(OpNo-1).isImm());
875 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000876 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
877 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000878
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000879 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000880}
881
Daniel Sandersea4f6532015-11-06 12:22:31 +0000882template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000883unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000884MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
885 SmallVectorImpl<MCFixup> &Fixups,
886 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000887 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000888 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
889 Value -= Offset;
890 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000891}
892
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000893unsigned
894MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
895 SmallVectorImpl<MCFixup> &Fixups,
896 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000897 const MCOperand &MO = MI.getOperand(OpNo);
898 if (MO.isImm()) {
899 // The immediate is encoded as 'immediate << 2'.
900 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
901 assert((Res & 3) == 0);
902 return Res >> 2;
903 }
904
905 assert(MO.isExpr() &&
906 "getSimm19Lsl2Encoding expects only expressions or an immediate");
907
908 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000909 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000910 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
911 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000912}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000913
Zoran Jovanovic28551422014-06-09 09:49:51 +0000914unsigned
915MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
916 SmallVectorImpl<MCFixup> &Fixups,
917 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000918 const MCOperand &MO = MI.getOperand(OpNo);
919 if (MO.isImm()) {
920 // The immediate is encoded as 'immediate << 3'.
921 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
922 assert((Res & 7) == 0);
923 return Res >> 3;
924 }
925
926 assert(MO.isExpr() &&
927 "getSimm18Lsl2Encoding expects only expressions or an immediate");
928
929 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000930 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000931 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
932 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000933}
934
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000935unsigned
936MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
937 SmallVectorImpl<MCFixup> &Fixups,
938 const MCSubtargetInfo &STI) const {
939 assert(MI.getOperand(OpNo).isImm());
940 const MCOperand &MO = MI.getOperand(OpNo);
941 return MO.getImm() % 8;
942}
943
Zoran Jovanovic88531712014-11-05 17:31:00 +0000944unsigned
945MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
946 SmallVectorImpl<MCFixup> &Fixups,
947 const MCSubtargetInfo &STI) const {
948 assert(MI.getOperand(OpNo).isImm());
949 const MCOperand &MO = MI.getOperand(OpNo);
950 unsigned Value = MO.getImm();
951 switch (Value) {
952 case 128: return 0x0;
953 case 1: return 0x1;
954 case 2: return 0x2;
955 case 3: return 0x3;
956 case 4: return 0x4;
957 case 7: return 0x5;
958 case 8: return 0x6;
959 case 15: return 0x7;
960 case 16: return 0x8;
961 case 31: return 0x9;
962 case 32: return 0xa;
963 case 63: return 0xb;
964 case 64: return 0xc;
965 case 255: return 0xd;
966 case 32768: return 0xe;
967 case 65535: return 0xf;
968 }
969 llvm_unreachable("Unexpected value");
970}
971
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000972unsigned
973MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
974 SmallVectorImpl<MCFixup> &Fixups,
975 const MCSubtargetInfo &STI) const {
976 unsigned res = 0;
977
978 // Register list operand is always first operand of instruction and it is
979 // placed before memory operand (register + imm).
980
981 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
982 unsigned Reg = MI.getOperand(I).getReg();
983 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
984 if (RegNo != 31)
985 res++;
986 else
987 res |= 0x10;
988 }
989 return res;
990}
991
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000992unsigned
993MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
994 SmallVectorImpl<MCFixup> &Fixups,
995 const MCSubtargetInfo &STI) const {
996 return (MI.getNumOperands() - 4);
997}
998
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000999unsigned
1000MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
1001 SmallVectorImpl<MCFixup> &Fixups,
1002 const MCSubtargetInfo &STI) const {
1003 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1004}
1005
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001006unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +00001007MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1008 SmallVectorImpl<MCFixup> &Fixups,
1009 const MCSubtargetInfo &STI) const {
1010 unsigned res = 0;
1011
1012 if (MI.getOperand(0).getReg() == Mips::A1 &&
1013 MI.getOperand(1).getReg() == Mips::A2)
1014 res = 0;
1015 else if (MI.getOperand(0).getReg() == Mips::A1 &&
1016 MI.getOperand(1).getReg() == Mips::A3)
1017 res = 1;
1018 else if (MI.getOperand(0).getReg() == Mips::A2 &&
1019 MI.getOperand(1).getReg() == Mips::A3)
1020 res = 2;
1021 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1022 MI.getOperand(1).getReg() == Mips::S5)
1023 res = 3;
1024 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1025 MI.getOperand(1).getReg() == Mips::S6)
1026 res = 4;
1027 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1028 MI.getOperand(1).getReg() == Mips::A1)
1029 res = 5;
1030 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1031 MI.getOperand(1).getReg() == Mips::A2)
1032 res = 6;
1033 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1034 MI.getOperand(1).getReg() == Mips::A3)
1035 res = 7;
1036
1037 return res;
1038}
1039
1040unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001041MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1042 SmallVectorImpl<MCFixup> &Fixups,
1043 const MCSubtargetInfo &STI) const {
1044 const MCOperand &MO = MI.getOperand(OpNo);
1045 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1046 // The immediate is encoded as 'immediate >> 2'.
1047 unsigned Res = static_cast<unsigned>(MO.getImm());
1048 assert((Res & 3) == 0);
1049 return Res >> 2;
1050}
1051
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001052#include "MipsGenMCCodeEmitter.inc"