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