blob: e198a7c983f0448c690640fab67340fcc186a67e [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"
Jack Carteraa7aeaa2012-10-02 23:09:40 +000016#include "MCTargetDesc/MipsDirectObjLower.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000017#include "MCTargetDesc/MipsFixupKinds.h"
18#include "MCTargetDesc/MipsMCTargetDesc.h"
19#include "llvm/ADT/APFloat.h"
20#include "llvm/ADT/Statistic.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000021#include "llvm/MC/MCCodeEmitter.h"
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000022#include "llvm/MC/MCContext.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000023#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCRegisterInfo.h"
27#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
30using namespace llvm;
31
32namespace {
33class MipsMCCodeEmitter : public MCCodeEmitter {
Craig Topper2ed23ce2012-09-15 17:08:51 +000034 MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35 void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000036 const MCInstrInfo &MCII;
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000037 MCContext &Ctx;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000038 bool IsLittleEndian;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000039
40public:
Jack Carterab3cb422013-02-19 22:04:37 +000041 MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_,
42 const MCSubtargetInfo &sti, bool IsLittle) :
David Blaikie725fda12013-02-20 07:39:18 +000043 MCII(mcii), Ctx(Ctx_), IsLittleEndian(IsLittle) {}
Akira Hatanaka750ecec2011-09-30 20:40:03 +000044
45 ~MipsMCCodeEmitter() {}
46
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000047 void EmitByte(unsigned char C, raw_ostream &OS) const {
48 OS << (char)C;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000049 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000050
51 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
52 // Output the instruction encoding in little endian byte order.
Akira Hatanaka0137dfe2012-03-21 00:52:01 +000053 for (unsigned i = 0; i < Size; ++i) {
54 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
55 EmitByte((Val >> Shift) & 0xff, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000056 }
57 }
58
59 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
60 SmallVectorImpl<MCFixup> &Fixups) const;
61
62 // getBinaryCodeForInstr - TableGen'erated function for getting the
63 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000064 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000065 SmallVectorImpl<MCFixup> &Fixups) const;
66
67 // getBranchJumpOpValue - Return binary encoding of the jump
68 // target operand. If the machine operand requires relocation,
69 // record the relocation and return zero.
70 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixups) const;
72
73 // getBranchTargetOpValue - Return binary encoding of the branch
74 // target operand. If the machine operand requires relocation,
75 // record the relocation and return zero.
76 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
77 SmallVectorImpl<MCFixup> &Fixups) const;
78
79 // getMachineOpValue - Return binary encoding of operand. If the machin
80 // operand requires relocation, record the relocation and return zero.
81 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83
84 unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
85 SmallVectorImpl<MCFixup> &Fixups) const;
86 unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
87 SmallVectorImpl<MCFixup> &Fixups) const;
88 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
89 SmallVectorImpl<MCFixup> &Fixups) const;
90
Akira Hatanaka750ecec2011-09-30 20:40:03 +000091}; // class MipsMCCodeEmitter
92} // namespace
93
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000094MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000095 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000096 const MCSubtargetInfo &STI,
97 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000098{
Jack Carterab3cb422013-02-19 22:04:37 +000099 return new MipsMCCodeEmitter(MCII, Ctx, STI, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000100}
101
102MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000103 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000104 const MCSubtargetInfo &STI,
105 MCContext &Ctx)
106{
Jack Carterab3cb422013-02-19 22:04:37 +0000107 return new MipsMCCodeEmitter(MCII, Ctx, STI, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000108}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000109
110/// EncodeInstruction - Emit the instruction.
111/// Size the instruction (currently only 4 bytes
112void MipsMCCodeEmitter::
113EncodeInstruction(const MCInst &MI, raw_ostream &OS,
114 SmallVectorImpl<MCFixup> &Fixups) const
115{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000116
117 // Non-pseudo instructions that get changed for direct object
118 // only based on operand values.
119 // If this list of instructions get much longer we will move
120 // the check to a function call. Until then, this is more efficient.
121 MCInst TmpInst = MI;
122 switch (MI.getOpcode()) {
123 // If shift amount is >= 32 it the inst needs to be lowered further
124 case Mips::DSLL:
125 case Mips::DSRL:
126 case Mips::DSRA:
127 Mips::LowerLargeShift(TmpInst);
128 break;
129 // Double extract instruction is chosen by pos and size operands
130 case Mips::DEXT:
131 case Mips::DINS:
132 Mips::LowerDextDins(TmpInst);
133 }
134
135 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000136
137 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000138 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000139 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000140 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000141 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
142 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
143
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000144 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000145
Jack Carter5b5559d2012-10-03 21:58:54 +0000146 // Get byte count of instruction
147 unsigned Size = Desc.getSize();
148 if (!Size)
149 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000150
151 EmitInstruction(Binary, Size, OS);
152}
153
154/// getBranchTargetOpValue - Return binary encoding of the branch
155/// target operand. If the machine operand requires relocation,
156/// record the relocation and return zero.
157unsigned MipsMCCodeEmitter::
158getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
159 SmallVectorImpl<MCFixup> &Fixups) const {
160
161 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000162
Jack Carter4f69a0f2013-03-22 00:29:10 +0000163 // If the destination is an immediate, divide by 4.
164 if (MO.isImm()) return MO.getImm() >> 2;
165
Jack Carter71e6a742012-09-06 00:43:26 +0000166 assert(MO.isExpr() &&
167 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000168
169 const MCExpr *Expr = MO.getExpr();
170 Fixups.push_back(MCFixup::Create(0, Expr,
171 MCFixupKind(Mips::fixup_Mips_PC16)));
172 return 0;
173}
174
175/// getJumpTargetOpValue - Return binary encoding of the jump
176/// target operand. If the machine operand requires relocation,
177/// record the relocation and return zero.
178unsigned MipsMCCodeEmitter::
179getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
180 SmallVectorImpl<MCFixup> &Fixups) const {
181
182 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter4f69a0f2013-03-22 00:29:10 +0000183 // If the destination is an immediate, divide by 4.
184 if (MO.isImm()) return MO.getImm()>>2;
185
Jack Carter71e6a742012-09-06 00:43:26 +0000186 assert(MO.isExpr() &&
187 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000188
189 const MCExpr *Expr = MO.getExpr();
190 Fixups.push_back(MCFixup::Create(0, Expr,
191 MCFixupKind(Mips::fixup_Mips_26)));
192 return 0;
193}
194
195/// getMachineOpValue - Return binary encoding of operand. If the machine
196/// operand requires relocation, record the relocation and return zero.
197unsigned MipsMCCodeEmitter::
198getMachineOpValue(const MCInst &MI, const MCOperand &MO,
199 SmallVectorImpl<MCFixup> &Fixups) const {
200 if (MO.isReg()) {
201 unsigned Reg = MO.getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000202 unsigned RegNo = Ctx.getRegisterInfo().getEncodingValue(Reg);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000203 return RegNo;
204 } else if (MO.isImm()) {
205 return static_cast<unsigned>(MO.getImm());
206 } else if (MO.isFPImm()) {
207 return static_cast<unsigned>(APFloat(MO.getFPImm())
208 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000209 }
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000210
Akira Hatanakafe384a22012-03-27 02:33:05 +0000211 // MO must be an Expr.
212 assert(MO.isExpr());
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000213
Akira Hatanakafe384a22012-03-27 02:33:05 +0000214 const MCExpr *Expr = MO.getExpr();
215 MCExpr::ExprKind Kind = Expr->getKind();
Akira Hatanakae2eed962011-12-22 01:05:17 +0000216
Akira Hatanakafe384a22012-03-27 02:33:05 +0000217 if (Kind == MCExpr::Binary) {
218 Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
219 Kind = Expr->getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000220 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000221
222 assert (Kind == MCExpr::SymbolRef);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000223
Bill Wendlingf9774c32012-04-22 07:23:04 +0000224 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000225
226 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
Jack Carterb9f9de92012-06-27 22:48:25 +0000227 default: llvm_unreachable("Unknown fixup kind!");
228 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000229 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
230 FixupKind = Mips::fixup_Mips_GPOFF_HI;
231 break;
232 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
233 FixupKind = Mips::fixup_Mips_GPOFF_LO;
234 break;
235 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
236 FixupKind = Mips::fixup_Mips_GOT_PAGE;
237 break;
238 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
239 FixupKind = Mips::fixup_Mips_GOT_OFST;
240 break;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000241 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
242 FixupKind = Mips::fixup_Mips_GOT_DISP;
243 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000244 case MCSymbolRefExpr::VK_Mips_GPREL:
245 FixupKind = Mips::fixup_Mips_GPREL16;
246 break;
247 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
248 FixupKind = Mips::fixup_Mips_CALL16;
249 break;
250 case MCSymbolRefExpr::VK_Mips_GOT16:
251 FixupKind = Mips::fixup_Mips_GOT_Global;
252 break;
253 case MCSymbolRefExpr::VK_Mips_GOT:
254 FixupKind = Mips::fixup_Mips_GOT_Local;
255 break;
256 case MCSymbolRefExpr::VK_Mips_ABS_HI:
257 FixupKind = Mips::fixup_Mips_HI16;
258 break;
259 case MCSymbolRefExpr::VK_Mips_ABS_LO:
260 FixupKind = Mips::fixup_Mips_LO16;
261 break;
262 case MCSymbolRefExpr::VK_Mips_TLSGD:
263 FixupKind = Mips::fixup_Mips_TLSGD;
264 break;
265 case MCSymbolRefExpr::VK_Mips_TLSLDM:
266 FixupKind = Mips::fixup_Mips_TLSLDM;
267 break;
268 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
269 FixupKind = Mips::fixup_Mips_DTPREL_HI;
270 break;
271 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
272 FixupKind = Mips::fixup_Mips_DTPREL_LO;
273 break;
274 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
275 FixupKind = Mips::fixup_Mips_GOTTPREL;
276 break;
277 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
278 FixupKind = Mips::fixup_Mips_TPREL_HI;
279 break;
280 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
281 FixupKind = Mips::fixup_Mips_TPREL_LO;
282 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000283 case MCSymbolRefExpr::VK_Mips_HIGHER:
284 FixupKind = Mips::fixup_Mips_HIGHER;
285 break;
286 case MCSymbolRefExpr::VK_Mips_HIGHEST:
287 FixupKind = Mips::fixup_Mips_HIGHEST;
288 break;
Jack Carterb05cb672012-11-21 23:38:59 +0000289 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
290 FixupKind = Mips::fixup_Mips_GOT_HI16;
291 break;
292 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
293 FixupKind = Mips::fixup_Mips_GOT_LO16;
294 break;
295 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
296 FixupKind = Mips::fixup_Mips_CALL_HI16;
297 break;
298 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
299 FixupKind = Mips::fixup_Mips_CALL_LO16;
300 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000301 } // switch
302
303 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
304
305 // All of the information is in the fixup.
306 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000307}
308
309/// getMemEncoding - Return binary encoding of memory related operand.
310/// If the offset operand requires relocation, record the relocation.
311unsigned
312MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
313 SmallVectorImpl<MCFixup> &Fixups) const {
314 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
315 assert(MI.getOperand(OpNo).isReg());
316 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
317 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
318
319 return (OffBits & 0xFFFF) | RegBits;
320}
321
322unsigned
323MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
324 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000325 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000326 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
327 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000328}
329
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000330// FIXME: should be called getMSBEncoding
331//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000332unsigned
333MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
334 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000335 assert(MI.getOperand(OpNo-1).isImm());
336 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000337 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
338 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000339
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000340 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000341}
342
343#include "MipsGenMCCodeEmitter.inc"
344