blob: 6c87c9fd7852eae9be1bc1e6bbc5936b34fba535 [file] [log] [blame]
Evandro Menezes5cee6212012-04-12 17:55:53 +00001//===- 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
Evandro Menezes5cee6212012-04-12 17:55:53 +000014#include "HexagonAsmPrinter.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000015#include "Hexagon.h"
16#include "HexagonInstPrinter.h"
Colin LeMahieu1174fea2015-02-19 21:10:50 +000017#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/StringExtras.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000019#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCExpr.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/MC/MCInst.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000022#include "llvm/Support/raw_ostream.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000023
24using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "asm-printer"
27
Evandro Menezes5cee6212012-04-12 17:55:53 +000028#define GET_INSTRUCTION_NAME
29#include "HexagonGenAsmWriter.inc"
30
Jyotsna Verma7503a622013-02-20 16:13:27 +000031const char HexagonInstPrinter::PacketPadding = '\t';
Sid Manning12cd21a2014-10-15 18:27:40 +000032// Return the minimum value that a constant extendable operand can have
33// without being extended.
34static int getMinValue(uint64_t TSFlags) {
35 unsigned isSigned =
36 (TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
37 unsigned bits =
38 (TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
39
40 if (isSigned)
41 return -1U << (bits - 1);
Sid Manningc374ac92014-10-20 13:08:19 +000042
43 return 0;
Sid Manning12cd21a2014-10-15 18:27:40 +000044}
45
46// Return the maximum value that a constant extendable operand can have
47// without being extended.
48static int getMaxValue(uint64_t TSFlags) {
49 unsigned isSigned =
50 (TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
51 unsigned bits =
52 (TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
53
54 if (isSigned)
55 return ~(-1U << (bits - 1));
Sid Manningc374ac92014-10-20 13:08:19 +000056
57 return ~(-1U << bits);
Sid Manning12cd21a2014-10-15 18:27:40 +000058}
59
60// Return true if the instruction must be extended.
61static bool isExtended(uint64_t TSFlags) {
62 return (TSFlags >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
63}
64
Sid Manning74cd0202014-10-15 19:24:14 +000065// Currently just used in an assert statement
Sid Manninga0022962014-10-15 20:41:17 +000066static bool isExtendable(uint64_t TSFlags) LLVM_ATTRIBUTE_UNUSED;
Sid Manning12cd21a2014-10-15 18:27:40 +000067// Return true if the instruction may be extended based on the operand value.
68static bool isExtendable(uint64_t TSFlags) {
69 return (TSFlags >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
70}
Jyotsna Verma7503a622013-02-20 16:13:27 +000071
Evandro Menezes5cee6212012-04-12 17:55:53 +000072StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
73 return MII.getName(Opcode);
74}
75
76StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
77 return getRegisterName(RegNo);
78}
79
Colin LeMahieu1174fea2015-02-19 21:10:50 +000080void HexagonInstPrinter::printInst(MCInst const *MI, raw_ostream &O,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081 StringRef Annot) {
Evandro Menezes5cee6212012-04-12 17:55:53 +000082 const char startPacket = '{',
83 endPacket = '}';
84 // TODO: add outer HW loop when it's supported too.
85 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000086 // Ending a harware loop is different from ending an regular packet.
Colin LeMahieu1174fea2015-02-19 21:10:50 +000087 assert(HexagonMCInstrInfo::isPacketEnd(*MI) && "Loop-end must also end the packet");
Evandro Menezes5cee6212012-04-12 17:55:53 +000088
Colin LeMahieu1174fea2015-02-19 21:10:50 +000089 if (HexagonMCInstrInfo::isPacketBegin(*MI)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090 // There must be a packet to end a loop.
91 // FIXME: when shuffling is always run, this shouldn't be needed.
Colin LeMahieu1174fea2015-02-19 21:10:50 +000092 MCInst Nop;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093 StringRef NoAnnot;
94
Colin LeMahieu6f352b02014-11-25 21:23:07 +000095 Nop.setOpcode (Hexagon::A2_nop);
Colin LeMahieu1174fea2015-02-19 21:10:50 +000096 HexagonMCInstrInfo::setPacketBegin (Nop, HexagonMCInstrInfo::isPacketBegin(*MI));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000097 printInst (&Nop, O, NoAnnot);
98 }
99
100 // Close the packet.
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000101 if (HexagonMCInstrInfo::isPacketEnd(*MI))
Jyotsna Verma7503a622013-02-20 16:13:27 +0000102 O << PacketPadding << endPacket;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000103
104 printInstruction(MI, O);
105 }
106 else {
107 // Prefix the insn opening the packet.
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000108 if (HexagonMCInstrInfo::isPacketBegin(*MI))
Jyotsna Verma7503a622013-02-20 16:13:27 +0000109 O << PacketPadding << startPacket << '\n';
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110
111 printInstruction(MI, O);
112
113 // Suffix the insn closing the packet.
Colin LeMahieu1174fea2015-02-19 21:10:50 +0000114 if (HexagonMCInstrInfo::isPacketEnd(*MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115 // Suffix the packet in a new line always, since the GNU assembler has
116 // issues with a closing brace on the same line as CONST{32,64}.
Jyotsna Verma7503a622013-02-20 16:13:27 +0000117 O << '\n' << PacketPadding << endPacket;
Evandro Menezes5cee6212012-04-12 17:55:53 +0000118 }
119
Evandro Menezes5cee6212012-04-12 17:55:53 +0000120 printAnnotation(O, Annot);
121}
122
123void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
124 raw_ostream &O) const {
125 const MCOperand& MO = MI->getOperand(OpNo);
126
127 if (MO.isReg()) {
128 O << getRegisterName(MO.getReg());
129 } else if(MO.isExpr()) {
130 O << *MO.getExpr();
131 } else if(MO.isImm()) {
132 printImmOperand(MI, OpNo, O);
133 } else {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000134 llvm_unreachable("Unknown operand");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000135 }
136}
137
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000138void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
139 raw_ostream &O) const {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000140 const MCOperand& MO = MI->getOperand(OpNo);
141
142 if(MO.isExpr()) {
143 O << *MO.getExpr();
144 } else if(MO.isImm()) {
145 O << MI->getOperand(OpNo).getImm();
146 } else {
147 llvm_unreachable("Unknown operand");
148 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000149}
150
151void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000152 raw_ostream &O) const {
Sid Manning12cd21a2014-10-15 18:27:40 +0000153 const MCOperand &MO = MI->getOperand(OpNo);
154 const MCInstrDesc &MII = getMII().get(MI->getOpcode());
155
156 assert((isExtendable(MII.TSFlags) || isExtended(MII.TSFlags)) &&
157 "Expecting an extendable operand");
158
159 if (MO.isExpr() || isExtended(MII.TSFlags)) {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000160 O << "#";
Sid Manning12cd21a2014-10-15 18:27:40 +0000161 } else if (MO.isImm()) {
162 int ImmValue = MO.getImm();
163 if (ImmValue < getMinValue(MII.TSFlags) ||
164 ImmValue > getMaxValue(MII.TSFlags))
165 O << "#";
166 }
Jyotsna Verma7503a622013-02-20 16:13:27 +0000167 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000168}
169
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000170void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
171 unsigned OpNo, raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000172 O << MI->getOperand(OpNo).getImm();
173}
174
175void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
176 raw_ostream &O) const {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000177 O << -MI->getOperand(OpNo).getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000178}
179
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000180void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
181 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000182 O << -1;
183}
184
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
186 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000187 const MCOperand& MO0 = MI->getOperand(OpNo);
188 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
189
190 O << getRegisterName(MO0.getReg());
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000191 O << " + #" << MO1.getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000192}
193
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000194void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
195 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000196 const MCOperand& MO0 = MI->getOperand(OpNo);
197 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
198
199 O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
200}
201
202void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
203 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000204 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000205
206 printOperand(MI, OpNo, O);
207}
208
209void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
210 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000211 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000212
213 printOperand(MI, OpNo, O);
214}
215
216void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
217 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000218 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000219
220 printOperand(MI, OpNo, O);
221}
222
223void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
224 raw_ostream &O) const {
225 // Branches can take an immediate operand. This is used by the branch
226 // selection pass to print $+8, an eight byte displacement from the PC.
Richard Trieud7fd95a2013-06-28 23:46:19 +0000227 llvm_unreachable("Unknown branch operand.");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000228}
229
230void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
231 raw_ostream &O) const {
232}
233
234void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
235 raw_ostream &O) const {
236}
237
238void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
239 raw_ostream &O) const {
240}
241
242void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
243 raw_ostream &O, bool hi) const {
Benjamin Kramer755bf4f2013-07-02 17:24:00 +0000244 assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000245
Benjamin Kramer755bf4f2013-07-02 17:24:00 +0000246 O << '#' << (hi ? "HI" : "LO") << "(#";
Richard Trieufab01e52013-07-01 23:06:23 +0000247 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000248 O << ')';
249}