blob: 61b53788bf394df45170a2d360b560fc9a522bd2 [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;
Hrvoje Varga24b975d2016-06-27 08:23:28 +000084 case Mips::DSRL_MM64R6:
85 Inst.setOpcode(Mips::DSRL32_MM64R6);
86 return;
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +000087 case Mips::DSRA_MM64R6:
88 Inst.setOpcode(Mips::DSRA32_MM64R6);
89 return;
90 case Mips::DROTR_MM64R6:
91 Inst.setOpcode(Mips::DROTR32_MM64R6);
92 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000093 }
94}
95
Daniel Sanders611eb822016-02-29 15:26:54 +000096// Pick a DINS instruction variant based on the pos and size operands
97static void LowerDins(MCInst& InstIn) {
98 assert(InstIn.getNumOperands() == 5 &&
99 "Invalid no. of machine operands for DINS!");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000100
101 assert(InstIn.getOperand(2).isImm());
102 int64_t pos = InstIn.getOperand(2).getImm();
103 assert(InstIn.getOperand(3).isImm());
104 int64_t size = InstIn.getOperand(3).getImm();
105
106 if (size <= 32) {
Daniel Sanders611eb822016-02-29 15:26:54 +0000107 if (pos < 32) // DINS, do nothing
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000108 return;
Daniel Sanders611eb822016-02-29 15:26:54 +0000109 // DINSU
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000110 InstIn.getOperand(2).setImm(pos - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +0000111 InstIn.setOpcode(Mips::DINSU);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000112 return;
113 }
Daniel Sanders611eb822016-02-29 15:26:54 +0000114 // DINSM
115 assert(pos < 32 && "DINS cannot have both size and pos > 32");
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000116 InstIn.getOperand(3).setImm(size - 32);
Daniel Sanders611eb822016-02-29 15:26:54 +0000117 InstIn.setOpcode(Mips::DINSM);
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000118 return;
119}
120
Simon Dardis669d8dd2016-05-18 10:38:01 +0000121// Fix a bad compact branch encoding for beqc/bnec.
122void MipsMCCodeEmitter::LowerCompactBranch(MCInst& Inst) const {
123
124 // Encoding may be illegal !(rs < rt), but this situation is
125 // easily fixed.
126 unsigned RegOp0 = Inst.getOperand(0).getReg();
127 unsigned RegOp1 = Inst.getOperand(1).getReg();
128
129 unsigned Reg0 = Ctx.getRegisterInfo()->getEncodingValue(RegOp0);
130 unsigned Reg1 = Ctx.getRegisterInfo()->getEncodingValue(RegOp1);
131
Simon Dardisb60833c2016-05-31 17:34:42 +0000132 if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC) {
133 assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
134 if (Reg0 < Reg1)
135 return;
136 } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
137 if (Reg0 >= Reg1)
138 return;
Hrvoje Vargac962c492016-06-09 12:57:23 +0000139 } else if (Inst.getOpcode() == Mips::BNVC_MMR6 ||
140 Inst.getOpcode() == Mips::BOVC_MMR6) {
141 if (Reg1 >= Reg0)
142 return;
Simon Dardisb60833c2016-05-31 17:34:42 +0000143 } else
144 llvm_unreachable("Cannot rewrite unknown branch!");
Simon Dardis669d8dd2016-05-18 10:38:01 +0000145
146 Inst.getOperand(0).setReg(RegOp1);
147 Inst.getOperand(1).setReg(RegOp0);
148
149}
150
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000151bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000152 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000153}
154
Jozef Kolekc22555d2015-04-20 12:23:06 +0000155bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000156 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000157}
158
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000159void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
160 OS << (char)C;
161}
162
163void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
164 const MCSubtargetInfo &STI,
165 raw_ostream &OS) const {
166 // Output the instruction encoding in little endian byte order.
167 // Little-endian byte ordering:
168 // mips32r2: 4 | 3 | 2 | 1
169 // microMIPS: 2 | 1 | 4 | 3
170 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
171 EmitInstruction(Val >> 16, 2, STI, OS);
172 EmitInstruction(Val, 2, STI, OS);
173 } else {
174 for (unsigned i = 0; i < Size; ++i) {
175 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
176 EmitByte((Val >> Shift) & 0xff, OS);
177 }
178 }
179}
180
Jim Grosbach91df21f2015-05-15 19:13:16 +0000181/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000182/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000183void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000184encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000185 SmallVectorImpl<MCFixup> &Fixups,
186 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000187{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000188
189 // Non-pseudo instructions that get changed for direct object
190 // only based on operand values.
191 // If this list of instructions get much longer we will move
192 // the check to a function call. Until then, this is more efficient.
193 MCInst TmpInst = MI;
194 switch (MI.getOpcode()) {
195 // If shift amount is >= 32 it the inst needs to be lowered further
196 case Mips::DSLL:
197 case Mips::DSRL:
198 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000199 case Mips::DROTR:
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +0000200 case Mips::DSLL_MM64R6:
Hrvoje Varga24b975d2016-06-27 08:23:28 +0000201 case Mips::DSRL_MM64R6:
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +0000202 case Mips::DSRA_MM64R6:
203 case Mips::DROTR_MM64R6:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000204 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000205 break;
206 // Double extract instruction is chosen by pos and size operands
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000207 case Mips::DINS:
Daniel Sanders611eb822016-02-29 15:26:54 +0000208 LowerDins(TmpInst);
Simon Dardis669d8dd2016-05-18 10:38:01 +0000209 break;
Simon Dardisb60833c2016-05-31 17:34:42 +0000210 // Compact branches, enforce encoding restrictions.
Simon Dardis669d8dd2016-05-18 10:38:01 +0000211 case Mips::BEQC:
212 case Mips::BNEC:
Simon Dardisb60833c2016-05-31 17:34:42 +0000213 case Mips::BOVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000214 case Mips::BOVC_MMR6:
Simon Dardisb60833c2016-05-31 17:34:42 +0000215 case Mips::BNVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000216 case Mips::BNVC_MMR6:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000217 LowerCompactBranch(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000218 }
219
Jack Carter97700972013-08-13 20:19:16 +0000220 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000221 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000222
223 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000224 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000225 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000226 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000227 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
228 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000229 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000230
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000231 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000232 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000233 if (isMips32r6(STI)) {
234 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
235 if (NewOpcode == -1)
236 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
237 }
238 else
239 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
240
Zoran Jovanovic2e386d32015-10-12 16:07:25 +0000241 // Check whether it is Dsp instruction.
242 if (NewOpcode == -1)
243 NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
244
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000245 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000246 if (Fixups.size() > N)
247 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000248
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000249 Opcode = NewOpcode;
250 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000251 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000252 }
253 }
254
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000255 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000256
Jack Carter5b5559d2012-10-03 21:58:54 +0000257 // Get byte count of instruction
258 unsigned Size = Desc.getSize();
259 if (!Size)
260 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000261
David Woodhoused2cca112014-01-28 23:13:25 +0000262 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000263}
264
265/// getBranchTargetOpValue - Return binary encoding of the branch
266/// target operand. If the machine operand requires relocation,
267/// record the relocation and return zero.
268unsigned MipsMCCodeEmitter::
269getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000270 SmallVectorImpl<MCFixup> &Fixups,
271 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000272
273 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000274
Jack Carter4f69a0f2013-03-22 00:29:10 +0000275 // If the destination is an immediate, divide by 4.
276 if (MO.isImm()) return MO.getImm() >> 2;
277
Jack Carter71e6a742012-09-06 00:43:26 +0000278 assert(MO.isExpr() &&
279 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000280
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000281 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
282 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
283 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000284 MCFixupKind(Mips::fixup_Mips_PC16)));
285 return 0;
286}
287
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +0000288/// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
289/// target operand. If the machine operand requires relocation,
290/// record the relocation and return zero.
291unsigned MipsMCCodeEmitter::
292getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
293 SmallVectorImpl<MCFixup> &Fixups,
294 const MCSubtargetInfo &STI) const {
295
296 const MCOperand &MO = MI.getOperand(OpNo);
297
298 // If the destination is an immediate, divide by 2.
299 if (MO.isImm()) return MO.getImm() >> 1;
300
301 assert(MO.isExpr() &&
302 "getBranchTargetOpValue expects only expressions or immediates");
303
304 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
305 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
306 Fixups.push_back(MCFixup::create(0, FixupExpression,
307 MCFixupKind(Mips::fixup_Mips_PC16)));
308 return 0;
309}
310
Hrvoje Vargac962c492016-06-09 12:57:23 +0000311/// getBranchTargetOpValueMMR6 - Return binary encoding of the branch
312/// target operand. If the machine operand requires relocation,
313/// record the relocation and return zero.
314unsigned MipsMCCodeEmitter::
315getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
316 SmallVectorImpl<MCFixup> &Fixups,
317 const MCSubtargetInfo &STI) const {
318
319 const MCOperand &MO = MI.getOperand(OpNo);
320
321 // If the destination is an immediate, divide by 2.
322 if (MO.isImm())
323 return MO.getImm() >> 1;
324
325 assert(MO.isExpr() &&
326 "getBranchTargetOpValueMMR6 expects only expressions or immediates");
327
328 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
329 MO.getExpr(), MCConstantExpr::create(-2, Ctx), Ctx);
330 Fixups.push_back(MCFixup::create(0, FixupExpression,
331 MCFixupKind(Mips::fixup_Mips_PC16)));
332 return 0;
333}
334
Jozef Kolek9761e962015-01-12 12:03:34 +0000335/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
336/// target operand. If the machine operand requires relocation,
337/// record the relocation and return zero.
338unsigned MipsMCCodeEmitter::
339getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
340 SmallVectorImpl<MCFixup> &Fixups,
341 const MCSubtargetInfo &STI) const {
342
343 const MCOperand &MO = MI.getOperand(OpNo);
344
345 // If the destination is an immediate, divide by 2.
346 if (MO.isImm()) return MO.getImm() >> 1;
347
348 assert(MO.isExpr() &&
349 "getBranchTargetOpValueMM expects only expressions or immediates");
350
351 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000352 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000353 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
354 return 0;
355}
356
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000357/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
358/// 10-bit branch target operand. If the machine operand requires relocation,
359/// record the relocation and return zero.
360unsigned MipsMCCodeEmitter::
361getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
362 SmallVectorImpl<MCFixup> &Fixups,
363 const MCSubtargetInfo &STI) const {
364
365 const MCOperand &MO = MI.getOperand(OpNo);
366
367 // If the destination is an immediate, divide by 2.
368 if (MO.isImm()) return MO.getImm() >> 1;
369
370 assert(MO.isExpr() &&
371 "getBranchTargetOpValuePC10 expects only expressions or immediates");
372
373 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000374 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000375 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
376 return 0;
377}
378
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000379/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
380/// target operand. If the machine operand requires relocation,
381/// record the relocation and return zero.
382unsigned MipsMCCodeEmitter::
383getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000384 SmallVectorImpl<MCFixup> &Fixups,
385 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000386
387 const MCOperand &MO = MI.getOperand(OpNo);
388
389 // If the destination is an immediate, divide by 2.
390 if (MO.isImm()) return MO.getImm() >> 1;
391
392 assert(MO.isExpr() &&
393 "getBranchTargetOpValueMM expects only expressions or immediates");
394
395 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000396 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000397 MCFixupKind(Mips::
398 fixup_MICROMIPS_PC16_S1)));
399 return 0;
400}
401
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000402/// getBranchTarget21OpValue - Return binary encoding of the branch
403/// target operand. If the machine operand requires relocation,
404/// record the relocation and return zero.
405unsigned MipsMCCodeEmitter::
406getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
407 SmallVectorImpl<MCFixup> &Fixups,
408 const MCSubtargetInfo &STI) const {
409
410 const MCOperand &MO = MI.getOperand(OpNo);
411
412 // If the destination is an immediate, divide by 4.
413 if (MO.isImm()) return MO.getImm() >> 2;
414
415 assert(MO.isExpr() &&
416 "getBranchTarget21OpValue expects only expressions or immediates");
417
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000418 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
419 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
420 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000421 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000422 return 0;
423}
424
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000425/// getBranchTarget21OpValueMM - Return binary encoding of the branch
426/// target operand for microMIPS. If the machine operand requires
427/// relocation, record the relocation and return zero.
428unsigned MipsMCCodeEmitter::
429getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
430 SmallVectorImpl<MCFixup> &Fixups,
431 const MCSubtargetInfo &STI) const {
432
433 const MCOperand &MO = MI.getOperand(OpNo);
434
435 // If the destination is an immediate, divide by 2.
436 if (MO.isImm()) return MO.getImm() >> 1;
437
438 assert(MO.isExpr() &&
439 "getBranchTarget21OpValueMM expects only expressions or immediates");
440
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000441 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
442 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
443 Fixups.push_back(MCFixup::create(0, FixupExpression,
444 MCFixupKind(Mips::fixup_MICROMIPS_PC21_S1)));
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000445 return 0;
446}
447
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000448/// getBranchTarget26OpValue - Return binary encoding of the branch
449/// target operand. If the machine operand requires relocation,
450/// record the relocation and return zero.
451unsigned MipsMCCodeEmitter::
452getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
453 SmallVectorImpl<MCFixup> &Fixups,
454 const MCSubtargetInfo &STI) const {
455
456 const MCOperand &MO = MI.getOperand(OpNo);
457
458 // If the destination is an immediate, divide by 4.
459 if (MO.isImm()) return MO.getImm() >> 2;
460
461 assert(MO.isExpr() &&
462 "getBranchTarget26OpValue expects only expressions or immediates");
463
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000464 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
465 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
466 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000467 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000468 return 0;
469}
470
Zoran Jovanovica887b362015-11-30 12:56:18 +0000471/// getBranchTarget26OpValueMM - Return binary encoding of the branch
472/// target operand. If the machine operand requires relocation,
473/// record the relocation and return zero.
474unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
475 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
476 const MCSubtargetInfo &STI) const {
477
478 const MCOperand &MO = MI.getOperand(OpNo);
479
480 // If the destination is an immediate, divide by 2.
481 if (MO.isImm())
482 return MO.getImm() >> 1;
483
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000484 assert(MO.isExpr() &&
485 "getBranchTarget26OpValueMM expects only expressions or immediates");
486
487 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
488 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
489 Fixups.push_back(MCFixup::create(0, FixupExpression,
490 MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
Zoran Jovanovica887b362015-11-30 12:56:18 +0000491 return 0;
492}
493
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000494/// getJumpOffset16OpValue - Return binary encoding of the jump
495/// target operand. If the machine operand requires relocation,
496/// record the relocation and return zero.
497unsigned MipsMCCodeEmitter::
498getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
499 SmallVectorImpl<MCFixup> &Fixups,
500 const MCSubtargetInfo &STI) const {
501
502 const MCOperand &MO = MI.getOperand(OpNo);
503
504 if (MO.isImm()) return MO.getImm();
505
506 assert(MO.isExpr() &&
507 "getJumpOffset16OpValue expects only expressions or an immediate");
508
509 // TODO: Push fixup.
510 return 0;
511}
512
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000513/// getJumpTargetOpValue - Return binary encoding of the jump
514/// target operand. If the machine operand requires relocation,
515/// record the relocation and return zero.
516unsigned MipsMCCodeEmitter::
517getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000518 SmallVectorImpl<MCFixup> &Fixups,
519 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000520
521 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000522 // If the destination is an immediate, divide by 4.
523 if (MO.isImm()) return MO.getImm()>>2;
524
Jack Carter71e6a742012-09-06 00:43:26 +0000525 assert(MO.isExpr() &&
526 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000527
528 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000529 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000530 MCFixupKind(Mips::fixup_Mips_26)));
531 return 0;
532}
533
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000534unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000535getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000536 SmallVectorImpl<MCFixup> &Fixups,
537 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000538
539 const MCOperand &MO = MI.getOperand(OpNo);
540 // If the destination is an immediate, divide by 2.
541 if (MO.isImm()) return MO.getImm() >> 1;
542
543 assert(MO.isExpr() &&
544 "getJumpTargetOpValueMM expects only expressions or an immediate");
545
546 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000547 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000548 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
549 return 0;
550}
551
552unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000553getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
554 SmallVectorImpl<MCFixup> &Fixups,
555 const MCSubtargetInfo &STI) const {
556
557 const MCOperand &MO = MI.getOperand(OpNo);
558 if (MO.isImm()) {
559 // The immediate is encoded as 'immediate << 2'.
560 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
561 assert((Res & 3) == 0);
562 return Res >> 2;
563 }
564
565 assert(MO.isExpr() &&
566 "getUImm5Lsl2Encoding expects only expressions or an immediate");
567
568 return 0;
569}
570
571unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000572getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
573 SmallVectorImpl<MCFixup> &Fixups,
574 const MCSubtargetInfo &STI) const {
575
576 const MCOperand &MO = MI.getOperand(OpNo);
577 if (MO.isImm()) {
578 int Value = MO.getImm();
579 return Value >> 2;
580 }
581
582 return 0;
583}
584
585unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000586getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
587 SmallVectorImpl<MCFixup> &Fixups,
588 const MCSubtargetInfo &STI) const {
589
590 const MCOperand &MO = MI.getOperand(OpNo);
591 if (MO.isImm()) {
592 unsigned Value = MO.getImm();
593 return Value >> 2;
594 }
595
596 return 0;
597}
598
599unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000600getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
601 SmallVectorImpl<MCFixup> &Fixups,
602 const MCSubtargetInfo &STI) const {
603
604 const MCOperand &MO = MI.getOperand(OpNo);
605 if (MO.isImm()) {
606 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
607 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
608 }
609
610 return 0;
611}
612
613unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000614getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000615 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000616 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000617
Jim Grosbach13760bd2015-05-30 01:25:56 +0000618 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000619 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000620
Akira Hatanakafe384a22012-03-27 02:33:05 +0000621 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000622 if (Kind == MCExpr::Constant) {
623 return cast<MCConstantExpr>(Expr)->getValue();
624 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000625
Akira Hatanakafe384a22012-03-27 02:33:05 +0000626 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000627 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
628 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000629 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000630 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000631
632 if (Kind == MCExpr::Target) {
633 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
634
635 Mips::Fixups FixupKind = Mips::Fixups(0);
636 switch (MipsExpr->getKind()) {
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000637 case MipsMCExpr::MEK_NEG:
638 case MipsMCExpr::MEK_None:
639 case MipsMCExpr::MEK_Special:
640 llvm_unreachable("Unhandled fixup kind!");
641 break;
642 case MipsMCExpr::MEK_CALL_HI16:
643 FixupKind = Mips::fixup_Mips_CALL_HI16;
644 break;
645 case MipsMCExpr::MEK_CALL_LO16:
646 FixupKind = Mips::fixup_Mips_CALL_LO16;
647 break;
648 case MipsMCExpr::MEK_DTPREL_HI:
649 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
650 : Mips::fixup_Mips_DTPREL_HI;
651 break;
652 case MipsMCExpr::MEK_DTPREL_LO:
653 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
654 : Mips::fixup_Mips_DTPREL_LO;
655 break;
656 case MipsMCExpr::MEK_GOTTPREL:
657 FixupKind = Mips::fixup_Mips_GOTTPREL;
658 break;
659 case MipsMCExpr::MEK_GOT:
660 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
661 : Mips::fixup_Mips_GOT;
662 break;
663 case MipsMCExpr::MEK_GOT_CALL:
664 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
665 : Mips::fixup_Mips_CALL16;
666 break;
667 case MipsMCExpr::MEK_GOT_DISP:
668 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
669 : Mips::fixup_Mips_GOT_DISP;
670 break;
671 case MipsMCExpr::MEK_GOT_HI16:
672 FixupKind = Mips::fixup_Mips_GOT_HI16;
673 break;
674 case MipsMCExpr::MEK_GOT_LO16:
675 FixupKind = Mips::fixup_Mips_GOT_LO16;
676 break;
677 case MipsMCExpr::MEK_GOT_PAGE:
678 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
679 : Mips::fixup_Mips_GOT_PAGE;
680 break;
681 case MipsMCExpr::MEK_GOT_OFST:
682 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
683 : Mips::fixup_Mips_GOT_OFST;
684 break;
685 case MipsMCExpr::MEK_GPREL:
686 FixupKind = Mips::fixup_Mips_GPREL16;
687 break;
688 case MipsMCExpr::MEK_LO: {
689 // Check for %lo(%neg(%gp_rel(X)))
690 if (MipsExpr->isGpOff()) {
691 FixupKind = Mips::fixup_Mips_GPOFF_LO;
692 break;
693 }
694 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
695 : Mips::fixup_Mips_LO16;
696 break;
697 }
698 case MipsMCExpr::MEK_HIGHEST:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000699 FixupKind = Mips::fixup_Mips_HIGHEST;
700 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000701 case MipsMCExpr::MEK_HIGHER:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000702 FixupKind = Mips::fixup_Mips_HIGHER;
703 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000704 case MipsMCExpr::MEK_HI:
705 // Check for %hi(%neg(%gp_rel(X)))
706 if (MipsExpr->isGpOff()) {
707 FixupKind = Mips::fixup_Mips_GPOFF_HI;
708 break;
709 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000710 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
711 : Mips::fixup_Mips_HI16;
712 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000713 case MipsMCExpr::MEK_PCREL_HI16:
714 FixupKind = Mips::fixup_MIPS_PCHI16;
715 break;
716 case MipsMCExpr::MEK_PCREL_LO16:
717 FixupKind = Mips::fixup_MIPS_PCLO16;
718 break;
719 case MipsMCExpr::MEK_TLSGD:
720 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
721 : Mips::fixup_Mips_TLSGD;
722 break;
723 case MipsMCExpr::MEK_TLSLDM:
724 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
725 : Mips::fixup_Mips_TLSLDM;
726 break;
727 case MipsMCExpr::MEK_TPREL_HI:
728 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
729 : Mips::fixup_Mips_TPREL_HI;
730 break;
731 case MipsMCExpr::MEK_TPREL_LO:
732 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
733 : Mips::fixup_Mips_TPREL_LO;
Petar Jovanovica5da5882014-02-04 18:41:57 +0000734 break;
735 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000736 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000737 return 0;
738 }
739
Jack Carterb5cf5902013-04-17 00:18:04 +0000740 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000741 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000742
Mark Seabornc3bd1772013-12-31 13:05:15 +0000743 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
744 default: llvm_unreachable("Unknown fixup kind!");
745 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000746 case MCSymbolRefExpr::VK_None:
747 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
748 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000749 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000750
Jim Grosbach63661f82015-05-15 19:13:05 +0000751 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000752 return 0;
753 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000754 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000755}
756
Jack Carterb5cf5902013-04-17 00:18:04 +0000757/// getMachineOpValue - Return binary encoding of operand. If the machine
758/// operand requires relocation, record the relocation and return zero.
759unsigned MipsMCCodeEmitter::
760getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000761 SmallVectorImpl<MCFixup> &Fixups,
762 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000763 if (MO.isReg()) {
764 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000765 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000766 return RegNo;
767 } else if (MO.isImm()) {
768 return static_cast<unsigned>(MO.getImm());
769 } else if (MO.isFPImm()) {
770 return static_cast<unsigned>(APFloat(MO.getFPImm())
771 .bitcastToAPInt().getHiBits(32).getLimitedValue());
772 }
773 // MO must be an Expr.
774 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000775 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000776}
777
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000778/// Return binary encoding of memory related operand.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000779/// If the offset operand requires relocation, record the relocation.
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000780template <unsigned ShiftAmount>
781unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
782 SmallVectorImpl<MCFixup> &Fixups,
783 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000784 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
785 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000786 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
787 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000788
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000789 // Apply the scale factor if there is one.
790 OffBits >>= ShiftAmount;
791
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000792 return (OffBits & 0xFFFF) | RegBits;
793}
794
Jack Carter97700972013-08-13 20:19:16 +0000795unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000796getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
797 SmallVectorImpl<MCFixup> &Fixups,
798 const MCSubtargetInfo &STI) const {
799 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
800 assert(MI.getOperand(OpNo).isReg());
801 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
802 Fixups, STI) << 4;
803 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
804 Fixups, STI);
805
806 return (OffBits & 0xF) | RegBits;
807}
808
809unsigned MipsMCCodeEmitter::
810getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
811 SmallVectorImpl<MCFixup> &Fixups,
812 const MCSubtargetInfo &STI) const {
813 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
814 assert(MI.getOperand(OpNo).isReg());
815 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
816 Fixups, STI) << 4;
817 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
818 Fixups, STI) >> 1;
819
820 return (OffBits & 0xF) | RegBits;
821}
822
823unsigned MipsMCCodeEmitter::
824getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
825 SmallVectorImpl<MCFixup> &Fixups,
826 const MCSubtargetInfo &STI) const {
827 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
828 assert(MI.getOperand(OpNo).isReg());
829 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
830 Fixups, STI) << 4;
831 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
832 Fixups, STI) >> 2;
833
834 return (OffBits & 0xF) | RegBits;
835}
836
837unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000838getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
839 SmallVectorImpl<MCFixup> &Fixups,
840 const MCSubtargetInfo &STI) const {
841 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
842 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000843 (MI.getOperand(OpNo).getReg() == Mips::SP ||
844 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000845 "Unexpected base register!");
846 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
847 Fixups, STI) >> 2;
848
849 return OffBits & 0x1F;
850}
851
852unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000853getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
854 SmallVectorImpl<MCFixup> &Fixups,
855 const MCSubtargetInfo &STI) const {
856 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
857 assert(MI.getOperand(OpNo).isReg() &&
858 MI.getOperand(OpNo).getReg() == Mips::GP &&
859 "Unexpected base register!");
860
861 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
862 Fixups, STI) >> 2;
863
864 return OffBits & 0x7F;
865}
866
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000867unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000868getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
869 SmallVectorImpl<MCFixup> &Fixups,
870 const MCSubtargetInfo &STI) const {
871 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
872 assert(MI.getOperand(OpNo).isReg());
873 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
874 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000875 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000876
877 return (OffBits & 0x1FF) | RegBits;
878}
879
Jozef Koleke10a02e2015-01-28 17:27:26 +0000880unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000881getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000882 SmallVectorImpl<MCFixup> &Fixups,
883 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000884 // opNum can be invalid if instruction had reglist as operand.
885 // MemOperand is always last operand of instruction (base + offset).
886 switch (MI.getOpcode()) {
887 default:
888 break;
889 case Mips::SWM32_MM:
890 case Mips::LWM32_MM:
891 OpNo = MI.getNumOperands() - 2;
892 break;
893 }
894
Jack Carter97700972013-08-13 20:19:16 +0000895 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
896 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000897 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
898 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000899
900 return (OffBits & 0x0FFF) | RegBits;
901}
902
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000903unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000904getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
905 SmallVectorImpl<MCFixup> &Fixups,
906 const MCSubtargetInfo &STI) const {
907 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
908 assert(MI.getOperand(OpNo).isReg());
909 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
910 STI) << 16;
911 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
912
913 return (OffBits & 0xFFFF) | RegBits;
914}
915
916unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000917getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
918 SmallVectorImpl<MCFixup> &Fixups,
919 const MCSubtargetInfo &STI) const {
920 // opNum can be invalid if instruction had reglist as operand
921 // MemOperand is always last operand of instruction (base + offset)
922 switch (MI.getOpcode()) {
923 default:
924 break;
925 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000926 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000927 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000928 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000929 OpNo = MI.getNumOperands() - 2;
930 break;
931 }
932
933 // Offset is encoded in bits 4-0.
934 assert(MI.getOperand(OpNo).isReg());
935 // Base register is always SP - thus it is not encoded.
936 assert(MI.getOperand(OpNo+1).isImm());
937 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
938
939 return ((OffBits >> 2) & 0x0F);
940}
941
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000942// FIXME: should be called getMSBEncoding
943//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000944unsigned
945MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000946 SmallVectorImpl<MCFixup> &Fixups,
947 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000948 assert(MI.getOperand(OpNo-1).isImm());
949 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000950 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
951 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000952
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000953 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000954}
955
Daniel Sandersea4f6532015-11-06 12:22:31 +0000956template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000957unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000958MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
959 SmallVectorImpl<MCFixup> &Fixups,
960 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000961 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000962 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
963 Value -= Offset;
964 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000965}
966
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000967unsigned
968MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
969 SmallVectorImpl<MCFixup> &Fixups,
970 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000971 const MCOperand &MO = MI.getOperand(OpNo);
972 if (MO.isImm()) {
973 // The immediate is encoded as 'immediate << 2'.
974 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
975 assert((Res & 3) == 0);
976 return Res >> 2;
977 }
978
979 assert(MO.isExpr() &&
980 "getSimm19Lsl2Encoding expects only expressions or an immediate");
981
982 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000983 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
984 : Mips::fixup_MIPS_PC19_S2;
985 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000986 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000987}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000988
Zoran Jovanovic28551422014-06-09 09:49:51 +0000989unsigned
990MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
991 SmallVectorImpl<MCFixup> &Fixups,
992 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000993 const MCOperand &MO = MI.getOperand(OpNo);
994 if (MO.isImm()) {
995 // The immediate is encoded as 'immediate << 3'.
996 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
997 assert((Res & 7) == 0);
998 return Res >> 3;
999 }
1000
1001 assert(MO.isExpr() &&
1002 "getSimm18Lsl2Encoding expects only expressions or an immediate");
1003
1004 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic8e366822016-04-22 10:15:12 +00001005 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
1006 : Mips::fixup_MIPS_PC18_S3;
1007 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +00001008 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +00001009}
1010
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +00001011unsigned
1012MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
1013 SmallVectorImpl<MCFixup> &Fixups,
1014 const MCSubtargetInfo &STI) const {
1015 assert(MI.getOperand(OpNo).isImm());
1016 const MCOperand &MO = MI.getOperand(OpNo);
1017 return MO.getImm() % 8;
1018}
1019
Zoran Jovanovic88531712014-11-05 17:31:00 +00001020unsigned
1021MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
1022 SmallVectorImpl<MCFixup> &Fixups,
1023 const MCSubtargetInfo &STI) const {
1024 assert(MI.getOperand(OpNo).isImm());
1025 const MCOperand &MO = MI.getOperand(OpNo);
1026 unsigned Value = MO.getImm();
1027 switch (Value) {
1028 case 128: return 0x0;
1029 case 1: return 0x1;
1030 case 2: return 0x2;
1031 case 3: return 0x3;
1032 case 4: return 0x4;
1033 case 7: return 0x5;
1034 case 8: return 0x6;
1035 case 15: return 0x7;
1036 case 16: return 0x8;
1037 case 31: return 0x9;
1038 case 32: return 0xa;
1039 case 63: return 0xb;
1040 case 64: return 0xc;
1041 case 255: return 0xd;
1042 case 32768: return 0xe;
1043 case 65535: return 0xf;
1044 }
1045 llvm_unreachable("Unexpected value");
1046}
1047
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001048unsigned
1049MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
1050 SmallVectorImpl<MCFixup> &Fixups,
1051 const MCSubtargetInfo &STI) const {
1052 unsigned res = 0;
1053
1054 // Register list operand is always first operand of instruction and it is
1055 // placed before memory operand (register + imm).
1056
1057 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
1058 unsigned Reg = MI.getOperand(I).getReg();
1059 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
1060 if (RegNo != 31)
1061 res++;
1062 else
1063 res |= 0x10;
1064 }
1065 return res;
1066}
1067
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001068unsigned
1069MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
1070 SmallVectorImpl<MCFixup> &Fixups,
1071 const MCSubtargetInfo &STI) const {
1072 return (MI.getNumOperands() - 4);
1073}
1074
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001075unsigned
1076MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
1077 SmallVectorImpl<MCFixup> &Fixups,
1078 const MCSubtargetInfo &STI) const {
1079 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1080}
1081
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001082unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +00001083MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1084 SmallVectorImpl<MCFixup> &Fixups,
1085 const MCSubtargetInfo &STI) const {
1086 unsigned res = 0;
1087
1088 if (MI.getOperand(0).getReg() == Mips::A1 &&
1089 MI.getOperand(1).getReg() == Mips::A2)
1090 res = 0;
1091 else if (MI.getOperand(0).getReg() == Mips::A1 &&
1092 MI.getOperand(1).getReg() == Mips::A3)
1093 res = 1;
1094 else if (MI.getOperand(0).getReg() == Mips::A2 &&
1095 MI.getOperand(1).getReg() == Mips::A3)
1096 res = 2;
1097 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1098 MI.getOperand(1).getReg() == Mips::S5)
1099 res = 3;
1100 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1101 MI.getOperand(1).getReg() == Mips::S6)
1102 res = 4;
1103 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1104 MI.getOperand(1).getReg() == Mips::A1)
1105 res = 5;
1106 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1107 MI.getOperand(1).getReg() == Mips::A2)
1108 res = 6;
1109 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1110 MI.getOperand(1).getReg() == Mips::A3)
1111 res = 7;
1112
1113 return res;
1114}
1115
1116unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001117MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1118 SmallVectorImpl<MCFixup> &Fixups,
1119 const MCSubtargetInfo &STI) const {
1120 const MCOperand &MO = MI.getOperand(OpNo);
1121 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1122 // The immediate is encoded as 'immediate >> 2'.
1123 unsigned Res = static_cast<unsigned>(MO.getImm());
1124 assert((Res & 3) == 0);
1125 return Res >> 2;
1126}
1127
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001128#include "MipsGenMCCodeEmitter.inc"