OSDN Git Service

Hexagon constant extender support.
[android-x86/external-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 #define DEBUG_TYPE "asm-printer"
15 #include "Hexagon.h"
16 #include "HexagonConstExtInfo.h"
17 #include "HexagonAsmPrinter.h"
18 #include "HexagonInstPrinter.h"
19 #include "HexagonMCInst.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <cstdio>
26
27 using namespace llvm;
28
29 #define GET_INSTRUCTION_NAME
30 #include "HexagonGenAsmWriter.inc"
31
32 StringRef HexagonInstPrinter::getOpcodeName(unsigned Opcode) const {
33   return MII.getName(Opcode);
34 }
35
36 StringRef HexagonInstPrinter::getRegName(unsigned RegNo) const {
37   return getRegisterName(RegNo);
38 }
39
40 void HexagonInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
41                                    StringRef Annot) {
42   printInst((const HexagonMCInst*)(MI), O, Annot);
43 }
44
45 void HexagonInstPrinter::printInst(const HexagonMCInst *MI, raw_ostream &O,
46                                    StringRef Annot) {
47   const char packetPadding[] = "      ";
48   const char startPacket = '{',
49              endPacket = '}';
50   // TODO: add outer HW loop when it's supported too.
51   if (MI->getOpcode() == Hexagon::ENDLOOP0) {
52     // Ending a harware loop is different from ending an regular packet.
53     assert(MI->isEndPacket() && "Loop end must also end the packet");
54
55     if (MI->isStartPacket()) {
56       // 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);
62       Nop.setStartPacket (MI->isStartPacket());
63       printInst (&Nop, O, NoAnnot);
64     }
65
66     // Close the packet.
67     if (MI->isEndPacket())
68       O << packetPadding << endPacket;
69
70     printInstruction(MI, O);
71   }
72   else {
73     // Prefix the insn opening the packet.
74     if (MI->isStartPacket())
75       O << packetPadding << startPacket << '\n';
76
77     printInstruction(MI, O);
78
79     // Suffix the insn closing the packet.
80     if (MI->isEndPacket())
81       // 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}.
83       O << '\n' << packetPadding << endPacket;
84   }
85
86   printAnnotation(O, Annot);
87 }
88
89 void 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 {
100     llvm_unreachable("Unknown operand");
101   }
102 }
103
104 void HexagonInstPrinter::printImmOperand(const MCInst *MI, unsigned OpNo,
105                                          raw_ostream &O) const {
106   O << MI->getOperand(OpNo).getImm();
107 }
108
109 void HexagonInstPrinter::printExtOperand(const MCInst *MI, unsigned OpNo,
110                                          raw_ostream &O) const {
111   if (isConstExtended(MI))
112     O << "#" << MI->getOperand(OpNo).getImm();
113   else
114     O << MI->getOperand(OpNo).getImm();
115 }
116
117 void HexagonInstPrinter::printUnsignedImmOperand(const MCInst *MI,
118                                     unsigned OpNo, raw_ostream &O) const {
119   O << MI->getOperand(OpNo).getImm();
120 }
121
122 void HexagonInstPrinter::printNegImmOperand(const MCInst *MI, unsigned OpNo,
123                                             raw_ostream &O) const {
124     O << -MI->getOperand(OpNo).getImm();
125 }
126
127 void HexagonInstPrinter::printNOneImmOperand(const MCInst *MI, unsigned OpNo,
128                                              raw_ostream &O) const {
129   O << -1;
130 }
131
132 void HexagonInstPrinter::printMEMriOperand(const MCInst *MI, unsigned OpNo,
133                                            raw_ostream &O) const {
134   const MCOperand& MO0 = MI->getOperand(OpNo);
135   const MCOperand& MO1 = MI->getOperand(OpNo + 1);
136
137   O << getRegisterName(MO0.getReg());
138   if (isConstExtended(MI))
139     O << " + ##" << MO1.getImm();
140   else
141     O << " + #" << MO1.getImm();
142 }
143
144 void HexagonInstPrinter::printFrameIndexOperand(const MCInst *MI, unsigned OpNo,
145                                                 raw_ostream &O) const {
146   const MCOperand& MO0 = MI->getOperand(OpNo);
147   const MCOperand& MO1 = MI->getOperand(OpNo + 1);
148
149   O << getRegisterName(MO0.getReg()) << ", #" << MO1.getImm();
150 }
151
152 void HexagonInstPrinter::printGlobalOperand(const MCInst *MI, unsigned OpNo,
153                                             raw_ostream &O) const {
154   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
155
156   printOperand(MI, OpNo, O);
157 }
158
159 void HexagonInstPrinter::printJumpTable(const MCInst *MI, unsigned OpNo,
160                                         raw_ostream &O) const {
161   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
162
163   printOperand(MI, OpNo, O);
164 }
165
166 void HexagonInstPrinter::printConstantPool(const MCInst *MI, unsigned OpNo,
167                                            raw_ostream &O) const {
168   assert(MI->getOperand(OpNo).isExpr() && "Expecting expression");
169
170   printOperand(MI, OpNo, O);
171 }
172
173 void HexagonInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
174                                             raw_ostream &O) const {
175   // Branches can take an immediate operand.  This is used by the branch
176   // selection pass to print $+8, an eight byte displacement from the PC.
177   assert("Unknown branch operand.");
178 }
179
180 void HexagonInstPrinter::printCallOperand(const MCInst *MI, unsigned OpNo,
181                                           raw_ostream &O) const {
182 }
183
184 void HexagonInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
185                                              raw_ostream &O) const {
186 }
187
188 void HexagonInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
189                                                raw_ostream &O) const {
190 }
191
192 void HexagonInstPrinter::printSymbol(const MCInst *MI, unsigned OpNo,
193                                      raw_ostream &O, bool hi) const {
194   const MCOperand& MO = MI->getOperand(OpNo);
195
196   O << '#' << (hi? "HI": "LO") << '(';
197   if (MO.isImm()) {
198     O << '#';
199     printOperand(MI, OpNo, O);
200   } else {
201     assert("Unknown symbol operand");
202     printOperand(MI, OpNo, O);
203   }
204   O << ')';
205 }
206
207 bool HexagonInstPrinter::isConstExtended(const MCInst *MI) const{
208   unsigned short Opcode = MI->getOpcode();
209   short ExtOpNum = HexagonConstExt::getCExtOpNum(Opcode);
210   int MinValue = HexagonConstExt::getMinValue(Opcode);
211   int MaxValue = HexagonConstExt::getMaxValue(Opcode);
212
213   // Instruction has no constant extended operand
214   if (ExtOpNum == -1)
215     return false;
216
217   int ImmValue = MI->getOperand(ExtOpNum).getImm();
218   return (ImmValue < MinValue || ImmValue > MaxValue);
219 }