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