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 "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 //#include "llvm/MC/MCAsmInfo.h"
19 //#include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include "PPCGenRegisterNames.inc"
23 #include "PPCGenInstrNames.inc"
26 #define GET_INSTRUCTION_NAME
27 #define PPCAsmPrinter PPCInstPrinter
28 #define MachineInstr MCInst
29 #include "PPCGenAsmWriter.inc"
31 StringRef PPCInstPrinter::getOpcodeName(unsigned Opcode) const {
32 return getInstructionName(Opcode);
36 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
39 // Check for slwi/srwi mnemonics.
40 if (MI->getOpcode() == PPC::RLWINM) {
41 unsigned char SH = MI->getOperand(2).getImm();
42 unsigned char MB = MI->getOperand(3).getImm();
43 unsigned char ME = MI->getOperand(4).getImm();
44 bool useSubstituteMnemonic = false;
45 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
46 O << "\tslwi "; useSubstituteMnemonic = true;
48 if (SH <= 31 && MB == (32-SH) && ME == 31) {
49 O << "\tsrwi "; useSubstituteMnemonic = true;
52 if (useSubstituteMnemonic) {
53 printOperand(MI, 0, O);
55 printOperand(MI, 1, O);
56 O << ", " << (unsigned int)SH;
61 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
62 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
64 printOperand(MI, 0, O);
66 printOperand(MI, 1, O);
70 if (MI->getOpcode() == PPC::RLDICR) {
71 unsigned char SH = MI->getOperand(2).getImm();
72 unsigned char ME = MI->getOperand(3).getImm();
73 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
76 printOperand(MI, 0, O);
78 printOperand(MI, 1, O);
79 O << ", " << (unsigned int)SH;
84 printInstruction(MI, O);
87 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
89 char Value = MI->getOperand(OpNo).getImm();
90 Value = (Value << (32-5)) >> (32-5);
94 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
96 unsigned char Value = MI->getOperand(OpNo).getImm();
97 assert(Value <= 31 && "Invalid u5imm argument!");
98 O << (unsigned int)Value;
101 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
103 unsigned char Value = MI->getOperand(OpNo).getImm();
104 assert(Value <= 63 && "Invalid u6imm argument!");
105 O << (unsigned int)Value;
108 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
110 O << (short)MI->getOperand(OpNo).getImm();
113 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
115 O << (unsigned short)MI->getOperand(OpNo).getImm();
118 void PPCInstPrinter::printS16X4ImmOperand(const MCInst *MI, unsigned OpNo,
120 if (MI->getOperand(OpNo).isImm()) {
121 O << (short)(MI->getOperand(OpNo).getImm()*4);
125 assert(0 && "Unhandled operand");
128 printOp(MI->getOperand(OpNo), O);
129 if (TM.getRelocationModel() == Reloc::PIC_)
130 O << "-\"L" << getFunctionNumber() << "$pb\")";
136 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
138 if (!MI->getOperand(OpNo).isImm())
139 return printOperand(MI, OpNo, O);
141 // Branches can take an immediate operand. This is used by the branch
142 // selection pass to print $+8, an eight byte displacement from the PC.
143 O << "$+" << MI->getOperand(OpNo).getImm()*4;
149 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
151 unsigned CCReg = MI->getOperand(OpNo).getReg();
154 default: assert(0 && "Unknown CR register");
155 case PPC::CR0: RegNo = 0; break;
156 case PPC::CR1: RegNo = 1; break;
157 case PPC::CR2: RegNo = 2; break;
158 case PPC::CR3: RegNo = 3; break;
159 case PPC::CR4: RegNo = 4; break;
160 case PPC::CR5: RegNo = 5; break;
161 case PPC::CR6: RegNo = 6; break;
162 case PPC::CR7: RegNo = 7; break;
164 O << (0x80 >> RegNo);
167 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
169 printSymbolLo(MI, OpNo, O);
171 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
173 if (MI->getOperand(OpNo+1).isReg() &&
174 MI->getOperand(OpNo+1).getReg() == PPC::R0)
177 printOperand(MI, OpNo+1, O);
181 void PPCInstPrinter::printMemRegImmShifted(const MCInst *MI, unsigned OpNo,
183 if (MI->getOperand(OpNo).isImm())
184 printS16X4ImmOperand(MI, OpNo, O);
186 printSymbolLo(MI, OpNo, O);
189 assert(MI->getOperand(OpNo+1).isReg() && "Bad operand");
191 if (MI->getOperand(OpNo+1).isReg() &&
192 MI->getOperand(OpNo+1).getReg() == PPC::R0)
195 printOperand(MI, OpNo+1, O);
200 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
202 // When used as the base register, r0 reads constant zero rather than
203 // the value contained in the register. For this reason, the darwin
204 // assembler requires that we print r0 as 0 (no r) when used as the base.
205 if (MI->getOperand(OpNo).getReg() == PPC::R0)
208 printOperand(MI, OpNo, O);
210 printOperand(MI, OpNo+1, O);
215 /// stripRegisterPrefix - This method strips the character prefix from a
216 /// register name so that only the number is left. Used by for linux asm.
217 const char *stripRegisterPrefix(const char *RegName) {
218 switch (RegName[0]) {
221 case 'v': return RegName + 1;
222 case 'c': if (RegName[1] == 'r') return RegName + 2;
228 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
230 const MCOperand &Op = MI->getOperand(OpNo);
232 const char *RegName = getRegisterName(Op.getReg());
233 // The linux and AIX assembler does not take register prefixes.
234 if (!isDarwinSyntax())
235 RegName = stripRegisterPrefix(RegName);
246 assert(Op.isExpr() && "unknown operand kind in printOperand");
250 void PPCInstPrinter::printSymbolLo(const MCInst *MI, unsigned OpNo,
252 if (MI->getOperand(OpNo).isImm())
253 printS16ImmOperand(MI, OpNo, O);
255 printOperand(MI, OpNo, O);
258 void PPCInstPrinter::printSymbolHi(const MCInst *MI, unsigned OpNo,
260 if (MI->getOperand(OpNo).isImm())
261 printS16ImmOperand(MI, OpNo, O);
263 printOperand(MI, OpNo, O);