1 //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the MipsMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mccodeemitter"
15 #include "MCTargetDesc/MipsBaseInfo.h"
16 #include "MCTargetDesc/MipsDirectObjLower.h"
17 #include "MCTargetDesc/MipsFixupKinds.h"
18 #include "MCTargetDesc/MipsMCTargetDesc.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/Statistic.h"
21 #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"
27 #include "llvm/Support/raw_ostream.h"
32 class MipsMCCodeEmitter : public MCCodeEmitter {
33 MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34 void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35 const MCInstrInfo &MCII;
39 MipsMCCodeEmitter(const MCInstrInfo &mcii, bool IsLittle) :
40 MCII(mcii), IsLittleEndian(IsLittle) {}
42 ~MipsMCCodeEmitter() {}
44 void EmitByte(unsigned char C, raw_ostream &OS) const {
48 void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
49 // Output the instruction encoding in little endian byte order.
50 for (unsigned i = 0; i < Size; ++i) {
51 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
52 EmitByte((Val >> Shift) & 0xff, OS);
56 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
57 SmallVectorImpl<MCFixup> &Fixups) const;
59 // getBinaryCodeForInstr - TableGen'erated function for getting the
60 // binary encoding for an instruction.
61 uint64_t getBinaryCodeForInstr(const MCInst &MI,
62 SmallVectorImpl<MCFixup> &Fixups) const;
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;
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;
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;
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;
88 }; // class MipsMCCodeEmitter
91 MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
92 const MCRegisterInfo &MRI,
93 const MCSubtargetInfo &STI,
96 return new MipsMCCodeEmitter(MCII, false);
99 MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
100 const MCRegisterInfo &MRI,
101 const MCSubtargetInfo &STI,
104 return new MipsMCCodeEmitter(MCII, true);
107 /// EncodeInstruction - Emit the instruction.
108 /// Size the instruction (currently only 4 bytes
109 void MipsMCCodeEmitter::
110 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
111 SmallVectorImpl<MCFixup> &Fixups) const
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.
119 switch (MI.getOpcode()) {
120 // If shift amount is >= 32 it the inst needs to be lowered further
124 Mips::LowerLargeShift(TmpInst);
126 // Double extract instruction is chosen by pos and size operands
129 Mips::LowerDextDins(TmpInst);
132 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
134 // Check for unimplemented opcodes.
135 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
136 // so we have to special check for them.
137 unsigned Opcode = TmpInst.getOpcode();
138 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
139 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
141 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
142 uint64_t TSFlags = Desc.TSFlags;
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()");
149 // Get byte count of instruction
150 unsigned Size = Desc.getSize();
152 llvm_unreachable("Desc.getSize() returns 0");
154 EmitInstruction(Binary, Size, OS);
157 /// getBranchTargetOpValue - Return binary encoding of the branch
158 /// target operand. If the machine operand requires relocation,
159 /// record the relocation and return zero.
160 unsigned MipsMCCodeEmitter::
161 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
162 SmallVectorImpl<MCFixup> &Fixups) const {
164 const MCOperand &MO = MI.getOperand(OpNo);
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");
171 const MCExpr *Expr = MO.getExpr();
172 Fixups.push_back(MCFixup::Create(0, Expr,
173 MCFixupKind(Mips::fixup_Mips_PC16)));
177 /// getJumpTargetOpValue - Return binary encoding of the jump
178 /// target operand. If the machine operand requires relocation,
179 /// record the relocation and return zero.
180 unsigned MipsMCCodeEmitter::
181 getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
182 SmallVectorImpl<MCFixup> &Fixups) const {
184 const MCOperand &MO = MI.getOperand(OpNo);
185 // 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");
190 const MCExpr *Expr = MO.getExpr();
191 Fixups.push_back(MCFixup::Create(0, Expr,
192 MCFixupKind(Mips::fixup_Mips_26)));
196 /// getMachineOpValue - Return binary encoding of operand. If the machine
197 /// operand requires relocation, record the relocation and return zero.
198 unsigned MipsMCCodeEmitter::
199 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
200 SmallVectorImpl<MCFixup> &Fixups) const {
202 unsigned Reg = MO.getReg();
203 unsigned RegNo = getMipsRegisterNumbering(Reg);
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());
212 // MO must be an Expr.
215 const MCExpr *Expr = MO.getExpr();
216 MCExpr::ExprKind Kind = Expr->getKind();
218 if (Kind == MCExpr::Binary) {
219 Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
220 Kind = Expr->getKind();
223 assert (Kind == MCExpr::SymbolRef);
225 Mips::Fixups FixupKind = Mips::Fixups(0);
227 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
228 default: llvm_unreachable("Unknown fixup kind!");
230 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
231 FixupKind = Mips::fixup_Mips_GPOFF_HI;
233 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
234 FixupKind = Mips::fixup_Mips_GPOFF_LO;
236 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
237 FixupKind = Mips::fixup_Mips_GOT_PAGE;
239 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
240 FixupKind = Mips::fixup_Mips_GOT_OFST;
242 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
243 FixupKind = Mips::fixup_Mips_GOT_DISP;
245 case MCSymbolRefExpr::VK_Mips_GPREL:
246 FixupKind = Mips::fixup_Mips_GPREL16;
248 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
249 FixupKind = Mips::fixup_Mips_CALL16;
251 case MCSymbolRefExpr::VK_Mips_GOT16:
252 FixupKind = Mips::fixup_Mips_GOT_Global;
254 case MCSymbolRefExpr::VK_Mips_GOT:
255 FixupKind = Mips::fixup_Mips_GOT_Local;
257 case MCSymbolRefExpr::VK_Mips_ABS_HI:
258 FixupKind = Mips::fixup_Mips_HI16;
260 case MCSymbolRefExpr::VK_Mips_ABS_LO:
261 FixupKind = Mips::fixup_Mips_LO16;
263 case MCSymbolRefExpr::VK_Mips_TLSGD:
264 FixupKind = Mips::fixup_Mips_TLSGD;
266 case MCSymbolRefExpr::VK_Mips_TLSLDM:
267 FixupKind = Mips::fixup_Mips_TLSLDM;
269 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
270 FixupKind = Mips::fixup_Mips_DTPREL_HI;
272 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
273 FixupKind = Mips::fixup_Mips_DTPREL_LO;
275 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
276 FixupKind = Mips::fixup_Mips_GOTTPREL;
278 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
279 FixupKind = Mips::fixup_Mips_TPREL_HI;
281 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
282 FixupKind = Mips::fixup_Mips_TPREL_LO;
284 case MCSymbolRefExpr::VK_Mips_HIGHER:
285 FixupKind = Mips::fixup_Mips_HIGHER;
287 case MCSymbolRefExpr::VK_Mips_HIGHEST:
288 FixupKind = Mips::fixup_Mips_HIGHEST;
292 Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
294 // All of the information is in the fixup.
298 /// getMemEncoding - Return binary encoding of memory related operand.
299 /// If the offset operand requires relocation, record the relocation.
301 MipsMCCodeEmitter::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);
308 return (OffBits & 0xFFFF) | RegBits;
312 MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
313 SmallVectorImpl<MCFixup> &Fixups) const {
314 assert(MI.getOperand(OpNo).isImm());
315 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
316 return SizeEncoding - 1;
319 // FIXME: should be called getMSBEncoding
322 MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
323 SmallVectorImpl<MCFixup> &Fixups) const {
324 assert(MI.getOperand(OpNo-1).isImm());
325 assert(MI.getOperand(OpNo).isImm());
326 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
327 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
329 return Position + Size - 1;
332 #include "MipsGenMCCodeEmitter.inc"