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