blob: 2e50560a3f97335ddf1757deb86dc1d0589b1856 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
Akira Hatanaka750ecec2011-09-30 20:40:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MipsMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13//
Matheus Almeida9e1450b2014-03-20 09:29:54 +000014
Matheus Almeida9e1450b2014-03-20 09:29:54 +000015#include "MipsMCCodeEmitter.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000016#include "MCTargetDesc/MipsFixupKinds.h"
Petar Jovanovica5da5882014-02-04 18:41:57 +000017#include "MCTargetDesc/MipsMCExpr.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000018#include "MCTargetDesc/MipsMCTargetDesc.h"
19#include "llvm/ADT/APFloat.h"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000020#include "llvm/ADT/SmallVector.h"
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000021#include "llvm/MC/MCContext.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000022#include "llvm/MC/MCExpr.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000023#include "llvm/MC/MCFixup.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000024#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstrInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000026#include "llvm/MC/MCRegisterInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000027#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000028#include "llvm/Support/raw_ostream.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000029
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "mccodeemitter"
31
Akira Hatanakabe6a8182013-04-19 19:03:11 +000032#define GET_INSTRMAP_INFO
33#include "MipsGenInstrInfo.inc"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000034#undef GET_INSTRMAP_INFO
Akira Hatanakabe6a8182013-04-19 19:03:11 +000035
Matheus Almeida9e1450b2014-03-20 09:29:54 +000036namespace llvm {
37MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
38 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000039 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000040 return new MipsMCCodeEmitter(MCII, Ctx, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000041}
42
Matheus Almeida9e1450b2014-03-20 09:29:54 +000043MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
44 const MCRegisterInfo &MRI,
Matheus Almeida9e1450b2014-03-20 09:29:54 +000045 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000046 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +000047}
Matheus Almeida9e1450b2014-03-20 09:29:54 +000048} // End of namespace llvm.
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000049
50// If the D<shift> instruction has a shift amount that is greater
51// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
52static void LowerLargeShift(MCInst& Inst) {
53
54 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
55 assert(Inst.getOperand(2).isImm());
56
57 int64_t Shift = Inst.getOperand(2).getImm();
58 if (Shift <= 31)
59 return; // Do nothing
60 Shift -= 32;
61
62 // saminus32
63 Inst.getOperand(2).setImm(Shift);
64
65 switch (Inst.getOpcode()) {
66 default:
67 // Calling function is not synchronized
68 llvm_unreachable("Unexpected shift instruction");
69 case Mips::DSLL:
70 Inst.setOpcode(Mips::DSLL32);
71 return;
72 case Mips::DSRL:
73 Inst.setOpcode(Mips::DSRL32);
74 return;
75 case Mips::DSRA:
76 Inst.setOpcode(Mips::DSRA32);
77 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +000078 case Mips::DROTR:
79 Inst.setOpcode(Mips::DROTR32);
80 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000081 }
82}
83
84// Pick a DEXT or DINS instruction variant based on the pos and size operands
85static void LowerDextDins(MCInst& InstIn) {
86 int Opcode = InstIn.getOpcode();
87
88 if (Opcode == Mips::DEXT)
89 assert(InstIn.getNumOperands() == 4 &&
90 "Invalid no. of machine operands for DEXT!");
91 else // Only DEXT and DINS are possible
92 assert(InstIn.getNumOperands() == 5 &&
93 "Invalid no. of machine operands for DINS!");
94
95 assert(InstIn.getOperand(2).isImm());
96 int64_t pos = InstIn.getOperand(2).getImm();
97 assert(InstIn.getOperand(3).isImm());
98 int64_t size = InstIn.getOperand(3).getImm();
99
100 if (size <= 32) {
101 if (pos < 32) // DEXT/DINS, do nothing
102 return;
103 // DEXTU/DINSU
104 InstIn.getOperand(2).setImm(pos - 32);
105 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
106 return;
107 }
108 // DEXTM/DINSM
109 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
110 InstIn.getOperand(3).setImm(size - 32);
111 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
112 return;
113}
114
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000115bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000116 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000117}
118
Jozef Kolekc22555d2015-04-20 12:23:06 +0000119bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000120 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000121}
122
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000123void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
124 OS << (char)C;
125}
126
127void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
128 const MCSubtargetInfo &STI,
129 raw_ostream &OS) const {
130 // Output the instruction encoding in little endian byte order.
131 // Little-endian byte ordering:
132 // mips32r2: 4 | 3 | 2 | 1
133 // microMIPS: 2 | 1 | 4 | 3
134 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
135 EmitInstruction(Val >> 16, 2, STI, OS);
136 EmitInstruction(Val, 2, STI, OS);
137 } else {
138 for (unsigned i = 0; i < Size; ++i) {
139 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
140 EmitByte((Val >> Shift) & 0xff, OS);
141 }
142 }
143}
144
Jim Grosbach91df21f2015-05-15 19:13:16 +0000145/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000146/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000147void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000148encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000149 SmallVectorImpl<MCFixup> &Fixups,
150 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000151{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000152
153 // Non-pseudo instructions that get changed for direct object
154 // only based on operand values.
155 // If this list of instructions get much longer we will move
156 // the check to a function call. Until then, this is more efficient.
157 MCInst TmpInst = MI;
158 switch (MI.getOpcode()) {
159 // If shift amount is >= 32 it the inst needs to be lowered further
160 case Mips::DSLL:
161 case Mips::DSRL:
162 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000163 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000164 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000165 break;
166 // Double extract instruction is chosen by pos and size operands
167 case Mips::DEXT:
168 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000169 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000170 }
171
Jack Carter97700972013-08-13 20:19:16 +0000172 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000173 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000174
175 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000176 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000177 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000178 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000179 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
180 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000181 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000182
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000183 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000184 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000185 if (isMips32r6(STI)) {
186 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
187 if (NewOpcode == -1)
188 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
189 }
190 else
191 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
192
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000193 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000194 if (Fixups.size() > N)
195 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000196
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000197 Opcode = NewOpcode;
198 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000199 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000200 }
201 }
202
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000203 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000204
Jack Carter5b5559d2012-10-03 21:58:54 +0000205 // Get byte count of instruction
206 unsigned Size = Desc.getSize();
207 if (!Size)
208 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000209
David Woodhoused2cca112014-01-28 23:13:25 +0000210 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000211}
212
213/// getBranchTargetOpValue - Return binary encoding of the branch
214/// target operand. If the machine operand requires relocation,
215/// record the relocation and return zero.
216unsigned MipsMCCodeEmitter::
217getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000218 SmallVectorImpl<MCFixup> &Fixups,
219 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000220
221 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000222
Jack Carter4f69a0f2013-03-22 00:29:10 +0000223 // If the destination is an immediate, divide by 4.
224 if (MO.isImm()) return MO.getImm() >> 2;
225
Jack Carter71e6a742012-09-06 00:43:26 +0000226 assert(MO.isExpr() &&
227 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000228
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000229 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
230 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
231 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000232 MCFixupKind(Mips::fixup_Mips_PC16)));
233 return 0;
234}
235
Jozef Kolek9761e962015-01-12 12:03:34 +0000236/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
237/// target operand. If the machine operand requires relocation,
238/// record the relocation and return zero.
239unsigned MipsMCCodeEmitter::
240getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
241 SmallVectorImpl<MCFixup> &Fixups,
242 const MCSubtargetInfo &STI) const {
243
244 const MCOperand &MO = MI.getOperand(OpNo);
245
246 // If the destination is an immediate, divide by 2.
247 if (MO.isImm()) return MO.getImm() >> 1;
248
249 assert(MO.isExpr() &&
250 "getBranchTargetOpValueMM expects only expressions or immediates");
251
252 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000253 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000254 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
255 return 0;
256}
257
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000258/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
259/// 10-bit branch target operand. If the machine operand requires relocation,
260/// record the relocation and return zero.
261unsigned MipsMCCodeEmitter::
262getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
263 SmallVectorImpl<MCFixup> &Fixups,
264 const MCSubtargetInfo &STI) const {
265
266 const MCOperand &MO = MI.getOperand(OpNo);
267
268 // If the destination is an immediate, divide by 2.
269 if (MO.isImm()) return MO.getImm() >> 1;
270
271 assert(MO.isExpr() &&
272 "getBranchTargetOpValuePC10 expects only expressions or immediates");
273
274 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000275 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000276 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
277 return 0;
278}
279
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000280/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
281/// target operand. If the machine operand requires relocation,
282/// record the relocation and return zero.
283unsigned MipsMCCodeEmitter::
284getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000285 SmallVectorImpl<MCFixup> &Fixups,
286 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000287
288 const MCOperand &MO = MI.getOperand(OpNo);
289
290 // If the destination is an immediate, divide by 2.
291 if (MO.isImm()) return MO.getImm() >> 1;
292
293 assert(MO.isExpr() &&
294 "getBranchTargetOpValueMM expects only expressions or immediates");
295
296 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000297 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000298 MCFixupKind(Mips::
299 fixup_MICROMIPS_PC16_S1)));
300 return 0;
301}
302
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000303/// getBranchTarget21OpValue - Return binary encoding of the branch
304/// target operand. If the machine operand requires relocation,
305/// record the relocation and return zero.
306unsigned MipsMCCodeEmitter::
307getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
308 SmallVectorImpl<MCFixup> &Fixups,
309 const MCSubtargetInfo &STI) const {
310
311 const MCOperand &MO = MI.getOperand(OpNo);
312
313 // If the destination is an immediate, divide by 4.
314 if (MO.isImm()) return MO.getImm() >> 2;
315
316 assert(MO.isExpr() &&
317 "getBranchTarget21OpValue expects only expressions or immediates");
318
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000319 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
320 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
321 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000322 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000323 return 0;
324}
325
326/// getBranchTarget26OpValue - Return binary encoding of the branch
327/// target operand. If the machine operand requires relocation,
328/// record the relocation and return zero.
329unsigned MipsMCCodeEmitter::
330getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
331 SmallVectorImpl<MCFixup> &Fixups,
332 const MCSubtargetInfo &STI) const {
333
334 const MCOperand &MO = MI.getOperand(OpNo);
335
336 // If the destination is an immediate, divide by 4.
337 if (MO.isImm()) return MO.getImm() >> 2;
338
339 assert(MO.isExpr() &&
340 "getBranchTarget26OpValue expects only expressions or immediates");
341
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000342 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
343 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
344 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000345 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000346 return 0;
347}
348
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000349/// getJumpOffset16OpValue - Return binary encoding of the jump
350/// target operand. If the machine operand requires relocation,
351/// record the relocation and return zero.
352unsigned MipsMCCodeEmitter::
353getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
354 SmallVectorImpl<MCFixup> &Fixups,
355 const MCSubtargetInfo &STI) const {
356
357 const MCOperand &MO = MI.getOperand(OpNo);
358
359 if (MO.isImm()) return MO.getImm();
360
361 assert(MO.isExpr() &&
362 "getJumpOffset16OpValue expects only expressions or an immediate");
363
364 // TODO: Push fixup.
365 return 0;
366}
367
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000368/// getJumpTargetOpValue - Return binary encoding of the jump
369/// target operand. If the machine operand requires relocation,
370/// record the relocation and return zero.
371unsigned MipsMCCodeEmitter::
372getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000373 SmallVectorImpl<MCFixup> &Fixups,
374 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000375
376 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000377 // If the destination is an immediate, divide by 4.
378 if (MO.isImm()) return MO.getImm()>>2;
379
Jack Carter71e6a742012-09-06 00:43:26 +0000380 assert(MO.isExpr() &&
381 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000382
383 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000384 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000385 MCFixupKind(Mips::fixup_Mips_26)));
386 return 0;
387}
388
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000389unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000390getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000391 SmallVectorImpl<MCFixup> &Fixups,
392 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000393
394 const MCOperand &MO = MI.getOperand(OpNo);
395 // If the destination is an immediate, divide by 2.
396 if (MO.isImm()) return MO.getImm() >> 1;
397
398 assert(MO.isExpr() &&
399 "getJumpTargetOpValueMM expects only expressions or an immediate");
400
401 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000402 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000403 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
404 return 0;
405}
406
407unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000408getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
409 SmallVectorImpl<MCFixup> &Fixups,
410 const MCSubtargetInfo &STI) const {
411
412 const MCOperand &MO = MI.getOperand(OpNo);
413 if (MO.isImm()) {
414 // The immediate is encoded as 'immediate << 2'.
415 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
416 assert((Res & 3) == 0);
417 return Res >> 2;
418 }
419
420 assert(MO.isExpr() &&
421 "getUImm5Lsl2Encoding expects only expressions or an immediate");
422
423 return 0;
424}
425
426unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000427getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
428 SmallVectorImpl<MCFixup> &Fixups,
429 const MCSubtargetInfo &STI) const {
430
431 const MCOperand &MO = MI.getOperand(OpNo);
432 if (MO.isImm()) {
433 int Value = MO.getImm();
434 return Value >> 2;
435 }
436
437 return 0;
438}
439
440unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000441getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
442 SmallVectorImpl<MCFixup> &Fixups,
443 const MCSubtargetInfo &STI) const {
444
445 const MCOperand &MO = MI.getOperand(OpNo);
446 if (MO.isImm()) {
447 unsigned Value = MO.getImm();
448 return Value >> 2;
449 }
450
451 return 0;
452}
453
454unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000455getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
456 SmallVectorImpl<MCFixup> &Fixups,
457 const MCSubtargetInfo &STI) const {
458
459 const MCOperand &MO = MI.getOperand(OpNo);
460 if (MO.isImm()) {
461 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
462 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
463 }
464
465 return 0;
466}
467
468unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000469getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000470 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000471 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000472
Jim Grosbach13760bd2015-05-30 01:25:56 +0000473 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000474 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000475
Akira Hatanakafe384a22012-03-27 02:33:05 +0000476 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000477 if (Kind == MCExpr::Constant) {
478 return cast<MCConstantExpr>(Expr)->getValue();
479 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000480
Akira Hatanakafe384a22012-03-27 02:33:05 +0000481 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000482 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
483 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000484 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000485 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000486
487 if (Kind == MCExpr::Target) {
488 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
489
490 Mips::Fixups FixupKind = Mips::Fixups(0);
491 switch (MipsExpr->getKind()) {
492 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000493 case MipsMCExpr::VK_Mips_HIGHEST:
494 FixupKind = Mips::fixup_Mips_HIGHEST;
495 break;
496 case MipsMCExpr::VK_Mips_HIGHER:
497 FixupKind = Mips::fixup_Mips_HIGHER;
498 break;
499 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000500 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
501 : Mips::fixup_Mips_HI16;
502 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000503 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000504 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
505 : Mips::fixup_Mips_LO16;
506 break;
507 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000508 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000509 return 0;
510 }
511
Jack Carterb5cf5902013-04-17 00:18:04 +0000512 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000513 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000514
Mark Seabornc3bd1772013-12-31 13:05:15 +0000515 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
516 default: llvm_unreachable("Unknown fixup kind!");
517 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000518 case MCSymbolRefExpr::VK_None:
519 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
520 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000521 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
522 FixupKind = Mips::fixup_Mips_GPOFF_HI;
523 break;
524 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
525 FixupKind = Mips::fixup_Mips_GPOFF_LO;
526 break;
527 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000528 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000529 : Mips::fixup_Mips_GOT_PAGE;
530 break;
531 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000532 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000533 : Mips::fixup_Mips_GOT_OFST;
534 break;
535 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000536 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000537 : Mips::fixup_Mips_GOT_DISP;
538 break;
539 case MCSymbolRefExpr::VK_Mips_GPREL:
540 FixupKind = Mips::fixup_Mips_GPREL16;
541 break;
542 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000543 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000544 : Mips::fixup_Mips_CALL16;
545 break;
546 case MCSymbolRefExpr::VK_Mips_GOT16:
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_Global;
549 break;
550 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000551 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000552 : Mips::fixup_Mips_GOT_Local;
553 break;
554 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000555 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000556 : Mips::fixup_Mips_HI16;
557 break;
558 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000559 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000560 : Mips::fixup_Mips_LO16;
561 break;
562 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000563 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000564 : Mips::fixup_Mips_TLSGD;
565 break;
566 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000567 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000568 : Mips::fixup_Mips_TLSLDM;
569 break;
570 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000571 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000572 : Mips::fixup_Mips_DTPREL_HI;
573 break;
574 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000575 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000576 : Mips::fixup_Mips_DTPREL_LO;
577 break;
578 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
579 FixupKind = Mips::fixup_Mips_GOTTPREL;
580 break;
581 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000582 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000583 : Mips::fixup_Mips_TPREL_HI;
584 break;
585 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000586 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000587 : Mips::fixup_Mips_TPREL_LO;
588 break;
589 case MCSymbolRefExpr::VK_Mips_HIGHER:
590 FixupKind = Mips::fixup_Mips_HIGHER;
591 break;
592 case MCSymbolRefExpr::VK_Mips_HIGHEST:
593 FixupKind = Mips::fixup_Mips_HIGHEST;
594 break;
595 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
596 FixupKind = Mips::fixup_Mips_GOT_HI16;
597 break;
598 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
599 FixupKind = Mips::fixup_Mips_GOT_LO16;
600 break;
601 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
602 FixupKind = Mips::fixup_Mips_CALL_HI16;
603 break;
604 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
605 FixupKind = Mips::fixup_Mips_CALL_LO16;
606 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000607 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
608 FixupKind = Mips::fixup_MIPS_PCHI16;
609 break;
610 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
611 FixupKind = Mips::fixup_MIPS_PCLO16;
612 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000613 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000614
Jim Grosbach63661f82015-05-15 19:13:05 +0000615 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000616 return 0;
617 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000618 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000619}
620
Jack Carterb5cf5902013-04-17 00:18:04 +0000621/// getMachineOpValue - Return binary encoding of operand. If the machine
622/// operand requires relocation, record the relocation and return zero.
623unsigned MipsMCCodeEmitter::
624getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000625 SmallVectorImpl<MCFixup> &Fixups,
626 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000627 if (MO.isReg()) {
628 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000629 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000630 return RegNo;
631 } else if (MO.isImm()) {
632 return static_cast<unsigned>(MO.getImm());
633 } else if (MO.isFPImm()) {
634 return static_cast<unsigned>(APFloat(MO.getFPImm())
635 .bitcastToAPInt().getHiBits(32).getLimitedValue());
636 }
637 // MO must be an Expr.
638 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000639 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000640}
641
Matheus Almeida6b59c442013-12-05 11:06:22 +0000642/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
643/// instructions.
644unsigned
645MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000646 SmallVectorImpl<MCFixup> &Fixups,
647 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000648 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
649 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000650 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
651 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000652
653 // The immediate field of an LD/ST instruction is scaled which means it must
654 // be divided (when encoding) by the size (in bytes) of the instructions'
655 // data format.
656 // .b - 1 byte
657 // .h - 2 bytes
658 // .w - 4 bytes
659 // .d - 8 bytes
660 switch(MI.getOpcode())
661 {
662 default:
663 assert (0 && "Unexpected instruction");
664 break;
665 case Mips::LD_B:
666 case Mips::ST_B:
667 // We don't need to scale the offset in this case
668 break;
669 case Mips::LD_H:
670 case Mips::ST_H:
671 OffBits >>= 1;
672 break;
673 case Mips::LD_W:
674 case Mips::ST_W:
675 OffBits >>= 2;
676 break;
677 case Mips::LD_D:
678 case Mips::ST_D:
679 OffBits >>= 3;
680 break;
681 }
682
683 return (OffBits & 0xFFFF) | RegBits;
684}
685
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000686/// getMemEncoding - Return binary encoding of memory related operand.
687/// If the offset operand requires relocation, record the relocation.
688unsigned
689MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000690 SmallVectorImpl<MCFixup> &Fixups,
691 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000692 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
693 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000694 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
695 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000696
697 return (OffBits & 0xFFFF) | RegBits;
698}
699
Jack Carter97700972013-08-13 20:19:16 +0000700unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000701getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
702 SmallVectorImpl<MCFixup> &Fixups,
703 const MCSubtargetInfo &STI) const {
704 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
705 assert(MI.getOperand(OpNo).isReg());
706 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
707 Fixups, STI) << 4;
708 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
709 Fixups, STI);
710
711 return (OffBits & 0xF) | RegBits;
712}
713
714unsigned MipsMCCodeEmitter::
715getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
716 SmallVectorImpl<MCFixup> &Fixups,
717 const MCSubtargetInfo &STI) const {
718 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
719 assert(MI.getOperand(OpNo).isReg());
720 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
721 Fixups, STI) << 4;
722 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
723 Fixups, STI) >> 1;
724
725 return (OffBits & 0xF) | RegBits;
726}
727
728unsigned MipsMCCodeEmitter::
729getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
730 SmallVectorImpl<MCFixup> &Fixups,
731 const MCSubtargetInfo &STI) const {
732 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
733 assert(MI.getOperand(OpNo).isReg());
734 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
735 Fixups, STI) << 4;
736 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
737 Fixups, STI) >> 2;
738
739 return (OffBits & 0xF) | RegBits;
740}
741
742unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000743getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
744 SmallVectorImpl<MCFixup> &Fixups,
745 const MCSubtargetInfo &STI) const {
746 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
747 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000748 (MI.getOperand(OpNo).getReg() == Mips::SP ||
749 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000750 "Unexpected base register!");
751 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
752 Fixups, STI) >> 2;
753
754 return OffBits & 0x1F;
755}
756
757unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000758getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
759 SmallVectorImpl<MCFixup> &Fixups,
760 const MCSubtargetInfo &STI) const {
761 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
762 assert(MI.getOperand(OpNo).isReg() &&
763 MI.getOperand(OpNo).getReg() == Mips::GP &&
764 "Unexpected base register!");
765
766 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
767 Fixups, STI) >> 2;
768
769 return OffBits & 0x7F;
770}
771
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000772 unsigned MipsMCCodeEmitter::
773getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
774 SmallVectorImpl<MCFixup> &Fixups,
775 const MCSubtargetInfo &STI) const {
776 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
777 assert(MI.getOperand(OpNo).isReg());
778 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
779 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000780 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000781
782 return (OffBits & 0x1FF) | RegBits;
783}
784
Jozef Koleke10a02e2015-01-28 17:27:26 +0000785unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000786getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000787 SmallVectorImpl<MCFixup> &Fixups,
788 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000789 // opNum can be invalid if instruction had reglist as operand.
790 // MemOperand is always last operand of instruction (base + offset).
791 switch (MI.getOpcode()) {
792 default:
793 break;
794 case Mips::SWM32_MM:
795 case Mips::LWM32_MM:
796 OpNo = MI.getNumOperands() - 2;
797 break;
798 }
799
Jack Carter97700972013-08-13 20:19:16 +0000800 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
801 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000802 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
803 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000804
805 return (OffBits & 0x0FFF) | RegBits;
806}
807
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000808unsigned MipsMCCodeEmitter::
809getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
810 SmallVectorImpl<MCFixup> &Fixups,
811 const MCSubtargetInfo &STI) const {
812 // opNum can be invalid if instruction had reglist as operand
813 // MemOperand is always last operand of instruction (base + offset)
814 switch (MI.getOpcode()) {
815 default:
816 break;
817 case Mips::SWM16_MM:
818 case Mips::LWM16_MM:
819 OpNo = MI.getNumOperands() - 2;
820 break;
821 }
822
823 // Offset is encoded in bits 4-0.
824 assert(MI.getOperand(OpNo).isReg());
825 // Base register is always SP - thus it is not encoded.
826 assert(MI.getOperand(OpNo+1).isImm());
827 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
828
829 return ((OffBits >> 2) & 0x0F);
830}
831
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000832unsigned
833MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000834 SmallVectorImpl<MCFixup> &Fixups,
835 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000836 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000837 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000838 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000839}
840
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000841// FIXME: should be called getMSBEncoding
842//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000843unsigned
844MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000845 SmallVectorImpl<MCFixup> &Fixups,
846 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000847 assert(MI.getOperand(OpNo-1).isImm());
848 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000849 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
850 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000851
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000852 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000853}
854
Matheus Almeida779c5932013-11-18 12:32:49 +0000855unsigned
856MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000857 SmallVectorImpl<MCFixup> &Fixups,
858 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000859 assert(MI.getOperand(OpNo).isImm());
860 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000861 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000862}
863
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000864unsigned
865MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
866 SmallVectorImpl<MCFixup> &Fixups,
867 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000868 const MCOperand &MO = MI.getOperand(OpNo);
869 if (MO.isImm()) {
870 // The immediate is encoded as 'immediate << 2'.
871 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
872 assert((Res & 3) == 0);
873 return Res >> 2;
874 }
875
876 assert(MO.isExpr() &&
877 "getSimm19Lsl2Encoding expects only expressions or an immediate");
878
879 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000880 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000881 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
882 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000883}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000884
Zoran Jovanovic28551422014-06-09 09:49:51 +0000885unsigned
886MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
887 SmallVectorImpl<MCFixup> &Fixups,
888 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000889 const MCOperand &MO = MI.getOperand(OpNo);
890 if (MO.isImm()) {
891 // The immediate is encoded as 'immediate << 3'.
892 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
893 assert((Res & 7) == 0);
894 return Res >> 3;
895 }
896
897 assert(MO.isExpr() &&
898 "getSimm18Lsl2Encoding expects only expressions or an immediate");
899
900 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000901 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000902 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
903 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000904}
905
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000906unsigned
907MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
908 SmallVectorImpl<MCFixup> &Fixups,
909 const MCSubtargetInfo &STI) const {
910 assert(MI.getOperand(OpNo).isImm());
911 const MCOperand &MO = MI.getOperand(OpNo);
912 return MO.getImm() % 8;
913}
914
Zoran Jovanovic88531712014-11-05 17:31:00 +0000915unsigned
916MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
917 SmallVectorImpl<MCFixup> &Fixups,
918 const MCSubtargetInfo &STI) const {
919 assert(MI.getOperand(OpNo).isImm());
920 const MCOperand &MO = MI.getOperand(OpNo);
921 unsigned Value = MO.getImm();
922 switch (Value) {
923 case 128: return 0x0;
924 case 1: return 0x1;
925 case 2: return 0x2;
926 case 3: return 0x3;
927 case 4: return 0x4;
928 case 7: return 0x5;
929 case 8: return 0x6;
930 case 15: return 0x7;
931 case 16: return 0x8;
932 case 31: return 0x9;
933 case 32: return 0xa;
934 case 63: return 0xb;
935 case 64: return 0xc;
936 case 255: return 0xd;
937 case 32768: return 0xe;
938 case 65535: return 0xf;
939 }
940 llvm_unreachable("Unexpected value");
941}
942
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000943unsigned
944MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
945 SmallVectorImpl<MCFixup> &Fixups,
946 const MCSubtargetInfo &STI) const {
947 unsigned res = 0;
948
949 // Register list operand is always first operand of instruction and it is
950 // placed before memory operand (register + imm).
951
952 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
953 unsigned Reg = MI.getOperand(I).getReg();
954 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
955 if (RegNo != 31)
956 res++;
957 else
958 res |= 0x10;
959 }
960 return res;
961}
962
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000963unsigned
964MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
965 SmallVectorImpl<MCFixup> &Fixups,
966 const MCSubtargetInfo &STI) const {
967 return (MI.getNumOperands() - 4);
968}
969
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000970unsigned
971MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
972 SmallVectorImpl<MCFixup> &Fixups,
973 const MCSubtargetInfo &STI) const {
974 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
975}
976
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000977unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +0000978MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
979 SmallVectorImpl<MCFixup> &Fixups,
980 const MCSubtargetInfo &STI) const {
981 unsigned res = 0;
982
983 if (MI.getOperand(0).getReg() == Mips::A1 &&
984 MI.getOperand(1).getReg() == Mips::A2)
985 res = 0;
986 else if (MI.getOperand(0).getReg() == Mips::A1 &&
987 MI.getOperand(1).getReg() == Mips::A3)
988 res = 1;
989 else if (MI.getOperand(0).getReg() == Mips::A2 &&
990 MI.getOperand(1).getReg() == Mips::A3)
991 res = 2;
992 else if (MI.getOperand(0).getReg() == Mips::A0 &&
993 MI.getOperand(1).getReg() == Mips::S5)
994 res = 3;
995 else if (MI.getOperand(0).getReg() == Mips::A0 &&
996 MI.getOperand(1).getReg() == Mips::S6)
997 res = 4;
998 else if (MI.getOperand(0).getReg() == Mips::A0 &&
999 MI.getOperand(1).getReg() == Mips::A1)
1000 res = 5;
1001 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1002 MI.getOperand(1).getReg() == Mips::A2)
1003 res = 6;
1004 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1005 MI.getOperand(1).getReg() == Mips::A3)
1006 res = 7;
1007
1008 return res;
1009}
1010
1011unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001012MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1013 SmallVectorImpl<MCFixup> &Fixups,
1014 const MCSubtargetInfo &STI) const {
1015 const MCOperand &MO = MI.getOperand(OpNo);
1016 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1017 // The immediate is encoded as 'immediate >> 2'.
1018 unsigned Res = static_cast<unsigned>(MO.getImm());
1019 assert((Res & 3) == 0);
1020 return Res >> 2;
1021}
1022
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001023#include "MipsGenMCCodeEmitter.inc"