blob: 2bb048c0c5fee9f41d96df0317bc7fe4d6378ef2 [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"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000026#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000027#include "llvm/Support/raw_ostream.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000028
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "mccodeemitter"
30
Akira Hatanakabe6a8182013-04-19 19:03:11 +000031#define GET_INSTRMAP_INFO
32#include "MipsGenInstrInfo.inc"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000033#undef GET_INSTRMAP_INFO
Akira Hatanakabe6a8182013-04-19 19:03:11 +000034
Matheus Almeida9e1450b2014-03-20 09:29:54 +000035namespace llvm {
36MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
37 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000038 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000039 return new MipsMCCodeEmitter(MCII, Ctx, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000040}
41
Matheus Almeida9e1450b2014-03-20 09:29:54 +000042MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
43 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000044 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000045 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +000046}
Matheus Almeida9e1450b2014-03-20 09:29:54 +000047} // End of namespace llvm.
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000048
49// If the D<shift> instruction has a shift amount that is greater
50// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
51static void LowerLargeShift(MCInst& Inst) {
52
53 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
54 assert(Inst.getOperand(2).isImm());
55
56 int64_t Shift = Inst.getOperand(2).getImm();
57 if (Shift <= 31)
58 return; // Do nothing
59 Shift -= 32;
60
61 // saminus32
62 Inst.getOperand(2).setImm(Shift);
63
64 switch (Inst.getOpcode()) {
65 default:
66 // Calling function is not synchronized
67 llvm_unreachable("Unexpected shift instruction");
68 case Mips::DSLL:
69 Inst.setOpcode(Mips::DSLL32);
70 return;
71 case Mips::DSRL:
72 Inst.setOpcode(Mips::DSRL32);
73 return;
74 case Mips::DSRA:
75 Inst.setOpcode(Mips::DSRA32);
76 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +000077 case Mips::DROTR:
78 Inst.setOpcode(Mips::DROTR32);
79 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000080 }
81}
82
83// Pick a DEXT or DINS instruction variant based on the pos and size operands
84static void LowerDextDins(MCInst& InstIn) {
85 int Opcode = InstIn.getOpcode();
86
87 if (Opcode == Mips::DEXT)
88 assert(InstIn.getNumOperands() == 4 &&
89 "Invalid no. of machine operands for DEXT!");
90 else // Only DEXT and DINS are possible
91 assert(InstIn.getNumOperands() == 5 &&
92 "Invalid no. of machine operands for DINS!");
93
94 assert(InstIn.getOperand(2).isImm());
95 int64_t pos = InstIn.getOperand(2).getImm();
96 assert(InstIn.getOperand(3).isImm());
97 int64_t size = InstIn.getOperand(3).getImm();
98
99 if (size <= 32) {
100 if (pos < 32) // DEXT/DINS, do nothing
101 return;
102 // DEXTU/DINSU
103 InstIn.getOperand(2).setImm(pos - 32);
104 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
105 return;
106 }
107 // DEXTM/DINSM
108 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
109 InstIn.getOperand(3).setImm(size - 32);
110 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
111 return;
112}
113
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000114bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000115 return STI.getFeatureBits() & Mips::FeatureMicroMips;
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000116}
117
118void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
119 OS << (char)C;
120}
121
122void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
123 const MCSubtargetInfo &STI,
124 raw_ostream &OS) const {
125 // Output the instruction encoding in little endian byte order.
126 // Little-endian byte ordering:
127 // mips32r2: 4 | 3 | 2 | 1
128 // microMIPS: 2 | 1 | 4 | 3
129 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
130 EmitInstruction(Val >> 16, 2, STI, OS);
131 EmitInstruction(Val, 2, STI, OS);
132 } else {
133 for (unsigned i = 0; i < Size; ++i) {
134 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
135 EmitByte((Val >> Shift) & 0xff, OS);
136 }
137 }
138}
139
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000140/// EncodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000141/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000142void MipsMCCodeEmitter::
143EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000144 SmallVectorImpl<MCFixup> &Fixups,
145 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000146{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000147
148 // Non-pseudo instructions that get changed for direct object
149 // only based on operand values.
150 // If this list of instructions get much longer we will move
151 // the check to a function call. Until then, this is more efficient.
152 MCInst TmpInst = MI;
153 switch (MI.getOpcode()) {
154 // If shift amount is >= 32 it the inst needs to be lowered further
155 case Mips::DSLL:
156 case Mips::DSRL:
157 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000158 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000159 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000160 break;
161 // Double extract instruction is chosen by pos and size operands
162 case Mips::DEXT:
163 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000164 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000165 }
166
Jack Carter97700972013-08-13 20:19:16 +0000167 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000168 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000169
170 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000171 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000172 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000173 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000174 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
175 (Opcode != Mips::SLL_MM) && !Binary)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000176 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
177
Michael Kupersteinefd7a962015-02-19 11:38:11 +0000178 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000179 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
180 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000181 if (Fixups.size() > N)
182 Fixups.pop_back();
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000183 Opcode = NewOpcode;
184 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000185 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000186 }
187 }
188
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000189 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000190
Jack Carter5b5559d2012-10-03 21:58:54 +0000191 // Get byte count of instruction
192 unsigned Size = Desc.getSize();
193 if (!Size)
194 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000195
David Woodhoused2cca112014-01-28 23:13:25 +0000196 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000197}
198
199/// getBranchTargetOpValue - Return binary encoding of the branch
200/// target operand. If the machine operand requires relocation,
201/// record the relocation and return zero.
202unsigned MipsMCCodeEmitter::
203getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000204 SmallVectorImpl<MCFixup> &Fixups,
205 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000206
207 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000208
Jack Carter4f69a0f2013-03-22 00:29:10 +0000209 // If the destination is an immediate, divide by 4.
210 if (MO.isImm()) return MO.getImm() >> 2;
211
Jack Carter71e6a742012-09-06 00:43:26 +0000212 assert(MO.isExpr() &&
213 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000214
215 const MCExpr *Expr = MO.getExpr();
216 Fixups.push_back(MCFixup::Create(0, Expr,
217 MCFixupKind(Mips::fixup_Mips_PC16)));
218 return 0;
219}
220
Jozef Kolek9761e962015-01-12 12:03:34 +0000221/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
222/// target operand. If the machine operand requires relocation,
223/// record the relocation and return zero.
224unsigned MipsMCCodeEmitter::
225getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
226 SmallVectorImpl<MCFixup> &Fixups,
227 const MCSubtargetInfo &STI) const {
228
229 const MCOperand &MO = MI.getOperand(OpNo);
230
231 // If the destination is an immediate, divide by 2.
232 if (MO.isImm()) return MO.getImm() >> 1;
233
234 assert(MO.isExpr() &&
235 "getBranchTargetOpValueMM expects only expressions or immediates");
236
237 const MCExpr *Expr = MO.getExpr();
238 Fixups.push_back(MCFixup::Create(0, Expr,
239 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
240 return 0;
241}
242
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000243/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
244/// 10-bit branch target operand. If the machine operand requires relocation,
245/// record the relocation and return zero.
246unsigned MipsMCCodeEmitter::
247getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
248 SmallVectorImpl<MCFixup> &Fixups,
249 const MCSubtargetInfo &STI) const {
250
251 const MCOperand &MO = MI.getOperand(OpNo);
252
253 // If the destination is an immediate, divide by 2.
254 if (MO.isImm()) return MO.getImm() >> 1;
255
256 assert(MO.isExpr() &&
257 "getBranchTargetOpValuePC10 expects only expressions or immediates");
258
259 const MCExpr *Expr = MO.getExpr();
260 Fixups.push_back(MCFixup::Create(0, Expr,
261 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
262 return 0;
263}
264
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000265/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
266/// target operand. If the machine operand requires relocation,
267/// record the relocation and return zero.
268unsigned MipsMCCodeEmitter::
269getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000270 SmallVectorImpl<MCFixup> &Fixups,
271 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000272
273 const MCOperand &MO = MI.getOperand(OpNo);
274
275 // If the destination is an immediate, divide by 2.
276 if (MO.isImm()) return MO.getImm() >> 1;
277
278 assert(MO.isExpr() &&
279 "getBranchTargetOpValueMM expects only expressions or immediates");
280
281 const MCExpr *Expr = MO.getExpr();
282 Fixups.push_back(MCFixup::Create(0, Expr,
283 MCFixupKind(Mips::
284 fixup_MICROMIPS_PC16_S1)));
285 return 0;
286}
287
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000288/// getBranchTarget21OpValue - Return binary encoding of the branch
289/// target operand. If the machine operand requires relocation,
290/// record the relocation and return zero.
291unsigned MipsMCCodeEmitter::
292getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
293 SmallVectorImpl<MCFixup> &Fixups,
294 const MCSubtargetInfo &STI) const {
295
296 const MCOperand &MO = MI.getOperand(OpNo);
297
298 // If the destination is an immediate, divide by 4.
299 if (MO.isImm()) return MO.getImm() >> 2;
300
301 assert(MO.isExpr() &&
302 "getBranchTarget21OpValue expects only expressions or immediates");
303
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000304 const MCExpr *Expr = MO.getExpr();
305 Fixups.push_back(MCFixup::Create(0, Expr,
306 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000307 return 0;
308}
309
310/// getBranchTarget26OpValue - Return binary encoding of the branch
311/// target operand. If the machine operand requires relocation,
312/// record the relocation and return zero.
313unsigned MipsMCCodeEmitter::
314getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
315 SmallVectorImpl<MCFixup> &Fixups,
316 const MCSubtargetInfo &STI) const {
317
318 const MCOperand &MO = MI.getOperand(OpNo);
319
320 // If the destination is an immediate, divide by 4.
321 if (MO.isImm()) return MO.getImm() >> 2;
322
323 assert(MO.isExpr() &&
324 "getBranchTarget26OpValue expects only expressions or immediates");
325
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000326 const MCExpr *Expr = MO.getExpr();
327 Fixups.push_back(MCFixup::Create(0, Expr,
328 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000329 return 0;
330}
331
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000332/// getJumpOffset16OpValue - Return binary encoding of the jump
333/// target operand. If the machine operand requires relocation,
334/// record the relocation and return zero.
335unsigned MipsMCCodeEmitter::
336getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
337 SmallVectorImpl<MCFixup> &Fixups,
338 const MCSubtargetInfo &STI) const {
339
340 const MCOperand &MO = MI.getOperand(OpNo);
341
342 if (MO.isImm()) return MO.getImm();
343
344 assert(MO.isExpr() &&
345 "getJumpOffset16OpValue expects only expressions or an immediate");
346
347 // TODO: Push fixup.
348 return 0;
349}
350
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000351/// getJumpTargetOpValue - Return binary encoding of the jump
352/// target operand. If the machine operand requires relocation,
353/// record the relocation and return zero.
354unsigned MipsMCCodeEmitter::
355getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000356 SmallVectorImpl<MCFixup> &Fixups,
357 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000358
359 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000360 // If the destination is an immediate, divide by 4.
361 if (MO.isImm()) return MO.getImm()>>2;
362
Jack Carter71e6a742012-09-06 00:43:26 +0000363 assert(MO.isExpr() &&
364 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000365
366 const MCExpr *Expr = MO.getExpr();
367 Fixups.push_back(MCFixup::Create(0, Expr,
368 MCFixupKind(Mips::fixup_Mips_26)));
369 return 0;
370}
371
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000372unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000373getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000374 SmallVectorImpl<MCFixup> &Fixups,
375 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000376
377 const MCOperand &MO = MI.getOperand(OpNo);
378 // If the destination is an immediate, divide by 2.
379 if (MO.isImm()) return MO.getImm() >> 1;
380
381 assert(MO.isExpr() &&
382 "getJumpTargetOpValueMM expects only expressions or an immediate");
383
384 const MCExpr *Expr = MO.getExpr();
385 Fixups.push_back(MCFixup::Create(0, Expr,
386 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
387 return 0;
388}
389
390unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000391getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
392 SmallVectorImpl<MCFixup> &Fixups,
393 const MCSubtargetInfo &STI) const {
394
395 const MCOperand &MO = MI.getOperand(OpNo);
396 if (MO.isImm()) {
397 // The immediate is encoded as 'immediate << 2'.
398 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
399 assert((Res & 3) == 0);
400 return Res >> 2;
401 }
402
403 assert(MO.isExpr() &&
404 "getUImm5Lsl2Encoding expects only expressions or an immediate");
405
406 return 0;
407}
408
409unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000410getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
411 SmallVectorImpl<MCFixup> &Fixups,
412 const MCSubtargetInfo &STI) const {
413
414 const MCOperand &MO = MI.getOperand(OpNo);
415 if (MO.isImm()) {
416 int Value = MO.getImm();
417 return Value >> 2;
418 }
419
420 return 0;
421}
422
423unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000424getUImm6Lsl2Encoding(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 unsigned Value = MO.getImm();
431 return Value >> 2;
432 }
433
434 return 0;
435}
436
437unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000438getSImm9AddiuspValue(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 Binary = (MO.getImm() >> 2) & 0x0000ffff;
445 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
446 }
447
448 return 0;
449}
450
451unsigned MipsMCCodeEmitter::
David Woodhouse3fa98a62014-01-28 23:13:18 +0000452getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
453 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000454 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000455
Jack Carterb5cf5902013-04-17 00:18:04 +0000456 if (Expr->EvaluateAsAbsolute(Res))
457 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000458
Akira Hatanakafe384a22012-03-27 02:33:05 +0000459 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000460 if (Kind == MCExpr::Constant) {
461 return cast<MCConstantExpr>(Expr)->getValue();
462 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000463
Akira Hatanakafe384a22012-03-27 02:33:05 +0000464 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000465 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
466 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000467 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000468 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000469
470 if (Kind == MCExpr::Target) {
471 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
472
473 Mips::Fixups FixupKind = Mips::Fixups(0);
474 switch (MipsExpr->getKind()) {
475 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000476 case MipsMCExpr::VK_Mips_HIGHEST:
477 FixupKind = Mips::fixup_Mips_HIGHEST;
478 break;
479 case MipsMCExpr::VK_Mips_HIGHER:
480 FixupKind = Mips::fixup_Mips_HIGHER;
481 break;
482 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000483 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
484 : Mips::fixup_Mips_HI16;
485 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000486 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000487 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
488 : Mips::fixup_Mips_LO16;
489 break;
490 }
491 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
492 return 0;
493 }
494
Jack Carterb5cf5902013-04-17 00:18:04 +0000495 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000496 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000497
Mark Seabornc3bd1772013-12-31 13:05:15 +0000498 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
499 default: llvm_unreachable("Unknown fixup kind!");
500 break;
501 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
502 FixupKind = Mips::fixup_Mips_GPOFF_HI;
503 break;
504 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
505 FixupKind = Mips::fixup_Mips_GPOFF_LO;
506 break;
507 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000508 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000509 : Mips::fixup_Mips_GOT_PAGE;
510 break;
511 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000512 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000513 : Mips::fixup_Mips_GOT_OFST;
514 break;
515 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000516 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000517 : Mips::fixup_Mips_GOT_DISP;
518 break;
519 case MCSymbolRefExpr::VK_Mips_GPREL:
520 FixupKind = Mips::fixup_Mips_GPREL16;
521 break;
522 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000523 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000524 : Mips::fixup_Mips_CALL16;
525 break;
526 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000527 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000528 : Mips::fixup_Mips_GOT_Global;
529 break;
530 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000531 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000532 : Mips::fixup_Mips_GOT_Local;
533 break;
534 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000535 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000536 : Mips::fixup_Mips_HI16;
537 break;
538 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000539 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000540 : Mips::fixup_Mips_LO16;
541 break;
542 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000543 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000544 : Mips::fixup_Mips_TLSGD;
545 break;
546 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000547 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000548 : Mips::fixup_Mips_TLSLDM;
549 break;
550 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000551 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000552 : Mips::fixup_Mips_DTPREL_HI;
553 break;
554 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000555 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000556 : Mips::fixup_Mips_DTPREL_LO;
557 break;
558 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
559 FixupKind = Mips::fixup_Mips_GOTTPREL;
560 break;
561 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000562 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000563 : Mips::fixup_Mips_TPREL_HI;
564 break;
565 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000566 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000567 : Mips::fixup_Mips_TPREL_LO;
568 break;
569 case MCSymbolRefExpr::VK_Mips_HIGHER:
570 FixupKind = Mips::fixup_Mips_HIGHER;
571 break;
572 case MCSymbolRefExpr::VK_Mips_HIGHEST:
573 FixupKind = Mips::fixup_Mips_HIGHEST;
574 break;
575 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
576 FixupKind = Mips::fixup_Mips_GOT_HI16;
577 break;
578 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
579 FixupKind = Mips::fixup_Mips_GOT_LO16;
580 break;
581 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
582 FixupKind = Mips::fixup_Mips_CALL_HI16;
583 break;
584 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
585 FixupKind = Mips::fixup_Mips_CALL_LO16;
586 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000587 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
588 FixupKind = Mips::fixup_MIPS_PCHI16;
589 break;
590 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
591 FixupKind = Mips::fixup_MIPS_PCLO16;
592 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000593 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000594
Jack Carterb5cf5902013-04-17 00:18:04 +0000595 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
596 return 0;
597 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000598 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000599}
600
Jack Carterb5cf5902013-04-17 00:18:04 +0000601/// getMachineOpValue - Return binary encoding of operand. If the machine
602/// operand requires relocation, record the relocation and return zero.
603unsigned MipsMCCodeEmitter::
604getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000605 SmallVectorImpl<MCFixup> &Fixups,
606 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000607 if (MO.isReg()) {
608 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000609 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000610 return RegNo;
611 } else if (MO.isImm()) {
612 return static_cast<unsigned>(MO.getImm());
613 } else if (MO.isFPImm()) {
614 return static_cast<unsigned>(APFloat(MO.getFPImm())
615 .bitcastToAPInt().getHiBits(32).getLimitedValue());
616 }
617 // MO must be an Expr.
618 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000619 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000620}
621
Matheus Almeida6b59c442013-12-05 11:06:22 +0000622/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
623/// instructions.
624unsigned
625MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000626 SmallVectorImpl<MCFixup> &Fixups,
627 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000628 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
629 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000630 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
631 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000632
633 // The immediate field of an LD/ST instruction is scaled which means it must
634 // be divided (when encoding) by the size (in bytes) of the instructions'
635 // data format.
636 // .b - 1 byte
637 // .h - 2 bytes
638 // .w - 4 bytes
639 // .d - 8 bytes
640 switch(MI.getOpcode())
641 {
642 default:
643 assert (0 && "Unexpected instruction");
644 break;
645 case Mips::LD_B:
646 case Mips::ST_B:
647 // We don't need to scale the offset in this case
648 break;
649 case Mips::LD_H:
650 case Mips::ST_H:
651 OffBits >>= 1;
652 break;
653 case Mips::LD_W:
654 case Mips::ST_W:
655 OffBits >>= 2;
656 break;
657 case Mips::LD_D:
658 case Mips::ST_D:
659 OffBits >>= 3;
660 break;
661 }
662
663 return (OffBits & 0xFFFF) | RegBits;
664}
665
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000666/// getMemEncoding - Return binary encoding of memory related operand.
667/// If the offset operand requires relocation, record the relocation.
668unsigned
669MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000670 SmallVectorImpl<MCFixup> &Fixups,
671 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000672 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
673 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000674 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
675 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000676
677 return (OffBits & 0xFFFF) | RegBits;
678}
679
Jack Carter97700972013-08-13 20:19:16 +0000680unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000681getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
682 SmallVectorImpl<MCFixup> &Fixups,
683 const MCSubtargetInfo &STI) const {
684 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
685 assert(MI.getOperand(OpNo).isReg());
686 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
687 Fixups, STI) << 4;
688 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
689 Fixups, STI);
690
691 return (OffBits & 0xF) | RegBits;
692}
693
694unsigned MipsMCCodeEmitter::
695getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
696 SmallVectorImpl<MCFixup> &Fixups,
697 const MCSubtargetInfo &STI) const {
698 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
699 assert(MI.getOperand(OpNo).isReg());
700 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
701 Fixups, STI) << 4;
702 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
703 Fixups, STI) >> 1;
704
705 return (OffBits & 0xF) | RegBits;
706}
707
708unsigned MipsMCCodeEmitter::
709getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
710 SmallVectorImpl<MCFixup> &Fixups,
711 const MCSubtargetInfo &STI) const {
712 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
713 assert(MI.getOperand(OpNo).isReg());
714 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
715 Fixups, STI) << 4;
716 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
717 Fixups, STI) >> 2;
718
719 return (OffBits & 0xF) | RegBits;
720}
721
722unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000723getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
724 SmallVectorImpl<MCFixup> &Fixups,
725 const MCSubtargetInfo &STI) const {
726 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
727 assert(MI.getOperand(OpNo).isReg() &&
728 MI.getOperand(OpNo).getReg() == Mips::SP &&
729 "Unexpected base register!");
730 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
731 Fixups, STI) >> 2;
732
733 return OffBits & 0x1F;
734}
735
736unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000737getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
738 SmallVectorImpl<MCFixup> &Fixups,
739 const MCSubtargetInfo &STI) const {
740 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
741 assert(MI.getOperand(OpNo).isReg() &&
742 MI.getOperand(OpNo).getReg() == Mips::GP &&
743 "Unexpected base register!");
744
745 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
746 Fixups, STI) >> 2;
747
748 return OffBits & 0x7F;
749}
750
751unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000752getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000753 SmallVectorImpl<MCFixup> &Fixups,
754 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000755 // opNum can be invalid if instruction had reglist as operand.
756 // MemOperand is always last operand of instruction (base + offset).
757 switch (MI.getOpcode()) {
758 default:
759 break;
760 case Mips::SWM32_MM:
761 case Mips::LWM32_MM:
762 OpNo = MI.getNumOperands() - 2;
763 break;
764 }
765
Jack Carter97700972013-08-13 20:19:16 +0000766 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
767 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000768 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
769 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000770
771 return (OffBits & 0x0FFF) | RegBits;
772}
773
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000774unsigned MipsMCCodeEmitter::
775getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
776 SmallVectorImpl<MCFixup> &Fixups,
777 const MCSubtargetInfo &STI) const {
778 // opNum can be invalid if instruction had reglist as operand
779 // MemOperand is always last operand of instruction (base + offset)
780 switch (MI.getOpcode()) {
781 default:
782 break;
783 case Mips::SWM16_MM:
784 case Mips::LWM16_MM:
785 OpNo = MI.getNumOperands() - 2;
786 break;
787 }
788
789 // Offset is encoded in bits 4-0.
790 assert(MI.getOperand(OpNo).isReg());
791 // Base register is always SP - thus it is not encoded.
792 assert(MI.getOperand(OpNo+1).isImm());
793 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
794
795 return ((OffBits >> 2) & 0x0F);
796}
797
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000798unsigned
799MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000800 SmallVectorImpl<MCFixup> &Fixups,
801 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000802 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000803 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000804 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000805}
806
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000807// FIXME: should be called getMSBEncoding
808//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000809unsigned
810MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000811 SmallVectorImpl<MCFixup> &Fixups,
812 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000813 assert(MI.getOperand(OpNo-1).isImm());
814 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000815 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
816 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000817
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000818 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000819}
820
Matheus Almeida779c5932013-11-18 12:32:49 +0000821unsigned
822MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000823 SmallVectorImpl<MCFixup> &Fixups,
824 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000825 assert(MI.getOperand(OpNo).isImm());
826 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000827 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000828}
829
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000830unsigned
831MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
832 SmallVectorImpl<MCFixup> &Fixups,
833 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000834 const MCOperand &MO = MI.getOperand(OpNo);
835 if (MO.isImm()) {
836 // The immediate is encoded as 'immediate << 2'.
837 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
838 assert((Res & 3) == 0);
839 return Res >> 2;
840 }
841
842 assert(MO.isExpr() &&
843 "getSimm19Lsl2Encoding expects only expressions or an immediate");
844
845 const MCExpr *Expr = MO.getExpr();
846 Fixups.push_back(MCFixup::Create(0, Expr,
847 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
848 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000849}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000850
Zoran Jovanovic28551422014-06-09 09:49:51 +0000851unsigned
852MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
853 SmallVectorImpl<MCFixup> &Fixups,
854 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000855 const MCOperand &MO = MI.getOperand(OpNo);
856 if (MO.isImm()) {
857 // The immediate is encoded as 'immediate << 3'.
858 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
859 assert((Res & 7) == 0);
860 return Res >> 3;
861 }
862
863 assert(MO.isExpr() &&
864 "getSimm18Lsl2Encoding expects only expressions or an immediate");
865
866 const MCExpr *Expr = MO.getExpr();
867 Fixups.push_back(MCFixup::Create(0, Expr,
868 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
869 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000870}
871
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000872unsigned
873MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
874 SmallVectorImpl<MCFixup> &Fixups,
875 const MCSubtargetInfo &STI) const {
876 assert(MI.getOperand(OpNo).isImm());
877 const MCOperand &MO = MI.getOperand(OpNo);
878 return MO.getImm() % 8;
879}
880
Zoran Jovanovic88531712014-11-05 17:31:00 +0000881unsigned
882MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
883 SmallVectorImpl<MCFixup> &Fixups,
884 const MCSubtargetInfo &STI) const {
885 assert(MI.getOperand(OpNo).isImm());
886 const MCOperand &MO = MI.getOperand(OpNo);
887 unsigned Value = MO.getImm();
888 switch (Value) {
889 case 128: return 0x0;
890 case 1: return 0x1;
891 case 2: return 0x2;
892 case 3: return 0x3;
893 case 4: return 0x4;
894 case 7: return 0x5;
895 case 8: return 0x6;
896 case 15: return 0x7;
897 case 16: return 0x8;
898 case 31: return 0x9;
899 case 32: return 0xa;
900 case 63: return 0xb;
901 case 64: return 0xc;
902 case 255: return 0xd;
903 case 32768: return 0xe;
904 case 65535: return 0xf;
905 }
906 llvm_unreachable("Unexpected value");
907}
908
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000909unsigned
910MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
911 SmallVectorImpl<MCFixup> &Fixups,
912 const MCSubtargetInfo &STI) const {
913 unsigned res = 0;
914
915 // Register list operand is always first operand of instruction and it is
916 // placed before memory operand (register + imm).
917
918 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
919 unsigned Reg = MI.getOperand(I).getReg();
920 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
921 if (RegNo != 31)
922 res++;
923 else
924 res |= 0x10;
925 }
926 return res;
927}
928
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000929unsigned
930MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
931 SmallVectorImpl<MCFixup> &Fixups,
932 const MCSubtargetInfo &STI) const {
933 return (MI.getNumOperands() - 4);
934}
935
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000936unsigned
937MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
938 SmallVectorImpl<MCFixup> &Fixups,
939 const MCSubtargetInfo &STI) const {
940 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
941}
942
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000943unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +0000944MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
945 SmallVectorImpl<MCFixup> &Fixups,
946 const MCSubtargetInfo &STI) const {
947 unsigned res = 0;
948
949 if (MI.getOperand(0).getReg() == Mips::A1 &&
950 MI.getOperand(1).getReg() == Mips::A2)
951 res = 0;
952 else if (MI.getOperand(0).getReg() == Mips::A1 &&
953 MI.getOperand(1).getReg() == Mips::A3)
954 res = 1;
955 else if (MI.getOperand(0).getReg() == Mips::A2 &&
956 MI.getOperand(1).getReg() == Mips::A3)
957 res = 2;
958 else if (MI.getOperand(0).getReg() == Mips::A0 &&
959 MI.getOperand(1).getReg() == Mips::S5)
960 res = 3;
961 else if (MI.getOperand(0).getReg() == Mips::A0 &&
962 MI.getOperand(1).getReg() == Mips::S6)
963 res = 4;
964 else if (MI.getOperand(0).getReg() == Mips::A0 &&
965 MI.getOperand(1).getReg() == Mips::A1)
966 res = 5;
967 else if (MI.getOperand(0).getReg() == Mips::A0 &&
968 MI.getOperand(1).getReg() == Mips::A2)
969 res = 6;
970 else if (MI.getOperand(0).getReg() == Mips::A0 &&
971 MI.getOperand(1).getReg() == Mips::A3)
972 res = 7;
973
974 return res;
975}
976
977unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000978MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
979 SmallVectorImpl<MCFixup> &Fixups,
980 const MCSubtargetInfo &STI) const {
981 const MCOperand &MO = MI.getOperand(OpNo);
982 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
983 // The immediate is encoded as 'immediate >> 2'.
984 unsigned Res = static_cast<unsigned>(MO.getImm());
985 assert((Res & 3) == 0);
986 return Res >> 2;
987}
988
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000989#include "MipsGenMCCodeEmitter.inc"