blob: 4cc55f86a993ea7fb7cfa4f093c65bcc3a4ae076 [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 Dardis68a204d2016-07-26 10:25:07 +0000132 if (Inst.getOpcode() == Mips::BNEC || Inst.getOpcode() == Mips::BEQC ||
133 Inst.getOpcode() == Mips::BNEC64 || Inst.getOpcode() == Mips::BEQC64) {
Simon Dardisb60833c2016-05-31 17:34:42 +0000134 assert(Reg0 != Reg1 && "Instruction has bad operands ($rs == $rt)!");
135 if (Reg0 < Reg1)
136 return;
137 } else if (Inst.getOpcode() == Mips::BNVC || Inst.getOpcode() == Mips::BOVC) {
138 if (Reg0 >= Reg1)
139 return;
Hrvoje Vargac962c492016-06-09 12:57:23 +0000140 } else if (Inst.getOpcode() == Mips::BNVC_MMR6 ||
141 Inst.getOpcode() == Mips::BOVC_MMR6) {
142 if (Reg1 >= Reg0)
143 return;
Simon Dardisb60833c2016-05-31 17:34:42 +0000144 } else
Simon Dardis68a204d2016-07-26 10:25:07 +0000145 llvm_unreachable("Cannot rewrite unknown branch!");
Simon Dardis669d8dd2016-05-18 10:38:01 +0000146
147 Inst.getOperand(0).setReg(RegOp1);
148 Inst.getOperand(1).setReg(RegOp0);
149
150}
151
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000152bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000153 return STI.getFeatureBits()[Mips::FeatureMicroMips];
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000154}
155
Jozef Kolekc22555d2015-04-20 12:23:06 +0000156bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const {
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000157 return STI.getFeatureBits()[Mips::FeatureMips32r6];
Jozef Kolekc22555d2015-04-20 12:23:06 +0000158}
159
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000160void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
161 OS << (char)C;
162}
163
164void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
165 const MCSubtargetInfo &STI,
166 raw_ostream &OS) const {
167 // Output the instruction encoding in little endian byte order.
168 // Little-endian byte ordering:
169 // mips32r2: 4 | 3 | 2 | 1
170 // microMIPS: 2 | 1 | 4 | 3
171 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
172 EmitInstruction(Val >> 16, 2, STI, OS);
173 EmitInstruction(Val, 2, STI, OS);
174 } else {
175 for (unsigned i = 0; i < Size; ++i) {
176 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
177 EmitByte((Val >> Shift) & 0xff, OS);
178 }
179 }
180}
181
Jim Grosbach91df21f2015-05-15 19:13:16 +0000182/// encodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000183/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000184void MipsMCCodeEmitter::
Jim Grosbach91df21f2015-05-15 19:13:16 +0000185encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000186 SmallVectorImpl<MCFixup> &Fixups,
187 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000188{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000189
190 // Non-pseudo instructions that get changed for direct object
191 // only based on operand values.
192 // If this list of instructions get much longer we will move
193 // the check to a function call. Until then, this is more efficient.
194 MCInst TmpInst = MI;
195 switch (MI.getOpcode()) {
196 // If shift amount is >= 32 it the inst needs to be lowered further
197 case Mips::DSLL:
198 case Mips::DSRL:
199 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000200 case Mips::DROTR:
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +0000201 case Mips::DSLL_MM64R6:
Hrvoje Varga24b975d2016-06-27 08:23:28 +0000202 case Mips::DSRL_MM64R6:
Hrvoje Vargaf1e0a032016-06-16 07:06:25 +0000203 case Mips::DSRA_MM64R6:
204 case Mips::DROTR_MM64R6:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000205 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000206 break;
207 // Double extract instruction is chosen by pos and size operands
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000208 case Mips::DINS:
Daniel Sanders611eb822016-02-29 15:26:54 +0000209 LowerDins(TmpInst);
Simon Dardis669d8dd2016-05-18 10:38:01 +0000210 break;
Simon Dardisb60833c2016-05-31 17:34:42 +0000211 // Compact branches, enforce encoding restrictions.
Simon Dardis669d8dd2016-05-18 10:38:01 +0000212 case Mips::BEQC:
213 case Mips::BNEC:
Simon Dardis68a204d2016-07-26 10:25:07 +0000214 case Mips::BEQC64:
215 case Mips::BNEC64:
Simon Dardisb60833c2016-05-31 17:34:42 +0000216 case Mips::BOVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000217 case Mips::BOVC_MMR6:
Simon Dardisb60833c2016-05-31 17:34:42 +0000218 case Mips::BNVC:
Hrvoje Vargac962c492016-06-09 12:57:23 +0000219 case Mips::BNVC_MMR6:
Simon Dardis669d8dd2016-05-18 10:38:01 +0000220 LowerCompactBranch(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000221 }
222
Jack Carter97700972013-08-13 20:19:16 +0000223 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000224 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000225
226 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000227 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000228 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000229 unsigned Opcode = TmpInst.getOpcode();
Jozef Kolekc7e220f2014-11-29 13:29:24 +0000230 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) &&
231 (Opcode != Mips::SLL_MM) && !Binary)
Jim Grosbach91df21f2015-05-15 19:13:16 +0000232 llvm_unreachable("unimplemented opcode in encodeInstruction()");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000233
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000234 int NewOpcode = -1;
Jozef Kolek6ca13ea2015-04-20 12:42:08 +0000235 if (isMicroMips(STI)) {
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000236 if (isMips32r6(STI)) {
237 NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
238 if (NewOpcode == -1)
239 NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6);
240 }
241 else
242 NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips);
243
Zoran Jovanovic2e386d32015-10-12 16:07:25 +0000244 // Check whether it is Dsp instruction.
245 if (NewOpcode == -1)
246 NewOpcode = Mips::Dsp2MicroMips(Opcode, Mips::Arch_mmdsp);
247
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000248 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000249 if (Fixups.size() > N)
250 Fixups.pop_back();
Zoran Jovanovicb59a5412015-04-22 13:27:34 +0000251
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000252 Opcode = NewOpcode;
253 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000254 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000255 }
256 }
257
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000258 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000259
Jack Carter5b5559d2012-10-03 21:58:54 +0000260 // Get byte count of instruction
261 unsigned Size = Desc.getSize();
262 if (!Size)
263 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000264
David Woodhoused2cca112014-01-28 23:13:25 +0000265 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000266}
267
268/// getBranchTargetOpValue - Return binary encoding of the branch
269/// target operand. If the machine operand requires relocation,
270/// record the relocation and return zero.
271unsigned MipsMCCodeEmitter::
272getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000273 SmallVectorImpl<MCFixup> &Fixups,
274 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000275
276 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000277
Jack Carter4f69a0f2013-03-22 00:29:10 +0000278 // If the destination is an immediate, divide by 4.
279 if (MO.isImm()) return MO.getImm() >> 2;
280
Jack Carter71e6a742012-09-06 00:43:26 +0000281 assert(MO.isExpr() &&
282 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000283
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000284 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
285 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
286 Fixups.push_back(MCFixup::create(0, FixupExpression,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000287 MCFixupKind(Mips::fixup_Mips_PC16)));
288 return 0;
289}
290
Hrvoje Varga6f09cdf2016-05-13 11:32:53 +0000291/// getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
292/// target operand. If the machine operand requires relocation,
293/// record the relocation and return zero.
294unsigned MipsMCCodeEmitter::
295getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
296 SmallVectorImpl<MCFixup> &Fixups,
297 const MCSubtargetInfo &STI) const {
298
299 const MCOperand &MO = MI.getOperand(OpNo);
300
301 // If the destination is an immediate, divide by 2.
302 if (MO.isImm()) return MO.getImm() >> 1;
303
304 assert(MO.isExpr() &&
305 "getBranchTargetOpValue expects only expressions or immediates");
306
307 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
308 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
309 Fixups.push_back(MCFixup::create(0, FixupExpression,
310 MCFixupKind(Mips::fixup_Mips_PC16)));
311 return 0;
312}
313
Hrvoje Vargac962c492016-06-09 12:57:23 +0000314/// getBranchTargetOpValueMMR6 - Return binary encoding of the branch
315/// target operand. If the machine operand requires relocation,
316/// record the relocation and return zero.
317unsigned MipsMCCodeEmitter::
318getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
319 SmallVectorImpl<MCFixup> &Fixups,
320 const MCSubtargetInfo &STI) const {
321
322 const MCOperand &MO = MI.getOperand(OpNo);
323
324 // If the destination is an immediate, divide by 2.
325 if (MO.isImm())
326 return MO.getImm() >> 1;
327
328 assert(MO.isExpr() &&
329 "getBranchTargetOpValueMMR6 expects only expressions or immediates");
330
331 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
332 MO.getExpr(), MCConstantExpr::create(-2, Ctx), Ctx);
333 Fixups.push_back(MCFixup::create(0, FixupExpression,
334 MCFixupKind(Mips::fixup_Mips_PC16)));
335 return 0;
336}
337
Jozef Kolek9761e962015-01-12 12:03:34 +0000338/// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch
339/// target operand. If the machine operand requires relocation,
340/// record the relocation and return zero.
341unsigned MipsMCCodeEmitter::
342getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
343 SmallVectorImpl<MCFixup> &Fixups,
344 const MCSubtargetInfo &STI) const {
345
346 const MCOperand &MO = MI.getOperand(OpNo);
347
348 // If the destination is an immediate, divide by 2.
349 if (MO.isImm()) return MO.getImm() >> 1;
350
351 assert(MO.isExpr() &&
352 "getBranchTargetOpValueMM expects only expressions or immediates");
353
354 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000355 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek9761e962015-01-12 12:03:34 +0000356 MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1)));
357 return 0;
358}
359
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000360/// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
361/// 10-bit branch target operand. If the machine operand requires relocation,
362/// record the relocation and return zero.
363unsigned MipsMCCodeEmitter::
364getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
365 SmallVectorImpl<MCFixup> &Fixups,
366 const MCSubtargetInfo &STI) const {
367
368 const MCOperand &MO = MI.getOperand(OpNo);
369
370 // If the destination is an immediate, divide by 2.
371 if (MO.isImm()) return MO.getImm() >> 1;
372
373 assert(MO.isExpr() &&
374 "getBranchTargetOpValuePC10 expects only expressions or immediates");
375
376 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000377 Fixups.push_back(MCFixup::create(0, Expr,
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000378 MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1)));
379 return 0;
380}
381
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000382/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
383/// target operand. If the machine operand requires relocation,
384/// record the relocation and return zero.
385unsigned MipsMCCodeEmitter::
386getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000387 SmallVectorImpl<MCFixup> &Fixups,
388 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000389
390 const MCOperand &MO = MI.getOperand(OpNo);
391
392 // If the destination is an immediate, divide by 2.
393 if (MO.isImm()) return MO.getImm() >> 1;
394
395 assert(MO.isExpr() &&
396 "getBranchTargetOpValueMM expects only expressions or immediates");
397
398 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000399 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000400 MCFixupKind(Mips::
401 fixup_MICROMIPS_PC16_S1)));
402 return 0;
403}
404
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000405/// getBranchTarget21OpValue - Return binary encoding of the branch
406/// target operand. If the machine operand requires relocation,
407/// record the relocation and return zero.
408unsigned MipsMCCodeEmitter::
409getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
410 SmallVectorImpl<MCFixup> &Fixups,
411 const MCSubtargetInfo &STI) const {
412
413 const MCOperand &MO = MI.getOperand(OpNo);
414
415 // If the destination is an immediate, divide by 4.
416 if (MO.isImm()) return MO.getImm() >> 2;
417
418 assert(MO.isExpr() &&
419 "getBranchTarget21OpValue expects only expressions or immediates");
420
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000421 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
422 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
423 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000424 MCFixupKind(Mips::fixup_MIPS_PC21_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000425 return 0;
426}
427
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000428/// getBranchTarget21OpValueMM - Return binary encoding of the branch
429/// target operand for microMIPS. If the machine operand requires
430/// relocation, record the relocation and return zero.
431unsigned MipsMCCodeEmitter::
432getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
433 SmallVectorImpl<MCFixup> &Fixups,
434 const MCSubtargetInfo &STI) const {
435
436 const MCOperand &MO = MI.getOperand(OpNo);
437
438 // If the destination is an immediate, divide by 2.
439 if (MO.isImm()) return MO.getImm() >> 1;
440
441 assert(MO.isExpr() &&
442 "getBranchTarget21OpValueMM expects only expressions or immediates");
443
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000444 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
445 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
446 Fixups.push_back(MCFixup::create(0, FixupExpression,
447 MCFixupKind(Mips::fixup_MICROMIPS_PC21_S1)));
Zoran Jovanovic84e4d592016-05-17 11:10:15 +0000448 return 0;
449}
450
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000451/// getBranchTarget26OpValue - Return binary encoding of the branch
452/// target operand. If the machine operand requires relocation,
453/// record the relocation and return zero.
454unsigned MipsMCCodeEmitter::
455getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
456 SmallVectorImpl<MCFixup> &Fixups,
457 const MCSubtargetInfo &STI) const {
458
459 const MCOperand &MO = MI.getOperand(OpNo);
460
461 // If the destination is an immediate, divide by 4.
462 if (MO.isImm()) return MO.getImm() >> 2;
463
464 assert(MO.isExpr() &&
465 "getBranchTarget26OpValue expects only expressions or immediates");
466
Petar Jovanovicb7915a12015-06-23 13:54:42 +0000467 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
468 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
469 Fixups.push_back(MCFixup::create(0, FixupExpression,
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000470 MCFixupKind(Mips::fixup_MIPS_PC26_S2)));
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000471 return 0;
472}
473
Zoran Jovanovica887b362015-11-30 12:56:18 +0000474/// getBranchTarget26OpValueMM - Return binary encoding of the branch
475/// target operand. If the machine operand requires relocation,
476/// record the relocation and return zero.
477unsigned MipsMCCodeEmitter::getBranchTarget26OpValueMM(
478 const MCInst &MI, unsigned OpNo, SmallVectorImpl<MCFixup> &Fixups,
479 const MCSubtargetInfo &STI) const {
480
481 const MCOperand &MO = MI.getOperand(OpNo);
482
483 // If the destination is an immediate, divide by 2.
484 if (MO.isImm())
485 return MO.getImm() >> 1;
486
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000487 assert(MO.isExpr() &&
488 "getBranchTarget26OpValueMM expects only expressions or immediates");
489
490 const MCExpr *FixupExpression = MCBinaryExpr::createAdd(
491 MO.getExpr(), MCConstantExpr::create(-4, Ctx), Ctx);
492 Fixups.push_back(MCFixup::create(0, FixupExpression,
493 MCFixupKind(Mips::fixup_MICROMIPS_PC26_S1)));
Zoran Jovanovica887b362015-11-30 12:56:18 +0000494 return 0;
495}
496
Zoran Jovanovic52c56b92014-05-16 13:19:46 +0000497/// getJumpOffset16OpValue - Return binary encoding of the jump
498/// target operand. If the machine operand requires relocation,
499/// record the relocation and return zero.
500unsigned MipsMCCodeEmitter::
501getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
502 SmallVectorImpl<MCFixup> &Fixups,
503 const MCSubtargetInfo &STI) const {
504
505 const MCOperand &MO = MI.getOperand(OpNo);
506
507 if (MO.isImm()) return MO.getImm();
508
509 assert(MO.isExpr() &&
510 "getJumpOffset16OpValue expects only expressions or an immediate");
511
512 // TODO: Push fixup.
513 return 0;
514}
515
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000516/// getJumpTargetOpValue - Return binary encoding of the jump
517/// target operand. If the machine operand requires relocation,
518/// record the relocation and return zero.
519unsigned MipsMCCodeEmitter::
520getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000521 SmallVectorImpl<MCFixup> &Fixups,
522 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000523
524 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000525 // If the destination is an immediate, divide by 4.
526 if (MO.isImm()) return MO.getImm()>>2;
527
Jack Carter71e6a742012-09-06 00:43:26 +0000528 assert(MO.isExpr() &&
529 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000530
531 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000532 Fixups.push_back(MCFixup::create(0, Expr,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000533 MCFixupKind(Mips::fixup_Mips_26)));
534 return 0;
535}
536
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000537unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000538getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000539 SmallVectorImpl<MCFixup> &Fixups,
540 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000541
542 const MCOperand &MO = MI.getOperand(OpNo);
543 // If the destination is an immediate, divide by 2.
544 if (MO.isImm()) return MO.getImm() >> 1;
545
546 assert(MO.isExpr() &&
547 "getJumpTargetOpValueMM expects only expressions or an immediate");
548
549 const MCExpr *Expr = MO.getExpr();
Jim Grosbach63661f82015-05-15 19:13:05 +0000550 Fixups.push_back(MCFixup::create(0, Expr,
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000551 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
552 return 0;
553}
554
555unsigned MipsMCCodeEmitter::
Zoran Jovanovicc74e3eb92014-09-12 14:29:54 +0000556getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
557 SmallVectorImpl<MCFixup> &Fixups,
558 const MCSubtargetInfo &STI) const {
559
560 const MCOperand &MO = MI.getOperand(OpNo);
561 if (MO.isImm()) {
562 // The immediate is encoded as 'immediate << 2'.
563 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
564 assert((Res & 3) == 0);
565 return Res >> 2;
566 }
567
568 assert(MO.isExpr() &&
569 "getUImm5Lsl2Encoding expects only expressions or an immediate");
570
571 return 0;
572}
573
574unsigned MipsMCCodeEmitter::
Zoran Jovanovicbac36192014-10-23 11:06:34 +0000575getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
576 SmallVectorImpl<MCFixup> &Fixups,
577 const MCSubtargetInfo &STI) const {
578
579 const MCOperand &MO = MI.getOperand(OpNo);
580 if (MO.isImm()) {
581 int Value = MO.getImm();
582 return Value >> 2;
583 }
584
585 return 0;
586}
587
588unsigned MipsMCCodeEmitter::
Zoran Jovanovic42b84442014-10-23 11:13:59 +0000589getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
590 SmallVectorImpl<MCFixup> &Fixups,
591 const MCSubtargetInfo &STI) const {
592
593 const MCOperand &MO = MI.getOperand(OpNo);
594 if (MO.isImm()) {
595 unsigned Value = MO.getImm();
596 return Value >> 2;
597 }
598
599 return 0;
600}
601
602unsigned MipsMCCodeEmitter::
Zoran Jovanovic98bd58c2014-10-10 14:37:30 +0000603getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
604 SmallVectorImpl<MCFixup> &Fixups,
605 const MCSubtargetInfo &STI) const {
606
607 const MCOperand &MO = MI.getOperand(OpNo);
608 if (MO.isImm()) {
609 unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff;
610 return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff));
611 }
612
613 return 0;
614}
615
616unsigned MipsMCCodeEmitter::
Daniel Sanders60f1db02015-03-13 12:45:09 +0000617getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000618 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000619 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000620
Jim Grosbach13760bd2015-05-30 01:25:56 +0000621 if (Expr->evaluateAsAbsolute(Res))
Jack Carterb5cf5902013-04-17 00:18:04 +0000622 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000623
Akira Hatanakafe384a22012-03-27 02:33:05 +0000624 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000625 if (Kind == MCExpr::Constant) {
626 return cast<MCConstantExpr>(Expr)->getValue();
627 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000628
Akira Hatanakafe384a22012-03-27 02:33:05 +0000629 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000630 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
631 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000632 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000633 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000634
635 if (Kind == MCExpr::Target) {
636 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
637
638 Mips::Fixups FixupKind = Mips::Fixups(0);
639 switch (MipsExpr->getKind()) {
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000640 case MipsMCExpr::MEK_None:
641 case MipsMCExpr::MEK_Special:
642 llvm_unreachable("Unhandled fixup kind!");
643 break;
644 case MipsMCExpr::MEK_CALL_HI16:
645 FixupKind = Mips::fixup_Mips_CALL_HI16;
646 break;
647 case MipsMCExpr::MEK_CALL_LO16:
648 FixupKind = Mips::fixup_Mips_CALL_LO16;
649 break;
650 case MipsMCExpr::MEK_DTPREL_HI:
651 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
652 : Mips::fixup_Mips_DTPREL_HI;
653 break;
654 case MipsMCExpr::MEK_DTPREL_LO:
655 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
656 : Mips::fixup_Mips_DTPREL_LO;
657 break;
658 case MipsMCExpr::MEK_GOTTPREL:
659 FixupKind = Mips::fixup_Mips_GOTTPREL;
660 break;
661 case MipsMCExpr::MEK_GOT:
662 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
663 : Mips::fixup_Mips_GOT;
664 break;
665 case MipsMCExpr::MEK_GOT_CALL:
666 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
667 : Mips::fixup_Mips_CALL16;
668 break;
669 case MipsMCExpr::MEK_GOT_DISP:
670 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
671 : Mips::fixup_Mips_GOT_DISP;
672 break;
673 case MipsMCExpr::MEK_GOT_HI16:
674 FixupKind = Mips::fixup_Mips_GOT_HI16;
675 break;
676 case MipsMCExpr::MEK_GOT_LO16:
677 FixupKind = Mips::fixup_Mips_GOT_LO16;
678 break;
679 case MipsMCExpr::MEK_GOT_PAGE:
680 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
681 : Mips::fixup_Mips_GOT_PAGE;
682 break;
683 case MipsMCExpr::MEK_GOT_OFST:
684 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
685 : Mips::fixup_Mips_GOT_OFST;
686 break;
687 case MipsMCExpr::MEK_GPREL:
688 FixupKind = Mips::fixup_Mips_GPREL16;
689 break;
690 case MipsMCExpr::MEK_LO: {
691 // Check for %lo(%neg(%gp_rel(X)))
692 if (MipsExpr->isGpOff()) {
693 FixupKind = Mips::fixup_Mips_GPOFF_LO;
694 break;
695 }
696 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
697 : Mips::fixup_Mips_LO16;
698 break;
699 }
700 case MipsMCExpr::MEK_HIGHEST:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000701 FixupKind = Mips::fixup_Mips_HIGHEST;
702 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000703 case MipsMCExpr::MEK_HIGHER:
Sasa Stankovic06c47802014-04-03 10:37:45 +0000704 FixupKind = Mips::fixup_Mips_HIGHER;
705 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000706 case MipsMCExpr::MEK_HI:
707 // Check for %hi(%neg(%gp_rel(X)))
708 if (MipsExpr->isGpOff()) {
709 FixupKind = Mips::fixup_Mips_GPOFF_HI;
710 break;
711 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000712 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
713 : Mips::fixup_Mips_HI16;
714 break;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000715 case MipsMCExpr::MEK_PCREL_HI16:
716 FixupKind = Mips::fixup_MIPS_PCHI16;
717 break;
718 case MipsMCExpr::MEK_PCREL_LO16:
719 FixupKind = Mips::fixup_MIPS_PCLO16;
720 break;
721 case MipsMCExpr::MEK_TLSGD:
722 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
723 : Mips::fixup_Mips_TLSGD;
724 break;
725 case MipsMCExpr::MEK_TLSLDM:
726 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
727 : Mips::fixup_Mips_TLSLDM;
728 break;
729 case MipsMCExpr::MEK_TPREL_HI:
730 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
731 : Mips::fixup_Mips_TPREL_HI;
732 break;
733 case MipsMCExpr::MEK_TPREL_LO:
734 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
735 : Mips::fixup_Mips_TPREL_LO;
Petar Jovanovica5da5882014-02-04 18:41:57 +0000736 break;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000737 case MipsMCExpr::MEK_NEG:
738 FixupKind =
739 isMicroMips(STI) ? Mips::fixup_MICROMIPS_SUB : Mips::fixup_Mips_SUB;
740 break;
Petar Jovanovica5da5882014-02-04 18:41:57 +0000741 }
Jim Grosbach63661f82015-05-15 19:13:05 +0000742 Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind)));
Petar Jovanovica5da5882014-02-04 18:41:57 +0000743 return 0;
744 }
745
Jack Carterb5cf5902013-04-17 00:18:04 +0000746 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000747 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000748
Mark Seabornc3bd1772013-12-31 13:05:15 +0000749 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
750 default: llvm_unreachable("Unknown fixup kind!");
751 break;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000752 case MCSymbolRefExpr::VK_None:
753 FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64.
754 break;
Mark Seabornc3bd1772013-12-31 13:05:15 +0000755 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000756
Jim Grosbach63661f82015-05-15 19:13:05 +0000757 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Jack Carterb5cf5902013-04-17 00:18:04 +0000758 return 0;
759 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000760 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000761}
762
Jack Carterb5cf5902013-04-17 00:18:04 +0000763/// getMachineOpValue - Return binary encoding of operand. If the machine
764/// operand requires relocation, record the relocation and return zero.
765unsigned MipsMCCodeEmitter::
766getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000767 SmallVectorImpl<MCFixup> &Fixups,
768 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000769 if (MO.isReg()) {
770 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000771 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000772 return RegNo;
773 } else if (MO.isImm()) {
774 return static_cast<unsigned>(MO.getImm());
775 } else if (MO.isFPImm()) {
776 return static_cast<unsigned>(APFloat(MO.getFPImm())
777 .bitcastToAPInt().getHiBits(32).getLimitedValue());
778 }
779 // MO must be an Expr.
780 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000781 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000782}
783
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000784/// Return binary encoding of memory related operand.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000785/// If the offset operand requires relocation, record the relocation.
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000786template <unsigned ShiftAmount>
787unsigned MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
788 SmallVectorImpl<MCFixup> &Fixups,
789 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000790 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
791 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000792 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
793 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000794
Daniel Sandersdc0602a2016-03-31 14:12:01 +0000795 // Apply the scale factor if there is one.
796 OffBits >>= ShiftAmount;
797
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000798 return (OffBits & 0xFFFF) | RegBits;
799}
800
Jack Carter97700972013-08-13 20:19:16 +0000801unsigned MipsMCCodeEmitter::
Jozef Koleke8c9d1e2014-11-24 14:39:13 +0000802getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo,
803 SmallVectorImpl<MCFixup> &Fixups,
804 const MCSubtargetInfo &STI) const {
805 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
806 assert(MI.getOperand(OpNo).isReg());
807 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
808 Fixups, STI) << 4;
809 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
810 Fixups, STI);
811
812 return (OffBits & 0xF) | RegBits;
813}
814
815unsigned MipsMCCodeEmitter::
816getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo,
817 SmallVectorImpl<MCFixup> &Fixups,
818 const MCSubtargetInfo &STI) const {
819 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
820 assert(MI.getOperand(OpNo).isReg());
821 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
822 Fixups, STI) << 4;
823 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
824 Fixups, STI) >> 1;
825
826 return (OffBits & 0xF) | RegBits;
827}
828
829unsigned MipsMCCodeEmitter::
830getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo,
831 SmallVectorImpl<MCFixup> &Fixups,
832 const MCSubtargetInfo &STI) const {
833 // Base register is encoded in bits 6-4, offset is encoded in bits 3-0.
834 assert(MI.getOperand(OpNo).isReg());
835 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),
836 Fixups, STI) << 4;
837 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
838 Fixups, STI) >> 2;
839
840 return (OffBits & 0xF) | RegBits;
841}
842
843unsigned MipsMCCodeEmitter::
Jozef Kolek12c69822014-12-23 16:16:33 +0000844getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo,
845 SmallVectorImpl<MCFixup> &Fixups,
846 const MCSubtargetInfo &STI) const {
847 // Register is encoded in bits 9-5, offset is encoded in bits 4-0.
848 assert(MI.getOperand(OpNo).isReg() &&
Zoran Jovanovic68be5f22015-09-08 08:25:34 +0000849 (MI.getOperand(OpNo).getReg() == Mips::SP ||
850 MI.getOperand(OpNo).getReg() == Mips::SP_64) &&
Jozef Kolek12c69822014-12-23 16:16:33 +0000851 "Unexpected base register!");
852 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
853 Fixups, STI) >> 2;
854
855 return OffBits & 0x1F;
856}
857
858unsigned MipsMCCodeEmitter::
Jozef Koleke10a02e2015-01-28 17:27:26 +0000859getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo,
860 SmallVectorImpl<MCFixup> &Fixups,
861 const MCSubtargetInfo &STI) const {
862 // Register is encoded in bits 9-7, offset is encoded in bits 6-0.
863 assert(MI.getOperand(OpNo).isReg() &&
864 MI.getOperand(OpNo).getReg() == Mips::GP &&
865 "Unexpected base register!");
866
867 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1),
868 Fixups, STI) >> 2;
869
870 return OffBits & 0x7F;
871}
872
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000873unsigned MipsMCCodeEmitter::
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000874getMemEncodingMMImm9(const MCInst &MI, unsigned OpNo,
875 SmallVectorImpl<MCFixup> &Fixups,
876 const MCSubtargetInfo &STI) const {
877 // Base register is encoded in bits 20-16, offset is encoded in bits 8-0.
878 assert(MI.getOperand(OpNo).isReg());
879 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
880 STI) << 16;
Zoran Jovanovic7beb7372015-09-15 10:05:10 +0000881 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo + 1), Fixups, STI);
Zoran Jovanovic9eaa30d2015-09-08 10:18:38 +0000882
883 return (OffBits & 0x1FF) | RegBits;
884}
885
Jozef Koleke10a02e2015-01-28 17:27:26 +0000886unsigned MipsMCCodeEmitter::
Zlatko Buljancba9f802016-07-11 07:41:56 +0000887getMemEncodingMMImm11(const MCInst &MI, unsigned OpNo,
888 SmallVectorImpl<MCFixup> &Fixups,
889 const MCSubtargetInfo &STI) const {
890 // Base register is encoded in bits 20-16, offset is encoded in bits 10-0.
891 assert(MI.getOperand(OpNo).isReg());
892 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
893 STI) << 16;
894 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
895
896 return (OffBits & 0x07FF) | RegBits;
897}
898
899unsigned MipsMCCodeEmitter::
Jack Carter97700972013-08-13 20:19:16 +0000900getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000901 SmallVectorImpl<MCFixup> &Fixups,
902 const MCSubtargetInfo &STI) const {
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +0000903 // opNum can be invalid if instruction had reglist as operand.
904 // MemOperand is always last operand of instruction (base + offset).
905 switch (MI.getOpcode()) {
906 default:
907 break;
908 case Mips::SWM32_MM:
909 case Mips::LWM32_MM:
910 OpNo = MI.getNumOperands() - 2;
911 break;
912 }
913
Jack Carter97700972013-08-13 20:19:16 +0000914 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
915 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000916 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
917 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000918
919 return (OffBits & 0x0FFF) | RegBits;
920}
921
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000922unsigned MipsMCCodeEmitter::
Hrvoje Varga3c88fbd2015-10-16 12:24:58 +0000923getMemEncodingMMImm16(const MCInst &MI, unsigned OpNo,
924 SmallVectorImpl<MCFixup> &Fixups,
925 const MCSubtargetInfo &STI) const {
926 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
927 assert(MI.getOperand(OpNo).isReg());
928 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups,
929 STI) << 16;
930 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
931
932 return (OffBits & 0xFFFF) | RegBits;
933}
934
935unsigned MipsMCCodeEmitter::
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000936getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo,
937 SmallVectorImpl<MCFixup> &Fixups,
938 const MCSubtargetInfo &STI) const {
939 // opNum can be invalid if instruction had reglist as operand
940 // MemOperand is always last operand of instruction (base + offset)
941 switch (MI.getOpcode()) {
942 default:
943 break;
944 case Mips::SWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000945 case Mips::SWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000946 case Mips::LWM16_MM:
Zlatko Buljan797c2ae2015-11-12 13:21:33 +0000947 case Mips::LWM16_MMR6:
Zoran Jovanovicf9a02502014-11-27 18:28:59 +0000948 OpNo = MI.getNumOperands() - 2;
949 break;
950 }
951
952 // Offset is encoded in bits 4-0.
953 assert(MI.getOperand(OpNo).isReg());
954 // Base register is always SP - thus it is not encoded.
955 assert(MI.getOperand(OpNo+1).isImm());
956 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
957
958 return ((OffBits >> 2) & 0x0F);
959}
960
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000961// FIXME: should be called getMSBEncoding
962//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000963unsigned
964MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000965 SmallVectorImpl<MCFixup> &Fixups,
966 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000967 assert(MI.getOperand(OpNo-1).isImm());
968 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000969 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
970 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000971
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000972 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000973}
974
Daniel Sandersea4f6532015-11-06 12:22:31 +0000975template <unsigned Bits, int Offset>
Matheus Almeida779c5932013-11-18 12:32:49 +0000976unsigned
Daniel Sandersea4f6532015-11-06 12:22:31 +0000977MipsMCCodeEmitter::getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
978 SmallVectorImpl<MCFixup> &Fixups,
979 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000980 assert(MI.getOperand(OpNo).isImm());
Daniel Sandersea4f6532015-11-06 12:22:31 +0000981 unsigned Value = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
982 Value -= Offset;
983 return Value;
Matheus Almeida779c5932013-11-18 12:32:49 +0000984}
985
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000986unsigned
987MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
988 SmallVectorImpl<MCFixup> &Fixups,
989 const MCSubtargetInfo &STI) const {
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000990 const MCOperand &MO = MI.getOperand(OpNo);
991 if (MO.isImm()) {
992 // The immediate is encoded as 'immediate << 2'.
993 unsigned Res = getMachineOpValue(MI, MO, Fixups, STI);
994 assert((Res & 3) == 0);
995 return Res >> 2;
996 }
997
998 assert(MO.isExpr() &&
999 "getSimm19Lsl2Encoding expects only expressions or an immediate");
1000
1001 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic6764fa72016-04-21 14:09:35 +00001002 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC19_S2
1003 : Mips::fixup_MIPS_PC19_S2;
1004 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +00001005 return 0;
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001006}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +00001007
Zoran Jovanovic28551422014-06-09 09:49:51 +00001008unsigned
1009MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
1010 SmallVectorImpl<MCFixup> &Fixups,
1011 const MCSubtargetInfo &STI) const {
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +00001012 const MCOperand &MO = MI.getOperand(OpNo);
1013 if (MO.isImm()) {
1014 // The immediate is encoded as 'immediate << 3'.
1015 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1016 assert((Res & 7) == 0);
1017 return Res >> 3;
1018 }
1019
1020 assert(MO.isExpr() &&
1021 "getSimm18Lsl2Encoding expects only expressions or an immediate");
1022
1023 const MCExpr *Expr = MO.getExpr();
Zoran Jovanovic8e366822016-04-22 10:15:12 +00001024 Mips::Fixups FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_PC18_S3
1025 : Mips::fixup_MIPS_PC18_S3;
1026 Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind)));
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +00001027 return 0;
Zoran Jovanovic28551422014-06-09 09:49:51 +00001028}
1029
Zoran Jovanovic4a00fdc2014-10-23 10:42:01 +00001030unsigned
1031MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
1032 SmallVectorImpl<MCFixup> &Fixups,
1033 const MCSubtargetInfo &STI) const {
1034 assert(MI.getOperand(OpNo).isImm());
1035 const MCOperand &MO = MI.getOperand(OpNo);
1036 return MO.getImm() % 8;
1037}
1038
Zoran Jovanovic88531712014-11-05 17:31:00 +00001039unsigned
1040MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo,
1041 SmallVectorImpl<MCFixup> &Fixups,
1042 const MCSubtargetInfo &STI) const {
1043 assert(MI.getOperand(OpNo).isImm());
1044 const MCOperand &MO = MI.getOperand(OpNo);
1045 unsigned Value = MO.getImm();
1046 switch (Value) {
1047 case 128: return 0x0;
1048 case 1: return 0x1;
1049 case 2: return 0x2;
1050 case 3: return 0x3;
1051 case 4: return 0x4;
1052 case 7: return 0x5;
1053 case 8: return 0x6;
1054 case 15: return 0x7;
1055 case 16: return 0x8;
1056 case 31: return 0x9;
1057 case 32: return 0xa;
1058 case 63: return 0xb;
1059 case 64: return 0xc;
1060 case 255: return 0xd;
1061 case 32768: return 0xe;
1062 case 65535: return 0xf;
1063 }
1064 llvm_unreachable("Unexpected value");
1065}
1066
Zoran Jovanovica4c4b5f2014-11-19 16:44:02 +00001067unsigned
1068MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
1069 SmallVectorImpl<MCFixup> &Fixups,
1070 const MCSubtargetInfo &STI) const {
1071 unsigned res = 0;
1072
1073 // Register list operand is always first operand of instruction and it is
1074 // placed before memory operand (register + imm).
1075
1076 for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) {
1077 unsigned Reg = MI.getOperand(I).getReg();
1078 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
1079 if (RegNo != 31)
1080 res++;
1081 else
1082 res |= 0x10;
1083 }
1084 return res;
1085}
1086
Zoran Jovanovicf9a02502014-11-27 18:28:59 +00001087unsigned
1088MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
1089 SmallVectorImpl<MCFixup> &Fixups,
1090 const MCSubtargetInfo &STI) const {
1091 return (MI.getNumOperands() - 4);
1092}
1093
Zoran Jovanovic2deca342014-12-16 14:59:10 +00001094unsigned
1095MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo,
1096 SmallVectorImpl<MCFixup> &Fixups,
1097 const MCSubtargetInfo &STI) const {
1098 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
1099}
1100
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001101unsigned
Zoran Jovanovic41688672015-02-10 16:36:20 +00001102MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
1103 SmallVectorImpl<MCFixup> &Fixups,
1104 const MCSubtargetInfo &STI) const {
1105 unsigned res = 0;
1106
1107 if (MI.getOperand(0).getReg() == Mips::A1 &&
1108 MI.getOperand(1).getReg() == Mips::A2)
1109 res = 0;
1110 else if (MI.getOperand(0).getReg() == Mips::A1 &&
1111 MI.getOperand(1).getReg() == Mips::A3)
1112 res = 1;
1113 else if (MI.getOperand(0).getReg() == Mips::A2 &&
1114 MI.getOperand(1).getReg() == Mips::A3)
1115 res = 2;
1116 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1117 MI.getOperand(1).getReg() == Mips::S5)
1118 res = 3;
1119 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1120 MI.getOperand(1).getReg() == Mips::S6)
1121 res = 4;
1122 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1123 MI.getOperand(1).getReg() == Mips::A1)
1124 res = 5;
1125 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1126 MI.getOperand(1).getReg() == Mips::A2)
1127 res = 6;
1128 else if (MI.getOperand(0).getReg() == Mips::A0 &&
1129 MI.getOperand(1).getReg() == Mips::A3)
1130 res = 7;
1131
1132 return res;
1133}
1134
1135unsigned
Jozef Kolek2c6d7322015-01-21 12:10:11 +00001136MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
1137 SmallVectorImpl<MCFixup> &Fixups,
1138 const MCSubtargetInfo &STI) const {
1139 const MCOperand &MO = MI.getOperand(OpNo);
1140 assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate");
1141 // The immediate is encoded as 'immediate >> 2'.
1142 unsigned Res = static_cast<unsigned>(MO.getImm());
1143 assert((Res & 3) == 0);
1144 return Res >> 2;
1145}
1146
Daniel Sandersb59e1a42014-05-15 10:45:58 +00001147#include "MipsGenMCCodeEmitter.inc"