blob: 22907f52da74ac9d5ce3f502e96a9180457962bf [file] [log] [blame]
Chris Lattner60d5b5f2010-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"
Chris Lattnerb2e477f2010-11-14 21:51:37 +000016#include "PPCPredicates.h"
Chris Lattner0d1b7d92010-11-14 20:02:39 +000017#include "llvm/MC/MCExpr.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000018#include "llvm/MC/MCInst.h"
19//#include "llvm/MC/MCAsmInfo.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000020//#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/raw_ostream.h"
Chris Lattner60d5b5f2010-11-14 19:40:38 +000022using namespace llvm;
23
24#define GET_INSTRUCTION_NAME
25#define PPCAsmPrinter PPCInstPrinter
26#define MachineInstr MCInst
27#include "PPCGenAsmWriter.inc"
28
29StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
30 return getInstructionName(Opcode);
31}
32
33
34void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
35 // TODO: pseudo ops.
36
Chris Lattner2e352482010-11-14 21:39:51 +000037 // Check for slwi/srwi mnemonics.
38 if (MI->getOpcode() == PPC::RLWINM) {
39 unsigned char SH = MI->getOperand(2).getImm();
40 unsigned char MB = MI->getOperand(3).getImm();
41 unsigned char ME = MI->getOperand(4).getImm();
42 bool useSubstituteMnemonic = false;
43 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
44 O << "\tslwi "; useSubstituteMnemonic = true;
45 }
46 if (SH <= 31 && MB == (32-SH) && ME == 31) {
47 O << "\tsrwi "; useSubstituteMnemonic = true;
48 SH = 32-SH;
49 }
50 if (useSubstituteMnemonic) {
51 printOperand(MI, 0, O);
52 O << ", ";
53 printOperand(MI, 1, O);
54 O << ", " << (unsigned int)SH;
55 return;
56 }
57 }
58
59 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
60 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
61 O << "\tmr ";
62 printOperand(MI, 0, O);
63 O << ", ";
64 printOperand(MI, 1, O);
65 return;
66 }
67
68 if (MI->getOpcode() == PPC::RLDICR) {
69 unsigned char SH = MI->getOperand(2).getImm();
70 unsigned char ME = MI->getOperand(3).getImm();
71 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
72 if (63-SH == ME) {
73 O << "\tsldi ";
74 printOperand(MI, 0, O);
75 O << ", ";
76 printOperand(MI, 1, O);
77 O << ", " << (unsigned int)SH;
78 return;
79 }
80 }
81
Chris Lattner60d5b5f2010-11-14 19:40:38 +000082 printInstruction(MI, O);
83}
84
Chris Lattnerb2e477f2010-11-14 21:51:37 +000085
86void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
87 raw_ostream &O,
88 const char *Modifier) {
89 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
90 unsigned Code = MI->getOperand(OpNo).getImm();
91 if (StringRef(Modifier) == "cc") {
92 switch ((PPC::Predicate)Code) {
93 default: assert(0 && "Invalid predicate");
94 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
95 case PPC::PRED_LT: O << "lt"; return;
96 case PPC::PRED_LE: O << "le"; return;
97 case PPC::PRED_EQ: O << "eq"; return;
98 case PPC::PRED_GE: O << "ge"; return;
99 case PPC::PRED_GT: O << "gt"; return;
100 case PPC::PRED_NE: O << "ne"; return;
101 case PPC::PRED_UN: O << "un"; return;
102 case PPC::PRED_NU: O << "nu"; return;
103 }
104 }
105
106 assert(StringRef(Modifier) == "reg" &&
107 "Need to specify 'cc' or 'reg' as predicate op modifier!");
108 // Don't print the register for 'always'.
109 if (Code == PPC::PRED_ALWAYS) return;
110 printOperand(MI, OpNo+1, O);
111}
112
Chris Lattner99889132010-11-14 20:11:21 +0000113void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
114 raw_ostream &O) {
115 char Value = MI->getOperand(OpNo).getImm();
116 Value = (Value << (32-5)) >> (32-5);
117 O << (int)Value;
118}
119
120void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
121 raw_ostream &O) {
122 unsigned char Value = MI->getOperand(OpNo).getImm();
123 assert(Value <= 31 && "Invalid u5imm argument!");
124 O << (unsigned int)Value;
125}
126
127void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
128 raw_ostream &O) {
129 unsigned char Value = MI->getOperand(OpNo).getImm();
130 assert(Value <= 63 && "Invalid u6imm argument!");
131 O << (unsigned int)Value;
132}
133
134void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
135 raw_ostream &O) {
136 O << (short)MI->getOperand(OpNo).getImm();
137}
138
139void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
140 raw_ostream &O) {
141 O << (unsigned short)MI->getOperand(OpNo).getImm();
142}
143
144void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
145 raw_ostream &O) {
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000146 if (MI->getOperand(OpNo).isImm())
Chris Lattner99889132010-11-14 20:11:21 +0000147 O << (short)(MI->getOperand(OpNo).getImm()*4);
Chris Lattner99889132010-11-14 20:11:21 +0000148 else
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000149 printOperand(MI, OpNo, O);
Chris Lattner99889132010-11-14 20:11:21 +0000150}
151
Chris Lattner1520fd62010-11-14 21:20:46 +0000152void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
153 raw_ostream &O) {
154 if (!MI->getOperand(OpNo).isImm())
155 return printOperand(MI, OpNo, O);
156
157 // Branches can take an immediate operand. This is used by the branch
158 // selection pass to print $+8, an eight byte displacement from the PC.
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000159 O << "$+";
160 printAbsAddrOperand(MI, OpNo, O);
Chris Lattner1520fd62010-11-14 21:20:46 +0000161}
162
Chris Lattnerb2e477f2010-11-14 21:51:37 +0000163void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
164 raw_ostream &O) {
165 O << (int)MI->getOperand(OpNo).getImm()*4;
166}
Chris Lattner1520fd62010-11-14 21:20:46 +0000167
168
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000169void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
170 raw_ostream &O) {
171 unsigned CCReg = MI->getOperand(OpNo).getReg();
172 unsigned RegNo;
173 switch (CCReg) {
174 default: assert(0 && "Unknown CR register");
175 case PPC::CR0: RegNo = 0; break;
176 case PPC::CR1: RegNo = 1; break;
177 case PPC::CR2: RegNo = 2; break;
178 case PPC::CR3: RegNo = 3; break;
179 case PPC::CR4: RegNo = 4; break;
180 case PPC::CR5: RegNo = 5; break;
181 case PPC::CR6: RegNo = 6; break;
182 case PPC::CR7: RegNo = 7; break;
183 }
184 O << (0x80 >> RegNo);
185}
186
187void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
188 raw_ostream &O) {
189 printSymbolLo(MI, OpNo, O);
190 O << '(';
191 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
192 // FIXME: Simplify.
193 if (MI->getOperand(OpNo+1).isReg() &&
194 MI->getOperand(OpNo+1).getReg() == PPC::R0)
195 O << "0";
196 else
197 printOperand(MI, OpNo+1, O);
198 O << ')';
199}
200
201void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
202 raw_ostream &O) {
203 if (MI->getOperand(OpNo).isImm())
204 printS16X4ImmOperand(MI, OpNo, O);
205 else
206 printSymbolLo(MI, OpNo, O);
207 O << '(';
208
209 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
210 // FIXME: Simplify.
211 if (MI->getOperand(OpNo+1).isReg() &&
212 MI->getOperand(OpNo+1).getReg() == PPC::R0)
213 O << "0";
214 else
215 printOperand(MI, OpNo+1, O);
216 O << ')';
217}
218
219
220void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
221 raw_ostream &O) {
222 // When used as the base register, r0 reads constant zero rather than
223 // the value contained in the register. For this reason, the darwin
224 // assembler requires that we print r0 as 0 (no r) when used as the base.
225 if (MI->getOperand(OpNo).getReg() == PPC::R0)
226 O << "0";
227 else
228 printOperand(MI, OpNo, O);
229 O << ", ";
230 printOperand(MI, OpNo+1, O);
231}
232
233
Chris Lattner99889132010-11-14 20:11:21 +0000234
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000235/// stripRegisterPrefix - This method strips the character prefix from a
236/// register name so that only the number is left. Used by for linux asm.
237const char *stripRegisterPrefix(const char *RegName) {
238 switch (RegName[0]) {
239 case 'r':
240 case 'f':
241 case 'v': return RegName + 1;
242 case 'c': if (RegName[1] == 'r') return RegName + 2;
243 }
244
245 return RegName;
246}
247
248void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
249 raw_ostream &O) {
250 const MCOperand &Op = MI->getOperand(OpNo);
251 if (Op.isReg()) {
252 const char *RegName = getRegisterName(Op.getReg());
253 // The linux and AIX assembler does not take register prefixes.
254 if (!isDarwinSyntax())
255 RegName = stripRegisterPrefix(RegName);
256
257 O << RegName;
258 return;
259 }
260
261 if (Op.isImm()) {
262 O << Op.getImm();
263 return;
264 }
265
266 assert(Op.isExpr() && "unknown operand kind in printOperand");
267 O << *Op.getExpr();
268}
269
Chris Lattner58d014f2010-11-14 21:33:07 +0000270void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
271 raw_ostream &O) {
272 if (MI->getOperand(OpNo).isImm())
273 printS16ImmOperand(MI, OpNo, O);
274 else
275 printOperand(MI, OpNo, O);
276}
277
278void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
279 raw_ostream &O) {
280 if (MI->getOperand(OpNo).isImm())
281 printS16ImmOperand(MI, OpNo, O);
282 else
283 printOperand(MI, OpNo, O);
284}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000285
286
287void PPCInstPrinter::PrintSpecial(const MCInst *MI, raw_ostream &O,
288 const char *Modifier) {
289 assert(0 && "FIXME: PrintSpecial should be dead");
290}
291void PPCInstPrinter::printPICLabel(const MCInst *MI, unsigned OpNo,
292 raw_ostream &O) {
293 assert(0 && "FIXME: printPICLabel should be dead");
294}
295void PPCInstPrinter::printTOCEntryLabel(const MCInst *MI, unsigned OpNo,
296 raw_ostream &O) {
297 assert(0 && "FIXME: printTOCEntryLabel should be dead");
298}
299