9cf54b41c2fc2b0276001bcbff4c620cf5a82a1f
[oota-llvm.git] / lib / Target / Hexagon / InstPrinter / HexagonInstPrinter.cpp
1 //===- 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 #include "HexagonAsmPrinter.h"
15 #include "Hexagon.h"
16 #include "HexagonInstPrinter.h"
17 #include "MCTargetDesc/HexagonMCInst.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"
23
24 using namespace llvm;
25
26 #define DEBUG_TYPE "asm-printer"
27
28 #define GET_INSTRUCTION_NAME
29 #include "HexagonGenAsmWriter.inc"
30
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) {
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);
42   else
43     return 0;
44 }
45
46 // Return the maximum value that a constant extendable operand can have
47 // without being extended.
48 static 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));
56   else
57     return ~(-1U << bits);
58 }
59
60 // Return true if the instruction must be extended.
61 static bool isExtended(uint64_t TSFlags) {
62   return (TSFlags >> HexagonII::ExtendedPos) & HexagonII::ExtendedMask;
63 }
64
65 // Return true if the instruction may be extended based on the operand value.
66 static bool isExtendable(uint64_t TSFlags) {
67   return (TSFlags >> HexagonII::ExtendablePos) & HexagonII::ExtendableMask;
68 }
69
70 StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
71   return MII.getName(Opcode);
72 }
73
74 StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
75   return getRegisterName(RegNo);
76 }
77
78 void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
79                                    StringRef Annot) {
80   printInst((const HexagonMCInst*)(MI), O, Annot);
81 }
82
83 void HexagonInstPrinter::printInst(const HexagonMCInst *MI, raw_ostream &O,
84                                    StringRef Annot) {
85   const char startPacket = '{',
86              endPacket = '}';
87   // TODO: add outer HW loop when it's supported too.
88   if (MI->getOpcode() == Hexagon::ENDLOOP0) {
89     // Ending a harware loop is different from ending an regular packet.
90     assert(MI->isPacketEnd() && "Loop-end must also end the packet");
91
92     if (MI->isPacketStart()) {
93       // There must be a packet to end a loop.
94       // FIXME: when shuffling is always run, this shouldn't be needed.
95       HexagonMCInst Nop;
96       StringRef NoAnnot;
97
98       Nop.setOpcode (Hexagon::NOP);
99       Nop.setPacketStart (MI->isPacketStart());
100       printInst (&Nop, O, NoAnnot);
101     }
102
103     // Close the packet.
104     if (MI->isPacketEnd())
105       O << PacketPadding << endPacket;
106
107     printInstruction(MI, O);
108   }
109   else {
110     // Prefix the insn opening the packet.
111     if (MI->isPacketStart())
112       O << PacketPadding << startPacket << '\n';
113
114     printInstruction(MI, O);
115
116     // Suffix the insn closing the packet.
117     if (MI->isPacketEnd())
118       // Suffix the packet in a new line always, since the GNU assembler has
119       // issues with a closing brace on the same line as CONST{32,64}.
120       O << '\n' << PacketPadding << endPacket;
121   }
122
123   printAnnotation(O, Annot);
124 }
125
126 void HexagonInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
127                                       raw_ostream &O) const {
128   const MCOperand& MO = MI->getOperand(OpNo);
129
130   if (MO.isReg()) {
131     O << getRegisterName(MO.getReg());
132   } else if(MO.isExpr()) {
133     O << *MO.getExpr();
134   } else if(MO.isImm()) {
135     printImmOperand(MI, OpNo, O);
136   } else {
137     llvm_unreachable("Unknown operand");
138   }
139 }
140
141 void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
142                                          raw_ostream &O) const {
143   const MCOperand& MO = MI->getOperand(OpNo);
144
145   if(MO.isExpr()) {
146     O << *MO.getExpr();
147   } else if(MO.isImm()) {
148     O << MI->getOperand(OpNo).getImm();
149   } else {
150     llvm_unreachable("Unknown operand");
151   }
152 }
153
154 void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
155                                          raw_ostream &O) const {
156   const MCOperand &MO = MI->getOperand(OpNo);
157   const MCInstrDesc &MII = getMII().get(MI->getOpcode());
158
159   assert((isExtendable(MII.TSFlags) || isExtended(MII.TSFlags)) &&
160          "Expecting an extendable operand");
161
162   if (MO.isExpr() || isExtended(MII.TSFlags)) {
163     O << "#";
164   } else if (MO.isImm()) {
165     int ImmValue = MO.getImm();
166     if (ImmValue < getMinValue(MII.TSFlags) ||
167         ImmValue > getMaxValue(MII.TSFlags))
168       O << "#";
169   }
170   printOperand(MI, OpNo, O);
171 }
172
173 void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
174                                     unsigned OpNo, raw_ostream &O) const {
175   O << MI->getOperand(OpNo).getImm();
176 }
177
178 void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
179                                             raw_ostream &O) const {
180   O << -MI->getOperand(OpNo).getImm();
181 }
182
183 void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
184                                              raw_ostream &O) const {
185   O << -1;
186 }
187
188 void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
189                                            raw_ostream &O) const {
190   const MCOperand& MO0 = MI->getOperand(OpNo);
191   const MCOperand& MO1 = MI->getOperand(OpNo + 1);
192
193   O << getRegisterName(MO0.getReg());
194   O << " + #" << MO1.getImm();
195 }
196
197 void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
198                                                 raw_ostream &O) const {
199   const MCOperand& MO0 = MI->getOperand(OpNo);
200   const MCOperand& MO1 = MI->getOperand(OpNo + 1);
201
202   O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
203 }
204
205 void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
206                                             raw_ostream &O) const {
207   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
208
209   printOperand(MI, OpNo, O);
210 }
211
212 void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
213                                         raw_ostream &O) const {
214   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
215
216   printOperand(MI, OpNo, O);
217 }
218
219 void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
220                                            raw_ostream &O) const {
221   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
222
223   printOperand(MI, OpNo, O);
224 }
225
226 void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
227                                             raw_ostream &O) const {
228   // Branches can take an immediate operand.  This is used by the branch
229   // selection pass to print $+8, an eight byte displacement from the PC.
230   llvm_unreachable("Unknown branch operand.");
231 }
232
233 void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
234                                           raw_ostream &O) const {
235 }
236
237 void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
238                                              raw_ostream &O) const {
239 }
240
241 void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
242                                                raw_ostream &O) const {
243 }
244
245 void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
246                                      raw_ostream &O, bool hi) const {
247   assert(MI->getOperand(OpNo).isImm() && "Unknown symbol operand");
248
249   O << '#' << (hi ? "HI" : "LO") << "(#";
250   printOperand(MI, OpNo, O);
251   O << ')';
252 }