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