blob: 251385e95f37f1d87e1a26d7f721644c1d1ebcfc [file] [log] [blame]
Chris Lattnera76eab42010-11-14 19:40:38 +00001//===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnera76eab42010-11-14 19:40:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class prints an PPC MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
Chris Lattnera76eab42010-11-14 19:40:38 +000013#include "PPCInstPrinter.h"
Hal Finkelfeea6532013-03-26 20:08:20 +000014#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000015#include "MCTargetDesc/PPCPredicates.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "PPCInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000017#include "llvm/CodeGen/TargetOpcodes.h"
Chris Lattner7a5c57e2010-11-14 20:02:39 +000018#include "llvm/MC/MCExpr.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000019#include "llvm/MC/MCInst.h"
Craig Topperdab9e352012-04-02 07:01:04 +000020#include "llvm/MC/MCInstrInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000021#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCSubtargetInfo.h"
Hal Finkel7c8ae532014-07-25 17:47:22 +000023#include "llvm/MC/MCSymbol.h"
Hal Finkelc6a24392013-11-11 14:58:40 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "asm-printer"
29
Hal Finkelc6a24392013-11-11 14:58:40 +000030// FIXME: Once the integrated assembler supports full register names, tie this
31// to the verbose-asm setting.
32static cl::opt<bool>
33FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
34 cl::desc("Use full register names when printing assembly"));
35
Nemanja Ivanovic6e7879c2016-09-22 09:52:19 +000036// Useful for testing purposes. Prints vs{31-63} as v{0-31} respectively.
37static cl::opt<bool>
38ShowVSRNumsAsVR("ppc-vsr-nums-as-vr", cl::Hidden, cl::init(false),
39 cl::desc("Prints full register names with vs{31-63} as v{0-31}"));
40
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +000041// Prints full register names with percent symbol.
42static cl::opt<bool>
43FullRegNamesWithPercent("ppc-reg-with-percent-prefix", cl::Hidden,
44 cl::init(false),
45 cl::desc("Prints full register names with percent"));
46
Hal Finkel7c5cb062015-04-23 18:30:38 +000047#define PRINT_ALIAS_INSTR
Chris Lattnera76eab42010-11-14 19:40:38 +000048#include "PPCGenAsmWriter.inc"
49
Rafael Espindolad6860522011-06-02 02:34:55 +000050void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000051 const char *RegName = getRegisterName(RegNo);
52 if (RegName[0] == 'q' /* QPX */) {
53 // The system toolchain on the BG/Q does not understand QPX register names
54 // in .cfi_* directives, so print the name of the floating-point
55 // subregister instead.
56 std::string RN(RegName);
57
58 RN[0] = 'f';
59 OS << RN;
60
61 return;
62 }
63
64 OS << RegName;
Rafael Espindola08600bc2011-05-30 20:20:15 +000065}
Chris Lattnera76eab42010-11-14 19:40:38 +000066
Owen Andersona0c3b972011-09-15 23:38:46 +000067void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
Akira Hatanakab46d0232015-03-27 20:36:02 +000068 StringRef Annot, const MCSubtargetInfo &STI) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000069 // Check for slwi/srwi mnemonics.
70 if (MI->getOpcode() == PPC::RLWINM) {
71 unsigned char SH = MI->getOperand(2).getImm();
72 unsigned char MB = MI->getOperand(3).getImm();
73 unsigned char ME = MI->getOperand(4).getImm();
74 bool useSubstituteMnemonic = false;
75 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
76 O << "\tslwi "; useSubstituteMnemonic = true;
77 }
78 if (SH <= 31 && MB == (32-SH) && ME == 31) {
79 O << "\tsrwi "; useSubstituteMnemonic = true;
80 SH = 32-SH;
81 }
82 if (useSubstituteMnemonic) {
83 printOperand(MI, 0, O);
84 O << ", ";
85 printOperand(MI, 1, O);
86 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000087
Owen Andersonbcc3fad2011-09-21 17:58:45 +000088 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000089 return;
90 }
91 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +000092
Chris Lattner219cc3d2010-11-14 21:39:51 +000093 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
94 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
95 O << "\tmr ";
96 printOperand(MI, 0, O);
97 O << ", ";
98 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000099 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000100 return;
101 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000102
Nemanja Ivanovic96c3d622017-05-11 16:54:23 +0000103 if (MI->getOpcode() == PPC::RLDICR ||
104 MI->getOpcode() == PPC::RLDICR_32) {
Chris Lattner219cc3d2010-11-14 21:39:51 +0000105 unsigned char SH = MI->getOperand(2).getImm();
106 unsigned char ME = MI->getOperand(3).getImm();
107 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
108 if (63-SH == ME) {
109 O << "\tsldi ";
110 printOperand(MI, 0, O);
111 O << ", ";
112 printOperand(MI, 1, O);
113 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000114 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000115 return;
116 }
117 }
Hal Finkelfefcfff2015-04-23 22:47:57 +0000118
119 // dcbt[st] is printed manually here because:
120 // 1. The assembly syntax is different between embedded and server targets
121 // 2. We must print the short mnemonics for TH == 0 because the
122 // embedded/server syntax default will not be stable across assemblers
123 // The syntax for dcbt is:
124 // dcbt ra, rb, th [server]
125 // dcbt th, ra, rb [embedded]
126 // where th can be omitted when it is 0. dcbtst is the same.
127 if (MI->getOpcode() == PPC::DCBT || MI->getOpcode() == PPC::DCBTST) {
128 unsigned char TH = MI->getOperand(0).getImm();
129 O << "\tdcbt";
130 if (MI->getOpcode() == PPC::DCBTST)
131 O << "st";
132 if (TH == 16)
133 O << "t";
134 O << " ";
135
Michael Kupersteindb0712f2015-05-26 10:47:10 +0000136 bool IsBookE = STI.getFeatureBits()[PPC::FeatureBookE];
Hal Finkelfefcfff2015-04-23 22:47:57 +0000137 if (IsBookE && TH != 0 && TH != 16)
138 O << (unsigned int) TH << ", ";
139
140 printOperand(MI, 1, O);
141 O << ", ";
142 printOperand(MI, 2, O);
143
144 if (!IsBookE && TH != 0 && TH != 16)
145 O << ", " << (unsigned int) TH;
146
147 printAnnotation(O, Annot);
148 return;
149 }
Hal Finkel277736e2016-09-02 23:41:54 +0000150
151 if (MI->getOpcode() == PPC::DCBF) {
152 unsigned char L = MI->getOperand(0).getImm();
153 if (!L || L == 1 || L == 3) {
154 O << "\tdcbf";
155 if (L == 1 || L == 3)
156 O << "l";
157 if (L == 3)
158 O << "p";
159 O << " ";
160
161 printOperand(MI, 1, O);
162 O << ", ";
163 printOperand(MI, 2, O);
164
165 printAnnotation(O, Annot);
166 return;
167 }
168 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000169
Hal Finkel7c5cb062015-04-23 18:30:38 +0000170 if (!printAliasInstr(MI, O))
171 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000172 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +0000173}
174
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000175
176void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
Fangrui Songf78650a2018-07-30 19:41:25 +0000177 raw_ostream &O,
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000178 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000179 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +0000180
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000181 if (StringRef(Modifier) == "cc") {
182 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +0000183 case PPC::PRED_LT_MINUS:
184 case PPC::PRED_LT_PLUS:
185 case PPC::PRED_LT:
186 O << "lt";
187 return;
188 case PPC::PRED_LE_MINUS:
189 case PPC::PRED_LE_PLUS:
190 case PPC::PRED_LE:
191 O << "le";
192 return;
193 case PPC::PRED_EQ_MINUS:
194 case PPC::PRED_EQ_PLUS:
195 case PPC::PRED_EQ:
196 O << "eq";
197 return;
198 case PPC::PRED_GE_MINUS:
199 case PPC::PRED_GE_PLUS:
200 case PPC::PRED_GE:
201 O << "ge";
202 return;
203 case PPC::PRED_GT_MINUS:
204 case PPC::PRED_GT_PLUS:
205 case PPC::PRED_GT:
206 O << "gt";
207 return;
208 case PPC::PRED_NE_MINUS:
209 case PPC::PRED_NE_PLUS:
210 case PPC::PRED_NE:
211 O << "ne";
212 return;
213 case PPC::PRED_UN_MINUS:
214 case PPC::PRED_UN_PLUS:
215 case PPC::PRED_UN:
216 O << "un";
217 return;
218 case PPC::PRED_NU_MINUS:
219 case PPC::PRED_NU_PLUS:
220 case PPC::PRED_NU:
221 O << "nu";
222 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000223 case PPC::PRED_BIT_SET:
224 case PPC::PRED_BIT_UNSET:
225 llvm_unreachable("Invalid use of bit predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000226 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000227 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000228 }
229
230 if (StringRef(Modifier) == "pm") {
231 switch ((PPC::Predicate)Code) {
232 case PPC::PRED_LT:
233 case PPC::PRED_LE:
234 case PPC::PRED_EQ:
235 case PPC::PRED_GE:
236 case PPC::PRED_GT:
237 case PPC::PRED_NE:
238 case PPC::PRED_UN:
239 case PPC::PRED_NU:
240 return;
241 case PPC::PRED_LT_MINUS:
242 case PPC::PRED_LE_MINUS:
243 case PPC::PRED_EQ_MINUS:
244 case PPC::PRED_GE_MINUS:
245 case PPC::PRED_GT_MINUS:
246 case PPC::PRED_NE_MINUS:
247 case PPC::PRED_UN_MINUS:
248 case PPC::PRED_NU_MINUS:
249 O << "-";
250 return;
251 case PPC::PRED_LT_PLUS:
252 case PPC::PRED_LE_PLUS:
253 case PPC::PRED_EQ_PLUS:
254 case PPC::PRED_GE_PLUS:
255 case PPC::PRED_GT_PLUS:
256 case PPC::PRED_NE_PLUS:
257 case PPC::PRED_UN_PLUS:
258 case PPC::PRED_NU_PLUS:
259 O << "+";
260 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000261 case PPC::PRED_BIT_SET:
262 case PPC::PRED_BIT_UNSET:
263 llvm_unreachable("Invalid use of bit predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000264 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000265 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000266 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000267
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000268 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000269 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000270 printOperand(MI, OpNo+1, O);
271}
272
Hal Finkel522e4d92016-09-03 02:31:44 +0000273void PPCInstPrinter::printATBitsAsHint(const MCInst *MI, unsigned OpNo,
274 raw_ostream &O) {
275 unsigned Code = MI->getOperand(OpNo).getImm();
276 if (Code == 2)
277 O << "-";
278 else if (Code == 3)
279 O << "+";
280}
281
Nemanja Ivanovice8effe12015-03-04 20:44:33 +0000282void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
283 raw_ostream &O) {
284 unsigned int Value = MI->getOperand(OpNo).getImm();
285 assert(Value <= 1 && "Invalid u1imm argument!");
286 O << (unsigned int)Value;
287}
288
Hal Finkel27774d92014-03-13 07:58:58 +0000289void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
290 raw_ostream &O) {
291 unsigned int Value = MI->getOperand(OpNo).getImm();
292 assert(Value <= 3 && "Invalid u2imm argument!");
293 O << (unsigned int)Value;
294}
295
Kit Barton535e69d2015-03-25 19:36:23 +0000296void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
297 raw_ostream &O) {
298 unsigned int Value = MI->getOperand(OpNo).getImm();
299 assert(Value <= 8 && "Invalid u3imm argument!");
300 O << (unsigned int)Value;
301}
302
Joerg Sonnenberger9e9623c2014-07-29 22:21:57 +0000303void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
304 raw_ostream &O) {
305 unsigned int Value = MI->getOperand(OpNo).getImm();
306 assert(Value <= 15 && "Invalid u4imm argument!");
307 O << (unsigned int)Value;
308}
309
Chris Lattner94881432010-11-14 20:11:21 +0000310void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
311 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000312 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000313 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000314 O << (int)Value;
315}
316
317void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
318 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000319 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000320 assert(Value <= 31 && "Invalid u5imm argument!");
321 O << (unsigned int)Value;
322}
323
324void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
325 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000326 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000327 assert(Value <= 63 && "Invalid u6imm argument!");
328 O << (unsigned int)Value;
329}
330
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000331void PPCInstPrinter::printU7ImmOperand(const MCInst *MI, unsigned OpNo,
332 raw_ostream &O) {
333 unsigned int Value = MI->getOperand(OpNo).getImm();
334 assert(Value <= 127 && "Invalid u7imm argument!");
335 O << (unsigned int)Value;
336}
337
Nemanja Ivanovicd2c3c512016-09-23 13:25:31 +0000338// Operands of BUILD_VECTOR are signed and we use this to print operands
339// of XXSPLTIB which are unsigned. So we simply truncate to 8 bits and
340// print as unsigned.
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000341void PPCInstPrinter::printU8ImmOperand(const MCInst *MI, unsigned OpNo,
342 raw_ostream &O) {
Nemanja Ivanovicd2c3c512016-09-23 13:25:31 +0000343 unsigned char Value = MI->getOperand(OpNo).getImm();
Chuang-Yu Cheng80722712016-03-28 08:34:28 +0000344 O << (unsigned int)Value;
345}
346
Bill Schmidte26236e2015-05-22 16:44:10 +0000347void PPCInstPrinter::printU10ImmOperand(const MCInst *MI, unsigned OpNo,
348 raw_ostream &O) {
349 unsigned short Value = MI->getOperand(OpNo).getImm();
350 assert(Value <= 1023 && "Invalid u10imm argument!");
351 O << (unsigned short)Value;
352}
353
Hal Finkelc93a9a22015-02-25 01:06:45 +0000354void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
355 raw_ostream &O) {
356 unsigned short Value = MI->getOperand(OpNo).getImm();
357 assert(Value <= 4095 && "Invalid u12imm argument!");
358 O << (unsigned short)Value;
359}
360
Chris Lattner94881432010-11-14 20:11:21 +0000361void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
362 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000363 if (MI->getOperand(OpNo).isImm())
364 O << (short)MI->getOperand(OpNo).getImm();
365 else
366 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000367}
368
369void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
370 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000371 if (MI->getOperand(OpNo).isImm())
372 O << (unsigned short)MI->getOperand(OpNo).getImm();
373 else
374 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000375}
376
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000377void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
378 raw_ostream &O) {
379 if (!MI->getOperand(OpNo).isImm())
380 return printOperand(MI, OpNo, O);
381
382 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000383 // selection pass to print .+8, an eight byte displacement from the PC.
Sean Fertilec0694522019-02-12 17:48:22 +0000384 O << ".";
Sean Fertile9850a482019-02-12 20:03:04 +0000385 int32_t Imm = SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
Sean Fertilec0694522019-02-12 17:48:22 +0000386 if (Imm >= 0)
387 O << "+";
388 O << Imm;
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000389}
390
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000391void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
392 raw_ostream &O) {
393 if (!MI->getOperand(OpNo).isImm())
394 return printOperand(MI, OpNo, O);
395
Alexey Samsonov9ca48702014-09-02 17:38:34 +0000396 O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000397}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000398
399
Chris Lattner0dcd8002010-11-14 20:22:56 +0000400void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
401 raw_ostream &O) {
402 unsigned CCReg = MI->getOperand(OpNo).getReg();
403 unsigned RegNo;
404 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000405 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000406 case PPC::CR0: RegNo = 0; break;
407 case PPC::CR1: RegNo = 1; break;
408 case PPC::CR2: RegNo = 2; break;
409 case PPC::CR3: RegNo = 3; break;
410 case PPC::CR4: RegNo = 4; break;
411 case PPC::CR5: RegNo = 5; break;
412 case PPC::CR6: RegNo = 6; break;
413 case PPC::CR7: RegNo = 7; break;
414 }
415 O << (0x80 >> RegNo);
416}
417
418void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
419 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000420 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000421 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000422 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000423 O << "0";
424 else
425 printOperand(MI, OpNo+1, O);
426 O << ')';
427}
428
Chris Lattner0dcd8002010-11-14 20:22:56 +0000429void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
430 raw_ostream &O) {
431 // When used as the base register, r0 reads constant zero rather than
432 // the value contained in the register. For this reason, the darwin
433 // assembler requires that we print r0 as 0 (no r) when used as the base.
434 if (MI->getOperand(OpNo).getReg() == PPC::R0)
435 O << "0";
436 else
437 printOperand(MI, OpNo, O);
438 O << ", ";
439 printOperand(MI, OpNo+1, O);
440}
441
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000442void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
443 raw_ostream &O) {
Hal Finkel7c8ae532014-07-25 17:47:22 +0000444 // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
445 // come at the _end_ of the expression.
446 const MCOperand &Op = MI->getOperand(OpNo);
Strahinja Petrovic94fccc92019-03-06 15:00:10 +0000447 const MCSymbolRefExpr *RefExp = nullptr;
448 const MCConstantExpr *ConstExp = nullptr;
449 if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Op.getExpr())) {
450 RefExp = cast<MCSymbolRefExpr>(BinExpr->getLHS());
451 ConstExp = cast<MCConstantExpr>(BinExpr->getRHS());
452 } else
453 RefExp = cast<MCSymbolRefExpr>(Op.getExpr());
454
455 O << RefExp->getSymbol().getName();
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000456 O << '(';
457 printOperand(MI, OpNo+1, O);
458 O << ')';
Strahinja Petrovic94fccc92019-03-06 15:00:10 +0000459 if (RefExp->getKind() != MCSymbolRefExpr::VK_None)
460 O << '@' << MCSymbolRefExpr::getVariantKindName(RefExp->getKind());
461 if (ConstExp != nullptr)
462 O << '+' << ConstExp->getValue();
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000463}
Chris Lattner0dcd8002010-11-14 20:22:56 +0000464
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000465/// showRegistersWithPercentPrefix - Check if this register name should be
466/// printed with a percentage symbol as prefix.
467bool PPCInstPrinter::showRegistersWithPercentPrefix(const char *RegName) const {
468 if (!FullRegNamesWithPercent || TT.isOSDarwin() || TT.getOS() == Triple::AIX)
469 return false;
470
471 switch (RegName[0]) {
472 default:
473 return false;
474 case 'r':
475 case 'f':
476 case 'q':
477 case 'v':
478 case 'c':
479 return true;
480 }
481}
482
483/// getVerboseConditionalRegName - This method expands the condition register
484/// when requested explicitly or targetting Darwin.
485const char *PPCInstPrinter::getVerboseConditionRegName(unsigned RegNum,
486 unsigned RegEncoding)
487 const {
488 if (!TT.isOSDarwin() && !FullRegNames)
489 return nullptr;
490 if (RegNum < PPC::CR0EQ || RegNum > PPC::CR7UN)
491 return nullptr;
492 const char *CRBits[] = {
493 "lt", "gt", "eq", "un",
494 "4*cr1+lt", "4*cr1+gt", "4*cr1+eq", "4*cr1+un",
495 "4*cr2+lt", "4*cr2+gt", "4*cr2+eq", "4*cr2+un",
496 "4*cr3+lt", "4*cr3+gt", "4*cr3+eq", "4*cr3+un",
497 "4*cr4+lt", "4*cr4+gt", "4*cr4+eq", "4*cr4+un",
498 "4*cr5+lt", "4*cr5+gt", "4*cr5+eq", "4*cr5+un",
499 "4*cr6+lt", "4*cr6+gt", "4*cr6+eq", "4*cr6+un",
500 "4*cr7+lt", "4*cr7+gt", "4*cr7+eq", "4*cr7+un"
501 };
502 return CRBits[RegEncoding];
503}
504
505// showRegistersWithPrefix - This method determines whether registers
506// should be number-only or include the prefix.
507bool PPCInstPrinter::showRegistersWithPrefix() const {
508 if (TT.getOS() == Triple::AIX)
509 return false;
510 return TT.isOSDarwin() || FullRegNamesWithPercent || FullRegNames;
511}
Chris Lattner94881432010-11-14 20:11:21 +0000512
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000513void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
514 raw_ostream &O) {
515 const MCOperand &Op = MI->getOperand(OpNo);
516 if (Op.isReg()) {
Nemanja Ivanovic11049f82016-10-04 06:59:23 +0000517 unsigned Reg = Op.getReg();
Nemanja Ivanovica5909672018-09-27 11:49:47 +0000518 if (!ShowVSRNumsAsVR)
519 Reg = PPCInstrInfo::getRegNumForOperand(MII.get(MI->getOpcode()),
520 Reg, OpNo);
Nemanja Ivanovic11049f82016-10-04 06:59:23 +0000521
Joerg Sonnenberger4b1acff2017-11-29 23:05:56 +0000522 const char *RegName;
523 RegName = getVerboseConditionRegName(Reg, MRI.getEncodingValue(Reg));
524 if (RegName == nullptr)
525 RegName = getRegisterName(Reg);
526 if (showRegistersWithPercentPrefix(RegName))
527 O << "%";
528 if (!showRegistersWithPrefix())
Nemanja Ivanovica5909672018-09-27 11:49:47 +0000529 RegName = PPCRegisterInfo::stripRegisterPrefix(RegName);
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000530
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000531 O << RegName;
532 return;
533 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000534
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000535 if (Op.isImm()) {
536 O << Op.getImm();
537 return;
538 }
Nemanja Ivanovic009016b2017-07-25 18:26:35 +0000539
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000540 assert(Op.isExpr() && "unknown operand kind in printOperand");
Matt Arsenault8b643552015-06-09 00:31:39 +0000541 Op.getExpr()->print(O, &MAI);
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000542}
Chris Lattnercfb62872010-11-14 21:54:34 +0000543