| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 1 | //===- HexagonInstPrinter.cpp - Convert Hexagon MCInst to assembly syntax -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class prints an Hexagon MCInst to a .s file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "asm-printer" |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 15 | #include "HexagonAsmPrinter.h" |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 16 | #include "Hexagon.h" |
| 17 | #include "HexagonInstPrinter.h" |
| 18 | #include "MCTargetDesc/HexagonMCInst.h" |
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame^] | 22 | #include "llvm/MC/MCInst.h" |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | #define GET_INSTRUCTION_NAME |
| 28 | #include "HexagonGenAsmWriter.inc" |
| 29 | |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 30 | const char HexagonInstPrinter::PacketPadding = '\t'; |
| 31 | |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 32 | StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const { |
| 33 | return MII.getName(Opcode); |
| 34 | } |
| 35 | |
| 36 | StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const { |
| 37 | return getRegisterName(RegNo); |
| 38 | } |
| 39 | |
| 40 | void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &O, |
| 41 | StringRef Annot) { |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 42 | printInst((const HexagonMCInst*)(MI), O, Annot); |
| 43 | } |
| 44 | |
| 45 | void HexagonInstPrinter::printInst(const HexagonMCInst *MI, raw_ostream &O, |
| 46 | StringRef Annot) { |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 47 | const char startPacket = '{', |
| 48 | endPacket = '}'; |
| 49 | // TODO: add outer HW loop when it's supported too. |
| 50 | if (MI->getOpcode() == Hexagon::ENDLOOP0) { |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 51 | // Ending a harware loop is different from ending an regular packet. |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 52 | assert(MI->isPacketEnd() && "Loop-end must also end the packet"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 53 | |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 54 | if (MI->isPacketStart()) { |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 55 | // There must be a packet to end a loop. |
| 56 | // FIXME: when shuffling is always run, this shouldn't be needed. |
| 57 | HexagonMCInst Nop; |
| 58 | StringRef NoAnnot; |
| 59 | |
| 60 | Nop.setOpcode (Hexagon::NOP); |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 61 | Nop.setPacketStart (MI->isPacketStart()); |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 62 | printInst (&Nop, O, NoAnnot); |
| 63 | } |
| 64 | |
| 65 | // Close the packet. |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 66 | if (MI->isPacketEnd()) |
| 67 | O << PacketPadding << endPacket; |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 68 | |
| 69 | printInstruction(MI, O); |
| 70 | } |
| 71 | else { |
| 72 | // Prefix the insn opening the packet. |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 73 | if (MI->isPacketStart()) |
| 74 | O << PacketPadding << startPacket << '\n'; |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 75 | |
| 76 | printInstruction(MI, O); |
| 77 | |
| 78 | // Suffix the insn closing the packet. |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 79 | if (MI->isPacketEnd()) |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 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}. |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 82 | O << '\n' << PacketPadding << endPacket; |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 85 | printAnnotation(O, Annot); |
| 86 | } |
| 87 | |
| 88 | void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 89 | raw_ostream &O) const { |
| 90 | const MCOperand& MO = MI->getOperand(OpNo); |
| 91 | |
| 92 | if (MO.isReg()) { |
| 93 | O << getRegisterName(MO.getReg()); |
| 94 | } else if(MO.isExpr()) { |
| 95 | O << *MO.getExpr(); |
| 96 | } else if(MO.isImm()) { |
| 97 | printImmOperand(MI, OpNo, O); |
| 98 | } else { |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 99 | llvm_unreachable("Unknown operand"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 103 | void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo, |
| 104 | raw_ostream &O) const { |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 105 | const MCOperand& MO = MI->getOperand(OpNo); |
| 106 | |
| 107 | if(MO.isExpr()) { |
| 108 | O << *MO.getExpr(); |
| 109 | } else if(MO.isImm()) { |
| 110 | O << MI->getOperand(OpNo).getImm(); |
| 111 | } else { |
| 112 | llvm_unreachable("Unknown operand"); |
| 113 | } |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo, |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 117 | raw_ostream &O) const { |
| Jyotsna Verma | 7503a62 | 2013-02-20 16:13:27 +0000 | [diff] [blame] | 118 | const HexagonMCInst *HMCI = static_cast<const HexagonMCInst*>(MI); |
| 119 | if (HMCI->isConstExtended()) |
| 120 | O << "#"; |
| 121 | printOperand(MI, OpNo, O); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 124 | void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI, |
| 125 | unsigned OpNo, raw_ostream &O) const { |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 126 | O << MI->getOperand(OpNo).getImm(); |
| 127 | } |
| 128 | |
| 129 | void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo, |
| 130 | raw_ostream &O) const { |
| Brendon Cahoon | f6b687e | 2012-05-14 19:35:42 +0000 | [diff] [blame] | 131 | O << -MI->getOperand(OpNo).getImm(); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 134 | void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo, |
| 135 | raw_ostream &O) const { |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 136 | O << -1; |
| 137 | } |
| 138 | |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 139 | void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo, |
| 140 | raw_ostream &O) const { |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 141 | const MCOperand& MO0 = MI->getOperand(OpNo); |
| 142 | const MCOperand& MO1 = MI->getOperand(OpNo + 1); |
| 143 | |
| 144 | O << getRegisterName(MO0.getReg()); |
| Brendon Cahoon | f6b687e | 2012-05-14 19:35:42 +0000 | [diff] [blame] | 145 | O << " + #" << MO1.getImm(); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| Sirish Pande | f8e5e3c | 2012-05-03 21:52:53 +0000 | [diff] [blame] | 148 | void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo, |
| 149 | raw_ostream &O) const { |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 150 | const MCOperand& MO0 = MI->getOperand(OpNo); |
| 151 | const MCOperand& MO1 = MI->getOperand(OpNo + 1); |
| 152 | |
| 153 | O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm(); |
| 154 | } |
| 155 | |
| 156 | void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo, |
| 157 | raw_ostream &O) const { |
| NAKAMURA Takumi | df3d5ea | 2012-04-21 11:24:55 +0000 | [diff] [blame] | 158 | assert(MI->getOperand(OpNo).isExpr() && "Expecting expression"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 159 | |
| 160 | printOperand(MI, OpNo, O); |
| 161 | } |
| 162 | |
| 163 | void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo, |
| 164 | raw_ostream &O) const { |
| NAKAMURA Takumi | df3d5ea | 2012-04-21 11:24:55 +0000 | [diff] [blame] | 165 | assert(MI->getOperand(OpNo).isExpr() && "Expecting expression"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 166 | |
| 167 | printOperand(MI, OpNo, O); |
| 168 | } |
| 169 | |
| 170 | void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo, |
| 171 | raw_ostream &O) const { |
| NAKAMURA Takumi | df3d5ea | 2012-04-21 11:24:55 +0000 | [diff] [blame] | 172 | assert(MI->getOperand(OpNo).isExpr() && "Expecting expression"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 173 | |
| 174 | printOperand(MI, OpNo, O); |
| 175 | } |
| 176 | |
| 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. |
| Richard Trieu | d7fd95a | 2013-06-28 23:46:19 +0000 | [diff] [blame] | 181 | llvm_unreachable("Unknown branch operand."); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo, |
| 185 | raw_ostream &O) const { |
| 186 | } |
| 187 | |
| 188 | void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo, |
| 189 | raw_ostream &O) const { |
| 190 | } |
| 191 | |
| 192 | void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo, |
| 193 | raw_ostream &O) const { |
| 194 | } |
| 195 | |
| 196 | void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo, |
| 197 | raw_ostream &O, bool hi) const { |
| Benjamin Kramer | 755bf4f | 2013-07-02 17:24:00 +0000 | [diff] [blame] | 198 | assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand"); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 199 | |
| Benjamin Kramer | 755bf4f | 2013-07-02 17:24:00 +0000 | [diff] [blame] | 200 | O << '#' << (hi ? "HI" : "LO") << "(#"; |
| Richard Trieu | fab01e5 | 2013-07-01 23:06:23 +0000 | [diff] [blame] | 201 | printOperand(MI, OpNo, O); |
| Evandro Menezes | 5cee621 | 2012-04-12 17:55:53 +0000 | [diff] [blame] | 202 | O << ')'; |
| 203 | } |