LLVM API Documentation

PPCAsmParser.cpp
Go to the documentation of this file.
00001 //===-- PPCAsmParser.cpp - Parse PowerPC asm to MCInst instructions ---------===//
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 #include "MCTargetDesc/PPCMCTargetDesc.h"
00011 #include "MCTargetDesc/PPCMCExpr.h"
00012 #include "PPCTargetStreamer.h"
00013 #include "llvm/ADT/STLExtras.h"
00014 #include "llvm/ADT/SmallString.h"
00015 #include "llvm/ADT/SmallVector.h"
00016 #include "llvm/ADT/StringSwitch.h"
00017 #include "llvm/ADT/Twine.h"
00018 #include "llvm/MC/MCContext.h"
00019 #include "llvm/MC/MCExpr.h"
00020 #include "llvm/MC/MCInst.h"
00021 #include "llvm/MC/MCInstrInfo.h"
00022 #include "llvm/MC/MCParser/MCAsmLexer.h"
00023 #include "llvm/MC/MCParser/MCAsmParser.h"
00024 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
00025 #include "llvm/MC/MCRegisterInfo.h"
00026 #include "llvm/MC/MCStreamer.h"
00027 #include "llvm/MC/MCSubtargetInfo.h"
00028 #include "llvm/MC/MCTargetAsmParser.h"
00029 #include "llvm/Support/SourceMgr.h"
00030 #include "llvm/Support/TargetRegistry.h"
00031 #include "llvm/Support/raw_ostream.h"
00032 
00033 using namespace llvm;
00034 
00035 namespace {
00036 
00037 static unsigned RRegs[32] = {
00038   PPC::R0,  PPC::R1,  PPC::R2,  PPC::R3,
00039   PPC::R4,  PPC::R5,  PPC::R6,  PPC::R7,
00040   PPC::R8,  PPC::R9,  PPC::R10, PPC::R11,
00041   PPC::R12, PPC::R13, PPC::R14, PPC::R15,
00042   PPC::R16, PPC::R17, PPC::R18, PPC::R19,
00043   PPC::R20, PPC::R21, PPC::R22, PPC::R23,
00044   PPC::R24, PPC::R25, PPC::R26, PPC::R27,
00045   PPC::R28, PPC::R29, PPC::R30, PPC::R31
00046 };
00047 static unsigned RRegsNoR0[32] = {
00048   PPC::ZERO,
00049             PPC::R1,  PPC::R2,  PPC::R3,
00050   PPC::R4,  PPC::R5,  PPC::R6,  PPC::R7,
00051   PPC::R8,  PPC::R9,  PPC::R10, PPC::R11,
00052   PPC::R12, PPC::R13, PPC::R14, PPC::R15,
00053   PPC::R16, PPC::R17, PPC::R18, PPC::R19,
00054   PPC::R20, PPC::R21, PPC::R22, PPC::R23,
00055   PPC::R24, PPC::R25, PPC::R26, PPC::R27,
00056   PPC::R28, PPC::R29, PPC::R30, PPC::R31
00057 };
00058 static unsigned XRegs[32] = {
00059   PPC::X0,  PPC::X1,  PPC::X2,  PPC::X3,
00060   PPC::X4,  PPC::X5,  PPC::X6,  PPC::X7,
00061   PPC::X8,  PPC::X9,  PPC::X10, PPC::X11,
00062   PPC::X12, PPC::X13, PPC::X14, PPC::X15,
00063   PPC::X16, PPC::X17, PPC::X18, PPC::X19,
00064   PPC::X20, PPC::X21, PPC::X22, PPC::X23,
00065   PPC::X24, PPC::X25, PPC::X26, PPC::X27,
00066   PPC::X28, PPC::X29, PPC::X30, PPC::X31
00067 };
00068 static unsigned XRegsNoX0[32] = {
00069   PPC::ZERO8,
00070             PPC::X1,  PPC::X2,  PPC::X3,
00071   PPC::X4,  PPC::X5,  PPC::X6,  PPC::X7,
00072   PPC::X8,  PPC::X9,  PPC::X10, PPC::X11,
00073   PPC::X12, PPC::X13, PPC::X14, PPC::X15,
00074   PPC::X16, PPC::X17, PPC::X18, PPC::X19,
00075   PPC::X20, PPC::X21, PPC::X22, PPC::X23,
00076   PPC::X24, PPC::X25, PPC::X26, PPC::X27,
00077   PPC::X28, PPC::X29, PPC::X30, PPC::X31
00078 };
00079 static unsigned FRegs[32] = {
00080   PPC::F0,  PPC::F1,  PPC::F2,  PPC::F3,
00081   PPC::F4,  PPC::F5,  PPC::F6,  PPC::F7,
00082   PPC::F8,  PPC::F9,  PPC::F10, PPC::F11,
00083   PPC::F12, PPC::F13, PPC::F14, PPC::F15,
00084   PPC::F16, PPC::F17, PPC::F18, PPC::F19,
00085   PPC::F20, PPC::F21, PPC::F22, PPC::F23,
00086   PPC::F24, PPC::F25, PPC::F26, PPC::F27,
00087   PPC::F28, PPC::F29, PPC::F30, PPC::F31
00088 };
00089 static unsigned VRegs[32] = {
00090   PPC::V0,  PPC::V1,  PPC::V2,  PPC::V3,
00091   PPC::V4,  PPC::V5,  PPC::V6,  PPC::V7,
00092   PPC::V8,  PPC::V9,  PPC::V10, PPC::V11,
00093   PPC::V12, PPC::V13, PPC::V14, PPC::V15,
00094   PPC::V16, PPC::V17, PPC::V18, PPC::V19,
00095   PPC::V20, PPC::V21, PPC::V22, PPC::V23,
00096   PPC::V24, PPC::V25, PPC::V26, PPC::V27,
00097   PPC::V28, PPC::V29, PPC::V30, PPC::V31
00098 };
00099 static unsigned VSRegs[64] = {
00100   PPC::VSL0,  PPC::VSL1,  PPC::VSL2,  PPC::VSL3,
00101   PPC::VSL4,  PPC::VSL5,  PPC::VSL6,  PPC::VSL7,
00102   PPC::VSL8,  PPC::VSL9,  PPC::VSL10, PPC::VSL11,
00103   PPC::VSL12, PPC::VSL13, PPC::VSL14, PPC::VSL15,
00104   PPC::VSL16, PPC::VSL17, PPC::VSL18, PPC::VSL19,
00105   PPC::VSL20, PPC::VSL21, PPC::VSL22, PPC::VSL23,
00106   PPC::VSL24, PPC::VSL25, PPC::VSL26, PPC::VSL27,
00107   PPC::VSL28, PPC::VSL29, PPC::VSL30, PPC::VSL31,
00108 
00109   PPC::VSH0,  PPC::VSH1,  PPC::VSH2,  PPC::VSH3,
00110   PPC::VSH4,  PPC::VSH5,  PPC::VSH6,  PPC::VSH7,
00111   PPC::VSH8,  PPC::VSH9,  PPC::VSH10, PPC::VSH11,
00112   PPC::VSH12, PPC::VSH13, PPC::VSH14, PPC::VSH15,
00113   PPC::VSH16, PPC::VSH17, PPC::VSH18, PPC::VSH19,
00114   PPC::VSH20, PPC::VSH21, PPC::VSH22, PPC::VSH23,
00115   PPC::VSH24, PPC::VSH25, PPC::VSH26, PPC::VSH27,
00116   PPC::VSH28, PPC::VSH29, PPC::VSH30, PPC::VSH31
00117 };
00118 static unsigned VSFRegs[64] = {
00119   PPC::F0,  PPC::F1,  PPC::F2,  PPC::F3,
00120   PPC::F4,  PPC::F5,  PPC::F6,  PPC::F7,
00121   PPC::F8,  PPC::F9,  PPC::F10, PPC::F11,
00122   PPC::F12, PPC::F13, PPC::F14, PPC::F15,
00123   PPC::F16, PPC::F17, PPC::F18, PPC::F19,
00124   PPC::F20, PPC::F21, PPC::F22, PPC::F23,
00125   PPC::F24, PPC::F25, PPC::F26, PPC::F27,
00126   PPC::F28, PPC::F29, PPC::F30, PPC::F31,
00127 
00128   PPC::VF0,  PPC::VF1,  PPC::VF2,  PPC::VF3,
00129   PPC::VF4,  PPC::VF5,  PPC::VF6,  PPC::VF7,
00130   PPC::VF8,  PPC::VF9,  PPC::VF10, PPC::VF11,
00131   PPC::VF12, PPC::VF13, PPC::VF14, PPC::VF15,
00132   PPC::VF16, PPC::VF17, PPC::VF18, PPC::VF19,
00133   PPC::VF20, PPC::VF21, PPC::VF22, PPC::VF23,
00134   PPC::VF24, PPC::VF25, PPC::VF26, PPC::VF27,
00135   PPC::VF28, PPC::VF29, PPC::VF30, PPC::VF31
00136 };
00137 static unsigned CRBITRegs[32] = {
00138   PPC::CR0LT, PPC::CR0GT, PPC::CR0EQ, PPC::CR0UN,
00139   PPC::CR1LT, PPC::CR1GT, PPC::CR1EQ, PPC::CR1UN,
00140   PPC::CR2LT, PPC::CR2GT, PPC::CR2EQ, PPC::CR2UN,
00141   PPC::CR3LT, PPC::CR3GT, PPC::CR3EQ, PPC::CR3UN,
00142   PPC::CR4LT, PPC::CR4GT, PPC::CR4EQ, PPC::CR4UN,
00143   PPC::CR5LT, PPC::CR5GT, PPC::CR5EQ, PPC::CR5UN,
00144   PPC::CR6LT, PPC::CR6GT, PPC::CR6EQ, PPC::CR6UN,
00145   PPC::CR7LT, PPC::CR7GT, PPC::CR7EQ, PPC::CR7UN
00146 };
00147 static unsigned CRRegs[8] = {
00148   PPC::CR0, PPC::CR1, PPC::CR2, PPC::CR3,
00149   PPC::CR4, PPC::CR5, PPC::CR6, PPC::CR7
00150 };
00151 
00152 // Evaluate an expression containing condition register
00153 // or condition register field symbols.  Returns positive
00154 // value on success, or -1 on error.
00155 static int64_t
00156 EvaluateCRExpr(const MCExpr *E) {
00157   switch (E->getKind()) {
00158   case MCExpr::Target:
00159     return -1;
00160 
00161   case MCExpr::Constant: {
00162     int64_t Res = cast<MCConstantExpr>(E)->getValue();
00163     return Res < 0 ? -1 : Res;
00164   }
00165 
00166   case MCExpr::SymbolRef: {
00167     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
00168     StringRef Name = SRE->getSymbol().getName();
00169 
00170     if (Name == "lt") return 0;
00171     if (Name == "gt") return 1;
00172     if (Name == "eq") return 2;
00173     if (Name == "so") return 3;
00174     if (Name == "un") return 3;
00175 
00176     if (Name == "cr0") return 0;
00177     if (Name == "cr1") return 1;
00178     if (Name == "cr2") return 2;
00179     if (Name == "cr3") return 3;
00180     if (Name == "cr4") return 4;
00181     if (Name == "cr5") return 5;
00182     if (Name == "cr6") return 6;
00183     if (Name == "cr7") return 7;
00184 
00185     return -1;
00186   }
00187 
00188   case MCExpr::Unary:
00189     return -1;
00190 
00191   case MCExpr::Binary: {
00192     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
00193     int64_t LHSVal = EvaluateCRExpr(BE->getLHS());
00194     int64_t RHSVal = EvaluateCRExpr(BE->getRHS());
00195     int64_t Res;
00196 
00197     if (LHSVal < 0 || RHSVal < 0)
00198       return -1;
00199 
00200     switch (BE->getOpcode()) {
00201     default: return -1;
00202     case MCBinaryExpr::Add: Res = LHSVal + RHSVal; break;
00203     case MCBinaryExpr::Mul: Res = LHSVal * RHSVal; break;
00204     }
00205 
00206     return Res < 0 ? -1 : Res;
00207   }
00208   }
00209 
00210   llvm_unreachable("Invalid expression kind!");
00211 }
00212 
00213 struct PPCOperand;
00214 
00215 class PPCAsmParser : public MCTargetAsmParser {
00216   MCSubtargetInfo &STI;
00217   MCAsmParser &Parser;
00218   const MCInstrInfo &MII;
00219   bool IsPPC64;
00220   bool IsDarwin;
00221 
00222   MCAsmParser &getParser() const { return Parser; }
00223   MCAsmLexer &getLexer() const { return Parser.getLexer(); }
00224 
00225   void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
00226   bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
00227 
00228   bool isPPC64() const { return IsPPC64; }
00229   bool isDarwin() const { return IsDarwin; }
00230 
00231   bool MatchRegisterName(const AsmToken &Tok,
00232                          unsigned &RegNo, int64_t &IntVal);
00233 
00234   bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override;
00235 
00236   const MCExpr *ExtractModifierFromExpr(const MCExpr *E,
00237                                         PPCMCExpr::VariantKind &Variant);
00238   const MCExpr *FixupVariantKind(const MCExpr *E);
00239   bool ParseExpression(const MCExpr *&EVal);
00240   bool ParseDarwinExpression(const MCExpr *&EVal);
00241 
00242   bool ParseOperand(OperandVector &Operands);
00243 
00244   bool ParseDirectiveWord(unsigned Size, SMLoc L);
00245   bool ParseDirectiveTC(unsigned Size, SMLoc L);
00246   bool ParseDirectiveMachine(SMLoc L);
00247   bool ParseDarwinDirectiveMachine(SMLoc L);
00248   bool ParseDirectiveAbiVersion(SMLoc L);
00249   bool ParseDirectiveLocalEntry(SMLoc L);
00250 
00251   bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
00252                                OperandVector &Operands, MCStreamer &Out,
00253                                uint64_t &ErrorInfo,
00254                                bool MatchingInlineAsm) override;
00255 
00256   void ProcessInstruction(MCInst &Inst, const OperandVector &Ops);
00257 
00258   /// @name Auto-generated Match Functions
00259   /// {
00260 
00261 #define GET_ASSEMBLER_HEADER
00262 #include "PPCGenAsmMatcher.inc"
00263 
00264   /// }
00265 
00266 
00267 public:
00268   PPCAsmParser(MCSubtargetInfo &_STI, MCAsmParser &_Parser,
00269                const MCInstrInfo &_MII,
00270                const MCTargetOptions &Options)
00271       : MCTargetAsmParser(), STI(_STI), Parser(_Parser), MII(_MII) {
00272     // Check for 64-bit vs. 32-bit pointer mode.
00273     Triple TheTriple(STI.getTargetTriple());
00274     IsPPC64 = (TheTriple.getArch() == Triple::ppc64 ||
00275                TheTriple.getArch() == Triple::ppc64le);
00276     IsDarwin = TheTriple.isMacOSX();
00277     // Initialize the set of available features.
00278     setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
00279   }
00280 
00281   bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
00282                         SMLoc NameLoc, OperandVector &Operands) override;
00283 
00284   bool ParseDirective(AsmToken DirectiveID) override;
00285 
00286   unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
00287                                       unsigned Kind) override;
00288 
00289   const MCExpr *applyModifierToExpr(const MCExpr *E,
00290                                     MCSymbolRefExpr::VariantKind,
00291                                     MCContext &Ctx) override;
00292 };
00293 
00294 /// PPCOperand - Instances of this class represent a parsed PowerPC machine
00295 /// instruction.
00296 struct PPCOperand : public MCParsedAsmOperand {
00297   enum KindTy {
00298     Token,
00299     Immediate,
00300     ContextImmediate,
00301     Expression,
00302     TLSRegister
00303   } Kind;
00304 
00305   SMLoc StartLoc, EndLoc;
00306   bool IsPPC64;
00307 
00308   struct TokOp {
00309     const char *Data;
00310     unsigned Length;
00311   };
00312 
00313   struct ImmOp {
00314     int64_t Val;
00315   };
00316 
00317   struct ExprOp {
00318     const MCExpr *Val;
00319     int64_t CRVal;     // Cached result of EvaluateCRExpr(Val)
00320   };
00321 
00322   struct TLSRegOp {
00323     const MCSymbolRefExpr *Sym;
00324   };
00325 
00326   union {
00327     struct TokOp Tok;
00328     struct ImmOp Imm;
00329     struct ExprOp Expr;
00330     struct TLSRegOp TLSReg;
00331   };
00332 
00333   PPCOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
00334 public:
00335   PPCOperand(const PPCOperand &o) : MCParsedAsmOperand() {
00336     Kind = o.Kind;
00337     StartLoc = o.StartLoc;
00338     EndLoc = o.EndLoc;
00339     IsPPC64 = o.IsPPC64;
00340     switch (Kind) {
00341     case Token:
00342       Tok = o.Tok;
00343       break;
00344     case Immediate:
00345     case ContextImmediate:
00346       Imm = o.Imm;
00347       break;
00348     case Expression:
00349       Expr = o.Expr;
00350       break;
00351     case TLSRegister:
00352       TLSReg = o.TLSReg;
00353       break;
00354     }
00355   }
00356 
00357   /// getStartLoc - Get the location of the first token of this operand.
00358   SMLoc getStartLoc() const override { return StartLoc; }
00359 
00360   /// getEndLoc - Get the location of the last token of this operand.
00361   SMLoc getEndLoc() const override { return EndLoc; }
00362 
00363   /// isPPC64 - True if this operand is for an instruction in 64-bit mode.
00364   bool isPPC64() const { return IsPPC64; }
00365 
00366   int64_t getImm() const {
00367     assert(Kind == Immediate && "Invalid access!");
00368     return Imm.Val;
00369   }
00370   int64_t getImmS16Context() const {
00371     assert((Kind == Immediate || Kind == ContextImmediate) && "Invalid access!");
00372     if (Kind == Immediate)
00373       return Imm.Val;
00374     return static_cast<int16_t>(Imm.Val);
00375   }
00376   int64_t getImmU16Context() const {
00377     assert((Kind == Immediate || Kind == ContextImmediate) && "Invalid access!");
00378     return Imm.Val;
00379   }
00380 
00381   const MCExpr *getExpr() const {
00382     assert(Kind == Expression && "Invalid access!");
00383     return Expr.Val;
00384   }
00385 
00386   int64_t getExprCRVal() const {
00387     assert(Kind == Expression && "Invalid access!");
00388     return Expr.CRVal;
00389   }
00390 
00391   const MCExpr *getTLSReg() const {
00392     assert(Kind == TLSRegister && "Invalid access!");
00393     return TLSReg.Sym;
00394   }
00395 
00396   unsigned getReg() const override {
00397     assert(isRegNumber() && "Invalid access!");
00398     return (unsigned) Imm.Val;
00399   }
00400 
00401   unsigned getVSReg() const {
00402     assert(isVSRegNumber() && "Invalid access!");
00403     return (unsigned) Imm.Val;
00404   }
00405 
00406   unsigned getCCReg() const {
00407     assert(isCCRegNumber() && "Invalid access!");
00408     return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
00409   }
00410 
00411   unsigned getCRBit() const {
00412     assert(isCRBitNumber() && "Invalid access!");
00413     return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal);
00414   }
00415 
00416   unsigned getCRBitMask() const {
00417     assert(isCRBitMask() && "Invalid access!");
00418     return 7 - countTrailingZeros<uint64_t>(Imm.Val);
00419   }
00420 
00421   bool isToken() const override { return Kind == Token; }
00422   bool isImm() const override { return Kind == Immediate || Kind == Expression; }
00423   bool isU2Imm() const { return Kind == Immediate && isUInt<2>(getImm()); }
00424   bool isU4Imm() const { return Kind == Immediate && isUInt<4>(getImm()); }
00425   bool isU5Imm() const { return Kind == Immediate && isUInt<5>(getImm()); }
00426   bool isS5Imm() const { return Kind == Immediate && isInt<5>(getImm()); }
00427   bool isU6Imm() const { return Kind == Immediate && isUInt<6>(getImm()); }
00428   bool isU6ImmX2() const { return Kind == Immediate &&
00429                                   isUInt<6>(getImm()) &&
00430                                   (getImm() & 1) == 0; }
00431   bool isU7ImmX4() const { return Kind == Immediate &&
00432                                   isUInt<7>(getImm()) &&
00433                                   (getImm() & 3) == 0; }
00434   bool isU8ImmX8() const { return Kind == Immediate &&
00435                                   isUInt<8>(getImm()) &&
00436                                   (getImm() & 7) == 0; }
00437   bool isU16Imm() const {
00438     switch (Kind) {
00439       case Expression:
00440         return true;
00441       case Immediate:
00442       case ContextImmediate:
00443         return isUInt<16>(getImmU16Context());
00444       default:
00445         return false;
00446     }
00447   }
00448   bool isS16Imm() const {
00449     switch (Kind) {
00450       case Expression:
00451         return true;
00452       case Immediate:
00453       case ContextImmediate:
00454         return isInt<16>(getImmS16Context());
00455       default:
00456         return false;
00457     }
00458   }
00459   bool isS16ImmX4() const { return Kind == Expression ||
00460                                    (Kind == Immediate && isInt<16>(getImm()) &&
00461                                     (getImm() & 3) == 0); }
00462   bool isS17Imm() const {
00463     switch (Kind) {
00464       case Expression:
00465         return true;
00466       case Immediate:
00467       case ContextImmediate:
00468         return isInt<17>(getImmS16Context());
00469       default:
00470         return false;
00471     }
00472   }
00473   bool isTLSReg() const { return Kind == TLSRegister; }
00474   bool isDirectBr() const {
00475     if (Kind == Expression)
00476       return true;
00477     if (Kind != Immediate)
00478       return false;
00479     // Operand must be 64-bit aligned, signed 27-bit immediate.
00480     if ((getImm() & 3) != 0)
00481       return false;
00482     if (isInt<26>(getImm()))
00483       return true;
00484     if (!IsPPC64) {
00485       // In 32-bit mode, large 32-bit quantities wrap around.
00486       if (isUInt<32>(getImm()) && isInt<26>(static_cast<int32_t>(getImm())))
00487         return true;
00488     }
00489     return false;
00490   }
00491   bool isCondBr() const { return Kind == Expression ||
00492                                  (Kind == Immediate && isInt<16>(getImm()) &&
00493                                   (getImm() & 3) == 0); }
00494   bool isRegNumber() const { return Kind == Immediate && isUInt<5>(getImm()); }
00495   bool isVSRegNumber() const { return Kind == Immediate && isUInt<6>(getImm()); }
00496   bool isCCRegNumber() const { return (Kind == Expression
00497                                        && isUInt<3>(getExprCRVal())) ||
00498                                       (Kind == Immediate
00499                                        && isUInt<3>(getImm())); }
00500   bool isCRBitNumber() const { return (Kind == Expression
00501                                        && isUInt<5>(getExprCRVal())) ||
00502                                       (Kind == Immediate
00503                                        && isUInt<5>(getImm())); }
00504   bool isCRBitMask() const { return Kind == Immediate && isUInt<8>(getImm()) &&
00505                                     isPowerOf2_32(getImm()); }
00506   bool isMem() const override { return false; }
00507   bool isReg() const override { return false; }
00508 
00509   void addRegOperands(MCInst &Inst, unsigned N) const {
00510     llvm_unreachable("addRegOperands");
00511   }
00512 
00513   void addRegGPRCOperands(MCInst &Inst, unsigned N) const {
00514     assert(N == 1 && "Invalid number of operands!");
00515     Inst.addOperand(MCOperand::CreateReg(RRegs[getReg()]));
00516   }
00517 
00518   void addRegGPRCNoR0Operands(MCInst &Inst, unsigned N) const {
00519     assert(N == 1 && "Invalid number of operands!");
00520     Inst.addOperand(MCOperand::CreateReg(RRegsNoR0[getReg()]));
00521   }
00522 
00523   void addRegG8RCOperands(MCInst &Inst, unsigned N) const {
00524     assert(N == 1 && "Invalid number of operands!");
00525     Inst.addOperand(MCOperand::CreateReg(XRegs[getReg()]));
00526   }
00527 
00528   void addRegG8RCNoX0Operands(MCInst &Inst, unsigned N) const {
00529     assert(N == 1 && "Invalid number of operands!");
00530     Inst.addOperand(MCOperand::CreateReg(XRegsNoX0[getReg()]));
00531   }
00532 
00533   void addRegGxRCOperands(MCInst &Inst, unsigned N) const {
00534     if (isPPC64())
00535       addRegG8RCOperands(Inst, N);
00536     else
00537       addRegGPRCOperands(Inst, N);
00538   }
00539 
00540   void addRegGxRCNoR0Operands(MCInst &Inst, unsigned N) const {
00541     if (isPPC64())
00542       addRegG8RCNoX0Operands(Inst, N);
00543     else
00544       addRegGPRCNoR0Operands(Inst, N);
00545   }
00546 
00547   void addRegF4RCOperands(MCInst &Inst, unsigned N) const {
00548     assert(N == 1 && "Invalid number of operands!");
00549     Inst.addOperand(MCOperand::CreateReg(FRegs[getReg()]));
00550   }
00551 
00552   void addRegF8RCOperands(MCInst &Inst, unsigned N) const {
00553     assert(N == 1 && "Invalid number of operands!");
00554     Inst.addOperand(MCOperand::CreateReg(FRegs[getReg()]));
00555   }
00556 
00557   void addRegVRRCOperands(MCInst &Inst, unsigned N) const {
00558     assert(N == 1 && "Invalid number of operands!");
00559     Inst.addOperand(MCOperand::CreateReg(VRegs[getReg()]));
00560   }
00561 
00562   void addRegVSRCOperands(MCInst &Inst, unsigned N) const {
00563     assert(N == 1 && "Invalid number of operands!");
00564     Inst.addOperand(MCOperand::CreateReg(VSRegs[getVSReg()]));
00565   }
00566 
00567   void addRegVSFRCOperands(MCInst &Inst, unsigned N) const {
00568     assert(N == 1 && "Invalid number of operands!");
00569     Inst.addOperand(MCOperand::CreateReg(VSFRegs[getVSReg()]));
00570   }
00571 
00572   void addRegCRBITRCOperands(MCInst &Inst, unsigned N) const {
00573     assert(N == 1 && "Invalid number of operands!");
00574     Inst.addOperand(MCOperand::CreateReg(CRBITRegs[getCRBit()]));
00575   }
00576 
00577   void addRegCRRCOperands(MCInst &Inst, unsigned N) const {
00578     assert(N == 1 && "Invalid number of operands!");
00579     Inst.addOperand(MCOperand::CreateReg(CRRegs[getCCReg()]));
00580   }
00581 
00582   void addCRBitMaskOperands(MCInst &Inst, unsigned N) const {
00583     assert(N == 1 && "Invalid number of operands!");
00584     Inst.addOperand(MCOperand::CreateReg(CRRegs[getCRBitMask()]));
00585   }
00586 
00587   void addImmOperands(MCInst &Inst, unsigned N) const {
00588     assert(N == 1 && "Invalid number of operands!");
00589     if (Kind == Immediate)
00590       Inst.addOperand(MCOperand::CreateImm(getImm()));
00591     else
00592       Inst.addOperand(MCOperand::CreateExpr(getExpr()));
00593   }
00594 
00595   void addS16ImmOperands(MCInst &Inst, unsigned N) const {
00596     assert(N == 1 && "Invalid number of operands!");
00597     switch (Kind) {
00598       case Immediate:
00599         Inst.addOperand(MCOperand::CreateImm(getImm()));
00600         break;
00601       case ContextImmediate:
00602         Inst.addOperand(MCOperand::CreateImm(getImmS16Context()));
00603         break;
00604       default:
00605         Inst.addOperand(MCOperand::CreateExpr(getExpr()));
00606         break;
00607     }
00608   }
00609 
00610   void addU16ImmOperands(MCInst &Inst, unsigned N) const {
00611     assert(N == 1 && "Invalid number of operands!");
00612     switch (Kind) {
00613       case Immediate:
00614         Inst.addOperand(MCOperand::CreateImm(getImm()));
00615         break;
00616       case ContextImmediate:
00617         Inst.addOperand(MCOperand::CreateImm(getImmU16Context()));
00618         break;
00619       default:
00620         Inst.addOperand(MCOperand::CreateExpr(getExpr()));
00621         break;
00622     }
00623   }
00624 
00625   void addBranchTargetOperands(MCInst &Inst, unsigned N) const {
00626     assert(N == 1 && "Invalid number of operands!");
00627     if (Kind == Immediate)
00628       Inst.addOperand(MCOperand::CreateImm(getImm() / 4));
00629     else
00630       Inst.addOperand(MCOperand::CreateExpr(getExpr()));
00631   }
00632 
00633   void addTLSRegOperands(MCInst &Inst, unsigned N) const {
00634     assert(N == 1 && "Invalid number of operands!");
00635     Inst.addOperand(MCOperand::CreateExpr(getTLSReg()));
00636   }
00637 
00638   StringRef getToken() const {
00639     assert(Kind == Token && "Invalid access!");
00640     return StringRef(Tok.Data, Tok.Length);
00641   }
00642 
00643   void print(raw_ostream &OS) const override;
00644 
00645   static std::unique_ptr<PPCOperand> CreateToken(StringRef Str, SMLoc S,
00646                                                  bool IsPPC64) {
00647     auto Op = make_unique<PPCOperand>(Token);
00648     Op->Tok.Data = Str.data();
00649     Op->Tok.Length = Str.size();
00650     Op->StartLoc = S;
00651     Op->EndLoc = S;
00652     Op->IsPPC64 = IsPPC64;
00653     return Op;
00654   }
00655 
00656   static std::unique_ptr<PPCOperand>
00657   CreateTokenWithStringCopy(StringRef Str, SMLoc S, bool IsPPC64) {
00658     // Allocate extra memory for the string and copy it.
00659     // FIXME: This is incorrect, Operands are owned by unique_ptr with a default
00660     // deleter which will destroy them by simply using "delete", not correctly
00661     // calling operator delete on this extra memory after calling the dtor
00662     // explicitly.
00663     void *Mem = ::operator new(sizeof(PPCOperand) + Str.size());
00664     std::unique_ptr<PPCOperand> Op(new (Mem) PPCOperand(Token));
00665     Op->Tok.Data = reinterpret_cast<const char *>(Op.get() + 1);
00666     Op->Tok.Length = Str.size();
00667     std::memcpy(const_cast<char *>(Op->Tok.Data), Str.data(), Str.size());
00668     Op->StartLoc = S;
00669     Op->EndLoc = S;
00670     Op->IsPPC64 = IsPPC64;
00671     return Op;
00672   }
00673 
00674   static std::unique_ptr<PPCOperand> CreateImm(int64_t Val, SMLoc S, SMLoc E,
00675                                                bool IsPPC64) {
00676     auto Op = make_unique<PPCOperand>(Immediate);
00677     Op->Imm.Val = Val;
00678     Op->StartLoc = S;
00679     Op->EndLoc = E;
00680     Op->IsPPC64 = IsPPC64;
00681     return Op;
00682   }
00683 
00684   static std::unique_ptr<PPCOperand> CreateExpr(const MCExpr *Val, SMLoc S,
00685                                                 SMLoc E, bool IsPPC64) {
00686     auto Op = make_unique<PPCOperand>(Expression);
00687     Op->Expr.Val = Val;
00688     Op->Expr.CRVal = EvaluateCRExpr(Val);
00689     Op->StartLoc = S;
00690     Op->EndLoc = E;
00691     Op->IsPPC64 = IsPPC64;
00692     return Op;
00693   }
00694 
00695   static std::unique_ptr<PPCOperand>
00696   CreateTLSReg(const MCSymbolRefExpr *Sym, SMLoc S, SMLoc E, bool IsPPC64) {
00697     auto Op = make_unique<PPCOperand>(TLSRegister);
00698     Op->TLSReg.Sym = Sym;
00699     Op->StartLoc = S;
00700     Op->EndLoc = E;
00701     Op->IsPPC64 = IsPPC64;
00702     return Op;
00703   }
00704 
00705   static std::unique_ptr<PPCOperand>
00706   CreateContextImm(int64_t Val, SMLoc S, SMLoc E, bool IsPPC64) {
00707     auto Op = make_unique<PPCOperand>(ContextImmediate);
00708     Op->Imm.Val = Val;
00709     Op->StartLoc = S;
00710     Op->EndLoc = E;
00711     Op->IsPPC64 = IsPPC64;
00712     return Op;
00713   }
00714 
00715   static std::unique_ptr<PPCOperand>
00716   CreateFromMCExpr(const MCExpr *Val, SMLoc S, SMLoc E, bool IsPPC64) {
00717     if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Val))
00718       return CreateImm(CE->getValue(), S, E, IsPPC64);
00719 
00720     if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Val))
00721       if (SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS)
00722         return CreateTLSReg(SRE, S, E, IsPPC64);
00723 
00724     if (const PPCMCExpr *TE = dyn_cast<PPCMCExpr>(Val)) {
00725       int64_t Res;
00726       if (TE->EvaluateAsConstant(Res))
00727         return CreateContextImm(Res, S, E, IsPPC64);
00728     }
00729 
00730     return CreateExpr(Val, S, E, IsPPC64);
00731   }
00732 };
00733 
00734 } // end anonymous namespace.
00735 
00736 void PPCOperand::print(raw_ostream &OS) const {
00737   switch (Kind) {
00738   case Token:
00739     OS << "'" << getToken() << "'";
00740     break;
00741   case Immediate:
00742   case ContextImmediate:
00743     OS << getImm();
00744     break;
00745   case Expression:
00746     getExpr()->print(OS);
00747     break;
00748   case TLSRegister:
00749     getTLSReg()->print(OS);
00750     break;
00751   }
00752 }
00753 
00754 static void
00755 addNegOperand(MCInst &Inst, MCOperand &Op, MCContext &Ctx) {
00756   if (Op.isImm()) {
00757     Inst.addOperand(MCOperand::CreateImm(-Op.getImm()));
00758     return;
00759   }
00760   const MCExpr *Expr = Op.getExpr();
00761   if (const MCUnaryExpr *UnExpr = dyn_cast<MCUnaryExpr>(Expr)) {
00762     if (UnExpr->getOpcode() == MCUnaryExpr::Minus) {
00763       Inst.addOperand(MCOperand::CreateExpr(UnExpr->getSubExpr()));
00764       return;
00765     }
00766   } else if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Expr)) {
00767     if (BinExpr->getOpcode() == MCBinaryExpr::Sub) {
00768       const MCExpr *NE = MCBinaryExpr::CreateSub(BinExpr->getRHS(),
00769                                                  BinExpr->getLHS(), Ctx);
00770       Inst.addOperand(MCOperand::CreateExpr(NE));
00771       return;
00772     }
00773   }
00774   Inst.addOperand(MCOperand::CreateExpr(MCUnaryExpr::CreateMinus(Expr, Ctx)));
00775 }
00776 
00777 void PPCAsmParser::ProcessInstruction(MCInst &Inst,
00778                                       const OperandVector &Operands) {
00779   int Opcode = Inst.getOpcode();
00780   switch (Opcode) {
00781   case PPC::LAx: {
00782     MCInst TmpInst;
00783     TmpInst.setOpcode(PPC::LA);
00784     TmpInst.addOperand(Inst.getOperand(0));
00785     TmpInst.addOperand(Inst.getOperand(2));
00786     TmpInst.addOperand(Inst.getOperand(1));
00787     Inst = TmpInst;
00788     break;
00789   }
00790   case PPC::SUBI: {
00791     MCInst TmpInst;
00792     TmpInst.setOpcode(PPC::ADDI);
00793     TmpInst.addOperand(Inst.getOperand(0));
00794     TmpInst.addOperand(Inst.getOperand(1));
00795     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
00796     Inst = TmpInst;
00797     break;
00798   }
00799   case PPC::SUBIS: {
00800     MCInst TmpInst;
00801     TmpInst.setOpcode(PPC::ADDIS);
00802     TmpInst.addOperand(Inst.getOperand(0));
00803     TmpInst.addOperand(Inst.getOperand(1));
00804     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
00805     Inst = TmpInst;
00806     break;
00807   }
00808   case PPC::SUBIC: {
00809     MCInst TmpInst;
00810     TmpInst.setOpcode(PPC::ADDIC);
00811     TmpInst.addOperand(Inst.getOperand(0));
00812     TmpInst.addOperand(Inst.getOperand(1));
00813     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
00814     Inst = TmpInst;
00815     break;
00816   }
00817   case PPC::SUBICo: {
00818     MCInst TmpInst;
00819     TmpInst.setOpcode(PPC::ADDICo);
00820     TmpInst.addOperand(Inst.getOperand(0));
00821     TmpInst.addOperand(Inst.getOperand(1));
00822     addNegOperand(TmpInst, Inst.getOperand(2), getContext());
00823     Inst = TmpInst;
00824     break;
00825   }
00826   case PPC::EXTLWI:
00827   case PPC::EXTLWIo: {
00828     MCInst TmpInst;
00829     int64_t N = Inst.getOperand(2).getImm();
00830     int64_t B = Inst.getOperand(3).getImm();
00831     TmpInst.setOpcode(Opcode == PPC::EXTLWI? PPC::RLWINM : PPC::RLWINMo);
00832     TmpInst.addOperand(Inst.getOperand(0));
00833     TmpInst.addOperand(Inst.getOperand(1));
00834     TmpInst.addOperand(MCOperand::CreateImm(B));
00835     TmpInst.addOperand(MCOperand::CreateImm(0));
00836     TmpInst.addOperand(MCOperand::CreateImm(N - 1));
00837     Inst = TmpInst;
00838     break;
00839   }
00840   case PPC::EXTRWI:
00841   case PPC::EXTRWIo: {
00842     MCInst TmpInst;
00843     int64_t N = Inst.getOperand(2).getImm();
00844     int64_t B = Inst.getOperand(3).getImm();
00845     TmpInst.setOpcode(Opcode == PPC::EXTRWI? PPC::RLWINM : PPC::RLWINMo);
00846     TmpInst.addOperand(Inst.getOperand(0));
00847     TmpInst.addOperand(Inst.getOperand(1));
00848     TmpInst.addOperand(MCOperand::CreateImm(B + N));
00849     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
00850     TmpInst.addOperand(MCOperand::CreateImm(31));
00851     Inst = TmpInst;
00852     break;
00853   }
00854   case PPC::INSLWI:
00855   case PPC::INSLWIo: {
00856     MCInst TmpInst;
00857     int64_t N = Inst.getOperand(2).getImm();
00858     int64_t B = Inst.getOperand(3).getImm();
00859     TmpInst.setOpcode(Opcode == PPC::INSLWI? PPC::RLWIMI : PPC::RLWIMIo);
00860     TmpInst.addOperand(Inst.getOperand(0));
00861     TmpInst.addOperand(Inst.getOperand(0));
00862     TmpInst.addOperand(Inst.getOperand(1));
00863     TmpInst.addOperand(MCOperand::CreateImm(32 - B));
00864     TmpInst.addOperand(MCOperand::CreateImm(B));
00865     TmpInst.addOperand(MCOperand::CreateImm((B + N) - 1));
00866     Inst = TmpInst;
00867     break;
00868   }
00869   case PPC::INSRWI:
00870   case PPC::INSRWIo: {
00871     MCInst TmpInst;
00872     int64_t N = Inst.getOperand(2).getImm();
00873     int64_t B = Inst.getOperand(3).getImm();
00874     TmpInst.setOpcode(Opcode == PPC::INSRWI? PPC::RLWIMI : PPC::RLWIMIo);
00875     TmpInst.addOperand(Inst.getOperand(0));
00876     TmpInst.addOperand(Inst.getOperand(0));
00877     TmpInst.addOperand(Inst.getOperand(1));
00878     TmpInst.addOperand(MCOperand::CreateImm(32 - (B + N)));
00879     TmpInst.addOperand(MCOperand::CreateImm(B));
00880     TmpInst.addOperand(MCOperand::CreateImm((B + N) - 1));
00881     Inst = TmpInst;
00882     break;
00883   }
00884   case PPC::ROTRWI:
00885   case PPC::ROTRWIo: {
00886     MCInst TmpInst;
00887     int64_t N = Inst.getOperand(2).getImm();
00888     TmpInst.setOpcode(Opcode == PPC::ROTRWI? PPC::RLWINM : PPC::RLWINMo);
00889     TmpInst.addOperand(Inst.getOperand(0));
00890     TmpInst.addOperand(Inst.getOperand(1));
00891     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
00892     TmpInst.addOperand(MCOperand::CreateImm(0));
00893     TmpInst.addOperand(MCOperand::CreateImm(31));
00894     Inst = TmpInst;
00895     break;
00896   }
00897   case PPC::SLWI:
00898   case PPC::SLWIo: {
00899     MCInst TmpInst;
00900     int64_t N = Inst.getOperand(2).getImm();
00901     TmpInst.setOpcode(Opcode == PPC::SLWI? PPC::RLWINM : PPC::RLWINMo);
00902     TmpInst.addOperand(Inst.getOperand(0));
00903     TmpInst.addOperand(Inst.getOperand(1));
00904     TmpInst.addOperand(MCOperand::CreateImm(N));
00905     TmpInst.addOperand(MCOperand::CreateImm(0));
00906     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
00907     Inst = TmpInst;
00908     break;
00909   }
00910   case PPC::SRWI:
00911   case PPC::SRWIo: {
00912     MCInst TmpInst;
00913     int64_t N = Inst.getOperand(2).getImm();
00914     TmpInst.setOpcode(Opcode == PPC::SRWI? PPC::RLWINM : PPC::RLWINMo);
00915     TmpInst.addOperand(Inst.getOperand(0));
00916     TmpInst.addOperand(Inst.getOperand(1));
00917     TmpInst.addOperand(MCOperand::CreateImm(32 - N));
00918     TmpInst.addOperand(MCOperand::CreateImm(N));
00919     TmpInst.addOperand(MCOperand::CreateImm(31));
00920     Inst = TmpInst;
00921     break;
00922   }
00923   case PPC::CLRRWI:
00924   case PPC::CLRRWIo: {
00925     MCInst TmpInst;
00926     int64_t N = Inst.getOperand(2).getImm();
00927     TmpInst.setOpcode(Opcode == PPC::CLRRWI? PPC::RLWINM : PPC::RLWINMo);
00928     TmpInst.addOperand(Inst.getOperand(0));
00929     TmpInst.addOperand(Inst.getOperand(1));
00930     TmpInst.addOperand(MCOperand::CreateImm(0));
00931     TmpInst.addOperand(MCOperand::CreateImm(0));
00932     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
00933     Inst = TmpInst;
00934     break;
00935   }
00936   case PPC::CLRLSLWI:
00937   case PPC::CLRLSLWIo: {
00938     MCInst TmpInst;
00939     int64_t B = Inst.getOperand(2).getImm();
00940     int64_t N = Inst.getOperand(3).getImm();
00941     TmpInst.setOpcode(Opcode == PPC::CLRLSLWI? PPC::RLWINM : PPC::RLWINMo);
00942     TmpInst.addOperand(Inst.getOperand(0));
00943     TmpInst.addOperand(Inst.getOperand(1));
00944     TmpInst.addOperand(MCOperand::CreateImm(N));
00945     TmpInst.addOperand(MCOperand::CreateImm(B - N));
00946     TmpInst.addOperand(MCOperand::CreateImm(31 - N));
00947     Inst = TmpInst;
00948     break;
00949   }
00950   case PPC::EXTLDI:
00951   case PPC::EXTLDIo: {
00952     MCInst TmpInst;
00953     int64_t N = Inst.getOperand(2).getImm();
00954     int64_t B = Inst.getOperand(3).getImm();
00955     TmpInst.setOpcode(Opcode == PPC::EXTLDI? PPC::RLDICR : PPC::RLDICRo);
00956     TmpInst.addOperand(Inst.getOperand(0));
00957     TmpInst.addOperand(Inst.getOperand(1));
00958     TmpInst.addOperand(MCOperand::CreateImm(B));
00959     TmpInst.addOperand(MCOperand::CreateImm(N - 1));
00960     Inst = TmpInst;
00961     break;
00962   }
00963   case PPC::EXTRDI:
00964   case PPC::EXTRDIo: {
00965     MCInst TmpInst;
00966     int64_t N = Inst.getOperand(2).getImm();
00967     int64_t B = Inst.getOperand(3).getImm();
00968     TmpInst.setOpcode(Opcode == PPC::EXTRDI? PPC::RLDICL : PPC::RLDICLo);
00969     TmpInst.addOperand(Inst.getOperand(0));
00970     TmpInst.addOperand(Inst.getOperand(1));
00971     TmpInst.addOperand(MCOperand::CreateImm(B + N));
00972     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
00973     Inst = TmpInst;
00974     break;
00975   }
00976   case PPC::INSRDI:
00977   case PPC::INSRDIo: {
00978     MCInst TmpInst;
00979     int64_t N = Inst.getOperand(2).getImm();
00980     int64_t B = Inst.getOperand(3).getImm();
00981     TmpInst.setOpcode(Opcode == PPC::INSRDI? PPC::RLDIMI : PPC::RLDIMIo);
00982     TmpInst.addOperand(Inst.getOperand(0));
00983     TmpInst.addOperand(Inst.getOperand(0));
00984     TmpInst.addOperand(Inst.getOperand(1));
00985     TmpInst.addOperand(MCOperand::CreateImm(64 - (B + N)));
00986     TmpInst.addOperand(MCOperand::CreateImm(B));
00987     Inst = TmpInst;
00988     break;
00989   }
00990   case PPC::ROTRDI:
00991   case PPC::ROTRDIo: {
00992     MCInst TmpInst;
00993     int64_t N = Inst.getOperand(2).getImm();
00994     TmpInst.setOpcode(Opcode == PPC::ROTRDI? PPC::RLDICL : PPC::RLDICLo);
00995     TmpInst.addOperand(Inst.getOperand(0));
00996     TmpInst.addOperand(Inst.getOperand(1));
00997     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
00998     TmpInst.addOperand(MCOperand::CreateImm(0));
00999     Inst = TmpInst;
01000     break;
01001   }
01002   case PPC::SLDI:
01003   case PPC::SLDIo: {
01004     MCInst TmpInst;
01005     int64_t N = Inst.getOperand(2).getImm();
01006     TmpInst.setOpcode(Opcode == PPC::SLDI? PPC::RLDICR : PPC::RLDICRo);
01007     TmpInst.addOperand(Inst.getOperand(0));
01008     TmpInst.addOperand(Inst.getOperand(1));
01009     TmpInst.addOperand(MCOperand::CreateImm(N));
01010     TmpInst.addOperand(MCOperand::CreateImm(63 - N));
01011     Inst = TmpInst;
01012     break;
01013   }
01014   case PPC::SRDI:
01015   case PPC::SRDIo: {
01016     MCInst TmpInst;
01017     int64_t N = Inst.getOperand(2).getImm();
01018     TmpInst.setOpcode(Opcode == PPC::SRDI? PPC::RLDICL : PPC::RLDICLo);
01019     TmpInst.addOperand(Inst.getOperand(0));
01020     TmpInst.addOperand(Inst.getOperand(1));
01021     TmpInst.addOperand(MCOperand::CreateImm(64 - N));
01022     TmpInst.addOperand(MCOperand::CreateImm(N));
01023     Inst = TmpInst;
01024     break;
01025   }
01026   case PPC::CLRRDI:
01027   case PPC::CLRRDIo: {
01028     MCInst TmpInst;
01029     int64_t N = Inst.getOperand(2).getImm();
01030     TmpInst.setOpcode(Opcode == PPC::CLRRDI? PPC::RLDICR : PPC::RLDICRo);
01031     TmpInst.addOperand(Inst.getOperand(0));
01032     TmpInst.addOperand(Inst.getOperand(1));
01033     TmpInst.addOperand(MCOperand::CreateImm(0));
01034     TmpInst.addOperand(MCOperand::CreateImm(63 - N));
01035     Inst = TmpInst;
01036     break;
01037   }
01038   case PPC::CLRLSLDI:
01039   case PPC::CLRLSLDIo: {
01040     MCInst TmpInst;
01041     int64_t B = Inst.getOperand(2).getImm();
01042     int64_t N = Inst.getOperand(3).getImm();
01043     TmpInst.setOpcode(Opcode == PPC::CLRLSLDI? PPC::RLDIC : PPC::RLDICo);
01044     TmpInst.addOperand(Inst.getOperand(0));
01045     TmpInst.addOperand(Inst.getOperand(1));
01046     TmpInst.addOperand(MCOperand::CreateImm(N));
01047     TmpInst.addOperand(MCOperand::CreateImm(B - N));
01048     Inst = TmpInst;
01049     break;
01050   }
01051   }
01052 }
01053 
01054 bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
01055                                            OperandVector &Operands,
01056                                            MCStreamer &Out, uint64_t &ErrorInfo,
01057                                            bool MatchingInlineAsm) {
01058   MCInst Inst;
01059 
01060   switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) {
01061   default: break;
01062   case Match_Success:
01063     // Post-process instructions (typically extended mnemonics)
01064     ProcessInstruction(Inst, Operands);
01065     Inst.setLoc(IDLoc);
01066     Out.EmitInstruction(Inst, STI);
01067     return false;
01068   case Match_MissingFeature:
01069     return Error(IDLoc, "instruction use requires an option to be enabled");
01070   case Match_MnemonicFail:
01071       return Error(IDLoc, "unrecognized instruction mnemonic");
01072   case Match_InvalidOperand: {
01073     SMLoc ErrorLoc = IDLoc;
01074     if (ErrorInfo != ~0ULL) {
01075       if (ErrorInfo >= Operands.size())
01076         return Error(IDLoc, "too few operands for instruction");
01077 
01078       ErrorLoc = ((PPCOperand &)*Operands[ErrorInfo]).getStartLoc();
01079       if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
01080     }
01081 
01082     return Error(ErrorLoc, "invalid operand for instruction");
01083   }
01084   }
01085 
01086   llvm_unreachable("Implement any new match types added!");
01087 }
01088 
01089 bool PPCAsmParser::
01090 MatchRegisterName(const AsmToken &Tok, unsigned &RegNo, int64_t &IntVal) {
01091   if (Tok.is(AsmToken::Identifier)) {
01092     StringRef Name = Tok.getString();
01093 
01094     if (Name.equals_lower("lr")) {
01095       RegNo = isPPC64()? PPC::LR8 : PPC::LR;
01096       IntVal = 8;
01097       return false;
01098     } else if (Name.equals_lower("ctr")) {
01099       RegNo = isPPC64()? PPC::CTR8 : PPC::CTR;
01100       IntVal = 9;
01101       return false;
01102     } else if (Name.equals_lower("vrsave")) {
01103       RegNo = PPC::VRSAVE;
01104       IntVal = 256;
01105       return false;
01106     } else if (Name.startswith_lower("r") &&
01107                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
01108       RegNo = isPPC64()? XRegs[IntVal] : RRegs[IntVal];
01109       return false;
01110     } else if (Name.startswith_lower("f") &&
01111                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
01112       RegNo = FRegs[IntVal];
01113       return false;
01114     } else if (Name.startswith_lower("v") &&
01115                !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) {
01116       RegNo = VRegs[IntVal];
01117       return false;
01118     } else if (Name.startswith_lower("cr") &&
01119                !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 8) {
01120       RegNo = CRRegs[IntVal];
01121       return false;
01122     }
01123   }
01124 
01125   return true;
01126 }
01127 
01128 bool PPCAsmParser::
01129 ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) {
01130   const AsmToken &Tok = Parser.getTok();
01131   StartLoc = Tok.getLoc();
01132   EndLoc = Tok.getEndLoc();
01133   RegNo = 0;
01134   int64_t IntVal;
01135 
01136   if (!MatchRegisterName(Tok, RegNo, IntVal)) {
01137     Parser.Lex(); // Eat identifier token.
01138     return false;
01139   }
01140 
01141   return Error(StartLoc, "invalid register name");
01142 }
01143 
01144 /// Extract \code @l/@ha \endcode modifier from expression.  Recursively scan
01145 /// the expression and check for VK_PPC_LO/HI/HA
01146 /// symbol variants.  If all symbols with modifier use the same
01147 /// variant, return the corresponding PPCMCExpr::VariantKind,
01148 /// and a modified expression using the default symbol variant.
01149 /// Otherwise, return NULL.
01150 const MCExpr *PPCAsmParser::
01151 ExtractModifierFromExpr(const MCExpr *E,
01152                         PPCMCExpr::VariantKind &Variant) {
01153   MCContext &Context = getParser().getContext();
01154   Variant = PPCMCExpr::VK_PPC_None;
01155 
01156   switch (E->getKind()) {
01157   case MCExpr::Target:
01158   case MCExpr::Constant:
01159     return nullptr;
01160 
01161   case MCExpr::SymbolRef: {
01162     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
01163 
01164     switch (SRE->getKind()) {
01165     case MCSymbolRefExpr::VK_PPC_LO:
01166       Variant = PPCMCExpr::VK_PPC_LO;
01167       break;
01168     case MCSymbolRefExpr::VK_PPC_HI:
01169       Variant = PPCMCExpr::VK_PPC_HI;
01170       break;
01171     case MCSymbolRefExpr::VK_PPC_HA:
01172       Variant = PPCMCExpr::VK_PPC_HA;
01173       break;
01174     case MCSymbolRefExpr::VK_PPC_HIGHER:
01175       Variant = PPCMCExpr::VK_PPC_HIGHER;
01176       break;
01177     case MCSymbolRefExpr::VK_PPC_HIGHERA:
01178       Variant = PPCMCExpr::VK_PPC_HIGHERA;
01179       break;
01180     case MCSymbolRefExpr::VK_PPC_HIGHEST:
01181       Variant = PPCMCExpr::VK_PPC_HIGHEST;
01182       break;
01183     case MCSymbolRefExpr::VK_PPC_HIGHESTA:
01184       Variant = PPCMCExpr::VK_PPC_HIGHESTA;
01185       break;
01186     default:
01187       return nullptr;
01188     }
01189 
01190     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Context);
01191   }
01192 
01193   case MCExpr::Unary: {
01194     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
01195     const MCExpr *Sub = ExtractModifierFromExpr(UE->getSubExpr(), Variant);
01196     if (!Sub)
01197       return nullptr;
01198     return MCUnaryExpr::Create(UE->getOpcode(), Sub, Context);
01199   }
01200 
01201   case MCExpr::Binary: {
01202     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
01203     PPCMCExpr::VariantKind LHSVariant, RHSVariant;
01204     const MCExpr *LHS = ExtractModifierFromExpr(BE->getLHS(), LHSVariant);
01205     const MCExpr *RHS = ExtractModifierFromExpr(BE->getRHS(), RHSVariant);
01206 
01207     if (!LHS && !RHS)
01208       return nullptr;
01209 
01210     if (!LHS) LHS = BE->getLHS();
01211     if (!RHS) RHS = BE->getRHS();
01212 
01213     if (LHSVariant == PPCMCExpr::VK_PPC_None)
01214       Variant = RHSVariant;
01215     else if (RHSVariant == PPCMCExpr::VK_PPC_None)
01216       Variant = LHSVariant;
01217     else if (LHSVariant == RHSVariant)
01218       Variant = LHSVariant;
01219     else
01220       return nullptr;
01221 
01222     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, Context);
01223   }
01224   }
01225 
01226   llvm_unreachable("Invalid expression kind!");
01227 }
01228 
01229 /// Find all VK_TLSGD/VK_TLSLD symbol references in expression and replace
01230 /// them by VK_PPC_TLSGD/VK_PPC_TLSLD.  This is necessary to avoid having
01231 /// _GLOBAL_OFFSET_TABLE_ created via ELFObjectWriter::RelocNeedsGOT.
01232 /// FIXME: This is a hack.
01233 const MCExpr *PPCAsmParser::
01234 FixupVariantKind(const MCExpr *E) {
01235   MCContext &Context = getParser().getContext();
01236 
01237   switch (E->getKind()) {
01238   case MCExpr::Target:
01239   case MCExpr::Constant:
01240     return E;
01241 
01242   case MCExpr::SymbolRef: {
01243     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
01244     MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
01245 
01246     switch (SRE->getKind()) {
01247     case MCSymbolRefExpr::VK_TLSGD:
01248       Variant = MCSymbolRefExpr::VK_PPC_TLSGD;
01249       break;
01250     case MCSymbolRefExpr::VK_TLSLD:
01251       Variant = MCSymbolRefExpr::VK_PPC_TLSLD;
01252       break;
01253     default:
01254       return E;
01255     }
01256     return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, Context);
01257   }
01258 
01259   case MCExpr::Unary: {
01260     const MCUnaryExpr *UE = cast<MCUnaryExpr>(E);
01261     const MCExpr *Sub = FixupVariantKind(UE->getSubExpr());
01262     if (Sub == UE->getSubExpr())
01263       return E;
01264     return MCUnaryExpr::Create(UE->getOpcode(), Sub, Context);
01265   }
01266 
01267   case MCExpr::Binary: {
01268     const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
01269     const MCExpr *LHS = FixupVariantKind(BE->getLHS());
01270     const MCExpr *RHS = FixupVariantKind(BE->getRHS());
01271     if (LHS == BE->getLHS() && RHS == BE->getRHS())
01272       return E;
01273     return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, Context);
01274   }
01275   }
01276 
01277   llvm_unreachable("Invalid expression kind!");
01278 }
01279 
01280 /// ParseExpression.  This differs from the default "parseExpression" in that
01281 /// it handles modifiers.
01282 bool PPCAsmParser::
01283 ParseExpression(const MCExpr *&EVal) {
01284 
01285   if (isDarwin())
01286     return ParseDarwinExpression(EVal);
01287 
01288   // (ELF Platforms)
01289   // Handle \code @l/@ha \endcode
01290   if (getParser().parseExpression(EVal))
01291     return true;
01292 
01293   EVal = FixupVariantKind(EVal);
01294 
01295   PPCMCExpr::VariantKind Variant;
01296   const MCExpr *E = ExtractModifierFromExpr(EVal, Variant);
01297   if (E)
01298     EVal = PPCMCExpr::Create(Variant, E, false, getParser().getContext());
01299 
01300   return false;
01301 }
01302 
01303 /// ParseDarwinExpression.  (MachO Platforms)
01304 /// This differs from the default "parseExpression" in that it handles detection
01305 /// of the \code hi16(), ha16() and lo16() \endcode modifiers.  At present,
01306 /// parseExpression() doesn't recognise the modifiers when in the Darwin/MachO
01307 /// syntax form so it is done here.  TODO: Determine if there is merit in arranging
01308 /// for this to be done at a higher level.
01309 bool PPCAsmParser::
01310 ParseDarwinExpression(const MCExpr *&EVal) {
01311   PPCMCExpr::VariantKind Variant = PPCMCExpr::VK_PPC_None;
01312   switch (getLexer().getKind()) {
01313   default:
01314     break;
01315   case AsmToken::Identifier:
01316     // Compiler-generated Darwin identifiers begin with L,l,_ or "; thus
01317     // something starting with any other char should be part of the
01318     // asm syntax.  If handwritten asm includes an identifier like lo16,
01319     // then all bets are off - but no-one would do that, right?
01320     StringRef poss = Parser.getTok().getString();
01321     if (poss.equals_lower("lo16")) {
01322       Variant = PPCMCExpr::VK_PPC_LO;
01323     } else if (poss.equals_lower("hi16")) {
01324       Variant = PPCMCExpr::VK_PPC_HI;
01325     } else if (poss.equals_lower("ha16")) {
01326       Variant = PPCMCExpr::VK_PPC_HA;
01327     }
01328     if (Variant != PPCMCExpr::VK_PPC_None) {
01329       Parser.Lex(); // Eat the xx16
01330       if (getLexer().isNot(AsmToken::LParen))
01331         return Error(Parser.getTok().getLoc(), "expected '('");
01332       Parser.Lex(); // Eat the '('
01333     }
01334     break;
01335   }
01336 
01337   if (getParser().parseExpression(EVal))
01338     return true;
01339 
01340   if (Variant != PPCMCExpr::VK_PPC_None) {
01341     if (getLexer().isNot(AsmToken::RParen))
01342       return Error(Parser.getTok().getLoc(), "expected ')'");
01343     Parser.Lex(); // Eat the ')'
01344     EVal = PPCMCExpr::Create(Variant, EVal, false, getParser().getContext());
01345   }
01346   return false;
01347 }
01348 
01349 /// ParseOperand
01350 /// This handles registers in the form 'NN', '%rNN' for ELF platforms and
01351 /// rNN for MachO.
01352 bool PPCAsmParser::ParseOperand(OperandVector &Operands) {
01353   SMLoc S = Parser.getTok().getLoc();
01354   SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
01355   const MCExpr *EVal;
01356 
01357   // Attempt to parse the next token as an immediate
01358   switch (getLexer().getKind()) {
01359   // Special handling for register names.  These are interpreted
01360   // as immediates corresponding to the register number.
01361   case AsmToken::Percent:
01362     Parser.Lex(); // Eat the '%'.
01363     unsigned RegNo;
01364     int64_t IntVal;
01365     if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
01366       Parser.Lex(); // Eat the identifier token.
01367       Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
01368       return false;
01369     }
01370     return Error(S, "invalid register name");
01371 
01372   case AsmToken::Identifier:
01373     // Note that non-register-name identifiers from the compiler will begin
01374     // with '_', 'L'/'l' or '"'.  Of course, handwritten asm could include
01375     // identifiers like r31foo - so we fall through in the event that parsing
01376     // a register name fails.
01377     if (isDarwin()) {
01378       unsigned RegNo;
01379       int64_t IntVal;
01380       if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
01381         Parser.Lex(); // Eat the identifier token.
01382         Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
01383         return false;
01384       }
01385     }
01386   // Fall-through to process non-register-name identifiers as expression.
01387   // All other expressions
01388   case AsmToken::LParen:
01389   case AsmToken::Plus:
01390   case AsmToken::Minus:
01391   case AsmToken::Integer:
01392   case AsmToken::Dot:
01393   case AsmToken::Dollar:
01394   case AsmToken::Exclaim:
01395   case AsmToken::Tilde:
01396     if (!ParseExpression(EVal))
01397       break;
01398     /* fall through */
01399   default:
01400     return Error(S, "unknown operand");
01401   }
01402 
01403   // Push the parsed operand into the list of operands
01404   Operands.push_back(PPCOperand::CreateFromMCExpr(EVal, S, E, isPPC64()));
01405 
01406   // Check whether this is a TLS call expression
01407   bool TLSCall = false;
01408   if (const MCSymbolRefExpr *Ref = dyn_cast<MCSymbolRefExpr>(EVal))
01409     TLSCall = Ref->getSymbol().getName() == "__tls_get_addr";
01410 
01411   if (TLSCall && getLexer().is(AsmToken::LParen)) {
01412     const MCExpr *TLSSym;
01413 
01414     Parser.Lex(); // Eat the '('.
01415     S = Parser.getTok().getLoc();
01416     if (ParseExpression(TLSSym))
01417       return Error(S, "invalid TLS call expression");
01418     if (getLexer().isNot(AsmToken::RParen))
01419       return Error(Parser.getTok().getLoc(), "missing ')'");
01420     E = Parser.getTok().getLoc();
01421     Parser.Lex(); // Eat the ')'.
01422 
01423     Operands.push_back(PPCOperand::CreateFromMCExpr(TLSSym, S, E, isPPC64()));
01424   }
01425 
01426   // Otherwise, check for D-form memory operands
01427   if (!TLSCall && getLexer().is(AsmToken::LParen)) {
01428     Parser.Lex(); // Eat the '('.
01429     S = Parser.getTok().getLoc();
01430 
01431     int64_t IntVal;
01432     switch (getLexer().getKind()) {
01433     case AsmToken::Percent:
01434       Parser.Lex(); // Eat the '%'.
01435       unsigned RegNo;
01436       if (MatchRegisterName(Parser.getTok(), RegNo, IntVal))
01437         return Error(S, "invalid register name");
01438       Parser.Lex(); // Eat the identifier token.
01439       break;
01440 
01441     case AsmToken::Integer:
01442       if (!isDarwin()) {
01443         if (getParser().parseAbsoluteExpression(IntVal) ||
01444           IntVal < 0 || IntVal > 31)
01445         return Error(S, "invalid register number");
01446       } else {
01447         return Error(S, "unexpected integer value");
01448       }
01449       break;
01450 
01451    case AsmToken::Identifier:
01452     if (isDarwin()) {
01453       unsigned RegNo;
01454       if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) {
01455         Parser.Lex(); // Eat the identifier token.
01456         break;
01457       }
01458     }
01459     // Fall-through..
01460 
01461     default:
01462       return Error(S, "invalid memory operand");
01463     }
01464 
01465     if (getLexer().isNot(AsmToken::RParen))
01466       return Error(Parser.getTok().getLoc(), "missing ')'");
01467     E = Parser.getTok().getLoc();
01468     Parser.Lex(); // Eat the ')'.
01469 
01470     Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64()));
01471   }
01472 
01473   return false;
01474 }
01475 
01476 /// Parse an instruction mnemonic followed by its operands.
01477 bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
01478                                     SMLoc NameLoc, OperandVector &Operands) {
01479   // The first operand is the token for the instruction name.
01480   // If the next character is a '+' or '-', we need to add it to the
01481   // instruction name, to match what TableGen is doing.
01482   std::string NewOpcode;
01483   if (getLexer().is(AsmToken::Plus)) {
01484     getLexer().Lex();
01485     NewOpcode = Name;
01486     NewOpcode += '+';
01487     Name = NewOpcode;
01488   }
01489   if (getLexer().is(AsmToken::Minus)) {
01490     getLexer().Lex();
01491     NewOpcode = Name;
01492     NewOpcode += '-';
01493     Name = NewOpcode;
01494   }
01495   // If the instruction ends in a '.', we need to create a separate
01496   // token for it, to match what TableGen is doing.
01497   size_t Dot = Name.find('.');
01498   StringRef Mnemonic = Name.slice(0, Dot);
01499   if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
01500     Operands.push_back(
01501         PPCOperand::CreateTokenWithStringCopy(Mnemonic, NameLoc, isPPC64()));
01502   else
01503     Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64()));
01504   if (Dot != StringRef::npos) {
01505     SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot);
01506     StringRef DotStr = Name.slice(Dot, StringRef::npos);
01507     if (!NewOpcode.empty()) // Underlying memory for Name is volatile.
01508       Operands.push_back(
01509           PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64()));
01510     else
01511       Operands.push_back(PPCOperand::CreateToken(DotStr, DotLoc, isPPC64()));
01512   }
01513 
01514   // If there are no more operands then finish
01515   if (getLexer().is(AsmToken::EndOfStatement))
01516     return false;
01517 
01518   // Parse the first operand
01519   if (ParseOperand(Operands))
01520     return true;
01521 
01522   while (getLexer().isNot(AsmToken::EndOfStatement) &&
01523          getLexer().is(AsmToken::Comma)) {
01524     // Consume the comma token
01525     getLexer().Lex();
01526 
01527     // Parse the next operand
01528     if (ParseOperand(Operands))
01529       return true;
01530   }
01531 
01532   return false;
01533 }
01534 
01535 /// ParseDirective parses the PPC specific directives
01536 bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) {
01537   StringRef IDVal = DirectiveID.getIdentifier();
01538   if (!isDarwin()) {
01539     if (IDVal == ".word")
01540       return ParseDirectiveWord(2, DirectiveID.getLoc());
01541     if (IDVal == ".llong")
01542       return ParseDirectiveWord(8, DirectiveID.getLoc());
01543     if (IDVal == ".tc")
01544       return ParseDirectiveTC(isPPC64()? 8 : 4, DirectiveID.getLoc());
01545     if (IDVal == ".machine")
01546       return ParseDirectiveMachine(DirectiveID.getLoc());
01547     if (IDVal == ".abiversion")
01548       return ParseDirectiveAbiVersion(DirectiveID.getLoc());
01549     if (IDVal == ".localentry")
01550       return ParseDirectiveLocalEntry(DirectiveID.getLoc());
01551   } else {
01552     if (IDVal == ".machine")
01553       return ParseDarwinDirectiveMachine(DirectiveID.getLoc());
01554   }
01555   return true;
01556 }
01557 
01558 /// ParseDirectiveWord
01559 ///  ::= .word [ expression (, expression)* ]
01560 bool PPCAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
01561   if (getLexer().isNot(AsmToken::EndOfStatement)) {
01562     for (;;) {
01563       const MCExpr *Value;
01564       if (getParser().parseExpression(Value))
01565         return false;
01566 
01567       getParser().getStreamer().EmitValue(Value, Size);
01568 
01569       if (getLexer().is(AsmToken::EndOfStatement))
01570         break;
01571 
01572       if (getLexer().isNot(AsmToken::Comma))
01573         return Error(L, "unexpected token in directive");
01574       Parser.Lex();
01575     }
01576   }
01577 
01578   Parser.Lex();
01579   return false;
01580 }
01581 
01582 /// ParseDirectiveTC
01583 ///  ::= .tc [ symbol (, expression)* ]
01584 bool PPCAsmParser::ParseDirectiveTC(unsigned Size, SMLoc L) {
01585   // Skip TC symbol, which is only used with XCOFF.
01586   while (getLexer().isNot(AsmToken::EndOfStatement)
01587          && getLexer().isNot(AsmToken::Comma))
01588     Parser.Lex();
01589   if (getLexer().isNot(AsmToken::Comma)) {
01590     Error(L, "unexpected token in directive");
01591     return false;
01592   }
01593   Parser.Lex();
01594 
01595   // Align to word size.
01596   getParser().getStreamer().EmitValueToAlignment(Size);
01597 
01598   // Emit expressions.
01599   return ParseDirectiveWord(Size, L);
01600 }
01601 
01602 /// ParseDirectiveMachine (ELF platforms)
01603 ///  ::= .machine [ cpu | "push" | "pop" ]
01604 bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) {
01605   if (getLexer().isNot(AsmToken::Identifier) &&
01606       getLexer().isNot(AsmToken::String)) {
01607     Error(L, "unexpected token in directive");
01608     return false;
01609   }
01610 
01611   StringRef CPU = Parser.getTok().getIdentifier();
01612   Parser.Lex();
01613 
01614   // FIXME: Right now, the parser always allows any available
01615   // instruction, so the .machine directive is not useful.
01616   // Implement ".machine any" (by doing nothing) for the benefit
01617   // of existing assembler code.  Likewise, we can then implement
01618   // ".machine push" and ".machine pop" as no-op.
01619   if (CPU != "any" && CPU != "push" && CPU != "pop") {
01620     Error(L, "unrecognized machine type");
01621     return false;
01622   }
01623 
01624   if (getLexer().isNot(AsmToken::EndOfStatement)) {
01625     Error(L, "unexpected token in directive");
01626     return false;
01627   }
01628   PPCTargetStreamer &TStreamer =
01629       *static_cast<PPCTargetStreamer *>(
01630            getParser().getStreamer().getTargetStreamer());
01631   TStreamer.emitMachine(CPU);
01632 
01633   return false;
01634 }
01635 
01636 /// ParseDarwinDirectiveMachine (Mach-o platforms)
01637 ///  ::= .machine cpu-identifier
01638 bool PPCAsmParser::ParseDarwinDirectiveMachine(SMLoc L) {
01639   if (getLexer().isNot(AsmToken::Identifier) &&
01640       getLexer().isNot(AsmToken::String)) {
01641     Error(L, "unexpected token in directive");
01642     return false;
01643   }
01644 
01645   StringRef CPU = Parser.getTok().getIdentifier();
01646   Parser.Lex();
01647 
01648   // FIXME: this is only the 'default' set of cpu variants.
01649   // However we don't act on this information at present, this is simply
01650   // allowing parsing to proceed with minimal sanity checking.
01651   if (CPU != "ppc7400" && CPU != "ppc" && CPU != "ppc64") {
01652     Error(L, "unrecognized cpu type");
01653     return false;
01654   }
01655 
01656   if (isPPC64() && (CPU == "ppc7400" || CPU == "ppc")) {
01657     Error(L, "wrong cpu type specified for 64bit");
01658     return false;
01659   }
01660   if (!isPPC64() && CPU == "ppc64") {
01661     Error(L, "wrong cpu type specified for 32bit");
01662     return false;
01663   }
01664 
01665   if (getLexer().isNot(AsmToken::EndOfStatement)) {
01666     Error(L, "unexpected token in directive");
01667     return false;
01668   }
01669 
01670   return false;
01671 }
01672 
01673 /// ParseDirectiveAbiVersion
01674 ///  ::= .abiversion constant-expression
01675 bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L) {
01676   int64_t AbiVersion;
01677   if (getParser().parseAbsoluteExpression(AbiVersion)){
01678     Error(L, "expected constant expression");
01679     return false;
01680   }
01681   if (getLexer().isNot(AsmToken::EndOfStatement)) {
01682     Error(L, "unexpected token in directive");
01683     return false;
01684   }
01685 
01686   PPCTargetStreamer &TStreamer =
01687       *static_cast<PPCTargetStreamer *>(
01688            getParser().getStreamer().getTargetStreamer());
01689   TStreamer.emitAbiVersion(AbiVersion);
01690 
01691   return false;
01692 }
01693 
01694 /// ParseDirectiveLocalEntry
01695 ///  ::= .localentry symbol, expression
01696 bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L) {
01697   StringRef Name;
01698   if (getParser().parseIdentifier(Name)) {
01699     Error(L, "expected identifier in directive");
01700     return false;
01701   }
01702   MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
01703 
01704   if (getLexer().isNot(AsmToken::Comma)) {
01705     Error(L, "unexpected token in directive");
01706     return false;
01707   }
01708   Lex();
01709 
01710   const MCExpr *Expr;
01711   if (getParser().parseExpression(Expr)) {
01712     Error(L, "expected expression");
01713     return false;
01714   }
01715 
01716   if (getLexer().isNot(AsmToken::EndOfStatement)) {
01717     Error(L, "unexpected token in directive");
01718     return false;
01719   }
01720 
01721   PPCTargetStreamer &TStreamer =
01722       *static_cast<PPCTargetStreamer *>(
01723            getParser().getStreamer().getTargetStreamer());
01724   TStreamer.emitLocalEntry(Sym, Expr);
01725 
01726   return false;
01727 }
01728 
01729 
01730 
01731 /// Force static initialization.
01732 extern "C" void LLVMInitializePowerPCAsmParser() {
01733   RegisterMCAsmParser<PPCAsmParser> A(ThePPC32Target);
01734   RegisterMCAsmParser<PPCAsmParser> B(ThePPC64Target);
01735   RegisterMCAsmParser<PPCAsmParser> C(ThePPC64LETarget);
01736 }
01737 
01738 #define GET_REGISTER_MATCHER
01739 #define GET_MATCHER_IMPLEMENTATION
01740 #include "PPCGenAsmMatcher.inc"
01741 
01742 // Define this matcher function after the auto-generated include so we
01743 // have the match class enum definitions.
01744 unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
01745                                                   unsigned Kind) {
01746   // If the kind is a token for a literal immediate, check if our asm
01747   // operand matches. This is for InstAliases which have a fixed-value
01748   // immediate in the syntax.
01749   int64_t ImmVal;
01750   switch (Kind) {
01751     case MCK_0: ImmVal = 0; break;
01752     case MCK_1: ImmVal = 1; break;
01753     case MCK_2: ImmVal = 2; break;
01754     case MCK_3: ImmVal = 3; break;
01755     case MCK_4: ImmVal = 4; break;
01756     case MCK_5: ImmVal = 5; break;
01757     case MCK_6: ImmVal = 6; break;
01758     case MCK_7: ImmVal = 7; break;
01759     default: return Match_InvalidOperand;
01760   }
01761 
01762   PPCOperand &Op = static_cast<PPCOperand &>(AsmOp);
01763   if (Op.isImm() && Op.getImm() == ImmVal)
01764     return Match_Success;
01765 
01766   return Match_InvalidOperand;
01767 }
01768 
01769 const MCExpr *
01770 PPCAsmParser::applyModifierToExpr(const MCExpr *E,
01771                                   MCSymbolRefExpr::VariantKind Variant,
01772                                   MCContext &Ctx) {
01773   switch (Variant) {
01774   case MCSymbolRefExpr::VK_PPC_LO:
01775     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_LO, E, false, Ctx);
01776   case MCSymbolRefExpr::VK_PPC_HI:
01777     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HI, E, false, Ctx);
01778   case MCSymbolRefExpr::VK_PPC_HA:
01779     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HA, E, false, Ctx);
01780   case MCSymbolRefExpr::VK_PPC_HIGHER:
01781     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHER, E, false, Ctx);
01782   case MCSymbolRefExpr::VK_PPC_HIGHERA:
01783     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHERA, E, false, Ctx);
01784   case MCSymbolRefExpr::VK_PPC_HIGHEST:
01785     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHEST, E, false, Ctx);
01786   case MCSymbolRefExpr::VK_PPC_HIGHESTA:
01787     return PPCMCExpr::Create(PPCMCExpr::VK_PPC_HIGHESTA, E, false, Ctx);
01788   default:
01789     return nullptr;
01790   }
01791 }