c87499ffeaeb7857f186ce290f54fac49342c14e
[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 #include "PPCInstPrinter.h"
15 #include "MCTargetDesc/PPCMCTargetDesc.h"
16 #include "MCTargetDesc/PPCPredicates.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCSymbol.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 #define DEBUG_TYPE "asm-printer"
27
28 // FIXME: Once the integrated assembler supports full register names, tie this
29 // to the verbose-asm setting.
30 static cl::opt<bool>
31 FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
32              cl::desc("Use full register names when printing assembly"));
33
34 #define PRINT_ALIAS_INSTR
35 #include "PPCGenAsmWriter.inc"
36
37 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
38   const char *RegName = getRegisterName(RegNo);
39   if (RegName[0] == 'q' /* QPX */) {
40     // The system toolchain on the BG/Q does not understand QPX register names
41     // in .cfi_* directives, so print the name of the floating-point
42     // subregister instead.
43     std::string RN(RegName);
44
45     RN[0] = 'f';
46     OS << RN;
47
48     return;
49   }
50
51   OS << RegName;
52 }
53
54 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
55                                StringRef Annot, const MCSubtargetInfo &STI) {
56   // Check for slwi/srwi mnemonics.
57   if (MI->getOpcode() == PPC::RLWINM) {
58     unsigned char SH = MI->getOperand(2).getImm();
59     unsigned char MB = MI->getOperand(3).getImm();
60     unsigned char ME = MI->getOperand(4).getImm();
61     bool useSubstituteMnemonic = false;
62     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
63       O << "\tslwi "; useSubstituteMnemonic = true;
64     }
65     if (SH <= 31 && MB == (32-SH) && ME == 31) {
66       O << "\tsrwi "; useSubstituteMnemonic = true;
67       SH = 32-SH;
68     }
69     if (useSubstituteMnemonic) {
70       printOperand(MI, 0, O);
71       O << ", ";
72       printOperand(MI, 1, O);
73       O << ", " << (unsigned int)SH;
74
75       printAnnotation(O, Annot);
76       return;
77     }
78   }
79   
80   if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
81       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
82     O << "\tmr ";
83     printOperand(MI, 0, O);
84     O << ", ";
85     printOperand(MI, 1, O);
86     printAnnotation(O, Annot);
87     return;
88   }
89   
90   if (MI->getOpcode() == PPC::RLDICR) {
91     unsigned char SH = MI->getOperand(2).getImm();
92     unsigned char ME = MI->getOperand(3).getImm();
93     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
94     if (63-SH == ME) {
95       O << "\tsldi ";
96       printOperand(MI, 0, O);
97       O << ", ";
98       printOperand(MI, 1, O);
99       O << ", " << (unsigned int)SH;
100       printAnnotation(O, Annot);
101       return;
102     }
103   }
104
105   // dcbt[st] is printed manually here because:
106   //  1. The assembly syntax is different between embedded and server targets
107   //  2. We must print the short mnemonics for TH == 0 because the
108   //     embedded/server syntax default will not be stable across assemblers
109   //  The syntax for dcbt is:
110   //    dcbt ra, rb, th [server]
111   //    dcbt th, ra, rb [embedded]
112   //  where th can be omitted when it is 0. dcbtst is the same.
113   if (MI->getOpcode() == PPC::DCBT || MI->getOpcode() == PPC::DCBTST) {
114     unsigned char TH = MI->getOperand(0).getImm();
115     O << "\tdcbt";
116     if (MI->getOpcode() == PPC::DCBTST)
117       O << "st";
118     if (TH == 16)
119       O << "t";
120     O << " ";
121
122     bool IsBookE = (STI.getFeatureBits() & PPC::FeatureBookE) != 0;
123     if (IsBookE && TH != 0 && TH != 16)
124       O << (unsigned int) TH << ", ";
125
126     printOperand(MI, 1, O);
127     O << ", ";
128     printOperand(MI, 2, O);
129
130     if (!IsBookE && TH != 0 && TH != 16)
131       O << ", " << (unsigned int) TH;
132
133     printAnnotation(O, Annot);
134     return;
135   }
136   
137   // For fast-isel, a COPY_TO_REGCLASS may survive this long.  This is
138   // used when converting a 32-bit float to a 64-bit float as part of
139   // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
140   // as otherwise we have problems with incorrect register classes
141   // in machine instruction verification.  For now, just avoid trying
142   // to print it as such an instruction has no effect (a 32-bit float
143   // in a register is already in 64-bit form, just with lower
144   // precision).  FIXME: Is there a better solution?
145   if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
146     return;
147
148   if (!printAliasInstr(MI, O))
149     printInstruction(MI, O);
150   printAnnotation(O, Annot);
151 }
152
153
154 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
155                                            raw_ostream &O, 
156                                            const char *Modifier) {
157   unsigned Code = MI->getOperand(OpNo).getImm();
158
159   if (StringRef(Modifier) == "cc") {
160     switch ((PPC::Predicate)Code) {
161     case PPC::PRED_LT_MINUS:
162     case PPC::PRED_LT_PLUS:
163     case PPC::PRED_LT:
164       O << "lt";
165       return;
166     case PPC::PRED_LE_MINUS:
167     case PPC::PRED_LE_PLUS:
168     case PPC::PRED_LE:
169       O << "le";
170       return;
171     case PPC::PRED_EQ_MINUS:
172     case PPC::PRED_EQ_PLUS:
173     case PPC::PRED_EQ:
174       O << "eq";
175       return;
176     case PPC::PRED_GE_MINUS:
177     case PPC::PRED_GE_PLUS:
178     case PPC::PRED_GE:
179       O << "ge";
180       return;
181     case PPC::PRED_GT_MINUS:
182     case PPC::PRED_GT_PLUS:
183     case PPC::PRED_GT:
184       O << "gt";
185       return;
186     case PPC::PRED_NE_MINUS:
187     case PPC::PRED_NE_PLUS:
188     case PPC::PRED_NE:
189       O << "ne";
190       return;
191     case PPC::PRED_UN_MINUS:
192     case PPC::PRED_UN_PLUS:
193     case PPC::PRED_UN:
194       O << "un";
195       return;
196     case PPC::PRED_NU_MINUS:
197     case PPC::PRED_NU_PLUS:
198     case PPC::PRED_NU:
199       O << "nu";
200       return;
201     case PPC::PRED_BIT_SET:
202     case PPC::PRED_BIT_UNSET:
203       llvm_unreachable("Invalid use of bit predicate code");
204     }
205     llvm_unreachable("Invalid predicate code");
206   }
207
208   if (StringRef(Modifier) == "pm") {
209     switch ((PPC::Predicate)Code) {
210     case PPC::PRED_LT:
211     case PPC::PRED_LE:
212     case PPC::PRED_EQ:
213     case PPC::PRED_GE:
214     case PPC::PRED_GT:
215     case PPC::PRED_NE:
216     case PPC::PRED_UN:
217     case PPC::PRED_NU:
218       return;
219     case PPC::PRED_LT_MINUS:
220     case PPC::PRED_LE_MINUS:
221     case PPC::PRED_EQ_MINUS:
222     case PPC::PRED_GE_MINUS:
223     case PPC::PRED_GT_MINUS:
224     case PPC::PRED_NE_MINUS:
225     case PPC::PRED_UN_MINUS:
226     case PPC::PRED_NU_MINUS:
227       O << "-";
228       return;
229     case PPC::PRED_LT_PLUS:
230     case PPC::PRED_LE_PLUS:
231     case PPC::PRED_EQ_PLUS:
232     case PPC::PRED_GE_PLUS:
233     case PPC::PRED_GT_PLUS:
234     case PPC::PRED_NE_PLUS:
235     case PPC::PRED_UN_PLUS:
236     case PPC::PRED_NU_PLUS:
237       O << "+";
238       return;
239     case PPC::PRED_BIT_SET:
240     case PPC::PRED_BIT_UNSET:
241       llvm_unreachable("Invalid use of bit predicate code");
242     }
243     llvm_unreachable("Invalid predicate code");
244   }
245   
246   assert(StringRef(Modifier) == "reg" &&
247          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
248   printOperand(MI, OpNo+1, O);
249 }
250
251 void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
252                                        raw_ostream &O) {
253   unsigned int Value = MI->getOperand(OpNo).getImm();
254   assert(Value <= 1 && "Invalid u1imm argument!");
255   O << (unsigned int)Value;
256 }
257
258 void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
259                                        raw_ostream &O) {
260   unsigned int Value = MI->getOperand(OpNo).getImm();
261   assert(Value <= 3 && "Invalid u2imm argument!");
262   O << (unsigned int)Value;
263 }
264
265 void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
266                                        raw_ostream &O) {
267   unsigned int Value = MI->getOperand(OpNo).getImm();
268   assert(Value <= 8 && "Invalid u3imm argument!");
269   O << (unsigned int)Value;
270 }
271
272 void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
273                                        raw_ostream &O) {
274   unsigned int Value = MI->getOperand(OpNo).getImm();
275   assert(Value <= 15 && "Invalid u4imm argument!");
276   O << (unsigned int)Value;
277 }
278
279 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
280                                        raw_ostream &O) {
281   int Value = MI->getOperand(OpNo).getImm();
282   Value = SignExtend32<5>(Value);
283   O << (int)Value;
284 }
285
286 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
287                                        raw_ostream &O) {
288   unsigned int Value = MI->getOperand(OpNo).getImm();
289   assert(Value <= 31 && "Invalid u5imm argument!");
290   O << (unsigned int)Value;
291 }
292
293 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
294                                        raw_ostream &O) {
295   unsigned int Value = MI->getOperand(OpNo).getImm();
296   assert(Value <= 63 && "Invalid u6imm argument!");
297   O << (unsigned int)Value;
298 }
299
300 void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
301                                         raw_ostream &O) {
302   unsigned short Value = MI->getOperand(OpNo).getImm();
303   assert(Value <= 4095 && "Invalid u12imm argument!");
304   O << (unsigned short)Value;
305 }
306
307 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
308                                         raw_ostream &O) {
309   if (MI->getOperand(OpNo).isImm())
310     O << (short)MI->getOperand(OpNo).getImm();
311   else
312     printOperand(MI, OpNo, O);
313 }
314
315 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
316                                         raw_ostream &O) {
317   if (MI->getOperand(OpNo).isImm())
318     O << (unsigned short)MI->getOperand(OpNo).getImm();
319   else
320     printOperand(MI, OpNo, O);
321 }
322
323 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
324                                         raw_ostream &O) {
325   if (!MI->getOperand(OpNo).isImm())
326     return printOperand(MI, OpNo, O);
327
328   // Branches can take an immediate operand.  This is used by the branch
329   // selection pass to print .+8, an eight byte displacement from the PC.
330   O << ".+";
331   printAbsBranchOperand(MI, OpNo, O);
332 }
333
334 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
335                                            raw_ostream &O) {
336   if (!MI->getOperand(OpNo).isImm())
337     return printOperand(MI, OpNo, O);
338
339   O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
340 }
341
342
343 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
344                                  raw_ostream &O) {
345   unsigned CCReg = MI->getOperand(OpNo).getReg();
346   unsigned RegNo;
347   switch (CCReg) {
348   default: llvm_unreachable("Unknown CR register");
349   case PPC::CR0: RegNo = 0; break;
350   case PPC::CR1: RegNo = 1; break;
351   case PPC::CR2: RegNo = 2; break;
352   case PPC::CR3: RegNo = 3; break;
353   case PPC::CR4: RegNo = 4; break;
354   case PPC::CR5: RegNo = 5; break;
355   case PPC::CR6: RegNo = 6; break;
356   case PPC::CR7: RegNo = 7; break;
357   }
358   O << (0x80 >> RegNo);
359 }
360
361 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
362                                     raw_ostream &O) {
363   printS16ImmOperand(MI, OpNo, O);
364   O << '(';
365   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
366     O << "0";
367   else
368     printOperand(MI, OpNo+1, O);
369   O << ')';
370 }
371
372 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
373                                     raw_ostream &O) {
374   // When used as the base register, r0 reads constant zero rather than
375   // the value contained in the register.  For this reason, the darwin
376   // assembler requires that we print r0 as 0 (no r) when used as the base.
377   if (MI->getOperand(OpNo).getReg() == PPC::R0)
378     O << "0";
379   else
380     printOperand(MI, OpNo, O);
381   O << ", ";
382   printOperand(MI, OpNo+1, O);
383 }
384
385 void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
386                                   raw_ostream &O) {
387   // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
388   // come at the _end_ of the expression.
389   const MCOperand &Op = MI->getOperand(OpNo);
390   const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
391   O << refExp.getSymbol().getName();
392   O << '(';
393   printOperand(MI, OpNo+1, O);
394   O << ')';
395   if (refExp.getKind() != MCSymbolRefExpr::VK_None)
396     O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
397 }
398
399
400 /// stripRegisterPrefix - This method strips the character prefix from a
401 /// register name so that only the number is left.  Used by for linux asm.
402 static const char *stripRegisterPrefix(const char *RegName) {
403   if (FullRegNames)
404     return RegName;
405
406   switch (RegName[0]) {
407   case 'r':
408   case 'f':
409   case 'q': // for QPX
410   case 'v':
411     if (RegName[1] == 's')
412       return RegName + 2;
413     return RegName + 1;
414   case 'c': if (RegName[1] == 'r') return RegName + 2;
415   }
416   
417   return RegName;
418 }
419
420 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
421                                   raw_ostream &O) {
422   const MCOperand &Op = MI->getOperand(OpNo);
423   if (Op.isReg()) {
424     const char *RegName = getRegisterName(Op.getReg());
425     // The linux and AIX assembler does not take register prefixes.
426     if (!isDarwinSyntax())
427       RegName = stripRegisterPrefix(RegName);
428     
429     O << RegName;
430     return;
431   }
432   
433   if (Op.isImm()) {
434     O << Op.getImm();
435     return;
436   }
437   
438   assert(Op.isExpr() && "unknown operand kind in printOperand");
439   O << *Op.getExpr();
440 }
441