2012-04-13 01:55:53 +08:00
|
|
|
//===- HexagonInstPrinter.cpp - Convert Hexagon MCInst to assembly 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 Hexagon MCInst to a .s file.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "HexagonAsmPrinter.h"
|
2013-02-21 00:13:27 +08:00
|
|
|
#include "Hexagon.h"
|
|
|
|
#include "HexagonInstPrinter.h"
|
2015-02-20 05:10:50 +08:00
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2012-04-13 01:55:53 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2012-04-13 01:55:53 +08:00
|
|
|
#define GET_INSTRUCTION_NAME
|
|
|
|
#include "HexagonGenAsmWriter.inc"
|
|
|
|
|
2013-02-21 00:13:27 +08:00
|
|
|
const char HexagonInstPrinter::PacketPadding = '\t';
|
2014-10-16 02:27:40 +08:00
|
|
|
// Return the minimum value that a constant extendable operand can have
|
|
|
|
// without being extended.
|
|
|
|
static int getMinValue(uint64_t TSFlags) {
|
|
|
|
unsigned isSigned =
|
|
|
|
(TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
|
|
|
|
unsigned bits =
|
|
|
|
(TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
|
|
|
|
|
|
|
|
if (isSigned)
|
|
|
|
return -1U << (bits - 1);
|
2014-10-20 21:08:19 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-10-16 02:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the maximum value that a constant extendable operand can have
|
|
|
|
// without being extended.
|
|
|
|
static int getMaxValue(uint64_t TSFlags) {
|
|
|
|
unsigned isSigned =
|
|
|
|
(TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
|
|
|
|
unsigned bits =
|
|
|
|
(TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
|
|
|
|
|
|
|
|
if (isSigned)
|
|
|
|
return ~(-1U << (bits - 1));
|
2014-10-20 21:08:19 +08:00
|
|
|
|
|
|
|
return ~(-1U << bits);
|
2014-10-16 02:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the instruction must be extended.
|
|
|
|
static bool isExtended(uint64_t TSFlags) {
|
|
|
|
return (TSFlags >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
|
|
|
|
}
|
|
|
|
|
2014-10-16 03:24:14 +08:00
|
|
|
// Currently just used in an assert statement
|
2014-10-16 04:41:17 +08:00
|
|
|
static bool isExtendable(uint64_t TSFlags) LLVM_ATTRIBUTE_UNUSED;
|
2014-10-16 02:27:40 +08:00
|
|
|
// Return true if the instruction may be extended based on the operand value.
|
|
|
|
static bool isExtendable(uint64_t TSFlags) {
|
|
|
|
return (TSFlags >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
|
|
|
|
}
|
2013-02-21 00:13:27 +08:00
|
|
|
|
2012-04-13 01:55:53 +08:00
|
|
|
StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
|
|
|
|
return MII.getName(Opcode);
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:38:17 +08:00
|
|
|
void HexagonInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
|
|
|
|
OS << getRegisterName(RegNo);
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 05:10:50 +08:00
|
|
|
void HexagonInstPrinter::printInst(MCInst const *MI, raw_ostream &O,
|
2015-03-28 04:36:02 +08:00
|
|
|
StringRef Annot,
|
|
|
|
const MCSubtargetInfo &STI) {
|
2012-04-13 01:55:53 +08:00
|
|
|
const char startPacket = '{',
|
|
|
|
endPacket = '}';
|
|
|
|
// TODO: add outer HW loop when it's supported too.
|
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP0) {
|
2012-05-04 05:52:53 +08:00
|
|
|
// Ending a harware loop is different from ending an regular packet.
|
2015-02-20 05:10:50 +08:00
|
|
|
assert(HexagonMCInstrInfo::isPacketEnd(*MI) && "Loop-end must also end the packet");
|
2012-05-04 05:52:53 +08:00
|
|
|
|
2015-02-20 05:10:50 +08:00
|
|
|
if (HexagonMCInstrInfo::isPacketBegin(*MI)) {
|
2012-05-04 05:52:53 +08:00
|
|
|
// There must be a packet to end a loop.
|
|
|
|
// FIXME: when shuffling is always run, this shouldn't be needed.
|
2015-02-20 05:10:50 +08:00
|
|
|
MCInst Nop;
|
2012-05-04 05:52:53 +08:00
|
|
|
StringRef NoAnnot;
|
|
|
|
|
2014-11-26 05:23:07 +08:00
|
|
|
Nop.setOpcode (Hexagon::A2_nop);
|
2015-02-20 05:10:50 +08:00
|
|
|
HexagonMCInstrInfo::setPacketBegin (Nop, HexagonMCInstrInfo::isPacketBegin(*MI));
|
2015-03-28 04:36:02 +08:00
|
|
|
printInst (&Nop, O, NoAnnot, STI);
|
2012-05-04 05:52:53 +08:00
|
|
|
}
|
2012-04-13 05:06:38 +08:00
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
// Close the packet.
|
2015-02-20 05:10:50 +08:00
|
|
|
if (HexagonMCInstrInfo::isPacketEnd(*MI))
|
2013-02-21 00:13:27 +08:00
|
|
|
O << PacketPadding << endPacket;
|
2012-05-04 05:52:53 +08:00
|
|
|
|
|
|
|
printInstruction(MI, O);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Prefix the insn opening the packet.
|
2015-02-20 05:10:50 +08:00
|
|
|
if (HexagonMCInstrInfo::isPacketBegin(*MI))
|
2013-02-21 00:13:27 +08:00
|
|
|
O << PacketPadding << startPacket << '\n';
|
2012-05-04 05:52:53 +08:00
|
|
|
|
|
|
|
printInstruction(MI, O);
|
|
|
|
|
|
|
|
// Suffix the insn closing the packet.
|
2015-02-20 05:10:50 +08:00
|
|
|
if (HexagonMCInstrInfo::isPacketEnd(*MI))
|
2012-05-04 05:52:53 +08:00
|
|
|
// Suffix the packet in a new line always, since the GNU assembler has
|
|
|
|
// issues with a closing brace on the same line as CONST{32,64}.
|
2013-02-21 00:13:27 +08:00
|
|
|
O << '\n' << PacketPadding << endPacket;
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printAnnotation(O, Annot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
|
|
|
const MCOperand& MO = MI->getOperand(OpNo);
|
|
|
|
|
|
|
|
if (MO.isReg()) {
|
2015-04-22 23:38:17 +08:00
|
|
|
printRegName(O, MO.getReg());
|
2012-04-13 01:55:53 +08:00
|
|
|
} else if(MO.isExpr()) {
|
|
|
|
O << *MO.getExpr();
|
|
|
|
} else if(MO.isImm()) {
|
|
|
|
printImmOperand(MI, OpNo, O);
|
|
|
|
} else {
|
2012-05-04 05:52:53 +08:00
|
|
|
llvm_unreachable("Unknown operand");
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2013-02-21 00:13:27 +08:00
|
|
|
const MCOperand& MO = MI->getOperand(OpNo);
|
|
|
|
|
|
|
|
if(MO.isExpr()) {
|
|
|
|
O << *MO.getExpr();
|
|
|
|
} else if(MO.isImm()) {
|
|
|
|
O << MI->getOperand(OpNo).getImm();
|
|
|
|
} else {
|
|
|
|
llvm_unreachable("Unknown operand");
|
|
|
|
}
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
|
2012-05-04 05:52:53 +08:00
|
|
|
raw_ostream &O) const {
|
2014-10-16 02:27:40 +08:00
|
|
|
const MCOperand &MO = MI->getOperand(OpNo);
|
|
|
|
const MCInstrDesc &MII = getMII().get(MI->getOpcode());
|
|
|
|
|
|
|
|
assert((isExtendable(MII.TSFlags) || isExtended(MII.TSFlags)) &&
|
|
|
|
"Expecting an extendable operand");
|
|
|
|
|
|
|
|
if (MO.isExpr() || isExtended(MII.TSFlags)) {
|
2013-02-21 00:13:27 +08:00
|
|
|
O << "#";
|
2014-10-16 02:27:40 +08:00
|
|
|
} else if (MO.isImm()) {
|
|
|
|
int ImmValue = MO.getImm();
|
|
|
|
if (ImmValue < getMinValue(MII.TSFlags) ||
|
|
|
|
ImmValue > getMaxValue(MII.TSFlags))
|
|
|
|
O << "#";
|
|
|
|
}
|
2013-02-21 00:13:27 +08:00
|
|
|
printOperand(MI, OpNo, O);
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
|
|
|
|
unsigned OpNo, raw_ostream &O) const {
|
2012-04-13 01:55:53 +08:00
|
|
|
O << MI->getOperand(OpNo).getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-05-15 03:35:42 +08:00
|
|
|
O << -MI->getOperand(OpNo).getImm();
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-13 01:55:53 +08:00
|
|
|
O << -1;
|
|
|
|
}
|
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-13 01:55:53 +08:00
|
|
|
const MCOperand& MO0 = MI->getOperand(OpNo);
|
|
|
|
const MCOperand& MO1 = MI->getOperand(OpNo + 1);
|
|
|
|
|
2015-04-22 23:38:17 +08:00
|
|
|
printRegName(O, MO0.getReg());
|
2012-05-15 03:35:42 +08:00
|
|
|
O << " + #" << MO1.getImm();
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-04 05:52:53 +08:00
|
|
|
void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-13 01:55:53 +08:00
|
|
|
const MCOperand& MO0 = MI->getOperand(OpNo);
|
|
|
|
const MCOperand& MO1 = MI->getOperand(OpNo + 1);
|
|
|
|
|
2015-04-22 23:38:17 +08:00
|
|
|
printRegName(O, MO0.getReg());
|
|
|
|
O << ", #" << MO1.getImm();
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-21 19:24:55 +08:00
|
|
|
assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-21 19:24:55 +08:00
|
|
|
assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
2012-04-21 19:24:55 +08:00
|
|
|
assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
|
2012-04-13 01:55:53 +08:00
|
|
|
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
|
|
|
// Branches can take an immediate operand. This is used by the branch
|
|
|
|
// selection pass to print $+8, an eight byte displacement from the PC.
|
2013-06-29 07:46:19 +08:00
|
|
|
llvm_unreachable("Unknown branch operand.");
|
2012-04-13 01:55:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) const {
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O, bool hi) const {
|
2013-07-03 01:24:00 +08:00
|
|
|
assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
|
2013-07-02 07:06:23 +08:00
|
|
|
|
2013-07-03 01:24:00 +08:00
|
|
|
O << '#' << (hi ? "HI" : "LO") << "(#";
|
2013-07-02 07:06:23 +08:00
|
|
|
printOperand(MI, OpNo, O);
|
2012-04-13 01:55:53 +08:00
|
|
|
O << ')';
|
|
|
|
}
|