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 "MCTargetDesc/PPCMCTargetDesc.h"
17 #include "MCTargetDesc/PPCPredicates.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/Support/raw_ostream.h"
24 #include "PPCGenAsmWriter.inc"
26 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
27 OS << getRegisterName(RegNo);
30 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
32 // 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;
41 if (SH <= 31 && MB == (32-SH) && ME == 31) {
42 O << "\tsrwi "; useSubstituteMnemonic = true;
45 if (useSubstituteMnemonic) {
46 printOperand(MI, 0, O);
48 printOperand(MI, 1, O);
49 O << ", " << (unsigned int)SH;
51 printAnnotation(O, Annot);
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);
62 printAnnotation(O, Annot);
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
72 printOperand(MI, 0, O);
74 printOperand(MI, 1, O);
75 O << ", " << (unsigned int)SH;
76 printAnnotation(O, Annot);
81 printInstruction(MI, O);
82 printAnnotation(O, Annot);
86 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
88 const char *Modifier) {
89 unsigned Code = MI->getOperand(OpNo).getImm();
91 if (StringRef(Modifier) == "cc") {
92 switch ((PPC::Predicate)Code) {
93 case PPC::PRED_LT: O << "lt"; return;
94 case PPC::PRED_LE: O << "le"; return;
95 case PPC::PRED_EQ: O << "eq"; return;
96 case PPC::PRED_GE: O << "ge"; return;
97 case PPC::PRED_GT: O << "gt"; return;
98 case PPC::PRED_NE: O << "ne"; return;
99 case PPC::PRED_UN: O << "un"; return;
100 case PPC::PRED_NU: O << "nu"; return;
104 assert(StringRef(Modifier) == "reg" &&
105 "Need to specify 'cc' or 'reg' as predicate op modifier!");
106 printOperand(MI, OpNo+1, O);
109 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
111 int Value = MI->getOperand(OpNo).getImm();
112 Value = SignExtend32<5>(Value);
116 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
118 unsigned int Value = MI->getOperand(OpNo).getImm();
119 assert(Value <= 31 && "Invalid u5imm argument!");
120 O << (unsigned int)Value;
123 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
125 unsigned int Value = MI->getOperand(OpNo).getImm();
126 assert(Value <= 63 && "Invalid u6imm argument!");
127 O << (unsigned int)Value;
130 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
132 O << (short)MI->getOperand(OpNo).getImm();
135 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
137 O << (unsigned short)MI->getOperand(OpNo).getImm();
140 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
142 if (!MI->getOperand(OpNo).isImm())
143 return printOperand(MI, OpNo, O);
145 // Branches can take an immediate operand. This is used by the branch
146 // selection pass to print .+8, an eight byte displacement from the PC.
148 printAbsAddrOperand(MI, OpNo, O);
151 void PPCInstPrinter::printAbsAddrOperand(const MCInst *MI, unsigned OpNo,
153 O << (int)MI->getOperand(OpNo).getImm()*4;
157 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
159 unsigned CCReg = MI->getOperand(OpNo).getReg();
162 default: llvm_unreachable("Unknown CR register");
163 case PPC::CR0: RegNo = 0; break;
164 case PPC::CR1: RegNo = 1; break;
165 case PPC::CR2: RegNo = 2; break;
166 case PPC::CR3: RegNo = 3; break;
167 case PPC::CR4: RegNo = 4; break;
168 case PPC::CR5: RegNo = 5; break;
169 case PPC::CR6: RegNo = 6; break;
170 case PPC::CR7: RegNo = 7; break;
172 O << (0x80 >> RegNo);
175 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
177 printSymbolLo(MI, OpNo, O);
179 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
182 printOperand(MI, OpNo+1, O);
186 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
188 // When used as the base register, r0 reads constant zero rather than
189 // the value contained in the register. For this reason, the darwin
190 // assembler requires that we print r0 as 0 (no r) when used as the base.
191 if (MI->getOperand(OpNo).getReg() == PPC::R0)
194 printOperand(MI, OpNo, O);
196 printOperand(MI, OpNo+1, O);
201 /// stripRegisterPrefix - This method strips the character prefix from a
202 /// register name so that only the number is left. Used by for linux asm.
203 static const char *stripRegisterPrefix(const char *RegName) {
204 switch (RegName[0]) {
207 case 'v': return RegName + 1;
208 case 'c': if (RegName[1] == 'r') return RegName + 2;
214 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
216 const MCOperand &Op = MI->getOperand(OpNo);
218 const char *RegName = getRegisterName(Op.getReg());
219 // The linux and AIX assembler does not take register prefixes.
220 if (!isDarwinSyntax())
221 RegName = stripRegisterPrefix(RegName);
232 assert(Op.isExpr() && "unknown operand kind in printOperand");
236 void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
238 if (MI->getOperand(OpNo).isImm())
239 return printS16ImmOperand(MI, OpNo, O);
241 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
242 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
243 if (MI->getOperand(OpNo).isExpr() &&
244 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
246 printOperand(MI, OpNo, O);
249 printOperand(MI, OpNo, O);
253 void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
255 if (MI->getOperand(OpNo).isImm())
256 return printS16ImmOperand(MI, OpNo, O);
258 // FIXME: This is a terrible hack because we can't encode lo16() as an operand
259 // flag of a subtraction. See the FIXME in GetSymbolRef in PPCMCInstLower.
260 if (MI->getOperand(OpNo).isExpr() &&
261 isa<MCBinaryExpr>(MI->getOperand(OpNo).getExpr())) {
263 printOperand(MI, OpNo, O);
266 printOperand(MI, OpNo, O);