blob: be8ccdac9849e273a6d4deaa73063ca8a430c80e [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//
14#define DEBUG_TYPE "mccodeemitter"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000015#include "MCTargetDesc/MipsBaseInfo.h"
16#include "MCTargetDesc/MipsFixupKinds.h"
17#include "MCTargetDesc/MipsMCTargetDesc.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/Statistic.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000020#include "llvm/MC/MCCodeEmitter.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"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCRegisterInfo.h"
26#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000027#include "llvm/Support/raw_ostream.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000028
Akira Hatanakabe6a8182013-04-19 19:03:11 +000029#define GET_INSTRMAP_INFO
30#include "MipsGenInstrInfo.inc"
31
Akira Hatanaka750ecec2011-09-30 20:40:03 +000032using namespace llvm;
33
34namespace {
35class MipsMCCodeEmitter : public MCCodeEmitter {
Craig Topper2ed23ce2012-09-15 17:08:51 +000036 MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
37 void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000038 const MCInstrInfo &MCII;
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000039 MCContext &Ctx;
Akira Hatanakabe6a8182013-04-19 19:03:11 +000040 const MCSubtargetInfo &STI;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000041 bool IsLittleEndian;
Jack Carter7bd3c7d2013-08-08 23:30:40 +000042 bool IsMicroMips;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000043
44public:
Jack Carterab3cb422013-02-19 22:04:37 +000045 MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_,
46 const MCSubtargetInfo &sti, bool IsLittle) :
Jack Carter7bd3c7d2013-08-08 23:30:40 +000047 MCII(mcii), Ctx(Ctx_), STI (sti), IsLittleEndian(IsLittle) {
48 IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips;
49 }
Akira Hatanaka750ecec2011-09-30 20:40:03 +000050
51 ~MipsMCCodeEmitter() {}
52
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000053 void EmitByte(unsigned char C, raw_ostream &OS) const {
54 OS << (char)C;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000055 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000056
57 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
58 // Output the instruction encoding in little endian byte order.
Jack Carter7bd3c7d2013-08-08 23:30:40 +000059 // Little-endian byte ordering:
60 // mips32r2: 4 | 3 | 2 | 1
61 // microMIPS: 2 | 1 | 4 | 3
62 if (IsLittleEndian && Size == 4 && IsMicroMips) {
63 EmitInstruction(Val>>16, 2, OS);
64 EmitInstruction(Val, 2, OS);
65 } else {
66 for (unsigned i = 0; i < Size; ++i) {
67 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
68 EmitByte((Val >> Shift) & 0xff, OS);
69 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000070 }
71 }
72
73 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +000074 SmallVectorImpl<MCFixup> &Fixups,
75 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000076
77 // getBinaryCodeForInstr - TableGen'erated function for getting the
78 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000079 uint64_t getBinaryCodeForInstr(const MCInst &MI,
David Woodhouse3fa98a62014-01-28 23:13:18 +000080 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000082
83 // getBranchJumpOpValue - Return binary encoding of the jump
84 // target operand. If the machine operand requires relocation,
85 // record the relocation and return zero.
Mark Seaborn774c2432013-12-29 10:47:04 +000086 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000087 SmallVectorImpl<MCFixup> &Fixups,
88 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000089
Zoran Jovanovic507e0842013-10-29 16:38:59 +000090 // getBranchJumpOpValueMM - Return binary encoding of the microMIPS jump
91 // target operand. If the machine operand requires relocation,
92 // record the relocation and return zero.
93 unsigned getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000094 SmallVectorImpl<MCFixup> &Fixups,
95 const MCSubtargetInfo &STI) const;
Zoran Jovanovic507e0842013-10-29 16:38:59 +000096
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000097 // getBranchTargetOpValue - Return binary encoding of the branch
98 // target operand. If the machine operand requires relocation,
99 // record the relocation and return zero.
100 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000101 SmallVectorImpl<MCFixup> &Fixups,
102 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000103
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000104 // getBranchTargetOpValue - Return binary encoding of the microMIPS branch
105 // target operand. If the machine operand requires relocation,
106 // record the relocation and return zero.
107 unsigned getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000108 SmallVectorImpl<MCFixup> &Fixups,
109 const MCSubtargetInfo &STI) const;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000110
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000111 // getMachineOpValue - Return binary encoding of operand. If the machin
112 // operand requires relocation, record the relocation and return zero.
113 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000114 SmallVectorImpl<MCFixup> &Fixups,
115 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000116
Matheus Almeida6b59c442013-12-05 11:06:22 +0000117 unsigned getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000118 SmallVectorImpl<MCFixup> &Fixups,
119 const MCSubtargetInfo &STI) const;
Matheus Almeida6b59c442013-12-05 11:06:22 +0000120
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000121 unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000122 SmallVectorImpl<MCFixup> &Fixups,
123 const MCSubtargetInfo &STI) const;
Jack Carter97700972013-08-13 20:19:16 +0000124 unsigned getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000125 SmallVectorImpl<MCFixup> &Fixups,
126 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000127 unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000128 SmallVectorImpl<MCFixup> &Fixups,
129 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000130 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000131 SmallVectorImpl<MCFixup> &Fixups,
132 const MCSubtargetInfo &STI) const;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000133
Matheus Almeida779c5932013-11-18 12:32:49 +0000134 // getLSAImmEncoding - Return binary encoding of LSA immediate.
135 unsigned getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000136 SmallVectorImpl<MCFixup> &Fixups,
137 const MCSubtargetInfo &STI) const;
Matheus Almeida779c5932013-11-18 12:32:49 +0000138
Jack Carterb5cf5902013-04-17 00:18:04 +0000139 unsigned
David Woodhouse3fa98a62014-01-28 23:13:18 +0000140 getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
141 const MCSubtargetInfo &STI) const;
Jack Carterb5cf5902013-04-17 00:18:04 +0000142
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000143}; // class MipsMCCodeEmitter
144} // namespace
145
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000146MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000147 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000148 const MCSubtargetInfo &STI,
149 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000150{
Jack Carterab3cb422013-02-19 22:04:37 +0000151 return new MipsMCCodeEmitter(MCII, Ctx, STI, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000152}
153
154MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000155 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000156 const MCSubtargetInfo &STI,
157 MCContext &Ctx)
158{
Jack Carterab3cb422013-02-19 22:04:37 +0000159 return new MipsMCCodeEmitter(MCII, Ctx, STI, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000160}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000161
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000162
163// If the D<shift> instruction has a shift amount that is greater
164// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
165static void LowerLargeShift(MCInst& Inst) {
166
167 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
168 assert(Inst.getOperand(2).isImm());
169
170 int64_t Shift = Inst.getOperand(2).getImm();
171 if (Shift <= 31)
172 return; // Do nothing
173 Shift -= 32;
174
175 // saminus32
176 Inst.getOperand(2).setImm(Shift);
177
178 switch (Inst.getOpcode()) {
179 default:
180 // Calling function is not synchronized
181 llvm_unreachable("Unexpected shift instruction");
182 case Mips::DSLL:
183 Inst.setOpcode(Mips::DSLL32);
184 return;
185 case Mips::DSRL:
186 Inst.setOpcode(Mips::DSRL32);
187 return;
188 case Mips::DSRA:
189 Inst.setOpcode(Mips::DSRA32);
190 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000191 case Mips::DROTR:
192 Inst.setOpcode(Mips::DROTR32);
193 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000194 }
195}
196
197// Pick a DEXT or DINS instruction variant based on the pos and size operands
198static void LowerDextDins(MCInst& InstIn) {
199 int Opcode = InstIn.getOpcode();
200
201 if (Opcode == Mips::DEXT)
202 assert(InstIn.getNumOperands() == 4 &&
203 "Invalid no. of machine operands for DEXT!");
204 else // Only DEXT and DINS are possible
205 assert(InstIn.getNumOperands() == 5 &&
206 "Invalid no. of machine operands for DINS!");
207
208 assert(InstIn.getOperand(2).isImm());
209 int64_t pos = InstIn.getOperand(2).getImm();
210 assert(InstIn.getOperand(3).isImm());
211 int64_t size = InstIn.getOperand(3).getImm();
212
213 if (size <= 32) {
214 if (pos < 32) // DEXT/DINS, do nothing
215 return;
216 // DEXTU/DINSU
217 InstIn.getOperand(2).setImm(pos - 32);
218 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
219 return;
220 }
221 // DEXTM/DINSM
222 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
223 InstIn.getOperand(3).setImm(size - 32);
224 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
225 return;
226}
227
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000228/// EncodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000229/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000230void MipsMCCodeEmitter::
231EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000232 SmallVectorImpl<MCFixup> &Fixups,
233 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000234{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000235
236 // Non-pseudo instructions that get changed for direct object
237 // only based on operand values.
238 // If this list of instructions get much longer we will move
239 // the check to a function call. Until then, this is more efficient.
240 MCInst TmpInst = MI;
241 switch (MI.getOpcode()) {
242 // If shift amount is >= 32 it the inst needs to be lowered further
243 case Mips::DSLL:
244 case Mips::DSRL:
245 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000246 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000247 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000248 break;
249 // Double extract instruction is chosen by pos and size operands
250 case Mips::DEXT:
251 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000252 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000253 }
254
Jack Carter97700972013-08-13 20:19:16 +0000255 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000256 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000257
258 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000259 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000260 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000261 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000262 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
263 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
264
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000265 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
266 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
267 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000268 if (Fixups.size() > N)
269 Fixups.pop_back();
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000270 Opcode = NewOpcode;
271 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000272 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000273 }
274 }
275
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000276 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000277
Jack Carter5b5559d2012-10-03 21:58:54 +0000278 // Get byte count of instruction
279 unsigned Size = Desc.getSize();
280 if (!Size)
281 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000282
283 EmitInstruction(Binary, Size, OS);
284}
285
286/// getBranchTargetOpValue - Return binary encoding of the branch
287/// target operand. If the machine operand requires relocation,
288/// record the relocation and return zero.
289unsigned MipsMCCodeEmitter::
290getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000291 SmallVectorImpl<MCFixup> &Fixups,
292 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000293
294 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000295
Jack Carter4f69a0f2013-03-22 00:29:10 +0000296 // If the destination is an immediate, divide by 4.
297 if (MO.isImm()) return MO.getImm() >> 2;
298
Jack Carter71e6a742012-09-06 00:43:26 +0000299 assert(MO.isExpr() &&
300 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000301
302 const MCExpr *Expr = MO.getExpr();
303 Fixups.push_back(MCFixup::Create(0, Expr,
304 MCFixupKind(Mips::fixup_Mips_PC16)));
305 return 0;
306}
307
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000308/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
309/// target operand. If the machine operand requires relocation,
310/// record the relocation and return zero.
311unsigned MipsMCCodeEmitter::
312getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000313 SmallVectorImpl<MCFixup> &Fixups,
314 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000315
316 const MCOperand &MO = MI.getOperand(OpNo);
317
318 // If the destination is an immediate, divide by 2.
319 if (MO.isImm()) return MO.getImm() >> 1;
320
321 assert(MO.isExpr() &&
322 "getBranchTargetOpValueMM expects only expressions or immediates");
323
324 const MCExpr *Expr = MO.getExpr();
325 Fixups.push_back(MCFixup::Create(0, Expr,
326 MCFixupKind(Mips::
327 fixup_MICROMIPS_PC16_S1)));
328 return 0;
329}
330
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000331/// getJumpTargetOpValue - Return binary encoding of the jump
332/// target operand. If the machine operand requires relocation,
333/// record the relocation and return zero.
334unsigned MipsMCCodeEmitter::
335getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000336 SmallVectorImpl<MCFixup> &Fixups,
337 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000338
339 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000340 // If the destination is an immediate, divide by 4.
341 if (MO.isImm()) return MO.getImm()>>2;
342
Jack Carter71e6a742012-09-06 00:43:26 +0000343 assert(MO.isExpr() &&
344 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000345
346 const MCExpr *Expr = MO.getExpr();
347 Fixups.push_back(MCFixup::Create(0, Expr,
348 MCFixupKind(Mips::fixup_Mips_26)));
349 return 0;
350}
351
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000352unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000353getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000354 SmallVectorImpl<MCFixup> &Fixups,
355 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000356
357 const MCOperand &MO = MI.getOperand(OpNo);
358 // If the destination is an immediate, divide by 2.
359 if (MO.isImm()) return MO.getImm() >> 1;
360
361 assert(MO.isExpr() &&
362 "getJumpTargetOpValueMM expects only expressions or an immediate");
363
364 const MCExpr *Expr = MO.getExpr();
365 Fixups.push_back(MCFixup::Create(0, Expr,
366 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
367 return 0;
368}
369
370unsigned MipsMCCodeEmitter::
David Woodhouse3fa98a62014-01-28 23:13:18 +0000371getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
372 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000373 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000374
Jack Carterb5cf5902013-04-17 00:18:04 +0000375 if (Expr->EvaluateAsAbsolute(Res))
376 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000377
Akira Hatanakafe384a22012-03-27 02:33:05 +0000378 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000379 if (Kind == MCExpr::Constant) {
380 return cast<MCConstantExpr>(Expr)->getValue();
381 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000382
Akira Hatanakafe384a22012-03-27 02:33:05 +0000383 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000384 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
385 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000386 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000387 }
Jack Carterb5cf5902013-04-17 00:18:04 +0000388 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000389 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000390
Mark Seabornc3bd1772013-12-31 13:05:15 +0000391 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
392 default: llvm_unreachable("Unknown fixup kind!");
393 break;
394 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
395 FixupKind = Mips::fixup_Mips_GPOFF_HI;
396 break;
397 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
398 FixupKind = Mips::fixup_Mips_GPOFF_LO;
399 break;
400 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
401 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_PAGE
402 : Mips::fixup_Mips_GOT_PAGE;
403 break;
404 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
405 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_OFST
406 : Mips::fixup_Mips_GOT_OFST;
407 break;
408 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
409 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_DISP
410 : Mips::fixup_Mips_GOT_DISP;
411 break;
412 case MCSymbolRefExpr::VK_Mips_GPREL:
413 FixupKind = Mips::fixup_Mips_GPREL16;
414 break;
415 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
416 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_CALL16
417 : Mips::fixup_Mips_CALL16;
418 break;
419 case MCSymbolRefExpr::VK_Mips_GOT16:
420 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT16
421 : Mips::fixup_Mips_GOT_Global;
422 break;
423 case MCSymbolRefExpr::VK_Mips_GOT:
424 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT16
425 : Mips::fixup_Mips_GOT_Local;
426 break;
427 case MCSymbolRefExpr::VK_Mips_ABS_HI:
428 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_HI16
429 : Mips::fixup_Mips_HI16;
430 break;
431 case MCSymbolRefExpr::VK_Mips_ABS_LO:
432 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_LO16
433 : Mips::fixup_Mips_LO16;
434 break;
435 case MCSymbolRefExpr::VK_Mips_TLSGD:
436 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_GD
437 : Mips::fixup_Mips_TLSGD;
438 break;
439 case MCSymbolRefExpr::VK_Mips_TLSLDM:
440 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_LDM
441 : Mips::fixup_Mips_TLSLDM;
442 break;
443 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
444 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
445 : Mips::fixup_Mips_DTPREL_HI;
446 break;
447 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
448 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
449 : Mips::fixup_Mips_DTPREL_LO;
450 break;
451 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
452 FixupKind = Mips::fixup_Mips_GOTTPREL;
453 break;
454 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
455 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
456 : Mips::fixup_Mips_TPREL_HI;
457 break;
458 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
459 FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
460 : Mips::fixup_Mips_TPREL_LO;
461 break;
462 case MCSymbolRefExpr::VK_Mips_HIGHER:
463 FixupKind = Mips::fixup_Mips_HIGHER;
464 break;
465 case MCSymbolRefExpr::VK_Mips_HIGHEST:
466 FixupKind = Mips::fixup_Mips_HIGHEST;
467 break;
468 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
469 FixupKind = Mips::fixup_Mips_GOT_HI16;
470 break;
471 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
472 FixupKind = Mips::fixup_Mips_GOT_LO16;
473 break;
474 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
475 FixupKind = Mips::fixup_Mips_CALL_HI16;
476 break;
477 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
478 FixupKind = Mips::fixup_Mips_CALL_LO16;
479 break;
480 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000481
Jack Carterb5cf5902013-04-17 00:18:04 +0000482 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
483 return 0;
484 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000485 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000486}
487
Jack Carterb5cf5902013-04-17 00:18:04 +0000488/// getMachineOpValue - Return binary encoding of operand. If the machine
489/// operand requires relocation, record the relocation and return zero.
490unsigned MipsMCCodeEmitter::
491getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000492 SmallVectorImpl<MCFixup> &Fixups,
493 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000494 if (MO.isReg()) {
495 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000496 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000497 return RegNo;
498 } else if (MO.isImm()) {
499 return static_cast<unsigned>(MO.getImm());
500 } else if (MO.isFPImm()) {
501 return static_cast<unsigned>(APFloat(MO.getFPImm())
502 .bitcastToAPInt().getHiBits(32).getLimitedValue());
503 }
504 // MO must be an Expr.
505 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000506 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000507}
508
Matheus Almeida6b59c442013-12-05 11:06:22 +0000509/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
510/// instructions.
511unsigned
512MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000513 SmallVectorImpl<MCFixup> &Fixups,
514 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000515 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
516 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000517 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
518 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000519
520 // The immediate field of an LD/ST instruction is scaled which means it must
521 // be divided (when encoding) by the size (in bytes) of the instructions'
522 // data format.
523 // .b - 1 byte
524 // .h - 2 bytes
525 // .w - 4 bytes
526 // .d - 8 bytes
527 switch(MI.getOpcode())
528 {
529 default:
530 assert (0 && "Unexpected instruction");
531 break;
532 case Mips::LD_B:
533 case Mips::ST_B:
534 // We don't need to scale the offset in this case
535 break;
536 case Mips::LD_H:
537 case Mips::ST_H:
538 OffBits >>= 1;
539 break;
540 case Mips::LD_W:
541 case Mips::ST_W:
542 OffBits >>= 2;
543 break;
544 case Mips::LD_D:
545 case Mips::ST_D:
546 OffBits >>= 3;
547 break;
548 }
549
550 return (OffBits & 0xFFFF) | RegBits;
551}
552
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000553/// getMemEncoding - Return binary encoding of memory related operand.
554/// If the offset operand requires relocation, record the relocation.
555unsigned
556MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000557 SmallVectorImpl<MCFixup> &Fixups,
558 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000559 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
560 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000561 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
562 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000563
564 return (OffBits & 0xFFFF) | RegBits;
565}
566
Jack Carter97700972013-08-13 20:19:16 +0000567unsigned MipsMCCodeEmitter::
568getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000569 SmallVectorImpl<MCFixup> &Fixups,
570 const MCSubtargetInfo &STI) const {
Jack Carter97700972013-08-13 20:19:16 +0000571 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
572 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000573 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
574 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000575
576 return (OffBits & 0x0FFF) | RegBits;
577}
578
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000579unsigned
580MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000581 SmallVectorImpl<MCFixup> &Fixups,
582 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000583 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000584 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000585 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000586}
587
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000588// FIXME: should be called getMSBEncoding
589//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000590unsigned
591MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000592 SmallVectorImpl<MCFixup> &Fixups,
593 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000594 assert(MI.getOperand(OpNo-1).isImm());
595 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000596 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
597 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000598
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000599 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000600}
601
Matheus Almeida779c5932013-11-18 12:32:49 +0000602unsigned
603MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000604 SmallVectorImpl<MCFixup> &Fixups,
605 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000606 assert(MI.getOperand(OpNo).isImm());
607 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000608 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000609}
610
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000611#include "MipsGenMCCodeEmitter.inc"
612