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