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