blob: 7279b091b34ceea70f2e0d66ca95e75bf93cf766 [file] [log] [blame]
Chris Lattnera76eab42010-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
Chris Lattnera76eab42010-11-14 19:40:38 +000014#include "PPCInstPrinter.h"
Hal Finkelfeea6532013-03-26 20:08:20 +000015#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000016#include "MCTargetDesc/PPCPredicates.h"
Chris Lattner7a5c57e2010-11-14 20:02:39 +000017#include "llvm/MC/MCExpr.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000018#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000019#include "llvm/MC/MCInstrInfo.h"
Hal Finkelc6a24392013-11-11 14:58:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000021#include "llvm/Support/raw_ostream.h"
Bill Schmidt8d86fe72013-08-30 15:18:11 +000022#include "llvm/Target/TargetOpcodes.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "asm-printer"
26
Hal Finkelc6a24392013-11-11 14:58:40 +000027// FIXME: Once the integrated assembler supports full register names, tie this
28// to the verbose-asm setting.
29static cl::opt<bool>
30FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
31 cl::desc("Use full register names when printing assembly"));
32
Chris Lattnera76eab42010-11-14 19:40:38 +000033#include "PPCGenAsmWriter.inc"
34
Rafael Espindolad6860522011-06-02 02:34:55 +000035void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
36 OS << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000037}
Chris Lattnera76eab42010-11-14 19:40:38 +000038
Owen Andersona0c3b972011-09-15 23:38:46 +000039void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
40 StringRef Annot) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000041 // Check for slwi/srwi mnemonics.
42 if (MI->getOpcode() == PPC::RLWINM) {
43 unsigned char SH = MI->getOperand(2).getImm();
44 unsigned char MB = MI->getOperand(3).getImm();
45 unsigned char ME = MI->getOperand(4).getImm();
46 bool useSubstituteMnemonic = false;
47 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
48 O << "\tslwi "; useSubstituteMnemonic = true;
49 }
50 if (SH <= 31 && MB == (32-SH) && ME == 31) {
51 O << "\tsrwi "; useSubstituteMnemonic = true;
52 SH = 32-SH;
53 }
54 if (useSubstituteMnemonic) {
55 printOperand(MI, 0, O);
56 O << ", ";
57 printOperand(MI, 1, O);
58 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000059
Owen Andersonbcc3fad2011-09-21 17:58:45 +000060 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000061 return;
62 }
63 }
64
65 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
66 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
67 O << "\tmr ";
68 printOperand(MI, 0, O);
69 O << ", ";
70 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000071 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000072 return;
73 }
74
75 if (MI->getOpcode() == PPC::RLDICR) {
76 unsigned char SH = MI->getOperand(2).getImm();
77 unsigned char ME = MI->getOperand(3).getImm();
78 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
79 if (63-SH == ME) {
80 O << "\tsldi ";
81 printOperand(MI, 0, O);
82 O << ", ";
83 printOperand(MI, 1, O);
84 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +000085 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000086 return;
87 }
88 }
89
Bill Schmidt8d86fe72013-08-30 15:18:11 +000090 // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is
91 // used when converting a 32-bit float to a 64-bit float as part of
92 // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
93 // as otherwise we have problems with incorrect register classes
94 // in machine instruction verification. For now, just avoid trying
95 // to print it as such an instruction has no effect (a 32-bit float
96 // in a register is already in 64-bit form, just with lower
97 // precision). FIXME: Is there a better solution?
98 if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
99 return;
100
Chris Lattnera76eab42010-11-14 19:40:38 +0000101 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000102 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +0000103}
104
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000105
106void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
107 raw_ostream &O,
108 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000109 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +0000110
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000111 if (StringRef(Modifier) == "cc") {
112 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +0000113 case PPC::PRED_LT_MINUS:
114 case PPC::PRED_LT_PLUS:
115 case PPC::PRED_LT:
116 O << "lt";
117 return;
118 case PPC::PRED_LE_MINUS:
119 case PPC::PRED_LE_PLUS:
120 case PPC::PRED_LE:
121 O << "le";
122 return;
123 case PPC::PRED_EQ_MINUS:
124 case PPC::PRED_EQ_PLUS:
125 case PPC::PRED_EQ:
126 O << "eq";
127 return;
128 case PPC::PRED_GE_MINUS:
129 case PPC::PRED_GE_PLUS:
130 case PPC::PRED_GE:
131 O << "ge";
132 return;
133 case PPC::PRED_GT_MINUS:
134 case PPC::PRED_GT_PLUS:
135 case PPC::PRED_GT:
136 O << "gt";
137 return;
138 case PPC::PRED_NE_MINUS:
139 case PPC::PRED_NE_PLUS:
140 case PPC::PRED_NE:
141 O << "ne";
142 return;
143 case PPC::PRED_UN_MINUS:
144 case PPC::PRED_UN_PLUS:
145 case PPC::PRED_UN:
146 O << "un";
147 return;
148 case PPC::PRED_NU_MINUS:
149 case PPC::PRED_NU_PLUS:
150 case PPC::PRED_NU:
151 O << "nu";
152 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000153 case PPC::PRED_BIT_SET:
154 case PPC::PRED_BIT_UNSET:
155 llvm_unreachable("Invalid use of bit predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000156 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000157 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000158 }
159
160 if (StringRef(Modifier) == "pm") {
161 switch ((PPC::Predicate)Code) {
162 case PPC::PRED_LT:
163 case PPC::PRED_LE:
164 case PPC::PRED_EQ:
165 case PPC::PRED_GE:
166 case PPC::PRED_GT:
167 case PPC::PRED_NE:
168 case PPC::PRED_UN:
169 case PPC::PRED_NU:
170 return;
171 case PPC::PRED_LT_MINUS:
172 case PPC::PRED_LE_MINUS:
173 case PPC::PRED_EQ_MINUS:
174 case PPC::PRED_GE_MINUS:
175 case PPC::PRED_GT_MINUS:
176 case PPC::PRED_NE_MINUS:
177 case PPC::PRED_UN_MINUS:
178 case PPC::PRED_NU_MINUS:
179 O << "-";
180 return;
181 case PPC::PRED_LT_PLUS:
182 case PPC::PRED_LE_PLUS:
183 case PPC::PRED_EQ_PLUS:
184 case PPC::PRED_GE_PLUS:
185 case PPC::PRED_GT_PLUS:
186 case PPC::PRED_NE_PLUS:
187 case PPC::PRED_UN_PLUS:
188 case PPC::PRED_NU_PLUS:
189 O << "+";
190 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000191 case PPC::PRED_BIT_SET:
192 case PPC::PRED_BIT_UNSET:
193 llvm_unreachable("Invalid use of bit predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000194 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000195 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000196 }
197
198 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000199 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000200 printOperand(MI, OpNo+1, O);
201}
202
Hal Finkel27774d92014-03-13 07:58:58 +0000203void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
204 raw_ostream &O) {
205 unsigned int Value = MI->getOperand(OpNo).getImm();
206 assert(Value <= 3 && "Invalid u2imm argument!");
207 O << (unsigned int)Value;
208}
209
Chris Lattner94881432010-11-14 20:11:21 +0000210void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
211 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000212 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000213 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000214 O << (int)Value;
215}
216
217void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
218 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000219 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000220 assert(Value <= 31 && "Invalid u5imm argument!");
221 O << (unsigned int)Value;
222}
223
224void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
225 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000226 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000227 assert(Value <= 63 && "Invalid u6imm argument!");
228 O << (unsigned int)Value;
229}
230
231void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
232 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000233 if (MI->getOperand(OpNo).isImm())
234 O << (short)MI->getOperand(OpNo).getImm();
235 else
236 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000237}
238
239void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
240 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000241 if (MI->getOperand(OpNo).isImm())
242 O << (unsigned short)MI->getOperand(OpNo).getImm();
243 else
244 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000245}
246
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000247void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
248 raw_ostream &O) {
249 if (!MI->getOperand(OpNo).isImm())
250 return printOperand(MI, OpNo, O);
251
252 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000253 // selection pass to print .+8, an eight byte displacement from the PC.
254 O << ".+";
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000255 printAbsBranchOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000256}
257
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000258void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
259 raw_ostream &O) {
260 if (!MI->getOperand(OpNo).isImm())
261 return printOperand(MI, OpNo, O);
262
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000263 O << (int)MI->getOperand(OpNo).getImm()*4;
264}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000265
266
Chris Lattner0dcd8002010-11-14 20:22:56 +0000267void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
268 raw_ostream &O) {
269 unsigned CCReg = MI->getOperand(OpNo).getReg();
270 unsigned RegNo;
271 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000272 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000273 case PPC::CR0: RegNo = 0; break;
274 case PPC::CR1: RegNo = 1; break;
275 case PPC::CR2: RegNo = 2; break;
276 case PPC::CR3: RegNo = 3; break;
277 case PPC::CR4: RegNo = 4; break;
278 case PPC::CR5: RegNo = 5; break;
279 case PPC::CR6: RegNo = 6; break;
280 case PPC::CR7: RegNo = 7; break;
281 }
282 O << (0x80 >> RegNo);
283}
284
285void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
286 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000287 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000288 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000289 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000290 O << "0";
291 else
292 printOperand(MI, OpNo+1, O);
293 O << ')';
294}
295
Chris Lattner0dcd8002010-11-14 20:22:56 +0000296void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
297 raw_ostream &O) {
298 // When used as the base register, r0 reads constant zero rather than
299 // the value contained in the register. For this reason, the darwin
300 // assembler requires that we print r0 as 0 (no r) when used as the base.
301 if (MI->getOperand(OpNo).getReg() == PPC::R0)
302 O << "0";
303 else
304 printOperand(MI, OpNo, O);
305 O << ", ";
306 printOperand(MI, OpNo+1, O);
307}
308
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000309void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
310 raw_ostream &O) {
311 printBranchOperand(MI, OpNo, O);
312 O << '(';
313 printOperand(MI, OpNo+1, O);
314 O << ')';
315}
Chris Lattner0dcd8002010-11-14 20:22:56 +0000316
Chris Lattner94881432010-11-14 20:11:21 +0000317
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000318/// stripRegisterPrefix - This method strips the character prefix from a
319/// register name so that only the number is left. Used by for linux asm.
Benjamin Krameraef5bd02010-11-25 16:42:51 +0000320static const char *stripRegisterPrefix(const char *RegName) {
Hal Finkelc6a24392013-11-11 14:58:40 +0000321 if (FullRegNames)
322 return RegName;
323
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000324 switch (RegName[0]) {
325 case 'r':
326 case 'f':
Hal Finkel27774d92014-03-13 07:58:58 +0000327 case 'v':
328 if (RegName[1] == 's')
329 return RegName + 2;
330 return RegName + 1;
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000331 case 'c': if (RegName[1] == 'r') return RegName + 2;
332 }
333
334 return RegName;
335}
336
337void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
338 raw_ostream &O) {
339 const MCOperand &Op = MI->getOperand(OpNo);
340 if (Op.isReg()) {
341 const char *RegName = getRegisterName(Op.getReg());
342 // The linux and AIX assembler does not take register prefixes.
343 if (!isDarwinSyntax())
344 RegName = stripRegisterPrefix(RegName);
345
346 O << RegName;
347 return;
348 }
349
350 if (Op.isImm()) {
351 O << Op.getImm();
352 return;
353 }
354
355 assert(Op.isExpr() && "unknown operand kind in printOperand");
356 O << *Op.getExpr();
357}
Chris Lattnercfb62872010-11-14 21:54:34 +0000358