blob: 711e548cb08d2fe4676c0c1f5eed40e61f6b9672 [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;
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +000081 case Mips::DSLL_MM64R6:
82 Inst.setOpcode(Mips::DSLL32_MM64R6);
83 return;
84 case Mips::DSRA_MM64R6:
85 Inst.setOpcode(Mips::DSRA32_MM64R6);
86 return;
87 case Mips::DROTR_MM64R6:
88 Inst.setOpcode(Mips::DROTR32_MM64R6);
89 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000090 }
91}
92
Daniel Sanders611eb822016-02-29 15:26:54 +000093// Pick a DINS instruction variant based on the pos and size operands
94static void LowerDins(MCInst& InstIn) {
95 assert(InstIn.getNumOperands() == 5 &&
96 "Invalid no. of machine operands for DINS!");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000097
98 assert(InstIn.getOperand(2).isImm());
99 int64_t pos = InstIn.getOperand(2).getImm();
100 assert(InstIn.getOperand(3).isImm());
101 int64_t size = InstIn.getOperand(3).getImm();
102
103 if (size <= 32) {
Daniel Sanders611eb822016-02-29 15:26:54 +0000104 if (pos < 32) // DINS, do nothing
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000105 return;
Daniel Sanders611eb822016-02-29 15:26:54 +0000106 // DINSU
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000107 InstIn.getOperand(2).setImm(pos - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +0000108 InstIn.setOpcode(Mips::DINSU);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000109 return;
110 }
Daniel Sanders611eb822016-02-29 15:26:54 +0000111 // DINSM
112 assert(pos < 32 && "DINS cannot have both size and pos > 32");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000113 InstIn.getOperand(3).setImm(size - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +0000114 InstIn.setOpcode(Mips::DINSM);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000115 return;
116}
117
Simon Dardis669d8dd2016-05-18 10:38:01 +0000118// Fix a bad compact branch encoding for beqc/bnec.
119void MipsMCCodeEmitter::LowerCompactBranch(MCInst& Inst) const {
120
121 // Encoding may be illegal !(rs < rt), but this situation is
122 // easily fixed.
123 unsigned RegOp0 = Inst.getOperand(0).getReg();
124 unsigned RegOp1 = Inst.getOperand(1).getReg();
125
126 unsigned Reg0 = Ctx.getRegisterInfo()->getEncodingValue(RegOp0);
127 unsigned Reg1 = Ctx.getRegisterInfo()->getEncodingValue(RegOp1);
128
Simon Dardisb60833c2016-05-31 17:34:42 +0000129 if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC) {
130 assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
131 if (Reg0 < Reg1)
132 return;
133 } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
134 if (Reg0 >= Reg1)
135 return;
Hrvoje Vargac962c492016-06-09 12:57:23 +0000136 } else if (Inst.getOpcode() == Mips::BNVC_MMR6 ||
137 Inst.getOpcode() == Mips::BOVC_MMR6) {
138 if (Reg1 >= Reg0)
139 return;
Simon Dardisb60833c2016-05-31 17:34:42 +0000140 } else
141 llvm_unreachable("Cannot rewrite unknown branch!");
Simon Dardis669d8dd2016-05-18 10:38:01 +0000142
143 Inst.getOperand(0).setReg(RegOp1);
144 Inst.getOperand(1).setReg(RegOp0);
145
146}
147
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000148bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000149 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000150}
151
Jozef Kolekc22555d2015-04-20 12:23:06 +0000152bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000153 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000154}
155
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000156void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
157 OS << (char)C;
158}
159
160void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
161 const MCSubtargetInfo &STI,
162 raw_ostream &OS) const {
163 // Output the instruction encoding in little endian byte order.
164 // Little-endian byte ordering:
165 // mips32r2: 4 | 3 | 2 | 1
166 // microMIPS: 2 | 1 | 4 | 3
167 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
168 EmitInstruction(Val >> 16, 2, STI, OS);
169 EmitInstruction(Val, 2, STI, OS);
170 } else {
171 for (unsigned i = 0; i < Size; ++i) {
172 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
173 EmitByte((Val >> Shift) & 0xff, OS);
174 }
175 }
176}
177
Jim Grosbach91df21f2015-05-15 19:13:16 +0000178/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000179/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000180void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000181encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000182 SmallVectorImpl<MCFixup> &Fixups,
183 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000184{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000185
186 // Non-pseudo instructions that get changed for direct object
187 // only based on operand values.
188 // If this list of instructions get much longer we will move
189 // the check to a function call. Until then, this is more efficient.
190 MCInst TmpInst = MI;
191 switch (MI.getOpcode()) {
192 // If shift amount is >= 32 it the inst needs to be lowered further
193 case Mips::DSLL:
194 case Mips::DSRL:
195 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000196 case Mips::DROTR:
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +0000197 case Mips::DSLL_MM64R6:
198 case Mips::DSRA_MM64R6:
199 case Mips::DROTR_MM64R6:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000200 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000201 break;
202 // Double extract instruction is chosen by pos and size operands
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000203 case Mips::DINS:
Daniel Sanders611eb822016-02-29 15:26:54 +0000204 LowerDins(TmpInst);
Simon Dardis669d8dd2016-05-18 10:38:01 +0000205 break;
Simon Dardisb60833c2016-05-31 17:34:42 +0000206 // Compact branches, enforce encoding restrictions.
Simon Dardis669d8dd2016-05-18 10:38:01 +0000207 case Mips::BEQC:
208 case Mips::BNEC:
Simon Dardisb60833c2016-05-31 17:34:42 +0000209 case Mips::BOVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000210 case Mips::BOVC_MMR6:
Simon Dardisb60833c2016-05-31 17:34:42 +0000211 case Mips::BNVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000212 case Mips::BNVC_MMR6:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000213 LowerCompactBranch(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000214 }
215
Jack Carter97700972013-08-13 20:19:16 +0000216 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000217 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000218
219 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000220 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000221 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000222 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000223 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
224 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000225 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000226
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000227 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000228 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000229 if (isMips32r6(STI)) {
230 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
231 if (NewOpcode == -1)
232 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
233 }
234 else
235 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
236
Zoran Jovanovic2e386d32015-10-12 16:07:25 +0000237 // Check whether it is Dsp instruction.
238 if (NewOpcode == -1)
239 NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
240
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000241 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000242 if (Fixups.size() > N)
243 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000244
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000245 Opcode = NewOpcode;
246 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000247 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000248 }
249 }
250
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000251 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000252
Jack Carter5b5559d2012-10-03 21:58:54 +0000253 // Get byte count of instruction
254 unsigned Size = Desc.getSize();
255 if (!Size)
256 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000257
David Woodhoused2cca112014-01-28 23:13:25 +0000258 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000259}
260
261/// getBranchTargetOpValue - Return binary encoding of the branch
262/// target operand. If the machine operand requires relocation,
263/// record the relocation and return zero.
264unsigned MipsMCCodeEmitter::
265getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000266 SmallVectorImpl<MCFixup> &Fixups,
267 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000268
269 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000270
Jack Carter4f69a0f2013-03-22 00:29:10 +0000271 // If the destination is an immediate, divide by 4.
272 if (MO.isImm()) return MO.getImm() >> 2;
273
Jack Carter71e6a742012-09-06 00:43:26 +0000274 assert(MO.isExpr() &&
275 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000276
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000277 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
278 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
279 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000280 MCFixupKind(Mips::fixup_Mips_PC16)));
281 return 0;
282}
283
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +0000284/// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
285/// target operand. If the machine operand requires relocation,
286/// record the relocation and return zero.
287unsigned MipsMCCodeEmitter::
288getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
289 SmallVectorImpl<MCFixup> &Fixups,
290 const MCSubtargetInfo &STI) const {
291
292 const MCOperand &MO = MI.getOperand(OpNo);
293
294 // If the destination is an immediate, divide by 2.
295 if (MO.isImm()) return MO.getImm() >> 1;
296
297 assert(MO.isExpr() &&
298 "getBranchTargetOpValue expects only expressions or immediates");
299
300 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
301 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
302 Fixups.push_back(MCFixup::create(0, FixupExpression,
303 MCFixupKind(Mips::fixup_Mips_PC16)));
304 return 0;
305}
306
Hrvoje Vargac962c492016-06-09 12:57:23 +0000307/// getBranchTargetOpValueMMR6 - Return binary encoding of the branch
308/// target operand. If the machine operand requires relocation,
309/// record the relocation and return zero.
310unsigned MipsMCCodeEmitter::
311getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
312 SmallVectorImpl<MCFixup> &Fixups,
313 const MCSubtargetInfo &STI) const {
314
315 const MCOperand &MO = MI.getOperand(OpNo);
316
317 // If the destination is an immediate, divide by 2.
318 if (MO.isImm())
319 return MO.getImm() >> 1;
320
321 assert(MO.isExpr() &&
322 "getBranchTargetOpValueMMR6 expects only expressions or immediates");
323
324 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
325 MO.getExpr(), MCConstantExpr::create(-2, Ctx), Ctx);
326 Fixups.push_back(MCFixup::create(0, FixupExpression,
327 MCFixupKind(Mips::fixup_Mips_PC16)));
328 return 0;
329}
330
Jozef Kolek9761e962015-01-12 12:03:34 +0000331/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
332/// target operand. If the machine operand requires relocation,
333/// record the relocation and return zero.
334unsigned MipsMCCodeEmitter::
335getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
336 SmallVectorImpl<MCFixup> &Fixups,
337 const MCSubtargetInfo &STI) const {
338
339 const MCOperand &MO = MI.getOperand(OpNo);
340
341 // If the destination is an immediate, divide by 2.
342 if (MO.isImm()) return MO.getImm() >> 1;
343
344 assert(MO.isExpr() &&
345 "getBranchTargetOpValueMM expects only expressions or immediates");
346
347 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000348 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000349 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
350 return 0;
351}
352
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000353/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
354/// 10-bit branch target operand. If the machine operand requires relocation,
355/// record the relocation and return zero.
356unsigned MipsMCCodeEmitter::
357getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
358 SmallVectorImpl<MCFixup> &Fixups,
359 const MCSubtargetInfo &STI) const {
360
361 const MCOperand &MO = MI.getOperand(OpNo);
362
363 // If the destination is an immediate, divide by 2.
364 if (MO.isImm()) return MO.getImm() >> 1;
365
366 assert(MO.isExpr() &&
367 "getBranchTargetOpValuePC10 expects only expressions or immediates");
368
369 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000370 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000371 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
372 return 0;
373}
374
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000375/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
376/// target operand. If the machine operand requires relocation,
377/// record the relocation and return zero.
378unsigned MipsMCCodeEmitter::
379getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000380 SmallVectorImpl<MCFixup> &Fixups,
381 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000382
383 const MCOperand &MO = MI.getOperand(OpNo);
384
385 // If the destination is an immediate, divide by 2.
386 if (MO.isImm()) return MO.getImm() >> 1;
387
388 assert(MO.isExpr() &&
389 "getBranchTargetOpValueMM expects only expressions or immediates");
390
391 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000392 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000393 MCFixupKind(Mips::
394 fixup_MICROMIPS_PC16_S1)));
395 return 0;
396}
397
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000398/// getBranchTarget21OpValue - Return binary encoding of the branch
399/// target operand. If the machine operand requires relocation,
400/// record the relocation and return zero.
401unsigned MipsMCCodeEmitter::
402getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
403 SmallVectorImpl<MCFixup> &Fixups,
404 const MCSubtargetInfo &STI) const {
405
406 const MCOperand &MO = MI.getOperand(OpNo);
407
408 // If the destination is an immediate, divide by 4.
409 if (MO.isImm()) return MO.getImm() >> 2;
410
411 assert(MO.isExpr() &&
412 "getBranchTarget21OpValue expects only expressions or immediates");
413
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000414 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
415 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
416 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000417 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000418 return 0;
419}
420
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000421/// getBranchTarget21OpValueMM - Return binary encoding of the branch
422/// target operand for microMIPS. If the machine operand requires
423/// relocation, record the relocation and return zero.
424unsigned MipsMCCodeEmitter::
425getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
426 SmallVectorImpl<MCFixup> &Fixups,
427 const MCSubtargetInfo &STI) const {
428
429 const MCOperand &MO = MI.getOperand(OpNo);
430
431 // If the destination is an immediate, divide by 2.
432 if (MO.isImm()) return MO.getImm() >> 1;
433
434 assert(MO.isExpr() &&
435 "getBranchTarget21OpValueMM expects only expressions or immediates");
436
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000437 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
438 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
439 Fixups.push_back(MCFixup::create(0, FixupExpression,
440 MCFixupKind(Mips::fixup_MICROMIPS_PC21_S1)));
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000441 return 0;
442}
443
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000444/// getBranchTarget26OpValue - Return binary encoding of the branch
445/// target operand. If the machine operand requires relocation,
446/// record the relocation and return zero.
447unsigned MipsMCCodeEmitter::
448getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
449 SmallVectorImpl<MCFixup> &Fixups,
450 const MCSubtargetInfo &STI) const {
451
452 const MCOperand &MO = MI.getOperand(OpNo);
453
454 // If the destination is an immediate, divide by 4.
455 if (MO.isImm()) return MO.getImm() >> 2;
456
457 assert(MO.isExpr() &&
458 "getBranchTarget26OpValue expects only expressions or immediates");
459
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000460 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
461 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
462 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000463 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000464 return 0;
465}
466
Zoran Jovanovica887b362015-11-30 12:56:18 +0000467/// getBranchTarget26OpValueMM - Return binary encoding of the branch
468/// target operand. If the machine operand requires relocation,
469/// record the relocation and return zero.
470unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
471 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
472 const MCSubtargetInfo &STI) const {
473
474 const MCOperand &MO = MI.getOperand(OpNo);
475
476 // If the destination is an immediate, divide by 2.
477 if (MO.isImm())
478 return MO.getImm() >> 1;
479
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000480 assert(MO.isExpr() &&
481 "getBranchTarget26OpValueMM expects only expressions or immediates");
482
483 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
484 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
485 Fixups.push_back(MCFixup::create(0, FixupExpression,
486 MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
Zoran Jovanovica887b362015-11-30 12:56:18 +0000487 return 0;
488}
489
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000490/// getJumpOffset16OpValue - Return binary encoding of the jump
491/// target operand. If the machine operand requires relocation,
492/// record the relocation and return zero.
493unsigned MipsMCCodeEmitter::
494getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
495 SmallVectorImpl<MCFixup> &Fixups,
496 const MCSubtargetInfo &STI) const {
497
498 const MCOperand &MO = MI.getOperand(OpNo);
499
500 if (MO.isImm()) return MO.getImm();
501
502 assert(MO.isExpr() &&
503 "getJumpOffset16OpValue expects only expressions or an immediate");
504
505 // TODO: Push fixup.
506 return 0;
507}
508
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000509/// getJumpTargetOpValue - Return binary encoding of the jump
510/// target operand. If the machine operand requires relocation,
511/// record the relocation and return zero.
512unsigned MipsMCCodeEmitter::
513getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000514 SmallVectorImpl<MCFixup> &Fixups,
515 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000516
517 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000518 // If the destination is an immediate, divide by 4.
519 if (MO.isImm()) return MO.getImm()>>2;
520
Jack Carter71e6a742012-09-06 00:43:26 +0000521 assert(MO.isExpr() &&
522 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000523
524 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000525 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000526 MCFixupKind(Mips::fixup_Mips_26)));
527 return 0;
528}
529
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000530unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000531getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000532 SmallVectorImpl<MCFixup> &Fixups,
533 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000534
535 const MCOperand &MO = MI.getOperand(OpNo);
536 // If the destination is an immediate, divide by 2.
537 if (MO.isImm()) return MO.getImm() >> 1;
538
539 assert(MO.isExpr() &&
540 "getJumpTargetOpValueMM expects only expressions or an immediate");
541
542 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000543 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000544 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
545 return 0;
546}
547
548unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000549getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
550 SmallVectorImpl<MCFixup> &Fixups,
551 const MCSubtargetInfo &STI) const {
552
553 const MCOperand &MO = MI.getOperand(OpNo);
554 if (MO.isImm()) {
555 // The immediate is encoded as 'immediate << 2'.
556 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
557 assert((Res & 3) == 0);
558 return Res >> 2;
559 }
560
561 assert(MO.isExpr() &&
562 "getUImm5Lsl2Encoding expects only expressions or an immediate");
563
564 return 0;
565}
566
567unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000568getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
569 SmallVectorImpl<MCFixup> &Fixups,
570 const MCSubtargetInfo &STI) const {
571
572 const MCOperand &MO = MI.getOperand(OpNo);
573 if (MO.isImm()) {
574 int Value = MO.getImm();
575 return Value >> 2;
576 }
577
578 return 0;
579}
580
581unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000582getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
583 SmallVectorImpl<MCFixup> &Fixups,
584 const MCSubtargetInfo &STI) const {
585
586 const MCOperand &MO = MI.getOperand(OpNo);
587 if (MO.isImm()) {
588 unsigned Value = MO.getImm();
589 return Value >> 2;
590 }
591
592 return 0;
593}
594
595unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000596getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
597 SmallVectorImpl<MCFixup> &Fixups,
598 const MCSubtargetInfo &STI) const {
599
600 const MCOperand &MO = MI.getOperand(OpNo);
601 if (MO.isImm()) {
602 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
603 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
604 }
605
606 return 0;
607}
608
609unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000610getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000611 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000612 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000613
Jim Grosbach13760bd2015-05-30 01:25:56 +0000614 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000615 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000616
Akira Hatanakafe384a22012-03-27 02:33:05 +0000617 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000618 if (Kind == MCExpr::Constant) {
619 return cast<MCConstantExpr>(Expr)->getValue();
620 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000621
Akira Hatanakafe384a22012-03-27 02:33:05 +0000622 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000623 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
624 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000625 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000626 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000627
628 if (Kind == MCExpr::Target) {
629 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
630
631 Mips::Fixups FixupKind = Mips::Fixups(0);
632 switch (MipsExpr->getKind()) {
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000633 case MipsMCExpr::MEK_NEG:
634 case MipsMCExpr::MEK_None:
635 case MipsMCExpr::MEK_Special:
636 llvm_unreachable("Unhandled fixup kind!");
637 break;
638 case MipsMCExpr::MEK_CALL_HI16:
639 FixupKind = Mips::fixup_Mips_CALL_HI16;
640 break;
641 case MipsMCExpr::MEK_CALL_LO16:
642 FixupKind = Mips::fixup_Mips_CALL_LO16;
643 break;
644 case MipsMCExpr::MEK_DTPREL_HI:
645 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
646 : Mips::fixup_Mips_DTPREL_HI;
647 break;
648 case MipsMCExpr::MEK_DTPREL_LO:
649 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
650 : Mips::fixup_Mips_DTPREL_LO;
651 break;
652 case MipsMCExpr::MEK_GOTTPREL:
653 FixupKind = Mips::fixup_Mips_GOTTPREL;
654 break;
655 case MipsMCExpr::MEK_GOT:
656 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
657 : Mips::fixup_Mips_GOT;
658 break;
659 case MipsMCExpr::MEK_GOT_CALL:
660 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
661 : Mips::fixup_Mips_CALL16;
662 break;
663 case MipsMCExpr::MEK_GOT_DISP:
664 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
665 : Mips::fixup_Mips_GOT_DISP;
666 break;
667 case MipsMCExpr::MEK_GOT_HI16:
668 FixupKind = Mips::fixup_Mips_GOT_HI16;
669 break;
670 case MipsMCExpr::MEK_GOT_LO16:
671 FixupKind = Mips::fixup_Mips_GOT_LO16;
672 break;
673 case MipsMCExpr::MEK_GOT_PAGE:
674 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
675 : Mips::fixup_Mips_GOT_PAGE;
676 break;
677 case MipsMCExpr::MEK_GOT_OFST:
678 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
679 : Mips::fixup_Mips_GOT_OFST;
680 break;
681 case MipsMCExpr::MEK_GPREL:
682 FixupKind = Mips::fixup_Mips_GPREL16;
683 break;
684 case MipsMCExpr::MEK_LO: {
685 // Check for %lo(%neg(%gp_rel(X)))
686 if (MipsExpr->isGpOff()) {
687 FixupKind = Mips::fixup_Mips_GPOFF_LO;
688 break;
689 }
690 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
691 : Mips::fixup_Mips_LO16;
692 break;
693 }
694 case MipsMCExpr::MEK_HIGHEST:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000695 FixupKind = Mips::fixup_Mips_HIGHEST;
696 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000697 case MipsMCExpr::MEK_HIGHER:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000698 FixupKind = Mips::fixup_Mips_HIGHER;
699 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000700 case MipsMCExpr::MEK_HI:
701 // Check for %hi(%neg(%gp_rel(X)))
702 if (MipsExpr->isGpOff()) {
703 FixupKind = Mips::fixup_Mips_GPOFF_HI;
704 break;
705 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000706 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
707 : Mips::fixup_Mips_HI16;
708 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000709 case MipsMCExpr::MEK_PCREL_HI16:
710 FixupKind = Mips::fixup_MIPS_PCHI16;
711 break;
712 case MipsMCExpr::MEK_PCREL_LO16:
713 FixupKind = Mips::fixup_MIPS_PCLO16;
714 break;
715 case MipsMCExpr::MEK_TLSGD:
716 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
717 : Mips::fixup_Mips_TLSGD;
718 break;
719 case MipsMCExpr::MEK_TLSLDM:
720 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
721 : Mips::fixup_Mips_TLSLDM;
722 break;
723 case MipsMCExpr::MEK_TPREL_HI:
724 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
725 : Mips::fixup_Mips_TPREL_HI;
726 break;
727 case MipsMCExpr::MEK_TPREL_LO:
728 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
729 : Mips::fixup_Mips_TPREL_LO;
Petar Jovanovica5da5882014-02-04 18:41:57 +0000730 break;
731 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000732 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000733 return 0;
734 }
735
Jack Carterb5cf5902013-04-17 00:18:04 +0000736 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000737 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000738
Mark Seabornc3bd1772013-12-31 13:05:15 +0000739 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
740 default: llvm_unreachable("Unknown fixup kind!");
741 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000742 case MCSymbolRefExpr::VK_None:
743 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
744 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000745 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000746
Jim Grosbach63661f82015-05-15 19:13:05 +0000747 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000748 return 0;
749 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000750 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000751}
752
Jack Carterb5cf5902013-04-17 00:18:04 +0000753/// getMachineOpValue - Return binary encoding of operand. If the machine
754/// operand requires relocation, record the relocation and return zero.
755unsigned MipsMCCodeEmitter::
756getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000757 SmallVectorImpl<MCFixup> &Fixups,
758 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000759 if (MO.isReg()) {
760 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000761 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000762 return RegNo;
763 } else if (MO.isImm()) {
764 return static_cast<unsigned>(MO.getImm());
765 } else if (MO.isFPImm()) {
766 return static_cast<unsigned>(APFloat(MO.getFPImm())
767 .bitcastToAPInt().getHiBits(32).getLimitedValue());
768 }
769 // MO must be an Expr.
770 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000771 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000772}
773
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000774/// Return binary encoding of memory related operand.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000775/// If the offset operand requires relocation, record the relocation.
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000776template <unsigned ShiftAmount>
777unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
778 SmallVectorImpl<MCFixup> &Fixups,
779 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000780 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
781 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000782 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
783 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000784
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000785 // Apply the scale factor if there is one.
786 OffBits >>= ShiftAmount;
787
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000788 return (OffBits & 0xFFFF) | RegBits;
789}
790
Jack Carter97700972013-08-13 20:19:16 +0000791unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000792getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
793 SmallVectorImpl<MCFixup> &Fixups,
794 const MCSubtargetInfo &STI) const {
795 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
796 assert(MI.getOperand(OpNo).isReg());
797 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
798 Fixups, STI) << 4;
799 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
800 Fixups, STI);
801
802 return (OffBits & 0xF) | RegBits;
803}
804
805unsigned MipsMCCodeEmitter::
806getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
807 SmallVectorImpl<MCFixup> &Fixups,
808 const MCSubtargetInfo &STI) const {
809 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
810 assert(MI.getOperand(OpNo).isReg());
811 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
812 Fixups, STI) << 4;
813 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
814 Fixups, STI) >> 1;
815
816 return (OffBits & 0xF) | RegBits;
817}
818
819unsigned MipsMCCodeEmitter::
820getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
821 SmallVectorImpl<MCFixup> &Fixups,
822 const MCSubtargetInfo &STI) const {
823 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
824 assert(MI.getOperand(OpNo).isReg());
825 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
826 Fixups, STI) << 4;
827 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
828 Fixups, STI) >> 2;
829
830 return (OffBits & 0xF) | RegBits;
831}
832
833unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000834getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
835 SmallVectorImpl<MCFixup> &Fixups,
836 const MCSubtargetInfo &STI) const {
837 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
838 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000839 (MI.getOperand(OpNo).getReg() == Mips::SP ||
840 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000841 "Unexpected base register!");
842 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
843 Fixups, STI) >> 2;
844
845 return OffBits & 0x1F;
846}
847
848unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000849getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
850 SmallVectorImpl<MCFixup> &Fixups,
851 const MCSubtargetInfo &STI) const {
852 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
853 assert(MI.getOperand(OpNo).isReg() &&
854 MI.getOperand(OpNo).getReg() == Mips::GP &&
855 "Unexpected base register!");
856
857 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
858 Fixups, STI) >> 2;
859
860 return OffBits & 0x7F;
861}
862
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000863unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000864getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
865 SmallVectorImpl<MCFixup> &Fixups,
866 const MCSubtargetInfo &STI) const {
867 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
868 assert(MI.getOperand(OpNo).isReg());
869 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
870 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000871 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000872
873 return (OffBits & 0x1FF) | RegBits;
874}
875
Jozef Koleke10a02e2015-01-28 17:27:26 +0000876unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000877getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000878 SmallVectorImpl<MCFixup> &Fixups,
879 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000880 // opNum can be invalid if instruction had reglist as operand.
881 // MemOperand is always last operand of instruction (base + offset).
882 switch (MI.getOpcode()) {
883 default:
884 break;
885 case Mips::SWM32_MM:
886 case Mips::LWM32_MM:
887 OpNo = MI.getNumOperands() - 2;
888 break;
889 }
890
Jack Carter97700972013-08-13 20:19:16 +0000891 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
892 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000893 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
894 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000895
896 return (OffBits & 0x0FFF) | RegBits;
897}
898
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000899unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000900getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
901 SmallVectorImpl<MCFixup> &Fixups,
902 const MCSubtargetInfo &STI) const {
903 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
904 assert(MI.getOperand(OpNo).isReg());
905 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
906 STI) << 16;
907 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
908
909 return (OffBits & 0xFFFF) | RegBits;
910}
911
912unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000913getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
914 SmallVectorImpl<MCFixup> &Fixups,
915 const MCSubtargetInfo &STI) const {
916 // opNum can be invalid if instruction had reglist as operand
917 // MemOperand is always last operand of instruction (base + offset)
918 switch (MI.getOpcode()) {
919 default:
920 break;
921 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000922 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000923 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000924 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000925 OpNo = MI.getNumOperands() - 2;
926 break;
927 }
928
929 // Offset is encoded in bits 4-0.
930 assert(MI.getOperand(OpNo).isReg());
931 // Base register is always SP - thus it is not encoded.
932 assert(MI.getOperand(OpNo+1).isImm());
933 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
934
935 return ((OffBits >> 2) & 0x0F);
936}
937
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000938// FIXME: should be called getMSBEncoding
939//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000940unsigned
941MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000942 SmallVectorImpl<MCFixup> &Fixups,
943 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000944 assert(MI.getOperand(OpNo-1).isImm());
945 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000946 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
947 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000948
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000949 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000950}
951
Daniel Sandersea4f6532015-11-06 12:22:31 +0000952template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000953unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000954MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
955 SmallVectorImpl<MCFixup> &Fixups,
956 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000957 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000958 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
959 Value -= Offset;
960 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000961}
962
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000963unsigned
964MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
965 SmallVectorImpl<MCFixup> &Fixups,
966 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000967 const MCOperand &MO = MI.getOperand(OpNo);
968 if (MO.isImm()) {
969 // The immediate is encoded as 'immediate << 2'.
970 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
971 assert((Res & 3) == 0);
972 return Res >> 2;
973 }
974
975 assert(MO.isExpr() &&
976 "getSimm19Lsl2Encoding expects only expressions or an immediate");
977
978 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000979 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
980 : Mips::fixup_MIPS_PC19_S2;
981 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000982 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000983}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000984
Zoran Jovanovic28551422014-06-09 09:49:51 +0000985unsigned
986MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
987 SmallVectorImpl<MCFixup> &Fixups,
988 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000989 const MCOperand &MO = MI.getOperand(OpNo);
990 if (MO.isImm()) {
991 // The immediate is encoded as 'immediate << 3'.
992 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
993 assert((Res & 7) == 0);
994 return Res >> 3;
995 }
996
997 assert(MO.isExpr() &&
998 "getSimm18Lsl2Encoding expects only expressions or an immediate");
999
1000 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic8e366822016-04-22 10:15:12 +00001001 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
1002 : Mips::fixup_MIPS_PC18_S3;
1003 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +00001004 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +00001005}
1006
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +00001007unsigned
1008MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
1009 SmallVectorImpl<MCFixup> &Fixups,
1010 const MCSubtargetInfo &STI) const {
1011 assert(MI.getOperand(OpNo).isImm());
1012 const MCOperand &MO = MI.getOperand(OpNo);
1013 return MO.getImm() % 8;
1014}
1015
Zoran Jovanovic88531712014-11-05 17:31:00 +00001016unsigned
1017MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
1018 SmallVectorImpl<MCFixup> &Fixups,
1019 const MCSubtargetInfo &STI) const {
1020 assert(MI.getOperand(OpNo).isImm());
1021 const MCOperand &MO = MI.getOperand(OpNo);
1022 unsigned Value = MO.getImm();
1023 switch (Value) {
1024 case 128: return 0x0;
1025 case 1: return 0x1;
1026 case 2: return 0x2;
1027 case 3: return 0x3;
1028 case 4: return 0x4;
1029 case 7: return 0x5;
1030 case 8: return 0x6;
1031 case 15: return 0x7;
1032 case 16: return 0x8;
1033 case 31: return 0x9;
1034 case 32: return 0xa;
1035 case 63: return 0xb;
1036 case 64: return 0xc;
1037 case 255: return 0xd;
1038 case 32768: return 0xe;
1039 case 65535: return 0xf;
1040 }
1041 llvm_unreachable("Unexpected value");
1042}
1043
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001044unsigned
1045MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
1046 SmallVectorImpl<MCFixup> &Fixups,
1047 const MCSubtargetInfo &STI) const {
1048 unsigned res = 0;
1049
1050 // Register list operand is always first operand of instruction and it is
1051 // placed before memory operand (register + imm).
1052
1053 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
1054 unsigned Reg = MI.getOperand(I).getReg();
1055 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
1056 if (RegNo != 31)
1057 res++;
1058 else
1059 res |= 0x10;
1060 }
1061 return res;
1062}
1063
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001064unsigned
1065MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
1066 SmallVectorImpl<MCFixup> &Fixups,
1067 const MCSubtargetInfo &STI) const {
1068 return (MI.getNumOperands() - 4);
1069}
1070
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001071unsigned
1072MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
1073 SmallVectorImpl<MCFixup> &Fixups,
1074 const MCSubtargetInfo &STI) const {
1075 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1076}
1077
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001078unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +00001079MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1080 SmallVectorImpl<MCFixup> &Fixups,
1081 const MCSubtargetInfo &STI) const {
1082 unsigned res = 0;
1083
1084 if (MI.getOperand(0).getReg() == Mips::A1 &&
1085 MI.getOperand(1).getReg() == Mips::A2)
1086 res = 0;
1087 else if (MI.getOperand(0).getReg() == Mips::A1 &&
1088 MI.getOperand(1).getReg() == Mips::A3)
1089 res = 1;
1090 else if (MI.getOperand(0).getReg() == Mips::A2 &&
1091 MI.getOperand(1).getReg() == Mips::A3)
1092 res = 2;
1093 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1094 MI.getOperand(1).getReg() == Mips::S5)
1095 res = 3;
1096 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1097 MI.getOperand(1).getReg() == Mips::S6)
1098 res = 4;
1099 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1100 MI.getOperand(1).getReg() == Mips::A1)
1101 res = 5;
1102 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1103 MI.getOperand(1).getReg() == Mips::A2)
1104 res = 6;
1105 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1106 MI.getOperand(1).getReg() == Mips::A3)
1107 res = 7;
1108
1109 return res;
1110}
1111
1112unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001113MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1114 SmallVectorImpl<MCFixup> &Fixups,
1115 const MCSubtargetInfo &STI) const {
1116 const MCOperand &MO = MI.getOperand(OpNo);
1117 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1118 // The immediate is encoded as 'immediate >> 2'.
1119 unsigned Res = static_cast<unsigned>(MO.getImm());
1120 assert((Res & 3) == 0);
1121 return Res >> 2;
1122}
1123
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001124#include "MipsGenMCCodeEmitter.inc"