blob: 1949f21402b8418ef0d6e4ea596ebc304b175b02 [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"
Craig Topper7c0b3c12012-04-02 07:01:04 +000020#include "llvm/MC/MCInstrInfo.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000022using namespace llvm;
23
Chris Lattner60d5b5f2010-11-14 19:40:38 +000024#include "PPCGenAsmWriter.inc"
25
Rafael Espindolacde4ce42011-06-02 02:34:55 +000026void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
27 OS << getRegisterName(RegNo);
Rafael Espindola6e032942011-05-30 20:20:15 +000028}
Chris Lattner60d5b5f2010-11-14 19:40:38 +000029
Owen Anderson98c5dda2011-09-15 23:38:46 +000030void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
31 StringRef Annot) {
Chris Lattner2e352482010-11-14 21:39:51 +000032 // 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;
Owen Anderson98c5dda2011-09-15 23:38:46 +000050
Owen Anderson519020a2011-09-21 17:58:45 +000051 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000052 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);
Owen Anderson519020a2011-09-21 17:58:45 +000062 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000063 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;
Owen Anderson519020a2011-09-21 17:58:45 +000076 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000077 return;
78 }
79 }
80
Chris Lattner60d5b5f2010-11-14 19:40:38 +000081 printInstruction(MI, O);
Owen Anderson519020a2011-09-21 17:58:45 +000082 printAnnotation(O, Annot);
Chris Lattner60d5b5f2010-11-14 19:40:38 +000083}
84
Chris Lattnerb2e477f2010-11-14 21:51:37 +000085
86void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
87 raw_ostream &O,
88 const char *Modifier) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +000089 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel009f7af2012-06-22 23:10:08 +000090
Chris Lattnerb2e477f2010-11-14 21:51:37 +000091 if (StringRef(Modifier) == "cc") {
92 switch ((PPC::Predicate)Code) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +000093 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!");
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000106 printOperand(MI, OpNo+1, O);
107}
108
Chris Lattner99889132010-11-14 20:11:21 +0000109void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
110 raw_ostream &O) {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000111 int Value = MI->getOperand(OpNo).getImm();
Richard Smith1144af32012-08-24 23:29:28 +0000112 Value = SignExtend32<5>(Value);
Chris Lattner99889132010-11-14 20:11:21 +0000113 O << (int)Value;
114}
115
116void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
117 raw_ostream &O) {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000118 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner99889132010-11-14 20:11:21 +0000119 assert(Value <= 31 && "Invalid u5imm argument!");
120 O << (unsigned int)Value;
121}
122
123void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
124 raw_ostream &O) {
Adhemerval Zanella1c7d69b2012-10-08 18:59:53 +0000125 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner99889132010-11-14 20:11:21 +0000126 assert(Value <= 63 && "Invalid u6imm argument!");
127 O << (unsigned int)Value;
128}
129
130void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
131 raw_ostream &O) {
132 O << (short)MI->getOperand(OpNo).getImm();
133}
134
135void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
136 raw_ostream &O) {
137 O << (unsigned short)MI->getOperand(OpNo).getImm();
138}
139
140void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
141 raw_ostream &O) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000142 if (MI->getOperand(OpNo).isImm())
Chris Lattner99889132010-11-14 20:11:21 +0000143 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner99889132010-11-14 20:11:21 +0000144 else
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000145 printOperand(MI, OpNo, O);
Chris Lattner99889132010-11-14 20:11:21 +0000146}
147
Chris Lattner1520fd62010-11-14 21:20:46 +0000148void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
149 raw_ostream &O) {
150 if (!MI->getOperand(OpNo).isImm())
151 return printOperand(MI, OpNo, O);
152
153 // Branches can take an immediate operand. This is used by the branch
154 // selection pass to print $+8, an eight byte displacement from the PC.
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000155 O << "$+";
156 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner1520fd62010-11-14 21:20:46 +0000157}
158
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000159void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
160 raw_ostream &O) {
161 O << (int)MI->getOperand(OpNo).getImm()*4;
162}
Chris Lattner1520fd62010-11-14 21:20:46 +0000163
164
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000165void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
166 raw_ostream &O) {
167 unsigned CCReg = MI->getOperand(OpNo).getReg();
168 unsigned RegNo;
169 switch (CCReg) {
Craig Topperbc219812012-02-07 02:50:20 +0000170 default: llvm_unreachable("Unknown CR register");
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000171 case PPC::CR0: RegNo = 0; break;
172 case PPC::CR1: RegNo = 1; break;
173 case PPC::CR2: RegNo = 2; break;
174 case PPC::CR3: RegNo = 3; break;
175 case PPC::CR4: RegNo = 4; break;
176 case PPC::CR5: RegNo = 5; break;
177 case PPC::CR6: RegNo = 6; break;
178 case PPC::CR7: RegNo = 7; break;
179 }
180 O << (0x80 >> RegNo);
181}
182
183void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
184 raw_ostream &O) {
185 printSymbolLo(MI, OpNo, O);
186 O << '(';
Chris Lattner0fe71842010-11-15 03:51:13 +0000187 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000188 O << "0";
189 else
190 printOperand(MI, OpNo+1, O);
191 O << ')';
192}
193
194void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
195 raw_ostream &O) {
196 if (MI->getOperand(OpNo).isImm())
197 printS16X4ImmOperand(MI, OpNo, O);
198 else
199 printSymbolLo(MI, OpNo, O);
200 O << '(';
201
Chris Lattner0fe71842010-11-15 03:51:13 +0000202 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000203 O << "0";
204 else
205 printOperand(MI, OpNo+1, O);
206 O << ')';
207}
208
209
210void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
211 raw_ostream &O) {
212 // When used as the base register, r0 reads constant zero rather than
213 // the value contained in the register. For this reason, the darwin
214 // assembler requires that we print r0 as 0 (no r) when used as the base.
215 if (MI->getOperand(OpNo).getReg() == PPC::R0)
216 O << "0";
217 else
218 printOperand(MI, OpNo, O);
219 O << ", ";
220 printOperand(MI, OpNo+1, O);
221}
222
223
Chris Lattner99889132010-11-14 20:11:21 +0000224
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000225/// stripRegisterPrefix - This method strips the character prefix from a
226/// register name so that only the number is left. Used by for linux asm.
Benjamin Kramerc62feda2010-11-25 16:42:51 +0000227static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000228 switch (RegName[0]) {
229 case 'r':
230 case 'f':
231 case 'v': return RegName + 1;
232 case 'c': if (RegName[1] == 'r') return RegName + 2;
233 }
234
235 return RegName;
236}
237
238void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
239 raw_ostream &O) {
240 const MCOperand &Op = MI->getOperand(OpNo);
241 if (Op.isReg()) {
242 const char *RegName = getRegisterName(Op.getReg());
243 // The linux and AIX assembler does not take register prefixes.
244 if (!isDarwinSyntax())
245 RegName = stripRegisterPrefix(RegName);
246
247 O << RegName;
248 return;
249 }
250
251 if (Op.isImm()) {
252 O << Op.getImm();
253 return;
254 }
255
256 assert(Op.isExpr() && "unknown operand kind in printOperand");
257 O << *Op.getExpr();
258}
259
Chris Lattner58d014f2010-11-14 21:33:07 +0000260void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
261 raw_ostream &O) {
262 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000263 return printS16ImmOperand(MI, OpNo, O);
264
265 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
266 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
267 if (MI->getOperand(OpNo).isExpr() &&
268 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
269 O << "lo16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000270 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000271 O << ')';
272 } else {
273 printOperand(MI, OpNo, O);
274 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000275}
276
277void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
278 raw_ostream &O) {
279 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000280 return printS16ImmOperand(MI, OpNo, O);
281
282 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
283 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
284 if (MI->getOperand(OpNo).isExpr() &&
285 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
286 O << "ha16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000287 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000288 O << ')';
289 } else {
290 printOperand(MI, OpNo, O);
291 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000292}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000293
294