1 //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "MCTargetDesc/MipsABIFlagsSection.h" 11 #include "MCTargetDesc/MipsABIInfo.h" 12 #include "MCTargetDesc/MipsMCExpr.h" 13 #include "MCTargetDesc/MipsMCTargetDesc.h" 14 #include "MipsTargetStreamer.h" 15 #include "MCTargetDesc/MipsBaseInfo.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/Triple.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/MC/MCContext.h" 23 #include "llvm/MC/MCExpr.h" 24 #include "llvm/MC/MCInst.h" 25 #include "llvm/MC/MCInstrDesc.h" 26 #include "llvm/MC/MCObjectFileInfo.h" 27 #include "llvm/MC/MCParser/MCAsmLexer.h" 28 #include "llvm/MC/MCParser/MCAsmParser.h" 29 #include "llvm/MC/MCParser/MCAsmParserExtension.h" 30 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 31 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 32 #include "llvm/MC/MCSectionELF.h" 33 #include "llvm/MC/MCStreamer.h" 34 #include "llvm/MC/MCSubtargetInfo.h" 35 #include "llvm/MC/MCSymbol.h" 36 #include "llvm/MC/MCSymbolELF.h" 37 #include "llvm/MC/MCValue.h" 38 #include "llvm/MC/SubtargetFeature.h" 39 #include "llvm/Support/Casting.h" 40 #include "llvm/Support/Compiler.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/ELF.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/MathExtras.h" 45 #include "llvm/Support/raw_ostream.h" 46 #include "llvm/Support/SMLoc.h" 47 #include "llvm/Support/SourceMgr.h" 48 #include "llvm/Support/TargetRegistry.h" 49 #include <algorithm> 50 #include <cassert> 51 #include <cstdint> 52 #include <memory> 53 #include <string> 54 #include <utility> 55 56 using namespace llvm; 57 58 #define DEBUG_TYPE "mips-asm-parser" 59 60 namespace llvm { 61 62 class MCInstrInfo; 63 64 } // end namespace llvm 65 66 namespace { 67 68 class MipsAssemblerOptions { 69 public: 70 MipsAssemblerOptions(const FeatureBitset &Features_) : Features(Features_) {} 71 72 MipsAssemblerOptions(const MipsAssemblerOptions *Opts) { 73 ATReg = Opts->getATRegIndex(); 74 Reorder = Opts->isReorder(); 75 Macro = Opts->isMacro(); 76 Features = Opts->getFeatures(); 77 } 78 79 unsigned getATRegIndex() const { return ATReg; } 80 bool setATRegIndex(unsigned Reg) { 81 if (Reg > 31) 82 return false; 83 84 ATReg = Reg; 85 return true; 86 } 87 88 bool isReorder() const { return Reorder; } 89 void setReorder() { Reorder = true; } 90 void setNoReorder() { Reorder = false; } 91 92 bool isMacro() const { return Macro; } 93 void setMacro() { Macro = true; } 94 void setNoMacro() { Macro = false; } 95 96 const FeatureBitset &getFeatures() const { return Features; } 97 void setFeatures(const FeatureBitset &Features_) { Features = Features_; } 98 99 // Set of features that are either architecture features or referenced 100 // by them (e.g.: FeatureNaN2008 implied by FeatureMips32r6). 101 // The full table can be found in MipsGenSubtargetInfo.inc (MipsFeatureKV[]). 102 // The reason we need this mask is explained in the selectArch function. 103 // FIXME: Ideally we would like TableGen to generate this information. 104 static const FeatureBitset AllArchRelatedMask; 105 106 private: 107 unsigned ATReg = 1; 108 bool Reorder = true; 109 bool Macro = true; 110 FeatureBitset Features; 111 }; 112 113 } // end anonymous namespace 114 115 const FeatureBitset MipsAssemblerOptions::AllArchRelatedMask = { 116 Mips::FeatureMips1, Mips::FeatureMips2, Mips::FeatureMips3, 117 Mips::FeatureMips3_32, Mips::FeatureMips3_32r2, Mips::FeatureMips4, 118 Mips::FeatureMips4_32, Mips::FeatureMips4_32r2, Mips::FeatureMips5, 119 Mips::FeatureMips5_32r2, Mips::FeatureMips32, Mips::FeatureMips32r2, 120 Mips::FeatureMips32r3, Mips::FeatureMips32r5, Mips::FeatureMips32r6, 121 Mips::FeatureMips64, Mips::FeatureMips64r2, Mips::FeatureMips64r3, 122 Mips::FeatureMips64r5, Mips::FeatureMips64r6, Mips::FeatureCnMips, 123 Mips::FeatureFP64Bit, Mips::FeatureGP64Bit, Mips::FeatureNaN2008 124 }; 125 126 namespace { 127 128 class MipsAsmParser : public MCTargetAsmParser { 129 MipsTargetStreamer &getTargetStreamer() { 130 MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer(); 131 return static_cast<MipsTargetStreamer &>(TS); 132 } 133 134 MipsABIInfo ABI; 135 SmallVector<std::unique_ptr<MipsAssemblerOptions>, 2> AssemblerOptions; 136 MCSymbol *CurrentFn; // Pointer to the function being parsed. It may be a 137 // nullptr, which indicates that no function is currently 138 // selected. This usually happens after an '.end func' 139 // directive. 140 bool IsLittleEndian; 141 bool IsPicEnabled; 142 bool IsCpRestoreSet; 143 int CpRestoreOffset; 144 unsigned CpSaveLocation; 145 /// If true, then CpSaveLocation is a register, otherwise it's an offset. 146 bool CpSaveLocationIsRegister; 147 148 // Print a warning along with its fix-it message at the given range. 149 void printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg, 150 SMRange Range, bool ShowColors = true); 151 152 #define GET_ASSEMBLER_HEADER 153 #include "MipsGenAsmMatcher.inc" 154 155 unsigned 156 checkEarlyTargetMatchPredicate(MCInst &Inst, 157 const OperandVector &Operands) override; 158 unsigned checkTargetMatchPredicate(MCInst &Inst) override; 159 160 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 161 OperandVector &Operands, MCStreamer &Out, 162 uint64_t &ErrorInfo, 163 bool MatchingInlineAsm) override; 164 165 /// Parse a register as used in CFI directives 166 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; 167 168 bool parseParenSuffix(StringRef Name, OperandVector &Operands); 169 170 bool parseBracketSuffix(StringRef Name, OperandVector &Operands); 171 172 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 173 SMLoc NameLoc, OperandVector &Operands) override; 174 175 bool ParseDirective(AsmToken DirectiveID) override; 176 177 OperandMatchResultTy parseMemOperand(OperandVector &Operands); 178 OperandMatchResultTy 179 matchAnyRegisterNameWithoutDollar(OperandVector &Operands, 180 StringRef Identifier, SMLoc S); 181 OperandMatchResultTy matchAnyRegisterWithoutDollar(OperandVector &Operands, 182 SMLoc S); 183 OperandMatchResultTy parseAnyRegister(OperandVector &Operands); 184 OperandMatchResultTy parseImm(OperandVector &Operands); 185 OperandMatchResultTy parseJumpTarget(OperandVector &Operands); 186 OperandMatchResultTy parseInvNum(OperandVector &Operands); 187 OperandMatchResultTy parseRegisterPair(OperandVector &Operands); 188 OperandMatchResultTy parseMovePRegPair(OperandVector &Operands); 189 OperandMatchResultTy parseRegisterList(OperandVector &Operands); 190 191 bool searchSymbolAlias(OperandVector &Operands); 192 193 bool parseOperand(OperandVector &, StringRef Mnemonic); 194 195 enum MacroExpanderResultTy { 196 MER_NotAMacro, 197 MER_Success, 198 MER_Fail, 199 }; 200 201 // Expands assembly pseudo instructions. 202 MacroExpanderResultTy tryExpandInstruction(MCInst &Inst, SMLoc IDLoc, 203 MCStreamer &Out, 204 const MCSubtargetInfo *STI); 205 206 bool expandJalWithRegs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 207 const MCSubtargetInfo *STI); 208 209 bool loadImmediate(int64_t ImmValue, unsigned DstReg, unsigned SrcReg, 210 bool Is32BitImm, bool IsAddress, SMLoc IDLoc, 211 MCStreamer &Out, const MCSubtargetInfo *STI); 212 213 bool loadAndAddSymbolAddress(const MCExpr *SymExpr, unsigned DstReg, 214 unsigned SrcReg, bool Is32BitSym, SMLoc IDLoc, 215 MCStreamer &Out, const MCSubtargetInfo *STI); 216 217 bool expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc, 218 MCStreamer &Out, const MCSubtargetInfo *STI); 219 220 bool expandLoadAddress(unsigned DstReg, unsigned BaseReg, 221 const MCOperand &Offset, bool Is32BitAddress, 222 SMLoc IDLoc, MCStreamer &Out, 223 const MCSubtargetInfo *STI); 224 225 bool expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 226 const MCSubtargetInfo *STI); 227 228 void expandMemInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 229 const MCSubtargetInfo *STI, bool IsLoad, bool IsImmOpnd); 230 231 void expandLoadInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 232 const MCSubtargetInfo *STI, bool IsImmOpnd); 233 234 void expandStoreInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 235 const MCSubtargetInfo *STI, bool IsImmOpnd); 236 237 bool expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 238 const MCSubtargetInfo *STI); 239 240 bool expandAliasImmediate(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 241 const MCSubtargetInfo *STI); 242 243 bool expandBranchImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 244 const MCSubtargetInfo *STI); 245 246 bool expandCondBranches(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 247 const MCSubtargetInfo *STI); 248 249 bool expandDiv(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 250 const MCSubtargetInfo *STI, const bool IsMips64, 251 const bool Signed); 252 253 bool expandTrunc(MCInst &Inst, bool IsDouble, bool Is64FPU, SMLoc IDLoc, 254 MCStreamer &Out, const MCSubtargetInfo *STI); 255 256 bool expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc, MCStreamer &Out, 257 const MCSubtargetInfo *STI); 258 259 bool expandUsh(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 260 const MCSubtargetInfo *STI); 261 262 bool expandUxw(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 263 const MCSubtargetInfo *STI); 264 265 bool expandRotation(MCInst &Inst, SMLoc IDLoc, 266 MCStreamer &Out, const MCSubtargetInfo *STI); 267 bool expandRotationImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 268 const MCSubtargetInfo *STI); 269 bool expandDRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 270 const MCSubtargetInfo *STI); 271 bool expandDRotationImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 272 const MCSubtargetInfo *STI); 273 274 bool expandAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 275 const MCSubtargetInfo *STI); 276 277 bool expandLoadStoreDMacro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 278 const MCSubtargetInfo *STI, bool IsLoad); 279 280 bool expandSeq(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 281 const MCSubtargetInfo *STI); 282 283 bool expandSeqI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 284 const MCSubtargetInfo *STI); 285 286 bool reportParseError(Twine ErrorMsg); 287 bool reportParseError(SMLoc Loc, Twine ErrorMsg); 288 289 bool parseMemOffset(const MCExpr *&Res, bool isParenExpr); 290 291 bool isEvaluated(const MCExpr *Expr); 292 bool parseSetMips0Directive(); 293 bool parseSetArchDirective(); 294 bool parseSetFeature(uint64_t Feature); 295 bool isPicAndNotNxxAbi(); // Used by .cpload, .cprestore, and .cpsetup. 296 bool parseDirectiveCpLoad(SMLoc Loc); 297 bool parseDirectiveCpRestore(SMLoc Loc); 298 bool parseDirectiveCPSetup(); 299 bool parseDirectiveCPReturn(); 300 bool parseDirectiveNaN(); 301 bool parseDirectiveSet(); 302 bool parseDirectiveOption(); 303 bool parseInsnDirective(); 304 bool parseSSectionDirective(StringRef Section, unsigned Type); 305 306 bool parseSetAtDirective(); 307 bool parseSetNoAtDirective(); 308 bool parseSetMacroDirective(); 309 bool parseSetNoMacroDirective(); 310 bool parseSetMsaDirective(); 311 bool parseSetNoMsaDirective(); 312 bool parseSetNoDspDirective(); 313 bool parseSetReorderDirective(); 314 bool parseSetNoReorderDirective(); 315 bool parseSetMips16Directive(); 316 bool parseSetNoMips16Directive(); 317 bool parseSetFpDirective(); 318 bool parseSetOddSPRegDirective(); 319 bool parseSetNoOddSPRegDirective(); 320 bool parseSetPopDirective(); 321 bool parseSetPushDirective(); 322 bool parseSetSoftFloatDirective(); 323 bool parseSetHardFloatDirective(); 324 325 bool parseSetAssignment(); 326 327 bool parseDataDirective(unsigned Size, SMLoc L); 328 bool parseDirectiveGpWord(); 329 bool parseDirectiveGpDWord(); 330 bool parseDirectiveDtpRelWord(); 331 bool parseDirectiveDtpRelDWord(); 332 bool parseDirectiveTpRelWord(); 333 bool parseDirectiveTpRelDWord(); 334 bool parseDirectiveModule(); 335 bool parseDirectiveModuleFP(); 336 bool parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI, 337 StringRef Directive); 338 339 bool parseInternalDirectiveReallowModule(); 340 341 bool eatComma(StringRef ErrorStr); 342 343 int matchCPURegisterName(StringRef Symbol); 344 345 int matchHWRegsRegisterName(StringRef Symbol); 346 347 int matchFPURegisterName(StringRef Name); 348 349 int matchFCCRegisterName(StringRef Name); 350 351 int matchACRegisterName(StringRef Name); 352 353 int matchMSA128RegisterName(StringRef Name); 354 355 int matchMSA128CtrlRegisterName(StringRef Name); 356 357 unsigned getReg(int RC, int RegNo); 358 359 /// Returns the internal register number for the current AT. Also checks if 360 /// the current AT is unavailable (set to $0) and gives an error if it is. 361 /// This should be used in pseudo-instruction expansions which need AT. 362 unsigned getATReg(SMLoc Loc); 363 364 bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 365 const MCSubtargetInfo *STI); 366 367 // Helper function that checks if the value of a vector index is within the 368 // boundaries of accepted values for each RegisterKind 369 // Example: INSERT.B $w0[n], $1 => 16 > n >= 0 370 bool validateMSAIndex(int Val, int RegKind); 371 372 // Selects a new architecture by updating the FeatureBits with the necessary 373 // info including implied dependencies. 374 // Internally, it clears all the feature bits related to *any* architecture 375 // and selects the new one using the ToggleFeature functionality of the 376 // MCSubtargetInfo object that handles implied dependencies. The reason we 377 // clear all the arch related bits manually is because ToggleFeature only 378 // clears the features that imply the feature being cleared and not the 379 // features implied by the feature being cleared. This is easier to see 380 // with an example: 381 // -------------------------------------------------- 382 // | Feature | Implies | 383 // | -------------------------------------------------| 384 // | FeatureMips1 | None | 385 // | FeatureMips2 | FeatureMips1 | 386 // | FeatureMips3 | FeatureMips2 | FeatureMipsGP64 | 387 // | FeatureMips4 | FeatureMips3 | 388 // | ... | | 389 // -------------------------------------------------- 390 // 391 // Setting Mips3 is equivalent to set: (FeatureMips3 | FeatureMips2 | 392 // FeatureMipsGP64 | FeatureMips1) 393 // Clearing Mips3 is equivalent to clear (FeatureMips3 | FeatureMips4). 394 void selectArch(StringRef ArchFeature) { 395 MCSubtargetInfo &STI = copySTI(); 396 FeatureBitset FeatureBits = STI.getFeatureBits(); 397 FeatureBits &= ~MipsAssemblerOptions::AllArchRelatedMask; 398 STI.setFeatureBits(FeatureBits); 399 setAvailableFeatures( 400 ComputeAvailableFeatures(STI.ToggleFeature(ArchFeature))); 401 AssemblerOptions.back()->setFeatures(STI.getFeatureBits()); 402 } 403 404 void setFeatureBits(uint64_t Feature, StringRef FeatureString) { 405 if (!(getSTI().getFeatureBits()[Feature])) { 406 MCSubtargetInfo &STI = copySTI(); 407 setAvailableFeatures( 408 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString))); 409 AssemblerOptions.back()->setFeatures(STI.getFeatureBits()); 410 } 411 } 412 413 void clearFeatureBits(uint64_t Feature, StringRef FeatureString) { 414 if (getSTI().getFeatureBits()[Feature]) { 415 MCSubtargetInfo &STI = copySTI(); 416 setAvailableFeatures( 417 ComputeAvailableFeatures(STI.ToggleFeature(FeatureString))); 418 AssemblerOptions.back()->setFeatures(STI.getFeatureBits()); 419 } 420 } 421 422 void setModuleFeatureBits(uint64_t Feature, StringRef FeatureString) { 423 setFeatureBits(Feature, FeatureString); 424 AssemblerOptions.front()->setFeatures(getSTI().getFeatureBits()); 425 } 426 427 void clearModuleFeatureBits(uint64_t Feature, StringRef FeatureString) { 428 clearFeatureBits(Feature, FeatureString); 429 AssemblerOptions.front()->setFeatures(getSTI().getFeatureBits()); 430 } 431 432 public: 433 enum MipsMatchResultTy { 434 Match_RequiresDifferentSrcAndDst = FIRST_TARGET_MATCH_RESULT_TY, 435 Match_RequiresDifferentOperands, 436 Match_RequiresNoZeroRegister, 437 Match_RequiresSameSrcAndDst, 438 Match_NoFCCRegisterForCurrentISA, 439 Match_NonZeroOperandForSync, 440 #define GET_OPERAND_DIAGNOSTIC_TYPES 441 #include "MipsGenAsmMatcher.inc" 442 #undef GET_OPERAND_DIAGNOSTIC_TYPES 443 }; 444 445 MipsAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser, 446 const MCInstrInfo &MII, const MCTargetOptions &Options) 447 : MCTargetAsmParser(Options, sti), 448 ABI(MipsABIInfo::computeTargetABI(Triple(sti.getTargetTriple()), 449 sti.getCPU(), Options)) { 450 MCAsmParserExtension::Initialize(parser); 451 452 parser.addAliasForDirective(".asciiz", ".asciz"); 453 454 // Initialize the set of available features. 455 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); 456 457 // Remember the initial assembler options. The user can not modify these. 458 AssemblerOptions.push_back( 459 llvm::make_unique<MipsAssemblerOptions>(getSTI().getFeatureBits())); 460 461 // Create an assembler options environment for the user to modify. 462 AssemblerOptions.push_back( 463 llvm::make_unique<MipsAssemblerOptions>(getSTI().getFeatureBits())); 464 465 getTargetStreamer().updateABIInfo(*this); 466 467 if (!isABI_O32() && !useOddSPReg() != 0) 468 report_fatal_error("-mno-odd-spreg requires the O32 ABI"); 469 470 CurrentFn = nullptr; 471 472 IsPicEnabled = getContext().getObjectFileInfo()->isPositionIndependent(); 473 474 IsCpRestoreSet = false; 475 CpRestoreOffset = -1; 476 477 const Triple &TheTriple = sti.getTargetTriple(); 478 if ((TheTriple.getArch() == Triple::mips) || 479 (TheTriple.getArch() == Triple::mips64)) 480 IsLittleEndian = false; 481 else 482 IsLittleEndian = true; 483 } 484 485 /// True if all of $fcc0 - $fcc7 exist for the current ISA. 486 bool hasEightFccRegisters() const { return hasMips4() || hasMips32(); } 487 488 bool isGP64bit() const { 489 return getSTI().getFeatureBits()[Mips::FeatureGP64Bit]; 490 } 491 492 bool isFP64bit() const { 493 return getSTI().getFeatureBits()[Mips::FeatureFP64Bit]; 494 } 495 496 const MipsABIInfo &getABI() const { return ABI; } 497 bool isABI_N32() const { return ABI.IsN32(); } 498 bool isABI_N64() const { return ABI.IsN64(); } 499 bool isABI_O32() const { return ABI.IsO32(); } 500 bool isABI_FPXX() const { 501 return getSTI().getFeatureBits()[Mips::FeatureFPXX]; 502 } 503 504 bool useOddSPReg() const { 505 return !(getSTI().getFeatureBits()[Mips::FeatureNoOddSPReg]); 506 } 507 508 bool inMicroMipsMode() const { 509 return getSTI().getFeatureBits()[Mips::FeatureMicroMips]; 510 } 511 512 bool hasMips1() const { 513 return getSTI().getFeatureBits()[Mips::FeatureMips1]; 514 } 515 516 bool hasMips2() const { 517 return getSTI().getFeatureBits()[Mips::FeatureMips2]; 518 } 519 520 bool hasMips3() const { 521 return getSTI().getFeatureBits()[Mips::FeatureMips3]; 522 } 523 524 bool hasMips4() const { 525 return getSTI().getFeatureBits()[Mips::FeatureMips4]; 526 } 527 528 bool hasMips5() const { 529 return getSTI().getFeatureBits()[Mips::FeatureMips5]; 530 } 531 532 bool hasMips32() const { 533 return getSTI().getFeatureBits()[Mips::FeatureMips32]; 534 } 535 536 bool hasMips64() const { 537 return getSTI().getFeatureBits()[Mips::FeatureMips64]; 538 } 539 540 bool hasMips32r2() const { 541 return getSTI().getFeatureBits()[Mips::FeatureMips32r2]; 542 } 543 544 bool hasMips64r2() const { 545 return getSTI().getFeatureBits()[Mips::FeatureMips64r2]; 546 } 547 548 bool hasMips32r3() const { 549 return (getSTI().getFeatureBits()[Mips::FeatureMips32r3]); 550 } 551 552 bool hasMips64r3() const { 553 return (getSTI().getFeatureBits()[Mips::FeatureMips64r3]); 554 } 555 556 bool hasMips32r5() const { 557 return (getSTI().getFeatureBits()[Mips::FeatureMips32r5]); 558 } 559 560 bool hasMips64r5() const { 561 return (getSTI().getFeatureBits()[Mips::FeatureMips64r5]); 562 } 563 564 bool hasMips32r6() const { 565 return getSTI().getFeatureBits()[Mips::FeatureMips32r6]; 566 } 567 568 bool hasMips64r6() const { 569 return getSTI().getFeatureBits()[Mips::FeatureMips64r6]; 570 } 571 572 bool hasDSP() const { 573 return getSTI().getFeatureBits()[Mips::FeatureDSP]; 574 } 575 576 bool hasDSPR2() const { 577 return getSTI().getFeatureBits()[Mips::FeatureDSPR2]; 578 } 579 580 bool hasDSPR3() const { 581 return getSTI().getFeatureBits()[Mips::FeatureDSPR3]; 582 } 583 584 bool hasMSA() const { 585 return getSTI().getFeatureBits()[Mips::FeatureMSA]; 586 } 587 588 bool hasCnMips() const { 589 return (getSTI().getFeatureBits()[Mips::FeatureCnMips]); 590 } 591 592 bool inPicMode() { 593 return IsPicEnabled; 594 } 595 596 bool inMips16Mode() const { 597 return getSTI().getFeatureBits()[Mips::FeatureMips16]; 598 } 599 600 bool useTraps() const { 601 return getSTI().getFeatureBits()[Mips::FeatureUseTCCInDIV]; 602 } 603 604 bool useSoftFloat() const { 605 return getSTI().getFeatureBits()[Mips::FeatureSoftFloat]; 606 } 607 608 /// Warn if RegIndex is the same as the current AT. 609 void warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc); 610 611 void warnIfNoMacro(SMLoc Loc); 612 613 bool isLittle() const { return IsLittleEndian; } 614 615 const MCExpr *createTargetUnaryExpr(const MCExpr *E, 616 AsmToken::TokenKind OperatorToken, 617 MCContext &Ctx) override { 618 switch(OperatorToken) { 619 default: 620 llvm_unreachable("Unknown token"); 621 return nullptr; 622 case AsmToken::PercentCall16: 623 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, E, Ctx); 624 case AsmToken::PercentCall_Hi: 625 return MipsMCExpr::create(MipsMCExpr::MEK_CALL_HI16, E, Ctx); 626 case AsmToken::PercentCall_Lo: 627 return MipsMCExpr::create(MipsMCExpr::MEK_CALL_LO16, E, Ctx); 628 case AsmToken::PercentDtprel_Hi: 629 return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL_HI, E, Ctx); 630 case AsmToken::PercentDtprel_Lo: 631 return MipsMCExpr::create(MipsMCExpr::MEK_DTPREL_LO, E, Ctx); 632 case AsmToken::PercentGot: 633 return MipsMCExpr::create(MipsMCExpr::MEK_GOT, E, Ctx); 634 case AsmToken::PercentGot_Disp: 635 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_DISP, E, Ctx); 636 case AsmToken::PercentGot_Hi: 637 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_HI16, E, Ctx); 638 case AsmToken::PercentGot_Lo: 639 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_LO16, E, Ctx); 640 case AsmToken::PercentGot_Ofst: 641 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_OFST, E, Ctx); 642 case AsmToken::PercentGot_Page: 643 return MipsMCExpr::create(MipsMCExpr::MEK_GOT_PAGE, E, Ctx); 644 case AsmToken::PercentGottprel: 645 return MipsMCExpr::create(MipsMCExpr::MEK_GOTTPREL, E, Ctx); 646 case AsmToken::PercentGp_Rel: 647 return MipsMCExpr::create(MipsMCExpr::MEK_GPREL, E, Ctx); 648 case AsmToken::PercentHi: 649 return MipsMCExpr::create(MipsMCExpr::MEK_HI, E, Ctx); 650 case AsmToken::PercentHigher: 651 return MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, E, Ctx); 652 case AsmToken::PercentHighest: 653 return MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, E, Ctx); 654 case AsmToken::PercentLo: 655 return MipsMCExpr::create(MipsMCExpr::MEK_LO, E, Ctx); 656 case AsmToken::PercentNeg: 657 return MipsMCExpr::create(MipsMCExpr::MEK_NEG, E, Ctx); 658 case AsmToken::PercentPcrel_Hi: 659 return MipsMCExpr::create(MipsMCExpr::MEK_PCREL_HI16, E, Ctx); 660 case AsmToken::PercentPcrel_Lo: 661 return MipsMCExpr::create(MipsMCExpr::MEK_PCREL_LO16, E, Ctx); 662 case AsmToken::PercentTlsgd: 663 return MipsMCExpr::create(MipsMCExpr::MEK_TLSGD, E, Ctx); 664 case AsmToken::PercentTlsldm: 665 return MipsMCExpr::create(MipsMCExpr::MEK_TLSLDM, E, Ctx); 666 case AsmToken::PercentTprel_Hi: 667 return MipsMCExpr::create(MipsMCExpr::MEK_TPREL_HI, E, Ctx); 668 case AsmToken::PercentTprel_Lo: 669 return MipsMCExpr::create(MipsMCExpr::MEK_TPREL_LO, E, Ctx); 670 } 671 } 672 }; 673 674 /// MipsOperand - Instances of this class represent a parsed Mips machine 675 /// instruction. 676 class MipsOperand : public MCParsedAsmOperand { 677 public: 678 /// Broad categories of register classes 679 /// The exact class is finalized by the render method. 680 enum RegKind { 681 RegKind_GPR = 1, /// GPR32 and GPR64 (depending on isGP64bit()) 682 RegKind_FGR = 2, /// FGR32, FGR64, AFGR64 (depending on context and 683 /// isFP64bit()) 684 RegKind_FCC = 4, /// FCC 685 RegKind_MSA128 = 8, /// MSA128[BHWD] (makes no difference which) 686 RegKind_MSACtrl = 16, /// MSA control registers 687 RegKind_COP2 = 32, /// COP2 688 RegKind_ACC = 64, /// HI32DSP, LO32DSP, and ACC64DSP (depending on 689 /// context). 690 RegKind_CCR = 128, /// CCR 691 RegKind_HWRegs = 256, /// HWRegs 692 RegKind_COP3 = 512, /// COP3 693 RegKind_COP0 = 1024, /// COP0 694 /// Potentially any (e.g. $1) 695 RegKind_Numeric = RegKind_GPR | RegKind_FGR | RegKind_FCC | RegKind_MSA128 | 696 RegKind_MSACtrl | RegKind_COP2 | RegKind_ACC | 697 RegKind_CCR | RegKind_HWRegs | RegKind_COP3 | RegKind_COP0 698 }; 699 700 private: 701 enum KindTy { 702 k_Immediate, /// An immediate (possibly involving symbol references) 703 k_Memory, /// Base + Offset Memory Address 704 k_RegisterIndex, /// A register index in one or more RegKind. 705 k_Token, /// A simple token 706 k_RegList, /// A physical register list 707 k_RegPair /// A pair of physical register 708 } Kind; 709 710 public: 711 MipsOperand(KindTy K, MipsAsmParser &Parser) 712 : MCParsedAsmOperand(), Kind(K), AsmParser(Parser) {} 713 714 ~MipsOperand() override { 715 switch (Kind) { 716 case k_Immediate: 717 break; 718 case k_Memory: 719 delete Mem.Base; 720 break; 721 case k_RegList: 722 delete RegList.List; 723 case k_RegisterIndex: 724 case k_Token: 725 case k_RegPair: 726 break; 727 } 728 } 729 730 private: 731 /// For diagnostics, and checking the assembler temporary 732 MipsAsmParser &AsmParser; 733 734 struct Token { 735 const char *Data; 736 unsigned Length; 737 }; 738 739 struct RegIdxOp { 740 unsigned Index; /// Index into the register class 741 RegKind Kind; /// Bitfield of the kinds it could possibly be 742 struct Token Tok; /// The input token this operand originated from. 743 const MCRegisterInfo *RegInfo; 744 }; 745 746 struct ImmOp { 747 const MCExpr *Val; 748 }; 749 750 struct MemOp { 751 MipsOperand *Base; 752 const MCExpr *Off; 753 }; 754 755 struct RegListOp { 756 SmallVector<unsigned, 10> *List; 757 }; 758 759 union { 760 struct Token Tok; 761 struct RegIdxOp RegIdx; 762 struct ImmOp Imm; 763 struct MemOp Mem; 764 struct RegListOp RegList; 765 }; 766 767 SMLoc StartLoc, EndLoc; 768 769 /// Internal constructor for register kinds 770 static std::unique_ptr<MipsOperand> CreateReg(unsigned Index, StringRef Str, 771 RegKind RegKind, 772 const MCRegisterInfo *RegInfo, 773 SMLoc S, SMLoc E, 774 MipsAsmParser &Parser) { 775 auto Op = llvm::make_unique<MipsOperand>(k_RegisterIndex, Parser); 776 Op->RegIdx.Index = Index; 777 Op->RegIdx.RegInfo = RegInfo; 778 Op->RegIdx.Kind = RegKind; 779 Op->RegIdx.Tok.Data = Str.data(); 780 Op->RegIdx.Tok.Length = Str.size(); 781 Op->StartLoc = S; 782 Op->EndLoc = E; 783 return Op; 784 } 785 786 public: 787 /// Coerce the register to GPR32 and return the real register for the current 788 /// target. 789 unsigned getGPR32Reg() const { 790 assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!"); 791 AsmParser.warnIfRegIndexIsAT(RegIdx.Index, StartLoc); 792 unsigned ClassID = Mips::GPR32RegClassID; 793 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 794 } 795 796 /// Coerce the register to GPR32 and return the real register for the current 797 /// target. 798 unsigned getGPRMM16Reg() const { 799 assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!"); 800 unsigned ClassID = Mips::GPR32RegClassID; 801 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 802 } 803 804 /// Coerce the register to GPR64 and return the real register for the current 805 /// target. 806 unsigned getGPR64Reg() const { 807 assert(isRegIdx() && (RegIdx.Kind & RegKind_GPR) && "Invalid access!"); 808 unsigned ClassID = Mips::GPR64RegClassID; 809 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 810 } 811 812 private: 813 /// Coerce the register to AFGR64 and return the real register for the current 814 /// target. 815 unsigned getAFGR64Reg() const { 816 assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!"); 817 if (RegIdx.Index % 2 != 0) 818 AsmParser.Warning(StartLoc, "Float register should be even."); 819 return RegIdx.RegInfo->getRegClass(Mips::AFGR64RegClassID) 820 .getRegister(RegIdx.Index / 2); 821 } 822 823 /// Coerce the register to FGR64 and return the real register for the current 824 /// target. 825 unsigned getFGR64Reg() const { 826 assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!"); 827 return RegIdx.RegInfo->getRegClass(Mips::FGR64RegClassID) 828 .getRegister(RegIdx.Index); 829 } 830 831 /// Coerce the register to FGR32 and return the real register for the current 832 /// target. 833 unsigned getFGR32Reg() const { 834 assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!"); 835 return RegIdx.RegInfo->getRegClass(Mips::FGR32RegClassID) 836 .getRegister(RegIdx.Index); 837 } 838 839 /// Coerce the register to FGRH32 and return the real register for the current 840 /// target. 841 unsigned getFGRH32Reg() const { 842 assert(isRegIdx() && (RegIdx.Kind & RegKind_FGR) && "Invalid access!"); 843 return RegIdx.RegInfo->getRegClass(Mips::FGRH32RegClassID) 844 .getRegister(RegIdx.Index); 845 } 846 847 /// Coerce the register to FCC and return the real register for the current 848 /// target. 849 unsigned getFCCReg() const { 850 assert(isRegIdx() && (RegIdx.Kind & RegKind_FCC) && "Invalid access!"); 851 return RegIdx.RegInfo->getRegClass(Mips::FCCRegClassID) 852 .getRegister(RegIdx.Index); 853 } 854 855 /// Coerce the register to MSA128 and return the real register for the current 856 /// target. 857 unsigned getMSA128Reg() const { 858 assert(isRegIdx() && (RegIdx.Kind & RegKind_MSA128) && "Invalid access!"); 859 // It doesn't matter which of the MSA128[BHWD] classes we use. They are all 860 // identical 861 unsigned ClassID = Mips::MSA128BRegClassID; 862 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 863 } 864 865 /// Coerce the register to MSACtrl and return the real register for the 866 /// current target. 867 unsigned getMSACtrlReg() const { 868 assert(isRegIdx() && (RegIdx.Kind & RegKind_MSACtrl) && "Invalid access!"); 869 unsigned ClassID = Mips::MSACtrlRegClassID; 870 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 871 } 872 873 /// Coerce the register to COP0 and return the real register for the 874 /// current target. 875 unsigned getCOP0Reg() const { 876 assert(isRegIdx() && (RegIdx.Kind & RegKind_COP0) && "Invalid access!"); 877 unsigned ClassID = Mips::COP0RegClassID; 878 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 879 } 880 881 /// Coerce the register to COP2 and return the real register for the 882 /// current target. 883 unsigned getCOP2Reg() const { 884 assert(isRegIdx() && (RegIdx.Kind & RegKind_COP2) && "Invalid access!"); 885 unsigned ClassID = Mips::COP2RegClassID; 886 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 887 } 888 889 /// Coerce the register to COP3 and return the real register for the 890 /// current target. 891 unsigned getCOP3Reg() const { 892 assert(isRegIdx() && (RegIdx.Kind & RegKind_COP3) && "Invalid access!"); 893 unsigned ClassID = Mips::COP3RegClassID; 894 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 895 } 896 897 /// Coerce the register to ACC64DSP and return the real register for the 898 /// current target. 899 unsigned getACC64DSPReg() const { 900 assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!"); 901 unsigned ClassID = Mips::ACC64DSPRegClassID; 902 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 903 } 904 905 /// Coerce the register to HI32DSP and return the real register for the 906 /// current target. 907 unsigned getHI32DSPReg() const { 908 assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!"); 909 unsigned ClassID = Mips::HI32DSPRegClassID; 910 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 911 } 912 913 /// Coerce the register to LO32DSP and return the real register for the 914 /// current target. 915 unsigned getLO32DSPReg() const { 916 assert(isRegIdx() && (RegIdx.Kind & RegKind_ACC) && "Invalid access!"); 917 unsigned ClassID = Mips::LO32DSPRegClassID; 918 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 919 } 920 921 /// Coerce the register to CCR and return the real register for the 922 /// current target. 923 unsigned getCCRReg() const { 924 assert(isRegIdx() && (RegIdx.Kind & RegKind_CCR) && "Invalid access!"); 925 unsigned ClassID = Mips::CCRRegClassID; 926 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 927 } 928 929 /// Coerce the register to HWRegs and return the real register for the 930 /// current target. 931 unsigned getHWRegsReg() const { 932 assert(isRegIdx() && (RegIdx.Kind & RegKind_HWRegs) && "Invalid access!"); 933 unsigned ClassID = Mips::HWRegsRegClassID; 934 return RegIdx.RegInfo->getRegClass(ClassID).getRegister(RegIdx.Index); 935 } 936 937 public: 938 void addExpr(MCInst &Inst, const MCExpr *Expr) const { 939 // Add as immediate when possible. Null MCExpr = 0. 940 if (!Expr) 941 Inst.addOperand(MCOperand::createImm(0)); 942 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) 943 Inst.addOperand(MCOperand::createImm(CE->getValue())); 944 else 945 Inst.addOperand(MCOperand::createExpr(Expr)); 946 } 947 948 void addRegOperands(MCInst &Inst, unsigned N) const { 949 llvm_unreachable("Use a custom parser instead"); 950 } 951 952 /// Render the operand to an MCInst as a GPR32 953 /// Asserts if the wrong number of operands are requested, or the operand 954 /// is not a k_RegisterIndex compatible with RegKind_GPR 955 void addGPR32AsmRegOperands(MCInst &Inst, unsigned N) const { 956 assert(N == 1 && "Invalid number of operands!"); 957 Inst.addOperand(MCOperand::createReg(getGPR32Reg())); 958 } 959 960 void addGPRMM16AsmRegOperands(MCInst &Inst, unsigned N) const { 961 assert(N == 1 && "Invalid number of operands!"); 962 Inst.addOperand(MCOperand::createReg(getGPRMM16Reg())); 963 } 964 965 void addGPRMM16AsmRegZeroOperands(MCInst &Inst, unsigned N) const { 966 assert(N == 1 && "Invalid number of operands!"); 967 Inst.addOperand(MCOperand::createReg(getGPRMM16Reg())); 968 } 969 970 void addGPRMM16AsmRegMovePOperands(MCInst &Inst, unsigned N) const { 971 assert(N == 1 && "Invalid number of operands!"); 972 Inst.addOperand(MCOperand::createReg(getGPRMM16Reg())); 973 } 974 975 /// Render the operand to an MCInst as a GPR64 976 /// Asserts if the wrong number of operands are requested, or the operand 977 /// is not a k_RegisterIndex compatible with RegKind_GPR 978 void addGPR64AsmRegOperands(MCInst &Inst, unsigned N) const { 979 assert(N == 1 && "Invalid number of operands!"); 980 Inst.addOperand(MCOperand::createReg(getGPR64Reg())); 981 } 982 983 void addAFGR64AsmRegOperands(MCInst &Inst, unsigned N) const { 984 assert(N == 1 && "Invalid number of operands!"); 985 Inst.addOperand(MCOperand::createReg(getAFGR64Reg())); 986 } 987 988 void addFGR64AsmRegOperands(MCInst &Inst, unsigned N) const { 989 assert(N == 1 && "Invalid number of operands!"); 990 Inst.addOperand(MCOperand::createReg(getFGR64Reg())); 991 } 992 993 void addFGR32AsmRegOperands(MCInst &Inst, unsigned N) const { 994 assert(N == 1 && "Invalid number of operands!"); 995 Inst.addOperand(MCOperand::createReg(getFGR32Reg())); 996 // FIXME: We ought to do this for -integrated-as without -via-file-asm too. 997 // FIXME: This should propagate failure up to parseStatement. 998 if (!AsmParser.useOddSPReg() && RegIdx.Index & 1) 999 AsmParser.getParser().printError( 1000 StartLoc, "-mno-odd-spreg prohibits the use of odd FPU " 1001 "registers"); 1002 } 1003 1004 void addFGRH32AsmRegOperands(MCInst &Inst, unsigned N) const { 1005 assert(N == 1 && "Invalid number of operands!"); 1006 Inst.addOperand(MCOperand::createReg(getFGRH32Reg())); 1007 } 1008 1009 void addFCCAsmRegOperands(MCInst &Inst, unsigned N) const { 1010 assert(N == 1 && "Invalid number of operands!"); 1011 Inst.addOperand(MCOperand::createReg(getFCCReg())); 1012 } 1013 1014 void addMSA128AsmRegOperands(MCInst &Inst, unsigned N) const { 1015 assert(N == 1 && "Invalid number of operands!"); 1016 Inst.addOperand(MCOperand::createReg(getMSA128Reg())); 1017 } 1018 1019 void addMSACtrlAsmRegOperands(MCInst &Inst, unsigned N) const { 1020 assert(N == 1 && "Invalid number of operands!"); 1021 Inst.addOperand(MCOperand::createReg(getMSACtrlReg())); 1022 } 1023 1024 void addCOP0AsmRegOperands(MCInst &Inst, unsigned N) const { 1025 assert(N == 1 && "Invalid number of operands!"); 1026 Inst.addOperand(MCOperand::createReg(getCOP0Reg())); 1027 } 1028 1029 void addCOP2AsmRegOperands(MCInst &Inst, unsigned N) const { 1030 assert(N == 1 && "Invalid number of operands!"); 1031 Inst.addOperand(MCOperand::createReg(getCOP2Reg())); 1032 } 1033 1034 void addCOP3AsmRegOperands(MCInst &Inst, unsigned N) const { 1035 assert(N == 1 && "Invalid number of operands!"); 1036 Inst.addOperand(MCOperand::createReg(getCOP3Reg())); 1037 } 1038 1039 void addACC64DSPAsmRegOperands(MCInst &Inst, unsigned N) const { 1040 assert(N == 1 && "Invalid number of operands!"); 1041 Inst.addOperand(MCOperand::createReg(getACC64DSPReg())); 1042 } 1043 1044 void addHI32DSPAsmRegOperands(MCInst &Inst, unsigned N) const { 1045 assert(N == 1 && "Invalid number of operands!"); 1046 Inst.addOperand(MCOperand::createReg(getHI32DSPReg())); 1047 } 1048 1049 void addLO32DSPAsmRegOperands(MCInst &Inst, unsigned N) const { 1050 assert(N == 1 && "Invalid number of operands!"); 1051 Inst.addOperand(MCOperand::createReg(getLO32DSPReg())); 1052 } 1053 1054 void addCCRAsmRegOperands(MCInst &Inst, unsigned N) const { 1055 assert(N == 1 && "Invalid number of operands!"); 1056 Inst.addOperand(MCOperand::createReg(getCCRReg())); 1057 } 1058 1059 void addHWRegsAsmRegOperands(MCInst &Inst, unsigned N) const { 1060 assert(N == 1 && "Invalid number of operands!"); 1061 Inst.addOperand(MCOperand::createReg(getHWRegsReg())); 1062 } 1063 1064 template <unsigned Bits, int Offset = 0, int AdjustOffset = 0> 1065 void addConstantUImmOperands(MCInst &Inst, unsigned N) const { 1066 assert(N == 1 && "Invalid number of operands!"); 1067 uint64_t Imm = getConstantImm() - Offset; 1068 Imm &= (1ULL << Bits) - 1; 1069 Imm += Offset; 1070 Imm += AdjustOffset; 1071 Inst.addOperand(MCOperand::createImm(Imm)); 1072 } 1073 1074 template <unsigned Bits> 1075 void addSImmOperands(MCInst &Inst, unsigned N) const { 1076 if (isImm() && !isConstantImm()) { 1077 addExpr(Inst, getImm()); 1078 return; 1079 } 1080 addConstantSImmOperands<Bits, 0, 0>(Inst, N); 1081 } 1082 1083 template <unsigned Bits> 1084 void addUImmOperands(MCInst &Inst, unsigned N) const { 1085 if (isImm() && !isConstantImm()) { 1086 addExpr(Inst, getImm()); 1087 return; 1088 } 1089 addConstantUImmOperands<Bits, 0, 0>(Inst, N); 1090 } 1091 1092 template <unsigned Bits, int Offset = 0, int AdjustOffset = 0> 1093 void addConstantSImmOperands(MCInst &Inst, unsigned N) const { 1094 assert(N == 1 && "Invalid number of operands!"); 1095 int64_t Imm = getConstantImm() - Offset; 1096 Imm = SignExtend64<Bits>(Imm); 1097 Imm += Offset; 1098 Imm += AdjustOffset; 1099 Inst.addOperand(MCOperand::createImm(Imm)); 1100 } 1101 1102 void addImmOperands(MCInst &Inst, unsigned N) const { 1103 assert(N == 1 && "Invalid number of operands!"); 1104 const MCExpr *Expr = getImm(); 1105 addExpr(Inst, Expr); 1106 } 1107 1108 void addMemOperands(MCInst &Inst, unsigned N) const { 1109 assert(N == 2 && "Invalid number of operands!"); 1110 1111 Inst.addOperand(MCOperand::createReg(AsmParser.getABI().ArePtrs64bit() 1112 ? getMemBase()->getGPR64Reg() 1113 : getMemBase()->getGPR32Reg())); 1114 1115 const MCExpr *Expr = getMemOff(); 1116 addExpr(Inst, Expr); 1117 } 1118 1119 void addMicroMipsMemOperands(MCInst &Inst, unsigned N) const { 1120 assert(N == 2 && "Invalid number of operands!"); 1121 1122 Inst.addOperand(MCOperand::createReg(getMemBase()->getGPRMM16Reg())); 1123 1124 const MCExpr *Expr = getMemOff(); 1125 addExpr(Inst, Expr); 1126 } 1127 1128 void addRegListOperands(MCInst &Inst, unsigned N) const { 1129 assert(N == 1 && "Invalid number of operands!"); 1130 1131 for (auto RegNo : getRegList()) 1132 Inst.addOperand(MCOperand::createReg(RegNo)); 1133 } 1134 1135 void addRegPairOperands(MCInst &Inst, unsigned N) const { 1136 assert(N == 2 && "Invalid number of operands!"); 1137 assert((RegIdx.Kind & RegKind_GPR) && "Invalid access!"); 1138 unsigned RegNo = getRegPair(); 1139 AsmParser.warnIfRegIndexIsAT(RegNo, StartLoc); 1140 Inst.addOperand(MCOperand::createReg( 1141 RegIdx.RegInfo->getRegClass( 1142 AsmParser.getABI().AreGprs64bit() 1143 ? Mips::GPR64RegClassID 1144 : Mips::GPR32RegClassID).getRegister(RegNo++))); 1145 Inst.addOperand(MCOperand::createReg( 1146 RegIdx.RegInfo->getRegClass( 1147 AsmParser.getABI().AreGprs64bit() 1148 ? Mips::GPR64RegClassID 1149 : Mips::GPR32RegClassID).getRegister(RegNo))); 1150 } 1151 1152 void addMovePRegPairOperands(MCInst &Inst, unsigned N) const { 1153 assert(N == 2 && "Invalid number of operands!"); 1154 for (auto RegNo : getRegList()) 1155 Inst.addOperand(MCOperand::createReg(RegNo)); 1156 } 1157 1158 bool isReg() const override { 1159 // As a special case until we sort out the definition of div/divu, accept 1160 // $0/$zero here so that MCK_ZERO works correctly. 1161 return isGPRAsmReg() && RegIdx.Index == 0; 1162 } 1163 1164 bool isRegIdx() const { return Kind == k_RegisterIndex; } 1165 bool isImm() const override { return Kind == k_Immediate; } 1166 1167 bool isConstantImm() const { 1168 int64_t Res; 1169 return isImm() && getImm()->evaluateAsAbsolute(Res); 1170 } 1171 1172 bool isConstantImmz() const { 1173 return isConstantImm() && getConstantImm() == 0; 1174 } 1175 1176 template <unsigned Bits, int Offset = 0> bool isConstantUImm() const { 1177 return isConstantImm() && isUInt<Bits>(getConstantImm() - Offset); 1178 } 1179 1180 template <unsigned Bits> bool isSImm() const { 1181 return isConstantImm() ? isInt<Bits>(getConstantImm()) : isImm(); 1182 } 1183 1184 template <unsigned Bits> bool isUImm() const { 1185 return isConstantImm() ? isUInt<Bits>(getConstantImm()) : isImm(); 1186 } 1187 1188 template <unsigned Bits> bool isAnyImm() const { 1189 return isConstantImm() ? (isInt<Bits>(getConstantImm()) || 1190 isUInt<Bits>(getConstantImm())) 1191 : isImm(); 1192 } 1193 1194 template <unsigned Bits, int Offset = 0> bool isConstantSImm() const { 1195 return isConstantImm() && isInt<Bits>(getConstantImm() - Offset); 1196 } 1197 1198 template <unsigned Bottom, unsigned Top> bool isConstantUImmRange() const { 1199 return isConstantImm() && getConstantImm() >= Bottom && 1200 getConstantImm() <= Top; 1201 } 1202 1203 bool isToken() const override { 1204 // Note: It's not possible to pretend that other operand kinds are tokens. 1205 // The matcher emitter checks tokens first. 1206 return Kind == k_Token; 1207 } 1208 1209 bool isMem() const override { return Kind == k_Memory; } 1210 1211 bool isConstantMemOff() const { 1212 return isMem() && isa<MCConstantExpr>(getMemOff()); 1213 } 1214 1215 // Allow relocation operators. 1216 // FIXME: This predicate and others need to look through binary expressions 1217 // and determine whether a Value is a constant or not. 1218 template <unsigned Bits, unsigned ShiftAmount = 0> 1219 bool isMemWithSimmOffset() const { 1220 if (!isMem()) 1221 return false; 1222 if (!getMemBase()->isGPRAsmReg()) 1223 return false; 1224 if (isa<MCTargetExpr>(getMemOff()) || 1225 (isConstantMemOff() && 1226 isShiftedInt<Bits, ShiftAmount>(getConstantMemOff()))) 1227 return true; 1228 MCValue Res; 1229 bool IsReloc = getMemOff()->evaluateAsRelocatable(Res, nullptr, nullptr); 1230 return IsReloc && isShiftedInt<Bits, ShiftAmount>(Res.getConstant()); 1231 } 1232 1233 bool isMemWithGRPMM16Base() const { 1234 return isMem() && getMemBase()->isMM16AsmReg(); 1235 } 1236 1237 template <unsigned Bits> bool isMemWithUimmOffsetSP() const { 1238 return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff()) 1239 && getMemBase()->isRegIdx() && (getMemBase()->getGPR32Reg() == Mips::SP); 1240 } 1241 1242 template <unsigned Bits> bool isMemWithUimmWordAlignedOffsetSP() const { 1243 return isMem() && isConstantMemOff() && isUInt<Bits>(getConstantMemOff()) 1244 && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx() 1245 && (getMemBase()->getGPR32Reg() == Mips::SP); 1246 } 1247 1248 template <unsigned Bits> bool isMemWithSimmWordAlignedOffsetGP() const { 1249 return isMem() && isConstantMemOff() && isInt<Bits>(getConstantMemOff()) 1250 && (getConstantMemOff() % 4 == 0) && getMemBase()->isRegIdx() 1251 && (getMemBase()->getGPR32Reg() == Mips::GP); 1252 } 1253 1254 template <unsigned Bits, unsigned ShiftLeftAmount> 1255 bool isScaledUImm() const { 1256 return isConstantImm() && 1257 isShiftedUInt<Bits, ShiftLeftAmount>(getConstantImm()); 1258 } 1259 1260 template <unsigned Bits, unsigned ShiftLeftAmount> 1261 bool isScaledSImm() const { 1262 if (isConstantImm() && isShiftedInt<Bits, ShiftLeftAmount>(getConstantImm())) 1263 return true; 1264 // Operand can also be a symbol or symbol plus offset in case of relocations. 1265 if (Kind != k_Immediate) 1266 return false; 1267 MCValue Res; 1268 bool Success = getImm()->evaluateAsRelocatable(Res, nullptr, nullptr); 1269 return Success && isShiftedInt<Bits, ShiftLeftAmount>(Res.getConstant()); 1270 } 1271 1272 bool isRegList16() const { 1273 if (!isRegList()) 1274 return false; 1275 1276 int Size = RegList.List->size(); 1277 if (Size < 2 || Size > 5) 1278 return false; 1279 1280 unsigned R0 = RegList.List->front(); 1281 unsigned R1 = RegList.List->back(); 1282 if (!((R0 == Mips::S0 && R1 == Mips::RA) || 1283 (R0 == Mips::S0_64 && R1 == Mips::RA_64))) 1284 return false; 1285 1286 int PrevReg = *RegList.List->begin(); 1287 for (int i = 1; i < Size - 1; i++) { 1288 int Reg = (*(RegList.List))[i]; 1289 if ( Reg != PrevReg + 1) 1290 return false; 1291 PrevReg = Reg; 1292 } 1293 1294 return true; 1295 } 1296 1297 bool isInvNum() const { return Kind == k_Immediate; } 1298 1299 bool isLSAImm() const { 1300 if (!isConstantImm()) 1301 return false; 1302 int64_t Val = getConstantImm(); 1303 return 1 <= Val && Val <= 4; 1304 } 1305 1306 bool isRegList() const { return Kind == k_RegList; } 1307 1308 bool isMovePRegPair() const { 1309 if (Kind != k_RegList || RegList.List->size() != 2) 1310 return false; 1311 1312 unsigned R0 = RegList.List->front(); 1313 unsigned R1 = RegList.List->back(); 1314 1315 if ((R0 == Mips::A1 && R1 == Mips::A2) || 1316 (R0 == Mips::A1 && R1 == Mips::A3) || 1317 (R0 == Mips::A2 && R1 == Mips::A3) || 1318 (R0 == Mips::A0 && R1 == Mips::S5) || 1319 (R0 == Mips::A0 && R1 == Mips::S6) || 1320 (R0 == Mips::A0 && R1 == Mips::A1) || 1321 (R0 == Mips::A0 && R1 == Mips::A2) || 1322 (R0 == Mips::A0 && R1 == Mips::A3) || 1323 (R0 == Mips::A1_64 && R1 == Mips::A2_64) || 1324 (R0 == Mips::A1_64 && R1 == Mips::A3_64) || 1325 (R0 == Mips::A2_64 && R1 == Mips::A3_64) || 1326 (R0 == Mips::A0_64 && R1 == Mips::S5_64) || 1327 (R0 == Mips::A0_64 && R1 == Mips::S6_64) || 1328 (R0 == Mips::A0_64 && R1 == Mips::A1_64) || 1329 (R0 == Mips::A0_64 && R1 == Mips::A2_64) || 1330 (R0 == Mips::A0_64 && R1 == Mips::A3_64)) 1331 return true; 1332 1333 return false; 1334 } 1335 1336 StringRef getToken() const { 1337 assert(Kind == k_Token && "Invalid access!"); 1338 return StringRef(Tok.Data, Tok.Length); 1339 } 1340 1341 bool isRegPair() const { 1342 return Kind == k_RegPair && RegIdx.Index <= 30; 1343 } 1344 1345 unsigned getReg() const override { 1346 // As a special case until we sort out the definition of div/divu, accept 1347 // $0/$zero here so that MCK_ZERO works correctly. 1348 if (Kind == k_RegisterIndex && RegIdx.Index == 0 && 1349 RegIdx.Kind & RegKind_GPR) 1350 return getGPR32Reg(); // FIXME: GPR64 too 1351 1352 llvm_unreachable("Invalid access!"); 1353 return 0; 1354 } 1355 1356 const MCExpr *getImm() const { 1357 assert((Kind == k_Immediate) && "Invalid access!"); 1358 return Imm.Val; 1359 } 1360 1361 int64_t getConstantImm() const { 1362 const MCExpr *Val = getImm(); 1363 int64_t Value = 0; 1364 (void)Val->evaluateAsAbsolute(Value); 1365 return Value; 1366 } 1367 1368 MipsOperand *getMemBase() const { 1369 assert((Kind == k_Memory) && "Invalid access!"); 1370 return Mem.Base; 1371 } 1372 1373 const MCExpr *getMemOff() const { 1374 assert((Kind == k_Memory) && "Invalid access!"); 1375 return Mem.Off; 1376 } 1377 1378 int64_t getConstantMemOff() const { 1379 return static_cast<const MCConstantExpr *>(getMemOff())->getValue(); 1380 } 1381 1382 const SmallVectorImpl<unsigned> &getRegList() const { 1383 assert((Kind == k_RegList) && "Invalid access!"); 1384 return *(RegList.List); 1385 } 1386 1387 unsigned getRegPair() const { 1388 assert((Kind == k_RegPair) && "Invalid access!"); 1389 return RegIdx.Index; 1390 } 1391 1392 static std::unique_ptr<MipsOperand> CreateToken(StringRef Str, SMLoc S, 1393 MipsAsmParser &Parser) { 1394 auto Op = llvm::make_unique<MipsOperand>(k_Token, Parser); 1395 Op->Tok.Data = Str.data(); 1396 Op->Tok.Length = Str.size(); 1397 Op->StartLoc = S; 1398 Op->EndLoc = S; 1399 return Op; 1400 } 1401 1402 /// Create a numeric register (e.g. $1). The exact register remains 1403 /// unresolved until an instruction successfully matches 1404 static std::unique_ptr<MipsOperand> 1405 createNumericReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1406 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1407 DEBUG(dbgs() << "createNumericReg(" << Index << ", ...)\n"); 1408 return CreateReg(Index, Str, RegKind_Numeric, RegInfo, S, E, Parser); 1409 } 1410 1411 /// Create a register that is definitely a GPR. 1412 /// This is typically only used for named registers such as $gp. 1413 static std::unique_ptr<MipsOperand> 1414 createGPRReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1415 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1416 return CreateReg(Index, Str, RegKind_GPR, RegInfo, S, E, Parser); 1417 } 1418 1419 /// Create a register that is definitely a FGR. 1420 /// This is typically only used for named registers such as $f0. 1421 static std::unique_ptr<MipsOperand> 1422 createFGRReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1423 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1424 return CreateReg(Index, Str, RegKind_FGR, RegInfo, S, E, Parser); 1425 } 1426 1427 /// Create a register that is definitely a HWReg. 1428 /// This is typically only used for named registers such as $hwr_cpunum. 1429 static std::unique_ptr<MipsOperand> 1430 createHWRegsReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1431 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1432 return CreateReg(Index, Str, RegKind_HWRegs, RegInfo, S, E, Parser); 1433 } 1434 1435 /// Create a register that is definitely an FCC. 1436 /// This is typically only used for named registers such as $fcc0. 1437 static std::unique_ptr<MipsOperand> 1438 createFCCReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1439 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1440 return CreateReg(Index, Str, RegKind_FCC, RegInfo, S, E, Parser); 1441 } 1442 1443 /// Create a register that is definitely an ACC. 1444 /// This is typically only used for named registers such as $ac0. 1445 static std::unique_ptr<MipsOperand> 1446 createACCReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1447 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1448 return CreateReg(Index, Str, RegKind_ACC, RegInfo, S, E, Parser); 1449 } 1450 1451 /// Create a register that is definitely an MSA128. 1452 /// This is typically only used for named registers such as $w0. 1453 static std::unique_ptr<MipsOperand> 1454 createMSA128Reg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1455 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1456 return CreateReg(Index, Str, RegKind_MSA128, RegInfo, S, E, Parser); 1457 } 1458 1459 /// Create a register that is definitely an MSACtrl. 1460 /// This is typically only used for named registers such as $msaaccess. 1461 static std::unique_ptr<MipsOperand> 1462 createMSACtrlReg(unsigned Index, StringRef Str, const MCRegisterInfo *RegInfo, 1463 SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1464 return CreateReg(Index, Str, RegKind_MSACtrl, RegInfo, S, E, Parser); 1465 } 1466 1467 static std::unique_ptr<MipsOperand> 1468 CreateImm(const MCExpr *Val, SMLoc S, SMLoc E, MipsAsmParser &Parser) { 1469 auto Op = llvm::make_unique<MipsOperand>(k_Immediate, Parser); 1470 Op->Imm.Val = Val; 1471 Op->StartLoc = S; 1472 Op->EndLoc = E; 1473 return Op; 1474 } 1475 1476 static std::unique_ptr<MipsOperand> 1477 CreateMem(std::unique_ptr<MipsOperand> Base, const MCExpr *Off, SMLoc S, 1478 SMLoc E, MipsAsmParser &Parser) { 1479 auto Op = llvm::make_unique<MipsOperand>(k_Memory, Parser); 1480 Op->Mem.Base = Base.release(); 1481 Op->Mem.Off = Off; 1482 Op->StartLoc = S; 1483 Op->EndLoc = E; 1484 return Op; 1485 } 1486 1487 static std::unique_ptr<MipsOperand> 1488 CreateRegList(SmallVectorImpl<unsigned> &Regs, SMLoc StartLoc, SMLoc EndLoc, 1489 MipsAsmParser &Parser) { 1490 assert(Regs.size() > 0 && "Empty list not allowed"); 1491 1492 auto Op = llvm::make_unique<MipsOperand>(k_RegList, Parser); 1493 Op->RegList.List = new SmallVector<unsigned, 10>(Regs.begin(), Regs.end()); 1494 Op->StartLoc = StartLoc; 1495 Op->EndLoc = EndLoc; 1496 return Op; 1497 } 1498 1499 static std::unique_ptr<MipsOperand> CreateRegPair(const MipsOperand &MOP, 1500 SMLoc S, SMLoc E, 1501 MipsAsmParser &Parser) { 1502 auto Op = llvm::make_unique<MipsOperand>(k_RegPair, Parser); 1503 Op->RegIdx.Index = MOP.RegIdx.Index; 1504 Op->RegIdx.RegInfo = MOP.RegIdx.RegInfo; 1505 Op->RegIdx.Kind = MOP.RegIdx.Kind; 1506 Op->StartLoc = S; 1507 Op->EndLoc = E; 1508 return Op; 1509 } 1510 1511 bool isGPRAsmReg() const { 1512 return isRegIdx() && RegIdx.Kind & RegKind_GPR && RegIdx.Index <= 31; 1513 } 1514 1515 bool isMM16AsmReg() const { 1516 if (!(isRegIdx() && RegIdx.Kind)) 1517 return false; 1518 return ((RegIdx.Index >= 2 && RegIdx.Index <= 7) 1519 || RegIdx.Index == 16 || RegIdx.Index == 17); 1520 1521 } 1522 bool isMM16AsmRegZero() const { 1523 if (!(isRegIdx() && RegIdx.Kind)) 1524 return false; 1525 return (RegIdx.Index == 0 || 1526 (RegIdx.Index >= 2 && RegIdx.Index <= 7) || 1527 RegIdx.Index == 17); 1528 } 1529 1530 bool isMM16AsmRegMoveP() const { 1531 if (!(isRegIdx() && RegIdx.Kind)) 1532 return false; 1533 return (RegIdx.Index == 0 || (RegIdx.Index >= 2 && RegIdx.Index <= 3) || 1534 (RegIdx.Index >= 16 && RegIdx.Index <= 20)); 1535 } 1536 1537 bool isFGRAsmReg() const { 1538 // AFGR64 is $0-$15 but we handle this in getAFGR64() 1539 return isRegIdx() && RegIdx.Kind & RegKind_FGR && RegIdx.Index <= 31; 1540 } 1541 1542 bool isHWRegsAsmReg() const { 1543 return isRegIdx() && RegIdx.Kind & RegKind_HWRegs && RegIdx.Index <= 31; 1544 } 1545 1546 bool isCCRAsmReg() const { 1547 return isRegIdx() && RegIdx.Kind & RegKind_CCR && RegIdx.Index <= 31; 1548 } 1549 1550 bool isFCCAsmReg() const { 1551 if (!(isRegIdx() && RegIdx.Kind & RegKind_FCC)) 1552 return false; 1553 return RegIdx.Index <= 7; 1554 } 1555 1556 bool isACCAsmReg() const { 1557 return isRegIdx() && RegIdx.Kind & RegKind_ACC && RegIdx.Index <= 3; 1558 } 1559 1560 bool isCOP0AsmReg() const { 1561 return isRegIdx() && RegIdx.Kind & RegKind_COP0 && RegIdx.Index <= 31; 1562 } 1563 1564 bool isCOP2AsmReg() const { 1565 return isRegIdx() && RegIdx.Kind & RegKind_COP2 && RegIdx.Index <= 31; 1566 } 1567 1568 bool isCOP3AsmReg() const { 1569 return isRegIdx() && RegIdx.Kind & RegKind_COP3 && RegIdx.Index <= 31; 1570 } 1571 1572 bool isMSA128AsmReg() const { 1573 return isRegIdx() && RegIdx.Kind & RegKind_MSA128 && RegIdx.Index <= 31; 1574 } 1575 1576 bool isMSACtrlAsmReg() const { 1577 return isRegIdx() && RegIdx.Kind & RegKind_MSACtrl && RegIdx.Index <= 7; 1578 } 1579 1580 /// getStartLoc - Get the location of the first token of this operand. 1581 SMLoc getStartLoc() const override { return StartLoc; } 1582 /// getEndLoc - Get the location of the last token of this operand. 1583 SMLoc getEndLoc() const override { return EndLoc; } 1584 1585 void print(raw_ostream &OS) const override { 1586 switch (Kind) { 1587 case k_Immediate: 1588 OS << "Imm<"; 1589 OS << *Imm.Val; 1590 OS << ">"; 1591 break; 1592 case k_Memory: 1593 OS << "Mem<"; 1594 Mem.Base->print(OS); 1595 OS << ", "; 1596 OS << *Mem.Off; 1597 OS << ">"; 1598 break; 1599 case k_RegisterIndex: 1600 OS << "RegIdx<" << RegIdx.Index << ":" << RegIdx.Kind << ", " 1601 << StringRef(RegIdx.Tok.Data, RegIdx.Tok.Length) << ">"; 1602 break; 1603 case k_Token: 1604 OS << getToken(); 1605 break; 1606 case k_RegList: 1607 OS << "RegList< "; 1608 for (auto Reg : (*RegList.List)) 1609 OS << Reg << " "; 1610 OS << ">"; 1611 break; 1612 case k_RegPair: 1613 OS << "RegPair<" << RegIdx.Index << "," << RegIdx.Index + 1 << ">"; 1614 break; 1615 } 1616 } 1617 1618 bool isValidForTie(const MipsOperand &Other) const { 1619 if (Kind != Other.Kind) 1620 return false; 1621 1622 switch (Kind) { 1623 default: 1624 llvm_unreachable("Unexpected kind"); 1625 return false; 1626 case k_RegisterIndex: { 1627 StringRef Token(RegIdx.Tok.Data, RegIdx.Tok.Length); 1628 StringRef OtherToken(Other.RegIdx.Tok.Data, Other.RegIdx.Tok.Length); 1629 return Token == OtherToken; 1630 } 1631 } 1632 } 1633 }; // class MipsOperand 1634 1635 } // end anonymous namespace 1636 1637 namespace llvm { 1638 1639 extern const MCInstrDesc MipsInsts[]; 1640 1641 } // end namespace llvm 1642 1643 static const MCInstrDesc &getInstDesc(unsigned Opcode) { 1644 return MipsInsts[Opcode]; 1645 } 1646 1647 static bool hasShortDelaySlot(unsigned Opcode) { 1648 switch (Opcode) { 1649 case Mips::JALS_MM: 1650 case Mips::JALRS_MM: 1651 case Mips::JALRS16_MM: 1652 case Mips::BGEZALS_MM: 1653 case Mips::BLTZALS_MM: 1654 return true; 1655 default: 1656 return false; 1657 } 1658 } 1659 1660 static const MCSymbol *getSingleMCSymbol(const MCExpr *Expr) { 1661 if (const MCSymbolRefExpr *SRExpr = dyn_cast<MCSymbolRefExpr>(Expr)) { 1662 return &SRExpr->getSymbol(); 1663 } 1664 1665 if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr)) { 1666 const MCSymbol *LHSSym = getSingleMCSymbol(BExpr->getLHS()); 1667 const MCSymbol *RHSSym = getSingleMCSymbol(BExpr->getRHS()); 1668 1669 if (LHSSym) 1670 return LHSSym; 1671 1672 if (RHSSym) 1673 return RHSSym; 1674 1675 return nullptr; 1676 } 1677 1678 if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr)) 1679 return getSingleMCSymbol(UExpr->getSubExpr()); 1680 1681 return nullptr; 1682 } 1683 1684 static unsigned countMCSymbolRefExpr(const MCExpr *Expr) { 1685 if (isa<MCSymbolRefExpr>(Expr)) 1686 return 1; 1687 1688 if (const MCBinaryExpr *BExpr = dyn_cast<MCBinaryExpr>(Expr)) 1689 return countMCSymbolRefExpr(BExpr->getLHS()) + 1690 countMCSymbolRefExpr(BExpr->getRHS()); 1691 1692 if (const MCUnaryExpr *UExpr = dyn_cast<MCUnaryExpr>(Expr)) 1693 return countMCSymbolRefExpr(UExpr->getSubExpr()); 1694 1695 return 0; 1696 } 1697 1698 bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc, 1699 MCStreamer &Out, 1700 const MCSubtargetInfo *STI) { 1701 MipsTargetStreamer &TOut = getTargetStreamer(); 1702 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode()); 1703 bool ExpandedJalSym = false; 1704 1705 Inst.setLoc(IDLoc); 1706 1707 if (MCID.isBranch() || MCID.isCall()) { 1708 const unsigned Opcode = Inst.getOpcode(); 1709 MCOperand Offset; 1710 1711 switch (Opcode) { 1712 default: 1713 break; 1714 case Mips::BBIT0: 1715 case Mips::BBIT032: 1716 case Mips::BBIT1: 1717 case Mips::BBIT132: 1718 assert(hasCnMips() && "instruction only valid for octeon cpus"); 1719 LLVM_FALLTHROUGH; 1720 1721 case Mips::BEQ: 1722 case Mips::BNE: 1723 case Mips::BEQ_MM: 1724 case Mips::BNE_MM: 1725 assert(MCID.getNumOperands() == 3 && "unexpected number of operands"); 1726 Offset = Inst.getOperand(2); 1727 if (!Offset.isImm()) 1728 break; // We'll deal with this situation later on when applying fixups. 1729 if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm())) 1730 return Error(IDLoc, "branch target out of range"); 1731 if (OffsetToAlignment(Offset.getImm(), 1732 1LL << (inMicroMipsMode() ? 1 : 2))) 1733 return Error(IDLoc, "branch to misaligned address"); 1734 break; 1735 case Mips::BGEZ: 1736 case Mips::BGTZ: 1737 case Mips::BLEZ: 1738 case Mips::BLTZ: 1739 case Mips::BGEZAL: 1740 case Mips::BLTZAL: 1741 case Mips::BC1F: 1742 case Mips::BC1T: 1743 case Mips::BGEZ_MM: 1744 case Mips::BGTZ_MM: 1745 case Mips::BLEZ_MM: 1746 case Mips::BLTZ_MM: 1747 case Mips::BGEZAL_MM: 1748 case Mips::BLTZAL_MM: 1749 case Mips::BC1F_MM: 1750 case Mips::BC1T_MM: 1751 case Mips::BC1EQZC_MMR6: 1752 case Mips::BC1NEZC_MMR6: 1753 case Mips::BC2EQZC_MMR6: 1754 case Mips::BC2NEZC_MMR6: 1755 assert(MCID.getNumOperands() == 2 && "unexpected number of operands"); 1756 Offset = Inst.getOperand(1); 1757 if (!Offset.isImm()) 1758 break; // We'll deal with this situation later on when applying fixups. 1759 if (!isIntN(inMicroMipsMode() ? 17 : 18, Offset.getImm())) 1760 return Error(IDLoc, "branch target out of range"); 1761 if (OffsetToAlignment(Offset.getImm(), 1762 1LL << (inMicroMipsMode() ? 1 : 2))) 1763 return Error(IDLoc, "branch to misaligned address"); 1764 break; 1765 case Mips::BGEC: case Mips::BGEC_MMR6: 1766 case Mips::BLTC: case Mips::BLTC_MMR6: 1767 case Mips::BGEUC: case Mips::BGEUC_MMR6: 1768 case Mips::BLTUC: case Mips::BLTUC_MMR6: 1769 case Mips::BEQC: case Mips::BEQC_MMR6: 1770 case Mips::BNEC: case Mips::BNEC_MMR6: 1771 assert(MCID.getNumOperands() == 3 && "unexpected number of operands"); 1772 Offset = Inst.getOperand(2); 1773 if (!Offset.isImm()) 1774 break; // We'll deal with this situation later on when applying fixups. 1775 if (!isIntN(18, Offset.getImm())) 1776 return Error(IDLoc, "branch target out of range"); 1777 if (OffsetToAlignment(Offset.getImm(), 1LL << 2)) 1778 return Error(IDLoc, "branch to misaligned address"); 1779 break; 1780 case Mips::BLEZC: case Mips::BLEZC_MMR6: 1781 case Mips::BGEZC: case Mips::BGEZC_MMR6: 1782 case Mips::BGTZC: case Mips::BGTZC_MMR6: 1783 case Mips::BLTZC: case Mips::BLTZC_MMR6: 1784 assert(MCID.getNumOperands() == 2 && "unexpected number of operands"); 1785 Offset = Inst.getOperand(1); 1786 if (!Offset.isImm()) 1787 break; // We'll deal with this situation later on when applying fixups. 1788 if (!isIntN(18, Offset.getImm())) 1789 return Error(IDLoc, "branch target out of range"); 1790 if (OffsetToAlignment(Offset.getImm(), 1LL << 2)) 1791 return Error(IDLoc, "branch to misaligned address"); 1792 break; 1793 case Mips::BEQZC: case Mips::BEQZC_MMR6: 1794 case Mips::BNEZC: case Mips::BNEZC_MMR6: 1795 assert(MCID.getNumOperands() == 2 && "unexpected number of operands"); 1796 Offset = Inst.getOperand(1); 1797 if (!Offset.isImm()) 1798 break; // We'll deal with this situation later on when applying fixups. 1799 if (!isIntN(23, Offset.getImm())) 1800 return Error(IDLoc, "branch target out of range"); 1801 if (OffsetToAlignment(Offset.getImm(), 1LL << 2)) 1802 return Error(IDLoc, "branch to misaligned address"); 1803 break; 1804 case Mips::BEQZ16_MM: 1805 case Mips::BEQZC16_MMR6: 1806 case Mips::BNEZ16_MM: 1807 case Mips::BNEZC16_MMR6: 1808 assert(MCID.getNumOperands() == 2 && "unexpected number of operands"); 1809 Offset = Inst.getOperand(1); 1810 if (!Offset.isImm()) 1811 break; // We'll deal with this situation later on when applying fixups. 1812 if (!isInt<8>(Offset.getImm())) 1813 return Error(IDLoc, "branch target out of range"); 1814 if (OffsetToAlignment(Offset.getImm(), 2LL)) 1815 return Error(IDLoc, "branch to misaligned address"); 1816 break; 1817 } 1818 } 1819 1820 // SSNOP is deprecated on MIPS32r6/MIPS64r6 1821 // We still accept it but it is a normal nop. 1822 if (hasMips32r6() && Inst.getOpcode() == Mips::SSNOP) { 1823 std::string ISA = hasMips64r6() ? "MIPS64r6" : "MIPS32r6"; 1824 Warning(IDLoc, "ssnop is deprecated for " + ISA + " and is equivalent to a " 1825 "nop instruction"); 1826 } 1827 1828 if (hasCnMips()) { 1829 const unsigned Opcode = Inst.getOpcode(); 1830 MCOperand Opnd; 1831 int Imm; 1832 1833 switch (Opcode) { 1834 default: 1835 break; 1836 1837 case Mips::BBIT0: 1838 case Mips::BBIT032: 1839 case Mips::BBIT1: 1840 case Mips::BBIT132: 1841 assert(MCID.getNumOperands() == 3 && "unexpected number of operands"); 1842 // The offset is handled above 1843 Opnd = Inst.getOperand(1); 1844 if (!Opnd.isImm()) 1845 return Error(IDLoc, "expected immediate operand kind"); 1846 Imm = Opnd.getImm(); 1847 if (Imm < 0 || Imm > (Opcode == Mips::BBIT0 || 1848 Opcode == Mips::BBIT1 ? 63 : 31)) 1849 return Error(IDLoc, "immediate operand value out of range"); 1850 if (Imm > 31) { 1851 Inst.setOpcode(Opcode == Mips::BBIT0 ? Mips::BBIT032 1852 : Mips::BBIT132); 1853 Inst.getOperand(1).setImm(Imm - 32); 1854 } 1855 break; 1856 1857 case Mips::SEQi: 1858 case Mips::SNEi: 1859 assert(MCID.getNumOperands() == 3 && "unexpected number of operands"); 1860 Opnd = Inst.getOperand(2); 1861 if (!Opnd.isImm()) 1862 return Error(IDLoc, "expected immediate operand kind"); 1863 Imm = Opnd.getImm(); 1864 if (!isInt<10>(Imm)) 1865 return Error(IDLoc, "immediate operand value out of range"); 1866 break; 1867 } 1868 } 1869 1870 // For PIC code convert unconditional jump to unconditional branch. 1871 if ((Inst.getOpcode() == Mips::J || Inst.getOpcode() == Mips::J_MM) && 1872 inPicMode()) { 1873 MCInst BInst; 1874 BInst.setOpcode(inMicroMipsMode() ? Mips::BEQ_MM : Mips::BEQ); 1875 BInst.addOperand(MCOperand::createReg(Mips::ZERO)); 1876 BInst.addOperand(MCOperand::createReg(Mips::ZERO)); 1877 BInst.addOperand(Inst.getOperand(0)); 1878 Inst = BInst; 1879 } 1880 1881 // This expansion is not in a function called by tryExpandInstruction() 1882 // because the pseudo-instruction doesn't have a distinct opcode. 1883 if ((Inst.getOpcode() == Mips::JAL || Inst.getOpcode() == Mips::JAL_MM) && 1884 inPicMode()) { 1885 warnIfNoMacro(IDLoc); 1886 1887 const MCExpr *JalExpr = Inst.getOperand(0).getExpr(); 1888 1889 // We can do this expansion if there's only 1 symbol in the argument 1890 // expression. 1891 if (countMCSymbolRefExpr(JalExpr) > 1) 1892 return Error(IDLoc, "jal doesn't support multiple symbols in PIC mode"); 1893 1894 // FIXME: This is checking the expression can be handled by the later stages 1895 // of the assembler. We ought to leave it to those later stages. 1896 const MCSymbol *JalSym = getSingleMCSymbol(JalExpr); 1897 1898 // FIXME: Add support for label+offset operands (currently causes an error). 1899 // FIXME: Add support for forward-declared local symbols. 1900 // FIXME: Add expansion for when the LargeGOT option is enabled. 1901 if (JalSym->isInSection() || JalSym->isTemporary() || 1902 (JalSym->isELF() && cast<MCSymbolELF>(JalSym)->getBinding() == ELF::STB_LOCAL)) { 1903 if (isABI_O32()) { 1904 // If it's a local symbol and the O32 ABI is being used, we expand to: 1905 // lw $25, 0($gp) 1906 // R_(MICRO)MIPS_GOT16 label 1907 // addiu $25, $25, 0 1908 // R_(MICRO)MIPS_LO16 label 1909 // jalr $25 1910 const MCExpr *Got16RelocExpr = 1911 MipsMCExpr::create(MipsMCExpr::MEK_GOT, JalExpr, getContext()); 1912 const MCExpr *Lo16RelocExpr = 1913 MipsMCExpr::create(MipsMCExpr::MEK_LO, JalExpr, getContext()); 1914 1915 TOut.emitRRX(Mips::LW, Mips::T9, Mips::GP, 1916 MCOperand::createExpr(Got16RelocExpr), IDLoc, STI); 1917 TOut.emitRRX(Mips::ADDiu, Mips::T9, Mips::T9, 1918 MCOperand::createExpr(Lo16RelocExpr), IDLoc, STI); 1919 } else if (isABI_N32() || isABI_N64()) { 1920 // If it's a local symbol and the N32/N64 ABIs are being used, 1921 // we expand to: 1922 // lw/ld $25, 0($gp) 1923 // R_(MICRO)MIPS_GOT_DISP label 1924 // jalr $25 1925 const MCExpr *GotDispRelocExpr = 1926 MipsMCExpr::create(MipsMCExpr::MEK_GOT_DISP, JalExpr, getContext()); 1927 1928 TOut.emitRRX(ABI.ArePtrs64bit() ? Mips::LD : Mips::LW, Mips::T9, 1929 Mips::GP, MCOperand::createExpr(GotDispRelocExpr), IDLoc, 1930 STI); 1931 } 1932 } else { 1933 // If it's an external/weak symbol, we expand to: 1934 // lw/ld $25, 0($gp) 1935 // R_(MICRO)MIPS_CALL16 label 1936 // jalr $25 1937 const MCExpr *Call16RelocExpr = 1938 MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, JalExpr, getContext()); 1939 1940 TOut.emitRRX(ABI.ArePtrs64bit() ? Mips::LD : Mips::LW, Mips::T9, Mips::GP, 1941 MCOperand::createExpr(Call16RelocExpr), IDLoc, STI); 1942 } 1943 1944 MCInst JalrInst; 1945 if (IsCpRestoreSet && inMicroMipsMode()) 1946 JalrInst.setOpcode(Mips::JALRS_MM); 1947 else 1948 JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR); 1949 JalrInst.addOperand(MCOperand::createReg(Mips::RA)); 1950 JalrInst.addOperand(MCOperand::createReg(Mips::T9)); 1951 1952 // FIXME: Add an R_(MICRO)MIPS_JALR relocation after the JALR. 1953 // This relocation is supposed to be an optimization hint for the linker 1954 // and is not necessary for correctness. 1955 1956 Inst = JalrInst; 1957 ExpandedJalSym = true; 1958 } 1959 1960 bool IsPCRelativeLoad = (MCID.TSFlags & MipsII::IsPCRelativeLoad) != 0; 1961 if ((MCID.mayLoad() || MCID.mayStore()) && !IsPCRelativeLoad) { 1962 // Check the offset of memory operand, if it is a symbol 1963 // reference or immediate we may have to expand instructions. 1964 for (unsigned i = 0; i < MCID.getNumOperands(); i++) { 1965 const MCOperandInfo &OpInfo = MCID.OpInfo[i]; 1966 if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) || 1967 (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) { 1968 MCOperand &Op = Inst.getOperand(i); 1969 if (Op.isImm()) { 1970 int MemOffset = Op.getImm(); 1971 if (MemOffset < -32768 || MemOffset > 32767) { 1972 // Offset can't exceed 16bit value. 1973 expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad(), true); 1974 return getParser().hasPendingError(); 1975 } 1976 } else if (Op.isExpr()) { 1977 const MCExpr *Expr = Op.getExpr(); 1978 if (Expr->getKind() == MCExpr::SymbolRef) { 1979 const MCSymbolRefExpr *SR = 1980 static_cast<const MCSymbolRefExpr *>(Expr); 1981 if (SR->getKind() == MCSymbolRefExpr::VK_None) { 1982 // Expand symbol. 1983 expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad(), false); 1984 return getParser().hasPendingError(); 1985 } 1986 } else if (!isEvaluated(Expr)) { 1987 expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad(), false); 1988 return getParser().hasPendingError(); 1989 } 1990 } 1991 } 1992 } // for 1993 } // if load/store 1994 1995 if (inMicroMipsMode()) { 1996 if (MCID.mayLoad()) { 1997 // Try to create 16-bit GP relative load instruction. 1998 for (unsigned i = 0; i < MCID.getNumOperands(); i++) { 1999 const MCOperandInfo &OpInfo = MCID.OpInfo[i]; 2000 if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) || 2001 (OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) { 2002 MCOperand &Op = Inst.getOperand(i); 2003 if (Op.isImm()) { 2004 int MemOffset = Op.getImm(); 2005 MCOperand &DstReg = Inst.getOperand(0); 2006 MCOperand &BaseReg = Inst.getOperand(1); 2007 if (isInt<9>(MemOffset) && (MemOffset % 4 == 0) && 2008 getContext().getRegisterInfo()->getRegClass( 2009 Mips::GPRMM16RegClassID).contains(DstReg.getReg()) && 2010 (BaseReg.getReg() == Mips::GP || 2011 BaseReg.getReg() == Mips::GP_64)) { 2012 2013 TOut.emitRRI(Mips::LWGP_MM, DstReg.getReg(), Mips::GP, MemOffset, 2014 IDLoc, STI); 2015 return false; 2016 } 2017 } 2018 } 2019 } // for 2020 } // if load 2021 2022 // TODO: Handle this with the AsmOperandClass.PredicateMethod. 2023 2024 MCOperand Opnd; 2025 int Imm; 2026 2027 switch (Inst.getOpcode()) { 2028 default: 2029 break; 2030 case Mips::ADDIUSP_MM: 2031 Opnd = Inst.getOperand(0); 2032 if (!Opnd.isImm()) 2033 return Error(IDLoc, "expected immediate operand kind"); 2034 Imm = Opnd.getImm(); 2035 if (Imm < -1032 || Imm > 1028 || (Imm < 8 && Imm > -12) || 2036 Imm % 4 != 0) 2037 return Error(IDLoc, "immediate operand value out of range"); 2038 break; 2039 case Mips::SLL16_MM: 2040 case Mips::SRL16_MM: 2041 Opnd = Inst.getOperand(2); 2042 if (!Opnd.isImm()) 2043 return Error(IDLoc, "expected immediate operand kind"); 2044 Imm = Opnd.getImm(); 2045 if (Imm < 1 || Imm > 8) 2046 return Error(IDLoc, "immediate operand value out of range"); 2047 break; 2048 case Mips::LI16_MM: 2049 Opnd = Inst.getOperand(1); 2050 if (!Opnd.isImm()) 2051 return Error(IDLoc, "expected immediate operand kind"); 2052 Imm = Opnd.getImm(); 2053 if (Imm < -1 || Imm > 126) 2054 return Error(IDLoc, "immediate operand value out of range"); 2055 break; 2056 case Mips::ADDIUR2_MM: 2057 Opnd = Inst.getOperand(2); 2058 if (!Opnd.isImm()) 2059 return Error(IDLoc, "expected immediate operand kind"); 2060 Imm = Opnd.getImm(); 2061 if (!(Imm == 1 || Imm == -1 || 2062 ((Imm % 4 == 0) && Imm < 28 && Imm > 0))) 2063 return Error(IDLoc, "immediate operand value out of range"); 2064 break; 2065 case Mips::ANDI16_MM: 2066 Opnd = Inst.getOperand(2); 2067 if (!Opnd.isImm()) 2068 return Error(IDLoc, "expected immediate operand kind"); 2069 Imm = Opnd.getImm(); 2070 if (!(Imm == 128 || (Imm >= 1 && Imm <= 4) || Imm == 7 || Imm == 8 || 2071 Imm == 15 || Imm == 16 || Imm == 31 || Imm == 32 || Imm == 63 || 2072 Imm == 64 || Imm == 255 || Imm == 32768 || Imm == 65535)) 2073 return Error(IDLoc, "immediate operand value out of range"); 2074 break; 2075 case Mips::LBU16_MM: 2076 Opnd = Inst.getOperand(2); 2077 if (!Opnd.isImm()) 2078 return Error(IDLoc, "expected immediate operand kind"); 2079 Imm = Opnd.getImm(); 2080 if (Imm < -1 || Imm > 14) 2081 return Error(IDLoc, "immediate operand value out of range"); 2082 break; 2083 case Mips::SB16_MM: 2084 case Mips::SB16_MMR6: 2085 Opnd = Inst.getOperand(2); 2086 if (!Opnd.isImm()) 2087 return Error(IDLoc, "expected immediate operand kind"); 2088 Imm = Opnd.getImm(); 2089 if (Imm < 0 || Imm > 15) 2090 return Error(IDLoc, "immediate operand value out of range"); 2091 break; 2092 case Mips::LHU16_MM: 2093 case Mips::SH16_MM: 2094 case Mips::SH16_MMR6: 2095 Opnd = Inst.getOperand(2); 2096 if (!Opnd.isImm()) 2097 return Error(IDLoc, "expected immediate operand kind"); 2098 Imm = Opnd.getImm(); 2099 if (Imm < 0 || Imm > 30 || (Imm % 2 != 0)) 2100 return Error(IDLoc, "immediate operand value out of range"); 2101 break; 2102 case Mips::LW16_MM: 2103 case Mips::SW16_MM: 2104 case Mips::SW16_MMR6: 2105 Opnd = Inst.getOperand(2); 2106 if (!Opnd.isImm()) 2107 return Error(IDLoc, "expected immediate operand kind"); 2108 Imm = Opnd.getImm(); 2109 if (Imm < 0 || Imm > 60 || (Imm % 4 != 0)) 2110 return Error(IDLoc, "immediate operand value out of range"); 2111 break; 2112 case Mips::ADDIUPC_MM: 2113 MCOperand Opnd = Inst.getOperand(1); 2114 if (!Opnd.isImm()) 2115 return Error(IDLoc, "expected immediate operand kind"); 2116 int Imm = Opnd.getImm(); 2117 if ((Imm % 4 != 0) || !isInt<25>(Imm)) 2118 return Error(IDLoc, "immediate operand value out of range"); 2119 break; 2120 } 2121 } 2122 2123 bool FillDelaySlot = 2124 MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder(); 2125 if (FillDelaySlot) 2126 TOut.emitDirectiveSetNoReorder(); 2127 2128 MacroExpanderResultTy ExpandResult = 2129 tryExpandInstruction(Inst, IDLoc, Out, STI); 2130 switch (ExpandResult) { 2131 case MER_NotAMacro: 2132 Out.EmitInstruction(Inst, *STI); 2133 break; 2134 case MER_Success: 2135 break; 2136 case MER_Fail: 2137 return true; 2138 } 2139 2140 // We know we emitted an instruction on the MER_NotAMacro or MER_Success path. 2141 // If we're in microMIPS mode then we must also set EF_MIPS_MICROMIPS. 2142 if (inMicroMipsMode()) 2143 TOut.setUsesMicroMips(); 2144 2145 // If this instruction has a delay slot and .set reorder is active, 2146 // emit a NOP after it. 2147 if (FillDelaySlot) { 2148 TOut.emitEmptyDelaySlot(hasShortDelaySlot(Inst.getOpcode()), IDLoc, STI); 2149 TOut.emitDirectiveSetReorder(); 2150 } 2151 2152 if ((Inst.getOpcode() == Mips::JalOneReg || 2153 Inst.getOpcode() == Mips::JalTwoReg || ExpandedJalSym) && 2154 isPicAndNotNxxAbi()) { 2155 if (IsCpRestoreSet) { 2156 // We need a NOP between the JALR and the LW: 2157 // If .set reorder has been used, we've already emitted a NOP. 2158 // If .set noreorder has been used, we need to emit a NOP at this point. 2159 if (!AssemblerOptions.back()->isReorder()) 2160 TOut.emitEmptyDelaySlot(hasShortDelaySlot(Inst.getOpcode()), IDLoc, 2161 STI); 2162 2163 // Load the $gp from the stack. 2164 TOut.emitGPRestore(CpRestoreOffset, IDLoc, STI); 2165 } else 2166 Warning(IDLoc, "no .cprestore used in PIC mode"); 2167 } 2168 2169 return false; 2170 } 2171 2172 MipsAsmParser::MacroExpanderResultTy 2173 MipsAsmParser::tryExpandInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 2174 const MCSubtargetInfo *STI) { 2175 switch (Inst.getOpcode()) { 2176 default: 2177 return MER_NotAMacro; 2178 case Mips::LoadImm32: 2179 return expandLoadImm(Inst, true, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2180 case Mips::LoadImm64: 2181 return expandLoadImm(Inst, false, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2182 case Mips::LoadAddrImm32: 2183 case Mips::LoadAddrImm64: 2184 assert(Inst.getOperand(0).isReg() && "expected register operand kind"); 2185 assert((Inst.getOperand(1).isImm() || Inst.getOperand(1).isExpr()) && 2186 "expected immediate operand kind"); 2187 2188 return expandLoadAddress(Inst.getOperand(0).getReg(), Mips::NoRegister, 2189 Inst.getOperand(1), 2190 Inst.getOpcode() == Mips::LoadAddrImm32, IDLoc, 2191 Out, STI) 2192 ? MER_Fail 2193 : MER_Success; 2194 case Mips::LoadAddrReg32: 2195 case Mips::LoadAddrReg64: 2196 assert(Inst.getOperand(0).isReg() && "expected register operand kind"); 2197 assert(Inst.getOperand(1).isReg() && "expected register operand kind"); 2198 assert((Inst.getOperand(2).isImm() || Inst.getOperand(2).isExpr()) && 2199 "expected immediate operand kind"); 2200 2201 return expandLoadAddress(Inst.getOperand(0).getReg(), 2202 Inst.getOperand(1).getReg(), Inst.getOperand(2), 2203 Inst.getOpcode() == Mips::LoadAddrReg32, IDLoc, 2204 Out, STI) 2205 ? MER_Fail 2206 : MER_Success; 2207 case Mips::B_MM_Pseudo: 2208 case Mips::B_MMR6_Pseudo: 2209 return expandUncondBranchMMPseudo(Inst, IDLoc, Out, STI) ? MER_Fail 2210 : MER_Success; 2211 case Mips::SWM_MM: 2212 case Mips::LWM_MM: 2213 return expandLoadStoreMultiple(Inst, IDLoc, Out, STI) ? MER_Fail 2214 : MER_Success; 2215 case Mips::JalOneReg: 2216 case Mips::JalTwoReg: 2217 return expandJalWithRegs(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2218 case Mips::BneImm: 2219 case Mips::BeqImm: 2220 case Mips::BEQLImmMacro: 2221 case Mips::BNELImmMacro: 2222 return expandBranchImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2223 case Mips::BLT: 2224 case Mips::BLE: 2225 case Mips::BGE: 2226 case Mips::BGT: 2227 case Mips::BLTU: 2228 case Mips::BLEU: 2229 case Mips::BGEU: 2230 case Mips::BGTU: 2231 case Mips::BLTL: 2232 case Mips::BLEL: 2233 case Mips::BGEL: 2234 case Mips::BGTL: 2235 case Mips::BLTUL: 2236 case Mips::BLEUL: 2237 case Mips::BGEUL: 2238 case Mips::BGTUL: 2239 case Mips::BLTImmMacro: 2240 case Mips::BLEImmMacro: 2241 case Mips::BGEImmMacro: 2242 case Mips::BGTImmMacro: 2243 case Mips::BLTUImmMacro: 2244 case Mips::BLEUImmMacro: 2245 case Mips::BGEUImmMacro: 2246 case Mips::BGTUImmMacro: 2247 case Mips::BLTLImmMacro: 2248 case Mips::BLELImmMacro: 2249 case Mips::BGELImmMacro: 2250 case Mips::BGTLImmMacro: 2251 case Mips::BLTULImmMacro: 2252 case Mips::BLEULImmMacro: 2253 case Mips::BGEULImmMacro: 2254 case Mips::BGTULImmMacro: 2255 return expandCondBranches(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2256 case Mips::SDivMacro: 2257 case Mips::SDivIMacro: 2258 return expandDiv(Inst, IDLoc, Out, STI, false, true) ? MER_Fail 2259 : MER_Success; 2260 case Mips::DSDivMacro: 2261 case Mips::DSDivIMacro: 2262 return expandDiv(Inst, IDLoc, Out, STI, true, true) ? MER_Fail 2263 : MER_Success; 2264 case Mips::UDivMacro: 2265 case Mips::UDivIMacro: 2266 return expandDiv(Inst, IDLoc, Out, STI, false, false) ? MER_Fail 2267 : MER_Success; 2268 case Mips::DUDivMacro: 2269 case Mips::DUDivIMacro: 2270 return expandDiv(Inst, IDLoc, Out, STI, true, false) ? MER_Fail 2271 : MER_Success; 2272 case Mips::PseudoTRUNC_W_S: 2273 return expandTrunc(Inst, false, false, IDLoc, Out, STI) ? MER_Fail 2274 : MER_Success; 2275 case Mips::PseudoTRUNC_W_D32: 2276 return expandTrunc(Inst, true, false, IDLoc, Out, STI) ? MER_Fail 2277 : MER_Success; 2278 case Mips::PseudoTRUNC_W_D: 2279 return expandTrunc(Inst, true, true, IDLoc, Out, STI) ? MER_Fail 2280 : MER_Success; 2281 case Mips::Ulh: 2282 return expandUlh(Inst, true, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2283 case Mips::Ulhu: 2284 return expandUlh(Inst, false, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2285 case Mips::Ush: 2286 return expandUsh(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2287 case Mips::Ulw: 2288 case Mips::Usw: 2289 return expandUxw(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2290 case Mips::NORImm: 2291 return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2292 case Mips::ADDi: 2293 case Mips::ADDiu: 2294 case Mips::SLTi: 2295 case Mips::SLTiu: 2296 if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() && 2297 Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) { 2298 int64_t ImmValue = Inst.getOperand(2).getImm(); 2299 if (isInt<16>(ImmValue)) 2300 return MER_NotAMacro; 2301 return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail 2302 : MER_Success; 2303 } 2304 return MER_NotAMacro; 2305 case Mips::ANDi: 2306 case Mips::ORi: 2307 case Mips::XORi: 2308 if ((Inst.getNumOperands() == 3) && Inst.getOperand(0).isReg() && 2309 Inst.getOperand(1).isReg() && Inst.getOperand(2).isImm()) { 2310 int64_t ImmValue = Inst.getOperand(2).getImm(); 2311 if (isUInt<16>(ImmValue)) 2312 return MER_NotAMacro; 2313 return expandAliasImmediate(Inst, IDLoc, Out, STI) ? MER_Fail 2314 : MER_Success; 2315 } 2316 return MER_NotAMacro; 2317 case Mips::ROL: 2318 case Mips::ROR: 2319 return expandRotation(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2320 case Mips::ROLImm: 2321 case Mips::RORImm: 2322 return expandRotationImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2323 case Mips::DROL: 2324 case Mips::DROR: 2325 return expandDRotation(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2326 case Mips::DROLImm: 2327 case Mips::DRORImm: 2328 return expandDRotationImm(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2329 case Mips::ABSMacro: 2330 return expandAbs(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2331 case Mips::LDMacro: 2332 case Mips::SDMacro: 2333 return expandLoadStoreDMacro(Inst, IDLoc, Out, STI, 2334 Inst.getOpcode() == Mips::LDMacro) 2335 ? MER_Fail 2336 : MER_Success; 2337 case Mips::SEQMacro: 2338 return expandSeq(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2339 case Mips::SEQIMacro: 2340 return expandSeqI(Inst, IDLoc, Out, STI) ? MER_Fail : MER_Success; 2341 } 2342 } 2343 2344 bool MipsAsmParser::expandJalWithRegs(MCInst &Inst, SMLoc IDLoc, 2345 MCStreamer &Out, 2346 const MCSubtargetInfo *STI) { 2347 MipsTargetStreamer &TOut = getTargetStreamer(); 2348 2349 // Create a JALR instruction which is going to replace the pseudo-JAL. 2350 MCInst JalrInst; 2351 JalrInst.setLoc(IDLoc); 2352 const MCOperand FirstRegOp = Inst.getOperand(0); 2353 const unsigned Opcode = Inst.getOpcode(); 2354 2355 if (Opcode == Mips::JalOneReg) { 2356 // jal $rs => jalr $rs 2357 if (IsCpRestoreSet && inMicroMipsMode()) { 2358 JalrInst.setOpcode(Mips::JALRS16_MM); 2359 JalrInst.addOperand(FirstRegOp); 2360 } else if (inMicroMipsMode()) { 2361 JalrInst.setOpcode(hasMips32r6() ? Mips::JALRC16_MMR6 : Mips::JALR16_MM); 2362 JalrInst.addOperand(FirstRegOp); 2363 } else { 2364 JalrInst.setOpcode(Mips::JALR); 2365 JalrInst.addOperand(MCOperand::createReg(Mips::RA)); 2366 JalrInst.addOperand(FirstRegOp); 2367 } 2368 } else if (Opcode == Mips::JalTwoReg) { 2369 // jal $rd, $rs => jalr $rd, $rs 2370 if (IsCpRestoreSet && inMicroMipsMode()) 2371 JalrInst.setOpcode(Mips::JALRS_MM); 2372 else 2373 JalrInst.setOpcode(inMicroMipsMode() ? Mips::JALR_MM : Mips::JALR); 2374 JalrInst.addOperand(FirstRegOp); 2375 const MCOperand SecondRegOp = Inst.getOperand(1); 2376 JalrInst.addOperand(SecondRegOp); 2377 } 2378 Out.EmitInstruction(JalrInst, *STI); 2379 2380 // If .set reorder is active and branch instruction has a delay slot, 2381 // emit a NOP after it. 2382 const MCInstrDesc &MCID = getInstDesc(JalrInst.getOpcode()); 2383 if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) 2384 TOut.emitEmptyDelaySlot(hasShortDelaySlot(JalrInst.getOpcode()), IDLoc, 2385 STI); 2386 2387 return false; 2388 } 2389 2390 /// Can the value be represented by a unsigned N-bit value and a shift left? 2391 template <unsigned N> static bool isShiftedUIntAtAnyPosition(uint64_t x) { 2392 unsigned BitNum = findFirstSet(x); 2393 2394 return (x == x >> BitNum << BitNum) && isUInt<N>(x >> BitNum); 2395 } 2396 2397 /// Load (or add) an immediate into a register. 2398 /// 2399 /// @param ImmValue The immediate to load. 2400 /// @param DstReg The register that will hold the immediate. 2401 /// @param SrcReg A register to add to the immediate or Mips::NoRegister 2402 /// for a simple initialization. 2403 /// @param Is32BitImm Is ImmValue 32-bit or 64-bit? 2404 /// @param IsAddress True if the immediate represents an address. False if it 2405 /// is an integer. 2406 /// @param IDLoc Location of the immediate in the source file. 2407 bool MipsAsmParser::loadImmediate(int64_t ImmValue, unsigned DstReg, 2408 unsigned SrcReg, bool Is32BitImm, 2409 bool IsAddress, SMLoc IDLoc, MCStreamer &Out, 2410 const MCSubtargetInfo *STI) { 2411 MipsTargetStreamer &TOut = getTargetStreamer(); 2412 2413 if (!Is32BitImm && !isGP64bit()) { 2414 Error(IDLoc, "instruction requires a 64-bit architecture"); 2415 return true; 2416 } 2417 2418 if (Is32BitImm) { 2419 if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) { 2420 // Sign extend up to 64-bit so that the predicates match the hardware 2421 // behaviour. In particular, isInt<16>(0xffff8000) and similar should be 2422 // true. 2423 ImmValue = SignExtend64<32>(ImmValue); 2424 } else { 2425 Error(IDLoc, "instruction requires a 32-bit immediate"); 2426 return true; 2427 } 2428 } 2429 2430 unsigned ZeroReg = IsAddress ? ABI.GetNullPtr() : ABI.GetZeroReg(); 2431 unsigned AdduOp = !Is32BitImm ? Mips::DADDu : Mips::ADDu; 2432 2433 bool UseSrcReg = false; 2434 if (SrcReg != Mips::NoRegister) 2435 UseSrcReg = true; 2436 2437 unsigned TmpReg = DstReg; 2438 if (UseSrcReg && 2439 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg)) { 2440 // At this point we need AT to perform the expansions and we exit if it is 2441 // not available. 2442 unsigned ATReg = getATReg(IDLoc); 2443 if (!ATReg) 2444 return true; 2445 TmpReg = ATReg; 2446 } 2447 2448 if (isInt<16>(ImmValue)) { 2449 if (!UseSrcReg) 2450 SrcReg = ZeroReg; 2451 2452 // This doesn't quite follow the usual ABI expectations for N32 but matches 2453 // traditional assembler behaviour. N32 would normally use addiu for both 2454 // integers and addresses. 2455 if (IsAddress && !Is32BitImm) { 2456 TOut.emitRRI(Mips::DADDiu, DstReg, SrcReg, ImmValue, IDLoc, STI); 2457 return false; 2458 } 2459 2460 TOut.emitRRI(Mips::ADDiu, DstReg, SrcReg, ImmValue, IDLoc, STI); 2461 return false; 2462 } 2463 2464 if (isUInt<16>(ImmValue)) { 2465 unsigned TmpReg = DstReg; 2466 if (SrcReg == DstReg) { 2467 TmpReg = getATReg(IDLoc); 2468 if (!TmpReg) 2469 return true; 2470 } 2471 2472 TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, ImmValue, IDLoc, STI); 2473 if (UseSrcReg) 2474 TOut.emitRRR(ABI.GetPtrAdduOp(), DstReg, TmpReg, SrcReg, IDLoc, STI); 2475 return false; 2476 } 2477 2478 if (isInt<32>(ImmValue) || isUInt<32>(ImmValue)) { 2479 warnIfNoMacro(IDLoc); 2480 2481 uint16_t Bits31To16 = (ImmValue >> 16) & 0xffff; 2482 uint16_t Bits15To0 = ImmValue & 0xffff; 2483 2484 if (!Is32BitImm && !isInt<32>(ImmValue)) { 2485 // Traditional behaviour seems to special case this particular value. It's 2486 // not clear why other masks are handled differently. 2487 if (ImmValue == 0xffffffff) { 2488 TOut.emitRI(Mips::LUi, TmpReg, 0xffff, IDLoc, STI); 2489 TOut.emitRRI(Mips::DSRL32, TmpReg, TmpReg, 0, IDLoc, STI); 2490 if (UseSrcReg) 2491 TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI); 2492 return false; 2493 } 2494 2495 // Expand to an ORi instead of a LUi to avoid sign-extending into the 2496 // upper 32 bits. 2497 TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits31To16, IDLoc, STI); 2498 TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, 16, IDLoc, STI); 2499 if (Bits15To0) 2500 TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, STI); 2501 if (UseSrcReg) 2502 TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI); 2503 return false; 2504 } 2505 2506 TOut.emitRI(Mips::LUi, TmpReg, Bits31To16, IDLoc, STI); 2507 if (Bits15To0) 2508 TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, Bits15To0, IDLoc, STI); 2509 if (UseSrcReg) 2510 TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI); 2511 return false; 2512 } 2513 2514 if (isShiftedUIntAtAnyPosition<16>(ImmValue)) { 2515 if (Is32BitImm) { 2516 Error(IDLoc, "instruction requires a 32-bit immediate"); 2517 return true; 2518 } 2519 2520 // Traditionally, these immediates are shifted as little as possible and as 2521 // such we align the most significant bit to bit 15 of our temporary. 2522 unsigned FirstSet = findFirstSet((uint64_t)ImmValue); 2523 unsigned LastSet = findLastSet((uint64_t)ImmValue); 2524 unsigned ShiftAmount = FirstSet - (15 - (LastSet - FirstSet)); 2525 uint16_t Bits = (ImmValue >> ShiftAmount) & 0xffff; 2526 TOut.emitRRI(Mips::ORi, TmpReg, ZeroReg, Bits, IDLoc, STI); 2527 TOut.emitRRI(Mips::DSLL, TmpReg, TmpReg, ShiftAmount, IDLoc, STI); 2528 2529 if (UseSrcReg) 2530 TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI); 2531 2532 return false; 2533 } 2534 2535 warnIfNoMacro(IDLoc); 2536 2537 // The remaining case is packed with a sequence of dsll and ori with zeros 2538 // being omitted and any neighbouring dsll's being coalesced. 2539 // The highest 32-bit's are equivalent to a 32-bit immediate load. 2540 2541 // Load bits 32-63 of ImmValue into bits 0-31 of the temporary register. 2542 if (loadImmediate(ImmValue >> 32, TmpReg, Mips::NoRegister, true, false, 2543 IDLoc, Out, STI)) 2544 return false; 2545 2546 // Shift and accumulate into the register. If a 16-bit chunk is zero, then 2547 // skip it and defer the shift to the next chunk. 2548 unsigned ShiftCarriedForwards = 16; 2549 for (int BitNum = 16; BitNum >= 0; BitNum -= 16) { 2550 uint16_t ImmChunk = (ImmValue >> BitNum) & 0xffff; 2551 2552 if (ImmChunk != 0) { 2553 TOut.emitDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc, STI); 2554 TOut.emitRRI(Mips::ORi, TmpReg, TmpReg, ImmChunk, IDLoc, STI); 2555 ShiftCarriedForwards = 0; 2556 } 2557 2558 ShiftCarriedForwards += 16; 2559 } 2560 ShiftCarriedForwards -= 16; 2561 2562 // Finish any remaining shifts left by trailing zeros. 2563 if (ShiftCarriedForwards) 2564 TOut.emitDSLL(TmpReg, TmpReg, ShiftCarriedForwards, IDLoc, STI); 2565 2566 if (UseSrcReg) 2567 TOut.emitRRR(AdduOp, DstReg, TmpReg, SrcReg, IDLoc, STI); 2568 2569 return false; 2570 } 2571 2572 bool MipsAsmParser::expandLoadImm(MCInst &Inst, bool Is32BitImm, SMLoc IDLoc, 2573 MCStreamer &Out, const MCSubtargetInfo *STI) { 2574 const MCOperand &ImmOp = Inst.getOperand(1); 2575 assert(ImmOp.isImm() && "expected immediate operand kind"); 2576 const MCOperand &DstRegOp = Inst.getOperand(0); 2577 assert(DstRegOp.isReg() && "expected register operand kind"); 2578 2579 if (loadImmediate(ImmOp.getImm(), DstRegOp.getReg(), Mips::NoRegister, 2580 Is32BitImm, false, IDLoc, Out, STI)) 2581 return true; 2582 2583 return false; 2584 } 2585 2586 bool MipsAsmParser::expandLoadAddress(unsigned DstReg, unsigned BaseReg, 2587 const MCOperand &Offset, 2588 bool Is32BitAddress, SMLoc IDLoc, 2589 MCStreamer &Out, 2590 const MCSubtargetInfo *STI) { 2591 // la can't produce a usable address when addresses are 64-bit. 2592 if (Is32BitAddress && ABI.ArePtrs64bit()) { 2593 // FIXME: Demote this to a warning and continue as if we had 'dla' instead. 2594 // We currently can't do this because we depend on the equality 2595 // operator and N64 can end up with a GPR32/GPR64 mismatch. 2596 Error(IDLoc, "la used to load 64-bit address"); 2597 // Continue as if we had 'dla' instead. 2598 Is32BitAddress = false; 2599 return true; 2600 } 2601 2602 // dla requires 64-bit addresses. 2603 if (!Is32BitAddress && !hasMips3()) { 2604 Error(IDLoc, "instruction requires a 64-bit architecture"); 2605 return true; 2606 } 2607 2608 if (!Offset.isImm()) 2609 return loadAndAddSymbolAddress(Offset.getExpr(), DstReg, BaseReg, 2610 Is32BitAddress, IDLoc, Out, STI); 2611 2612 if (!ABI.ArePtrs64bit()) { 2613 // Continue as if we had 'la' whether we had 'la' or 'dla'. 2614 Is32BitAddress = true; 2615 } 2616 2617 return loadImmediate(Offset.getImm(), DstReg, BaseReg, Is32BitAddress, true, 2618 IDLoc, Out, STI); 2619 } 2620 2621 bool MipsAsmParser::loadAndAddSymbolAddress(const MCExpr *SymExpr, 2622 unsigned DstReg, unsigned SrcReg, 2623 bool Is32BitSym, SMLoc IDLoc, 2624 MCStreamer &Out, 2625 const MCSubtargetInfo *STI) { 2626 MipsTargetStreamer &TOut = getTargetStreamer(); 2627 bool UseSrcReg = SrcReg != Mips::NoRegister; 2628 warnIfNoMacro(IDLoc); 2629 2630 if (inPicMode() && ABI.IsO32()) { 2631 MCValue Res; 2632 if (!SymExpr->evaluateAsRelocatable(Res, nullptr, nullptr)) { 2633 Error(IDLoc, "expected relocatable expression"); 2634 return true; 2635 } 2636 if (Res.getSymB() != nullptr) { 2637 Error(IDLoc, "expected relocatable expression with only one symbol"); 2638 return true; 2639 } 2640 2641 // The case where the result register is $25 is somewhat special. If the 2642 // symbol in the final relocation is external and not modified with a 2643 // constant then we must use R_MIPS_CALL16 instead of R_MIPS_GOT16. 2644 if ((DstReg == Mips::T9 || DstReg == Mips::T9_64) && !UseSrcReg && 2645 Res.getConstant() == 0 && !Res.getSymA()->getSymbol().isInSection() && 2646 !Res.getSymA()->getSymbol().isTemporary()) { 2647 const MCExpr *CallExpr = 2648 MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, SymExpr, getContext()); 2649 TOut.emitRRX(Mips::LW, DstReg, ABI.GetGlobalPtr(), 2650 MCOperand::createExpr(CallExpr), IDLoc, STI); 2651 return false; 2652 } 2653 2654 // The remaining cases are: 2655 // External GOT: lw $tmp, %got(symbol+offset)($gp) 2656 // >addiu $tmp, $tmp, %lo(offset) 2657 // >addiu $rd, $tmp, $rs 2658 // Local GOT: lw $tmp, %got(symbol+offset)($gp) 2659 // addiu $tmp, $tmp, %lo(symbol+offset)($gp) 2660 // >addiu $rd, $tmp, $rs 2661 // The addiu's marked with a '>' may be omitted if they are redundant. If 2662 // this happens then the last instruction must use $rd as the result 2663 // register. 2664 const MipsMCExpr *GotExpr = 2665 MipsMCExpr::create(MipsMCExpr::MEK_GOT, SymExpr, getContext()); 2666 const MCExpr *LoExpr = nullptr; 2667 if (Res.getSymA()->getSymbol().isInSection() || 2668 Res.getSymA()->getSymbol().isTemporary()) 2669 LoExpr = MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext()); 2670 else if (Res.getConstant() != 0) { 2671 // External symbols fully resolve the symbol with just the %got(symbol) 2672 // but we must still account for any offset to the symbol for expressions 2673 // like symbol+8. 2674 LoExpr = MCConstantExpr::create(Res.getConstant(), getContext()); 2675 } 2676 2677 unsigned TmpReg = DstReg; 2678 if (UseSrcReg && 2679 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, 2680 SrcReg)) { 2681 // If $rs is the same as $rd, we need to use AT. 2682 // If it is not available we exit. 2683 unsigned ATReg = getATReg(IDLoc); 2684 if (!ATReg) 2685 return true; 2686 TmpReg = ATReg; 2687 } 2688 2689 TOut.emitRRX(Mips::LW, TmpReg, ABI.GetGlobalPtr(), 2690 MCOperand::createExpr(GotExpr), IDLoc, STI); 2691 2692 if (LoExpr) 2693 TOut.emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), 2694 IDLoc, STI); 2695 2696 if (UseSrcReg) 2697 TOut.emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, STI); 2698 2699 return false; 2700 } 2701 2702 const MipsMCExpr *HiExpr = 2703 MipsMCExpr::create(MipsMCExpr::MEK_HI, SymExpr, getContext()); 2704 const MipsMCExpr *LoExpr = 2705 MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext()); 2706 2707 // This is the 64-bit symbol address expansion. 2708 if (ABI.ArePtrs64bit() && isGP64bit()) { 2709 // We always need AT for the 64-bit expansion. 2710 // If it is not available we exit. 2711 unsigned ATReg = getATReg(IDLoc); 2712 if (!ATReg) 2713 return true; 2714 2715 const MipsMCExpr *HighestExpr = 2716 MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, SymExpr, getContext()); 2717 const MipsMCExpr *HigherExpr = 2718 MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, SymExpr, getContext()); 2719 2720 if (UseSrcReg && 2721 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, 2722 SrcReg)) { 2723 // If $rs is the same as $rd: 2724 // (d)la $rd, sym($rd) => lui $at, %highest(sym) 2725 // daddiu $at, $at, %higher(sym) 2726 // dsll $at, $at, 16 2727 // daddiu $at, $at, %hi(sym) 2728 // dsll $at, $at, 16 2729 // daddiu $at, $at, %lo(sym) 2730 // daddu $rd, $at, $rd 2731 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc, 2732 STI); 2733 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, 2734 MCOperand::createExpr(HigherExpr), IDLoc, STI); 2735 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 2736 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr), 2737 IDLoc, STI); 2738 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 2739 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), 2740 IDLoc, STI); 2741 TOut.emitRRR(Mips::DADDu, DstReg, ATReg, SrcReg, IDLoc, STI); 2742 2743 return false; 2744 } 2745 2746 // Otherwise, if the $rs is different from $rd or if $rs isn't specified: 2747 // (d)la $rd, sym/sym($rs) => lui $rd, %highest(sym) 2748 // lui $at, %hi(sym) 2749 // daddiu $rd, $rd, %higher(sym) 2750 // daddiu $at, $at, %lo(sym) 2751 // dsll32 $rd, $rd, 0 2752 // daddu $rd, $rd, $at 2753 // (daddu $rd, $rd, $rs) 2754 TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc, 2755 STI); 2756 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc, STI); 2757 TOut.emitRRX(Mips::DADDiu, DstReg, DstReg, 2758 MCOperand::createExpr(HigherExpr), IDLoc, STI); 2759 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), 2760 IDLoc, STI); 2761 TOut.emitRRI(Mips::DSLL32, DstReg, DstReg, 0, IDLoc, STI); 2762 TOut.emitRRR(Mips::DADDu, DstReg, DstReg, ATReg, IDLoc, STI); 2763 if (UseSrcReg) 2764 TOut.emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, STI); 2765 2766 return false; 2767 } 2768 2769 // And now, the 32-bit symbol address expansion: 2770 // If $rs is the same as $rd: 2771 // (d)la $rd, sym($rd) => lui $at, %hi(sym) 2772 // ori $at, $at, %lo(sym) 2773 // addu $rd, $at, $rd 2774 // Otherwise, if the $rs is different from $rd or if $rs isn't specified: 2775 // (d)la $rd, sym/sym($rs) => lui $rd, %hi(sym) 2776 // ori $rd, $rd, %lo(sym) 2777 // (addu $rd, $rd, $rs) 2778 unsigned TmpReg = DstReg; 2779 if (UseSrcReg && 2780 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg)) { 2781 // If $rs is the same as $rd, we need to use AT. 2782 // If it is not available we exit. 2783 unsigned ATReg = getATReg(IDLoc); 2784 if (!ATReg) 2785 return true; 2786 TmpReg = ATReg; 2787 } 2788 2789 TOut.emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(HiExpr), IDLoc, STI); 2790 TOut.emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), 2791 IDLoc, STI); 2792 2793 if (UseSrcReg) 2794 TOut.emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, STI); 2795 else 2796 assert( 2797 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, TmpReg)); 2798 2799 return false; 2800 } 2801 2802 bool MipsAsmParser::expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc, 2803 MCStreamer &Out, 2804 const MCSubtargetInfo *STI) { 2805 MipsTargetStreamer &TOut = getTargetStreamer(); 2806 2807 assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 && 2808 "unexpected number of operands"); 2809 2810 MCOperand Offset = Inst.getOperand(0); 2811 if (Offset.isExpr()) { 2812 Inst.clear(); 2813 Inst.setOpcode(Mips::BEQ_MM); 2814 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 2815 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 2816 Inst.addOperand(MCOperand::createExpr(Offset.getExpr())); 2817 } else { 2818 assert(Offset.isImm() && "expected immediate operand kind"); 2819 if (isInt<11>(Offset.getImm())) { 2820 // If offset fits into 11 bits then this instruction becomes microMIPS 2821 // 16-bit unconditional branch instruction. 2822 if (inMicroMipsMode()) 2823 Inst.setOpcode(hasMips32r6() ? Mips::BC16_MMR6 : Mips::B16_MM); 2824 } else { 2825 if (!isInt<17>(Offset.getImm())) 2826 return Error(IDLoc, "branch target out of range"); 2827 if (OffsetToAlignment(Offset.getImm(), 1LL << 1)) 2828 return Error(IDLoc, "branch to misaligned address"); 2829 Inst.clear(); 2830 Inst.setOpcode(Mips::BEQ_MM); 2831 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 2832 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 2833 Inst.addOperand(MCOperand::createImm(Offset.getImm())); 2834 } 2835 } 2836 Out.EmitInstruction(Inst, *STI); 2837 2838 // If .set reorder is active and branch instruction has a delay slot, 2839 // emit a NOP after it. 2840 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode()); 2841 if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) 2842 TOut.emitEmptyDelaySlot(true, IDLoc, STI); 2843 2844 return false; 2845 } 2846 2847 bool MipsAsmParser::expandBranchImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 2848 const MCSubtargetInfo *STI) { 2849 MipsTargetStreamer &TOut = getTargetStreamer(); 2850 const MCOperand &DstRegOp = Inst.getOperand(0); 2851 assert(DstRegOp.isReg() && "expected register operand kind"); 2852 2853 const MCOperand &ImmOp = Inst.getOperand(1); 2854 assert(ImmOp.isImm() && "expected immediate operand kind"); 2855 2856 const MCOperand &MemOffsetOp = Inst.getOperand(2); 2857 assert((MemOffsetOp.isImm() || MemOffsetOp.isExpr()) && 2858 "expected immediate or expression operand"); 2859 2860 bool IsLikely = false; 2861 2862 unsigned OpCode = 0; 2863 switch(Inst.getOpcode()) { 2864 case Mips::BneImm: 2865 OpCode = Mips::BNE; 2866 break; 2867 case Mips::BeqImm: 2868 OpCode = Mips::BEQ; 2869 break; 2870 case Mips::BEQLImmMacro: 2871 OpCode = Mips::BEQL; 2872 IsLikely = true; 2873 break; 2874 case Mips::BNELImmMacro: 2875 OpCode = Mips::BNEL; 2876 IsLikely = true; 2877 break; 2878 default: 2879 llvm_unreachable("Unknown immediate branch pseudo-instruction."); 2880 break; 2881 } 2882 2883 int64_t ImmValue = ImmOp.getImm(); 2884 if (ImmValue == 0) { 2885 if (IsLikely) { 2886 TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, 2887 MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI); 2888 TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 2889 } else 2890 TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, MemOffsetOp, IDLoc, 2891 STI); 2892 } else { 2893 warnIfNoMacro(IDLoc); 2894 2895 unsigned ATReg = getATReg(IDLoc); 2896 if (!ATReg) 2897 return true; 2898 2899 if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, !isGP64bit(), true, 2900 IDLoc, Out, STI)) 2901 return true; 2902 2903 if (IsLikely) { 2904 TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg, 2905 MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI); 2906 TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 2907 } else 2908 TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg, MemOffsetOp, IDLoc, STI); 2909 } 2910 return false; 2911 } 2912 2913 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 2914 const MCSubtargetInfo *STI, bool IsLoad, 2915 bool IsImmOpnd) { 2916 if (IsLoad) { 2917 expandLoadInst(Inst, IDLoc, Out, STI, IsImmOpnd); 2918 return; 2919 } 2920 expandStoreInst(Inst, IDLoc, Out, STI, IsImmOpnd); 2921 } 2922 2923 void MipsAsmParser::expandLoadInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 2924 const MCSubtargetInfo *STI, bool IsImmOpnd) { 2925 MipsTargetStreamer &TOut = getTargetStreamer(); 2926 2927 unsigned DstReg = Inst.getOperand(0).getReg(); 2928 unsigned BaseReg = Inst.getOperand(1).getReg(); 2929 2930 const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode()); 2931 int16_t DstRegClass = Desc.OpInfo[0].RegClass; 2932 unsigned DstRegClassID = 2933 getContext().getRegisterInfo()->getRegClass(DstRegClass).getID(); 2934 bool IsGPR = (DstRegClassID == Mips::GPR32RegClassID) || 2935 (DstRegClassID == Mips::GPR64RegClassID); 2936 2937 if (IsImmOpnd) { 2938 // Try to use DstReg as the temporary. 2939 if (IsGPR && (BaseReg != DstReg)) { 2940 TOut.emitLoadWithImmOffset(Inst.getOpcode(), DstReg, BaseReg, 2941 Inst.getOperand(2).getImm(), DstReg, IDLoc, 2942 STI); 2943 return; 2944 } 2945 2946 // At this point we need AT to perform the expansions and we exit if it is 2947 // not available. 2948 unsigned ATReg = getATReg(IDLoc); 2949 if (!ATReg) 2950 return; 2951 2952 TOut.emitLoadWithImmOffset(Inst.getOpcode(), DstReg, BaseReg, 2953 Inst.getOperand(2).getImm(), ATReg, IDLoc, STI); 2954 return; 2955 } 2956 2957 const MCExpr *ExprOffset = Inst.getOperand(2).getExpr(); 2958 MCOperand LoOperand = MCOperand::createExpr( 2959 MipsMCExpr::create(MipsMCExpr::MEK_LO, ExprOffset, getContext())); 2960 MCOperand HiOperand = MCOperand::createExpr( 2961 MipsMCExpr::create(MipsMCExpr::MEK_HI, ExprOffset, getContext())); 2962 2963 // Try to use DstReg as the temporary. 2964 if (IsGPR && (BaseReg != DstReg)) { 2965 TOut.emitLoadWithSymOffset(Inst.getOpcode(), DstReg, BaseReg, HiOperand, 2966 LoOperand, DstReg, IDLoc, STI); 2967 return; 2968 } 2969 2970 // At this point we need AT to perform the expansions and we exit if it is 2971 // not available. 2972 unsigned ATReg = getATReg(IDLoc); 2973 if (!ATReg) 2974 return; 2975 2976 TOut.emitLoadWithSymOffset(Inst.getOpcode(), DstReg, BaseReg, HiOperand, 2977 LoOperand, ATReg, IDLoc, STI); 2978 } 2979 2980 void MipsAsmParser::expandStoreInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 2981 const MCSubtargetInfo *STI, 2982 bool IsImmOpnd) { 2983 MipsTargetStreamer &TOut = getTargetStreamer(); 2984 2985 unsigned SrcReg = Inst.getOperand(0).getReg(); 2986 unsigned BaseReg = Inst.getOperand(1).getReg(); 2987 2988 if (IsImmOpnd) { 2989 TOut.emitStoreWithImmOffset(Inst.getOpcode(), SrcReg, BaseReg, 2990 Inst.getOperand(2).getImm(), 2991 [&]() { return getATReg(IDLoc); }, IDLoc, STI); 2992 return; 2993 } 2994 2995 unsigned ATReg = getATReg(IDLoc); 2996 if (!ATReg) 2997 return; 2998 2999 const MCExpr *ExprOffset = Inst.getOperand(2).getExpr(); 3000 MCOperand LoOperand = MCOperand::createExpr( 3001 MipsMCExpr::create(MipsMCExpr::MEK_LO, ExprOffset, getContext())); 3002 MCOperand HiOperand = MCOperand::createExpr( 3003 MipsMCExpr::create(MipsMCExpr::MEK_HI, ExprOffset, getContext())); 3004 TOut.emitStoreWithSymOffset(Inst.getOpcode(), SrcReg, BaseReg, HiOperand, 3005 LoOperand, ATReg, IDLoc, STI); 3006 } 3007 3008 bool MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc, 3009 MCStreamer &Out, 3010 const MCSubtargetInfo *STI) { 3011 unsigned OpNum = Inst.getNumOperands(); 3012 unsigned Opcode = Inst.getOpcode(); 3013 unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM; 3014 3015 assert(Inst.getOperand(OpNum - 1).isImm() && 3016 Inst.getOperand(OpNum - 2).isReg() && 3017 Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand."); 3018 3019 if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 && 3020 Inst.getOperand(OpNum - 1).getImm() >= 0 && 3021 (Inst.getOperand(OpNum - 2).getReg() == Mips::SP || 3022 Inst.getOperand(OpNum - 2).getReg() == Mips::SP_64) && 3023 (Inst.getOperand(OpNum - 3).getReg() == Mips::RA || 3024 Inst.getOperand(OpNum - 3).getReg() == Mips::RA_64)) { 3025 // It can be implemented as SWM16 or LWM16 instruction. 3026 if (inMicroMipsMode() && hasMips32r6()) 3027 NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MMR6 : Mips::LWM16_MMR6; 3028 else 3029 NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM; 3030 } 3031 3032 Inst.setOpcode(NewOpcode); 3033 Out.EmitInstruction(Inst, *STI); 3034 return false; 3035 } 3036 3037 bool MipsAsmParser::expandCondBranches(MCInst &Inst, SMLoc IDLoc, 3038 MCStreamer &Out, 3039 const MCSubtargetInfo *STI) { 3040 MipsTargetStreamer &TOut = getTargetStreamer(); 3041 bool EmittedNoMacroWarning = false; 3042 unsigned PseudoOpcode = Inst.getOpcode(); 3043 unsigned SrcReg = Inst.getOperand(0).getReg(); 3044 const MCOperand &TrgOp = Inst.getOperand(1); 3045 const MCExpr *OffsetExpr = Inst.getOperand(2).getExpr(); 3046 3047 unsigned ZeroSrcOpcode, ZeroTrgOpcode; 3048 bool ReverseOrderSLT, IsUnsigned, IsLikely, AcceptsEquality; 3049 3050 unsigned TrgReg; 3051 if (TrgOp.isReg()) 3052 TrgReg = TrgOp.getReg(); 3053 else if (TrgOp.isImm()) { 3054 warnIfNoMacro(IDLoc); 3055 EmittedNoMacroWarning = true; 3056 3057 TrgReg = getATReg(IDLoc); 3058 if (!TrgReg) 3059 return true; 3060 3061 switch(PseudoOpcode) { 3062 default: 3063 llvm_unreachable("unknown opcode for branch pseudo-instruction"); 3064 case Mips::BLTImmMacro: 3065 PseudoOpcode = Mips::BLT; 3066 break; 3067 case Mips::BLEImmMacro: 3068 PseudoOpcode = Mips::BLE; 3069 break; 3070 case Mips::BGEImmMacro: 3071 PseudoOpcode = Mips::BGE; 3072 break; 3073 case Mips::BGTImmMacro: 3074 PseudoOpcode = Mips::BGT; 3075 break; 3076 case Mips::BLTUImmMacro: 3077 PseudoOpcode = Mips::BLTU; 3078 break; 3079 case Mips::BLEUImmMacro: 3080 PseudoOpcode = Mips::BLEU; 3081 break; 3082 case Mips::BGEUImmMacro: 3083 PseudoOpcode = Mips::BGEU; 3084 break; 3085 case Mips::BGTUImmMacro: 3086 PseudoOpcode = Mips::BGTU; 3087 break; 3088 case Mips::BLTLImmMacro: 3089 PseudoOpcode = Mips::BLTL; 3090 break; 3091 case Mips::BLELImmMacro: 3092 PseudoOpcode = Mips::BLEL; 3093 break; 3094 case Mips::BGELImmMacro: 3095 PseudoOpcode = Mips::BGEL; 3096 break; 3097 case Mips::BGTLImmMacro: 3098 PseudoOpcode = Mips::BGTL; 3099 break; 3100 case Mips::BLTULImmMacro: 3101 PseudoOpcode = Mips::BLTUL; 3102 break; 3103 case Mips::BLEULImmMacro: 3104 PseudoOpcode = Mips::BLEUL; 3105 break; 3106 case Mips::BGEULImmMacro: 3107 PseudoOpcode = Mips::BGEUL; 3108 break; 3109 case Mips::BGTULImmMacro: 3110 PseudoOpcode = Mips::BGTUL; 3111 break; 3112 } 3113 3114 if (loadImmediate(TrgOp.getImm(), TrgReg, Mips::NoRegister, !isGP64bit(), 3115 false, IDLoc, Out, STI)) 3116 return true; 3117 } 3118 3119 switch (PseudoOpcode) { 3120 case Mips::BLT: 3121 case Mips::BLTU: 3122 case Mips::BLTL: 3123 case Mips::BLTUL: 3124 AcceptsEquality = false; 3125 ReverseOrderSLT = false; 3126 IsUnsigned = ((PseudoOpcode == Mips::BLTU) || (PseudoOpcode == Mips::BLTUL)); 3127 IsLikely = ((PseudoOpcode == Mips::BLTL) || (PseudoOpcode == Mips::BLTUL)); 3128 ZeroSrcOpcode = Mips::BGTZ; 3129 ZeroTrgOpcode = Mips::BLTZ; 3130 break; 3131 case Mips::BLE: 3132 case Mips::BLEU: 3133 case Mips::BLEL: 3134 case Mips::BLEUL: 3135 AcceptsEquality = true; 3136 ReverseOrderSLT = true; 3137 IsUnsigned = ((PseudoOpcode == Mips::BLEU) || (PseudoOpcode == Mips::BLEUL)); 3138 IsLikely = ((PseudoOpcode == Mips::BLEL) || (PseudoOpcode == Mips::BLEUL)); 3139 ZeroSrcOpcode = Mips::BGEZ; 3140 ZeroTrgOpcode = Mips::BLEZ; 3141 break; 3142 case Mips::BGE: 3143 case Mips::BGEU: 3144 case Mips::BGEL: 3145 case Mips::BGEUL: 3146 AcceptsEquality = true; 3147 ReverseOrderSLT = false; 3148 IsUnsigned = ((PseudoOpcode == Mips::BGEU) || (PseudoOpcode == Mips::BGEUL)); 3149 IsLikely = ((PseudoOpcode == Mips::BGEL) || (PseudoOpcode == Mips::BGEUL)); 3150 ZeroSrcOpcode = Mips::BLEZ; 3151 ZeroTrgOpcode = Mips::BGEZ; 3152 break; 3153 case Mips::BGT: 3154 case Mips::BGTU: 3155 case Mips::BGTL: 3156 case Mips::BGTUL: 3157 AcceptsEquality = false; 3158 ReverseOrderSLT = true; 3159 IsUnsigned = ((PseudoOpcode == Mips::BGTU) || (PseudoOpcode == Mips::BGTUL)); 3160 IsLikely = ((PseudoOpcode == Mips::BGTL) || (PseudoOpcode == Mips::BGTUL)); 3161 ZeroSrcOpcode = Mips::BLTZ; 3162 ZeroTrgOpcode = Mips::BGTZ; 3163 break; 3164 default: 3165 llvm_unreachable("unknown opcode for branch pseudo-instruction"); 3166 } 3167 3168 bool IsTrgRegZero = (TrgReg == Mips::ZERO); 3169 bool IsSrcRegZero = (SrcReg == Mips::ZERO); 3170 if (IsSrcRegZero && IsTrgRegZero) { 3171 // FIXME: All of these Opcode-specific if's are needed for compatibility 3172 // with GAS' behaviour. However, they may not generate the most efficient 3173 // code in some circumstances. 3174 if (PseudoOpcode == Mips::BLT) { 3175 TOut.emitRX(Mips::BLTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3176 IDLoc, STI); 3177 return false; 3178 } 3179 if (PseudoOpcode == Mips::BLE) { 3180 TOut.emitRX(Mips::BLEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3181 IDLoc, STI); 3182 Warning(IDLoc, "branch is always taken"); 3183 return false; 3184 } 3185 if (PseudoOpcode == Mips::BGE) { 3186 TOut.emitRX(Mips::BGEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3187 IDLoc, STI); 3188 Warning(IDLoc, "branch is always taken"); 3189 return false; 3190 } 3191 if (PseudoOpcode == Mips::BGT) { 3192 TOut.emitRX(Mips::BGTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3193 IDLoc, STI); 3194 return false; 3195 } 3196 if (PseudoOpcode == Mips::BGTU) { 3197 TOut.emitRRX(Mips::BNE, Mips::ZERO, Mips::ZERO, 3198 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3199 return false; 3200 } 3201 if (AcceptsEquality) { 3202 // If both registers are $0 and the pseudo-branch accepts equality, it 3203 // will always be taken, so we emit an unconditional branch. 3204 TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO, 3205 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3206 Warning(IDLoc, "branch is always taken"); 3207 return false; 3208 } 3209 // If both registers are $0 and the pseudo-branch does not accept 3210 // equality, it will never be taken, so we don't have to emit anything. 3211 return false; 3212 } 3213 if (IsSrcRegZero || IsTrgRegZero) { 3214 if ((IsSrcRegZero && PseudoOpcode == Mips::BGTU) || 3215 (IsTrgRegZero && PseudoOpcode == Mips::BLTU)) { 3216 // If the $rs is $0 and the pseudo-branch is BGTU (0 > x) or 3217 // if the $rt is $0 and the pseudo-branch is BLTU (x < 0), 3218 // the pseudo-branch will never be taken, so we don't emit anything. 3219 // This only applies to unsigned pseudo-branches. 3220 return false; 3221 } 3222 if ((IsSrcRegZero && PseudoOpcode == Mips::BLEU) || 3223 (IsTrgRegZero && PseudoOpcode == Mips::BGEU)) { 3224 // If the $rs is $0 and the pseudo-branch is BLEU (0 <= x) or 3225 // if the $rt is $0 and the pseudo-branch is BGEU (x >= 0), 3226 // the pseudo-branch will always be taken, so we emit an unconditional 3227 // branch. 3228 // This only applies to unsigned pseudo-branches. 3229 TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO, 3230 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3231 Warning(IDLoc, "branch is always taken"); 3232 return false; 3233 } 3234 if (IsUnsigned) { 3235 // If the $rs is $0 and the pseudo-branch is BLTU (0 < x) or 3236 // if the $rt is $0 and the pseudo-branch is BGTU (x > 0), 3237 // the pseudo-branch will be taken only when the non-zero register is 3238 // different from 0, so we emit a BNEZ. 3239 // 3240 // If the $rs is $0 and the pseudo-branch is BGEU (0 >= x) or 3241 // if the $rt is $0 and the pseudo-branch is BLEU (x <= 0), 3242 // the pseudo-branch will be taken only when the non-zero register is 3243 // equal to 0, so we emit a BEQZ. 3244 // 3245 // Because only BLEU and BGEU branch on equality, we can use the 3246 // AcceptsEquality variable to decide when to emit the BEQZ. 3247 TOut.emitRRX(AcceptsEquality ? Mips::BEQ : Mips::BNE, 3248 IsSrcRegZero ? TrgReg : SrcReg, Mips::ZERO, 3249 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3250 return false; 3251 } 3252 // If we have a signed pseudo-branch and one of the registers is $0, 3253 // we can use an appropriate compare-to-zero branch. We select which one 3254 // to use in the switch statement above. 3255 TOut.emitRX(IsSrcRegZero ? ZeroSrcOpcode : ZeroTrgOpcode, 3256 IsSrcRegZero ? TrgReg : SrcReg, 3257 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3258 return false; 3259 } 3260 3261 // If neither the SrcReg nor the TrgReg are $0, we need AT to perform the 3262 // expansions. If it is not available, we return. 3263 unsigned ATRegNum = getATReg(IDLoc); 3264 if (!ATRegNum) 3265 return true; 3266 3267 if (!EmittedNoMacroWarning) 3268 warnIfNoMacro(IDLoc); 3269 3270 // SLT fits well with 2 of our 4 pseudo-branches: 3271 // BLT, where $rs < $rt, translates into "slt $at, $rs, $rt" and 3272 // BGT, where $rs > $rt, translates into "slt $at, $rt, $rs". 3273 // If the result of the SLT is 1, we branch, and if it's 0, we don't. 3274 // This is accomplished by using a BNEZ with the result of the SLT. 3275 // 3276 // The other 2 pseudo-branches are opposites of the above 2 (BGE with BLT 3277 // and BLE with BGT), so we change the BNEZ into a a BEQZ. 3278 // Because only BGE and BLE branch on equality, we can use the 3279 // AcceptsEquality variable to decide when to emit the BEQZ. 3280 // Note that the order of the SLT arguments doesn't change between 3281 // opposites. 3282 // 3283 // The same applies to the unsigned variants, except that SLTu is used 3284 // instead of SLT. 3285 TOut.emitRRR(IsUnsigned ? Mips::SLTu : Mips::SLT, ATRegNum, 3286 ReverseOrderSLT ? TrgReg : SrcReg, 3287 ReverseOrderSLT ? SrcReg : TrgReg, IDLoc, STI); 3288 3289 TOut.emitRRX(IsLikely ? (AcceptsEquality ? Mips::BEQL : Mips::BNEL) 3290 : (AcceptsEquality ? Mips::BEQ : Mips::BNE), 3291 ATRegNum, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc, 3292 STI); 3293 return false; 3294 } 3295 3296 bool MipsAsmParser::expandDiv(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3297 const MCSubtargetInfo *STI, const bool IsMips64, 3298 const bool Signed) { 3299 MipsTargetStreamer &TOut = getTargetStreamer(); 3300 3301 warnIfNoMacro(IDLoc); 3302 3303 const MCOperand &RdRegOp = Inst.getOperand(0); 3304 assert(RdRegOp.isReg() && "expected register operand kind"); 3305 unsigned RdReg = RdRegOp.getReg(); 3306 3307 const MCOperand &RsRegOp = Inst.getOperand(1); 3308 assert(RsRegOp.isReg() && "expected register operand kind"); 3309 unsigned RsReg = RsRegOp.getReg(); 3310 3311 unsigned RtReg; 3312 int64_t ImmValue; 3313 3314 const MCOperand &RtOp = Inst.getOperand(2); 3315 assert((RtOp.isReg() || RtOp.isImm()) && 3316 "expected register or immediate operand kind"); 3317 if (RtOp.isReg()) 3318 RtReg = RtOp.getReg(); 3319 else 3320 ImmValue = RtOp.getImm(); 3321 3322 unsigned DivOp; 3323 unsigned ZeroReg; 3324 unsigned SubOp; 3325 3326 if (IsMips64) { 3327 DivOp = Signed ? Mips::DSDIV : Mips::DUDIV; 3328 ZeroReg = Mips::ZERO_64; 3329 SubOp = Mips::DSUB; 3330 } else { 3331 DivOp = Signed ? Mips::SDIV : Mips::UDIV; 3332 ZeroReg = Mips::ZERO; 3333 SubOp = Mips::SUB; 3334 } 3335 3336 bool UseTraps = useTraps(); 3337 3338 if (RtOp.isImm()) { 3339 unsigned ATReg = getATReg(IDLoc); 3340 if (!ATReg) 3341 return true; 3342 3343 if (ImmValue == 0) { 3344 if (RsReg == Mips::ZERO || RsReg == Mips::ZERO_64) 3345 Warning(IDLoc, "dividing zero by zero"); 3346 else 3347 Warning(IDLoc, "division by zero"); 3348 if (UseTraps) 3349 TOut.emitRRI(Mips::TEQ, ZeroReg, ZeroReg, 0x7, IDLoc, STI); 3350 else 3351 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 3352 return false; 3353 } 3354 3355 if (ImmValue == 1) { 3356 TOut.emitRRR(Mips::ADDu, RdReg, RsReg, Mips::ZERO, IDLoc, STI); 3357 return false; 3358 } else if (Signed && ImmValue == -1) { 3359 TOut.emitRRR(SubOp, RdReg, Mips::ZERO, RsReg, IDLoc, STI); 3360 return false; 3361 } else { 3362 if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, isInt<32>(ImmValue), 3363 false, Inst.getLoc(), Out, STI)) 3364 return true; 3365 TOut.emitRR(DivOp, RsReg, ATReg, IDLoc, STI); 3366 TOut.emitR(Mips::MFLO, RdReg, IDLoc, STI); 3367 return false; 3368 } 3369 return true; 3370 } 3371 3372 if (RsReg == Mips::ZERO || RsReg == Mips::ZERO_64) { 3373 if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) 3374 Warning(IDLoc, "dividing zero by zero"); 3375 if (IsMips64) { 3376 if (Signed && (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64)) { 3377 if (UseTraps) { 3378 TOut.emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, STI); 3379 return false; 3380 } 3381 3382 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 3383 return false; 3384 } 3385 } else { 3386 TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI); 3387 return false; 3388 } 3389 } 3390 3391 if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) { 3392 Warning(IDLoc, "division by zero"); 3393 if (Signed) { 3394 if (UseTraps) { 3395 TOut.emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, STI); 3396 return false; 3397 } 3398 3399 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 3400 return false; 3401 } 3402 } 3403 3404 // FIXME: The values for these two BranchTarget variables may be different in 3405 // micromips. These magic numbers need to be removed. 3406 unsigned BranchTargetNoTraps; 3407 unsigned BranchTarget; 3408 3409 if (UseTraps) { 3410 BranchTarget = IsMips64 ? 12 : 8; 3411 TOut.emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, STI); 3412 } else { 3413 BranchTarget = IsMips64 ? 20 : 16; 3414 BranchTargetNoTraps = 8; 3415 // Branch to the li instruction. 3416 TOut.emitRRI(Mips::BNE, RtReg, ZeroReg, BranchTargetNoTraps, IDLoc, STI); 3417 } 3418 3419 TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI); 3420 3421 if (!UseTraps) 3422 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 3423 3424 if (!Signed) { 3425 TOut.emitR(Mips::MFLO, RdReg, IDLoc, STI); 3426 return false; 3427 } 3428 3429 unsigned ATReg = getATReg(IDLoc); 3430 if (!ATReg) 3431 return true; 3432 3433 TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, -1, IDLoc, STI); 3434 if (IsMips64) { 3435 // Branch to the mflo instruction. 3436 TOut.emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, STI); 3437 TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, 1, IDLoc, STI); 3438 TOut.emitRRI(Mips::DSLL32, ATReg, ATReg, 0x1f, IDLoc, STI); 3439 } else { 3440 // Branch to the mflo instruction. 3441 TOut.emitRRI(Mips::BNE, RtReg, ATReg, BranchTarget, IDLoc, STI); 3442 TOut.emitRI(Mips::LUi, ATReg, (uint16_t)0x8000, IDLoc, STI); 3443 } 3444 3445 if (UseTraps) 3446 TOut.emitRRI(Mips::TEQ, RsReg, ATReg, 0x6, IDLoc, STI); 3447 else { 3448 // Branch to the mflo instruction. 3449 TOut.emitRRI(Mips::BNE, RsReg, ATReg, BranchTargetNoTraps, IDLoc, STI); 3450 TOut.emitRRI(Mips::SLL, ZeroReg, ZeroReg, 0, IDLoc, STI); 3451 TOut.emitII(Mips::BREAK, 0x6, 0, IDLoc, STI); 3452 } 3453 TOut.emitR(Mips::MFLO, RdReg, IDLoc, STI); 3454 return false; 3455 } 3456 3457 bool MipsAsmParser::expandTrunc(MCInst &Inst, bool IsDouble, bool Is64FPU, 3458 SMLoc IDLoc, MCStreamer &Out, 3459 const MCSubtargetInfo *STI) { 3460 MipsTargetStreamer &TOut = getTargetStreamer(); 3461 3462 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 3463 assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg() && 3464 Inst.getOperand(2).isReg() && "Invalid instruction operand."); 3465 3466 unsigned FirstReg = Inst.getOperand(0).getReg(); 3467 unsigned SecondReg = Inst.getOperand(1).getReg(); 3468 unsigned ThirdReg = Inst.getOperand(2).getReg(); 3469 3470 if (hasMips1() && !hasMips2()) { 3471 unsigned ATReg = getATReg(IDLoc); 3472 if (!ATReg) 3473 return true; 3474 TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI); 3475 TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI); 3476 TOut.emitNop(IDLoc, STI); 3477 TOut.emitRRI(Mips::ORi, ATReg, ThirdReg, 0x3, IDLoc, STI); 3478 TOut.emitRRI(Mips::XORi, ATReg, ATReg, 0x2, IDLoc, STI); 3479 TOut.emitRR(Mips::CTC1, Mips::RA, ATReg, IDLoc, STI); 3480 TOut.emitNop(IDLoc, STI); 3481 TOut.emitRR(IsDouble ? (Is64FPU ? Mips::CVT_W_D64 : Mips::CVT_W_D32) 3482 : Mips::CVT_W_S, 3483 FirstReg, SecondReg, IDLoc, STI); 3484 TOut.emitRR(Mips::CTC1, Mips::RA, ThirdReg, IDLoc, STI); 3485 TOut.emitNop(IDLoc, STI); 3486 return false; 3487 } 3488 3489 TOut.emitRR(IsDouble ? (Is64FPU ? Mips::TRUNC_W_D64 : Mips::TRUNC_W_D32) 3490 : Mips::TRUNC_W_S, 3491 FirstReg, SecondReg, IDLoc, STI); 3492 3493 return false; 3494 } 3495 3496 bool MipsAsmParser::expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc, 3497 MCStreamer &Out, const MCSubtargetInfo *STI) { 3498 if (hasMips32r6() || hasMips64r6()) { 3499 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 3500 } 3501 3502 const MCOperand &DstRegOp = Inst.getOperand(0); 3503 assert(DstRegOp.isReg() && "expected register operand kind"); 3504 const MCOperand &SrcRegOp = Inst.getOperand(1); 3505 assert(SrcRegOp.isReg() && "expected register operand kind"); 3506 const MCOperand &OffsetImmOp = Inst.getOperand(2); 3507 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 3508 3509 MipsTargetStreamer &TOut = getTargetStreamer(); 3510 unsigned DstReg = DstRegOp.getReg(); 3511 unsigned SrcReg = SrcRegOp.getReg(); 3512 int64_t OffsetValue = OffsetImmOp.getImm(); 3513 3514 // NOTE: We always need AT for ULHU, as it is always used as the source 3515 // register for one of the LBu's. 3516 warnIfNoMacro(IDLoc); 3517 unsigned ATReg = getATReg(IDLoc); 3518 if (!ATReg) 3519 return true; 3520 3521 bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue)); 3522 if (IsLargeOffset) { 3523 if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true, 3524 IDLoc, Out, STI)) 3525 return true; 3526 } 3527 3528 int64_t FirstOffset = IsLargeOffset ? 0 : OffsetValue; 3529 int64_t SecondOffset = IsLargeOffset ? 1 : (OffsetValue + 1); 3530 if (isLittle()) 3531 std::swap(FirstOffset, SecondOffset); 3532 3533 unsigned FirstLbuDstReg = IsLargeOffset ? DstReg : ATReg; 3534 unsigned SecondLbuDstReg = IsLargeOffset ? ATReg : DstReg; 3535 3536 unsigned LbuSrcReg = IsLargeOffset ? ATReg : SrcReg; 3537 unsigned SllReg = IsLargeOffset ? DstReg : ATReg; 3538 3539 TOut.emitRRI(Signed ? Mips::LB : Mips::LBu, FirstLbuDstReg, LbuSrcReg, 3540 FirstOffset, IDLoc, STI); 3541 TOut.emitRRI(Mips::LBu, SecondLbuDstReg, LbuSrcReg, SecondOffset, IDLoc, STI); 3542 TOut.emitRRI(Mips::SLL, SllReg, SllReg, 8, IDLoc, STI); 3543 TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI); 3544 3545 return false; 3546 } 3547 3548 bool MipsAsmParser::expandUsh(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3549 const MCSubtargetInfo *STI) { 3550 if (hasMips32r6() || hasMips64r6()) { 3551 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 3552 } 3553 3554 const MCOperand &DstRegOp = Inst.getOperand(0); 3555 assert(DstRegOp.isReg() && "expected register operand kind"); 3556 const MCOperand &SrcRegOp = Inst.getOperand(1); 3557 assert(SrcRegOp.isReg() && "expected register operand kind"); 3558 const MCOperand &OffsetImmOp = Inst.getOperand(2); 3559 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 3560 3561 MipsTargetStreamer &TOut = getTargetStreamer(); 3562 unsigned DstReg = DstRegOp.getReg(); 3563 unsigned SrcReg = SrcRegOp.getReg(); 3564 int64_t OffsetValue = OffsetImmOp.getImm(); 3565 3566 warnIfNoMacro(IDLoc); 3567 unsigned ATReg = getATReg(IDLoc); 3568 if (!ATReg) 3569 return true; 3570 3571 bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue)); 3572 if (IsLargeOffset) { 3573 if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true, 3574 IDLoc, Out, STI)) 3575 return true; 3576 } 3577 3578 int64_t FirstOffset = IsLargeOffset ? 1 : (OffsetValue + 1); 3579 int64_t SecondOffset = IsLargeOffset ? 0 : OffsetValue; 3580 if (isLittle()) 3581 std::swap(FirstOffset, SecondOffset); 3582 3583 if (IsLargeOffset) { 3584 TOut.emitRRI(Mips::SB, DstReg, ATReg, FirstOffset, IDLoc, STI); 3585 TOut.emitRRI(Mips::SRL, DstReg, DstReg, 8, IDLoc, STI); 3586 TOut.emitRRI(Mips::SB, DstReg, ATReg, SecondOffset, IDLoc, STI); 3587 TOut.emitRRI(Mips::LBu, ATReg, ATReg, 0, IDLoc, STI); 3588 TOut.emitRRI(Mips::SLL, DstReg, DstReg, 8, IDLoc, STI); 3589 TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI); 3590 } else { 3591 TOut.emitRRI(Mips::SB, DstReg, SrcReg, FirstOffset, IDLoc, STI); 3592 TOut.emitRRI(Mips::SRL, ATReg, DstReg, 8, IDLoc, STI); 3593 TOut.emitRRI(Mips::SB, ATReg, SrcReg, SecondOffset, IDLoc, STI); 3594 } 3595 3596 return false; 3597 } 3598 3599 bool MipsAsmParser::expandUxw(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3600 const MCSubtargetInfo *STI) { 3601 if (hasMips32r6() || hasMips64r6()) { 3602 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 3603 } 3604 3605 const MCOperand &DstRegOp = Inst.getOperand(0); 3606 assert(DstRegOp.isReg() && "expected register operand kind"); 3607 const MCOperand &SrcRegOp = Inst.getOperand(1); 3608 assert(SrcRegOp.isReg() && "expected register operand kind"); 3609 const MCOperand &OffsetImmOp = Inst.getOperand(2); 3610 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 3611 3612 MipsTargetStreamer &TOut = getTargetStreamer(); 3613 unsigned DstReg = DstRegOp.getReg(); 3614 unsigned SrcReg = SrcRegOp.getReg(); 3615 int64_t OffsetValue = OffsetImmOp.getImm(); 3616 3617 // Compute left/right load/store offsets. 3618 bool IsLargeOffset = !(isInt<16>(OffsetValue + 3) && isInt<16>(OffsetValue)); 3619 int64_t LxlOffset = IsLargeOffset ? 0 : OffsetValue; 3620 int64_t LxrOffset = IsLargeOffset ? 3 : (OffsetValue + 3); 3621 if (isLittle()) 3622 std::swap(LxlOffset, LxrOffset); 3623 3624 bool IsLoadInst = (Inst.getOpcode() == Mips::Ulw); 3625 bool DoMove = IsLoadInst && (SrcReg == DstReg) && !IsLargeOffset; 3626 unsigned TmpReg = SrcReg; 3627 if (IsLargeOffset || DoMove) { 3628 warnIfNoMacro(IDLoc); 3629 TmpReg = getATReg(IDLoc); 3630 if (!TmpReg) 3631 return true; 3632 } 3633 3634 if (IsLargeOffset) { 3635 if (loadImmediate(OffsetValue, TmpReg, SrcReg, !ABI.ArePtrs64bit(), true, 3636 IDLoc, Out, STI)) 3637 return true; 3638 } 3639 3640 if (DoMove) 3641 std::swap(DstReg, TmpReg); 3642 3643 unsigned XWL = IsLoadInst ? Mips::LWL : Mips::SWL; 3644 unsigned XWR = IsLoadInst ? Mips::LWR : Mips::SWR; 3645 TOut.emitRRI(XWL, DstReg, TmpReg, LxlOffset, IDLoc, STI); 3646 TOut.emitRRI(XWR, DstReg, TmpReg, LxrOffset, IDLoc, STI); 3647 3648 if (DoMove) 3649 TOut.emitRRR(Mips::OR, TmpReg, DstReg, Mips::ZERO, IDLoc, STI); 3650 3651 return false; 3652 } 3653 3654 bool MipsAsmParser::expandAliasImmediate(MCInst &Inst, SMLoc IDLoc, 3655 MCStreamer &Out, 3656 const MCSubtargetInfo *STI) { 3657 MipsTargetStreamer &TOut = getTargetStreamer(); 3658 3659 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 3660 assert(Inst.getOperand(0).isReg() && 3661 Inst.getOperand(1).isReg() && 3662 Inst.getOperand(2).isImm() && "Invalid instruction operand."); 3663 3664 unsigned ATReg = Mips::NoRegister; 3665 unsigned FinalDstReg = Mips::NoRegister; 3666 unsigned DstReg = Inst.getOperand(0).getReg(); 3667 unsigned SrcReg = Inst.getOperand(1).getReg(); 3668 int64_t ImmValue = Inst.getOperand(2).getImm(); 3669 3670 bool Is32Bit = isInt<32>(ImmValue) || isUInt<32>(ImmValue); 3671 3672 unsigned FinalOpcode = Inst.getOpcode(); 3673 3674 if (DstReg == SrcReg) { 3675 ATReg = getATReg(Inst.getLoc()); 3676 if (!ATReg) 3677 return true; 3678 FinalDstReg = DstReg; 3679 DstReg = ATReg; 3680 } 3681 3682 if (!loadImmediate(ImmValue, DstReg, Mips::NoRegister, Is32Bit, false, Inst.getLoc(), Out, STI)) { 3683 switch (FinalOpcode) { 3684 default: 3685 llvm_unreachable("unimplemented expansion"); 3686 case (Mips::ADDi): 3687 FinalOpcode = Mips::ADD; 3688 break; 3689 case (Mips::ADDiu): 3690 FinalOpcode = Mips::ADDu; 3691 break; 3692 case (Mips::ANDi): 3693 FinalOpcode = Mips::AND; 3694 break; 3695 case (Mips::NORImm): 3696 FinalOpcode = Mips::NOR; 3697 break; 3698 case (Mips::ORi): 3699 FinalOpcode = Mips::OR; 3700 break; 3701 case (Mips::SLTi): 3702 FinalOpcode = Mips::SLT; 3703 break; 3704 case (Mips::SLTiu): 3705 FinalOpcode = Mips::SLTu; 3706 break; 3707 case (Mips::XORi): 3708 FinalOpcode = Mips::XOR; 3709 break; 3710 } 3711 3712 if (FinalDstReg == Mips::NoRegister) 3713 TOut.emitRRR(FinalOpcode, DstReg, DstReg, SrcReg, IDLoc, STI); 3714 else 3715 TOut.emitRRR(FinalOpcode, FinalDstReg, FinalDstReg, DstReg, IDLoc, STI); 3716 return false; 3717 } 3718 return true; 3719 } 3720 3721 bool MipsAsmParser::expandRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3722 const MCSubtargetInfo *STI) { 3723 MipsTargetStreamer &TOut = getTargetStreamer(); 3724 unsigned ATReg = Mips::NoRegister; 3725 unsigned DReg = Inst.getOperand(0).getReg(); 3726 unsigned SReg = Inst.getOperand(1).getReg(); 3727 unsigned TReg = Inst.getOperand(2).getReg(); 3728 unsigned TmpReg = DReg; 3729 3730 unsigned FirstShift = Mips::NOP; 3731 unsigned SecondShift = Mips::NOP; 3732 3733 if (hasMips32r2()) { 3734 if (DReg == SReg) { 3735 TmpReg = getATReg(Inst.getLoc()); 3736 if (!TmpReg) 3737 return true; 3738 } 3739 3740 if (Inst.getOpcode() == Mips::ROL) { 3741 TOut.emitRRR(Mips::SUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 3742 TOut.emitRRR(Mips::ROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI); 3743 return false; 3744 } 3745 3746 if (Inst.getOpcode() == Mips::ROR) { 3747 TOut.emitRRR(Mips::ROTRV, DReg, SReg, TReg, Inst.getLoc(), STI); 3748 return false; 3749 } 3750 3751 return true; 3752 } 3753 3754 if (hasMips32()) { 3755 switch (Inst.getOpcode()) { 3756 default: 3757 llvm_unreachable("unexpected instruction opcode"); 3758 case Mips::ROL: 3759 FirstShift = Mips::SRLV; 3760 SecondShift = Mips::SLLV; 3761 break; 3762 case Mips::ROR: 3763 FirstShift = Mips::SLLV; 3764 SecondShift = Mips::SRLV; 3765 break; 3766 } 3767 3768 ATReg = getATReg(Inst.getLoc()); 3769 if (!ATReg) 3770 return true; 3771 3772 TOut.emitRRR(Mips::SUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 3773 TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI); 3774 TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI); 3775 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 3776 3777 return false; 3778 } 3779 3780 return true; 3781 } 3782 3783 bool MipsAsmParser::expandRotationImm(MCInst &Inst, SMLoc IDLoc, 3784 MCStreamer &Out, 3785 const MCSubtargetInfo *STI) { 3786 MipsTargetStreamer &TOut = getTargetStreamer(); 3787 unsigned ATReg = Mips::NoRegister; 3788 unsigned DReg = Inst.getOperand(0).getReg(); 3789 unsigned SReg = Inst.getOperand(1).getReg(); 3790 int64_t ImmValue = Inst.getOperand(2).getImm(); 3791 3792 unsigned FirstShift = Mips::NOP; 3793 unsigned SecondShift = Mips::NOP; 3794 3795 if (hasMips32r2()) { 3796 if (Inst.getOpcode() == Mips::ROLImm) { 3797 uint64_t MaxShift = 32; 3798 uint64_t ShiftValue = ImmValue; 3799 if (ImmValue != 0) 3800 ShiftValue = MaxShift - ImmValue; 3801 TOut.emitRRI(Mips::ROTR, DReg, SReg, ShiftValue, Inst.getLoc(), STI); 3802 return false; 3803 } 3804 3805 if (Inst.getOpcode() == Mips::RORImm) { 3806 TOut.emitRRI(Mips::ROTR, DReg, SReg, ImmValue, Inst.getLoc(), STI); 3807 return false; 3808 } 3809 3810 return true; 3811 } 3812 3813 if (hasMips32()) { 3814 if (ImmValue == 0) { 3815 TOut.emitRRI(Mips::SRL, DReg, SReg, 0, Inst.getLoc(), STI); 3816 return false; 3817 } 3818 3819 switch (Inst.getOpcode()) { 3820 default: 3821 llvm_unreachable("unexpected instruction opcode"); 3822 case Mips::ROLImm: 3823 FirstShift = Mips::SLL; 3824 SecondShift = Mips::SRL; 3825 break; 3826 case Mips::RORImm: 3827 FirstShift = Mips::SRL; 3828 SecondShift = Mips::SLL; 3829 break; 3830 } 3831 3832 ATReg = getATReg(Inst.getLoc()); 3833 if (!ATReg) 3834 return true; 3835 3836 TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue, Inst.getLoc(), STI); 3837 TOut.emitRRI(SecondShift, DReg, SReg, 32 - ImmValue, Inst.getLoc(), STI); 3838 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 3839 3840 return false; 3841 } 3842 3843 return true; 3844 } 3845 3846 bool MipsAsmParser::expandDRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3847 const MCSubtargetInfo *STI) { 3848 MipsTargetStreamer &TOut = getTargetStreamer(); 3849 unsigned ATReg = Mips::NoRegister; 3850 unsigned DReg = Inst.getOperand(0).getReg(); 3851 unsigned SReg = Inst.getOperand(1).getReg(); 3852 unsigned TReg = Inst.getOperand(2).getReg(); 3853 unsigned TmpReg = DReg; 3854 3855 unsigned FirstShift = Mips::NOP; 3856 unsigned SecondShift = Mips::NOP; 3857 3858 if (hasMips64r2()) { 3859 if (TmpReg == SReg) { 3860 TmpReg = getATReg(Inst.getLoc()); 3861 if (!TmpReg) 3862 return true; 3863 } 3864 3865 if (Inst.getOpcode() == Mips::DROL) { 3866 TOut.emitRRR(Mips::DSUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 3867 TOut.emitRRR(Mips::DROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI); 3868 return false; 3869 } 3870 3871 if (Inst.getOpcode() == Mips::DROR) { 3872 TOut.emitRRR(Mips::DROTRV, DReg, SReg, TReg, Inst.getLoc(), STI); 3873 return false; 3874 } 3875 3876 return true; 3877 } 3878 3879 if (hasMips64()) { 3880 switch (Inst.getOpcode()) { 3881 default: 3882 llvm_unreachable("unexpected instruction opcode"); 3883 case Mips::DROL: 3884 FirstShift = Mips::DSRLV; 3885 SecondShift = Mips::DSLLV; 3886 break; 3887 case Mips::DROR: 3888 FirstShift = Mips::DSLLV; 3889 SecondShift = Mips::DSRLV; 3890 break; 3891 } 3892 3893 ATReg = getATReg(Inst.getLoc()); 3894 if (!ATReg) 3895 return true; 3896 3897 TOut.emitRRR(Mips::DSUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 3898 TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI); 3899 TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI); 3900 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 3901 3902 return false; 3903 } 3904 3905 return true; 3906 } 3907 3908 bool MipsAsmParser::expandDRotationImm(MCInst &Inst, SMLoc IDLoc, 3909 MCStreamer &Out, 3910 const MCSubtargetInfo *STI) { 3911 MipsTargetStreamer &TOut = getTargetStreamer(); 3912 unsigned ATReg = Mips::NoRegister; 3913 unsigned DReg = Inst.getOperand(0).getReg(); 3914 unsigned SReg = Inst.getOperand(1).getReg(); 3915 int64_t ImmValue = Inst.getOperand(2).getImm() % 64; 3916 3917 unsigned FirstShift = Mips::NOP; 3918 unsigned SecondShift = Mips::NOP; 3919 3920 MCInst TmpInst; 3921 3922 if (hasMips64r2()) { 3923 unsigned FinalOpcode = Mips::NOP; 3924 if (ImmValue == 0) 3925 FinalOpcode = Mips::DROTR; 3926 else if (ImmValue % 32 == 0) 3927 FinalOpcode = Mips::DROTR32; 3928 else if ((ImmValue >= 1) && (ImmValue <= 32)) { 3929 if (Inst.getOpcode() == Mips::DROLImm) 3930 FinalOpcode = Mips::DROTR32; 3931 else 3932 FinalOpcode = Mips::DROTR; 3933 } else if (ImmValue >= 33) { 3934 if (Inst.getOpcode() == Mips::DROLImm) 3935 FinalOpcode = Mips::DROTR; 3936 else 3937 FinalOpcode = Mips::DROTR32; 3938 } 3939 3940 uint64_t ShiftValue = ImmValue % 32; 3941 if (Inst.getOpcode() == Mips::DROLImm) 3942 ShiftValue = (32 - ImmValue % 32) % 32; 3943 3944 TOut.emitRRI(FinalOpcode, DReg, SReg, ShiftValue, Inst.getLoc(), STI); 3945 3946 return false; 3947 } 3948 3949 if (hasMips64()) { 3950 if (ImmValue == 0) { 3951 TOut.emitRRI(Mips::DSRL, DReg, SReg, 0, Inst.getLoc(), STI); 3952 return false; 3953 } 3954 3955 switch (Inst.getOpcode()) { 3956 default: 3957 llvm_unreachable("unexpected instruction opcode"); 3958 case Mips::DROLImm: 3959 if ((ImmValue >= 1) && (ImmValue <= 31)) { 3960 FirstShift = Mips::DSLL; 3961 SecondShift = Mips::DSRL32; 3962 } 3963 if (ImmValue == 32) { 3964 FirstShift = Mips::DSLL32; 3965 SecondShift = Mips::DSRL32; 3966 } 3967 if ((ImmValue >= 33) && (ImmValue <= 63)) { 3968 FirstShift = Mips::DSLL32; 3969 SecondShift = Mips::DSRL; 3970 } 3971 break; 3972 case Mips::DRORImm: 3973 if ((ImmValue >= 1) && (ImmValue <= 31)) { 3974 FirstShift = Mips::DSRL; 3975 SecondShift = Mips::DSLL32; 3976 } 3977 if (ImmValue == 32) { 3978 FirstShift = Mips::DSRL32; 3979 SecondShift = Mips::DSLL32; 3980 } 3981 if ((ImmValue >= 33) && (ImmValue <= 63)) { 3982 FirstShift = Mips::DSRL32; 3983 SecondShift = Mips::DSLL; 3984 } 3985 break; 3986 } 3987 3988 ATReg = getATReg(Inst.getLoc()); 3989 if (!ATReg) 3990 return true; 3991 3992 TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue % 32, Inst.getLoc(), STI); 3993 TOut.emitRRI(SecondShift, DReg, SReg, (32 - ImmValue % 32) % 32, 3994 Inst.getLoc(), STI); 3995 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 3996 3997 return false; 3998 } 3999 4000 return true; 4001 } 4002 4003 bool MipsAsmParser::expandAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4004 const MCSubtargetInfo *STI) { 4005 MipsTargetStreamer &TOut = getTargetStreamer(); 4006 unsigned FirstRegOp = Inst.getOperand(0).getReg(); 4007 unsigned SecondRegOp = Inst.getOperand(1).getReg(); 4008 4009 TOut.emitRI(Mips::BGEZ, SecondRegOp, 8, IDLoc, STI); 4010 if (FirstRegOp != SecondRegOp) 4011 TOut.emitRRR(Mips::ADDu, FirstRegOp, SecondRegOp, Mips::ZERO, IDLoc, STI); 4012 else 4013 TOut.emitEmptyDelaySlot(false, IDLoc, STI); 4014 TOut.emitRRR(Mips::SUB, FirstRegOp, Mips::ZERO, SecondRegOp, IDLoc, STI); 4015 4016 return false; 4017 } 4018 4019 static unsigned nextReg(unsigned Reg) { 4020 switch (Reg) { 4021 case Mips::ZERO: return Mips::AT; 4022 case Mips::AT: return Mips::V0; 4023 case Mips::V0: return Mips::V1; 4024 case Mips::V1: return Mips::A0; 4025 case Mips::A0: return Mips::A1; 4026 case Mips::A1: return Mips::A2; 4027 case Mips::A2: return Mips::A3; 4028 case Mips::A3: return Mips::T0; 4029 case Mips::T0: return Mips::T1; 4030 case Mips::T1: return Mips::T2; 4031 case Mips::T2: return Mips::T3; 4032 case Mips::T3: return Mips::T4; 4033 case Mips::T4: return Mips::T5; 4034 case Mips::T5: return Mips::T6; 4035 case Mips::T6: return Mips::T7; 4036 case Mips::T7: return Mips::S0; 4037 case Mips::S0: return Mips::S1; 4038 case Mips::S1: return Mips::S2; 4039 case Mips::S2: return Mips::S3; 4040 case Mips::S3: return Mips::S4; 4041 case Mips::S4: return Mips::S5; 4042 case Mips::S5: return Mips::S6; 4043 case Mips::S6: return Mips::S7; 4044 case Mips::S7: return Mips::T8; 4045 case Mips::T8: return Mips::T9; 4046 case Mips::T9: return Mips::K0; 4047 case Mips::K0: return Mips::K1; 4048 case Mips::K1: return Mips::GP; 4049 case Mips::GP: return Mips::SP; 4050 case Mips::SP: return Mips::FP; 4051 case Mips::FP: return Mips::RA; 4052 case Mips::RA: return Mips::ZERO; 4053 default: return 0; 4054 } 4055 4056 } 4057 4058 // Expand 'ld $<reg> offset($reg2)' to 'lw $<reg>, offset($reg2); 4059 // lw $<reg+1>>, offset+4($reg2)' 4060 // or expand 'sd $<reg> offset($reg2)' to 'sw $<reg>, offset($reg2); 4061 // sw $<reg+1>>, offset+4($reg2)' 4062 // for O32. 4063 bool MipsAsmParser::expandLoadStoreDMacro(MCInst &Inst, SMLoc IDLoc, 4064 MCStreamer &Out, 4065 const MCSubtargetInfo *STI, 4066 bool IsLoad) { 4067 if (!isABI_O32()) 4068 return true; 4069 4070 warnIfNoMacro(IDLoc); 4071 4072 MipsTargetStreamer &TOut = getTargetStreamer(); 4073 unsigned Opcode = IsLoad ? Mips::LW : Mips::SW; 4074 unsigned FirstReg = Inst.getOperand(0).getReg(); 4075 unsigned SecondReg = nextReg(FirstReg); 4076 unsigned BaseReg = Inst.getOperand(1).getReg(); 4077 if (!SecondReg) 4078 return true; 4079 4080 warnIfRegIndexIsAT(FirstReg, IDLoc); 4081 4082 assert(Inst.getOperand(2).isImm() && 4083 "Offset for load macro is not immediate!"); 4084 4085 MCOperand &FirstOffset = Inst.getOperand(2); 4086 signed NextOffset = FirstOffset.getImm() + 4; 4087 MCOperand SecondOffset = MCOperand::createImm(NextOffset); 4088 4089 if (!isInt<16>(FirstOffset.getImm()) || !isInt<16>(NextOffset)) 4090 return true; 4091 4092 // For loads, clobber the base register with the second load instead of the 4093 // first if the BaseReg == FirstReg. 4094 if (FirstReg != BaseReg || !IsLoad) { 4095 TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI); 4096 TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI); 4097 } else { 4098 TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI); 4099 TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI); 4100 } 4101 4102 return false; 4103 } 4104 4105 bool MipsAsmParser::expandSeq(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4106 const MCSubtargetInfo *STI) { 4107 4108 warnIfNoMacro(IDLoc); 4109 MipsTargetStreamer &TOut = getTargetStreamer(); 4110 4111 if (Inst.getOperand(1).getReg() != Mips::ZERO && 4112 Inst.getOperand(2).getReg() != Mips::ZERO) { 4113 TOut.emitRRR(Mips::XOR, Inst.getOperand(0).getReg(), 4114 Inst.getOperand(1).getReg(), Inst.getOperand(2).getReg(), 4115 IDLoc, STI); 4116 TOut.emitRRI(Mips::SLTiu, Inst.getOperand(0).getReg(), 4117 Inst.getOperand(0).getReg(), 1, IDLoc, STI); 4118 return false; 4119 } 4120 4121 unsigned Reg = 0; 4122 if (Inst.getOperand(1).getReg() == Mips::ZERO) { 4123 Reg = Inst.getOperand(2).getReg(); 4124 } else { 4125 Reg = Inst.getOperand(1).getReg(); 4126 } 4127 TOut.emitRRI(Mips::SLTiu, Inst.getOperand(0).getReg(), Reg, 1, IDLoc, STI); 4128 return false; 4129 } 4130 4131 bool MipsAsmParser::expandSeqI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4132 const MCSubtargetInfo *STI) { 4133 warnIfNoMacro(IDLoc); 4134 MipsTargetStreamer &TOut = getTargetStreamer(); 4135 4136 unsigned Opc; 4137 int64_t Imm = Inst.getOperand(2).getImm(); 4138 unsigned Reg = Inst.getOperand(1).getReg(); 4139 4140 if (Imm == 0) { 4141 TOut.emitRRI(Mips::SLTiu, Inst.getOperand(0).getReg(), 4142 Inst.getOperand(1).getReg(), 1, IDLoc, STI); 4143 return false; 4144 } else { 4145 4146 if (Reg == Mips::ZERO) { 4147 Warning(IDLoc, "comparison is always false"); 4148 TOut.emitRRR(isGP64bit() ? Mips::DADDu : Mips::ADDu, 4149 Inst.getOperand(0).getReg(), Reg, Reg, IDLoc, STI); 4150 return false; 4151 } 4152 4153 if (Imm > -0x8000 && Imm < 0) { 4154 Imm = -Imm; 4155 Opc = isGP64bit() ? Mips::DADDiu : Mips::ADDiu; 4156 } else { 4157 Opc = Mips::XORi; 4158 } 4159 } 4160 if (!isUInt<16>(Imm)) { 4161 unsigned ATReg = getATReg(IDLoc); 4162 if (!ATReg) 4163 return true; 4164 4165 if (loadImmediate(Imm, ATReg, Mips::NoRegister, true, isGP64bit(), IDLoc, 4166 Out, STI)) 4167 return true; 4168 4169 TOut.emitRRR(Mips::XOR, Inst.getOperand(0).getReg(), 4170 Inst.getOperand(1).getReg(), ATReg, IDLoc, STI); 4171 TOut.emitRRI(Mips::SLTiu, Inst.getOperand(0).getReg(), 4172 Inst.getOperand(0).getReg(), 1, IDLoc, STI); 4173 return false; 4174 } 4175 4176 TOut.emitRRI(Opc, Inst.getOperand(0).getReg(), Inst.getOperand(1).getReg(), 4177 Imm, IDLoc, STI); 4178 TOut.emitRRI(Mips::SLTiu, Inst.getOperand(0).getReg(), 4179 Inst.getOperand(0).getReg(), 1, IDLoc, STI); 4180 return false; 4181 } 4182 4183 unsigned 4184 MipsAsmParser::checkEarlyTargetMatchPredicate(MCInst &Inst, 4185 const OperandVector &Operands) { 4186 switch (Inst.getOpcode()) { 4187 default: 4188 return Match_Success; 4189 case Mips::DATI: 4190 case Mips::DAHI: 4191 case Mips::DATI_MM64R6: 4192 case Mips::DAHI_MM64R6: 4193 if (static_cast<MipsOperand &>(*Operands[1]) 4194 .isValidForTie(static_cast<MipsOperand &>(*Operands[2]))) 4195 return Match_Success; 4196 return Match_RequiresSameSrcAndDst; 4197 } 4198 } 4199 4200 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) { 4201 switch (Inst.getOpcode()) { 4202 // As described by the MIPSR6 spec, daui must not use the zero operand for 4203 // its source operand. 4204 case Mips::DAUI: 4205 case Mips::DAUI_MM64R6: 4206 if (Inst.getOperand(1).getReg() == Mips::ZERO || 4207 Inst.getOperand(1).getReg() == Mips::ZERO_64) 4208 return Match_RequiresNoZeroRegister; 4209 return Match_Success; 4210 // As described by the Mips32r2 spec, the registers Rd and Rs for 4211 // jalr.hb must be different. 4212 // It also applies for registers Rt and Rs of microMIPSr6 jalrc.hb instruction 4213 // and registers Rd and Base for microMIPS lwp instruction 4214 case Mips::JALR_HB: 4215 case Mips::JALRC_HB_MMR6: 4216 case Mips::JALRC_MMR6: 4217 if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) 4218 return Match_RequiresDifferentSrcAndDst; 4219 return Match_Success; 4220 case Mips::LWP_MM: 4221 case Mips::LWP_MMR6: 4222 if (Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) 4223 return Match_RequiresDifferentSrcAndDst; 4224 return Match_Success; 4225 case Mips::SYNC: 4226 if (Inst.getOperand(0).getImm() != 0 && !hasMips32()) 4227 return Match_NonZeroOperandForSync; 4228 return Match_Success; 4229 // As described the MIPSR6 spec, the compact branches that compare registers 4230 // must: 4231 // a) Not use the zero register. 4232 // b) Not use the same register twice. 4233 // c) rs < rt for bnec, beqc. 4234 // NB: For this case, the encoding will swap the operands as their 4235 // ordering doesn't matter. GAS performs this transformation too. 4236 // Hence, that constraint does not have to be enforced. 4237 // 4238 // The compact branches that branch iff the signed addition of two registers 4239 // would overflow must have rs >= rt. That can be handled like beqc/bnec with 4240 // operand swapping. They do not have restriction of using the zero register. 4241 case Mips::BLEZC: case Mips::BLEZC_MMR6: 4242 case Mips::BGEZC: case Mips::BGEZC_MMR6: 4243 case Mips::BGTZC: case Mips::BGTZC_MMR6: 4244 case Mips::BLTZC: case Mips::BLTZC_MMR6: 4245 case Mips::BEQZC: case Mips::BEQZC_MMR6: 4246 case Mips::BNEZC: case Mips::BNEZC_MMR6: 4247 case Mips::BLEZC64: 4248 case Mips::BGEZC64: 4249 case Mips::BGTZC64: 4250 case Mips::BLTZC64: 4251 case Mips::BEQZC64: 4252 case Mips::BNEZC64: 4253 if (Inst.getOperand(0).getReg() == Mips::ZERO || 4254 Inst.getOperand(0).getReg() == Mips::ZERO_64) 4255 return Match_RequiresNoZeroRegister; 4256 return Match_Success; 4257 case Mips::BGEC: case Mips::BGEC_MMR6: 4258 case Mips::BLTC: case Mips::BLTC_MMR6: 4259 case Mips::BGEUC: case Mips::BGEUC_MMR6: 4260 case Mips::BLTUC: case Mips::BLTUC_MMR6: 4261 case Mips::BEQC: case Mips::BEQC_MMR6: 4262 case Mips::BNEC: case Mips::BNEC_MMR6: 4263 case Mips::BGEC64: 4264 case Mips::BLTC64: 4265 case Mips::BGEUC64: 4266 case Mips::BLTUC64: 4267 case Mips::BEQC64: 4268 case Mips::BNEC64: 4269 if (Inst.getOperand(0).getReg() == Mips::ZERO || 4270 Inst.getOperand(0).getReg() == Mips::ZERO_64) 4271 return Match_RequiresNoZeroRegister; 4272 if (Inst.getOperand(1).getReg() == Mips::ZERO || 4273 Inst.getOperand(1).getReg() == Mips::ZERO_64) 4274 return Match_RequiresNoZeroRegister; 4275 if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) 4276 return Match_RequiresDifferentOperands; 4277 return Match_Success; 4278 } 4279 4280 uint64_t TSFlags = getInstDesc(Inst.getOpcode()).TSFlags; 4281 if ((TSFlags & MipsII::HasFCCRegOperand) && 4282 (Inst.getOperand(0).getReg() != Mips::FCC0) && !hasEightFccRegisters()) 4283 return Match_NoFCCRegisterForCurrentISA; 4284 4285 return Match_Success; 4286 4287 } 4288 4289 static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands, 4290 uint64_t ErrorInfo) { 4291 if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) { 4292 SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc(); 4293 if (ErrorLoc == SMLoc()) 4294 return Loc; 4295 return ErrorLoc; 4296 } 4297 return Loc; 4298 } 4299 4300 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 4301 OperandVector &Operands, 4302 MCStreamer &Out, 4303 uint64_t &ErrorInfo, 4304 bool MatchingInlineAsm) { 4305 MCInst Inst; 4306 unsigned MatchResult = 4307 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); 4308 4309 switch (MatchResult) { 4310 case Match_Success: 4311 if (processInstruction(Inst, IDLoc, Out, STI)) 4312 return true; 4313 return false; 4314 case Match_MissingFeature: 4315 Error(IDLoc, "instruction requires a CPU feature not currently enabled"); 4316 return true; 4317 case Match_InvalidOperand: { 4318 SMLoc ErrorLoc = IDLoc; 4319 if (ErrorInfo != ~0ULL) { 4320 if (ErrorInfo >= Operands.size()) 4321 return Error(IDLoc, "too few operands for instruction"); 4322 4323 ErrorLoc = Operands[ErrorInfo]->getStartLoc(); 4324 if (ErrorLoc == SMLoc()) 4325 ErrorLoc = IDLoc; 4326 } 4327 4328 return Error(ErrorLoc, "invalid operand for instruction"); 4329 } 4330 case Match_NonZeroOperandForSync: 4331 return Error(IDLoc, "s-type must be zero or unspecified for pre-MIPS32 ISAs"); 4332 case Match_MnemonicFail: 4333 return Error(IDLoc, "invalid instruction"); 4334 case Match_RequiresDifferentSrcAndDst: 4335 return Error(IDLoc, "source and destination must be different"); 4336 case Match_RequiresDifferentOperands: 4337 return Error(IDLoc, "registers must be different"); 4338 case Match_RequiresNoZeroRegister: 4339 return Error(IDLoc, "invalid operand ($zero) for instruction"); 4340 case Match_RequiresSameSrcAndDst: 4341 return Error(IDLoc, "source and destination must match"); 4342 case Match_NoFCCRegisterForCurrentISA: 4343 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4344 "non-zero fcc register doesn't exist in current ISA level"); 4345 case Match_Immz: 4346 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), "expected '0'"); 4347 case Match_UImm1_0: 4348 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4349 "expected 1-bit unsigned immediate"); 4350 case Match_UImm2_0: 4351 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4352 "expected 2-bit unsigned immediate"); 4353 case Match_UImm2_1: 4354 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4355 "expected immediate in range 1 .. 4"); 4356 case Match_UImm3_0: 4357 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4358 "expected 3-bit unsigned immediate"); 4359 case Match_UImm4_0: 4360 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4361 "expected 4-bit unsigned immediate"); 4362 case Match_SImm4_0: 4363 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4364 "expected 4-bit signed immediate"); 4365 case Match_UImm5_0: 4366 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4367 "expected 5-bit unsigned immediate"); 4368 case Match_SImm5_0: 4369 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4370 "expected 5-bit signed immediate"); 4371 case Match_UImm5_1: 4372 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4373 "expected immediate in range 1 .. 32"); 4374 case Match_UImm5_32: 4375 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4376 "expected immediate in range 32 .. 63"); 4377 case Match_UImm5_33: 4378 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4379 "expected immediate in range 33 .. 64"); 4380 case Match_UImm5_0_Report_UImm6: 4381 // This is used on UImm5 operands that have a corresponding UImm5_32 4382 // operand to avoid confusing the user. 4383 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4384 "expected 6-bit unsigned immediate"); 4385 case Match_UImm5_Lsl2: 4386 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4387 "expected both 7-bit unsigned immediate and multiple of 4"); 4388 case Match_UImmRange2_64: 4389 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4390 "expected immediate in range 2 .. 64"); 4391 case Match_UImm6_0: 4392 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4393 "expected 6-bit unsigned immediate"); 4394 case Match_UImm6_Lsl2: 4395 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4396 "expected both 8-bit unsigned immediate and multiple of 4"); 4397 case Match_SImm6_0: 4398 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4399 "expected 6-bit signed immediate"); 4400 case Match_UImm7_0: 4401 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4402 "expected 7-bit unsigned immediate"); 4403 case Match_UImm7_N1: 4404 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4405 "expected immediate in range -1 .. 126"); 4406 case Match_SImm7_Lsl2: 4407 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4408 "expected both 9-bit signed immediate and multiple of 4"); 4409 case Match_UImm8_0: 4410 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4411 "expected 8-bit unsigned immediate"); 4412 case Match_UImm10_0: 4413 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4414 "expected 10-bit unsigned immediate"); 4415 case Match_SImm10_0: 4416 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4417 "expected 10-bit signed immediate"); 4418 case Match_SImm11_0: 4419 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4420 "expected 11-bit signed immediate"); 4421 case Match_UImm16: 4422 case Match_UImm16_Relaxed: 4423 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4424 "expected 16-bit unsigned immediate"); 4425 case Match_SImm16: 4426 case Match_SImm16_Relaxed: 4427 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4428 "expected 16-bit signed immediate"); 4429 case Match_SImm19_Lsl2: 4430 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4431 "expected both 19-bit signed immediate and multiple of 4"); 4432 case Match_UImm20_0: 4433 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4434 "expected 20-bit unsigned immediate"); 4435 case Match_UImm26_0: 4436 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4437 "expected 26-bit unsigned immediate"); 4438 case Match_SImm32: 4439 case Match_SImm32_Relaxed: 4440 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4441 "expected 32-bit signed immediate"); 4442 case Match_UImm32_Coerced: 4443 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4444 "expected 32-bit immediate"); 4445 case Match_MemSImm9: 4446 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4447 "expected memory with 9-bit signed offset"); 4448 case Match_MemSImm10: 4449 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4450 "expected memory with 10-bit signed offset"); 4451 case Match_MemSImm10Lsl1: 4452 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4453 "expected memory with 11-bit signed offset and multiple of 2"); 4454 case Match_MemSImm10Lsl2: 4455 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4456 "expected memory with 12-bit signed offset and multiple of 4"); 4457 case Match_MemSImm10Lsl3: 4458 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4459 "expected memory with 13-bit signed offset and multiple of 8"); 4460 case Match_MemSImm11: 4461 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4462 "expected memory with 11-bit signed offset"); 4463 case Match_MemSImm12: 4464 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4465 "expected memory with 12-bit signed offset"); 4466 case Match_MemSImm16: 4467 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 4468 "expected memory with 16-bit signed offset"); 4469 } 4470 4471 llvm_unreachable("Implement any new match types added!"); 4472 } 4473 4474 void MipsAsmParser::warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc) { 4475 if (RegIndex != 0 && AssemblerOptions.back()->getATRegIndex() == RegIndex) 4476 Warning(Loc, "used $at (currently $" + Twine(RegIndex) + 4477 ") without \".set noat\""); 4478 } 4479 4480 void MipsAsmParser::warnIfNoMacro(SMLoc Loc) { 4481 if (!AssemblerOptions.back()->isMacro()) 4482 Warning(Loc, "macro instruction expanded into multiple instructions"); 4483 } 4484 4485 void 4486 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg, 4487 SMRange Range, bool ShowColors) { 4488 getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg, 4489 Range, SMFixIt(Range, FixMsg), 4490 ShowColors); 4491 } 4492 4493 int MipsAsmParser::matchCPURegisterName(StringRef Name) { 4494 int CC; 4495 4496 CC = StringSwitch<unsigned>(Name) 4497 .Case("zero", 0) 4498 .Case("at", 1) 4499 .Case("a0", 4) 4500 .Case("a1", 5) 4501 .Case("a2", 6) 4502 .Case("a3", 7) 4503 .Case("v0", 2) 4504 .Case("v1", 3) 4505 .Case("s0", 16) 4506 .Case("s1", 17) 4507 .Case("s2", 18) 4508 .Case("s3", 19) 4509 .Case("s4", 20) 4510 .Case("s5", 21) 4511 .Case("s6", 22) 4512 .Case("s7", 23) 4513 .Case("k0", 26) 4514 .Case("k1", 27) 4515 .Case("gp", 28) 4516 .Case("sp", 29) 4517 .Case("fp", 30) 4518 .Case("s8", 30) 4519 .Case("ra", 31) 4520 .Case("t0", 8) 4521 .Case("t1", 9) 4522 .Case("t2", 10) 4523 .Case("t3", 11) 4524 .Case("t4", 12) 4525 .Case("t5", 13) 4526 .Case("t6", 14) 4527 .Case("t7", 15) 4528 .Case("t8", 24) 4529 .Case("t9", 25) 4530 .Default(-1); 4531 4532 if (!(isABI_N32() || isABI_N64())) 4533 return CC; 4534 4535 if (12 <= CC && CC <= 15) { 4536 // Name is one of t4-t7 4537 AsmToken RegTok = getLexer().peekTok(); 4538 SMRange RegRange = RegTok.getLocRange(); 4539 4540 StringRef FixedName = StringSwitch<StringRef>(Name) 4541 .Case("t4", "t0") 4542 .Case("t5", "t1") 4543 .Case("t6", "t2") 4544 .Case("t7", "t3") 4545 .Default(""); 4546 assert(FixedName != "" && "Register name is not one of t4-t7."); 4547 4548 printWarningWithFixIt("register names $t4-$t7 are only available in O32.", 4549 "Did you mean $" + FixedName + "?", RegRange); 4550 } 4551 4552 // Although SGI documentation just cuts out t0-t3 for n32/n64, 4553 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7 4554 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7. 4555 if (8 <= CC && CC <= 11) 4556 CC += 4; 4557 4558 if (CC == -1) 4559 CC = StringSwitch<unsigned>(Name) 4560 .Case("a4", 8) 4561 .Case("a5", 9) 4562 .Case("a6", 10) 4563 .Case("a7", 11) 4564 .Case("kt0", 26) 4565 .Case("kt1", 27) 4566 .Default(-1); 4567 4568 return CC; 4569 } 4570 4571 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) { 4572 int CC; 4573 4574 CC = StringSwitch<unsigned>(Name) 4575 .Case("hwr_cpunum", 0) 4576 .Case("hwr_synci_step", 1) 4577 .Case("hwr_cc", 2) 4578 .Case("hwr_ccres", 3) 4579 .Case("hwr_ulr", 29) 4580 .Default(-1); 4581 4582 return CC; 4583 } 4584 4585 int MipsAsmParser::matchFPURegisterName(StringRef Name) { 4586 if (Name[0] == 'f') { 4587 StringRef NumString = Name.substr(1); 4588 unsigned IntVal; 4589 if (NumString.getAsInteger(10, IntVal)) 4590 return -1; // This is not an integer. 4591 if (IntVal > 31) // Maximum index for fpu register. 4592 return -1; 4593 return IntVal; 4594 } 4595 return -1; 4596 } 4597 4598 int MipsAsmParser::matchFCCRegisterName(StringRef Name) { 4599 if (Name.startswith("fcc")) { 4600 StringRef NumString = Name.substr(3); 4601 unsigned IntVal; 4602 if (NumString.getAsInteger(10, IntVal)) 4603 return -1; // This is not an integer. 4604 if (IntVal > 7) // There are only 8 fcc registers. 4605 return -1; 4606 return IntVal; 4607 } 4608 return -1; 4609 } 4610 4611 int MipsAsmParser::matchACRegisterName(StringRef Name) { 4612 if (Name.startswith("ac")) { 4613 StringRef NumString = Name.substr(2); 4614 unsigned IntVal; 4615 if (NumString.getAsInteger(10, IntVal)) 4616 return -1; // This is not an integer. 4617 if (IntVal > 3) // There are only 3 acc registers. 4618 return -1; 4619 return IntVal; 4620 } 4621 return -1; 4622 } 4623 4624 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) { 4625 unsigned IntVal; 4626 4627 if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal)) 4628 return -1; 4629 4630 if (IntVal > 31) 4631 return -1; 4632 4633 return IntVal; 4634 } 4635 4636 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) { 4637 int CC; 4638 4639 CC = StringSwitch<unsigned>(Name) 4640 .Case("msair", 0) 4641 .Case("msacsr", 1) 4642 .Case("msaaccess", 2) 4643 .Case("msasave", 3) 4644 .Case("msamodify", 4) 4645 .Case("msarequest", 5) 4646 .Case("msamap", 6) 4647 .Case("msaunmap", 7) 4648 .Default(-1); 4649 4650 return CC; 4651 } 4652 4653 unsigned MipsAsmParser::getATReg(SMLoc Loc) { 4654 unsigned ATIndex = AssemblerOptions.back()->getATRegIndex(); 4655 if (ATIndex == 0) { 4656 reportParseError(Loc, 4657 "pseudo-instruction requires $at, which is not available"); 4658 return 0; 4659 } 4660 unsigned AT = getReg( 4661 (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex); 4662 return AT; 4663 } 4664 4665 unsigned MipsAsmParser::getReg(int RC, int RegNo) { 4666 return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo); 4667 } 4668 4669 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) { 4670 MCAsmParser &Parser = getParser(); 4671 DEBUG(dbgs() << "parseOperand\n"); 4672 4673 // Check if the current operand has a custom associated parser, if so, try to 4674 // custom parse the operand, or fallback to the general approach. 4675 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); 4676 if (ResTy == MatchOperand_Success) 4677 return false; 4678 // If there wasn't a custom match, try the generic matcher below. Otherwise, 4679 // there was a match, but an error occurred, in which case, just return that 4680 // the operand parsing failed. 4681 if (ResTy == MatchOperand_ParseFail) 4682 return true; 4683 4684 DEBUG(dbgs() << ".. Generic Parser\n"); 4685 4686 switch (getLexer().getKind()) { 4687 case AsmToken::Dollar: { 4688 // Parse the register. 4689 SMLoc S = Parser.getTok().getLoc(); 4690 4691 // Almost all registers have been parsed by custom parsers. There is only 4692 // one exception to this. $zero (and it's alias $0) will reach this point 4693 // for div, divu, and similar instructions because it is not an operand 4694 // to the instruction definition but an explicit register. Special case 4695 // this situation for now. 4696 if (parseAnyRegister(Operands) != MatchOperand_NoMatch) 4697 return false; 4698 4699 // Maybe it is a symbol reference. 4700 StringRef Identifier; 4701 if (Parser.parseIdentifier(Identifier)) 4702 return true; 4703 4704 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 4705 MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier); 4706 // Otherwise create a symbol reference. 4707 const MCExpr *Res = 4708 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 4709 4710 Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this)); 4711 return false; 4712 } 4713 default: { 4714 DEBUG(dbgs() << ".. generic integer expression\n"); 4715 4716 const MCExpr *Expr; 4717 SMLoc S = Parser.getTok().getLoc(); // Start location of the operand. 4718 if (getParser().parseExpression(Expr)) 4719 return true; 4720 4721 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 4722 4723 Operands.push_back(MipsOperand::CreateImm(Expr, S, E, *this)); 4724 return false; 4725 } 4726 } // switch(getLexer().getKind()) 4727 return true; 4728 } 4729 4730 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) { 4731 switch (Expr->getKind()) { 4732 case MCExpr::Constant: 4733 return true; 4734 case MCExpr::SymbolRef: 4735 return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None); 4736 case MCExpr::Binary: 4737 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) { 4738 if (!isEvaluated(BE->getLHS())) 4739 return false; 4740 return isEvaluated(BE->getRHS()); 4741 } 4742 case MCExpr::Unary: 4743 return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr()); 4744 case MCExpr::Target: 4745 return true; 4746 } 4747 return false; 4748 } 4749 4750 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, 4751 SMLoc &EndLoc) { 4752 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands; 4753 OperandMatchResultTy ResTy = parseAnyRegister(Operands); 4754 if (ResTy == MatchOperand_Success) { 4755 assert(Operands.size() == 1); 4756 MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front()); 4757 StartLoc = Operand.getStartLoc(); 4758 EndLoc = Operand.getEndLoc(); 4759 4760 // AFAIK, we only support numeric registers and named GPR's in CFI 4761 // directives. 4762 // Don't worry about eating tokens before failing. Using an unrecognised 4763 // register is a parse error. 4764 if (Operand.isGPRAsmReg()) { 4765 // Resolve to GPR32 or GPR64 appropriately. 4766 RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg(); 4767 } 4768 4769 return (RegNo == (unsigned)-1); 4770 } 4771 4772 assert(Operands.size() == 0); 4773 return (RegNo == (unsigned)-1); 4774 } 4775 4776 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) { 4777 SMLoc S; 4778 4779 if (isParenExpr) 4780 return getParser().parseParenExprOfDepth(0, Res, S); 4781 return getParser().parseExpression(Res); 4782 } 4783 4784 OperandMatchResultTy 4785 MipsAsmParser::parseMemOperand(OperandVector &Operands) { 4786 MCAsmParser &Parser = getParser(); 4787 DEBUG(dbgs() << "parseMemOperand\n"); 4788 const MCExpr *IdVal = nullptr; 4789 SMLoc S; 4790 bool isParenExpr = false; 4791 OperandMatchResultTy Res = MatchOperand_NoMatch; 4792 // First operand is the offset. 4793 S = Parser.getTok().getLoc(); 4794 4795 if (getLexer().getKind() == AsmToken::LParen) { 4796 Parser.Lex(); 4797 isParenExpr = true; 4798 } 4799 4800 if (getLexer().getKind() != AsmToken::Dollar) { 4801 if (parseMemOffset(IdVal, isParenExpr)) 4802 return MatchOperand_ParseFail; 4803 4804 const AsmToken &Tok = Parser.getTok(); // Get the next token. 4805 if (Tok.isNot(AsmToken::LParen)) { 4806 MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]); 4807 if (Mnemonic.getToken() == "la" || Mnemonic.getToken() == "dla") { 4808 SMLoc E = 4809 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 4810 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this)); 4811 return MatchOperand_Success; 4812 } 4813 if (Tok.is(AsmToken::EndOfStatement)) { 4814 SMLoc E = 4815 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 4816 4817 // Zero register assumed, add a memory operand with ZERO as its base. 4818 // "Base" will be managed by k_Memory. 4819 auto Base = MipsOperand::createGPRReg( 4820 0, "0", getContext().getRegisterInfo(), S, E, *this); 4821 Operands.push_back( 4822 MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this)); 4823 return MatchOperand_Success; 4824 } 4825 MCBinaryExpr::Opcode Opcode; 4826 // GAS and LLVM treat comparison operators different. GAS will generate -1 4827 // or 0, while LLVM will generate 0 or 1. Since a comparsion operator is 4828 // highly unlikely to be found in a memory offset expression, we don't 4829 // handle them. 4830 switch (Tok.getKind()) { 4831 case AsmToken::Plus: 4832 Opcode = MCBinaryExpr::Add; 4833 Parser.Lex(); 4834 break; 4835 case AsmToken::Minus: 4836 Opcode = MCBinaryExpr::Sub; 4837 Parser.Lex(); 4838 break; 4839 case AsmToken::Star: 4840 Opcode = MCBinaryExpr::Mul; 4841 Parser.Lex(); 4842 break; 4843 case AsmToken::Pipe: 4844 Opcode = MCBinaryExpr::Or; 4845 Parser.Lex(); 4846 break; 4847 case AsmToken::Amp: 4848 Opcode = MCBinaryExpr::And; 4849 Parser.Lex(); 4850 break; 4851 case AsmToken::LessLess: 4852 Opcode = MCBinaryExpr::Shl; 4853 Parser.Lex(); 4854 break; 4855 case AsmToken::GreaterGreater: 4856 Opcode = MCBinaryExpr::LShr; 4857 Parser.Lex(); 4858 break; 4859 case AsmToken::Caret: 4860 Opcode = MCBinaryExpr::Xor; 4861 Parser.Lex(); 4862 break; 4863 case AsmToken::Slash: 4864 Opcode = MCBinaryExpr::Div; 4865 Parser.Lex(); 4866 break; 4867 case AsmToken::Percent: 4868 Opcode = MCBinaryExpr::Mod; 4869 Parser.Lex(); 4870 break; 4871 default: 4872 Error(Parser.getTok().getLoc(), "'(' or expression expected"); 4873 return MatchOperand_ParseFail; 4874 } 4875 const MCExpr * NextExpr; 4876 if (getParser().parseExpression(NextExpr)) 4877 return MatchOperand_ParseFail; 4878 IdVal = MCBinaryExpr::create(Opcode, IdVal, NextExpr, getContext()); 4879 } 4880 4881 Parser.Lex(); // Eat the '(' token. 4882 } 4883 4884 Res = parseAnyRegister(Operands); 4885 if (Res != MatchOperand_Success) 4886 return Res; 4887 4888 if (Parser.getTok().isNot(AsmToken::RParen)) { 4889 Error(Parser.getTok().getLoc(), "')' expected"); 4890 return MatchOperand_ParseFail; 4891 } 4892 4893 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 4894 4895 Parser.Lex(); // Eat the ')' token. 4896 4897 if (!IdVal) 4898 IdVal = MCConstantExpr::create(0, getContext()); 4899 4900 // Replace the register operand with the memory operand. 4901 std::unique_ptr<MipsOperand> op( 4902 static_cast<MipsOperand *>(Operands.back().release())); 4903 // Remove the register from the operands. 4904 // "op" will be managed by k_Memory. 4905 Operands.pop_back(); 4906 // Add the memory operand. 4907 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) { 4908 int64_t Imm; 4909 if (IdVal->evaluateAsAbsolute(Imm)) 4910 IdVal = MCConstantExpr::create(Imm, getContext()); 4911 else if (BE->getLHS()->getKind() != MCExpr::SymbolRef) 4912 IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(), 4913 getContext()); 4914 } 4915 4916 Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this)); 4917 return MatchOperand_Success; 4918 } 4919 4920 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) { 4921 MCAsmParser &Parser = getParser(); 4922 MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier()); 4923 if (Sym) { 4924 SMLoc S = Parser.getTok().getLoc(); 4925 const MCExpr *Expr; 4926 if (Sym->isVariable()) 4927 Expr = Sym->getVariableValue(); 4928 else 4929 return false; 4930 if (Expr->getKind() == MCExpr::SymbolRef) { 4931 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr); 4932 StringRef DefSymbol = Ref->getSymbol().getName(); 4933 if (DefSymbol.startswith("$")) { 4934 OperandMatchResultTy ResTy = 4935 matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S); 4936 if (ResTy == MatchOperand_Success) { 4937 Parser.Lex(); 4938 return true; 4939 } else if (ResTy == MatchOperand_ParseFail) 4940 llvm_unreachable("Should never ParseFail"); 4941 return false; 4942 } 4943 } 4944 } 4945 return false; 4946 } 4947 4948 OperandMatchResultTy 4949 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands, 4950 StringRef Identifier, 4951 SMLoc S) { 4952 int Index = matchCPURegisterName(Identifier); 4953 if (Index != -1) { 4954 Operands.push_back(MipsOperand::createGPRReg( 4955 Index, Identifier, getContext().getRegisterInfo(), S, 4956 getLexer().getLoc(), *this)); 4957 return MatchOperand_Success; 4958 } 4959 4960 Index = matchHWRegsRegisterName(Identifier); 4961 if (Index != -1) { 4962 Operands.push_back(MipsOperand::createHWRegsReg( 4963 Index, Identifier, getContext().getRegisterInfo(), S, 4964 getLexer().getLoc(), *this)); 4965 return MatchOperand_Success; 4966 } 4967 4968 Index = matchFPURegisterName(Identifier); 4969 if (Index != -1) { 4970 Operands.push_back(MipsOperand::createFGRReg( 4971 Index, Identifier, getContext().getRegisterInfo(), S, 4972 getLexer().getLoc(), *this)); 4973 return MatchOperand_Success; 4974 } 4975 4976 Index = matchFCCRegisterName(Identifier); 4977 if (Index != -1) { 4978 Operands.push_back(MipsOperand::createFCCReg( 4979 Index, Identifier, getContext().getRegisterInfo(), S, 4980 getLexer().getLoc(), *this)); 4981 return MatchOperand_Success; 4982 } 4983 4984 Index = matchACRegisterName(Identifier); 4985 if (Index != -1) { 4986 Operands.push_back(MipsOperand::createACCReg( 4987 Index, Identifier, getContext().getRegisterInfo(), S, 4988 getLexer().getLoc(), *this)); 4989 return MatchOperand_Success; 4990 } 4991 4992 Index = matchMSA128RegisterName(Identifier); 4993 if (Index != -1) { 4994 Operands.push_back(MipsOperand::createMSA128Reg( 4995 Index, Identifier, getContext().getRegisterInfo(), S, 4996 getLexer().getLoc(), *this)); 4997 return MatchOperand_Success; 4998 } 4999 5000 Index = matchMSA128CtrlRegisterName(Identifier); 5001 if (Index != -1) { 5002 Operands.push_back(MipsOperand::createMSACtrlReg( 5003 Index, Identifier, getContext().getRegisterInfo(), S, 5004 getLexer().getLoc(), *this)); 5005 return MatchOperand_Success; 5006 } 5007 5008 return MatchOperand_NoMatch; 5009 } 5010 5011 OperandMatchResultTy 5012 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) { 5013 MCAsmParser &Parser = getParser(); 5014 auto Token = Parser.getLexer().peekTok(false); 5015 5016 if (Token.is(AsmToken::Identifier)) { 5017 DEBUG(dbgs() << ".. identifier\n"); 5018 StringRef Identifier = Token.getIdentifier(); 5019 OperandMatchResultTy ResTy = 5020 matchAnyRegisterNameWithoutDollar(Operands, Identifier, S); 5021 return ResTy; 5022 } else if (Token.is(AsmToken::Integer)) { 5023 DEBUG(dbgs() << ".. integer\n"); 5024 Operands.push_back(MipsOperand::createNumericReg( 5025 Token.getIntVal(), Token.getString(), getContext().getRegisterInfo(), S, 5026 Token.getLoc(), *this)); 5027 return MatchOperand_Success; 5028 } 5029 5030 DEBUG(dbgs() << Parser.getTok().getKind() << "\n"); 5031 5032 return MatchOperand_NoMatch; 5033 } 5034 5035 OperandMatchResultTy 5036 MipsAsmParser::parseAnyRegister(OperandVector &Operands) { 5037 MCAsmParser &Parser = getParser(); 5038 DEBUG(dbgs() << "parseAnyRegister\n"); 5039 5040 auto Token = Parser.getTok(); 5041 5042 SMLoc S = Token.getLoc(); 5043 5044 if (Token.isNot(AsmToken::Dollar)) { 5045 DEBUG(dbgs() << ".. !$ -> try sym aliasing\n"); 5046 if (Token.is(AsmToken::Identifier)) { 5047 if (searchSymbolAlias(Operands)) 5048 return MatchOperand_Success; 5049 } 5050 DEBUG(dbgs() << ".. !symalias -> NoMatch\n"); 5051 return MatchOperand_NoMatch; 5052 } 5053 DEBUG(dbgs() << ".. $\n"); 5054 5055 OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S); 5056 if (ResTy == MatchOperand_Success) { 5057 Parser.Lex(); // $ 5058 Parser.Lex(); // identifier 5059 } 5060 return ResTy; 5061 } 5062 5063 OperandMatchResultTy 5064 MipsAsmParser::parseJumpTarget(OperandVector &Operands) { 5065 MCAsmParser &Parser = getParser(); 5066 DEBUG(dbgs() << "parseJumpTarget\n"); 5067 5068 SMLoc S = getLexer().getLoc(); 5069 5070 // Registers are a valid target and have priority over symbols. 5071 OperandMatchResultTy ResTy = parseAnyRegister(Operands); 5072 if (ResTy != MatchOperand_NoMatch) 5073 return ResTy; 5074 5075 // Integers and expressions are acceptable 5076 const MCExpr *Expr = nullptr; 5077 if (Parser.parseExpression(Expr)) { 5078 // We have no way of knowing if a symbol was consumed so we must ParseFail 5079 return MatchOperand_ParseFail; 5080 } 5081 Operands.push_back( 5082 MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this)); 5083 return MatchOperand_Success; 5084 } 5085 5086 OperandMatchResultTy 5087 MipsAsmParser::parseInvNum(OperandVector &Operands) { 5088 MCAsmParser &Parser = getParser(); 5089 const MCExpr *IdVal; 5090 // If the first token is '$' we may have register operand. 5091 if (Parser.getTok().is(AsmToken::Dollar)) 5092 return MatchOperand_NoMatch; 5093 SMLoc S = Parser.getTok().getLoc(); 5094 if (getParser().parseExpression(IdVal)) 5095 return MatchOperand_ParseFail; 5096 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal); 5097 assert(MCE && "Unexpected MCExpr type."); 5098 int64_t Val = MCE->getValue(); 5099 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 5100 Operands.push_back(MipsOperand::CreateImm( 5101 MCConstantExpr::create(0 - Val, getContext()), S, E, *this)); 5102 return MatchOperand_Success; 5103 } 5104 5105 OperandMatchResultTy 5106 MipsAsmParser::parseRegisterList(OperandVector &Operands) { 5107 MCAsmParser &Parser = getParser(); 5108 SmallVector<unsigned, 10> Regs; 5109 unsigned RegNo; 5110 unsigned PrevReg = Mips::NoRegister; 5111 bool RegRange = false; 5112 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands; 5113 5114 if (Parser.getTok().isNot(AsmToken::Dollar)) 5115 return MatchOperand_ParseFail; 5116 5117 SMLoc S = Parser.getTok().getLoc(); 5118 while (parseAnyRegister(TmpOperands) == MatchOperand_Success) { 5119 SMLoc E = getLexer().getLoc(); 5120 MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back()); 5121 RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg(); 5122 if (RegRange) { 5123 // Remove last register operand because registers from register range 5124 // should be inserted first. 5125 if ((isGP64bit() && RegNo == Mips::RA_64) || 5126 (!isGP64bit() && RegNo == Mips::RA)) { 5127 Regs.push_back(RegNo); 5128 } else { 5129 unsigned TmpReg = PrevReg + 1; 5130 while (TmpReg <= RegNo) { 5131 if ((((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) && !isGP64bit()) || 5132 (((TmpReg < Mips::S0_64) || (TmpReg > Mips::S7_64)) && 5133 isGP64bit())) { 5134 Error(E, "invalid register operand"); 5135 return MatchOperand_ParseFail; 5136 } 5137 5138 PrevReg = TmpReg; 5139 Regs.push_back(TmpReg++); 5140 } 5141 } 5142 5143 RegRange = false; 5144 } else { 5145 if ((PrevReg == Mips::NoRegister) && 5146 ((isGP64bit() && (RegNo != Mips::S0_64) && (RegNo != Mips::RA_64)) || 5147 (!isGP64bit() && (RegNo != Mips::S0) && (RegNo != Mips::RA)))) { 5148 Error(E, "$16 or $31 expected"); 5149 return MatchOperand_ParseFail; 5150 } else if (!(((RegNo == Mips::FP || RegNo == Mips::RA || 5151 (RegNo >= Mips::S0 && RegNo <= Mips::S7)) && 5152 !isGP64bit()) || 5153 ((RegNo == Mips::FP_64 || RegNo == Mips::RA_64 || 5154 (RegNo >= Mips::S0_64 && RegNo <= Mips::S7_64)) && 5155 isGP64bit()))) { 5156 Error(E, "invalid register operand"); 5157 return MatchOperand_ParseFail; 5158 } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) && 5159 ((RegNo != Mips::FP && RegNo != Mips::RA && !isGP64bit()) || 5160 (RegNo != Mips::FP_64 && RegNo != Mips::RA_64 && 5161 isGP64bit()))) { 5162 Error(E, "consecutive register numbers expected"); 5163 return MatchOperand_ParseFail; 5164 } 5165 5166 Regs.push_back(RegNo); 5167 } 5168 5169 if (Parser.getTok().is(AsmToken::Minus)) 5170 RegRange = true; 5171 5172 if (!Parser.getTok().isNot(AsmToken::Minus) && 5173 !Parser.getTok().isNot(AsmToken::Comma)) { 5174 Error(E, "',' or '-' expected"); 5175 return MatchOperand_ParseFail; 5176 } 5177 5178 Lex(); // Consume comma or minus 5179 if (Parser.getTok().isNot(AsmToken::Dollar)) 5180 break; 5181 5182 PrevReg = RegNo; 5183 } 5184 5185 SMLoc E = Parser.getTok().getLoc(); 5186 Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this)); 5187 parseMemOperand(Operands); 5188 return MatchOperand_Success; 5189 } 5190 5191 OperandMatchResultTy 5192 MipsAsmParser::parseRegisterPair(OperandVector &Operands) { 5193 MCAsmParser &Parser = getParser(); 5194 5195 SMLoc S = Parser.getTok().getLoc(); 5196 if (parseAnyRegister(Operands) != MatchOperand_Success) 5197 return MatchOperand_ParseFail; 5198 5199 SMLoc E = Parser.getTok().getLoc(); 5200 MipsOperand Op = static_cast<MipsOperand &>(*Operands.back()); 5201 5202 Operands.pop_back(); 5203 Operands.push_back(MipsOperand::CreateRegPair(Op, S, E, *this)); 5204 return MatchOperand_Success; 5205 } 5206 5207 OperandMatchResultTy 5208 MipsAsmParser::parseMovePRegPair(OperandVector &Operands) { 5209 MCAsmParser &Parser = getParser(); 5210 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands; 5211 SmallVector<unsigned, 10> Regs; 5212 5213 if (Parser.getTok().isNot(AsmToken::Dollar)) 5214 return MatchOperand_ParseFail; 5215 5216 SMLoc S = Parser.getTok().getLoc(); 5217 5218 if (parseAnyRegister(TmpOperands) != MatchOperand_Success) 5219 return MatchOperand_ParseFail; 5220 5221 MipsOperand *Reg = &static_cast<MipsOperand &>(*TmpOperands.back()); 5222 unsigned RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg(); 5223 Regs.push_back(RegNo); 5224 5225 SMLoc E = Parser.getTok().getLoc(); 5226 if (Parser.getTok().isNot(AsmToken::Comma)) { 5227 Error(E, "',' expected"); 5228 return MatchOperand_ParseFail; 5229 } 5230 5231 // Remove comma. 5232 Parser.Lex(); 5233 5234 if (parseAnyRegister(TmpOperands) != MatchOperand_Success) 5235 return MatchOperand_ParseFail; 5236 5237 Reg = &static_cast<MipsOperand &>(*TmpOperands.back()); 5238 RegNo = isGP64bit() ? Reg->getGPR64Reg() : Reg->getGPR32Reg(); 5239 Regs.push_back(RegNo); 5240 5241 Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this)); 5242 5243 return MatchOperand_Success; 5244 } 5245 5246 /// Sometimes (i.e. load/stores) the operand may be followed immediately by 5247 /// either this. 5248 /// ::= '(', register, ')' 5249 /// handle it before we iterate so we don't get tripped up by the lack of 5250 /// a comma. 5251 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) { 5252 MCAsmParser &Parser = getParser(); 5253 if (getLexer().is(AsmToken::LParen)) { 5254 Operands.push_back( 5255 MipsOperand::CreateToken("(", getLexer().getLoc(), *this)); 5256 Parser.Lex(); 5257 if (parseOperand(Operands, Name)) { 5258 SMLoc Loc = getLexer().getLoc(); 5259 return Error(Loc, "unexpected token in argument list"); 5260 } 5261 if (Parser.getTok().isNot(AsmToken::RParen)) { 5262 SMLoc Loc = getLexer().getLoc(); 5263 return Error(Loc, "unexpected token, expected ')'"); 5264 } 5265 Operands.push_back( 5266 MipsOperand::CreateToken(")", getLexer().getLoc(), *this)); 5267 Parser.Lex(); 5268 } 5269 return false; 5270 } 5271 5272 /// Sometimes (i.e. in MSA) the operand may be followed immediately by 5273 /// either one of these. 5274 /// ::= '[', register, ']' 5275 /// ::= '[', integer, ']' 5276 /// handle it before we iterate so we don't get tripped up by the lack of 5277 /// a comma. 5278 bool MipsAsmParser::parseBracketSuffix(StringRef Name, 5279 OperandVector &Operands) { 5280 MCAsmParser &Parser = getParser(); 5281 if (getLexer().is(AsmToken::LBrac)) { 5282 Operands.push_back( 5283 MipsOperand::CreateToken("[", getLexer().getLoc(), *this)); 5284 Parser.Lex(); 5285 if (parseOperand(Operands, Name)) { 5286 SMLoc Loc = getLexer().getLoc(); 5287 return Error(Loc, "unexpected token in argument list"); 5288 } 5289 if (Parser.getTok().isNot(AsmToken::RBrac)) { 5290 SMLoc Loc = getLexer().getLoc(); 5291 return Error(Loc, "unexpected token, expected ']'"); 5292 } 5293 Operands.push_back( 5294 MipsOperand::CreateToken("]", getLexer().getLoc(), *this)); 5295 Parser.Lex(); 5296 } 5297 return false; 5298 } 5299 5300 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 5301 SMLoc NameLoc, OperandVector &Operands) { 5302 MCAsmParser &Parser = getParser(); 5303 DEBUG(dbgs() << "ParseInstruction\n"); 5304 5305 // We have reached first instruction, module directive are now forbidden. 5306 getTargetStreamer().forbidModuleDirective(); 5307 5308 // Check if we have valid mnemonic 5309 if (!mnemonicIsValid(Name, 0)) { 5310 return Error(NameLoc, "unknown instruction"); 5311 } 5312 // First operand in MCInst is instruction mnemonic. 5313 Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this)); 5314 5315 // Read the remaining operands. 5316 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5317 // Read the first operand. 5318 if (parseOperand(Operands, Name)) { 5319 SMLoc Loc = getLexer().getLoc(); 5320 return Error(Loc, "unexpected token in argument list"); 5321 } 5322 if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands)) 5323 return true; 5324 // AFAIK, parenthesis suffixes are never on the first operand 5325 5326 while (getLexer().is(AsmToken::Comma)) { 5327 Parser.Lex(); // Eat the comma. 5328 // Parse and remember the operand. 5329 if (parseOperand(Operands, Name)) { 5330 SMLoc Loc = getLexer().getLoc(); 5331 return Error(Loc, "unexpected token in argument list"); 5332 } 5333 // Parse bracket and parenthesis suffixes before we iterate 5334 if (getLexer().is(AsmToken::LBrac)) { 5335 if (parseBracketSuffix(Name, Operands)) 5336 return true; 5337 } else if (getLexer().is(AsmToken::LParen) && 5338 parseParenSuffix(Name, Operands)) 5339 return true; 5340 } 5341 } 5342 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5343 SMLoc Loc = getLexer().getLoc(); 5344 return Error(Loc, "unexpected token in argument list"); 5345 } 5346 Parser.Lex(); // Consume the EndOfStatement. 5347 return false; 5348 } 5349 5350 // FIXME: Given that these have the same name, these should both be 5351 // consistent on affecting the Parser. 5352 bool MipsAsmParser::reportParseError(Twine ErrorMsg) { 5353 SMLoc Loc = getLexer().getLoc(); 5354 return Error(Loc, ErrorMsg); 5355 } 5356 5357 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) { 5358 return Error(Loc, ErrorMsg); 5359 } 5360 5361 bool MipsAsmParser::parseSetNoAtDirective() { 5362 MCAsmParser &Parser = getParser(); 5363 // Line should look like: ".set noat". 5364 5365 // Set the $at register to $0. 5366 AssemblerOptions.back()->setATRegIndex(0); 5367 5368 Parser.Lex(); // Eat "noat". 5369 5370 // If this is not the end of the statement, report an error. 5371 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5372 reportParseError("unexpected token, expected end of statement"); 5373 return false; 5374 } 5375 5376 getTargetStreamer().emitDirectiveSetNoAt(); 5377 Parser.Lex(); // Consume the EndOfStatement. 5378 return false; 5379 } 5380 5381 bool MipsAsmParser::parseSetAtDirective() { 5382 // Line can be: ".set at", which sets $at to $1 5383 // or ".set at=$reg", which sets $at to $reg. 5384 MCAsmParser &Parser = getParser(); 5385 Parser.Lex(); // Eat "at". 5386 5387 if (getLexer().is(AsmToken::EndOfStatement)) { 5388 // No register was specified, so we set $at to $1. 5389 AssemblerOptions.back()->setATRegIndex(1); 5390 5391 getTargetStreamer().emitDirectiveSetAt(); 5392 Parser.Lex(); // Consume the EndOfStatement. 5393 return false; 5394 } 5395 5396 if (getLexer().isNot(AsmToken::Equal)) { 5397 reportParseError("unexpected token, expected equals sign"); 5398 return false; 5399 } 5400 Parser.Lex(); // Eat "=". 5401 5402 if (getLexer().isNot(AsmToken::Dollar)) { 5403 if (getLexer().is(AsmToken::EndOfStatement)) { 5404 reportParseError("no register specified"); 5405 return false; 5406 } else { 5407 reportParseError("unexpected token, expected dollar sign '$'"); 5408 return false; 5409 } 5410 } 5411 Parser.Lex(); // Eat "$". 5412 5413 // Find out what "reg" is. 5414 unsigned AtRegNo; 5415 const AsmToken &Reg = Parser.getTok(); 5416 if (Reg.is(AsmToken::Identifier)) { 5417 AtRegNo = matchCPURegisterName(Reg.getIdentifier()); 5418 } else if (Reg.is(AsmToken::Integer)) { 5419 AtRegNo = Reg.getIntVal(); 5420 } else { 5421 reportParseError("unexpected token, expected identifier or integer"); 5422 return false; 5423 } 5424 5425 // Check if $reg is a valid register. If it is, set $at to $reg. 5426 if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) { 5427 reportParseError("invalid register"); 5428 return false; 5429 } 5430 Parser.Lex(); // Eat "reg". 5431 5432 // If this is not the end of the statement, report an error. 5433 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5434 reportParseError("unexpected token, expected end of statement"); 5435 return false; 5436 } 5437 5438 getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo); 5439 5440 Parser.Lex(); // Consume the EndOfStatement. 5441 return false; 5442 } 5443 5444 bool MipsAsmParser::parseSetReorderDirective() { 5445 MCAsmParser &Parser = getParser(); 5446 Parser.Lex(); 5447 // If this is not the end of the statement, report an error. 5448 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5449 reportParseError("unexpected token, expected end of statement"); 5450 return false; 5451 } 5452 AssemblerOptions.back()->setReorder(); 5453 getTargetStreamer().emitDirectiveSetReorder(); 5454 Parser.Lex(); // Consume the EndOfStatement. 5455 return false; 5456 } 5457 5458 bool MipsAsmParser::parseSetNoReorderDirective() { 5459 MCAsmParser &Parser = getParser(); 5460 Parser.Lex(); 5461 // If this is not the end of the statement, report an error. 5462 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5463 reportParseError("unexpected token, expected end of statement"); 5464 return false; 5465 } 5466 AssemblerOptions.back()->setNoReorder(); 5467 getTargetStreamer().emitDirectiveSetNoReorder(); 5468 Parser.Lex(); // Consume the EndOfStatement. 5469 return false; 5470 } 5471 5472 bool MipsAsmParser::parseSetMacroDirective() { 5473 MCAsmParser &Parser = getParser(); 5474 Parser.Lex(); 5475 // If this is not the end of the statement, report an error. 5476 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5477 reportParseError("unexpected token, expected end of statement"); 5478 return false; 5479 } 5480 AssemblerOptions.back()->setMacro(); 5481 getTargetStreamer().emitDirectiveSetMacro(); 5482 Parser.Lex(); // Consume the EndOfStatement. 5483 return false; 5484 } 5485 5486 bool MipsAsmParser::parseSetNoMacroDirective() { 5487 MCAsmParser &Parser = getParser(); 5488 Parser.Lex(); 5489 // If this is not the end of the statement, report an error. 5490 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5491 reportParseError("unexpected token, expected end of statement"); 5492 return false; 5493 } 5494 if (AssemblerOptions.back()->isReorder()) { 5495 reportParseError("`noreorder' must be set before `nomacro'"); 5496 return false; 5497 } 5498 AssemblerOptions.back()->setNoMacro(); 5499 getTargetStreamer().emitDirectiveSetNoMacro(); 5500 Parser.Lex(); // Consume the EndOfStatement. 5501 return false; 5502 } 5503 5504 bool MipsAsmParser::parseSetMsaDirective() { 5505 MCAsmParser &Parser = getParser(); 5506 Parser.Lex(); 5507 5508 // If this is not the end of the statement, report an error. 5509 if (getLexer().isNot(AsmToken::EndOfStatement)) 5510 return reportParseError("unexpected token, expected end of statement"); 5511 5512 setFeatureBits(Mips::FeatureMSA, "msa"); 5513 getTargetStreamer().emitDirectiveSetMsa(); 5514 return false; 5515 } 5516 5517 bool MipsAsmParser::parseSetNoMsaDirective() { 5518 MCAsmParser &Parser = getParser(); 5519 Parser.Lex(); 5520 5521 // If this is not the end of the statement, report an error. 5522 if (getLexer().isNot(AsmToken::EndOfStatement)) 5523 return reportParseError("unexpected token, expected end of statement"); 5524 5525 clearFeatureBits(Mips::FeatureMSA, "msa"); 5526 getTargetStreamer().emitDirectiveSetNoMsa(); 5527 return false; 5528 } 5529 5530 bool MipsAsmParser::parseSetNoDspDirective() { 5531 MCAsmParser &Parser = getParser(); 5532 Parser.Lex(); // Eat "nodsp". 5533 5534 // If this is not the end of the statement, report an error. 5535 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5536 reportParseError("unexpected token, expected end of statement"); 5537 return false; 5538 } 5539 5540 clearFeatureBits(Mips::FeatureDSP, "dsp"); 5541 getTargetStreamer().emitDirectiveSetNoDsp(); 5542 return false; 5543 } 5544 5545 bool MipsAsmParser::parseSetMips16Directive() { 5546 MCAsmParser &Parser = getParser(); 5547 Parser.Lex(); // Eat "mips16". 5548 5549 // If this is not the end of the statement, report an error. 5550 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5551 reportParseError("unexpected token, expected end of statement"); 5552 return false; 5553 } 5554 5555 setFeatureBits(Mips::FeatureMips16, "mips16"); 5556 getTargetStreamer().emitDirectiveSetMips16(); 5557 Parser.Lex(); // Consume the EndOfStatement. 5558 return false; 5559 } 5560 5561 bool MipsAsmParser::parseSetNoMips16Directive() { 5562 MCAsmParser &Parser = getParser(); 5563 Parser.Lex(); // Eat "nomips16". 5564 5565 // If this is not the end of the statement, report an error. 5566 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5567 reportParseError("unexpected token, expected end of statement"); 5568 return false; 5569 } 5570 5571 clearFeatureBits(Mips::FeatureMips16, "mips16"); 5572 getTargetStreamer().emitDirectiveSetNoMips16(); 5573 Parser.Lex(); // Consume the EndOfStatement. 5574 return false; 5575 } 5576 5577 bool MipsAsmParser::parseSetFpDirective() { 5578 MCAsmParser &Parser = getParser(); 5579 MipsABIFlagsSection::FpABIKind FpAbiVal; 5580 // Line can be: .set fp=32 5581 // .set fp=xx 5582 // .set fp=64 5583 Parser.Lex(); // Eat fp token 5584 AsmToken Tok = Parser.getTok(); 5585 if (Tok.isNot(AsmToken::Equal)) { 5586 reportParseError("unexpected token, expected equals sign '='"); 5587 return false; 5588 } 5589 Parser.Lex(); // Eat '=' token. 5590 Tok = Parser.getTok(); 5591 5592 if (!parseFpABIValue(FpAbiVal, ".set")) 5593 return false; 5594 5595 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5596 reportParseError("unexpected token, expected end of statement"); 5597 return false; 5598 } 5599 getTargetStreamer().emitDirectiveSetFp(FpAbiVal); 5600 Parser.Lex(); // Consume the EndOfStatement. 5601 return false; 5602 } 5603 5604 bool MipsAsmParser::parseSetOddSPRegDirective() { 5605 MCAsmParser &Parser = getParser(); 5606 5607 Parser.Lex(); // Eat "oddspreg". 5608 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5609 reportParseError("unexpected token, expected end of statement"); 5610 return false; 5611 } 5612 5613 clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 5614 getTargetStreamer().emitDirectiveSetOddSPReg(); 5615 return false; 5616 } 5617 5618 bool MipsAsmParser::parseSetNoOddSPRegDirective() { 5619 MCAsmParser &Parser = getParser(); 5620 5621 Parser.Lex(); // Eat "nooddspreg". 5622 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5623 reportParseError("unexpected token, expected end of statement"); 5624 return false; 5625 } 5626 5627 setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 5628 getTargetStreamer().emitDirectiveSetNoOddSPReg(); 5629 return false; 5630 } 5631 5632 bool MipsAsmParser::parseSetPopDirective() { 5633 MCAsmParser &Parser = getParser(); 5634 SMLoc Loc = getLexer().getLoc(); 5635 5636 Parser.Lex(); 5637 if (getLexer().isNot(AsmToken::EndOfStatement)) 5638 return reportParseError("unexpected token, expected end of statement"); 5639 5640 // Always keep an element on the options "stack" to prevent the user 5641 // from changing the initial options. This is how we remember them. 5642 if (AssemblerOptions.size() == 2) 5643 return reportParseError(Loc, ".set pop with no .set push"); 5644 5645 MCSubtargetInfo &STI = copySTI(); 5646 AssemblerOptions.pop_back(); 5647 setAvailableFeatures( 5648 ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures())); 5649 STI.setFeatureBits(AssemblerOptions.back()->getFeatures()); 5650 5651 getTargetStreamer().emitDirectiveSetPop(); 5652 return false; 5653 } 5654 5655 bool MipsAsmParser::parseSetPushDirective() { 5656 MCAsmParser &Parser = getParser(); 5657 Parser.Lex(); 5658 if (getLexer().isNot(AsmToken::EndOfStatement)) 5659 return reportParseError("unexpected token, expected end of statement"); 5660 5661 // Create a copy of the current assembler options environment and push it. 5662 AssemblerOptions.push_back( 5663 llvm::make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get())); 5664 5665 getTargetStreamer().emitDirectiveSetPush(); 5666 return false; 5667 } 5668 5669 bool MipsAsmParser::parseSetSoftFloatDirective() { 5670 MCAsmParser &Parser = getParser(); 5671 Parser.Lex(); 5672 if (getLexer().isNot(AsmToken::EndOfStatement)) 5673 return reportParseError("unexpected token, expected end of statement"); 5674 5675 setFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 5676 getTargetStreamer().emitDirectiveSetSoftFloat(); 5677 return false; 5678 } 5679 5680 bool MipsAsmParser::parseSetHardFloatDirective() { 5681 MCAsmParser &Parser = getParser(); 5682 Parser.Lex(); 5683 if (getLexer().isNot(AsmToken::EndOfStatement)) 5684 return reportParseError("unexpected token, expected end of statement"); 5685 5686 clearFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 5687 getTargetStreamer().emitDirectiveSetHardFloat(); 5688 return false; 5689 } 5690 5691 bool MipsAsmParser::parseSetAssignment() { 5692 StringRef Name; 5693 const MCExpr *Value; 5694 MCAsmParser &Parser = getParser(); 5695 5696 if (Parser.parseIdentifier(Name)) 5697 reportParseError("expected identifier after .set"); 5698 5699 if (getLexer().isNot(AsmToken::Comma)) 5700 return reportParseError("unexpected token, expected comma"); 5701 Lex(); // Eat comma 5702 5703 if (Parser.parseExpression(Value)) 5704 return reportParseError("expected valid expression after comma"); 5705 5706 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 5707 Sym->setVariableValue(Value); 5708 5709 return false; 5710 } 5711 5712 bool MipsAsmParser::parseSetMips0Directive() { 5713 MCAsmParser &Parser = getParser(); 5714 Parser.Lex(); 5715 if (getLexer().isNot(AsmToken::EndOfStatement)) 5716 return reportParseError("unexpected token, expected end of statement"); 5717 5718 // Reset assembler options to their initial values. 5719 MCSubtargetInfo &STI = copySTI(); 5720 setAvailableFeatures( 5721 ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures())); 5722 STI.setFeatureBits(AssemblerOptions.front()->getFeatures()); 5723 AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures()); 5724 5725 getTargetStreamer().emitDirectiveSetMips0(); 5726 return false; 5727 } 5728 5729 bool MipsAsmParser::parseSetArchDirective() { 5730 MCAsmParser &Parser = getParser(); 5731 Parser.Lex(); 5732 if (getLexer().isNot(AsmToken::Equal)) 5733 return reportParseError("unexpected token, expected equals sign"); 5734 5735 Parser.Lex(); 5736 StringRef Arch; 5737 if (Parser.parseIdentifier(Arch)) 5738 return reportParseError("expected arch identifier"); 5739 5740 StringRef ArchFeatureName = 5741 StringSwitch<StringRef>(Arch) 5742 .Case("mips1", "mips1") 5743 .Case("mips2", "mips2") 5744 .Case("mips3", "mips3") 5745 .Case("mips4", "mips4") 5746 .Case("mips5", "mips5") 5747 .Case("mips32", "mips32") 5748 .Case("mips32r2", "mips32r2") 5749 .Case("mips32r3", "mips32r3") 5750 .Case("mips32r5", "mips32r5") 5751 .Case("mips32r6", "mips32r6") 5752 .Case("mips64", "mips64") 5753 .Case("mips64r2", "mips64r2") 5754 .Case("mips64r3", "mips64r3") 5755 .Case("mips64r5", "mips64r5") 5756 .Case("mips64r6", "mips64r6") 5757 .Case("octeon", "cnmips") 5758 .Case("r4000", "mips3") // This is an implementation of Mips3. 5759 .Default(""); 5760 5761 if (ArchFeatureName.empty()) 5762 return reportParseError("unsupported architecture"); 5763 5764 selectArch(ArchFeatureName); 5765 getTargetStreamer().emitDirectiveSetArch(Arch); 5766 return false; 5767 } 5768 5769 bool MipsAsmParser::parseSetFeature(uint64_t Feature) { 5770 MCAsmParser &Parser = getParser(); 5771 Parser.Lex(); 5772 if (getLexer().isNot(AsmToken::EndOfStatement)) 5773 return reportParseError("unexpected token, expected end of statement"); 5774 5775 switch (Feature) { 5776 default: 5777 llvm_unreachable("Unimplemented feature"); 5778 case Mips::FeatureDSP: 5779 setFeatureBits(Mips::FeatureDSP, "dsp"); 5780 getTargetStreamer().emitDirectiveSetDsp(); 5781 break; 5782 case Mips::FeatureMicroMips: 5783 setFeatureBits(Mips::FeatureMicroMips, "micromips"); 5784 getTargetStreamer().emitDirectiveSetMicroMips(); 5785 break; 5786 case Mips::FeatureMips1: 5787 selectArch("mips1"); 5788 getTargetStreamer().emitDirectiveSetMips1(); 5789 break; 5790 case Mips::FeatureMips2: 5791 selectArch("mips2"); 5792 getTargetStreamer().emitDirectiveSetMips2(); 5793 break; 5794 case Mips::FeatureMips3: 5795 selectArch("mips3"); 5796 getTargetStreamer().emitDirectiveSetMips3(); 5797 break; 5798 case Mips::FeatureMips4: 5799 selectArch("mips4"); 5800 getTargetStreamer().emitDirectiveSetMips4(); 5801 break; 5802 case Mips::FeatureMips5: 5803 selectArch("mips5"); 5804 getTargetStreamer().emitDirectiveSetMips5(); 5805 break; 5806 case Mips::FeatureMips32: 5807 selectArch("mips32"); 5808 getTargetStreamer().emitDirectiveSetMips32(); 5809 break; 5810 case Mips::FeatureMips32r2: 5811 selectArch("mips32r2"); 5812 getTargetStreamer().emitDirectiveSetMips32R2(); 5813 break; 5814 case Mips::FeatureMips32r3: 5815 selectArch("mips32r3"); 5816 getTargetStreamer().emitDirectiveSetMips32R3(); 5817 break; 5818 case Mips::FeatureMips32r5: 5819 selectArch("mips32r5"); 5820 getTargetStreamer().emitDirectiveSetMips32R5(); 5821 break; 5822 case Mips::FeatureMips32r6: 5823 selectArch("mips32r6"); 5824 getTargetStreamer().emitDirectiveSetMips32R6(); 5825 break; 5826 case Mips::FeatureMips64: 5827 selectArch("mips64"); 5828 getTargetStreamer().emitDirectiveSetMips64(); 5829 break; 5830 case Mips::FeatureMips64r2: 5831 selectArch("mips64r2"); 5832 getTargetStreamer().emitDirectiveSetMips64R2(); 5833 break; 5834 case Mips::FeatureMips64r3: 5835 selectArch("mips64r3"); 5836 getTargetStreamer().emitDirectiveSetMips64R3(); 5837 break; 5838 case Mips::FeatureMips64r5: 5839 selectArch("mips64r5"); 5840 getTargetStreamer().emitDirectiveSetMips64R5(); 5841 break; 5842 case Mips::FeatureMips64r6: 5843 selectArch("mips64r6"); 5844 getTargetStreamer().emitDirectiveSetMips64R6(); 5845 break; 5846 } 5847 return false; 5848 } 5849 5850 bool MipsAsmParser::eatComma(StringRef ErrorStr) { 5851 MCAsmParser &Parser = getParser(); 5852 if (getLexer().isNot(AsmToken::Comma)) { 5853 SMLoc Loc = getLexer().getLoc(); 5854 return Error(Loc, ErrorStr); 5855 } 5856 5857 Parser.Lex(); // Eat the comma. 5858 return true; 5859 } 5860 5861 // Used to determine if .cpload, .cprestore, and .cpsetup have any effect. 5862 // In this class, it is only used for .cprestore. 5863 // FIXME: Only keep track of IsPicEnabled in one place, instead of in both 5864 // MipsTargetELFStreamer and MipsAsmParser. 5865 bool MipsAsmParser::isPicAndNotNxxAbi() { 5866 return inPicMode() && !(isABI_N32() || isABI_N64()); 5867 } 5868 5869 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) { 5870 if (AssemblerOptions.back()->isReorder()) 5871 Warning(Loc, ".cpload should be inside a noreorder section"); 5872 5873 if (inMips16Mode()) { 5874 reportParseError(".cpload is not supported in Mips16 mode"); 5875 return false; 5876 } 5877 5878 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg; 5879 OperandMatchResultTy ResTy = parseAnyRegister(Reg); 5880 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 5881 reportParseError("expected register containing function address"); 5882 return false; 5883 } 5884 5885 MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]); 5886 if (!RegOpnd.isGPRAsmReg()) { 5887 reportParseError(RegOpnd.getStartLoc(), "invalid register"); 5888 return false; 5889 } 5890 5891 // If this is not the end of the statement, report an error. 5892 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5893 reportParseError("unexpected token, expected end of statement"); 5894 return false; 5895 } 5896 5897 getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg()); 5898 return false; 5899 } 5900 5901 bool MipsAsmParser::parseDirectiveCpRestore(SMLoc Loc) { 5902 MCAsmParser &Parser = getParser(); 5903 5904 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it 5905 // is used in non-PIC mode. 5906 5907 if (inMips16Mode()) { 5908 reportParseError(".cprestore is not supported in Mips16 mode"); 5909 return false; 5910 } 5911 5912 // Get the stack offset value. 5913 const MCExpr *StackOffset; 5914 int64_t StackOffsetVal; 5915 if (Parser.parseExpression(StackOffset)) { 5916 reportParseError("expected stack offset value"); 5917 return false; 5918 } 5919 5920 if (!StackOffset->evaluateAsAbsolute(StackOffsetVal)) { 5921 reportParseError("stack offset is not an absolute expression"); 5922 return false; 5923 } 5924 5925 if (StackOffsetVal < 0) { 5926 Warning(Loc, ".cprestore with negative stack offset has no effect"); 5927 IsCpRestoreSet = false; 5928 } else { 5929 IsCpRestoreSet = true; 5930 CpRestoreOffset = StackOffsetVal; 5931 } 5932 5933 // If this is not the end of the statement, report an error. 5934 if (getLexer().isNot(AsmToken::EndOfStatement)) { 5935 reportParseError("unexpected token, expected end of statement"); 5936 return false; 5937 } 5938 5939 if (!getTargetStreamer().emitDirectiveCpRestore( 5940 CpRestoreOffset, [&]() { return getATReg(Loc); }, Loc, STI)) 5941 return true; 5942 Parser.Lex(); // Consume the EndOfStatement. 5943 return false; 5944 } 5945 5946 bool MipsAsmParser::parseDirectiveCPSetup() { 5947 MCAsmParser &Parser = getParser(); 5948 unsigned FuncReg; 5949 unsigned Save; 5950 bool SaveIsReg = true; 5951 5952 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg; 5953 OperandMatchResultTy ResTy = parseAnyRegister(TmpReg); 5954 if (ResTy == MatchOperand_NoMatch) { 5955 reportParseError("expected register containing function address"); 5956 return false; 5957 } 5958 5959 MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 5960 if (!FuncRegOpnd.isGPRAsmReg()) { 5961 reportParseError(FuncRegOpnd.getStartLoc(), "invalid register"); 5962 return false; 5963 } 5964 5965 FuncReg = FuncRegOpnd.getGPR32Reg(); 5966 TmpReg.clear(); 5967 5968 if (!eatComma("unexpected token, expected comma")) 5969 return true; 5970 5971 ResTy = parseAnyRegister(TmpReg); 5972 if (ResTy == MatchOperand_NoMatch) { 5973 const MCExpr *OffsetExpr; 5974 int64_t OffsetVal; 5975 SMLoc ExprLoc = getLexer().getLoc(); 5976 5977 if (Parser.parseExpression(OffsetExpr) || 5978 !OffsetExpr->evaluateAsAbsolute(OffsetVal)) { 5979 reportParseError(ExprLoc, "expected save register or stack offset"); 5980 return false; 5981 } 5982 5983 Save = OffsetVal; 5984 SaveIsReg = false; 5985 } else { 5986 MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 5987 if (!SaveOpnd.isGPRAsmReg()) { 5988 reportParseError(SaveOpnd.getStartLoc(), "invalid register"); 5989 return false; 5990 } 5991 Save = SaveOpnd.getGPR32Reg(); 5992 } 5993 5994 if (!eatComma("unexpected token, expected comma")) 5995 return true; 5996 5997 const MCExpr *Expr; 5998 if (Parser.parseExpression(Expr)) { 5999 reportParseError("expected expression"); 6000 return false; 6001 } 6002 6003 if (Expr->getKind() != MCExpr::SymbolRef) { 6004 reportParseError("expected symbol"); 6005 return false; 6006 } 6007 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr); 6008 6009 CpSaveLocation = Save; 6010 CpSaveLocationIsRegister = SaveIsReg; 6011 6012 getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(), 6013 SaveIsReg); 6014 return false; 6015 } 6016 6017 bool MipsAsmParser::parseDirectiveCPReturn() { 6018 getTargetStreamer().emitDirectiveCpreturn(CpSaveLocation, 6019 CpSaveLocationIsRegister); 6020 return false; 6021 } 6022 6023 bool MipsAsmParser::parseDirectiveNaN() { 6024 MCAsmParser &Parser = getParser(); 6025 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6026 const AsmToken &Tok = Parser.getTok(); 6027 6028 if (Tok.getString() == "2008") { 6029 Parser.Lex(); 6030 getTargetStreamer().emitDirectiveNaN2008(); 6031 return false; 6032 } else if (Tok.getString() == "legacy") { 6033 Parser.Lex(); 6034 getTargetStreamer().emitDirectiveNaNLegacy(); 6035 return false; 6036 } 6037 } 6038 // If we don't recognize the option passed to the .nan 6039 // directive (e.g. no option or unknown option), emit an error. 6040 reportParseError("invalid option in .nan directive"); 6041 return false; 6042 } 6043 6044 bool MipsAsmParser::parseDirectiveSet() { 6045 MCAsmParser &Parser = getParser(); 6046 // Get the next token. 6047 const AsmToken &Tok = Parser.getTok(); 6048 6049 if (Tok.getString() == "noat") { 6050 return parseSetNoAtDirective(); 6051 } else if (Tok.getString() == "at") { 6052 return parseSetAtDirective(); 6053 } else if (Tok.getString() == "arch") { 6054 return parseSetArchDirective(); 6055 } else if (Tok.getString() == "bopt") { 6056 Warning(Tok.getLoc(), "'bopt' feature is unsupported"); 6057 getParser().Lex(); 6058 return false; 6059 } else if (Tok.getString() == "nobopt") { 6060 // We're already running in nobopt mode, so nothing to do. 6061 getParser().Lex(); 6062 return false; 6063 } else if (Tok.getString() == "fp") { 6064 return parseSetFpDirective(); 6065 } else if (Tok.getString() == "oddspreg") { 6066 return parseSetOddSPRegDirective(); 6067 } else if (Tok.getString() == "nooddspreg") { 6068 return parseSetNoOddSPRegDirective(); 6069 } else if (Tok.getString() == "pop") { 6070 return parseSetPopDirective(); 6071 } else if (Tok.getString() == "push") { 6072 return parseSetPushDirective(); 6073 } else if (Tok.getString() == "reorder") { 6074 return parseSetReorderDirective(); 6075 } else if (Tok.getString() == "noreorder") { 6076 return parseSetNoReorderDirective(); 6077 } else if (Tok.getString() == "macro") { 6078 return parseSetMacroDirective(); 6079 } else if (Tok.getString() == "nomacro") { 6080 return parseSetNoMacroDirective(); 6081 } else if (Tok.getString() == "mips16") { 6082 return parseSetMips16Directive(); 6083 } else if (Tok.getString() == "nomips16") { 6084 return parseSetNoMips16Directive(); 6085 } else if (Tok.getString() == "nomicromips") { 6086 clearFeatureBits(Mips::FeatureMicroMips, "micromips"); 6087 getTargetStreamer().emitDirectiveSetNoMicroMips(); 6088 Parser.eatToEndOfStatement(); 6089 return false; 6090 } else if (Tok.getString() == "micromips") { 6091 return parseSetFeature(Mips::FeatureMicroMips); 6092 } else if (Tok.getString() == "mips0") { 6093 return parseSetMips0Directive(); 6094 } else if (Tok.getString() == "mips1") { 6095 return parseSetFeature(Mips::FeatureMips1); 6096 } else if (Tok.getString() == "mips2") { 6097 return parseSetFeature(Mips::FeatureMips2); 6098 } else if (Tok.getString() == "mips3") { 6099 return parseSetFeature(Mips::FeatureMips3); 6100 } else if (Tok.getString() == "mips4") { 6101 return parseSetFeature(Mips::FeatureMips4); 6102 } else if (Tok.getString() == "mips5") { 6103 return parseSetFeature(Mips::FeatureMips5); 6104 } else if (Tok.getString() == "mips32") { 6105 return parseSetFeature(Mips::FeatureMips32); 6106 } else if (Tok.getString() == "mips32r2") { 6107 return parseSetFeature(Mips::FeatureMips32r2); 6108 } else if (Tok.getString() == "mips32r3") { 6109 return parseSetFeature(Mips::FeatureMips32r3); 6110 } else if (Tok.getString() == "mips32r5") { 6111 return parseSetFeature(Mips::FeatureMips32r5); 6112 } else if (Tok.getString() == "mips32r6") { 6113 return parseSetFeature(Mips::FeatureMips32r6); 6114 } else if (Tok.getString() == "mips64") { 6115 return parseSetFeature(Mips::FeatureMips64); 6116 } else if (Tok.getString() == "mips64r2") { 6117 return parseSetFeature(Mips::FeatureMips64r2); 6118 } else if (Tok.getString() == "mips64r3") { 6119 return parseSetFeature(Mips::FeatureMips64r3); 6120 } else if (Tok.getString() == "mips64r5") { 6121 return parseSetFeature(Mips::FeatureMips64r5); 6122 } else if (Tok.getString() == "mips64r6") { 6123 return parseSetFeature(Mips::FeatureMips64r6); 6124 } else if (Tok.getString() == "dsp") { 6125 return parseSetFeature(Mips::FeatureDSP); 6126 } else if (Tok.getString() == "nodsp") { 6127 return parseSetNoDspDirective(); 6128 } else if (Tok.getString() == "msa") { 6129 return parseSetMsaDirective(); 6130 } else if (Tok.getString() == "nomsa") { 6131 return parseSetNoMsaDirective(); 6132 } else if (Tok.getString() == "softfloat") { 6133 return parseSetSoftFloatDirective(); 6134 } else if (Tok.getString() == "hardfloat") { 6135 return parseSetHardFloatDirective(); 6136 } else { 6137 // It is just an identifier, look for an assignment. 6138 parseSetAssignment(); 6139 return false; 6140 } 6141 6142 return true; 6143 } 6144 6145 /// parseDataDirective 6146 /// ::= .word [ expression (, expression)* ] 6147 bool MipsAsmParser::parseDataDirective(unsigned Size, SMLoc L) { 6148 MCAsmParser &Parser = getParser(); 6149 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6150 while (true) { 6151 const MCExpr *Value; 6152 if (getParser().parseExpression(Value)) 6153 return true; 6154 6155 getParser().getStreamer().EmitValue(Value, Size); 6156 6157 if (getLexer().is(AsmToken::EndOfStatement)) 6158 break; 6159 6160 if (getLexer().isNot(AsmToken::Comma)) 6161 return Error(L, "unexpected token, expected comma"); 6162 Parser.Lex(); 6163 } 6164 } 6165 6166 Parser.Lex(); 6167 return false; 6168 } 6169 6170 /// parseDirectiveGpWord 6171 /// ::= .gpword local_sym 6172 bool MipsAsmParser::parseDirectiveGpWord() { 6173 MCAsmParser &Parser = getParser(); 6174 const MCExpr *Value; 6175 // EmitGPRel32Value requires an expression, so we are using base class 6176 // method to evaluate the expression. 6177 if (getParser().parseExpression(Value)) 6178 return true; 6179 getParser().getStreamer().EmitGPRel32Value(Value); 6180 6181 if (getLexer().isNot(AsmToken::EndOfStatement)) 6182 return Error(getLexer().getLoc(), 6183 "unexpected token, expected end of statement"); 6184 Parser.Lex(); // Eat EndOfStatement token. 6185 return false; 6186 } 6187 6188 /// parseDirectiveGpDWord 6189 /// ::= .gpdword local_sym 6190 bool MipsAsmParser::parseDirectiveGpDWord() { 6191 MCAsmParser &Parser = getParser(); 6192 const MCExpr *Value; 6193 // EmitGPRel64Value requires an expression, so we are using base class 6194 // method to evaluate the expression. 6195 if (getParser().parseExpression(Value)) 6196 return true; 6197 getParser().getStreamer().EmitGPRel64Value(Value); 6198 6199 if (getLexer().isNot(AsmToken::EndOfStatement)) 6200 return Error(getLexer().getLoc(), 6201 "unexpected token, expected end of statement"); 6202 Parser.Lex(); // Eat EndOfStatement token. 6203 return false; 6204 } 6205 6206 /// parseDirectiveDtpRelWord 6207 /// ::= .dtprelword tls_sym 6208 bool MipsAsmParser::parseDirectiveDtpRelWord() { 6209 MCAsmParser &Parser = getParser(); 6210 const MCExpr *Value; 6211 // EmitDTPRel32Value requires an expression, so we are using base class 6212 // method to evaluate the expression. 6213 if (getParser().parseExpression(Value)) 6214 return true; 6215 getParser().getStreamer().EmitDTPRel32Value(Value); 6216 6217 if (getLexer().isNot(AsmToken::EndOfStatement)) 6218 return Error(getLexer().getLoc(), 6219 "unexpected token, expected end of statement"); 6220 Parser.Lex(); // Eat EndOfStatement token. 6221 return false; 6222 } 6223 6224 /// parseDirectiveDtpRelDWord 6225 /// ::= .dtpreldword tls_sym 6226 bool MipsAsmParser::parseDirectiveDtpRelDWord() { 6227 MCAsmParser &Parser = getParser(); 6228 const MCExpr *Value; 6229 // EmitDTPRel64Value requires an expression, so we are using base class 6230 // method to evaluate the expression. 6231 if (getParser().parseExpression(Value)) 6232 return true; 6233 getParser().getStreamer().EmitDTPRel64Value(Value); 6234 6235 if (getLexer().isNot(AsmToken::EndOfStatement)) 6236 return Error(getLexer().getLoc(), 6237 "unexpected token, expected end of statement"); 6238 Parser.Lex(); // Eat EndOfStatement token. 6239 return false; 6240 } 6241 6242 /// parseDirectiveTpRelWord 6243 /// ::= .tprelword tls_sym 6244 bool MipsAsmParser::parseDirectiveTpRelWord() { 6245 MCAsmParser &Parser = getParser(); 6246 const MCExpr *Value; 6247 // EmitTPRel32Value requires an expression, so we are using base class 6248 // method to evaluate the expression. 6249 if (getParser().parseExpression(Value)) 6250 return true; 6251 getParser().getStreamer().EmitTPRel32Value(Value); 6252 6253 if (getLexer().isNot(AsmToken::EndOfStatement)) 6254 return Error(getLexer().getLoc(), 6255 "unexpected token, expected end of statement"); 6256 Parser.Lex(); // Eat EndOfStatement token. 6257 return false; 6258 } 6259 6260 /// parseDirectiveTpRelDWord 6261 /// ::= .tpreldword tls_sym 6262 bool MipsAsmParser::parseDirectiveTpRelDWord() { 6263 MCAsmParser &Parser = getParser(); 6264 const MCExpr *Value; 6265 // EmitTPRel64Value requires an expression, so we are using base class 6266 // method to evaluate the expression. 6267 if (getParser().parseExpression(Value)) 6268 return true; 6269 getParser().getStreamer().EmitTPRel64Value(Value); 6270 6271 if (getLexer().isNot(AsmToken::EndOfStatement)) 6272 return Error(getLexer().getLoc(), 6273 "unexpected token, expected end of statement"); 6274 Parser.Lex(); // Eat EndOfStatement token. 6275 return false; 6276 } 6277 6278 bool MipsAsmParser::parseDirectiveOption() { 6279 MCAsmParser &Parser = getParser(); 6280 // Get the option token. 6281 AsmToken Tok = Parser.getTok(); 6282 // At the moment only identifiers are supported. 6283 if (Tok.isNot(AsmToken::Identifier)) { 6284 return Error(Parser.getTok().getLoc(), 6285 "unexpected token, expected identifier"); 6286 } 6287 6288 StringRef Option = Tok.getIdentifier(); 6289 6290 if (Option == "pic0") { 6291 // MipsAsmParser needs to know if the current PIC mode changes. 6292 IsPicEnabled = false; 6293 6294 getTargetStreamer().emitDirectiveOptionPic0(); 6295 Parser.Lex(); 6296 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 6297 return Error(Parser.getTok().getLoc(), 6298 "unexpected token, expected end of statement"); 6299 } 6300 return false; 6301 } 6302 6303 if (Option == "pic2") { 6304 // MipsAsmParser needs to know if the current PIC mode changes. 6305 IsPicEnabled = true; 6306 6307 getTargetStreamer().emitDirectiveOptionPic2(); 6308 Parser.Lex(); 6309 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 6310 return Error(Parser.getTok().getLoc(), 6311 "unexpected token, expected end of statement"); 6312 } 6313 return false; 6314 } 6315 6316 // Unknown option. 6317 Warning(Parser.getTok().getLoc(), 6318 "unknown option, expected 'pic0' or 'pic2'"); 6319 Parser.eatToEndOfStatement(); 6320 return false; 6321 } 6322 6323 /// parseInsnDirective 6324 /// ::= .insn 6325 bool MipsAsmParser::parseInsnDirective() { 6326 // If this is not the end of the statement, report an error. 6327 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6328 reportParseError("unexpected token, expected end of statement"); 6329 return false; 6330 } 6331 6332 // The actual label marking happens in 6333 // MipsELFStreamer::createPendingLabelRelocs(). 6334 getTargetStreamer().emitDirectiveInsn(); 6335 6336 getParser().Lex(); // Eat EndOfStatement token. 6337 return false; 6338 } 6339 6340 /// parseSSectionDirective 6341 /// ::= .sbss 6342 /// ::= .sdata 6343 bool MipsAsmParser::parseSSectionDirective(StringRef Section, unsigned Type) { 6344 // If this is not the end of the statement, report an error. 6345 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6346 reportParseError("unexpected token, expected end of statement"); 6347 return false; 6348 } 6349 6350 MCSection *ELFSection = getContext().getELFSection( 6351 Section, Type, ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL); 6352 getParser().getStreamer().SwitchSection(ELFSection); 6353 6354 getParser().Lex(); // Eat EndOfStatement token. 6355 return false; 6356 } 6357 6358 /// parseDirectiveModule 6359 /// ::= .module oddspreg 6360 /// ::= .module nooddspreg 6361 /// ::= .module fp=value 6362 /// ::= .module softfloat 6363 /// ::= .module hardfloat 6364 bool MipsAsmParser::parseDirectiveModule() { 6365 MCAsmParser &Parser = getParser(); 6366 MCAsmLexer &Lexer = getLexer(); 6367 SMLoc L = Lexer.getLoc(); 6368 6369 if (!getTargetStreamer().isModuleDirectiveAllowed()) { 6370 // TODO : get a better message. 6371 reportParseError(".module directive must appear before any code"); 6372 return false; 6373 } 6374 6375 StringRef Option; 6376 if (Parser.parseIdentifier(Option)) { 6377 reportParseError("expected .module option identifier"); 6378 return false; 6379 } 6380 6381 if (Option == "oddspreg") { 6382 clearModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 6383 6384 // Synchronize the abiflags information with the FeatureBits information we 6385 // changed above. 6386 getTargetStreamer().updateABIInfo(*this); 6387 6388 // If printing assembly, use the recently updated abiflags information. 6389 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 6390 // emitted at the end). 6391 getTargetStreamer().emitDirectiveModuleOddSPReg(); 6392 6393 // If this is not the end of the statement, report an error. 6394 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6395 reportParseError("unexpected token, expected end of statement"); 6396 return false; 6397 } 6398 6399 return false; // parseDirectiveModule has finished successfully. 6400 } else if (Option == "nooddspreg") { 6401 if (!isABI_O32()) { 6402 return Error(L, "'.module nooddspreg' requires the O32 ABI"); 6403 } 6404 6405 setModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 6406 6407 // Synchronize the abiflags information with the FeatureBits information we 6408 // changed above. 6409 getTargetStreamer().updateABIInfo(*this); 6410 6411 // If printing assembly, use the recently updated abiflags information. 6412 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 6413 // emitted at the end). 6414 getTargetStreamer().emitDirectiveModuleOddSPReg(); 6415 6416 // If this is not the end of the statement, report an error. 6417 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6418 reportParseError("unexpected token, expected end of statement"); 6419 return false; 6420 } 6421 6422 return false; // parseDirectiveModule has finished successfully. 6423 } else if (Option == "fp") { 6424 return parseDirectiveModuleFP(); 6425 } else if (Option == "softfloat") { 6426 setModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 6427 6428 // Synchronize the ABI Flags information with the FeatureBits information we 6429 // updated above. 6430 getTargetStreamer().updateABIInfo(*this); 6431 6432 // If printing assembly, use the recently updated ABI Flags information. 6433 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 6434 // emitted later). 6435 getTargetStreamer().emitDirectiveModuleSoftFloat(); 6436 6437 // If this is not the end of the statement, report an error. 6438 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6439 reportParseError("unexpected token, expected end of statement"); 6440 return false; 6441 } 6442 6443 return false; // parseDirectiveModule has finished successfully. 6444 } else if (Option == "hardfloat") { 6445 clearModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 6446 6447 // Synchronize the ABI Flags information with the FeatureBits information we 6448 // updated above. 6449 getTargetStreamer().updateABIInfo(*this); 6450 6451 // If printing assembly, use the recently updated ABI Flags information. 6452 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 6453 // emitted later). 6454 getTargetStreamer().emitDirectiveModuleHardFloat(); 6455 6456 // If this is not the end of the statement, report an error. 6457 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6458 reportParseError("unexpected token, expected end of statement"); 6459 return false; 6460 } 6461 6462 return false; // parseDirectiveModule has finished successfully. 6463 } else { 6464 return Error(L, "'" + Twine(Option) + "' is not a valid .module option."); 6465 } 6466 } 6467 6468 /// parseDirectiveModuleFP 6469 /// ::= =32 6470 /// ::= =xx 6471 /// ::= =64 6472 bool MipsAsmParser::parseDirectiveModuleFP() { 6473 MCAsmParser &Parser = getParser(); 6474 MCAsmLexer &Lexer = getLexer(); 6475 6476 if (Lexer.isNot(AsmToken::Equal)) { 6477 reportParseError("unexpected token, expected equals sign '='"); 6478 return false; 6479 } 6480 Parser.Lex(); // Eat '=' token. 6481 6482 MipsABIFlagsSection::FpABIKind FpABI; 6483 if (!parseFpABIValue(FpABI, ".module")) 6484 return false; 6485 6486 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6487 reportParseError("unexpected token, expected end of statement"); 6488 return false; 6489 } 6490 6491 // Synchronize the abiflags information with the FeatureBits information we 6492 // changed above. 6493 getTargetStreamer().updateABIInfo(*this); 6494 6495 // If printing assembly, use the recently updated abiflags information. 6496 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 6497 // emitted at the end). 6498 getTargetStreamer().emitDirectiveModuleFP(); 6499 6500 Parser.Lex(); // Consume the EndOfStatement. 6501 return false; 6502 } 6503 6504 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI, 6505 StringRef Directive) { 6506 MCAsmParser &Parser = getParser(); 6507 MCAsmLexer &Lexer = getLexer(); 6508 bool ModuleLevelOptions = Directive == ".module"; 6509 6510 if (Lexer.is(AsmToken::Identifier)) { 6511 StringRef Value = Parser.getTok().getString(); 6512 Parser.Lex(); 6513 6514 if (Value != "xx") { 6515 reportParseError("unsupported value, expected 'xx', '32' or '64'"); 6516 return false; 6517 } 6518 6519 if (!isABI_O32()) { 6520 reportParseError("'" + Directive + " fp=xx' requires the O32 ABI"); 6521 return false; 6522 } 6523 6524 FpABI = MipsABIFlagsSection::FpABIKind::XX; 6525 if (ModuleLevelOptions) { 6526 setModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 6527 clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6528 } else { 6529 setFeatureBits(Mips::FeatureFPXX, "fpxx"); 6530 clearFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6531 } 6532 return true; 6533 } 6534 6535 if (Lexer.is(AsmToken::Integer)) { 6536 unsigned Value = Parser.getTok().getIntVal(); 6537 Parser.Lex(); 6538 6539 if (Value != 32 && Value != 64) { 6540 reportParseError("unsupported value, expected 'xx', '32' or '64'"); 6541 return false; 6542 } 6543 6544 if (Value == 32) { 6545 if (!isABI_O32()) { 6546 reportParseError("'" + Directive + " fp=32' requires the O32 ABI"); 6547 return false; 6548 } 6549 6550 FpABI = MipsABIFlagsSection::FpABIKind::S32; 6551 if (ModuleLevelOptions) { 6552 clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 6553 clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6554 } else { 6555 clearFeatureBits(Mips::FeatureFPXX, "fpxx"); 6556 clearFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6557 } 6558 } else { 6559 FpABI = MipsABIFlagsSection::FpABIKind::S64; 6560 if (ModuleLevelOptions) { 6561 clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 6562 setModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6563 } else { 6564 clearFeatureBits(Mips::FeatureFPXX, "fpxx"); 6565 setFeatureBits(Mips::FeatureFP64Bit, "fp64"); 6566 } 6567 } 6568 6569 return true; 6570 } 6571 6572 return false; 6573 } 6574 6575 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) { 6576 // This returns false if this function recognizes the directive 6577 // regardless of whether it is successfully handles or reports an 6578 // error. Otherwise it returns true to give the generic parser a 6579 // chance at recognizing it. 6580 6581 MCAsmParser &Parser = getParser(); 6582 StringRef IDVal = DirectiveID.getString(); 6583 6584 if (IDVal == ".cpload") { 6585 parseDirectiveCpLoad(DirectiveID.getLoc()); 6586 return false; 6587 } 6588 if (IDVal == ".cprestore") { 6589 parseDirectiveCpRestore(DirectiveID.getLoc()); 6590 return false; 6591 } 6592 if (IDVal == ".dword") { 6593 parseDataDirective(8, DirectiveID.getLoc()); 6594 return false; 6595 } 6596 if (IDVal == ".ent") { 6597 StringRef SymbolName; 6598 6599 if (Parser.parseIdentifier(SymbolName)) { 6600 reportParseError("expected identifier after .ent"); 6601 return false; 6602 } 6603 6604 // There's an undocumented extension that allows an integer to 6605 // follow the name of the procedure which AFAICS is ignored by GAS. 6606 // Example: .ent foo,2 6607 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6608 if (getLexer().isNot(AsmToken::Comma)) { 6609 // Even though we accept this undocumented extension for compatibility 6610 // reasons, the additional integer argument does not actually change 6611 // the behaviour of the '.ent' directive, so we would like to discourage 6612 // its use. We do this by not referring to the extended version in 6613 // error messages which are not directly related to its use. 6614 reportParseError("unexpected token, expected end of statement"); 6615 return false; 6616 } 6617 Parser.Lex(); // Eat the comma. 6618 const MCExpr *DummyNumber; 6619 int64_t DummyNumberVal; 6620 // If the user was explicitly trying to use the extended version, 6621 // we still give helpful extension-related error messages. 6622 if (Parser.parseExpression(DummyNumber)) { 6623 reportParseError("expected number after comma"); 6624 return false; 6625 } 6626 if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) { 6627 reportParseError("expected an absolute expression after comma"); 6628 return false; 6629 } 6630 } 6631 6632 // If this is not the end of the statement, report an error. 6633 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6634 reportParseError("unexpected token, expected end of statement"); 6635 return false; 6636 } 6637 6638 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); 6639 6640 getTargetStreamer().emitDirectiveEnt(*Sym); 6641 CurrentFn = Sym; 6642 IsCpRestoreSet = false; 6643 return false; 6644 } 6645 6646 if (IDVal == ".end") { 6647 StringRef SymbolName; 6648 6649 if (Parser.parseIdentifier(SymbolName)) { 6650 reportParseError("expected identifier after .end"); 6651 return false; 6652 } 6653 6654 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6655 reportParseError("unexpected token, expected end of statement"); 6656 return false; 6657 } 6658 6659 if (CurrentFn == nullptr) { 6660 reportParseError(".end used without .ent"); 6661 return false; 6662 } 6663 6664 if ((SymbolName != CurrentFn->getName())) { 6665 reportParseError(".end symbol does not match .ent symbol"); 6666 return false; 6667 } 6668 6669 getTargetStreamer().emitDirectiveEnd(SymbolName); 6670 CurrentFn = nullptr; 6671 IsCpRestoreSet = false; 6672 return false; 6673 } 6674 6675 if (IDVal == ".frame") { 6676 // .frame $stack_reg, frame_size_in_bytes, $return_reg 6677 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg; 6678 OperandMatchResultTy ResTy = parseAnyRegister(TmpReg); 6679 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 6680 reportParseError("expected stack register"); 6681 return false; 6682 } 6683 6684 MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 6685 if (!StackRegOpnd.isGPRAsmReg()) { 6686 reportParseError(StackRegOpnd.getStartLoc(), 6687 "expected general purpose register"); 6688 return false; 6689 } 6690 unsigned StackReg = StackRegOpnd.getGPR32Reg(); 6691 6692 if (Parser.getTok().is(AsmToken::Comma)) 6693 Parser.Lex(); 6694 else { 6695 reportParseError("unexpected token, expected comma"); 6696 return false; 6697 } 6698 6699 // Parse the frame size. 6700 const MCExpr *FrameSize; 6701 int64_t FrameSizeVal; 6702 6703 if (Parser.parseExpression(FrameSize)) { 6704 reportParseError("expected frame size value"); 6705 return false; 6706 } 6707 6708 if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) { 6709 reportParseError("frame size not an absolute expression"); 6710 return false; 6711 } 6712 6713 if (Parser.getTok().is(AsmToken::Comma)) 6714 Parser.Lex(); 6715 else { 6716 reportParseError("unexpected token, expected comma"); 6717 return false; 6718 } 6719 6720 // Parse the return register. 6721 TmpReg.clear(); 6722 ResTy = parseAnyRegister(TmpReg); 6723 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 6724 reportParseError("expected return register"); 6725 return false; 6726 } 6727 6728 MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 6729 if (!ReturnRegOpnd.isGPRAsmReg()) { 6730 reportParseError(ReturnRegOpnd.getStartLoc(), 6731 "expected general purpose register"); 6732 return false; 6733 } 6734 6735 // If this is not the end of the statement, report an error. 6736 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6737 reportParseError("unexpected token, expected end of statement"); 6738 return false; 6739 } 6740 6741 getTargetStreamer().emitFrame(StackReg, FrameSizeVal, 6742 ReturnRegOpnd.getGPR32Reg()); 6743 IsCpRestoreSet = false; 6744 return false; 6745 } 6746 6747 if (IDVal == ".set") { 6748 parseDirectiveSet(); 6749 return false; 6750 } 6751 6752 if (IDVal == ".mask" || IDVal == ".fmask") { 6753 // .mask bitmask, frame_offset 6754 // bitmask: One bit for each register used. 6755 // frame_offset: Offset from Canonical Frame Address ($sp on entry) where 6756 // first register is expected to be saved. 6757 // Examples: 6758 // .mask 0x80000000, -4 6759 // .fmask 0x80000000, -4 6760 // 6761 6762 // Parse the bitmask 6763 const MCExpr *BitMask; 6764 int64_t BitMaskVal; 6765 6766 if (Parser.parseExpression(BitMask)) { 6767 reportParseError("expected bitmask value"); 6768 return false; 6769 } 6770 6771 if (!BitMask->evaluateAsAbsolute(BitMaskVal)) { 6772 reportParseError("bitmask not an absolute expression"); 6773 return false; 6774 } 6775 6776 if (Parser.getTok().is(AsmToken::Comma)) 6777 Parser.Lex(); 6778 else { 6779 reportParseError("unexpected token, expected comma"); 6780 return false; 6781 } 6782 6783 // Parse the frame_offset 6784 const MCExpr *FrameOffset; 6785 int64_t FrameOffsetVal; 6786 6787 if (Parser.parseExpression(FrameOffset)) { 6788 reportParseError("expected frame offset value"); 6789 return false; 6790 } 6791 6792 if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) { 6793 reportParseError("frame offset not an absolute expression"); 6794 return false; 6795 } 6796 6797 // If this is not the end of the statement, report an error. 6798 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6799 reportParseError("unexpected token, expected end of statement"); 6800 return false; 6801 } 6802 6803 if (IDVal == ".mask") 6804 getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal); 6805 else 6806 getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal); 6807 return false; 6808 } 6809 6810 if (IDVal == ".nan") 6811 return parseDirectiveNaN(); 6812 6813 if (IDVal == ".gpword") { 6814 parseDirectiveGpWord(); 6815 return false; 6816 } 6817 6818 if (IDVal == ".gpdword") { 6819 parseDirectiveGpDWord(); 6820 return false; 6821 } 6822 6823 if (IDVal == ".dtprelword") { 6824 parseDirectiveDtpRelWord(); 6825 return false; 6826 } 6827 6828 if (IDVal == ".dtpreldword") { 6829 parseDirectiveDtpRelDWord(); 6830 return false; 6831 } 6832 6833 if (IDVal == ".tprelword") { 6834 parseDirectiveTpRelWord(); 6835 return false; 6836 } 6837 6838 if (IDVal == ".tpreldword") { 6839 parseDirectiveTpRelDWord(); 6840 return false; 6841 } 6842 6843 if (IDVal == ".word") { 6844 parseDataDirective(4, DirectiveID.getLoc()); 6845 return false; 6846 } 6847 6848 if (IDVal == ".hword") { 6849 parseDataDirective(2, DirectiveID.getLoc()); 6850 return false; 6851 } 6852 6853 if (IDVal == ".option") { 6854 parseDirectiveOption(); 6855 return false; 6856 } 6857 6858 if (IDVal == ".abicalls") { 6859 getTargetStreamer().emitDirectiveAbiCalls(); 6860 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 6861 Error(Parser.getTok().getLoc(), 6862 "unexpected token, expected end of statement"); 6863 } 6864 return false; 6865 } 6866 6867 if (IDVal == ".cpsetup") { 6868 parseDirectiveCPSetup(); 6869 return false; 6870 } 6871 if (IDVal == ".cpreturn") { 6872 parseDirectiveCPReturn(); 6873 return false; 6874 } 6875 if (IDVal == ".module") { 6876 parseDirectiveModule(); 6877 return false; 6878 } 6879 if (IDVal == ".llvm_internal_mips_reallow_module_directive") { 6880 parseInternalDirectiveReallowModule(); 6881 return false; 6882 } 6883 if (IDVal == ".insn") { 6884 parseInsnDirective(); 6885 return false; 6886 } 6887 if (IDVal == ".sbss") { 6888 parseSSectionDirective(IDVal, ELF::SHT_NOBITS); 6889 return false; 6890 } 6891 if (IDVal == ".sdata") { 6892 parseSSectionDirective(IDVal, ELF::SHT_PROGBITS); 6893 return false; 6894 } 6895 6896 return true; 6897 } 6898 6899 bool MipsAsmParser::parseInternalDirectiveReallowModule() { 6900 // If this is not the end of the statement, report an error. 6901 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6902 reportParseError("unexpected token, expected end of statement"); 6903 return false; 6904 } 6905 6906 getTargetStreamer().reallowModuleDirective(); 6907 6908 getParser().Lex(); // Eat EndOfStatement token. 6909 return false; 6910 } 6911 6912 extern "C" void LLVMInitializeMipsAsmParser() { 6913 RegisterMCAsmParser<MipsAsmParser> X(getTheMipsTarget()); 6914 RegisterMCAsmParser<MipsAsmParser> Y(getTheMipselTarget()); 6915 RegisterMCAsmParser<MipsAsmParser> A(getTheMips64Target()); 6916 RegisterMCAsmParser<MipsAsmParser> B(getTheMips64elTarget()); 6917 } 6918 6919 #define GET_REGISTER_MATCHER 6920 #define GET_MATCHER_IMPLEMENTATION 6921 #include "MipsGenAsmMatcher.inc" 6922