1 //===-- PPCAsmParser.cpp - Parse PowerPC asm to MCInst instructions -------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "MCTargetDesc/PPCMCExpr.h" 10 #include "MCTargetDesc/PPCMCTargetDesc.h" 11 #include "PPCTargetStreamer.h" 12 #include "TargetInfo/PowerPCTargetInfo.h" 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/ADT/Twine.h" 15 #include "llvm/MC/MCContext.h" 16 #include "llvm/MC/MCExpr.h" 17 #include "llvm/MC/MCInst.h" 18 #include "llvm/MC/MCInstrInfo.h" 19 #include "llvm/MC/MCParser/MCAsmLexer.h" 20 #include "llvm/MC/MCParser/MCAsmParser.h" 21 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 22 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 23 #include "llvm/MC/MCStreamer.h" 24 #include "llvm/MC/MCSubtargetInfo.h" 25 #include "llvm/MC/MCSymbolELF.h" 26 #include "llvm/MC/TargetRegistry.h" 27 #include "llvm/Support/SourceMgr.h" 28 #include "llvm/Support/raw_ostream.h" 29 30 using namespace llvm; 31 32 DEFINE_PPC_REGCLASSES 33 34 // Evaluate an expression containing condition register 35 // or condition register field symbols. Returns positive 36 // value on success, or -1 on error. 37 static int64_t 38 EvaluateCRExpr(const MCExpr *E) { 39 switch (E->getKind()) { 40 case MCExpr::Target: 41 return -1; 42 43 case MCExpr::Constant: { 44 int64_t Res = cast<MCConstantExpr>(E)->getValue(); 45 return Res < 0 ? -1 : Res; 46 } 47 48 case MCExpr::SymbolRef: { 49 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); 50 StringRef Name = SRE->getSymbol().getName(); 51 52 if (Name == "lt") return 0; 53 if (Name == "gt") return 1; 54 if (Name == "eq") return 2; 55 if (Name == "so") return 3; 56 if (Name == "un") return 3; 57 58 if (Name == "cr0") return 0; 59 if (Name == "cr1") return 1; 60 if (Name == "cr2") return 2; 61 if (Name == "cr3") return 3; 62 if (Name == "cr4") return 4; 63 if (Name == "cr5") return 5; 64 if (Name == "cr6") return 6; 65 if (Name == "cr7") return 7; 66 67 return -1; 68 } 69 70 case MCExpr::Unary: 71 return -1; 72 73 case MCExpr::Binary: { 74 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); 75 int64_t LHSVal = EvaluateCRExpr(BE->getLHS()); 76 int64_t RHSVal = EvaluateCRExpr(BE->getRHS()); 77 int64_t Res; 78 79 if (LHSVal < 0 || RHSVal < 0) 80 return -1; 81 82 switch (BE->getOpcode()) { 83 default: return -1; 84 case MCBinaryExpr::Add: Res = LHSVal + RHSVal; break; 85 case MCBinaryExpr::Mul: Res = LHSVal * RHSVal; break; 86 } 87 88 return Res < 0 ? -1 : Res; 89 } 90 } 91 92 llvm_unreachable("Invalid expression kind!"); 93 } 94 95 namespace { 96 97 struct PPCOperand; 98 99 class PPCAsmParser : public MCTargetAsmParser { 100 bool IsPPC64; 101 102 void Warning(SMLoc L, const Twine &Msg) { getParser().Warning(L, Msg); } 103 104 bool isPPC64() const { return IsPPC64; } 105 106 bool MatchRegisterName(MCRegister &RegNo, int64_t &IntVal); 107 108 bool parseRegister(MCRegister &RegNo, SMLoc &StartLoc, 109 SMLoc &EndLoc) override; 110 OperandMatchResultTy tryParseRegister(MCRegister &RegNo, SMLoc &StartLoc, 111 SMLoc &EndLoc) override; 112 113 const MCExpr *ExtractModifierFromExpr(const MCExpr *E, 114 PPCMCExpr::VariantKind &Variant); 115 const MCExpr *FixupVariantKind(const MCExpr *E); 116 bool ParseExpression(const MCExpr *&EVal); 117 118 bool ParseOperand(OperandVector &Operands); 119 120 bool ParseDirectiveWord(unsigned Size, AsmToken ID); 121 bool ParseDirectiveTC(unsigned Size, AsmToken ID); 122 bool ParseDirectiveMachine(SMLoc L); 123 bool ParseDirectiveAbiVersion(SMLoc L); 124 bool ParseDirectiveLocalEntry(SMLoc L); 125 bool ParseGNUAttribute(SMLoc L); 126 127 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 128 OperandVector &Operands, MCStreamer &Out, 129 uint64_t &ErrorInfo, 130 bool MatchingInlineAsm) override; 131 132 void ProcessInstruction(MCInst &Inst, const OperandVector &Ops); 133 134 /// @name Auto-generated Match Functions 135 /// { 136 137 #define GET_ASSEMBLER_HEADER 138 #include "PPCGenAsmMatcher.inc" 139 140 /// } 141 142 143 public: 144 PPCAsmParser(const MCSubtargetInfo &STI, MCAsmParser &, 145 const MCInstrInfo &MII, const MCTargetOptions &Options) 146 : MCTargetAsmParser(Options, STI, MII) { 147 // Check for 64-bit vs. 32-bit pointer mode. 148 const Triple &TheTriple = STI.getTargetTriple(); 149 IsPPC64 = TheTriple.isPPC64(); 150 // Initialize the set of available features. 151 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); 152 } 153 154 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 155 SMLoc NameLoc, OperandVector &Operands) override; 156 157 bool ParseDirective(AsmToken DirectiveID) override; 158 159 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, 160 unsigned Kind) override; 161 162 const MCExpr *applyModifierToExpr(const MCExpr *E, 163 MCSymbolRefExpr::VariantKind, 164 MCContext &Ctx) override; 165 }; 166 167 /// PPCOperand - Instances of this class represent a parsed PowerPC machine 168 /// instruction. 169 struct PPCOperand : public MCParsedAsmOperand { 170 enum KindTy { 171 Token, 172 Immediate, 173 ContextImmediate, 174 Expression, 175 TLSRegister 176 } Kind; 177 178 SMLoc StartLoc, EndLoc; 179 bool IsPPC64; 180 181 struct TokOp { 182 const char *Data; 183 unsigned Length; 184 }; 185 186 struct ImmOp { 187 int64_t Val; 188 }; 189 190 struct ExprOp { 191 const MCExpr *Val; 192 int64_t CRVal; // Cached result of EvaluateCRExpr(Val) 193 }; 194 195 struct TLSRegOp { 196 const MCSymbolRefExpr *Sym; 197 }; 198 199 union { 200 struct TokOp Tok; 201 struct ImmOp Imm; 202 struct ExprOp Expr; 203 struct TLSRegOp TLSReg; 204 }; 205 206 PPCOperand(KindTy K) : Kind(K) {} 207 208 public: 209 PPCOperand(const PPCOperand &o) : MCParsedAsmOperand() { 210 Kind = o.Kind; 211 StartLoc = o.StartLoc; 212 EndLoc = o.EndLoc; 213 IsPPC64 = o.IsPPC64; 214 switch (Kind) { 215 case Token: 216 Tok = o.Tok; 217 break; 218 case Immediate: 219 case ContextImmediate: 220 Imm = o.Imm; 221 break; 222 case Expression: 223 Expr = o.Expr; 224 break; 225 case TLSRegister: 226 TLSReg = o.TLSReg; 227 break; 228 } 229 } 230 231 // Disable use of sized deallocation due to overallocation of PPCOperand 232 // objects in CreateTokenWithStringCopy. 233 void operator delete(void *p) { ::operator delete(p); } 234 235 /// getStartLoc - Get the location of the first token of this operand. 236 SMLoc getStartLoc() const override { return StartLoc; } 237 238 /// getEndLoc - Get the location of the last token of this operand. 239 SMLoc getEndLoc() const override { return EndLoc; } 240 241 /// getLocRange - Get the range between the first and last token of this 242 /// operand. 243 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); } 244 245 /// isPPC64 - True if this operand is for an instruction in 64-bit mode. 246 bool isPPC64() const { return IsPPC64; } 247 248 int64_t getImm() const { 249 assert(Kind == Immediate && "Invalid access!"); 250 return Imm.Val; 251 } 252 int64_t getImmS16Context() const { 253 assert((Kind == Immediate || Kind == ContextImmediate) && 254 "Invalid access!"); 255 if (Kind == Immediate) 256 return Imm.Val; 257 return static_cast<int16_t>(Imm.Val); 258 } 259 int64_t getImmU16Context() const { 260 assert((Kind == Immediate || Kind == ContextImmediate) && 261 "Invalid access!"); 262 return Imm.Val; 263 } 264 265 const MCExpr *getExpr() const { 266 assert(Kind == Expression && "Invalid access!"); 267 return Expr.Val; 268 } 269 270 int64_t getExprCRVal() const { 271 assert(Kind == Expression && "Invalid access!"); 272 return Expr.CRVal; 273 } 274 275 const MCExpr *getTLSReg() const { 276 assert(Kind == TLSRegister && "Invalid access!"); 277 return TLSReg.Sym; 278 } 279 280 unsigned getReg() const override { 281 assert(isRegNumber() && "Invalid access!"); 282 return (unsigned) Imm.Val; 283 } 284 285 unsigned getFpReg() const { 286 assert(isEvenRegNumber() && "Invalid access!"); 287 return (unsigned)(Imm.Val >> 1); 288 } 289 290 unsigned getVSReg() const { 291 assert(isVSRegNumber() && "Invalid access!"); 292 return (unsigned) Imm.Val; 293 } 294 295 unsigned getACCReg() const { 296 assert(isACCRegNumber() && "Invalid access!"); 297 return (unsigned) Imm.Val; 298 } 299 300 unsigned getDMRROWReg() const { 301 assert(isDMRROWRegNumber() && "Invalid access!"); 302 return (unsigned)Imm.Val; 303 } 304 305 unsigned getDMRROWpReg() const { 306 assert(isDMRROWpRegNumber() && "Invalid access!"); 307 return (unsigned)Imm.Val; 308 } 309 310 unsigned getDMRReg() const { 311 assert(isDMRRegNumber() && "Invalid access!"); 312 return (unsigned)Imm.Val; 313 } 314 315 unsigned getDMRpReg() const { 316 assert(isDMRpRegNumber() && "Invalid access!"); 317 return (unsigned)Imm.Val; 318 } 319 320 unsigned getVSRpEvenReg() const { 321 assert(isVSRpEvenRegNumber() && "Invalid access!"); 322 return (unsigned) Imm.Val >> 1; 323 } 324 325 unsigned getG8pReg() const { 326 assert(isEvenRegNumber() && "Invalid access!"); 327 return (unsigned)Imm.Val; 328 } 329 330 unsigned getCCReg() const { 331 assert(isCCRegNumber() && "Invalid access!"); 332 return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal); 333 } 334 335 unsigned getCRBit() const { 336 assert(isCRBitNumber() && "Invalid access!"); 337 return (unsigned) (Kind == Immediate ? Imm.Val : Expr.CRVal); 338 } 339 340 unsigned getCRBitMask() const { 341 assert(isCRBitMask() && "Invalid access!"); 342 return 7 - llvm::countr_zero<uint64_t>(Imm.Val); 343 } 344 345 bool isToken() const override { return Kind == Token; } 346 bool isImm() const override { 347 return Kind == Immediate || Kind == Expression; 348 } 349 bool isU1Imm() const { return Kind == Immediate && isUInt<1>(getImm()); } 350 bool isU2Imm() const { return Kind == Immediate && isUInt<2>(getImm()); } 351 bool isU3Imm() const { return Kind == Immediate && isUInt<3>(getImm()); } 352 bool isU4Imm() const { return Kind == Immediate && isUInt<4>(getImm()); } 353 bool isU5Imm() const { return Kind == Immediate && isUInt<5>(getImm()); } 354 bool isS5Imm() const { return Kind == Immediate && isInt<5>(getImm()); } 355 bool isU6Imm() const { return Kind == Immediate && isUInt<6>(getImm()); } 356 bool isU6ImmX2() const { return Kind == Immediate && 357 isUInt<6>(getImm()) && 358 (getImm() & 1) == 0; } 359 bool isU7Imm() const { return Kind == Immediate && isUInt<7>(getImm()); } 360 bool isU7ImmX4() const { return Kind == Immediate && 361 isUInt<7>(getImm()) && 362 (getImm() & 3) == 0; } 363 bool isU8Imm() const { return Kind == Immediate && isUInt<8>(getImm()); } 364 bool isU8ImmX8() const { return Kind == Immediate && 365 isUInt<8>(getImm()) && 366 (getImm() & 7) == 0; } 367 368 bool isU10Imm() const { return Kind == Immediate && isUInt<10>(getImm()); } 369 bool isU12Imm() const { return Kind == Immediate && isUInt<12>(getImm()); } 370 bool isU16Imm() const { return isExtImm<16>(/*Signed*/ false, 1); } 371 bool isS16Imm() const { return isExtImm<16>(/*Signed*/ true, 1); } 372 bool isS16ImmX4() const { return isExtImm<16>(/*Signed*/ true, 4); } 373 bool isS16ImmX16() const { return isExtImm<16>(/*Signed*/ true, 16); } 374 bool isS17Imm() const { return isExtImm<17>(/*Signed*/ true, 1); } 375 376 bool isHashImmX8() const { 377 // The Hash Imm form is used for instructions that check or store a hash. 378 // These instructions have a small immediate range that spans between 379 // -8 and -512. 380 return (Kind == Immediate && getImm() <= -8 && getImm() >= -512 && 381 (getImm() & 7) == 0); 382 } 383 384 bool isS34ImmX16() const { 385 return Kind == Expression || 386 (Kind == Immediate && isInt<34>(getImm()) && (getImm() & 15) == 0); 387 } 388 bool isS34Imm() const { 389 // Once the PC-Rel ABI is finalized, evaluate whether a 34-bit 390 // ContextImmediate is needed. 391 return Kind == Expression || (Kind == Immediate && isInt<34>(getImm())); 392 } 393 394 bool isTLSReg() const { return Kind == TLSRegister; } 395 bool isDirectBr() const { 396 if (Kind == Expression) 397 return true; 398 if (Kind != Immediate) 399 return false; 400 // Operand must be 64-bit aligned, signed 27-bit immediate. 401 if ((getImm() & 3) != 0) 402 return false; 403 if (isInt<26>(getImm())) 404 return true; 405 if (!IsPPC64) { 406 // In 32-bit mode, large 32-bit quantities wrap around. 407 if (isUInt<32>(getImm()) && isInt<26>(static_cast<int32_t>(getImm()))) 408 return true; 409 } 410 return false; 411 } 412 bool isCondBr() const { return Kind == Expression || 413 (Kind == Immediate && isInt<16>(getImm()) && 414 (getImm() & 3) == 0); } 415 bool isImmZero() const { return Kind == Immediate && getImm() == 0; } 416 bool isRegNumber() const { return Kind == Immediate && isUInt<5>(getImm()); } 417 bool isACCRegNumber() const { 418 return Kind == Immediate && isUInt<3>(getImm()); 419 } 420 bool isDMRROWRegNumber() const { 421 return Kind == Immediate && isUInt<6>(getImm()); 422 } 423 bool isDMRROWpRegNumber() const { 424 return Kind == Immediate && isUInt<5>(getImm()); 425 } 426 bool isDMRRegNumber() const { 427 return Kind == Immediate && isUInt<3>(getImm()); 428 } 429 bool isDMRpRegNumber() const { 430 return Kind == Immediate && isUInt<2>(getImm()); 431 } 432 bool isVSRpEvenRegNumber() const { 433 return Kind == Immediate && isUInt<6>(getImm()) && ((getImm() & 1) == 0); 434 } 435 bool isVSRegNumber() const { 436 return Kind == Immediate && isUInt<6>(getImm()); 437 } 438 bool isCCRegNumber() const { return (Kind == Expression 439 && isUInt<3>(getExprCRVal())) || 440 (Kind == Immediate 441 && isUInt<3>(getImm())); } 442 bool isCRBitNumber() const { return (Kind == Expression 443 && isUInt<5>(getExprCRVal())) || 444 (Kind == Immediate 445 && isUInt<5>(getImm())); } 446 447 bool isEvenRegNumber() const { return isRegNumber() && (getImm() & 1) == 0; } 448 449 bool isCRBitMask() const { 450 return Kind == Immediate && isUInt<8>(getImm()) && 451 llvm::has_single_bit<uint32_t>(getImm()); 452 } 453 bool isATBitsAsHint() const { return false; } 454 bool isMem() const override { return false; } 455 bool isReg() const override { return false; } 456 457 void addRegOperands(MCInst &Inst, unsigned N) const { 458 llvm_unreachable("addRegOperands"); 459 } 460 461 void addRegGPRCOperands(MCInst &Inst, unsigned N) const { 462 assert(N == 1 && "Invalid number of operands!"); 463 Inst.addOperand(MCOperand::createReg(RRegs[getReg()])); 464 } 465 466 void addRegGPRCNoR0Operands(MCInst &Inst, unsigned N) const { 467 assert(N == 1 && "Invalid number of operands!"); 468 Inst.addOperand(MCOperand::createReg(RRegsNoR0[getReg()])); 469 } 470 471 void addRegG8RCOperands(MCInst &Inst, unsigned N) const { 472 assert(N == 1 && "Invalid number of operands!"); 473 Inst.addOperand(MCOperand::createReg(XRegs[getReg()])); 474 } 475 476 void addRegG8RCNoX0Operands(MCInst &Inst, unsigned N) const { 477 assert(N == 1 && "Invalid number of operands!"); 478 Inst.addOperand(MCOperand::createReg(XRegsNoX0[getReg()])); 479 } 480 481 void addRegG8pRCOperands(MCInst &Inst, unsigned N) const { 482 assert(N == 1 && "Invalid number of operands!"); 483 Inst.addOperand(MCOperand::createReg(XRegs[getG8pReg()])); 484 } 485 486 void addRegGxRCOperands(MCInst &Inst, unsigned N) const { 487 if (isPPC64()) 488 addRegG8RCOperands(Inst, N); 489 else 490 addRegGPRCOperands(Inst, N); 491 } 492 493 void addRegGxRCNoR0Operands(MCInst &Inst, unsigned N) const { 494 if (isPPC64()) 495 addRegG8RCNoX0Operands(Inst, N); 496 else 497 addRegGPRCNoR0Operands(Inst, N); 498 } 499 500 void addRegF4RCOperands(MCInst &Inst, unsigned N) const { 501 assert(N == 1 && "Invalid number of operands!"); 502 Inst.addOperand(MCOperand::createReg(FRegs[getReg()])); 503 } 504 505 void addRegF8RCOperands(MCInst &Inst, unsigned N) const { 506 assert(N == 1 && "Invalid number of operands!"); 507 Inst.addOperand(MCOperand::createReg(FRegs[getReg()])); 508 } 509 510 void addRegFpRCOperands(MCInst &Inst, unsigned N) const { 511 assert(N == 1 && "Invalid number of operands!"); 512 Inst.addOperand(MCOperand::createReg(FpRegs[getFpReg()])); 513 } 514 515 void addRegVFRCOperands(MCInst &Inst, unsigned N) const { 516 assert(N == 1 && "Invalid number of operands!"); 517 Inst.addOperand(MCOperand::createReg(VFRegs[getReg()])); 518 } 519 520 void addRegVRRCOperands(MCInst &Inst, unsigned N) const { 521 assert(N == 1 && "Invalid number of operands!"); 522 Inst.addOperand(MCOperand::createReg(VRegs[getReg()])); 523 } 524 525 void addRegVSRCOperands(MCInst &Inst, unsigned N) const { 526 assert(N == 1 && "Invalid number of operands!"); 527 Inst.addOperand(MCOperand::createReg(VSRegs[getVSReg()])); 528 } 529 530 void addRegVSFRCOperands(MCInst &Inst, unsigned N) const { 531 assert(N == 1 && "Invalid number of operands!"); 532 Inst.addOperand(MCOperand::createReg(VSFRegs[getVSReg()])); 533 } 534 535 void addRegVSSRCOperands(MCInst &Inst, unsigned N) const { 536 assert(N == 1 && "Invalid number of operands!"); 537 Inst.addOperand(MCOperand::createReg(VSSRegs[getVSReg()])); 538 } 539 540 void addRegSPE4RCOperands(MCInst &Inst, unsigned N) const { 541 assert(N == 1 && "Invalid number of operands!"); 542 Inst.addOperand(MCOperand::createReg(RRegs[getReg()])); 543 } 544 545 void addRegSPERCOperands(MCInst &Inst, unsigned N) const { 546 assert(N == 1 && "Invalid number of operands!"); 547 Inst.addOperand(MCOperand::createReg(SPERegs[getReg()])); 548 } 549 550 void addRegACCRCOperands(MCInst &Inst, unsigned N) const { 551 assert(N == 1 && "Invalid number of operands!"); 552 Inst.addOperand(MCOperand::createReg(ACCRegs[getACCReg()])); 553 } 554 555 void addRegDMRROWRCOperands(MCInst &Inst, unsigned N) const { 556 assert(N == 1 && "Invalid number of operands!"); 557 Inst.addOperand(MCOperand::createReg(DMRROWRegs[getDMRROWReg()])); 558 } 559 560 void addRegDMRROWpRCOperands(MCInst &Inst, unsigned N) const { 561 assert(N == 1 && "Invalid number of operands!"); 562 Inst.addOperand(MCOperand::createReg(DMRROWpRegs[getDMRROWpReg()])); 563 } 564 565 void addRegDMRRCOperands(MCInst &Inst, unsigned N) const { 566 assert(N == 1 && "Invalid number of operands!"); 567 Inst.addOperand(MCOperand::createReg(DMRRegs[getDMRReg()])); 568 } 569 570 void addRegDMRpRCOperands(MCInst &Inst, unsigned N) const { 571 assert(N == 1 && "Invalid number of operands!"); 572 Inst.addOperand(MCOperand::createReg(DMRpRegs[getDMRpReg()])); 573 } 574 575 void addRegWACCRCOperands(MCInst &Inst, unsigned N) const { 576 assert(N == 1 && "Invalid number of operands!"); 577 Inst.addOperand(MCOperand::createReg(WACCRegs[getACCReg()])); 578 } 579 580 void addRegWACC_HIRCOperands(MCInst &Inst, unsigned N) const { 581 assert(N == 1 && "Invalid number of operands!"); 582 Inst.addOperand(MCOperand::createReg(WACC_HIRegs[getACCReg()])); 583 } 584 585 void addRegVSRpRCOperands(MCInst &Inst, unsigned N) const { 586 assert(N == 1 && "Invalid number of operands!"); 587 Inst.addOperand(MCOperand::createReg(VSRpRegs[getVSRpEvenReg()])); 588 } 589 590 void addRegVSRpEvenRCOperands(MCInst &Inst, unsigned N) const { 591 assert(N == 1 && "Invalid number of operands!"); 592 Inst.addOperand(MCOperand::createReg(VSRpRegs[getVSRpEvenReg()])); 593 } 594 595 void addRegCRBITRCOperands(MCInst &Inst, unsigned N) const { 596 assert(N == 1 && "Invalid number of operands!"); 597 Inst.addOperand(MCOperand::createReg(CRBITRegs[getCRBit()])); 598 } 599 600 void addRegCRRCOperands(MCInst &Inst, unsigned N) const { 601 assert(N == 1 && "Invalid number of operands!"); 602 Inst.addOperand(MCOperand::createReg(CRRegs[getCCReg()])); 603 } 604 605 void addCRBitMaskOperands(MCInst &Inst, unsigned N) const { 606 assert(N == 1 && "Invalid number of operands!"); 607 Inst.addOperand(MCOperand::createReg(CRRegs[getCRBitMask()])); 608 } 609 610 void addImmOperands(MCInst &Inst, unsigned N) const { 611 assert(N == 1 && "Invalid number of operands!"); 612 if (Kind == Immediate) 613 Inst.addOperand(MCOperand::createImm(getImm())); 614 else 615 Inst.addOperand(MCOperand::createExpr(getExpr())); 616 } 617 618 void addS16ImmOperands(MCInst &Inst, unsigned N) const { 619 assert(N == 1 && "Invalid number of operands!"); 620 switch (Kind) { 621 case Immediate: 622 Inst.addOperand(MCOperand::createImm(getImm())); 623 break; 624 case ContextImmediate: 625 Inst.addOperand(MCOperand::createImm(getImmS16Context())); 626 break; 627 default: 628 Inst.addOperand(MCOperand::createExpr(getExpr())); 629 break; 630 } 631 } 632 633 void addU16ImmOperands(MCInst &Inst, unsigned N) const { 634 assert(N == 1 && "Invalid number of operands!"); 635 switch (Kind) { 636 case Immediate: 637 Inst.addOperand(MCOperand::createImm(getImm())); 638 break; 639 case ContextImmediate: 640 Inst.addOperand(MCOperand::createImm(getImmU16Context())); 641 break; 642 default: 643 Inst.addOperand(MCOperand::createExpr(getExpr())); 644 break; 645 } 646 } 647 648 void addBranchTargetOperands(MCInst &Inst, unsigned N) const { 649 assert(N == 1 && "Invalid number of operands!"); 650 if (Kind == Immediate) 651 Inst.addOperand(MCOperand::createImm(getImm() / 4)); 652 else 653 Inst.addOperand(MCOperand::createExpr(getExpr())); 654 } 655 656 void addTLSRegOperands(MCInst &Inst, unsigned N) const { 657 assert(N == 1 && "Invalid number of operands!"); 658 Inst.addOperand(MCOperand::createExpr(getTLSReg())); 659 } 660 661 StringRef getToken() const { 662 assert(Kind == Token && "Invalid access!"); 663 return StringRef(Tok.Data, Tok.Length); 664 } 665 666 void print(raw_ostream &OS) const override; 667 668 static std::unique_ptr<PPCOperand> CreateToken(StringRef Str, SMLoc S, 669 bool IsPPC64) { 670 auto Op = std::make_unique<PPCOperand>(Token); 671 Op->Tok.Data = Str.data(); 672 Op->Tok.Length = Str.size(); 673 Op->StartLoc = S; 674 Op->EndLoc = S; 675 Op->IsPPC64 = IsPPC64; 676 return Op; 677 } 678 679 static std::unique_ptr<PPCOperand> 680 CreateTokenWithStringCopy(StringRef Str, SMLoc S, bool IsPPC64) { 681 // Allocate extra memory for the string and copy it. 682 // FIXME: This is incorrect, Operands are owned by unique_ptr with a default 683 // deleter which will destroy them by simply using "delete", not correctly 684 // calling operator delete on this extra memory after calling the dtor 685 // explicitly. 686 void *Mem = ::operator new(sizeof(PPCOperand) + Str.size()); 687 std::unique_ptr<PPCOperand> Op(new (Mem) PPCOperand(Token)); 688 Op->Tok.Data = reinterpret_cast<const char *>(Op.get() + 1); 689 Op->Tok.Length = Str.size(); 690 std::memcpy(const_cast<char *>(Op->Tok.Data), Str.data(), Str.size()); 691 Op->StartLoc = S; 692 Op->EndLoc = S; 693 Op->IsPPC64 = IsPPC64; 694 return Op; 695 } 696 697 static std::unique_ptr<PPCOperand> CreateImm(int64_t Val, SMLoc S, SMLoc E, 698 bool IsPPC64) { 699 auto Op = std::make_unique<PPCOperand>(Immediate); 700 Op->Imm.Val = Val; 701 Op->StartLoc = S; 702 Op->EndLoc = E; 703 Op->IsPPC64 = IsPPC64; 704 return Op; 705 } 706 707 static std::unique_ptr<PPCOperand> CreateExpr(const MCExpr *Val, SMLoc S, 708 SMLoc E, bool IsPPC64) { 709 auto Op = std::make_unique<PPCOperand>(Expression); 710 Op->Expr.Val = Val; 711 Op->Expr.CRVal = EvaluateCRExpr(Val); 712 Op->StartLoc = S; 713 Op->EndLoc = E; 714 Op->IsPPC64 = IsPPC64; 715 return Op; 716 } 717 718 static std::unique_ptr<PPCOperand> 719 CreateTLSReg(const MCSymbolRefExpr *Sym, SMLoc S, SMLoc E, bool IsPPC64) { 720 auto Op = std::make_unique<PPCOperand>(TLSRegister); 721 Op->TLSReg.Sym = Sym; 722 Op->StartLoc = S; 723 Op->EndLoc = E; 724 Op->IsPPC64 = IsPPC64; 725 return Op; 726 } 727 728 static std::unique_ptr<PPCOperand> 729 CreateContextImm(int64_t Val, SMLoc S, SMLoc E, bool IsPPC64) { 730 auto Op = std::make_unique<PPCOperand>(ContextImmediate); 731 Op->Imm.Val = Val; 732 Op->StartLoc = S; 733 Op->EndLoc = E; 734 Op->IsPPC64 = IsPPC64; 735 return Op; 736 } 737 738 static std::unique_ptr<PPCOperand> 739 CreateFromMCExpr(const MCExpr *Val, SMLoc S, SMLoc E, bool IsPPC64) { 740 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Val)) 741 return CreateImm(CE->getValue(), S, E, IsPPC64); 742 743 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Val)) 744 if (SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS || 745 SRE->getKind() == MCSymbolRefExpr::VK_PPC_TLS_PCREL) 746 return CreateTLSReg(SRE, S, E, IsPPC64); 747 748 if (const PPCMCExpr *TE = dyn_cast<PPCMCExpr>(Val)) { 749 int64_t Res; 750 if (TE->evaluateAsConstant(Res)) 751 return CreateContextImm(Res, S, E, IsPPC64); 752 } 753 754 return CreateExpr(Val, S, E, IsPPC64); 755 } 756 757 private: 758 template <unsigned Width> 759 bool isExtImm(bool Signed, unsigned Multiple) const { 760 switch (Kind) { 761 default: 762 return false; 763 case Expression: 764 return true; 765 case Immediate: 766 case ContextImmediate: 767 if (Signed) 768 return isInt<Width>(getImmS16Context()) && 769 (getImmS16Context() & (Multiple - 1)) == 0; 770 else 771 return isUInt<Width>(getImmU16Context()) && 772 (getImmU16Context() & (Multiple - 1)) == 0; 773 } 774 } 775 }; 776 777 } // end anonymous namespace. 778 779 void PPCOperand::print(raw_ostream &OS) const { 780 switch (Kind) { 781 case Token: 782 OS << "'" << getToken() << "'"; 783 break; 784 case Immediate: 785 case ContextImmediate: 786 OS << getImm(); 787 break; 788 case Expression: 789 OS << *getExpr(); 790 break; 791 case TLSRegister: 792 OS << *getTLSReg(); 793 break; 794 } 795 } 796 797 static void 798 addNegOperand(MCInst &Inst, MCOperand &Op, MCContext &Ctx) { 799 if (Op.isImm()) { 800 Inst.addOperand(MCOperand::createImm(-Op.getImm())); 801 return; 802 } 803 const MCExpr *Expr = Op.getExpr(); 804 if (const MCUnaryExpr *UnExpr = dyn_cast<MCUnaryExpr>(Expr)) { 805 if (UnExpr->getOpcode() == MCUnaryExpr::Minus) { 806 Inst.addOperand(MCOperand::createExpr(UnExpr->getSubExpr())); 807 return; 808 } 809 } else if (const MCBinaryExpr *BinExpr = dyn_cast<MCBinaryExpr>(Expr)) { 810 if (BinExpr->getOpcode() == MCBinaryExpr::Sub) { 811 const MCExpr *NE = MCBinaryExpr::createSub(BinExpr->getRHS(), 812 BinExpr->getLHS(), Ctx); 813 Inst.addOperand(MCOperand::createExpr(NE)); 814 return; 815 } 816 } 817 Inst.addOperand(MCOperand::createExpr(MCUnaryExpr::createMinus(Expr, Ctx))); 818 } 819 820 void PPCAsmParser::ProcessInstruction(MCInst &Inst, 821 const OperandVector &Operands) { 822 int Opcode = Inst.getOpcode(); 823 switch (Opcode) { 824 case PPC::DCBTx: 825 case PPC::DCBTT: 826 case PPC::DCBTSTx: 827 case PPC::DCBTSTT: { 828 MCInst TmpInst; 829 TmpInst.setOpcode((Opcode == PPC::DCBTx || Opcode == PPC::DCBTT) ? 830 PPC::DCBT : PPC::DCBTST); 831 TmpInst.addOperand(MCOperand::createImm( 832 (Opcode == PPC::DCBTx || Opcode == PPC::DCBTSTx) ? 0 : 16)); 833 TmpInst.addOperand(Inst.getOperand(0)); 834 TmpInst.addOperand(Inst.getOperand(1)); 835 Inst = TmpInst; 836 break; 837 } 838 case PPC::DCBTCT: 839 case PPC::DCBTDS: { 840 MCInst TmpInst; 841 TmpInst.setOpcode(PPC::DCBT); 842 TmpInst.addOperand(Inst.getOperand(2)); 843 TmpInst.addOperand(Inst.getOperand(0)); 844 TmpInst.addOperand(Inst.getOperand(1)); 845 Inst = TmpInst; 846 break; 847 } 848 case PPC::DCBTSTCT: 849 case PPC::DCBTSTDS: { 850 MCInst TmpInst; 851 TmpInst.setOpcode(PPC::DCBTST); 852 TmpInst.addOperand(Inst.getOperand(2)); 853 TmpInst.addOperand(Inst.getOperand(0)); 854 TmpInst.addOperand(Inst.getOperand(1)); 855 Inst = TmpInst; 856 break; 857 } 858 case PPC::DCBFx: 859 case PPC::DCBFL: 860 case PPC::DCBFLP: 861 case PPC::DCBFPS: 862 case PPC::DCBSTPS: { 863 int L = 0; 864 if (Opcode == PPC::DCBFL) 865 L = 1; 866 else if (Opcode == PPC::DCBFLP) 867 L = 3; 868 else if (Opcode == PPC::DCBFPS) 869 L = 4; 870 else if (Opcode == PPC::DCBSTPS) 871 L = 6; 872 873 MCInst TmpInst; 874 TmpInst.setOpcode(PPC::DCBF); 875 TmpInst.addOperand(MCOperand::createImm(L)); 876 TmpInst.addOperand(Inst.getOperand(0)); 877 TmpInst.addOperand(Inst.getOperand(1)); 878 Inst = TmpInst; 879 break; 880 } 881 case PPC::LAx: { 882 MCInst TmpInst; 883 TmpInst.setOpcode(PPC::LA); 884 TmpInst.addOperand(Inst.getOperand(0)); 885 TmpInst.addOperand(Inst.getOperand(2)); 886 TmpInst.addOperand(Inst.getOperand(1)); 887 Inst = TmpInst; 888 break; 889 } 890 case PPC::SUBI: { 891 MCInst TmpInst; 892 TmpInst.setOpcode(PPC::ADDI); 893 TmpInst.addOperand(Inst.getOperand(0)); 894 TmpInst.addOperand(Inst.getOperand(1)); 895 addNegOperand(TmpInst, Inst.getOperand(2), getContext()); 896 Inst = TmpInst; 897 break; 898 } 899 case PPC::SUBIS: { 900 MCInst TmpInst; 901 TmpInst.setOpcode(PPC::ADDIS); 902 TmpInst.addOperand(Inst.getOperand(0)); 903 TmpInst.addOperand(Inst.getOperand(1)); 904 addNegOperand(TmpInst, Inst.getOperand(2), getContext()); 905 Inst = TmpInst; 906 break; 907 } 908 case PPC::SUBIC: { 909 MCInst TmpInst; 910 TmpInst.setOpcode(PPC::ADDIC); 911 TmpInst.addOperand(Inst.getOperand(0)); 912 TmpInst.addOperand(Inst.getOperand(1)); 913 addNegOperand(TmpInst, Inst.getOperand(2), getContext()); 914 Inst = TmpInst; 915 break; 916 } 917 case PPC::SUBIC_rec: { 918 MCInst TmpInst; 919 TmpInst.setOpcode(PPC::ADDIC_rec); 920 TmpInst.addOperand(Inst.getOperand(0)); 921 TmpInst.addOperand(Inst.getOperand(1)); 922 addNegOperand(TmpInst, Inst.getOperand(2), getContext()); 923 Inst = TmpInst; 924 break; 925 } 926 case PPC::EXTLWI: 927 case PPC::EXTLWI_rec: { 928 MCInst TmpInst; 929 int64_t N = Inst.getOperand(2).getImm(); 930 int64_t B = Inst.getOperand(3).getImm(); 931 TmpInst.setOpcode(Opcode == PPC::EXTLWI ? PPC::RLWINM : PPC::RLWINM_rec); 932 TmpInst.addOperand(Inst.getOperand(0)); 933 TmpInst.addOperand(Inst.getOperand(1)); 934 TmpInst.addOperand(MCOperand::createImm(B)); 935 TmpInst.addOperand(MCOperand::createImm(0)); 936 TmpInst.addOperand(MCOperand::createImm(N - 1)); 937 Inst = TmpInst; 938 break; 939 } 940 case PPC::EXTRWI: 941 case PPC::EXTRWI_rec: { 942 MCInst TmpInst; 943 int64_t N = Inst.getOperand(2).getImm(); 944 int64_t B = Inst.getOperand(3).getImm(); 945 TmpInst.setOpcode(Opcode == PPC::EXTRWI ? PPC::RLWINM : PPC::RLWINM_rec); 946 TmpInst.addOperand(Inst.getOperand(0)); 947 TmpInst.addOperand(Inst.getOperand(1)); 948 TmpInst.addOperand(MCOperand::createImm(B + N)); 949 TmpInst.addOperand(MCOperand::createImm(32 - N)); 950 TmpInst.addOperand(MCOperand::createImm(31)); 951 Inst = TmpInst; 952 break; 953 } 954 case PPC::INSLWI: 955 case PPC::INSLWI_rec: { 956 MCInst TmpInst; 957 int64_t N = Inst.getOperand(2).getImm(); 958 int64_t B = Inst.getOperand(3).getImm(); 959 TmpInst.setOpcode(Opcode == PPC::INSLWI ? PPC::RLWIMI : PPC::RLWIMI_rec); 960 TmpInst.addOperand(Inst.getOperand(0)); 961 TmpInst.addOperand(Inst.getOperand(0)); 962 TmpInst.addOperand(Inst.getOperand(1)); 963 TmpInst.addOperand(MCOperand::createImm(32 - B)); 964 TmpInst.addOperand(MCOperand::createImm(B)); 965 TmpInst.addOperand(MCOperand::createImm((B + N) - 1)); 966 Inst = TmpInst; 967 break; 968 } 969 case PPC::INSRWI: 970 case PPC::INSRWI_rec: { 971 MCInst TmpInst; 972 int64_t N = Inst.getOperand(2).getImm(); 973 int64_t B = Inst.getOperand(3).getImm(); 974 TmpInst.setOpcode(Opcode == PPC::INSRWI ? PPC::RLWIMI : PPC::RLWIMI_rec); 975 TmpInst.addOperand(Inst.getOperand(0)); 976 TmpInst.addOperand(Inst.getOperand(0)); 977 TmpInst.addOperand(Inst.getOperand(1)); 978 TmpInst.addOperand(MCOperand::createImm(32 - (B + N))); 979 TmpInst.addOperand(MCOperand::createImm(B)); 980 TmpInst.addOperand(MCOperand::createImm((B + N) - 1)); 981 Inst = TmpInst; 982 break; 983 } 984 case PPC::ROTRWI: 985 case PPC::ROTRWI_rec: { 986 MCInst TmpInst; 987 int64_t N = Inst.getOperand(2).getImm(); 988 TmpInst.setOpcode(Opcode == PPC::ROTRWI ? PPC::RLWINM : PPC::RLWINM_rec); 989 TmpInst.addOperand(Inst.getOperand(0)); 990 TmpInst.addOperand(Inst.getOperand(1)); 991 TmpInst.addOperand(MCOperand::createImm(32 - N)); 992 TmpInst.addOperand(MCOperand::createImm(0)); 993 TmpInst.addOperand(MCOperand::createImm(31)); 994 Inst = TmpInst; 995 break; 996 } 997 case PPC::SLWI: 998 case PPC::SLWI_rec: { 999 MCInst TmpInst; 1000 int64_t N = Inst.getOperand(2).getImm(); 1001 TmpInst.setOpcode(Opcode == PPC::SLWI ? PPC::RLWINM : PPC::RLWINM_rec); 1002 TmpInst.addOperand(Inst.getOperand(0)); 1003 TmpInst.addOperand(Inst.getOperand(1)); 1004 TmpInst.addOperand(MCOperand::createImm(N)); 1005 TmpInst.addOperand(MCOperand::createImm(0)); 1006 TmpInst.addOperand(MCOperand::createImm(31 - N)); 1007 Inst = TmpInst; 1008 break; 1009 } 1010 case PPC::SRWI: 1011 case PPC::SRWI_rec: { 1012 MCInst TmpInst; 1013 int64_t N = Inst.getOperand(2).getImm(); 1014 TmpInst.setOpcode(Opcode == PPC::SRWI ? PPC::RLWINM : PPC::RLWINM_rec); 1015 TmpInst.addOperand(Inst.getOperand(0)); 1016 TmpInst.addOperand(Inst.getOperand(1)); 1017 TmpInst.addOperand(MCOperand::createImm(32 - N)); 1018 TmpInst.addOperand(MCOperand::createImm(N)); 1019 TmpInst.addOperand(MCOperand::createImm(31)); 1020 Inst = TmpInst; 1021 break; 1022 } 1023 case PPC::CLRRWI: 1024 case PPC::CLRRWI_rec: { 1025 MCInst TmpInst; 1026 int64_t N = Inst.getOperand(2).getImm(); 1027 TmpInst.setOpcode(Opcode == PPC::CLRRWI ? PPC::RLWINM : PPC::RLWINM_rec); 1028 TmpInst.addOperand(Inst.getOperand(0)); 1029 TmpInst.addOperand(Inst.getOperand(1)); 1030 TmpInst.addOperand(MCOperand::createImm(0)); 1031 TmpInst.addOperand(MCOperand::createImm(0)); 1032 TmpInst.addOperand(MCOperand::createImm(31 - N)); 1033 Inst = TmpInst; 1034 break; 1035 } 1036 case PPC::CLRLSLWI: 1037 case PPC::CLRLSLWI_rec: { 1038 MCInst TmpInst; 1039 int64_t B = Inst.getOperand(2).getImm(); 1040 int64_t N = Inst.getOperand(3).getImm(); 1041 TmpInst.setOpcode(Opcode == PPC::CLRLSLWI ? PPC::RLWINM : PPC::RLWINM_rec); 1042 TmpInst.addOperand(Inst.getOperand(0)); 1043 TmpInst.addOperand(Inst.getOperand(1)); 1044 TmpInst.addOperand(MCOperand::createImm(N)); 1045 TmpInst.addOperand(MCOperand::createImm(B - N)); 1046 TmpInst.addOperand(MCOperand::createImm(31 - N)); 1047 Inst = TmpInst; 1048 break; 1049 } 1050 case PPC::EXTLDI: 1051 case PPC::EXTLDI_rec: { 1052 MCInst TmpInst; 1053 int64_t N = Inst.getOperand(2).getImm(); 1054 int64_t B = Inst.getOperand(3).getImm(); 1055 TmpInst.setOpcode(Opcode == PPC::EXTLDI ? PPC::RLDICR : PPC::RLDICR_rec); 1056 TmpInst.addOperand(Inst.getOperand(0)); 1057 TmpInst.addOperand(Inst.getOperand(1)); 1058 TmpInst.addOperand(MCOperand::createImm(B)); 1059 TmpInst.addOperand(MCOperand::createImm(N - 1)); 1060 Inst = TmpInst; 1061 break; 1062 } 1063 case PPC::EXTRDI: 1064 case PPC::EXTRDI_rec: { 1065 MCInst TmpInst; 1066 int64_t N = Inst.getOperand(2).getImm(); 1067 int64_t B = Inst.getOperand(3).getImm(); 1068 TmpInst.setOpcode(Opcode == PPC::EXTRDI ? PPC::RLDICL : PPC::RLDICL_rec); 1069 TmpInst.addOperand(Inst.getOperand(0)); 1070 TmpInst.addOperand(Inst.getOperand(1)); 1071 TmpInst.addOperand(MCOperand::createImm(B + N)); 1072 TmpInst.addOperand(MCOperand::createImm(64 - N)); 1073 Inst = TmpInst; 1074 break; 1075 } 1076 case PPC::INSRDI: 1077 case PPC::INSRDI_rec: { 1078 MCInst TmpInst; 1079 int64_t N = Inst.getOperand(2).getImm(); 1080 int64_t B = Inst.getOperand(3).getImm(); 1081 TmpInst.setOpcode(Opcode == PPC::INSRDI ? PPC::RLDIMI : PPC::RLDIMI_rec); 1082 TmpInst.addOperand(Inst.getOperand(0)); 1083 TmpInst.addOperand(Inst.getOperand(0)); 1084 TmpInst.addOperand(Inst.getOperand(1)); 1085 TmpInst.addOperand(MCOperand::createImm(64 - (B + N))); 1086 TmpInst.addOperand(MCOperand::createImm(B)); 1087 Inst = TmpInst; 1088 break; 1089 } 1090 case PPC::ROTRDI: 1091 case PPC::ROTRDI_rec: { 1092 MCInst TmpInst; 1093 int64_t N = Inst.getOperand(2).getImm(); 1094 TmpInst.setOpcode(Opcode == PPC::ROTRDI ? PPC::RLDICL : PPC::RLDICL_rec); 1095 TmpInst.addOperand(Inst.getOperand(0)); 1096 TmpInst.addOperand(Inst.getOperand(1)); 1097 TmpInst.addOperand(MCOperand::createImm(64 - N)); 1098 TmpInst.addOperand(MCOperand::createImm(0)); 1099 Inst = TmpInst; 1100 break; 1101 } 1102 case PPC::SLDI: 1103 case PPC::SLDI_rec: { 1104 MCInst TmpInst; 1105 int64_t N = Inst.getOperand(2).getImm(); 1106 TmpInst.setOpcode(Opcode == PPC::SLDI ? PPC::RLDICR : PPC::RLDICR_rec); 1107 TmpInst.addOperand(Inst.getOperand(0)); 1108 TmpInst.addOperand(Inst.getOperand(1)); 1109 TmpInst.addOperand(MCOperand::createImm(N)); 1110 TmpInst.addOperand(MCOperand::createImm(63 - N)); 1111 Inst = TmpInst; 1112 break; 1113 } 1114 case PPC::SUBPCIS: { 1115 MCInst TmpInst; 1116 int64_t N = Inst.getOperand(1).getImm(); 1117 TmpInst.setOpcode(PPC::ADDPCIS); 1118 TmpInst.addOperand(Inst.getOperand(0)); 1119 TmpInst.addOperand(MCOperand::createImm(-N)); 1120 Inst = TmpInst; 1121 break; 1122 } 1123 case PPC::SRDI: 1124 case PPC::SRDI_rec: { 1125 MCInst TmpInst; 1126 int64_t N = Inst.getOperand(2).getImm(); 1127 TmpInst.setOpcode(Opcode == PPC::SRDI ? PPC::RLDICL : PPC::RLDICL_rec); 1128 TmpInst.addOperand(Inst.getOperand(0)); 1129 TmpInst.addOperand(Inst.getOperand(1)); 1130 TmpInst.addOperand(MCOperand::createImm(64 - N)); 1131 TmpInst.addOperand(MCOperand::createImm(N)); 1132 Inst = TmpInst; 1133 break; 1134 } 1135 case PPC::CLRRDI: 1136 case PPC::CLRRDI_rec: { 1137 MCInst TmpInst; 1138 int64_t N = Inst.getOperand(2).getImm(); 1139 TmpInst.setOpcode(Opcode == PPC::CLRRDI ? PPC::RLDICR : PPC::RLDICR_rec); 1140 TmpInst.addOperand(Inst.getOperand(0)); 1141 TmpInst.addOperand(Inst.getOperand(1)); 1142 TmpInst.addOperand(MCOperand::createImm(0)); 1143 TmpInst.addOperand(MCOperand::createImm(63 - N)); 1144 Inst = TmpInst; 1145 break; 1146 } 1147 case PPC::CLRLSLDI: 1148 case PPC::CLRLSLDI_rec: { 1149 MCInst TmpInst; 1150 int64_t B = Inst.getOperand(2).getImm(); 1151 int64_t N = Inst.getOperand(3).getImm(); 1152 TmpInst.setOpcode(Opcode == PPC::CLRLSLDI ? PPC::RLDIC : PPC::RLDIC_rec); 1153 TmpInst.addOperand(Inst.getOperand(0)); 1154 TmpInst.addOperand(Inst.getOperand(1)); 1155 TmpInst.addOperand(MCOperand::createImm(N)); 1156 TmpInst.addOperand(MCOperand::createImm(B - N)); 1157 Inst = TmpInst; 1158 break; 1159 } 1160 case PPC::RLWINMbm: 1161 case PPC::RLWINMbm_rec: { 1162 unsigned MB, ME; 1163 int64_t BM = Inst.getOperand(3).getImm(); 1164 if (!isRunOfOnes(BM, MB, ME)) 1165 break; 1166 1167 MCInst TmpInst; 1168 TmpInst.setOpcode(Opcode == PPC::RLWINMbm ? PPC::RLWINM : PPC::RLWINM_rec); 1169 TmpInst.addOperand(Inst.getOperand(0)); 1170 TmpInst.addOperand(Inst.getOperand(1)); 1171 TmpInst.addOperand(Inst.getOperand(2)); 1172 TmpInst.addOperand(MCOperand::createImm(MB)); 1173 TmpInst.addOperand(MCOperand::createImm(ME)); 1174 Inst = TmpInst; 1175 break; 1176 } 1177 case PPC::RLWIMIbm: 1178 case PPC::RLWIMIbm_rec: { 1179 unsigned MB, ME; 1180 int64_t BM = Inst.getOperand(3).getImm(); 1181 if (!isRunOfOnes(BM, MB, ME)) 1182 break; 1183 1184 MCInst TmpInst; 1185 TmpInst.setOpcode(Opcode == PPC::RLWIMIbm ? PPC::RLWIMI : PPC::RLWIMI_rec); 1186 TmpInst.addOperand(Inst.getOperand(0)); 1187 TmpInst.addOperand(Inst.getOperand(0)); // The tied operand. 1188 TmpInst.addOperand(Inst.getOperand(1)); 1189 TmpInst.addOperand(Inst.getOperand(2)); 1190 TmpInst.addOperand(MCOperand::createImm(MB)); 1191 TmpInst.addOperand(MCOperand::createImm(ME)); 1192 Inst = TmpInst; 1193 break; 1194 } 1195 case PPC::RLWNMbm: 1196 case PPC::RLWNMbm_rec: { 1197 unsigned MB, ME; 1198 int64_t BM = Inst.getOperand(3).getImm(); 1199 if (!isRunOfOnes(BM, MB, ME)) 1200 break; 1201 1202 MCInst TmpInst; 1203 TmpInst.setOpcode(Opcode == PPC::RLWNMbm ? PPC::RLWNM : PPC::RLWNM_rec); 1204 TmpInst.addOperand(Inst.getOperand(0)); 1205 TmpInst.addOperand(Inst.getOperand(1)); 1206 TmpInst.addOperand(Inst.getOperand(2)); 1207 TmpInst.addOperand(MCOperand::createImm(MB)); 1208 TmpInst.addOperand(MCOperand::createImm(ME)); 1209 Inst = TmpInst; 1210 break; 1211 } 1212 case PPC::MFTB: { 1213 if (getSTI().hasFeature(PPC::FeatureMFTB)) { 1214 assert(Inst.getNumOperands() == 2 && "Expecting two operands"); 1215 Inst.setOpcode(PPC::MFSPR); 1216 } 1217 break; 1218 } 1219 } 1220 } 1221 1222 static std::string PPCMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS, 1223 unsigned VariantID = 0); 1224 1225 bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 1226 OperandVector &Operands, 1227 MCStreamer &Out, uint64_t &ErrorInfo, 1228 bool MatchingInlineAsm) { 1229 MCInst Inst; 1230 1231 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm)) { 1232 case Match_Success: 1233 // Post-process instructions (typically extended mnemonics) 1234 ProcessInstruction(Inst, Operands); 1235 Inst.setLoc(IDLoc); 1236 Out.emitInstruction(Inst, getSTI()); 1237 return false; 1238 case Match_MissingFeature: 1239 return Error(IDLoc, "instruction use requires an option to be enabled"); 1240 case Match_MnemonicFail: { 1241 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits()); 1242 std::string Suggestion = PPCMnemonicSpellCheck( 1243 ((PPCOperand &)*Operands[0]).getToken(), FBS); 1244 return Error(IDLoc, "invalid instruction" + Suggestion, 1245 ((PPCOperand &)*Operands[0]).getLocRange()); 1246 } 1247 case Match_InvalidOperand: { 1248 SMLoc ErrorLoc = IDLoc; 1249 if (ErrorInfo != ~0ULL) { 1250 if (ErrorInfo >= Operands.size()) 1251 return Error(IDLoc, "too few operands for instruction"); 1252 1253 ErrorLoc = ((PPCOperand &)*Operands[ErrorInfo]).getStartLoc(); 1254 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; 1255 } 1256 1257 return Error(ErrorLoc, "invalid operand for instruction"); 1258 } 1259 } 1260 1261 llvm_unreachable("Implement any new match types added!"); 1262 } 1263 1264 bool PPCAsmParser::MatchRegisterName(MCRegister &RegNo, int64_t &IntVal) { 1265 if (getParser().getTok().is(AsmToken::Percent)) 1266 getParser().Lex(); // Eat the '%'. 1267 1268 if (!getParser().getTok().is(AsmToken::Identifier)) 1269 return true; 1270 1271 StringRef Name = getParser().getTok().getString(); 1272 if (Name.equals_insensitive("lr")) { 1273 RegNo = isPPC64() ? PPC::LR8 : PPC::LR; 1274 IntVal = 8; 1275 } else if (Name.equals_insensitive("ctr")) { 1276 RegNo = isPPC64() ? PPC::CTR8 : PPC::CTR; 1277 IntVal = 9; 1278 } else if (Name.equals_insensitive("vrsave")) { 1279 RegNo = PPC::VRSAVE; 1280 IntVal = 256; 1281 } else if (Name.starts_with_insensitive("r") && 1282 !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) { 1283 RegNo = isPPC64() ? XRegs[IntVal] : RRegs[IntVal]; 1284 } else if (Name.starts_with_insensitive("f") && 1285 !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) { 1286 RegNo = FRegs[IntVal]; 1287 } else if (Name.starts_with_insensitive("vs") && 1288 !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 64) { 1289 RegNo = VSRegs[IntVal]; 1290 } else if (Name.starts_with_insensitive("v") && 1291 !Name.substr(1).getAsInteger(10, IntVal) && IntVal < 32) { 1292 RegNo = VRegs[IntVal]; 1293 } else if (Name.starts_with_insensitive("cr") && 1294 !Name.substr(2).getAsInteger(10, IntVal) && IntVal < 8) { 1295 RegNo = CRRegs[IntVal]; 1296 } else if (Name.starts_with_insensitive("acc") && 1297 !Name.substr(3).getAsInteger(10, IntVal) && IntVal < 8) { 1298 RegNo = ACCRegs[IntVal]; 1299 } else if (Name.starts_with_insensitive("wacc_hi") && 1300 !Name.substr(7).getAsInteger(10, IntVal) && IntVal < 8) { 1301 RegNo = ACCRegs[IntVal]; 1302 } else if (Name.starts_with_insensitive("wacc") && 1303 !Name.substr(4).getAsInteger(10, IntVal) && IntVal < 8) { 1304 RegNo = WACCRegs[IntVal]; 1305 } else if (Name.starts_with_insensitive("dmrrowp") && 1306 !Name.substr(7).getAsInteger(10, IntVal) && IntVal < 32) { 1307 RegNo = DMRROWpRegs[IntVal]; 1308 } else if (Name.starts_with_insensitive("dmrrow") && 1309 !Name.substr(6).getAsInteger(10, IntVal) && IntVal < 64) { 1310 RegNo = DMRROWRegs[IntVal]; 1311 } else if (Name.starts_with_insensitive("dmrp") && 1312 !Name.substr(4).getAsInteger(10, IntVal) && IntVal < 4) { 1313 RegNo = DMRROWpRegs[IntVal]; 1314 } else if (Name.starts_with_insensitive("dmr") && 1315 !Name.substr(3).getAsInteger(10, IntVal) && IntVal < 8) { 1316 RegNo = DMRRegs[IntVal]; 1317 } else 1318 return true; 1319 getParser().Lex(); 1320 return false; 1321 } 1322 1323 bool PPCAsmParser::parseRegister(MCRegister &RegNo, SMLoc &StartLoc, 1324 SMLoc &EndLoc) { 1325 if (tryParseRegister(RegNo, StartLoc, EndLoc) != MatchOperand_Success) 1326 return TokError("invalid register name"); 1327 return false; 1328 } 1329 1330 OperandMatchResultTy PPCAsmParser::tryParseRegister(MCRegister &RegNo, 1331 SMLoc &StartLoc, 1332 SMLoc &EndLoc) { 1333 const AsmToken &Tok = getParser().getTok(); 1334 StartLoc = Tok.getLoc(); 1335 EndLoc = Tok.getEndLoc(); 1336 RegNo = 0; 1337 int64_t IntVal; 1338 if (MatchRegisterName(RegNo, IntVal)) 1339 return MatchOperand_NoMatch; 1340 return MatchOperand_Success; 1341 } 1342 1343 /// Extract \code @l/@ha \endcode modifier from expression. Recursively scan 1344 /// the expression and check for VK_PPC_LO/HI/HA 1345 /// symbol variants. If all symbols with modifier use the same 1346 /// variant, return the corresponding PPCMCExpr::VariantKind, 1347 /// and a modified expression using the default symbol variant. 1348 /// Otherwise, return NULL. 1349 const MCExpr *PPCAsmParser:: 1350 ExtractModifierFromExpr(const MCExpr *E, 1351 PPCMCExpr::VariantKind &Variant) { 1352 MCContext &Context = getParser().getContext(); 1353 Variant = PPCMCExpr::VK_PPC_None; 1354 1355 switch (E->getKind()) { 1356 case MCExpr::Target: 1357 case MCExpr::Constant: 1358 return nullptr; 1359 1360 case MCExpr::SymbolRef: { 1361 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); 1362 1363 switch (SRE->getKind()) { 1364 case MCSymbolRefExpr::VK_PPC_LO: 1365 Variant = PPCMCExpr::VK_PPC_LO; 1366 break; 1367 case MCSymbolRefExpr::VK_PPC_HI: 1368 Variant = PPCMCExpr::VK_PPC_HI; 1369 break; 1370 case MCSymbolRefExpr::VK_PPC_HA: 1371 Variant = PPCMCExpr::VK_PPC_HA; 1372 break; 1373 case MCSymbolRefExpr::VK_PPC_HIGH: 1374 Variant = PPCMCExpr::VK_PPC_HIGH; 1375 break; 1376 case MCSymbolRefExpr::VK_PPC_HIGHA: 1377 Variant = PPCMCExpr::VK_PPC_HIGHA; 1378 break; 1379 case MCSymbolRefExpr::VK_PPC_HIGHER: 1380 Variant = PPCMCExpr::VK_PPC_HIGHER; 1381 break; 1382 case MCSymbolRefExpr::VK_PPC_HIGHERA: 1383 Variant = PPCMCExpr::VK_PPC_HIGHERA; 1384 break; 1385 case MCSymbolRefExpr::VK_PPC_HIGHEST: 1386 Variant = PPCMCExpr::VK_PPC_HIGHEST; 1387 break; 1388 case MCSymbolRefExpr::VK_PPC_HIGHESTA: 1389 Variant = PPCMCExpr::VK_PPC_HIGHESTA; 1390 break; 1391 default: 1392 return nullptr; 1393 } 1394 1395 return MCSymbolRefExpr::create(&SRE->getSymbol(), Context); 1396 } 1397 1398 case MCExpr::Unary: { 1399 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); 1400 const MCExpr *Sub = ExtractModifierFromExpr(UE->getSubExpr(), Variant); 1401 if (!Sub) 1402 return nullptr; 1403 return MCUnaryExpr::create(UE->getOpcode(), Sub, Context); 1404 } 1405 1406 case MCExpr::Binary: { 1407 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); 1408 PPCMCExpr::VariantKind LHSVariant, RHSVariant; 1409 const MCExpr *LHS = ExtractModifierFromExpr(BE->getLHS(), LHSVariant); 1410 const MCExpr *RHS = ExtractModifierFromExpr(BE->getRHS(), RHSVariant); 1411 1412 if (!LHS && !RHS) 1413 return nullptr; 1414 1415 if (!LHS) LHS = BE->getLHS(); 1416 if (!RHS) RHS = BE->getRHS(); 1417 1418 if (LHSVariant == PPCMCExpr::VK_PPC_None) 1419 Variant = RHSVariant; 1420 else if (RHSVariant == PPCMCExpr::VK_PPC_None) 1421 Variant = LHSVariant; 1422 else if (LHSVariant == RHSVariant) 1423 Variant = LHSVariant; 1424 else 1425 return nullptr; 1426 1427 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context); 1428 } 1429 } 1430 1431 llvm_unreachable("Invalid expression kind!"); 1432 } 1433 1434 /// Find all VK_TLSGD/VK_TLSLD symbol references in expression and replace 1435 /// them by VK_PPC_TLSGD/VK_PPC_TLSLD. This is necessary to avoid having 1436 /// _GLOBAL_OFFSET_TABLE_ created via ELFObjectWriter::RelocNeedsGOT. 1437 /// FIXME: This is a hack. 1438 const MCExpr *PPCAsmParser:: 1439 FixupVariantKind(const MCExpr *E) { 1440 MCContext &Context = getParser().getContext(); 1441 1442 switch (E->getKind()) { 1443 case MCExpr::Target: 1444 case MCExpr::Constant: 1445 return E; 1446 1447 case MCExpr::SymbolRef: { 1448 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); 1449 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1450 1451 switch (SRE->getKind()) { 1452 case MCSymbolRefExpr::VK_TLSGD: 1453 Variant = MCSymbolRefExpr::VK_PPC_TLSGD; 1454 break; 1455 case MCSymbolRefExpr::VK_TLSLD: 1456 Variant = MCSymbolRefExpr::VK_PPC_TLSLD; 1457 break; 1458 default: 1459 return E; 1460 } 1461 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, Context); 1462 } 1463 1464 case MCExpr::Unary: { 1465 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); 1466 const MCExpr *Sub = FixupVariantKind(UE->getSubExpr()); 1467 if (Sub == UE->getSubExpr()) 1468 return E; 1469 return MCUnaryExpr::create(UE->getOpcode(), Sub, Context); 1470 } 1471 1472 case MCExpr::Binary: { 1473 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); 1474 const MCExpr *LHS = FixupVariantKind(BE->getLHS()); 1475 const MCExpr *RHS = FixupVariantKind(BE->getRHS()); 1476 if (LHS == BE->getLHS() && RHS == BE->getRHS()) 1477 return E; 1478 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, Context); 1479 } 1480 } 1481 1482 llvm_unreachable("Invalid expression kind!"); 1483 } 1484 1485 /// ParseExpression. This differs from the default "parseExpression" in that 1486 /// it handles modifiers. 1487 bool PPCAsmParser:: 1488 ParseExpression(const MCExpr *&EVal) { 1489 // (ELF Platforms) 1490 // Handle \code @l/@ha \endcode 1491 if (getParser().parseExpression(EVal)) 1492 return true; 1493 1494 EVal = FixupVariantKind(EVal); 1495 1496 PPCMCExpr::VariantKind Variant; 1497 const MCExpr *E = ExtractModifierFromExpr(EVal, Variant); 1498 if (E) 1499 EVal = PPCMCExpr::create(Variant, E, getParser().getContext()); 1500 1501 return false; 1502 } 1503 1504 /// ParseOperand 1505 /// This handles registers in the form 'NN', '%rNN' for ELF platforms and 1506 /// rNN for MachO. 1507 bool PPCAsmParser::ParseOperand(OperandVector &Operands) { 1508 MCAsmParser &Parser = getParser(); 1509 SMLoc S = Parser.getTok().getLoc(); 1510 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 1511 const MCExpr *EVal; 1512 1513 // Attempt to parse the next token as an immediate 1514 switch (getLexer().getKind()) { 1515 // Special handling for register names. These are interpreted 1516 // as immediates corresponding to the register number. 1517 case AsmToken::Percent: { 1518 MCRegister RegNo; 1519 int64_t IntVal; 1520 if (MatchRegisterName(RegNo, IntVal)) 1521 return Error(S, "invalid register name"); 1522 1523 Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64())); 1524 return false; 1525 } 1526 case AsmToken::Identifier: 1527 case AsmToken::LParen: 1528 case AsmToken::Plus: 1529 case AsmToken::Minus: 1530 case AsmToken::Integer: 1531 case AsmToken::Dot: 1532 case AsmToken::Dollar: 1533 case AsmToken::Exclaim: 1534 case AsmToken::Tilde: 1535 if (!ParseExpression(EVal)) 1536 break; 1537 // Fall-through 1538 [[fallthrough]]; 1539 default: 1540 return Error(S, "unknown operand"); 1541 } 1542 1543 // Push the parsed operand into the list of operands 1544 Operands.push_back(PPCOperand::CreateFromMCExpr(EVal, S, E, isPPC64())); 1545 1546 // Check whether this is a TLS call expression 1547 const char TlsGetAddr[] = "__tls_get_addr"; 1548 bool TlsCall = false; 1549 const MCExpr *TlsCallAddend = nullptr; 1550 if (auto *Ref = dyn_cast<MCSymbolRefExpr>(EVal)) { 1551 TlsCall = Ref->getSymbol().getName() == TlsGetAddr; 1552 } else if (auto *Bin = dyn_cast<MCBinaryExpr>(EVal); 1553 Bin && Bin->getOpcode() == MCBinaryExpr::Add) { 1554 if (auto *Ref = dyn_cast<MCSymbolRefExpr>(Bin->getLHS())) { 1555 TlsCall = Ref->getSymbol().getName() == TlsGetAddr; 1556 TlsCallAddend = Bin->getRHS(); 1557 } 1558 } 1559 1560 if (TlsCall && parseOptionalToken(AsmToken::LParen)) { 1561 const MCExpr *TLSSym; 1562 const SMLoc S2 = Parser.getTok().getLoc(); 1563 if (ParseExpression(TLSSym)) 1564 return Error(S2, "invalid TLS call expression"); 1565 E = Parser.getTok().getLoc(); 1566 if (parseToken(AsmToken::RParen, "expected ')'")) 1567 return true; 1568 // PPC32 allows bl __tls_get_addr[+a](x@tlsgd)@plt+b. Parse "@plt[+b]". 1569 if (!isPPC64() && parseOptionalToken(AsmToken::At)) { 1570 AsmToken Tok = getTok(); 1571 if (!(parseOptionalToken(AsmToken::Identifier) && 1572 Tok.getString().compare_insensitive("plt") == 0)) 1573 return Error(Tok.getLoc(), "expected 'plt'"); 1574 EVal = MCSymbolRefExpr::create(TlsGetAddr, MCSymbolRefExpr::VK_PLT, 1575 getContext()); 1576 if (parseOptionalToken(AsmToken::Plus)) { 1577 const MCExpr *Addend = nullptr; 1578 SMLoc EndLoc; 1579 if (parsePrimaryExpr(Addend, EndLoc)) 1580 return true; 1581 if (TlsCallAddend) // __tls_get_addr+a(x@tlsgd)@plt+b 1582 TlsCallAddend = 1583 MCBinaryExpr::createAdd(TlsCallAddend, Addend, getContext()); 1584 else // __tls_get_addr(x@tlsgd)@plt+b 1585 TlsCallAddend = Addend; 1586 } 1587 if (TlsCallAddend) 1588 EVal = MCBinaryExpr::createAdd(EVal, TlsCallAddend, getContext()); 1589 // Add a __tls_get_addr operand with addend a, b, or a+b. 1590 Operands.back() = PPCOperand::CreateFromMCExpr( 1591 EVal, S, Parser.getTok().getLoc(), false); 1592 } 1593 1594 Operands.push_back(PPCOperand::CreateFromMCExpr(TLSSym, S, E, isPPC64())); 1595 } 1596 1597 // Otherwise, check for D-form memory operands 1598 if (!TlsCall && parseOptionalToken(AsmToken::LParen)) { 1599 S = Parser.getTok().getLoc(); 1600 1601 int64_t IntVal; 1602 switch (getLexer().getKind()) { 1603 case AsmToken::Percent: { 1604 MCRegister RegNo; 1605 if (MatchRegisterName(RegNo, IntVal)) 1606 return Error(S, "invalid register name"); 1607 break; 1608 } 1609 case AsmToken::Integer: 1610 if (getParser().parseAbsoluteExpression(IntVal) || IntVal < 0 || 1611 IntVal > 31) 1612 return Error(S, "invalid register number"); 1613 break; 1614 case AsmToken::Identifier: 1615 default: 1616 return Error(S, "invalid memory operand"); 1617 } 1618 1619 E = Parser.getTok().getLoc(); 1620 if (parseToken(AsmToken::RParen, "missing ')'")) 1621 return true; 1622 Operands.push_back(PPCOperand::CreateImm(IntVal, S, E, isPPC64())); 1623 } 1624 1625 return false; 1626 } 1627 1628 /// Parse an instruction mnemonic followed by its operands. 1629 bool PPCAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 1630 SMLoc NameLoc, OperandVector &Operands) { 1631 // The first operand is the token for the instruction name. 1632 // If the next character is a '+' or '-', we need to add it to the 1633 // instruction name, to match what TableGen is doing. 1634 std::string NewOpcode; 1635 if (parseOptionalToken(AsmToken::Plus)) { 1636 NewOpcode = std::string(Name); 1637 NewOpcode += '+'; 1638 Name = NewOpcode; 1639 } 1640 if (parseOptionalToken(AsmToken::Minus)) { 1641 NewOpcode = std::string(Name); 1642 NewOpcode += '-'; 1643 Name = NewOpcode; 1644 } 1645 // If the instruction ends in a '.', we need to create a separate 1646 // token for it, to match what TableGen is doing. 1647 size_t Dot = Name.find('.'); 1648 StringRef Mnemonic = Name.slice(0, Dot); 1649 if (!NewOpcode.empty()) // Underlying memory for Name is volatile. 1650 Operands.push_back( 1651 PPCOperand::CreateTokenWithStringCopy(Mnemonic, NameLoc, isPPC64())); 1652 else 1653 Operands.push_back(PPCOperand::CreateToken(Mnemonic, NameLoc, isPPC64())); 1654 if (Dot != StringRef::npos) { 1655 SMLoc DotLoc = SMLoc::getFromPointer(NameLoc.getPointer() + Dot); 1656 StringRef DotStr = Name.slice(Dot, StringRef::npos); 1657 if (!NewOpcode.empty()) // Underlying memory for Name is volatile. 1658 Operands.push_back( 1659 PPCOperand::CreateTokenWithStringCopy(DotStr, DotLoc, isPPC64())); 1660 else 1661 Operands.push_back(PPCOperand::CreateToken(DotStr, DotLoc, isPPC64())); 1662 } 1663 1664 // If there are no more operands then finish 1665 if (parseOptionalToken(AsmToken::EndOfStatement)) 1666 return false; 1667 1668 // Parse the first operand 1669 if (ParseOperand(Operands)) 1670 return true; 1671 1672 while (!parseOptionalToken(AsmToken::EndOfStatement)) { 1673 if (parseToken(AsmToken::Comma) || ParseOperand(Operands)) 1674 return true; 1675 } 1676 1677 // We'll now deal with an unfortunate special case: the syntax for the dcbt 1678 // and dcbtst instructions differs for server vs. embedded cores. 1679 // The syntax for dcbt is: 1680 // dcbt ra, rb, th [server] 1681 // dcbt th, ra, rb [embedded] 1682 // where th can be omitted when it is 0. dcbtst is the same. We take the 1683 // server form to be the default, so swap the operands if we're parsing for 1684 // an embedded core (they'll be swapped again upon printing). 1685 if (getSTI().hasFeature(PPC::FeatureBookE) && 1686 Operands.size() == 4 && 1687 (Name == "dcbt" || Name == "dcbtst")) { 1688 std::swap(Operands[1], Operands[3]); 1689 std::swap(Operands[2], Operands[1]); 1690 } 1691 1692 // Handle base mnemonic for atomic loads where the EH bit is zero. 1693 if (Name == "lqarx" || Name == "ldarx" || Name == "lwarx" || 1694 Name == "lharx" || Name == "lbarx") { 1695 if (Operands.size() != 5) 1696 return false; 1697 PPCOperand &EHOp = (PPCOperand &)*Operands[4]; 1698 if (EHOp.isU1Imm() && EHOp.getImm() == 0) 1699 Operands.pop_back(); 1700 } 1701 1702 return false; 1703 } 1704 1705 /// ParseDirective parses the PPC specific directives 1706 bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) { 1707 StringRef IDVal = DirectiveID.getIdentifier(); 1708 if (IDVal == ".word") 1709 ParseDirectiveWord(2, DirectiveID); 1710 else if (IDVal == ".llong") 1711 ParseDirectiveWord(8, DirectiveID); 1712 else if (IDVal == ".tc") 1713 ParseDirectiveTC(isPPC64() ? 8 : 4, DirectiveID); 1714 else if (IDVal == ".machine") 1715 ParseDirectiveMachine(DirectiveID.getLoc()); 1716 else if (IDVal == ".abiversion") 1717 ParseDirectiveAbiVersion(DirectiveID.getLoc()); 1718 else if (IDVal == ".localentry") 1719 ParseDirectiveLocalEntry(DirectiveID.getLoc()); 1720 else if (IDVal.startswith(".gnu_attribute")) 1721 ParseGNUAttribute(DirectiveID.getLoc()); 1722 else 1723 return true; 1724 return false; 1725 } 1726 1727 /// ParseDirectiveWord 1728 /// ::= .word [ expression (, expression)* ] 1729 bool PPCAsmParser::ParseDirectiveWord(unsigned Size, AsmToken ID) { 1730 auto parseOp = [&]() -> bool { 1731 const MCExpr *Value; 1732 SMLoc ExprLoc = getParser().getTok().getLoc(); 1733 if (getParser().parseExpression(Value)) 1734 return true; 1735 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) { 1736 assert(Size <= 8 && "Invalid size"); 1737 uint64_t IntValue = MCE->getValue(); 1738 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) 1739 return Error(ExprLoc, "literal value out of range for '" + 1740 ID.getIdentifier() + "' directive"); 1741 getStreamer().emitIntValue(IntValue, Size); 1742 } else 1743 getStreamer().emitValue(Value, Size, ExprLoc); 1744 return false; 1745 }; 1746 1747 if (parseMany(parseOp)) 1748 return addErrorSuffix(" in '" + ID.getIdentifier() + "' directive"); 1749 return false; 1750 } 1751 1752 /// ParseDirectiveTC 1753 /// ::= .tc [ symbol (, expression)* ] 1754 bool PPCAsmParser::ParseDirectiveTC(unsigned Size, AsmToken ID) { 1755 MCAsmParser &Parser = getParser(); 1756 // Skip TC symbol, which is only used with XCOFF. 1757 while (getLexer().isNot(AsmToken::EndOfStatement) 1758 && getLexer().isNot(AsmToken::Comma)) 1759 Parser.Lex(); 1760 if (parseToken(AsmToken::Comma)) 1761 return addErrorSuffix(" in '.tc' directive"); 1762 1763 // Align to word size. 1764 getParser().getStreamer().emitValueToAlignment(Align(Size)); 1765 1766 // Emit expressions. 1767 return ParseDirectiveWord(Size, ID); 1768 } 1769 1770 /// ParseDirectiveMachine (ELF platforms) 1771 /// ::= .machine [ cpu | "push" | "pop" ] 1772 bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) { 1773 MCAsmParser &Parser = getParser(); 1774 if (Parser.getTok().isNot(AsmToken::Identifier) && 1775 Parser.getTok().isNot(AsmToken::String)) 1776 return Error(L, "unexpected token in '.machine' directive"); 1777 1778 StringRef CPU = Parser.getTok().getIdentifier(); 1779 1780 // FIXME: Right now, the parser always allows any available 1781 // instruction, so the .machine directive is not useful. 1782 // In the wild, any/push/pop/ppc64/altivec/power[4-9] are seen. 1783 1784 Parser.Lex(); 1785 1786 if (parseToken(AsmToken::EndOfStatement)) 1787 return addErrorSuffix(" in '.machine' directive"); 1788 1789 PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>( 1790 getParser().getStreamer().getTargetStreamer()); 1791 if (TStreamer != nullptr) 1792 TStreamer->emitMachine(CPU); 1793 1794 return false; 1795 } 1796 1797 /// ParseDirectiveAbiVersion 1798 /// ::= .abiversion constant-expression 1799 bool PPCAsmParser::ParseDirectiveAbiVersion(SMLoc L) { 1800 int64_t AbiVersion; 1801 if (check(getParser().parseAbsoluteExpression(AbiVersion), L, 1802 "expected constant expression") || 1803 parseToken(AsmToken::EndOfStatement)) 1804 return addErrorSuffix(" in '.abiversion' directive"); 1805 1806 PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>( 1807 getParser().getStreamer().getTargetStreamer()); 1808 if (TStreamer != nullptr) 1809 TStreamer->emitAbiVersion(AbiVersion); 1810 1811 return false; 1812 } 1813 1814 /// ParseDirectiveLocalEntry 1815 /// ::= .localentry symbol, expression 1816 bool PPCAsmParser::ParseDirectiveLocalEntry(SMLoc L) { 1817 StringRef Name; 1818 if (getParser().parseIdentifier(Name)) 1819 return Error(L, "expected identifier in '.localentry' directive"); 1820 1821 MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name)); 1822 const MCExpr *Expr; 1823 1824 if (parseToken(AsmToken::Comma) || 1825 check(getParser().parseExpression(Expr), L, "expected expression") || 1826 parseToken(AsmToken::EndOfStatement)) 1827 return addErrorSuffix(" in '.localentry' directive"); 1828 1829 PPCTargetStreamer *TStreamer = static_cast<PPCTargetStreamer *>( 1830 getParser().getStreamer().getTargetStreamer()); 1831 if (TStreamer != nullptr) 1832 TStreamer->emitLocalEntry(Sym, Expr); 1833 1834 return false; 1835 } 1836 1837 bool PPCAsmParser::ParseGNUAttribute(SMLoc L) { 1838 int64_t Tag; 1839 int64_t IntegerValue; 1840 if (!getParser().parseGNUAttribute(L, Tag, IntegerValue)) 1841 return false; 1842 1843 getParser().getStreamer().emitGNUAttribute(Tag, IntegerValue); 1844 1845 return true; 1846 } 1847 1848 /// Force static initialization. 1849 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializePowerPCAsmParser() { 1850 RegisterMCAsmParser<PPCAsmParser> A(getThePPC32Target()); 1851 RegisterMCAsmParser<PPCAsmParser> B(getThePPC32LETarget()); 1852 RegisterMCAsmParser<PPCAsmParser> C(getThePPC64Target()); 1853 RegisterMCAsmParser<PPCAsmParser> D(getThePPC64LETarget()); 1854 } 1855 1856 #define GET_REGISTER_MATCHER 1857 #define GET_MATCHER_IMPLEMENTATION 1858 #define GET_MNEMONIC_SPELL_CHECKER 1859 #include "PPCGenAsmMatcher.inc" 1860 1861 // Define this matcher function after the auto-generated include so we 1862 // have the match class enum definitions. 1863 unsigned PPCAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp, 1864 unsigned Kind) { 1865 // If the kind is a token for a literal immediate, check if our asm 1866 // operand matches. This is for InstAliases which have a fixed-value 1867 // immediate in the syntax. 1868 int64_t ImmVal; 1869 switch (Kind) { 1870 case MCK_0: ImmVal = 0; break; 1871 case MCK_1: ImmVal = 1; break; 1872 case MCK_2: ImmVal = 2; break; 1873 case MCK_3: ImmVal = 3; break; 1874 case MCK_4: ImmVal = 4; break; 1875 case MCK_5: ImmVal = 5; break; 1876 case MCK_6: ImmVal = 6; break; 1877 case MCK_7: ImmVal = 7; break; 1878 default: return Match_InvalidOperand; 1879 } 1880 1881 PPCOperand &Op = static_cast<PPCOperand &>(AsmOp); 1882 if (Op.isU3Imm() && Op.getImm() == ImmVal) 1883 return Match_Success; 1884 1885 return Match_InvalidOperand; 1886 } 1887 1888 const MCExpr * 1889 PPCAsmParser::applyModifierToExpr(const MCExpr *E, 1890 MCSymbolRefExpr::VariantKind Variant, 1891 MCContext &Ctx) { 1892 switch (Variant) { 1893 case MCSymbolRefExpr::VK_PPC_LO: 1894 return PPCMCExpr::create(PPCMCExpr::VK_PPC_LO, E, Ctx); 1895 case MCSymbolRefExpr::VK_PPC_HI: 1896 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HI, E, Ctx); 1897 case MCSymbolRefExpr::VK_PPC_HA: 1898 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HA, E, Ctx); 1899 case MCSymbolRefExpr::VK_PPC_HIGH: 1900 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGH, E, Ctx); 1901 case MCSymbolRefExpr::VK_PPC_HIGHA: 1902 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHA, E, Ctx); 1903 case MCSymbolRefExpr::VK_PPC_HIGHER: 1904 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHER, E, Ctx); 1905 case MCSymbolRefExpr::VK_PPC_HIGHERA: 1906 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHERA, E, Ctx); 1907 case MCSymbolRefExpr::VK_PPC_HIGHEST: 1908 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHEST, E, Ctx); 1909 case MCSymbolRefExpr::VK_PPC_HIGHESTA: 1910 return PPCMCExpr::create(PPCMCExpr::VK_PPC_HIGHESTA, E, Ctx); 1911 default: 1912 return nullptr; 1913 } 1914 } 1915