blob: 223a95a698a72eb585e5d5455a5f4620d28044f6 [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"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000025#include "llvm/MC/MCFixup.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,
38 const MCSubtargetInfo &STI,
39 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,
45 const MCSubtargetInfo &STI,
46 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000047 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +000048}
Matheus Almeida9e1450b2014-03-20 09:29:54 +000049} // End of namespace llvm.
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000050
51// If the D<shift> instruction has a shift amount that is greater
52// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
53static void LowerLargeShift(MCInst& Inst) {
54
55 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
56 assert(Inst.getOperand(2).isImm());
57
58 int64_t Shift = Inst.getOperand(2).getImm();
59 if (Shift <= 31)
60 return; // Do nothing
61 Shift -= 32;
62
63 // saminus32
64 Inst.getOperand(2).setImm(Shift);
65
66 switch (Inst.getOpcode()) {
67 default:
68 // Calling function is not synchronized
69 llvm_unreachable("Unexpected shift instruction");
70 case Mips::DSLL:
71 Inst.setOpcode(Mips::DSLL32);
72 return;
73 case Mips::DSRL:
74 Inst.setOpcode(Mips::DSRL32);
75 return;
76 case Mips::DSRA:
77 Inst.setOpcode(Mips::DSRA32);
78 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +000079 case Mips::DROTR:
80 Inst.setOpcode(Mips::DROTR32);
81 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000082 }
83}
84
85// Pick a DEXT or DINS instruction variant based on the pos and size operands
86static void LowerDextDins(MCInst& InstIn) {
87 int Opcode = InstIn.getOpcode();
88
89 if (Opcode == Mips::DEXT)
90 assert(InstIn.getNumOperands() == 4 &&
91 "Invalid no. of machine operands for DEXT!");
92 else // Only DEXT and DINS are possible
93 assert(InstIn.getNumOperands() == 5 &&
94 "Invalid no. of machine operands for DINS!");
95
96 assert(InstIn.getOperand(2).isImm());
97 int64_t pos = InstIn.getOperand(2).getImm();
98 assert(InstIn.getOperand(3).isImm());
99 int64_t size = InstIn.getOperand(3).getImm();
100
101 if (size <= 32) {
102 if (pos < 32) // DEXT/DINS, do nothing
103 return;
104 // DEXTU/DINSU
105 InstIn.getOperand(2).setImm(pos - 32);
106 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
107 return;
108 }
109 // DEXTM/DINSM
110 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
111 InstIn.getOperand(3).setImm(size - 32);
112 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
113 return;
114}
115
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000116bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
117 return STI.getFeatureBits() & Mips::FeatureMicroMips;
118}
119
120void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
121 OS << (char)C;
122}
123
124void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
125 const MCSubtargetInfo &STI,
126 raw_ostream &OS) const {
127 // Output the instruction encoding in little endian byte order.
128 // Little-endian byte ordering:
129 // mips32r2: 4 | 3 | 2 | 1
130 // microMIPS: 2 | 1 | 4 | 3
131 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
132 EmitInstruction(Val >> 16, 2, STI, OS);
133 EmitInstruction(Val, 2, STI, OS);
134 } else {
135 for (unsigned i = 0; i < Size; ++i) {
136 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
137 EmitByte((Val >> Shift) & 0xff, OS);
138 }
139 }
140}
141
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000142/// EncodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000143/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000144void MipsMCCodeEmitter::
145EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000146 SmallVectorImpl<MCFixup> &Fixups,
147 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000148{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000149
150 // Non-pseudo instructions that get changed for direct object
151 // only based on operand values.
152 // If this list of instructions get much longer we will move
153 // the check to a function call. Until then, this is more efficient.
154 MCInst TmpInst = MI;
155 switch (MI.getOpcode()) {
156 // If shift amount is >= 32 it the inst needs to be lowered further
157 case Mips::DSLL:
158 case Mips::DSRL:
159 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000160 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000161 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000162 break;
163 // Double extract instruction is chosen by pos and size operands
164 case Mips::DEXT:
165 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000166 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000167 }
168
Jack Carter97700972013-08-13 20:19:16 +0000169 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000170 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000171
172 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000173 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000174 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000175 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000176 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
177 (Opcode != Mips::SLL_MM) && !Binary)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000178 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
179
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000180 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
181 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
182 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000183 if (Fixups.size() > N)
184 Fixups.pop_back();
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000185 Opcode = NewOpcode;
186 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000187 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000188 }
189 }
190
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000191 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000192
Jack Carter5b5559d2012-10-03 21:58:54 +0000193 // Get byte count of instruction
194 unsigned Size = Desc.getSize();
195 if (!Size)
196 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000197
David Woodhoused2cca112014-01-28 23:13:25 +0000198 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000199}
200
201/// getBranchTargetOpValue - Return binary encoding of the branch
202/// target operand. If the machine operand requires relocation,
203/// record the relocation and return zero.
204unsigned MipsMCCodeEmitter::
205getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000206 SmallVectorImpl<MCFixup> &Fixups,
207 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208
209 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000210
Jack Carter4f69a0f2013-03-22 00:29:10 +0000211 // If the destination is an immediate, divide by 4.
212 if (MO.isImm()) return MO.getImm() >> 2;
213
Jack Carter71e6a742012-09-06 00:43:26 +0000214 assert(MO.isExpr() &&
215 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000216
217 const MCExpr *Expr = MO.getExpr();
218 Fixups.push_back(MCFixup::Create(0, Expr,
219 MCFixupKind(Mips::fixup_Mips_PC16)));
220 return 0;
221}
222
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000223/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
224/// target operand. If the machine operand requires relocation,
225/// record the relocation and return zero.
226unsigned MipsMCCodeEmitter::
227getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000228 SmallVectorImpl<MCFixup> &Fixups,
229 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000230
231 const MCOperand &MO = MI.getOperand(OpNo);
232
233 // If the destination is an immediate, divide by 2.
234 if (MO.isImm()) return MO.getImm() >> 1;
235
236 assert(MO.isExpr() &&
237 "getBranchTargetOpValueMM expects only expressions or immediates");
238
239 const MCExpr *Expr = MO.getExpr();
240 Fixups.push_back(MCFixup::Create(0, Expr,
241 MCFixupKind(Mips::
242 fixup_MICROMIPS_PC16_S1)));
243 return 0;
244}
245
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000246/// getBranchTarget21OpValue - Return binary encoding of the branch
247/// target operand. If the machine operand requires relocation,
248/// record the relocation and return zero.
249unsigned MipsMCCodeEmitter::
250getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
251 SmallVectorImpl<MCFixup> &Fixups,
252 const MCSubtargetInfo &STI) const {
253
254 const MCOperand &MO = MI.getOperand(OpNo);
255
256 // If the destination is an immediate, divide by 4.
257 if (MO.isImm()) return MO.getImm() >> 2;
258
259 assert(MO.isExpr() &&
260 "getBranchTarget21OpValue expects only expressions or immediates");
261
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000262 const MCExpr *Expr = MO.getExpr();
263 Fixups.push_back(MCFixup::Create(0, Expr,
264 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000265 return 0;
266}
267
268/// getBranchTarget26OpValue - Return binary encoding of the branch
269/// target operand. If the machine operand requires relocation,
270/// record the relocation and return zero.
271unsigned MipsMCCodeEmitter::
272getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
273 SmallVectorImpl<MCFixup> &Fixups,
274 const MCSubtargetInfo &STI) const {
275
276 const MCOperand &MO = MI.getOperand(OpNo);
277
278 // If the destination is an immediate, divide by 4.
279 if (MO.isImm()) return MO.getImm() >> 2;
280
281 assert(MO.isExpr() &&
282 "getBranchTarget26OpValue expects only expressions or immediates");
283
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000284 const MCExpr *Expr = MO.getExpr();
285 Fixups.push_back(MCFixup::Create(0, Expr,
286 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000287 return 0;
288}
289
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000290/// getJumpOffset16OpValue - Return binary encoding of the jump
291/// target operand. If the machine operand requires relocation,
292/// record the relocation and return zero.
293unsigned MipsMCCodeEmitter::
294getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
295 SmallVectorImpl<MCFixup> &Fixups,
296 const MCSubtargetInfo &STI) const {
297
298 const MCOperand &MO = MI.getOperand(OpNo);
299
300 if (MO.isImm()) return MO.getImm();
301
302 assert(MO.isExpr() &&
303 "getJumpOffset16OpValue expects only expressions or an immediate");
304
305 // TODO: Push fixup.
306 return 0;
307}
308
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000309/// getJumpTargetOpValue - Return binary encoding of the jump
310/// target operand. If the machine operand requires relocation,
311/// record the relocation and return zero.
312unsigned MipsMCCodeEmitter::
313getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000314 SmallVectorImpl<MCFixup> &Fixups,
315 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000316
317 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000318 // If the destination is an immediate, divide by 4.
319 if (MO.isImm()) return MO.getImm()>>2;
320
Jack Carter71e6a742012-09-06 00:43:26 +0000321 assert(MO.isExpr() &&
322 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000323
324 const MCExpr *Expr = MO.getExpr();
325 Fixups.push_back(MCFixup::Create(0, Expr,
326 MCFixupKind(Mips::fixup_Mips_26)));
327 return 0;
328}
329
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000330unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000331getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000332 SmallVectorImpl<MCFixup> &Fixups,
333 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000334
335 const MCOperand &MO = MI.getOperand(OpNo);
336 // If the destination is an immediate, divide by 2.
337 if (MO.isImm()) return MO.getImm() >> 1;
338
339 assert(MO.isExpr() &&
340 "getJumpTargetOpValueMM expects only expressions or an immediate");
341
342 const MCExpr *Expr = MO.getExpr();
343 Fixups.push_back(MCFixup::Create(0, Expr,
344 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
345 return 0;
346}
347
348unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000349getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
350 SmallVectorImpl<MCFixup> &Fixups,
351 const MCSubtargetInfo &STI) const {
352
353 const MCOperand &MO = MI.getOperand(OpNo);
354 if (MO.isImm()) {
355 // The immediate is encoded as 'immediate << 2'.
356 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
357 assert((Res & 3) == 0);
358 return Res >> 2;
359 }
360
361 assert(MO.isExpr() &&
362 "getUImm5Lsl2Encoding expects only expressions or an immediate");
363
364 return 0;
365}
366
367unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000368getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
369 SmallVectorImpl<MCFixup> &Fixups,
370 const MCSubtargetInfo &STI) const {
371
372 const MCOperand &MO = MI.getOperand(OpNo);
373 if (MO.isImm()) {
374 int Value = MO.getImm();
375 return Value >> 2;
376 }
377
378 return 0;
379}
380
381unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000382getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
383 SmallVectorImpl<MCFixup> &Fixups,
384 const MCSubtargetInfo &STI) const {
385
386 const MCOperand &MO = MI.getOperand(OpNo);
387 if (MO.isImm()) {
388 unsigned Value = MO.getImm();
389 return Value >> 2;
390 }
391
392 return 0;
393}
394
395unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000396getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
397 SmallVectorImpl<MCFixup> &Fixups,
398 const MCSubtargetInfo &STI) const {
399
400 const MCOperand &MO = MI.getOperand(OpNo);
401 if (MO.isImm()) {
402 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
403 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
404 }
405
406 return 0;
407}
408
409unsigned MipsMCCodeEmitter::
David Woodhouse3fa98a62014-01-28 23:13:18 +0000410getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
411 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000412 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000413
Jack Carterb5cf5902013-04-17 00:18:04 +0000414 if (Expr->EvaluateAsAbsolute(Res))
415 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000416
Akira Hatanakafe384a22012-03-27 02:33:05 +0000417 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000418 if (Kind == MCExpr::Constant) {
419 return cast<MCConstantExpr>(Expr)->getValue();
420 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000421
Akira Hatanakafe384a22012-03-27 02:33:05 +0000422 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000423 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
424 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000425 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000426 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000427
428 if (Kind == MCExpr::Target) {
429 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
430
431 Mips::Fixups FixupKind = Mips::Fixups(0);
432 switch (MipsExpr->getKind()) {
433 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000434 case MipsMCExpr::VK_Mips_HIGHEST:
435 FixupKind = Mips::fixup_Mips_HIGHEST;
436 break;
437 case MipsMCExpr::VK_Mips_HIGHER:
438 FixupKind = Mips::fixup_Mips_HIGHER;
439 break;
440 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000441 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
442 : Mips::fixup_Mips_HI16;
443 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000444 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000445 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
446 : Mips::fixup_Mips_LO16;
447 break;
448 }
449 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
450 return 0;
451 }
452
Jack Carterb5cf5902013-04-17 00:18:04 +0000453 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000454 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000455
Mark Seabornc3bd1772013-12-31 13:05:15 +0000456 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
457 default: llvm_unreachable("Unknown fixup kind!");
458 break;
459 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
460 FixupKind = Mips::fixup_Mips_GPOFF_HI;
461 break;
462 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
463 FixupKind = Mips::fixup_Mips_GPOFF_LO;
464 break;
465 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000466 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000467 : Mips::fixup_Mips_GOT_PAGE;
468 break;
469 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000470 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000471 : Mips::fixup_Mips_GOT_OFST;
472 break;
473 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000474 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000475 : Mips::fixup_Mips_GOT_DISP;
476 break;
477 case MCSymbolRefExpr::VK_Mips_GPREL:
478 FixupKind = Mips::fixup_Mips_GPREL16;
479 break;
480 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000481 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000482 : Mips::fixup_Mips_CALL16;
483 break;
484 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000485 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000486 : Mips::fixup_Mips_GOT_Global;
487 break;
488 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000489 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000490 : Mips::fixup_Mips_GOT_Local;
491 break;
492 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000493 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000494 : Mips::fixup_Mips_HI16;
495 break;
496 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000497 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000498 : Mips::fixup_Mips_LO16;
499 break;
500 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000501 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000502 : Mips::fixup_Mips_TLSGD;
503 break;
504 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000505 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000506 : Mips::fixup_Mips_TLSLDM;
507 break;
508 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000509 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000510 : Mips::fixup_Mips_DTPREL_HI;
511 break;
512 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000513 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000514 : Mips::fixup_Mips_DTPREL_LO;
515 break;
516 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
517 FixupKind = Mips::fixup_Mips_GOTTPREL;
518 break;
519 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000520 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000521 : Mips::fixup_Mips_TPREL_HI;
522 break;
523 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000524 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000525 : Mips::fixup_Mips_TPREL_LO;
526 break;
527 case MCSymbolRefExpr::VK_Mips_HIGHER:
528 FixupKind = Mips::fixup_Mips_HIGHER;
529 break;
530 case MCSymbolRefExpr::VK_Mips_HIGHEST:
531 FixupKind = Mips::fixup_Mips_HIGHEST;
532 break;
533 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
534 FixupKind = Mips::fixup_Mips_GOT_HI16;
535 break;
536 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
537 FixupKind = Mips::fixup_Mips_GOT_LO16;
538 break;
539 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
540 FixupKind = Mips::fixup_Mips_CALL_HI16;
541 break;
542 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
543 FixupKind = Mips::fixup_Mips_CALL_LO16;
544 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000545 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
546 FixupKind = Mips::fixup_MIPS_PCHI16;
547 break;
548 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
549 FixupKind = Mips::fixup_MIPS_PCLO16;
550 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000551 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000552
Jack Carterb5cf5902013-04-17 00:18:04 +0000553 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
554 return 0;
555 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000556 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000557}
558
Jack Carterb5cf5902013-04-17 00:18:04 +0000559/// getMachineOpValue - Return binary encoding of operand. If the machine
560/// operand requires relocation, record the relocation and return zero.
561unsigned MipsMCCodeEmitter::
562getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000563 SmallVectorImpl<MCFixup> &Fixups,
564 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000565 if (MO.isReg()) {
566 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000567 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000568 return RegNo;
569 } else if (MO.isImm()) {
570 return static_cast<unsigned>(MO.getImm());
571 } else if (MO.isFPImm()) {
572 return static_cast<unsigned>(APFloat(MO.getFPImm())
573 .bitcastToAPInt().getHiBits(32).getLimitedValue());
574 }
575 // MO must be an Expr.
576 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000577 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000578}
579
Matheus Almeida6b59c442013-12-05 11:06:22 +0000580/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
581/// instructions.
582unsigned
583MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000584 SmallVectorImpl<MCFixup> &Fixups,
585 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000586 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
587 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000588 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
589 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000590
591 // The immediate field of an LD/ST instruction is scaled which means it must
592 // be divided (when encoding) by the size (in bytes) of the instructions'
593 // data format.
594 // .b - 1 byte
595 // .h - 2 bytes
596 // .w - 4 bytes
597 // .d - 8 bytes
598 switch(MI.getOpcode())
599 {
600 default:
601 assert (0 && "Unexpected instruction");
602 break;
603 case Mips::LD_B:
604 case Mips::ST_B:
605 // We don't need to scale the offset in this case
606 break;
607 case Mips::LD_H:
608 case Mips::ST_H:
609 OffBits >>= 1;
610 break;
611 case Mips::LD_W:
612 case Mips::ST_W:
613 OffBits >>= 2;
614 break;
615 case Mips::LD_D:
616 case Mips::ST_D:
617 OffBits >>= 3;
618 break;
619 }
620
621 return (OffBits & 0xFFFF) | RegBits;
622}
623
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000624/// getMemEncoding - Return binary encoding of memory related operand.
625/// If the offset operand requires relocation, record the relocation.
626unsigned
627MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000628 SmallVectorImpl<MCFixup> &Fixups,
629 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000630 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
631 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000632 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
633 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000634
635 return (OffBits & 0xFFFF) | RegBits;
636}
637
Jack Carter97700972013-08-13 20:19:16 +0000638unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000639getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
640 SmallVectorImpl<MCFixup> &Fixups,
641 const MCSubtargetInfo &STI) const {
642 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
643 assert(MI.getOperand(OpNo).isReg());
644 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
645 Fixups, STI) << 4;
646 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
647 Fixups, STI);
648
649 return (OffBits & 0xF) | RegBits;
650}
651
652unsigned MipsMCCodeEmitter::
653getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
654 SmallVectorImpl<MCFixup> &Fixups,
655 const MCSubtargetInfo &STI) const {
656 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
657 assert(MI.getOperand(OpNo).isReg());
658 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
659 Fixups, STI) << 4;
660 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
661 Fixups, STI) >> 1;
662
663 return (OffBits & 0xF) | RegBits;
664}
665
666unsigned MipsMCCodeEmitter::
667getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
668 SmallVectorImpl<MCFixup> &Fixups,
669 const MCSubtargetInfo &STI) const {
670 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
671 assert(MI.getOperand(OpNo).isReg());
672 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
673 Fixups, STI) << 4;
674 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
675 Fixups, STI) >> 2;
676
677 return (OffBits & 0xF) | RegBits;
678}
679
680unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000681getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
682 SmallVectorImpl<MCFixup> &Fixups,
683 const MCSubtargetInfo &STI) const {
684 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
685 assert(MI.getOperand(OpNo).isReg() &&
686 MI.getOperand(OpNo).getReg() == Mips::SP &&
687 "Unexpected base register!");
688 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
689 Fixups, STI) >> 2;
690
691 return OffBits & 0x1F;
692}
693
694unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000695getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000696 SmallVectorImpl<MCFixup> &Fixups,
697 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000698 // opNum can be invalid if instruction had reglist as operand.
699 // MemOperand is always last operand of instruction (base + offset).
700 switch (MI.getOpcode()) {
701 default:
702 break;
703 case Mips::SWM32_MM:
704 case Mips::LWM32_MM:
705 OpNo = MI.getNumOperands() - 2;
706 break;
707 }
708
Jack Carter97700972013-08-13 20:19:16 +0000709 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
710 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000711 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
712 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000713
714 return (OffBits & 0x0FFF) | RegBits;
715}
716
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000717unsigned MipsMCCodeEmitter::
718getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
719 SmallVectorImpl<MCFixup> &Fixups,
720 const MCSubtargetInfo &STI) const {
721 // opNum can be invalid if instruction had reglist as operand
722 // MemOperand is always last operand of instruction (base + offset)
723 switch (MI.getOpcode()) {
724 default:
725 break;
726 case Mips::SWM16_MM:
727 case Mips::LWM16_MM:
728 OpNo = MI.getNumOperands() - 2;
729 break;
730 }
731
732 // Offset is encoded in bits 4-0.
733 assert(MI.getOperand(OpNo).isReg());
734 // Base register is always SP - thus it is not encoded.
735 assert(MI.getOperand(OpNo+1).isImm());
736 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
737
738 return ((OffBits >> 2) & 0x0F);
739}
740
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000741unsigned
742MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000743 SmallVectorImpl<MCFixup> &Fixups,
744 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000745 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000746 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000747 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000748}
749
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000750// FIXME: should be called getMSBEncoding
751//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000752unsigned
753MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000754 SmallVectorImpl<MCFixup> &Fixups,
755 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000756 assert(MI.getOperand(OpNo-1).isImm());
757 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000758 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
759 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000760
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000761 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000762}
763
Matheus Almeida779c5932013-11-18 12:32:49 +0000764unsigned
765MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000766 SmallVectorImpl<MCFixup> &Fixups,
767 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000768 assert(MI.getOperand(OpNo).isImm());
769 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000770 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000771}
772
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000773unsigned
774MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
775 SmallVectorImpl<MCFixup> &Fixups,
776 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000777 const MCOperand &MO = MI.getOperand(OpNo);
778 if (MO.isImm()) {
779 // The immediate is encoded as 'immediate << 2'.
780 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
781 assert((Res & 3) == 0);
782 return Res >> 2;
783 }
784
785 assert(MO.isExpr() &&
786 "getSimm19Lsl2Encoding expects only expressions or an immediate");
787
788 const MCExpr *Expr = MO.getExpr();
789 Fixups.push_back(MCFixup::Create(0, Expr,
790 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
791 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000792}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000793
Zoran Jovanovic28551422014-06-09 09:49:51 +0000794unsigned
795MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
796 SmallVectorImpl<MCFixup> &Fixups,
797 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000798 const MCOperand &MO = MI.getOperand(OpNo);
799 if (MO.isImm()) {
800 // The immediate is encoded as 'immediate << 3'.
801 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
802 assert((Res & 7) == 0);
803 return Res >> 3;
804 }
805
806 assert(MO.isExpr() &&
807 "getSimm18Lsl2Encoding expects only expressions or an immediate");
808
809 const MCExpr *Expr = MO.getExpr();
810 Fixups.push_back(MCFixup::Create(0, Expr,
811 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
812 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000813}
814
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000815unsigned
816MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
817 SmallVectorImpl<MCFixup> &Fixups,
818 const MCSubtargetInfo &STI) const {
819 assert(MI.getOperand(OpNo).isImm());
820 const MCOperand &MO = MI.getOperand(OpNo);
821 return MO.getImm() % 8;
822}
823
Zoran Jovanovic88531712014-11-05 17:31:00 +0000824unsigned
825MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
826 SmallVectorImpl<MCFixup> &Fixups,
827 const MCSubtargetInfo &STI) const {
828 assert(MI.getOperand(OpNo).isImm());
829 const MCOperand &MO = MI.getOperand(OpNo);
830 unsigned Value = MO.getImm();
831 switch (Value) {
832 case 128: return 0x0;
833 case 1: return 0x1;
834 case 2: return 0x2;
835 case 3: return 0x3;
836 case 4: return 0x4;
837 case 7: return 0x5;
838 case 8: return 0x6;
839 case 15: return 0x7;
840 case 16: return 0x8;
841 case 31: return 0x9;
842 case 32: return 0xa;
843 case 63: return 0xb;
844 case 64: return 0xc;
845 case 255: return 0xd;
846 case 32768: return 0xe;
847 case 65535: return 0xf;
848 }
849 llvm_unreachable("Unexpected value");
850}
851
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000852unsigned
853MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
854 SmallVectorImpl<MCFixup> &Fixups,
855 const MCSubtargetInfo &STI) const {
856 unsigned res = 0;
857
858 // Register list operand is always first operand of instruction and it is
859 // placed before memory operand (register + imm).
860
861 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
862 unsigned Reg = MI.getOperand(I).getReg();
863 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
864 if (RegNo != 31)
865 res++;
866 else
867 res |= 0x10;
868 }
869 return res;
870}
871
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000872unsigned
873MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
874 SmallVectorImpl<MCFixup> &Fixups,
875 const MCSubtargetInfo &STI) const {
876 return (MI.getNumOperands() - 4);
877}
878
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000879unsigned
880MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
881 SmallVectorImpl<MCFixup> &Fixups,
882 const MCSubtargetInfo &STI) const {
883 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
884}
885
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000886#include "MipsGenMCCodeEmitter.inc"