LLVM API Documentation

PPCInstPrinter.cpp
Go to the documentation of this file.
00001 //===-- PPCInstPrinter.cpp - Convert PPC MCInst to assembly syntax --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class prints an PPC MCInst to a .s file.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPCInstPrinter.h"
00015 #include "MCTargetDesc/PPCMCTargetDesc.h"
00016 #include "MCTargetDesc/PPCPredicates.h"
00017 #include "llvm/MC/MCExpr.h"
00018 #include "llvm/MC/MCInst.h"
00019 #include "llvm/MC/MCInstrInfo.h"
00020 #include "llvm/MC/MCSymbol.h"
00021 #include "llvm/Support/CommandLine.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include "llvm/Target/TargetOpcodes.h"
00024 using namespace llvm;
00025 
00026 #define DEBUG_TYPE "asm-printer"
00027 
00028 // FIXME: Once the integrated assembler supports full register names, tie this
00029 // to the verbose-asm setting.
00030 static cl::opt<bool>
00031 FullRegNames("ppc-asm-full-reg-names", cl::Hidden, cl::init(false),
00032              cl::desc("Use full register names when printing assembly"));
00033 
00034 #include "PPCGenAsmWriter.inc"
00035 
00036 void PPCInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
00037   OS << getRegisterName(RegNo);
00038 }
00039 
00040 void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
00041                                StringRef Annot) {
00042   // Check for slwi/srwi mnemonics.
00043   if (MI->getOpcode() == PPC::RLWINM) {
00044     unsigned char SH = MI->getOperand(2).getImm();
00045     unsigned char MB = MI->getOperand(3).getImm();
00046     unsigned char ME = MI->getOperand(4).getImm();
00047     bool useSubstituteMnemonic = false;
00048     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
00049       O << "\tslwi "; useSubstituteMnemonic = true;
00050     }
00051     if (SH <= 31 && MB == (32-SH) && ME == 31) {
00052       O << "\tsrwi "; useSubstituteMnemonic = true;
00053       SH = 32-SH;
00054     }
00055     if (useSubstituteMnemonic) {
00056       printOperand(MI, 0, O);
00057       O << ", ";
00058       printOperand(MI, 1, O);
00059       O << ", " << (unsigned int)SH;
00060 
00061       printAnnotation(O, Annot);
00062       return;
00063     }
00064   }
00065   
00066   if ((MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) &&
00067       MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
00068     O << "\tmr ";
00069     printOperand(MI, 0, O);
00070     O << ", ";
00071     printOperand(MI, 1, O);
00072     printAnnotation(O, Annot);
00073     return;
00074   }
00075   
00076   if (MI->getOpcode() == PPC::RLDICR) {
00077     unsigned char SH = MI->getOperand(2).getImm();
00078     unsigned char ME = MI->getOperand(3).getImm();
00079     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
00080     if (63-SH == ME) {
00081       O << "\tsldi ";
00082       printOperand(MI, 0, O);
00083       O << ", ";
00084       printOperand(MI, 1, O);
00085       O << ", " << (unsigned int)SH;
00086       printAnnotation(O, Annot);
00087       return;
00088     }
00089   }
00090   
00091   // For fast-isel, a COPY_TO_REGCLASS may survive this long.  This is
00092   // used when converting a 32-bit float to a 64-bit float as part of
00093   // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()),
00094   // as otherwise we have problems with incorrect register classes
00095   // in machine instruction verification.  For now, just avoid trying
00096   // to print it as such an instruction has no effect (a 32-bit float
00097   // in a register is already in 64-bit form, just with lower
00098   // precision).  FIXME: Is there a better solution?
00099   if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS)
00100     return;
00101   
00102   printInstruction(MI, O);
00103   printAnnotation(O, Annot);
00104 }
00105 
00106 
00107 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
00108                                            raw_ostream &O, 
00109                                            const char *Modifier) {
00110   unsigned Code = MI->getOperand(OpNo).getImm();
00111 
00112   if (StringRef(Modifier) == "cc") {
00113     switch ((PPC::Predicate)Code) {
00114     case PPC::PRED_LT_MINUS:
00115     case PPC::PRED_LT_PLUS:
00116     case PPC::PRED_LT:
00117       O << "lt";
00118       return;
00119     case PPC::PRED_LE_MINUS:
00120     case PPC::PRED_LE_PLUS:
00121     case PPC::PRED_LE:
00122       O << "le";
00123       return;
00124     case PPC::PRED_EQ_MINUS:
00125     case PPC::PRED_EQ_PLUS:
00126     case PPC::PRED_EQ:
00127       O << "eq";
00128       return;
00129     case PPC::PRED_GE_MINUS:
00130     case PPC::PRED_GE_PLUS:
00131     case PPC::PRED_GE:
00132       O << "ge";
00133       return;
00134     case PPC::PRED_GT_MINUS:
00135     case PPC::PRED_GT_PLUS:
00136     case PPC::PRED_GT:
00137       O << "gt";
00138       return;
00139     case PPC::PRED_NE_MINUS:
00140     case PPC::PRED_NE_PLUS:
00141     case PPC::PRED_NE:
00142       O << "ne";
00143       return;
00144     case PPC::PRED_UN_MINUS:
00145     case PPC::PRED_UN_PLUS:
00146     case PPC::PRED_UN:
00147       O << "un";
00148       return;
00149     case PPC::PRED_NU_MINUS:
00150     case PPC::PRED_NU_PLUS:
00151     case PPC::PRED_NU:
00152       O << "nu";
00153       return;
00154     case PPC::PRED_BIT_SET:
00155     case PPC::PRED_BIT_UNSET:
00156       llvm_unreachable("Invalid use of bit predicate code");
00157     }
00158     llvm_unreachable("Invalid predicate code");
00159   }
00160 
00161   if (StringRef(Modifier) == "pm") {
00162     switch ((PPC::Predicate)Code) {
00163     case PPC::PRED_LT:
00164     case PPC::PRED_LE:
00165     case PPC::PRED_EQ:
00166     case PPC::PRED_GE:
00167     case PPC::PRED_GT:
00168     case PPC::PRED_NE:
00169     case PPC::PRED_UN:
00170     case PPC::PRED_NU:
00171       return;
00172     case PPC::PRED_LT_MINUS:
00173     case PPC::PRED_LE_MINUS:
00174     case PPC::PRED_EQ_MINUS:
00175     case PPC::PRED_GE_MINUS:
00176     case PPC::PRED_GT_MINUS:
00177     case PPC::PRED_NE_MINUS:
00178     case PPC::PRED_UN_MINUS:
00179     case PPC::PRED_NU_MINUS:
00180       O << "-";
00181       return;
00182     case PPC::PRED_LT_PLUS:
00183     case PPC::PRED_LE_PLUS:
00184     case PPC::PRED_EQ_PLUS:
00185     case PPC::PRED_GE_PLUS:
00186     case PPC::PRED_GT_PLUS:
00187     case PPC::PRED_NE_PLUS:
00188     case PPC::PRED_UN_PLUS:
00189     case PPC::PRED_NU_PLUS:
00190       O << "+";
00191       return;
00192     case PPC::PRED_BIT_SET:
00193     case PPC::PRED_BIT_UNSET:
00194       llvm_unreachable("Invalid use of bit predicate code");
00195     }
00196     llvm_unreachable("Invalid predicate code");
00197   }
00198   
00199   assert(StringRef(Modifier) == "reg" &&
00200          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
00201   printOperand(MI, OpNo+1, O);
00202 }
00203 
00204 void PPCInstPrinter::printU2ImmOperand(const MCInst *MI, unsigned OpNo,
00205                                        raw_ostream &O) {
00206   unsigned int Value = MI->getOperand(OpNo).getImm();
00207   assert(Value <= 3 && "Invalid u2imm argument!");
00208   O << (unsigned int)Value;
00209 }
00210 
00211 void PPCInstPrinter::printU4ImmOperand(const MCInst *MI, unsigned OpNo,
00212                                        raw_ostream &O) {
00213   unsigned int Value = MI->getOperand(OpNo).getImm();
00214   assert(Value <= 15 && "Invalid u4imm argument!");
00215   O << (unsigned int)Value;
00216 }
00217 
00218 void PPCInstPrinter::printS5ImmOperand(const MCInst *MI, unsigned OpNo,
00219                                        raw_ostream &O) {
00220   int Value = MI->getOperand(OpNo).getImm();
00221   Value = SignExtend32<5>(Value);
00222   O << (int)Value;
00223 }
00224 
00225 void PPCInstPrinter::printU5ImmOperand(const MCInst *MI, unsigned OpNo,
00226                                        raw_ostream &O) {
00227   unsigned int Value = MI->getOperand(OpNo).getImm();
00228   assert(Value <= 31 && "Invalid u5imm argument!");
00229   O << (unsigned int)Value;
00230 }
00231 
00232 void PPCInstPrinter::printU6ImmOperand(const MCInst *MI, unsigned OpNo,
00233                                        raw_ostream &O) {
00234   unsigned int Value = MI->getOperand(OpNo).getImm();
00235   assert(Value <= 63 && "Invalid u6imm argument!");
00236   O << (unsigned int)Value;
00237 }
00238 
00239 void PPCInstPrinter::printS16ImmOperand(const MCInst *MI, unsigned OpNo,
00240                                         raw_ostream &O) {
00241   if (MI->getOperand(OpNo).isImm())
00242     O << (short)MI->getOperand(OpNo).getImm();
00243   else
00244     printOperand(MI, OpNo, O);
00245 }
00246 
00247 void PPCInstPrinter::printU16ImmOperand(const MCInst *MI, unsigned OpNo,
00248                                         raw_ostream &O) {
00249   if (MI->getOperand(OpNo).isImm())
00250     O << (unsigned short)MI->getOperand(OpNo).getImm();
00251   else
00252     printOperand(MI, OpNo, O);
00253 }
00254 
00255 void PPCInstPrinter::printBranchOperand(const MCInst *MI, unsigned OpNo,
00256                                         raw_ostream &O) {
00257   if (!MI->getOperand(OpNo).isImm())
00258     return printOperand(MI, OpNo, O);
00259 
00260   // Branches can take an immediate operand.  This is used by the branch
00261   // selection pass to print .+8, an eight byte displacement from the PC.
00262   O << ".+";
00263   printAbsBranchOperand(MI, OpNo, O);
00264 }
00265 
00266 void PPCInstPrinter::printAbsBranchOperand(const MCInst *MI, unsigned OpNo,
00267                                            raw_ostream &O) {
00268   if (!MI->getOperand(OpNo).isImm())
00269     return printOperand(MI, OpNo, O);
00270 
00271   O << SignExtend32<32>((unsigned)MI->getOperand(OpNo).getImm() << 2);
00272 }
00273 
00274 
00275 void PPCInstPrinter::printcrbitm(const MCInst *MI, unsigned OpNo,
00276                                  raw_ostream &O) {
00277   unsigned CCReg = MI->getOperand(OpNo).getReg();
00278   unsigned RegNo;
00279   switch (CCReg) {
00280   default: llvm_unreachable("Unknown CR register");
00281   case PPC::CR0: RegNo = 0; break;
00282   case PPC::CR1: RegNo = 1; break;
00283   case PPC::CR2: RegNo = 2; break;
00284   case PPC::CR3: RegNo = 3; break;
00285   case PPC::CR4: RegNo = 4; break;
00286   case PPC::CR5: RegNo = 5; break;
00287   case PPC::CR6: RegNo = 6; break;
00288   case PPC::CR7: RegNo = 7; break;
00289   }
00290   O << (0x80 >> RegNo);
00291 }
00292 
00293 void PPCInstPrinter::printMemRegImm(const MCInst *MI, unsigned OpNo,
00294                                     raw_ostream &O) {
00295   printS16ImmOperand(MI, OpNo, O);
00296   O << '(';
00297   if (MI->getOperand(OpNo+1).getReg() == PPC::R0)
00298     O << "0";
00299   else
00300     printOperand(MI, OpNo+1, O);
00301   O << ')';
00302 }
00303 
00304 void PPCInstPrinter::printMemRegReg(const MCInst *MI, unsigned OpNo,
00305                                     raw_ostream &O) {
00306   // When used as the base register, r0 reads constant zero rather than
00307   // the value contained in the register.  For this reason, the darwin
00308   // assembler requires that we print r0 as 0 (no r) when used as the base.
00309   if (MI->getOperand(OpNo).getReg() == PPC::R0)
00310     O << "0";
00311   else
00312     printOperand(MI, OpNo, O);
00313   O << ", ";
00314   printOperand(MI, OpNo+1, O);
00315 }
00316 
00317 void PPCInstPrinter::printTLSCall(const MCInst *MI, unsigned OpNo,
00318                                   raw_ostream &O) {
00319   // On PPC64, VariantKind is VK_None, but on PPC32, it's VK_PLT, and it must
00320   // come at the _end_ of the expression.
00321   const MCOperand &Op = MI->getOperand(OpNo);
00322   const MCSymbolRefExpr &refExp = cast<MCSymbolRefExpr>(*Op.getExpr());
00323   O << refExp.getSymbol().getName();
00324   O << '(';
00325   printOperand(MI, OpNo+1, O);
00326   O << ')';
00327   if (refExp.getKind() != MCSymbolRefExpr::VK_None)
00328     O << '@' << MCSymbolRefExpr::getVariantKindName(refExp.getKind());
00329 }
00330 
00331 
00332 /// stripRegisterPrefix - This method strips the character prefix from a
00333 /// register name so that only the number is left.  Used by for linux asm.
00334 static const char *stripRegisterPrefix(const char *RegName) {
00335   if (FullRegNames)
00336     return RegName;
00337 
00338   switch (RegName[0]) {
00339   case 'r':
00340   case 'f':
00341   case 'v':
00342     if (RegName[1] == 's')
00343       return RegName + 2;
00344     return RegName + 1;
00345   case 'c': if (RegName[1] == 'r') return RegName + 2;
00346   }
00347   
00348   return RegName;
00349 }
00350 
00351 void PPCInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
00352                                   raw_ostream &O) {
00353   const MCOperand &Op = MI->getOperand(OpNo);
00354   if (Op.isReg()) {
00355     const char *RegName = getRegisterName(Op.getReg());
00356     // The linux and AIX assembler does not take register prefixes.
00357     if (!isDarwinSyntax())
00358       RegName = stripRegisterPrefix(RegName);
00359     
00360     O << RegName;
00361     return;
00362   }
00363   
00364   if (Op.isImm()) {
00365     O << Op.getImm();
00366     return;
00367   }
00368   
00369   assert(Op.isExpr() && "unknown operand kind in printOperand");
00370   O << *Op.getExpr();
00371 }
00372