blob: 7b3c016b3ac6181cc5ba51750ca24436ef244edf [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 "HexagonInstPrinter.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000016#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieu1174fea2015-02-19 21:10:50 +000017#include "MCTargetDesc/HexagonMCInstrInfo.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000018#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/MC/MCInst.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000021#include "llvm/Support/Debug.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
Colin LeMahieu7cd08922015-11-09 04:07:48 +000031HexagonInstPrinter::HexagonInstPrinter(MCAsmInfo const &MAI,
32 MCInstrInfo const &MII,
33 MCRegisterInfo const &MRI)
34 : MCInstPrinter(MAI, MII, MRI), MII(MII), HasExtender(false) {
Sid Manning12cd21a2014-10-15 18:27:40 +000035}
Jyotsna Verma7503a622013-02-20 16:13:27 +000036
Evandro Menezes5cee6212012-04-12 17:55:53 +000037StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
38 return MII.getName(Opcode);
39}
40
Colin LeMahieu7cd08922015-11-09 04:07:48 +000041void HexagonInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
42 O << getRegName(RegNo);
43}
44
45StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
46 return getRegisterName(RegNo);
Evandro Menezes5cee6212012-04-12 17:55:53 +000047}
48
Colin LeMahieu68d967d2015-05-29 14:44:13 +000049void HexagonInstPrinter::setExtender(MCInst const &MCI) {
50 HasExtender = HexagonMCInstrInfo::isImmext(MCI);
51}
52
Colin LeMahieu7cd08922015-11-09 04:07:48 +000053void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
54 StringRef Annot, const MCSubtargetInfo &STI) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000055 assert(HexagonMCInstrInfo::isBundle(*MI));
56 assert(HexagonMCInstrInfo::bundleSize(*MI) <= HEXAGON_PACKET_SIZE);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000057 assert(HexagonMCInstrInfo::bundleSize(*MI) > 0);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000058 HasExtender = false;
59 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MI)) {
60 MCInst const &MCI = *I.getInst();
Colin LeMahieube8c4532015-06-05 16:00:11 +000061 if (HexagonMCInstrInfo::isDuplex(MII, MCI)) {
62 printInstruction(MCI.getOperand(1).getInst(), OS);
63 OS << '\v';
64 HasExtender = false;
65 printInstruction(MCI.getOperand(0).getInst(), OS);
66 } else
67 printInstruction(&MCI, OS);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000068 setExtender(MCI);
69 OS << "\n";
Evandro Menezes5cee6212012-04-12 17:55:53 +000070 }
71
Colin LeMahieu68d967d2015-05-29 14:44:13 +000072 auto Separator = "";
73 if (HexagonMCInstrInfo::isInnerLoop(*MI)) {
74 OS << Separator;
75 Separator = " ";
76 MCInst ME;
77 ME.setOpcode(Hexagon::ENDLOOP0);
78 printInstruction(&ME, OS);
79 }
80 if (HexagonMCInstrInfo::isOuterLoop(*MI)) {
81 OS << Separator;
82 Separator = " ";
83 MCInst ME;
84 ME.setOpcode(Hexagon::ENDLOOP1);
85 printInstruction(&ME, OS);
86 }
Evandro Menezes5cee6212012-04-12 17:55:53 +000087}
88
Colin LeMahieu7cd08922015-11-09 04:07:48 +000089void HexagonInstPrinter::printOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +000090 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +000091 if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo &&
92 (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI)))
Jyotsna Verma7503a622013-02-20 16:13:27 +000093 O << "#";
Colin LeMahieu7cd08922015-11-09 04:07:48 +000094 MCOperand const &MO = MI->getOperand(OpNo);
95 if (MO.isReg()) {
96 O << getRegisterName(MO.getReg());
97 } else if (MO.isExpr()) {
98 int64_t Value;
99 if (MO.getExpr()->evaluateAsAbsolute(Value))
100 O << formatImm(Value);
101 else
102 O << *MO.getExpr();
103 } else {
104 llvm_unreachable("Unknown operand");
Sid Manning12cd21a2014-10-15 18:27:40 +0000105 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000106}
107
108void HexagonInstPrinter::printExtOperand(MCInst const *MI, unsigned OpNo,
109 raw_ostream &O) const {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000110 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000111}
112
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000113void HexagonInstPrinter::printUnsignedImmOperand(MCInst const *MI,
114 unsigned OpNo,
115 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000116 O << MI->getOperand(OpNo).getImm();
117}
118
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000119void HexagonInstPrinter::printNegImmOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000120 raw_ostream &O) const {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000121 O << -MI->getOperand(OpNo).getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000122}
123
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000124void HexagonInstPrinter::printNOneImmOperand(MCInst const *MI, unsigned OpNo,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000125 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000126 O << -1;
127}
128
Colin LeMahieu7c958712015-10-17 01:33:04 +0000129void HexagonInstPrinter::prints3_6ImmOperand(MCInst const *MI, unsigned OpNo,
130 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000131 int64_t Imm;
132 bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
133 Imm = SignExtend64<9>(Imm);
134 assert(Success); (void)Success;
Colin LeMahieu7c958712015-10-17 01:33:04 +0000135 assert(((Imm & 0x3f) == 0) && "Lower 6 bits must be ZERO.");
136 O << formatImm(Imm/64);
137}
138
139void HexagonInstPrinter::prints3_7ImmOperand(MCInst const *MI, unsigned OpNo,
140 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000141 int64_t Imm;
142 bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
143 Imm = SignExtend64<10>(Imm);
144 assert(Success); (void)Success;
Colin LeMahieu7c958712015-10-17 01:33:04 +0000145 assert(((Imm & 0x7f) == 0) && "Lower 7 bits must be ZERO.");
146 O << formatImm(Imm/128);
147}
148
149void HexagonInstPrinter::prints4_6ImmOperand(MCInst const *MI, unsigned OpNo,
150 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000151 int64_t Imm;
152 bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
153 Imm = SignExtend64<10>(Imm);
154 assert(Success); (void)Success;
Colin LeMahieu7c958712015-10-17 01:33:04 +0000155 assert(((Imm & 0x3f) == 0) && "Lower 6 bits must be ZERO.");
156 O << formatImm(Imm/64);
157}
158
159void HexagonInstPrinter::prints4_7ImmOperand(MCInst const *MI, unsigned OpNo,
160 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000161 int64_t Imm;
162 bool Success = MI->getOperand(OpNo).getExpr()->evaluateAsAbsolute(Imm);
163 Imm = SignExtend64<11>(Imm);
164 assert(Success); (void)Success;
Colin LeMahieu7c958712015-10-17 01:33:04 +0000165 assert(((Imm & 0x7f) == 0) && "Lower 7 bits must be ZERO.");
166 O << formatImm(Imm/128);
167}
168
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000169void HexagonInstPrinter::printGlobalOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000170 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000171 printOperand(MI, OpNo, O);
172}
173
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000174void HexagonInstPrinter::printJumpTable(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000175 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000176 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000177
178 printOperand(MI, OpNo, O);
179}
180
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000181void HexagonInstPrinter::printConstantPool(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000182 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000183 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000184
185 printOperand(MI, OpNo, O);
186}
187
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000188void HexagonInstPrinter::printBranchOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000189 raw_ostream &O) const {
190 // Branches can take an immediate operand. This is used by the branch
191 // selection pass to print $+8, an eight byte displacement from the PC.
Richard Trieud7fd95a2013-06-28 23:46:19 +0000192 llvm_unreachable("Unknown branch operand.");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000193}
194
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000195void HexagonInstPrinter::printCallOperand(MCInst const *MI, unsigned OpNo,
196 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000197
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000198void HexagonInstPrinter::printAbsAddrOperand(MCInst const *MI, unsigned OpNo,
199 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000200
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000201void HexagonInstPrinter::printPredicateOperand(MCInst const *MI, unsigned OpNo,
202 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000203
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000204void HexagonInstPrinter::printSymbol(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000205 raw_ostream &O, bool hi) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000206 MCOperand const &MO = MI->getOperand(OpNo);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000207
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000208 O << '#' << (hi ? "HI" : "LO") << '(';
209 if (MO.isImm()) {
210 O << '#';
211 printOperand(MI, OpNo, O);
212 } else {
213 printOperand(MI, OpNo, O);
Craig Topper855d1822016-04-24 02:01:25 +0000214 assert(!"Unknown symbol operand");
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000215 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000216 O << ')';
217}
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000218
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000219void HexagonInstPrinter::printBrtarget(MCInst const *MI, unsigned OpNo,
220 raw_ostream &O) const {
221 MCOperand const &MO = MI->getOperand(OpNo);
222 assert (MO.isExpr());
223 MCExpr const &Expr = *MO.getExpr();
224 int64_t Value;
225 if (Expr.evaluateAsAbsolute(Value))
226 O << format("0x%" PRIx64, Value);
227 else {
228 if (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI))
229 if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo)
230 O << "##";
231 O << Expr;
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000232 }
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000233}