blob: 941adfbf0d9d1cb98ef9610155d90e452b177109 [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 << '(';
Chris Lattner0fe71842010-11-15 03:51:13 +0000191 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000192 O << "0";
193 else
194 printOperand(MI, OpNo+1, O);
195 O << ')';
196}
197
198void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
199 raw_ostream &O) {
200 if (MI->getOperand(OpNo).isImm())
201 printS16X4ImmOperand(MI, OpNo, O);
202 else
203 printSymbolLo(MI, OpNo, O);
204 O << '(';
205
Chris Lattner0fe71842010-11-15 03:51:13 +0000206 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
Chris Lattnerfdb2ded2010-11-14 20:22:56 +0000207 O << "0";
208 else
209 printOperand(MI, OpNo+1, O);
210 O << ')';
211}
212
213
214void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
215 raw_ostream &O) {
216 // When used as the base register, r0 reads constant zero rather than
217 // the value contained in the register. For this reason, the darwin
218 // assembler requires that we print r0 as 0 (no r) when used as the base.
219 if (MI->getOperand(OpNo).getReg() == PPC::R0)
220 O << "0";
221 else
222 printOperand(MI, OpNo, O);
223 O << ", ";
224 printOperand(MI, OpNo+1, O);
225}
226
227
Chris Lattner99889132010-11-14 20:11:21 +0000228
Chris Lattner0d1b7d92010-11-14 20:02:39 +0000229/// stripRegisterPrefix - This method strips the character prefix from a
230/// register name so that only the number is left. Used by for linux asm.
231const char *stripRegisterPrefix(const char *RegName) {
232 switch (RegName[0]) {
233 case 'r':
234 case 'f':
235 case 'v': return RegName + 1;
236 case 'c': if (RegName[1] == 'r') return RegName + 2;
237 }
238
239 return RegName;
240}
241
242void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
243 raw_ostream &O) {
244 const MCOperand &Op = MI->getOperand(OpNo);
245 if (Op.isReg()) {
246 const char *RegName = getRegisterName(Op.getReg());
247 // The linux and AIX assembler does not take register prefixes.
248 if (!isDarwinSyntax())
249 RegName = stripRegisterPrefix(RegName);
250
251 O << RegName;
252 return;
253 }
254
255 if (Op.isImm()) {
256 O << Op.getImm();
257 return;
258 }
259
260 assert(Op.isExpr() && "unknown operand kind in printOperand");
261 O << *Op.getExpr();
262}
263
Chris Lattner58d014f2010-11-14 21:33:07 +0000264void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
265 raw_ostream &O) {
266 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000267 return printS16ImmOperand(MI, OpNo, O);
268
269 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
270 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
271 if (MI->getOperand(OpNo).isExpr() &&
272 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
273 O << "lo16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000274 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000275 O << ')';
276 } else {
277 printOperand(MI, OpNo, O);
278 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000279}
280
281void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
282 raw_ostream &O) {
283 if (MI->getOperand(OpNo).isImm())
Chris Lattner1e61e692010-11-15 02:46:57 +0000284 return printS16ImmOperand(MI, OpNo, O);
285
286 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
287 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
288 if (MI->getOperand(OpNo).isExpr() &&
289 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
290 O << "ha16(";
Chris Lattner58d014f2010-11-14 21:33:07 +0000291 printOperand(MI, OpNo, O);
Chris Lattner1e61e692010-11-15 02:46:57 +0000292 O << ')';
293 } else {
294 printOperand(MI, OpNo, O);
295 }
Chris Lattner58d014f2010-11-14 21:33:07 +0000296}
Chris Lattner959fb3d2010-11-14 21:54:34 +0000297
298