blob: b039678175032607de0b6f771a94ddac53e745a5 [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"
16#include "MCTargetDesc/MipsFixupKinds.h"
17#include "MCTargetDesc/MipsMCTargetDesc.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/Statistic.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000026#include "llvm/Support/raw_ostream.h"
Akira Hatanaka750ecec2011-09-30 20:40:03 +000027
28using namespace llvm;
29
30namespace {
31class MipsMCCodeEmitter : public MCCodeEmitter {
32 MipsMCCodeEmitter(const MipsMCCodeEmitter &); // DO NOT IMPLEMENT
33 void operator=(const MipsMCCodeEmitter &); // DO NOT IMPLEMENT
34 const MCInstrInfo &MCII;
35 const MCSubtargetInfo &STI;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000036 MCContext &Ctx;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000037 bool IsLittleEndian;
Akira Hatanaka750ecec2011-09-30 20:40:03 +000038
39public:
40 MipsMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000041 MCContext &ctx, bool IsLittle) :
42 MCII(mcii), STI(sti) , 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.
52 for (unsigned i = 0; i != Size; ++i) {
53 EmitByte(Val & 255, OS);
54 Val >>= 8;
55 }
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,
94 const MCSubtargetInfo &STI,
95 MCContext &Ctx)
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000096{
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000097 return new MipsMCCodeEmitter(MCII, STI, Ctx, false);
98}
99
100MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
101 const MCSubtargetInfo &STI,
102 MCContext &Ctx)
103{
104 return new MipsMCCodeEmitter(MCII, STI, Ctx, 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{
113 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
114
115 // Check for unimplemented opcodes.
116 // Unfortunately in MIPS both NOT and SLL will come in with Binary == 0
117 // so we have to special check for them.
118 unsigned Opcode = MI.getOpcode();
119 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
120 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
121
122 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
123 uint64_t TSFlags = Desc.TSFlags;
124
125 // Pseudo instructions don't get encoded and shouldn't be here
126 // in the first place!
127 if ((TSFlags & MipsII::FormMask) == MipsII::Pseudo)
128 llvm_unreachable("Pseudo opcode found in EncodeInstruction()");
129
130 // For now all instructions are 4 bytes
131 int Size = 4; // FIXME: Have Desc.getSize() return the correct value!
132
133 EmitInstruction(Binary, Size, OS);
134}
135
136/// getBranchTargetOpValue - Return binary encoding of the branch
137/// target operand. If the machine operand requires relocation,
138/// record the relocation and return zero.
139unsigned MipsMCCodeEmitter::
140getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
141 SmallVectorImpl<MCFixup> &Fixups) const {
142
143 const MCOperand &MO = MI.getOperand(OpNo);
144 assert(MO.isExpr() && "getBranchTargetOpValue expects only expressions");
145
146 const MCExpr *Expr = MO.getExpr();
147 Fixups.push_back(MCFixup::Create(0, Expr,
148 MCFixupKind(Mips::fixup_Mips_PC16)));
149 return 0;
150}
151
152/// getJumpTargetOpValue - Return binary encoding of the jump
153/// target operand. If the machine operand requires relocation,
154/// record the relocation and return zero.
155unsigned MipsMCCodeEmitter::
156getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
157 SmallVectorImpl<MCFixup> &Fixups) const {
158
159 const MCOperand &MO = MI.getOperand(OpNo);
160 assert(MO.isExpr() && "getJumpTargetOpValue expects only expressions");
161
162 const MCExpr *Expr = MO.getExpr();
163 Fixups.push_back(MCFixup::Create(0, Expr,
164 MCFixupKind(Mips::fixup_Mips_26)));
165 return 0;
166}
167
168/// getMachineOpValue - Return binary encoding of operand. If the machine
169/// operand requires relocation, record the relocation and return zero.
170unsigned MipsMCCodeEmitter::
171getMachineOpValue(const MCInst &MI, const MCOperand &MO,
172 SmallVectorImpl<MCFixup> &Fixups) const {
173 if (MO.isReg()) {
174 unsigned Reg = MO.getReg();
175 unsigned RegNo = getMipsRegisterNumbering(Reg);
176 return RegNo;
177 } else if (MO.isImm()) {
178 return static_cast<unsigned>(MO.getImm());
179 } else if (MO.isFPImm()) {
180 return static_cast<unsigned>(APFloat(MO.getFPImm())
181 .bitcastToAPInt().getHiBits(32).getLimitedValue());
182 } else if (MO.isExpr()) {
183 const MCExpr *Expr = MO.getExpr();
184 MCExpr::ExprKind Kind = Expr->getKind();
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000185 unsigned Ret = 0;
186
187 if (Kind == MCExpr::Binary) {
188 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Expr);
189 Expr = BE->getLHS();
190 Kind = Expr->getKind();
191 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
192 assert((Kind == MCExpr::SymbolRef) && CE &&
193 "Binary expression must be sym+const.");
194 Ret = CE->getValue();
195 }
196
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000197 if (Kind == MCExpr::SymbolRef) {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000198 Mips::Fixups FixupKind;
Akira Hatanakae2eed962011-12-22 01:05:17 +0000199
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000200 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000201 case MCSymbolRefExpr::VK_Mips_GPREL:
202 FixupKind = Mips::fixup_Mips_GPREL16;
203 break;
204 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
205 FixupKind = Mips::fixup_Mips_CALL16;
206 break;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000207 case MCSymbolRefExpr::VK_Mips_GOT16:
208 FixupKind = Mips::fixup_Mips_GOT_Global;
209 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000210 case MCSymbolRefExpr::VK_Mips_GOT:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000211 FixupKind = Mips::fixup_Mips_GOT_Local;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000212 break;
213 case MCSymbolRefExpr::VK_Mips_ABS_HI:
214 FixupKind = Mips::fixup_Mips_HI16;
215 break;
216 case MCSymbolRefExpr::VK_Mips_ABS_LO:
217 FixupKind = Mips::fixup_Mips_LO16;
218 break;
219 case MCSymbolRefExpr::VK_Mips_TLSGD:
220 FixupKind = Mips::fixup_Mips_TLSGD;
221 break;
Akira Hatanakae2eed962011-12-22 01:05:17 +0000222 case MCSymbolRefExpr::VK_Mips_TLSLDM:
223 FixupKind = Mips::fixup_Mips_TLSLDM;
224 break;
225 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
226 FixupKind = Mips::fixup_Mips_DTPREL_HI;
227 break;
228 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
229 FixupKind = Mips::fixup_Mips_DTPREL_LO;
230 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000231 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
232 FixupKind = Mips::fixup_Mips_GOTTPREL;
233 break;
234 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
235 FixupKind = Mips::fixup_Mips_TPREL_HI;
236 break;
237 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
238 FixupKind = Mips::fixup_Mips_TPREL_LO;
239 break;
240 default:
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000241 return Ret;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000242 } // switch
243 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
244 } // if SymbolRef
245 // All of the information is in the fixup.
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000246 return Ret;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000247 }
248 llvm_unreachable("Unable to encode MCOperand!");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000249}
250
251/// getMemEncoding - Return binary encoding of memory related operand.
252/// If the offset operand requires relocation, record the relocation.
253unsigned
254MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
255 SmallVectorImpl<MCFixup> &Fixups) const {
256 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
257 assert(MI.getOperand(OpNo).isReg());
258 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
259 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
260
261 return (OffBits & 0xFFFF) | RegBits;
262}
263
264unsigned
265MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
266 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000267 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000268 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
269 return SizeEncoding - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000270}
271
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000272// FIXME: should be called getMSBEncoding
273//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000274unsigned
275MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
276 SmallVectorImpl<MCFixup> &Fixups) const {
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000277 assert(MI.getOperand(OpNo-1).isImm());
278 assert(MI.getOperand(OpNo).isImm());
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000279 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
280 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
Akira Hatanaka049e9e42011-11-23 22:19:28 +0000281
Bruno Cardoso Lopes56b70de2011-12-07 22:35:30 +0000282 return Position + Size - 1;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000283}
284
285#include "MipsGenMCCodeEmitter.inc"
286