blob: c8db0c40476540b04e9d5aeed8871a1c2f525c0e [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"
Chris Lattnerb2e477f2010-11-14 21:51:37 +000016#include "PPCPredicates.h"
Chris Lattner0d1b7d92010-11-14 20:02:39 +000017#include "llvm/MC/MCExpr.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000018#include "llvm/MC/MCInst.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000020using namespace llvm;
21
22#define GET_INSTRUCTION_NAME
Chris Lattner60d5b5f2010-11-14 19:40:38 +000023#include "PPCGenAsmWriter.inc"
24
25StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
26 return getInstructionName(Opcode);
27}
28
29
30void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
Chris Lattner2e352482010-11-14 21:39:51 +000031 // Check for slwi/srwi mnemonics.
32 if (MI->getOpcode() == PPC::RLWINM) {
33 unsigned char SH = MI->getOperand(2).getImm();
34 unsigned char MB = MI->getOperand(3).getImm();
35 unsigned char ME = MI->getOperand(4).getImm();
36 bool useSubstituteMnemonic = false;
37 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
38 O << "\tslwi "; useSubstituteMnemonic = true;
39 }
40 if (SH <= 31 && MB == (32-SH) && ME == 31) {
41 O << "\tsrwi "; useSubstituteMnemonic = true;
42 SH = 32-SH;
43 }
44 if (useSubstituteMnemonic) {
45 printOperand(MI, 0, O);
46 O << ", ";
47 printOperand(MI, 1, O);
48 O << ", " << (unsigned int)SH;
49 return;
50 }
51 }
52
53 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
54 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
55 O << "\tmr ";
56 printOperand(MI, 0, O);
57 O << ", ";
58 printOperand(MI, 1, O);
59 return;
60 }
61
62 if (MI->getOpcode() == PPC::RLDICR) {
63 unsigned char SH = MI->getOperand(2).getImm();
64 unsigned char ME = MI->getOperand(3).getImm();
65 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
66 if (63-SH == ME) {
67 O << "\tsldi ";
68 printOperand(MI, 0, O);
69 O << ", ";
70 printOperand(MI, 1, O);
71 O << ", " << (unsigned int)SH;
72 return;
73 }
74 }
75
Chris Lattner60d5b5f2010-11-14 19:40:38 +000076 printInstruction(MI, O);
77}
78
Chris Lattnerb2e477f2010-11-14 21:51:37 +000079
80void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
81 raw_ostream &O,
82 const char *Modifier) {
83 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
84 unsigned Code = MI->getOperand(OpNo).getImm();
85 if (StringRef(Modifier) == "cc") {
86 switch ((PPC::Predicate)Code) {
87 default: assert(0 && "Invalid predicate");
88 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
89 case PPC::PRED_LT: O << "lt"; return;
90 case PPC::PRED_LE: O << "le"; return;
91 case PPC::PRED_EQ: O << "eq"; return;
92 case PPC::PRED_GE: O << "ge"; return;
93 case PPC::PRED_GT: O << "gt"; return;
94 case PPC::PRED_NE: O << "ne"; return;
95 case PPC::PRED_UN: O << "un"; return;
96 case PPC::PRED_NU: O << "nu"; return;
97 }
98 }
99
100 assert(StringRef(Modifier) == "reg" &&
101 "Need to specify 'cc' or 'reg' as predicate op modifier!");
102 // Don't print the register for 'always'.
103 if (Code == PPC::PRED_ALWAYS) return;
104 printOperand(MI, OpNo+1, O);
105}
106
Chris Lattner99889132010-11-14 20:11:21 +0000107void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
108 raw_ostream &O) {
109 char Value = MI->getOperand(OpNo).getImm();
110 Value = (Value << (32-5)) >> (32-5);
111 O << (int)Value;
112}
113
114void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
115 raw_ostream &O) {
116 unsigned char Value = MI->getOperand(OpNo).getImm();
117 assert(Value <= 31 && "Invalid u5imm argument!");
118 O << (unsigned int)Value;
119}
120
121void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
122 raw_ostream &O) {
123 unsigned char Value = MI->getOperand(OpNo).getImm();
124 assert(Value <= 63 && "Invalid u6imm argument!");
125 O << (unsigned int)Value;
126}
127
128void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
129 raw_ostream &O) {
130 O << (short)MI->getOperand(OpNo).getImm();
131}
132
133void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
134 raw_ostream &O) {
135 O << (unsigned short)MI->getOperand(OpNo).getImm();
136}
137
138void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
139 raw_ostream &O) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000140 if (MI->getOperand(OpNo).isImm())
Chris Lattner99889132010-11-14 20:11:21 +0000141 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner99889132010-11-14 20:11:21 +0000142 else
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000143 printOperand(MI, OpNo, O);
Chris Lattner99889132010-11-14 20:11:21 +0000144}
145
Chris Lattner1520fd62010-11-14 21:20:46 +0000146void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
147 raw_ostream &O) {
148 if (!MI->getOperand(OpNo).isImm())
149 return printOperand(MI, OpNo, O);
150
151 // Branches can take an immediate operand. This is used by the branch
152 // selection pass to print $+8, an eight byte displacement from the PC.
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000153 O << "$+";
154 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner1520fd62010-11-14 21:20:46 +0000155}
156
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000157void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
158 raw_ostream &O) {
159 O << (int)MI->getOperand(OpNo).getImm()*4;
160}
Chris Lattner1520fd62010-11-14 21:20:46 +0000161
162
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000163void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
164 raw_ostream &O) {
165 unsigned CCReg = MI->getOperand(OpNo).getReg();
166 unsigned RegNo;
167 switch (CCReg) {
168 default: assert(0 && "Unknown CR register");
169 case PPC::CR0: RegNo = 0; break;
170 case PPC::CR1: RegNo = 1; break;
171 case PPC::CR2: RegNo = 2; break;
172 case PPC::CR3: RegNo = 3; break;
173 case PPC::CR4: RegNo = 4; break;
174 case PPC::CR5: RegNo = 5; break;
175 case PPC::CR6: RegNo = 6; break;
176 case PPC::CR7: RegNo = 7; break;
177 }
178 O << (0x80 >> RegNo);
179}
180
181void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
182 raw_ostream &O) {
183 printSymbolLo(MI, OpNo, O);
184 O << '(';
Chris Lattner0fe71842010-11-15 03:51:13 +0000185 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000186 O << "0";
187 else
188 printOperand(MI, OpNo+1, O);
189 O << ')';
190}
191
192void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
193 raw_ostream &O) {
194 if (MI->getOperand(OpNo).isImm())
195 printS16X4ImmOperand(MI, OpNo, O);
196 else
197 printSymbolLo(MI, OpNo, O);
198 O << '(';
199
Chris Lattner0fe71842010-11-15 03:51:13 +0000200 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000201 O << "0";
202 else
203 printOperand(MI, OpNo+1, O);
204 O << ')';
205}
206
207
208void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
209 raw_ostream &O) {
210 // When used as the base register, r0 reads constant zero rather than
211 // the value contained in the register. For this reason, the darwin
212 // assembler requires that we print r0 as 0 (no r) when used as the base.
213 if (MI->getOperand(OpNo).getReg() == PPC::R0)
214 O << "0";
215 else
216 printOperand(MI, OpNo, O);
217 O << ", ";
218 printOperand(MI, OpNo+1, O);
219}
220
221
Chris Lattner99889132010-11-14 20:11:21 +0000222
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000223/// stripRegisterPrefix - This method strips the character prefix from a
224/// register name so that only the number is left. Used by for linux asm.
Benjamin Kramerc62feda2010-11-25 16:42:51 +0000225static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000226 switch (RegName[0]) {
227 case 'r':
228 case 'f':
229 case 'v': return RegName + 1;
230 case 'c': if (RegName[1] == 'r') return RegName + 2;
231 }
232
233 return RegName;
234}
235
236void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
237 raw_ostream &O) {
238 const MCOperand &Op = MI->getOperand(OpNo);
239 if (Op.isReg()) {
240 const char *RegName = getRegisterName(Op.getReg());
241 // The linux and AIX assembler does not take register prefixes.
242 if (!isDarwinSyntax())
243 RegName = stripRegisterPrefix(RegName);
244
245 O << RegName;
246 return;
247 }
248
249 if (Op.isImm()) {
250 O << Op.getImm();
251 return;
252 }
253
254 assert(Op.isExpr() && "unknown operand kind in printOperand");
255 O << *Op.getExpr();
256}
257
Chris Lattner58d014f2010-11-14 21:33:07 +0000258void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
259 raw_ostream &O) {
260 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000261 return printS16ImmOperand(MI, OpNo, O);
262
263 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
264 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
265 if (MI->getOperand(OpNo).isExpr() &&
266 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
267 O << "lo16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000268 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000269 O << ')';
270 } else {
271 printOperand(MI, OpNo, O);
272 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000273}
274
275void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
276 raw_ostream &O) {
277 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000278 return printS16ImmOperand(MI, OpNo, O);
279
280 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
281 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
282 if (MI->getOperand(OpNo).isExpr() &&
283 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
284 O << "ha16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000285 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000286 O << ')';
287 } else {
288 printOperand(MI, OpNo, O);
289 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000290}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000291
292