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"
15 #include "HexagonAsmPrinter.h"
17 #include "HexagonInstPrinter.h"
18 #include "MCTargetDesc/HexagonMCInst.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/Support/raw_ostream.h"
27 #define GET_INSTRUCTION_NAME
28 #include "HexagonGenAsmWriter.inc"
30 const char HexagonInstPrinter::PacketPadding = '\t';
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 startPacket = '{',
49 // TODO: add outer HW loop when it's supported too.
50 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
51 // Ending a harware loop is different from ending an regular packet.
52 assert(MI->isPacketEnd() && "Loop-end must also end the packet");
54 if (MI->isPacketStart()) {
55 // There must be a packet to end a loop.
56 // FIXME: when shuffling is always run, this shouldn't be needed.
60 Nop.setOpcode (Hexagon::NOP);
61 Nop.setPacketStart (MI->isPacketStart());
62 printInst (&Nop, O, NoAnnot);
66 if (MI->isPacketEnd())
67 O << PacketPadding << endPacket;
69 printInstruction(MI, O);
72 // Prefix the insn opening the packet.
73 if (MI->isPacketStart())
74 O << PacketPadding << startPacket << '\n';
76 printInstruction(MI, O);
78 // Suffix the insn closing the packet.
79 if (MI->isPacketEnd())
80 // Suffix the packet in a new line always, since the GNU assembler has
81 // issues with a closing brace on the same line as CONST{32,64}.
82 O << '\n' << PacketPadding << endPacket;
85 printAnnotation(O, Annot);
88 void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
89 raw_ostream &O) const {
90 const MCOperand& MO = MI->getOperand(OpNo);
93 O << getRegisterName(MO.getReg());
94 } else if(MO.isExpr()) {
96 } else if(MO.isImm()) {
97 printImmOperand(MI, OpNo, O);
99 llvm_unreachable("Unknown operand");
103 void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
104 raw_ostream &O) const {
105 const MCOperand& MO = MI->getOperand(OpNo);
109 } else if(MO.isImm()) {
110 O << MI->getOperand(OpNo).getImm();
112 llvm_unreachable("Unknown operand");
116 void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
117 raw_ostream &O) const {
118 const HexagonMCInst *HMCI = static_cast<const HexagonMCInst*>(MI);
119 if (HMCI->isConstExtended())
121 printOperand(MI, OpNo, O);
124 void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
125 unsigned OpNo, raw_ostream &O) const {
126 O << MI->getOperand(OpNo).getImm();
129 void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
130 raw_ostream &O) const {
131 O << -MI->getOperand(OpNo).getImm();
134 void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
135 raw_ostream &O) const {
139 void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
140 raw_ostream &O) const {
141 const MCOperand& MO0 = MI->getOperand(OpNo);
142 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
144 O << getRegisterName(MO0.getReg());
145 O << " + #" << MO1.getImm();
148 void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
149 raw_ostream &O) const {
150 const MCOperand& MO0 = MI->getOperand(OpNo);
151 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
153 O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
156 void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
157 raw_ostream &O) const {
158 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
160 printOperand(MI, OpNo, O);
163 void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
164 raw_ostream &O) const {
165 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
167 printOperand(MI, OpNo, O);
170 void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
171 raw_ostream &O) const {
172 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
174 printOperand(MI, OpNo, O);
177 void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
178 raw_ostream &O) const {
179 // Branches can take an immediate operand. This is used by the branch
180 // selection pass to print $+8, an eight byte displacement from the PC.
181 llvm_unreachable("Unknown branch operand.");
184 void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
185 raw_ostream &O) const {
188 void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
189 raw_ostream &O) const {
192 void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
193 raw_ostream &O) const {
196 void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
197 raw_ostream &O, bool hi) const {
198 assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
200 O << '#' << (hi ? "HI" : "LO") << "(#";
201 printOperand(MI, OpNo, O);