1 //===- HexagonInstPrinter.cpp - Convert Hexagon MCInst to assembly syntax -===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class prints an Hexagon MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
14 #include "HexagonAsmPrinter.h"
16 #include "HexagonInstPrinter.h"
17 #include "MCTargetDesc/HexagonMCInstrInfo.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "asm-printer"
28 #define GET_INSTRUCTION_NAME
29 #include "HexagonGenAsmWriter.inc"
31 const char HexagonInstPrinter::PacketPadding = '\t';
32 // Return the minimum value that a constant extendable operand can have
33 // without being extended.
34 static int getMinValue(uint64_t TSFlags) {
36 (TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
38 (TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
41 return -1U << (bits - 1);
46 // Return the maximum value that a constant extendable operand can have
47 // without being extended.
48 static int getMaxValue(uint64_t TSFlags) {
50 (TSFlags >> HexagonII::ExtentSignedPos) & HexagonII::ExtentSignedMask;
52 (TSFlags >> HexagonII::ExtentBitsPos) & HexagonII::ExtentBitsMask;
55 return ~(-1U << (bits - 1));
57 return ~(-1U << bits);
60 // Return true if the instruction must be extended.
61 static bool isExtended(uint64_t TSFlags) {
62 return (TSFlags >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
65 // Currently just used in an assert statement
66 static bool isExtendable(uint64_t TSFlags) LLVM_ATTRIBUTE_UNUSED;
67 // Return true if the instruction may be extended based on the operand value.
68 static bool isExtendable(uint64_t TSFlags) {
69 return (TSFlags >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
72 StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
73 return MII.getName(Opcode);
76 void HexagonInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
77 OS << getRegisterName(RegNo);
80 void HexagonInstPrinter::printInst(MCInst const *MI, raw_ostream &O,
82 const MCSubtargetInfo &STI) {
83 const char startPacket = '{',
85 // TODO: add outer HW loop when it's supported too.
86 if (MI->getOpcode() == Hexagon::ENDLOOP0) {
87 // Ending a harware loop is different from ending an regular packet.
88 assert(HexagonMCInstrInfo::isPacketEnd(*MI) && "Loop-end must also end the packet");
90 if (HexagonMCInstrInfo::isPacketBegin(*MI)) {
91 // There must be a packet to end a loop.
92 // FIXME: when shuffling is always run, this shouldn't be needed.
96 Nop.setOpcode (Hexagon::A2_nop);
97 HexagonMCInstrInfo::setPacketBegin (Nop, HexagonMCInstrInfo::isPacketBegin(*MI));
98 printInst (&Nop, O, NoAnnot, STI);
102 if (HexagonMCInstrInfo::isPacketEnd(*MI))
103 O << PacketPadding << endPacket;
105 printInstruction(MI, O);
108 // Prefix the insn opening the packet.
109 if (HexagonMCInstrInfo::isPacketBegin(*MI))
110 O << PacketPadding << startPacket << '\n';
112 printInstruction(MI, O);
114 // Suffix the insn closing the packet.
115 if (HexagonMCInstrInfo::isPacketEnd(*MI))
116 // Suffix the packet in a new line always, since the GNU assembler has
117 // issues with a closing brace on the same line as CONST{32,64}.
118 O << '\n' << PacketPadding << endPacket;
121 printAnnotation(O, Annot);
124 void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
125 raw_ostream &O) const {
126 const MCOperand& MO = MI->getOperand(OpNo);
129 printRegName(O, MO.getReg());
130 } else if(MO.isExpr()) {
132 } else if(MO.isImm()) {
133 printImmOperand(MI, OpNo, O);
135 llvm_unreachable("Unknown operand");
139 void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
140 raw_ostream &O) const {
141 const MCOperand& MO = MI->getOperand(OpNo);
145 } else if(MO.isImm()) {
146 O << MI->getOperand(OpNo).getImm();
148 llvm_unreachable("Unknown operand");
152 void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
153 raw_ostream &O) const {
154 const MCOperand &MO = MI->getOperand(OpNo);
155 const MCInstrDesc &MII = getMII().get(MI->getOpcode());
157 assert((isExtendable(MII.TSFlags) || isExtended(MII.TSFlags)) &&
158 "Expecting an extendable operand");
160 if (MO.isExpr() || isExtended(MII.TSFlags)) {
162 } else if (MO.isImm()) {
163 int ImmValue = MO.getImm();
164 if (ImmValue < getMinValue(MII.TSFlags) ||
165 ImmValue > getMaxValue(MII.TSFlags))
168 printOperand(MI, OpNo, O);
171 void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
172 unsigned OpNo, raw_ostream &O) const {
173 O << MI->getOperand(OpNo).getImm();
176 void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
177 raw_ostream &O) const {
178 O << -MI->getOperand(OpNo).getImm();
181 void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
182 raw_ostream &O) const {
186 void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
187 raw_ostream &O) const {
188 const MCOperand& MO0 = MI->getOperand(OpNo);
189 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
191 printRegName(O, MO0.getReg());
192 O << " + #" << MO1.getImm();
195 void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
196 raw_ostream &O) const {
197 const MCOperand& MO0 = MI->getOperand(OpNo);
198 const MCOperand& MO1 = MI->getOperand(OpNo + 1);
200 printRegName(O, MO0.getReg());
201 O << ", #" << MO1.getImm();
204 void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
205 raw_ostream &O) const {
206 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
208 printOperand(MI, OpNo, O);
211 void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
212 raw_ostream &O) const {
213 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
215 printOperand(MI, OpNo, O);
218 void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
219 raw_ostream &O) const {
220 assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
222 printOperand(MI, OpNo, O);
225 void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
226 raw_ostream &O) const {
227 // Branches can take an immediate operand. This is used by the branch
228 // selection pass to print $+8, an eight byte displacement from the PC.
229 llvm_unreachable("Unknown branch operand.");
232 void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
233 raw_ostream &O) const {
236 void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
237 raw_ostream &O) const {
240 void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
241 raw_ostream &O) const {
244 void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
245 raw_ostream &O, bool hi) const {
246 assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
248 O << '#' << (hi ? "HI" : "LO") << "(#";
249 printOperand(MI, OpNo, O);
253 void HexagonInstPrinter::printExtBrtarget(const MCInst *MI, unsigned OpNo,
254 raw_ostream &O) const {
255 const MCOperand &MO = MI->getOperand(OpNo);
256 const MCInstrDesc &MII = getMII().get(MI->getOpcode());
258 assert((isExtendable(MII.TSFlags) || isExtended(MII.TSFlags)) &&
259 "Expecting an extendable operand");
261 if (MO.isExpr() || isExtended(MII.TSFlags)) {
264 printOperand(MI, OpNo, O);