2012-02-17 16:55:11 +08:00
|
|
|
//===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===//
|
2011-10-01 04:40:03 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MipsMCCodeEmitter class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
#define DEBUG_TYPE "mccodeemitter"
|
2011-11-12 06:58:42 +08:00
|
|
|
#include "MCTargetDesc/MipsBaseInfo.h"
|
2012-10-03 07:09:40 +08:00
|
|
|
#include "MCTargetDesc/MipsDirectObjLower.h"
|
2011-11-12 06:58:42 +08:00
|
|
|
#include "MCTargetDesc/MipsFixupKinds.h"
|
|
|
|
#include "MCTargetDesc/MipsMCTargetDesc.h"
|
|
|
|
#include "llvm/ADT/APFloat.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2011-10-01 04:40:03 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MipsMCCodeEmitter : public MCCodeEmitter {
|
2012-09-16 01:08:51 +08:00
|
|
|
MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
|
|
|
void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION;
|
2011-10-01 04:40:03 +08:00
|
|
|
const MCInstrInfo &MCII;
|
2012-03-01 09:53:15 +08:00
|
|
|
bool IsLittleEndian;
|
2011-10-01 04:40:03 +08:00
|
|
|
|
|
|
|
public:
|
2012-09-16 01:08:51 +08:00
|
|
|
MipsMCCodeEmitter(const MCInstrInfo &mcii, bool IsLittle) :
|
|
|
|
MCII(mcii), IsLittleEndian(IsLittle) {}
|
2011-10-01 04:40:03 +08:00
|
|
|
|
|
|
|
~MipsMCCodeEmitter() {}
|
|
|
|
|
2011-11-12 06:58:42 +08:00
|
|
|
void EmitByte(unsigned char C, raw_ostream &OS) const {
|
|
|
|
OS << (char)C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const {
|
|
|
|
// Output the instruction encoding in little endian byte order.
|
2012-03-21 08:52:01 +08:00
|
|
|
for (unsigned i = 0; i < Size; ++i) {
|
|
|
|
unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
|
|
|
|
EmitByte((Val >> Shift) & 0xff, OS);
|
2011-11-12 06:58:42 +08:00
|
|
|
}
|
2011-10-01 04:40:03 +08:00
|
|
|
}
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
// getBinaryCodeForInstr - TableGen'erated function for getting the
|
|
|
|
// binary encoding for an instruction.
|
2012-01-25 02:37:29 +08:00
|
|
|
uint64_t getBinaryCodeForInstr(const MCInst &MI,
|
2011-11-12 06:58:42 +08:00
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
// getBranchJumpOpValue - Return binary encoding of the jump
|
|
|
|
// target operand. If the machine operand requires relocation,
|
|
|
|
// record the relocation and return zero.
|
|
|
|
unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
// getBranchTargetOpValue - Return binary encoding of the branch
|
|
|
|
// target operand. If the machine operand requires relocation,
|
|
|
|
// record the relocation and return zero.
|
|
|
|
unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
// getMachineOpValue - Return binary encoding of operand. If the machin
|
|
|
|
// operand requires relocation, record the relocation and return zero.
|
|
|
|
unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
|
|
|
unsigned getMemEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
|
2011-10-01 04:40:03 +08:00
|
|
|
}; // class MipsMCCodeEmitter
|
|
|
|
} // namespace
|
|
|
|
|
2012-03-01 09:53:15 +08:00
|
|
|
MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII,
|
2012-05-16 01:35:52 +08:00
|
|
|
const MCRegisterInfo &MRI,
|
2012-03-01 09:53:15 +08:00
|
|
|
const MCSubtargetInfo &STI,
|
|
|
|
MCContext &Ctx)
|
2011-11-12 06:58:42 +08:00
|
|
|
{
|
2012-09-16 01:08:51 +08:00
|
|
|
return new MipsMCCodeEmitter(MCII, false);
|
2012-03-01 09:53:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII,
|
2012-05-16 01:35:52 +08:00
|
|
|
const MCRegisterInfo &MRI,
|
2012-03-01 09:53:15 +08:00
|
|
|
const MCSubtargetInfo &STI,
|
|
|
|
MCContext &Ctx)
|
|
|
|
{
|
2012-09-16 01:08:51 +08:00
|
|
|
return new MipsMCCodeEmitter(MCII, true);
|
2011-10-01 04:40:03 +08:00
|
|
|
}
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
/// EncodeInstruction - Emit the instruction.
|
|
|
|
/// Size the instruction (currently only 4 bytes
|
|
|
|
void MipsMCCodeEmitter::
|
|
|
|
EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const
|
|
|
|
{
|
2012-10-03 07:09:40 +08:00
|
|
|
|
|
|
|
// Non-pseudo instructions that get changed for direct object
|
|
|
|
// only based on operand values.
|
|
|
|
// If this list of instructions get much longer we will move
|
|
|
|
// the check to a function call. Until then, this is more efficient.
|
|
|
|
MCInst TmpInst = MI;
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
// If shift amount is >= 32 it the inst needs to be lowered further
|
|
|
|
case Mips::DSLL:
|
|
|
|
case Mips::DSRL:
|
|
|
|
case Mips::DSRA:
|
|
|
|
Mips::LowerLargeShift(TmpInst);
|
|
|
|
break;
|
|
|
|
// Double extract instruction is chosen by pos and size operands
|
|
|
|
case Mips::DEXT:
|
|
|
|
case Mips::DINS:
|
|
|
|
Mips::LowerDextDins(TmpInst);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups);
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
// Check for unimplemented opcodes.
|
2012-10-03 07:09:40 +08:00
|
|
|
// Unfortunately in MIPS both NOP and SLL will come in with Binary == 0
|
2011-11-12 06:58:42 +08:00
|
|
|
// so we have to special check for them.
|
2012-10-03 07:09:40 +08:00
|
|
|
unsigned Opcode = TmpInst.getOpcode();
|
2011-11-12 06:58:42 +08:00
|
|
|
if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary)
|
|
|
|
llvm_unreachable("unimplemented opcode in EncodeInstruction()");
|
|
|
|
|
2012-10-03 07:09:40 +08:00
|
|
|
const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode());
|
2011-11-12 06:58:42 +08:00
|
|
|
uint64_t TSFlags = Desc.TSFlags;
|
|
|
|
|
|
|
|
// Pseudo instructions don't get encoded and shouldn't be here
|
|
|
|
// in the first place!
|
|
|
|
if ((TSFlags & MipsII::FormMask) == MipsII::Pseudo)
|
|
|
|
llvm_unreachable("Pseudo opcode found in EncodeInstruction()");
|
|
|
|
|
2012-10-04 05:58:54 +08:00
|
|
|
// Get byte count of instruction
|
|
|
|
unsigned Size = Desc.getSize();
|
|
|
|
if (!Size)
|
|
|
|
llvm_unreachable("Desc.getSize() returns 0");
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
EmitInstruction(Binary, Size, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getBranchTargetOpValue - Return binary encoding of the branch
|
|
|
|
/// target operand. If the machine operand requires relocation,
|
|
|
|
/// record the relocation and return zero.
|
|
|
|
unsigned MipsMCCodeEmitter::
|
|
|
|
getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2012-09-06 08:43:26 +08:00
|
|
|
|
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm()) return MO.getImm();
|
|
|
|
assert(MO.isExpr() &&
|
|
|
|
"getBranchTargetOpValue expects only expressions or immediates");
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
Fixups.push_back(MCFixup::Create(0, Expr,
|
|
|
|
MCFixupKind(Mips::fixup_Mips_PC16)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getJumpTargetOpValue - Return binary encoding of the jump
|
|
|
|
/// target operand. If the machine operand requires relocation,
|
|
|
|
/// record the relocation and return zero.
|
|
|
|
unsigned MipsMCCodeEmitter::
|
|
|
|
getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
|
|
|
|
const MCOperand &MO = MI.getOperand(OpNo);
|
2012-09-06 08:43:26 +08:00
|
|
|
// If the destination is an immediate, we have nothing to do.
|
|
|
|
if (MO.isImm()) return MO.getImm();
|
|
|
|
assert(MO.isExpr() &&
|
|
|
|
"getJumpTargetOpValue expects only expressions or an immediate");
|
2011-11-12 06:58:42 +08:00
|
|
|
|
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
Fixups.push_back(MCFixup::Create(0, Expr,
|
|
|
|
MCFixupKind(Mips::fixup_Mips_26)));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getMachineOpValue - Return binary encoding of operand. If the machine
|
|
|
|
/// operand requires relocation, record the relocation and return zero.
|
|
|
|
unsigned MipsMCCodeEmitter::
|
|
|
|
getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
if (MO.isReg()) {
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
unsigned RegNo = getMipsRegisterNumbering(Reg);
|
|
|
|
return RegNo;
|
|
|
|
} else if (MO.isImm()) {
|
|
|
|
return static_cast<unsigned>(MO.getImm());
|
|
|
|
} else if (MO.isFPImm()) {
|
|
|
|
return static_cast<unsigned>(APFloat(MO.getFPImm())
|
|
|
|
.bitcastToAPInt().getHiBits(32).getLimitedValue());
|
2012-06-15 05:10:56 +08:00
|
|
|
}
|
2012-03-27 10:33:05 +08:00
|
|
|
|
|
|
|
// MO must be an Expr.
|
|
|
|
assert(MO.isExpr());
|
|
|
|
|
|
|
|
const MCExpr *Expr = MO.getExpr();
|
|
|
|
MCExpr::ExprKind Kind = Expr->getKind();
|
2011-11-24 06:19:28 +08:00
|
|
|
|
2012-03-27 10:33:05 +08:00
|
|
|
if (Kind == MCExpr::Binary) {
|
|
|
|
Expr = static_cast<const MCBinaryExpr*>(Expr)->getLHS();
|
|
|
|
Kind = Expr->getKind();
|
2011-11-12 06:58:42 +08:00
|
|
|
}
|
2012-03-27 10:33:05 +08:00
|
|
|
|
|
|
|
assert (Kind == MCExpr::SymbolRef);
|
2012-06-15 05:10:56 +08:00
|
|
|
|
2012-04-22 15:23:04 +08:00
|
|
|
Mips::Fixups FixupKind = Mips::Fixups(0);
|
2012-03-27 10:33:05 +08:00
|
|
|
|
|
|
|
switch(cast<MCSymbolRefExpr>(Expr)->getKind()) {
|
2012-06-28 06:48:25 +08:00
|
|
|
default: llvm_unreachable("Unknown fixup kind!");
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GPOFF_HI :
|
|
|
|
FixupKind = Mips::fixup_Mips_GPOFF_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GPOFF_LO :
|
|
|
|
FixupKind = Mips::fixup_Mips_GPOFF_LO;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_PAGE :
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_PAGE;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_OFST :
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_OFST;
|
|
|
|
break;
|
2012-07-14 03:15:47 +08:00
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_DISP :
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_DISP;
|
|
|
|
break;
|
2012-03-27 10:33:05 +08:00
|
|
|
case MCSymbolRefExpr::VK_Mips_GPREL:
|
|
|
|
FixupKind = Mips::fixup_Mips_GPREL16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_CALL:
|
|
|
|
FixupKind = Mips::fixup_Mips_CALL16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT16:
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_Global;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT:
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_Local;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_HI:
|
|
|
|
FixupKind = Mips::fixup_Mips_HI16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_LO:
|
|
|
|
FixupKind = Mips::fixup_Mips_LO16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TLSGD:
|
|
|
|
FixupKind = Mips::fixup_Mips_TLSGD;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TLSLDM:
|
|
|
|
FixupKind = Mips::fixup_Mips_TLSLDM;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_DTPREL_HI:
|
|
|
|
FixupKind = Mips::fixup_Mips_DTPREL_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_DTPREL_LO:
|
|
|
|
FixupKind = Mips::fixup_Mips_DTPREL_LO;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOTTPREL:
|
|
|
|
FixupKind = Mips::fixup_Mips_GOTTPREL;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TPREL_HI:
|
|
|
|
FixupKind = Mips::fixup_Mips_TPREL_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TPREL_LO:
|
|
|
|
FixupKind = Mips::fixup_Mips_TPREL_LO;
|
|
|
|
break;
|
2012-08-07 05:26:03 +08:00
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHER:
|
|
|
|
FixupKind = Mips::fixup_Mips_HIGHER;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHEST:
|
|
|
|
FixupKind = Mips::fixup_Mips_HIGHEST;
|
|
|
|
break;
|
2012-11-22 07:38:59 +08:00
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_HI16:
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_HI16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_LO16:
|
|
|
|
FixupKind = Mips::fixup_Mips_GOT_LO16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_CALL_HI16:
|
|
|
|
FixupKind = Mips::fixup_Mips_CALL_HI16;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_CALL_LO16:
|
|
|
|
FixupKind = Mips::fixup_Mips_CALL_LO16;
|
|
|
|
break;
|
2012-03-27 10:33:05 +08:00
|
|
|
} // switch
|
|
|
|
|
|
|
|
Fixups.push_back(MCFixup::Create(0, MO.getExpr(), MCFixupKind(FixupKind)));
|
|
|
|
|
|
|
|
// All of the information is in the fixup.
|
|
|
|
return 0;
|
2011-11-12 06:58:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getMemEncoding - Return binary encoding of memory related operand.
|
|
|
|
/// If the offset operand requires relocation, record the relocation.
|
|
|
|
unsigned
|
|
|
|
MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
|
|
// Base register is encoded in bits 20-16, offset is encoded in bits 15-0.
|
|
|
|
assert(MI.getOperand(OpNo).isReg());
|
|
|
|
unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16;
|
|
|
|
unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups);
|
|
|
|
|
|
|
|
return (OffBits & 0xFFFF) | RegBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2011-11-24 06:19:28 +08:00
|
|
|
assert(MI.getOperand(OpNo).isImm());
|
2011-12-08 06:35:30 +08:00
|
|
|
unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
|
|
|
|
return SizeEncoding - 1;
|
2011-11-12 06:58:42 +08:00
|
|
|
}
|
|
|
|
|
2011-11-24 06:19:28 +08:00
|
|
|
// FIXME: should be called getMSBEncoding
|
|
|
|
//
|
2011-11-12 06:58:42 +08:00
|
|
|
unsigned
|
|
|
|
MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
2011-11-24 06:19:28 +08:00
|
|
|
assert(MI.getOperand(OpNo-1).isImm());
|
|
|
|
assert(MI.getOperand(OpNo).isImm());
|
2011-12-08 06:35:30 +08:00
|
|
|
unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups);
|
|
|
|
unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups);
|
2011-11-24 06:19:28 +08:00
|
|
|
|
2011-12-08 06:35:30 +08:00
|
|
|
return Position + Size - 1;
|
2011-11-12 06:58:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "MipsGenMCCodeEmitter.inc"
|
|
|
|
|