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 llvm::make_unique<MipsAssemblerOptions>(getSTI().getFeatureBits())); 516 517 // Create an assembler options environment for the user to modify. 518 AssemblerOptions.push_back( 519 llvm::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 = llvm::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 = llvm::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 = llvm::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 = llvm::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 = llvm::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; 2874 warnIfNoMacro(IDLoc); 2875 2876 if (inPicMode() && ABI.IsO32()) { 2877 MCValue Res; 2878 if (!SymExpr->evaluateAsRelocatable(Res, nullptr, nullptr)) { 2879 Error(IDLoc, "expected relocatable expression"); 2880 return true; 2881 } 2882 if (Res.getSymB() != nullptr) { 2883 Error(IDLoc, "expected relocatable expression with only one symbol"); 2884 return true; 2885 } 2886 2887 // The case where the result register is $25 is somewhat special. If the 2888 // symbol in the final relocation is external and not modified with a 2889 // constant then we must use R_MIPS_CALL16 instead of R_MIPS_GOT16. 2890 if ((DstReg == Mips::T9 || DstReg == Mips::T9_64) && !UseSrcReg && 2891 Res.getConstant() == 0 && 2892 !(Res.getSymA()->getSymbol().isInSection() || 2893 Res.getSymA()->getSymbol().isTemporary() || 2894 (Res.getSymA()->getSymbol().isELF() && 2895 cast<MCSymbolELF>(Res.getSymA()->getSymbol()).getBinding() == 2896 ELF::STB_LOCAL))) { 2897 const MCExpr *CallExpr = 2898 MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, SymExpr, getContext()); 2899 TOut.emitRRX(Mips::LW, DstReg, GPReg, MCOperand::createExpr(CallExpr), 2900 IDLoc, STI); 2901 return false; 2902 } 2903 2904 // The remaining cases are: 2905 // External GOT: lw $tmp, %got(symbol+offset)($gp) 2906 // >addiu $tmp, $tmp, %lo(offset) 2907 // >addiu $rd, $tmp, $rs 2908 // Local GOT: lw $tmp, %got(symbol+offset)($gp) 2909 // addiu $tmp, $tmp, %lo(symbol+offset)($gp) 2910 // >addiu $rd, $tmp, $rs 2911 // The addiu's marked with a '>' may be omitted if they are redundant. If 2912 // this happens then the last instruction must use $rd as the result 2913 // register. 2914 const MipsMCExpr *GotExpr = 2915 MipsMCExpr::create(MipsMCExpr::MEK_GOT, SymExpr, getContext()); 2916 const MCExpr *LoExpr = nullptr; 2917 if (Res.getSymA()->getSymbol().isInSection() || 2918 Res.getSymA()->getSymbol().isTemporary()) 2919 LoExpr = MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext()); 2920 else if (Res.getConstant() != 0) { 2921 // External symbols fully resolve the symbol with just the %got(symbol) 2922 // but we must still account for any offset to the symbol for expressions 2923 // like symbol+8. 2924 LoExpr = MCConstantExpr::create(Res.getConstant(), getContext()); 2925 } 2926 2927 unsigned TmpReg = DstReg; 2928 if (UseSrcReg && 2929 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, 2930 SrcReg)) { 2931 // If $rs is the same as $rd, we need to use AT. 2932 // If it is not available we exit. 2933 unsigned ATReg = getATReg(IDLoc); 2934 if (!ATReg) 2935 return true; 2936 TmpReg = ATReg; 2937 } 2938 2939 TOut.emitRRX(Mips::LW, TmpReg, GPReg, MCOperand::createExpr(GotExpr), IDLoc, 2940 STI); 2941 2942 if (LoExpr) 2943 TOut.emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), 2944 IDLoc, STI); 2945 2946 if (UseSrcReg) 2947 TOut.emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, STI); 2948 2949 return false; 2950 } 2951 2952 if (inPicMode() && ABI.ArePtrs64bit()) { 2953 MCValue Res; 2954 if (!SymExpr->evaluateAsRelocatable(Res, nullptr, nullptr)) { 2955 Error(IDLoc, "expected relocatable expression"); 2956 return true; 2957 } 2958 if (Res.getSymB() != nullptr) { 2959 Error(IDLoc, "expected relocatable expression with only one symbol"); 2960 return true; 2961 } 2962 2963 // The case where the result register is $25 is somewhat special. If the 2964 // symbol in the final relocation is external and not modified with a 2965 // constant then we must use R_MIPS_CALL16 instead of R_MIPS_GOT_DISP. 2966 if ((DstReg == Mips::T9 || DstReg == Mips::T9_64) && !UseSrcReg && 2967 Res.getConstant() == 0 && 2968 !(Res.getSymA()->getSymbol().isInSection() || 2969 Res.getSymA()->getSymbol().isTemporary() || 2970 (Res.getSymA()->getSymbol().isELF() && 2971 cast<MCSymbolELF>(Res.getSymA()->getSymbol()).getBinding() == 2972 ELF::STB_LOCAL))) { 2973 const MCExpr *CallExpr = 2974 MipsMCExpr::create(MipsMCExpr::MEK_GOT_CALL, SymExpr, getContext()); 2975 TOut.emitRRX(Mips::LD, DstReg, GPReg, MCOperand::createExpr(CallExpr), 2976 IDLoc, STI); 2977 return false; 2978 } 2979 2980 // The remaining cases are: 2981 // Small offset: ld $tmp, %got_disp(symbol)($gp) 2982 // >daddiu $tmp, $tmp, offset 2983 // >daddu $rd, $tmp, $rs 2984 // The daddiu's marked with a '>' may be omitted if they are redundant. If 2985 // this happens then the last instruction must use $rd as the result 2986 // register. 2987 const MipsMCExpr *GotExpr = MipsMCExpr::create(MipsMCExpr::MEK_GOT_DISP, 2988 Res.getSymA(), 2989 getContext()); 2990 const MCExpr *LoExpr = nullptr; 2991 if (Res.getConstant() != 0) { 2992 // Symbols fully resolve with just the %got_disp(symbol) but we 2993 // must still account for any offset to the symbol for 2994 // expressions like symbol+8. 2995 LoExpr = MCConstantExpr::create(Res.getConstant(), getContext()); 2996 2997 // FIXME: Offsets greater than 16 bits are not yet implemented. 2998 // FIXME: The correct range is a 32-bit sign-extended number. 2999 if (Res.getConstant() < -0x8000 || Res.getConstant() > 0x7fff) { 3000 Error(IDLoc, "macro instruction uses large offset, which is not " 3001 "currently supported"); 3002 return true; 3003 } 3004 } 3005 3006 unsigned TmpReg = DstReg; 3007 if (UseSrcReg && 3008 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, 3009 SrcReg)) { 3010 // If $rs is the same as $rd, we need to use AT. 3011 // If it is not available we exit. 3012 unsigned ATReg = getATReg(IDLoc); 3013 if (!ATReg) 3014 return true; 3015 TmpReg = ATReg; 3016 } 3017 3018 TOut.emitRRX(Mips::LD, TmpReg, GPReg, MCOperand::createExpr(GotExpr), IDLoc, 3019 STI); 3020 3021 if (LoExpr) 3022 TOut.emitRRX(Mips::DADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), 3023 IDLoc, STI); 3024 3025 if (UseSrcReg) 3026 TOut.emitRRR(Mips::DADDu, DstReg, TmpReg, SrcReg, IDLoc, STI); 3027 3028 return false; 3029 } 3030 3031 const MipsMCExpr *HiExpr = 3032 MipsMCExpr::create(MipsMCExpr::MEK_HI, SymExpr, getContext()); 3033 const MipsMCExpr *LoExpr = 3034 MipsMCExpr::create(MipsMCExpr::MEK_LO, SymExpr, getContext()); 3035 3036 // This is the 64-bit symbol address expansion. 3037 if (ABI.ArePtrs64bit() && isGP64bit()) { 3038 // We need AT for the 64-bit expansion in the cases where the optional 3039 // source register is the destination register and for the superscalar 3040 // scheduled form. 3041 // 3042 // If it is not available we exit if the destination is the same as the 3043 // source register. 3044 3045 const MipsMCExpr *HighestExpr = 3046 MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, SymExpr, getContext()); 3047 const MipsMCExpr *HigherExpr = 3048 MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, SymExpr, getContext()); 3049 3050 bool RdRegIsRsReg = 3051 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg); 3052 3053 if (canUseATReg() && UseSrcReg && RdRegIsRsReg) { 3054 unsigned ATReg = getATReg(IDLoc); 3055 3056 // If $rs is the same as $rd: 3057 // (d)la $rd, sym($rd) => lui $at, %highest(sym) 3058 // daddiu $at, $at, %higher(sym) 3059 // dsll $at, $at, 16 3060 // daddiu $at, $at, %hi(sym) 3061 // dsll $at, $at, 16 3062 // daddiu $at, $at, %lo(sym) 3063 // daddu $rd, $at, $rd 3064 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc, 3065 STI); 3066 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, 3067 MCOperand::createExpr(HigherExpr), IDLoc, STI); 3068 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 3069 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr), 3070 IDLoc, STI); 3071 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 3072 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), 3073 IDLoc, STI); 3074 TOut.emitRRR(Mips::DADDu, DstReg, ATReg, SrcReg, IDLoc, STI); 3075 3076 return false; 3077 } else if (canUseATReg() && !RdRegIsRsReg) { 3078 unsigned ATReg = getATReg(IDLoc); 3079 3080 // If the $rs is different from $rd or if $rs isn't specified and we 3081 // have $at available: 3082 // (d)la $rd, sym/sym($rs) => lui $rd, %highest(sym) 3083 // lui $at, %hi(sym) 3084 // daddiu $rd, $rd, %higher(sym) 3085 // daddiu $at, $at, %lo(sym) 3086 // dsll32 $rd, $rd, 0 3087 // daddu $rd, $rd, $at 3088 // (daddu $rd, $rd, $rs) 3089 // 3090 // Which is preferred for superscalar issue. 3091 TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc, 3092 STI); 3093 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc, STI); 3094 TOut.emitRRX(Mips::DADDiu, DstReg, DstReg, 3095 MCOperand::createExpr(HigherExpr), IDLoc, STI); 3096 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(LoExpr), 3097 IDLoc, STI); 3098 TOut.emitRRI(Mips::DSLL32, DstReg, DstReg, 0, IDLoc, STI); 3099 TOut.emitRRR(Mips::DADDu, DstReg, DstReg, ATReg, IDLoc, STI); 3100 if (UseSrcReg) 3101 TOut.emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, STI); 3102 3103 return false; 3104 } else if (!canUseATReg() && !RdRegIsRsReg) { 3105 // Otherwise, synthesize the address in the destination register 3106 // serially: 3107 // (d)la $rd, sym/sym($rs) => lui $rd, %highest(sym) 3108 // daddiu $rd, $rd, %higher(sym) 3109 // dsll $rd, $rd, 16 3110 // daddiu $rd, $rd, %hi(sym) 3111 // dsll $rd, $rd, 16 3112 // daddiu $rd, $rd, %lo(sym) 3113 TOut.emitRX(Mips::LUi, DstReg, MCOperand::createExpr(HighestExpr), IDLoc, 3114 STI); 3115 TOut.emitRRX(Mips::DADDiu, DstReg, DstReg, 3116 MCOperand::createExpr(HigherExpr), IDLoc, STI); 3117 TOut.emitRRI(Mips::DSLL, DstReg, DstReg, 16, IDLoc, STI); 3118 TOut.emitRRX(Mips::DADDiu, DstReg, DstReg, 3119 MCOperand::createExpr(HiExpr), IDLoc, STI); 3120 TOut.emitRRI(Mips::DSLL, DstReg, DstReg, 16, IDLoc, STI); 3121 TOut.emitRRX(Mips::DADDiu, DstReg, DstReg, 3122 MCOperand::createExpr(LoExpr), IDLoc, STI); 3123 if (UseSrcReg) 3124 TOut.emitRRR(Mips::DADDu, DstReg, DstReg, SrcReg, IDLoc, STI); 3125 3126 return false; 3127 } else { 3128 // We have a case where SrcReg == DstReg and we don't have $at 3129 // available. We can't expand this case, so error out appropriately. 3130 assert(SrcReg == DstReg && !canUseATReg() && 3131 "Could have expanded dla but didn't?"); 3132 reportParseError(IDLoc, 3133 "pseudo-instruction requires $at, which is not available"); 3134 return true; 3135 } 3136 } 3137 3138 // And now, the 32-bit symbol address expansion: 3139 // If $rs is the same as $rd: 3140 // (d)la $rd, sym($rd) => lui $at, %hi(sym) 3141 // ori $at, $at, %lo(sym) 3142 // addu $rd, $at, $rd 3143 // Otherwise, if the $rs is different from $rd or if $rs isn't specified: 3144 // (d)la $rd, sym/sym($rs) => lui $rd, %hi(sym) 3145 // ori $rd, $rd, %lo(sym) 3146 // (addu $rd, $rd, $rs) 3147 unsigned TmpReg = DstReg; 3148 if (UseSrcReg && 3149 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, SrcReg)) { 3150 // If $rs is the same as $rd, we need to use AT. 3151 // If it is not available we exit. 3152 unsigned ATReg = getATReg(IDLoc); 3153 if (!ATReg) 3154 return true; 3155 TmpReg = ATReg; 3156 } 3157 3158 TOut.emitRX(Mips::LUi, TmpReg, MCOperand::createExpr(HiExpr), IDLoc, STI); 3159 TOut.emitRRX(Mips::ADDiu, TmpReg, TmpReg, MCOperand::createExpr(LoExpr), 3160 IDLoc, STI); 3161 3162 if (UseSrcReg) 3163 TOut.emitRRR(Mips::ADDu, DstReg, TmpReg, SrcReg, IDLoc, STI); 3164 else 3165 assert( 3166 getContext().getRegisterInfo()->isSuperOrSubRegisterEq(DstReg, TmpReg)); 3167 3168 return false; 3169 } 3170 3171 // Each double-precision register DO-D15 overlaps with two of the single 3172 // precision registers F0-F31. As an example, all of the following hold true: 3173 // D0 + 1 == F1, F1 + 1 == D1, F1 + 1 == F2, depending on the context. 3174 static unsigned nextReg(unsigned Reg) { 3175 if (MipsMCRegisterClasses[Mips::FGR32RegClassID].contains(Reg)) 3176 return Reg == (unsigned)Mips::F31 ? (unsigned)Mips::F0 : Reg + 1; 3177 switch (Reg) { 3178 default: llvm_unreachable("Unknown register in assembly macro expansion!"); 3179 case Mips::ZERO: return Mips::AT; 3180 case Mips::AT: return Mips::V0; 3181 case Mips::V0: return Mips::V1; 3182 case Mips::V1: return Mips::A0; 3183 case Mips::A0: return Mips::A1; 3184 case Mips::A1: return Mips::A2; 3185 case Mips::A2: return Mips::A3; 3186 case Mips::A3: return Mips::T0; 3187 case Mips::T0: return Mips::T1; 3188 case Mips::T1: return Mips::T2; 3189 case Mips::T2: return Mips::T3; 3190 case Mips::T3: return Mips::T4; 3191 case Mips::T4: return Mips::T5; 3192 case Mips::T5: return Mips::T6; 3193 case Mips::T6: return Mips::T7; 3194 case Mips::T7: return Mips::S0; 3195 case Mips::S0: return Mips::S1; 3196 case Mips::S1: return Mips::S2; 3197 case Mips::S2: return Mips::S3; 3198 case Mips::S3: return Mips::S4; 3199 case Mips::S4: return Mips::S5; 3200 case Mips::S5: return Mips::S6; 3201 case Mips::S6: return Mips::S7; 3202 case Mips::S7: return Mips::T8; 3203 case Mips::T8: return Mips::T9; 3204 case Mips::T9: return Mips::K0; 3205 case Mips::K0: return Mips::K1; 3206 case Mips::K1: return Mips::GP; 3207 case Mips::GP: return Mips::SP; 3208 case Mips::SP: return Mips::FP; 3209 case Mips::FP: return Mips::RA; 3210 case Mips::RA: return Mips::ZERO; 3211 case Mips::D0: return Mips::F1; 3212 case Mips::D1: return Mips::F3; 3213 case Mips::D2: return Mips::F5; 3214 case Mips::D3: return Mips::F7; 3215 case Mips::D4: return Mips::F9; 3216 case Mips::D5: return Mips::F11; 3217 case Mips::D6: return Mips::F13; 3218 case Mips::D7: return Mips::F15; 3219 case Mips::D8: return Mips::F17; 3220 case Mips::D9: return Mips::F19; 3221 case Mips::D10: return Mips::F21; 3222 case Mips::D11: return Mips::F23; 3223 case Mips::D12: return Mips::F25; 3224 case Mips::D13: return Mips::F27; 3225 case Mips::D14: return Mips::F29; 3226 case Mips::D15: return Mips::F31; 3227 } 3228 } 3229 3230 // FIXME: This method is too general. In principle we should compute the number 3231 // of instructions required to synthesize the immediate inline compared to 3232 // synthesizing the address inline and relying on non .text sections. 3233 // For static O32 and N32 this may yield a small benefit, for static N64 this is 3234 // likely to yield a much larger benefit as we have to synthesize a 64bit 3235 // address to load a 64 bit value. 3236 bool MipsAsmParser::emitPartialAddress(MipsTargetStreamer &TOut, SMLoc IDLoc, 3237 MCSymbol *Sym) { 3238 unsigned ATReg = getATReg(IDLoc); 3239 if (!ATReg) 3240 return true; 3241 3242 if(IsPicEnabled) { 3243 const MCExpr *GotSym = 3244 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3245 const MipsMCExpr *GotExpr = 3246 MipsMCExpr::create(MipsMCExpr::MEK_GOT, GotSym, getContext()); 3247 3248 if(isABI_O32() || isABI_N32()) { 3249 TOut.emitRRX(Mips::LW, ATReg, GPReg, MCOperand::createExpr(GotExpr), 3250 IDLoc, STI); 3251 } else { //isABI_N64() 3252 TOut.emitRRX(Mips::LD, ATReg, GPReg, MCOperand::createExpr(GotExpr), 3253 IDLoc, STI); 3254 } 3255 } else { //!IsPicEnabled 3256 const MCExpr *HiSym = 3257 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3258 const MipsMCExpr *HiExpr = 3259 MipsMCExpr::create(MipsMCExpr::MEK_HI, HiSym, getContext()); 3260 3261 // FIXME: This is technically correct but gives a different result to gas, 3262 // but gas is incomplete there (it has a fixme noting it doesn't work with 3263 // 64-bit addresses). 3264 // FIXME: With -msym32 option, the address expansion for N64 should probably 3265 // use the O32 / N32 case. It's safe to use the 64 address expansion as the 3266 // symbol's value is considered sign extended. 3267 if(isABI_O32() || isABI_N32()) { 3268 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HiExpr), IDLoc, STI); 3269 } else { //isABI_N64() 3270 const MCExpr *HighestSym = 3271 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3272 const MipsMCExpr *HighestExpr = 3273 MipsMCExpr::create(MipsMCExpr::MEK_HIGHEST, HighestSym, getContext()); 3274 const MCExpr *HigherSym = 3275 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3276 const MipsMCExpr *HigherExpr = 3277 MipsMCExpr::create(MipsMCExpr::MEK_HIGHER, HigherSym, getContext()); 3278 3279 TOut.emitRX(Mips::LUi, ATReg, MCOperand::createExpr(HighestExpr), IDLoc, 3280 STI); 3281 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, 3282 MCOperand::createExpr(HigherExpr), IDLoc, STI); 3283 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 3284 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, MCOperand::createExpr(HiExpr), 3285 IDLoc, STI); 3286 TOut.emitRRI(Mips::DSLL, ATReg, ATReg, 16, IDLoc, STI); 3287 } 3288 } 3289 return false; 3290 } 3291 3292 bool MipsAsmParser::expandLoadImmReal(MCInst &Inst, bool IsSingle, bool IsGPR, 3293 bool Is64FPU, SMLoc IDLoc, 3294 MCStreamer &Out, 3295 const MCSubtargetInfo *STI) { 3296 MipsTargetStreamer &TOut = getTargetStreamer(); 3297 assert(Inst.getNumOperands() == 2 && "Invalid operand count"); 3298 assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isImm() && 3299 "Invalid instruction operand."); 3300 3301 unsigned FirstReg = Inst.getOperand(0).getReg(); 3302 uint64_t ImmOp64 = Inst.getOperand(1).getImm(); 3303 3304 uint32_t HiImmOp64 = (ImmOp64 & 0xffffffff00000000) >> 32; 3305 // If ImmOp64 is AsmToken::Integer type (all bits set to zero in the 3306 // exponent field), convert it to double (e.g. 1 to 1.0) 3307 if ((HiImmOp64 & 0x7ff00000) == 0) { 3308 APFloat RealVal(APFloat::IEEEdouble(), ImmOp64); 3309 ImmOp64 = RealVal.bitcastToAPInt().getZExtValue(); 3310 } 3311 3312 uint32_t LoImmOp64 = ImmOp64 & 0xffffffff; 3313 HiImmOp64 = (ImmOp64 & 0xffffffff00000000) >> 32; 3314 3315 if (IsSingle) { 3316 // Conversion of a double in an uint64_t to a float in a uint32_t, 3317 // retaining the bit pattern of a float. 3318 uint32_t ImmOp32; 3319 double doubleImm = BitsToDouble(ImmOp64); 3320 float tmp_float = static_cast<float>(doubleImm); 3321 ImmOp32 = FloatToBits(tmp_float); 3322 3323 if (IsGPR) { 3324 if (loadImmediate(ImmOp32, FirstReg, Mips::NoRegister, true, true, IDLoc, 3325 Out, STI)) 3326 return true; 3327 return false; 3328 } else { 3329 unsigned ATReg = getATReg(IDLoc); 3330 if (!ATReg) 3331 return true; 3332 if (LoImmOp64 == 0) { 3333 if (loadImmediate(ImmOp32, ATReg, Mips::NoRegister, true, true, IDLoc, 3334 Out, STI)) 3335 return true; 3336 TOut.emitRR(Mips::MTC1, FirstReg, ATReg, IDLoc, STI); 3337 return false; 3338 } 3339 3340 MCSection *CS = getStreamer().getCurrentSectionOnly(); 3341 // FIXME: Enhance this expansion to use the .lit4 & .lit8 sections 3342 // where appropriate. 3343 MCSection *ReadOnlySection = getContext().getELFSection( 3344 ".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 3345 3346 MCSymbol *Sym = getContext().createTempSymbol(); 3347 const MCExpr *LoSym = 3348 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3349 const MipsMCExpr *LoExpr = 3350 MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext()); 3351 3352 getStreamer().SwitchSection(ReadOnlySection); 3353 getStreamer().EmitLabel(Sym, IDLoc); 3354 getStreamer().EmitIntValue(ImmOp32, 4); 3355 getStreamer().SwitchSection(CS); 3356 3357 if(emitPartialAddress(TOut, IDLoc, Sym)) 3358 return true; 3359 TOut.emitRRX(Mips::LWC1, FirstReg, ATReg, 3360 MCOperand::createExpr(LoExpr), IDLoc, STI); 3361 } 3362 return false; 3363 } 3364 3365 // if(!IsSingle) 3366 unsigned ATReg = getATReg(IDLoc); 3367 if (!ATReg) 3368 return true; 3369 3370 if (IsGPR) { 3371 if (LoImmOp64 == 0) { 3372 if(isABI_N32() || isABI_N64()) { 3373 if (loadImmediate(HiImmOp64, FirstReg, Mips::NoRegister, false, true, 3374 IDLoc, Out, STI)) 3375 return true; 3376 return false; 3377 } else { 3378 if (loadImmediate(HiImmOp64, FirstReg, Mips::NoRegister, true, true, 3379 IDLoc, Out, STI)) 3380 return true; 3381 3382 if (loadImmediate(0, nextReg(FirstReg), Mips::NoRegister, true, true, 3383 IDLoc, Out, STI)) 3384 return true; 3385 return false; 3386 } 3387 } 3388 3389 MCSection *CS = getStreamer().getCurrentSectionOnly(); 3390 MCSection *ReadOnlySection = getContext().getELFSection( 3391 ".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 3392 3393 MCSymbol *Sym = getContext().createTempSymbol(); 3394 const MCExpr *LoSym = 3395 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3396 const MipsMCExpr *LoExpr = 3397 MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext()); 3398 3399 getStreamer().SwitchSection(ReadOnlySection); 3400 getStreamer().EmitLabel(Sym, IDLoc); 3401 getStreamer().EmitIntValue(HiImmOp64, 4); 3402 getStreamer().EmitIntValue(LoImmOp64, 4); 3403 getStreamer().SwitchSection(CS); 3404 3405 if(emitPartialAddress(TOut, IDLoc, Sym)) 3406 return true; 3407 if(isABI_N64()) 3408 TOut.emitRRX(Mips::DADDiu, ATReg, ATReg, 3409 MCOperand::createExpr(LoExpr), IDLoc, STI); 3410 else 3411 TOut.emitRRX(Mips::ADDiu, ATReg, ATReg, 3412 MCOperand::createExpr(LoExpr), IDLoc, STI); 3413 3414 if(isABI_N32() || isABI_N64()) 3415 TOut.emitRRI(Mips::LD, FirstReg, ATReg, 0, IDLoc, STI); 3416 else { 3417 TOut.emitRRI(Mips::LW, FirstReg, ATReg, 0, IDLoc, STI); 3418 TOut.emitRRI(Mips::LW, nextReg(FirstReg), ATReg, 4, IDLoc, STI); 3419 } 3420 return false; 3421 } else { // if(!IsGPR && !IsSingle) 3422 if ((LoImmOp64 == 0) && 3423 !((HiImmOp64 & 0xffff0000) && (HiImmOp64 & 0x0000ffff))) { 3424 // FIXME: In the case where the constant is zero, we can load the 3425 // register directly from the zero register. 3426 if (loadImmediate(HiImmOp64, ATReg, Mips::NoRegister, true, true, IDLoc, 3427 Out, STI)) 3428 return true; 3429 if (isABI_N32() || isABI_N64()) 3430 TOut.emitRR(Mips::DMTC1, FirstReg, ATReg, IDLoc, STI); 3431 else if (hasMips32r2()) { 3432 TOut.emitRR(Mips::MTC1, FirstReg, Mips::ZERO, IDLoc, STI); 3433 TOut.emitRRR(Mips::MTHC1_D32, FirstReg, FirstReg, ATReg, IDLoc, STI); 3434 } else { 3435 TOut.emitRR(Mips::MTC1, nextReg(FirstReg), ATReg, IDLoc, STI); 3436 TOut.emitRR(Mips::MTC1, FirstReg, Mips::ZERO, IDLoc, STI); 3437 } 3438 return false; 3439 } 3440 3441 MCSection *CS = getStreamer().getCurrentSectionOnly(); 3442 // FIXME: Enhance this expansion to use the .lit4 & .lit8 sections 3443 // where appropriate. 3444 MCSection *ReadOnlySection = getContext().getELFSection( 3445 ".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 3446 3447 MCSymbol *Sym = getContext().createTempSymbol(); 3448 const MCExpr *LoSym = 3449 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 3450 const MipsMCExpr *LoExpr = 3451 MipsMCExpr::create(MipsMCExpr::MEK_LO, LoSym, getContext()); 3452 3453 getStreamer().SwitchSection(ReadOnlySection); 3454 getStreamer().EmitLabel(Sym, IDLoc); 3455 getStreamer().EmitIntValue(HiImmOp64, 4); 3456 getStreamer().EmitIntValue(LoImmOp64, 4); 3457 getStreamer().SwitchSection(CS); 3458 3459 if(emitPartialAddress(TOut, IDLoc, Sym)) 3460 return true; 3461 TOut.emitRRX(Is64FPU ? Mips::LDC164 : Mips::LDC1, FirstReg, ATReg, 3462 MCOperand::createExpr(LoExpr), IDLoc, STI); 3463 } 3464 return false; 3465 } 3466 3467 bool MipsAsmParser::expandUncondBranchMMPseudo(MCInst &Inst, SMLoc IDLoc, 3468 MCStreamer &Out, 3469 const MCSubtargetInfo *STI) { 3470 MipsTargetStreamer &TOut = getTargetStreamer(); 3471 3472 assert(getInstDesc(Inst.getOpcode()).getNumOperands() == 1 && 3473 "unexpected number of operands"); 3474 3475 MCOperand Offset = Inst.getOperand(0); 3476 if (Offset.isExpr()) { 3477 Inst.clear(); 3478 Inst.setOpcode(Mips::BEQ_MM); 3479 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 3480 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 3481 Inst.addOperand(MCOperand::createExpr(Offset.getExpr())); 3482 } else { 3483 assert(Offset.isImm() && "expected immediate operand kind"); 3484 if (isInt<11>(Offset.getImm())) { 3485 // If offset fits into 11 bits then this instruction becomes microMIPS 3486 // 16-bit unconditional branch instruction. 3487 if (inMicroMipsMode()) 3488 Inst.setOpcode(hasMips32r6() ? Mips::BC16_MMR6 : Mips::B16_MM); 3489 } else { 3490 if (!isInt<17>(Offset.getImm())) 3491 return Error(IDLoc, "branch target out of range"); 3492 if (OffsetToAlignment(Offset.getImm(), 1LL << 1)) 3493 return Error(IDLoc, "branch to misaligned address"); 3494 Inst.clear(); 3495 Inst.setOpcode(Mips::BEQ_MM); 3496 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 3497 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 3498 Inst.addOperand(MCOperand::createImm(Offset.getImm())); 3499 } 3500 } 3501 Out.EmitInstruction(Inst, *STI); 3502 3503 // If .set reorder is active and branch instruction has a delay slot, 3504 // emit a NOP after it. 3505 const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode()); 3506 if (MCID.hasDelaySlot() && AssemblerOptions.back()->isReorder()) 3507 TOut.emitEmptyDelaySlot(true, IDLoc, STI); 3508 3509 return false; 3510 } 3511 3512 bool MipsAsmParser::expandBranchImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3513 const MCSubtargetInfo *STI) { 3514 MipsTargetStreamer &TOut = getTargetStreamer(); 3515 const MCOperand &DstRegOp = Inst.getOperand(0); 3516 assert(DstRegOp.isReg() && "expected register operand kind"); 3517 3518 const MCOperand &ImmOp = Inst.getOperand(1); 3519 assert(ImmOp.isImm() && "expected immediate operand kind"); 3520 3521 const MCOperand &MemOffsetOp = Inst.getOperand(2); 3522 assert((MemOffsetOp.isImm() || MemOffsetOp.isExpr()) && 3523 "expected immediate or expression operand"); 3524 3525 bool IsLikely = false; 3526 3527 unsigned OpCode = 0; 3528 switch(Inst.getOpcode()) { 3529 case Mips::BneImm: 3530 OpCode = Mips::BNE; 3531 break; 3532 case Mips::BeqImm: 3533 OpCode = Mips::BEQ; 3534 break; 3535 case Mips::BEQLImmMacro: 3536 OpCode = Mips::BEQL; 3537 IsLikely = true; 3538 break; 3539 case Mips::BNELImmMacro: 3540 OpCode = Mips::BNEL; 3541 IsLikely = true; 3542 break; 3543 default: 3544 llvm_unreachable("Unknown immediate branch pseudo-instruction."); 3545 break; 3546 } 3547 3548 int64_t ImmValue = ImmOp.getImm(); 3549 if (ImmValue == 0) { 3550 if (IsLikely) { 3551 TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, 3552 MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI); 3553 TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 3554 } else 3555 TOut.emitRRX(OpCode, DstRegOp.getReg(), Mips::ZERO, MemOffsetOp, IDLoc, 3556 STI); 3557 } else { 3558 warnIfNoMacro(IDLoc); 3559 3560 unsigned ATReg = getATReg(IDLoc); 3561 if (!ATReg) 3562 return true; 3563 3564 if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, !isGP64bit(), true, 3565 IDLoc, Out, STI)) 3566 return true; 3567 3568 if (IsLikely) { 3569 TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg, 3570 MCOperand::createExpr(MemOffsetOp.getExpr()), IDLoc, STI); 3571 TOut.emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 3572 } else 3573 TOut.emitRRX(OpCode, DstRegOp.getReg(), ATReg, MemOffsetOp, IDLoc, STI); 3574 } 3575 return false; 3576 } 3577 3578 void MipsAsmParser::expandMemInst(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3579 const MCSubtargetInfo *STI, bool IsLoad) { 3580 const MCOperand &DstRegOp = Inst.getOperand(0); 3581 assert(DstRegOp.isReg() && "expected register operand kind"); 3582 const MCOperand &BaseRegOp = Inst.getOperand(1); 3583 assert(BaseRegOp.isReg() && "expected register operand kind"); 3584 const MCOperand &OffsetOp = Inst.getOperand(2); 3585 3586 MipsTargetStreamer &TOut = getTargetStreamer(); 3587 unsigned DstReg = DstRegOp.getReg(); 3588 unsigned BaseReg = BaseRegOp.getReg(); 3589 unsigned TmpReg = DstReg; 3590 3591 const MCInstrDesc &Desc = getInstDesc(Inst.getOpcode()); 3592 int16_t DstRegClass = Desc.OpInfo[0].RegClass; 3593 unsigned DstRegClassID = 3594 getContext().getRegisterInfo()->getRegClass(DstRegClass).getID(); 3595 bool IsGPR = (DstRegClassID == Mips::GPR32RegClassID) || 3596 (DstRegClassID == Mips::GPR64RegClassID); 3597 3598 if (!IsLoad || !IsGPR || (BaseReg == DstReg)) { 3599 // At this point we need AT to perform the expansions 3600 // and we exit if it is not available. 3601 TmpReg = getATReg(IDLoc); 3602 if (!TmpReg) 3603 return; 3604 } 3605 3606 if (OffsetOp.isImm()) { 3607 int64_t LoOffset = OffsetOp.getImm() & 0xffff; 3608 int64_t HiOffset = OffsetOp.getImm() & ~0xffff; 3609 3610 // If msb of LoOffset is 1(negative number) we must increment 3611 // HiOffset to account for the sign-extension of the low part. 3612 if (LoOffset & 0x8000) 3613 HiOffset += 0x10000; 3614 3615 bool IsLargeOffset = HiOffset != 0; 3616 3617 if (IsLargeOffset) { 3618 bool Is32BitImm = (HiOffset >> 32) == 0; 3619 if (loadImmediate(HiOffset, TmpReg, Mips::NoRegister, Is32BitImm, true, 3620 IDLoc, Out, STI)) 3621 return; 3622 } 3623 3624 if (BaseReg != Mips::ZERO && BaseReg != Mips::ZERO_64) 3625 TOut.emitRRR(isGP64bit() ? Mips::DADDu : Mips::ADDu, TmpReg, TmpReg, 3626 BaseReg, IDLoc, STI); 3627 TOut.emitRRI(Inst.getOpcode(), DstReg, TmpReg, LoOffset, IDLoc, STI); 3628 } else { 3629 assert(OffsetOp.isExpr() && "expected expression operand kind"); 3630 const MCExpr *ExprOffset = OffsetOp.getExpr(); 3631 MCOperand LoOperand = MCOperand::createExpr( 3632 MipsMCExpr::create(MipsMCExpr::MEK_LO, ExprOffset, getContext())); 3633 MCOperand HiOperand = MCOperand::createExpr( 3634 MipsMCExpr::create(MipsMCExpr::MEK_HI, ExprOffset, getContext())); 3635 3636 if (IsLoad) 3637 TOut.emitLoadWithSymOffset(Inst.getOpcode(), DstReg, BaseReg, HiOperand, 3638 LoOperand, TmpReg, IDLoc, STI); 3639 else 3640 TOut.emitStoreWithSymOffset(Inst.getOpcode(), DstReg, BaseReg, HiOperand, 3641 LoOperand, TmpReg, IDLoc, STI); 3642 } 3643 } 3644 3645 bool MipsAsmParser::expandLoadStoreMultiple(MCInst &Inst, SMLoc IDLoc, 3646 MCStreamer &Out, 3647 const MCSubtargetInfo *STI) { 3648 unsigned OpNum = Inst.getNumOperands(); 3649 unsigned Opcode = Inst.getOpcode(); 3650 unsigned NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM32_MM : Mips::LWM32_MM; 3651 3652 assert(Inst.getOperand(OpNum - 1).isImm() && 3653 Inst.getOperand(OpNum - 2).isReg() && 3654 Inst.getOperand(OpNum - 3).isReg() && "Invalid instruction operand."); 3655 3656 if (OpNum < 8 && Inst.getOperand(OpNum - 1).getImm() <= 60 && 3657 Inst.getOperand(OpNum - 1).getImm() >= 0 && 3658 (Inst.getOperand(OpNum - 2).getReg() == Mips::SP || 3659 Inst.getOperand(OpNum - 2).getReg() == Mips::SP_64) && 3660 (Inst.getOperand(OpNum - 3).getReg() == Mips::RA || 3661 Inst.getOperand(OpNum - 3).getReg() == Mips::RA_64)) { 3662 // It can be implemented as SWM16 or LWM16 instruction. 3663 if (inMicroMipsMode() && hasMips32r6()) 3664 NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MMR6 : Mips::LWM16_MMR6; 3665 else 3666 NewOpcode = Opcode == Mips::SWM_MM ? Mips::SWM16_MM : Mips::LWM16_MM; 3667 } 3668 3669 Inst.setOpcode(NewOpcode); 3670 Out.EmitInstruction(Inst, *STI); 3671 return false; 3672 } 3673 3674 bool MipsAsmParser::expandCondBranches(MCInst &Inst, SMLoc IDLoc, 3675 MCStreamer &Out, 3676 const MCSubtargetInfo *STI) { 3677 MipsTargetStreamer &TOut = getTargetStreamer(); 3678 bool EmittedNoMacroWarning = false; 3679 unsigned PseudoOpcode = Inst.getOpcode(); 3680 unsigned SrcReg = Inst.getOperand(0).getReg(); 3681 const MCOperand &TrgOp = Inst.getOperand(1); 3682 const MCExpr *OffsetExpr = Inst.getOperand(2).getExpr(); 3683 3684 unsigned ZeroSrcOpcode, ZeroTrgOpcode; 3685 bool ReverseOrderSLT, IsUnsigned, IsLikely, AcceptsEquality; 3686 3687 unsigned TrgReg; 3688 if (TrgOp.isReg()) 3689 TrgReg = TrgOp.getReg(); 3690 else if (TrgOp.isImm()) { 3691 warnIfNoMacro(IDLoc); 3692 EmittedNoMacroWarning = true; 3693 3694 TrgReg = getATReg(IDLoc); 3695 if (!TrgReg) 3696 return true; 3697 3698 switch(PseudoOpcode) { 3699 default: 3700 llvm_unreachable("unknown opcode for branch pseudo-instruction"); 3701 case Mips::BLTImmMacro: 3702 PseudoOpcode = Mips::BLT; 3703 break; 3704 case Mips::BLEImmMacro: 3705 PseudoOpcode = Mips::BLE; 3706 break; 3707 case Mips::BGEImmMacro: 3708 PseudoOpcode = Mips::BGE; 3709 break; 3710 case Mips::BGTImmMacro: 3711 PseudoOpcode = Mips::BGT; 3712 break; 3713 case Mips::BLTUImmMacro: 3714 PseudoOpcode = Mips::BLTU; 3715 break; 3716 case Mips::BLEUImmMacro: 3717 PseudoOpcode = Mips::BLEU; 3718 break; 3719 case Mips::BGEUImmMacro: 3720 PseudoOpcode = Mips::BGEU; 3721 break; 3722 case Mips::BGTUImmMacro: 3723 PseudoOpcode = Mips::BGTU; 3724 break; 3725 case Mips::BLTLImmMacro: 3726 PseudoOpcode = Mips::BLTL; 3727 break; 3728 case Mips::BLELImmMacro: 3729 PseudoOpcode = Mips::BLEL; 3730 break; 3731 case Mips::BGELImmMacro: 3732 PseudoOpcode = Mips::BGEL; 3733 break; 3734 case Mips::BGTLImmMacro: 3735 PseudoOpcode = Mips::BGTL; 3736 break; 3737 case Mips::BLTULImmMacro: 3738 PseudoOpcode = Mips::BLTUL; 3739 break; 3740 case Mips::BLEULImmMacro: 3741 PseudoOpcode = Mips::BLEUL; 3742 break; 3743 case Mips::BGEULImmMacro: 3744 PseudoOpcode = Mips::BGEUL; 3745 break; 3746 case Mips::BGTULImmMacro: 3747 PseudoOpcode = Mips::BGTUL; 3748 break; 3749 } 3750 3751 if (loadImmediate(TrgOp.getImm(), TrgReg, Mips::NoRegister, !isGP64bit(), 3752 false, IDLoc, Out, STI)) 3753 return true; 3754 } 3755 3756 switch (PseudoOpcode) { 3757 case Mips::BLT: 3758 case Mips::BLTU: 3759 case Mips::BLTL: 3760 case Mips::BLTUL: 3761 AcceptsEquality = false; 3762 ReverseOrderSLT = false; 3763 IsUnsigned = 3764 ((PseudoOpcode == Mips::BLTU) || (PseudoOpcode == Mips::BLTUL)); 3765 IsLikely = ((PseudoOpcode == Mips::BLTL) || (PseudoOpcode == Mips::BLTUL)); 3766 ZeroSrcOpcode = Mips::BGTZ; 3767 ZeroTrgOpcode = Mips::BLTZ; 3768 break; 3769 case Mips::BLE: 3770 case Mips::BLEU: 3771 case Mips::BLEL: 3772 case Mips::BLEUL: 3773 AcceptsEquality = true; 3774 ReverseOrderSLT = true; 3775 IsUnsigned = 3776 ((PseudoOpcode == Mips::BLEU) || (PseudoOpcode == Mips::BLEUL)); 3777 IsLikely = ((PseudoOpcode == Mips::BLEL) || (PseudoOpcode == Mips::BLEUL)); 3778 ZeroSrcOpcode = Mips::BGEZ; 3779 ZeroTrgOpcode = Mips::BLEZ; 3780 break; 3781 case Mips::BGE: 3782 case Mips::BGEU: 3783 case Mips::BGEL: 3784 case Mips::BGEUL: 3785 AcceptsEquality = true; 3786 ReverseOrderSLT = false; 3787 IsUnsigned = 3788 ((PseudoOpcode == Mips::BGEU) || (PseudoOpcode == Mips::BGEUL)); 3789 IsLikely = ((PseudoOpcode == Mips::BGEL) || (PseudoOpcode == Mips::BGEUL)); 3790 ZeroSrcOpcode = Mips::BLEZ; 3791 ZeroTrgOpcode = Mips::BGEZ; 3792 break; 3793 case Mips::BGT: 3794 case Mips::BGTU: 3795 case Mips::BGTL: 3796 case Mips::BGTUL: 3797 AcceptsEquality = false; 3798 ReverseOrderSLT = true; 3799 IsUnsigned = 3800 ((PseudoOpcode == Mips::BGTU) || (PseudoOpcode == Mips::BGTUL)); 3801 IsLikely = ((PseudoOpcode == Mips::BGTL) || (PseudoOpcode == Mips::BGTUL)); 3802 ZeroSrcOpcode = Mips::BLTZ; 3803 ZeroTrgOpcode = Mips::BGTZ; 3804 break; 3805 default: 3806 llvm_unreachable("unknown opcode for branch pseudo-instruction"); 3807 } 3808 3809 bool IsTrgRegZero = (TrgReg == Mips::ZERO); 3810 bool IsSrcRegZero = (SrcReg == Mips::ZERO); 3811 if (IsSrcRegZero && IsTrgRegZero) { 3812 // FIXME: All of these Opcode-specific if's are needed for compatibility 3813 // with GAS' behaviour. However, they may not generate the most efficient 3814 // code in some circumstances. 3815 if (PseudoOpcode == Mips::BLT) { 3816 TOut.emitRX(Mips::BLTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3817 IDLoc, STI); 3818 return false; 3819 } 3820 if (PseudoOpcode == Mips::BLE) { 3821 TOut.emitRX(Mips::BLEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3822 IDLoc, STI); 3823 Warning(IDLoc, "branch is always taken"); 3824 return false; 3825 } 3826 if (PseudoOpcode == Mips::BGE) { 3827 TOut.emitRX(Mips::BGEZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3828 IDLoc, STI); 3829 Warning(IDLoc, "branch is always taken"); 3830 return false; 3831 } 3832 if (PseudoOpcode == Mips::BGT) { 3833 TOut.emitRX(Mips::BGTZ, Mips::ZERO, MCOperand::createExpr(OffsetExpr), 3834 IDLoc, STI); 3835 return false; 3836 } 3837 if (PseudoOpcode == Mips::BGTU) { 3838 TOut.emitRRX(Mips::BNE, Mips::ZERO, Mips::ZERO, 3839 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3840 return false; 3841 } 3842 if (AcceptsEquality) { 3843 // If both registers are $0 and the pseudo-branch accepts equality, it 3844 // will always be taken, so we emit an unconditional branch. 3845 TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO, 3846 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3847 Warning(IDLoc, "branch is always taken"); 3848 return false; 3849 } 3850 // If both registers are $0 and the pseudo-branch does not accept 3851 // equality, it will never be taken, so we don't have to emit anything. 3852 return false; 3853 } 3854 if (IsSrcRegZero || IsTrgRegZero) { 3855 if ((IsSrcRegZero && PseudoOpcode == Mips::BGTU) || 3856 (IsTrgRegZero && PseudoOpcode == Mips::BLTU)) { 3857 // If the $rs is $0 and the pseudo-branch is BGTU (0 > x) or 3858 // if the $rt is $0 and the pseudo-branch is BLTU (x < 0), 3859 // the pseudo-branch will never be taken, so we don't emit anything. 3860 // This only applies to unsigned pseudo-branches. 3861 return false; 3862 } 3863 if ((IsSrcRegZero && PseudoOpcode == Mips::BLEU) || 3864 (IsTrgRegZero && PseudoOpcode == Mips::BGEU)) { 3865 // If the $rs is $0 and the pseudo-branch is BLEU (0 <= x) or 3866 // if the $rt is $0 and the pseudo-branch is BGEU (x >= 0), 3867 // the pseudo-branch will always be taken, so we emit an unconditional 3868 // branch. 3869 // This only applies to unsigned pseudo-branches. 3870 TOut.emitRRX(Mips::BEQ, Mips::ZERO, Mips::ZERO, 3871 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3872 Warning(IDLoc, "branch is always taken"); 3873 return false; 3874 } 3875 if (IsUnsigned) { 3876 // If the $rs is $0 and the pseudo-branch is BLTU (0 < x) or 3877 // if the $rt is $0 and the pseudo-branch is BGTU (x > 0), 3878 // the pseudo-branch will be taken only when the non-zero register is 3879 // different from 0, so we emit a BNEZ. 3880 // 3881 // If the $rs is $0 and the pseudo-branch is BGEU (0 >= x) or 3882 // if the $rt is $0 and the pseudo-branch is BLEU (x <= 0), 3883 // the pseudo-branch will be taken only when the non-zero register is 3884 // equal to 0, so we emit a BEQZ. 3885 // 3886 // Because only BLEU and BGEU branch on equality, we can use the 3887 // AcceptsEquality variable to decide when to emit the BEQZ. 3888 TOut.emitRRX(AcceptsEquality ? Mips::BEQ : Mips::BNE, 3889 IsSrcRegZero ? TrgReg : SrcReg, Mips::ZERO, 3890 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3891 return false; 3892 } 3893 // If we have a signed pseudo-branch and one of the registers is $0, 3894 // we can use an appropriate compare-to-zero branch. We select which one 3895 // to use in the switch statement above. 3896 TOut.emitRX(IsSrcRegZero ? ZeroSrcOpcode : ZeroTrgOpcode, 3897 IsSrcRegZero ? TrgReg : SrcReg, 3898 MCOperand::createExpr(OffsetExpr), IDLoc, STI); 3899 return false; 3900 } 3901 3902 // If neither the SrcReg nor the TrgReg are $0, we need AT to perform the 3903 // expansions. If it is not available, we return. 3904 unsigned ATRegNum = getATReg(IDLoc); 3905 if (!ATRegNum) 3906 return true; 3907 3908 if (!EmittedNoMacroWarning) 3909 warnIfNoMacro(IDLoc); 3910 3911 // SLT fits well with 2 of our 4 pseudo-branches: 3912 // BLT, where $rs < $rt, translates into "slt $at, $rs, $rt" and 3913 // BGT, where $rs > $rt, translates into "slt $at, $rt, $rs". 3914 // If the result of the SLT is 1, we branch, and if it's 0, we don't. 3915 // This is accomplished by using a BNEZ with the result of the SLT. 3916 // 3917 // The other 2 pseudo-branches are opposites of the above 2 (BGE with BLT 3918 // and BLE with BGT), so we change the BNEZ into a BEQZ. 3919 // Because only BGE and BLE branch on equality, we can use the 3920 // AcceptsEquality variable to decide when to emit the BEQZ. 3921 // Note that the order of the SLT arguments doesn't change between 3922 // opposites. 3923 // 3924 // The same applies to the unsigned variants, except that SLTu is used 3925 // instead of SLT. 3926 TOut.emitRRR(IsUnsigned ? Mips::SLTu : Mips::SLT, ATRegNum, 3927 ReverseOrderSLT ? TrgReg : SrcReg, 3928 ReverseOrderSLT ? SrcReg : TrgReg, IDLoc, STI); 3929 3930 TOut.emitRRX(IsLikely ? (AcceptsEquality ? Mips::BEQL : Mips::BNEL) 3931 : (AcceptsEquality ? Mips::BEQ : Mips::BNE), 3932 ATRegNum, Mips::ZERO, MCOperand::createExpr(OffsetExpr), IDLoc, 3933 STI); 3934 return false; 3935 } 3936 3937 // Expand a integer division macro. 3938 // 3939 // Notably we don't have to emit a warning when encountering $rt as the $zero 3940 // register, or 0 as an immediate. processInstruction() has already done that. 3941 // 3942 // The destination register can only be $zero when expanding (S)DivIMacro or 3943 // D(S)DivMacro. 3944 3945 bool MipsAsmParser::expandDivRem(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 3946 const MCSubtargetInfo *STI, const bool IsMips64, 3947 const bool Signed) { 3948 MipsTargetStreamer &TOut = getTargetStreamer(); 3949 3950 warnIfNoMacro(IDLoc); 3951 3952 const MCOperand &RdRegOp = Inst.getOperand(0); 3953 assert(RdRegOp.isReg() && "expected register operand kind"); 3954 unsigned RdReg = RdRegOp.getReg(); 3955 3956 const MCOperand &RsRegOp = Inst.getOperand(1); 3957 assert(RsRegOp.isReg() && "expected register operand kind"); 3958 unsigned RsReg = RsRegOp.getReg(); 3959 3960 unsigned RtReg; 3961 int64_t ImmValue; 3962 3963 const MCOperand &RtOp = Inst.getOperand(2); 3964 assert((RtOp.isReg() || RtOp.isImm()) && 3965 "expected register or immediate operand kind"); 3966 if (RtOp.isReg()) 3967 RtReg = RtOp.getReg(); 3968 else 3969 ImmValue = RtOp.getImm(); 3970 3971 unsigned DivOp; 3972 unsigned ZeroReg; 3973 unsigned SubOp; 3974 3975 if (IsMips64) { 3976 DivOp = Signed ? Mips::DSDIV : Mips::DUDIV; 3977 ZeroReg = Mips::ZERO_64; 3978 SubOp = Mips::DSUB; 3979 } else { 3980 DivOp = Signed ? Mips::SDIV : Mips::UDIV; 3981 ZeroReg = Mips::ZERO; 3982 SubOp = Mips::SUB; 3983 } 3984 3985 bool UseTraps = useTraps(); 3986 3987 unsigned Opcode = Inst.getOpcode(); 3988 bool isDiv = Opcode == Mips::SDivMacro || Opcode == Mips::SDivIMacro || 3989 Opcode == Mips::UDivMacro || Opcode == Mips::UDivIMacro || 3990 Opcode == Mips::DSDivMacro || Opcode == Mips::DSDivIMacro || 3991 Opcode == Mips::DUDivMacro || Opcode == Mips::DUDivIMacro; 3992 3993 bool isRem = Opcode == Mips::SRemMacro || Opcode == Mips::SRemIMacro || 3994 Opcode == Mips::URemMacro || Opcode == Mips::URemIMacro || 3995 Opcode == Mips::DSRemMacro || Opcode == Mips::DSRemIMacro || 3996 Opcode == Mips::DURemMacro || Opcode == Mips::DURemIMacro; 3997 3998 if (RtOp.isImm()) { 3999 unsigned ATReg = getATReg(IDLoc); 4000 if (!ATReg) 4001 return true; 4002 4003 if (ImmValue == 0) { 4004 if (UseTraps) 4005 TOut.emitRRI(Mips::TEQ, ZeroReg, ZeroReg, 0x7, IDLoc, STI); 4006 else 4007 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 4008 return false; 4009 } 4010 4011 if (isRem && (ImmValue == 1 || (Signed && (ImmValue == -1)))) { 4012 TOut.emitRRR(Mips::OR, RdReg, ZeroReg, ZeroReg, IDLoc, STI); 4013 return false; 4014 } else if (isDiv && ImmValue == 1) { 4015 TOut.emitRRR(Mips::OR, RdReg, RsReg, Mips::ZERO, IDLoc, STI); 4016 return false; 4017 } else if (isDiv && Signed && ImmValue == -1) { 4018 TOut.emitRRR(SubOp, RdReg, ZeroReg, RsReg, IDLoc, STI); 4019 return false; 4020 } else { 4021 if (loadImmediate(ImmValue, ATReg, Mips::NoRegister, isInt<32>(ImmValue), 4022 false, Inst.getLoc(), Out, STI)) 4023 return true; 4024 TOut.emitRR(DivOp, RsReg, ATReg, IDLoc, STI); 4025 TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI); 4026 return false; 4027 } 4028 return true; 4029 } 4030 4031 // If the macro expansion of (d)div(u) or (d)rem(u) would always trap or 4032 // break, insert the trap/break and exit. This gives a different result to 4033 // GAS. GAS has an inconsistency/missed optimization in that not all cases 4034 // are handled equivalently. As the observed behaviour is the same, we're ok. 4035 if (RtReg == Mips::ZERO || RtReg == Mips::ZERO_64) { 4036 if (UseTraps) { 4037 TOut.emitRRI(Mips::TEQ, ZeroReg, ZeroReg, 0x7, IDLoc, STI); 4038 return false; 4039 } 4040 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 4041 return false; 4042 } 4043 4044 // (d)rem(u) $0, $X, $Y is a special case. Like div $zero, $X, $Y, it does 4045 // not expand to macro sequence. 4046 if (isRem && (RdReg == Mips::ZERO || RdReg == Mips::ZERO_64)) { 4047 TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI); 4048 return false; 4049 } 4050 4051 // Temporary label for first branch traget 4052 MCContext &Context = TOut.getStreamer().getContext(); 4053 MCSymbol *BrTarget; 4054 MCOperand LabelOp; 4055 4056 if (UseTraps) { 4057 TOut.emitRRI(Mips::TEQ, RtReg, ZeroReg, 0x7, IDLoc, STI); 4058 } else { 4059 // Branch to the li instruction. 4060 BrTarget = Context.createTempSymbol(); 4061 LabelOp = MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context)); 4062 TOut.emitRRX(Mips::BNE, RtReg, ZeroReg, LabelOp, IDLoc, STI); 4063 } 4064 4065 TOut.emitRR(DivOp, RsReg, RtReg, IDLoc, STI); 4066 4067 if (!UseTraps) 4068 TOut.emitII(Mips::BREAK, 0x7, 0, IDLoc, STI); 4069 4070 if (!Signed) { 4071 if (!UseTraps) 4072 TOut.getStreamer().EmitLabel(BrTarget); 4073 4074 TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI); 4075 return false; 4076 } 4077 4078 unsigned ATReg = getATReg(IDLoc); 4079 if (!ATReg) 4080 return true; 4081 4082 if (!UseTraps) 4083 TOut.getStreamer().EmitLabel(BrTarget); 4084 4085 TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, -1, IDLoc, STI); 4086 4087 // Temporary label for the second branch target. 4088 MCSymbol *BrTargetEnd = Context.createTempSymbol(); 4089 MCOperand LabelOpEnd = 4090 MCOperand::createExpr(MCSymbolRefExpr::create(BrTargetEnd, Context)); 4091 4092 // Branch to the mflo instruction. 4093 TOut.emitRRX(Mips::BNE, RtReg, ATReg, LabelOpEnd, IDLoc, STI); 4094 4095 if (IsMips64) { 4096 TOut.emitRRI(Mips::ADDiu, ATReg, ZeroReg, 1, IDLoc, STI); 4097 TOut.emitDSLL(ATReg, ATReg, 63, IDLoc, STI); 4098 } else { 4099 TOut.emitRI(Mips::LUi, ATReg, (uint16_t)0x8000, IDLoc, STI); 4100 } 4101 4102 if (UseTraps) 4103 TOut.emitRRI(Mips::TEQ, RsReg, ATReg, 0x6, IDLoc, STI); 4104 else { 4105 // Branch to the mflo instruction. 4106 TOut.emitRRX(Mips::BNE, RsReg, ATReg, LabelOpEnd, IDLoc, STI); 4107 TOut.emitNop(IDLoc, STI); 4108 TOut.emitII(Mips::BREAK, 0x6, 0, IDLoc, STI); 4109 } 4110 4111 TOut.getStreamer().EmitLabel(BrTargetEnd); 4112 TOut.emitR(isDiv ? Mips::MFLO : Mips::MFHI, RdReg, IDLoc, STI); 4113 return false; 4114 } 4115 4116 bool MipsAsmParser::expandTrunc(MCInst &Inst, bool IsDouble, bool Is64FPU, 4117 SMLoc IDLoc, MCStreamer &Out, 4118 const MCSubtargetInfo *STI) { 4119 MipsTargetStreamer &TOut = getTargetStreamer(); 4120 4121 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 4122 assert(Inst.getOperand(0).isReg() && Inst.getOperand(1).isReg() && 4123 Inst.getOperand(2).isReg() && "Invalid instruction operand."); 4124 4125 unsigned FirstReg = Inst.getOperand(0).getReg(); 4126 unsigned SecondReg = Inst.getOperand(1).getReg(); 4127 unsigned ThirdReg = Inst.getOperand(2).getReg(); 4128 4129 if (hasMips1() && !hasMips2()) { 4130 unsigned ATReg = getATReg(IDLoc); 4131 if (!ATReg) 4132 return true; 4133 TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI); 4134 TOut.emitRR(Mips::CFC1, ThirdReg, Mips::RA, IDLoc, STI); 4135 TOut.emitNop(IDLoc, STI); 4136 TOut.emitRRI(Mips::ORi, ATReg, ThirdReg, 0x3, IDLoc, STI); 4137 TOut.emitRRI(Mips::XORi, ATReg, ATReg, 0x2, IDLoc, STI); 4138 TOut.emitRR(Mips::CTC1, Mips::RA, ATReg, IDLoc, STI); 4139 TOut.emitNop(IDLoc, STI); 4140 TOut.emitRR(IsDouble ? (Is64FPU ? Mips::CVT_W_D64 : Mips::CVT_W_D32) 4141 : Mips::CVT_W_S, 4142 FirstReg, SecondReg, IDLoc, STI); 4143 TOut.emitRR(Mips::CTC1, Mips::RA, ThirdReg, IDLoc, STI); 4144 TOut.emitNop(IDLoc, STI); 4145 return false; 4146 } 4147 4148 TOut.emitRR(IsDouble ? (Is64FPU ? Mips::TRUNC_W_D64 : Mips::TRUNC_W_D32) 4149 : Mips::TRUNC_W_S, 4150 FirstReg, SecondReg, IDLoc, STI); 4151 4152 return false; 4153 } 4154 4155 bool MipsAsmParser::expandUlh(MCInst &Inst, bool Signed, SMLoc IDLoc, 4156 MCStreamer &Out, const MCSubtargetInfo *STI) { 4157 if (hasMips32r6() || hasMips64r6()) { 4158 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 4159 } 4160 4161 const MCOperand &DstRegOp = Inst.getOperand(0); 4162 assert(DstRegOp.isReg() && "expected register operand kind"); 4163 const MCOperand &SrcRegOp = Inst.getOperand(1); 4164 assert(SrcRegOp.isReg() && "expected register operand kind"); 4165 const MCOperand &OffsetImmOp = Inst.getOperand(2); 4166 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 4167 4168 MipsTargetStreamer &TOut = getTargetStreamer(); 4169 unsigned DstReg = DstRegOp.getReg(); 4170 unsigned SrcReg = SrcRegOp.getReg(); 4171 int64_t OffsetValue = OffsetImmOp.getImm(); 4172 4173 // NOTE: We always need AT for ULHU, as it is always used as the source 4174 // register for one of the LBu's. 4175 warnIfNoMacro(IDLoc); 4176 unsigned ATReg = getATReg(IDLoc); 4177 if (!ATReg) 4178 return true; 4179 4180 bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue)); 4181 if (IsLargeOffset) { 4182 if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true, 4183 IDLoc, Out, STI)) 4184 return true; 4185 } 4186 4187 int64_t FirstOffset = IsLargeOffset ? 0 : OffsetValue; 4188 int64_t SecondOffset = IsLargeOffset ? 1 : (OffsetValue + 1); 4189 if (isLittle()) 4190 std::swap(FirstOffset, SecondOffset); 4191 4192 unsigned FirstLbuDstReg = IsLargeOffset ? DstReg : ATReg; 4193 unsigned SecondLbuDstReg = IsLargeOffset ? ATReg : DstReg; 4194 4195 unsigned LbuSrcReg = IsLargeOffset ? ATReg : SrcReg; 4196 unsigned SllReg = IsLargeOffset ? DstReg : ATReg; 4197 4198 TOut.emitRRI(Signed ? Mips::LB : Mips::LBu, FirstLbuDstReg, LbuSrcReg, 4199 FirstOffset, IDLoc, STI); 4200 TOut.emitRRI(Mips::LBu, SecondLbuDstReg, LbuSrcReg, SecondOffset, IDLoc, STI); 4201 TOut.emitRRI(Mips::SLL, SllReg, SllReg, 8, IDLoc, STI); 4202 TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI); 4203 4204 return false; 4205 } 4206 4207 bool MipsAsmParser::expandUsh(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4208 const MCSubtargetInfo *STI) { 4209 if (hasMips32r6() || hasMips64r6()) { 4210 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 4211 } 4212 4213 const MCOperand &DstRegOp = Inst.getOperand(0); 4214 assert(DstRegOp.isReg() && "expected register operand kind"); 4215 const MCOperand &SrcRegOp = Inst.getOperand(1); 4216 assert(SrcRegOp.isReg() && "expected register operand kind"); 4217 const MCOperand &OffsetImmOp = Inst.getOperand(2); 4218 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 4219 4220 MipsTargetStreamer &TOut = getTargetStreamer(); 4221 unsigned DstReg = DstRegOp.getReg(); 4222 unsigned SrcReg = SrcRegOp.getReg(); 4223 int64_t OffsetValue = OffsetImmOp.getImm(); 4224 4225 warnIfNoMacro(IDLoc); 4226 unsigned ATReg = getATReg(IDLoc); 4227 if (!ATReg) 4228 return true; 4229 4230 bool IsLargeOffset = !(isInt<16>(OffsetValue + 1) && isInt<16>(OffsetValue)); 4231 if (IsLargeOffset) { 4232 if (loadImmediate(OffsetValue, ATReg, SrcReg, !ABI.ArePtrs64bit(), true, 4233 IDLoc, Out, STI)) 4234 return true; 4235 } 4236 4237 int64_t FirstOffset = IsLargeOffset ? 1 : (OffsetValue + 1); 4238 int64_t SecondOffset = IsLargeOffset ? 0 : OffsetValue; 4239 if (isLittle()) 4240 std::swap(FirstOffset, SecondOffset); 4241 4242 if (IsLargeOffset) { 4243 TOut.emitRRI(Mips::SB, DstReg, ATReg, FirstOffset, IDLoc, STI); 4244 TOut.emitRRI(Mips::SRL, DstReg, DstReg, 8, IDLoc, STI); 4245 TOut.emitRRI(Mips::SB, DstReg, ATReg, SecondOffset, IDLoc, STI); 4246 TOut.emitRRI(Mips::LBu, ATReg, ATReg, 0, IDLoc, STI); 4247 TOut.emitRRI(Mips::SLL, DstReg, DstReg, 8, IDLoc, STI); 4248 TOut.emitRRR(Mips::OR, DstReg, DstReg, ATReg, IDLoc, STI); 4249 } else { 4250 TOut.emitRRI(Mips::SB, DstReg, SrcReg, FirstOffset, IDLoc, STI); 4251 TOut.emitRRI(Mips::SRL, ATReg, DstReg, 8, IDLoc, STI); 4252 TOut.emitRRI(Mips::SB, ATReg, SrcReg, SecondOffset, IDLoc, STI); 4253 } 4254 4255 return false; 4256 } 4257 4258 bool MipsAsmParser::expandUxw(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4259 const MCSubtargetInfo *STI) { 4260 if (hasMips32r6() || hasMips64r6()) { 4261 return Error(IDLoc, "instruction not supported on mips32r6 or mips64r6"); 4262 } 4263 4264 const MCOperand &DstRegOp = Inst.getOperand(0); 4265 assert(DstRegOp.isReg() && "expected register operand kind"); 4266 const MCOperand &SrcRegOp = Inst.getOperand(1); 4267 assert(SrcRegOp.isReg() && "expected register operand kind"); 4268 const MCOperand &OffsetImmOp = Inst.getOperand(2); 4269 assert(OffsetImmOp.isImm() && "expected immediate operand kind"); 4270 4271 MipsTargetStreamer &TOut = getTargetStreamer(); 4272 unsigned DstReg = DstRegOp.getReg(); 4273 unsigned SrcReg = SrcRegOp.getReg(); 4274 int64_t OffsetValue = OffsetImmOp.getImm(); 4275 4276 // Compute left/right load/store offsets. 4277 bool IsLargeOffset = !(isInt<16>(OffsetValue + 3) && isInt<16>(OffsetValue)); 4278 int64_t LxlOffset = IsLargeOffset ? 0 : OffsetValue; 4279 int64_t LxrOffset = IsLargeOffset ? 3 : (OffsetValue + 3); 4280 if (isLittle()) 4281 std::swap(LxlOffset, LxrOffset); 4282 4283 bool IsLoadInst = (Inst.getOpcode() == Mips::Ulw); 4284 bool DoMove = IsLoadInst && (SrcReg == DstReg) && !IsLargeOffset; 4285 unsigned TmpReg = SrcReg; 4286 if (IsLargeOffset || DoMove) { 4287 warnIfNoMacro(IDLoc); 4288 TmpReg = getATReg(IDLoc); 4289 if (!TmpReg) 4290 return true; 4291 } 4292 4293 if (IsLargeOffset) { 4294 if (loadImmediate(OffsetValue, TmpReg, SrcReg, !ABI.ArePtrs64bit(), true, 4295 IDLoc, Out, STI)) 4296 return true; 4297 } 4298 4299 if (DoMove) 4300 std::swap(DstReg, TmpReg); 4301 4302 unsigned XWL = IsLoadInst ? Mips::LWL : Mips::SWL; 4303 unsigned XWR = IsLoadInst ? Mips::LWR : Mips::SWR; 4304 TOut.emitRRI(XWL, DstReg, TmpReg, LxlOffset, IDLoc, STI); 4305 TOut.emitRRI(XWR, DstReg, TmpReg, LxrOffset, IDLoc, STI); 4306 4307 if (DoMove) 4308 TOut.emitRRR(Mips::OR, TmpReg, DstReg, Mips::ZERO, IDLoc, STI); 4309 4310 return false; 4311 } 4312 4313 bool MipsAsmParser::expandSge(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4314 const MCSubtargetInfo *STI) { 4315 MipsTargetStreamer &TOut = getTargetStreamer(); 4316 4317 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 4318 assert(Inst.getOperand(0).isReg() && 4319 Inst.getOperand(1).isReg() && 4320 Inst.getOperand(2).isReg() && "Invalid instruction operand."); 4321 4322 unsigned DstReg = Inst.getOperand(0).getReg(); 4323 unsigned SrcReg = Inst.getOperand(1).getReg(); 4324 unsigned OpReg = Inst.getOperand(2).getReg(); 4325 unsigned OpCode; 4326 4327 warnIfNoMacro(IDLoc); 4328 4329 switch (Inst.getOpcode()) { 4330 case Mips::SGE: 4331 OpCode = Mips::SLT; 4332 break; 4333 case Mips::SGEU: 4334 OpCode = Mips::SLTu; 4335 break; 4336 default: 4337 llvm_unreachable("unexpected 'sge' opcode"); 4338 } 4339 4340 // $SrcReg >= $OpReg is equal to (not ($SrcReg < $OpReg)) 4341 TOut.emitRRR(OpCode, DstReg, SrcReg, OpReg, IDLoc, STI); 4342 TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI); 4343 4344 return false; 4345 } 4346 4347 bool MipsAsmParser::expandSgeImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4348 const MCSubtargetInfo *STI) { 4349 MipsTargetStreamer &TOut = getTargetStreamer(); 4350 4351 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 4352 assert(Inst.getOperand(0).isReg() && 4353 Inst.getOperand(1).isReg() && 4354 Inst.getOperand(2).isImm() && "Invalid instruction operand."); 4355 4356 unsigned DstReg = Inst.getOperand(0).getReg(); 4357 unsigned SrcReg = Inst.getOperand(1).getReg(); 4358 int64_t ImmValue = Inst.getOperand(2).getImm(); 4359 unsigned OpRegCode, OpImmCode; 4360 4361 warnIfNoMacro(IDLoc); 4362 4363 switch (Inst.getOpcode()) { 4364 case Mips::SGEImm: 4365 case Mips::SGEImm64: 4366 OpRegCode = Mips::SLT; 4367 OpImmCode = Mips::SLTi; 4368 break; 4369 case Mips::SGEUImm: 4370 case Mips::SGEUImm64: 4371 OpRegCode = Mips::SLTu; 4372 OpImmCode = Mips::SLTiu; 4373 break; 4374 default: 4375 llvm_unreachable("unexpected 'sge' opcode with immediate"); 4376 } 4377 4378 // $SrcReg >= Imm is equal to (not ($SrcReg < Imm)) 4379 if (isInt<16>(ImmValue)) { 4380 // Use immediate version of STL. 4381 TOut.emitRRI(OpImmCode, DstReg, SrcReg, ImmValue, IDLoc, STI); 4382 TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI); 4383 } else { 4384 unsigned ImmReg = DstReg; 4385 if (DstReg == SrcReg) { 4386 unsigned ATReg = getATReg(Inst.getLoc()); 4387 if (!ATReg) 4388 return true; 4389 ImmReg = ATReg; 4390 } 4391 4392 if (loadImmediate(ImmValue, ImmReg, Mips::NoRegister, isInt<32>(ImmValue), 4393 false, IDLoc, Out, STI)) 4394 return true; 4395 4396 TOut.emitRRR(OpRegCode, DstReg, SrcReg, ImmReg, IDLoc, STI); 4397 TOut.emitRRI(Mips::XORi, DstReg, DstReg, 1, IDLoc, STI); 4398 } 4399 4400 return false; 4401 } 4402 4403 bool MipsAsmParser::expandSgtImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4404 const MCSubtargetInfo *STI) { 4405 MipsTargetStreamer &TOut = getTargetStreamer(); 4406 4407 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 4408 assert(Inst.getOperand(0).isReg() && 4409 Inst.getOperand(1).isReg() && 4410 Inst.getOperand(2).isImm() && "Invalid instruction operand."); 4411 4412 unsigned DstReg = Inst.getOperand(0).getReg(); 4413 unsigned SrcReg = Inst.getOperand(1).getReg(); 4414 unsigned ImmReg = DstReg; 4415 int64_t ImmValue = Inst.getOperand(2).getImm(); 4416 unsigned OpCode; 4417 4418 warnIfNoMacro(IDLoc); 4419 4420 switch (Inst.getOpcode()) { 4421 case Mips::SGTImm: 4422 case Mips::SGTImm64: 4423 OpCode = Mips::SLT; 4424 break; 4425 case Mips::SGTUImm: 4426 case Mips::SGTUImm64: 4427 OpCode = Mips::SLTu; 4428 break; 4429 default: 4430 llvm_unreachable("unexpected 'sgt' opcode with immediate"); 4431 } 4432 4433 if (DstReg == SrcReg) { 4434 unsigned ATReg = getATReg(Inst.getLoc()); 4435 if (!ATReg) 4436 return true; 4437 ImmReg = ATReg; 4438 } 4439 4440 if (loadImmediate(ImmValue, ImmReg, Mips::NoRegister, isInt<32>(ImmValue), 4441 false, IDLoc, Out, STI)) 4442 return true; 4443 4444 // $SrcReg > $ImmReg is equal to $ImmReg < $SrcReg 4445 TOut.emitRRR(OpCode, DstReg, ImmReg, SrcReg, IDLoc, STI); 4446 4447 return false; 4448 } 4449 4450 bool MipsAsmParser::expandAliasImmediate(MCInst &Inst, SMLoc IDLoc, 4451 MCStreamer &Out, 4452 const MCSubtargetInfo *STI) { 4453 MipsTargetStreamer &TOut = getTargetStreamer(); 4454 4455 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 4456 assert(Inst.getOperand(0).isReg() && 4457 Inst.getOperand(1).isReg() && 4458 Inst.getOperand(2).isImm() && "Invalid instruction operand."); 4459 4460 unsigned ATReg = Mips::NoRegister; 4461 unsigned FinalDstReg = Mips::NoRegister; 4462 unsigned DstReg = Inst.getOperand(0).getReg(); 4463 unsigned SrcReg = Inst.getOperand(1).getReg(); 4464 int64_t ImmValue = Inst.getOperand(2).getImm(); 4465 4466 bool Is32Bit = isInt<32>(ImmValue) || (!isGP64bit() && isUInt<32>(ImmValue)); 4467 4468 unsigned FinalOpcode = Inst.getOpcode(); 4469 4470 if (DstReg == SrcReg) { 4471 ATReg = getATReg(Inst.getLoc()); 4472 if (!ATReg) 4473 return true; 4474 FinalDstReg = DstReg; 4475 DstReg = ATReg; 4476 } 4477 4478 if (!loadImmediate(ImmValue, DstReg, Mips::NoRegister, Is32Bit, false, 4479 Inst.getLoc(), Out, STI)) { 4480 switch (FinalOpcode) { 4481 default: 4482 llvm_unreachable("unimplemented expansion"); 4483 case Mips::ADDi: 4484 FinalOpcode = Mips::ADD; 4485 break; 4486 case Mips::ADDiu: 4487 FinalOpcode = Mips::ADDu; 4488 break; 4489 case Mips::ANDi: 4490 FinalOpcode = Mips::AND; 4491 break; 4492 case Mips::NORImm: 4493 FinalOpcode = Mips::NOR; 4494 break; 4495 case Mips::ORi: 4496 FinalOpcode = Mips::OR; 4497 break; 4498 case Mips::SLTi: 4499 FinalOpcode = Mips::SLT; 4500 break; 4501 case Mips::SLTiu: 4502 FinalOpcode = Mips::SLTu; 4503 break; 4504 case Mips::XORi: 4505 FinalOpcode = Mips::XOR; 4506 break; 4507 case Mips::ADDi_MM: 4508 FinalOpcode = Mips::ADD_MM; 4509 break; 4510 case Mips::ADDiu_MM: 4511 FinalOpcode = Mips::ADDu_MM; 4512 break; 4513 case Mips::ANDi_MM: 4514 FinalOpcode = Mips::AND_MM; 4515 break; 4516 case Mips::ORi_MM: 4517 FinalOpcode = Mips::OR_MM; 4518 break; 4519 case Mips::SLTi_MM: 4520 FinalOpcode = Mips::SLT_MM; 4521 break; 4522 case Mips::SLTiu_MM: 4523 FinalOpcode = Mips::SLTu_MM; 4524 break; 4525 case Mips::XORi_MM: 4526 FinalOpcode = Mips::XOR_MM; 4527 break; 4528 case Mips::ANDi64: 4529 FinalOpcode = Mips::AND64; 4530 break; 4531 case Mips::NORImm64: 4532 FinalOpcode = Mips::NOR64; 4533 break; 4534 case Mips::ORi64: 4535 FinalOpcode = Mips::OR64; 4536 break; 4537 case Mips::SLTImm64: 4538 FinalOpcode = Mips::SLT64; 4539 break; 4540 case Mips::SLTUImm64: 4541 FinalOpcode = Mips::SLTu64; 4542 break; 4543 case Mips::XORi64: 4544 FinalOpcode = Mips::XOR64; 4545 break; 4546 } 4547 4548 if (FinalDstReg == Mips::NoRegister) 4549 TOut.emitRRR(FinalOpcode, DstReg, DstReg, SrcReg, IDLoc, STI); 4550 else 4551 TOut.emitRRR(FinalOpcode, FinalDstReg, FinalDstReg, DstReg, IDLoc, STI); 4552 return false; 4553 } 4554 return true; 4555 } 4556 4557 bool MipsAsmParser::expandRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4558 const MCSubtargetInfo *STI) { 4559 MipsTargetStreamer &TOut = getTargetStreamer(); 4560 unsigned ATReg = Mips::NoRegister; 4561 unsigned DReg = Inst.getOperand(0).getReg(); 4562 unsigned SReg = Inst.getOperand(1).getReg(); 4563 unsigned TReg = Inst.getOperand(2).getReg(); 4564 unsigned TmpReg = DReg; 4565 4566 unsigned FirstShift = Mips::NOP; 4567 unsigned SecondShift = Mips::NOP; 4568 4569 if (hasMips32r2()) { 4570 if (DReg == SReg) { 4571 TmpReg = getATReg(Inst.getLoc()); 4572 if (!TmpReg) 4573 return true; 4574 } 4575 4576 if (Inst.getOpcode() == Mips::ROL) { 4577 TOut.emitRRR(Mips::SUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 4578 TOut.emitRRR(Mips::ROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI); 4579 return false; 4580 } 4581 4582 if (Inst.getOpcode() == Mips::ROR) { 4583 TOut.emitRRR(Mips::ROTRV, DReg, SReg, TReg, Inst.getLoc(), STI); 4584 return false; 4585 } 4586 4587 return true; 4588 } 4589 4590 if (hasMips32()) { 4591 switch (Inst.getOpcode()) { 4592 default: 4593 llvm_unreachable("unexpected instruction opcode"); 4594 case Mips::ROL: 4595 FirstShift = Mips::SRLV; 4596 SecondShift = Mips::SLLV; 4597 break; 4598 case Mips::ROR: 4599 FirstShift = Mips::SLLV; 4600 SecondShift = Mips::SRLV; 4601 break; 4602 } 4603 4604 ATReg = getATReg(Inst.getLoc()); 4605 if (!ATReg) 4606 return true; 4607 4608 TOut.emitRRR(Mips::SUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 4609 TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI); 4610 TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI); 4611 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 4612 4613 return false; 4614 } 4615 4616 return true; 4617 } 4618 4619 bool MipsAsmParser::expandRotationImm(MCInst &Inst, SMLoc IDLoc, 4620 MCStreamer &Out, 4621 const MCSubtargetInfo *STI) { 4622 MipsTargetStreamer &TOut = getTargetStreamer(); 4623 unsigned ATReg = Mips::NoRegister; 4624 unsigned DReg = Inst.getOperand(0).getReg(); 4625 unsigned SReg = Inst.getOperand(1).getReg(); 4626 int64_t ImmValue = Inst.getOperand(2).getImm(); 4627 4628 unsigned FirstShift = Mips::NOP; 4629 unsigned SecondShift = Mips::NOP; 4630 4631 if (hasMips32r2()) { 4632 if (Inst.getOpcode() == Mips::ROLImm) { 4633 uint64_t MaxShift = 32; 4634 uint64_t ShiftValue = ImmValue; 4635 if (ImmValue != 0) 4636 ShiftValue = MaxShift - ImmValue; 4637 TOut.emitRRI(Mips::ROTR, DReg, SReg, ShiftValue, Inst.getLoc(), STI); 4638 return false; 4639 } 4640 4641 if (Inst.getOpcode() == Mips::RORImm) { 4642 TOut.emitRRI(Mips::ROTR, DReg, SReg, ImmValue, Inst.getLoc(), STI); 4643 return false; 4644 } 4645 4646 return true; 4647 } 4648 4649 if (hasMips32()) { 4650 if (ImmValue == 0) { 4651 TOut.emitRRI(Mips::SRL, DReg, SReg, 0, Inst.getLoc(), STI); 4652 return false; 4653 } 4654 4655 switch (Inst.getOpcode()) { 4656 default: 4657 llvm_unreachable("unexpected instruction opcode"); 4658 case Mips::ROLImm: 4659 FirstShift = Mips::SLL; 4660 SecondShift = Mips::SRL; 4661 break; 4662 case Mips::RORImm: 4663 FirstShift = Mips::SRL; 4664 SecondShift = Mips::SLL; 4665 break; 4666 } 4667 4668 ATReg = getATReg(Inst.getLoc()); 4669 if (!ATReg) 4670 return true; 4671 4672 TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue, Inst.getLoc(), STI); 4673 TOut.emitRRI(SecondShift, DReg, SReg, 32 - ImmValue, Inst.getLoc(), STI); 4674 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 4675 4676 return false; 4677 } 4678 4679 return true; 4680 } 4681 4682 bool MipsAsmParser::expandDRotation(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4683 const MCSubtargetInfo *STI) { 4684 MipsTargetStreamer &TOut = getTargetStreamer(); 4685 unsigned ATReg = Mips::NoRegister; 4686 unsigned DReg = Inst.getOperand(0).getReg(); 4687 unsigned SReg = Inst.getOperand(1).getReg(); 4688 unsigned TReg = Inst.getOperand(2).getReg(); 4689 unsigned TmpReg = DReg; 4690 4691 unsigned FirstShift = Mips::NOP; 4692 unsigned SecondShift = Mips::NOP; 4693 4694 if (hasMips64r2()) { 4695 if (TmpReg == SReg) { 4696 TmpReg = getATReg(Inst.getLoc()); 4697 if (!TmpReg) 4698 return true; 4699 } 4700 4701 if (Inst.getOpcode() == Mips::DROL) { 4702 TOut.emitRRR(Mips::DSUBu, TmpReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 4703 TOut.emitRRR(Mips::DROTRV, DReg, SReg, TmpReg, Inst.getLoc(), STI); 4704 return false; 4705 } 4706 4707 if (Inst.getOpcode() == Mips::DROR) { 4708 TOut.emitRRR(Mips::DROTRV, DReg, SReg, TReg, Inst.getLoc(), STI); 4709 return false; 4710 } 4711 4712 return true; 4713 } 4714 4715 if (hasMips64()) { 4716 switch (Inst.getOpcode()) { 4717 default: 4718 llvm_unreachable("unexpected instruction opcode"); 4719 case Mips::DROL: 4720 FirstShift = Mips::DSRLV; 4721 SecondShift = Mips::DSLLV; 4722 break; 4723 case Mips::DROR: 4724 FirstShift = Mips::DSLLV; 4725 SecondShift = Mips::DSRLV; 4726 break; 4727 } 4728 4729 ATReg = getATReg(Inst.getLoc()); 4730 if (!ATReg) 4731 return true; 4732 4733 TOut.emitRRR(Mips::DSUBu, ATReg, Mips::ZERO, TReg, Inst.getLoc(), STI); 4734 TOut.emitRRR(FirstShift, ATReg, SReg, ATReg, Inst.getLoc(), STI); 4735 TOut.emitRRR(SecondShift, DReg, SReg, TReg, Inst.getLoc(), STI); 4736 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 4737 4738 return false; 4739 } 4740 4741 return true; 4742 } 4743 4744 bool MipsAsmParser::expandDRotationImm(MCInst &Inst, SMLoc IDLoc, 4745 MCStreamer &Out, 4746 const MCSubtargetInfo *STI) { 4747 MipsTargetStreamer &TOut = getTargetStreamer(); 4748 unsigned ATReg = Mips::NoRegister; 4749 unsigned DReg = Inst.getOperand(0).getReg(); 4750 unsigned SReg = Inst.getOperand(1).getReg(); 4751 int64_t ImmValue = Inst.getOperand(2).getImm() % 64; 4752 4753 unsigned FirstShift = Mips::NOP; 4754 unsigned SecondShift = Mips::NOP; 4755 4756 MCInst TmpInst; 4757 4758 if (hasMips64r2()) { 4759 unsigned FinalOpcode = Mips::NOP; 4760 if (ImmValue == 0) 4761 FinalOpcode = Mips::DROTR; 4762 else if (ImmValue % 32 == 0) 4763 FinalOpcode = Mips::DROTR32; 4764 else if ((ImmValue >= 1) && (ImmValue <= 32)) { 4765 if (Inst.getOpcode() == Mips::DROLImm) 4766 FinalOpcode = Mips::DROTR32; 4767 else 4768 FinalOpcode = Mips::DROTR; 4769 } else if (ImmValue >= 33) { 4770 if (Inst.getOpcode() == Mips::DROLImm) 4771 FinalOpcode = Mips::DROTR; 4772 else 4773 FinalOpcode = Mips::DROTR32; 4774 } 4775 4776 uint64_t ShiftValue = ImmValue % 32; 4777 if (Inst.getOpcode() == Mips::DROLImm) 4778 ShiftValue = (32 - ImmValue % 32) % 32; 4779 4780 TOut.emitRRI(FinalOpcode, DReg, SReg, ShiftValue, Inst.getLoc(), STI); 4781 4782 return false; 4783 } 4784 4785 if (hasMips64()) { 4786 if (ImmValue == 0) { 4787 TOut.emitRRI(Mips::DSRL, DReg, SReg, 0, Inst.getLoc(), STI); 4788 return false; 4789 } 4790 4791 switch (Inst.getOpcode()) { 4792 default: 4793 llvm_unreachable("unexpected instruction opcode"); 4794 case Mips::DROLImm: 4795 if ((ImmValue >= 1) && (ImmValue <= 31)) { 4796 FirstShift = Mips::DSLL; 4797 SecondShift = Mips::DSRL32; 4798 } 4799 if (ImmValue == 32) { 4800 FirstShift = Mips::DSLL32; 4801 SecondShift = Mips::DSRL32; 4802 } 4803 if ((ImmValue >= 33) && (ImmValue <= 63)) { 4804 FirstShift = Mips::DSLL32; 4805 SecondShift = Mips::DSRL; 4806 } 4807 break; 4808 case Mips::DRORImm: 4809 if ((ImmValue >= 1) && (ImmValue <= 31)) { 4810 FirstShift = Mips::DSRL; 4811 SecondShift = Mips::DSLL32; 4812 } 4813 if (ImmValue == 32) { 4814 FirstShift = Mips::DSRL32; 4815 SecondShift = Mips::DSLL32; 4816 } 4817 if ((ImmValue >= 33) && (ImmValue <= 63)) { 4818 FirstShift = Mips::DSRL32; 4819 SecondShift = Mips::DSLL; 4820 } 4821 break; 4822 } 4823 4824 ATReg = getATReg(Inst.getLoc()); 4825 if (!ATReg) 4826 return true; 4827 4828 TOut.emitRRI(FirstShift, ATReg, SReg, ImmValue % 32, Inst.getLoc(), STI); 4829 TOut.emitRRI(SecondShift, DReg, SReg, (32 - ImmValue % 32) % 32, 4830 Inst.getLoc(), STI); 4831 TOut.emitRRR(Mips::OR, DReg, DReg, ATReg, Inst.getLoc(), STI); 4832 4833 return false; 4834 } 4835 4836 return true; 4837 } 4838 4839 bool MipsAsmParser::expandAbs(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4840 const MCSubtargetInfo *STI) { 4841 MipsTargetStreamer &TOut = getTargetStreamer(); 4842 unsigned FirstRegOp = Inst.getOperand(0).getReg(); 4843 unsigned SecondRegOp = Inst.getOperand(1).getReg(); 4844 4845 TOut.emitRI(Mips::BGEZ, SecondRegOp, 8, IDLoc, STI); 4846 if (FirstRegOp != SecondRegOp) 4847 TOut.emitRRR(Mips::ADDu, FirstRegOp, SecondRegOp, Mips::ZERO, IDLoc, STI); 4848 else 4849 TOut.emitEmptyDelaySlot(false, IDLoc, STI); 4850 TOut.emitRRR(Mips::SUB, FirstRegOp, Mips::ZERO, SecondRegOp, IDLoc, STI); 4851 4852 return false; 4853 } 4854 4855 bool MipsAsmParser::expandMulImm(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4856 const MCSubtargetInfo *STI) { 4857 MipsTargetStreamer &TOut = getTargetStreamer(); 4858 unsigned ATReg = Mips::NoRegister; 4859 unsigned DstReg = Inst.getOperand(0).getReg(); 4860 unsigned SrcReg = Inst.getOperand(1).getReg(); 4861 int32_t ImmValue = Inst.getOperand(2).getImm(); 4862 4863 ATReg = getATReg(IDLoc); 4864 if (!ATReg) 4865 return true; 4866 4867 loadImmediate(ImmValue, ATReg, Mips::NoRegister, true, false, IDLoc, Out, 4868 STI); 4869 4870 TOut.emitRR(Inst.getOpcode() == Mips::MULImmMacro ? Mips::MULT : Mips::DMULT, 4871 SrcReg, ATReg, IDLoc, STI); 4872 4873 TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI); 4874 4875 return false; 4876 } 4877 4878 bool MipsAsmParser::expandMulO(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4879 const MCSubtargetInfo *STI) { 4880 MipsTargetStreamer &TOut = getTargetStreamer(); 4881 unsigned ATReg = Mips::NoRegister; 4882 unsigned DstReg = Inst.getOperand(0).getReg(); 4883 unsigned SrcReg = Inst.getOperand(1).getReg(); 4884 unsigned TmpReg = Inst.getOperand(2).getReg(); 4885 4886 ATReg = getATReg(Inst.getLoc()); 4887 if (!ATReg) 4888 return true; 4889 4890 TOut.emitRR(Inst.getOpcode() == Mips::MULOMacro ? Mips::MULT : Mips::DMULT, 4891 SrcReg, TmpReg, IDLoc, STI); 4892 4893 TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI); 4894 4895 TOut.emitRRI(Inst.getOpcode() == Mips::MULOMacro ? Mips::SRA : Mips::DSRA32, 4896 DstReg, DstReg, 0x1F, IDLoc, STI); 4897 4898 TOut.emitR(Mips::MFHI, ATReg, IDLoc, STI); 4899 4900 if (useTraps()) { 4901 TOut.emitRRI(Mips::TNE, DstReg, ATReg, 6, IDLoc, STI); 4902 } else { 4903 MCContext & Context = TOut.getStreamer().getContext(); 4904 MCSymbol * BrTarget = Context.createTempSymbol(); 4905 MCOperand LabelOp = 4906 MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context)); 4907 4908 TOut.emitRRX(Mips::BEQ, DstReg, ATReg, LabelOp, IDLoc, STI); 4909 if (AssemblerOptions.back()->isReorder()) 4910 TOut.emitNop(IDLoc, STI); 4911 TOut.emitII(Mips::BREAK, 6, 0, IDLoc, STI); 4912 4913 TOut.getStreamer().EmitLabel(BrTarget); 4914 } 4915 TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI); 4916 4917 return false; 4918 } 4919 4920 bool MipsAsmParser::expandMulOU(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4921 const MCSubtargetInfo *STI) { 4922 MipsTargetStreamer &TOut = getTargetStreamer(); 4923 unsigned ATReg = Mips::NoRegister; 4924 unsigned DstReg = Inst.getOperand(0).getReg(); 4925 unsigned SrcReg = Inst.getOperand(1).getReg(); 4926 unsigned TmpReg = Inst.getOperand(2).getReg(); 4927 4928 ATReg = getATReg(IDLoc); 4929 if (!ATReg) 4930 return true; 4931 4932 TOut.emitRR(Inst.getOpcode() == Mips::MULOUMacro ? Mips::MULTu : Mips::DMULTu, 4933 SrcReg, TmpReg, IDLoc, STI); 4934 4935 TOut.emitR(Mips::MFHI, ATReg, IDLoc, STI); 4936 TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI); 4937 if (useTraps()) { 4938 TOut.emitRRI(Mips::TNE, ATReg, Mips::ZERO, 6, IDLoc, STI); 4939 } else { 4940 MCContext & Context = TOut.getStreamer().getContext(); 4941 MCSymbol * BrTarget = Context.createTempSymbol(); 4942 MCOperand LabelOp = 4943 MCOperand::createExpr(MCSymbolRefExpr::create(BrTarget, Context)); 4944 4945 TOut.emitRRX(Mips::BEQ, ATReg, Mips::ZERO, LabelOp, IDLoc, STI); 4946 if (AssemblerOptions.back()->isReorder()) 4947 TOut.emitNop(IDLoc, STI); 4948 TOut.emitII(Mips::BREAK, 6, 0, IDLoc, STI); 4949 4950 TOut.getStreamer().EmitLabel(BrTarget); 4951 } 4952 4953 return false; 4954 } 4955 4956 bool MipsAsmParser::expandDMULMacro(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 4957 const MCSubtargetInfo *STI) { 4958 MipsTargetStreamer &TOut = getTargetStreamer(); 4959 unsigned DstReg = Inst.getOperand(0).getReg(); 4960 unsigned SrcReg = Inst.getOperand(1).getReg(); 4961 unsigned TmpReg = Inst.getOperand(2).getReg(); 4962 4963 TOut.emitRR(Mips::DMULTu, SrcReg, TmpReg, IDLoc, STI); 4964 TOut.emitR(Mips::MFLO, DstReg, IDLoc, STI); 4965 4966 return false; 4967 } 4968 4969 // Expand 'ld $<reg> offset($reg2)' to 'lw $<reg>, offset($reg2); 4970 // lw $<reg+1>>, offset+4($reg2)' 4971 // or expand 'sd $<reg> offset($reg2)' to 'sw $<reg>, offset($reg2); 4972 // sw $<reg+1>>, offset+4($reg2)' 4973 // for O32. 4974 bool MipsAsmParser::expandLoadStoreDMacro(MCInst &Inst, SMLoc IDLoc, 4975 MCStreamer &Out, 4976 const MCSubtargetInfo *STI, 4977 bool IsLoad) { 4978 if (!isABI_O32()) 4979 return true; 4980 4981 warnIfNoMacro(IDLoc); 4982 4983 MipsTargetStreamer &TOut = getTargetStreamer(); 4984 unsigned Opcode = IsLoad ? Mips::LW : Mips::SW; 4985 unsigned FirstReg = Inst.getOperand(0).getReg(); 4986 unsigned SecondReg = nextReg(FirstReg); 4987 unsigned BaseReg = Inst.getOperand(1).getReg(); 4988 if (!SecondReg) 4989 return true; 4990 4991 warnIfRegIndexIsAT(FirstReg, IDLoc); 4992 4993 assert(Inst.getOperand(2).isImm() && 4994 "Offset for load macro is not immediate!"); 4995 4996 MCOperand &FirstOffset = Inst.getOperand(2); 4997 signed NextOffset = FirstOffset.getImm() + 4; 4998 MCOperand SecondOffset = MCOperand::createImm(NextOffset); 4999 5000 if (!isInt<16>(FirstOffset.getImm()) || !isInt<16>(NextOffset)) 5001 return true; 5002 5003 // For loads, clobber the base register with the second load instead of the 5004 // first if the BaseReg == FirstReg. 5005 if (FirstReg != BaseReg || !IsLoad) { 5006 TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI); 5007 TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI); 5008 } else { 5009 TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI); 5010 TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI); 5011 } 5012 5013 return false; 5014 } 5015 5016 5017 // Expand 's.d $<reg> offset($reg2)' to 'swc1 $<reg+1>, offset($reg2); 5018 // swc1 $<reg>, offset+4($reg2)' 5019 // or if little endian to 'swc1 $<reg>, offset($reg2); 5020 // swc1 $<reg+1>, offset+4($reg2)' 5021 // for Mips1. 5022 bool MipsAsmParser::expandStoreDM1Macro(MCInst &Inst, SMLoc IDLoc, 5023 MCStreamer &Out, 5024 const MCSubtargetInfo *STI) { 5025 if (!isABI_O32()) 5026 return true; 5027 5028 warnIfNoMacro(IDLoc); 5029 5030 MipsTargetStreamer &TOut = getTargetStreamer(); 5031 unsigned Opcode = Mips::SWC1; 5032 unsigned FirstReg = Inst.getOperand(0).getReg(); 5033 unsigned SecondReg = nextReg(FirstReg); 5034 unsigned BaseReg = Inst.getOperand(1).getReg(); 5035 if (!SecondReg) 5036 return true; 5037 5038 warnIfRegIndexIsAT(FirstReg, IDLoc); 5039 5040 assert(Inst.getOperand(2).isImm() && 5041 "Offset for macro is not immediate!"); 5042 5043 MCOperand &FirstOffset = Inst.getOperand(2); 5044 signed NextOffset = FirstOffset.getImm() + 4; 5045 MCOperand SecondOffset = MCOperand::createImm(NextOffset); 5046 5047 if (!isInt<16>(FirstOffset.getImm()) || !isInt<16>(NextOffset)) 5048 return true; 5049 5050 if (!IsLittleEndian) 5051 std::swap(FirstReg, SecondReg); 5052 5053 TOut.emitRRX(Opcode, FirstReg, BaseReg, FirstOffset, IDLoc, STI); 5054 TOut.emitRRX(Opcode, SecondReg, BaseReg, SecondOffset, IDLoc, STI); 5055 5056 return false; 5057 } 5058 5059 bool MipsAsmParser::expandSeq(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 5060 const MCSubtargetInfo *STI) { 5061 MipsTargetStreamer &TOut = getTargetStreamer(); 5062 5063 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 5064 assert(Inst.getOperand(0).isReg() && 5065 Inst.getOperand(1).isReg() && 5066 Inst.getOperand(2).isReg() && "Invalid instruction operand."); 5067 5068 unsigned DstReg = Inst.getOperand(0).getReg(); 5069 unsigned SrcReg = Inst.getOperand(1).getReg(); 5070 unsigned OpReg = Inst.getOperand(2).getReg(); 5071 5072 warnIfNoMacro(IDLoc); 5073 5074 if (SrcReg != Mips::ZERO && OpReg != Mips::ZERO) { 5075 TOut.emitRRR(Mips::XOR, DstReg, SrcReg, OpReg, IDLoc, STI); 5076 TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI); 5077 return false; 5078 } 5079 5080 unsigned Reg = SrcReg == Mips::ZERO ? OpReg : SrcReg; 5081 TOut.emitRRI(Mips::SLTiu, DstReg, Reg, 1, IDLoc, STI); 5082 return false; 5083 } 5084 5085 bool MipsAsmParser::expandSeqI(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 5086 const MCSubtargetInfo *STI) { 5087 MipsTargetStreamer &TOut = getTargetStreamer(); 5088 5089 assert(Inst.getNumOperands() == 3 && "Invalid operand count"); 5090 assert(Inst.getOperand(0).isReg() && 5091 Inst.getOperand(1).isReg() && 5092 Inst.getOperand(2).isImm() && "Invalid instruction operand."); 5093 5094 unsigned DstReg = Inst.getOperand(0).getReg(); 5095 unsigned SrcReg = Inst.getOperand(1).getReg(); 5096 int64_t Imm = Inst.getOperand(2).getImm(); 5097 5098 warnIfNoMacro(IDLoc); 5099 5100 if (Imm == 0) { 5101 TOut.emitRRI(Mips::SLTiu, DstReg, SrcReg, 1, IDLoc, STI); 5102 return false; 5103 } 5104 5105 if (SrcReg == Mips::ZERO) { 5106 Warning(IDLoc, "comparison is always false"); 5107 TOut.emitRRR(isGP64bit() ? Mips::DADDu : Mips::ADDu, 5108 DstReg, SrcReg, SrcReg, IDLoc, STI); 5109 return false; 5110 } 5111 5112 unsigned Opc; 5113 if (Imm > -0x8000 && Imm < 0) { 5114 Imm = -Imm; 5115 Opc = isGP64bit() ? Mips::DADDiu : Mips::ADDiu; 5116 } else { 5117 Opc = Mips::XORi; 5118 } 5119 5120 if (!isUInt<16>(Imm)) { 5121 unsigned ATReg = getATReg(IDLoc); 5122 if (!ATReg) 5123 return true; 5124 5125 if (loadImmediate(Imm, ATReg, Mips::NoRegister, true, isGP64bit(), IDLoc, 5126 Out, STI)) 5127 return true; 5128 5129 TOut.emitRRR(Mips::XOR, DstReg, SrcReg, ATReg, IDLoc, STI); 5130 TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI); 5131 return false; 5132 } 5133 5134 TOut.emitRRI(Opc, DstReg, SrcReg, Imm, IDLoc, STI); 5135 TOut.emitRRI(Mips::SLTiu, DstReg, DstReg, 1, IDLoc, STI); 5136 return false; 5137 } 5138 5139 // Map the DSP accumulator and control register to the corresponding gpr 5140 // operand. Unlike the other alias, the m(f|t)t(lo|hi|acx) instructions 5141 // do not map the DSP registers contigously to gpr registers. 5142 static unsigned getRegisterForMxtrDSP(MCInst &Inst, bool IsMFDSP) { 5143 switch (Inst.getOpcode()) { 5144 case Mips::MFTLO: 5145 case Mips::MTTLO: 5146 switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) { 5147 case Mips::AC0: 5148 return Mips::ZERO; 5149 case Mips::AC1: 5150 return Mips::A0; 5151 case Mips::AC2: 5152 return Mips::T0; 5153 case Mips::AC3: 5154 return Mips::T4; 5155 default: 5156 llvm_unreachable("Unknown register for 'mttr' alias!"); 5157 } 5158 case Mips::MFTHI: 5159 case Mips::MTTHI: 5160 switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) { 5161 case Mips::AC0: 5162 return Mips::AT; 5163 case Mips::AC1: 5164 return Mips::A1; 5165 case Mips::AC2: 5166 return Mips::T1; 5167 case Mips::AC3: 5168 return Mips::T5; 5169 default: 5170 llvm_unreachable("Unknown register for 'mttr' alias!"); 5171 } 5172 case Mips::MFTACX: 5173 case Mips::MTTACX: 5174 switch (Inst.getOperand(IsMFDSP ? 1 : 0).getReg()) { 5175 case Mips::AC0: 5176 return Mips::V0; 5177 case Mips::AC1: 5178 return Mips::A2; 5179 case Mips::AC2: 5180 return Mips::T2; 5181 case Mips::AC3: 5182 return Mips::T6; 5183 default: 5184 llvm_unreachable("Unknown register for 'mttr' alias!"); 5185 } 5186 case Mips::MFTDSP: 5187 case Mips::MTTDSP: 5188 return Mips::S0; 5189 default: 5190 llvm_unreachable("Unknown instruction for 'mttr' dsp alias!"); 5191 } 5192 } 5193 5194 // Map the floating point register operand to the corresponding register 5195 // operand. 5196 static unsigned getRegisterForMxtrFP(MCInst &Inst, bool IsMFTC1) { 5197 switch (Inst.getOperand(IsMFTC1 ? 1 : 0).getReg()) { 5198 case Mips::F0: return Mips::ZERO; 5199 case Mips::F1: return Mips::AT; 5200 case Mips::F2: return Mips::V0; 5201 case Mips::F3: return Mips::V1; 5202 case Mips::F4: return Mips::A0; 5203 case Mips::F5: return Mips::A1; 5204 case Mips::F6: return Mips::A2; 5205 case Mips::F7: return Mips::A3; 5206 case Mips::F8: return Mips::T0; 5207 case Mips::F9: return Mips::T1; 5208 case Mips::F10: return Mips::T2; 5209 case Mips::F11: return Mips::T3; 5210 case Mips::F12: return Mips::T4; 5211 case Mips::F13: return Mips::T5; 5212 case Mips::F14: return Mips::T6; 5213 case Mips::F15: return Mips::T7; 5214 case Mips::F16: return Mips::S0; 5215 case Mips::F17: return Mips::S1; 5216 case Mips::F18: return Mips::S2; 5217 case Mips::F19: return Mips::S3; 5218 case Mips::F20: return Mips::S4; 5219 case Mips::F21: return Mips::S5; 5220 case Mips::F22: return Mips::S6; 5221 case Mips::F23: return Mips::S7; 5222 case Mips::F24: return Mips::T8; 5223 case Mips::F25: return Mips::T9; 5224 case Mips::F26: return Mips::K0; 5225 case Mips::F27: return Mips::K1; 5226 case Mips::F28: return Mips::GP; 5227 case Mips::F29: return Mips::SP; 5228 case Mips::F30: return Mips::FP; 5229 case Mips::F31: return Mips::RA; 5230 default: llvm_unreachable("Unknown register for mttc1 alias!"); 5231 } 5232 } 5233 5234 // Map the coprocessor operand the corresponding gpr register operand. 5235 static unsigned getRegisterForMxtrC0(MCInst &Inst, bool IsMFTC0) { 5236 switch (Inst.getOperand(IsMFTC0 ? 1 : 0).getReg()) { 5237 case Mips::COP00: return Mips::ZERO; 5238 case Mips::COP01: return Mips::AT; 5239 case Mips::COP02: return Mips::V0; 5240 case Mips::COP03: return Mips::V1; 5241 case Mips::COP04: return Mips::A0; 5242 case Mips::COP05: return Mips::A1; 5243 case Mips::COP06: return Mips::A2; 5244 case Mips::COP07: return Mips::A3; 5245 case Mips::COP08: return Mips::T0; 5246 case Mips::COP09: return Mips::T1; 5247 case Mips::COP010: return Mips::T2; 5248 case Mips::COP011: return Mips::T3; 5249 case Mips::COP012: return Mips::T4; 5250 case Mips::COP013: return Mips::T5; 5251 case Mips::COP014: return Mips::T6; 5252 case Mips::COP015: return Mips::T7; 5253 case Mips::COP016: return Mips::S0; 5254 case Mips::COP017: return Mips::S1; 5255 case Mips::COP018: return Mips::S2; 5256 case Mips::COP019: return Mips::S3; 5257 case Mips::COP020: return Mips::S4; 5258 case Mips::COP021: return Mips::S5; 5259 case Mips::COP022: return Mips::S6; 5260 case Mips::COP023: return Mips::S7; 5261 case Mips::COP024: return Mips::T8; 5262 case Mips::COP025: return Mips::T9; 5263 case Mips::COP026: return Mips::K0; 5264 case Mips::COP027: return Mips::K1; 5265 case Mips::COP028: return Mips::GP; 5266 case Mips::COP029: return Mips::SP; 5267 case Mips::COP030: return Mips::FP; 5268 case Mips::COP031: return Mips::RA; 5269 default: llvm_unreachable("Unknown register for mttc0 alias!"); 5270 } 5271 } 5272 5273 /// Expand an alias of 'mftr' or 'mttr' into the full instruction, by producing 5274 /// an mftr or mttr with the correctly mapped gpr register, u, sel and h bits. 5275 bool MipsAsmParser::expandMXTRAlias(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out, 5276 const MCSubtargetInfo *STI) { 5277 MipsTargetStreamer &TOut = getTargetStreamer(); 5278 unsigned rd = 0; 5279 unsigned u = 1; 5280 unsigned sel = 0; 5281 unsigned h = 0; 5282 bool IsMFTR = false; 5283 switch (Inst.getOpcode()) { 5284 case Mips::MFTC0: 5285 IsMFTR = true; 5286 LLVM_FALLTHROUGH; 5287 case Mips::MTTC0: 5288 u = 0; 5289 rd = getRegisterForMxtrC0(Inst, IsMFTR); 5290 sel = Inst.getOperand(2).getImm(); 5291 break; 5292 case Mips::MFTGPR: 5293 IsMFTR = true; 5294 LLVM_FALLTHROUGH; 5295 case Mips::MTTGPR: 5296 rd = Inst.getOperand(IsMFTR ? 1 : 0).getReg(); 5297 break; 5298 case Mips::MFTLO: 5299 case Mips::MFTHI: 5300 case Mips::MFTACX: 5301 case Mips::MFTDSP: 5302 IsMFTR = true; 5303 LLVM_FALLTHROUGH; 5304 case Mips::MTTLO: 5305 case Mips::MTTHI: 5306 case Mips::MTTACX: 5307 case Mips::MTTDSP: 5308 rd = getRegisterForMxtrDSP(Inst, IsMFTR); 5309 sel = 1; 5310 break; 5311 case Mips::MFTHC1: 5312 h = 1; 5313 LLVM_FALLTHROUGH; 5314 case Mips::MFTC1: 5315 IsMFTR = true; 5316 rd = getRegisterForMxtrFP(Inst, IsMFTR); 5317 sel = 2; 5318 break; 5319 case Mips::MTTHC1: 5320 h = 1; 5321 LLVM_FALLTHROUGH; 5322 case Mips::MTTC1: 5323 rd = getRegisterForMxtrFP(Inst, IsMFTR); 5324 sel = 2; 5325 break; 5326 case Mips::CFTC1: 5327 IsMFTR = true; 5328 LLVM_FALLTHROUGH; 5329 case Mips::CTTC1: 5330 rd = getRegisterForMxtrFP(Inst, IsMFTR); 5331 sel = 3; 5332 break; 5333 } 5334 unsigned Op0 = IsMFTR ? Inst.getOperand(0).getReg() : rd; 5335 unsigned Op1 = 5336 IsMFTR ? rd 5337 : (Inst.getOpcode() != Mips::MTTDSP ? Inst.getOperand(1).getReg() 5338 : Inst.getOperand(0).getReg()); 5339 5340 TOut.emitRRIII(IsMFTR ? Mips::MFTR : Mips::MTTR, Op0, Op1, u, sel, h, IDLoc, 5341 STI); 5342 return false; 5343 } 5344 5345 unsigned 5346 MipsAsmParser::checkEarlyTargetMatchPredicate(MCInst &Inst, 5347 const OperandVector &Operands) { 5348 switch (Inst.getOpcode()) { 5349 default: 5350 return Match_Success; 5351 case Mips::DATI: 5352 case Mips::DAHI: 5353 if (static_cast<MipsOperand &>(*Operands[1]) 5354 .isValidForTie(static_cast<MipsOperand &>(*Operands[2]))) 5355 return Match_Success; 5356 return Match_RequiresSameSrcAndDst; 5357 } 5358 } 5359 5360 unsigned MipsAsmParser::checkTargetMatchPredicate(MCInst &Inst) { 5361 switch (Inst.getOpcode()) { 5362 // As described by the MIPSR6 spec, daui must not use the zero operand for 5363 // its source operand. 5364 case Mips::DAUI: 5365 if (Inst.getOperand(1).getReg() == Mips::ZERO || 5366 Inst.getOperand(1).getReg() == Mips::ZERO_64) 5367 return Match_RequiresNoZeroRegister; 5368 return Match_Success; 5369 // As described by the Mips32r2 spec, the registers Rd and Rs for 5370 // jalr.hb must be different. 5371 // It also applies for registers Rt and Rs of microMIPSr6 jalrc.hb instruction 5372 // and registers Rd and Base for microMIPS lwp instruction 5373 case Mips::JALR_HB: 5374 case Mips::JALR_HB64: 5375 case Mips::JALRC_HB_MMR6: 5376 case Mips::JALRC_MMR6: 5377 if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) 5378 return Match_RequiresDifferentSrcAndDst; 5379 return Match_Success; 5380 case Mips::LWP_MM: 5381 if (Inst.getOperand(0).getReg() == Inst.getOperand(2).getReg()) 5382 return Match_RequiresDifferentSrcAndDst; 5383 return Match_Success; 5384 case Mips::SYNC: 5385 if (Inst.getOperand(0).getImm() != 0 && !hasMips32()) 5386 return Match_NonZeroOperandForSync; 5387 return Match_Success; 5388 case Mips::MFC0: 5389 case Mips::MTC0: 5390 case Mips::MTC2: 5391 case Mips::MFC2: 5392 if (Inst.getOperand(2).getImm() != 0 && !hasMips32()) 5393 return Match_NonZeroOperandForMTCX; 5394 return Match_Success; 5395 // As described the MIPSR6 spec, the compact branches that compare registers 5396 // must: 5397 // a) Not use the zero register. 5398 // b) Not use the same register twice. 5399 // c) rs < rt for bnec, beqc. 5400 // NB: For this case, the encoding will swap the operands as their 5401 // ordering doesn't matter. GAS performs this transformation too. 5402 // Hence, that constraint does not have to be enforced. 5403 // 5404 // The compact branches that branch iff the signed addition of two registers 5405 // would overflow must have rs >= rt. That can be handled like beqc/bnec with 5406 // operand swapping. They do not have restriction of using the zero register. 5407 case Mips::BLEZC: case Mips::BLEZC_MMR6: 5408 case Mips::BGEZC: case Mips::BGEZC_MMR6: 5409 case Mips::BGTZC: case Mips::BGTZC_MMR6: 5410 case Mips::BLTZC: case Mips::BLTZC_MMR6: 5411 case Mips::BEQZC: case Mips::BEQZC_MMR6: 5412 case Mips::BNEZC: case Mips::BNEZC_MMR6: 5413 case Mips::BLEZC64: 5414 case Mips::BGEZC64: 5415 case Mips::BGTZC64: 5416 case Mips::BLTZC64: 5417 case Mips::BEQZC64: 5418 case Mips::BNEZC64: 5419 if (Inst.getOperand(0).getReg() == Mips::ZERO || 5420 Inst.getOperand(0).getReg() == Mips::ZERO_64) 5421 return Match_RequiresNoZeroRegister; 5422 return Match_Success; 5423 case Mips::BGEC: case Mips::BGEC_MMR6: 5424 case Mips::BLTC: case Mips::BLTC_MMR6: 5425 case Mips::BGEUC: case Mips::BGEUC_MMR6: 5426 case Mips::BLTUC: case Mips::BLTUC_MMR6: 5427 case Mips::BEQC: case Mips::BEQC_MMR6: 5428 case Mips::BNEC: case Mips::BNEC_MMR6: 5429 case Mips::BGEC64: 5430 case Mips::BLTC64: 5431 case Mips::BGEUC64: 5432 case Mips::BLTUC64: 5433 case Mips::BEQC64: 5434 case Mips::BNEC64: 5435 if (Inst.getOperand(0).getReg() == Mips::ZERO || 5436 Inst.getOperand(0).getReg() == Mips::ZERO_64) 5437 return Match_RequiresNoZeroRegister; 5438 if (Inst.getOperand(1).getReg() == Mips::ZERO || 5439 Inst.getOperand(1).getReg() == Mips::ZERO_64) 5440 return Match_RequiresNoZeroRegister; 5441 if (Inst.getOperand(0).getReg() == Inst.getOperand(1).getReg()) 5442 return Match_RequiresDifferentOperands; 5443 return Match_Success; 5444 case Mips::DINS: { 5445 assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() && 5446 "Operands must be immediates for dins!"); 5447 const signed Pos = Inst.getOperand(2).getImm(); 5448 const signed Size = Inst.getOperand(3).getImm(); 5449 if ((0 > (Pos + Size)) || ((Pos + Size) > 32)) 5450 return Match_RequiresPosSizeRange0_32; 5451 return Match_Success; 5452 } 5453 case Mips::DINSM: 5454 case Mips::DINSU: { 5455 assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() && 5456 "Operands must be immediates for dinsm/dinsu!"); 5457 const signed Pos = Inst.getOperand(2).getImm(); 5458 const signed Size = Inst.getOperand(3).getImm(); 5459 if ((32 >= (Pos + Size)) || ((Pos + Size) > 64)) 5460 return Match_RequiresPosSizeRange33_64; 5461 return Match_Success; 5462 } 5463 case Mips::DEXT: { 5464 assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() && 5465 "Operands must be immediates for DEXTM!"); 5466 const signed Pos = Inst.getOperand(2).getImm(); 5467 const signed Size = Inst.getOperand(3).getImm(); 5468 if ((1 > (Pos + Size)) || ((Pos + Size) > 63)) 5469 return Match_RequiresPosSizeUImm6; 5470 return Match_Success; 5471 } 5472 case Mips::DEXTM: 5473 case Mips::DEXTU: { 5474 assert(Inst.getOperand(2).isImm() && Inst.getOperand(3).isImm() && 5475 "Operands must be immediates for dextm/dextu!"); 5476 const signed Pos = Inst.getOperand(2).getImm(); 5477 const signed Size = Inst.getOperand(3).getImm(); 5478 if ((32 > (Pos + Size)) || ((Pos + Size) > 64)) 5479 return Match_RequiresPosSizeRange33_64; 5480 return Match_Success; 5481 } 5482 case Mips::CRC32B: case Mips::CRC32CB: 5483 case Mips::CRC32H: case Mips::CRC32CH: 5484 case Mips::CRC32W: case Mips::CRC32CW: 5485 case Mips::CRC32D: case Mips::CRC32CD: 5486 if (Inst.getOperand(0).getReg() != Inst.getOperand(2).getReg()) 5487 return Match_RequiresSameSrcAndDst; 5488 return Match_Success; 5489 } 5490 5491 uint64_t TSFlags = getInstDesc(Inst.getOpcode()).TSFlags; 5492 if ((TSFlags & MipsII::HasFCCRegOperand) && 5493 (Inst.getOperand(0).getReg() != Mips::FCC0) && !hasEightFccRegisters()) 5494 return Match_NoFCCRegisterForCurrentISA; 5495 5496 return Match_Success; 5497 5498 } 5499 5500 static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands, 5501 uint64_t ErrorInfo) { 5502 if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) { 5503 SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc(); 5504 if (ErrorLoc == SMLoc()) 5505 return Loc; 5506 return ErrorLoc; 5507 } 5508 return Loc; 5509 } 5510 5511 bool MipsAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 5512 OperandVector &Operands, 5513 MCStreamer &Out, 5514 uint64_t &ErrorInfo, 5515 bool MatchingInlineAsm) { 5516 MCInst Inst; 5517 unsigned MatchResult = 5518 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm); 5519 5520 switch (MatchResult) { 5521 case Match_Success: 5522 if (processInstruction(Inst, IDLoc, Out, STI)) 5523 return true; 5524 return false; 5525 case Match_MissingFeature: 5526 Error(IDLoc, "instruction requires a CPU feature not currently enabled"); 5527 return true; 5528 case Match_InvalidOperand: { 5529 SMLoc ErrorLoc = IDLoc; 5530 if (ErrorInfo != ~0ULL) { 5531 if (ErrorInfo >= Operands.size()) 5532 return Error(IDLoc, "too few operands for instruction"); 5533 5534 ErrorLoc = Operands[ErrorInfo]->getStartLoc(); 5535 if (ErrorLoc == SMLoc()) 5536 ErrorLoc = IDLoc; 5537 } 5538 5539 return Error(ErrorLoc, "invalid operand for instruction"); 5540 } 5541 case Match_NonZeroOperandForSync: 5542 return Error(IDLoc, 5543 "s-type must be zero or unspecified for pre-MIPS32 ISAs"); 5544 case Match_NonZeroOperandForMTCX: 5545 return Error(IDLoc, "selector must be zero for pre-MIPS32 ISAs"); 5546 case Match_MnemonicFail: 5547 return Error(IDLoc, "invalid instruction"); 5548 case Match_RequiresDifferentSrcAndDst: 5549 return Error(IDLoc, "source and destination must be different"); 5550 case Match_RequiresDifferentOperands: 5551 return Error(IDLoc, "registers must be different"); 5552 case Match_RequiresNoZeroRegister: 5553 return Error(IDLoc, "invalid operand ($zero) for instruction"); 5554 case Match_RequiresSameSrcAndDst: 5555 return Error(IDLoc, "source and destination must match"); 5556 case Match_NoFCCRegisterForCurrentISA: 5557 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5558 "non-zero fcc register doesn't exist in current ISA level"); 5559 case Match_Immz: 5560 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), "expected '0'"); 5561 case Match_UImm1_0: 5562 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5563 "expected 1-bit unsigned immediate"); 5564 case Match_UImm2_0: 5565 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5566 "expected 2-bit unsigned immediate"); 5567 case Match_UImm2_1: 5568 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5569 "expected immediate in range 1 .. 4"); 5570 case Match_UImm3_0: 5571 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5572 "expected 3-bit unsigned immediate"); 5573 case Match_UImm4_0: 5574 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5575 "expected 4-bit unsigned immediate"); 5576 case Match_SImm4_0: 5577 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5578 "expected 4-bit signed immediate"); 5579 case Match_UImm5_0: 5580 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5581 "expected 5-bit unsigned immediate"); 5582 case Match_SImm5_0: 5583 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5584 "expected 5-bit signed immediate"); 5585 case Match_UImm5_1: 5586 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5587 "expected immediate in range 1 .. 32"); 5588 case Match_UImm5_32: 5589 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5590 "expected immediate in range 32 .. 63"); 5591 case Match_UImm5_33: 5592 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5593 "expected immediate in range 33 .. 64"); 5594 case Match_UImm5_0_Report_UImm6: 5595 // This is used on UImm5 operands that have a corresponding UImm5_32 5596 // operand to avoid confusing the user. 5597 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5598 "expected 6-bit unsigned immediate"); 5599 case Match_UImm5_Lsl2: 5600 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5601 "expected both 7-bit unsigned immediate and multiple of 4"); 5602 case Match_UImmRange2_64: 5603 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5604 "expected immediate in range 2 .. 64"); 5605 case Match_UImm6_0: 5606 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5607 "expected 6-bit unsigned immediate"); 5608 case Match_UImm6_Lsl2: 5609 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5610 "expected both 8-bit unsigned immediate and multiple of 4"); 5611 case Match_SImm6_0: 5612 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5613 "expected 6-bit signed immediate"); 5614 case Match_UImm7_0: 5615 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5616 "expected 7-bit unsigned immediate"); 5617 case Match_UImm7_N1: 5618 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5619 "expected immediate in range -1 .. 126"); 5620 case Match_SImm7_Lsl2: 5621 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5622 "expected both 9-bit signed immediate and multiple of 4"); 5623 case Match_UImm8_0: 5624 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5625 "expected 8-bit unsigned immediate"); 5626 case Match_UImm10_0: 5627 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5628 "expected 10-bit unsigned immediate"); 5629 case Match_SImm10_0: 5630 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5631 "expected 10-bit signed immediate"); 5632 case Match_SImm11_0: 5633 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5634 "expected 11-bit signed immediate"); 5635 case Match_UImm16: 5636 case Match_UImm16_Relaxed: 5637 case Match_UImm16_AltRelaxed: 5638 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5639 "expected 16-bit unsigned immediate"); 5640 case Match_SImm16: 5641 case Match_SImm16_Relaxed: 5642 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5643 "expected 16-bit signed immediate"); 5644 case Match_SImm19_Lsl2: 5645 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5646 "expected both 19-bit signed immediate and multiple of 4"); 5647 case Match_UImm20_0: 5648 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5649 "expected 20-bit unsigned immediate"); 5650 case Match_UImm26_0: 5651 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5652 "expected 26-bit unsigned immediate"); 5653 case Match_SImm32: 5654 case Match_SImm32_Relaxed: 5655 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5656 "expected 32-bit signed immediate"); 5657 case Match_UImm32_Coerced: 5658 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5659 "expected 32-bit immediate"); 5660 case Match_MemSImm9: 5661 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5662 "expected memory with 9-bit signed offset"); 5663 case Match_MemSImm10: 5664 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5665 "expected memory with 10-bit signed offset"); 5666 case Match_MemSImm10Lsl1: 5667 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5668 "expected memory with 11-bit signed offset and multiple of 2"); 5669 case Match_MemSImm10Lsl2: 5670 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5671 "expected memory with 12-bit signed offset and multiple of 4"); 5672 case Match_MemSImm10Lsl3: 5673 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5674 "expected memory with 13-bit signed offset and multiple of 8"); 5675 case Match_MemSImm11: 5676 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5677 "expected memory with 11-bit signed offset"); 5678 case Match_MemSImm12: 5679 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5680 "expected memory with 12-bit signed offset"); 5681 case Match_MemSImm16: 5682 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5683 "expected memory with 16-bit signed offset"); 5684 case Match_MemSImmPtr: 5685 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo), 5686 "expected memory with 32-bit signed offset"); 5687 case Match_RequiresPosSizeRange0_32: { 5688 SMLoc ErrorStart = Operands[3]->getStartLoc(); 5689 SMLoc ErrorEnd = Operands[4]->getEndLoc(); 5690 return Error(ErrorStart, "size plus position are not in the range 0 .. 32", 5691 SMRange(ErrorStart, ErrorEnd)); 5692 } 5693 case Match_RequiresPosSizeUImm6: { 5694 SMLoc ErrorStart = Operands[3]->getStartLoc(); 5695 SMLoc ErrorEnd = Operands[4]->getEndLoc(); 5696 return Error(ErrorStart, "size plus position are not in the range 1 .. 63", 5697 SMRange(ErrorStart, ErrorEnd)); 5698 } 5699 case Match_RequiresPosSizeRange33_64: { 5700 SMLoc ErrorStart = Operands[3]->getStartLoc(); 5701 SMLoc ErrorEnd = Operands[4]->getEndLoc(); 5702 return Error(ErrorStart, "size plus position are not in the range 33 .. 64", 5703 SMRange(ErrorStart, ErrorEnd)); 5704 } 5705 } 5706 5707 llvm_unreachable("Implement any new match types added!"); 5708 } 5709 5710 void MipsAsmParser::warnIfRegIndexIsAT(unsigned RegIndex, SMLoc Loc) { 5711 if (RegIndex != 0 && AssemblerOptions.back()->getATRegIndex() == RegIndex) 5712 Warning(Loc, "used $at (currently $" + Twine(RegIndex) + 5713 ") without \".set noat\""); 5714 } 5715 5716 void MipsAsmParser::warnIfNoMacro(SMLoc Loc) { 5717 if (!AssemblerOptions.back()->isMacro()) 5718 Warning(Loc, "macro instruction expanded into multiple instructions"); 5719 } 5720 5721 void MipsAsmParser::ConvertXWPOperands(MCInst &Inst, 5722 const OperandVector &Operands) { 5723 assert( 5724 (Inst.getOpcode() == Mips::LWP_MM || Inst.getOpcode() == Mips::SWP_MM) && 5725 "Unexpected instruction!"); 5726 ((MipsOperand &)*Operands[1]).addGPR32ZeroAsmRegOperands(Inst, 1); 5727 int NextReg = nextReg(((MipsOperand &)*Operands[1]).getGPR32Reg()); 5728 Inst.addOperand(MCOperand::createReg(NextReg)); 5729 ((MipsOperand &)*Operands[2]).addMemOperands(Inst, 2); 5730 } 5731 5732 void 5733 MipsAsmParser::printWarningWithFixIt(const Twine &Msg, const Twine &FixMsg, 5734 SMRange Range, bool ShowColors) { 5735 getSourceManager().PrintMessage(Range.Start, SourceMgr::DK_Warning, Msg, 5736 Range, SMFixIt(Range, FixMsg), 5737 ShowColors); 5738 } 5739 5740 int MipsAsmParser::matchCPURegisterName(StringRef Name) { 5741 int CC; 5742 5743 CC = StringSwitch<unsigned>(Name) 5744 .Case("zero", 0) 5745 .Cases("at", "AT", 1) 5746 .Case("a0", 4) 5747 .Case("a1", 5) 5748 .Case("a2", 6) 5749 .Case("a3", 7) 5750 .Case("v0", 2) 5751 .Case("v1", 3) 5752 .Case("s0", 16) 5753 .Case("s1", 17) 5754 .Case("s2", 18) 5755 .Case("s3", 19) 5756 .Case("s4", 20) 5757 .Case("s5", 21) 5758 .Case("s6", 22) 5759 .Case("s7", 23) 5760 .Case("k0", 26) 5761 .Case("k1", 27) 5762 .Case("gp", 28) 5763 .Case("sp", 29) 5764 .Case("fp", 30) 5765 .Case("s8", 30) 5766 .Case("ra", 31) 5767 .Case("t0", 8) 5768 .Case("t1", 9) 5769 .Case("t2", 10) 5770 .Case("t3", 11) 5771 .Case("t4", 12) 5772 .Case("t5", 13) 5773 .Case("t6", 14) 5774 .Case("t7", 15) 5775 .Case("t8", 24) 5776 .Case("t9", 25) 5777 .Default(-1); 5778 5779 if (!(isABI_N32() || isABI_N64())) 5780 return CC; 5781 5782 if (12 <= CC && CC <= 15) { 5783 // Name is one of t4-t7 5784 AsmToken RegTok = getLexer().peekTok(); 5785 SMRange RegRange = RegTok.getLocRange(); 5786 5787 StringRef FixedName = StringSwitch<StringRef>(Name) 5788 .Case("t4", "t0") 5789 .Case("t5", "t1") 5790 .Case("t6", "t2") 5791 .Case("t7", "t3") 5792 .Default(""); 5793 assert(FixedName != "" && "Register name is not one of t4-t7."); 5794 5795 printWarningWithFixIt("register names $t4-$t7 are only available in O32.", 5796 "Did you mean $" + FixedName + "?", RegRange); 5797 } 5798 5799 // Although SGI documentation just cuts out t0-t3 for n32/n64, 5800 // GNU pushes the values of t0-t3 to override the o32/o64 values for t4-t7 5801 // We are supporting both cases, so for t0-t3 we'll just push them to t4-t7. 5802 if (8 <= CC && CC <= 11) 5803 CC += 4; 5804 5805 if (CC == -1) 5806 CC = StringSwitch<unsigned>(Name) 5807 .Case("a4", 8) 5808 .Case("a5", 9) 5809 .Case("a6", 10) 5810 .Case("a7", 11) 5811 .Case("kt0", 26) 5812 .Case("kt1", 27) 5813 .Default(-1); 5814 5815 return CC; 5816 } 5817 5818 int MipsAsmParser::matchHWRegsRegisterName(StringRef Name) { 5819 int CC; 5820 5821 CC = StringSwitch<unsigned>(Name) 5822 .Case("hwr_cpunum", 0) 5823 .Case("hwr_synci_step", 1) 5824 .Case("hwr_cc", 2) 5825 .Case("hwr_ccres", 3) 5826 .Case("hwr_ulr", 29) 5827 .Default(-1); 5828 5829 return CC; 5830 } 5831 5832 int MipsAsmParser::matchFPURegisterName(StringRef Name) { 5833 if (Name[0] == 'f') { 5834 StringRef NumString = Name.substr(1); 5835 unsigned IntVal; 5836 if (NumString.getAsInteger(10, IntVal)) 5837 return -1; // This is not an integer. 5838 if (IntVal > 31) // Maximum index for fpu register. 5839 return -1; 5840 return IntVal; 5841 } 5842 return -1; 5843 } 5844 5845 int MipsAsmParser::matchFCCRegisterName(StringRef Name) { 5846 if (Name.startswith("fcc")) { 5847 StringRef NumString = Name.substr(3); 5848 unsigned IntVal; 5849 if (NumString.getAsInteger(10, IntVal)) 5850 return -1; // This is not an integer. 5851 if (IntVal > 7) // There are only 8 fcc registers. 5852 return -1; 5853 return IntVal; 5854 } 5855 return -1; 5856 } 5857 5858 int MipsAsmParser::matchACRegisterName(StringRef Name) { 5859 if (Name.startswith("ac")) { 5860 StringRef NumString = Name.substr(2); 5861 unsigned IntVal; 5862 if (NumString.getAsInteger(10, IntVal)) 5863 return -1; // This is not an integer. 5864 if (IntVal > 3) // There are only 3 acc registers. 5865 return -1; 5866 return IntVal; 5867 } 5868 return -1; 5869 } 5870 5871 int MipsAsmParser::matchMSA128RegisterName(StringRef Name) { 5872 unsigned IntVal; 5873 5874 if (Name.front() != 'w' || Name.drop_front(1).getAsInteger(10, IntVal)) 5875 return -1; 5876 5877 if (IntVal > 31) 5878 return -1; 5879 5880 return IntVal; 5881 } 5882 5883 int MipsAsmParser::matchMSA128CtrlRegisterName(StringRef Name) { 5884 int CC; 5885 5886 CC = StringSwitch<unsigned>(Name) 5887 .Case("msair", 0) 5888 .Case("msacsr", 1) 5889 .Case("msaaccess", 2) 5890 .Case("msasave", 3) 5891 .Case("msamodify", 4) 5892 .Case("msarequest", 5) 5893 .Case("msamap", 6) 5894 .Case("msaunmap", 7) 5895 .Default(-1); 5896 5897 return CC; 5898 } 5899 5900 bool MipsAsmParser::canUseATReg() { 5901 return AssemblerOptions.back()->getATRegIndex() != 0; 5902 } 5903 5904 unsigned MipsAsmParser::getATReg(SMLoc Loc) { 5905 unsigned ATIndex = AssemblerOptions.back()->getATRegIndex(); 5906 if (ATIndex == 0) { 5907 reportParseError(Loc, 5908 "pseudo-instruction requires $at, which is not available"); 5909 return 0; 5910 } 5911 unsigned AT = getReg( 5912 (isGP64bit()) ? Mips::GPR64RegClassID : Mips::GPR32RegClassID, ATIndex); 5913 return AT; 5914 } 5915 5916 unsigned MipsAsmParser::getReg(int RC, int RegNo) { 5917 return *(getContext().getRegisterInfo()->getRegClass(RC).begin() + RegNo); 5918 } 5919 5920 bool MipsAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic) { 5921 MCAsmParser &Parser = getParser(); 5922 LLVM_DEBUG(dbgs() << "parseOperand\n"); 5923 5924 // Check if the current operand has a custom associated parser, if so, try to 5925 // custom parse the operand, or fallback to the general approach. 5926 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); 5927 if (ResTy == MatchOperand_Success) 5928 return false; 5929 // If there wasn't a custom match, try the generic matcher below. Otherwise, 5930 // there was a match, but an error occurred, in which case, just return that 5931 // the operand parsing failed. 5932 if (ResTy == MatchOperand_ParseFail) 5933 return true; 5934 5935 LLVM_DEBUG(dbgs() << ".. Generic Parser\n"); 5936 5937 switch (getLexer().getKind()) { 5938 case AsmToken::Dollar: { 5939 // Parse the register. 5940 SMLoc S = Parser.getTok().getLoc(); 5941 5942 // Almost all registers have been parsed by custom parsers. There is only 5943 // one exception to this. $zero (and it's alias $0) will reach this point 5944 // for div, divu, and similar instructions because it is not an operand 5945 // to the instruction definition but an explicit register. Special case 5946 // this situation for now. 5947 if (parseAnyRegister(Operands) != MatchOperand_NoMatch) 5948 return false; 5949 5950 // Maybe it is a symbol reference. 5951 StringRef Identifier; 5952 if (Parser.parseIdentifier(Identifier)) 5953 return true; 5954 5955 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 5956 MCSymbol *Sym = getContext().getOrCreateSymbol("$" + Identifier); 5957 // Otherwise create a symbol reference. 5958 const MCExpr *Res = 5959 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 5960 5961 Operands.push_back(MipsOperand::CreateImm(Res, S, E, *this)); 5962 return false; 5963 } 5964 default: { 5965 LLVM_DEBUG(dbgs() << ".. generic integer expression\n"); 5966 5967 const MCExpr *Expr; 5968 SMLoc S = Parser.getTok().getLoc(); // Start location of the operand. 5969 if (getParser().parseExpression(Expr)) 5970 return true; 5971 5972 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 5973 5974 Operands.push_back(MipsOperand::CreateImm(Expr, S, E, *this)); 5975 return false; 5976 } 5977 } // switch(getLexer().getKind()) 5978 return true; 5979 } 5980 5981 bool MipsAsmParser::isEvaluated(const MCExpr *Expr) { 5982 switch (Expr->getKind()) { 5983 case MCExpr::Constant: 5984 return true; 5985 case MCExpr::SymbolRef: 5986 return (cast<MCSymbolRefExpr>(Expr)->getKind() != MCSymbolRefExpr::VK_None); 5987 case MCExpr::Binary: { 5988 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr); 5989 if (!isEvaluated(BE->getLHS())) 5990 return false; 5991 return isEvaluated(BE->getRHS()); 5992 } 5993 case MCExpr::Unary: 5994 return isEvaluated(cast<MCUnaryExpr>(Expr)->getSubExpr()); 5995 case MCExpr::Target: 5996 return true; 5997 } 5998 return false; 5999 } 6000 6001 bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, 6002 SMLoc &EndLoc) { 6003 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Operands; 6004 OperandMatchResultTy ResTy = parseAnyRegister(Operands); 6005 if (ResTy == MatchOperand_Success) { 6006 assert(Operands.size() == 1); 6007 MipsOperand &Operand = static_cast<MipsOperand &>(*Operands.front()); 6008 StartLoc = Operand.getStartLoc(); 6009 EndLoc = Operand.getEndLoc(); 6010 6011 // AFAIK, we only support numeric registers and named GPR's in CFI 6012 // directives. 6013 // Don't worry about eating tokens before failing. Using an unrecognised 6014 // register is a parse error. 6015 if (Operand.isGPRAsmReg()) { 6016 // Resolve to GPR32 or GPR64 appropriately. 6017 RegNo = isGP64bit() ? Operand.getGPR64Reg() : Operand.getGPR32Reg(); 6018 } 6019 6020 return (RegNo == (unsigned)-1); 6021 } 6022 6023 assert(Operands.size() == 0); 6024 return (RegNo == (unsigned)-1); 6025 } 6026 6027 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res, bool isParenExpr) { 6028 SMLoc S; 6029 6030 if (isParenExpr) 6031 return getParser().parseParenExprOfDepth(0, Res, S); 6032 return getParser().parseExpression(Res); 6033 } 6034 6035 OperandMatchResultTy 6036 MipsAsmParser::parseMemOperand(OperandVector &Operands) { 6037 MCAsmParser &Parser = getParser(); 6038 LLVM_DEBUG(dbgs() << "parseMemOperand\n"); 6039 const MCExpr *IdVal = nullptr; 6040 SMLoc S; 6041 bool isParenExpr = false; 6042 OperandMatchResultTy Res = MatchOperand_NoMatch; 6043 // First operand is the offset. 6044 S = Parser.getTok().getLoc(); 6045 6046 if (getLexer().getKind() == AsmToken::LParen) { 6047 Parser.Lex(); 6048 isParenExpr = true; 6049 } 6050 6051 if (getLexer().getKind() != AsmToken::Dollar) { 6052 if (parseMemOffset(IdVal, isParenExpr)) 6053 return MatchOperand_ParseFail; 6054 6055 const AsmToken &Tok = Parser.getTok(); // Get the next token. 6056 if (Tok.isNot(AsmToken::LParen)) { 6057 MipsOperand &Mnemonic = static_cast<MipsOperand &>(*Operands[0]); 6058 if (Mnemonic.getToken() == "la" || Mnemonic.getToken() == "dla") { 6059 SMLoc E = 6060 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 6061 Operands.push_back(MipsOperand::CreateImm(IdVal, S, E, *this)); 6062 return MatchOperand_Success; 6063 } 6064 if (Tok.is(AsmToken::EndOfStatement)) { 6065 SMLoc E = 6066 SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 6067 6068 // Zero register assumed, add a memory operand with ZERO as its base. 6069 // "Base" will be managed by k_Memory. 6070 auto Base = MipsOperand::createGPRReg( 6071 0, "0", getContext().getRegisterInfo(), S, E, *this); 6072 Operands.push_back( 6073 MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this)); 6074 return MatchOperand_Success; 6075 } 6076 MCBinaryExpr::Opcode Opcode; 6077 // GAS and LLVM treat comparison operators different. GAS will generate -1 6078 // or 0, while LLVM will generate 0 or 1. Since a comparsion operator is 6079 // highly unlikely to be found in a memory offset expression, we don't 6080 // handle them. 6081 switch (Tok.getKind()) { 6082 case AsmToken::Plus: 6083 Opcode = MCBinaryExpr::Add; 6084 Parser.Lex(); 6085 break; 6086 case AsmToken::Minus: 6087 Opcode = MCBinaryExpr::Sub; 6088 Parser.Lex(); 6089 break; 6090 case AsmToken::Star: 6091 Opcode = MCBinaryExpr::Mul; 6092 Parser.Lex(); 6093 break; 6094 case AsmToken::Pipe: 6095 Opcode = MCBinaryExpr::Or; 6096 Parser.Lex(); 6097 break; 6098 case AsmToken::Amp: 6099 Opcode = MCBinaryExpr::And; 6100 Parser.Lex(); 6101 break; 6102 case AsmToken::LessLess: 6103 Opcode = MCBinaryExpr::Shl; 6104 Parser.Lex(); 6105 break; 6106 case AsmToken::GreaterGreater: 6107 Opcode = MCBinaryExpr::LShr; 6108 Parser.Lex(); 6109 break; 6110 case AsmToken::Caret: 6111 Opcode = MCBinaryExpr::Xor; 6112 Parser.Lex(); 6113 break; 6114 case AsmToken::Slash: 6115 Opcode = MCBinaryExpr::Div; 6116 Parser.Lex(); 6117 break; 6118 case AsmToken::Percent: 6119 Opcode = MCBinaryExpr::Mod; 6120 Parser.Lex(); 6121 break; 6122 default: 6123 Error(Parser.getTok().getLoc(), "'(' or expression expected"); 6124 return MatchOperand_ParseFail; 6125 } 6126 const MCExpr * NextExpr; 6127 if (getParser().parseExpression(NextExpr)) 6128 return MatchOperand_ParseFail; 6129 IdVal = MCBinaryExpr::create(Opcode, IdVal, NextExpr, getContext()); 6130 } 6131 6132 Parser.Lex(); // Eat the '(' token. 6133 } 6134 6135 Res = parseAnyRegister(Operands); 6136 if (Res != MatchOperand_Success) 6137 return Res; 6138 6139 if (Parser.getTok().isNot(AsmToken::RParen)) { 6140 Error(Parser.getTok().getLoc(), "')' expected"); 6141 return MatchOperand_ParseFail; 6142 } 6143 6144 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 6145 6146 Parser.Lex(); // Eat the ')' token. 6147 6148 if (!IdVal) 6149 IdVal = MCConstantExpr::create(0, getContext()); 6150 6151 // Replace the register operand with the memory operand. 6152 std::unique_ptr<MipsOperand> op( 6153 static_cast<MipsOperand *>(Operands.back().release())); 6154 // Remove the register from the operands. 6155 // "op" will be managed by k_Memory. 6156 Operands.pop_back(); 6157 // Add the memory operand. 6158 if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(IdVal)) { 6159 int64_t Imm; 6160 if (IdVal->evaluateAsAbsolute(Imm)) 6161 IdVal = MCConstantExpr::create(Imm, getContext()); 6162 else if (BE->getLHS()->getKind() != MCExpr::SymbolRef) 6163 IdVal = MCBinaryExpr::create(BE->getOpcode(), BE->getRHS(), BE->getLHS(), 6164 getContext()); 6165 } 6166 6167 Operands.push_back(MipsOperand::CreateMem(std::move(op), IdVal, S, E, *this)); 6168 return MatchOperand_Success; 6169 } 6170 6171 bool MipsAsmParser::searchSymbolAlias(OperandVector &Operands) { 6172 MCAsmParser &Parser = getParser(); 6173 MCSymbol *Sym = getContext().lookupSymbol(Parser.getTok().getIdentifier()); 6174 if (!Sym) 6175 return false; 6176 6177 SMLoc S = Parser.getTok().getLoc(); 6178 if (Sym->isVariable()) { 6179 const MCExpr *Expr = Sym->getVariableValue(); 6180 if (Expr->getKind() == MCExpr::SymbolRef) { 6181 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr); 6182 StringRef DefSymbol = Ref->getSymbol().getName(); 6183 if (DefSymbol.startswith("$")) { 6184 OperandMatchResultTy ResTy = 6185 matchAnyRegisterNameWithoutDollar(Operands, DefSymbol.substr(1), S); 6186 if (ResTy == MatchOperand_Success) { 6187 Parser.Lex(); 6188 return true; 6189 } 6190 if (ResTy == MatchOperand_ParseFail) 6191 llvm_unreachable("Should never ParseFail"); 6192 } 6193 } 6194 } else if (Sym->isUnset()) { 6195 // If symbol is unset, it might be created in the `parseSetAssignment` 6196 // routine as an alias for a numeric register name. 6197 // Lookup in the aliases list. 6198 auto Entry = RegisterSets.find(Sym->getName()); 6199 if (Entry != RegisterSets.end()) { 6200 OperandMatchResultTy ResTy = 6201 matchAnyRegisterWithoutDollar(Operands, Entry->getValue(), S); 6202 if (ResTy == MatchOperand_Success) { 6203 Parser.Lex(); 6204 return true; 6205 } 6206 } 6207 } 6208 6209 return false; 6210 } 6211 6212 OperandMatchResultTy 6213 MipsAsmParser::matchAnyRegisterNameWithoutDollar(OperandVector &Operands, 6214 StringRef Identifier, 6215 SMLoc S) { 6216 int Index = matchCPURegisterName(Identifier); 6217 if (Index != -1) { 6218 Operands.push_back(MipsOperand::createGPRReg( 6219 Index, Identifier, getContext().getRegisterInfo(), S, 6220 getLexer().getLoc(), *this)); 6221 return MatchOperand_Success; 6222 } 6223 6224 Index = matchHWRegsRegisterName(Identifier); 6225 if (Index != -1) { 6226 Operands.push_back(MipsOperand::createHWRegsReg( 6227 Index, Identifier, getContext().getRegisterInfo(), S, 6228 getLexer().getLoc(), *this)); 6229 return MatchOperand_Success; 6230 } 6231 6232 Index = matchFPURegisterName(Identifier); 6233 if (Index != -1) { 6234 Operands.push_back(MipsOperand::createFGRReg( 6235 Index, Identifier, getContext().getRegisterInfo(), S, 6236 getLexer().getLoc(), *this)); 6237 return MatchOperand_Success; 6238 } 6239 6240 Index = matchFCCRegisterName(Identifier); 6241 if (Index != -1) { 6242 Operands.push_back(MipsOperand::createFCCReg( 6243 Index, Identifier, getContext().getRegisterInfo(), S, 6244 getLexer().getLoc(), *this)); 6245 return MatchOperand_Success; 6246 } 6247 6248 Index = matchACRegisterName(Identifier); 6249 if (Index != -1) { 6250 Operands.push_back(MipsOperand::createACCReg( 6251 Index, Identifier, getContext().getRegisterInfo(), S, 6252 getLexer().getLoc(), *this)); 6253 return MatchOperand_Success; 6254 } 6255 6256 Index = matchMSA128RegisterName(Identifier); 6257 if (Index != -1) { 6258 Operands.push_back(MipsOperand::createMSA128Reg( 6259 Index, Identifier, getContext().getRegisterInfo(), S, 6260 getLexer().getLoc(), *this)); 6261 return MatchOperand_Success; 6262 } 6263 6264 Index = matchMSA128CtrlRegisterName(Identifier); 6265 if (Index != -1) { 6266 Operands.push_back(MipsOperand::createMSACtrlReg( 6267 Index, Identifier, getContext().getRegisterInfo(), S, 6268 getLexer().getLoc(), *this)); 6269 return MatchOperand_Success; 6270 } 6271 6272 return MatchOperand_NoMatch; 6273 } 6274 6275 OperandMatchResultTy 6276 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, 6277 const AsmToken &Token, SMLoc S) { 6278 if (Token.is(AsmToken::Identifier)) { 6279 LLVM_DEBUG(dbgs() << ".. identifier\n"); 6280 StringRef Identifier = Token.getIdentifier(); 6281 OperandMatchResultTy ResTy = 6282 matchAnyRegisterNameWithoutDollar(Operands, Identifier, S); 6283 return ResTy; 6284 } else if (Token.is(AsmToken::Integer)) { 6285 LLVM_DEBUG(dbgs() << ".. integer\n"); 6286 int64_t RegNum = Token.getIntVal(); 6287 if (RegNum < 0 || RegNum > 31) { 6288 // Show the error, but treat invalid register 6289 // number as a normal one to continue parsing 6290 // and catch other possible errors. 6291 Error(getLexer().getLoc(), "invalid register number"); 6292 } 6293 Operands.push_back(MipsOperand::createNumericReg( 6294 RegNum, Token.getString(), getContext().getRegisterInfo(), S, 6295 Token.getLoc(), *this)); 6296 return MatchOperand_Success; 6297 } 6298 6299 LLVM_DEBUG(dbgs() << Token.getKind() << "\n"); 6300 6301 return MatchOperand_NoMatch; 6302 } 6303 6304 OperandMatchResultTy 6305 MipsAsmParser::matchAnyRegisterWithoutDollar(OperandVector &Operands, SMLoc S) { 6306 auto Token = getLexer().peekTok(false); 6307 return matchAnyRegisterWithoutDollar(Operands, Token, S); 6308 } 6309 6310 OperandMatchResultTy 6311 MipsAsmParser::parseAnyRegister(OperandVector &Operands) { 6312 MCAsmParser &Parser = getParser(); 6313 LLVM_DEBUG(dbgs() << "parseAnyRegister\n"); 6314 6315 auto Token = Parser.getTok(); 6316 6317 SMLoc S = Token.getLoc(); 6318 6319 if (Token.isNot(AsmToken::Dollar)) { 6320 LLVM_DEBUG(dbgs() << ".. !$ -> try sym aliasing\n"); 6321 if (Token.is(AsmToken::Identifier)) { 6322 if (searchSymbolAlias(Operands)) 6323 return MatchOperand_Success; 6324 } 6325 LLVM_DEBUG(dbgs() << ".. !symalias -> NoMatch\n"); 6326 return MatchOperand_NoMatch; 6327 } 6328 LLVM_DEBUG(dbgs() << ".. $\n"); 6329 6330 OperandMatchResultTy ResTy = matchAnyRegisterWithoutDollar(Operands, S); 6331 if (ResTy == MatchOperand_Success) { 6332 Parser.Lex(); // $ 6333 Parser.Lex(); // identifier 6334 } 6335 return ResTy; 6336 } 6337 6338 OperandMatchResultTy 6339 MipsAsmParser::parseJumpTarget(OperandVector &Operands) { 6340 MCAsmParser &Parser = getParser(); 6341 LLVM_DEBUG(dbgs() << "parseJumpTarget\n"); 6342 6343 SMLoc S = getLexer().getLoc(); 6344 6345 // Registers are a valid target and have priority over symbols. 6346 OperandMatchResultTy ResTy = parseAnyRegister(Operands); 6347 if (ResTy != MatchOperand_NoMatch) 6348 return ResTy; 6349 6350 // Integers and expressions are acceptable 6351 const MCExpr *Expr = nullptr; 6352 if (Parser.parseExpression(Expr)) { 6353 // We have no way of knowing if a symbol was consumed so we must ParseFail 6354 return MatchOperand_ParseFail; 6355 } 6356 Operands.push_back( 6357 MipsOperand::CreateImm(Expr, S, getLexer().getLoc(), *this)); 6358 return MatchOperand_Success; 6359 } 6360 6361 OperandMatchResultTy 6362 MipsAsmParser::parseInvNum(OperandVector &Operands) { 6363 MCAsmParser &Parser = getParser(); 6364 const MCExpr *IdVal; 6365 // If the first token is '$' we may have register operand. We have to reject 6366 // cases where it is not a register. Complicating the matter is that 6367 // register names are not reserved across all ABIs. 6368 // Peek past the dollar to see if it's a register name for this ABI. 6369 SMLoc S = Parser.getTok().getLoc(); 6370 if (Parser.getTok().is(AsmToken::Dollar)) { 6371 return matchCPURegisterName(Parser.getLexer().peekTok().getString()) == -1 6372 ? MatchOperand_ParseFail 6373 : MatchOperand_NoMatch; 6374 } 6375 if (getParser().parseExpression(IdVal)) 6376 return MatchOperand_ParseFail; 6377 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal); 6378 if (!MCE) 6379 return MatchOperand_NoMatch; 6380 int64_t Val = MCE->getValue(); 6381 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); 6382 Operands.push_back(MipsOperand::CreateImm( 6383 MCConstantExpr::create(0 - Val, getContext()), S, E, *this)); 6384 return MatchOperand_Success; 6385 } 6386 6387 OperandMatchResultTy 6388 MipsAsmParser::parseRegisterList(OperandVector &Operands) { 6389 MCAsmParser &Parser = getParser(); 6390 SmallVector<unsigned, 10> Regs; 6391 unsigned RegNo; 6392 unsigned PrevReg = Mips::NoRegister; 6393 bool RegRange = false; 6394 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> TmpOperands; 6395 6396 if (Parser.getTok().isNot(AsmToken::Dollar)) 6397 return MatchOperand_ParseFail; 6398 6399 SMLoc S = Parser.getTok().getLoc(); 6400 while (parseAnyRegister(TmpOperands) == MatchOperand_Success) { 6401 SMLoc E = getLexer().getLoc(); 6402 MipsOperand &Reg = static_cast<MipsOperand &>(*TmpOperands.back()); 6403 RegNo = isGP64bit() ? Reg.getGPR64Reg() : Reg.getGPR32Reg(); 6404 if (RegRange) { 6405 // Remove last register operand because registers from register range 6406 // should be inserted first. 6407 if ((isGP64bit() && RegNo == Mips::RA_64) || 6408 (!isGP64bit() && RegNo == Mips::RA)) { 6409 Regs.push_back(RegNo); 6410 } else { 6411 unsigned TmpReg = PrevReg + 1; 6412 while (TmpReg <= RegNo) { 6413 if ((((TmpReg < Mips::S0) || (TmpReg > Mips::S7)) && !isGP64bit()) || 6414 (((TmpReg < Mips::S0_64) || (TmpReg > Mips::S7_64)) && 6415 isGP64bit())) { 6416 Error(E, "invalid register operand"); 6417 return MatchOperand_ParseFail; 6418 } 6419 6420 PrevReg = TmpReg; 6421 Regs.push_back(TmpReg++); 6422 } 6423 } 6424 6425 RegRange = false; 6426 } else { 6427 if ((PrevReg == Mips::NoRegister) && 6428 ((isGP64bit() && (RegNo != Mips::S0_64) && (RegNo != Mips::RA_64)) || 6429 (!isGP64bit() && (RegNo != Mips::S0) && (RegNo != Mips::RA)))) { 6430 Error(E, "$16 or $31 expected"); 6431 return MatchOperand_ParseFail; 6432 } else if (!(((RegNo == Mips::FP || RegNo == Mips::RA || 6433 (RegNo >= Mips::S0 && RegNo <= Mips::S7)) && 6434 !isGP64bit()) || 6435 ((RegNo == Mips::FP_64 || RegNo == Mips::RA_64 || 6436 (RegNo >= Mips::S0_64 && RegNo <= Mips::S7_64)) && 6437 isGP64bit()))) { 6438 Error(E, "invalid register operand"); 6439 return MatchOperand_ParseFail; 6440 } else if ((PrevReg != Mips::NoRegister) && (RegNo != PrevReg + 1) && 6441 ((RegNo != Mips::FP && RegNo != Mips::RA && !isGP64bit()) || 6442 (RegNo != Mips::FP_64 && RegNo != Mips::RA_64 && 6443 isGP64bit()))) { 6444 Error(E, "consecutive register numbers expected"); 6445 return MatchOperand_ParseFail; 6446 } 6447 6448 Regs.push_back(RegNo); 6449 } 6450 6451 if (Parser.getTok().is(AsmToken::Minus)) 6452 RegRange = true; 6453 6454 if (!Parser.getTok().isNot(AsmToken::Minus) && 6455 !Parser.getTok().isNot(AsmToken::Comma)) { 6456 Error(E, "',' or '-' expected"); 6457 return MatchOperand_ParseFail; 6458 } 6459 6460 Lex(); // Consume comma or minus 6461 if (Parser.getTok().isNot(AsmToken::Dollar)) 6462 break; 6463 6464 PrevReg = RegNo; 6465 } 6466 6467 SMLoc E = Parser.getTok().getLoc(); 6468 Operands.push_back(MipsOperand::CreateRegList(Regs, S, E, *this)); 6469 parseMemOperand(Operands); 6470 return MatchOperand_Success; 6471 } 6472 6473 /// Sometimes (i.e. load/stores) the operand may be followed immediately by 6474 /// either this. 6475 /// ::= '(', register, ')' 6476 /// handle it before we iterate so we don't get tripped up by the lack of 6477 /// a comma. 6478 bool MipsAsmParser::parseParenSuffix(StringRef Name, OperandVector &Operands) { 6479 MCAsmParser &Parser = getParser(); 6480 if (getLexer().is(AsmToken::LParen)) { 6481 Operands.push_back( 6482 MipsOperand::CreateToken("(", getLexer().getLoc(), *this)); 6483 Parser.Lex(); 6484 if (parseOperand(Operands, Name)) { 6485 SMLoc Loc = getLexer().getLoc(); 6486 return Error(Loc, "unexpected token in argument list"); 6487 } 6488 if (Parser.getTok().isNot(AsmToken::RParen)) { 6489 SMLoc Loc = getLexer().getLoc(); 6490 return Error(Loc, "unexpected token, expected ')'"); 6491 } 6492 Operands.push_back( 6493 MipsOperand::CreateToken(")", getLexer().getLoc(), *this)); 6494 Parser.Lex(); 6495 } 6496 return false; 6497 } 6498 6499 /// Sometimes (i.e. in MSA) the operand may be followed immediately by 6500 /// either one of these. 6501 /// ::= '[', register, ']' 6502 /// ::= '[', integer, ']' 6503 /// handle it before we iterate so we don't get tripped up by the lack of 6504 /// a comma. 6505 bool MipsAsmParser::parseBracketSuffix(StringRef Name, 6506 OperandVector &Operands) { 6507 MCAsmParser &Parser = getParser(); 6508 if (getLexer().is(AsmToken::LBrac)) { 6509 Operands.push_back( 6510 MipsOperand::CreateToken("[", getLexer().getLoc(), *this)); 6511 Parser.Lex(); 6512 if (parseOperand(Operands, Name)) { 6513 SMLoc Loc = getLexer().getLoc(); 6514 return Error(Loc, "unexpected token in argument list"); 6515 } 6516 if (Parser.getTok().isNot(AsmToken::RBrac)) { 6517 SMLoc Loc = getLexer().getLoc(); 6518 return Error(Loc, "unexpected token, expected ']'"); 6519 } 6520 Operands.push_back( 6521 MipsOperand::CreateToken("]", getLexer().getLoc(), *this)); 6522 Parser.Lex(); 6523 } 6524 return false; 6525 } 6526 6527 static std::string MipsMnemonicSpellCheck(StringRef S, const FeatureBitset &FBS, 6528 unsigned VariantID = 0); 6529 6530 bool MipsAsmParser::ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 6531 SMLoc NameLoc, OperandVector &Operands) { 6532 MCAsmParser &Parser = getParser(); 6533 LLVM_DEBUG(dbgs() << "ParseInstruction\n"); 6534 6535 // We have reached first instruction, module directive are now forbidden. 6536 getTargetStreamer().forbidModuleDirective(); 6537 6538 // Check if we have valid mnemonic 6539 if (!mnemonicIsValid(Name, 0)) { 6540 FeatureBitset FBS = ComputeAvailableFeatures(getSTI().getFeatureBits()); 6541 std::string Suggestion = MipsMnemonicSpellCheck(Name, FBS); 6542 return Error(NameLoc, "unknown instruction" + Suggestion); 6543 } 6544 // First operand in MCInst is instruction mnemonic. 6545 Operands.push_back(MipsOperand::CreateToken(Name, NameLoc, *this)); 6546 6547 // Read the remaining operands. 6548 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6549 // Read the first operand. 6550 if (parseOperand(Operands, Name)) { 6551 SMLoc Loc = getLexer().getLoc(); 6552 return Error(Loc, "unexpected token in argument list"); 6553 } 6554 if (getLexer().is(AsmToken::LBrac) && parseBracketSuffix(Name, Operands)) 6555 return true; 6556 // AFAIK, parenthesis suffixes are never on the first operand 6557 6558 while (getLexer().is(AsmToken::Comma)) { 6559 Parser.Lex(); // Eat the comma. 6560 // Parse and remember the operand. 6561 if (parseOperand(Operands, Name)) { 6562 SMLoc Loc = getLexer().getLoc(); 6563 return Error(Loc, "unexpected token in argument list"); 6564 } 6565 // Parse bracket and parenthesis suffixes before we iterate 6566 if (getLexer().is(AsmToken::LBrac)) { 6567 if (parseBracketSuffix(Name, Operands)) 6568 return true; 6569 } else if (getLexer().is(AsmToken::LParen) && 6570 parseParenSuffix(Name, Operands)) 6571 return true; 6572 } 6573 } 6574 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6575 SMLoc Loc = getLexer().getLoc(); 6576 return Error(Loc, "unexpected token in argument list"); 6577 } 6578 Parser.Lex(); // Consume the EndOfStatement. 6579 return false; 6580 } 6581 6582 // FIXME: Given that these have the same name, these should both be 6583 // consistent on affecting the Parser. 6584 bool MipsAsmParser::reportParseError(Twine ErrorMsg) { 6585 SMLoc Loc = getLexer().getLoc(); 6586 return Error(Loc, ErrorMsg); 6587 } 6588 6589 bool MipsAsmParser::reportParseError(SMLoc Loc, Twine ErrorMsg) { 6590 return Error(Loc, ErrorMsg); 6591 } 6592 6593 bool MipsAsmParser::parseSetNoAtDirective() { 6594 MCAsmParser &Parser = getParser(); 6595 // Line should look like: ".set noat". 6596 6597 // Set the $at register to $0. 6598 AssemblerOptions.back()->setATRegIndex(0); 6599 6600 Parser.Lex(); // Eat "noat". 6601 6602 // If this is not the end of the statement, report an error. 6603 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6604 reportParseError("unexpected token, expected end of statement"); 6605 return false; 6606 } 6607 6608 getTargetStreamer().emitDirectiveSetNoAt(); 6609 Parser.Lex(); // Consume the EndOfStatement. 6610 return false; 6611 } 6612 6613 bool MipsAsmParser::parseSetAtDirective() { 6614 // Line can be: ".set at", which sets $at to $1 6615 // or ".set at=$reg", which sets $at to $reg. 6616 MCAsmParser &Parser = getParser(); 6617 Parser.Lex(); // Eat "at". 6618 6619 if (getLexer().is(AsmToken::EndOfStatement)) { 6620 // No register was specified, so we set $at to $1. 6621 AssemblerOptions.back()->setATRegIndex(1); 6622 6623 getTargetStreamer().emitDirectiveSetAt(); 6624 Parser.Lex(); // Consume the EndOfStatement. 6625 return false; 6626 } 6627 6628 if (getLexer().isNot(AsmToken::Equal)) { 6629 reportParseError("unexpected token, expected equals sign"); 6630 return false; 6631 } 6632 Parser.Lex(); // Eat "=". 6633 6634 if (getLexer().isNot(AsmToken::Dollar)) { 6635 if (getLexer().is(AsmToken::EndOfStatement)) { 6636 reportParseError("no register specified"); 6637 return false; 6638 } else { 6639 reportParseError("unexpected token, expected dollar sign '$'"); 6640 return false; 6641 } 6642 } 6643 Parser.Lex(); // Eat "$". 6644 6645 // Find out what "reg" is. 6646 unsigned AtRegNo; 6647 const AsmToken &Reg = Parser.getTok(); 6648 if (Reg.is(AsmToken::Identifier)) { 6649 AtRegNo = matchCPURegisterName(Reg.getIdentifier()); 6650 } else if (Reg.is(AsmToken::Integer)) { 6651 AtRegNo = Reg.getIntVal(); 6652 } else { 6653 reportParseError("unexpected token, expected identifier or integer"); 6654 return false; 6655 } 6656 6657 // Check if $reg is a valid register. If it is, set $at to $reg. 6658 if (!AssemblerOptions.back()->setATRegIndex(AtRegNo)) { 6659 reportParseError("invalid register"); 6660 return false; 6661 } 6662 Parser.Lex(); // Eat "reg". 6663 6664 // If this is not the end of the statement, report an error. 6665 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6666 reportParseError("unexpected token, expected end of statement"); 6667 return false; 6668 } 6669 6670 getTargetStreamer().emitDirectiveSetAtWithArg(AtRegNo); 6671 6672 Parser.Lex(); // Consume the EndOfStatement. 6673 return false; 6674 } 6675 6676 bool MipsAsmParser::parseSetReorderDirective() { 6677 MCAsmParser &Parser = getParser(); 6678 Parser.Lex(); 6679 // If this is not the end of the statement, report an error. 6680 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6681 reportParseError("unexpected token, expected end of statement"); 6682 return false; 6683 } 6684 AssemblerOptions.back()->setReorder(); 6685 getTargetStreamer().emitDirectiveSetReorder(); 6686 Parser.Lex(); // Consume the EndOfStatement. 6687 return false; 6688 } 6689 6690 bool MipsAsmParser::parseSetNoReorderDirective() { 6691 MCAsmParser &Parser = getParser(); 6692 Parser.Lex(); 6693 // If this is not the end of the statement, report an error. 6694 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6695 reportParseError("unexpected token, expected end of statement"); 6696 return false; 6697 } 6698 AssemblerOptions.back()->setNoReorder(); 6699 getTargetStreamer().emitDirectiveSetNoReorder(); 6700 Parser.Lex(); // Consume the EndOfStatement. 6701 return false; 6702 } 6703 6704 bool MipsAsmParser::parseSetMacroDirective() { 6705 MCAsmParser &Parser = getParser(); 6706 Parser.Lex(); 6707 // If this is not the end of the statement, report an error. 6708 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6709 reportParseError("unexpected token, expected end of statement"); 6710 return false; 6711 } 6712 AssemblerOptions.back()->setMacro(); 6713 getTargetStreamer().emitDirectiveSetMacro(); 6714 Parser.Lex(); // Consume the EndOfStatement. 6715 return false; 6716 } 6717 6718 bool MipsAsmParser::parseSetNoMacroDirective() { 6719 MCAsmParser &Parser = getParser(); 6720 Parser.Lex(); 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 if (AssemblerOptions.back()->isReorder()) { 6727 reportParseError("`noreorder' must be set before `nomacro'"); 6728 return false; 6729 } 6730 AssemblerOptions.back()->setNoMacro(); 6731 getTargetStreamer().emitDirectiveSetNoMacro(); 6732 Parser.Lex(); // Consume the EndOfStatement. 6733 return false; 6734 } 6735 6736 bool MipsAsmParser::parseSetMsaDirective() { 6737 MCAsmParser &Parser = getParser(); 6738 Parser.Lex(); 6739 6740 // If this is not the end of the statement, report an error. 6741 if (getLexer().isNot(AsmToken::EndOfStatement)) 6742 return reportParseError("unexpected token, expected end of statement"); 6743 6744 setFeatureBits(Mips::FeatureMSA, "msa"); 6745 getTargetStreamer().emitDirectiveSetMsa(); 6746 return false; 6747 } 6748 6749 bool MipsAsmParser::parseSetNoMsaDirective() { 6750 MCAsmParser &Parser = getParser(); 6751 Parser.Lex(); 6752 6753 // If this is not the end of the statement, report an error. 6754 if (getLexer().isNot(AsmToken::EndOfStatement)) 6755 return reportParseError("unexpected token, expected end of statement"); 6756 6757 clearFeatureBits(Mips::FeatureMSA, "msa"); 6758 getTargetStreamer().emitDirectiveSetNoMsa(); 6759 return false; 6760 } 6761 6762 bool MipsAsmParser::parseSetNoDspDirective() { 6763 MCAsmParser &Parser = getParser(); 6764 Parser.Lex(); // Eat "nodsp". 6765 6766 // If this is not the end of the statement, report an error. 6767 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6768 reportParseError("unexpected token, expected end of statement"); 6769 return false; 6770 } 6771 6772 clearFeatureBits(Mips::FeatureDSP, "dsp"); 6773 getTargetStreamer().emitDirectiveSetNoDsp(); 6774 return false; 6775 } 6776 6777 bool MipsAsmParser::parseSetMips16Directive() { 6778 MCAsmParser &Parser = getParser(); 6779 Parser.Lex(); // Eat "mips16". 6780 6781 // If this is not the end of the statement, report an error. 6782 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6783 reportParseError("unexpected token, expected end of statement"); 6784 return false; 6785 } 6786 6787 setFeatureBits(Mips::FeatureMips16, "mips16"); 6788 getTargetStreamer().emitDirectiveSetMips16(); 6789 Parser.Lex(); // Consume the EndOfStatement. 6790 return false; 6791 } 6792 6793 bool MipsAsmParser::parseSetNoMips16Directive() { 6794 MCAsmParser &Parser = getParser(); 6795 Parser.Lex(); // Eat "nomips16". 6796 6797 // If this is not the end of the statement, report an error. 6798 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6799 reportParseError("unexpected token, expected end of statement"); 6800 return false; 6801 } 6802 6803 clearFeatureBits(Mips::FeatureMips16, "mips16"); 6804 getTargetStreamer().emitDirectiveSetNoMips16(); 6805 Parser.Lex(); // Consume the EndOfStatement. 6806 return false; 6807 } 6808 6809 bool MipsAsmParser::parseSetFpDirective() { 6810 MCAsmParser &Parser = getParser(); 6811 MipsABIFlagsSection::FpABIKind FpAbiVal; 6812 // Line can be: .set fp=32 6813 // .set fp=xx 6814 // .set fp=64 6815 Parser.Lex(); // Eat fp token 6816 AsmToken Tok = Parser.getTok(); 6817 if (Tok.isNot(AsmToken::Equal)) { 6818 reportParseError("unexpected token, expected equals sign '='"); 6819 return false; 6820 } 6821 Parser.Lex(); // Eat '=' token. 6822 Tok = Parser.getTok(); 6823 6824 if (!parseFpABIValue(FpAbiVal, ".set")) 6825 return false; 6826 6827 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6828 reportParseError("unexpected token, expected end of statement"); 6829 return false; 6830 } 6831 getTargetStreamer().emitDirectiveSetFp(FpAbiVal); 6832 Parser.Lex(); // Consume the EndOfStatement. 6833 return false; 6834 } 6835 6836 bool MipsAsmParser::parseSetOddSPRegDirective() { 6837 MCAsmParser &Parser = getParser(); 6838 6839 Parser.Lex(); // Eat "oddspreg". 6840 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6841 reportParseError("unexpected token, expected end of statement"); 6842 return false; 6843 } 6844 6845 clearFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 6846 getTargetStreamer().emitDirectiveSetOddSPReg(); 6847 return false; 6848 } 6849 6850 bool MipsAsmParser::parseSetNoOddSPRegDirective() { 6851 MCAsmParser &Parser = getParser(); 6852 6853 Parser.Lex(); // Eat "nooddspreg". 6854 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6855 reportParseError("unexpected token, expected end of statement"); 6856 return false; 6857 } 6858 6859 setFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 6860 getTargetStreamer().emitDirectiveSetNoOddSPReg(); 6861 return false; 6862 } 6863 6864 bool MipsAsmParser::parseSetMtDirective() { 6865 MCAsmParser &Parser = getParser(); 6866 Parser.Lex(); // Eat "mt". 6867 6868 // If this is not the end of the statement, report an error. 6869 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6870 reportParseError("unexpected token, expected end of statement"); 6871 return false; 6872 } 6873 6874 setFeatureBits(Mips::FeatureMT, "mt"); 6875 getTargetStreamer().emitDirectiveSetMt(); 6876 Parser.Lex(); // Consume the EndOfStatement. 6877 return false; 6878 } 6879 6880 bool MipsAsmParser::parseSetNoMtDirective() { 6881 MCAsmParser &Parser = getParser(); 6882 Parser.Lex(); // Eat "nomt". 6883 6884 // If this is not the end of the statement, report an error. 6885 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6886 reportParseError("unexpected token, expected end of statement"); 6887 return false; 6888 } 6889 6890 clearFeatureBits(Mips::FeatureMT, "mt"); 6891 6892 getTargetStreamer().emitDirectiveSetNoMt(); 6893 Parser.Lex(); // Consume the EndOfStatement. 6894 return false; 6895 } 6896 6897 bool MipsAsmParser::parseSetNoCRCDirective() { 6898 MCAsmParser &Parser = getParser(); 6899 Parser.Lex(); // Eat "nocrc". 6900 6901 // If this is not the end of the statement, report an error. 6902 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6903 reportParseError("unexpected token, expected end of statement"); 6904 return false; 6905 } 6906 6907 clearFeatureBits(Mips::FeatureCRC, "crc"); 6908 6909 getTargetStreamer().emitDirectiveSetNoCRC(); 6910 Parser.Lex(); // Consume the EndOfStatement. 6911 return false; 6912 } 6913 6914 bool MipsAsmParser::parseSetNoVirtDirective() { 6915 MCAsmParser &Parser = getParser(); 6916 Parser.Lex(); // Eat "novirt". 6917 6918 // If this is not the end of the statement, report an error. 6919 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6920 reportParseError("unexpected token, expected end of statement"); 6921 return false; 6922 } 6923 6924 clearFeatureBits(Mips::FeatureVirt, "virt"); 6925 6926 getTargetStreamer().emitDirectiveSetNoVirt(); 6927 Parser.Lex(); // Consume the EndOfStatement. 6928 return false; 6929 } 6930 6931 bool MipsAsmParser::parseSetNoGINVDirective() { 6932 MCAsmParser &Parser = getParser(); 6933 Parser.Lex(); // Eat "noginv". 6934 6935 // If this is not the end of the statement, report an error. 6936 if (getLexer().isNot(AsmToken::EndOfStatement)) { 6937 reportParseError("unexpected token, expected end of statement"); 6938 return false; 6939 } 6940 6941 clearFeatureBits(Mips::FeatureGINV, "ginv"); 6942 6943 getTargetStreamer().emitDirectiveSetNoGINV(); 6944 Parser.Lex(); // Consume the EndOfStatement. 6945 return false; 6946 } 6947 6948 bool MipsAsmParser::parseSetPopDirective() { 6949 MCAsmParser &Parser = getParser(); 6950 SMLoc Loc = getLexer().getLoc(); 6951 6952 Parser.Lex(); 6953 if (getLexer().isNot(AsmToken::EndOfStatement)) 6954 return reportParseError("unexpected token, expected end of statement"); 6955 6956 // Always keep an element on the options "stack" to prevent the user 6957 // from changing the initial options. This is how we remember them. 6958 if (AssemblerOptions.size() == 2) 6959 return reportParseError(Loc, ".set pop with no .set push"); 6960 6961 MCSubtargetInfo &STI = copySTI(); 6962 AssemblerOptions.pop_back(); 6963 setAvailableFeatures( 6964 ComputeAvailableFeatures(AssemblerOptions.back()->getFeatures())); 6965 STI.setFeatureBits(AssemblerOptions.back()->getFeatures()); 6966 6967 getTargetStreamer().emitDirectiveSetPop(); 6968 return false; 6969 } 6970 6971 bool MipsAsmParser::parseSetPushDirective() { 6972 MCAsmParser &Parser = getParser(); 6973 Parser.Lex(); 6974 if (getLexer().isNot(AsmToken::EndOfStatement)) 6975 return reportParseError("unexpected token, expected end of statement"); 6976 6977 // Create a copy of the current assembler options environment and push it. 6978 AssemblerOptions.push_back( 6979 llvm::make_unique<MipsAssemblerOptions>(AssemblerOptions.back().get())); 6980 6981 getTargetStreamer().emitDirectiveSetPush(); 6982 return false; 6983 } 6984 6985 bool MipsAsmParser::parseSetSoftFloatDirective() { 6986 MCAsmParser &Parser = getParser(); 6987 Parser.Lex(); 6988 if (getLexer().isNot(AsmToken::EndOfStatement)) 6989 return reportParseError("unexpected token, expected end of statement"); 6990 6991 setFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 6992 getTargetStreamer().emitDirectiveSetSoftFloat(); 6993 return false; 6994 } 6995 6996 bool MipsAsmParser::parseSetHardFloatDirective() { 6997 MCAsmParser &Parser = getParser(); 6998 Parser.Lex(); 6999 if (getLexer().isNot(AsmToken::EndOfStatement)) 7000 return reportParseError("unexpected token, expected end of statement"); 7001 7002 clearFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 7003 getTargetStreamer().emitDirectiveSetHardFloat(); 7004 return false; 7005 } 7006 7007 bool MipsAsmParser::parseSetAssignment() { 7008 StringRef Name; 7009 MCAsmParser &Parser = getParser(); 7010 7011 if (Parser.parseIdentifier(Name)) 7012 return reportParseError("expected identifier after .set"); 7013 7014 if (getLexer().isNot(AsmToken::Comma)) 7015 return reportParseError("unexpected token, expected comma"); 7016 Lex(); // Eat comma 7017 7018 if (getLexer().is(AsmToken::Dollar) && 7019 getLexer().peekTok().is(AsmToken::Integer)) { 7020 // Parse assignment of a numeric register: 7021 // .set r1,$1 7022 Parser.Lex(); // Eat $. 7023 RegisterSets[Name] = Parser.getTok(); 7024 Parser.Lex(); // Eat identifier. 7025 getContext().getOrCreateSymbol(Name); 7026 return false; 7027 } 7028 7029 MCSymbol *Sym; 7030 const MCExpr *Value; 7031 if (MCParserUtils::parseAssignmentExpression(Name, /* allow_redef */ true, 7032 Parser, Sym, Value)) 7033 return true; 7034 Sym->setVariableValue(Value); 7035 7036 return false; 7037 } 7038 7039 bool MipsAsmParser::parseSetMips0Directive() { 7040 MCAsmParser &Parser = getParser(); 7041 Parser.Lex(); 7042 if (getLexer().isNot(AsmToken::EndOfStatement)) 7043 return reportParseError("unexpected token, expected end of statement"); 7044 7045 // Reset assembler options to their initial values. 7046 MCSubtargetInfo &STI = copySTI(); 7047 setAvailableFeatures( 7048 ComputeAvailableFeatures(AssemblerOptions.front()->getFeatures())); 7049 STI.setFeatureBits(AssemblerOptions.front()->getFeatures()); 7050 AssemblerOptions.back()->setFeatures(AssemblerOptions.front()->getFeatures()); 7051 7052 getTargetStreamer().emitDirectiveSetMips0(); 7053 return false; 7054 } 7055 7056 bool MipsAsmParser::parseSetArchDirective() { 7057 MCAsmParser &Parser = getParser(); 7058 Parser.Lex(); 7059 if (getLexer().isNot(AsmToken::Equal)) 7060 return reportParseError("unexpected token, expected equals sign"); 7061 7062 Parser.Lex(); 7063 StringRef Arch; 7064 if (Parser.parseIdentifier(Arch)) 7065 return reportParseError("expected arch identifier"); 7066 7067 StringRef ArchFeatureName = 7068 StringSwitch<StringRef>(Arch) 7069 .Case("mips1", "mips1") 7070 .Case("mips2", "mips2") 7071 .Case("mips3", "mips3") 7072 .Case("mips4", "mips4") 7073 .Case("mips5", "mips5") 7074 .Case("mips32", "mips32") 7075 .Case("mips32r2", "mips32r2") 7076 .Case("mips32r3", "mips32r3") 7077 .Case("mips32r5", "mips32r5") 7078 .Case("mips32r6", "mips32r6") 7079 .Case("mips64", "mips64") 7080 .Case("mips64r2", "mips64r2") 7081 .Case("mips64r3", "mips64r3") 7082 .Case("mips64r5", "mips64r5") 7083 .Case("mips64r6", "mips64r6") 7084 .Case("octeon", "cnmips") 7085 .Case("r4000", "mips3") // This is an implementation of Mips3. 7086 .Default(""); 7087 7088 if (ArchFeatureName.empty()) 7089 return reportParseError("unsupported architecture"); 7090 7091 if (ArchFeatureName == "mips64r6" && inMicroMipsMode()) 7092 return reportParseError("mips64r6 does not support microMIPS"); 7093 7094 selectArch(ArchFeatureName); 7095 getTargetStreamer().emitDirectiveSetArch(Arch); 7096 return false; 7097 } 7098 7099 bool MipsAsmParser::parseSetFeature(uint64_t Feature) { 7100 MCAsmParser &Parser = getParser(); 7101 Parser.Lex(); 7102 if (getLexer().isNot(AsmToken::EndOfStatement)) 7103 return reportParseError("unexpected token, expected end of statement"); 7104 7105 switch (Feature) { 7106 default: 7107 llvm_unreachable("Unimplemented feature"); 7108 case Mips::FeatureDSP: 7109 setFeatureBits(Mips::FeatureDSP, "dsp"); 7110 getTargetStreamer().emitDirectiveSetDsp(); 7111 break; 7112 case Mips::FeatureDSPR2: 7113 setFeatureBits(Mips::FeatureDSPR2, "dspr2"); 7114 getTargetStreamer().emitDirectiveSetDspr2(); 7115 break; 7116 case Mips::FeatureMicroMips: 7117 setFeatureBits(Mips::FeatureMicroMips, "micromips"); 7118 getTargetStreamer().emitDirectiveSetMicroMips(); 7119 break; 7120 case Mips::FeatureMips1: 7121 selectArch("mips1"); 7122 getTargetStreamer().emitDirectiveSetMips1(); 7123 break; 7124 case Mips::FeatureMips2: 7125 selectArch("mips2"); 7126 getTargetStreamer().emitDirectiveSetMips2(); 7127 break; 7128 case Mips::FeatureMips3: 7129 selectArch("mips3"); 7130 getTargetStreamer().emitDirectiveSetMips3(); 7131 break; 7132 case Mips::FeatureMips4: 7133 selectArch("mips4"); 7134 getTargetStreamer().emitDirectiveSetMips4(); 7135 break; 7136 case Mips::FeatureMips5: 7137 selectArch("mips5"); 7138 getTargetStreamer().emitDirectiveSetMips5(); 7139 break; 7140 case Mips::FeatureMips32: 7141 selectArch("mips32"); 7142 getTargetStreamer().emitDirectiveSetMips32(); 7143 break; 7144 case Mips::FeatureMips32r2: 7145 selectArch("mips32r2"); 7146 getTargetStreamer().emitDirectiveSetMips32R2(); 7147 break; 7148 case Mips::FeatureMips32r3: 7149 selectArch("mips32r3"); 7150 getTargetStreamer().emitDirectiveSetMips32R3(); 7151 break; 7152 case Mips::FeatureMips32r5: 7153 selectArch("mips32r5"); 7154 getTargetStreamer().emitDirectiveSetMips32R5(); 7155 break; 7156 case Mips::FeatureMips32r6: 7157 selectArch("mips32r6"); 7158 getTargetStreamer().emitDirectiveSetMips32R6(); 7159 break; 7160 case Mips::FeatureMips64: 7161 selectArch("mips64"); 7162 getTargetStreamer().emitDirectiveSetMips64(); 7163 break; 7164 case Mips::FeatureMips64r2: 7165 selectArch("mips64r2"); 7166 getTargetStreamer().emitDirectiveSetMips64R2(); 7167 break; 7168 case Mips::FeatureMips64r3: 7169 selectArch("mips64r3"); 7170 getTargetStreamer().emitDirectiveSetMips64R3(); 7171 break; 7172 case Mips::FeatureMips64r5: 7173 selectArch("mips64r5"); 7174 getTargetStreamer().emitDirectiveSetMips64R5(); 7175 break; 7176 case Mips::FeatureMips64r6: 7177 selectArch("mips64r6"); 7178 getTargetStreamer().emitDirectiveSetMips64R6(); 7179 break; 7180 case Mips::FeatureCRC: 7181 setFeatureBits(Mips::FeatureCRC, "crc"); 7182 getTargetStreamer().emitDirectiveSetCRC(); 7183 break; 7184 case Mips::FeatureVirt: 7185 setFeatureBits(Mips::FeatureVirt, "virt"); 7186 getTargetStreamer().emitDirectiveSetVirt(); 7187 break; 7188 case Mips::FeatureGINV: 7189 setFeatureBits(Mips::FeatureGINV, "ginv"); 7190 getTargetStreamer().emitDirectiveSetGINV(); 7191 break; 7192 } 7193 return false; 7194 } 7195 7196 bool MipsAsmParser::eatComma(StringRef ErrorStr) { 7197 MCAsmParser &Parser = getParser(); 7198 if (getLexer().isNot(AsmToken::Comma)) { 7199 SMLoc Loc = getLexer().getLoc(); 7200 return Error(Loc, ErrorStr); 7201 } 7202 7203 Parser.Lex(); // Eat the comma. 7204 return true; 7205 } 7206 7207 // Used to determine if .cpload, .cprestore, and .cpsetup have any effect. 7208 // In this class, it is only used for .cprestore. 7209 // FIXME: Only keep track of IsPicEnabled in one place, instead of in both 7210 // MipsTargetELFStreamer and MipsAsmParser. 7211 bool MipsAsmParser::isPicAndNotNxxAbi() { 7212 return inPicMode() && !(isABI_N32() || isABI_N64()); 7213 } 7214 7215 bool MipsAsmParser::parseDirectiveCpLoad(SMLoc Loc) { 7216 if (AssemblerOptions.back()->isReorder()) 7217 Warning(Loc, ".cpload should be inside a noreorder section"); 7218 7219 if (inMips16Mode()) { 7220 reportParseError(".cpload is not supported in Mips16 mode"); 7221 return false; 7222 } 7223 7224 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg; 7225 OperandMatchResultTy ResTy = parseAnyRegister(Reg); 7226 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 7227 reportParseError("expected register containing function address"); 7228 return false; 7229 } 7230 7231 MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]); 7232 if (!RegOpnd.isGPRAsmReg()) { 7233 reportParseError(RegOpnd.getStartLoc(), "invalid register"); 7234 return false; 7235 } 7236 7237 // If this is not the end of the statement, report an error. 7238 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7239 reportParseError("unexpected token, expected end of statement"); 7240 return false; 7241 } 7242 7243 getTargetStreamer().emitDirectiveCpLoad(RegOpnd.getGPR32Reg()); 7244 return false; 7245 } 7246 7247 bool MipsAsmParser::parseDirectiveCpLocal(SMLoc Loc) { 7248 if (!isABI_N32() && !isABI_N64()) { 7249 reportParseError(".cplocal is allowed only in N32 or N64 mode"); 7250 return false; 7251 } 7252 7253 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> Reg; 7254 OperandMatchResultTy ResTy = parseAnyRegister(Reg); 7255 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 7256 reportParseError("expected register containing global pointer"); 7257 return false; 7258 } 7259 7260 MipsOperand &RegOpnd = static_cast<MipsOperand &>(*Reg[0]); 7261 if (!RegOpnd.isGPRAsmReg()) { 7262 reportParseError(RegOpnd.getStartLoc(), "invalid register"); 7263 return false; 7264 } 7265 7266 // If this is not the end of the statement, report an error. 7267 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7268 reportParseError("unexpected token, expected end of statement"); 7269 return false; 7270 } 7271 getParser().Lex(); // Consume the EndOfStatement. 7272 7273 unsigned NewReg = RegOpnd.getGPR32Reg(); 7274 if (IsPicEnabled) 7275 GPReg = NewReg; 7276 7277 getTargetStreamer().emitDirectiveCpLocal(NewReg); 7278 return false; 7279 } 7280 7281 bool MipsAsmParser::parseDirectiveCpRestore(SMLoc Loc) { 7282 MCAsmParser &Parser = getParser(); 7283 7284 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it 7285 // is used in non-PIC mode. 7286 7287 if (inMips16Mode()) { 7288 reportParseError(".cprestore is not supported in Mips16 mode"); 7289 return false; 7290 } 7291 7292 // Get the stack offset value. 7293 const MCExpr *StackOffset; 7294 int64_t StackOffsetVal; 7295 if (Parser.parseExpression(StackOffset)) { 7296 reportParseError("expected stack offset value"); 7297 return false; 7298 } 7299 7300 if (!StackOffset->evaluateAsAbsolute(StackOffsetVal)) { 7301 reportParseError("stack offset is not an absolute expression"); 7302 return false; 7303 } 7304 7305 if (StackOffsetVal < 0) { 7306 Warning(Loc, ".cprestore with negative stack offset has no effect"); 7307 IsCpRestoreSet = false; 7308 } else { 7309 IsCpRestoreSet = true; 7310 CpRestoreOffset = StackOffsetVal; 7311 } 7312 7313 // If this is not the end of the statement, report an error. 7314 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7315 reportParseError("unexpected token, expected end of statement"); 7316 return false; 7317 } 7318 7319 if (!getTargetStreamer().emitDirectiveCpRestore( 7320 CpRestoreOffset, [&]() { return getATReg(Loc); }, Loc, STI)) 7321 return true; 7322 Parser.Lex(); // Consume the EndOfStatement. 7323 return false; 7324 } 7325 7326 bool MipsAsmParser::parseDirectiveCPSetup() { 7327 MCAsmParser &Parser = getParser(); 7328 unsigned FuncReg; 7329 unsigned Save; 7330 bool SaveIsReg = true; 7331 7332 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg; 7333 OperandMatchResultTy ResTy = parseAnyRegister(TmpReg); 7334 if (ResTy == MatchOperand_NoMatch) { 7335 reportParseError("expected register containing function address"); 7336 return false; 7337 } 7338 7339 MipsOperand &FuncRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 7340 if (!FuncRegOpnd.isGPRAsmReg()) { 7341 reportParseError(FuncRegOpnd.getStartLoc(), "invalid register"); 7342 return false; 7343 } 7344 7345 FuncReg = FuncRegOpnd.getGPR32Reg(); 7346 TmpReg.clear(); 7347 7348 if (!eatComma("unexpected token, expected comma")) 7349 return true; 7350 7351 ResTy = parseAnyRegister(TmpReg); 7352 if (ResTy == MatchOperand_NoMatch) { 7353 const MCExpr *OffsetExpr; 7354 int64_t OffsetVal; 7355 SMLoc ExprLoc = getLexer().getLoc(); 7356 7357 if (Parser.parseExpression(OffsetExpr) || 7358 !OffsetExpr->evaluateAsAbsolute(OffsetVal)) { 7359 reportParseError(ExprLoc, "expected save register or stack offset"); 7360 return false; 7361 } 7362 7363 Save = OffsetVal; 7364 SaveIsReg = false; 7365 } else { 7366 MipsOperand &SaveOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 7367 if (!SaveOpnd.isGPRAsmReg()) { 7368 reportParseError(SaveOpnd.getStartLoc(), "invalid register"); 7369 return false; 7370 } 7371 Save = SaveOpnd.getGPR32Reg(); 7372 } 7373 7374 if (!eatComma("unexpected token, expected comma")) 7375 return true; 7376 7377 const MCExpr *Expr; 7378 if (Parser.parseExpression(Expr)) { 7379 reportParseError("expected expression"); 7380 return false; 7381 } 7382 7383 if (Expr->getKind() != MCExpr::SymbolRef) { 7384 reportParseError("expected symbol"); 7385 return false; 7386 } 7387 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr *>(Expr); 7388 7389 CpSaveLocation = Save; 7390 CpSaveLocationIsRegister = SaveIsReg; 7391 7392 getTargetStreamer().emitDirectiveCpsetup(FuncReg, Save, Ref->getSymbol(), 7393 SaveIsReg); 7394 return false; 7395 } 7396 7397 bool MipsAsmParser::parseDirectiveCPReturn() { 7398 getTargetStreamer().emitDirectiveCpreturn(CpSaveLocation, 7399 CpSaveLocationIsRegister); 7400 return false; 7401 } 7402 7403 bool MipsAsmParser::parseDirectiveNaN() { 7404 MCAsmParser &Parser = getParser(); 7405 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7406 const AsmToken &Tok = Parser.getTok(); 7407 7408 if (Tok.getString() == "2008") { 7409 Parser.Lex(); 7410 getTargetStreamer().emitDirectiveNaN2008(); 7411 return false; 7412 } else if (Tok.getString() == "legacy") { 7413 Parser.Lex(); 7414 getTargetStreamer().emitDirectiveNaNLegacy(); 7415 return false; 7416 } 7417 } 7418 // If we don't recognize the option passed to the .nan 7419 // directive (e.g. no option or unknown option), emit an error. 7420 reportParseError("invalid option in .nan directive"); 7421 return false; 7422 } 7423 7424 bool MipsAsmParser::parseDirectiveSet() { 7425 const AsmToken &Tok = getParser().getTok(); 7426 StringRef IdVal = Tok.getString(); 7427 SMLoc Loc = Tok.getLoc(); 7428 7429 if (IdVal == "noat") 7430 return parseSetNoAtDirective(); 7431 if (IdVal == "at") 7432 return parseSetAtDirective(); 7433 if (IdVal == "arch") 7434 return parseSetArchDirective(); 7435 if (IdVal == "bopt") { 7436 Warning(Loc, "'bopt' feature is unsupported"); 7437 getParser().Lex(); 7438 return false; 7439 } 7440 if (IdVal == "nobopt") { 7441 // We're already running in nobopt mode, so nothing to do. 7442 getParser().Lex(); 7443 return false; 7444 } 7445 if (IdVal == "fp") 7446 return parseSetFpDirective(); 7447 if (IdVal == "oddspreg") 7448 return parseSetOddSPRegDirective(); 7449 if (IdVal == "nooddspreg") 7450 return parseSetNoOddSPRegDirective(); 7451 if (IdVal == "pop") 7452 return parseSetPopDirective(); 7453 if (IdVal == "push") 7454 return parseSetPushDirective(); 7455 if (IdVal == "reorder") 7456 return parseSetReorderDirective(); 7457 if (IdVal == "noreorder") 7458 return parseSetNoReorderDirective(); 7459 if (IdVal == "macro") 7460 return parseSetMacroDirective(); 7461 if (IdVal == "nomacro") 7462 return parseSetNoMacroDirective(); 7463 if (IdVal == "mips16") 7464 return parseSetMips16Directive(); 7465 if (IdVal == "nomips16") 7466 return parseSetNoMips16Directive(); 7467 if (IdVal == "nomicromips") { 7468 clearFeatureBits(Mips::FeatureMicroMips, "micromips"); 7469 getTargetStreamer().emitDirectiveSetNoMicroMips(); 7470 getParser().eatToEndOfStatement(); 7471 return false; 7472 } 7473 if (IdVal == "micromips") { 7474 if (hasMips64r6()) { 7475 Error(Loc, ".set micromips directive is not supported with MIPS64R6"); 7476 return false; 7477 } 7478 return parseSetFeature(Mips::FeatureMicroMips); 7479 } 7480 if (IdVal == "mips0") 7481 return parseSetMips0Directive(); 7482 if (IdVal == "mips1") 7483 return parseSetFeature(Mips::FeatureMips1); 7484 if (IdVal == "mips2") 7485 return parseSetFeature(Mips::FeatureMips2); 7486 if (IdVal == "mips3") 7487 return parseSetFeature(Mips::FeatureMips3); 7488 if (IdVal == "mips4") 7489 return parseSetFeature(Mips::FeatureMips4); 7490 if (IdVal == "mips5") 7491 return parseSetFeature(Mips::FeatureMips5); 7492 if (IdVal == "mips32") 7493 return parseSetFeature(Mips::FeatureMips32); 7494 if (IdVal == "mips32r2") 7495 return parseSetFeature(Mips::FeatureMips32r2); 7496 if (IdVal == "mips32r3") 7497 return parseSetFeature(Mips::FeatureMips32r3); 7498 if (IdVal == "mips32r5") 7499 return parseSetFeature(Mips::FeatureMips32r5); 7500 if (IdVal == "mips32r6") 7501 return parseSetFeature(Mips::FeatureMips32r6); 7502 if (IdVal == "mips64") 7503 return parseSetFeature(Mips::FeatureMips64); 7504 if (IdVal == "mips64r2") 7505 return parseSetFeature(Mips::FeatureMips64r2); 7506 if (IdVal == "mips64r3") 7507 return parseSetFeature(Mips::FeatureMips64r3); 7508 if (IdVal == "mips64r5") 7509 return parseSetFeature(Mips::FeatureMips64r5); 7510 if (IdVal == "mips64r6") { 7511 if (inMicroMipsMode()) { 7512 Error(Loc, "MIPS64R6 is not supported with microMIPS"); 7513 return false; 7514 } 7515 return parseSetFeature(Mips::FeatureMips64r6); 7516 } 7517 if (IdVal == "dsp") 7518 return parseSetFeature(Mips::FeatureDSP); 7519 if (IdVal == "dspr2") 7520 return parseSetFeature(Mips::FeatureDSPR2); 7521 if (IdVal == "nodsp") 7522 return parseSetNoDspDirective(); 7523 if (IdVal == "msa") 7524 return parseSetMsaDirective(); 7525 if (IdVal == "nomsa") 7526 return parseSetNoMsaDirective(); 7527 if (IdVal == "mt") 7528 return parseSetMtDirective(); 7529 if (IdVal == "nomt") 7530 return parseSetNoMtDirective(); 7531 if (IdVal == "softfloat") 7532 return parseSetSoftFloatDirective(); 7533 if (IdVal == "hardfloat") 7534 return parseSetHardFloatDirective(); 7535 if (IdVal == "crc") 7536 return parseSetFeature(Mips::FeatureCRC); 7537 if (IdVal == "nocrc") 7538 return parseSetNoCRCDirective(); 7539 if (IdVal == "virt") 7540 return parseSetFeature(Mips::FeatureVirt); 7541 if (IdVal == "novirt") 7542 return parseSetNoVirtDirective(); 7543 if (IdVal == "ginv") 7544 return parseSetFeature(Mips::FeatureGINV); 7545 if (IdVal == "noginv") 7546 return parseSetNoGINVDirective(); 7547 7548 // It is just an identifier, look for an assignment. 7549 return parseSetAssignment(); 7550 } 7551 7552 /// parseDirectiveGpWord 7553 /// ::= .gpword local_sym 7554 bool MipsAsmParser::parseDirectiveGpWord() { 7555 MCAsmParser &Parser = getParser(); 7556 const MCExpr *Value; 7557 // EmitGPRel32Value requires an expression, so we are using base class 7558 // method to evaluate the expression. 7559 if (getParser().parseExpression(Value)) 7560 return true; 7561 getParser().getStreamer().EmitGPRel32Value(Value); 7562 7563 if (getLexer().isNot(AsmToken::EndOfStatement)) 7564 return Error(getLexer().getLoc(), 7565 "unexpected token, expected end of statement"); 7566 Parser.Lex(); // Eat EndOfStatement token. 7567 return false; 7568 } 7569 7570 /// parseDirectiveGpDWord 7571 /// ::= .gpdword local_sym 7572 bool MipsAsmParser::parseDirectiveGpDWord() { 7573 MCAsmParser &Parser = getParser(); 7574 const MCExpr *Value; 7575 // EmitGPRel64Value requires an expression, so we are using base class 7576 // method to evaluate the expression. 7577 if (getParser().parseExpression(Value)) 7578 return true; 7579 getParser().getStreamer().EmitGPRel64Value(Value); 7580 7581 if (getLexer().isNot(AsmToken::EndOfStatement)) 7582 return Error(getLexer().getLoc(), 7583 "unexpected token, expected end of statement"); 7584 Parser.Lex(); // Eat EndOfStatement token. 7585 return false; 7586 } 7587 7588 /// parseDirectiveDtpRelWord 7589 /// ::= .dtprelword tls_sym 7590 bool MipsAsmParser::parseDirectiveDtpRelWord() { 7591 MCAsmParser &Parser = getParser(); 7592 const MCExpr *Value; 7593 // EmitDTPRel32Value requires an expression, so we are using base class 7594 // method to evaluate the expression. 7595 if (getParser().parseExpression(Value)) 7596 return true; 7597 getParser().getStreamer().EmitDTPRel32Value(Value); 7598 7599 if (getLexer().isNot(AsmToken::EndOfStatement)) 7600 return Error(getLexer().getLoc(), 7601 "unexpected token, expected end of statement"); 7602 Parser.Lex(); // Eat EndOfStatement token. 7603 return false; 7604 } 7605 7606 /// parseDirectiveDtpRelDWord 7607 /// ::= .dtpreldword tls_sym 7608 bool MipsAsmParser::parseDirectiveDtpRelDWord() { 7609 MCAsmParser &Parser = getParser(); 7610 const MCExpr *Value; 7611 // EmitDTPRel64Value requires an expression, so we are using base class 7612 // method to evaluate the expression. 7613 if (getParser().parseExpression(Value)) 7614 return true; 7615 getParser().getStreamer().EmitDTPRel64Value(Value); 7616 7617 if (getLexer().isNot(AsmToken::EndOfStatement)) 7618 return Error(getLexer().getLoc(), 7619 "unexpected token, expected end of statement"); 7620 Parser.Lex(); // Eat EndOfStatement token. 7621 return false; 7622 } 7623 7624 /// parseDirectiveTpRelWord 7625 /// ::= .tprelword tls_sym 7626 bool MipsAsmParser::parseDirectiveTpRelWord() { 7627 MCAsmParser &Parser = getParser(); 7628 const MCExpr *Value; 7629 // EmitTPRel32Value requires an expression, so we are using base class 7630 // method to evaluate the expression. 7631 if (getParser().parseExpression(Value)) 7632 return true; 7633 getParser().getStreamer().EmitTPRel32Value(Value); 7634 7635 if (getLexer().isNot(AsmToken::EndOfStatement)) 7636 return Error(getLexer().getLoc(), 7637 "unexpected token, expected end of statement"); 7638 Parser.Lex(); // Eat EndOfStatement token. 7639 return false; 7640 } 7641 7642 /// parseDirectiveTpRelDWord 7643 /// ::= .tpreldword tls_sym 7644 bool MipsAsmParser::parseDirectiveTpRelDWord() { 7645 MCAsmParser &Parser = getParser(); 7646 const MCExpr *Value; 7647 // EmitTPRel64Value requires an expression, so we are using base class 7648 // method to evaluate the expression. 7649 if (getParser().parseExpression(Value)) 7650 return true; 7651 getParser().getStreamer().EmitTPRel64Value(Value); 7652 7653 if (getLexer().isNot(AsmToken::EndOfStatement)) 7654 return Error(getLexer().getLoc(), 7655 "unexpected token, expected end of statement"); 7656 Parser.Lex(); // Eat EndOfStatement token. 7657 return false; 7658 } 7659 7660 bool MipsAsmParser::parseDirectiveOption() { 7661 MCAsmParser &Parser = getParser(); 7662 // Get the option token. 7663 AsmToken Tok = Parser.getTok(); 7664 // At the moment only identifiers are supported. 7665 if (Tok.isNot(AsmToken::Identifier)) { 7666 return Error(Parser.getTok().getLoc(), 7667 "unexpected token, expected identifier"); 7668 } 7669 7670 StringRef Option = Tok.getIdentifier(); 7671 7672 if (Option == "pic0") { 7673 // MipsAsmParser needs to know if the current PIC mode changes. 7674 IsPicEnabled = false; 7675 7676 getTargetStreamer().emitDirectiveOptionPic0(); 7677 Parser.Lex(); 7678 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 7679 return Error(Parser.getTok().getLoc(), 7680 "unexpected token, expected end of statement"); 7681 } 7682 return false; 7683 } 7684 7685 if (Option == "pic2") { 7686 // MipsAsmParser needs to know if the current PIC mode changes. 7687 IsPicEnabled = true; 7688 7689 getTargetStreamer().emitDirectiveOptionPic2(); 7690 Parser.Lex(); 7691 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 7692 return Error(Parser.getTok().getLoc(), 7693 "unexpected token, expected end of statement"); 7694 } 7695 return false; 7696 } 7697 7698 // Unknown option. 7699 Warning(Parser.getTok().getLoc(), 7700 "unknown option, expected 'pic0' or 'pic2'"); 7701 Parser.eatToEndOfStatement(); 7702 return false; 7703 } 7704 7705 /// parseInsnDirective 7706 /// ::= .insn 7707 bool MipsAsmParser::parseInsnDirective() { 7708 // If this is not the end of the statement, report an error. 7709 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7710 reportParseError("unexpected token, expected end of statement"); 7711 return false; 7712 } 7713 7714 // The actual label marking happens in 7715 // MipsELFStreamer::createPendingLabelRelocs(). 7716 getTargetStreamer().emitDirectiveInsn(); 7717 7718 getParser().Lex(); // Eat EndOfStatement token. 7719 return false; 7720 } 7721 7722 /// parseRSectionDirective 7723 /// ::= .rdata 7724 bool MipsAsmParser::parseRSectionDirective(StringRef Section) { 7725 // If this is not the end of the statement, report an error. 7726 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7727 reportParseError("unexpected token, expected end of statement"); 7728 return false; 7729 } 7730 7731 MCSection *ELFSection = getContext().getELFSection( 7732 Section, ELF::SHT_PROGBITS, ELF::SHF_ALLOC); 7733 getParser().getStreamer().SwitchSection(ELFSection); 7734 7735 getParser().Lex(); // Eat EndOfStatement token. 7736 return false; 7737 } 7738 7739 /// parseSSectionDirective 7740 /// ::= .sbss 7741 /// ::= .sdata 7742 bool MipsAsmParser::parseSSectionDirective(StringRef Section, unsigned Type) { 7743 // If this is not the end of the statement, report an error. 7744 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7745 reportParseError("unexpected token, expected end of statement"); 7746 return false; 7747 } 7748 7749 MCSection *ELFSection = getContext().getELFSection( 7750 Section, Type, ELF::SHF_WRITE | ELF::SHF_ALLOC | ELF::SHF_MIPS_GPREL); 7751 getParser().getStreamer().SwitchSection(ELFSection); 7752 7753 getParser().Lex(); // Eat EndOfStatement token. 7754 return false; 7755 } 7756 7757 /// parseDirectiveModule 7758 /// ::= .module oddspreg 7759 /// ::= .module nooddspreg 7760 /// ::= .module fp=value 7761 /// ::= .module softfloat 7762 /// ::= .module hardfloat 7763 /// ::= .module mt 7764 /// ::= .module crc 7765 /// ::= .module nocrc 7766 /// ::= .module virt 7767 /// ::= .module novirt 7768 /// ::= .module ginv 7769 /// ::= .module noginv 7770 bool MipsAsmParser::parseDirectiveModule() { 7771 MCAsmParser &Parser = getParser(); 7772 MCAsmLexer &Lexer = getLexer(); 7773 SMLoc L = Lexer.getLoc(); 7774 7775 if (!getTargetStreamer().isModuleDirectiveAllowed()) { 7776 // TODO : get a better message. 7777 reportParseError(".module directive must appear before any code"); 7778 return false; 7779 } 7780 7781 StringRef Option; 7782 if (Parser.parseIdentifier(Option)) { 7783 reportParseError("expected .module option identifier"); 7784 return false; 7785 } 7786 7787 if (Option == "oddspreg") { 7788 clearModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 7789 7790 // Synchronize the abiflags information with the FeatureBits information we 7791 // changed above. 7792 getTargetStreamer().updateABIInfo(*this); 7793 7794 // If printing assembly, use the recently updated abiflags information. 7795 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 7796 // emitted at the end). 7797 getTargetStreamer().emitDirectiveModuleOddSPReg(); 7798 7799 // If this is not the end of the statement, report an error. 7800 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7801 reportParseError("unexpected token, expected end of statement"); 7802 return false; 7803 } 7804 7805 return false; // parseDirectiveModule has finished successfully. 7806 } else if (Option == "nooddspreg") { 7807 if (!isABI_O32()) { 7808 return Error(L, "'.module nooddspreg' requires the O32 ABI"); 7809 } 7810 7811 setModuleFeatureBits(Mips::FeatureNoOddSPReg, "nooddspreg"); 7812 7813 // Synchronize the abiflags information with the FeatureBits information we 7814 // changed above. 7815 getTargetStreamer().updateABIInfo(*this); 7816 7817 // If printing assembly, use the recently updated abiflags information. 7818 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 7819 // emitted at the end). 7820 getTargetStreamer().emitDirectiveModuleOddSPReg(); 7821 7822 // If this is not the end of the statement, report an error. 7823 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7824 reportParseError("unexpected token, expected end of statement"); 7825 return false; 7826 } 7827 7828 return false; // parseDirectiveModule has finished successfully. 7829 } else if (Option == "fp") { 7830 return parseDirectiveModuleFP(); 7831 } else if (Option == "softfloat") { 7832 setModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 7833 7834 // Synchronize the ABI Flags information with the FeatureBits information we 7835 // updated above. 7836 getTargetStreamer().updateABIInfo(*this); 7837 7838 // If printing assembly, use the recently updated ABI Flags information. 7839 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 7840 // emitted later). 7841 getTargetStreamer().emitDirectiveModuleSoftFloat(); 7842 7843 // If this is not the end of the statement, report an error. 7844 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7845 reportParseError("unexpected token, expected end of statement"); 7846 return false; 7847 } 7848 7849 return false; // parseDirectiveModule has finished successfully. 7850 } else if (Option == "hardfloat") { 7851 clearModuleFeatureBits(Mips::FeatureSoftFloat, "soft-float"); 7852 7853 // Synchronize the ABI Flags information with the FeatureBits information we 7854 // updated above. 7855 getTargetStreamer().updateABIInfo(*this); 7856 7857 // If printing assembly, use the recently updated ABI Flags information. 7858 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 7859 // emitted later). 7860 getTargetStreamer().emitDirectiveModuleHardFloat(); 7861 7862 // If this is not the end of the statement, report an error. 7863 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7864 reportParseError("unexpected token, expected end of statement"); 7865 return false; 7866 } 7867 7868 return false; // parseDirectiveModule has finished successfully. 7869 } else if (Option == "mt") { 7870 setModuleFeatureBits(Mips::FeatureMT, "mt"); 7871 7872 // Synchronize the ABI Flags information with the FeatureBits information we 7873 // updated above. 7874 getTargetStreamer().updateABIInfo(*this); 7875 7876 // If printing assembly, use the recently updated ABI Flags information. 7877 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 7878 // emitted later). 7879 getTargetStreamer().emitDirectiveModuleMT(); 7880 7881 // If this is not the end of the statement, report an error. 7882 if (getLexer().isNot(AsmToken::EndOfStatement)) { 7883 reportParseError("unexpected token, expected end of statement"); 7884 return false; 7885 } 7886 7887 return false; // parseDirectiveModule has finished successfully. 7888 } else if (Option == "crc") { 7889 setModuleFeatureBits(Mips::FeatureCRC, "crc"); 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().emitDirectiveModuleCRC(); 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 == "nocrc") { 7908 clearModuleFeatureBits(Mips::FeatureCRC, "crc"); 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().emitDirectiveModuleNoCRC(); 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 == "virt") { 7927 setModuleFeatureBits(Mips::FeatureVirt, "virt"); 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().emitDirectiveModuleVirt(); 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 == "novirt") { 7946 clearModuleFeatureBits(Mips::FeatureVirt, "virt"); 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().emitDirectiveModuleNoVirt(); 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 == "ginv") { 7965 setModuleFeatureBits(Mips::FeatureGINV, "ginv"); 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().emitDirectiveModuleGINV(); 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 == "noginv") { 7984 clearModuleFeatureBits(Mips::FeatureGINV, "ginv"); 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().emitDirectiveModuleNoGINV(); 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 { 8003 return Error(L, "'" + Twine(Option) + "' is not a valid .module option."); 8004 } 8005 } 8006 8007 /// parseDirectiveModuleFP 8008 /// ::= =32 8009 /// ::= =xx 8010 /// ::= =64 8011 bool MipsAsmParser::parseDirectiveModuleFP() { 8012 MCAsmParser &Parser = getParser(); 8013 MCAsmLexer &Lexer = getLexer(); 8014 8015 if (Lexer.isNot(AsmToken::Equal)) { 8016 reportParseError("unexpected token, expected equals sign '='"); 8017 return false; 8018 } 8019 Parser.Lex(); // Eat '=' token. 8020 8021 MipsABIFlagsSection::FpABIKind FpABI; 8022 if (!parseFpABIValue(FpABI, ".module")) 8023 return false; 8024 8025 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8026 reportParseError("unexpected token, expected end of statement"); 8027 return false; 8028 } 8029 8030 // Synchronize the abiflags information with the FeatureBits information we 8031 // changed above. 8032 getTargetStreamer().updateABIInfo(*this); 8033 8034 // If printing assembly, use the recently updated abiflags information. 8035 // If generating ELF, don't do anything (the .MIPS.abiflags section gets 8036 // emitted at the end). 8037 getTargetStreamer().emitDirectiveModuleFP(); 8038 8039 Parser.Lex(); // Consume the EndOfStatement. 8040 return false; 8041 } 8042 8043 bool MipsAsmParser::parseFpABIValue(MipsABIFlagsSection::FpABIKind &FpABI, 8044 StringRef Directive) { 8045 MCAsmParser &Parser = getParser(); 8046 MCAsmLexer &Lexer = getLexer(); 8047 bool ModuleLevelOptions = Directive == ".module"; 8048 8049 if (Lexer.is(AsmToken::Identifier)) { 8050 StringRef Value = Parser.getTok().getString(); 8051 Parser.Lex(); 8052 8053 if (Value != "xx") { 8054 reportParseError("unsupported value, expected 'xx', '32' or '64'"); 8055 return false; 8056 } 8057 8058 if (!isABI_O32()) { 8059 reportParseError("'" + Directive + " fp=xx' requires the O32 ABI"); 8060 return false; 8061 } 8062 8063 FpABI = MipsABIFlagsSection::FpABIKind::XX; 8064 if (ModuleLevelOptions) { 8065 setModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 8066 clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8067 } else { 8068 setFeatureBits(Mips::FeatureFPXX, "fpxx"); 8069 clearFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8070 } 8071 return true; 8072 } 8073 8074 if (Lexer.is(AsmToken::Integer)) { 8075 unsigned Value = Parser.getTok().getIntVal(); 8076 Parser.Lex(); 8077 8078 if (Value != 32 && Value != 64) { 8079 reportParseError("unsupported value, expected 'xx', '32' or '64'"); 8080 return false; 8081 } 8082 8083 if (Value == 32) { 8084 if (!isABI_O32()) { 8085 reportParseError("'" + Directive + " fp=32' requires the O32 ABI"); 8086 return false; 8087 } 8088 8089 FpABI = MipsABIFlagsSection::FpABIKind::S32; 8090 if (ModuleLevelOptions) { 8091 clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 8092 clearModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8093 } else { 8094 clearFeatureBits(Mips::FeatureFPXX, "fpxx"); 8095 clearFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8096 } 8097 } else { 8098 FpABI = MipsABIFlagsSection::FpABIKind::S64; 8099 if (ModuleLevelOptions) { 8100 clearModuleFeatureBits(Mips::FeatureFPXX, "fpxx"); 8101 setModuleFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8102 } else { 8103 clearFeatureBits(Mips::FeatureFPXX, "fpxx"); 8104 setFeatureBits(Mips::FeatureFP64Bit, "fp64"); 8105 } 8106 } 8107 8108 return true; 8109 } 8110 8111 return false; 8112 } 8113 8114 bool MipsAsmParser::ParseDirective(AsmToken DirectiveID) { 8115 // This returns false if this function recognizes the directive 8116 // regardless of whether it is successfully handles or reports an 8117 // error. Otherwise it returns true to give the generic parser a 8118 // chance at recognizing it. 8119 8120 MCAsmParser &Parser = getParser(); 8121 StringRef IDVal = DirectiveID.getString(); 8122 8123 if (IDVal == ".cpload") { 8124 parseDirectiveCpLoad(DirectiveID.getLoc()); 8125 return false; 8126 } 8127 if (IDVal == ".cprestore") { 8128 parseDirectiveCpRestore(DirectiveID.getLoc()); 8129 return false; 8130 } 8131 if (IDVal == ".cplocal") { 8132 parseDirectiveCpLocal(DirectiveID.getLoc()); 8133 return false; 8134 } 8135 if (IDVal == ".ent") { 8136 StringRef SymbolName; 8137 8138 if (Parser.parseIdentifier(SymbolName)) { 8139 reportParseError("expected identifier after .ent"); 8140 return false; 8141 } 8142 8143 // There's an undocumented extension that allows an integer to 8144 // follow the name of the procedure which AFAICS is ignored by GAS. 8145 // Example: .ent foo,2 8146 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8147 if (getLexer().isNot(AsmToken::Comma)) { 8148 // Even though we accept this undocumented extension for compatibility 8149 // reasons, the additional integer argument does not actually change 8150 // the behaviour of the '.ent' directive, so we would like to discourage 8151 // its use. We do this by not referring to the extended version in 8152 // error messages which are not directly related to its use. 8153 reportParseError("unexpected token, expected end of statement"); 8154 return false; 8155 } 8156 Parser.Lex(); // Eat the comma. 8157 const MCExpr *DummyNumber; 8158 int64_t DummyNumberVal; 8159 // If the user was explicitly trying to use the extended version, 8160 // we still give helpful extension-related error messages. 8161 if (Parser.parseExpression(DummyNumber)) { 8162 reportParseError("expected number after comma"); 8163 return false; 8164 } 8165 if (!DummyNumber->evaluateAsAbsolute(DummyNumberVal)) { 8166 reportParseError("expected an absolute expression after comma"); 8167 return false; 8168 } 8169 } 8170 8171 // If this is not the end of the statement, report an error. 8172 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8173 reportParseError("unexpected token, expected end of statement"); 8174 return false; 8175 } 8176 8177 MCSymbol *Sym = getContext().getOrCreateSymbol(SymbolName); 8178 8179 getTargetStreamer().emitDirectiveEnt(*Sym); 8180 CurrentFn = Sym; 8181 IsCpRestoreSet = false; 8182 return false; 8183 } 8184 8185 if (IDVal == ".end") { 8186 StringRef SymbolName; 8187 8188 if (Parser.parseIdentifier(SymbolName)) { 8189 reportParseError("expected identifier after .end"); 8190 return false; 8191 } 8192 8193 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8194 reportParseError("unexpected token, expected end of statement"); 8195 return false; 8196 } 8197 8198 if (CurrentFn == nullptr) { 8199 reportParseError(".end used without .ent"); 8200 return false; 8201 } 8202 8203 if ((SymbolName != CurrentFn->getName())) { 8204 reportParseError(".end symbol does not match .ent symbol"); 8205 return false; 8206 } 8207 8208 getTargetStreamer().emitDirectiveEnd(SymbolName); 8209 CurrentFn = nullptr; 8210 IsCpRestoreSet = false; 8211 return false; 8212 } 8213 8214 if (IDVal == ".frame") { 8215 // .frame $stack_reg, frame_size_in_bytes, $return_reg 8216 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 1> TmpReg; 8217 OperandMatchResultTy ResTy = parseAnyRegister(TmpReg); 8218 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 8219 reportParseError("expected stack register"); 8220 return false; 8221 } 8222 8223 MipsOperand &StackRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 8224 if (!StackRegOpnd.isGPRAsmReg()) { 8225 reportParseError(StackRegOpnd.getStartLoc(), 8226 "expected general purpose register"); 8227 return false; 8228 } 8229 unsigned StackReg = StackRegOpnd.getGPR32Reg(); 8230 8231 if (Parser.getTok().is(AsmToken::Comma)) 8232 Parser.Lex(); 8233 else { 8234 reportParseError("unexpected token, expected comma"); 8235 return false; 8236 } 8237 8238 // Parse the frame size. 8239 const MCExpr *FrameSize; 8240 int64_t FrameSizeVal; 8241 8242 if (Parser.parseExpression(FrameSize)) { 8243 reportParseError("expected frame size value"); 8244 return false; 8245 } 8246 8247 if (!FrameSize->evaluateAsAbsolute(FrameSizeVal)) { 8248 reportParseError("frame size not an absolute expression"); 8249 return false; 8250 } 8251 8252 if (Parser.getTok().is(AsmToken::Comma)) 8253 Parser.Lex(); 8254 else { 8255 reportParseError("unexpected token, expected comma"); 8256 return false; 8257 } 8258 8259 // Parse the return register. 8260 TmpReg.clear(); 8261 ResTy = parseAnyRegister(TmpReg); 8262 if (ResTy == MatchOperand_NoMatch || ResTy == MatchOperand_ParseFail) { 8263 reportParseError("expected return register"); 8264 return false; 8265 } 8266 8267 MipsOperand &ReturnRegOpnd = static_cast<MipsOperand &>(*TmpReg[0]); 8268 if (!ReturnRegOpnd.isGPRAsmReg()) { 8269 reportParseError(ReturnRegOpnd.getStartLoc(), 8270 "expected general purpose register"); 8271 return false; 8272 } 8273 8274 // If this is not the end of the statement, report an error. 8275 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8276 reportParseError("unexpected token, expected end of statement"); 8277 return false; 8278 } 8279 8280 getTargetStreamer().emitFrame(StackReg, FrameSizeVal, 8281 ReturnRegOpnd.getGPR32Reg()); 8282 IsCpRestoreSet = false; 8283 return false; 8284 } 8285 8286 if (IDVal == ".set") { 8287 parseDirectiveSet(); 8288 return false; 8289 } 8290 8291 if (IDVal == ".mask" || IDVal == ".fmask") { 8292 // .mask bitmask, frame_offset 8293 // bitmask: One bit for each register used. 8294 // frame_offset: Offset from Canonical Frame Address ($sp on entry) where 8295 // first register is expected to be saved. 8296 // Examples: 8297 // .mask 0x80000000, -4 8298 // .fmask 0x80000000, -4 8299 // 8300 8301 // Parse the bitmask 8302 const MCExpr *BitMask; 8303 int64_t BitMaskVal; 8304 8305 if (Parser.parseExpression(BitMask)) { 8306 reportParseError("expected bitmask value"); 8307 return false; 8308 } 8309 8310 if (!BitMask->evaluateAsAbsolute(BitMaskVal)) { 8311 reportParseError("bitmask not an absolute expression"); 8312 return false; 8313 } 8314 8315 if (Parser.getTok().is(AsmToken::Comma)) 8316 Parser.Lex(); 8317 else { 8318 reportParseError("unexpected token, expected comma"); 8319 return false; 8320 } 8321 8322 // Parse the frame_offset 8323 const MCExpr *FrameOffset; 8324 int64_t FrameOffsetVal; 8325 8326 if (Parser.parseExpression(FrameOffset)) { 8327 reportParseError("expected frame offset value"); 8328 return false; 8329 } 8330 8331 if (!FrameOffset->evaluateAsAbsolute(FrameOffsetVal)) { 8332 reportParseError("frame offset not an absolute expression"); 8333 return false; 8334 } 8335 8336 // If this is not the end of the statement, report an error. 8337 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8338 reportParseError("unexpected token, expected end of statement"); 8339 return false; 8340 } 8341 8342 if (IDVal == ".mask") 8343 getTargetStreamer().emitMask(BitMaskVal, FrameOffsetVal); 8344 else 8345 getTargetStreamer().emitFMask(BitMaskVal, FrameOffsetVal); 8346 return false; 8347 } 8348 8349 if (IDVal == ".nan") 8350 return parseDirectiveNaN(); 8351 8352 if (IDVal == ".gpword") { 8353 parseDirectiveGpWord(); 8354 return false; 8355 } 8356 8357 if (IDVal == ".gpdword") { 8358 parseDirectiveGpDWord(); 8359 return false; 8360 } 8361 8362 if (IDVal == ".dtprelword") { 8363 parseDirectiveDtpRelWord(); 8364 return false; 8365 } 8366 8367 if (IDVal == ".dtpreldword") { 8368 parseDirectiveDtpRelDWord(); 8369 return false; 8370 } 8371 8372 if (IDVal == ".tprelword") { 8373 parseDirectiveTpRelWord(); 8374 return false; 8375 } 8376 8377 if (IDVal == ".tpreldword") { 8378 parseDirectiveTpRelDWord(); 8379 return false; 8380 } 8381 8382 if (IDVal == ".option") { 8383 parseDirectiveOption(); 8384 return false; 8385 } 8386 8387 if (IDVal == ".abicalls") { 8388 getTargetStreamer().emitDirectiveAbiCalls(); 8389 if (Parser.getTok().isNot(AsmToken::EndOfStatement)) { 8390 Error(Parser.getTok().getLoc(), 8391 "unexpected token, expected end of statement"); 8392 } 8393 return false; 8394 } 8395 8396 if (IDVal == ".cpsetup") { 8397 parseDirectiveCPSetup(); 8398 return false; 8399 } 8400 if (IDVal == ".cpreturn") { 8401 parseDirectiveCPReturn(); 8402 return false; 8403 } 8404 if (IDVal == ".module") { 8405 parseDirectiveModule(); 8406 return false; 8407 } 8408 if (IDVal == ".llvm_internal_mips_reallow_module_directive") { 8409 parseInternalDirectiveReallowModule(); 8410 return false; 8411 } 8412 if (IDVal == ".insn") { 8413 parseInsnDirective(); 8414 return false; 8415 } 8416 if (IDVal == ".rdata") { 8417 parseRSectionDirective(".rodata"); 8418 return false; 8419 } 8420 if (IDVal == ".sbss") { 8421 parseSSectionDirective(IDVal, ELF::SHT_NOBITS); 8422 return false; 8423 } 8424 if (IDVal == ".sdata") { 8425 parseSSectionDirective(IDVal, ELF::SHT_PROGBITS); 8426 return false; 8427 } 8428 8429 return true; 8430 } 8431 8432 bool MipsAsmParser::parseInternalDirectiveReallowModule() { 8433 // If this is not the end of the statement, report an error. 8434 if (getLexer().isNot(AsmToken::EndOfStatement)) { 8435 reportParseError("unexpected token, expected end of statement"); 8436 return false; 8437 } 8438 8439 getTargetStreamer().reallowModuleDirective(); 8440 8441 getParser().Lex(); // Eat EndOfStatement token. 8442 return false; 8443 } 8444 8445 extern "C" void LLVMInitializeMipsAsmParser() { 8446 RegisterMCAsmParser<MipsAsmParser> X(getTheMipsTarget()); 8447 RegisterMCAsmParser<MipsAsmParser> Y(getTheMipselTarget()); 8448 RegisterMCAsmParser<MipsAsmParser> A(getTheMips64Target()); 8449 RegisterMCAsmParser<MipsAsmParser> B(getTheMips64elTarget()); 8450 } 8451 8452 #define GET_REGISTER_MATCHER 8453 #define GET_MATCHER_IMPLEMENTATION 8454 #define GET_MNEMONIC_SPELL_CHECKER 8455 #include "MipsGenAsmMatcher.inc" 8456 8457 bool MipsAsmParser::mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) { 8458 // Find the appropriate table for this asm variant. 8459 const MatchEntry *Start, *End; 8460 switch (VariantID) { 8461 default: llvm_unreachable("invalid variant!"); 8462 case 0: Start = std::begin(MatchTable0); End = std::end(MatchTable0); break; 8463 } 8464 // Search the table. 8465 auto MnemonicRange = std::equal_range(Start, End, Mnemonic, LessOpcode()); 8466 return MnemonicRange.first != MnemonicRange.second; 8467 } 8468