blob: 9f2d1e4fe59e758a9aa53ed5a8087706d2383e36 [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:
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000041 MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_, bool IsLittle) :
42 MCII(mcii), Ctx(Ctx_), IsLittleEndian(IsLittle) {}
Akira Hatanaka750ecec2011-09-30 20:40:03 +000043
44 ~MipsMCCodeEmitter() {}
45
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000046 void EmitByte(unsigned char C, raw_ostream &OS) const {
47 OS << (char)C;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000048 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000049
50 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
51 // Output the instruction encoding in little endian byte order.
Akira Hatanaka0137dfe2012-03-21 00:52:01 +000052 for (unsigned i = 0; i < Size; ++i) {
53 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
54 EmitByte((Val >> Shift) & 0xff, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000055 }
56 }
57
58 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
59 SmallVectorImpl<MCFixup> &Fixups) const;
60
61 // getBinaryCodeForInstr - TableGen'erated function for getting the
62 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000063 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000064 SmallVectorImpl<MCFixup> &Fixups) const;
65
66 // getBranchJumpOpValue - Return binary encoding of the jump
67 // target operand. If the machine operand requires relocation,
68 // record the relocation and return zero.
69 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups) const;
71
72 // getBranchTargetOpValue - Return binary encoding of the branch
73 // target operand. If the machine operand requires relocation,
74 // record the relocation and return zero.
75 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups) const;
77
78 // getMachineOpValue - Return binary encoding of operand. If the machin
79 // operand requires relocation, record the relocation and return zero.
80 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
81 SmallVectorImpl<MCFixup> &Fixups) const;
82
83 unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85 unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups) const;
89
Akira Hatanaka750ecec2011-09-30 20:40:03 +000090}; // class MipsMCCodeEmitter
91} // namespace
92
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000093MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000094 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000095 const MCSubtargetInfo &STI,
96 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000097{
Akira Hatanaka5d6faed2012-12-10 20:04:40 +000098 return new MipsMCCodeEmitter(MCII, Ctx, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000099}
100
101MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000102 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000103 const MCSubtargetInfo &STI,
104 MCContext &Ctx)
105{
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000106 return new MipsMCCodeEmitter(MCII, Ctx, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000107}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000108
109/// EncodeInstruction - Emit the instruction.
110/// Size the instruction (currently only 4 bytes
111void MipsMCCodeEmitter::
112EncodeInstruction(const MCInst &MI, raw_ostream &OS,
113 SmallVectorImpl<MCFixup> &Fixups) const
114{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000115
116 // Non-pseudo instructions that get changed for direct object
117 // only based on operand values.
118 // If this list of instructions get much longer we will move
119 // the check to a function call. Until then, this is more efficient.
120 MCInst TmpInst = MI;
121 switch (MI.getOpcode()) {
122 // If shift amount is >= 32 it the inst needs to be lowered further
123 case Mips::DSLL:
124 case Mips::DSRL:
125 case Mips::DSRA:
126 Mips::LowerLargeShift(TmpInst);
127 break;
128 // Double extract instruction is chosen by pos and size operands
129 case Mips::DEXT:
130 case Mips::DINS:
131 Mips::LowerDextDins(TmpInst);
132 }
133
134 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000135
136 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000137 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000138 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000139 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000140 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
141 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
142
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000143 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000144
Jack Carter5b5559d2012-10-03 21:58:54 +0000145 // Get byte count of instruction
146 unsigned Size = Desc.getSize();
147 if (!Size)
148 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000149
150 EmitInstruction(Binary, Size, OS);
151}
152
153/// getBranchTargetOpValue - Return binary encoding of the branch
154/// target operand. If the machine operand requires relocation,
155/// record the relocation and return zero.
156unsigned MipsMCCodeEmitter::
157getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
158 SmallVectorImpl<MCFixup> &Fixups) const {
159
160 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000161
162 // If the destination is an immediate, we have nothing to do.
163 if (MO.isImm()) return MO.getImm();
164 assert(MO.isExpr() &&
165 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000166
167 const MCExpr *Expr = MO.getExpr();
168 Fixups.push_back(MCFixup::Create(0, Expr,
169 MCFixupKind(Mips::fixup_Mips_PC16)));
170 return 0;
171}
172
173/// getJumpTargetOpValue - Return binary encoding of the jump
174/// target operand. If the machine operand requires relocation,
175/// record the relocation and return zero.
176unsigned MipsMCCodeEmitter::
177getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
178 SmallVectorImpl<MCFixup> &Fixups) const {
179
180 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000181 // If the destination is an immediate, we have nothing to do.
182 if (MO.isImm()) return MO.getImm();
183 assert(MO.isExpr() &&
184 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000185
186 const MCExpr *Expr = MO.getExpr();
187 Fixups.push_back(MCFixup::Create(0, Expr,
188 MCFixupKind(Mips::fixup_Mips_26)));
189 return 0;
190}
191
192/// getMachineOpValue - Return binary encoding of operand. If the machine
193/// operand requires relocation, record the relocation and return zero.
194unsigned MipsMCCodeEmitter::
195getMachineOpValue(const MCInst &MI, const MCOperand &MO,
196 SmallVectorImpl<MCFixup> &Fixups) const {
197 if (MO.isReg()) {
198 unsigned Reg = MO.getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000199 unsigned RegNo = Ctx.getRegisterInfo().getEncodingValue(Reg);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000200 return RegNo;
201 } else if (MO.isImm()) {
202 return static_cast<unsigned>(MO.getImm());
203 } else if (MO.isFPImm()) {
204 return static_cast<unsigned>(APFloat(MO.getFPImm())
205 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000206 }
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000207
Akira Hatanakafe384a22012-03-27 02:33:05 +0000208 // MO must be an Expr.
209 assert(MO.isExpr());
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000210
Akira Hatanakafe384a22012-03-27 02:33:05 +0000211 const MCExpr *Expr = MO.getExpr();
212 MCExpr::ExprKind Kind = Expr->getKind();
Akira Hatanakae2eed962011-12-22 01:05:17 +0000213
Akira Hatanakafe384a22012-03-27 02:33:05 +0000214 if (Kind == MCExpr::Binary) {
215 Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
216 Kind = Expr->getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000217 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000218
219 assert (Kind == MCExpr::SymbolRef);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000220
Bill Wendlingf9774c32012-04-22 07:23:04 +0000221 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000222
223 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
Jack Carterb9f9de92012-06-27 22:48:25 +0000224 default: llvm_unreachable("Unknown fixup kind!");
225 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000226 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
227 FixupKind = Mips::fixup_Mips_GPOFF_HI;
228 break;
229 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
230 FixupKind = Mips::fixup_Mips_GPOFF_LO;
231 break;
232 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
233 FixupKind = Mips::fixup_Mips_GOT_PAGE;
234 break;
235 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
236 FixupKind = Mips::fixup_Mips_GOT_OFST;
237 break;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000238 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
239 FixupKind = Mips::fixup_Mips_GOT_DISP;
240 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000241 case MCSymbolRefExpr::VK_Mips_GPREL:
242 FixupKind = Mips::fixup_Mips_GPREL16;
243 break;
244 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
245 FixupKind = Mips::fixup_Mips_CALL16;
246 break;
247 case MCSymbolRefExpr::VK_Mips_GOT16:
248 FixupKind = Mips::fixup_Mips_GOT_Global;
249 break;
250 case MCSymbolRefExpr::VK_Mips_GOT:
251 FixupKind = Mips::fixup_Mips_GOT_Local;
252 break;
253 case MCSymbolRefExpr::VK_Mips_ABS_HI:
254 FixupKind = Mips::fixup_Mips_HI16;
255 break;
256 case MCSymbolRefExpr::VK_Mips_ABS_LO:
257 FixupKind = Mips::fixup_Mips_LO16;
258 break;
259 case MCSymbolRefExpr::VK_Mips_TLSGD:
260 FixupKind = Mips::fixup_Mips_TLSGD;
261 break;
262 case MCSymbolRefExpr::VK_Mips_TLSLDM:
263 FixupKind = Mips::fixup_Mips_TLSLDM;
264 break;
265 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
266 FixupKind = Mips::fixup_Mips_DTPREL_HI;
267 break;
268 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
269 FixupKind = Mips::fixup_Mips_DTPREL_LO;
270 break;
271 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
272 FixupKind = Mips::fixup_Mips_GOTTPREL;
273 break;
274 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
275 FixupKind = Mips::fixup_Mips_TPREL_HI;
276 break;
277 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
278 FixupKind = Mips::fixup_Mips_TPREL_LO;
279 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000280 case MCSymbolRefExpr::VK_Mips_HIGHER:
281 FixupKind = Mips::fixup_Mips_HIGHER;
282 break;
283 case MCSymbolRefExpr::VK_Mips_HIGHEST:
284 FixupKind = Mips::fixup_Mips_HIGHEST;
285 break;
Jack Carterb05cb672012-11-21 23:38:59 +0000286 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
287 FixupKind = Mips::fixup_Mips_GOT_HI16;
288 break;
289 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
290 FixupKind = Mips::fixup_Mips_GOT_LO16;
291 break;
292 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
293 FixupKind = Mips::fixup_Mips_CALL_HI16;
294 break;
295 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
296 FixupKind = Mips::fixup_Mips_CALL_LO16;
297 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000298 } // switch
299
300 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
301
302 // All of the information is in the fixup.
303 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000304}
305
306/// getMemEncoding - Return binary encoding of memory related operand.
307/// If the offset operand requires relocation, record the relocation.
308unsigned
309MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
310 SmallVectorImpl<MCFixup> &Fixups) const {
311 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
312 assert(MI.getOperand(OpNo).isReg());
313 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
314 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
315
316 return (OffBits & 0xFFFF) | RegBits;
317}
318
319unsigned
320MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
321 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000322 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000323 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
324 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000325}
326
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000327// FIXME: should be called getMSBEncoding
328//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000329unsigned
330MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
331 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000332 assert(MI.getOperand(OpNo-1).isImm());
333 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000334 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
335 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000336
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000337 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000338}
339
340#include "MipsGenMCCodeEmitter.inc"
341