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