LLVM 24.0.0git
SparcAsmParser.cpp
Go to the documentation of this file.
1//===-- SparcAsmParser.cpp - Parse Sparc 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
14#include "llvm/ADT/StringRef.h"
16#include "llvm/MC/MCAsmMacro.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
28#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
36#include "llvm/Support/SMLoc.h"
39#include <algorithm>
40#include <cassert>
41#include <cstdint>
42#include <memory>
43#include <string>
44
45using namespace llvm;
46
47// The generated AsmMatcher SparcGenAsmMatcher uses "Sparc" as the target
48// namespace. But SPARC backend uses "SP" as its namespace.
49namespace llvm {
50namespace Sparc {
51
52 using namespace SP;
53
54} // end namespace Sparc
55} // end namespace llvm
56
57namespace {
58
59class SparcOperand;
60
61class SparcAsmParser : public MCTargetAsmParser {
62 MCAsmParser &Parser;
63 const MCRegisterInfo &MRI;
64
65 enum class TailRelocKind { Load_GOT, Add_TLS, Load_TLS, Call_TLS };
66
67 /// @name Auto-generated Match Functions
68 /// {
69
70#define GET_ASSEMBLER_HEADER
71#include "SparcGenAsmMatcher.inc"
72
73 /// }
74
75 // public interface of the MCTargetAsmParser.
76 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
79 bool MatchingInlineAsm) override;
80 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
81 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
82 SMLoc &EndLoc) override;
83 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
84 SMLoc NameLoc, OperandVector &Operands) override;
85 ParseStatus parseDirective(AsmToken DirectiveID) override;
86 bool parseDirectiveRegister();
87 bool parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E);
88 bool parseDataExpr(const MCExpr *&Res) override;
89
90 // Custom parse functions for Sparc specific operands.
91 ParseStatus parseMEMOperand(OperandVector &Operands);
92
93 ParseStatus parseMembarTag(OperandVector &Operands);
94
96
97 ParseStatus parsePrefetchTag(OperandVector &Operands);
98
99 template <TailRelocKind Kind>
100 ParseStatus parseTailRelocSym(OperandVector &Operands);
101
102 template <unsigned N> ParseStatus parseShiftAmtImm(OperandVector &Operands);
103
104 ParseStatus parseCallTarget(OperandVector &Operands);
105
106 ParseStatus parseOperand(OperandVector &Operands, StringRef Name);
107
108 ParseStatus parseSparcAsmOperand(std::unique_ptr<SparcOperand> &Operand);
109
110 ParseStatus parseBranchModifiers(OperandVector &Operands);
111
112 ParseStatus parseExpression(int64_t &Val);
113
114 // Helper function for dealing with %lo / %hi in PIC mode.
115 const MCSpecifierExpr *adjustPICRelocation(uint16_t VK,
116 const MCExpr *subExpr);
117
118 // Helper function to see if current token can start an expression.
119 bool isPossibleExpression(const AsmToken &Token);
120
121 // Check if mnemonic is valid.
122 MatchResultTy mnemonicIsValid(StringRef Mnemonic, unsigned VariantID);
123 bool mnemonicTakesBranchModifiers(StringRef Mnemonic);
124
125 // returns true if Tok is matched to a register and returns register in RegNo.
126 MCRegister matchRegisterName(const AsmToken &Tok, unsigned &RegKind);
127
128 ParseStatus matchSparcAsmModifiers(const MCExpr *&EVal, SMLoc &EndLoc);
129
130 bool is64Bit() const { return getSTI().getTargetTriple().isSPARC64(); }
131 bool isV9() const { return getSTI().hasFeature(Sparc::FeatureV9); }
132
133 bool expandSET(MCInst &Inst, SMLoc IDLoc,
134 SmallVectorImpl<MCInst> &Instructions);
135
136 bool expandSETSW(MCInst &Inst, SMLoc IDLoc,
137 SmallVectorImpl<MCInst> &Instructions);
138
139 bool expandSETX(MCInst &Inst, SMLoc IDLoc,
140 SmallVectorImpl<MCInst> &Instructions);
141
142 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
143
144public:
145 SparcAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
146 const MCInstrInfo &MII)
147 : MCTargetAsmParser(sti, MII), Parser(parser),
148 MRI(*Parser.getContext().getRegisterInfo()) {
149 Parser.addAliasForDirective(".half", ".2byte");
150 Parser.addAliasForDirective(".uahalf", ".2byte");
151 Parser.addAliasForDirective(".word", ".4byte");
152 Parser.addAliasForDirective(".uaword", ".4byte");
153 Parser.addAliasForDirective(".nword", is64Bit() ? ".8byte" : ".4byte");
154 if (is64Bit())
155 Parser.addAliasForDirective(".xword", ".8byte");
156
157 // Initialize the set of available features.
158 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
159 }
160};
161
162} // end anonymous namespace
163
164 static const MCPhysReg IntRegs[32] = {
165 Sparc::G0, Sparc::G1, Sparc::G2, Sparc::G3,
166 Sparc::G4, Sparc::G5, Sparc::G6, Sparc::G7,
167 Sparc::O0, Sparc::O1, Sparc::O2, Sparc::O3,
168 Sparc::O4, Sparc::O5, Sparc::O6, Sparc::O7,
169 Sparc::L0, Sparc::L1, Sparc::L2, Sparc::L3,
170 Sparc::L4, Sparc::L5, Sparc::L6, Sparc::L7,
171 Sparc::I0, Sparc::I1, Sparc::I2, Sparc::I3,
172 Sparc::I4, Sparc::I5, Sparc::I6, Sparc::I7 };
173
174namespace {
175
176/// SparcOperand - Instances of this class represent a parsed Sparc machine
177/// instruction.
178class SparcOperand : public MCParsedAsmOperand {
179public:
180 enum RegisterKind {
181 rk_None,
182 rk_IntReg,
183 rk_Special,
184 };
185
186private:
187 enum KindTy {
188 k_Token,
189 k_Register,
190 k_Immediate,
191 k_MemoryReg,
192 k_MemoryImm,
193 k_ASITag,
194 k_PrefetchTag,
195 k_TailRelocSym, // Special kind of immediate for TLS relocation purposes.
196 } Kind;
197
198 SMLoc StartLoc, EndLoc;
199
200 struct Token {
201 const char *Data;
202 unsigned Length;
203 };
204
205 struct RegOp {
206 MCRegister Reg;
207 RegisterKind Kind;
208 };
209
210 struct ImmOp {
211 const MCExpr *Val;
212 };
213
214 struct MemOp {
215 MCRegister Base;
216 MCRegister OffsetReg;
217 const MCExpr *Off;
218 };
219
220 union {
221 struct Token Tok;
222 struct RegOp Reg;
223 struct ImmOp Imm;
224 struct MemOp Mem;
225 unsigned ASI;
226 unsigned Prefetch;
227 };
228
229public:
230 SparcOperand(KindTy K) : Kind(K) {}
231
232 bool isToken() const override { return Kind == k_Token; }
233 bool isReg() const override { return Kind == k_Register; }
234 bool isImm() const override { return Kind == k_Immediate; }
235 bool isMem() const override { return isMEMrr() || isMEMri(); }
236 bool isMEMrr() const { return Kind == k_MemoryReg; }
237 bool isMEMri() const { return Kind == k_MemoryImm; }
238 bool isMembarTag() const { return Kind == k_Immediate; }
239 bool isASITag() const { return Kind == k_ASITag; }
240 bool isPrefetchTag() const { return Kind == k_PrefetchTag; }
241 bool isTailRelocSym() const { return Kind == k_TailRelocSym; }
242
243 bool isCallTarget() const {
244 if (!isImm())
245 return false;
246
247 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val))
248 return CE->getValue() % 4 == 0;
249
250 return true;
251 }
252
253 bool isShiftAmtImm5() const {
254 if (!isImm())
255 return false;
256
257 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val))
258 return isUInt<5>(CE->getValue());
259
260 return false;
261 }
262
263 bool isShiftAmtImm6() const {
264 if (!isImm())
265 return false;
266
267 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Imm.Val))
268 return isUInt<6>(CE->getValue());
269
270 return false;
271 }
272
273 bool isIntReg() const {
274 return (Kind == k_Register && Reg.Kind == rk_IntReg);
275 }
276
277 StringRef getToken() const {
278 assert(Kind == k_Token && "Invalid access!");
279 return StringRef(Tok.Data, Tok.Length);
280 }
281
282 MCRegister getReg() const override {
283 assert((Kind == k_Register) && "Invalid access!");
284 return Reg.Reg;
285 }
286
287 const MCExpr *getImm() const {
288 assert((Kind == k_Immediate) && "Invalid access!");
289 return Imm.Val;
290 }
291
292 MCRegister getMemBase() const {
293 assert((Kind == k_MemoryReg || Kind == k_MemoryImm) && "Invalid access!");
294 return Mem.Base;
295 }
296
297 MCRegister getMemOffsetReg() const {
298 assert((Kind == k_MemoryReg) && "Invalid access!");
299 return Mem.OffsetReg;
300 }
301
302 const MCExpr *getMemOff() const {
303 assert((Kind == k_MemoryImm) && "Invalid access!");
304 return Mem.Off;
305 }
306
307 unsigned getASITag() const {
308 assert((Kind == k_ASITag) && "Invalid access!");
309 return ASI;
310 }
311
312 unsigned getPrefetchTag() const {
313 assert((Kind == k_PrefetchTag) && "Invalid access!");
314 return Prefetch;
315 }
316
317 const MCExpr *getTailRelocSym() const {
318 assert((Kind == k_TailRelocSym) && "Invalid access!");
319 return Imm.Val;
320 }
321
322 /// getStartLoc - Get the location of the first token of this operand.
323 SMLoc getStartLoc() const override {
324 return StartLoc;
325 }
326 /// getEndLoc - Get the location of the last token of this operand.
327 SMLoc getEndLoc() const override {
328 return EndLoc;
329 }
330
331 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
332 switch (Kind) {
333 case k_Token: OS << "Token: " << getToken() << "\n"; break;
334 case k_Register:
335 OS << "Reg: #" << getReg().id() << "\n";
336 break;
337 case k_Immediate: OS << "Imm: " << getImm() << "\n"; break;
338 case k_MemoryReg:
339 OS << "Mem: " << getMemBase().id() << "+" << getMemOffsetReg().id()
340 << "\n";
341 break;
342 case k_MemoryImm: assert(getMemOff() != nullptr);
343 OS << "Mem: " << getMemBase().id() << "+";
344 MAI.printExpr(OS, *getMemOff());
345 OS << "\n";
346 break;
347 case k_ASITag:
348 OS << "ASI tag: " << getASITag() << "\n";
349 break;
350 case k_PrefetchTag:
351 OS << "Prefetch tag: " << getPrefetchTag() << "\n";
352 break;
353 case k_TailRelocSym:
354 OS << "TailReloc: " << getTailRelocSym() << "\n";
355 break;
356 }
357 }
358
359 void addRegOperands(MCInst &Inst, unsigned N) const {
360 assert(N == 1 && "Invalid number of operands!");
362 }
363
364 void addImmOperands(MCInst &Inst, unsigned N) const {
365 assert(N == 1 && "Invalid number of operands!");
366 const MCExpr *Expr = getImm();
367 addExpr(Inst, Expr);
368 }
369
370 void addShiftAmtImm5Operands(MCInst &Inst, unsigned N) const {
371 assert(N == 1 && "Invalid number of operands!");
372 addExpr(Inst, getImm());
373 }
374 void addShiftAmtImm6Operands(MCInst &Inst, unsigned N) const {
375 assert(N == 1 && "Invalid number of operands!");
376 addExpr(Inst, getImm());
377 }
378
379 void addExpr(MCInst &Inst, const MCExpr *Expr) const{
380 // Add as immediate when possible. Null MCExpr = 0.
381 if (!Expr)
383 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
384 Inst.addOperand(MCOperand::createImm(CE->getValue()));
385 else
387 }
388
389 void addMEMrrOperands(MCInst &Inst, unsigned N) const {
390 assert(N == 2 && "Invalid number of operands!");
391
392 Inst.addOperand(MCOperand::createReg(getMemBase()));
393
394 assert(getMemOffsetReg().isValid() && "Invalid offset");
395 Inst.addOperand(MCOperand::createReg(getMemOffsetReg()));
396 }
397
398 void addMEMriOperands(MCInst &Inst, unsigned N) const {
399 assert(N == 2 && "Invalid number of operands!");
400
401 Inst.addOperand(MCOperand::createReg(getMemBase()));
402
403 const MCExpr *Expr = getMemOff();
404 addExpr(Inst, Expr);
405 }
406
407 void addASITagOperands(MCInst &Inst, unsigned N) const {
408 assert(N == 1 && "Invalid number of operands!");
409 Inst.addOperand(MCOperand::createImm(getASITag()));
410 }
411
412 void addPrefetchTagOperands(MCInst &Inst, unsigned N) const {
413 assert(N == 1 && "Invalid number of operands!");
414 Inst.addOperand(MCOperand::createImm(getPrefetchTag()));
415 }
416
417 void addMembarTagOperands(MCInst &Inst, unsigned N) const {
418 assert(N == 1 && "Invalid number of operands!");
419 const MCExpr *Expr = getImm();
420 addExpr(Inst, Expr);
421 }
422
423 void addCallTargetOperands(MCInst &Inst, unsigned N) const {
424 assert(N == 1 && "Invalid number of operands!");
425 addExpr(Inst, getImm());
426 }
427
428 void addTailRelocSymOperands(MCInst &Inst, unsigned N) const {
429 assert(N == 1 && "Invalid number of operands!");
430 addExpr(Inst, getTailRelocSym());
431 }
432
433 static std::unique_ptr<SparcOperand> CreateToken(StringRef Str, SMLoc S) {
434 auto Op = std::make_unique<SparcOperand>(k_Token);
435 Op->Tok.Data = Str.data();
436 Op->Tok.Length = Str.size();
437 Op->StartLoc = S;
438 Op->EndLoc = S;
439 return Op;
440 }
441
442 static std::unique_ptr<SparcOperand> CreateReg(MCRegister Reg, unsigned Kind,
443 SMLoc S, SMLoc E) {
444 auto Op = std::make_unique<SparcOperand>(k_Register);
445 Op->Reg.Reg = Reg;
446 Op->Reg.Kind = (SparcOperand::RegisterKind)Kind;
447 Op->StartLoc = S;
448 Op->EndLoc = E;
449 return Op;
450 }
451
452 static std::unique_ptr<SparcOperand> CreateImm(const MCExpr *Val, SMLoc S,
453 SMLoc E) {
454 auto Op = std::make_unique<SparcOperand>(k_Immediate);
455 Op->Imm.Val = Val;
456 Op->StartLoc = S;
457 Op->EndLoc = E;
458 return Op;
459 }
460
461 static std::unique_ptr<SparcOperand> CreateASITag(unsigned Val, SMLoc S,
462 SMLoc E) {
463 auto Op = std::make_unique<SparcOperand>(k_ASITag);
464 Op->ASI = Val;
465 Op->StartLoc = S;
466 Op->EndLoc = E;
467 return Op;
468 }
469
470 static std::unique_ptr<SparcOperand> CreatePrefetchTag(unsigned Val, SMLoc S,
471 SMLoc E) {
472 auto Op = std::make_unique<SparcOperand>(k_PrefetchTag);
473 Op->Prefetch = Val;
474 Op->StartLoc = S;
475 Op->EndLoc = E;
476 return Op;
477 }
478
479 static std::unique_ptr<SparcOperand> CreateTailRelocSym(const MCExpr *Val,
480 SMLoc S, SMLoc E) {
481 auto Op = std::make_unique<SparcOperand>(k_TailRelocSym);
482 Op->Imm.Val = Val;
483 Op->StartLoc = S;
484 Op->EndLoc = E;
485 return Op;
486 }
487
488 static std::unique_ptr<SparcOperand>
489 MorphToMEMrr(MCRegister Base, std::unique_ptr<SparcOperand> Op) {
490 MCRegister offsetReg = Op->getReg();
491 Op->Kind = k_MemoryReg;
492 Op->Mem.Base = Base;
493 Op->Mem.OffsetReg = offsetReg;
494 Op->Mem.Off = nullptr;
495 return Op;
496 }
497
498 static std::unique_ptr<SparcOperand> CreateMEMr(MCRegister Base, SMLoc S,
499 SMLoc E) {
500 auto Op = std::make_unique<SparcOperand>(k_MemoryReg);
501 Op->Mem.Base = Base;
502 Op->Mem.OffsetReg = Sparc::G0; // always 0
503 Op->Mem.Off = nullptr;
504 Op->StartLoc = S;
505 Op->EndLoc = E;
506 return Op;
507 }
508
509 static std::unique_ptr<SparcOperand>
510 MorphToMEMri(MCRegister Base, std::unique_ptr<SparcOperand> Op) {
511 const MCExpr *Imm = Op->getImm();
512 Op->Kind = k_MemoryImm;
513 Op->Mem.Base = Base;
514 Op->Mem.OffsetReg = MCRegister();
515 Op->Mem.Off = Imm;
516 return Op;
517 }
518};
519
520} // end anonymous namespace
521
522#define GET_MATCHER_IMPLEMENTATION
523#define GET_REGISTER_MATCHER
524#define GET_MNEMONIC_SPELL_CHECKER
525#include "SparcGenAsmMatcher.inc"
526
527// Use a custom function instead of the one from SparcGenAsmMatcher
528// so we can differentiate between unavailable and unknown instructions.
529SparcAsmParser::MatchResultTy
530SparcAsmParser::mnemonicIsValid(StringRef Mnemonic, unsigned VariantID) {
531 // Process all MnemonicAliases to remap the mnemonic.
532 applyMnemonicAliases(Mnemonic, getAvailableFeatures(), VariantID);
533
534 // Find the appropriate table for this asm variant.
535 const MatchEntry *Start, *End;
536 switch (VariantID) {
537 default:
538 llvm_unreachable("invalid variant!");
539 case 0:
540 Start = std::begin(MatchTable0);
541 End = std::end(MatchTable0);
542 break;
543 }
544
545 // Search the table.
546 auto MnemonicRange = std::equal_range(Start, End, Mnemonic, LessOpcode());
547
548 if (MnemonicRange.first == MnemonicRange.second)
549 return Match_MnemonicFail;
550
551 for (const MatchEntry *it = MnemonicRange.first, *ie = MnemonicRange.second;
552 it != ie; ++it) {
553 const FeatureBitset &RequiredFeatures =
554 FeatureBitsets[it->RequiredFeaturesIdx];
555 if ((getAvailableFeatures() & RequiredFeatures) == RequiredFeatures)
556 return Match_Success;
557 }
558 return Match_MissingFeature;
559}
560
561/// Return true if some form of the instruction takes branch modifiers
562/// (",a", ",pn" or ",pt") right after the mnemonic.
563bool SparcAsmParser::mnemonicTakesBranchModifiers(StringRef Mnemonic) {
564 auto MnemonicRange = std::equal_range(
565 std::begin(MatchTable0), std::end(MatchTable0), Mnemonic, LessOpcode());
566 return llvm::any_of(make_range(MnemonicRange), [](const MatchEntry &E) {
567 return E.Classes[0] == MCK_a || E.Classes[0] == MCK_pn ||
568 E.Classes[0] == MCK_pt;
569 });
570}
571
572bool SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc,
573 SmallVectorImpl<MCInst> &Instructions) {
574 MCOperand MCRegOp = Inst.getOperand(0);
575 MCOperand MCValOp = Inst.getOperand(1);
576 assert(MCRegOp.isReg());
577 assert(MCValOp.isImm() || MCValOp.isExpr());
578
579 // the imm operand can be either an expression or an immediate.
580 bool IsImm = Inst.getOperand(1).isImm();
581 int64_t RawImmValue = IsImm ? MCValOp.getImm() : 0;
582
583 // Allow either a signed or unsigned 32-bit immediate.
584 if (RawImmValue < -2147483648LL || RawImmValue > 4294967295LL) {
585 return Error(IDLoc,
586 "set: argument must be between -2147483648 and 4294967295");
587 }
588
589 // If the value was expressed as a large unsigned number, that's ok.
590 // We want to see if it "looks like" a small signed number.
591 int32_t ImmValue = RawImmValue;
592 // For 'set' you can't use 'or' with a negative operand on V9 because
593 // that would splat the sign bit across the upper half of the destination
594 // register, whereas 'set' is defined to zero the high 32 bits.
595 bool IsEffectivelyImm13 =
596 IsImm && ((is64Bit() ? 0 : -4096) <= ImmValue && ImmValue < 4096);
597 const MCExpr *ValExpr;
598 if (IsImm)
599 ValExpr = MCConstantExpr::create(ImmValue, getContext());
600 else
601 ValExpr = MCValOp.getExpr();
602
603 MCOperand PrevReg = MCOperand::createReg(Sparc::G0);
604
605 // If not just a signed imm13 value, then either we use a 'sethi' with a
606 // following 'or', or a 'sethi' by itself if there are no more 1 bits.
607 // In either case, start with the 'sethi'.
608 if (!IsEffectivelyImm13) {
609 MCInst TmpInst;
610 const MCExpr *Expr = adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr);
611 TmpInst.setLoc(IDLoc);
612 TmpInst.setOpcode(SP::SETHIi);
613 TmpInst.addOperand(MCRegOp);
614 TmpInst.addOperand(MCOperand::createExpr(Expr));
615 Instructions.push_back(TmpInst);
616 PrevReg = MCRegOp;
617 }
618
619 // The low bits require touching in 3 cases:
620 // * A non-immediate value will always require both instructions.
621 // * An effectively imm13 value needs only an 'or' instruction.
622 // * Otherwise, an immediate that is not effectively imm13 requires the
623 // 'or' only if bits remain after clearing the 22 bits that 'sethi' set.
624 // If the low bits are known zeros, there's nothing to do.
625 // In the second case, and only in that case, must we NOT clear
626 // bits of the immediate value via the %lo() assembler function.
627 // Note also, the 'or' instruction doesn't mind a large value in the case
628 // where the operand to 'set' was 0xFFFFFzzz - it does exactly what you mean.
629 if (!IsImm || IsEffectivelyImm13 || (ImmValue & 0x3ff)) {
630 MCInst TmpInst;
631 const MCExpr *Expr;
632 if (IsEffectivelyImm13)
633 Expr = ValExpr;
634 else
635 Expr = adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr);
636 TmpInst.setLoc(IDLoc);
637 TmpInst.setOpcode(SP::ORri);
638 TmpInst.addOperand(MCRegOp);
639 TmpInst.addOperand(PrevReg);
640 TmpInst.addOperand(MCOperand::createExpr(Expr));
641 Instructions.push_back(TmpInst);
642 }
643 return false;
644}
645
646bool SparcAsmParser::expandSETSW(MCInst &Inst, SMLoc IDLoc,
647 SmallVectorImpl<MCInst> &Instructions) {
648 MCOperand MCRegOp = Inst.getOperand(0);
649 MCOperand MCValOp = Inst.getOperand(1);
650 assert(MCRegOp.isReg());
651 assert(MCValOp.isImm() || MCValOp.isExpr());
652
653 // The imm operand can be either an expression or an immediate.
654 bool IsImm = Inst.getOperand(1).isImm();
655 int64_t ImmValue = IsImm ? MCValOp.getImm() : 0;
656 const MCExpr *ValExpr = IsImm ? MCConstantExpr::create(ImmValue, getContext())
657 : MCValOp.getExpr();
658
659 bool IsSmallImm = IsImm && isInt<13>(ImmValue);
660 bool NoLowBitsImm = IsImm && ((ImmValue & 0x3FF) == 0);
661
662 MCOperand PrevReg = MCOperand::createReg(Sparc::G0);
663
664 if (!isInt<32>(ImmValue)) {
665 return Error(IDLoc,
666 "set: argument must be between -2147483648 and 2147483647");
667 }
668
669 // Very small immediates can be expressed without emitting a sethi.
670 if (!IsSmallImm) {
671 // sethi %hi(val), rd
672 Instructions.push_back(
673 MCInstBuilder(SP::SETHIi)
674 .addReg(MCRegOp.getReg())
675 .addExpr(adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr)));
676
677 PrevReg = MCRegOp;
678 }
679
680 // If the immediate has the lower bits set or is small, we need to emit an or.
681 if (!NoLowBitsImm || IsSmallImm) {
682 const MCExpr *Expr =
683 IsSmallImm ? ValExpr : adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr);
684
685 // or rd, %lo(val), rd
686 Instructions.push_back(MCInstBuilder(SP::ORri)
687 .addReg(MCRegOp.getReg())
688 .addReg(PrevReg.getReg())
689 .addExpr(Expr));
690
691 // If it's a small immediate there's nothing more to do.
692 if (IsSmallImm)
693 return false;
694 }
695
696 // Large negative or non-immediate expressions would need an sra.
697 if (!IsImm || ImmValue < 0) {
698 // sra rd, %g0, rd
699 Instructions.push_back(MCInstBuilder(SP::SRArr)
700 .addReg(MCRegOp.getReg())
701 .addReg(MCRegOp.getReg())
702 .addReg(Sparc::G0));
703 }
704
705 return false;
706}
707
708bool SparcAsmParser::expandSETX(MCInst &Inst, SMLoc IDLoc,
709 SmallVectorImpl<MCInst> &Instructions) {
710 MCOperand MCRegOp = Inst.getOperand(0);
711 MCOperand MCValOp = Inst.getOperand(1);
712 MCOperand MCTmpOp = Inst.getOperand(2);
713 assert(MCRegOp.isReg() && MCTmpOp.isReg());
714 assert(MCValOp.isImm() || MCValOp.isExpr());
715
716 // the imm operand can be either an expression or an immediate.
717 bool IsImm = MCValOp.isImm();
718 int64_t ImmValue = IsImm ? MCValOp.getImm() : 0;
719
720 const MCExpr *ValExpr = IsImm ? MCConstantExpr::create(ImmValue, getContext())
721 : MCValOp.getExpr();
722
723 // Very small immediates can be expressed directly as a single `or`.
724 if (IsImm && isInt<13>(ImmValue)) {
725 // or rd, val, rd
726 Instructions.push_back(MCInstBuilder(SP::ORri)
727 .addReg(MCRegOp.getReg())
728 .addReg(Sparc::G0)
729 .addExpr(ValExpr));
730 return false;
731 }
732
733 // Otherwise, first we set the lower half of the register.
734
735 // sethi %hi(val), rd
736 Instructions.push_back(
737 MCInstBuilder(SP::SETHIi)
738 .addReg(MCRegOp.getReg())
739 .addExpr(adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr)));
740 // or rd, %lo(val), rd
741 Instructions.push_back(
742 MCInstBuilder(SP::ORri)
743 .addReg(MCRegOp.getReg())
744 .addReg(MCRegOp.getReg())
745 .addExpr(adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr)));
746
747 // Small positive immediates can be expressed as a single `sethi`+`or`
748 // combination, so we can just return here.
749 if (IsImm && isUInt<32>(ImmValue))
750 return false;
751
752 // For bigger immediates, we need to generate the upper half, then shift and
753 // merge it with the lower half that has just been generated above.
754
755 // sethi %hh(val), tmp
756 Instructions.push_back(MCInstBuilder(SP::SETHIi)
757 .addReg(MCTmpOp.getReg())
759 ValExpr, ELF::R_SPARC_HH22, getContext())));
760 // or tmp, %hm(val), tmp
761 Instructions.push_back(MCInstBuilder(SP::ORri)
762 .addReg(MCTmpOp.getReg())
763 .addReg(MCTmpOp.getReg())
765 ValExpr, ELF::R_SPARC_HM10, getContext())));
766 // sllx tmp, 32, tmp
767 Instructions.push_back(MCInstBuilder(SP::SLLXri)
768 .addReg(MCTmpOp.getReg())
769 .addReg(MCTmpOp.getReg())
770 .addImm(32));
771 // or tmp, rd, rd
772 Instructions.push_back(MCInstBuilder(SP::ORrr)
773 .addReg(MCRegOp.getReg())
774 .addReg(MCTmpOp.getReg())
775 .addReg(MCRegOp.getReg()));
776
777 return false;
778}
779
780bool SparcAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
782 MCStreamer &Out,
783 uint64_t &ErrorInfo,
784 bool MatchingInlineAsm) {
785 MCInst Inst;
787 unsigned MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo,
788 MatchingInlineAsm);
789 switch (MatchResult) {
790 case Match_Success: {
791 // The floating-point compares that name an %fcc register, even %fcc0,
792 // are V9 only. V8 has just the form without it.
793 switch (Inst.getOpcode()) {
794 case SP::V9FCMPS:
795 case SP::V9FCMPD:
796 case SP::V9FCMPQ:
797 case SP::V9FCMPES:
798 case SP::V9FCMPED:
799 case SP::V9FCMPEQ:
800 if (!isV9() && Operands.size() == 4)
801 return Error(((SparcOperand &)*Operands[1]).getStartLoc(),
802 "instruction requires a CPU feature not currently "
803 "enabled");
804 break;
805 }
806
807 switch (Inst.getOpcode()) {
808 default:
809 Inst.setLoc(IDLoc);
810 Instructions.push_back(Inst);
811 break;
812 case SP::SET:
813 if (expandSET(Inst, IDLoc, Instructions))
814 return true;
815 break;
816 case SP::SETSW:
817 if (expandSETSW(Inst, IDLoc, Instructions))
818 return true;
819 break;
820 case SP::SETX:
821 if (expandSETX(Inst, IDLoc, Instructions))
822 return true;
823 break;
824 }
825
826 for (const MCInst &I : Instructions) {
827 Out.emitInstruction(I, getSTI());
828 }
829 return false;
830 }
831
832 case Match_MissingFeature:
833 return Error(IDLoc,
834 "instruction requires a CPU feature not currently enabled");
835
836 case Match_InvalidOperand: {
837 SMLoc ErrorLoc = IDLoc;
838 if (ErrorInfo != ~0ULL) {
839 if (ErrorInfo >= Operands.size())
840 return Error(IDLoc, "too few operands for instruction");
841
842 ErrorLoc = ((SparcOperand &)*Operands[ErrorInfo]).getStartLoc();
843 if (ErrorLoc == SMLoc())
844 ErrorLoc = IDLoc;
845 }
846
847 return Error(ErrorLoc, "invalid operand for instruction");
848 }
849 case Match_MnemonicFail:
850 return Error(IDLoc, "invalid instruction mnemonic");
851 }
852 llvm_unreachable("Implement any new match types added!");
853}
854
855bool SparcAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
856 SMLoc &EndLoc) {
857 if (!tryParseRegister(Reg, StartLoc, EndLoc).isSuccess())
858 return Error(StartLoc, "invalid register name");
859 return false;
860}
861
862ParseStatus SparcAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
863 SMLoc &EndLoc) {
864 const AsmToken &Tok = Parser.getTok();
865 StartLoc = Tok.getLoc();
866 EndLoc = Tok.getEndLoc();
867 Reg = Sparc::NoRegister;
868 if (getLexer().getKind() != AsmToken::Percent)
870 Parser.Lex();
871 unsigned RegKind = SparcOperand::rk_None;
872 Reg = matchRegisterName(Tok, RegKind);
873 if (Reg) {
874 Parser.Lex();
876 }
877
878 getLexer().UnLex(Tok);
880}
881
882bool SparcAsmParser::parseInstruction(ParseInstructionInfo &Info,
883 StringRef Name, SMLoc NameLoc,
885 // Validate and reject unavailable mnemonics early before
886 // running any operand parsing.
887 // This is needed because some operands (mainly memory ones)
888 // differ between V8 and V9 ISA and so any operand parsing errors
889 // will cause IAS to bail out before it reaches matchAndEmitInstruction
890 // (where the instruction as a whole, including the mnemonic, is validated
891 // once again just before emission).
892 // As a nice side effect this also allows us to reject unknown
893 // instructions and suggest replacements.
894 MatchResultTy MS = mnemonicIsValid(Name, 0);
895 switch (MS) {
896 case Match_Success:
897 break;
898 case Match_MissingFeature:
899 return Error(NameLoc,
900 "instruction requires a CPU feature not currently enabled");
901 case Match_MnemonicFail:
902 return Error(NameLoc,
903 "invalid instruction mnemonic" +
904 SparcMnemonicSpellCheck(Name, getAvailableFeatures(), 0));
905 default:
906 llvm_unreachable("invalid return status!");
907 }
908
909 // First operand in MCInst is instruction mnemonic.
910 Operands.push_back(SparcOperand::CreateToken(Name, NameLoc));
911
912 // apply mnemonic aliases, if any, so that we can parse operands correctly.
913 applyMnemonicAliases(Name, getAvailableFeatures(), 0);
914
915 // An operand parser that fails has already reported an error. If nothing
916 // matched, report a generic one.
917 auto OperandFailed = [&](ParseStatus Res) {
918 if (Res.isFailure())
919 return true;
920 return Error(getLexer().getLoc(), "unexpected token");
921 };
922
923 if (getLexer().isNot(AsmToken::EndOfStatement)) {
924 // Read the first operand.
925 if (getLexer().is(AsmToken::Comma) && mnemonicTakesBranchModifiers(Name)) {
926 ParseStatus Res = parseBranchModifiers(Operands);
927 if (!Res.isSuccess())
928 return OperandFailed(Res);
929 }
930 ParseStatus Res = parseOperand(Operands, Name);
931 if (!Res.isSuccess())
932 return OperandFailed(Res);
933
934 while (getLexer().is(AsmToken::Comma) || getLexer().is(AsmToken::Plus)) {
935 if (getLexer().is(AsmToken::Plus)) {
936 // Plus tokens are significant in software_traps (p83, sparcv8.pdf). We must capture them.
937 Operands.push_back(SparcOperand::CreateToken("+", Parser.getTok().getLoc()));
938 }
939 Parser.Lex(); // Eat the comma or plus.
940 // Parse and remember the operand.
941 Res = parseOperand(Operands, Name);
942 if (!Res.isSuccess())
943 return OperandFailed(Res);
944 }
945 }
946 if (getLexer().isNot(AsmToken::EndOfStatement)) {
947 SMLoc Loc = getLexer().getLoc();
948 return Error(Loc, "unexpected token");
949 }
950 Parser.Lex(); // Consume the EndOfStatement.
951 return false;
952}
953
954/// parseDirectiveRegister
955/// ::= .register %g[2367], {#scratch|#ignore|symbolname}
956bool SparcAsmParser::parseDirectiveRegister() {
957 const char *Syntax =
958 "register syntax is .register %g[2367],{#scratch|symbolname|#ignore}";
959
960 SMLoc RegLoc = getLoc();
961 MCRegister Reg;
962 SMLoc S, E;
963 if (!tryParseRegister(Reg, S, E).isSuccess() ||
964 (Reg != SP::G2 && Reg != SP::G3 && Reg != SP::G6 && Reg != SP::G7))
965 return Error(RegLoc, Syntax);
966 if (parseToken(AsmToken::Comma, Syntax))
967 return true;
968
969 SMLoc NameLoc = getLoc();
970 bool Ignore = false;
971 StringRef Name;
972 if (parseOptionalToken(AsmToken::Hash)) {
973 StringRef Kind;
974 if (Parser.parseIdentifier(Kind) || (Kind != "scratch" && Kind != "ignore"))
975 return Error(NameLoc, Syntax);
976 Ignore = Kind == "ignore";
977 } else if (Parser.parseIdentifier(Name)) {
978 return Error(NameLoc, Syntax);
979 }
980 if (parseEOL())
981 return true;
982
983 auto &TS = static_cast<SparcTargetStreamer &>(
984 *getParser().getStreamer().getTargetStreamer());
985
986 // Like GNU as, a register may be declared again only in the same way, and
987 // its symbol may not be defined elsewhere. Only 64-bit code records the
988 // declaration.
989 if (is64Bit()) {
990 unsigned RegNo = MRI.getEncodingValue(Reg);
991 switch (TS.getRegisterDeclKind(RegNo, Ignore, Name)) {
992 case SparcTargetStreamer::RegisterDeclKind::Conflict:
993 return Error(RegLoc, "redefinition of global register");
994 case SparcTargetStreamer::RegisterDeclKind::New:
995 if (!Name.empty() && getContext().lookupSymbol(Name))
996 return Error(NameLoc, "register symbol '" + Name + "' already defined");
997 TS.declareRegister(RegNo, Ignore, Name);
998 break;
999 case SparcTargetStreamer::RegisterDeclKind::Same:
1000 break;
1001 }
1002 }
1003
1004 if (Ignore)
1005 TS.emitSparcRegisterIgnore(Reg);
1006 else if (Name.empty())
1007 TS.emitSparcRegisterScratch(Reg);
1008 else
1009 TS.emitSparcRegisterName(Reg, Name);
1010 return false;
1011}
1012
1013ParseStatus SparcAsmParser::parseDirective(AsmToken DirectiveID) {
1014 StringRef IDVal = DirectiveID.getString();
1015
1016 if (IDVal == ".register")
1017 return parseDirectiveRegister();
1018 if (IDVal == ".proc") {
1019 // For compatibility, ignore this directive.
1020 // (It's supposed to be an "optimization" in the Sun assembler)
1021 Parser.eatToEndOfStatement();
1022 return ParseStatus::Success;
1023 }
1024 if (IDVal == ".seg") {
1025 std::string Name;
1026 if (Parser.parseEscapedString(Name) || Parser.parseEOL())
1027 return ParseStatus::Failure;
1028
1029 MCSection *Section;
1030 uint32_t Subsection = 0;
1031 const MCObjectFileInfo *MCOFI = getContext().getObjectFileInfo();
1032 if (Name == "text") {
1033 Section = MCOFI->getTextSection();
1034 } else if (Name == "data" || Name == "data1") {
1035 Section = MCOFI->getDataSection();
1036 Subsection = Name == "data1" ? 1 : 0;
1037 } else if (Name == "bss") {
1038 Section = MCOFI->getBSSSection();
1039 } else {
1040 return Error(DirectiveID.getLoc(), "unknown segment type");
1041 }
1042 getStreamer().switchSection(Section, Subsection);
1043 return ParseStatus::Success;
1044 }
1045
1046 // Let the MC layer to handle other directives.
1047 return ParseStatus::NoMatch;
1048}
1049
1050ParseStatus SparcAsmParser::parseMEMOperand(OperandVector &Operands) {
1051 SMLoc S, E;
1052
1053 std::unique_ptr<SparcOperand> LHS;
1054 ParseStatus Res = parseSparcAsmOperand(LHS);
1055 if (!Res.isSuccess())
1056 return Res;
1057
1058 // Single immediate operand
1059 if (LHS->isImm()) {
1060 Operands.push_back(SparcOperand::MorphToMEMri(Sparc::G0, std::move(LHS)));
1061 return ParseStatus::Success;
1062 }
1063
1064 if (!LHS->isIntReg())
1065 return Error(LHS->getStartLoc(), "invalid register kind for this operand");
1066
1067 AsmToken Tok = getLexer().getTok();
1068 // The plus token may be followed by a register or an immediate value, the
1069 // minus one is always interpreted as sign for the immediate value
1070 if (Tok.is(AsmToken::Plus) || Tok.is(AsmToken::Minus)) {
1071 (void)Parser.parseOptionalToken(AsmToken::Plus);
1072
1073 std::unique_ptr<SparcOperand> RHS;
1074 Res = parseSparcAsmOperand(RHS);
1075 if (Res.isNoMatch())
1076 return Error(getLoc(), "expected register or immediate");
1077 if (Res.isFailure())
1078 return Res;
1079
1080 if (RHS->isReg() && !RHS->isIntReg())
1081 return Error(RHS->getStartLoc(),
1082 "invalid register kind for this operand");
1083
1084 Operands.push_back(
1085 RHS->isImm()
1086 ? SparcOperand::MorphToMEMri(LHS->getReg(), std::move(RHS))
1087 : SparcOperand::MorphToMEMrr(LHS->getReg(), std::move(RHS)));
1088
1089 return ParseStatus::Success;
1090 }
1091
1092 Operands.push_back(SparcOperand::CreateMEMr(LHS->getReg(), S, E));
1093 return ParseStatus::Success;
1094}
1095
1096template <unsigned N>
1097ParseStatus SparcAsmParser::parseShiftAmtImm(OperandVector &Operands) {
1098 SMLoc S = Parser.getTok().getLoc();
1099 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1100
1101 // This is a register, not an immediate
1102 if (getLexer().getKind() == AsmToken::Percent)
1103 return ParseStatus::NoMatch;
1104
1105 const MCExpr *Expr;
1106 if (getParser().parseExpression(Expr))
1107 return ParseStatus::Failure;
1108
1109 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr);
1110 if (!CE)
1111 return Error(S, "constant expression expected");
1112
1113 if (!isUInt<N>(CE->getValue()))
1114 return Error(S, "immediate shift value out of range");
1115
1116 Operands.push_back(SparcOperand::CreateImm(Expr, S, E));
1117 return ParseStatus::Success;
1118}
1119
1120template <SparcAsmParser::TailRelocKind Kind>
1121ParseStatus SparcAsmParser::parseTailRelocSym(OperandVector &Operands) {
1122 SMLoc S = getLoc();
1123 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1124
1125 auto MatchesKind = [](uint16_t RelType) -> bool {
1126 switch (Kind) {
1127 case TailRelocKind::Load_GOT:
1128 // Non-TLS relocations on ld (or ldx).
1129 // ld [%rr + %rr], %rr, %rel(sym)
1130 return RelType == ELF::R_SPARC_GOTDATA_OP;
1131 case TailRelocKind::Add_TLS:
1132 // TLS relocations on add.
1133 // add %rr, %rr, %rr, %rel(sym)
1134 switch (RelType) {
1135 case ELF::R_SPARC_TLS_GD_ADD:
1136 case ELF::R_SPARC_TLS_IE_ADD:
1137 case ELF::R_SPARC_TLS_LDM_ADD:
1138 case ELF::R_SPARC_TLS_LDO_ADD:
1139 return true;
1140 default:
1141 return false;
1142 }
1143 case TailRelocKind::Load_TLS:
1144 // TLS relocations on ld (or ldx).
1145 // ld[x] %addr, %rr, %rel(sym)
1146 switch (RelType) {
1147 case ELF::R_SPARC_TLS_IE_LD:
1148 case ELF::R_SPARC_TLS_IE_LDX:
1149 return true;
1150 default:
1151 return false;
1152 }
1153 case TailRelocKind::Call_TLS:
1154 // TLS relocations on call.
1155 // call sym, %rel(sym)
1156 switch (RelType) {
1157 case ELF::R_SPARC_TLS_GD_CALL:
1158 case ELF::R_SPARC_TLS_LDM_CALL:
1159 return true;
1160 default:
1161 return false;
1162 }
1163 }
1164 llvm_unreachable("Unhandled SparcAsmParser::TailRelocKind enum");
1165 };
1166
1167 if (getLexer().getKind() != AsmToken::Percent)
1168 return ParseStatus::NoMatch;
1169
1170 const AsmToken Tok = Parser.getTok();
1171 getParser().Lex(); // Eat '%'
1172
1173 if (getLexer().getKind() != AsmToken::Identifier)
1174 return Error(getLoc(), "expected valid identifier for operand modifier");
1175
1176 StringRef Name = getParser().getTok().getIdentifier();
1177 uint16_t RelType = Sparc::parseSpecifier(Name);
1178 if (RelType == 0)
1179 return Error(getLoc(), "invalid relocation specifier");
1180
1181 if (!MatchesKind(RelType)) {
1182 // Did not match the specified set of relocation types, put '%' back.
1183 getLexer().UnLex(Tok);
1184 return ParseStatus::NoMatch;
1185 }
1186
1187 Parser.Lex(); // Eat the identifier.
1188 if (getLexer().getKind() != AsmToken::LParen)
1189 return Error(getLoc(), "expected '('");
1190
1191 getParser().Lex(); // Eat '('
1192 const MCExpr *SubExpr;
1193 if (getParser().parseParenExpression(SubExpr, E))
1194 return ParseStatus::Failure;
1195
1196 const MCExpr *Val = adjustPICRelocation(RelType, SubExpr);
1197 Operands.push_back(SparcOperand::CreateTailRelocSym(Val, S, E));
1198 return ParseStatus::Success;
1199}
1200
1201ParseStatus SparcAsmParser::parseMembarTag(OperandVector &Operands) {
1202 SMLoc S = Parser.getTok().getLoc();
1203 const MCExpr *EVal;
1204 int64_t ImmVal = 0;
1205
1206 std::unique_ptr<SparcOperand> Mask;
1207 ParseStatus Res = parseSparcAsmOperand(Mask);
1208 if (Res.isFailure())
1209 return Res;
1210 if (Res.isSuccess()) {
1211 if (!Mask->isImm() || !Mask->getImm()->evaluateAsAbsolute(ImmVal) ||
1212 ImmVal < 0 || ImmVal > 127)
1213 return Error(S, "invalid membar mask number");
1214 }
1215
1216 while (getLexer().getKind() == AsmToken::Hash) {
1217 SMLoc TagStart = getLexer().getLoc();
1218 Parser.Lex(); // Eat the '#'.
1219 unsigned MaskVal = StringSwitch<unsigned>(Parser.getTok().getString())
1220 .Case("LoadLoad", 0x1)
1221 .Case("StoreLoad", 0x2)
1222 .Case("LoadStore", 0x4)
1223 .Case("StoreStore", 0x8)
1224 .Case("Lookaside", 0x10)
1225 .Case("MemIssue", 0x20)
1226 .Case("Sync", 0x40)
1227 .Default(0);
1228
1229 Parser.Lex(); // Eat the identifier token.
1230
1231 if (!MaskVal)
1232 return Error(TagStart, "unknown membar tag");
1233
1234 ImmVal |= MaskVal;
1235
1236 if (getLexer().getKind() == AsmToken::Pipe)
1237 Parser.Lex(); // Eat the '|'.
1238 }
1239
1240 EVal = MCConstantExpr::create(ImmVal, getContext());
1241 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1242 Operands.push_back(SparcOperand::CreateImm(EVal, S, E));
1243 return ParseStatus::Success;
1244}
1245
1246ParseStatus SparcAsmParser::parseASITag(OperandVector &Operands) {
1247 SMLoc S = Parser.getTok().getLoc();
1248 SMLoc E = Parser.getTok().getEndLoc();
1249 int64_t ASIVal = 0;
1250
1251 if (getLexer().getKind() != AsmToken::Hash) {
1252 // If the ASI tag provided is not a named tag, then it
1253 // must be a constant expression.
1254 ParseStatus ParseExprStatus = parseExpression(ASIVal);
1255 if (!ParseExprStatus.isSuccess())
1256 return ParseExprStatus;
1257
1258 if (!isUInt<8>(ASIVal))
1259 return Error(S, "invalid ASI number, must be between 0 and 255");
1260
1261 Operands.push_back(SparcOperand::CreateASITag(ASIVal, S, E));
1262 return ParseStatus::Success;
1263 }
1264
1265 // For now we only support named tags for 64-bit/V9 systems.
1266 // TODO: add support for 32-bit/V8 systems.
1267 SMLoc TagStart = getLexer().peekTok(false).getLoc();
1268 Parser.Lex(); // Eat the '#'.
1269 const StringRef ASIName = Parser.getTok().getString();
1270 const SparcASITag::ASITag *ASITag = SparcASITag::lookupASITagByName(ASIName);
1271 if (!ASITag)
1272 ASITag = SparcASITag::lookupASITagByAltName(ASIName);
1273 Parser.Lex(); // Eat the identifier token.
1274
1275 if (!ASITag)
1276 return Error(TagStart, "unknown ASI tag");
1277
1278 ASIVal = ASITag->Encoding;
1279
1280 Operands.push_back(SparcOperand::CreateASITag(ASIVal, S, E));
1281 return ParseStatus::Success;
1282}
1283
1284ParseStatus SparcAsmParser::parsePrefetchTag(OperandVector &Operands) {
1285 SMLoc S = Parser.getTok().getLoc();
1286 SMLoc E = Parser.getTok().getEndLoc();
1287 int64_t PrefetchVal = 0;
1288
1289 if (getLexer().getKind() != AsmToken::Hash) {
1290 // If the prefetch tag provided is not a named tag, then it
1291 // must be a constant expression.
1292 ParseStatus ParseExprStatus = parseExpression(PrefetchVal);
1293 if (!ParseExprStatus.isSuccess())
1294 return ParseExprStatus;
1295
1296 if (!isUInt<8>(PrefetchVal))
1297 return Error(S, "invalid prefetch number, must be between 0 and 31");
1298
1299 Operands.push_back(SparcOperand::CreatePrefetchTag(PrefetchVal, S, E));
1300 return ParseStatus::Success;
1301 }
1302
1303 SMLoc TagStart = getLexer().peekTok(false).getLoc();
1304 Parser.Lex(); // Eat the '#'.
1305 const StringRef PrefetchName = Parser.getTok().getString();
1306 const SparcPrefetchTag::PrefetchTag *PrefetchTag =
1307 SparcPrefetchTag::lookupPrefetchTagByName(PrefetchName);
1308 Parser.Lex(); // Eat the identifier token.
1309
1310 if (!PrefetchTag)
1311 return Error(TagStart, "unknown prefetch tag");
1312
1313 PrefetchVal = PrefetchTag->Encoding;
1314
1315 Operands.push_back(SparcOperand::CreatePrefetchTag(PrefetchVal, S, E));
1316 return ParseStatus::Success;
1317}
1318
1319ParseStatus SparcAsmParser::parseCallTarget(OperandVector &Operands) {
1320 SMLoc S = Parser.getTok().getLoc();
1321 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
1322
1323 switch (getLexer().getKind()) {
1324 default:
1325 return ParseStatus::NoMatch;
1326 case AsmToken::LParen:
1327 case AsmToken::Integer:
1329 case AsmToken::Dot:
1330 break;
1331 }
1332
1333 const MCExpr *DestValue;
1334 if (getParser().parseExpression(DestValue))
1335 return ParseStatus::Failure;
1336
1337 Operands.push_back(SparcOperand::CreateImm(DestValue, S, E));
1338 return ParseStatus::Success;
1339}
1340
1341ParseStatus SparcAsmParser::parseOperand(OperandVector &Operands,
1342 StringRef Mnemonic) {
1343
1344 ParseStatus Res = MatchOperandParserImpl(Operands, Mnemonic);
1345
1346 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1347 // there was a match, but an error occurred, in which case, just return that
1348 // the operand parsing failed.
1349 if (Res.isSuccess() || Res.isFailure())
1350 return Res;
1351
1352 if (getLexer().is(AsmToken::LBrac)) {
1353 // Memory operand
1354 Operands.push_back(SparcOperand::CreateToken("[",
1355 Parser.getTok().getLoc()));
1356 Parser.Lex(); // Eat the [
1357
1358 if (Mnemonic == "cas" || Mnemonic == "casl" || Mnemonic == "casa" ||
1359 Mnemonic == "casx" || Mnemonic == "casxl" || Mnemonic == "casxa") {
1360 SMLoc S = Parser.getTok().getLoc();
1361 if (getLexer().getKind() != AsmToken::Percent)
1362 return Error(getLoc(), "expected register name");
1363 Parser.Lex(); // eat %
1364
1365 unsigned RegKind;
1366 MCRegister Reg = matchRegisterName(Parser.getTok(), RegKind);
1367 if (!Reg)
1368 return Error(getLoc(), "expected register name");
1369
1370 Parser.Lex(); // Eat the identifier token.
1371 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer()-1);
1372 Operands.push_back(SparcOperand::CreateReg(Reg, RegKind, S, E));
1374 } else {
1375 Res = parseMEMOperand(Operands);
1376 if (Res.isNoMatch())
1377 return Error(getLoc(), "expected register or immediate");
1378 }
1379
1380 if (!Res.isSuccess())
1381 return Res;
1382
1383 if (!getLexer().is(AsmToken::RBrac))
1384 return Error(getLoc(), "expected ']'");
1385
1386 Operands.push_back(SparcOperand::CreateToken("]",
1387 Parser.getTok().getLoc()));
1388 Parser.Lex(); // Eat the ]
1389
1390 // Parse an optional address-space identifier after the address.
1391 // This will be either an immediate constant expression, or, on 64-bit
1392 // processors, the %asi register.
1393 StringRef MalformedASITag =
1394 is64Bit() ? "malformed ASI tag, must be %asi, a constant integer "
1395 "expression, or a named tag"
1396 : "malformed ASI tag, must be a constant integer expression";
1397 if (getLexer().is(AsmToken::Percent)) {
1398 SMLoc S = Parser.getTok().getLoc();
1399 if (!is64Bit())
1400 return Error(S, MalformedASITag);
1401
1402 Parser.Lex(); // Eat the %.
1403 const AsmToken Tok = Parser.getTok();
1404 if (Tok.is(AsmToken::Identifier) && Tok.getString() == "asi") {
1405 // Here we patch the MEM operand from [base + %g0] into [base + 0]
1406 // as memory operations with ASI tag stored in %asi register needs
1407 // to use immediate offset. We need to do this because Reg addressing
1408 // will be parsed as Reg+G0 initially.
1409 // This allows forms such as `ldxa [%o0] %asi, %o0` to parse correctly.
1410 SparcOperand &OldMemOp = (SparcOperand &)*Operands[Operands.size() - 2];
1411 if (OldMemOp.isMEMrr()) {
1412 if (OldMemOp.getMemOffsetReg() != Sparc::G0) {
1413 return Error(S, "invalid operand for instruction");
1414 }
1415 Operands[Operands.size() - 2] = SparcOperand::MorphToMEMri(
1416 OldMemOp.getMemBase(),
1417 SparcOperand::CreateImm(MCConstantExpr::create(0, getContext()),
1418 OldMemOp.getStartLoc(),
1419 OldMemOp.getEndLoc()));
1420 }
1421 Parser.Lex(); // Eat the identifier.
1422 // In this context, we convert the register operand into
1423 // a plain "%asi" token since the register access is already
1424 // implicit in the instruction definition and encoding.
1425 // See LoadASI/StoreASI in SparcInstrInfo.td.
1426 Operands.push_back(SparcOperand::CreateToken("%asi", S));
1427 return ParseStatus::Success;
1428 }
1429
1430 return Error(S, MalformedASITag);
1431 }
1432
1433 // If we're not at the end of statement and the next token is not a comma,
1434 // then it is an immediate ASI value.
1435 if (getLexer().isNot(AsmToken::EndOfStatement) &&
1436 getLexer().isNot(AsmToken::Comma)) {
1437 Res = parseASITag(Operands);
1438 if (Res.isNoMatch())
1439 return Error(getLoc(), MalformedASITag);
1440 return Res;
1441 }
1442 return ParseStatus::Success;
1443 }
1444
1445 std::unique_ptr<SparcOperand> Op;
1446
1447 Res = parseSparcAsmOperand(Op);
1448 if (!Res.isSuccess())
1449 return Res;
1450
1451 // Push the parsed operand into the list of operands
1452 Operands.push_back(std::move(Op));
1453
1454 return ParseStatus::Success;
1455}
1456
1457ParseStatus
1458SparcAsmParser::parseSparcAsmOperand(std::unique_ptr<SparcOperand> &Op) {
1459 SMLoc S = Parser.getTok().getLoc();
1460 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1461 const MCExpr *EVal;
1462
1463 Op = nullptr;
1464 switch (getLexer().getKind()) {
1465 default:
1466 return ParseStatus::NoMatch;
1467
1468 case AsmToken::Percent: {
1469 Parser.Lex(); // Eat the '%'.
1470 unsigned RegKind;
1471 if (MCRegister Reg = matchRegisterName(Parser.getTok(), RegKind)) {
1472 StringRef Name = Parser.getTok().getString();
1473 Parser.Lex(); // Eat the identifier token.
1474
1475 if (Name == "ncc")
1476 Name = is64Bit() ? "xcc" : "icc";
1477
1478 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1479 if (Reg == Sparc::ICC && Name == "xcc")
1480 Op = SparcOperand::CreateToken("%xcc", S);
1481 else
1482 Op = SparcOperand::CreateReg(Reg, RegKind, S, E);
1483 return ParseStatus::Success;
1484 }
1485 ParseStatus Res = matchSparcAsmModifiers(EVal, E);
1486 // Neither a register name nor a relocation specifier.
1487 if (Res.isNoMatch())
1488 return Error(getLoc(), "invalid register name");
1489 if (Res.isFailure())
1490 return Res;
1491 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1492 Op = SparcOperand::CreateImm(EVal, S, E);
1493 return ParseStatus::Success;
1494 }
1495
1496 case AsmToken::Plus:
1497 case AsmToken::Minus:
1498 case AsmToken::Tilde:
1499 case AsmToken::Integer:
1500 case AsmToken::LParen:
1501 case AsmToken::Dot:
1503 if (getParser().parseExpression(EVal, E))
1504 return ParseStatus::Failure;
1505
1506 Op = SparcOperand::CreateImm(EVal, S, E);
1507 return ParseStatus::Success;
1508 }
1509}
1510
1511ParseStatus SparcAsmParser::parseBranchModifiers(OperandVector &Operands) {
1512 // parse (,a|,pn|,pt)+
1513
1514 while (getLexer().is(AsmToken::Comma)) {
1515 Parser.Lex(); // Eat the comma
1516
1517 if (!getLexer().is(AsmToken::Identifier))
1518 return Error(getLoc(), "expected branch modifier");
1519 StringRef modName = Parser.getTok().getString();
1520 if (modName == "a" || modName == "pn" || modName == "pt") {
1521 Operands.push_back(SparcOperand::CreateToken(modName,
1522 Parser.getTok().getLoc()));
1523 Parser.Lex(); // eat the identifier.
1524 }
1525 }
1526 return ParseStatus::Success;
1527}
1528
1529ParseStatus SparcAsmParser::parseExpression(int64_t &Val) {
1530 AsmToken Tok = getLexer().getTok();
1531
1532 if (!isPossibleExpression(Tok))
1533 return ParseStatus::NoMatch;
1534
1535 return getParser().parseAbsoluteExpression(Val);
1536}
1537
1538MCRegister SparcAsmParser::matchRegisterName(const AsmToken &Tok,
1539 unsigned &RegKind) {
1540 RegKind = SparcOperand::rk_None;
1541 if (!Tok.is(AsmToken::Identifier))
1542 return SP::NoRegister;
1543
1544 StringRef Name = Tok.getString();
1545 // Register names are case-sensitive, as in GNU as.
1546 MCRegister Reg = MatchRegisterName(Name);
1547 if (!Reg) {
1548 Reg = MatchRegisterAltName(Name);
1549 // The alternate names of %asr2-%asr6 (%ccr, %asi, %pc and %fprs) are V9
1550 // only.
1551 if (Reg && !isV9())
1552 return SP::NoRegister;
1553 }
1554
1555 // %tick is V9 only. It also matches the privileged register TICK.
1556 if (Reg == SP::TICK && !isV9())
1557 return SP::NoRegister;
1558
1559 if (Reg) {
1560 // Some registers share a spelling (e.g. "f0" for F0/D0/Q0, "sp" for
1561 // O6/O6_O7). PositionOrder in SparcRegisterInfo.td makes the generated
1562 // matcher return the canonical one.
1563
1564 // See the note in SparcRegisterInfo.td near ASRRegs register class.
1565 if (Reg == SP::ASR4 && Name == "tick") {
1566 RegKind = SparcOperand::rk_Special;
1567 return SP::TICK;
1568 }
1569
1570 if (MRI.getRegClass(SP::IntRegsRegClassID).contains(Reg)) {
1571 RegKind = SparcOperand::rk_IntReg;
1572 return Reg;
1573 }
1574
1575 // The generated matcher never returns QFP registers because of
1576 // PositionOrder.
1577 assert(!MRI.getRegClass(SP::QFPRegsRegClassID).contains(Reg));
1578
1579 // Other registers do not need special handling.
1580 RegKind = SparcOperand::rk_Special;
1581 return Reg;
1582 }
1583
1584 // If we still have no match, try custom parsing.
1585 // Not all registers and their spellings are modeled in td files.
1586
1587 // O6 and I6 have primary AsmNames "sp" and "fp", so handle "%o6"/"%i6" here.
1588 if (Name == "o6") {
1589 RegKind = SparcOperand::rk_IntReg;
1590 return SP::O6;
1591 }
1592 if (Name == "i6") {
1593 RegKind = SparcOperand::rk_IntReg;
1594 return SP::I6;
1595 }
1596
1597 // %r0 - %r31
1598 unsigned RegNo;
1599 if (Name.starts_with("r") && !Name.drop_front().getAsInteger(10, RegNo) &&
1600 RegNo <= 31) {
1601 RegKind = SparcOperand::rk_IntReg;
1602 return IntRegs[RegNo];
1603 }
1604
1605 if (Name == "xcc" || Name == "ncc") {
1606 // FIXME:: check 64bit.
1607 RegKind = SparcOperand::rk_Special;
1608 return SP::ICC;
1609 }
1610
1611 // The remaining names are V9 only.
1612 if (!isV9())
1613 return SP::NoRegister;
1614
1615 // JPS1 extension - aliases for ASRs
1616 // Section 5.2.11 - Ancillary State Registers (ASRs)
1617 if (Name == "pcr") {
1618 RegKind = SparcOperand::rk_Special;
1619 return SP::ASR16;
1620 }
1621 if (Name == "pic") {
1622 RegKind = SparcOperand::rk_Special;
1623 return SP::ASR17;
1624 }
1625 if (Name == "dcr") {
1626 RegKind = SparcOperand::rk_Special;
1627 return SP::ASR18;
1628 }
1629 if (Name == "gsr") {
1630 RegKind = SparcOperand::rk_Special;
1631 return SP::ASR19;
1632 }
1633 if (Name == "set_softint") {
1634 RegKind = SparcOperand::rk_Special;
1635 return SP::ASR20;
1636 }
1637 if (Name == "clear_softint") {
1638 RegKind = SparcOperand::rk_Special;
1639 return SP::ASR21;
1640 }
1641 if (Name == "softint") {
1642 RegKind = SparcOperand::rk_Special;
1643 return SP::ASR22;
1644 }
1645 if (Name == "tick_cmpr") {
1646 RegKind = SparcOperand::rk_Special;
1647 return SP::ASR23;
1648 }
1649 if (Name == "stick" || Name == "sys_tick") {
1650 RegKind = SparcOperand::rk_Special;
1651 return SP::ASR24;
1652 }
1653 if (Name == "stick_cmpr" || Name == "sys_tick_cmpr") {
1654 RegKind = SparcOperand::rk_Special;
1655 return SP::ASR25;
1656 }
1657
1658 return SP::NoRegister;
1659}
1660
1661// Determine if an expression contains a reference to the symbol
1662// "_GLOBAL_OFFSET_TABLE_".
1663static bool hasGOTReference(const MCExpr *Expr) {
1664 switch (Expr->getKind()) {
1665 case MCExpr::Target:
1666 if (const MCSpecifierExpr *SE = dyn_cast<MCSpecifierExpr>(Expr))
1667 return hasGOTReference(SE->getSubExpr());
1668 break;
1669
1670 case MCExpr::Constant:
1671 break;
1672
1673 case MCExpr::Binary: {
1674 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
1675 return hasGOTReference(BE->getLHS()) || hasGOTReference(BE->getRHS());
1676 }
1677
1678 case MCExpr::SymbolRef: {
1679 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
1680 return (SymRef.getSymbol().getName() == "_GLOBAL_OFFSET_TABLE_");
1681 }
1682
1683 case MCExpr::Unary:
1684 return hasGOTReference(cast<MCUnaryExpr>(Expr)->getSubExpr());
1685
1686 case MCExpr::Specifier:
1687 llvm_unreachable("unused by this backend");
1688 }
1689 return false;
1690}
1691
1692const MCSpecifierExpr *
1693SparcAsmParser::adjustPICRelocation(uint16_t RelType, const MCExpr *subExpr) {
1694 // When in PIC mode, "%lo(...)" and "%hi(...)" behave differently.
1695 // If the expression refers contains _GLOBAL_OFFSET_TABLE, it is
1696 // actually a %pc10 or %pc22 relocation. Otherwise, they are interpreted
1697 // as %got10 or %got22 relocation.
1698
1699 if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1700 switch (RelType) {
1701 default: break;
1702 case ELF::R_SPARC_LO10:
1703 RelType =
1704 hasGOTReference(subExpr) ? ELF::R_SPARC_PC10 : ELF::R_SPARC_GOT10;
1705 break;
1706 case ELF::R_SPARC_HI22:
1707 RelType =
1708 hasGOTReference(subExpr) ? ELF::R_SPARC_PC22 : ELF::R_SPARC_GOT22;
1709 break;
1710 }
1711 }
1712
1713 return MCSpecifierExpr::create(subExpr, RelType, getContext());
1714}
1715
1716ParseStatus SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
1717 SMLoc &EndLoc) {
1718 // A relocation specifier is an identifier followed by '('.
1719 AsmToken Tok = Parser.getTok();
1720 if (!Tok.is(AsmToken::Identifier) ||
1721 !getLexer().peekTok().is(AsmToken::LParen))
1722 return ParseStatus::NoMatch;
1723
1724 StringRef name = Tok.getString();
1725
1726 auto VK = Sparc::parseSpecifier(name);
1727 switch (VK) {
1728 case 0:
1729 return Error(getLoc(), "invalid relocation specifier");
1730
1731 case ELF::R_SPARC_GOTDATA_OP:
1732 case ELF::R_SPARC_TLS_GD_ADD:
1733 case ELF::R_SPARC_TLS_GD_CALL:
1734 case ELF::R_SPARC_TLS_IE_ADD:
1735 case ELF::R_SPARC_TLS_IE_LD:
1736 case ELF::R_SPARC_TLS_IE_LDX:
1737 case ELF::R_SPARC_TLS_LDM_ADD:
1738 case ELF::R_SPARC_TLS_LDM_CALL:
1739 case ELF::R_SPARC_TLS_LDO_ADD:
1740 // These are special-cased at tablegen level and are only valid as the
1741 // trailing relocation operand, see parseTailRelocSym.
1742 return Error(getLoc(), "unexpected relocation specifier");
1743
1744 default:
1745 break;
1746 }
1747
1748 Parser.Lex(); // Eat the identifier.
1749 Parser.Lex(); // Eat the LParen token.
1750 const MCExpr *subExpr;
1751 if (Parser.parseParenExpression(subExpr, EndLoc))
1752 return ParseStatus::Failure;
1753
1754 EVal = adjustPICRelocation(VK, subExpr);
1755 return ParseStatus::Success;
1756}
1757
1758bool SparcAsmParser::parseExprWithSpecifier(const MCExpr *&Res, SMLoc &E) {
1759 SMLoc Loc = getLoc();
1760 if (getLexer().getKind() != AsmToken::Identifier)
1761 return TokError("expected '%' relocation specifier");
1762 auto Spec = Sparc::parseDataSpecifier(Parser.getTok().getIdentifier());
1763 if (!Spec)
1764 return TokError("invalid relocation specifier");
1765
1766 Parser.Lex();
1767 if (parseToken(AsmToken::LParen, "expected '('"))
1768 return true;
1769
1770 const MCExpr *SubExpr;
1771 if (Parser.parseParenExpression(SubExpr, E))
1772 return true;
1773 Res = MCSpecifierExpr::create(SubExpr, Spec, getContext(), Loc);
1774 return false;
1775}
1776
1777bool SparcAsmParser::parseDataExpr(const MCExpr *&Res) {
1778 SMLoc EndLoc;
1779 if (parseOptionalToken(AsmToken::Percent))
1780 return parseExprWithSpecifier(Res, EndLoc);
1781 return Parser.parseExpression(Res);
1782}
1783
1784bool SparcAsmParser::isPossibleExpression(const AsmToken &Token) {
1785 switch (Token.getKind()) {
1786 case AsmToken::LParen:
1787 case AsmToken::Integer:
1789 case AsmToken::Plus:
1790 case AsmToken::Minus:
1791 case AsmToken::Tilde:
1792 return true;
1793 default:
1794 return false;
1795 }
1796}
1797
1798extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void applyMnemonicAliases(StringRef &Mnemonic, const FeatureBitset &Features, unsigned VariantID)
static bool addCallTargetOperands(MachineInstrBuilder &CallInst, MachineIRBuilder &MIRBuilder, AMDGPUCallLowering::CallLoweringInfo &Info, bool IsDynamicVGPRChainCall=false)
uint64_t uint64_t
uint64_t Imm
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
ReachingDefInfo InstSet InstSet & Ignore
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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
loop data Loop Data Prefetch
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
MachineRegisterInfo & MRI
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
static const char * name
This file defines the SmallVector class.
static bool hasGOTReference(const MCExpr *Expr)
static const MCPhysReg IntRegs[32]
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSparcAsmParser()
static bool is64Bit(const char *name)
Value * RHS
Value * LHS
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:31
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 SMLoc getEndLoc() const
Definition AsmLexer.cpp:33
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 eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
virtual bool parseEscapedString(std::string &Data)=0
Parse the current token as a string which may include escaped characters and return the string conten...
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
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 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression, assuming that an initial '(' has already been consumed.
bool parseOptionalToken(AsmToken::TokenKind T)
Attempt to parse and consume token, returning true on success.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
Binary assembler expressions.
Definition MCExpr.h:295
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition MCExpr.h:442
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition MCExpr.h:445
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
@ Unary
Unary expressions.
Definition MCExpr.h:41
@ Constant
Constant expressions.
Definition MCExpr.h:39
@ SymbolRef
References to labels and assigned expressions.
Definition MCExpr.h:40
@ Target
Target specific expression.
Definition MCExpr.h:43
@ Specifier
Expression with a relocation specifier.
Definition MCExpr.h:42
@ Binary
Binary expressions.
Definition MCExpr.h:38
ExprKind getKind() const
Definition MCExpr.h:82
void setLoc(SMLoc loc)
Definition MCInst.h:206
unsigned getOpcode() const
Definition MCInst.h:201
void addOperand(const MCOperand Op)
Definition MCInst.h:214
void setOpcode(unsigned Op)
Definition MCInst.h:200
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:209
MCSection * getBSSSection() const
MCSection * getTextSection() const
MCSection * getDataSection() const
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
int64_t getImm() const
Definition MCInst.h:83
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
bool isImm() const
Definition MCInst.h:65
bool isReg() const
Definition MCInst.h:64
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
const MCExpr * getExpr() const
Definition MCInst.h:117
bool isExpr() const
Definition MCInst.h:68
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
bool contains(MCRegister Reg) const
contains - Return true if the specified register is included in this register class.
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
constexpr unsigned id() const
Definition MCRegister.h:85
Extension point for target-specific MCExpr subclasses with a relocation specifier,...
Definition MCExpr.h:491
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:221
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:187
const MCSymbol & getSymbol() const
Definition MCExpr.h:223
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
MCTargetAsmParser - Generic interface to target specific assembly parsers.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
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
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
bool isIntReg(MCRegister Reg)
MCExpr const & getExpr(MCExpr const &Expr)
MCRegister matchRegisterName(StringRef Name, const MCRegisterInfo &MRI, unsigned RegClassID, unsigned AltIdx)
Match a symbolic name in RegClassID, or return an invalid register.
constexpr MCPhysReg SP
uint16_t parseSpecifier(StringRef name)
uint16_t parseDataSpecifier(StringRef name)
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
This is an optimization pass for GlobalISel generic memory operations.
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
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
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Target & getTheSparcTarget()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
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...
Target & getTheSparcV9Target()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Target & getTheSparcelTarget()
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...