2012-02-17 08:55:11 +00:00
|
|
|
//===-- MipsInstPrinter.cpp - Convert Mips MCInst to assembly syntax ------===//
|
2011-07-07 23:56:50 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class prints an Mips MCInst to a .s file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
#include "MipsInstPrinter.h"
|
2012-07-05 19:26:38 +00:00
|
|
|
#include "MipsInstrInfo.h"
|
2011-11-08 22:26:47 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2011-07-07 23:56:50 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
2012-04-02 07:01:04 +00:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2011-11-08 22:26:47 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2011-07-08 20:18:13 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-07-07 23:56:50 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-02-05 08:32:10 +00:00
|
|
|
#define PRINT_ALIAS_INSTR
|
2011-07-07 23:56:50 +00:00
|
|
|
#include "MipsGenAsmWriter.inc"
|
|
|
|
|
[mips] Print instructions "beq", "bne" and "or" using assembler pseudo
instructions "beqz", "bnez" and "move", when possible.
beq $2, $zero, $L1 => beqz $2, $L1
bne $2, $zero, $L1 => bnez $2, $L1
or $2, $3, $zero => move $2, $3
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187229 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-26 18:34:25 +00:00
|
|
|
template<unsigned R>
|
|
|
|
static bool isReg(const MCInst &MI, unsigned OpNo) {
|
|
|
|
assert(MI.getOperand(OpNo).isReg() && "Register operand expected.");
|
|
|
|
return MI.getOperand(OpNo).getReg() == R;
|
|
|
|
}
|
|
|
|
|
2011-07-07 23:56:50 +00:00
|
|
|
const char* Mips::MipsFCCToString(Mips::CondCode CC) {
|
|
|
|
switch (CC) {
|
|
|
|
case FCOND_F:
|
|
|
|
case FCOND_T: return "f";
|
|
|
|
case FCOND_UN:
|
|
|
|
case FCOND_OR: return "un";
|
|
|
|
case FCOND_OEQ:
|
|
|
|
case FCOND_UNE: return "eq";
|
|
|
|
case FCOND_UEQ:
|
|
|
|
case FCOND_ONE: return "ueq";
|
|
|
|
case FCOND_OLT:
|
|
|
|
case FCOND_UGE: return "olt";
|
|
|
|
case FCOND_ULT:
|
|
|
|
case FCOND_OGE: return "ult";
|
|
|
|
case FCOND_OLE:
|
|
|
|
case FCOND_UGT: return "ole";
|
|
|
|
case FCOND_ULE:
|
|
|
|
case FCOND_OGT: return "ule";
|
|
|
|
case FCOND_SF:
|
|
|
|
case FCOND_ST: return "sf";
|
|
|
|
case FCOND_NGLE:
|
|
|
|
case FCOND_GLE: return "ngle";
|
|
|
|
case FCOND_SEQ:
|
|
|
|
case FCOND_SNE: return "seq";
|
|
|
|
case FCOND_NGL:
|
|
|
|
case FCOND_GL: return "ngl";
|
|
|
|
case FCOND_LT:
|
|
|
|
case FCOND_NLT: return "lt";
|
|
|
|
case FCOND_NGE:
|
|
|
|
case FCOND_GE: return "nge";
|
|
|
|
case FCOND_LE:
|
|
|
|
case FCOND_NLE: return "le";
|
|
|
|
case FCOND_NGT:
|
|
|
|
case FCOND_GT: return "ngt";
|
|
|
|
}
|
2011-07-08 20:18:13 +00:00
|
|
|
llvm_unreachable("Impossible condition code!");
|
2011-07-07 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MipsInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
2011-11-06 20:37:06 +00:00
|
|
|
OS << '$' << StringRef(getRegisterName(RegNo)).lower();
|
2011-07-07 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
2011-09-15 23:38:46 +00:00
|
|
|
void MipsInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
|
|
|
|
StringRef Annot) {
|
2012-07-05 19:26:38 +00:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Mips::RDHWR:
|
|
|
|
case Mips::RDHWR64:
|
|
|
|
O << "\t.set\tpush\n";
|
|
|
|
O << "\t.set\tmips32r2\n";
|
|
|
|
}
|
|
|
|
|
2013-02-05 08:32:10 +00:00
|
|
|
// Try to print any aliases first.
|
[mips] Print instructions "beq", "bne" and "or" using assembler pseudo
instructions "beqz", "bnez" and "move", when possible.
beq $2, $zero, $L1 => beqz $2, $L1
bne $2, $zero, $L1 => bnez $2, $L1
or $2, $3, $zero => move $2, $3
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187229 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-26 18:34:25 +00:00
|
|
|
if (!printAliasInstr(MI, O) && !printAlias(*MI, O))
|
2013-02-05 08:32:10 +00:00
|
|
|
printInstruction(MI, O);
|
2011-09-21 17:58:45 +00:00
|
|
|
printAnnotation(O, Annot);
|
2012-07-05 19:26:38 +00:00
|
|
|
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Mips::RDHWR:
|
|
|
|
case Mips::RDHWR64:
|
|
|
|
O << "\n\t.set\tpop";
|
|
|
|
}
|
2011-07-07 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 22:26:47 +00:00
|
|
|
static void printExpr(const MCExpr *Expr, raw_ostream &OS) {
|
|
|
|
int Offset = 0;
|
|
|
|
const MCSymbolRefExpr *SRE;
|
|
|
|
|
|
|
|
if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
|
|
|
|
SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
|
|
|
|
const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
|
|
|
|
assert(SRE && CE && "Binary expression must be sym+const.");
|
|
|
|
Offset = CE->getValue();
|
|
|
|
}
|
|
|
|
else if (!(SRE = dyn_cast<MCSymbolRefExpr>(Expr)))
|
|
|
|
assert(false && "Unexpected MCExpr type.");
|
|
|
|
|
|
|
|
MCSymbolRefExpr::VariantKind Kind = SRE->getKind();
|
|
|
|
|
|
|
|
switch (Kind) {
|
2012-02-07 02:50:20 +00:00
|
|
|
default: llvm_unreachable("Invalid kind!");
|
2011-12-19 19:52:25 +00:00
|
|
|
case MCSymbolRefExpr::VK_None: break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GPREL: OS << "%gp_rel("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_CALL: OS << "%call16("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT16: OS << "%got("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT: OS << "%got("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_HI: OS << "%hi("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_LO: OS << "%lo("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TLSGD: OS << "%tlsgd("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TLSLDM: OS << "%tlsldm("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_DTPREL_HI: OS << "%dtprel_hi("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_DTPREL_LO: OS << "%dtprel_lo("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOTTPREL: OS << "%gottprel("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TPREL_HI: OS << "%tprel_hi("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_TPREL_LO: OS << "%tprel_lo("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GPOFF_HI: OS << "%hi(%neg(%gp_rel("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GPOFF_LO: OS << "%lo(%neg(%gp_rel("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_DISP: OS << "%got_disp("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_PAGE: OS << "%got_page("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_OFST: OS << "%got_ofst("; break;
|
2012-07-21 03:09:04 +00:00
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHER: OS << "%higher("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHEST: OS << "%highest("; break;
|
2012-11-21 20:40:38 +00:00
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_HI16: OS << "%got_hi("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_GOT_LO16: OS << "%got_lo("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_CALL_HI16: OS << "%call_hi("; break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_CALL_LO16: OS << "%call_lo("; break;
|
2011-11-08 22:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << SRE->getSymbol();
|
|
|
|
|
|
|
|
if (Offset) {
|
|
|
|
if (Offset > 0)
|
|
|
|
OS << '+';
|
|
|
|
OS << Offset;
|
|
|
|
}
|
|
|
|
|
2011-11-11 03:58:36 +00:00
|
|
|
if ((Kind == MCSymbolRefExpr::VK_Mips_GPOFF_HI) ||
|
|
|
|
(Kind == MCSymbolRefExpr::VK_Mips_GPOFF_LO))
|
|
|
|
OS << ")))";
|
|
|
|
else if (Kind != MCSymbolRefExpr::VK_None)
|
2011-11-08 22:26:47 +00:00
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
|
2011-07-07 23:56:50 +00:00
|
|
|
void MipsInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isReg()) {
|
|
|
|
printRegName(O, Op.getReg());
|
|
|
|
return;
|
|
|
|
}
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-07-07 23:56:50 +00:00
|
|
|
if (Op.isImm()) {
|
|
|
|
O << Op.getImm();
|
|
|
|
return;
|
|
|
|
}
|
2012-02-28 07:46:26 +00:00
|
|
|
|
2011-07-07 23:56:50 +00:00
|
|
|
assert(Op.isExpr() && "unknown operand kind in printOperand");
|
2011-11-08 22:26:47 +00:00
|
|
|
printExpr(Op.getExpr(), O);
|
2011-07-07 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MipsInstPrinter::printUnsignedImm(const MCInst *MI, int opNum,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MCOperand &MO = MI->getOperand(opNum);
|
|
|
|
if (MO.isImm())
|
|
|
|
O << (unsigned short int)MO.getImm();
|
|
|
|
else
|
|
|
|
printOperand(MI, opNum, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsInstPrinter::
|
|
|
|
printMemOperand(const MCInst *MI, int opNum, raw_ostream &O) {
|
|
|
|
// Load/Store memory operands -- imm($reg)
|
|
|
|
// If PIC target the target is loaded as the
|
|
|
|
// pattern lw $25,%call16($28)
|
|
|
|
printOperand(MI, opNum+1, O);
|
|
|
|
O << "(";
|
|
|
|
printOperand(MI, opNum, O);
|
|
|
|
O << ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsInstPrinter::
|
|
|
|
printMemOperandEA(const MCInst *MI, int opNum, raw_ostream &O) {
|
|
|
|
// when using stack locations for not load/store instructions
|
|
|
|
// print the same way as all normal 3 operand instructions.
|
|
|
|
printOperand(MI, opNum, O);
|
|
|
|
O << ", ";
|
|
|
|
printOperand(MI, opNum+1, O);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsInstPrinter::
|
|
|
|
printFCCOperand(const MCInst *MI, int opNum, raw_ostream &O) {
|
|
|
|
const MCOperand& MO = MI->getOperand(opNum);
|
|
|
|
O << MipsFCCToString((Mips::CondCode)MO.getImm());
|
|
|
|
}
|
[mips] Print instructions "beq", "bne" and "or" using assembler pseudo
instructions "beqz", "bnez" and "move", when possible.
beq $2, $zero, $L1 => beqz $2, $L1
bne $2, $zero, $L1 => bnez $2, $L1
or $2, $3, $zero => move $2, $3
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187229 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-26 18:34:25 +00:00
|
|
|
|
|
|
|
bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
|
|
|
|
unsigned OpNo, raw_ostream &OS) {
|
|
|
|
OS << "\t" << Str << "\t";
|
|
|
|
printOperand(&MI, OpNo, OS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MipsInstPrinter::printAlias(const char *Str, const MCInst &MI,
|
|
|
|
unsigned OpNo0, unsigned OpNo1,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
printAlias(Str, MI, OpNo0, OS);
|
|
|
|
OS << ", ";
|
|
|
|
printOperand(&MI, OpNo1, OS);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MipsInstPrinter::printAlias(const MCInst &MI, raw_ostream &OS) {
|
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case Mips::BEQ:
|
|
|
|
if (isReg<Mips::ZERO>(MI, 1) && printAlias("beqz", MI, 0, 2, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case Mips::BEQ64:
|
|
|
|
if (isReg<Mips::ZERO_64>(MI, 1) && printAlias("beqz", MI, 0, 2, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case Mips::BNE:
|
|
|
|
if (isReg<Mips::ZERO>(MI, 1) && printAlias("bnez", MI, 0, 2, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case Mips::BNE64:
|
|
|
|
if (isReg<Mips::ZERO_64>(MI, 1) && printAlias("bnez", MI, 0, 2, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
2013-07-26 20:13:47 +00:00
|
|
|
case Mips::BC1T:
|
|
|
|
if (isReg<Mips::FCC0>(MI, 0) && printAlias("bc1t", MI, 1, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
case Mips::BC1F:
|
|
|
|
if (isReg<Mips::FCC0>(MI, 0) && printAlias("bc1f", MI, 1, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
[mips] Print instructions "beq", "bne" and "or" using assembler pseudo
instructions "beqz", "bnez" and "move", when possible.
beq $2, $zero, $L1 => beqz $2, $L1
bne $2, $zero, $L1 => bnez $2, $L1
or $2, $3, $zero => move $2, $3
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187229 91177308-0d34-0410-b5e6-96231b3b80d8
2013-07-26 18:34:25 +00:00
|
|
|
case Mips::OR:
|
|
|
|
if (isReg<Mips::ZERO>(MI, 2) && printAlias("move", MI, 0, 1, OS))
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|