blob: ff3fd906fd9084ba9d2445a69559da29e8784019 [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;
Jack Carterab3cb422013-02-19 22:04:37 +000038 const MCSubtargetInfo &STI;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000039 bool IsLittleEndian;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000040
41public:
Jack Carterab3cb422013-02-19 22:04:37 +000042 MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_,
43 const MCSubtargetInfo &sti, bool IsLittle) :
44 MCII(mcii), Ctx(Ctx_), STI (sti), IsLittleEndian(IsLittle) {}
Akira Hatanaka750ecec2011-09-30 20:40:03 +000045
46 ~MipsMCCodeEmitter() {}
47
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000048 void EmitByte(unsigned char C, raw_ostream &OS) const {
49 OS << (char)C;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000050 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000051
52 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
53 // Output the instruction encoding in little endian byte order.
Akira Hatanaka0137dfe2012-03-21 00:52:01 +000054 for (unsigned i = 0; i < Size; ++i) {
55 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
56 EmitByte((Val >> Shift) & 0xff, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000057 }
58 }
59
60 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
61 SmallVectorImpl<MCFixup> &Fixups) const;
62
63 // getBinaryCodeForInstr - TableGen'erated function for getting the
64 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000065 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000066 SmallVectorImpl<MCFixup> &Fixups) const;
67
68 // getBranchJumpOpValue - Return binary encoding of the jump
69 // target operand. If the machine operand requires relocation,
70 // record the relocation and return zero.
71 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups) const;
73
74 // getBranchTargetOpValue - Return binary encoding of the branch
75 // target operand. If the machine operand requires relocation,
76 // record the relocation and return zero.
77 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
78 SmallVectorImpl<MCFixup> &Fixups) const;
79
80 // getMachineOpValue - Return binary encoding of operand. If the machin
81 // operand requires relocation, record the relocation and return zero.
82 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
83 SmallVectorImpl<MCFixup> &Fixups) const;
84
85 unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87 unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups) const;
89 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
90 SmallVectorImpl<MCFixup> &Fixups) const;
91
Akira Hatanaka750ecec2011-09-30 20:40:03 +000092}; // class MipsMCCodeEmitter
93} // namespace
94
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000095MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000096 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000097 const MCSubtargetInfo &STI,
98 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000099{
Jack Carterab3cb422013-02-19 22:04:37 +0000100 return new MipsMCCodeEmitter(MCII, Ctx, STI, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000101}
102
103MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000104 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000105 const MCSubtargetInfo &STI,
106 MCContext &Ctx)
107{
Jack Carterab3cb422013-02-19 22:04:37 +0000108 return new MipsMCCodeEmitter(MCII, Ctx, STI, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000109}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000110
111/// EncodeInstruction - Emit the instruction.
112/// Size the instruction (currently only 4 bytes
113void MipsMCCodeEmitter::
114EncodeInstruction(const MCInst &MI, raw_ostream &OS,
115 SmallVectorImpl<MCFixup> &Fixups) const
116{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000117
118 // Non-pseudo instructions that get changed for direct object
119 // only based on operand values.
120 // If this list of instructions get much longer we will move
121 // the check to a function call. Until then, this is more efficient.
122 MCInst TmpInst = MI;
123 switch (MI.getOpcode()) {
124 // If shift amount is >= 32 it the inst needs to be lowered further
125 case Mips::DSLL:
126 case Mips::DSRL:
127 case Mips::DSRA:
128 Mips::LowerLargeShift(TmpInst);
129 break;
130 // Double extract instruction is chosen by pos and size operands
131 case Mips::DEXT:
132 case Mips::DINS:
133 Mips::LowerDextDins(TmpInst);
134 }
135
136 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000137
138 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000139 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000140 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000141 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000142 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
143 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
144
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000145 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000146
Jack Carter5b5559d2012-10-03 21:58:54 +0000147 // Get byte count of instruction
148 unsigned Size = Desc.getSize();
149 if (!Size)
150 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000151
152 EmitInstruction(Binary, Size, OS);
153}
154
155/// getBranchTargetOpValue - Return binary encoding of the branch
156/// target operand. If the machine operand requires relocation,
157/// record the relocation and return zero.
158unsigned MipsMCCodeEmitter::
159getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
160 SmallVectorImpl<MCFixup> &Fixups) const {
161
162 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000163
164 // If the destination is an immediate, we have nothing to do.
165 if (MO.isImm()) return MO.getImm();
166 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 Carter71e6a742012-09-06 00:43:26 +0000183 // If the destination is an immediate, we have nothing to do.
184 if (MO.isImm()) return MO.getImm();
185 assert(MO.isExpr() &&
186 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000187
188 const MCExpr *Expr = MO.getExpr();
189 Fixups.push_back(MCFixup::Create(0, Expr,
190 MCFixupKind(Mips::fixup_Mips_26)));
191 return 0;
192}
193
194/// getMachineOpValue - Return binary encoding of operand. If the machine
195/// operand requires relocation, record the relocation and return zero.
196unsigned MipsMCCodeEmitter::
197getMachineOpValue(const MCInst &MI, const MCOperand &MO,
198 SmallVectorImpl<MCFixup> &Fixups) const {
199 if (MO.isReg()) {
200 unsigned Reg = MO.getReg();
Akira Hatanaka5d6faed2012-12-10 20:04:40 +0000201 unsigned RegNo = Ctx.getRegisterInfo().getEncodingValue(Reg);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000202 return RegNo;
203 } else if (MO.isImm()) {
204 return static_cast<unsigned>(MO.getImm());
205 } else if (MO.isFPImm()) {
206 return static_cast<unsigned>(APFloat(MO.getFPImm())
207 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000208 }
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000209
Akira Hatanakafe384a22012-03-27 02:33:05 +0000210 // MO must be an Expr.
211 assert(MO.isExpr());
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000212
Akira Hatanakafe384a22012-03-27 02:33:05 +0000213 const MCExpr *Expr = MO.getExpr();
214 MCExpr::ExprKind Kind = Expr->getKind();
Akira Hatanakae2eed962011-12-22 01:05:17 +0000215
Akira Hatanakafe384a22012-03-27 02:33:05 +0000216 if (Kind == MCExpr::Binary) {
217 Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
218 Kind = Expr->getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000219 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000220
221 assert (Kind == MCExpr::SymbolRef);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000222
Bill Wendlingf9774c32012-04-22 07:23:04 +0000223 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000224
225 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
Jack Carterb9f9de92012-06-27 22:48:25 +0000226 default: llvm_unreachable("Unknown fixup kind!");
227 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000228 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
229 FixupKind = Mips::fixup_Mips_GPOFF_HI;
230 break;
231 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
232 FixupKind = Mips::fixup_Mips_GPOFF_LO;
233 break;
234 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
235 FixupKind = Mips::fixup_Mips_GOT_PAGE;
236 break;
237 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
238 FixupKind = Mips::fixup_Mips_GOT_OFST;
239 break;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000240 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
241 FixupKind = Mips::fixup_Mips_GOT_DISP;
242 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000243 case MCSymbolRefExpr::VK_Mips_GPREL:
244 FixupKind = Mips::fixup_Mips_GPREL16;
245 break;
246 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
247 FixupKind = Mips::fixup_Mips_CALL16;
248 break;
249 case MCSymbolRefExpr::VK_Mips_GOT16:
250 FixupKind = Mips::fixup_Mips_GOT_Global;
251 break;
252 case MCSymbolRefExpr::VK_Mips_GOT:
253 FixupKind = Mips::fixup_Mips_GOT_Local;
254 break;
255 case MCSymbolRefExpr::VK_Mips_ABS_HI:
256 FixupKind = Mips::fixup_Mips_HI16;
257 break;
258 case MCSymbolRefExpr::VK_Mips_ABS_LO:
259 FixupKind = Mips::fixup_Mips_LO16;
260 break;
261 case MCSymbolRefExpr::VK_Mips_TLSGD:
262 FixupKind = Mips::fixup_Mips_TLSGD;
263 break;
264 case MCSymbolRefExpr::VK_Mips_TLSLDM:
265 FixupKind = Mips::fixup_Mips_TLSLDM;
266 break;
267 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
268 FixupKind = Mips::fixup_Mips_DTPREL_HI;
269 break;
270 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
271 FixupKind = Mips::fixup_Mips_DTPREL_LO;
272 break;
273 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
274 FixupKind = Mips::fixup_Mips_GOTTPREL;
275 break;
276 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
277 FixupKind = Mips::fixup_Mips_TPREL_HI;
278 break;
279 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
280 FixupKind = Mips::fixup_Mips_TPREL_LO;
281 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000282 case MCSymbolRefExpr::VK_Mips_HIGHER:
283 FixupKind = Mips::fixup_Mips_HIGHER;
284 break;
285 case MCSymbolRefExpr::VK_Mips_HIGHEST:
286 FixupKind = Mips::fixup_Mips_HIGHEST;
287 break;
Jack Carterb05cb672012-11-21 23:38:59 +0000288 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
289 FixupKind = Mips::fixup_Mips_GOT_HI16;
290 break;
291 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
292 FixupKind = Mips::fixup_Mips_GOT_LO16;
293 break;
294 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
295 FixupKind = Mips::fixup_Mips_CALL_HI16;
296 break;
297 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
298 FixupKind = Mips::fixup_Mips_CALL_LO16;
299 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000300 } // switch
301
302 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
303
304 // All of the information is in the fixup.
305 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000306}
307
308/// getMemEncoding - Return binary encoding of memory related operand.
309/// If the offset operand requires relocation, record the relocation.
310unsigned
311MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
312 SmallVectorImpl<MCFixup> &Fixups) const {
313 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
314 assert(MI.getOperand(OpNo).isReg());
315 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
316 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
317
318 return (OffBits & 0xFFFF) | RegBits;
319}
320
321unsigned
322MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
323 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000324 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000325 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
326 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000327}
328
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000329// FIXME: should be called getMSBEncoding
330//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000331unsigned
332MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
333 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000334 assert(MI.getOperand(OpNo-1).isImm());
335 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000336 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
337 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000338
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000339 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000340}
341
342#include "MipsGenMCCodeEmitter.inc"
343