1 //===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class prints an PPC MCInst to a .s file.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "asm-printer"
15 #include "PPCInstPrinter.h"
16 #include "PPCPredicates.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 //#include "llvm/MC/MCAsmInfo.h"
20 //#include "llvm/ADT/StringExtras.h"
21 #include "llvm/Support/raw_ostream.h"
24 #define GET_INSTRUCTION_NAME
25 #define PPCAsmPrinter PPCInstPrinter
26 #define MachineInstr MCInst
27 #include "PPCGenAsmWriter.inc"
29 StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
30 return getInstructionName(Opcode);
34 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
37 // 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;
46 if (SH <= 31 && MB == (32-SH) && ME == 31) {
47 O << "\tsrwi "; useSubstituteMnemonic = true;
50 if (useSubstituteMnemonic) {
51 printOperand(MI, 0, O);
53 printOperand(MI, 1, O);
54 O << ", " << (unsigned int)SH;
59 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
60 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
62 printOperand(MI, 0, O);
64 printOperand(MI, 1, O);
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
74 printOperand(MI, 0, O);
76 printOperand(MI, 1, O);
77 O << ", " << (unsigned int)SH;
82 printInstruction(MI, O);
86 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
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;
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);
113 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
115 char Value = MI->getOperand(OpNo).getImm();
116 Value = (Value << (32-5)) >> (32-5);
120 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
122 unsigned char Value = MI->getOperand(OpNo).getImm();
123 assert(Value <= 31 && "Invalid u5imm argument!");
124 O << (unsigned int)Value;
127 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
129 unsigned char Value = MI->getOperand(OpNo).getImm();
130 assert(Value <= 63 && "Invalid u6imm argument!");
131 O << (unsigned int)Value;
134 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
136 O << (short)MI->getOperand(OpNo).getImm();
139 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
141 O << (unsigned short)MI->getOperand(OpNo).getImm();
144 void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
146 if (MI->getOperand(OpNo).isImm())
147 O << (short)(MI->getOperand(OpNo).getImm()*4);
149 printOperand(MI, OpNo, O);
152 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
154 if (!MI->getOperand(OpNo).isImm())
155 return printOperand(MI, OpNo, O);
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.
160 printAbsAddrOperand(MI, OpNo, O);
163 void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
165 O << (int)MI->getOperand(OpNo).getImm()*4;
169 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
171 unsigned CCReg = MI->getOperand(OpNo).getReg();
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;
184 O << (0x80 >> RegNo);
187 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
189 printSymbolLo(MI, OpNo, O);
191 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
193 if (MI->getOperand(OpNo+1).isReg() &&
194 MI->getOperand(OpNo+1).getReg() == PPC::R0)
197 printOperand(MI, OpNo+1, O);
201 void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
203 if (MI->getOperand(OpNo).isImm())
204 printS16X4ImmOperand(MI, OpNo, O);
206 printSymbolLo(MI, OpNo, O);
209 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
211 if (MI->getOperand(OpNo+1).isReg() &&
212 MI->getOperand(OpNo+1).getReg() == PPC::R0)
215 printOperand(MI, OpNo+1, O);
220 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
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)
228 printOperand(MI, OpNo, O);
230 printOperand(MI, OpNo+1, O);
235 /// stripRegisterPrefix - This method strips the character prefix from a
236 /// register name so that only the number is left. Used by for linux asm.
237 const char *stripRegisterPrefix(const char *RegName) {
238 switch (RegName[0]) {
241 case 'v': return RegName + 1;
242 case 'c': if (RegName[1] == 'r') return RegName + 2;
248 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
250 const MCOperand &Op = MI->getOperand(OpNo);
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);
266 assert(Op.isExpr() && "unknown operand kind in printOperand");
270 void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
272 if (MI->getOperand(OpNo).isImm())
273 return printS16ImmOperand(MI, OpNo, O);
275 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
276 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
277 if (MI->getOperand(OpNo).isExpr() &&
278 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
280 printOperand(MI, OpNo, O);
283 printOperand(MI, OpNo, O);
287 void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
289 if (MI->getOperand(OpNo).isImm())
290 return printS16ImmOperand(MI, OpNo, O);
292 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
293 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
294 if (MI->getOperand(OpNo).isExpr() &&
295 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
297 printOperand(MI, OpNo, O);
300 printOperand(MI, OpNo, O);
305 void PPCInstPrinter::PrintSpecial(const MCInst *MI, raw_ostream &O,
306 const char *Modifier) {
307 assert(0 && "FIXME: PrintSpecial should be dead");
309 void PPCInstPrinter::printPICLabel(const MCInst *MI, unsigned OpNo,
311 assert(0 && "FIXME: printPICLabel should be dead");
313 void PPCInstPrinter::printTOCEntryLabel(const MCInst *MI, unsigned OpNo,
315 assert(0 && "FIXME: printTOCEntryLabel should be dead");