1 //===- HexagonInstPrinter.cpp - Convert Hexagon MCInst to assembly syntax -===//
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 class prints an Hexagon MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "asm-printer"
16 #include "HexagonConstExtInfo.h"
17 #include "HexagonAsmPrinter.h"
18 #include "HexagonInstPrinter.h"
19 #include "HexagonMCInst.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/raw_ostream.h"
29 #define GET_INSTRUCTION_NAME
30 #include "HexagonGenAsmWriter.inc"
32 StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
33 return MII.getName(Opcode);
36 StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
37 return getRegisterName(RegNo);
40 void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
42 printInst((const HexagonMCInst*)(MI), O, Annot);
45 void HexagonInstPrinter::printInst(const HexagonMCInst *MI, raw_ostream &O,
47 const char packetPadding[] = " ";
48 const char startPacket = '{',
50 // TODO: add outer HW loop when it's supported too.
51 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
52 // Ending a harware loop is different from ending an regular packet.
53 assert(MI->isEndPacket() && "Loop end must also end the packet");
55 if (MI->isStartPacket()) {
56 // There must be a packet to end a loop.
57 // FIXME: when shuffling is always run, this shouldn't be needed.
61 Nop.setOpcode (Hexagon::NOP);
62 Nop.setStartPacket (MI->isStartPacket());
63 printInst (&Nop, O, NoAnnot);
67 if (MI->isEndPacket())
68 O << packetPadding << endPacket;
70 printInstruction(MI, O);
73 // Prefix the insn opening the packet.
74 if (MI->isStartPacket())
75 O << packetPadding << startPacket << '\n';
77 printInstruction(MI, O);
79 // Suffix the insn closing the packet.
80 if (MI->isEndPacket())
81 // Suffix the packet in a new line always, since the GNU assembler has
82 // issues with a closing brace on the same line as CONST{32,64}.
83 O << '\n' << packetPadding << endPacket;
86 printAnnotation(O, Annot);
89 void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
90 raw_ostream &O) const {
91 const MCOperand& MO = MI->getOperand(OpNo);
94 O << getRegisterName(MO.getReg());
95 } else if(MO.isExpr()) {
97 } else if(MO.isImm()) {
98 printImmOperand(MI, OpNo, O);
100 llvm_unreachable("Unknown operand");
104 void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
105 raw_ostream &O) const {
106 O << MI->getOperand(OpNo).getImm();
109 void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
110 raw_ostream &O) const {
111 if (isConstExtended(MI))
112 O << "#" << MI->getOperand(OpNo).getImm();
114 O << MI->getOperand(OpNo).getImm();
117 void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
118 unsigned OpNo, raw_ostream &O) const {
119 O << MI->getOperand(OpNo).getImm();
122 void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
123 raw_ostream &O) const {
124 O << -MI->getOperand(OpNo).getImm();
127 void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
128 raw_ostream &O) const {
132 void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
133 raw_ostream &O) const {
134 const MCOperand& MO0 = MI->getOperand(OpNo);
135 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
137 O << getRegisterName(MO0.getReg());
138 if (isConstExtended(MI))
139 O << " + ##" << MO1.getImm();
141 O << " + #" << MO1.getImm();
144 void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
145 raw_ostream &O) const {
146 const MCOperand& MO0 = MI->getOperand(OpNo);
147 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
149 O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
152 void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
153 raw_ostream &O) const {
154 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
156 printOperand(MI, OpNo, O);
159 void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
160 raw_ostream &O) const {
161 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
163 printOperand(MI, OpNo, O);
166 void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
167 raw_ostream &O) const {
168 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
170 printOperand(MI, OpNo, O);
173 void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
174 raw_ostream &O) const {
175 // Branches can take an immediate operand. This is used by the branch
176 // selection pass to print $+8, an eight byte displacement from the PC.
177 assert("Unknown branch operand.");
180 void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
181 raw_ostream &O) const {
184 void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
185 raw_ostream &O) const {
188 void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
189 raw_ostream &O) const {
192 void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
193 raw_ostream &O, bool hi) const {
194 const MCOperand& MO = MI->getOperand(OpNo);
196 O << '#' << (hi? "HI": "LO") << '(';
199 printOperand(MI, OpNo, O);
201 assert("Unknown symbol operand");
202 printOperand(MI, OpNo, O);
207 bool HexagonInstPrinter::isConstExtended(const MCInst *MI) const{
208 unsigned short Opcode = MI->getOpcode();
209 short ExtOpNum = HexagonConstExt::getCExtOpNum(Opcode);
210 int MinValue = HexagonConstExt::getMinValue(Opcode);
211 int MaxValue = HexagonConstExt::getMaxValue(Opcode);
213 // Instruction has no constant extended operand
217 int ImmValue = MI->getOperand(ExtOpNum).getImm();
218 return (ImmValue < MinValue || ImmValue > MaxValue);