blob: fd7f81591426ae7b69c7922db792567cc9bcc8ef [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"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "PPCInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000018#include "llvm/CodeGen/TargetOpcodes.h"
Chris Lattner7a5c57e2010-11-14 20:02:39 +000019#include "llvm/MC/MCExpr.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000020#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000021#include "llvm/MC/MCInstrInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000022#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
Hal Finkel7c8ae532014-07-25 17:47:22 +000024#include "llvm/MC/MCSymbol.h"
Hal Finkelc6a24392013-11-11 14:58:40 +000025#include "llvm/Support/CommandLine.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "asm-printer"
30
Hal Finkelc6a24392013-11-11 14:58:40 +000031// FIXME: Once the integrated assembler supports full register names, tie this
32// to the verbose-asm setting.
33static cl::opt<bool>
34FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
35 cl::desc("Use full register names when printing assembly"));
36
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000037// Useful for testing purposes. Prints vs{31-63} as v{0-31} respectively.
38static cl::opt<bool>
39ShowVSRNumsAsVR("ppc-vsr-nums-as-vr", cl::Hidden, cl::init(false),
40 cl::desc("Prints full register names with vs{31-63} as v{0-31}"));
41
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +000042// Prints full register names with percent symbol.
43static cl::opt<bool>
44FullRegNamesWithPercent("ppc-reg-with-percent-prefix", cl::Hidden,
45 cl::init(false),
46 cl::desc("Prints full register names with percent"));
47
Hal Finkel7c5cb062015-04-23 18:30:38 +000048#define PRINT_ALIAS_INSTR
Chris Lattnera76eab42010-11-14 19:40:38 +000049#include "PPCGenAsmWriter.inc"
50
Rafael Espindolad6860522011-06-02 02:34:55 +000051void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000052 const char *RegName = getRegisterName(RegNo);
53 if (RegName[0] == 'q' /* QPX */) {
54 // The system toolchain on the BG/Q does not understand QPX register names
55 // in .cfi_* directives, so print the name of the floating-point
56 // subregister instead.
57 std::string RN(RegName);
58
59 RN[0] = 'f';
60 OS << RN;
61
62 return;
63 }
64
65 OS << RegName;
Rafael Espindola08600bc2011-05-30 20:20:15 +000066}
Chris Lattnera76eab42010-11-14 19:40:38 +000067
Owen Andersona0c3b972011-09-15 23:38:46 +000068void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
Akira Hatanakab46d0232015-03-27 20:36:02 +000069 StringRef Annot, const MCSubtargetInfo &STI) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000070 // Check for slwi/srwi mnemonics.
71 if (MI->getOpcode() == PPC::RLWINM) {
72 unsigned char SH = MI->getOperand(2).getImm();
73 unsigned char MB = MI->getOperand(3).getImm();
74 unsigned char ME = MI->getOperand(4).getImm();
75 bool useSubstituteMnemonic = false;
76 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
77 O << "\tslwi "; useSubstituteMnemonic = true;
78 }
79 if (SH <= 31 && MB == (32-SH) && ME == 31) {
80 O << "\tsrwi "; useSubstituteMnemonic = true;
81 SH = 32-SH;
82 }
83 if (useSubstituteMnemonic) {
84 printOperand(MI, 0, O);
85 O << ", ";
86 printOperand(MI, 1, O);
87 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000088
Owen Andersonbcc3fad2011-09-21 17:58:45 +000089 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000090 return;
91 }
92 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +000093
Chris Lattner219cc3d2010-11-14 21:39:51 +000094 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
95 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
96 O << "\tmr ";
97 printOperand(MI, 0, O);
98 O << ", ";
99 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000100 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000101 return;
102 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000103
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000104 if (MI->getOpcode() == PPC::RLDICR ||
105 MI->getOpcode() == PPC::RLDICR_32) {
Chris Lattner219cc3d2010-11-14 21:39:51 +0000106 unsigned char SH = MI->getOperand(2).getImm();
107 unsigned char ME = MI->getOperand(3).getImm();
108 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
109 if (63-SH == ME) {
110 O << "\tsldi ";
111 printOperand(MI, 0, O);
112 O << ", ";
113 printOperand(MI, 1, O);
114 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000115 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000116 return;
117 }
118 }
Hal Finkelfefcfff2015-04-23 22:47:57 +0000119
120 // dcbt[st] is printed manually here because:
121 // 1. The assembly syntax is different between embedded and server targets
122 // 2. We must print the short mnemonics for TH == 0 because the
123 // embedded/server syntax default will not be stable across assemblers
124 // The syntax for dcbt is:
125 // dcbt ra, rb, th [server]
126 // dcbt th, ra, rb [embedded]
127 // where th can be omitted when it is 0. dcbtst is the same.
128 if (MI->getOpcode() == PPC::DCBT || MI->getOpcode() == PPC::DCBTST) {
129 unsigned char TH = MI->getOperand(0).getImm();
130 O << "\tdcbt";
131 if (MI->getOpcode() == PPC::DCBTST)
132 O << "st";
133 if (TH == 16)
134 O << "t";
135 O << " ";
136
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000137 bool IsBookE = STI.getFeatureBits()[PPC::FeatureBookE];
Hal Finkelfefcfff2015-04-23 22:47:57 +0000138 if (IsBookE && TH != 0 && TH != 16)
139 O << (unsigned int) TH << ", ";
140
141 printOperand(MI, 1, O);
142 O << ", ";
143 printOperand(MI, 2, O);
144
145 if (!IsBookE && TH != 0 && TH != 16)
146 O << ", " << (unsigned int) TH;
147
148 printAnnotation(O, Annot);
149 return;
150 }
Hal Finkel277736e2016-09-02 23:41:54 +0000151
152 if (MI->getOpcode() == PPC::DCBF) {
153 unsigned char L = MI->getOperand(0).getImm();
154 if (!L || L == 1 || L == 3) {
155 O << "\tdcbf";
156 if (L == 1 || L == 3)
157 O << "l";
158 if (L == 3)
159 O << "p";
160 O << " ";
161
162 printOperand(MI, 1, O);
163 O << ", ";
164 printOperand(MI, 2, O);
165
166 printAnnotation(O, Annot);
167 return;
168 }
169 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000170
Hal Finkel7c5cb062015-04-23 18:30:38 +0000171 if (!printAliasInstr(MI, O))
172 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000173 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +0000174}
175
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000176
177void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
Fangrui Songf78650a2018-07-30 19:41:25 +0000178 raw_ostream &O,
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000179 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000180 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +0000181
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000182 if (StringRef(Modifier) == "cc") {
183 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +0000184 case PPC::PRED_LT_MINUS:
185 case PPC::PRED_LT_PLUS:
186 case PPC::PRED_LT:
187 O << "lt";
188 return;
189 case PPC::PRED_LE_MINUS:
190 case PPC::PRED_LE_PLUS:
191 case PPC::PRED_LE:
192 O << "le";
193 return;
194 case PPC::PRED_EQ_MINUS:
195 case PPC::PRED_EQ_PLUS:
196 case PPC::PRED_EQ:
197 O << "eq";
198 return;
199 case PPC::PRED_GE_MINUS:
200 case PPC::PRED_GE_PLUS:
201 case PPC::PRED_GE:
202 O << "ge";
203 return;
204 case PPC::PRED_GT_MINUS:
205 case PPC::PRED_GT_PLUS:
206 case PPC::PRED_GT:
207 O << "gt";
208 return;
209 case PPC::PRED_NE_MINUS:
210 case PPC::PRED_NE_PLUS:
211 case PPC::PRED_NE:
212 O << "ne";
213 return;
214 case PPC::PRED_UN_MINUS:
215 case PPC::PRED_UN_PLUS:
216 case PPC::PRED_UN:
217 O << "un";
218 return;
219 case PPC::PRED_NU_MINUS:
220 case PPC::PRED_NU_PLUS:
221 case PPC::PRED_NU:
222 O << "nu";
223 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000224 case PPC::PRED_BIT_SET:
225 case PPC::PRED_BIT_UNSET:
226 llvm_unreachable("Invalid use of bit predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000227 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000228 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000229 }
230
231 if (StringRef(Modifier) == "pm") {
232 switch ((PPC::Predicate)Code) {
233 case PPC::PRED_LT:
234 case PPC::PRED_LE:
235 case PPC::PRED_EQ:
236 case PPC::PRED_GE:
237 case PPC::PRED_GT:
238 case PPC::PRED_NE:
239 case PPC::PRED_UN:
240 case PPC::PRED_NU:
241 return;
242 case PPC::PRED_LT_MINUS:
243 case PPC::PRED_LE_MINUS:
244 case PPC::PRED_EQ_MINUS:
245 case PPC::PRED_GE_MINUS:
246 case PPC::PRED_GT_MINUS:
247 case PPC::PRED_NE_MINUS:
248 case PPC::PRED_UN_MINUS:
249 case PPC::PRED_NU_MINUS:
250 O << "-";
251 return;
252 case PPC::PRED_LT_PLUS:
253 case PPC::PRED_LE_PLUS:
254 case PPC::PRED_EQ_PLUS:
255 case PPC::PRED_GE_PLUS:
256 case PPC::PRED_GT_PLUS:
257 case PPC::PRED_NE_PLUS:
258 case PPC::PRED_UN_PLUS:
259 case PPC::PRED_NU_PLUS:
260 O << "+";
261 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000262 case PPC::PRED_BIT_SET:
263 case PPC::PRED_BIT_UNSET:
264 llvm_unreachable("Invalid use of bit predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000265 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000266 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000267 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000268
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000269 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000270 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000271 printOperand(MI, OpNo+1, O);
272}
273
Hal Finkel522e4d92016-09-03 02:31:44 +0000274void PPCInstPrinter::printATBitsAsHint(const MCInst *MI, unsigned OpNo,
275 raw_ostream &O) {
276 unsigned Code = MI->getOperand(OpNo).getImm();
277 if (Code == 2)
278 O << "-";
279 else if (Code == 3)
280 O << "+";
281}
282
Nemanja Ivanovice8effe12015-03-04 20:44:33 +0000283void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
284 raw_ostream &O) {
285 unsigned int Value = MI->getOperand(OpNo).getImm();
286 assert(Value <= 1 && "Invalid u1imm argument!");
287 O << (unsigned int)Value;
288}
289
Hal Finkel27774d92014-03-13 07:58:58 +0000290void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
291 raw_ostream &O) {
292 unsigned int Value = MI->getOperand(OpNo).getImm();
293 assert(Value <= 3 && "Invalid u2imm argument!");
294 O << (unsigned int)Value;
295}
296
Kit Barton535e69d2015-03-25 19:36:23 +0000297void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
298 raw_ostream &O) {
299 unsigned int Value = MI->getOperand(OpNo).getImm();
300 assert(Value <= 8 && "Invalid u3imm argument!");
301 O << (unsigned int)Value;
302}
303
Joerg Sonnenberger9e9623c2014-07-29 22:21:57 +0000304void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
305 raw_ostream &O) {
306 unsigned int Value = MI->getOperand(OpNo).getImm();
307 assert(Value <= 15 && "Invalid u4imm argument!");
308 O << (unsigned int)Value;
309}
310
Chris Lattner94881432010-11-14 20:11:21 +0000311void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
312 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000313 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000314 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000315 O << (int)Value;
316}
317
318void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
319 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000320 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000321 assert(Value <= 31 && "Invalid u5imm argument!");
322 O << (unsigned int)Value;
323}
324
325void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
326 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000327 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000328 assert(Value <= 63 && "Invalid u6imm argument!");
329 O << (unsigned int)Value;
330}
331
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000332void PPCInstPrinter::printU7ImmOperand(const MCInst *MI, unsigned OpNo,
333 raw_ostream &O) {
334 unsigned int Value = MI->getOperand(OpNo).getImm();
335 assert(Value <= 127 && "Invalid u7imm argument!");
336 O << (unsigned int)Value;
337}
338
Nemanja Ivanovicd2c3c512016-09-23 13:25:31 +0000339// Operands of BUILD_VECTOR are signed and we use this to print operands
340// of XXSPLTIB which are unsigned. So we simply truncate to 8 bits and
341// print as unsigned.
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000342void PPCInstPrinter::printU8ImmOperand(const MCInst *MI, unsigned OpNo,
343 raw_ostream &O) {
Nemanja Ivanovicd2c3c512016-09-23 13:25:31 +0000344 unsigned char Value = MI->getOperand(OpNo).getImm();
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000345 O << (unsigned int)Value;
346}
347
Bill Schmidte26236e2015-05-22 16:44:10 +0000348void PPCInstPrinter::printU10ImmOperand(const MCInst *MI, unsigned OpNo,
349 raw_ostream &O) {
350 unsigned short Value = MI->getOperand(OpNo).getImm();
351 assert(Value <= 1023 && "Invalid u10imm argument!");
352 O << (unsigned short)Value;
353}
354
Hal Finkelc93a9a22015-02-25 01:06:45 +0000355void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
356 raw_ostream &O) {
357 unsigned short Value = MI->getOperand(OpNo).getImm();
358 assert(Value <= 4095 && "Invalid u12imm argument!");
359 O << (unsigned short)Value;
360}
361
Chris Lattner94881432010-11-14 20:11:21 +0000362void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
363 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000364 if (MI->getOperand(OpNo).isImm())
365 O << (short)MI->getOperand(OpNo).getImm();
366 else
367 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000368}
369
370void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
371 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000372 if (MI->getOperand(OpNo).isImm())
373 O << (unsigned short)MI->getOperand(OpNo).getImm();
374 else
375 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000376}
377
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000378void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
379 raw_ostream &O) {
380 if (!MI->getOperand(OpNo).isImm())
381 return printOperand(MI, OpNo, O);
382
383 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000384 // selection pass to print .+8, an eight byte displacement from the PC.
385 O << ".+";
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000386 printAbsBranchOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000387}
388
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000389void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
390 raw_ostream &O) {
391 if (!MI->getOperand(OpNo).isImm())
392 return printOperand(MI, OpNo, O);
393
Alexey Samsonov9ca48702014-09-02 17:38:34 +0000394 O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000395}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000396
397
Chris Lattner0dcd8002010-11-14 20:22:56 +0000398void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
399 raw_ostream &O) {
400 unsigned CCReg = MI->getOperand(OpNo).getReg();
401 unsigned RegNo;
402 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000403 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000404 case PPC::CR0: RegNo = 0; break;
405 case PPC::CR1: RegNo = 1; break;
406 case PPC::CR2: RegNo = 2; break;
407 case PPC::CR3: RegNo = 3; break;
408 case PPC::CR4: RegNo = 4; break;
409 case PPC::CR5: RegNo = 5; break;
410 case PPC::CR6: RegNo = 6; break;
411 case PPC::CR7: RegNo = 7; break;
412 }
413 O << (0x80 >> RegNo);
414}
415
416void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
417 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000418 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000419 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000420 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000421 O << "0";
422 else
423 printOperand(MI, OpNo+1, O);
424 O << ')';
425}
426
Chris Lattner0dcd8002010-11-14 20:22:56 +0000427void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
428 raw_ostream &O) {
429 // When used as the base register, r0 reads constant zero rather than
430 // the value contained in the register. For this reason, the darwin
431 // assembler requires that we print r0 as 0 (no r) when used as the base.
432 if (MI->getOperand(OpNo).getReg() == PPC::R0)
433 O << "0";
434 else
435 printOperand(MI, OpNo, O);
436 O << ", ";
437 printOperand(MI, OpNo+1, O);
438}
439
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000440void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
441 raw_ostream &O) {
Hal Finkel7c8ae532014-07-25 17:47:22 +0000442 // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
443 // come at the _end_ of the expression.
444 const MCOperand &Op = MI->getOperand(OpNo);
445 const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
446 O << refExp.getSymbol().getName();
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000447 O << '(';
448 printOperand(MI, OpNo+1, O);
449 O << ')';
Hal Finkel7c8ae532014-07-25 17:47:22 +0000450 if (refExp.getKind() != MCSymbolRefExpr::VK_None)
451 O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000452}
Chris Lattner0dcd8002010-11-14 20:22:56 +0000453
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000454/// showRegistersWithPercentPrefix - Check if this register name should be
455/// printed with a percentage symbol as prefix.
456bool PPCInstPrinter::showRegistersWithPercentPrefix(const char *RegName) const {
457 if (!FullRegNamesWithPercent || TT.isOSDarwin() || TT.getOS() == Triple::AIX)
458 return false;
459
460 switch (RegName[0]) {
461 default:
462 return false;
463 case 'r':
464 case 'f':
465 case 'q':
466 case 'v':
467 case 'c':
468 return true;
469 }
470}
471
472/// getVerboseConditionalRegName - This method expands the condition register
473/// when requested explicitly or targetting Darwin.
474const char *PPCInstPrinter::getVerboseConditionRegName(unsigned RegNum,
475 unsigned RegEncoding)
476 const {
477 if (!TT.isOSDarwin() && !FullRegNames)
478 return nullptr;
479 if (RegNum < PPC::CR0EQ || RegNum > PPC::CR7UN)
480 return nullptr;
481 const char *CRBits[] = {
482 "lt", "gt", "eq", "un",
483 "4*cr1+lt", "4*cr1+gt", "4*cr1+eq", "4*cr1+un",
484 "4*cr2+lt", "4*cr2+gt", "4*cr2+eq", "4*cr2+un",
485 "4*cr3+lt", "4*cr3+gt", "4*cr3+eq", "4*cr3+un",
486 "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
487 "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un",
488 "4*cr6+lt", "4*cr6+gt", "4*cr6+eq", "4*cr6+un",
489 "4*cr7+lt", "4*cr7+gt", "4*cr7+eq", "4*cr7+un"
490 };
491 return CRBits[RegEncoding];
492}
493
494// showRegistersWithPrefix - This method determines whether registers
495// should be number-only or include the prefix.
496bool PPCInstPrinter::showRegistersWithPrefix() const {
497 if (TT.getOS() == Triple::AIX)
498 return false;
499 return TT.isOSDarwin() || FullRegNamesWithPercent || FullRegNames;
500}
Chris Lattner94881432010-11-14 20:11:21 +0000501
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000502/// stripRegisterPrefix - This method strips the character prefix from a
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000503/// register name so that only the number is left.
504static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000505 switch (RegName[0]) {
506 case 'r':
507 case 'f':
Hal Finkelc93a9a22015-02-25 01:06:45 +0000508 case 'q': // for QPX
Hal Finkel27774d92014-03-13 07:58:58 +0000509 case 'v':
510 if (RegName[1] == 's')
511 return RegName + 2;
512 return RegName + 1;
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000513 case 'c': if (RegName[1] == 'r') return RegName + 2;
514 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000515
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000516 return RegName;
517}
518
519void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
520 raw_ostream &O) {
521 const MCOperand &Op = MI->getOperand(OpNo);
522 if (Op.isReg()) {
Nemanja Ivanovic11049f82016-10-04 06:59:23 +0000523 unsigned Reg = Op.getReg();
524
525 // There are VSX instructions that use VSX register numbering (vs0 - vs63)
526 // as well as those that use VMX register numbering (v0 - v31 which
527 // correspond to vs32 - vs63). If we have an instruction that uses VSX
528 // numbering, we need to convert the VMX registers to VSX registers.
529 // Namely, we print 32-63 when the instruction operates on one of the
530 // VMX registers.
531 // (Please synchronize with PPCAsmPrinter::printOperand)
532 if ((MII.get(MI->getOpcode()).TSFlags & PPCII::UseVSXReg) &&
533 !ShowVSRNumsAsVR) {
534 if (PPCInstrInfo::isVRRegister(Reg))
535 Reg = PPC::VSX32 + (Reg - PPC::V0);
536 else if (PPCInstrInfo::isVFRegister(Reg))
537 Reg = PPC::VSX32 + (Reg - PPC::VF0);
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +0000538 }
Nemanja Ivanovic11049f82016-10-04 06:59:23 +0000539
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000540 const char *RegName;
541 RegName = getVerboseConditionRegName(Reg, MRI.getEncodingValue(Reg));
542 if (RegName == nullptr)
543 RegName = getRegisterName(Reg);
544 if (showRegistersWithPercentPrefix(RegName))
545 O << "%";
546 if (!showRegistersWithPrefix())
547 RegName = stripRegisterPrefix(RegName);
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000548
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000549 O << RegName;
550 return;
551 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000552
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000553 if (Op.isImm()) {
554 O << Op.getImm();
555 return;
556 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000557
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000558 assert(Op.isExpr() && "unknown operand kind in printOperand");
Matt Arsenault8b643552015-06-09 00:31:39 +0000559 Op.getExpr()->print(O, &MAI);
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000560}
Chris Lattnercfb62872010-11-14 21:54:34 +0000561