Add CR-bit tracking to the PowerPC backend for i1 values
[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/CommandLine.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetOpcodes.h"
24 using namespace llvm;
25
26 // FIXME: Once the integrated assembler supports full register names, tie this
27 // to the verbose-asm setting.
28 static cl::opt<bool>
29 FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
30              cl::desc("Use full register names when printing assembly"));
31
32 #include "PPCGenAsmWriter.inc"
33
34 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
35   OS << getRegisterName(RegNo);
36 }
37
38 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
39                                StringRef Annot) {
40   // Check for slwi/srwi mnemonics.
41   if (MI->getOpcode() == PPC::RLWINM) {
42     unsigned char SH = MI->getOperand(2).getImm();
43     unsigned char MB = MI->getOperand(3).getImm();
44     unsigned char ME = MI->getOperand(4).getImm();
45     bool useSubstituteMnemonic = false;
46     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
47       O << "\tslwi "; useSubstituteMnemonic = true;
48     }
49     if (SH <= 31 && MB == (32-SH) && ME == 31) {
50       O << "\tsrwi "; useSubstituteMnemonic = true;
51       SH = 32-SH;
52     }
53     if (useSubstituteMnemonic) {
54       printOperand(MI, 0, O);
55       O << ", ";
56       printOperand(MI, 1, O);
57       O << ", " << (unsigned int)SH;
58
59       printAnnotation(O, Annot);
60       return;
61     }
62   }
63   
64   if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
65       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
66     O << "\tmr ";
67     printOperand(MI, 0, O);
68     O << ", ";
69     printOperand(MI, 1, O);
70     printAnnotation(O, Annot);
71     return;
72   }
73   
74   if (MI->getOpcode() == PPC::RLDICR) {
75     unsigned char SH = MI->getOperand(2).getImm();
76     unsigned char ME = MI->getOperand(3).getImm();
77     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
78     if (63-SH == ME) {
79       O << "\tsldi ";
80       printOperand(MI, 0, O);
81       O << ", ";
82       printOperand(MI, 1, O);
83       O << ", " << (unsigned int)SH;
84       printAnnotation(O, Annot);
85       return;
86     }
87   }
88   
89   // For fast-isel, a COPY_TO_REGCLASS may survive this long.  This is
90   // used when converting a 32-bit float to a 64-bit float as part of
91   // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
92   // as otherwise we have problems with incorrect register classes
93   // in machine instruction verification.  For now, just avoid trying
94   // to print it as such an instruction has no effect (a 32-bit float
95   // in a register is already in 64-bit form, just with lower
96   // precision).  FIXME: Is there a better solution?
97   if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
98     return;
99   
100   printInstruction(MI, O);
101   printAnnotation(O, Annot);
102 }
103
104
105 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
106                                            raw_ostream &O, 
107                                            const char *Modifier) {
108   unsigned Code = MI->getOperand(OpNo).getImm();
109
110   if (StringRef(Modifier) == "cc") {
111     switch ((PPC::Predicate)Code) {
112     case PPC::PRED_LT_MINUS:
113     case PPC::PRED_LT_PLUS:
114     case PPC::PRED_LT:
115       O << "lt";
116       return;
117     case PPC::PRED_LE_MINUS:
118     case PPC::PRED_LE_PLUS:
119     case PPC::PRED_LE:
120       O << "le";
121       return;
122     case PPC::PRED_EQ_MINUS:
123     case PPC::PRED_EQ_PLUS:
124     case PPC::PRED_EQ:
125       O << "eq";
126       return;
127     case PPC::PRED_GE_MINUS:
128     case PPC::PRED_GE_PLUS:
129     case PPC::PRED_GE:
130       O << "ge";
131       return;
132     case PPC::PRED_GT_MINUS:
133     case PPC::PRED_GT_PLUS:
134     case PPC::PRED_GT:
135       O << "gt";
136       return;
137     case PPC::PRED_NE_MINUS:
138     case PPC::PRED_NE_PLUS:
139     case PPC::PRED_NE:
140       O << "ne";
141       return;
142     case PPC::PRED_UN_MINUS:
143     case PPC::PRED_UN_PLUS:
144     case PPC::PRED_UN:
145       O << "un";
146       return;
147     case PPC::PRED_NU_MINUS:
148     case PPC::PRED_NU_PLUS:
149     case PPC::PRED_NU:
150       O << "nu";
151       return;
152     case PPC::PRED_BIT_SET:
153     case PPC::PRED_BIT_UNSET:
154       llvm_unreachable("Invalid use of bit predicate code");
155     }
156     llvm_unreachable("Invalid predicate code");
157   }
158
159   if (StringRef(Modifier) == "pm") {
160     switch ((PPC::Predicate)Code) {
161     case PPC::PRED_LT:
162     case PPC::PRED_LE:
163     case PPC::PRED_EQ:
164     case PPC::PRED_GE:
165     case PPC::PRED_GT:
166     case PPC::PRED_NE:
167     case PPC::PRED_UN:
168     case PPC::PRED_NU:
169       return;
170     case PPC::PRED_LT_MINUS:
171     case PPC::PRED_LE_MINUS:
172     case PPC::PRED_EQ_MINUS:
173     case PPC::PRED_GE_MINUS:
174     case PPC::PRED_GT_MINUS:
175     case PPC::PRED_NE_MINUS:
176     case PPC::PRED_UN_MINUS:
177     case PPC::PRED_NU_MINUS:
178       O << "-";
179       return;
180     case PPC::PRED_LT_PLUS:
181     case PPC::PRED_LE_PLUS:
182     case PPC::PRED_EQ_PLUS:
183     case PPC::PRED_GE_PLUS:
184     case PPC::PRED_GT_PLUS:
185     case PPC::PRED_NE_PLUS:
186     case PPC::PRED_UN_PLUS:
187     case PPC::PRED_NU_PLUS:
188       O << "+";
189       return;
190     case PPC::PRED_BIT_SET:
191     case PPC::PRED_BIT_UNSET:
192       llvm_unreachable("Invalid use of bit predicate code");
193     }
194     llvm_unreachable("Invalid predicate code");
195   }
196   
197   assert(StringRef(Modifier) == "reg" &&
198          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
199   printOperand(MI, OpNo+1, O);
200 }
201
202 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
203                                        raw_ostream &O) {
204   int Value = MI->getOperand(OpNo).getImm();
205   Value = SignExtend32<5>(Value);
206   O << (int)Value;
207 }
208
209 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
210                                        raw_ostream &O) {
211   unsigned int Value = MI->getOperand(OpNo).getImm();
212   assert(Value <= 31 && "Invalid u5imm argument!");
213   O << (unsigned int)Value;
214 }
215
216 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
217                                        raw_ostream &O) {
218   unsigned int Value = MI->getOperand(OpNo).getImm();
219   assert(Value <= 63 && "Invalid u6imm argument!");
220   O << (unsigned int)Value;
221 }
222
223 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
224                                         raw_ostream &O) {
225   if (MI->getOperand(OpNo).isImm())
226     O << (short)MI->getOperand(OpNo).getImm();
227   else
228     printOperand(MI, OpNo, O);
229 }
230
231 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
232                                         raw_ostream &O) {
233   if (MI->getOperand(OpNo).isImm())
234     O << (unsigned short)MI->getOperand(OpNo).getImm();
235   else
236     printOperand(MI, OpNo, O);
237 }
238
239 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
240                                         raw_ostream &O) {
241   if (!MI->getOperand(OpNo).isImm())
242     return printOperand(MI, OpNo, O);
243
244   // Branches can take an immediate operand.  This is used by the branch
245   // selection pass to print .+8, an eight byte displacement from the PC.
246   O << ".+";
247   printAbsBranchOperand(MI, OpNo, O);
248 }
249
250 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
251                                            raw_ostream &O) {
252   if (!MI->getOperand(OpNo).isImm())
253     return printOperand(MI, OpNo, O);
254
255   O << (int)MI->getOperand(OpNo).getImm()*4;
256 }
257
258
259 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
260                                  raw_ostream &O) {
261   unsigned CCReg = MI->getOperand(OpNo).getReg();
262   unsigned RegNo;
263   switch (CCReg) {
264   default: llvm_unreachable("Unknown CR register");
265   case PPC::CR0: RegNo = 0; break;
266   case PPC::CR1: RegNo = 1; break;
267   case PPC::CR2: RegNo = 2; break;
268   case PPC::CR3: RegNo = 3; break;
269   case PPC::CR4: RegNo = 4; break;
270   case PPC::CR5: RegNo = 5; break;
271   case PPC::CR6: RegNo = 6; break;
272   case PPC::CR7: RegNo = 7; break;
273   }
274   O << (0x80 >> RegNo);
275 }
276
277 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
278                                     raw_ostream &O) {
279   printS16ImmOperand(MI, OpNo, O);
280   O << '(';
281   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
282     O << "0";
283   else
284     printOperand(MI, OpNo+1, O);
285   O << ')';
286 }
287
288 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
289                                     raw_ostream &O) {
290   // When used as the base register, r0 reads constant zero rather than
291   // the value contained in the register.  For this reason, the darwin
292   // assembler requires that we print r0 as 0 (no r) when used as the base.
293   if (MI->getOperand(OpNo).getReg() == PPC::R0)
294     O << "0";
295   else
296     printOperand(MI, OpNo, O);
297   O << ", ";
298   printOperand(MI, OpNo+1, O);
299 }
300
301 void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
302                                   raw_ostream &O) {
303   printBranchOperand(MI, OpNo, O);
304   O << '(';
305   printOperand(MI, OpNo+1, O);
306   O << ')';
307 }
308
309
310 /// stripRegisterPrefix - This method strips the character prefix from a
311 /// register name so that only the number is left.  Used by for linux asm.
312 static const char *stripRegisterPrefix(const char *RegName) {
313   if (FullRegNames)
314     return RegName;
315
316   switch (RegName[0]) {
317   case 'r':
318   case 'f':
319   case 'v': return RegName + 1;
320   case 'c': if (RegName[1] == 'r') return RegName + 2;
321   }
322   
323   return RegName;
324 }
325
326 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
327                                   raw_ostream &O) {
328   const MCOperand &Op = MI->getOperand(OpNo);
329   if (Op.isReg()) {
330     const char *RegName = getRegisterName(Op.getReg());
331     // The linux and AIX assembler does not take register prefixes.
332     if (!isDarwinSyntax())
333       RegName = stripRegisterPrefix(RegName);
334     
335     O << RegName;
336     return;
337   }
338   
339   if (Op.isImm()) {
340     O << Op.getImm();
341     return;
342   }
343   
344   assert(Op.isExpr() && "unknown operand kind in printOperand");
345   O << *Op.getExpr();
346 }
347