2016-03-28 21:09:54 +08:00
|
|
|
//===-- LanaiInstPrinter.cpp - Convert Lanai MCInst to asm syntax ---------===//
|
|
|
|
//
|
|
|
|
// 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 Lanai MCInst to a .s file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LanaiInstPrinter.h"
|
2016-05-21 05:41:53 +08:00
|
|
|
#include "Lanai.h"
|
2016-03-28 21:09:54 +08:00
|
|
|
#include "MCTargetDesc/LanaiMCExpr.h"
|
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/FormattedStream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
|
|
|
// Include the auto-generated portion of the assembly writer.
|
|
|
|
#define PRINT_ALIAS_INSTR
|
|
|
|
#include "LanaiGenAsmWriter.inc"
|
|
|
|
|
2016-04-03 08:49:27 +08:00
|
|
|
void LanaiInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
|
|
|
OS << StringRef(getRegisterName(RegNo)).lower();
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
2016-04-03 08:49:27 +08:00
|
|
|
bool LanaiInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
|
2016-03-28 21:09:54 +08:00
|
|
|
StringRef Alias, unsigned OpNo0,
|
|
|
|
unsigned OpNo1) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "\t" << Alias << " ";
|
|
|
|
printOperand(MI, OpNo0, OS);
|
|
|
|
OS << ", ";
|
|
|
|
printOperand(MI, OpNo1, OS);
|
2016-03-28 21:09:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool usesGivenOffset(const MCInst *MI, int AddOffset) {
|
|
|
|
unsigned AluCode = MI->getOperand(3).getImm();
|
|
|
|
return LPAC::encodeLanaiAluCode(AluCode) == LPAC::ADD &&
|
|
|
|
(MI->getOperand(2).getImm() == AddOffset ||
|
|
|
|
MI->getOperand(2).getImm() == -AddOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPreIncrementForm(const MCInst *MI, int AddOffset) {
|
|
|
|
unsigned AluCode = MI->getOperand(3).getImm();
|
|
|
|
return LPAC::isPreOp(AluCode) && usesGivenOffset(MI, AddOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPostIncrementForm(const MCInst *MI, int AddOffset) {
|
|
|
|
unsigned AluCode = MI->getOperand(3).getImm();
|
|
|
|
return LPAC::isPostOp(AluCode) && usesGivenOffset(MI, AddOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef decIncOperator(const MCInst *MI) {
|
|
|
|
if (MI->getOperand(2).getImm() < 0)
|
|
|
|
return "--";
|
|
|
|
return "++";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LanaiInstPrinter::printMemoryLoadIncrement(const MCInst *MI,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS,
|
2016-03-28 21:09:54 +08:00
|
|
|
StringRef Opcode,
|
|
|
|
int AddOffset) {
|
|
|
|
if (isPreIncrementForm(MI, AddOffset)) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "\t" << Opcode << "\t[" << decIncOperator(MI) << "%"
|
|
|
|
<< getRegisterName(MI->getOperand(1).getReg()) << "], %"
|
|
|
|
<< getRegisterName(MI->getOperand(0).getReg());
|
2016-03-28 21:09:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isPostIncrementForm(MI, AddOffset)) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "\t" << Opcode << "\t[%"
|
|
|
|
<< getRegisterName(MI->getOperand(1).getReg()) << decIncOperator(MI)
|
|
|
|
<< "], %" << getRegisterName(MI->getOperand(0).getReg());
|
2016-03-28 21:09:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LanaiInstPrinter::printMemoryStoreIncrement(const MCInst *MI,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS,
|
2016-03-28 21:09:54 +08:00
|
|
|
StringRef Opcode,
|
|
|
|
int AddOffset) {
|
|
|
|
if (isPreIncrementForm(MI, AddOffset)) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "\t" << Opcode << "\t%" << getRegisterName(MI->getOperand(0).getReg())
|
|
|
|
<< ", [" << decIncOperator(MI) << "%"
|
|
|
|
<< getRegisterName(MI->getOperand(1).getReg()) << "]";
|
2016-03-28 21:09:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isPostIncrementForm(MI, AddOffset)) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "\t" << Opcode << "\t%" << getRegisterName(MI->getOperand(0).getReg())
|
|
|
|
<< ", [%" << getRegisterName(MI->getOperand(1).getReg())
|
|
|
|
<< decIncOperator(MI) << "]";
|
2016-03-28 21:09:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-03 08:49:27 +08:00
|
|
|
bool LanaiInstPrinter::printAlias(const MCInst *MI, raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
case Lanai::LDW_RI:
|
|
|
|
// ld 4[*%rN], %rX => ld [++imm], %rX
|
|
|
|
// ld -4[*%rN], %rX => ld [--imm], %rX
|
|
|
|
// ld 4[%rN*], %rX => ld [imm++], %rX
|
|
|
|
// ld -4[%rN*], %rX => ld [imm--], %rX
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryLoadIncrement(MI, OS, "ld", 4);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::LDHs_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryLoadIncrement(MI, OS, "ld.h", 2);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::LDHz_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryLoadIncrement(MI, OS, "uld.h", 2);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::LDBs_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryLoadIncrement(MI, OS, "ld.b", 1);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::LDBz_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryLoadIncrement(MI, OS, "uld.b", 1);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::SW_RI:
|
|
|
|
// st %rX, 4[*%rN] => st %rX, [++imm]
|
|
|
|
// st %rX, -4[*%rN] => st %rX, [--imm]
|
|
|
|
// st %rX, 4[%rN*] => st %rX, [imm++]
|
|
|
|
// st %rX, -4[%rN*] => st %rX, [imm--]
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryStoreIncrement(MI, OS, "st", 4);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::STH_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryStoreIncrement(MI, OS, "st.h", 2);
|
2016-03-28 21:09:54 +08:00
|
|
|
case Lanai::STB_RI:
|
2016-04-03 08:49:27 +08:00
|
|
|
return printMemoryStoreIncrement(MI, OS, "st.b", 1);
|
2016-03-28 21:09:54 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 08:49:27 +08:00
|
|
|
void LanaiInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
|
2016-03-28 21:09:54 +08:00
|
|
|
StringRef Annotation,
|
2016-07-16 06:38:32 +08:00
|
|
|
const MCSubtargetInfo & /*STI*/) {
|
2016-05-21 05:41:53 +08:00
|
|
|
if (!printAlias(MI, OS) && !printAliasInstr(MI, OS))
|
|
|
|
printInstruction(MI, OS);
|
2016-04-03 08:49:27 +08:00
|
|
|
printAnnotation(OS, Annotation);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS, const char *Modifier) {
|
2016-03-28 21:09:54 +08:00
|
|
|
assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
|
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isReg())
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "%" << getRegisterName(Op.getReg());
|
2016-03-28 21:09:54 +08:00
|
|
|
else if (Op.isImm())
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << formatHex(Op.getImm());
|
2016-03-28 21:09:54 +08:00
|
|
|
else {
|
|
|
|
assert(Op.isExpr() && "Expected an expression");
|
2016-04-03 08:49:27 +08:00
|
|
|
Op.getExpr()->print(OS, &MAI);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printMemImmOperand(const MCInst *MI, unsigned OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isImm()) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << '[' << formatHex(Op.getImm()) << ']';
|
2016-03-28 21:09:54 +08:00
|
|
|
} else {
|
|
|
|
// Symbolic operand will be lowered to immediate value by linker
|
|
|
|
assert(Op.isExpr() && "Expected an expression");
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << '[';
|
|
|
|
Op.getExpr()->print(OS, &MAI);
|
|
|
|
OS << ']';
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printHi16ImmOperand(const MCInst *MI, unsigned OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isImm()) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << formatHex(Op.getImm() << 16);
|
2016-03-28 21:09:54 +08:00
|
|
|
} else {
|
|
|
|
// Symbolic operand will be lowered to immediate value by linker
|
|
|
|
assert(Op.isExpr() && "Expected an expression");
|
2016-04-03 08:49:27 +08:00
|
|
|
Op.getExpr()->print(OS, &MAI);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printHi16AndImmOperand(const MCInst *MI, unsigned OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isImm()) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << formatHex((Op.getImm() << 16) | 0xffff);
|
2016-03-28 21:09:54 +08:00
|
|
|
} else {
|
|
|
|
// Symbolic operand will be lowered to immediate value by linker
|
|
|
|
assert(Op.isExpr() && "Expected an expression");
|
2016-04-03 08:49:27 +08:00
|
|
|
Op.getExpr()->print(OS, &MAI);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printLo16AndImmOperand(const MCInst *MI, unsigned OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &Op = MI->getOperand(OpNo);
|
|
|
|
if (Op.isImm()) {
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << formatHex(0xffff0000 | Op.getImm());
|
2016-03-28 21:09:54 +08:00
|
|
|
} else {
|
|
|
|
// Symbolic operand will be lowered to immediate value by linker
|
|
|
|
assert(Op.isExpr() && "Expected an expression");
|
2016-04-03 08:49:27 +08:00
|
|
|
Op.getExpr()->print(OS, &MAI);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 08:49:27 +08:00
|
|
|
static void printMemoryBaseRegister(raw_ostream &OS, const unsigned AluCode,
|
|
|
|
const MCOperand &RegOp) {
|
2016-03-28 21:09:54 +08:00
|
|
|
assert(RegOp.isReg() && "Register operand expected");
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "[";
|
2016-05-21 05:41:53 +08:00
|
|
|
if (LPAC::isPreOp(AluCode))
|
|
|
|
OS << "*";
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "%" << LanaiInstPrinter::getRegisterName(RegOp.getReg());
|
2016-05-21 05:41:53 +08:00
|
|
|
if (LPAC::isPostOp(AluCode))
|
|
|
|
OS << "*";
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "]";
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned SizeInBits>
|
|
|
|
static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
|
|
|
|
const MCOperand &OffsetOp,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-03-28 21:09:54 +08:00
|
|
|
assert((OffsetOp.isImm() || OffsetOp.isExpr()) && "Immediate expected");
|
|
|
|
if (OffsetOp.isImm()) {
|
|
|
|
assert(isInt<SizeInBits>(OffsetOp.getImm()) && "Constant value truncated");
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << OffsetOp.getImm();
|
2016-03-28 21:09:54 +08:00
|
|
|
} else
|
2016-04-03 08:49:27 +08:00
|
|
|
OffsetOp.getExpr()->print(OS, &MAI);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS,
|
2016-07-16 06:38:32 +08:00
|
|
|
const char * /*Modifier*/) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &RegOp = MI->getOperand(OpNo);
|
|
|
|
const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
|
|
|
|
const MCOperand &AluOp = MI->getOperand(OpNo + 2);
|
|
|
|
const unsigned AluCode = AluOp.getImm();
|
|
|
|
|
|
|
|
// Offset
|
2016-04-03 08:49:27 +08:00
|
|
|
printMemoryImmediateOffset<16>(MAI, OffsetOp, OS);
|
2016-03-28 21:09:54 +08:00
|
|
|
|
|
|
|
// Register
|
2016-04-03 08:49:27 +08:00
|
|
|
printMemoryBaseRegister(OS, AluCode, RegOp);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS,
|
2016-07-16 06:38:32 +08:00
|
|
|
const char * /*Modifier*/) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &RegOp = MI->getOperand(OpNo);
|
|
|
|
const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
|
|
|
|
const MCOperand &AluOp = MI->getOperand(OpNo + 2);
|
|
|
|
const unsigned AluCode = AluOp.getImm();
|
|
|
|
assert(OffsetOp.isReg() && RegOp.isReg() && "Registers expected.");
|
|
|
|
|
|
|
|
// [ Base OP Offset ]
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "[";
|
2016-05-21 05:41:53 +08:00
|
|
|
if (LPAC::isPreOp(AluCode))
|
|
|
|
OS << "*";
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << "%" << getRegisterName(RegOp.getReg());
|
2016-05-21 05:41:53 +08:00
|
|
|
if (LPAC::isPostOp(AluCode))
|
|
|
|
OS << "*";
|
2016-04-03 08:49:27 +08:00
|
|
|
OS << " " << LPAC::lanaiAluCodeToString(AluCode) << " ";
|
|
|
|
OS << "%" << getRegisterName(OffsetOp.getReg());
|
|
|
|
OS << "]";
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS,
|
2016-07-16 06:38:32 +08:00
|
|
|
const char * /*Modifier*/) {
|
2016-03-28 21:09:54 +08:00
|
|
|
const MCOperand &RegOp = MI->getOperand(OpNo);
|
|
|
|
const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
|
|
|
|
const MCOperand &AluOp = MI->getOperand(OpNo + 2);
|
|
|
|
const unsigned AluCode = AluOp.getImm();
|
|
|
|
|
|
|
|
// Offset
|
2016-04-03 08:49:27 +08:00
|
|
|
printMemoryImmediateOffset<10>(MAI, OffsetOp, OS);
|
2016-03-28 21:09:54 +08:00
|
|
|
|
|
|
|
// Register
|
2016-04-03 08:49:27 +08:00
|
|
|
printMemoryBaseRegister(OS, AluCode, RegOp);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printCCOperand(const MCInst *MI, int OpNo,
|
2016-04-03 08:49:27 +08:00
|
|
|
raw_ostream &OS) {
|
2016-07-08 07:36:04 +08:00
|
|
|
LPCC::CondCode CC =
|
|
|
|
static_cast<LPCC::CondCode>(MI->getOperand(OpNo).getImm());
|
|
|
|
// Handle the undefined value here for printing so we don't abort().
|
|
|
|
if (CC >= LPCC::UNKNOWN)
|
|
|
|
OS << "<und>";
|
|
|
|
else
|
|
|
|
OS << lanaiCondCodeToString(CC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanaiInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
LPCC::CondCode CC =
|
|
|
|
static_cast<LPCC::CondCode>(MI->getOperand(OpNo).getImm());
|
|
|
|
// Handle the undefined value here for printing so we don't abort().
|
|
|
|
if (CC >= LPCC::UNKNOWN)
|
|
|
|
OS << "<und>";
|
|
|
|
else if (CC != LPCC::ICC_T)
|
|
|
|
OS << "." << lanaiCondCodeToString(CC);
|
2016-03-28 21:09:54 +08:00
|
|
|
}
|