blob: 6e85ab926cdb93537d2caa9641775f51c63d9499 [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
26StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
Craig Topper7c0b3c12012-04-02 07:01:04 +000027 return MII.getName(Opcode);
Chris Lattner60d5b5f2010-11-14 19:40:38 +000028}
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
Owen Anderson98c5dda2011-09-15 23:38:46 +000034void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
35 StringRef Annot) {
Chris Lattner2e352482010-11-14 21:39:51 +000036 // Check for slwi/srwi mnemonics.
37 if (MI->getOpcode() == PPC::RLWINM) {
38 unsigned char SH = MI->getOperand(2).getImm();
39 unsigned char MB = MI->getOperand(3).getImm();
40 unsigned char ME = MI->getOperand(4).getImm();
41 bool useSubstituteMnemonic = false;
42 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
43 O << "\tslwi "; useSubstituteMnemonic = true;
44 }
45 if (SH <= 31 && MB == (32-SH) && ME == 31) {
46 O << "\tsrwi "; useSubstituteMnemonic = true;
47 SH = 32-SH;
48 }
49 if (useSubstituteMnemonic) {
50 printOperand(MI, 0, O);
51 O << ", ";
52 printOperand(MI, 1, O);
53 O << ", " << (unsigned int)SH;
Owen Anderson98c5dda2011-09-15 23:38:46 +000054
Owen Anderson519020a2011-09-21 17:58:45 +000055 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000056 return;
57 }
58 }
59
60 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
61 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
62 O << "\tmr ";
63 printOperand(MI, 0, O);
64 O << ", ";
65 printOperand(MI, 1, O);
Owen Anderson519020a2011-09-21 17:58:45 +000066 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000067 return;
68 }
69
70 if (MI->getOpcode() == PPC::RLDICR) {
71 unsigned char SH = MI->getOperand(2).getImm();
72 unsigned char ME = MI->getOperand(3).getImm();
73 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
74 if (63-SH == ME) {
75 O << "\tsldi ";
76 printOperand(MI, 0, O);
77 O << ", ";
78 printOperand(MI, 1, O);
79 O << ", " << (unsigned int)SH;
Owen Anderson519020a2011-09-21 17:58:45 +000080 printAnnotation(O, Annot);
Chris Lattner2e352482010-11-14 21:39:51 +000081 return;
82 }
83 }
84
Chris Lattner60d5b5f2010-11-14 19:40:38 +000085 printInstruction(MI, O);
Owen Anderson519020a2011-09-21 17:58:45 +000086 printAnnotation(O, Annot);
Chris Lattner60d5b5f2010-11-14 19:40:38 +000087}
88
Chris Lattnerb2e477f2010-11-14 21:51:37 +000089
90void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
91 raw_ostream &O,
92 const char *Modifier) {
93 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
94 unsigned Code = MI->getOperand(OpNo).getImm();
95 if (StringRef(Modifier) == "cc") {
96 switch ((PPC::Predicate)Code) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +000097 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
98 case PPC::PRED_LT: O << "lt"; return;
99 case PPC::PRED_LE: O << "le"; return;
100 case PPC::PRED_EQ: O << "eq"; return;
101 case PPC::PRED_GE: O << "ge"; return;
102 case PPC::PRED_GT: O << "gt"; return;
103 case PPC::PRED_NE: O << "ne"; return;
104 case PPC::PRED_UN: O << "un"; return;
105 case PPC::PRED_NU: O << "nu"; return;
106 }
107 }
108
109 assert(StringRef(Modifier) == "reg" &&
110 "Need to specify 'cc' or 'reg' as predicate op modifier!");
111 // Don't print the register for 'always'.
112 if (Code == PPC::PRED_ALWAYS) return;
113 printOperand(MI, OpNo+1, O);
114}
115
Chris Lattner99889132010-11-14 20:11:21 +0000116void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
117 raw_ostream &O) {
118 char Value = MI->getOperand(OpNo).getImm();
119 Value = (Value << (32-5)) >> (32-5);
120 O << (int)Value;
121}
122
123void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
124 raw_ostream &O) {
125 unsigned char Value = MI->getOperand(OpNo).getImm();
126 assert(Value <= 31 && "Invalid u5imm argument!");
127 O << (unsigned int)Value;
128}
129
130void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
131 raw_ostream &O) {
132 unsigned char Value = MI->getOperand(OpNo).getImm();
133 assert(Value <= 63 && "Invalid u6imm argument!");
134 O << (unsigned int)Value;
135}
136
137void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
138 raw_ostream &O) {
139 O << (short)MI->getOperand(OpNo).getImm();
140}
141
142void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
143 raw_ostream &O) {
144 O << (unsigned short)MI->getOperand(OpNo).getImm();
145}
146
147void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
148 raw_ostream &O) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000149 if (MI->getOperand(OpNo).isImm())
Chris Lattner99889132010-11-14 20:11:21 +0000150 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner99889132010-11-14 20:11:21 +0000151 else
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000152 printOperand(MI, OpNo, O);
Chris Lattner99889132010-11-14 20:11:21 +0000153}
154
Chris Lattner1520fd62010-11-14 21:20:46 +0000155void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
156 raw_ostream &O) {
157 if (!MI->getOperand(OpNo).isImm())
158 return printOperand(MI, OpNo, O);
159
160 // Branches can take an immediate operand. This is used by the branch
161 // selection pass to print $+8, an eight byte displacement from the PC.
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000162 O << "$+";
163 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner1520fd62010-11-14 21:20:46 +0000164}
165
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000166void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
167 raw_ostream &O) {
168 O << (int)MI->getOperand(OpNo).getImm()*4;
169}
Chris Lattner1520fd62010-11-14 21:20:46 +0000170
171
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000172void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
173 raw_ostream &O) {
174 unsigned CCReg = MI->getOperand(OpNo).getReg();
175 unsigned RegNo;
176 switch (CCReg) {
Craig Topperbc219812012-02-07 02:50:20 +0000177 default: llvm_unreachable("Unknown CR register");
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000178 case PPC::CR0: RegNo = 0; break;
179 case PPC::CR1: RegNo = 1; break;
180 case PPC::CR2: RegNo = 2; break;
181 case PPC::CR3: RegNo = 3; break;
182 case PPC::CR4: RegNo = 4; break;
183 case PPC::CR5: RegNo = 5; break;
184 case PPC::CR6: RegNo = 6; break;
185 case PPC::CR7: RegNo = 7; break;
186 }
187 O << (0x80 >> RegNo);
188}
189
190void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
191 raw_ostream &O) {
192 printSymbolLo(MI, OpNo, O);
193 O << '(';
Chris Lattner0fe71842010-11-15 03:51:13 +0000194 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000195 O << "0";
196 else
197 printOperand(MI, OpNo+1, O);
198 O << ')';
199}
200
201void 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
Chris Lattner0fe71842010-11-15 03:51:13 +0000209 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000210 O << "0";
211 else
212 printOperand(MI, OpNo+1, O);
213 O << ')';
214}
215
216
217void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
218 raw_ostream &O) {
219 // When used as the base register, r0 reads constant zero rather than
220 // the value contained in the register. For this reason, the darwin
221 // assembler requires that we print r0 as 0 (no r) when used as the base.
222 if (MI->getOperand(OpNo).getReg() == PPC::R0)
223 O << "0";
224 else
225 printOperand(MI, OpNo, O);
226 O << ", ";
227 printOperand(MI, OpNo+1, O);
228}
229
230
Chris Lattner99889132010-11-14 20:11:21 +0000231
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000232/// stripRegisterPrefix - This method strips the character prefix from a
233/// register name so that only the number is left. Used by for linux asm.
Benjamin Kramerc62feda2010-11-25 16:42:51 +0000234static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000235 switch (RegName[0]) {
236 case 'r':
237 case 'f':
238 case 'v': return RegName + 1;
239 case 'c': if (RegName[1] == 'r') return RegName + 2;
240 }
241
242 return RegName;
243}
244
245void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
246 raw_ostream &O) {
247 const MCOperand &Op = MI->getOperand(OpNo);
248 if (Op.isReg()) {
249 const char *RegName = getRegisterName(Op.getReg());
250 // The linux and AIX assembler does not take register prefixes.
251 if (!isDarwinSyntax())
252 RegName = stripRegisterPrefix(RegName);
253
254 O << RegName;
255 return;
256 }
257
258 if (Op.isImm()) {
259 O << Op.getImm();
260 return;
261 }
262
263 assert(Op.isExpr() && "unknown operand kind in printOperand");
264 O << *Op.getExpr();
265}
266
Chris Lattner58d014f2010-11-14 21:33:07 +0000267void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
268 raw_ostream &O) {
269 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000270 return printS16ImmOperand(MI, OpNo, O);
271
272 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
273 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
274 if (MI->getOperand(OpNo).isExpr() &&
275 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
276 O << "lo16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000277 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000278 O << ')';
279 } else {
280 printOperand(MI, OpNo, O);
281 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000282}
283
284void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
285 raw_ostream &O) {
286 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000287 return printS16ImmOperand(MI, OpNo, O);
288
289 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
290 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
291 if (MI->getOperand(OpNo).isExpr() &&
292 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
293 O << "ha16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000294 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000295 O << ')';
296 } else {
297 printOperand(MI, OpNo, O);
298 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000299}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000300
301