blob: a67630266864edcac75903793540e7b3fd0eb158 [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
14#define DEBUG_TYPE "asm-printer"
15#include "PPCInstPrinter.h"
Hal Finkelfeea6532013-03-26 20:08:20 +000016#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng11424442011-07-26 00:24:13 +000017#include "MCTargetDesc/PPCPredicates.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"
Chris Lattnera76eab42010-11-14 19:40:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnera76eab42010-11-14 19:40:38 +000022using namespace llvm;
23
Chris Lattnera76eab42010-11-14 19:40:38 +000024#include "PPCGenAsmWriter.inc"
25
Rafael Espindolad6860522011-06-02 02:34:55 +000026void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
27 OS << getRegisterName(RegNo);
Rafael Espindola08600bc2011-05-30 20:20:15 +000028}
Chris Lattnera76eab42010-11-14 19:40:38 +000029
Owen Andersona0c3b972011-09-15 23:38:46 +000030void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
31 StringRef Annot) {
Chris Lattner219cc3d2010-11-14 21:39:51 +000032 // Check for slwi/srwi mnemonics.
33 if (MI->getOpcode() == PPC::RLWINM) {
34 unsigned char SH = MI->getOperand(2).getImm();
35 unsigned char MB = MI->getOperand(3).getImm();
36 unsigned char ME = MI->getOperand(4).getImm();
37 bool useSubstituteMnemonic = false;
38 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
39 O << "\tslwi "; useSubstituteMnemonic = true;
40 }
41 if (SH <= 31 && MB == (32-SH) && ME == 31) {
42 O << "\tsrwi "; useSubstituteMnemonic = true;
43 SH = 32-SH;
44 }
45 if (useSubstituteMnemonic) {
46 printOperand(MI, 0, O);
47 O << ", ";
48 printOperand(MI, 1, O);
49 O << ", " << (unsigned int)SH;
Owen Andersona0c3b972011-09-15 23:38:46 +000050
Owen Andersonbcc3fad2011-09-21 17:58:45 +000051 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000052 return;
53 }
54 }
55
56 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
57 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
58 O << "\tmr ";
59 printOperand(MI, 0, O);
60 O << ", ";
61 printOperand(MI, 1, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000062 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000063 return;
64 }
65
66 if (MI->getOpcode() == PPC::RLDICR) {
67 unsigned char SH = MI->getOperand(2).getImm();
68 unsigned char ME = MI->getOperand(3).getImm();
69 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
70 if (63-SH == ME) {
71 O << "\tsldi ";
72 printOperand(MI, 0, O);
73 O << ", ";
74 printOperand(MI, 1, O);
75 O << ", " << (unsigned int)SH;
Owen Andersonbcc3fad2011-09-21 17:58:45 +000076 printAnnotation(O, Annot);
Chris Lattner219cc3d2010-11-14 21:39:51 +000077 return;
78 }
79 }
80
Chris Lattnera76eab42010-11-14 19:40:38 +000081 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000082 printAnnotation(O, Annot);
Chris Lattnera76eab42010-11-14 19:40:38 +000083}
84
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000085
86void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
87 raw_ostream &O,
88 const char *Modifier) {
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000089 unsigned Code = MI->getOperand(OpNo).getImm();
Hal Finkel460e94d2012-06-22 23:10:08 +000090
Chris Lattnerf2cb69c2010-11-14 21:51:37 +000091 if (StringRef(Modifier) == "cc") {
92 switch ((PPC::Predicate)Code) {
Ulrich Weigand86247b62013-06-24 16:52:04 +000093 case PPC::PRED_LT_MINUS:
94 case PPC::PRED_LT_PLUS:
95 case PPC::PRED_LT:
96 O << "lt";
97 return;
98 case PPC::PRED_LE_MINUS:
99 case PPC::PRED_LE_PLUS:
100 case PPC::PRED_LE:
101 O << "le";
102 return;
103 case PPC::PRED_EQ_MINUS:
104 case PPC::PRED_EQ_PLUS:
105 case PPC::PRED_EQ:
106 O << "eq";
107 return;
108 case PPC::PRED_GE_MINUS:
109 case PPC::PRED_GE_PLUS:
110 case PPC::PRED_GE:
111 O << "ge";
112 return;
113 case PPC::PRED_GT_MINUS:
114 case PPC::PRED_GT_PLUS:
115 case PPC::PRED_GT:
116 O << "gt";
117 return;
118 case PPC::PRED_NE_MINUS:
119 case PPC::PRED_NE_PLUS:
120 case PPC::PRED_NE:
121 O << "ne";
122 return;
123 case PPC::PRED_UN_MINUS:
124 case PPC::PRED_UN_PLUS:
125 case PPC::PRED_UN:
126 O << "un";
127 return;
128 case PPC::PRED_NU_MINUS:
129 case PPC::PRED_NU_PLUS:
130 case PPC::PRED_NU:
131 O << "nu";
132 return;
Ulrich Weigand86247b62013-06-24 16:52:04 +0000133 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000134 llvm_unreachable("Invalid predicate code");
Ulrich Weigand86247b62013-06-24 16:52:04 +0000135 }
136
137 if (StringRef(Modifier) == "pm") {
138 switch ((PPC::Predicate)Code) {
139 case PPC::PRED_LT:
140 case PPC::PRED_LE:
141 case PPC::PRED_EQ:
142 case PPC::PRED_GE:
143 case PPC::PRED_GT:
144 case PPC::PRED_NE:
145 case PPC::PRED_UN:
146 case PPC::PRED_NU:
147 return;
148 case PPC::PRED_LT_MINUS:
149 case PPC::PRED_LE_MINUS:
150 case PPC::PRED_EQ_MINUS:
151 case PPC::PRED_GE_MINUS:
152 case PPC::PRED_GT_MINUS:
153 case PPC::PRED_NE_MINUS:
154 case PPC::PRED_UN_MINUS:
155 case PPC::PRED_NU_MINUS:
156 O << "-";
157 return;
158 case PPC::PRED_LT_PLUS:
159 case PPC::PRED_LE_PLUS:
160 case PPC::PRED_EQ_PLUS:
161 case PPC::PRED_GE_PLUS:
162 case PPC::PRED_GT_PLUS:
163 case PPC::PRED_NE_PLUS:
164 case PPC::PRED_UN_PLUS:
165 case PPC::PRED_NU_PLUS:
166 O << "+";
167 return;
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000168 }
Benjamin Kramer3912d782013-06-24 17:03:25 +0000169 llvm_unreachable("Invalid predicate code");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000170 }
171
172 assert(StringRef(Modifier) == "reg" &&
Ulrich Weigand86247b62013-06-24 16:52:04 +0000173 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000174 printOperand(MI, OpNo+1, O);
175}
176
Chris Lattner94881432010-11-14 20:11:21 +0000177void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
178 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000179 int Value = MI->getOperand(OpNo).getImm();
Richard Smith228e6d42012-08-24 23:29:28 +0000180 Value = SignExtend32<5>(Value);
Chris Lattner94881432010-11-14 20:11:21 +0000181 O << (int)Value;
182}
183
184void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
185 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000186 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000187 assert(Value <= 31 && "Invalid u5imm argument!");
188 O << (unsigned int)Value;
189}
190
191void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
192 raw_ostream &O) {
Adhemerval Zanellafe3f7932012-10-08 18:59:53 +0000193 unsigned int Value = MI->getOperand(OpNo).getImm();
Chris Lattner94881432010-11-14 20:11:21 +0000194 assert(Value <= 63 && "Invalid u6imm argument!");
195 O << (unsigned int)Value;
196}
197
198void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
199 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000200 if (MI->getOperand(OpNo).isImm())
201 O << (short)MI->getOperand(OpNo).getImm();
202 else
203 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000204}
205
206void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
207 raw_ostream &O) {
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000208 if (MI->getOperand(OpNo).isImm())
209 O << (unsigned short)MI->getOperand(OpNo).getImm();
210 else
211 printOperand(MI, OpNo, O);
Chris Lattner94881432010-11-14 20:11:21 +0000212}
213
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000214void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
215 raw_ostream &O) {
216 if (!MI->getOperand(OpNo).isImm())
217 return printOperand(MI, OpNo, O);
218
219 // Branches can take an immediate operand. This is used by the branch
Ulrich Weigandb9d5d072013-05-03 19:53:04 +0000220 // selection pass to print .+8, an eight byte displacement from the PC.
221 O << ".+";
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000222 printAbsBranchOperand(MI, OpNo, O);
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000223}
224
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000225void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
226 raw_ostream &O) {
227 if (!MI->getOperand(OpNo).isImm())
228 return printOperand(MI, OpNo, O);
229
Chris Lattnerf2cb69c2010-11-14 21:51:37 +0000230 O << (int)MI->getOperand(OpNo).getImm()*4;
231}
Chris Lattner3dc9bb22010-11-14 21:20:46 +0000232
233
Chris Lattner0dcd8002010-11-14 20:22:56 +0000234void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
235 raw_ostream &O) {
236 unsigned CCReg = MI->getOperand(OpNo).getReg();
237 unsigned RegNo;
238 switch (CCReg) {
Craig Toppere55c5562012-02-07 02:50:20 +0000239 default: llvm_unreachable("Unknown CR register");
Chris Lattner0dcd8002010-11-14 20:22:56 +0000240 case PPC::CR0: RegNo = 0; break;
241 case PPC::CR1: RegNo = 1; break;
242 case PPC::CR2: RegNo = 2; break;
243 case PPC::CR3: RegNo = 3; break;
244 case PPC::CR4: RegNo = 4; break;
245 case PPC::CR5: RegNo = 5; break;
246 case PPC::CR6: RegNo = 6; break;
247 case PPC::CR7: RegNo = 7; break;
248 }
249 O << (0x80 >> RegNo);
250}
251
252void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
253 raw_ostream &O) {
Ulrich Weigand41789de2013-05-23 22:26:41 +0000254 printS16ImmOperand(MI, OpNo, O);
Chris Lattner0dcd8002010-11-14 20:22:56 +0000255 O << '(';
Chris Lattnerfd56ee22010-11-15 03:51:13 +0000256 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattner0dcd8002010-11-14 20:22:56 +0000257 O << "0";
258 else
259 printOperand(MI, OpNo+1, O);
260 O << ')';
261}
262
Chris Lattner0dcd8002010-11-14 20:22:56 +0000263void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
264 raw_ostream &O) {
265 // When used as the base register, r0 reads constant zero rather than
266 // the value contained in the register. For this reason, the darwin
267 // assembler requires that we print r0 as 0 (no r) when used as the base.
268 if (MI->getOperand(OpNo).getReg() == PPC::R0)
269 O << "0";
270 else
271 printOperand(MI, OpNo, O);
272 O << ", ";
273 printOperand(MI, OpNo+1, O);
274}
275
276
Chris Lattner94881432010-11-14 20:11:21 +0000277
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000278/// stripRegisterPrefix - This method strips the character prefix from a
279/// register name so that only the number is left. Used by for linux asm.
Benjamin Krameraef5bd02010-11-25 16:42:51 +0000280static const char *stripRegisterPrefix(const char *RegName) {
Chris Lattner7a5c57e2010-11-14 20:02:39 +0000281 switch (RegName[0]) {
282 case 'r':
283 case 'f':
284 case 'v': return RegName + 1;
285 case 'c': if (RegName[1] == 'r') return RegName + 2;
286 }
287
288 return RegName;
289}
290
291void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
292 raw_ostream &O) {
293 const MCOperand &Op = MI->getOperand(OpNo);
294 if (Op.isReg()) {
295 const char *RegName = getRegisterName(Op.getReg());
296 // The linux and AIX assembler does not take register prefixes.
297 if (!isDarwinSyntax())
298 RegName = stripRegisterPrefix(RegName);
299
300 O << RegName;
301 return;
302 }
303
304 if (Op.isImm()) {
305 O << Op.getImm();
306 return;
307 }
308
309 assert(Op.isExpr() && "unknown operand kind in printOperand");
310 O << *Op.getExpr();
311}
Chris Lattnercfb62872010-11-14 21:54:34 +0000312