blob: 5d1aa1ad54088484e2ae1f9a1bfd2fc957540998 [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 Finkel7c8ae532014-07-25 17:47:22 +000020#include "llvm/MC/MCSymbol.h"
Hal Finkelc6a24392013-11-11 14:58:40 +000021#include "llvm/Support/CommandLine.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000022#include "llvm/Support/raw_ostream.h"
Bill Schmidt8d86fe72013-08-30 15:18:11 +000023#include "llvm/Target/TargetOpcodes.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "asm-printer"
27
Hal Finkelc6a24392013-11-11 14:58:40 +000028// FIXME: Once the integrated assembler supports full register names, tie this
29// to the verbose-asm setting.
30static cl::opt<bool>
31FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
32 cl::desc("Use full register names when printing assembly"));
33
Chris Lattnera76eab42010-11-14 19:40:38 +000034#include "PPCGenAsmWriter.inc"
35
Rafael Espindolad6860522011-06-02 02:34:55 +000036void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000037 const char *RegName = getRegisterName(RegNo);
38 if (RegName[0] == 'q' /* QPX */) {
39 // The system toolchain on the BG/Q does not understand QPX register names
40 // in .cfi_* directives, so print the name of the floating-point
41 // subregister instead.
42 std::string RN(RegName);
43
44 RN[0] = 'f';
45 OS << RN;
46
47 return;
48 }
49
50 OS << RegName;
Rafael Espindola08600bc2011-05-30 20:20:15 +000051}
Chris Lattnera76eab42010-11-14 19:40:38 +000052
Owen Andersona0c3b972011-09-15 23:38:46 +000053void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
54 StringRef Annot) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000055 // Check for slwi/srwi mnemonics.
56 if (MI->getOpcode() == PPC::RLWINM) {
57 unsigned char SH = MI->getOperand(2).getImm();
58 unsigned char MB = MI->getOperand(3).getImm();
59 unsigned char ME = MI->getOperand(4).getImm();
60 bool useSubstituteMnemonic = false;
61 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
62 O << "\tslwi "; useSubstituteMnemonic = true;
63 }
64 if (SH <= 31 && MB == (32-SH) && ME == 31) {
65 O << "\tsrwi "; useSubstituteMnemonic = true;
66 SH = 32-SH;
67 }
68 if (useSubstituteMnemonic) {
69 printOperand(MI, 0, O);
70 O << ", ";
71 printOperand(MI, 1, O);
72 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000073
Owen Andersonbcc3fad2011-09-21 17:58:45 +000074 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000075 return;
76 }
77 }
78
79 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
80 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
81 O << "\tmr ";
82 printOperand(MI, 0, O);
83 O << ", ";
84 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000085 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000086 return;
87 }
88
89 if (MI->getOpcode() == PPC::RLDICR) {
90 unsigned char SH = MI->getOperand(2).getImm();
91 unsigned char ME = MI->getOperand(3).getImm();
92 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
93 if (63-SH == ME) {
94 O << "\tsldi ";
95 printOperand(MI, 0, O);
96 O << ", ";
97 printOperand(MI, 1, O);
98 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +000099 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000100 return;
101 }
102 }
103
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000104 // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is
105 // used when converting a 32-bit float to a 64-bit float as part of
106 // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
107 // as otherwise we have problems with incorrect register classes
108 // in machine instruction verification. For now, just avoid trying
109 // to print it as such an instruction has no effect (a 32-bit float
110 // in a register is already in 64-bit form, just with lower
111 // precision). FIXME: Is there a better solution?
112 if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
113 return;
114
Chris Lattnera76eab42010-11-14 19:40:38 +0000115 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000116 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +0000117}
118
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000119
120void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
121 raw_ostream &O,
122 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000123 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +0000124
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000125 if (StringRef(Modifier) == "cc") {
126 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +0000127 case PPC::PRED_LT_MINUS:
128 case PPC::PRED_LT_PLUS:
129 case PPC::PRED_LT:
130 O << "lt";
131 return;
132 case PPC::PRED_LE_MINUS:
133 case PPC::PRED_LE_PLUS:
134 case PPC::PRED_LE:
135 O << "le";
136 return;
137 case PPC::PRED_EQ_MINUS:
138 case PPC::PRED_EQ_PLUS:
139 case PPC::PRED_EQ:
140 O << "eq";
141 return;
142 case PPC::PRED_GE_MINUS:
143 case PPC::PRED_GE_PLUS:
144 case PPC::PRED_GE:
145 O << "ge";
146 return;
147 case PPC::PRED_GT_MINUS:
148 case PPC::PRED_GT_PLUS:
149 case PPC::PRED_GT:
150 O << "gt";
151 return;
152 case PPC::PRED_NE_MINUS:
153 case PPC::PRED_NE_PLUS:
154 case PPC::PRED_NE:
155 O << "ne";
156 return;
157 case PPC::PRED_UN_MINUS:
158 case PPC::PRED_UN_PLUS:
159 case PPC::PRED_UN:
160 O << "un";
161 return;
162 case PPC::PRED_NU_MINUS:
163 case PPC::PRED_NU_PLUS:
164 case PPC::PRED_NU:
165 O << "nu";
166 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000167 case PPC::PRED_BIT_SET:
168 case PPC::PRED_BIT_UNSET:
169 llvm_unreachable("Invalid use of bit predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000170 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000171 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000172 }
173
174 if (StringRef(Modifier) == "pm") {
175 switch ((PPC::Predicate)Code) {
176 case PPC::PRED_LT:
177 case PPC::PRED_LE:
178 case PPC::PRED_EQ:
179 case PPC::PRED_GE:
180 case PPC::PRED_GT:
181 case PPC::PRED_NE:
182 case PPC::PRED_UN:
183 case PPC::PRED_NU:
184 return;
185 case PPC::PRED_LT_MINUS:
186 case PPC::PRED_LE_MINUS:
187 case PPC::PRED_EQ_MINUS:
188 case PPC::PRED_GE_MINUS:
189 case PPC::PRED_GT_MINUS:
190 case PPC::PRED_NE_MINUS:
191 case PPC::PRED_UN_MINUS:
192 case PPC::PRED_NU_MINUS:
193 O << "-";
194 return;
195 case PPC::PRED_LT_PLUS:
196 case PPC::PRED_LE_PLUS:
197 case PPC::PRED_EQ_PLUS:
198 case PPC::PRED_GE_PLUS:
199 case PPC::PRED_GT_PLUS:
200 case PPC::PRED_NE_PLUS:
201 case PPC::PRED_UN_PLUS:
202 case PPC::PRED_NU_PLUS:
203 O << "+";
204 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000205 case PPC::PRED_BIT_SET:
206 case PPC::PRED_BIT_UNSET:
207 llvm_unreachable("Invalid use of bit predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000208 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000209 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000210 }
211
212 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000213 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000214 printOperand(MI, OpNo+1, O);
215}
216
Nemanja Ivanovice8effe12015-03-04 20:44:33 +0000217void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
218 raw_ostream &O) {
219 unsigned int Value = MI->getOperand(OpNo).getImm();
220 assert(Value <= 1 && "Invalid u1imm argument!");
221 O << (unsigned int)Value;
222}
223
Hal Finkel27774d92014-03-13 07:58:58 +0000224void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
225 raw_ostream &O) {
226 unsigned int Value = MI->getOperand(OpNo).getImm();
227 assert(Value <= 3 && "Invalid u2imm argument!");
228 O << (unsigned int)Value;
229}
230
Joerg Sonnenberger9e9623c2014-07-29 22:21:57 +0000231void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
232 raw_ostream &O) {
233 unsigned int Value = MI->getOperand(OpNo).getImm();
234 assert(Value <= 15 && "Invalid u4imm argument!");
235 O << (unsigned int)Value;
236}
237
Chris Lattner94881432010-11-14 20:11:21 +0000238void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
239 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000240 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000241 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000242 O << (int)Value;
243}
244
245void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
246 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000247 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000248 assert(Value <= 31 && "Invalid u5imm argument!");
249 O << (unsigned int)Value;
250}
251
252void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
253 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000254 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000255 assert(Value <= 63 && "Invalid u6imm argument!");
256 O << (unsigned int)Value;
257}
258
Hal Finkelc93a9a22015-02-25 01:06:45 +0000259void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
260 raw_ostream &O) {
261 unsigned short Value = MI->getOperand(OpNo).getImm();
262 assert(Value <= 4095 && "Invalid u12imm argument!");
263 O << (unsigned short)Value;
264}
265
Chris Lattner94881432010-11-14 20:11:21 +0000266void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
267 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000268 if (MI->getOperand(OpNo).isImm())
269 O << (short)MI->getOperand(OpNo).getImm();
270 else
271 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000272}
273
274void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
275 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000276 if (MI->getOperand(OpNo).isImm())
277 O << (unsigned short)MI->getOperand(OpNo).getImm();
278 else
279 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000280}
281
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000282void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
283 raw_ostream &O) {
284 if (!MI->getOperand(OpNo).isImm())
285 return printOperand(MI, OpNo, O);
286
287 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000288 // selection pass to print .+8, an eight byte displacement from the PC.
289 O << ".+";
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000290 printAbsBranchOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000291}
292
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000293void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
294 raw_ostream &O) {
295 if (!MI->getOperand(OpNo).isImm())
296 return printOperand(MI, OpNo, O);
297
Alexey Samsonov9ca48702014-09-02 17:38:34 +0000298 O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000299}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000300
301
Chris Lattner0dcd8002010-11-14 20:22:56 +0000302void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
303 raw_ostream &O) {
304 unsigned CCReg = MI->getOperand(OpNo).getReg();
305 unsigned RegNo;
306 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000307 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000308 case PPC::CR0: RegNo = 0; break;
309 case PPC::CR1: RegNo = 1; break;
310 case PPC::CR2: RegNo = 2; break;
311 case PPC::CR3: RegNo = 3; break;
312 case PPC::CR4: RegNo = 4; break;
313 case PPC::CR5: RegNo = 5; break;
314 case PPC::CR6: RegNo = 6; break;
315 case PPC::CR7: RegNo = 7; break;
316 }
317 O << (0x80 >> RegNo);
318}
319
320void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
321 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000322 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000323 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000324 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000325 O << "0";
326 else
327 printOperand(MI, OpNo+1, O);
328 O << ')';
329}
330
Chris Lattner0dcd8002010-11-14 20:22:56 +0000331void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
332 raw_ostream &O) {
333 // When used as the base register, r0 reads constant zero rather than
334 // the value contained in the register. For this reason, the darwin
335 // assembler requires that we print r0 as 0 (no r) when used as the base.
336 if (MI->getOperand(OpNo).getReg() == PPC::R0)
337 O << "0";
338 else
339 printOperand(MI, OpNo, O);
340 O << ", ";
341 printOperand(MI, OpNo+1, O);
342}
343
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000344void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
345 raw_ostream &O) {
Hal Finkel7c8ae532014-07-25 17:47:22 +0000346 // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
347 // come at the _end_ of the expression.
348 const MCOperand &Op = MI->getOperand(OpNo);
349 const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
350 O << refExp.getSymbol().getName();
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000351 O << '(';
352 printOperand(MI, OpNo+1, O);
353 O << ')';
Hal Finkel7c8ae532014-07-25 17:47:22 +0000354 if (refExp.getKind() != MCSymbolRefExpr::VK_None)
355 O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000356}
Chris Lattner0dcd8002010-11-14 20:22:56 +0000357
Chris Lattner94881432010-11-14 20:11:21 +0000358
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000359/// stripRegisterPrefix - This method strips the character prefix from a
360/// register name so that only the number is left. Used by for linux asm.
Benjamin Krameraef5bd02010-11-25 16:42:51 +0000361static const char *stripRegisterPrefix(const char *RegName) {
Hal Finkelc6a24392013-11-11 14:58:40 +0000362 if (FullRegNames)
363 return RegName;
364
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000365 switch (RegName[0]) {
366 case 'r':
367 case 'f':
Hal Finkelc93a9a22015-02-25 01:06:45 +0000368 case 'q': // for QPX
Hal Finkel27774d92014-03-13 07:58:58 +0000369 case 'v':
370 if (RegName[1] == 's')
371 return RegName + 2;
372 return RegName + 1;
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000373 case 'c': if (RegName[1] == 'r') return RegName + 2;
374 }
375
376 return RegName;
377}
378
379void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
380 raw_ostream &O) {
381 const MCOperand &Op = MI->getOperand(OpNo);
382 if (Op.isReg()) {
383 const char *RegName = getRegisterName(Op.getReg());
384 // The linux and AIX assembler does not take register prefixes.
385 if (!isDarwinSyntax())
386 RegName = stripRegisterPrefix(RegName);
387
388 O << RegName;
389 return;
390 }
391
392 if (Op.isImm()) {
393 O << Op.getImm();
394 return;
395 }
396
397 assert(Op.isExpr() && "unknown operand kind in printOperand");
398 O << *Op.getExpr();
399}
Chris Lattnercfb62872010-11-14 21:54:34 +0000400