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