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