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 #include "PPCInstPrinter.h"
15 #include "MCTargetDesc/PPCMCTargetDesc.h"
16 #include "MCTargetDesc/PPCPredicates.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Target/TargetOpcodes.h"
26 #define DEBUG_TYPE "asm-printer"
28 // FIXME: Once the integrated assembler supports full register names, tie this
29 // to the verbose-asm setting.
31 FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
32 cl::desc("Use full register names when printing assembly"));
34 #include "PPCGenAsmWriter.inc"
36 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
37 OS << getRegisterName(RegNo);
40 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
42 // Check for slwi/srwi mnemonics.
43 if (MI->getOpcode() == PPC::RLWINM) {
44 unsigned char SH = MI->getOperand(2).getImm();
45 unsigned char MB = MI->getOperand(3).getImm();
46 unsigned char ME = MI->getOperand(4).getImm();
47 bool useSubstituteMnemonic = false;
48 if (SH <= 31 && MB == 0 && ME == (31-SH)) {
49 O << "\tslwi "; useSubstituteMnemonic = true;
51 if (SH <= 31 && MB == (32-SH) && ME == 31) {
52 O << "\tsrwi "; useSubstituteMnemonic = true;
55 if (useSubstituteMnemonic) {
56 printOperand(MI, 0, O);
58 printOperand(MI, 1, O);
59 O << ", " << (unsigned int)SH;
61 printAnnotation(O, Annot);
66 if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
67 MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
69 printOperand(MI, 0, O);
71 printOperand(MI, 1, O);
72 printAnnotation(O, Annot);
76 if (MI->getOpcode() == PPC::RLDICR) {
77 unsigned char SH = MI->getOperand(2).getImm();
78 unsigned char ME = MI->getOperand(3).getImm();
79 // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
82 printOperand(MI, 0, O);
84 printOperand(MI, 1, O);
85 O << ", " << (unsigned int)SH;
86 printAnnotation(O, Annot);
91 // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is
92 // used when converting a 32-bit float to a 64-bit float as part of
93 // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
94 // as otherwise we have problems with incorrect register classes
95 // in machine instruction verification. For now, just avoid trying
96 // to print it as such an instruction has no effect (a 32-bit float
97 // in a register is already in 64-bit form, just with lower
98 // precision). FIXME: Is there a better solution?
99 if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
102 printInstruction(MI, O);
103 printAnnotation(O, Annot);
107 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
109 const char *Modifier) {
110 unsigned Code = MI->getOperand(OpNo).getImm();
112 if (StringRef(Modifier) == "cc") {
113 switch ((PPC::Predicate)Code) {
114 case PPC::PRED_LT_MINUS:
115 case PPC::PRED_LT_PLUS:
119 case PPC::PRED_LE_MINUS:
120 case PPC::PRED_LE_PLUS:
124 case PPC::PRED_EQ_MINUS:
125 case PPC::PRED_EQ_PLUS:
129 case PPC::PRED_GE_MINUS:
130 case PPC::PRED_GE_PLUS:
134 case PPC::PRED_GT_MINUS:
135 case PPC::PRED_GT_PLUS:
139 case PPC::PRED_NE_MINUS:
140 case PPC::PRED_NE_PLUS:
144 case PPC::PRED_UN_MINUS:
145 case PPC::PRED_UN_PLUS:
149 case PPC::PRED_NU_MINUS:
150 case PPC::PRED_NU_PLUS:
154 case PPC::PRED_BIT_SET:
155 case PPC::PRED_BIT_UNSET:
156 llvm_unreachable("Invalid use of bit predicate code");
158 llvm_unreachable("Invalid predicate code");
161 if (StringRef(Modifier) == "pm") {
162 switch ((PPC::Predicate)Code) {
172 case PPC::PRED_LT_MINUS:
173 case PPC::PRED_LE_MINUS:
174 case PPC::PRED_EQ_MINUS:
175 case PPC::PRED_GE_MINUS:
176 case PPC::PRED_GT_MINUS:
177 case PPC::PRED_NE_MINUS:
178 case PPC::PRED_UN_MINUS:
179 case PPC::PRED_NU_MINUS:
182 case PPC::PRED_LT_PLUS:
183 case PPC::PRED_LE_PLUS:
184 case PPC::PRED_EQ_PLUS:
185 case PPC::PRED_GE_PLUS:
186 case PPC::PRED_GT_PLUS:
187 case PPC::PRED_NE_PLUS:
188 case PPC::PRED_UN_PLUS:
189 case PPC::PRED_NU_PLUS:
192 case PPC::PRED_BIT_SET:
193 case PPC::PRED_BIT_UNSET:
194 llvm_unreachable("Invalid use of bit predicate code");
196 llvm_unreachable("Invalid predicate code");
199 assert(StringRef(Modifier) == "reg" &&
200 "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
201 printOperand(MI, OpNo+1, O);
204 void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
206 unsigned int Value = MI->getOperand(OpNo).getImm();
207 assert(Value <= 3 && "Invalid u2imm argument!");
208 O << (unsigned int)Value;
211 void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
213 unsigned int Value = MI->getOperand(OpNo).getImm();
214 assert(Value <= 15 && "Invalid u4imm argument!");
215 O << (unsigned int)Value;
218 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
220 int Value = MI->getOperand(OpNo).getImm();
221 Value = SignExtend32<5>(Value);
225 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
227 unsigned int Value = MI->getOperand(OpNo).getImm();
228 assert(Value <= 31 && "Invalid u5imm argument!");
229 O << (unsigned int)Value;
232 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
234 unsigned int Value = MI->getOperand(OpNo).getImm();
235 assert(Value <= 63 && "Invalid u6imm argument!");
236 O << (unsigned int)Value;
239 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
241 if (MI->getOperand(OpNo).isImm())
242 O << (short)MI->getOperand(OpNo).getImm();
244 printOperand(MI, OpNo, O);
247 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
249 if (MI->getOperand(OpNo).isImm())
250 O << (unsigned short)MI->getOperand(OpNo).getImm();
252 printOperand(MI, OpNo, O);
255 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
257 if (!MI->getOperand(OpNo).isImm())
258 return printOperand(MI, OpNo, O);
260 // Branches can take an immediate operand. This is used by the branch
261 // selection pass to print .+8, an eight byte displacement from the PC.
263 printAbsBranchOperand(MI, OpNo, O);
266 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
268 if (!MI->getOperand(OpNo).isImm())
269 return printOperand(MI, OpNo, O);
271 O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
275 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
277 unsigned CCReg = MI->getOperand(OpNo).getReg();
280 default: llvm_unreachable("Unknown CR register");
281 case PPC::CR0: RegNo = 0; break;
282 case PPC::CR1: RegNo = 1; break;
283 case PPC::CR2: RegNo = 2; break;
284 case PPC::CR3: RegNo = 3; break;
285 case PPC::CR4: RegNo = 4; break;
286 case PPC::CR5: RegNo = 5; break;
287 case PPC::CR6: RegNo = 6; break;
288 case PPC::CR7: RegNo = 7; break;
290 O << (0x80 >> RegNo);
293 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
295 printS16ImmOperand(MI, OpNo, O);
297 if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
300 printOperand(MI, OpNo+1, O);
304 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
306 // When used as the base register, r0 reads constant zero rather than
307 // the value contained in the register. For this reason, the darwin
308 // assembler requires that we print r0 as 0 (no r) when used as the base.
309 if (MI->getOperand(OpNo).getReg() == PPC::R0)
312 printOperand(MI, OpNo, O);
314 printOperand(MI, OpNo+1, O);
317 void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
319 // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
320 // come at the _end_ of the expression.
321 const MCOperand &Op = MI->getOperand(OpNo);
322 const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
323 O << refExp.getSymbol().getName();
325 printOperand(MI, OpNo+1, O);
327 if (refExp.getKind() != MCSymbolRefExpr::VK_None)
328 O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
332 /// stripRegisterPrefix - This method strips the character prefix from a
333 /// register name so that only the number is left. Used by for linux asm.
334 static const char *stripRegisterPrefix(const char *RegName) {
338 switch (RegName[0]) {
342 if (RegName[1] == 's')
345 case 'c': if (RegName[1] == 'r') return RegName + 2;
351 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
353 const MCOperand &Op = MI->getOperand(OpNo);
355 const char *RegName = getRegisterName(Op.getReg());
356 // The linux and AIX assembler does not take register prefixes.
357 if (!isDarwinSyntax())
358 RegName = stripRegisterPrefix(RegName);
369 assert(Op.isExpr() && "unknown operand kind in printOperand");