blob: 21cd546c06c67b3883c11846802462f25f4cf694 [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 Kupersteinc3434b32015-05-13 10:28:46 +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 Kupersteinc3434b32015-05-13 10:28:46 +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
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000193 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000194 if (Fixups.size() > N)
195 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000196
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000197 Opcode = NewOpcode;
198 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000199 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000200 }
201 }
202
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000203 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000204
Jack Carter5b5559d2012-10-03 21:58:54 +0000205 // Get byte count of instruction
206 unsigned Size = Desc.getSize();
207 if (!Size)
208 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000209
David Woodhoused2cca112014-01-28 23:13:25 +0000210 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000211}
212
213/// getBranchTargetOpValue - Return binary encoding of the branch
214/// target operand. If the machine operand requires relocation,
215/// record the relocation and return zero.
216unsigned MipsMCCodeEmitter::
217getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000218 SmallVectorImpl<MCFixup> &Fixups,
219 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000220
221 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000222
Jack Carter4f69a0f2013-03-22 00:29:10 +0000223 // If the destination is an immediate, divide by 4.
224 if (MO.isImm()) return MO.getImm() >> 2;
225
Jack Carter71e6a742012-09-06 00:43:26 +0000226 assert(MO.isExpr() &&
227 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000228
229 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000230 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000231 MCFixupKind(Mips::fixup_Mips_PC16)));
232 return 0;
233}
234
Jozef Kolek9761e962015-01-12 12:03:34 +0000235/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
236/// target operand. If the machine operand requires relocation,
237/// record the relocation and return zero.
238unsigned MipsMCCodeEmitter::
239getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
240 SmallVectorImpl<MCFixup> &Fixups,
241 const MCSubtargetInfo &STI) const {
242
243 const MCOperand &MO = MI.getOperand(OpNo);
244
245 // If the destination is an immediate, divide by 2.
246 if (MO.isImm()) return MO.getImm() >> 1;
247
248 assert(MO.isExpr() &&
249 "getBranchTargetOpValueMM expects only expressions or immediates");
250
251 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000252 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000253 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
254 return 0;
255}
256
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000257/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
258/// 10-bit branch target operand. If the machine operand requires relocation,
259/// record the relocation and return zero.
260unsigned MipsMCCodeEmitter::
261getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
262 SmallVectorImpl<MCFixup> &Fixups,
263 const MCSubtargetInfo &STI) const {
264
265 const MCOperand &MO = MI.getOperand(OpNo);
266
267 // If the destination is an immediate, divide by 2.
268 if (MO.isImm()) return MO.getImm() >> 1;
269
270 assert(MO.isExpr() &&
271 "getBranchTargetOpValuePC10 expects only expressions or immediates");
272
273 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000274 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000275 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
276 return 0;
277}
278
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000279/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
280/// target operand. If the machine operand requires relocation,
281/// record the relocation and return zero.
282unsigned MipsMCCodeEmitter::
283getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000284 SmallVectorImpl<MCFixup> &Fixups,
285 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000286
287 const MCOperand &MO = MI.getOperand(OpNo);
288
289 // If the destination is an immediate, divide by 2.
290 if (MO.isImm()) return MO.getImm() >> 1;
291
292 assert(MO.isExpr() &&
293 "getBranchTargetOpValueMM expects only expressions or immediates");
294
295 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000296 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000297 MCFixupKind(Mips::
298 fixup_MICROMIPS_PC16_S1)));
299 return 0;
300}
301
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000302/// getBranchTarget21OpValue - Return binary encoding of the branch
303/// target operand. If the machine operand requires relocation,
304/// record the relocation and return zero.
305unsigned MipsMCCodeEmitter::
306getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
307 SmallVectorImpl<MCFixup> &Fixups,
308 const MCSubtargetInfo &STI) const {
309
310 const MCOperand &MO = MI.getOperand(OpNo);
311
312 // If the destination is an immediate, divide by 4.
313 if (MO.isImm()) return MO.getImm() >> 2;
314
315 assert(MO.isExpr() &&
316 "getBranchTarget21OpValue expects only expressions or immediates");
317
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000318 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000319 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000320 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000321 return 0;
322}
323
324/// getBranchTarget26OpValue - Return binary encoding of the branch
325/// target operand. If the machine operand requires relocation,
326/// record the relocation and return zero.
327unsigned MipsMCCodeEmitter::
328getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
329 SmallVectorImpl<MCFixup> &Fixups,
330 const MCSubtargetInfo &STI) const {
331
332 const MCOperand &MO = MI.getOperand(OpNo);
333
334 // If the destination is an immediate, divide by 4.
335 if (MO.isImm()) return MO.getImm() >> 2;
336
337 assert(MO.isExpr() &&
338 "getBranchTarget26OpValue expects only expressions or immediates");
339
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000340 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000341 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000342 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000343 return 0;
344}
345
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000346/// getJumpOffset16OpValue - Return binary encoding of the jump
347/// target operand. If the machine operand requires relocation,
348/// record the relocation and return zero.
349unsigned MipsMCCodeEmitter::
350getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
351 SmallVectorImpl<MCFixup> &Fixups,
352 const MCSubtargetInfo &STI) const {
353
354 const MCOperand &MO = MI.getOperand(OpNo);
355
356 if (MO.isImm()) return MO.getImm();
357
358 assert(MO.isExpr() &&
359 "getJumpOffset16OpValue expects only expressions or an immediate");
360
361 // TODO: Push fixup.
362 return 0;
363}
364
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000365/// getJumpTargetOpValue - Return binary encoding of the jump
366/// target operand. If the machine operand requires relocation,
367/// record the relocation and return zero.
368unsigned MipsMCCodeEmitter::
369getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000370 SmallVectorImpl<MCFixup> &Fixups,
371 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000372
373 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000374 // If the destination is an immediate, divide by 4.
375 if (MO.isImm()) return MO.getImm()>>2;
376
Jack Carter71e6a742012-09-06 00:43:26 +0000377 assert(MO.isExpr() &&
378 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000379
380 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000381 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000382 MCFixupKind(Mips::fixup_Mips_26)));
383 return 0;
384}
385
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000386unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000387getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000388 SmallVectorImpl<MCFixup> &Fixups,
389 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000390
391 const MCOperand &MO = MI.getOperand(OpNo);
392 // If the destination is an immediate, divide by 2.
393 if (MO.isImm()) return MO.getImm() >> 1;
394
395 assert(MO.isExpr() &&
396 "getJumpTargetOpValueMM expects only expressions or an immediate");
397
398 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000399 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000400 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
401 return 0;
402}
403
404unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000405getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
406 SmallVectorImpl<MCFixup> &Fixups,
407 const MCSubtargetInfo &STI) const {
408
409 const MCOperand &MO = MI.getOperand(OpNo);
410 if (MO.isImm()) {
411 // The immediate is encoded as 'immediate << 2'.
412 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
413 assert((Res & 3) == 0);
414 return Res >> 2;
415 }
416
417 assert(MO.isExpr() &&
418 "getUImm5Lsl2Encoding expects only expressions or an immediate");
419
420 return 0;
421}
422
423unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000424getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
425 SmallVectorImpl<MCFixup> &Fixups,
426 const MCSubtargetInfo &STI) const {
427
428 const MCOperand &MO = MI.getOperand(OpNo);
429 if (MO.isImm()) {
430 int Value = MO.getImm();
431 return Value >> 2;
432 }
433
434 return 0;
435}
436
437unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000438getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
439 SmallVectorImpl<MCFixup> &Fixups,
440 const MCSubtargetInfo &STI) const {
441
442 const MCOperand &MO = MI.getOperand(OpNo);
443 if (MO.isImm()) {
444 unsigned Value = MO.getImm();
445 return Value >> 2;
446 }
447
448 return 0;
449}
450
451unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000452getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
453 SmallVectorImpl<MCFixup> &Fixups,
454 const MCSubtargetInfo &STI) const {
455
456 const MCOperand &MO = MI.getOperand(OpNo);
457 if (MO.isImm()) {
458 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
459 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
460 }
461
462 return 0;
463}
464
465unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000466getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000467 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000468 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000469
Jack Carterb5cf5902013-04-17 00:18:04 +0000470 if (Expr->EvaluateAsAbsolute(Res))
471 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000472
Akira Hatanakafe384a22012-03-27 02:33:05 +0000473 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000474 if (Kind == MCExpr::Constant) {
475 return cast<MCConstantExpr>(Expr)->getValue();
476 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000477
Akira Hatanakafe384a22012-03-27 02:33:05 +0000478 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000479 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
480 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000481 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000482 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000483
484 if (Kind == MCExpr::Target) {
485 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
486
487 Mips::Fixups FixupKind = Mips::Fixups(0);
488 switch (MipsExpr->getKind()) {
489 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000490 case MipsMCExpr::VK_Mips_HIGHEST:
491 FixupKind = Mips::fixup_Mips_HIGHEST;
492 break;
493 case MipsMCExpr::VK_Mips_HIGHER:
494 FixupKind = Mips::fixup_Mips_HIGHER;
495 break;
496 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000497 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
498 : Mips::fixup_Mips_HI16;
499 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000500 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000501 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
502 : Mips::fixup_Mips_LO16;
503 break;
504 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000505 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000506 return 0;
507 }
508
Jack Carterb5cf5902013-04-17 00:18:04 +0000509 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000510 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000511
Mark Seabornc3bd1772013-12-31 13:05:15 +0000512 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
513 default: llvm_unreachable("Unknown fixup kind!");
514 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000515 case MCSymbolRefExpr::VK_None:
516 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
517 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000518 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
519 FixupKind = Mips::fixup_Mips_GPOFF_HI;
520 break;
521 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
522 FixupKind = Mips::fixup_Mips_GPOFF_LO;
523 break;
524 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000525 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000526 : Mips::fixup_Mips_GOT_PAGE;
527 break;
528 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000529 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000530 : Mips::fixup_Mips_GOT_OFST;
531 break;
532 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000533 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000534 : Mips::fixup_Mips_GOT_DISP;
535 break;
536 case MCSymbolRefExpr::VK_Mips_GPREL:
537 FixupKind = Mips::fixup_Mips_GPREL16;
538 break;
539 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000540 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000541 : Mips::fixup_Mips_CALL16;
542 break;
543 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000544 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000545 : Mips::fixup_Mips_GOT_Global;
546 break;
547 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000548 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000549 : Mips::fixup_Mips_GOT_Local;
550 break;
551 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000552 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000553 : Mips::fixup_Mips_HI16;
554 break;
555 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000556 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000557 : Mips::fixup_Mips_LO16;
558 break;
559 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000560 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000561 : Mips::fixup_Mips_TLSGD;
562 break;
563 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000564 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000565 : Mips::fixup_Mips_TLSLDM;
566 break;
567 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000568 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000569 : Mips::fixup_Mips_DTPREL_HI;
570 break;
571 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000572 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000573 : Mips::fixup_Mips_DTPREL_LO;
574 break;
575 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
576 FixupKind = Mips::fixup_Mips_GOTTPREL;
577 break;
578 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000579 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000580 : Mips::fixup_Mips_TPREL_HI;
581 break;
582 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000583 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000584 : Mips::fixup_Mips_TPREL_LO;
585 break;
586 case MCSymbolRefExpr::VK_Mips_HIGHER:
587 FixupKind = Mips::fixup_Mips_HIGHER;
588 break;
589 case MCSymbolRefExpr::VK_Mips_HIGHEST:
590 FixupKind = Mips::fixup_Mips_HIGHEST;
591 break;
592 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
593 FixupKind = Mips::fixup_Mips_GOT_HI16;
594 break;
595 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
596 FixupKind = Mips::fixup_Mips_GOT_LO16;
597 break;
598 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
599 FixupKind = Mips::fixup_Mips_CALL_HI16;
600 break;
601 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
602 FixupKind = Mips::fixup_Mips_CALL_LO16;
603 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000604 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
605 FixupKind = Mips::fixup_MIPS_PCHI16;
606 break;
607 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
608 FixupKind = Mips::fixup_MIPS_PCLO16;
609 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000610 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000611
Jim Grosbach63661f82015-05-15 19:13:05 +0000612 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000613 return 0;
614 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000615 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000616}
617
Jack Carterb5cf5902013-04-17 00:18:04 +0000618/// getMachineOpValue - Return binary encoding of operand. If the machine
619/// operand requires relocation, record the relocation and return zero.
620unsigned MipsMCCodeEmitter::
621getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000622 SmallVectorImpl<MCFixup> &Fixups,
623 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000624 if (MO.isReg()) {
625 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000626 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000627 return RegNo;
628 } else if (MO.isImm()) {
629 return static_cast<unsigned>(MO.getImm());
630 } else if (MO.isFPImm()) {
631 return static_cast<unsigned>(APFloat(MO.getFPImm())
632 .bitcastToAPInt().getHiBits(32).getLimitedValue());
633 }
634 // MO must be an Expr.
635 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000636 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000637}
638
Matheus Almeida6b59c442013-12-05 11:06:22 +0000639/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
640/// instructions.
641unsigned
642MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000643 SmallVectorImpl<MCFixup> &Fixups,
644 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000645 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
646 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000647 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
648 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000649
650 // The immediate field of an LD/ST instruction is scaled which means it must
651 // be divided (when encoding) by the size (in bytes) of the instructions'
652 // data format.
653 // .b - 1 byte
654 // .h - 2 bytes
655 // .w - 4 bytes
656 // .d - 8 bytes
657 switch(MI.getOpcode())
658 {
659 default:
660 assert (0 && "Unexpected instruction");
661 break;
662 case Mips::LD_B:
663 case Mips::ST_B:
664 // We don't need to scale the offset in this case
665 break;
666 case Mips::LD_H:
667 case Mips::ST_H:
668 OffBits >>= 1;
669 break;
670 case Mips::LD_W:
671 case Mips::ST_W:
672 OffBits >>= 2;
673 break;
674 case Mips::LD_D:
675 case Mips::ST_D:
676 OffBits >>= 3;
677 break;
678 }
679
680 return (OffBits & 0xFFFF) | RegBits;
681}
682
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000683/// getMemEncoding - Return binary encoding of memory related operand.
684/// If the offset operand requires relocation, record the relocation.
685unsigned
686MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000687 SmallVectorImpl<MCFixup> &Fixups,
688 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000689 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
690 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000691 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
692 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000693
694 return (OffBits & 0xFFFF) | RegBits;
695}
696
Jack Carter97700972013-08-13 20:19:16 +0000697unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000698getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
699 SmallVectorImpl<MCFixup> &Fixups,
700 const MCSubtargetInfo &STI) const {
701 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
702 assert(MI.getOperand(OpNo).isReg());
703 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
704 Fixups, STI) << 4;
705 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
706 Fixups, STI);
707
708 return (OffBits & 0xF) | RegBits;
709}
710
711unsigned MipsMCCodeEmitter::
712getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
713 SmallVectorImpl<MCFixup> &Fixups,
714 const MCSubtargetInfo &STI) const {
715 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
716 assert(MI.getOperand(OpNo).isReg());
717 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
718 Fixups, STI) << 4;
719 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
720 Fixups, STI) >> 1;
721
722 return (OffBits & 0xF) | RegBits;
723}
724
725unsigned MipsMCCodeEmitter::
726getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
727 SmallVectorImpl<MCFixup> &Fixups,
728 const MCSubtargetInfo &STI) const {
729 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
730 assert(MI.getOperand(OpNo).isReg());
731 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
732 Fixups, STI) << 4;
733 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
734 Fixups, STI) >> 2;
735
736 return (OffBits & 0xF) | RegBits;
737}
738
739unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000740getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
741 SmallVectorImpl<MCFixup> &Fixups,
742 const MCSubtargetInfo &STI) const {
743 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
744 assert(MI.getOperand(OpNo).isReg() &&
745 MI.getOperand(OpNo).getReg() == Mips::SP &&
746 "Unexpected base register!");
747 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
748 Fixups, STI) >> 2;
749
750 return OffBits & 0x1F;
751}
752
753unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000754getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
755 SmallVectorImpl<MCFixup> &Fixups,
756 const MCSubtargetInfo &STI) const {
757 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
758 assert(MI.getOperand(OpNo).isReg() &&
759 MI.getOperand(OpNo).getReg() == Mips::GP &&
760 "Unexpected base register!");
761
762 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
763 Fixups, STI) >> 2;
764
765 return OffBits & 0x7F;
766}
767
768unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000769getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000770 SmallVectorImpl<MCFixup> &Fixups,
771 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000772 // opNum can be invalid if instruction had reglist as operand.
773 // MemOperand is always last operand of instruction (base + offset).
774 switch (MI.getOpcode()) {
775 default:
776 break;
777 case Mips::SWM32_MM:
778 case Mips::LWM32_MM:
779 OpNo = MI.getNumOperands() - 2;
780 break;
781 }
782
Jack Carter97700972013-08-13 20:19:16 +0000783 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
784 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000785 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
786 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000787
788 return (OffBits & 0x0FFF) | RegBits;
789}
790
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000791unsigned MipsMCCodeEmitter::
792getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
793 SmallVectorImpl<MCFixup> &Fixups,
794 const MCSubtargetInfo &STI) const {
795 // opNum can be invalid if instruction had reglist as operand
796 // MemOperand is always last operand of instruction (base + offset)
797 switch (MI.getOpcode()) {
798 default:
799 break;
800 case Mips::SWM16_MM:
801 case Mips::LWM16_MM:
802 OpNo = MI.getNumOperands() - 2;
803 break;
804 }
805
806 // Offset is encoded in bits 4-0.
807 assert(MI.getOperand(OpNo).isReg());
808 // Base register is always SP - thus it is not encoded.
809 assert(MI.getOperand(OpNo+1).isImm());
810 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
811
812 return ((OffBits >> 2) & 0x0F);
813}
814
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000815unsigned
816MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000817 SmallVectorImpl<MCFixup> &Fixups,
818 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000819 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000820 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000821 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000822}
823
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000824// FIXME: should be called getMSBEncoding
825//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000826unsigned
827MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000828 SmallVectorImpl<MCFixup> &Fixups,
829 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000830 assert(MI.getOperand(OpNo-1).isImm());
831 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000832 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
833 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000834
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000835 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000836}
837
Matheus Almeida779c5932013-11-18 12:32:49 +0000838unsigned
839MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000840 SmallVectorImpl<MCFixup> &Fixups,
841 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000842 assert(MI.getOperand(OpNo).isImm());
843 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000844 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000845}
846
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000847unsigned
848MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
849 SmallVectorImpl<MCFixup> &Fixups,
850 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000851 const MCOperand &MO = MI.getOperand(OpNo);
852 if (MO.isImm()) {
853 // The immediate is encoded as 'immediate << 2'.
854 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
855 assert((Res & 3) == 0);
856 return Res >> 2;
857 }
858
859 assert(MO.isExpr() &&
860 "getSimm19Lsl2Encoding expects only expressions or an immediate");
861
862 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000863 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000864 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
865 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000866}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000867
Zoran Jovanovic28551422014-06-09 09:49:51 +0000868unsigned
869MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
870 SmallVectorImpl<MCFixup> &Fixups,
871 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000872 const MCOperand &MO = MI.getOperand(OpNo);
873 if (MO.isImm()) {
874 // The immediate is encoded as 'immediate << 3'.
875 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
876 assert((Res & 7) == 0);
877 return Res >> 3;
878 }
879
880 assert(MO.isExpr() &&
881 "getSimm18Lsl2Encoding expects only expressions or an immediate");
882
883 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000884 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000885 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
886 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000887}
888
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000889unsigned
890MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
891 SmallVectorImpl<MCFixup> &Fixups,
892 const MCSubtargetInfo &STI) const {
893 assert(MI.getOperand(OpNo).isImm());
894 const MCOperand &MO = MI.getOperand(OpNo);
895 return MO.getImm() % 8;
896}
897
Zoran Jovanovic88531712014-11-05 17:31:00 +0000898unsigned
899MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
900 SmallVectorImpl<MCFixup> &Fixups,
901 const MCSubtargetInfo &STI) const {
902 assert(MI.getOperand(OpNo).isImm());
903 const MCOperand &MO = MI.getOperand(OpNo);
904 unsigned Value = MO.getImm();
905 switch (Value) {
906 case 128: return 0x0;
907 case 1: return 0x1;
908 case 2: return 0x2;
909 case 3: return 0x3;
910 case 4: return 0x4;
911 case 7: return 0x5;
912 case 8: return 0x6;
913 case 15: return 0x7;
914 case 16: return 0x8;
915 case 31: return 0x9;
916 case 32: return 0xa;
917 case 63: return 0xb;
918 case 64: return 0xc;
919 case 255: return 0xd;
920 case 32768: return 0xe;
921 case 65535: return 0xf;
922 }
923 llvm_unreachable("Unexpected value");
924}
925
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000926unsigned
927MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
928 SmallVectorImpl<MCFixup> &Fixups,
929 const MCSubtargetInfo &STI) const {
930 unsigned res = 0;
931
932 // Register list operand is always first operand of instruction and it is
933 // placed before memory operand (register + imm).
934
935 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
936 unsigned Reg = MI.getOperand(I).getReg();
937 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
938 if (RegNo != 31)
939 res++;
940 else
941 res |= 0x10;
942 }
943 return res;
944}
945
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000946unsigned
947MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
948 SmallVectorImpl<MCFixup> &Fixups,
949 const MCSubtargetInfo &STI) const {
950 return (MI.getNumOperands() - 4);
951}
952
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000953unsigned
954MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
955 SmallVectorImpl<MCFixup> &Fixups,
956 const MCSubtargetInfo &STI) const {
957 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
958}
959
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000960unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +0000961MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
962 SmallVectorImpl<MCFixup> &Fixups,
963 const MCSubtargetInfo &STI) const {
964 unsigned res = 0;
965
966 if (MI.getOperand(0).getReg() == Mips::A1 &&
967 MI.getOperand(1).getReg() == Mips::A2)
968 res = 0;
969 else if (MI.getOperand(0).getReg() == Mips::A1 &&
970 MI.getOperand(1).getReg() == Mips::A3)
971 res = 1;
972 else if (MI.getOperand(0).getReg() == Mips::A2 &&
973 MI.getOperand(1).getReg() == Mips::A3)
974 res = 2;
975 else if (MI.getOperand(0).getReg() == Mips::A0 &&
976 MI.getOperand(1).getReg() == Mips::S5)
977 res = 3;
978 else if (MI.getOperand(0).getReg() == Mips::A0 &&
979 MI.getOperand(1).getReg() == Mips::S6)
980 res = 4;
981 else if (MI.getOperand(0).getReg() == Mips::A0 &&
982 MI.getOperand(1).getReg() == Mips::A1)
983 res = 5;
984 else if (MI.getOperand(0).getReg() == Mips::A0 &&
985 MI.getOperand(1).getReg() == Mips::A2)
986 res = 6;
987 else if (MI.getOperand(0).getReg() == Mips::A0 &&
988 MI.getOperand(1).getReg() == Mips::A3)
989 res = 7;
990
991 return res;
992}
993
994unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000995MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
996 SmallVectorImpl<MCFixup> &Fixups,
997 const MCSubtargetInfo &STI) const {
998 const MCOperand &MO = MI.getOperand(OpNo);
999 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1000 // The immediate is encoded as 'immediate >> 2'.
1001 unsigned Res = static_cast<unsigned>(MO.getImm());
1002 assert((Res & 3) == 0);
1003 return Res >> 2;
1004}
1005
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001006#include "MipsGenMCCodeEmitter.inc"