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