blob: 8f34b19925ee40d7c34484c57553c8eabdb4c334 [file] [log] [blame]
Chris Lattner60d5b5f2010-11-14 19:40:38 +00001//===-- 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"
Evan Cheng94b95502011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCBaseInfo.h"
17#include "MCTargetDesc/PPCPredicates.h"
Chris Lattner0d1b7d92010-11-14 20:02:39 +000018#include "llvm/MC/MCExpr.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000019#include "llvm/MC/MCInst.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000021using namespace llvm;
22
23#define GET_INSTRUCTION_NAME
Chris Lattner60d5b5f2010-11-14 19:40:38 +000024#include "PPCGenAsmWriter.inc"
25
26StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
27 return getInstructionName(Opcode);
28}
29
Rafael Espindolacde4ce42011-06-02 02:34:55 +000030void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
31 OS << getRegisterName(RegNo);
Rafael Espindola6e032942011-05-30 20:20:15 +000032}
Chris Lattner60d5b5f2010-11-14 19:40:38 +000033
34void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
Chris Lattner2e352482010-11-14 21:39:51 +000035 // Check for slwi/srwi mnemonics.
36 if (MI->getOpcode() == PPC::RLWINM) {
37 unsigned char SH = MI->getOperand(2).getImm();
38 unsigned char MB = MI->getOperand(3).getImm();
39 unsigned char ME = MI->getOperand(4).getImm();
40 bool useSubstituteMnemonic = false;
41 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
42 O << "\tslwi "; useSubstituteMnemonic = true;
43 }
44 if (SH <= 31 && MB == (32-SH) && ME == 31) {
45 O << "\tsrwi "; useSubstituteMnemonic = true;
46 SH = 32-SH;
47 }
48 if (useSubstituteMnemonic) {
49 printOperand(MI, 0, O);
50 O << ", ";
51 printOperand(MI, 1, O);
52 O << ", " << (unsigned int)SH;
53 return;
54 }
55 }
56
57 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
58 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
59 O << "\tmr ";
60 printOperand(MI, 0, O);
61 O << ", ";
62 printOperand(MI, 1, O);
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 return;
77 }
78 }
79
Chris Lattner60d5b5f2010-11-14 19:40:38 +000080 printInstruction(MI, O);
81}
82
Chris Lattnerb2e477f2010-11-14 21:51:37 +000083
84void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
85 raw_ostream &O,
86 const char *Modifier) {
87 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
88 unsigned Code = MI->getOperand(OpNo).getImm();
89 if (StringRef(Modifier) == "cc") {
90 switch ((PPC::Predicate)Code) {
91 default: assert(0 && "Invalid predicate");
92 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
93 case PPC::PRED_LT: O << "lt"; return;
94 case PPC::PRED_LE: O << "le"; return;
95 case PPC::PRED_EQ: O << "eq"; return;
96 case PPC::PRED_GE: O << "ge"; return;
97 case PPC::PRED_GT: O << "gt"; return;
98 case PPC::PRED_NE: O << "ne"; return;
99 case PPC::PRED_UN: O << "un"; return;
100 case PPC::PRED_NU: O << "nu"; return;
101 }
102 }
103
104 assert(StringRef(Modifier) == "reg" &&
105 "Need to specify 'cc' or 'reg' as predicate op modifier!");
106 // Don't print the register for 'always'.
107 if (Code == PPC::PRED_ALWAYS) return;
108 printOperand(MI, OpNo+1, O);
109}
110
Chris Lattner99889132010-11-14 20:11:21 +0000111void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
112 raw_ostream &O) {
113 char Value = MI->getOperand(OpNo).getImm();
114 Value = (Value << (32-5)) >> (32-5);
115 O << (int)Value;
116}
117
118void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
119 raw_ostream &O) {
120 unsigned char Value = MI->getOperand(OpNo).getImm();
121 assert(Value <= 31 && "Invalid u5imm argument!");
122 O << (unsigned int)Value;
123}
124
125void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
126 raw_ostream &O) {
127 unsigned char Value = MI->getOperand(OpNo).getImm();
128 assert(Value <= 63 && "Invalid u6imm argument!");
129 O << (unsigned int)Value;
130}
131
132void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
133 raw_ostream &O) {
134 O << (short)MI->getOperand(OpNo).getImm();
135}
136
137void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
138 raw_ostream &O) {
139 O << (unsigned short)MI->getOperand(OpNo).getImm();
140}
141
142void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
143 raw_ostream &O) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000144 if (MI->getOperand(OpNo).isImm())
Chris Lattner99889132010-11-14 20:11:21 +0000145 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner99889132010-11-14 20:11:21 +0000146 else
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000147 printOperand(MI, OpNo, O);
Chris Lattner99889132010-11-14 20:11:21 +0000148}
149
Chris Lattner1520fd62010-11-14 21:20:46 +0000150void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
151 raw_ostream &O) {
152 if (!MI->getOperand(OpNo).isImm())
153 return printOperand(MI, OpNo, O);
154
155 // Branches can take an immediate operand. This is used by the branch
156 // selection pass to print $+8, an eight byte displacement from the PC.
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000157 O << "$+";
158 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner1520fd62010-11-14 21:20:46 +0000159}
160
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000161void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
162 raw_ostream &O) {
163 O << (int)MI->getOperand(OpNo).getImm()*4;
164}
Chris Lattner1520fd62010-11-14 21:20:46 +0000165
166
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000167void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
168 raw_ostream &O) {
169 unsigned CCReg = MI->getOperand(OpNo).getReg();
170 unsigned RegNo;
171 switch (CCReg) {
172 default: assert(0 && "Unknown CR register");
173 case PPC::CR0: RegNo = 0; break;
174 case PPC::CR1: RegNo = 1; break;
175 case PPC::CR2: RegNo = 2; break;
176 case PPC::CR3: RegNo = 3; break;
177 case PPC::CR4: RegNo = 4; break;
178 case PPC::CR5: RegNo = 5; break;
179 case PPC::CR6: RegNo = 6; break;
180 case PPC::CR7: RegNo = 7; break;
181 }
182 O << (0x80 >> RegNo);
183}
184
185void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
186 raw_ostream &O) {
187 printSymbolLo(MI, OpNo, O);
188 O << '(';
Chris Lattner0fe71842010-11-15 03:51:13 +0000189 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000190 O << "0";
191 else
192 printOperand(MI, OpNo+1, O);
193 O << ')';
194}
195
196void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
197 raw_ostream &O) {
198 if (MI->getOperand(OpNo).isImm())
199 printS16X4ImmOperand(MI, OpNo, O);
200 else
201 printSymbolLo(MI, OpNo, O);
202 O << '(';
203
Chris Lattner0fe71842010-11-15 03:51:13 +0000204 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000205 O << "0";
206 else
207 printOperand(MI, OpNo+1, O);
208 O << ')';
209}
210
211
212void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
213 raw_ostream &O) {
214 // When used as the base register, r0 reads constant zero rather than
215 // the value contained in the register. For this reason, the darwin
216 // assembler requires that we print r0 as 0 (no r) when used as the base.
217 if (MI->getOperand(OpNo).getReg() == PPC::R0)
218 O << "0";
219 else
220 printOperand(MI, OpNo, O);
221 O << ", ";
222 printOperand(MI, OpNo+1, O);
223}
224
225
Chris Lattner99889132010-11-14 20:11:21 +0000226
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000227/// stripRegisterPrefix - This method strips the character prefix from a
228/// register name so that only the number is left. Used by for linux asm.
Benjamin Kramerc62feda2010-11-25 16:42:51 +0000229static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000230 switch (RegName[0]) {
231 case 'r':
232 case 'f':
233 case 'v': return RegName + 1;
234 case 'c': if (RegName[1] == 'r') return RegName + 2;
235 }
236
237 return RegName;
238}
239
240void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
241 raw_ostream &O) {
242 const MCOperand &Op = MI->getOperand(OpNo);
243 if (Op.isReg()) {
244 const char *RegName = getRegisterName(Op.getReg());
245 // The linux and AIX assembler does not take register prefixes.
246 if (!isDarwinSyntax())
247 RegName = stripRegisterPrefix(RegName);
248
249 O << RegName;
250 return;
251 }
252
253 if (Op.isImm()) {
254 O << Op.getImm();
255 return;
256 }
257
258 assert(Op.isExpr() && "unknown operand kind in printOperand");
259 O << *Op.getExpr();
260}
261
Chris Lattner58d014f2010-11-14 21:33:07 +0000262void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
263 raw_ostream &O) {
264 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000265 return printS16ImmOperand(MI, OpNo, O);
266
267 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
268 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
269 if (MI->getOperand(OpNo).isExpr() &&
270 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
271 O << "lo16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000272 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000273 O << ')';
274 } else {
275 printOperand(MI, OpNo, O);
276 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000277}
278
279void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
280 raw_ostream &O) {
281 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000282 return printS16ImmOperand(MI, OpNo, O);
283
284 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
285 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
286 if (MI->getOperand(OpNo).isExpr() &&
287 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
288 O << "ha16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000289 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000290 O << ')';
291 } else {
292 printOperand(MI, OpNo, O);
293 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000294}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000295
296