blob: 7fbdae02f4119f1c8b550899325961ac85b9efd7 [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"
22#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
29using namespace llvm;
30
31namespace {
32class MipsMCCodeEmitter : public MCCodeEmitter {
Craig Topper2ed23ce2012-09-15 17:08:51 +000033 MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34 void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000035 const MCInstrInfo &MCII;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000036 bool IsLittleEndian;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000037
38public:
Craig Topper2ed23ce2012-09-15 17:08:51 +000039 MipsMCCodeEmitter(const MCInstrInfo &mcii, bool IsLittle) :
40 MCII(mcii), IsLittleEndian(IsLittle) {}
Akira Hatanaka750ecec2011-09-30 20:40:03 +000041
42 ~MipsMCCodeEmitter() {}
43
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000044 void EmitByte(unsigned char C, raw_ostream &OS) const {
45 OS << (char)C;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000046 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000047
48 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
49 // Output the instruction encoding in little endian byte order.
Akira Hatanaka0137dfe2012-03-21 00:52:01 +000050 for (unsigned i = 0; i < Size; ++i) {
51 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
52 EmitByte((Val >> Shift) & 0xff, OS);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000053 }
54 }
55
56 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
57 SmallVectorImpl<MCFixup> &Fixups) const;
58
59 // getBinaryCodeForInstr - TableGen'erated function for getting the
60 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000061 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000062 SmallVectorImpl<MCFixup> &Fixups) const;
63
64 // getBranchJumpOpValue - Return binary encoding of the jump
65 // target operand. If the machine operand requires relocation,
66 // record the relocation and return zero.
67 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
68 SmallVectorImpl<MCFixup> &Fixups) const;
69
70 // getBranchTargetOpValue - Return binary encoding of the branch
71 // target operand. If the machine operand requires relocation,
72 // record the relocation and return zero.
73 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
74 SmallVectorImpl<MCFixup> &Fixups) const;
75
76 // getMachineOpValue - Return binary encoding of operand. If the machin
77 // operand requires relocation, record the relocation and return zero.
78 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
79 SmallVectorImpl<MCFixup> &Fixups) const;
80
81 unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83 unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87
Akira Hatanaka750ecec2011-09-30 20:40:03 +000088}; // class MipsMCCodeEmitter
89} // namespace
90
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000091MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000092 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000093 const MCSubtargetInfo &STI,
94 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000095{
Craig Topper2ed23ce2012-09-15 17:08:51 +000096 return new MipsMCCodeEmitter(MCII, false);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000097}
98
99MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000100 const MCRegisterInfo &MRI,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000101 const MCSubtargetInfo &STI,
102 MCContext &Ctx)
103{
Craig Topper2ed23ce2012-09-15 17:08:51 +0000104 return new MipsMCCodeEmitter(MCII, true);
Akira Hatanaka750ecec2011-09-30 20:40:03 +0000105}
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000106
107/// EncodeInstruction - Emit the instruction.
108/// Size the instruction (currently only 4 bytes
109void MipsMCCodeEmitter::
110EncodeInstruction(const MCInst &MI, raw_ostream &OS,
111 SmallVectorImpl<MCFixup> &Fixups) const
112{
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000113
114 // Non-pseudo instructions that get changed for direct object
115 // only based on operand values.
116 // If this list of instructions get much longer we will move
117 // the check to a function call. Until then, this is more efficient.
118 MCInst TmpInst = MI;
119 switch (MI.getOpcode()) {
120 // If shift amount is >= 32 it the inst needs to be lowered further
121 case Mips::DSLL:
122 case Mips::DSRL:
123 case Mips::DSRA:
124 Mips::LowerLargeShift(TmpInst);
125 break;
126 // Double extract instruction is chosen by pos and size operands
127 case Mips::DEXT:
128 case Mips::DINS:
129 Mips::LowerDextDins(TmpInst);
130 }
131
132 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000133
134 // Check for unimplemented opcodes.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000135 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000136 // so we have to special check for them.
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000137 unsigned Opcode = TmpInst.getOpcode();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000138 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
139 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
140
Jack Carteraa7aeaa2012-10-02 23:09:40 +0000141 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000142 uint64_t TSFlags = Desc.TSFlags;
143
144 // Pseudo instructions don't get encoded and shouldn't be here
145 // in the first place!
146 if ((TSFlags & MipsII::FormMask) == MipsII::Pseudo)
147 llvm_unreachable("Pseudo opcode found in EncodeInstruction()");
148
Jack Carter5b5559d2012-10-03 21:58:54 +0000149 // Get byte count of instruction
150 unsigned Size = Desc.getSize();
151 if (!Size)
152 llvm_unreachable("Desc.getSize() returns 0");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000153
154 EmitInstruction(Binary, Size, OS);
155}
156
157/// getBranchTargetOpValue - Return binary encoding of the branch
158/// target operand. If the machine operand requires relocation,
159/// record the relocation and return zero.
160unsigned MipsMCCodeEmitter::
161getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
162 SmallVectorImpl<MCFixup> &Fixups) const {
163
164 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000165
166 // If the destination is an immediate, we have nothing to do.
167 if (MO.isImm()) return MO.getImm();
168 assert(MO.isExpr() &&
169 "getBranchTargetOpValue expects only expressions or immediates");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000170
171 const MCExpr *Expr = MO.getExpr();
172 Fixups.push_back(MCFixup::Create(0, Expr,
173 MCFixupKind(Mips::fixup_Mips_PC16)));
174 return 0;
175}
176
177/// getJumpTargetOpValue - Return binary encoding of the jump
178/// target operand. If the machine operand requires relocation,
179/// record the relocation and return zero.
180unsigned MipsMCCodeEmitter::
181getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
182 SmallVectorImpl<MCFixup> &Fixups) const {
183
184 const MCOperand &MO = MI.getOperand(OpNo);
Jack Carter71e6a742012-09-06 00:43:26 +0000185 // If the destination is an immediate, we have nothing to do.
186 if (MO.isImm()) return MO.getImm();
187 assert(MO.isExpr() &&
188 "getJumpTargetOpValue expects only expressions or an immediate");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000189
190 const MCExpr *Expr = MO.getExpr();
191 Fixups.push_back(MCFixup::Create(0, Expr,
192 MCFixupKind(Mips::fixup_Mips_26)));
193 return 0;
194}
195
196/// getMachineOpValue - Return binary encoding of operand. If the machine
197/// operand requires relocation, record the relocation and return zero.
198unsigned MipsMCCodeEmitter::
199getMachineOpValue(const MCInst &MI, const MCOperand &MO,
200 SmallVectorImpl<MCFixup> &Fixups) const {
201 if (MO.isReg()) {
202 unsigned Reg = MO.getReg();
203 unsigned RegNo = getMipsRegisterNumbering(Reg);
204 return RegNo;
205 } else if (MO.isImm()) {
206 return static_cast<unsigned>(MO.getImm());
207 } else if (MO.isFPImm()) {
208 return static_cast<unsigned>(APFloat(MO.getFPImm())
209 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000210 }
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000211
Akira Hatanakafe384a22012-03-27 02:33:05 +0000212 // MO must be an Expr.
213 assert(MO.isExpr());
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000214
Akira Hatanakafe384a22012-03-27 02:33:05 +0000215 const MCExpr *Expr = MO.getExpr();
216 MCExpr::ExprKind Kind = Expr->getKind();
Akira Hatanakae2eed962011-12-22 01:05:17 +0000217
Akira Hatanakafe384a22012-03-27 02:33:05 +0000218 if (Kind == MCExpr::Binary) {
219 Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
220 Kind = Expr->getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000221 }
Akira Hatanakafe384a22012-03-27 02:33:05 +0000222
223 assert (Kind == MCExpr::SymbolRef);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000224
Bill Wendlingf9774c32012-04-22 07:23:04 +0000225 Mips::Fixups FixupKind = Mips::Fixups(0);
Akira Hatanakafe384a22012-03-27 02:33:05 +0000226
227 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
Jack Carterb9f9de92012-06-27 22:48:25 +0000228 default: llvm_unreachable("Unknown fixup kind!");
229 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000230 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
231 FixupKind = Mips::fixup_Mips_GPOFF_HI;
232 break;
233 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
234 FixupKind = Mips::fixup_Mips_GPOFF_LO;
235 break;
236 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
237 FixupKind = Mips::fixup_Mips_GOT_PAGE;
238 break;
239 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
240 FixupKind = Mips::fixup_Mips_GOT_OFST;
241 break;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000242 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
243 FixupKind = Mips::fixup_Mips_GOT_DISP;
244 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000245 case MCSymbolRefExpr::VK_Mips_GPREL:
246 FixupKind = Mips::fixup_Mips_GPREL16;
247 break;
248 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
249 FixupKind = Mips::fixup_Mips_CALL16;
250 break;
251 case MCSymbolRefExpr::VK_Mips_GOT16:
252 FixupKind = Mips::fixup_Mips_GOT_Global;
253 break;
254 case MCSymbolRefExpr::VK_Mips_GOT:
255 FixupKind = Mips::fixup_Mips_GOT_Local;
256 break;
257 case MCSymbolRefExpr::VK_Mips_ABS_HI:
258 FixupKind = Mips::fixup_Mips_HI16;
259 break;
260 case MCSymbolRefExpr::VK_Mips_ABS_LO:
261 FixupKind = Mips::fixup_Mips_LO16;
262 break;
263 case MCSymbolRefExpr::VK_Mips_TLSGD:
264 FixupKind = Mips::fixup_Mips_TLSGD;
265 break;
266 case MCSymbolRefExpr::VK_Mips_TLSLDM:
267 FixupKind = Mips::fixup_Mips_TLSLDM;
268 break;
269 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
270 FixupKind = Mips::fixup_Mips_DTPREL_HI;
271 break;
272 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
273 FixupKind = Mips::fixup_Mips_DTPREL_LO;
274 break;
275 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
276 FixupKind = Mips::fixup_Mips_GOTTPREL;
277 break;
278 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
279 FixupKind = Mips::fixup_Mips_TPREL_HI;
280 break;
281 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
282 FixupKind = Mips::fixup_Mips_TPREL_LO;
283 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000284 case MCSymbolRefExpr::VK_Mips_HIGHER:
285 FixupKind = Mips::fixup_Mips_HIGHER;
286 break;
287 case MCSymbolRefExpr::VK_Mips_HIGHEST:
288 FixupKind = Mips::fixup_Mips_HIGHEST;
289 break;
Akira Hatanakafe384a22012-03-27 02:33:05 +0000290 } // switch
291
292 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
293
294 // All of the information is in the fixup.
295 return 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000296}
297
298/// getMemEncoding - Return binary encoding of memory related operand.
299/// If the offset operand requires relocation, record the relocation.
300unsigned
301MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
302 SmallVectorImpl<MCFixup> &Fixups) const {
303 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
304 assert(MI.getOperand(OpNo).isReg());
305 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
306 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
307
308 return (OffBits & 0xFFFF) | RegBits;
309}
310
311unsigned
312MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
313 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000314 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000315 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
316 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000317}
318
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000319// FIXME: should be called getMSBEncoding
320//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000321unsigned
322MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
323 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000324 assert(MI.getOperand(OpNo-1).isImm());
325 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000326 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
327 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000328
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000329 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000330}
331
332#include "MipsGenMCCodeEmitter.inc"
333