blob: d043bdf6fdd9d680954b5f7983f5fc99c8e6688b [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
Jozef Kolek9761e962015-01-12 12:03:34 +0000233/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
234/// target operand. If the machine operand requires relocation,
235/// record the relocation and return zero.
236unsigned MipsMCCodeEmitter::
237getBranchTarget7OpValueMM(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 "getBranchTargetOpValueMM expects only expressions or immediates");
248
249 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000250 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000251 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
252 return 0;
253}
254
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000255/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
256/// 10-bit branch target operand. If the machine operand requires relocation,
257/// record the relocation and return zero.
258unsigned MipsMCCodeEmitter::
259getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
260 SmallVectorImpl<MCFixup> &Fixups,
261 const MCSubtargetInfo &STI) const {
262
263 const MCOperand &MO = MI.getOperand(OpNo);
264
265 // If the destination is an immediate, divide by 2.
266 if (MO.isImm()) return MO.getImm() >> 1;
267
268 assert(MO.isExpr() &&
269 "getBranchTargetOpValuePC10 expects only expressions or immediates");
270
271 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000272 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000273 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
274 return 0;
275}
276
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000277/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
278/// target operand. If the machine operand requires relocation,
279/// record the relocation and return zero.
280unsigned MipsMCCodeEmitter::
281getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000282 SmallVectorImpl<MCFixup> &Fixups,
283 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000284
285 const MCOperand &MO = MI.getOperand(OpNo);
286
287 // If the destination is an immediate, divide by 2.
288 if (MO.isImm()) return MO.getImm() >> 1;
289
290 assert(MO.isExpr() &&
291 "getBranchTargetOpValueMM expects only expressions or immediates");
292
293 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000294 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000295 MCFixupKind(Mips::
296 fixup_MICROMIPS_PC16_S1)));
297 return 0;
298}
299
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000300/// getBranchTarget21OpValue - Return binary encoding of the branch
301/// target operand. If the machine operand requires relocation,
302/// record the relocation and return zero.
303unsigned MipsMCCodeEmitter::
304getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
305 SmallVectorImpl<MCFixup> &Fixups,
306 const MCSubtargetInfo &STI) const {
307
308 const MCOperand &MO = MI.getOperand(OpNo);
309
310 // If the destination is an immediate, divide by 4.
311 if (MO.isImm()) return MO.getImm() >> 2;
312
313 assert(MO.isExpr() &&
314 "getBranchTarget21OpValue expects only expressions or immediates");
315
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000316 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
317 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
318 Fixups.push_back(MCFixup::create(0, FixupExpression,
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
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_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000343 return 0;
344}
345
Zoran Jovanovica887b362015-11-30 12:56:18 +0000346/// getBranchTarget26OpValueMM - Return binary encoding of the branch
347/// target operand. If the machine operand requires relocation,
348/// record the relocation and return zero.
349unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
350 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
351 const MCSubtargetInfo &STI) const {
352
353 const MCOperand &MO = MI.getOperand(OpNo);
354
355 // If the destination is an immediate, divide by 2.
356 if (MO.isImm())
357 return MO.getImm() >> 1;
358
359 // TODO: Push 26 PC fixup.
360 return 0;
361}
362
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000363/// getJumpOffset16OpValue - Return binary encoding of the jump
364/// target operand. If the machine operand requires relocation,
365/// record the relocation and return zero.
366unsigned MipsMCCodeEmitter::
367getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
368 SmallVectorImpl<MCFixup> &Fixups,
369 const MCSubtargetInfo &STI) const {
370
371 const MCOperand &MO = MI.getOperand(OpNo);
372
373 if (MO.isImm()) return MO.getImm();
374
375 assert(MO.isExpr() &&
376 "getJumpOffset16OpValue expects only expressions or an immediate");
377
378 // TODO: Push fixup.
379 return 0;
380}
381
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000382/// getJumpTargetOpValue - Return binary encoding of the jump
383/// target operand. If the machine operand requires relocation,
384/// record the relocation and return zero.
385unsigned MipsMCCodeEmitter::
386getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000387 SmallVectorImpl<MCFixup> &Fixups,
388 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000389
390 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000391 // If the destination is an immediate, divide by 4.
392 if (MO.isImm()) return MO.getImm()>>2;
393
Jack Carter71e6a742012-09-06 00:43:26 +0000394 assert(MO.isExpr() &&
395 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000396
397 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000398 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000399 MCFixupKind(Mips::fixup_Mips_26)));
400 return 0;
401}
402
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000403unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000404getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000405 SmallVectorImpl<MCFixup> &Fixups,
406 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000407
408 const MCOperand &MO = MI.getOperand(OpNo);
409 // If the destination is an immediate, divide by 2.
410 if (MO.isImm()) return MO.getImm() >> 1;
411
412 assert(MO.isExpr() &&
413 "getJumpTargetOpValueMM expects only expressions or an immediate");
414
415 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000416 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000417 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
418 return 0;
419}
420
421unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000422getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
423 SmallVectorImpl<MCFixup> &Fixups,
424 const MCSubtargetInfo &STI) const {
425
426 const MCOperand &MO = MI.getOperand(OpNo);
427 if (MO.isImm()) {
428 // The immediate is encoded as 'immediate << 2'.
429 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
430 assert((Res & 3) == 0);
431 return Res >> 2;
432 }
433
434 assert(MO.isExpr() &&
435 "getUImm5Lsl2Encoding expects only expressions or an immediate");
436
437 return 0;
438}
439
440unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000441getSImm3Lsa2Value(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 int Value = MO.getImm();
448 return Value >> 2;
449 }
450
451 return 0;
452}
453
454unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000455getUImm6Lsl2Encoding(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 Value = MO.getImm();
462 return Value >> 2;
463 }
464
465 return 0;
466}
467
468unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000469getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
470 SmallVectorImpl<MCFixup> &Fixups,
471 const MCSubtargetInfo &STI) const {
472
473 const MCOperand &MO = MI.getOperand(OpNo);
474 if (MO.isImm()) {
475 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
476 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
477 }
478
479 return 0;
480}
481
482unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000483getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000484 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000485 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000486
Jim Grosbach13760bd2015-05-30 01:25:56 +0000487 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000488 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000489
Akira Hatanakafe384a22012-03-27 02:33:05 +0000490 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000491 if (Kind == MCExpr::Constant) {
492 return cast<MCConstantExpr>(Expr)->getValue();
493 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000494
Akira Hatanakafe384a22012-03-27 02:33:05 +0000495 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000496 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
497 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000498 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000499 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000500
501 if (Kind == MCExpr::Target) {
502 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
503
504 Mips::Fixups FixupKind = Mips::Fixups(0);
505 switch (MipsExpr->getKind()) {
506 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000507 case MipsMCExpr::VK_Mips_HIGHEST:
508 FixupKind = Mips::fixup_Mips_HIGHEST;
509 break;
510 case MipsMCExpr::VK_Mips_HIGHER:
511 FixupKind = Mips::fixup_Mips_HIGHER;
512 break;
513 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000514 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
515 : Mips::fixup_Mips_HI16;
516 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000517 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000518 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
519 : Mips::fixup_Mips_LO16;
520 break;
521 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000522 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000523 return 0;
524 }
525
Jack Carterb5cf5902013-04-17 00:18:04 +0000526 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000527 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000528
Mark Seabornc3bd1772013-12-31 13:05:15 +0000529 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
530 default: llvm_unreachable("Unknown fixup kind!");
531 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000532 case MCSymbolRefExpr::VK_None:
533 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
534 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000535 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
536 FixupKind = Mips::fixup_Mips_GPOFF_HI;
537 break;
538 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
539 FixupKind = Mips::fixup_Mips_GPOFF_LO;
540 break;
541 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000542 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000543 : Mips::fixup_Mips_GOT_PAGE;
544 break;
545 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000546 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000547 : Mips::fixup_Mips_GOT_OFST;
548 break;
549 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000550 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000551 : Mips::fixup_Mips_GOT_DISP;
552 break;
553 case MCSymbolRefExpr::VK_Mips_GPREL:
554 FixupKind = Mips::fixup_Mips_GPREL16;
555 break;
556 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000557 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000558 : Mips::fixup_Mips_CALL16;
559 break;
560 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000561 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000562 : Mips::fixup_Mips_GOT_Global;
563 break;
564 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000565 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000566 : Mips::fixup_Mips_GOT_Local;
567 break;
568 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000569 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000570 : Mips::fixup_Mips_HI16;
571 break;
572 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000573 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000574 : Mips::fixup_Mips_LO16;
575 break;
576 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000577 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000578 : Mips::fixup_Mips_TLSGD;
579 break;
580 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000581 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000582 : Mips::fixup_Mips_TLSLDM;
583 break;
584 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000585 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000586 : Mips::fixup_Mips_DTPREL_HI;
587 break;
588 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000589 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000590 : Mips::fixup_Mips_DTPREL_LO;
591 break;
592 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
593 FixupKind = Mips::fixup_Mips_GOTTPREL;
594 break;
595 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000596 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000597 : Mips::fixup_Mips_TPREL_HI;
598 break;
599 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000600 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000601 : Mips::fixup_Mips_TPREL_LO;
602 break;
603 case MCSymbolRefExpr::VK_Mips_HIGHER:
604 FixupKind = Mips::fixup_Mips_HIGHER;
605 break;
606 case MCSymbolRefExpr::VK_Mips_HIGHEST:
607 FixupKind = Mips::fixup_Mips_HIGHEST;
608 break;
609 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
610 FixupKind = Mips::fixup_Mips_GOT_HI16;
611 break;
612 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
613 FixupKind = Mips::fixup_Mips_GOT_LO16;
614 break;
615 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
616 FixupKind = Mips::fixup_Mips_CALL_HI16;
617 break;
618 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
619 FixupKind = Mips::fixup_Mips_CALL_LO16;
620 break;
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000621 case MCSymbolRefExpr::VK_Mips_PCREL_HI16:
622 FixupKind = Mips::fixup_MIPS_PCHI16;
623 break;
624 case MCSymbolRefExpr::VK_Mips_PCREL_LO16:
625 FixupKind = Mips::fixup_MIPS_PCLO16;
626 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000627 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000628
Jim Grosbach63661f82015-05-15 19:13:05 +0000629 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000630 return 0;
631 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000632 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000633}
634
Jack Carterb5cf5902013-04-17 00:18:04 +0000635/// getMachineOpValue - Return binary encoding of operand. If the machine
636/// operand requires relocation, record the relocation and return zero.
637unsigned MipsMCCodeEmitter::
638getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000639 SmallVectorImpl<MCFixup> &Fixups,
640 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000641 if (MO.isReg()) {
642 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000643 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000644 return RegNo;
645 } else if (MO.isImm()) {
646 return static_cast<unsigned>(MO.getImm());
647 } else if (MO.isFPImm()) {
648 return static_cast<unsigned>(APFloat(MO.getFPImm())
649 .bitcastToAPInt().getHiBits(32).getLimitedValue());
650 }
651 // MO must be an Expr.
652 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000653 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000654}
655
Matheus Almeida6b59c442013-12-05 11:06:22 +0000656/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
657/// instructions.
658unsigned
659MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000660 SmallVectorImpl<MCFixup> &Fixups,
661 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000662 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
663 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000664 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
665 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000666
667 // The immediate field of an LD/ST instruction is scaled which means it must
668 // be divided (when encoding) by the size (in bytes) of the instructions'
669 // data format.
670 // .b - 1 byte
671 // .h - 2 bytes
672 // .w - 4 bytes
673 // .d - 8 bytes
674 switch(MI.getOpcode())
675 {
676 default:
677 assert (0 && "Unexpected instruction");
678 break;
679 case Mips::LD_B:
680 case Mips::ST_B:
681 // We don't need to scale the offset in this case
682 break;
683 case Mips::LD_H:
684 case Mips::ST_H:
685 OffBits >>= 1;
686 break;
687 case Mips::LD_W:
688 case Mips::ST_W:
689 OffBits >>= 2;
690 break;
691 case Mips::LD_D:
692 case Mips::ST_D:
693 OffBits >>= 3;
694 break;
695 }
696
697 return (OffBits & 0xFFFF) | RegBits;
698}
699
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000700/// getMemEncoding - Return binary encoding of memory related operand.
701/// If the offset operand requires relocation, record the relocation.
702unsigned
703MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000704 SmallVectorImpl<MCFixup> &Fixups,
705 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000706 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
707 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000708 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
709 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000710
711 return (OffBits & 0xFFFF) | RegBits;
712}
713
Jack Carter97700972013-08-13 20:19:16 +0000714unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000715getMemEncodingMMImm4(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);
724
725 return (OffBits & 0xF) | RegBits;
726}
727
728unsigned MipsMCCodeEmitter::
729getMemEncodingMMImm4Lsl1(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) >> 1;
738
739 return (OffBits & 0xF) | RegBits;
740}
741
742unsigned MipsMCCodeEmitter::
743getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
744 SmallVectorImpl<MCFixup> &Fixups,
745 const MCSubtargetInfo &STI) const {
746 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
747 assert(MI.getOperand(OpNo).isReg());
748 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
749 Fixups, STI) << 4;
750 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
751 Fixups, STI) >> 2;
752
753 return (OffBits & 0xF) | RegBits;
754}
755
756unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000757getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
758 SmallVectorImpl<MCFixup> &Fixups,
759 const MCSubtargetInfo &STI) const {
760 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
761 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000762 (MI.getOperand(OpNo).getReg() == Mips::SP ||
763 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000764 "Unexpected base register!");
765 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
766 Fixups, STI) >> 2;
767
768 return OffBits & 0x1F;
769}
770
771unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000772getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
773 SmallVectorImpl<MCFixup> &Fixups,
774 const MCSubtargetInfo &STI) const {
775 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
776 assert(MI.getOperand(OpNo).isReg() &&
777 MI.getOperand(OpNo).getReg() == Mips::GP &&
778 "Unexpected base register!");
779
780 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
781 Fixups, STI) >> 2;
782
783 return OffBits & 0x7F;
784}
785
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000786unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000787getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
788 SmallVectorImpl<MCFixup> &Fixups,
789 const MCSubtargetInfo &STI) const {
790 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
791 assert(MI.getOperand(OpNo).isReg());
792 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
793 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000794 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000795
796 return (OffBits & 0x1FF) | RegBits;
797}
798
Jozef Koleke10a02e2015-01-28 17:27:26 +0000799unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000800getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000801 SmallVectorImpl<MCFixup> &Fixups,
802 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000803 // opNum can be invalid if instruction had reglist as operand.
804 // MemOperand is always last operand of instruction (base + offset).
805 switch (MI.getOpcode()) {
806 default:
807 break;
808 case Mips::SWM32_MM:
809 case Mips::LWM32_MM:
810 OpNo = MI.getNumOperands() - 2;
811 break;
812 }
813
Jack Carter97700972013-08-13 20:19:16 +0000814 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
815 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000816 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
817 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000818
819 return (OffBits & 0x0FFF) | RegBits;
820}
821
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000822unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000823getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
824 SmallVectorImpl<MCFixup> &Fixups,
825 const MCSubtargetInfo &STI) const {
826 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
827 assert(MI.getOperand(OpNo).isReg());
828 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
829 STI) << 16;
830 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
831
832 return (OffBits & 0xFFFF) | RegBits;
833}
834
835unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000836getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
837 SmallVectorImpl<MCFixup> &Fixups,
838 const MCSubtargetInfo &STI) const {
839 // opNum can be invalid if instruction had reglist as operand
840 // MemOperand is always last operand of instruction (base + offset)
841 switch (MI.getOpcode()) {
842 default:
843 break;
844 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000845 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000846 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000847 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000848 OpNo = MI.getNumOperands() - 2;
849 break;
850 }
851
852 // Offset is encoded in bits 4-0.
853 assert(MI.getOperand(OpNo).isReg());
854 // Base register is always SP - thus it is not encoded.
855 assert(MI.getOperand(OpNo+1).isImm());
856 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
857
858 return ((OffBits >> 2) & 0x0F);
859}
860
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000861// FIXME: should be called getMSBEncoding
862//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000863unsigned
864MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000865 SmallVectorImpl<MCFixup> &Fixups,
866 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000867 assert(MI.getOperand(OpNo-1).isImm());
868 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000869 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
870 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000871
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000872 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000873}
874
Daniel Sandersea4f6532015-11-06 12:22:31 +0000875template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000876unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000877MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
878 SmallVectorImpl<MCFixup> &Fixups,
879 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000880 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000881 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
882 Value -= Offset;
883 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000884}
885
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000886unsigned
887MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
888 SmallVectorImpl<MCFixup> &Fixups,
889 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000890 const MCOperand &MO = MI.getOperand(OpNo);
891 if (MO.isImm()) {
892 // The immediate is encoded as 'immediate << 2'.
893 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
894 assert((Res & 3) == 0);
895 return Res >> 2;
896 }
897
898 assert(MO.isExpr() &&
899 "getSimm19Lsl2Encoding expects only expressions or an immediate");
900
901 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000902 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000903 MCFixupKind(Mips::fixup_MIPS_PC19_S2)));
904 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000905}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000906
Zoran Jovanovic28551422014-06-09 09:49:51 +0000907unsigned
908MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
909 SmallVectorImpl<MCFixup> &Fixups,
910 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000911 const MCOperand &MO = MI.getOperand(OpNo);
912 if (MO.isImm()) {
913 // The immediate is encoded as 'immediate << 3'.
914 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
915 assert((Res & 7) == 0);
916 return Res >> 3;
917 }
918
919 assert(MO.isExpr() &&
920 "getSimm18Lsl2Encoding expects only expressions or an immediate");
921
922 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000923 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000924 MCFixupKind(Mips::fixup_MIPS_PC18_S3)));
925 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +0000926}
927
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +0000928unsigned
929MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
930 SmallVectorImpl<MCFixup> &Fixups,
931 const MCSubtargetInfo &STI) const {
932 assert(MI.getOperand(OpNo).isImm());
933 const MCOperand &MO = MI.getOperand(OpNo);
934 return MO.getImm() % 8;
935}
936
Zoran Jovanovic88531712014-11-05 17:31:00 +0000937unsigned
938MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
939 SmallVectorImpl<MCFixup> &Fixups,
940 const MCSubtargetInfo &STI) const {
941 assert(MI.getOperand(OpNo).isImm());
942 const MCOperand &MO = MI.getOperand(OpNo);
943 unsigned Value = MO.getImm();
944 switch (Value) {
945 case 128: return 0x0;
946 case 1: return 0x1;
947 case 2: return 0x2;
948 case 3: return 0x3;
949 case 4: return 0x4;
950 case 7: return 0x5;
951 case 8: return 0x6;
952 case 15: return 0x7;
953 case 16: return 0x8;
954 case 31: return 0x9;
955 case 32: return 0xa;
956 case 63: return 0xb;
957 case 64: return 0xc;
958 case 255: return 0xd;
959 case 32768: return 0xe;
960 case 65535: return 0xf;
961 }
962 llvm_unreachable("Unexpected value");
963}
964
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000965unsigned
966MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
967 SmallVectorImpl<MCFixup> &Fixups,
968 const MCSubtargetInfo &STI) const {
969 unsigned res = 0;
970
971 // Register list operand is always first operand of instruction and it is
972 // placed before memory operand (register + imm).
973
974 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
975 unsigned Reg = MI.getOperand(I).getReg();
976 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
977 if (RegNo != 31)
978 res++;
979 else
980 res |= 0x10;
981 }
982 return res;
983}
984
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000985unsigned
986MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
987 SmallVectorImpl<MCFixup> &Fixups,
988 const MCSubtargetInfo &STI) const {
989 return (MI.getNumOperands() - 4);
990}
991
Zoran Jovanovic2deca342014-12-16 14:59:10 +0000992unsigned
993MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
994 SmallVectorImpl<MCFixup> &Fixups,
995 const MCSubtargetInfo &STI) const {
996 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
997}
998
Jozef Kolek2c6d7322015-01-21 12:10:11 +0000999unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +00001000MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1001 SmallVectorImpl<MCFixup> &Fixups,
1002 const MCSubtargetInfo &STI) const {
1003 unsigned res = 0;
1004
1005 if (MI.getOperand(0).getReg() == Mips::A1 &&
1006 MI.getOperand(1).getReg() == Mips::A2)
1007 res = 0;
1008 else if (MI.getOperand(0).getReg() == Mips::A1 &&
1009 MI.getOperand(1).getReg() == Mips::A3)
1010 res = 1;
1011 else if (MI.getOperand(0).getReg() == Mips::A2 &&
1012 MI.getOperand(1).getReg() == Mips::A3)
1013 res = 2;
1014 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1015 MI.getOperand(1).getReg() == Mips::S5)
1016 res = 3;
1017 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1018 MI.getOperand(1).getReg() == Mips::S6)
1019 res = 4;
1020 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1021 MI.getOperand(1).getReg() == Mips::A1)
1022 res = 5;
1023 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1024 MI.getOperand(1).getReg() == Mips::A2)
1025 res = 6;
1026 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1027 MI.getOperand(1).getReg() == Mips::A3)
1028 res = 7;
1029
1030 return res;
1031}
1032
1033unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001034MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1035 SmallVectorImpl<MCFixup> &Fixups,
1036 const MCSubtargetInfo &STI) const {
1037 const MCOperand &MO = MI.getOperand(OpNo);
1038 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1039 // The immediate is encoded as 'immediate >> 2'.
1040 unsigned Res = static_cast<unsigned>(MO.getImm());
1041 assert((Res & 3) == 0);
1042 return Res >> 2;
1043}
1044
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001045#include "MipsGenMCCodeEmitter.inc"