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 //===----------------------------------------------------------------------===//
15 #include "MipsMCCodeEmitter.h"
16 #include "MCTargetDesc/MipsFixupKinds.h"
17 #include "MCTargetDesc/MipsMCExpr.h"
18 #include "MCTargetDesc/MipsMCTargetDesc.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/Support/raw_ostream.h"
29 #define DEBUG_TYPE "mccodeemitter"
31 #define GET_INSTRMAP_INFO
32 #include "MipsGenInstrInfo.inc"
33 #undef GET_INSTRMAP_INFO
36 MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
37 const MCRegisterInfo &MRI,
38 const MCSubtargetInfo &STI,
40 return new MipsMCCodeEmitter(MCII, Ctx, false);
43 MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
44 const MCRegisterInfo &MRI,
45 const MCSubtargetInfo &STI,
47 return new MipsMCCodeEmitter(MCII, Ctx, true);
49 } // End of namespace llvm.
51 // If the D<shift> instruction has a shift amount that is greater
52 // than 31 (checked in calling routine), lower it to a D<shift>32 instruction
53 static void LowerLargeShift(MCInst& Inst) {
55 assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!");
56 assert(Inst.getOperand(2).isImm());
58 int64_t Shift = Inst.getOperand(2).getImm();
64 Inst.getOperand(2).setImm(Shift);
66 switch (Inst.getOpcode()) {
68 // Calling function is not synchronized
69 llvm_unreachable("Unexpected shift instruction");
71 Inst.setOpcode(Mips::DSLL32);
74 Inst.setOpcode(Mips::DSRL32);
77 Inst.setOpcode(Mips::DSRA32);
80 Inst.setOpcode(Mips::DROTR32);
85 // Pick a DEXT or DINS instruction variant based on the pos and size operands
86 static void LowerDextDins(MCInst& InstIn) {
87 int Opcode = InstIn.getOpcode();
89 if (Opcode == Mips::DEXT)
90 assert(InstIn.getNumOperands() == 4 &&
91 "Invalid no. of machine operands for DEXT!");
92 else // Only DEXT and DINS are possible
93 assert(InstIn.getNumOperands() == 5 &&
94 "Invalid no. of machine operands for DINS!");
96 assert(InstIn.getOperand(2).isImm());
97 int64_t pos = InstIn.getOperand(2).getImm();
98 assert(InstIn.getOperand(3).isImm());
99 int64_t size = InstIn.getOperand(3).getImm();
102 if (pos < 32) // DEXT/DINS, do nothing
105 InstIn.getOperand(2).setImm(pos - 32);
106 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU);
110 assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32");
111 InstIn.getOperand(3).setImm(size - 32);
112 InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM);
116 bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const {
117 return STI.getFeatureBits() & Mips::FeatureMicroMips;
120 void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const {
124 void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size,
125 const MCSubtargetInfo &STI,
126 raw_ostream &OS) const {
127 // Output the instruction encoding in little endian byte order.
128 // Little-endian byte ordering:
129 // mips32r2: 4 | 3 | 2 | 1
130 // microMIPS: 2 | 1 | 4 | 3
131 if (IsLittleEndian && Size == 4 && isMicroMips(STI)) {
132 EmitInstruction(Val >> 16, 2, STI, OS);
133 EmitInstruction(Val, 2, STI, OS);
135 for (unsigned i = 0; i < Size; ++i) {
136 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
137 EmitByte((Val >> Shift) & 0xff, OS);
142 /// EncodeInstruction - Emit the instruction.
143 /// Size the instruction with Desc.getSize().
144 void MipsMCCodeEmitter::
145 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
146 SmallVectorImpl<MCFixup> &Fixups,
147 const MCSubtargetInfo &STI) const
150 // Non-pseudo instructions that get changed for direct object
151 // only based on operand values.
152 // If this list of instructions get much longer we will move
153 // the check to a function call. Until then, this is more efficient.
155 switch (MI.getOpcode()) {
156 // If shift amount is >= 32 it the inst needs to be lowered further
161 LowerLargeShift(TmpInst);
163 // Double extract instruction is chosen by pos and size operands
166 LowerDextDins(TmpInst);
169 unsigned long N = Fixups.size();
170 uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
172 // Check for unimplemented opcodes.
173 // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
174 // so we have to special check for them.
175 unsigned Opcode = TmpInst.getOpcode();
176 if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
177 llvm_unreachable("unimplemented opcode in EncodeInstruction()");
179 if (STI.getFeatureBits() & Mips::FeatureMicroMips) {
180 int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips);
181 if (NewOpcode != -1) {
182 if (Fixups.size() > N)
185 TmpInst.setOpcode (NewOpcode);
186 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
190 const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
192 // Get byte count of instruction
193 unsigned Size = Desc.getSize();
195 llvm_unreachable("Desc.getSize() returns 0");
197 EmitInstruction(Binary, Size, STI, OS);
200 /// getBranchTargetOpValue - Return binary encoding of the branch
201 /// target operand. If the machine operand requires relocation,
202 /// record the relocation and return zero.
203 unsigned MipsMCCodeEmitter::
204 getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
205 SmallVectorImpl<MCFixup> &Fixups,
206 const MCSubtargetInfo &STI) const {
208 const MCOperand &MO = MI.getOperand(OpNo);
210 // If the destination is an immediate, divide by 4.
211 if (MO.isImm()) return MO.getImm() >> 2;
213 assert(MO.isExpr() &&
214 "getBranchTargetOpValue expects only expressions or immediates");
216 const MCExpr *Expr = MO.getExpr();
217 Fixups.push_back(MCFixup::Create(0, Expr,
218 MCFixupKind(Mips::fixup_Mips_PC16)));
222 /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch
223 /// target operand. If the machine operand requires relocation,
224 /// record the relocation and return zero.
225 unsigned MipsMCCodeEmitter::
226 getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
227 SmallVectorImpl<MCFixup> &Fixups,
228 const MCSubtargetInfo &STI) const {
230 const MCOperand &MO = MI.getOperand(OpNo);
232 // If the destination is an immediate, divide by 2.
233 if (MO.isImm()) return MO.getImm() >> 1;
235 assert(MO.isExpr() &&
236 "getBranchTargetOpValueMM expects only expressions or immediates");
238 const MCExpr *Expr = MO.getExpr();
239 Fixups.push_back(MCFixup::Create(0, Expr,
241 fixup_MICROMIPS_PC16_S1)));
245 /// getBranchTarget21OpValue - Return binary encoding of the branch
246 /// target operand. If the machine operand requires relocation,
247 /// record the relocation and return zero.
248 unsigned MipsMCCodeEmitter::
249 getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
250 SmallVectorImpl<MCFixup> &Fixups,
251 const MCSubtargetInfo &STI) const {
253 const MCOperand &MO = MI.getOperand(OpNo);
255 // If the destination is an immediate, divide by 4.
256 if (MO.isImm()) return MO.getImm() >> 2;
258 assert(MO.isExpr() &&
259 "getBranchTarget21OpValue expects only expressions or immediates");
261 // TODO: Push 21 PC fixup.
265 /// getBranchTarget26OpValue - Return binary encoding of the branch
266 /// target operand. If the machine operand requires relocation,
267 /// record the relocation and return zero.
268 unsigned MipsMCCodeEmitter::
269 getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
270 SmallVectorImpl<MCFixup> &Fixups,
271 const MCSubtargetInfo &STI) const {
273 const MCOperand &MO = MI.getOperand(OpNo);
275 // If the destination is an immediate, divide by 4.
276 if (MO.isImm()) return MO.getImm() >> 2;
278 assert(MO.isExpr() &&
279 "getBranchTarget26OpValue expects only expressions or immediates");
281 // TODO: Push 26 PC fixup.
285 /// getJumpTargetOpValue - Return binary encoding of the jump
286 /// target operand. If the machine operand requires relocation,
287 /// record the relocation and return zero.
288 unsigned MipsMCCodeEmitter::
289 getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
290 SmallVectorImpl<MCFixup> &Fixups,
291 const MCSubtargetInfo &STI) const {
293 const MCOperand &MO = MI.getOperand(OpNo);
294 // If the destination is an immediate, divide by 4.
295 if (MO.isImm()) return MO.getImm()>>2;
297 assert(MO.isExpr() &&
298 "getJumpTargetOpValue expects only expressions or an immediate");
300 const MCExpr *Expr = MO.getExpr();
301 Fixups.push_back(MCFixup::Create(0, Expr,
302 MCFixupKind(Mips::fixup_Mips_26)));
306 unsigned MipsMCCodeEmitter::
307 getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
308 SmallVectorImpl<MCFixup> &Fixups,
309 const MCSubtargetInfo &STI) const {
311 const MCOperand &MO = MI.getOperand(OpNo);
312 // If the destination is an immediate, divide by 2.
313 if (MO.isImm()) return MO.getImm() >> 1;
315 assert(MO.isExpr() &&
316 "getJumpTargetOpValueMM expects only expressions or an immediate");
318 const MCExpr *Expr = MO.getExpr();
319 Fixups.push_back(MCFixup::Create(0, Expr,
320 MCFixupKind(Mips::fixup_MICROMIPS_26_S1)));
324 unsigned MipsMCCodeEmitter::
325 getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups,
326 const MCSubtargetInfo &STI) const {
329 if (Expr->EvaluateAsAbsolute(Res))
332 MCExpr::ExprKind Kind = Expr->getKind();
333 if (Kind == MCExpr::Constant) {
334 return cast<MCConstantExpr>(Expr)->getValue();
337 if (Kind == MCExpr::Binary) {
338 unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
339 Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
343 if (Kind == MCExpr::Target) {
344 const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);
346 Mips::Fixups FixupKind = Mips::Fixups(0);
347 switch (MipsExpr->getKind()) {
348 default: llvm_unreachable("Unsupported fixup kind for target expression!");
349 case MipsMCExpr::VK_Mips_HIGHEST:
350 FixupKind = Mips::fixup_Mips_HIGHEST;
352 case MipsMCExpr::VK_Mips_HIGHER:
353 FixupKind = Mips::fixup_Mips_HIGHER;
355 case MipsMCExpr::VK_Mips_HI:
356 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
357 : Mips::fixup_Mips_HI16;
359 case MipsMCExpr::VK_Mips_LO:
360 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
361 : Mips::fixup_Mips_LO16;
364 Fixups.push_back(MCFixup::Create(0, MipsExpr, MCFixupKind(FixupKind)));
368 if (Kind == MCExpr::SymbolRef) {
369 Mips::Fixups FixupKind = Mips::Fixups(0);
371 switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
372 default: llvm_unreachable("Unknown fixup kind!");
374 case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
375 FixupKind = Mips::fixup_Mips_GPOFF_HI;
377 case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
378 FixupKind = Mips::fixup_Mips_GPOFF_LO;
380 case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
381 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE
382 : Mips::fixup_Mips_GOT_PAGE;
384 case MCSymbolRefExpr::VK_Mips_GOT_OFST :
385 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST
386 : Mips::fixup_Mips_GOT_OFST;
388 case MCSymbolRefExpr::VK_Mips_GOT_DISP :
389 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP
390 : Mips::fixup_Mips_GOT_DISP;
392 case MCSymbolRefExpr::VK_Mips_GPREL:
393 FixupKind = Mips::fixup_Mips_GPREL16;
395 case MCSymbolRefExpr::VK_Mips_GOT_CALL:
396 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16
397 : Mips::fixup_Mips_CALL16;
399 case MCSymbolRefExpr::VK_Mips_GOT16:
400 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
401 : Mips::fixup_Mips_GOT_Global;
403 case MCSymbolRefExpr::VK_Mips_GOT:
404 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16
405 : Mips::fixup_Mips_GOT_Local;
407 case MCSymbolRefExpr::VK_Mips_ABS_HI:
408 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16
409 : Mips::fixup_Mips_HI16;
411 case MCSymbolRefExpr::VK_Mips_ABS_LO:
412 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16
413 : Mips::fixup_Mips_LO16;
415 case MCSymbolRefExpr::VK_Mips_TLSGD:
416 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD
417 : Mips::fixup_Mips_TLSGD;
419 case MCSymbolRefExpr::VK_Mips_TLSLDM:
420 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM
421 : Mips::fixup_Mips_TLSLDM;
423 case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
424 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16
425 : Mips::fixup_Mips_DTPREL_HI;
427 case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
428 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16
429 : Mips::fixup_Mips_DTPREL_LO;
431 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
432 FixupKind = Mips::fixup_Mips_GOTTPREL;
434 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
435 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16
436 : Mips::fixup_Mips_TPREL_HI;
438 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
439 FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16
440 : Mips::fixup_Mips_TPREL_LO;
442 case MCSymbolRefExpr::VK_Mips_HIGHER:
443 FixupKind = Mips::fixup_Mips_HIGHER;
445 case MCSymbolRefExpr::VK_Mips_HIGHEST:
446 FixupKind = Mips::fixup_Mips_HIGHEST;
448 case MCSymbolRefExpr::VK_Mips_GOT_HI16:
449 FixupKind = Mips::fixup_Mips_GOT_HI16;
451 case MCSymbolRefExpr::VK_Mips_GOT_LO16:
452 FixupKind = Mips::fixup_Mips_GOT_LO16;
454 case MCSymbolRefExpr::VK_Mips_CALL_HI16:
455 FixupKind = Mips::fixup_Mips_CALL_HI16;
457 case MCSymbolRefExpr::VK_Mips_CALL_LO16:
458 FixupKind = Mips::fixup_Mips_CALL_LO16;
462 Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind)));
468 /// getMachineOpValue - Return binary encoding of operand. If the machine
469 /// operand requires relocation, record the relocation and return zero.
470 unsigned MipsMCCodeEmitter::
471 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
472 SmallVectorImpl<MCFixup> &Fixups,
473 const MCSubtargetInfo &STI) const {
475 unsigned Reg = MO.getReg();
476 unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg);
478 } else if (MO.isImm()) {
479 return static_cast<unsigned>(MO.getImm());
480 } else if (MO.isFPImm()) {
481 return static_cast<unsigned>(APFloat(MO.getFPImm())
482 .bitcastToAPInt().getHiBits(32).getLimitedValue());
484 // MO must be an Expr.
486 return getExprOpValue(MO.getExpr(),Fixups, STI);
489 /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST
492 MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
493 SmallVectorImpl<MCFixup> &Fixups,
494 const MCSubtargetInfo &STI) const {
495 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
496 assert(MI.getOperand(OpNo).isReg());
497 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
498 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
500 // The immediate field of an LD/ST instruction is scaled which means it must
501 // be divided (when encoding) by the size (in bytes) of the instructions'
507 switch(MI.getOpcode())
510 assert (0 && "Unexpected instruction");
514 // We don't need to scale the offset in this case
530 return (OffBits & 0xFFFF) | RegBits;
533 /// getMemEncoding - Return binary encoding of memory related operand.
534 /// If the offset operand requires relocation, record the relocation.
536 MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
537 SmallVectorImpl<MCFixup> &Fixups,
538 const MCSubtargetInfo &STI) const {
539 // Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
540 assert(MI.getOperand(OpNo).isReg());
541 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16;
542 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
544 return (OffBits & 0xFFFF) | RegBits;
547 unsigned MipsMCCodeEmitter::
548 getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo,
549 SmallVectorImpl<MCFixup> &Fixups,
550 const MCSubtargetInfo &STI) const {
551 // Base register is encoded in bits 20-16, offset is encoded in bits 11-0.
552 assert(MI.getOperand(OpNo).isReg());
553 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16;
554 unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI);
556 return (OffBits & 0x0FFF) | RegBits;
560 MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
561 SmallVectorImpl<MCFixup> &Fixups,
562 const MCSubtargetInfo &STI) const {
563 assert(MI.getOperand(OpNo).isImm());
564 unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
565 return SizeEncoding - 1;
568 // FIXME: should be called getMSBEncoding
571 MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
572 SmallVectorImpl<MCFixup> &Fixups,
573 const MCSubtargetInfo &STI) const {
574 assert(MI.getOperand(OpNo-1).isImm());
575 assert(MI.getOperand(OpNo).isImm());
576 unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI);
577 unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
579 return Position + Size - 1;
583 MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo,
584 SmallVectorImpl<MCFixup> &Fixups,
585 const MCSubtargetInfo &STI) const {
586 assert(MI.getOperand(OpNo).isImm());
587 // The immediate is encoded as 'immediate - 1'.
588 return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1;
592 MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
593 SmallVectorImpl<MCFixup> &Fixups,
594 const MCSubtargetInfo &STI) const {
595 assert(MI.getOperand(OpNo).isImm());
596 // The immediate is encoded as 'immediate << 2'.
597 unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI);
598 assert((Res & 3) == 0);
602 #include "MipsGenMCCodeEmitter.inc"