blob: 9d3e7b4ba8f7d4d708380a4378cd730b5c9144d8 [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
Daniel Sanders611eb822016-02-29 15:26:54 +000084// Pick a DINS instruction variant based on the pos and size operands
85static void LowerDins(MCInst& InstIn) {
86 assert(InstIn.getNumOperands() == 5 &&
87 "Invalid no. of machine operands for DINS!");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000088
89 assert(InstIn.getOperand(2).isImm());
90 int64_t pos = InstIn.getOperand(2).getImm();
91 assert(InstIn.getOperand(3).isImm());
92 int64_t size = InstIn.getOperand(3).getImm();
93
94 if (size <= 32) {
Daniel Sanders611eb822016-02-29 15:26:54 +000095 if (pos < 32) // DINS, do nothing
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000096 return;
Daniel Sanders611eb822016-02-29 15:26:54 +000097 // DINSU
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000098 InstIn.getOperand(2).setImm(pos - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +000099 InstIn.setOpcode(Mips::DINSU);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000100 return;
101 }
Daniel Sanders611eb822016-02-29 15:26:54 +0000102 // DINSM
103 assert(pos < 32 && "DINS cannot have both size and pos > 32");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000104 InstIn.getOperand(3).setImm(size - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +0000105 InstIn.setOpcode(Mips::DINSM);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000106 return;
107}
108
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000109bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000110 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000111}
112
Jozef Kolekc22555d2015-04-20 12:23:06 +0000113bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000114 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000115}
116
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000117void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
118 OS << (char)C;
119}
120
121void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
122 const MCSubtargetInfo &STI,
123 raw_ostream &OS) const {
124 // Output the instruction encoding in little endian byte order.
125 // Little-endian byte ordering:
126 // mips32r2: 4 | 3 | 2 | 1
127 // microMIPS: 2 | 1 | 4 | 3
128 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
129 EmitInstruction(Val >> 16, 2, STI, OS);
130 EmitInstruction(Val, 2, STI, OS);
131 } else {
132 for (unsigned i = 0; i < Size; ++i) {
133 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
134 EmitByte((Val >> Shift) & 0xff, OS);
135 }
136 }
137}
138
Jim Grosbach91df21f2015-05-15 19:13:16 +0000139/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000140/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000141void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000142encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000143 SmallVectorImpl<MCFixup> &Fixups,
144 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000145{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000146
147 // Non-pseudo instructions that get changed for direct object
148 // only based on operand values.
149 // If this list of instructions get much longer we will move
150 // the check to a function call. Until then, this is more efficient.
151 MCInst TmpInst = MI;
152 switch (MI.getOpcode()) {
153 // If shift amount is >= 32 it the inst needs to be lowered further
154 case Mips::DSLL:
155 case Mips::DSRL:
156 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000157 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000158 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000159 break;
160 // Double extract instruction is chosen by pos and size operands
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000161 case Mips::DINS:
Daniel Sanders611eb822016-02-29 15:26:54 +0000162 LowerDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000163 }
164
Jack Carter97700972013-08-13 20:19:16 +0000165 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000166 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000167
168 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000169 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000170 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000171 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000172 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
173 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000174 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000175
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000176 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000177 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000178 if (isMips32r6(STI)) {
179 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
180 if (NewOpcode == -1)
181 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
182 }
183 else
184 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
185
Zoran Jovanovic2e386d32015-10-12 16:07:25 +0000186 // Check whether it is Dsp instruction.
187 if (NewOpcode == -1)
188 NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
189
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000190 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000191 if (Fixups.size() > N)
192 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000193
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000194 Opcode = NewOpcode;
195 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000196 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000197 }
198 }
199
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000200 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000201
Jack Carter5b5559d2012-10-03 21:58:54 +0000202 // Get byte count of instruction
203 unsigned Size = Desc.getSize();
204 if (!Size)
205 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000206
David Woodhoused2cca112014-01-28 23:13:25 +0000207 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208}
209
210/// getBranchTargetOpValue - Return binary encoding of the branch
211/// target operand. If the machine operand requires relocation,
212/// record the relocation and return zero.
213unsigned MipsMCCodeEmitter::
214getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000215 SmallVectorImpl<MCFixup> &Fixups,
216 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000217
218 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000219
Jack Carter4f69a0f2013-03-22 00:29:10 +0000220 // If the destination is an immediate, divide by 4.
221 if (MO.isImm()) return MO.getImm() >> 2;
222
Jack Carter71e6a742012-09-06 00:43:26 +0000223 assert(MO.isExpr() &&
224 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000225
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000226 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
227 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
228 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000229 MCFixupKind(Mips::fixup_Mips_PC16)));
230 return 0;
231}
232
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +0000233/// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
234/// target operand. If the machine operand requires relocation,
235/// record the relocation and return zero.
236unsigned MipsMCCodeEmitter::
237getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
238 SmallVectorImpl<MCFixup> &Fixups,
239 const MCSubtargetInfo &STI) const {
240
241 const MCOperand &MO = MI.getOperand(OpNo);
242
243 // If the destination is an immediate, divide by 2.
244 if (MO.isImm()) return MO.getImm() >> 1;
245
246 assert(MO.isExpr() &&
247 "getBranchTargetOpValue expects only expressions or immediates");
248
249 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
250 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
251 Fixups.push_back(MCFixup::create(0, FixupExpression,
252 MCFixupKind(Mips::fixup_Mips_PC16)));
253 return 0;
254}
255
Jozef Kolek9761e962015-01-12 12:03:34 +0000256/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
257/// target operand. If the machine operand requires relocation,
258/// record the relocation and return zero.
259unsigned MipsMCCodeEmitter::
260getBranchTarget7OpValueMM(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 "getBranchTargetOpValueMM 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 Kolek9761e962015-01-12 12:03:34 +0000274 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
275 return 0;
276}
277
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000278/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
279/// 10-bit branch target operand. If the machine operand requires relocation,
280/// record the relocation and return zero.
281unsigned MipsMCCodeEmitter::
282getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
283 SmallVectorImpl<MCFixup> &Fixups,
284 const MCSubtargetInfo &STI) const {
285
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 "getBranchTargetOpValuePC10 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,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000296 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
297 return 0;
298}
299
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000300/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
301/// target operand. If the machine operand requires relocation,
302/// record the relocation and return zero.
303unsigned MipsMCCodeEmitter::
304getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000305 SmallVectorImpl<MCFixup> &Fixups,
306 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000307
308 const MCOperand &MO = MI.getOperand(OpNo);
309
310 // If the destination is an immediate, divide by 2.
311 if (MO.isImm()) return MO.getImm() >> 1;
312
313 assert(MO.isExpr() &&
314 "getBranchTargetOpValueMM expects only expressions or immediates");
315
316 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000317 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000318 MCFixupKind(Mips::
319 fixup_MICROMIPS_PC16_S1)));
320 return 0;
321}
322
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000323/// getBranchTarget21OpValue - Return binary encoding of the branch
324/// target operand. If the machine operand requires relocation,
325/// record the relocation and return zero.
326unsigned MipsMCCodeEmitter::
327getBranchTarget21OpValue(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 "getBranchTarget21OpValue expects only expressions or immediates");
338
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000339 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
340 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
341 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000342 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000343 return 0;
344}
345
346/// getBranchTarget26OpValue - Return binary encoding of the branch
347/// target operand. If the machine operand requires relocation,
348/// record the relocation and return zero.
349unsigned MipsMCCodeEmitter::
350getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
351 SmallVectorImpl<MCFixup> &Fixups,
352 const MCSubtargetInfo &STI) const {
353
354 const MCOperand &MO = MI.getOperand(OpNo);
355
356 // If the destination is an immediate, divide by 4.
357 if (MO.isImm()) return MO.getImm() >> 2;
358
359 assert(MO.isExpr() &&
360 "getBranchTarget26OpValue expects only expressions or immediates");
361
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000362 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
363 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
364 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000365 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000366 return 0;
367}
368
Zoran Jovanovica887b362015-11-30 12:56:18 +0000369/// getBranchTarget26OpValueMM - Return binary encoding of the branch
370/// target operand. If the machine operand requires relocation,
371/// record the relocation and return zero.
372unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
373 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
374 const MCSubtargetInfo &STI) const {
375
376 const MCOperand &MO = MI.getOperand(OpNo);
377
378 // If the destination is an immediate, divide by 2.
379 if (MO.isImm())
380 return MO.getImm() >> 1;
381
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000382 assert(MO.isExpr() &&
383 "getBranchTarget26OpValueMM expects only expressions or immediates");
384
385 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
386 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
387 Fixups.push_back(MCFixup::create(0, FixupExpression,
388 MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
Zoran Jovanovica887b362015-11-30 12:56:18 +0000389 return 0;
390}
391
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000392/// getJumpOffset16OpValue - Return binary encoding of the jump
393/// target operand. If the machine operand requires relocation,
394/// record the relocation and return zero.
395unsigned MipsMCCodeEmitter::
396getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
397 SmallVectorImpl<MCFixup> &Fixups,
398 const MCSubtargetInfo &STI) const {
399
400 const MCOperand &MO = MI.getOperand(OpNo);
401
402 if (MO.isImm()) return MO.getImm();
403
404 assert(MO.isExpr() &&
405 "getJumpOffset16OpValue expects only expressions or an immediate");
406
407 // TODO: Push fixup.
408 return 0;
409}
410
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000411/// getJumpTargetOpValue - Return binary encoding of the jump
412/// target operand. If the machine operand requires relocation,
413/// record the relocation and return zero.
414unsigned MipsMCCodeEmitter::
415getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000416 SmallVectorImpl<MCFixup> &Fixups,
417 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000418
419 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000420 // If the destination is an immediate, divide by 4.
421 if (MO.isImm()) return MO.getImm()>>2;
422
Jack Carter71e6a742012-09-06 00:43:26 +0000423 assert(MO.isExpr() &&
424 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000425
426 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000427 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000428 MCFixupKind(Mips::fixup_Mips_26)));
429 return 0;
430}
431
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000432unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000433getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000434 SmallVectorImpl<MCFixup> &Fixups,
435 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000436
437 const MCOperand &MO = MI.getOperand(OpNo);
438 // If the destination is an immediate, divide by 2.
439 if (MO.isImm()) return MO.getImm() >> 1;
440
441 assert(MO.isExpr() &&
442 "getJumpTargetOpValueMM expects only expressions or an immediate");
443
444 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000445 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000446 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
447 return 0;
448}
449
450unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000451getUImm5Lsl2Encoding(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 // The immediate is encoded as 'immediate << 2'.
458 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
459 assert((Res & 3) == 0);
460 return Res >> 2;
461 }
462
463 assert(MO.isExpr() &&
464 "getUImm5Lsl2Encoding expects only expressions or an immediate");
465
466 return 0;
467}
468
469unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000470getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
471 SmallVectorImpl<MCFixup> &Fixups,
472 const MCSubtargetInfo &STI) const {
473
474 const MCOperand &MO = MI.getOperand(OpNo);
475 if (MO.isImm()) {
476 int Value = MO.getImm();
477 return Value >> 2;
478 }
479
480 return 0;
481}
482
483unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000484getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
485 SmallVectorImpl<MCFixup> &Fixups,
486 const MCSubtargetInfo &STI) const {
487
488 const MCOperand &MO = MI.getOperand(OpNo);
489 if (MO.isImm()) {
490 unsigned Value = MO.getImm();
491 return Value >> 2;
492 }
493
494 return 0;
495}
496
497unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000498getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
499 SmallVectorImpl<MCFixup> &Fixups,
500 const MCSubtargetInfo &STI) const {
501
502 const MCOperand &MO = MI.getOperand(OpNo);
503 if (MO.isImm()) {
504 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
505 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
506 }
507
508 return 0;
509}
510
511unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000512getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000513 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000514 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000515
Jim Grosbach13760bd2015-05-30 01:25:56 +0000516 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000517 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000518
Akira Hatanakafe384a22012-03-27 02:33:05 +0000519 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000520 if (Kind == MCExpr::Constant) {
521 return cast<MCConstantExpr>(Expr)->getValue();
522 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000523
Akira Hatanakafe384a22012-03-27 02:33:05 +0000524 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000525 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
526 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000527 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000528 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000529
530 if (Kind == MCExpr::Target) {
531 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
532
533 Mips::Fixups FixupKind = Mips::Fixups(0);
534 switch (MipsExpr->getKind()) {
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000535 case MipsMCExpr::MEK_NEG:
536 case MipsMCExpr::MEK_None:
537 case MipsMCExpr::MEK_Special:
538 llvm_unreachable("Unhandled fixup kind!");
539 break;
540 case MipsMCExpr::MEK_CALL_HI16:
541 FixupKind = Mips::fixup_Mips_CALL_HI16;
542 break;
543 case MipsMCExpr::MEK_CALL_LO16:
544 FixupKind = Mips::fixup_Mips_CALL_LO16;
545 break;
546 case MipsMCExpr::MEK_DTPREL_HI:
547 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
548 : Mips::fixup_Mips_DTPREL_HI;
549 break;
550 case MipsMCExpr::MEK_DTPREL_LO:
551 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
552 : Mips::fixup_Mips_DTPREL_LO;
553 break;
554 case MipsMCExpr::MEK_GOTTPREL:
555 FixupKind = Mips::fixup_Mips_GOTTPREL;
556 break;
557 case MipsMCExpr::MEK_GOT:
558 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
559 : Mips::fixup_Mips_GOT;
560 break;
561 case MipsMCExpr::MEK_GOT_CALL:
562 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
563 : Mips::fixup_Mips_CALL16;
564 break;
565 case MipsMCExpr::MEK_GOT_DISP:
566 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
567 : Mips::fixup_Mips_GOT_DISP;
568 break;
569 case MipsMCExpr::MEK_GOT_HI16:
570 FixupKind = Mips::fixup_Mips_GOT_HI16;
571 break;
572 case MipsMCExpr::MEK_GOT_LO16:
573 FixupKind = Mips::fixup_Mips_GOT_LO16;
574 break;
575 case MipsMCExpr::MEK_GOT_PAGE:
576 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
577 : Mips::fixup_Mips_GOT_PAGE;
578 break;
579 case MipsMCExpr::MEK_GOT_OFST:
580 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
581 : Mips::fixup_Mips_GOT_OFST;
582 break;
583 case MipsMCExpr::MEK_GPREL:
584 FixupKind = Mips::fixup_Mips_GPREL16;
585 break;
586 case MipsMCExpr::MEK_LO: {
587 // Check for %lo(%neg(%gp_rel(X)))
588 if (MipsExpr->isGpOff()) {
589 FixupKind = Mips::fixup_Mips_GPOFF_LO;
590 break;
591 }
592 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
593 : Mips::fixup_Mips_LO16;
594 break;
595 }
596 case MipsMCExpr::MEK_HIGHEST:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000597 FixupKind = Mips::fixup_Mips_HIGHEST;
598 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000599 case MipsMCExpr::MEK_HIGHER:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000600 FixupKind = Mips::fixup_Mips_HIGHER;
601 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000602 case MipsMCExpr::MEK_HI:
603 // Check for %hi(%neg(%gp_rel(X)))
604 if (MipsExpr->isGpOff()) {
605 FixupKind = Mips::fixup_Mips_GPOFF_HI;
606 break;
607 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000608 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
609 : Mips::fixup_Mips_HI16;
610 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000611 case MipsMCExpr::MEK_PCREL_HI16:
612 FixupKind = Mips::fixup_MIPS_PCHI16;
613 break;
614 case MipsMCExpr::MEK_PCREL_LO16:
615 FixupKind = Mips::fixup_MIPS_PCLO16;
616 break;
617 case MipsMCExpr::MEK_TLSGD:
618 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
619 : Mips::fixup_Mips_TLSGD;
620 break;
621 case MipsMCExpr::MEK_TLSLDM:
622 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
623 : Mips::fixup_Mips_TLSLDM;
624 break;
625 case MipsMCExpr::MEK_TPREL_HI:
626 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
627 : Mips::fixup_Mips_TPREL_HI;
628 break;
629 case MipsMCExpr::MEK_TPREL_LO:
630 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
631 : Mips::fixup_Mips_TPREL_LO;
Petar Jovanovica5da5882014-02-04 18:41:57 +0000632 break;
633 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000634 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000635 return 0;
636 }
637
Jack Carterb5cf5902013-04-17 00:18:04 +0000638 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000639 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000640
Mark Seabornc3bd1772013-12-31 13:05:15 +0000641 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
642 default: llvm_unreachable("Unknown fixup kind!");
643 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000644 case MCSymbolRefExpr::VK_None:
645 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
646 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000647 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000648
Jim Grosbach63661f82015-05-15 19:13:05 +0000649 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000650 return 0;
651 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000652 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000653}
654
Jack Carterb5cf5902013-04-17 00:18:04 +0000655/// getMachineOpValue - Return binary encoding of operand. If the machine
656/// operand requires relocation, record the relocation and return zero.
657unsigned MipsMCCodeEmitter::
658getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000659 SmallVectorImpl<MCFixup> &Fixups,
660 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000661 if (MO.isReg()) {
662 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000663 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000664 return RegNo;
665 } else if (MO.isImm()) {
666 return static_cast<unsigned>(MO.getImm());
667 } else if (MO.isFPImm()) {
668 return static_cast<unsigned>(APFloat(MO.getFPImm())
669 .bitcastToAPInt().getHiBits(32).getLimitedValue());
670 }
671 // MO must be an Expr.
672 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000673 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000674}
675
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000676/// Return binary encoding of memory related operand.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000677/// If the offset operand requires relocation, record the relocation.
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000678template <unsigned ShiftAmount>
679unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
680 SmallVectorImpl<MCFixup> &Fixups,
681 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000682 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
683 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000684 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
685 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000686
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000687 // Apply the scale factor if there is one.
688 OffBits >>= ShiftAmount;
689
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000690 return (OffBits & 0xFFFF) | RegBits;
691}
692
Jack Carter97700972013-08-13 20:19:16 +0000693unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000694getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
695 SmallVectorImpl<MCFixup> &Fixups,
696 const MCSubtargetInfo &STI) const {
697 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
698 assert(MI.getOperand(OpNo).isReg());
699 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
700 Fixups, STI) << 4;
701 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
702 Fixups, STI);
703
704 return (OffBits & 0xF) | RegBits;
705}
706
707unsigned MipsMCCodeEmitter::
708getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
709 SmallVectorImpl<MCFixup> &Fixups,
710 const MCSubtargetInfo &STI) const {
711 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
712 assert(MI.getOperand(OpNo).isReg());
713 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
714 Fixups, STI) << 4;
715 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
716 Fixups, STI) >> 1;
717
718 return (OffBits & 0xF) | RegBits;
719}
720
721unsigned MipsMCCodeEmitter::
722getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
723 SmallVectorImpl<MCFixup> &Fixups,
724 const MCSubtargetInfo &STI) const {
725 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
726 assert(MI.getOperand(OpNo).isReg());
727 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
728 Fixups, STI) << 4;
729 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
730 Fixups, STI) >> 2;
731
732 return (OffBits & 0xF) | RegBits;
733}
734
735unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000736getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
737 SmallVectorImpl<MCFixup> &Fixups,
738 const MCSubtargetInfo &STI) const {
739 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
740 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000741 (MI.getOperand(OpNo).getReg() == Mips::SP ||
742 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000743 "Unexpected base register!");
744 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
745 Fixups, STI) >> 2;
746
747 return OffBits & 0x1F;
748}
749
750unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000751getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
752 SmallVectorImpl<MCFixup> &Fixups,
753 const MCSubtargetInfo &STI) const {
754 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
755 assert(MI.getOperand(OpNo).isReg() &&
756 MI.getOperand(OpNo).getReg() == Mips::GP &&
757 "Unexpected base register!");
758
759 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
760 Fixups, STI) >> 2;
761
762 return OffBits & 0x7F;
763}
764
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000765unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000766getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
767 SmallVectorImpl<MCFixup> &Fixups,
768 const MCSubtargetInfo &STI) const {
769 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
770 assert(MI.getOperand(OpNo).isReg());
771 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
772 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000773 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000774
775 return (OffBits & 0x1FF) | RegBits;
776}
777
Jozef Koleke10a02e2015-01-28 17:27:26 +0000778unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000779getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000780 SmallVectorImpl<MCFixup> &Fixups,
781 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000782 // opNum can be invalid if instruction had reglist as operand.
783 // MemOperand is always last operand of instruction (base + offset).
784 switch (MI.getOpcode()) {
785 default:
786 break;
787 case Mips::SWM32_MM:
788 case Mips::LWM32_MM:
789 OpNo = MI.getNumOperands() - 2;
790 break;
791 }
792
Jack Carter97700972013-08-13 20:19:16 +0000793 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
794 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000795 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
796 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000797
798 return (OffBits & 0x0FFF) | RegBits;
799}
800
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000801unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000802getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
803 SmallVectorImpl<MCFixup> &Fixups,
804 const MCSubtargetInfo &STI) const {
805 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
806 assert(MI.getOperand(OpNo).isReg());
807 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
808 STI) << 16;
809 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
810
811 return (OffBits & 0xFFFF) | RegBits;
812}
813
814unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000815getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
816 SmallVectorImpl<MCFixup> &Fixups,
817 const MCSubtargetInfo &STI) const {
818 // opNum can be invalid if instruction had reglist as operand
819 // MemOperand is always last operand of instruction (base + offset)
820 switch (MI.getOpcode()) {
821 default:
822 break;
823 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000824 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000825 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000826 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000827 OpNo = MI.getNumOperands() - 2;
828 break;
829 }
830
831 // Offset is encoded in bits 4-0.
832 assert(MI.getOperand(OpNo).isReg());
833 // Base register is always SP - thus it is not encoded.
834 assert(MI.getOperand(OpNo+1).isImm());
835 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
836
837 return ((OffBits >> 2) & 0x0F);
838}
839
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000840// FIXME: should be called getMSBEncoding
841//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000842unsigned
843MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000844 SmallVectorImpl<MCFixup> &Fixups,
845 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000846 assert(MI.getOperand(OpNo-1).isImm());
847 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000848 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
849 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000850
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000851 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000852}
853
Daniel Sandersea4f6532015-11-06 12:22:31 +0000854template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000855unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000856MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
857 SmallVectorImpl<MCFixup> &Fixups,
858 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000859 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000860 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
861 Value -= Offset;
862 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000863}
864
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000865unsigned
866MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
867 SmallVectorImpl<MCFixup> &Fixups,
868 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000869 const MCOperand &MO = MI.getOperand(OpNo);
870 if (MO.isImm()) {
871 // The immediate is encoded as 'immediate << 2'.
872 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
873 assert((Res & 3) == 0);
874 return Res >> 2;
875 }
876
877 assert(MO.isExpr() &&
878 "getSimm19Lsl2Encoding expects only expressions or an immediate");
879
880 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000881 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
882 : Mips::fixup_MIPS_PC19_S2;
883 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000884 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000885}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000886
Zoran Jovanovic28551422014-06-09 09:49:51 +0000887unsigned
888MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
889 SmallVectorImpl<MCFixup> &Fixups,
890 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000891 const MCOperand &MO = MI.getOperand(OpNo);
892 if (MO.isImm()) {
893 // The immediate is encoded as 'immediate << 3'.
894 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
895 assert((Res & 7) == 0);
896 return Res >> 3;
897 }
898
899 assert(MO.isExpr() &&
900 "getSimm18Lsl2Encoding expects only expressions or an immediate");
901
902 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000903 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
904 : Mips::fixup_MIPS_PC18_S3;
905 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000906 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000907}
908
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000909unsigned
910MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
911 SmallVectorImpl<MCFixup> &Fixups,
912 const MCSubtargetInfo &STI) const {
913 assert(MI.getOperand(OpNo).isImm());
914 const MCOperand &MO = MI.getOperand(OpNo);
915 return MO.getImm() % 8;
916}
917
Zoran Jovanovic88531712014-11-05 17:31:00 +0000918unsigned
919MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
920 SmallVectorImpl<MCFixup> &Fixups,
921 const MCSubtargetInfo &STI) const {
922 assert(MI.getOperand(OpNo).isImm());
923 const MCOperand &MO = MI.getOperand(OpNo);
924 unsigned Value = MO.getImm();
925 switch (Value) {
926 case 128: return 0x0;
927 case 1: return 0x1;
928 case 2: return 0x2;
929 case 3: return 0x3;
930 case 4: return 0x4;
931 case 7: return 0x5;
932 case 8: return 0x6;
933 case 15: return 0x7;
934 case 16: return 0x8;
935 case 31: return 0x9;
936 case 32: return 0xa;
937 case 63: return 0xb;
938 case 64: return 0xc;
939 case 255: return 0xd;
940 case 32768: return 0xe;
941 case 65535: return 0xf;
942 }
943 llvm_unreachable("Unexpected value");
944}
945
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000946unsigned
947MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
948 SmallVectorImpl<MCFixup> &Fixups,
949 const MCSubtargetInfo &STI) const {
950 unsigned res = 0;
951
952 // Register list operand is always first operand of instruction and it is
953 // placed before memory operand (register + imm).
954
955 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
956 unsigned Reg = MI.getOperand(I).getReg();
957 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
958 if (RegNo != 31)
959 res++;
960 else
961 res |= 0x10;
962 }
963 return res;
964}
965
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000966unsigned
967MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
968 SmallVectorImpl<MCFixup> &Fixups,
969 const MCSubtargetInfo &STI) const {
970 return (MI.getNumOperands() - 4);
971}
972
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000973unsigned
974MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
975 SmallVectorImpl<MCFixup> &Fixups,
976 const MCSubtargetInfo &STI) const {
977 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
978}
979
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000980unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +0000981MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
982 SmallVectorImpl<MCFixup> &Fixups,
983 const MCSubtargetInfo &STI) const {
984 unsigned res = 0;
985
986 if (MI.getOperand(0).getReg() == Mips::A1 &&
987 MI.getOperand(1).getReg() == Mips::A2)
988 res = 0;
989 else if (MI.getOperand(0).getReg() == Mips::A1 &&
990 MI.getOperand(1).getReg() == Mips::A3)
991 res = 1;
992 else if (MI.getOperand(0).getReg() == Mips::A2 &&
993 MI.getOperand(1).getReg() == Mips::A3)
994 res = 2;
995 else if (MI.getOperand(0).getReg() == Mips::A0 &&
996 MI.getOperand(1).getReg() == Mips::S5)
997 res = 3;
998 else if (MI.getOperand(0).getReg() == Mips::A0 &&
999 MI.getOperand(1).getReg() == Mips::S6)
1000 res = 4;
1001 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1002 MI.getOperand(1).getReg() == Mips::A1)
1003 res = 5;
1004 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1005 MI.getOperand(1).getReg() == Mips::A2)
1006 res = 6;
1007 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1008 MI.getOperand(1).getReg() == Mips::A3)
1009 res = 7;
1010
1011 return res;
1012}
1013
1014unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001015MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1016 SmallVectorImpl<MCFixup> &Fixups,
1017 const MCSubtargetInfo &STI) const {
1018 const MCOperand &MO = MI.getOperand(OpNo);
1019 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1020 // The immediate is encoded as 'immediate >> 2'.
1021 unsigned Res = static_cast<unsigned>(MO.getImm());
1022 assert((Res & 3) == 0);
1023 return Res >> 2;
1024}
1025
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001026#include "MipsGenMCCodeEmitter.inc"