blob: 1929152129fa70bd3531cf28c527424d49c18368 [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
Jyotsna Verma7503a622013-02-20 16:13:27 +000014#include "HexagonInstPrinter.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "HexagonAsmPrinter.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;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000082 MCInst ME;
83 ME.setOpcode(Hexagon::ENDLOOP1);
84 printInstruction(&ME, OS);
85 }
Evandro Menezes5cee6212012-04-12 17:55:53 +000086}
87
Colin LeMahieu7cd08922015-11-09 04:07:48 +000088void HexagonInstPrinter::printOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +000089 raw_ostream &O) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +000090 if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo &&
91 (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI)))
Jyotsna Verma7503a622013-02-20 16:13:27 +000092 O << "#";
Colin LeMahieu7cd08922015-11-09 04:07:48 +000093 MCOperand const &MO = MI->getOperand(OpNo);
94 if (MO.isReg()) {
95 O << getRegisterName(MO.getReg());
96 } else if (MO.isExpr()) {
97 int64_t Value;
98 if (MO.getExpr()->evaluateAsAbsolute(Value))
99 O << formatImm(Value);
100 else
101 O << *MO.getExpr();
102 } else {
103 llvm_unreachable("Unknown operand");
Sid Manning12cd21a2014-10-15 18:27:40 +0000104 }
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000105}
106
107void HexagonInstPrinter::printExtOperand(MCInst const *MI, unsigned OpNo,
108 raw_ostream &O) const {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000109 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000110}
111
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000112void HexagonInstPrinter::printUnsignedImmOperand(MCInst const *MI,
113 unsigned OpNo,
114 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000115 O << MI->getOperand(OpNo).getImm();
116}
117
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000118void HexagonInstPrinter::printNegImmOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000119 raw_ostream &O) const {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000120 O << -MI->getOperand(OpNo).getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000121}
122
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000123void HexagonInstPrinter::printNOneImmOperand(MCInst const *MI, unsigned OpNo,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000124 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000125 O << -1;
126}
127
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000128void HexagonInstPrinter::printGlobalOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000129 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000130 printOperand(MI, OpNo, O);
131}
132
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000133void HexagonInstPrinter::printJumpTable(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000134 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000135 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000136
137 printOperand(MI, OpNo, O);
138}
139
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000140void HexagonInstPrinter::printConstantPool(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000141 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000142 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000143
144 printOperand(MI, OpNo, O);
145}
146
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000147void HexagonInstPrinter::printBranchOperand(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000148 raw_ostream &O) const {
149 // Branches can take an immediate operand. This is used by the branch
150 // selection pass to print $+8, an eight byte displacement from the PC.
Richard Trieud7fd95a2013-06-28 23:46:19 +0000151 llvm_unreachable("Unknown branch operand.");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000152}
153
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000154void HexagonInstPrinter::printCallOperand(MCInst const *MI, unsigned OpNo,
155 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000156
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000157void HexagonInstPrinter::printAbsAddrOperand(MCInst const *MI, unsigned OpNo,
158 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000159
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000160void HexagonInstPrinter::printPredicateOperand(MCInst const *MI, unsigned OpNo,
161 raw_ostream &O) const {}
Evandro Menezes5cee6212012-04-12 17:55:53 +0000162
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000163void HexagonInstPrinter::printSymbol(MCInst const *MI, unsigned OpNo,
Evandro Menezes5cee6212012-04-12 17:55:53 +0000164 raw_ostream &O, bool hi) const {
David Blaikiebc744272016-05-19 20:44:22 +0000165 assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000166
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000167 O << '#' << (hi ? "HI" : "LO") << '(';
David Blaikief869d312016-05-19 20:28:40 +0000168 O << '#';
169 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000170 O << ')';
171}
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000172
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000173void HexagonInstPrinter::printBrtarget(MCInst const *MI, unsigned OpNo,
174 raw_ostream &O) const {
175 MCOperand const &MO = MI->getOperand(OpNo);
176 assert (MO.isExpr());
177 MCExpr const &Expr = *MO.getExpr();
178 int64_t Value;
179 if (Expr.evaluateAsAbsolute(Value))
180 O << format("0x%" PRIx64, Value);
181 else {
182 if (HasExtender || HexagonMCInstrInfo::isConstExtended(MII, *MI))
183 if (HexagonMCInstrInfo::getExtendableOp(MII, *MI) == OpNo)
184 O << "##";
185 O << Expr;
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000186 }
Brendon Cahoon55bdeb72015-04-27 14:16:43 +0000187}