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