blob: fc516188c3c6555f4b7cf560e0802ad3a4406114 [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"
23#include "llvm/MC/MCInst.h"
24#include "llvm/MC/MCInstrInfo.h"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000025#include "llvm/MC/MCFixup.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000026#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
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "mccodeemitter"
30
Akira Hatanakabe6a8182013-04-19 19:03:11 +000031#define GET_INSTRMAP_INFO
32#include "MipsGenInstrInfo.inc"
Matheus Almeida9e1450b2014-03-20 09:29:54 +000033#undef GET_INSTRMAP_INFO
Akira Hatanakabe6a8182013-04-19 19:03:11 +000034
Matheus Almeida9e1450b2014-03-20 09:29:54 +000035namespace llvm {
36MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
37 const MCRegisterInfo &MRI,
38 const MCSubtargetInfo &STI,
39 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,
45 const MCSubtargetInfo &STI,
46 MCContext &Ctx) {
David Woodhoused2cca112014-01-28 23:13:25 +000047 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +000048}
Matheus Almeida9e1450b2014-03-20 09:29:54 +000049} // End of namespace llvm.
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000050
51// If the D<shift> instruction has a shift amount that is greater
52// than 31 (checked in calling routine), lower it to a D<shift>32 instruction
53static void LowerLargeShift(MCInst& Inst) {
54
55 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
56 assert(Inst.getOperand(2).isImm());
57
58 int64_t Shift = Inst.getOperand(2).getImm();
59 if (Shift <= 31)
60 return; // Do nothing
61 Shift -= 32;
62
63 // saminus32
64 Inst.getOperand(2).setImm(Shift);
65
66 switch (Inst.getOpcode()) {
67 default:
68 // Calling function is not synchronized
69 llvm_unreachable("Unexpected shift instruction");
70 case Mips::DSLL:
71 Inst.setOpcode(Mips::DSLL32);
72 return;
73 case Mips::DSRL:
74 Inst.setOpcode(Mips::DSRL32);
75 return;
76 case Mips::DSRA:
77 Inst.setOpcode(Mips::DSRA32);
78 return;
Akira Hatanaka6a3fe572013-09-07 00:18:01 +000079 case Mips::DROTR:
80 Inst.setOpcode(Mips::DROTR32);
81 return;
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +000082 }
83}
84
85// Pick a DEXT or DINS instruction variant based on the pos and size operands
86static void LowerDextDins(MCInst& InstIn) {
87 int Opcode = InstIn.getOpcode();
88
89 if (Opcode == Mips::DEXT)
90 assert(InstIn.getNumOperands() == 4 &&
91 "Invalid no. of machine operands for DEXT!");
92 else // Only DEXT and DINS are possible
93 assert(InstIn.getNumOperands() == 5 &&
94 "Invalid no. of machine operands for DINS!");
95
96 assert(InstIn.getOperand(2).isImm());
97 int64_t pos = InstIn.getOperand(2).getImm();
98 assert(InstIn.getOperand(3).isImm());
99 int64_t size = InstIn.getOperand(3).getImm();
100
101 if (size <= 32) {
102 if (pos < 32) // DEXT/DINS, do nothing
103 return;
104 // DEXTU/DINSU
105 InstIn.getOperand(2).setImm(pos - 32);
106 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
107 return;
108 }
109 // DEXTM/DINSM
110 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
111 InstIn.getOperand(3).setImm(size - 32);
112 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
113 return;
114}
115
Matheus Almeida9e1450b2014-03-20 09:29:54 +0000116bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
117 return STI.getFeatureBits() & Mips::FeatureMicroMips;
118}
119
120void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
121 OS << (char)C;
122}
123
124void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
125 const MCSubtargetInfo &STI,
126 raw_ostream &OS) const {
127 // Output the instruction encoding in little endian byte order.
128 // Little-endian byte ordering:
129 // mips32r2: 4 | 3 | 2 | 1
130 // microMIPS: 2 | 1 | 4 | 3
131 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
132 EmitInstruction(Val >> 16, 2, STI, OS);
133 EmitInstruction(Val, 2, STI, OS);
134 } else {
135 for (unsigned i = 0; i < Size; ++i) {
136 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
137 EmitByte((Val >> Shift) & 0xff, OS);
138 }
139 }
140}
141
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000142/// EncodeInstruction - Emit the instruction.
Jack Carter4e07b95d2013-08-27 19:45:28 +0000143/// Size the instruction with Desc.getSize().
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000144void MipsMCCodeEmitter::
145EncodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000146 SmallVectorImpl<MCFixup> &Fixups,
147 const MCSubtargetInfo &STI) const
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000148{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000149
150 // Non-pseudo instructions that get changed for direct object
151 // only based on operand values.
152 // If this list of instructions get much longer we will move
153 // the check to a function call. Until then, this is more efficient.
154 MCInst TmpInst = MI;
155 switch (MI.getOpcode()) {
156 // If shift amount is >= 32 it the inst needs to be lowered further
157 case Mips::DSLL:
158 case Mips::DSRL:
159 case Mips::DSRA:
Akira Hatanaka6a3fe572013-09-07 00:18:01 +0000160 case Mips::DROTR:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000161 LowerLargeShift(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000162 break;
163 // Double extract instruction is chosen by pos and size operands
164 case Mips::DEXT:
165 case Mips::DINS:
Rafael Espindolaf30f2cc2013-05-27 22:34:59 +0000166 LowerDextDins(TmpInst);
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000167 }
168
Jack Carter97700972013-08-13 20:19:16 +0000169 unsigned long N = Fixups.size();
David Woodhouse3fa98a62014-01-28 23:13:18 +0000170 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000171
172 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000173 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000174 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000175 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000176 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
177 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
178
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000179 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
180 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
181 if (NewOpcode != -1) {
Jack Carter97700972013-08-13 20:19:16 +0000182 if (Fixups.size() > N)
183 Fixups.pop_back();
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000184 Opcode = NewOpcode;
185 TmpInst.setOpcode (NewOpcode);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000186 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
Akira Hatanakabe6a8182013-04-19 19:03:11 +0000187 }
188 }
189
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000190 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000191
Jack Carter5b5559d2012-10-03 21:58:54 +0000192 // Get byte count of instruction
193 unsigned Size = Desc.getSize();
194 if (!Size)
195 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000196
David Woodhoused2cca112014-01-28 23:13:25 +0000197 EmitInstruction(Binary, Size, STI, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000198}
199
200/// getBranchTargetOpValue - Return binary encoding of the branch
201/// target operand. If the machine operand requires relocation,
202/// record the relocation and return zero.
203unsigned MipsMCCodeEmitter::
204getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000205 SmallVectorImpl<MCFixup> &Fixups,
206 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000207
208 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000209
Jack Carter4f69a0f2013-03-22 00:29:10 +0000210 // If the destination is an immediate, divide by 4.
211 if (MO.isImm()) return MO.getImm() >> 2;
212
Jack Carter71e6a742012-09-06 00:43:26 +0000213 assert(MO.isExpr() &&
214 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000215
216 const MCExpr *Expr = MO.getExpr();
217 Fixups.push_back(MCFixup::Create(0, Expr,
218 MCFixupKind(Mips::fixup_Mips_PC16)));
219 return 0;
220}
221
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000222/// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
223/// target operand. If the machine operand requires relocation,
224/// record the relocation and return zero.
225unsigned MipsMCCodeEmitter::
226getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000227 SmallVectorImpl<MCFixup> &Fixups,
228 const MCSubtargetInfo &STI) const {
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000229
230 const MCOperand &MO = MI.getOperand(OpNo);
231
232 // If the destination is an immediate, divide by 2.
233 if (MO.isImm()) return MO.getImm() >> 1;
234
235 assert(MO.isExpr() &&
236 "getBranchTargetOpValueMM expects only expressions or immediates");
237
238 const MCExpr *Expr = MO.getExpr();
239 Fixups.push_back(MCFixup::Create(0, Expr,
240 MCFixupKind(Mips::
241 fixup_MICROMIPS_PC16_S1)));
242 return 0;
243}
244
Zoran Jovanovic3c8869d2014-05-16 11:03:45 +0000245/// getBranchTarget21OpValue - Return binary encoding of the branch
246/// target operand. If the machine operand requires relocation,
247/// record the relocation and return zero.
248unsigned MipsMCCodeEmitter::
249getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
250 SmallVectorImpl<MCFixup> &Fixups,
251 const MCSubtargetInfo &STI) const {
252
253 const MCOperand &MO = MI.getOperand(OpNo);
254
255 // If the destination is an immediate, divide by 4.
256 if (MO.isImm()) return MO.getImm() >> 2;
257
258 assert(MO.isExpr() &&
259 "getBranchTarget21OpValue expects only expressions or immediates");
260
261 // TODO: Push 21 PC fixup.
262 return 0;
263}
264
265/// getBranchTarget26OpValue - Return binary encoding of the branch
266/// target operand. If the machine operand requires relocation,
267/// record the relocation and return zero.
268unsigned MipsMCCodeEmitter::
269getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
270 SmallVectorImpl<MCFixup> &Fixups,
271 const MCSubtargetInfo &STI) const {
272
273 const MCOperand &MO = MI.getOperand(OpNo);
274
275 // If the destination is an immediate, divide by 4.
276 if (MO.isImm()) return MO.getImm() >> 2;
277
278 assert(MO.isExpr() &&
279 "getBranchTarget26OpValue expects only expressions or immediates");
280
281 // TODO: Push 26 PC fixup.
282 return 0;
283}
284
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000285/// getJumpTargetOpValue - Return binary encoding of the jump
286/// target operand. If the machine operand requires relocation,
287/// record the relocation and return zero.
288unsigned MipsMCCodeEmitter::
289getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000290 SmallVectorImpl<MCFixup> &Fixups,
291 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000292
293 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000294 // If the destination is an immediate, divide by 4.
295 if (MO.isImm()) return MO.getImm()>>2;
296
Jack Carter71e6a742012-09-06 00:43:26 +0000297 assert(MO.isExpr() &&
298 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000299
300 const MCExpr *Expr = MO.getExpr();
301 Fixups.push_back(MCFixup::Create(0, Expr,
302 MCFixupKind(Mips::fixup_Mips_26)));
303 return 0;
304}
305
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000306unsigned MipsMCCodeEmitter::
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000307getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000308 SmallVectorImpl<MCFixup> &Fixups,
309 const MCSubtargetInfo &STI) const {
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000310
311 const MCOperand &MO = MI.getOperand(OpNo);
312 // If the destination is an immediate, divide by 2.
313 if (MO.isImm()) return MO.getImm() >> 1;
314
315 assert(MO.isExpr() &&
316 "getJumpTargetOpValueMM expects only expressions or an immediate");
317
318 const MCExpr *Expr = MO.getExpr();
319 Fixups.push_back(MCFixup::Create(0, Expr,
320 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
321 return 0;
322}
323
324unsigned MipsMCCodeEmitter::
David Woodhouse3fa98a62014-01-28 23:13:18 +0000325getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
326 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000327 int64_t Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000328
Jack Carterb5cf5902013-04-17 00:18:04 +0000329 if (Expr->EvaluateAsAbsolute(Res))
330 return Res;
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000331
Akira Hatanakafe384a22012-03-27 02:33:05 +0000332 MCExpr::ExprKind Kind = Expr->getKind();
Jack Carterb5cf5902013-04-17 00:18:04 +0000333 if (Kind == MCExpr::Constant) {
334 return cast<MCConstantExpr>(Expr)->getValue();
335 }
Akira Hatanakae2eed962011-12-22 01:05:17 +0000336
Akira Hatanakafe384a22012-03-27 02:33:05 +0000337 if (Kind == MCExpr::Binary) {
David Woodhouse3fa98a62014-01-28 23:13:18 +0000338 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
339 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000340 return Res;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000341 }
Petar Jovanovica5da5882014-02-04 18:41:57 +0000342
343 if (Kind == MCExpr::Target) {
344 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
345
346 Mips::Fixups FixupKind = Mips::Fixups(0);
347 switch (MipsExpr->getKind()) {
348 default: llvm_unreachable("Unsupported fixup kind for target expression!");
Sasa Stankovic06c47802014-04-03 10:37:45 +0000349 case MipsMCExpr::VK_Mips_HIGHEST:
350 FixupKind = Mips::fixup_Mips_HIGHEST;
351 break;
352 case MipsMCExpr::VK_Mips_HIGHER:
353 FixupKind = Mips::fixup_Mips_HIGHER;
354 break;
355 case MipsMCExpr::VK_Mips_HI:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000356 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
357 : Mips::fixup_Mips_HI16;
358 break;
Sasa Stankovic06c47802014-04-03 10:37:45 +0000359 case MipsMCExpr::VK_Mips_LO:
Petar Jovanovica5da5882014-02-04 18:41:57 +0000360 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
361 : Mips::fixup_Mips_LO16;
362 break;
363 }
364 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
365 return 0;
366 }
367
Jack Carterb5cf5902013-04-17 00:18:04 +0000368 if (Kind == MCExpr::SymbolRef) {
Mark Seabornc3bd1772013-12-31 13:05:15 +0000369 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000370
Mark Seabornc3bd1772013-12-31 13:05:15 +0000371 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
372 default: llvm_unreachable("Unknown fixup kind!");
373 break;
374 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
375 FixupKind = Mips::fixup_Mips_GPOFF_HI;
376 break;
377 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
378 FixupKind = Mips::fixup_Mips_GPOFF_LO;
379 break;
380 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
David Woodhoused2cca112014-01-28 23:13:25 +0000381 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
Mark Seabornc3bd1772013-12-31 13:05:15 +0000382 : Mips::fixup_Mips_GOT_PAGE;
383 break;
384 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
David Woodhoused2cca112014-01-28 23:13:25 +0000385 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
Mark Seabornc3bd1772013-12-31 13:05:15 +0000386 : Mips::fixup_Mips_GOT_OFST;
387 break;
388 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
David Woodhoused2cca112014-01-28 23:13:25 +0000389 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
Mark Seabornc3bd1772013-12-31 13:05:15 +0000390 : Mips::fixup_Mips_GOT_DISP;
391 break;
392 case MCSymbolRefExpr::VK_Mips_GPREL:
393 FixupKind = Mips::fixup_Mips_GPREL16;
394 break;
395 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
David Woodhoused2cca112014-01-28 23:13:25 +0000396 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000397 : Mips::fixup_Mips_CALL16;
398 break;
399 case MCSymbolRefExpr::VK_Mips_GOT16:
David Woodhoused2cca112014-01-28 23:13:25 +0000400 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000401 : Mips::fixup_Mips_GOT_Global;
402 break;
403 case MCSymbolRefExpr::VK_Mips_GOT:
David Woodhoused2cca112014-01-28 23:13:25 +0000404 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000405 : Mips::fixup_Mips_GOT_Local;
406 break;
407 case MCSymbolRefExpr::VK_Mips_ABS_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000408 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000409 : Mips::fixup_Mips_HI16;
410 break;
411 case MCSymbolRefExpr::VK_Mips_ABS_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000412 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000413 : Mips::fixup_Mips_LO16;
414 break;
415 case MCSymbolRefExpr::VK_Mips_TLSGD:
David Woodhoused2cca112014-01-28 23:13:25 +0000416 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
Mark Seabornc3bd1772013-12-31 13:05:15 +0000417 : Mips::fixup_Mips_TLSGD;
418 break;
419 case MCSymbolRefExpr::VK_Mips_TLSLDM:
David Woodhoused2cca112014-01-28 23:13:25 +0000420 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
Mark Seabornc3bd1772013-12-31 13:05:15 +0000421 : Mips::fixup_Mips_TLSLDM;
422 break;
423 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000424 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000425 : Mips::fixup_Mips_DTPREL_HI;
426 break;
427 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000428 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000429 : Mips::fixup_Mips_DTPREL_LO;
430 break;
431 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
432 FixupKind = Mips::fixup_Mips_GOTTPREL;
433 break;
434 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
David Woodhoused2cca112014-01-28 23:13:25 +0000435 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000436 : Mips::fixup_Mips_TPREL_HI;
437 break;
438 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
David Woodhoused2cca112014-01-28 23:13:25 +0000439 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
Mark Seabornc3bd1772013-12-31 13:05:15 +0000440 : Mips::fixup_Mips_TPREL_LO;
441 break;
442 case MCSymbolRefExpr::VK_Mips_HIGHER:
443 FixupKind = Mips::fixup_Mips_HIGHER;
444 break;
445 case MCSymbolRefExpr::VK_Mips_HIGHEST:
446 FixupKind = Mips::fixup_Mips_HIGHEST;
447 break;
448 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
449 FixupKind = Mips::fixup_Mips_GOT_HI16;
450 break;
451 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
452 FixupKind = Mips::fixup_Mips_GOT_LO16;
453 break;
454 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
455 FixupKind = Mips::fixup_Mips_CALL_HI16;
456 break;
457 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
458 FixupKind = Mips::fixup_Mips_CALL_LO16;
459 break;
460 } // switch
Akira Hatanakafe384a22012-03-27 02:33:05 +0000461
Jack Carterb5cf5902013-04-17 00:18:04 +0000462 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
463 return 0;
464 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000465 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000466}
467
Jack Carterb5cf5902013-04-17 00:18:04 +0000468/// getMachineOpValue - Return binary encoding of operand. If the machine
469/// operand requires relocation, record the relocation and return zero.
470unsigned MipsMCCodeEmitter::
471getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000472 SmallVectorImpl<MCFixup> &Fixups,
473 const MCSubtargetInfo &STI) const {
Jack Carterb5cf5902013-04-17 00:18:04 +0000474 if (MO.isReg()) {
475 unsigned Reg = MO.getReg();
Bill Wendlingbc07a892013-06-18 07:20:20 +0000476 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
Jack Carterb5cf5902013-04-17 00:18:04 +0000477 return RegNo;
478 } else if (MO.isImm()) {
479 return static_cast<unsigned>(MO.getImm());
480 } else if (MO.isFPImm()) {
481 return static_cast<unsigned>(APFloat(MO.getFPImm())
482 .bitcastToAPInt().getHiBits(32).getLimitedValue());
483 }
484 // MO must be an Expr.
485 assert(MO.isExpr());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000486 return getExprOpValue(MO.getExpr(),Fixups, STI);
Jack Carterb5cf5902013-04-17 00:18:04 +0000487}
488
Matheus Almeida6b59c442013-12-05 11:06:22 +0000489/// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
490/// instructions.
491unsigned
492MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000493 SmallVectorImpl<MCFixup> &Fixups,
494 const MCSubtargetInfo &STI) const {
Matheus Almeida6b59c442013-12-05 11:06:22 +0000495 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
496 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000497 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
498 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Matheus Almeida6b59c442013-12-05 11:06:22 +0000499
500 // The immediate field of an LD/ST instruction is scaled which means it must
501 // be divided (when encoding) by the size (in bytes) of the instructions'
502 // data format.
503 // .b - 1 byte
504 // .h - 2 bytes
505 // .w - 4 bytes
506 // .d - 8 bytes
507 switch(MI.getOpcode())
508 {
509 default:
510 assert (0 && "Unexpected instruction");
511 break;
512 case Mips::LD_B:
513 case Mips::ST_B:
514 // We don't need to scale the offset in this case
515 break;
516 case Mips::LD_H:
517 case Mips::ST_H:
518 OffBits >>= 1;
519 break;
520 case Mips::LD_W:
521 case Mips::ST_W:
522 OffBits >>= 2;
523 break;
524 case Mips::LD_D:
525 case Mips::ST_D:
526 OffBits >>= 3;
527 break;
528 }
529
530 return (OffBits & 0xFFFF) | RegBits;
531}
532
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000533/// getMemEncoding - Return binary encoding of memory related operand.
534/// If the offset operand requires relocation, record the relocation.
535unsigned
536MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000537 SmallVectorImpl<MCFixup> &Fixups,
538 const MCSubtargetInfo &STI) const {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000539 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
540 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000541 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
542 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000543
544 return (OffBits & 0xFFFF) | RegBits;
545}
546
Jack Carter97700972013-08-13 20:19:16 +0000547unsigned MipsMCCodeEmitter::
548getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000549 SmallVectorImpl<MCFixup> &Fixups,
550 const MCSubtargetInfo &STI) const {
Jack Carter97700972013-08-13 20:19:16 +0000551 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
552 assert(MI.getOperand(OpNo).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000553 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
554 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
Jack Carter97700972013-08-13 20:19:16 +0000555
556 return (OffBits & 0x0FFF) | RegBits;
557}
558
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000559unsigned
560MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000561 SmallVectorImpl<MCFixup> &Fixups,
562 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000563 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000564 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000565 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000566}
567
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000568// FIXME: should be called getMSBEncoding
569//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000570unsigned
571MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000572 SmallVectorImpl<MCFixup> &Fixups,
573 const MCSubtargetInfo &STI) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000574 assert(MI.getOperand(OpNo-1).isImm());
575 assert(MI.getOperand(OpNo).isImm());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000576 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
577 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000578
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000579 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000580}
581
Matheus Almeida779c5932013-11-18 12:32:49 +0000582unsigned
583MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000584 SmallVectorImpl<MCFixup> &Fixups,
585 const MCSubtargetInfo &STI) const {
Matheus Almeida779c5932013-11-18 12:32:49 +0000586 assert(MI.getOperand(OpNo).isImm());
587 // The immediate is encoded as 'immediate - 1'.
David Woodhouse3fa98a62014-01-28 23:13:18 +0000588 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
Matheus Almeida779c5932013-11-18 12:32:49 +0000589}
590
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000591unsigned
592MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
593 SmallVectorImpl<MCFixup> &Fixups,
594 const MCSubtargetInfo &STI) const {
595 assert(MI.getOperand(OpNo).isImm());
596 // The immediate is encoded as 'immediate << 2'.
597 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
598 assert((Res & 3) == 0);
599 return Res >> 2;
600}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000601
Daniel Sandersb59e1a42014-05-15 10:45:58 +0000602#include "MipsGenMCCodeEmitter.inc"