[PowerPC] Fix switch warnings from r183841.
[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     default: llvm_unreachable("Bad predicate!");
94     case PPC::PRED_LT: O << "lt"; return;
95     case PPC::PRED_LE: O << "le"; return;
96     case PPC::PRED_EQ: O << "eq"; return;
97     case PPC::PRED_GE: O << "ge"; return;
98     case PPC::PRED_GT: O << "gt"; return;
99     case PPC::PRED_NE: O << "ne"; return;
100     case PPC::PRED_UN: O << "un"; return;
101     case PPC::PRED_NU: O << "nu"; return;
102     }
103   }
104   
105   assert(StringRef(Modifier) == "reg" &&
106          "Need to specify 'cc' or 'reg' as predicate op modifier!");
107   printOperand(MI, OpNo+1, O);
108 }
109
110 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
111                                        raw_ostream &O) {
112   int Value = MI->getOperand(OpNo).getImm();
113   Value = SignExtend32<5>(Value);
114   O << (int)Value;
115 }
116
117 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
118                                        raw_ostream &O) {
119   unsigned int Value = MI->getOperand(OpNo).getImm();
120   assert(Value <= 31 && "Invalid u5imm argument!");
121   O << (unsigned int)Value;
122 }
123
124 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
125                                        raw_ostream &O) {
126   unsigned int Value = MI->getOperand(OpNo).getImm();
127   assert(Value <= 63 && "Invalid u6imm argument!");
128   O << (unsigned int)Value;
129 }
130
131 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
132                                         raw_ostream &O) {
133   if (MI->getOperand(OpNo).isImm())
134     O << (short)MI->getOperand(OpNo).getImm();
135   else
136     printOperand(MI, OpNo, O);
137 }
138
139 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
140                                         raw_ostream &O) {
141   O << (unsigned short)MI->getOperand(OpNo).getImm();
142 }
143
144 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
145                                         raw_ostream &O) {
146   if (!MI->getOperand(OpNo).isImm())
147     return printOperand(MI, OpNo, O);
148
149   // Branches can take an immediate operand.  This is used by the branch
150   // selection pass to print .+8, an eight byte displacement from the PC.
151   O << ".+";
152   printAbsAddrOperand(MI, OpNo, O);
153 }
154
155 void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
156                                          raw_ostream &O) {
157   O << (int)MI->getOperand(OpNo).getImm()*4;
158 }
159
160
161 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
162                                  raw_ostream &O) {
163   unsigned CCReg = MI->getOperand(OpNo).getReg();
164   unsigned RegNo;
165   switch (CCReg) {
166   default: llvm_unreachable("Unknown CR register");
167   case PPC::CR0: RegNo = 0; break;
168   case PPC::CR1: RegNo = 1; break;
169   case PPC::CR2: RegNo = 2; break;
170   case PPC::CR3: RegNo = 3; break;
171   case PPC::CR4: RegNo = 4; break;
172   case PPC::CR5: RegNo = 5; break;
173   case PPC::CR6: RegNo = 6; break;
174   case PPC::CR7: RegNo = 7; break;
175   }
176   O << (0x80 >> RegNo);
177 }
178
179 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
180                                     raw_ostream &O) {
181   printS16ImmOperand(MI, OpNo, O);
182   O << '(';
183   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
184     O << "0";
185   else
186     printOperand(MI, OpNo+1, O);
187   O << ')';
188 }
189
190 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
191                                     raw_ostream &O) {
192   // When used as the base register, r0 reads constant zero rather than
193   // the value contained in the register.  For this reason, the darwin
194   // assembler requires that we print r0 as 0 (no r) when used as the base.
195   if (MI->getOperand(OpNo).getReg() == PPC::R0)
196     O << "0";
197   else
198     printOperand(MI, OpNo, O);
199   O << ", ";
200   printOperand(MI, OpNo+1, O);
201 }
202
203
204
205 /// stripRegisterPrefix - This method strips the character prefix from a
206 /// register name so that only the number is left.  Used by for linux asm.
207 static const char *stripRegisterPrefix(const char *RegName) {
208   switch (RegName[0]) {
209   case 'r':
210   case 'f':
211   case 'v': return RegName + 1;
212   case 'c': if (RegName[1] == 'r') return RegName + 2;
213   }
214   
215   return RegName;
216 }
217
218 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
219                                   raw_ostream &O) {
220   const MCOperand &Op = MI->getOperand(OpNo);
221   if (Op.isReg()) {
222     const char *RegName = getRegisterName(Op.getReg());
223     // The linux and AIX assembler does not take register prefixes.
224     if (!isDarwinSyntax())
225       RegName = stripRegisterPrefix(RegName);
226     
227     O << RegName;
228     return;
229   }
230   
231   if (Op.isImm()) {
232     O << Op.getImm();
233     return;
234   }
235   
236   assert(Op.isExpr() && "unknown operand kind in printOperand");
237   O << *Op.getExpr();
238 }
239