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/CommandLine.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetOpcodes.h"
26 // FIXME: Once the integrated assembler supports full register names, tie this
27 // to the verbose-asm setting.
29 FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
30 cl::desc("Use full register names when printing assembly"));
32 #include "PPCGenAsmWriter.inc"
34 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
35 OS << getRegisterName(RegNo);
38 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
40 // Check for slwi/srwi mnemonics.
41 if (MI->getOpcode() == PPC::RLWINM) {
42 unsigned char SH = MI->getOperand(2).getImm();
43 unsigned char MB = MI->getOperand(3).getImm();
44 unsigned char ME = MI->getOperand(4).getImm();
45 bool useSubstituteMnemonic = false;
46 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
47 O << "\tslwi "; useSubstituteMnemonic = true;
49 if (SH <= 31 && MB == (32-SH) && ME == 31) {
50 O << "\tsrwi "; useSubstituteMnemonic = true;
53 if (useSubstituteMnemonic) {
54 printOperand(MI, 0, O);
56 printOperand(MI, 1, O);
57 O << ", " << (unsigned int)SH;
59 printAnnotation(O, Annot);
64 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
65 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
67 printOperand(MI, 0, O);
69 printOperand(MI, 1, O);
70 printAnnotation(O, Annot);
74 if (MI->getOpcode() == PPC::RLDICR) {
75 unsigned char SH = MI->getOperand(2).getImm();
76 unsigned char ME = MI->getOperand(3).getImm();
77 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
80 printOperand(MI, 0, O);
82 printOperand(MI, 1, O);
83 O << ", " << (unsigned int)SH;
84 printAnnotation(O, Annot);
89 // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is
90 // used when converting a 32-bit float to a 64-bit float as part of
91 // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
92 // as otherwise we have problems with incorrect register classes
93 // in machine instruction verification. For now, just avoid trying
94 // to print it as such an instruction has no effect (a 32-bit float
95 // in a register is already in 64-bit form, just with lower
96 // precision). FIXME: Is there a better solution?
97 if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
100 printInstruction(MI, O);
101 printAnnotation(O, Annot);
105 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
107 const char *Modifier) {
108 unsigned Code = MI->getOperand(OpNo).getImm();
110 if (StringRef(Modifier) == "cc") {
111 switch ((PPC::Predicate)Code) {
112 case PPC::PRED_LT_MINUS:
113 case PPC::PRED_LT_PLUS:
117 case PPC::PRED_LE_MINUS:
118 case PPC::PRED_LE_PLUS:
122 case PPC::PRED_EQ_MINUS:
123 case PPC::PRED_EQ_PLUS:
127 case PPC::PRED_GE_MINUS:
128 case PPC::PRED_GE_PLUS:
132 case PPC::PRED_GT_MINUS:
133 case PPC::PRED_GT_PLUS:
137 case PPC::PRED_NE_MINUS:
138 case PPC::PRED_NE_PLUS:
142 case PPC::PRED_UN_MINUS:
143 case PPC::PRED_UN_PLUS:
147 case PPC::PRED_NU_MINUS:
148 case PPC::PRED_NU_PLUS:
152 case PPC::PRED_BIT_SET:
153 case PPC::PRED_BIT_UNSET:
154 llvm_unreachable("Invalid use of bit predicate code");
156 llvm_unreachable("Invalid predicate code");
159 if (StringRef(Modifier) == "pm") {
160 switch ((PPC::Predicate)Code) {
170 case PPC::PRED_LT_MINUS:
171 case PPC::PRED_LE_MINUS:
172 case PPC::PRED_EQ_MINUS:
173 case PPC::PRED_GE_MINUS:
174 case PPC::PRED_GT_MINUS:
175 case PPC::PRED_NE_MINUS:
176 case PPC::PRED_UN_MINUS:
177 case PPC::PRED_NU_MINUS:
180 case PPC::PRED_LT_PLUS:
181 case PPC::PRED_LE_PLUS:
182 case PPC::PRED_EQ_PLUS:
183 case PPC::PRED_GE_PLUS:
184 case PPC::PRED_GT_PLUS:
185 case PPC::PRED_NE_PLUS:
186 case PPC::PRED_UN_PLUS:
187 case PPC::PRED_NU_PLUS:
190 case PPC::PRED_BIT_SET:
191 case PPC::PRED_BIT_UNSET:
192 llvm_unreachable("Invalid use of bit predicate code");
194 llvm_unreachable("Invalid predicate code");
197 assert(StringRef(Modifier) == "reg" &&
198 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
199 printOperand(MI, OpNo+1, O);
202 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
204 int Value = MI->getOperand(OpNo).getImm();
205 Value = SignExtend32<5>(Value);
209 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
211 unsigned int Value = MI->getOperand(OpNo).getImm();
212 assert(Value <= 31 && "Invalid u5imm argument!");
213 O << (unsigned int)Value;
216 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
218 unsigned int Value = MI->getOperand(OpNo).getImm();
219 assert(Value <= 63 && "Invalid u6imm argument!");
220 O << (unsigned int)Value;
223 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
225 if (MI->getOperand(OpNo).isImm())
226 O << (short)MI->getOperand(OpNo).getImm();
228 printOperand(MI, OpNo, O);
231 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
233 if (MI->getOperand(OpNo).isImm())
234 O << (unsigned short)MI->getOperand(OpNo).getImm();
236 printOperand(MI, OpNo, O);
239 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
241 if (!MI->getOperand(OpNo).isImm())
242 return printOperand(MI, OpNo, O);
244 // Branches can take an immediate operand. This is used by the branch
245 // selection pass to print .+8, an eight byte displacement from the PC.
247 printAbsBranchOperand(MI, OpNo, O);
250 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
252 if (!MI->getOperand(OpNo).isImm())
253 return printOperand(MI, OpNo, O);
255 O << (int)MI->getOperand(OpNo).getImm()*4;
259 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
261 unsigned CCReg = MI->getOperand(OpNo).getReg();
264 default: llvm_unreachable("Unknown CR register");
265 case PPC::CR0: RegNo = 0; break;
266 case PPC::CR1: RegNo = 1; break;
267 case PPC::CR2: RegNo = 2; break;
268 case PPC::CR3: RegNo = 3; break;
269 case PPC::CR4: RegNo = 4; break;
270 case PPC::CR5: RegNo = 5; break;
271 case PPC::CR6: RegNo = 6; break;
272 case PPC::CR7: RegNo = 7; break;
274 O << (0x80 >> RegNo);
277 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
279 printS16ImmOperand(MI, OpNo, O);
281 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
284 printOperand(MI, OpNo+1, O);
288 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
290 // When used as the base register, r0 reads constant zero rather than
291 // the value contained in the register. For this reason, the darwin
292 // assembler requires that we print r0 as 0 (no r) when used as the base.
293 if (MI->getOperand(OpNo).getReg() == PPC::R0)
296 printOperand(MI, OpNo, O);
298 printOperand(MI, OpNo+1, O);
301 void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
303 printBranchOperand(MI, OpNo, O);
305 printOperand(MI, OpNo+1, O);
310 /// stripRegisterPrefix - This method strips the character prefix from a
311 /// register name so that only the number is left. Used by for linux asm.
312 static const char *stripRegisterPrefix(const char *RegName) {
316 switch (RegName[0]) {
319 case 'v': return RegName + 1;
320 case 'c': if (RegName[1] == 'r') return RegName + 2;
326 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
328 const MCOperand &Op = MI->getOperand(OpNo);
330 const char *RegName = getRegisterName(Op.getReg());
331 // The linux and AIX assembler does not take register prefixes.
332 if (!isDarwinSyntax())
333 RegName = stripRegisterPrefix(RegName);
344 assert(Op.isExpr() && "unknown operand kind in printOperand");