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/Support/raw_ostream.h"
22 #define GET_INSTRUCTION_NAME
23 #include "PPCGenAsmWriter.inc"
25 StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
26 return getInstructionName(Opcode);
29 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
30 OS << getRegisterName(RegNo);
33 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
34 // Check for slwi/srwi mnemonics.
35 if (MI->getOpcode() == PPC::RLWINM) {
36 unsigned char SH = MI->getOperand(2).getImm();
37 unsigned char MB = MI->getOperand(3).getImm();
38 unsigned char ME = MI->getOperand(4).getImm();
39 bool useSubstituteMnemonic = false;
40 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
41 O << "\tslwi "; useSubstituteMnemonic = true;
43 if (SH <= 31 && MB == (32-SH) && ME == 31) {
44 O << "\tsrwi "; useSubstituteMnemonic = true;
47 if (useSubstituteMnemonic) {
48 printOperand(MI, 0, O);
50 printOperand(MI, 1, O);
51 O << ", " << (unsigned int)SH;
56 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
57 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
59 printOperand(MI, 0, O);
61 printOperand(MI, 1, O);
65 if (MI->getOpcode() == PPC::RLDICR) {
66 unsigned char SH = MI->getOperand(2).getImm();
67 unsigned char ME = MI->getOperand(3).getImm();
68 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
71 printOperand(MI, 0, O);
73 printOperand(MI, 1, O);
74 O << ", " << (unsigned int)SH;
79 printInstruction(MI, O);
83 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
85 const char *Modifier) {
86 assert(Modifier && "Must specify 'cc' or 'reg' as predicate op modifier!");
87 unsigned Code = MI->getOperand(OpNo).getImm();
88 if (StringRef(Modifier) == "cc") {
89 switch ((PPC::Predicate)Code) {
90 default: assert(0 && "Invalid predicate");
91 case PPC::PRED_ALWAYS: return; // Don't print anything for always.
92 case PPC::PRED_LT: O << "lt"; return;
93 case PPC::PRED_LE: O << "le"; return;
94 case PPC::PRED_EQ: O << "eq"; return;
95 case PPC::PRED_GE: O << "ge"; return;
96 case PPC::PRED_GT: O << "gt"; return;
97 case PPC::PRED_NE: O << "ne"; return;
98 case PPC::PRED_UN: O << "un"; return;
99 case PPC::PRED_NU: O << "nu"; return;
103 assert(StringRef(Modifier) == "reg" &&
104 "Need to specify 'cc' or 'reg' as predicate op modifier!");
105 // Don't print the register for 'always'.
106 if (Code == PPC::PRED_ALWAYS) return;
107 printOperand(MI, OpNo+1, O);
110 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
112 char Value = MI->getOperand(OpNo).getImm();
113 Value = (Value << (32-5)) >> (32-5);
117 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
119 unsigned char Value = MI->getOperand(OpNo).getImm();
120 assert(Value <= 31 && "Invalid u5imm argument!");
121 O << (unsigned int)Value;
124 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
126 unsigned char Value = MI->getOperand(OpNo).getImm();
127 assert(Value <= 63 && "Invalid u6imm argument!");
128 O << (unsigned int)Value;
131 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
133 O << (short)MI->getOperand(OpNo).getImm();
136 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
138 O << (unsigned short)MI->getOperand(OpNo).getImm();
141 void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
143 if (MI->getOperand(OpNo).isImm())
144 O << (short)(MI->getOperand(OpNo).getImm()*4);
146 printOperand(MI, OpNo, O);
149 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
151 if (!MI->getOperand(OpNo).isImm())
152 return printOperand(MI, OpNo, O);
154 // Branches can take an immediate operand. This is used by the branch
155 // selection pass to print $+8, an eight byte displacement from the PC.
157 printAbsAddrOperand(MI, OpNo, O);
160 void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
162 O << (int)MI->getOperand(OpNo).getImm()*4;
166 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
168 unsigned CCReg = MI->getOperand(OpNo).getReg();
171 default: assert(0 && "Unknown CR register");
172 case PPC::CR0: RegNo = 0; break;
173 case PPC::CR1: RegNo = 1; break;
174 case PPC::CR2: RegNo = 2; break;
175 case PPC::CR3: RegNo = 3; break;
176 case PPC::CR4: RegNo = 4; break;
177 case PPC::CR5: RegNo = 5; break;
178 case PPC::CR6: RegNo = 6; break;
179 case PPC::CR7: RegNo = 7; break;
181 O << (0x80 >> RegNo);
184 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
186 printSymbolLo(MI, OpNo, O);
188 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
191 printOperand(MI, OpNo+1, O);
195 void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
197 if (MI->getOperand(OpNo).isImm())
198 printS16X4ImmOperand(MI, OpNo, O);
200 printSymbolLo(MI, OpNo, O);
203 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
206 printOperand(MI, OpNo+1, O);
211 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
213 // When used as the base register, r0 reads constant zero rather than
214 // the value contained in the register. For this reason, the darwin
215 // assembler requires that we print r0 as 0 (no r) when used as the base.
216 if (MI->getOperand(OpNo).getReg() == PPC::R0)
219 printOperand(MI, OpNo, O);
221 printOperand(MI, OpNo+1, O);
226 /// stripRegisterPrefix - This method strips the character prefix from a
227 /// register name so that only the number is left. Used by for linux asm.
228 static const char *stripRegisterPrefix(const char *RegName) {
229 switch (RegName[0]) {
232 case 'v': return RegName + 1;
233 case 'c': if (RegName[1] == 'r') return RegName + 2;
239 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
241 const MCOperand &Op = MI->getOperand(OpNo);
243 const char *RegName = getRegisterName(Op.getReg());
244 // The linux and AIX assembler does not take register prefixes.
245 if (!isDarwinSyntax())
246 RegName = stripRegisterPrefix(RegName);
257 assert(Op.isExpr() && "unknown operand kind in printOperand");
261 void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
263 if (MI->getOperand(OpNo).isImm())
264 return printS16ImmOperand(MI, OpNo, O);
266 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
267 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
268 if (MI->getOperand(OpNo).isExpr() &&
269 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
271 printOperand(MI, OpNo, O);
274 printOperand(MI, OpNo, O);
278 void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
280 if (MI->getOperand(OpNo).isImm())
281 return printS16ImmOperand(MI, OpNo, O);
283 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
284 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
285 if (MI->getOperand(OpNo).isExpr() &&
286 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
288 printOperand(MI, OpNo, O);
291 printOperand(MI, OpNo, O);