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