[PowerPC] Add predicted forms of branches
[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/PPCMCTargetDesc.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
91   if (StringRef(Modifier) == "cc") {
92     switch ((PPC::Predicate)Code) {
93     case PPC::PRED_LT_MINUS:
94     case PPC::PRED_LT_PLUS:
95     case PPC::PRED_LT:
96       O << "lt";
97       return;
98     case PPC::PRED_LE_MINUS:
99     case PPC::PRED_LE_PLUS:
100     case PPC::PRED_LE:
101       O << "le";
102       return;
103     case PPC::PRED_EQ_MINUS:
104     case PPC::PRED_EQ_PLUS:
105     case PPC::PRED_EQ:
106       O << "eq";
107       return;
108     case PPC::PRED_GE_MINUS:
109     case PPC::PRED_GE_PLUS:
110     case PPC::PRED_GE:
111       O << "ge";
112       return;
113     case PPC::PRED_GT_MINUS:
114     case PPC::PRED_GT_PLUS:
115     case PPC::PRED_GT:
116       O << "gt";
117       return;
118     case PPC::PRED_NE_MINUS:
119     case PPC::PRED_NE_PLUS:
120     case PPC::PRED_NE:
121       O << "ne";
122       return;
123     case PPC::PRED_UN_MINUS:
124     case PPC::PRED_UN_PLUS:
125     case PPC::PRED_UN:
126       O << "un";
127       return;
128     case PPC::PRED_NU_MINUS:
129     case PPC::PRED_NU_PLUS:
130     case PPC::PRED_NU:
131       O << "nu";
132       return;
133     default:
134       llvm_unreachable("Invalid predicate code");
135     }
136   }
137
138   if (StringRef(Modifier) == "pm") {
139     switch ((PPC::Predicate)Code) {
140     case PPC::PRED_LT:
141     case PPC::PRED_LE:
142     case PPC::PRED_EQ:
143     case PPC::PRED_GE:
144     case PPC::PRED_GT:
145     case PPC::PRED_NE:
146     case PPC::PRED_UN:
147     case PPC::PRED_NU:
148       return;
149     case PPC::PRED_LT_MINUS:
150     case PPC::PRED_LE_MINUS:
151     case PPC::PRED_EQ_MINUS:
152     case PPC::PRED_GE_MINUS:
153     case PPC::PRED_GT_MINUS:
154     case PPC::PRED_NE_MINUS:
155     case PPC::PRED_UN_MINUS:
156     case PPC::PRED_NU_MINUS:
157       O << "-";
158       return;
159     case PPC::PRED_LT_PLUS:
160     case PPC::PRED_LE_PLUS:
161     case PPC::PRED_EQ_PLUS:
162     case PPC::PRED_GE_PLUS:
163     case PPC::PRED_GT_PLUS:
164     case PPC::PRED_NE_PLUS:
165     case PPC::PRED_UN_PLUS:
166     case PPC::PRED_NU_PLUS:
167       O << "+";
168       return;
169     default:
170       llvm_unreachable("Invalid predicate code");
171     }
172   }
173   
174   assert(StringRef(Modifier) == "reg" &&
175          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
176   printOperand(MI, OpNo+1, O);
177 }
178
179 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
180                                        raw_ostream &O) {
181   int Value = MI->getOperand(OpNo).getImm();
182   Value = SignExtend32<5>(Value);
183   O << (int)Value;
184 }
185
186 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
187                                        raw_ostream &O) {
188   unsigned int Value = MI->getOperand(OpNo).getImm();
189   assert(Value <= 31 && "Invalid u5imm argument!");
190   O << (unsigned int)Value;
191 }
192
193 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
194                                        raw_ostream &O) {
195   unsigned int Value = MI->getOperand(OpNo).getImm();
196   assert(Value <= 63 && "Invalid u6imm argument!");
197   O << (unsigned int)Value;
198 }
199
200 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
201                                         raw_ostream &O) {
202   if (MI->getOperand(OpNo).isImm())
203     O << (short)MI->getOperand(OpNo).getImm();
204   else
205     printOperand(MI, OpNo, O);
206 }
207
208 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
209                                         raw_ostream &O) {
210   O << (unsigned short)MI->getOperand(OpNo).getImm();
211 }
212
213 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
214                                         raw_ostream &O) {
215   if (!MI->getOperand(OpNo).isImm())
216     return printOperand(MI, OpNo, O);
217
218   // Branches can take an immediate operand.  This is used by the branch
219   // selection pass to print .+8, an eight byte displacement from the PC.
220   O << ".+";
221   printAbsBranchOperand(MI, OpNo, O);
222 }
223
224 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
225                                            raw_ostream &O) {
226   if (!MI->getOperand(OpNo).isImm())
227     return printOperand(MI, OpNo, O);
228
229   O << (int)MI->getOperand(OpNo).getImm()*4;
230 }
231
232
233 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
234                                  raw_ostream &O) {
235   unsigned CCReg = MI->getOperand(OpNo).getReg();
236   unsigned RegNo;
237   switch (CCReg) {
238   default: llvm_unreachable("Unknown CR register");
239   case PPC::CR0: RegNo = 0; break;
240   case PPC::CR1: RegNo = 1; break;
241   case PPC::CR2: RegNo = 2; break;
242   case PPC::CR3: RegNo = 3; break;
243   case PPC::CR4: RegNo = 4; break;
244   case PPC::CR5: RegNo = 5; break;
245   case PPC::CR6: RegNo = 6; break;
246   case PPC::CR7: RegNo = 7; break;
247   }
248   O << (0x80 >> RegNo);
249 }
250
251 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
252                                     raw_ostream &O) {
253   printS16ImmOperand(MI, OpNo, O);
254   O << '(';
255   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
256     O << "0";
257   else
258     printOperand(MI, OpNo+1, O);
259   O << ')';
260 }
261
262 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
263                                     raw_ostream &O) {
264   // When used as the base register, r0 reads constant zero rather than
265   // the value contained in the register.  For this reason, the darwin
266   // assembler requires that we print r0 as 0 (no r) when used as the base.
267   if (MI->getOperand(OpNo).getReg() == PPC::R0)
268     O << "0";
269   else
270     printOperand(MI, OpNo, O);
271   O << ", ";
272   printOperand(MI, OpNo+1, O);
273 }
274
275
276
277 /// stripRegisterPrefix - This method strips the character prefix from a
278 /// register name so that only the number is left.  Used by for linux asm.
279 static const char *stripRegisterPrefix(const char *RegName) {
280   switch (RegName[0]) {
281   case 'r':
282   case 'f':
283   case 'v': return RegName + 1;
284   case 'c': if (RegName[1] == 'r') return RegName + 2;
285   }
286   
287   return RegName;
288 }
289
290 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
291                                   raw_ostream &O) {
292   const MCOperand &Op = MI->getOperand(OpNo);
293   if (Op.isReg()) {
294     const char *RegName = getRegisterName(Op.getReg());
295     // The linux and AIX assembler does not take register prefixes.
296     if (!isDarwinSyntax())
297       RegName = stripRegisterPrefix(RegName);
298     
299     O << RegName;
300     return;
301   }
302   
303   if (Op.isImm()) {
304     O << Op.getImm();
305     return;
306   }
307   
308   assert(Op.isExpr() && "unknown operand kind in printOperand");
309   O << *Op.getExpr();
310 }
311