blob: bafd3f058f8cff22b1e7a38bfcdd660753a8717d [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 Kupersteinc3434b32015-05-13 10:28:46 +0000115 return STI.getFeatureBits() & Mips::FeatureMicroMips;
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000116}
117
Jozef Kolekc22555d2015-04-20 12:23:06 +0000118bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteinc3434b32015-05-13 10:28:46 +0000119 return STI.getFeatureBits() & Mips::FeatureMips32r6;
Jozef Kolekc22555d2015-04-20 12:23:06 +0000120}
121
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000122void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
123 OS << (char)C;
124}
125
126void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
127 const MCSubtargetInfo &STI,
128 raw_ostream &OS) const {
129 // Output the instruction encoding in little endian byte order.
130 // Little-endian byte ordering:
131 // mips32r2: 4 | 3 | 2 | 1
132 // microMIPS: 2 | 1 | 4 | 3
133 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
134 EmitInstruction(Val >> 16, 2, STI, OS);
135 EmitInstruction(Val, 2, STI, OS);
136 } else {
137 for (unsigned i = 0; i < Size; ++i) {
138 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
139 EmitByte((Val >> Shift) & 0xff, OS);
140 }
141 }
142}
143
Jim Grosbach91df21f2015-05-15 19:13:16 +0000144/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000145/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000146void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000147encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000150{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000151
152 // Non-pseudo instructions that get changed for direct object
153 // only based on operand values.
154 // If this list of instructions get much longer we will move
155 // the check to a function call. Until then, this is more efficient.
156 MCInst TmpInst = MI;
157 switch (MI.getOpcode()) {
158 // If shift amount is >= 32 it the inst needs to be lowered further
159 case Mips::DSLL:
160 case Mips::DSRL:
161 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000162 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000163 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000164 break;
165 // Double extract instruction is chosen by pos and size operands
166 case Mips::DEXT:
167 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000168 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000169 }
170
Jack Carter97700972013-08-13 20:19:16 +0000171 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000172 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000173
174 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000175 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000176 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000177 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000178 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
179 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000180 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000181
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000182 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000183 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000184 if (isMips32r6(STI)) {
185 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
186 if (NewOpcode == -1)
187 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
188 }
189 else
190 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
191
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000192 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000193 if (Fixups.size() > N)
194 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000195
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000196 Opcode = NewOpcode;
197 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000198 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000199 }
200 }
201
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000202 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000203
Jack Carter5b5559d2012-10-03 21:58:54 +0000204 // Get byte count of instruction
205 unsigned Size = Desc.getSize();
206 if (!Size)
207 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208
David Woodhoused2cca112014-01-28 23:13:25 +0000209 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000210}
211
212/// getBranchTargetOpValue - Return binary encoding of the branch
213/// target operand. If the machine operand requires relocation,
214/// record the relocation and return zero.
215unsigned MipsMCCodeEmitter::
216getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000217 SmallVectorImpl<MCFixup> &Fixups,
218 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000219
220 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000221
Jack Carter4f69a0f2013-03-22 00:29:10 +0000222 // If the destination is an immediate, divide by 4.
223 if (MO.isImm()) return MO.getImm() >> 2;
224
Jack Carter71e6a742012-09-06 00:43:26 +0000225 assert(MO.isExpr() &&
226 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000227
228 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000229 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000230 MCFixupKind(Mips::fixup_Mips_PC16)));
231 return 0;
232}
233
Jozef Kolek9761e962015-01-12 12:03:34 +0000234/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
235/// target operand. If the machine operand requires relocation,
236/// record the relocation and return zero.
237unsigned MipsMCCodeEmitter::
238getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
239 SmallVectorImpl<MCFixup> &Fixups,
240 const MCSubtargetInfo &STI) const {
241
242 const MCOperand &MO = MI.getOperand(OpNo);
243
244 // If the destination is an immediate, divide by 2.
245 if (MO.isImm()) return MO.getImm() >> 1;
246
247 assert(MO.isExpr() &&
248 "getBranchTargetOpValueMM expects only expressions or immediates");
249
250 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000251 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000252 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
253 return 0;
254}
255
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000256/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
257/// 10-bit branch target operand. If the machine operand requires relocation,
258/// record the relocation and return zero.
259unsigned MipsMCCodeEmitter::
260getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
261 SmallVectorImpl<MCFixup> &Fixups,
262 const MCSubtargetInfo &STI) const {
263
264 const MCOperand &MO = MI.getOperand(OpNo);
265
266 // If the destination is an immediate, divide by 2.
267 if (MO.isImm()) return MO.getImm() >> 1;
268
269 assert(MO.isExpr() &&
270 "getBranchTargetOpValuePC10 expects only expressions or immediates");
271
272 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000273 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000274 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
275 return 0;
276}
277
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000278/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
279/// target operand. If the machine operand requires relocation,
280/// record the relocation and return zero.
281unsigned MipsMCCodeEmitter::
282getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000283 SmallVectorImpl<MCFixup> &Fixups,
284 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000285
286 const MCOperand &MO = MI.getOperand(OpNo);
287
288 // If the destination is an immediate, divide by 2.
289 if (MO.isImm()) return MO.getImm() >> 1;
290
291 assert(MO.isExpr() &&
292 "getBranchTargetOpValueMM expects only expressions or immediates");
293
294 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000295 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000296 MCFixupKind(Mips::
297 fixup_MICROMIPS_PC16_S1)));
298 return 0;
299}
300
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000301/// getBranchTarget21OpValue - Return binary encoding of the branch
302/// target operand. If the machine operand requires relocation,
303/// record the relocation and return zero.
304unsigned MipsMCCodeEmitter::
305getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
306 SmallVectorImpl<MCFixup> &Fixups,
307 const MCSubtargetInfo &STI) const {
308
309 const MCOperand &MO = MI.getOperand(OpNo);
310
311 // If the destination is an immediate, divide by 4.
312 if (MO.isImm()) return MO.getImm() >> 2;
313
314 assert(MO.isExpr() &&
315 "getBranchTarget21OpValue expects only expressions or immediates");
316
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000317 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000318 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000319 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000320 return 0;
321}
322
323/// getBranchTarget26OpValue - Return binary encoding of the branch
324/// target operand. If the machine operand requires relocation,
325/// record the relocation and return zero.
326unsigned MipsMCCodeEmitter::
327getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
328 SmallVectorImpl<MCFixup> &Fixups,
329 const MCSubtargetInfo &STI) const {
330
331 const MCOperand &MO = MI.getOperand(OpNo);
332
333 // If the destination is an immediate, divide by 4.
334 if (MO.isImm()) return MO.getImm() >> 2;
335
336 assert(MO.isExpr() &&
337 "getBranchTarget26OpValue expects only expressions or immediates");
338
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000339 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000340 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000341 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000342 return 0;
343}
344
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000345/// getJumpOffset16OpValue - Return binary encoding of the jump
346/// target operand. If the machine operand requires relocation,
347/// record the relocation and return zero.
348unsigned MipsMCCodeEmitter::
349getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
350 SmallVectorImpl<MCFixup> &Fixups,
351 const MCSubtargetInfo &STI) const {
352
353 const MCOperand &MO = MI.getOperand(OpNo);
354
355 if (MO.isImm()) return MO.getImm();
356
357 assert(MO.isExpr() &&
358 "getJumpOffset16OpValue expects only expressions or an immediate");
359
360 // TODO: Push fixup.
361 return 0;
362}
363
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000364/// getJumpTargetOpValue - Return binary encoding of the jump
365/// target operand. If the machine operand requires relocation,
366/// record the relocation and return zero.
367unsigned MipsMCCodeEmitter::
368getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000369 SmallVectorImpl<MCFixup> &Fixups,
370 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000371
372 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000373 // If the destination is an immediate, divide by 4.
374 if (MO.isImm()) return MO.getImm()>>2;
375
Jack Carter71e6a742012-09-06 00:43:26 +0000376 assert(MO.isExpr() &&
377 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000378
379 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000380 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000381 MCFixupKind(Mips::fixup_Mips_26)));
382 return 0;
383}
384
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000385unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000386getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000387 SmallVectorImpl<MCFixup> &Fixups,
388 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000389
390 const MCOperand &MO = MI.getOperand(OpNo);
391 // If the destination is an immediate, divide by 2.
392 if (MO.isImm()) return MO.getImm() >> 1;
393
394 assert(MO.isExpr() &&
395 "getJumpTargetOpValueMM expects only expressions or an immediate");
396
397 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000398 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000399 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
400 return 0;
401}
402
403unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000404getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
405 SmallVectorImpl<MCFixup> &Fixups,
406 const MCSubtargetInfo &STI) const {
407
408 const MCOperand &MO = MI.getOperand(OpNo);
409 if (MO.isImm()) {
410 // The immediate is encoded as 'immediate << 2'.
411 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
412 assert((Res & 3) == 0);
413 return Res >> 2;
414 }
415
416 assert(MO.isExpr() &&
417 "getUImm5Lsl2Encoding expects only expressions or an immediate");
418
419 return 0;
420}
421
422unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000423getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
424 SmallVectorImpl<MCFixup> &Fixups,
425 const MCSubtargetInfo &STI) const {
426
427 const MCOperand &MO = MI.getOperand(OpNo);
428 if (MO.isImm()) {
429 int Value = MO.getImm();
430 return Value >> 2;
431 }
432
433 return 0;
434}
435
436unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000437getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
438 SmallVectorImpl<MCFixup> &Fixups,
439 const MCSubtargetInfo &STI) const {
440
441 const MCOperand &MO = MI.getOperand(OpNo);
442 if (MO.isImm()) {
443 unsigned Value = MO.getImm();
444 return Value >> 2;
445 }
446
447 return 0;
448}
449
450unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000451getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
452 SmallVectorImpl<MCFixup> &Fixups,
453 const MCSubtargetInfo &STI) const {
454
455 const MCOperand &MO = MI.getOperand(OpNo);
456 if (MO.isImm()) {
457 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
458 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
459 }
460
461 return 0;
462}
463
464unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000465getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000466 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000467 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000468
Jack Carterb5cf5902013-04-17 00:18:04 +0000469 if (Expr->EvaluateAsAbsolute(Res))
470 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000471
Akira Hatanakafe384a22012-03-27 02:33:05 +0000472 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000473 if (Kind == MCExpr::Constant) {
474 return cast<MCConstantExpr>(Expr)->getValue();
475 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000476
Akira Hatanakafe384a22012-03-27 02:33:05 +0000477 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000478 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
479 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000480 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000481 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000482
483 if (Kind == MCExpr::Target) {
484 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
485
486 Mips::Fixups FixupKind = Mips::Fixups(0);
487 switch (MipsExpr->getKind()) {
488 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000489 case MipsMCExpr::VK_Mips_HIGHEST:
490 FixupKind = Mips::fixup_Mips_HIGHEST;
491 break;
492 case MipsMCExpr::VK_Mips_HIGHER:
493 FixupKind = Mips::fixup_Mips_HIGHER;
494 break;
495 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000496 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
497 : Mips::fixup_Mips_HI16;
498 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000499 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000500 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
501 : Mips::fixup_Mips_LO16;
502 break;
503 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000504 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000505 return 0;
506 }
507
Jack Carterb5cf5902013-04-17 00:18:04 +0000508 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000509 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000510
Mark Seabornc3bd1772013-12-31 13:05:15 +0000511 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
512 default: llvm_unreachable("Unknown fixup kind!");
513 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000514 case MCSymbolRefExpr::VK_None:
515 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
516 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000517 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
518 FixupKind = Mips::fixup_Mips_GPOFF_HI;
519 break;
520 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
521 FixupKind = Mips::fixup_Mips_GPOFF_LO;
522 break;
523 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000524 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000525 : Mips::fixup_Mips_GOT_PAGE;
526 break;
527 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000528 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000529 : Mips::fixup_Mips_GOT_OFST;
530 break;
531 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000532 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000533 : Mips::fixup_Mips_GOT_DISP;
534 break;
535 case MCSymbolRefExpr::VK_Mips_GPREL:
536 FixupKind = Mips::fixup_Mips_GPREL16;
537 break;
538 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000539 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000540 : Mips::fixup_Mips_CALL16;
541 break;
542 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000543 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000544 : Mips::fixup_Mips_GOT_Global;
545 break;
546 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000547 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000548 : Mips::fixup_Mips_GOT_Local;
549 break;
550 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000551 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000552 : Mips::fixup_Mips_HI16;
553 break;
554 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000555 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000556 : Mips::fixup_Mips_LO16;
557 break;
558 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000559 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000560 : Mips::fixup_Mips_TLSGD;
561 break;
562 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000563 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000564 : Mips::fixup_Mips_TLSLDM;
565 break;
566 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000567 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000568 : Mips::fixup_Mips_DTPREL_HI;
569 break;
570 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000571 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000572 : Mips::fixup_Mips_DTPREL_LO;
573 break;
574 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
575 FixupKind = Mips::fixup_Mips_GOTTPREL;
576 break;
577 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000578 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000579 : Mips::fixup_Mips_TPREL_HI;
580 break;
581 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000582 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000583 : Mips::fixup_Mips_TPREL_LO;
584 break;
585 case MCSymbolRefExpr::VK_Mips_HIGHER:
586 FixupKind = Mips::fixup_Mips_HIGHER;
587 break;
588 case MCSymbolRefExpr::VK_Mips_HIGHEST:
589 FixupKind = Mips::fixup_Mips_HIGHEST;
590 break;
591 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
592 FixupKind = Mips::fixup_Mips_GOT_HI16;
593 break;
594 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
595 FixupKind = Mips::fixup_Mips_GOT_LO16;
596 break;
597 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
598 FixupKind = Mips::fixup_Mips_CALL_HI16;
599 break;
600 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
601 FixupKind = Mips::fixup_Mips_CALL_LO16;
602 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000603 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
604 FixupKind = Mips::fixup_MIPS_PCHI16;
605 break;
606 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
607 FixupKind = Mips::fixup_MIPS_PCLO16;
608 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000609 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000610
Jim Grosbach63661f82015-05-15 19:13:05 +0000611 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000612 return 0;
613 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000614 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000615}
616
Jack Carterb5cf5902013-04-17 00:18:04 +0000617/// getMachineOpValue - Return binary encoding of operand. If the machine
618/// operand requires relocation, record the relocation and return zero.
619unsigned MipsMCCodeEmitter::
620getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000621 SmallVectorImpl<MCFixup> &Fixups,
622 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000623 if (MO.isReg()) {
624 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000625 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000626 return RegNo;
627 } else if (MO.isImm()) {
628 return static_cast<unsigned>(MO.getImm());
629 } else if (MO.isFPImm()) {
630 return static_cast<unsigned>(APFloat(MO.getFPImm())
631 .bitcastToAPInt().getHiBits(32).getLimitedValue());
632 }
633 // MO must be an Expr.
634 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000635 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000636}
637
Matheus Almeida6b59c442013-12-05 11:06:22 +0000638/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
639/// instructions.
640unsigned
641MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000642 SmallVectorImpl<MCFixup> &Fixups,
643 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000644 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
645 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000646 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
647 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000648
649 // The immediate field of an LD/ST instruction is scaled which means it must
650 // be divided (when encoding) by the size (in bytes) of the instructions'
651 // data format.
652 // .b - 1 byte
653 // .h - 2 bytes
654 // .w - 4 bytes
655 // .d - 8 bytes
656 switch(MI.getOpcode())
657 {
658 default:
659 assert (0 && "Unexpected instruction");
660 break;
661 case Mips::LD_B:
662 case Mips::ST_B:
663 // We don't need to scale the offset in this case
664 break;
665 case Mips::LD_H:
666 case Mips::ST_H:
667 OffBits >>= 1;
668 break;
669 case Mips::LD_W:
670 case Mips::ST_W:
671 OffBits >>= 2;
672 break;
673 case Mips::LD_D:
674 case Mips::ST_D:
675 OffBits >>= 3;
676 break;
677 }
678
679 return (OffBits & 0xFFFF) | RegBits;
680}
681
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000682/// getMemEncoding - Return binary encoding of memory related operand.
683/// If the offset operand requires relocation, record the relocation.
684unsigned
685MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000686 SmallVectorImpl<MCFixup> &Fixups,
687 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000688 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
689 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000690 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
691 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000692
693 return (OffBits & 0xFFFF) | RegBits;
694}
695
Jack Carter97700972013-08-13 20:19:16 +0000696unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000697getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
698 SmallVectorImpl<MCFixup> &Fixups,
699 const MCSubtargetInfo &STI) const {
700 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
701 assert(MI.getOperand(OpNo).isReg());
702 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
703 Fixups, STI) << 4;
704 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
705 Fixups, STI);
706
707 return (OffBits & 0xF) | RegBits;
708}
709
710unsigned MipsMCCodeEmitter::
711getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
712 SmallVectorImpl<MCFixup> &Fixups,
713 const MCSubtargetInfo &STI) const {
714 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
715 assert(MI.getOperand(OpNo).isReg());
716 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
717 Fixups, STI) << 4;
718 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
719 Fixups, STI) >> 1;
720
721 return (OffBits & 0xF) | RegBits;
722}
723
724unsigned MipsMCCodeEmitter::
725getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
726 SmallVectorImpl<MCFixup> &Fixups,
727 const MCSubtargetInfo &STI) const {
728 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
729 assert(MI.getOperand(OpNo).isReg());
730 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
731 Fixups, STI) << 4;
732 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
733 Fixups, STI) >> 2;
734
735 return (OffBits & 0xF) | RegBits;
736}
737
738unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000739getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
740 SmallVectorImpl<MCFixup> &Fixups,
741 const MCSubtargetInfo &STI) const {
742 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
743 assert(MI.getOperand(OpNo).isReg() &&
744 MI.getOperand(OpNo).getReg() == Mips::SP &&
745 "Unexpected base register!");
746 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
747 Fixups, STI) >> 2;
748
749 return OffBits & 0x1F;
750}
751
752unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000753getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
754 SmallVectorImpl<MCFixup> &Fixups,
755 const MCSubtargetInfo &STI) const {
756 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
757 assert(MI.getOperand(OpNo).isReg() &&
758 MI.getOperand(OpNo).getReg() == Mips::GP &&
759 "Unexpected base register!");
760
761 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
762 Fixups, STI) >> 2;
763
764 return OffBits & 0x7F;
765}
766
767unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000768getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000769 SmallVectorImpl<MCFixup> &Fixups,
770 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000771 // opNum can be invalid if instruction had reglist as operand.
772 // MemOperand is always last operand of instruction (base + offset).
773 switch (MI.getOpcode()) {
774 default:
775 break;
776 case Mips::SWM32_MM:
777 case Mips::LWM32_MM:
778 OpNo = MI.getNumOperands() - 2;
779 break;
780 }
781
Jack Carter97700972013-08-13 20:19:16 +0000782 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
783 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000784 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
785 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000786
787 return (OffBits & 0x0FFF) | RegBits;
788}
789
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000790unsigned MipsMCCodeEmitter::
791getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
792 SmallVectorImpl<MCFixup> &Fixups,
793 const MCSubtargetInfo &STI) const {
794 // opNum can be invalid if instruction had reglist as operand
795 // MemOperand is always last operand of instruction (base + offset)
796 switch (MI.getOpcode()) {
797 default:
798 break;
799 case Mips::SWM16_MM:
800 case Mips::LWM16_MM:
801 OpNo = MI.getNumOperands() - 2;
802 break;
803 }
804
805 // Offset is encoded in bits 4-0.
806 assert(MI.getOperand(OpNo).isReg());
807 // Base register is always SP - thus it is not encoded.
808 assert(MI.getOperand(OpNo+1).isImm());
809 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
810
811 return ((OffBits >> 2) & 0x0F);
812}
813
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000814unsigned
815MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000816 SmallVectorImpl<MCFixup> &Fixups,
817 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000818 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000819 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000820 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000821}
822
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000823// FIXME: should be called getMSBEncoding
824//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000825unsigned
826MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000827 SmallVectorImpl<MCFixup> &Fixups,
828 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000829 assert(MI.getOperand(OpNo-1).isImm());
830 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000831 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
832 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000833
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000834 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000835}
836
Matheus Almeida779c5932013-11-18 12:32:49 +0000837unsigned
838MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000839 SmallVectorImpl<MCFixup> &Fixups,
840 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000841 assert(MI.getOperand(OpNo).isImm());
842 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000843 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000844}
845
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000846unsigned
847MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
848 SmallVectorImpl<MCFixup> &Fixups,
849 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000850 const MCOperand &MO = MI.getOperand(OpNo);
851 if (MO.isImm()) {
852 // The immediate is encoded as 'immediate << 2'.
853 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
854 assert((Res & 3) == 0);
855 return Res >> 2;
856 }
857
858 assert(MO.isExpr() &&
859 "getSimm19Lsl2Encoding expects only expressions or an immediate");
860
861 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000862 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000863 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
864 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000865}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000866
Zoran Jovanovic28551422014-06-09 09:49:51 +0000867unsigned
868MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
869 SmallVectorImpl<MCFixup> &Fixups,
870 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000871 const MCOperand &MO = MI.getOperand(OpNo);
872 if (MO.isImm()) {
873 // The immediate is encoded as 'immediate << 3'.
874 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
875 assert((Res & 7) == 0);
876 return Res >> 3;
877 }
878
879 assert(MO.isExpr() &&
880 "getSimm18Lsl2Encoding expects only expressions or an immediate");
881
882 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000883 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000884 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
885 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000886}
887
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000888unsigned
889MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
890 SmallVectorImpl<MCFixup> &Fixups,
891 const MCSubtargetInfo &STI) const {
892 assert(MI.getOperand(OpNo).isImm());
893 const MCOperand &MO = MI.getOperand(OpNo);
894 return MO.getImm() % 8;
895}
896
Zoran Jovanovic88531712014-11-05 17:31:00 +0000897unsigned
898MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
899 SmallVectorImpl<MCFixup> &Fixups,
900 const MCSubtargetInfo &STI) const {
901 assert(MI.getOperand(OpNo).isImm());
902 const MCOperand &MO = MI.getOperand(OpNo);
903 unsigned Value = MO.getImm();
904 switch (Value) {
905 case 128: return 0x0;
906 case 1: return 0x1;
907 case 2: return 0x2;
908 case 3: return 0x3;
909 case 4: return 0x4;
910 case 7: return 0x5;
911 case 8: return 0x6;
912 case 15: return 0x7;
913 case 16: return 0x8;
914 case 31: return 0x9;
915 case 32: return 0xa;
916 case 63: return 0xb;
917 case 64: return 0xc;
918 case 255: return 0xd;
919 case 32768: return 0xe;
920 case 65535: return 0xf;
921 }
922 llvm_unreachable("Unexpected value");
923}
924
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000925unsigned
926MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
927 SmallVectorImpl<MCFixup> &Fixups,
928 const MCSubtargetInfo &STI) const {
929 unsigned res = 0;
930
931 // Register list operand is always first operand of instruction and it is
932 // placed before memory operand (register + imm).
933
934 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
935 unsigned Reg = MI.getOperand(I).getReg();
936 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
937 if (RegNo != 31)
938 res++;
939 else
940 res |= 0x10;
941 }
942 return res;
943}
944
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000945unsigned
946MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
947 SmallVectorImpl<MCFixup> &Fixups,
948 const MCSubtargetInfo &STI) const {
949 return (MI.getNumOperands() - 4);
950}
951
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000952unsigned
953MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
954 SmallVectorImpl<MCFixup> &Fixups,
955 const MCSubtargetInfo &STI) const {
956 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
957}
958
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000959unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +0000960MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
961 SmallVectorImpl<MCFixup> &Fixups,
962 const MCSubtargetInfo &STI) const {
963 unsigned res = 0;
964
965 if (MI.getOperand(0).getReg() == Mips::A1 &&
966 MI.getOperand(1).getReg() == Mips::A2)
967 res = 0;
968 else if (MI.getOperand(0).getReg() == Mips::A1 &&
969 MI.getOperand(1).getReg() == Mips::A3)
970 res = 1;
971 else if (MI.getOperand(0).getReg() == Mips::A2 &&
972 MI.getOperand(1).getReg() == Mips::A3)
973 res = 2;
974 else if (MI.getOperand(0).getReg() == Mips::A0 &&
975 MI.getOperand(1).getReg() == Mips::S5)
976 res = 3;
977 else if (MI.getOperand(0).getReg() == Mips::A0 &&
978 MI.getOperand(1).getReg() == Mips::S6)
979 res = 4;
980 else if (MI.getOperand(0).getReg() == Mips::A0 &&
981 MI.getOperand(1).getReg() == Mips::A1)
982 res = 5;
983 else if (MI.getOperand(0).getReg() == Mips::A0 &&
984 MI.getOperand(1).getReg() == Mips::A2)
985 res = 6;
986 else if (MI.getOperand(0).getReg() == Mips::A0 &&
987 MI.getOperand(1).getReg() == Mips::A3)
988 res = 7;
989
990 return res;
991}
992
993unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000994MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
995 SmallVectorImpl<MCFixup> &Fixups,
996 const MCSubtargetInfo &STI) const {
997 const MCOperand &MO = MI.getOperand(OpNo);
998 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
999 // The immediate is encoded as 'immediate >> 2'.
1000 unsigned Res = static_cast<unsigned>(MO.getImm());
1001 assert((Res & 3) == 0);
1002 return Res >> 2;
1003}
1004
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001005#include "MipsGenMCCodeEmitter.inc"