blob: 046682ddfc3ac7f071c899560009f036b763ed52 [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
Hal Finkel7c5cb062015-04-23 18:30:38 +000034#define PRINT_ALIAS_INSTR
Chris Lattnera76eab42010-11-14 19:40:38 +000035#include "PPCGenAsmWriter.inc"
36
Rafael Espindolad6860522011-06-02 02:34:55 +000037void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
Hal Finkelc93a9a22015-02-25 01:06:45 +000038 const char *RegName = getRegisterName(RegNo);
39 if (RegName[0] == 'q' /* QPX */) {
40 // The system toolchain on the BG/Q does not understand QPX register names
41 // in .cfi_* directives, so print the name of the floating-point
42 // subregister instead.
43 std::string RN(RegName);
44
45 RN[0] = 'f';
46 OS << RN;
47
48 return;
49 }
50
51 OS << RegName;
Rafael Espindola08600bc2011-05-30 20:20:15 +000052}
Chris Lattnera76eab42010-11-14 19:40:38 +000053
Owen Andersona0c3b972011-09-15 23:38:46 +000054void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
Akira Hatanakab46d0232015-03-27 20:36:02 +000055 StringRef Annot, const MCSubtargetInfo &STI) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000056 // Check for slwi/srwi mnemonics.
57 if (MI->getOpcode() == PPC::RLWINM) {
58 unsigned char SH = MI->getOperand(2).getImm();
59 unsigned char MB = MI->getOperand(3).getImm();
60 unsigned char ME = MI->getOperand(4).getImm();
61 bool useSubstituteMnemonic = false;
62 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
63 O << "\tslwi "; useSubstituteMnemonic = true;
64 }
65 if (SH <= 31 && MB == (32-SH) && ME == 31) {
66 O << "\tsrwi "; useSubstituteMnemonic = true;
67 SH = 32-SH;
68 }
69 if (useSubstituteMnemonic) {
70 printOperand(MI, 0, O);
71 O << ", ";
72 printOperand(MI, 1, O);
73 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000074
Owen Andersonbcc3fad2011-09-21 17:58:45 +000075 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000076 return;
77 }
78 }
79
80 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
81 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
82 O << "\tmr ";
83 printOperand(MI, 0, O);
84 O << ", ";
85 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000086 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000087 return;
88 }
89
90 if (MI->getOpcode() == PPC::RLDICR) {
91 unsigned char SH = MI->getOperand(2).getImm();
92 unsigned char ME = MI->getOperand(3).getImm();
93 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
94 if (63-SH == ME) {
95 O << "\tsldi ";
96 printOperand(MI, 0, O);
97 O << ", ";
98 printOperand(MI, 1, O);
99 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000100 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +0000101 return;
102 }
103 }
104
Bill Schmidt8d86fe72013-08-30 15:18:11 +0000105 // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is
106 // used when converting a 32-bit float to a 64-bit float as part of
107 // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
108 // as otherwise we have problems with incorrect register classes
109 // in machine instruction verification. For now, just avoid trying
110 // to print it as such an instruction has no effect (a 32-bit float
111 // in a register is already in 64-bit form, just with lower
112 // precision). FIXME: Is there a better solution?
113 if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
114 return;
Hal Finkel7c5cb062015-04-23 18:30:38 +0000115
116 if (!printAliasInstr(MI, O))
117 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +0000118 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +0000119}
120
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000121
122void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
123 raw_ostream &O,
124 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000125 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +0000126
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000127 if (StringRef(Modifier) == "cc") {
128 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +0000129 case PPC::PRED_LT_MINUS:
130 case PPC::PRED_LT_PLUS:
131 case PPC::PRED_LT:
132 O << "lt";
133 return;
134 case PPC::PRED_LE_MINUS:
135 case PPC::PRED_LE_PLUS:
136 case PPC::PRED_LE:
137 O << "le";
138 return;
139 case PPC::PRED_EQ_MINUS:
140 case PPC::PRED_EQ_PLUS:
141 case PPC::PRED_EQ:
142 O << "eq";
143 return;
144 case PPC::PRED_GE_MINUS:
145 case PPC::PRED_GE_PLUS:
146 case PPC::PRED_GE:
147 O << "ge";
148 return;
149 case PPC::PRED_GT_MINUS:
150 case PPC::PRED_GT_PLUS:
151 case PPC::PRED_GT:
152 O << "gt";
153 return;
154 case PPC::PRED_NE_MINUS:
155 case PPC::PRED_NE_PLUS:
156 case PPC::PRED_NE:
157 O << "ne";
158 return;
159 case PPC::PRED_UN_MINUS:
160 case PPC::PRED_UN_PLUS:
161 case PPC::PRED_UN:
162 O << "un";
163 return;
164 case PPC::PRED_NU_MINUS:
165 case PPC::PRED_NU_PLUS:
166 case PPC::PRED_NU:
167 O << "nu";
168 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000169 case PPC::PRED_BIT_SET:
170 case PPC::PRED_BIT_UNSET:
171 llvm_unreachable("Invalid use of bit predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000172 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000173 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000174 }
175
176 if (StringRef(Modifier) == "pm") {
177 switch ((PPC::Predicate)Code) {
178 case PPC::PRED_LT:
179 case PPC::PRED_LE:
180 case PPC::PRED_EQ:
181 case PPC::PRED_GE:
182 case PPC::PRED_GT:
183 case PPC::PRED_NE:
184 case PPC::PRED_UN:
185 case PPC::PRED_NU:
186 return;
187 case PPC::PRED_LT_MINUS:
188 case PPC::PRED_LE_MINUS:
189 case PPC::PRED_EQ_MINUS:
190 case PPC::PRED_GE_MINUS:
191 case PPC::PRED_GT_MINUS:
192 case PPC::PRED_NE_MINUS:
193 case PPC::PRED_UN_MINUS:
194 case PPC::PRED_NU_MINUS:
195 O << "-";
196 return;
197 case PPC::PRED_LT_PLUS:
198 case PPC::PRED_LE_PLUS:
199 case PPC::PRED_EQ_PLUS:
200 case PPC::PRED_GE_PLUS:
201 case PPC::PRED_GT_PLUS:
202 case PPC::PRED_NE_PLUS:
203 case PPC::PRED_UN_PLUS:
204 case PPC::PRED_NU_PLUS:
205 O << "+";
206 return;
Hal Finkel940ab932014-02-28 00:27:01 +0000207 case PPC::PRED_BIT_SET:
208 case PPC::PRED_BIT_UNSET:
209 llvm_unreachable("Invalid use of bit predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000210 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000211 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000212 }
213
214 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000215 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000216 printOperand(MI, OpNo+1, O);
217}
218
Nemanja Ivanovice8effe12015-03-04 20:44:33 +0000219void PPCInstPrinter::printU1ImmOperand(const MCInst *MI, unsigned OpNo,
220 raw_ostream &O) {
221 unsigned int Value = MI->getOperand(OpNo).getImm();
222 assert(Value <= 1 && "Invalid u1imm argument!");
223 O << (unsigned int)Value;
224}
225
Hal Finkel27774d92014-03-13 07:58:58 +0000226void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
227 raw_ostream &O) {
228 unsigned int Value = MI->getOperand(OpNo).getImm();
229 assert(Value <= 3 && "Invalid u2imm argument!");
230 O << (unsigned int)Value;
231}
232
Kit Barton535e69d2015-03-25 19:36:23 +0000233void PPCInstPrinter::printU3ImmOperand(const MCInst *MI, unsigned OpNo,
234 raw_ostream &O) {
235 unsigned int Value = MI->getOperand(OpNo).getImm();
236 assert(Value <= 8 && "Invalid u3imm argument!");
237 O << (unsigned int)Value;
238}
239
Joerg Sonnenberger9e9623c2014-07-29 22:21:57 +0000240void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
241 raw_ostream &O) {
242 unsigned int Value = MI->getOperand(OpNo).getImm();
243 assert(Value <= 15 && "Invalid u4imm argument!");
244 O << (unsigned int)Value;
245}
246
Chris Lattner94881432010-11-14 20:11:21 +0000247void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
248 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000249 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000250 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000251 O << (int)Value;
252}
253
254void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
255 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000256 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000257 assert(Value <= 31 && "Invalid u5imm argument!");
258 O << (unsigned int)Value;
259}
260
261void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
262 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000263 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000264 assert(Value <= 63 && "Invalid u6imm argument!");
265 O << (unsigned int)Value;
266}
267
Hal Finkelc93a9a22015-02-25 01:06:45 +0000268void PPCInstPrinter::printU12ImmOperand(const MCInst *MI, unsigned OpNo,
269 raw_ostream &O) {
270 unsigned short Value = MI->getOperand(OpNo).getImm();
271 assert(Value <= 4095 && "Invalid u12imm argument!");
272 O << (unsigned short)Value;
273}
274
Chris Lattner94881432010-11-14 20:11:21 +0000275void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
276 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000277 if (MI->getOperand(OpNo).isImm())
278 O << (short)MI->getOperand(OpNo).getImm();
279 else
280 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000281}
282
283void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
284 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000285 if (MI->getOperand(OpNo).isImm())
286 O << (unsigned short)MI->getOperand(OpNo).getImm();
287 else
288 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000289}
290
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000291void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
292 raw_ostream &O) {
293 if (!MI->getOperand(OpNo).isImm())
294 return printOperand(MI, OpNo, O);
295
296 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000297 // selection pass to print .+8, an eight byte displacement from the PC.
298 O << ".+";
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000299 printAbsBranchOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000300}
301
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000302void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
303 raw_ostream &O) {
304 if (!MI->getOperand(OpNo).isImm())
305 return printOperand(MI, OpNo, O);
306
Alexey Samsonov9ca48702014-09-02 17:38:34 +0000307 O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000308}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000309
310
Chris Lattner0dcd8002010-11-14 20:22:56 +0000311void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
312 raw_ostream &O) {
313 unsigned CCReg = MI->getOperand(OpNo).getReg();
314 unsigned RegNo;
315 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000316 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000317 case PPC::CR0: RegNo = 0; break;
318 case PPC::CR1: RegNo = 1; break;
319 case PPC::CR2: RegNo = 2; break;
320 case PPC::CR3: RegNo = 3; break;
321 case PPC::CR4: RegNo = 4; break;
322 case PPC::CR5: RegNo = 5; break;
323 case PPC::CR6: RegNo = 6; break;
324 case PPC::CR7: RegNo = 7; break;
325 }
326 O << (0x80 >> RegNo);
327}
328
329void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
330 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000331 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000332 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000333 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000334 O << "0";
335 else
336 printOperand(MI, OpNo+1, O);
337 O << ')';
338}
339
Chris Lattner0dcd8002010-11-14 20:22:56 +0000340void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
341 raw_ostream &O) {
342 // When used as the base register, r0 reads constant zero rather than
343 // the value contained in the register. For this reason, the darwin
344 // assembler requires that we print r0 as 0 (no r) when used as the base.
345 if (MI->getOperand(OpNo).getReg() == PPC::R0)
346 O << "0";
347 else
348 printOperand(MI, OpNo, O);
349 O << ", ";
350 printOperand(MI, OpNo+1, O);
351}
352
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000353void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
354 raw_ostream &O) {
Hal Finkel7c8ae532014-07-25 17:47:22 +0000355 // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
356 // come at the _end_ of the expression.
357 const MCOperand &Op = MI->getOperand(OpNo);
358 const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
359 O << refExp.getSymbol().getName();
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000360 O << '(';
361 printOperand(MI, OpNo+1, O);
362 O << ')';
Hal Finkel7c8ae532014-07-25 17:47:22 +0000363 if (refExp.getKind() != MCSymbolRefExpr::VK_None)
364 O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000365}
Chris Lattner0dcd8002010-11-14 20:22:56 +0000366
Chris Lattner94881432010-11-14 20:11:21 +0000367
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000368/// stripRegisterPrefix - This method strips the character prefix from a
369/// register name so that only the number is left. Used by for linux asm.
Benjamin Krameraef5bd02010-11-25 16:42:51 +0000370static const char *stripRegisterPrefix(const char *RegName) {
Hal Finkelc6a24392013-11-11 14:58:40 +0000371 if (FullRegNames)
372 return RegName;
373
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000374 switch (RegName[0]) {
375 case 'r':
376 case 'f':
Hal Finkelc93a9a22015-02-25 01:06:45 +0000377 case 'q': // for QPX
Hal Finkel27774d92014-03-13 07:58:58 +0000378 case 'v':
379 if (RegName[1] == 's')
380 return RegName + 2;
381 return RegName + 1;
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000382 case 'c': if (RegName[1] == 'r') return RegName + 2;
383 }
384
385 return RegName;
386}
387
388void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
389 raw_ostream &O) {
390 const MCOperand &Op = MI->getOperand(OpNo);
391 if (Op.isReg()) {
392 const char *RegName = getRegisterName(Op.getReg());
393 // The linux and AIX assembler does not take register prefixes.
394 if (!isDarwinSyntax())
395 RegName = stripRegisterPrefix(RegName);
396
397 O << RegName;
398 return;
399 }
400
401 if (Op.isImm()) {
402 O << Op.getImm();
403 return;
404 }
405
406 assert(Op.isExpr() && "unknown operand kind in printOperand");
407 O << *Op.getExpr();
408}
Chris Lattnercfb62872010-11-14 21:54:34 +0000409