blob: bcfd9bb04d2eb95deb33a261b6b05d4fa7ef34c3 [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
14#define DEBUG_TYPE "asm-printer"
Evandro Menezes5cee6212012-04-12 17:55:53 +000015#include "HexagonAsmPrinter.h"
Jyotsna Verma7503a622013-02-20 16:13:27 +000016#include "Hexagon.h"
17#include "HexagonInstPrinter.h"
18#include "MCTargetDesc/HexagonMCInst.h"
19#include "llvm/MC/MCInst.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/StringExtras.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000021#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCExpr.h"
Evandro Menezes5cee6212012-04-12 17:55:53 +000023#include "llvm/Support/raw_ostream.h"
24#include <cstdio>
25
26using namespace llvm;
27
28#define GET_INSTRUCTION_NAME
29#include "HexagonGenAsmWriter.inc"
30
Jyotsna Verma7503a622013-02-20 16:13:27 +000031const char HexagonInstPrinter::PacketPadding = '\t';
32
Evandro Menezes5cee6212012-04-12 17:55:53 +000033StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
34 return MII.getName(Opcode);
35}
36
37StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
38 return getRegisterName(RegNo);
39}
40
41void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
42 StringRef Annot) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000043 printInst((const HexagonMCInst*)(MI), O, Annot);
44}
45
46void HexagonInstPrinter::printInst(const HexagonMCInst *MI, raw_ostream &O,
47 StringRef Annot) {
Evandro Menezes5cee6212012-04-12 17:55:53 +000048 const char startPacket = '{',
49 endPacket = '}';
50 // TODO: add outer HW loop when it's supported too.
51 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000052 // Ending a harware loop is different from ending an regular packet.
Jyotsna Verma7503a622013-02-20 16:13:27 +000053 assert(MI->isPacketEnd() && "Loop-end must also end the packet");
Evandro Menezes5cee6212012-04-12 17:55:53 +000054
Jyotsna Verma7503a622013-02-20 16:13:27 +000055 if (MI->isPacketStart()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000056 // There must be a packet to end a loop.
57 // FIXME: when shuffling is always run, this shouldn't be needed.
58 HexagonMCInst Nop;
59 StringRef NoAnnot;
60
61 Nop.setOpcode (Hexagon::NOP);
Jyotsna Verma7503a622013-02-20 16:13:27 +000062 Nop.setPacketStart (MI->isPacketStart());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000063 printInst (&Nop, O, NoAnnot);
64 }
65
66 // Close the packet.
Jyotsna Verma7503a622013-02-20 16:13:27 +000067 if (MI->isPacketEnd())
68 O << PacketPadding << endPacket;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000069
70 printInstruction(MI, O);
71 }
72 else {
73 // Prefix the insn opening the packet.
Jyotsna Verma7503a622013-02-20 16:13:27 +000074 if (MI->isPacketStart())
75 O << PacketPadding << startPacket << '\n';
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000076
77 printInstruction(MI, O);
78
79 // Suffix the insn closing the packet.
Jyotsna Verma7503a622013-02-20 16:13:27 +000080 if (MI->isPacketEnd())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081 // Suffix the packet in a new line always, since the GNU assembler has
82 // issues with a closing brace on the same line as CONST{32,64}.
Jyotsna Verma7503a622013-02-20 16:13:27 +000083 O << '\n' << PacketPadding << endPacket;
Evandro Menezes5cee6212012-04-12 17:55:53 +000084 }
85
Evandro Menezes5cee6212012-04-12 17:55:53 +000086 printAnnotation(O, Annot);
87}
88
89void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
90 raw_ostream &O) const {
91 const MCOperand& MO = MI->getOperand(OpNo);
92
93 if (MO.isReg()) {
94 O << getRegisterName(MO.getReg());
95 } else if(MO.isExpr()) {
96 O << *MO.getExpr();
97 } else if(MO.isImm()) {
98 printImmOperand(MI, OpNo, O);
99 } else {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100 llvm_unreachable("Unknown operand");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000101 }
102}
103
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000104void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
105 raw_ostream &O) const {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000106 const MCOperand& MO = MI->getOperand(OpNo);
107
108 if(MO.isExpr()) {
109 O << *MO.getExpr();
110 } else if(MO.isImm()) {
111 O << MI->getOperand(OpNo).getImm();
112 } else {
113 llvm_unreachable("Unknown operand");
114 }
Evandro Menezes5cee6212012-04-12 17:55:53 +0000115}
116
117void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000118 raw_ostream &O) const {
Jyotsna Verma7503a622013-02-20 16:13:27 +0000119 const HexagonMCInst *HMCI = static_cast<const HexagonMCInst*>(MI);
120 if (HMCI->isConstExtended())
121 O << "#";
122 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000123}
124
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000125void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
126 unsigned OpNo, raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000127 O << MI->getOperand(OpNo).getImm();
128}
129
130void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
131 raw_ostream &O) const {
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000132 O << -MI->getOperand(OpNo).getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000133}
134
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000135void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
136 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000137 O << -1;
138}
139
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000140void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
141 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000142 const MCOperand& MO0 = MI->getOperand(OpNo);
143 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
144
145 O << getRegisterName(MO0.getReg());
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000146 O << " + #" << MO1.getImm();
Evandro Menezes5cee6212012-04-12 17:55:53 +0000147}
148
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000149void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
150 raw_ostream &O) const {
Evandro Menezes5cee6212012-04-12 17:55:53 +0000151 const MCOperand& MO0 = MI->getOperand(OpNo);
152 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
153
154 O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
155}
156
157void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
158 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000159 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000160
161 printOperand(MI, OpNo, O);
162}
163
164void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
165 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000166 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000167
168 printOperand(MI, OpNo, O);
169}
170
171void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
172 raw_ostream &O) const {
NAKAMURA Takumidf3d5ea2012-04-21 11:24:55 +0000173 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000174
175 printOperand(MI, OpNo, O);
176}
177
178void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
179 raw_ostream &O) const {
180 // Branches can take an immediate operand. This is used by the branch
181 // selection pass to print $+8, an eight byte displacement from the PC.
Richard Trieud7fd95a2013-06-28 23:46:19 +0000182 llvm_unreachable("Unknown branch operand.");
Evandro Menezes5cee6212012-04-12 17:55:53 +0000183}
184
185void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
186 raw_ostream &O) const {
187}
188
189void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
190 raw_ostream &O) const {
191}
192
193void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
194 raw_ostream &O) const {
195}
196
197void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
198 raw_ostream &O, bool hi) const {
199 const MCOperand& MO = MI->getOperand(OpNo);
200
201 O << '#' << (hi? "HI": "LO") << '(';
Richard Trieufab01e52013-07-01 23:06:23 +0000202
203 assert(MO.isImm() && "Unknown symbol operand");
204
205 O << '#';
206 printOperand(MI, OpNo, O);
Evandro Menezes5cee6212012-04-12 17:55:53 +0000207 O << ')';
208}