Remove getInstructionName from MCInstPrinter implementations in favor of using the...
[oota-llvm.git] / lib / Target / PowerPC / InstPrinter / PPCInstPrinter.cpp
1 //===-- PPCInstPrinter.cpp - Convert PPC 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 PPC MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "asm-printer"
15 #include "PPCInstPrinter.h"
16 #include "MCTargetDesc/PPCBaseInfo.h"
17 #include "MCTargetDesc/PPCPredicates.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
24 #include "PPCGenAsmWriter.inc"
25
26 StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
27   return MII.getName(Opcode);
28 }
29
30 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
31   OS << getRegisterName(RegNo);
32 }
33
34 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
35                                StringRef Annot) {
36   // Check for slwi/srwi mnemonics.
37   if (MI->getOpcode() == PPC::RLWINM) {
38     unsigned char SH = MI->getOperand(2).getImm();
39     unsigned char MB = MI->getOperand(3).getImm();
40     unsigned char ME = MI->getOperand(4).getImm();
41     bool useSubstituteMnemonic = false;
42     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
43       O << "\tslwi "; useSubstituteMnemonic = true;
44     }
45     if (SH <= 31 && MB == (32-SH) && ME == 31) {
46       O << "\tsrwi "; useSubstituteMnemonic = true;
47       SH = 32-SH;
48     }
49     if (useSubstituteMnemonic) {
50       printOperand(MI, 0, O);
51       O << ", ";
52       printOperand(MI, 1, O);
53       O << ", " << (unsigned int)SH;
54
55       printAnnotation(O, Annot);
56       return;
57     }
58   }
59   
60   if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
61       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
62     O << "\tmr ";
63     printOperand(MI, 0, O);
64     O << ", ";
65     printOperand(MI, 1, O);
66     printAnnotation(O, Annot);
67     return;
68   }
69   
70   if (MI->getOpcode() == PPC::RLDICR) {
71     unsigned char SH = MI->getOperand(2).getImm();
72     unsigned char ME = MI->getOperand(3).getImm();
73     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
74     if (63-SH == ME) {
75       O << "\tsldi ";
76       printOperand(MI, 0, O);
77       O << ", ";
78       printOperand(MI, 1, O);
79       O << ", " << (unsigned int)SH;
80       printAnnotation(O, Annot);
81       return;
82     }
83   }
84   
85   printInstruction(MI, O);
86   printAnnotation(O, Annot);
87 }
88
89
90 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
91                                            raw_ostream &O, 
92                                            const char *Modifier) {
93   assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
94   unsigned Code = MI->getOperand(OpNo).getImm();
95   if (StringRef(Modifier) == "cc") {
96     switch ((PPC::Predicate)Code) {
97     case PPC::PRED_ALWAYS: return; // Don't print anything for always.
98     case PPC::PRED_LT: O << "lt"; return;
99     case PPC::PRED_LE: O << "le"; return;
100     case PPC::PRED_EQ: O << "eq"; return;
101     case PPC::PRED_GE: O << "ge"; return;
102     case PPC::PRED_GT: O << "gt"; return;
103     case PPC::PRED_NE: O << "ne"; return;
104     case PPC::PRED_UN: O << "un"; return;
105     case PPC::PRED_NU: O << "nu"; return;
106     }
107   }
108   
109   assert(StringRef(Modifier) == "reg" &&
110          "Need to specify 'cc' or 'reg' as predicate op modifier!");
111   // Don't print the register for 'always'.
112   if (Code == PPC::PRED_ALWAYS) return;
113   printOperand(MI, OpNo+1, O);
114 }
115
116 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
117                                        raw_ostream &O) {
118   char Value = MI->getOperand(OpNo).getImm();
119   Value = (Value << (32-5)) >> (32-5);
120   O << (int)Value;
121 }
122
123 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
124                                        raw_ostream &O) {
125   unsigned char Value = MI->getOperand(OpNo).getImm();
126   assert(Value <= 31 && "Invalid u5imm argument!");
127   O << (unsigned int)Value;
128 }
129
130 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
131                                        raw_ostream &O) {
132   unsigned char Value = MI->getOperand(OpNo).getImm();
133   assert(Value <= 63 && "Invalid u6imm argument!");
134   O << (unsigned int)Value;
135 }
136
137 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
138                                         raw_ostream &O) {
139   O << (short)MI->getOperand(OpNo).getImm();
140 }
141
142 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
143                                         raw_ostream &O) {
144   O << (unsigned short)MI->getOperand(OpNo).getImm();
145 }
146
147 void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
148                                           raw_ostream &O) {
149   if (MI->getOperand(OpNo).isImm())
150     O << (short)(MI->getOperand(OpNo).getImm()*4);
151   else
152     printOperand(MI, OpNo, O);
153 }
154
155 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
156                                         raw_ostream &O) {
157   if (!MI->getOperand(OpNo).isImm())
158     return printOperand(MI, OpNo, O);
159
160   // Branches can take an immediate operand.  This is used by the branch
161   // selection pass to print $+8, an eight byte displacement from the PC.
162   O << "$+";
163   printAbsAddrOperand(MI, OpNo, O);
164 }
165
166 void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
167                                          raw_ostream &O) {
168   O << (int)MI->getOperand(OpNo).getImm()*4;
169 }
170
171
172 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
173                                  raw_ostream &O) {
174   unsigned CCReg = MI->getOperand(OpNo).getReg();
175   unsigned RegNo;
176   switch (CCReg) {
177   default: llvm_unreachable("Unknown CR register");
178   case PPC::CR0: RegNo = 0; break;
179   case PPC::CR1: RegNo = 1; break;
180   case PPC::CR2: RegNo = 2; break;
181   case PPC::CR3: RegNo = 3; break;
182   case PPC::CR4: RegNo = 4; break;
183   case PPC::CR5: RegNo = 5; break;
184   case PPC::CR6: RegNo = 6; break;
185   case PPC::CR7: RegNo = 7; break;
186   }
187   O << (0x80 >> RegNo);
188 }
189
190 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
191                                     raw_ostream &O) {
192   printSymbolLo(MI, OpNo, O);
193   O << '(';
194   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
195     O << "0";
196   else
197     printOperand(MI, OpNo+1, O);
198   O << ')';
199 }
200
201 void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
202                                            raw_ostream &O) {
203   if (MI->getOperand(OpNo).isImm())
204     printS16X4ImmOperand(MI, OpNo, O);
205   else
206     printSymbolLo(MI, OpNo, O);
207   O << '(';
208   
209   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
210     O << "0";
211   else
212     printOperand(MI, OpNo+1, O);
213   O << ')';
214 }
215
216
217 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
218                                     raw_ostream &O) {
219   // When used as the base register, r0 reads constant zero rather than
220   // the value contained in the register.  For this reason, the darwin
221   // assembler requires that we print r0 as 0 (no r) when used as the base.
222   if (MI->getOperand(OpNo).getReg() == PPC::R0)
223     O << "0";
224   else
225     printOperand(MI, OpNo, O);
226   O << ", ";
227   printOperand(MI, OpNo+1, O);
228 }
229
230
231
232 /// stripRegisterPrefix - This method strips the character prefix from a
233 /// register name so that only the number is left.  Used by for linux asm.
234 static const char *stripRegisterPrefix(const char *RegName) {
235   switch (RegName[0]) {
236   case 'r':
237   case 'f':
238   case 'v': return RegName + 1;
239   case 'c': if (RegName[1] == 'r') return RegName + 2;
240   }
241   
242   return RegName;
243 }
244
245 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
246                                   raw_ostream &O) {
247   const MCOperand &Op = MI->getOperand(OpNo);
248   if (Op.isReg()) {
249     const char *RegName = getRegisterName(Op.getReg());
250     // The linux and AIX assembler does not take register prefixes.
251     if (!isDarwinSyntax())
252       RegName = stripRegisterPrefix(RegName);
253     
254     O << RegName;
255     return;
256   }
257   
258   if (Op.isImm()) {
259     O << Op.getImm();
260     return;
261   }
262   
263   assert(Op.isExpr() && "unknown operand kind in printOperand");
264   O << *Op.getExpr();
265 }
266   
267 void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
268                                    raw_ostream &O) {
269   if (MI->getOperand(OpNo).isImm())
270     return printS16ImmOperand(MI, OpNo, O);
271   
272   // FIXME: This is a terrible hack because we can't encode lo16() as an operand
273   // flag of a subtraction.  See the FIXME in GetSymbolRef in PPCMCInstLower.
274   if (MI->getOperand(OpNo).isExpr() &&
275       isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
276     O << "lo16(";
277     printOperand(MI, OpNo, O);
278     O << ')';
279   } else {
280     printOperand(MI, OpNo, O);
281   }
282 }
283
284 void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
285                                    raw_ostream &O) {
286   if (MI->getOperand(OpNo).isImm())
287     return printS16ImmOperand(MI, OpNo, O);
288
289   // FIXME: This is a terrible hack because we can't encode lo16() as an operand
290   // flag of a subtraction.  See the FIXME in GetSymbolRef in PPCMCInstLower.
291   if (MI->getOperand(OpNo).isExpr() &&
292       isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
293     O << "ha16(";
294     printOperand(MI, OpNo, O);
295     O << ')';
296   } else {
297     printOperand(MI, OpNo, O);
298   }
299 }
300
301