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