LLVM 23.0.0git
XtensaAsmParser.cpp
Go to the documentation of this file.
1//===- XtensaAsmParser.cpp - Parse Xtensa assembly to MCInst instructions -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10
15#include "llvm/ADT/STLExtras.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/MC/MCInstrInfo.h"
25#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSymbol.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "xtensa-asm-parser"
34
35struct XtensaOperand;
36
38 const MCRegisterInfo &MRI;
39
40 enum XtensaRegisterType { Xtensa_Generic, Xtensa_SR, Xtensa_UR };
41 SMLoc getLoc() const { return getParser().getTok().getLoc(); }
42
43 XtensaTargetStreamer &getTargetStreamer() {
45 return static_cast<XtensaTargetStreamer &>(TS);
46 }
47
48 ParseStatus parseDirective(AsmToken DirectiveID) override;
49 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
50 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
51 SMLoc NameLoc, OperandVector &Operands) override;
52 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
53 OperandVector &Operands, MCStreamer &Out,
55 bool MatchingInlineAsm) override;
56 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
57 unsigned Kind) override;
58
59 bool processInstruction(MCInst &Inst, SMLoc IDLoc, MCStreamer &Out,
60 const MCSubtargetInfo *STI);
61
62// Auto-generated instruction matching functions
63#define GET_ASSEMBLER_HEADER
64#include "XtensaGenAsmMatcher.inc"
65
68 parseRegister(OperandVector &Operands, bool AllowParens = false,
69 XtensaRegisterType SR = Xtensa_Generic,
71 ParseStatus parseOperandWithModifier(OperandVector &Operands);
72 bool
73 parseOperand(OperandVector &Operands, StringRef Mnemonic,
74 XtensaRegisterType SR = Xtensa_Generic,
76 bool ParseInstructionWithSR(ParseInstructionInfo &Info, StringRef Name,
77 SMLoc NameLoc, OperandVector &Operands);
78 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
79 SMLoc &EndLoc) override {
81 }
82
83 ParseStatus parsePCRelTarget(OperandVector &Operands);
84 bool parseLiteralDirective(SMLoc L);
85
86public:
89#define GET_OPERAND_DIAGNOSTIC_TYPES
90#include "XtensaGenAsmMatcher.inc"
91#undef GET_OPERAND_DIAGNOSTIC_TYPES
92 };
93
95 const MCInstrInfo &MII)
97 MRI(*Parser.getContext().getRegisterInfo()) {
98 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
99 }
100
101 bool hasWindowed() const {
102 return getSTI().getFeatureBits()[Xtensa::FeatureWindowed];
103 };
104};
105
106// Return true if Expr is in the range [MinValue, MaxValue].
107static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
108 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
109 int64_t Value = CE->getValue();
110 return Value >= MinValue && Value <= MaxValue;
111 }
112 return false;
113}
114
116
122
123 struct RegOp {
124 unsigned RegNum;
125 };
126
127 struct ImmOp {
128 const MCExpr *Val;
129 };
130
132 union {
136 };
137
139
140public:
142 Kind = o.Kind;
143 StartLoc = o.StartLoc;
144 EndLoc = o.EndLoc;
145 switch (Kind) {
146 case Register:
147 Reg = o.Reg;
148 break;
149 case Immediate:
150 Imm = o.Imm;
151 break;
152 case Token:
153 Tok = o.Tok;
154 break;
155 }
156 }
157
158 bool isToken() const override { return Kind == Token; }
159 bool isReg() const override { return Kind == Register; }
160 bool isImm() const override { return Kind == Immediate; }
161 bool isMem() const override { return false; }
162
163 bool isImm(int64_t MinValue, int64_t MaxValue) const {
164 return Kind == Immediate && inRange(getImm(), MinValue, MaxValue);
165 }
166
167 bool isImm8() const { return isImm(-128, 127); }
168
169 bool isImm8_sh8() const {
170 return isImm(-32768, 32512) &&
171 ((cast<MCConstantExpr>(getImm())->getValue() & 0xFF) == 0);
172 }
173
174 bool isImm12() const { return isImm(-2048, 2047); }
175
176 // Convert MOVI to literal load, when immediate is not in range (-2048, 2047)
177 bool isImm12m() const { return Kind == Immediate; }
178
179 bool isOffset4m32() const {
180 return isImm(0, 60) &&
181 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
182 }
183
184 bool isOffset8m8() const { return isImm(0, 255); }
185
186 bool isOffset8m16() const {
187 return isImm(0, 510) &&
188 ((cast<MCConstantExpr>(getImm())->getValue() & 0x1) == 0);
189 }
190
191 bool isOffset8m32() const {
192 return isImm(0, 1020) &&
193 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
194 }
195
196 bool isentry_imm12() const {
197 return isImm(0, 32760) &&
198 ((cast<MCConstantExpr>(getImm())->getValue() % 8) == 0);
199 }
200
201 bool isUimm4() const { return isImm(0, 15); }
202
203 bool isUimm5() const { return isImm(0, 31); }
204
205 bool isImm8n_7() const { return isImm(-8, 7); }
206
207 bool isShimm1_31() const { return isImm(1, 31); }
208
209 bool isImm16_31() const { return isImm(16, 31); }
210
211 bool isImm1_16() const { return isImm(1, 16); }
212
213 // Check that value is either equals (-1) or from [1,15] range.
214 bool isImm1n_15() const { return isImm(1, 15) || isImm(-1, -1); }
215
216 bool isImm32n_95() const { return isImm(-32, 95); }
217
218 bool isImm64n_4n() const {
219 return isImm(-64, -4) &&
220 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
221 }
222
223 bool isB4const() const {
224 if (Kind != Immediate)
225 return false;
226 if (auto *CE = dyn_cast<MCConstantExpr>(getImm())) {
227 int64_t Value = CE->getValue();
228 switch (Value) {
229 case -1:
230 case 1:
231 case 2:
232 case 3:
233 case 4:
234 case 5:
235 case 6:
236 case 7:
237 case 8:
238 case 10:
239 case 12:
240 case 16:
241 case 32:
242 case 64:
243 case 128:
244 case 256:
245 return true;
246 default:
247 return false;
248 }
249 }
250 return false;
251 }
252
253 bool isB4constu() const {
254 if (Kind != Immediate)
255 return false;
256 if (auto *CE = dyn_cast<MCConstantExpr>(getImm())) {
257 int64_t Value = CE->getValue();
258 switch (Value) {
259 case 32768:
260 case 65536:
261 case 2:
262 case 3:
263 case 4:
264 case 5:
265 case 6:
266 case 7:
267 case 8:
268 case 10:
269 case 12:
270 case 16:
271 case 32:
272 case 64:
273 case 128:
274 case 256:
275 return true;
276 default:
277 return false;
278 }
279 }
280 return false;
281 }
282
283 bool isimm7_22() const { return isImm(7, 22); }
284
285 bool isSelect_256() const { return isImm(0, 255); }
286
287 /// getStartLoc - Gets location of the first token of this operand
288 SMLoc getStartLoc() const override { return StartLoc; }
289 /// getEndLoc - Gets location of the last token of this operand
290 SMLoc getEndLoc() const override { return EndLoc; }
291
292 MCRegister getReg() const override {
293 assert(Kind == Register && "Invalid type access!");
294 return Reg.RegNum;
295 }
296
297 const MCExpr *getImm() const {
298 assert(Kind == Immediate && "Invalid type access!");
299 return Imm.Val;
300 }
301
303 assert(Kind == Token && "Invalid type access!");
304 return Tok;
305 }
306
307 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
308 switch (Kind) {
309 case Immediate:
310 MAI.printExpr(OS, *getImm());
311 break;
312 case Register:
313 OS << "<register x";
314 OS << getReg() << ">";
315 break;
316 case Token:
317 OS << "'" << getToken() << "'";
318 break;
319 }
320 }
321
322 static std::unique_ptr<XtensaOperand> createToken(StringRef Str, SMLoc S) {
323 auto Op = std::make_unique<XtensaOperand>(Token);
324 Op->Tok = Str;
325 Op->StartLoc = S;
326 Op->EndLoc = S;
327 return Op;
328 }
329
330 static std::unique_ptr<XtensaOperand> createReg(unsigned RegNo, SMLoc S,
331 SMLoc E) {
332 auto Op = std::make_unique<XtensaOperand>(Register);
333 Op->Reg.RegNum = RegNo;
334 Op->StartLoc = S;
335 Op->EndLoc = E;
336 return Op;
337 }
338
339 static std::unique_ptr<XtensaOperand> createImm(const MCExpr *Val, SMLoc S,
340 SMLoc E) {
341 auto Op = std::make_unique<XtensaOperand>(Immediate);
342 Op->Imm.Val = Val;
343 Op->StartLoc = S;
344 Op->EndLoc = E;
345 return Op;
346 }
347
348 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
349 assert(Expr && "Expr shouldn't be null!");
350 int64_t Imm = 0;
351 bool IsConstant = false;
352
353 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
354 IsConstant = true;
355 Imm = CE->getValue();
356 }
357
358 if (IsConstant)
360 else
362 }
363
364 // Used by the TableGen Code
365 void addRegOperands(MCInst &Inst, unsigned N) const {
366 assert(N == 1 && "Invalid number of operands!");
368 }
369
370 void addImmOperands(MCInst &Inst, unsigned N) const {
371 assert(N == 1 && "Invalid number of operands!");
372 addExpr(Inst, getImm());
373 }
374};
375
376#define GET_REGISTER_MATCHER
377#define GET_MATCHER_IMPLEMENTATION
378#include "XtensaGenAsmMatcher.inc"
379
380unsigned XtensaAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
381 unsigned Kind) {
383}
384
385static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands,
387 if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) {
388 SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc();
389 if (ErrorLoc == SMLoc())
390 return Loc;
391 return ErrorLoc;
392 }
393 return Loc;
394}
395
396bool XtensaAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
397 MCStreamer &Out,
398 const MCSubtargetInfo *STI) {
399 Inst.setLoc(IDLoc);
400 const unsigned Opcode = Inst.getOpcode();
401 switch (Opcode) {
402 case Xtensa::L32R: {
403 const MCSymbolRefExpr *OpExpr =
404 static_cast<const MCSymbolRefExpr *>(Inst.getOperand(1).getExpr());
405 Inst.getOperand(1).setExpr(OpExpr);
406 break;
407 }
408 case Xtensa::MOVI: {
409 XtensaTargetStreamer &TS = this->getTargetStreamer();
410
411 // Expand MOVI operand
412 if (!Inst.getOperand(1).isExpr()) {
413 uint64_t ImmOp64 = Inst.getOperand(1).getImm();
414 int32_t Imm = ImmOp64;
415 if (!isInt<12>(Imm)) {
416 XtensaTargetStreamer &TS = this->getTargetStreamer();
417 MCInst TmpInst;
418 TmpInst.setLoc(IDLoc);
419 TmpInst.setOpcode(Xtensa::L32R);
420 const MCExpr *Value = MCConstantExpr::create(ImmOp64, getContext());
422 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
423 TmpInst.addOperand(Inst.getOperand(0));
424 MCOperand Op1 = MCOperand::createExpr(Expr);
425 TmpInst.addOperand(Op1);
426 TS.emitLiteral(Sym, Value, true, IDLoc);
427 Inst = TmpInst;
428 }
429 } else {
430 MCInst TmpInst;
431 TmpInst.setLoc(IDLoc);
432 TmpInst.setOpcode(Xtensa::L32R);
433 const MCExpr *Value = Inst.getOperand(1).getExpr();
435 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
436 TmpInst.addOperand(Inst.getOperand(0));
437 MCOperand Op1 = MCOperand::createExpr(Expr);
438 TmpInst.addOperand(Op1);
439 Inst = TmpInst;
440 TS.emitLiteral(Sym, Value, true, IDLoc);
441 }
442 break;
443 }
444 default:
445 break;
446 }
447
448 return true;
449}
450
451bool XtensaAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
452 OperandVector &Operands,
453 MCStreamer &Out,
455 bool MatchingInlineAsm) {
456 MCInst Inst;
457 auto Result =
458 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
459
460 switch (Result) {
461 default:
462 break;
463 case Match_Success:
464 processInstruction(Inst, IDLoc, Out, STI);
465 Inst.setLoc(IDLoc);
466 Out.emitInstruction(Inst, getSTI());
467 return false;
469 return Error(IDLoc, "instruction use requires an option to be enabled");
471 return Error(IDLoc, "unrecognized instruction mnemonic");
473 SMLoc ErrorLoc = IDLoc;
474 if (ErrorInfo != ~0U) {
475 if (ErrorInfo >= Operands.size())
476 return Error(ErrorLoc, "too few operands for instruction");
477
478 ErrorLoc = ((XtensaOperand &)*Operands[ErrorInfo]).getStartLoc();
479 if (ErrorLoc == SMLoc())
480 ErrorLoc = IDLoc;
481 }
482 return Error(ErrorLoc, "invalid operand for instruction");
483 }
484 case Match_InvalidImm8:
485 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
486 "expected immediate in range [-128, 127]");
487 case Match_InvalidImm8_sh8:
488 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
489 "expected immediate in range [-32768, 32512], first 8 bits "
490 "should be zero");
491 case Match_InvalidB4const:
492 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
493 "expected b4const immediate");
494 case Match_InvalidB4constu:
495 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
496 "expected b4constu immediate");
497 case Match_InvalidImm12:
498 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
499 "expected immediate in range [-2048, 2047]");
500 case Match_InvalidImm12m:
501 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
502 "expected immediate in range [-2048, 2047]");
503 case Match_InvalidImm1_16:
504 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
505 "expected immediate in range [1, 16]");
506 case Match_InvalidImm1n_15:
507 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
508 "expected immediate in range [-1, 15] except 0");
509 case Match_InvalidImm32n_95:
510 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
511 "expected immediate in range [-32, 95]");
512 case Match_InvalidImm64n_4n:
513 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
514 "expected immediate in range [-64, -4]");
515 case Match_InvalidImm8n_7:
516 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
517 "expected immediate in range [-8, 7]");
518 case Match_InvalidShimm1_31:
519 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
520 "expected immediate in range [1, 31]");
521 case Match_InvalidUimm4:
522 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
523 "expected immediate in range [0, 15]");
524 case Match_InvalidUimm5:
525 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
526 "expected immediate in range [0, 31]");
527 case Match_InvalidOffset8m8:
528 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
529 "expected immediate in range [0, 255]");
530 case Match_InvalidOffset8m16:
531 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
532 "expected immediate in range [0, 510], first bit "
533 "should be zero");
534 case Match_InvalidOffset8m32:
535 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
536 "expected immediate in range [0, 1020], first 2 bits "
537 "should be zero");
538 case Match_InvalidOffset4m32:
539 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
540 "expected immediate in range [0, 60], first 2 bits "
541 "should be zero");
542 case Match_Invalidentry_imm12:
543 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
544 "expected immediate in range [0, 32760], first 3 bits "
545 "should be zero");
546 case Match_Invalidimm7_22:
547 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
548 "expected immediate in range [7, 22]");
549 case Match_InvalidSelect_256:
550 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
551 "expected immediate in range [0, 255]");
552 }
553
554 report_fatal_error("Unknown match type detected!");
555}
556
557ParseStatus XtensaAsmParser::parsePCRelTarget(OperandVector &Operands) {
558 MCAsmParser &Parser = getParser();
559 LLVM_DEBUG(dbgs() << "parsePCRelTarget\n");
560
561 SMLoc S = getLexer().getLoc();
562
563 // Expressions are acceptable
564 const MCExpr *Expr = nullptr;
565 if (Parser.parseExpression(Expr)) {
566 // We have no way of knowing if a symbol was consumed so we must ParseFail
568 }
569
570 // Currently not support constants
571 if (Expr->getKind() == MCExpr::ExprKind::Constant)
572 return Error(getLoc(), "unknown operand");
573
574 Operands.push_back(XtensaOperand::createImm(Expr, S, getLexer().getLoc()));
576}
577
578bool XtensaAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
579 SMLoc &EndLoc) {
580 const AsmToken &Tok = getParser().getTok();
581 StartLoc = Tok.getLoc();
582 EndLoc = Tok.getEndLoc();
583 Reg = Xtensa::NoRegister;
584 StringRef Name = getLexer().getTok().getIdentifier();
585
586 if (!MatchRegisterName(Name) && !MatchRegisterAltName(Name)) {
587 getParser().Lex(); // Eat identifier token.
588 return false;
589 }
590
591 return Error(StartLoc, "invalid register name");
592}
593
594ParseStatus XtensaAsmParser::parseRegister(OperandVector &Operands,
595 bool AllowParens,
596 XtensaRegisterType RegType,
598 SMLoc FirstS = getLoc();
599 bool HadParens = false;
600 AsmToken Buf[2];
601 StringRef RegName;
602
603 // If this a parenthesised register name is allowed, parse it atomically
604 if (AllowParens && getLexer().is(AsmToken::LParen)) {
605 size_t ReadCount = getLexer().peekTokens(Buf);
606 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
607 if (Buf[0].getKind() == AsmToken::Integer && RegType == Xtensa_Generic)
609 HadParens = true;
610 getParser().Lex(); // Eat '('
611 }
612 }
613
614 MCRegister RegNo = 0;
615
616 switch (getLexer().getKind()) {
617 default:
620 if (RegType == Xtensa_Generic)
622
623 // Parse case when we expect UR register code as special case,
624 // because SR and UR registers may have the same number
625 // and such situation may lead to confilct
626 if (RegType == Xtensa_UR) {
627 int64_t RegCode = getLexer().getTok().getIntVal();
628 RegNo = Xtensa::getUserRegister(RegCode, MRI);
629 } else {
632 }
633 break;
636 RegNo = MatchRegisterName(RegName);
637 if (RegNo == 0)
639 break;
640 }
641
642 if (RegNo == 0) {
643 if (HadParens)
644 getLexer().UnLex(Buf[0]);
646 }
647
648 if (!Xtensa::checkRegister(RegNo, getSTI().getFeatureBits(), RAType))
650
651 if (HadParens)
652 Operands.push_back(XtensaOperand::createToken("(", FirstS));
653 SMLoc S = getLoc();
654 SMLoc E = getParser().getTok().getEndLoc();
655 getLexer().Lex();
656 Operands.push_back(XtensaOperand::createReg(RegNo, S, E));
657
658 if (HadParens) {
659 getParser().Lex(); // Eat ')'
660 Operands.push_back(XtensaOperand::createToken(")", getLoc()));
661 }
662
664}
665
666ParseStatus XtensaAsmParser::parseImmediate(OperandVector &Operands) {
667 SMLoc S = getLoc();
668 SMLoc E;
669 const MCExpr *Res;
670
671 switch (getLexer().getKind()) {
672 default:
674 case AsmToken::LParen:
675 case AsmToken::Minus:
676 case AsmToken::Plus:
677 case AsmToken::Tilde:
679 case AsmToken::String:
680 if (getParser().parseExpression(Res))
682 break;
684 if (getParser().parseExpression(Res))
686 break;
687 }
689 return parseOperandWithModifier(Operands);
690 }
691
693 Operands.push_back(XtensaOperand::createImm(Res, S, E));
695}
696
697ParseStatus XtensaAsmParser::parseOperandWithModifier(OperandVector &Operands) {
699}
700
701/// Looks at a token type and creates the relevant operand
702/// from this information, adding to Operands.
703/// If operand was parsed, returns false, else true.
704bool XtensaAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic,
705 XtensaRegisterType RegType,
707 // Check if the current operand has a custom associated parser, if so, try to
708 // custom parse the operand, or fallback to the general approach.
709 ParseStatus Res = MatchOperandParserImpl(Operands, Mnemonic);
710 if (Res.isSuccess())
711 return false;
712
713 // If there wasn't a custom match, try the generic matcher below. Otherwise,
714 // there was a match, but an error occurred, in which case, just return that
715 // the operand parsing failed.
716 if (Res.isFailure())
717 return true;
718
719 // Attempt to parse token as register
720 if (parseRegister(Operands, true, RegType, RAType).isSuccess())
721 return false;
722
723 // Attempt to parse token as an immediate
724 if (parseImmediate(Operands).isSuccess())
725 return false;
726
727 // Finally we have exhausted all options and must declare defeat.
728 return Error(getLoc(), "unknown operand");
729}
730
731bool XtensaAsmParser::ParseInstructionWithSR(ParseInstructionInfo &Info,
732 StringRef Name, SMLoc NameLoc,
733 OperandVector &Operands) {
735 Name[0] == 'w' ? Xtensa::REGISTER_WRITE
736 : (Name[0] == 'r' ? Xtensa::REGISTER_READ
738
739 if ((Name.size() > 4) && Name[3] == '.') {
740 // Parse case when instruction name is concatenated with SR/UR register
741 // name, like "wsr.sar a1" or "wur.fcr a1"
742
743 // First operand is token for instruction
744 Operands.push_back(XtensaOperand::createToken(Name.take_front(3), NameLoc));
745
746 StringRef RegName = Name.drop_front(4);
747 unsigned RegNo = MatchRegisterName(RegName);
748
749 if (RegNo == 0)
751
752 if (!Xtensa::checkRegister(RegNo, getSTI().getFeatureBits(), RAType))
753 return Error(NameLoc, "invalid register name");
754
755 // Parse operand
756 if (parseOperand(Operands, Name))
757 return true;
758
759 SMLoc S = getLoc();
760 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
761 Operands.push_back(XtensaOperand::createReg(RegNo, S, E));
762 } else {
763 // First operand is token for instruction
764 Operands.push_back(XtensaOperand::createToken(Name, NameLoc));
765
766 // Parse first operand
767 if (parseOperand(Operands, Name))
768 return true;
769
771 SMLoc Loc = getLexer().getLoc();
773 return Error(Loc, "unexpected token");
774 }
775
776 // Parse second operand
777 if (parseOperand(Operands, Name, Name[1] == 's' ? Xtensa_SR : Xtensa_UR,
778 RAType))
779 return true;
780 }
781
783 SMLoc Loc = getLexer().getLoc();
785 return Error(Loc, "unexpected token");
786 }
787
788 getParser().Lex(); // Consume the EndOfStatement.
789 return false;
790}
791
792bool XtensaAsmParser::parseInstruction(ParseInstructionInfo &Info,
793 StringRef Name, SMLoc NameLoc,
794 OperandVector &Operands) {
795 if (Name.starts_with("wsr") || Name.starts_with("rsr") ||
796 Name.starts_with("xsr") || Name.starts_with("rur") ||
797 Name.starts_with("wur")) {
798 return ParseInstructionWithSR(Info, Name, NameLoc, Operands);
799 }
800
801 // First operand is token for instruction
802 Operands.push_back(XtensaOperand::createToken(Name, NameLoc));
803
804 // If there are no more operands, then finish
806 return false;
807
808 // Parse first operand
809 if (parseOperand(Operands, Name))
810 return true;
811
812 // Parse until end of statement, consuming commas between operands
814 if (parseOperand(Operands, Name))
815 return true;
816
818 SMLoc Loc = getLexer().getLoc();
820 return Error(Loc, "unexpected token");
821 }
822
823 getParser().Lex(); // Consume the EndOfStatement.
824 return false;
825}
826
827bool XtensaAsmParser::parseLiteralDirective(SMLoc L) {
828 MCAsmParser &Parser = getParser();
829 const MCExpr *Value;
830 SMLoc LiteralLoc = getLexer().getLoc();
831 XtensaTargetStreamer &TS = this->getTargetStreamer();
832
833 if (Parser.parseExpression(Value))
834 return true;
835
836 const MCSymbolRefExpr *SE = dyn_cast<MCSymbolRefExpr>(Value);
837
838 if (!SE)
839 return Error(LiteralLoc, "literal label must be a symbol");
840
841 if (Parser.parseComma())
842 return true;
843
844 SMLoc OpcodeLoc = getLexer().getLoc();
846 return Error(OpcodeLoc, "expected value");
847
848 if (Parser.parseExpression(Value))
849 return true;
850
851 if (parseEOL())
852 return true;
853
855
856 TS.emitLiteral(Sym, Value, true, LiteralLoc);
857
858 return false;
859}
860
861ParseStatus XtensaAsmParser::parseDirective(AsmToken DirectiveID) {
862 StringRef IDVal = DirectiveID.getString();
863 SMLoc Loc = getLexer().getLoc();
864
865 if (IDVal == ".literal_position") {
866 XtensaTargetStreamer &TS = this->getTargetStreamer();
868 return parseEOL();
869 }
870
871 if (IDVal == ".literal") {
872 return parseLiteralDirective(Loc);
873 }
874
876}
877
878// Force static initialization.
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
Register Reg
This file contains some templates that are useful if you are working with the STL at all.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define LLVM_DEBUG(...)
Definition Debug.h:119
bool parseImmediate(MCInst &MI, uint64_t &Size, ArrayRef< uint8_t > Bytes)
static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue)
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaAsmParser()
static SMLoc RefineErrorLoc(const SMLoc Loc, const OperandVector &Operands, uint64_t ErrorInfo)
XtensaAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII)
bool hasWindowed() const
SMLoc getLoc() const
Get the current source location.
Definition AsmLexer.h:115
void UnLex(AsmToken const &Token)
Definition AsmLexer.h:106
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition AsmLexer.h:118
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition AsmLexer.h:92
LLVM_ABI size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)
Look ahead an arbitrary number of tokens.
Definition AsmLexer.cpp:768
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
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
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
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:66
void printExpr(raw_ostream &, const MCExpr &) const
bool parseOptionalToken(AsmToken::TokenKind T)
Generic assembler parser interface, for use by target specific assembly parsers.
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
MCStreamer & getStreamer()
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
ExprKind getKind() const
Definition MCExpr.h:85
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
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
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:210
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
void setExpr(const MCExpr *Val)
Definition MCInst.h:123
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
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.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Streaming machine code generation interface.
Definition MCStreamer.h:222
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
MCTargetStreamer * getTargetStreamer()
Definition MCStreamer.h:336
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
const MCSymbol & getSymbol() const
Definition MCExpr.h:227
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
const MCInstrInfo & MII
MCTargetAsmParser(const MCSubtargetInfo &STI, const MCInstrInfo &MII)
void setAvailableFeatures(const FeatureBitset &Value)
const MCSubtargetInfo & getSTI() const
const MCSubtargetInfo * STI
Current STI.
Target specific streamer interface.
Definition MCStreamer.h:95
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
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)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
virtual void emitLiteralPosition()=0
virtual void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, bool SwitchLiteralSection, SMLoc L=SMLoc())=0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
MCExpr const & getExpr(MCExpr const &Expr)
bool checkRegister(MCRegister RegNo, const FeatureBitset &FeatureBits, RegisterAccessType RA)
MCRegister getUserRegister(unsigned Code, const MCRegisterInfo &MRI)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
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
Target & getTheXtensaTarget()
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
#define N
bool isImm12() const
bool isOffset4m32() const
bool isOffset8m16() const
static std::unique_ptr< XtensaOperand > createToken(StringRef Str, SMLoc S)
bool isImm8_sh8() const
void addRegOperands(MCInst &Inst, unsigned N) const
void addExpr(MCInst &Inst, const MCExpr *Expr) const
void addImmOperands(MCInst &Inst, unsigned N) const
StringRef getToken() const
enum XtensaOperand::KindTy Kind
bool isMem() const override
isMem - Is this a memory operand?
bool isImm16_31() const
bool isImm8n_7() const
bool isToken() const override
isToken - Is this a token operand?
bool isImm1n_15() const
MCRegister getReg() const override
SMLoc getStartLoc() const override
getStartLoc - Gets location of the first token of this operand
bool isSelect_256() const
bool isImm8() const
bool isImm(int64_t MinValue, int64_t MaxValue) const
bool isImm12m() const
bool isReg() const override
isReg - Is this a register operand?
XtensaOperand(KindTy K)
bool isB4constu() const
bool isImm64n_4n() const
bool isImm() const override
isImm - Is this an immediate operand?
bool isUimm4() const
bool isentry_imm12() const
static std::unique_ptr< XtensaOperand > createReg(unsigned RegNo, SMLoc S, SMLoc E)
bool isimm7_22() const
SMLoc getEndLoc() const override
getEndLoc - Gets location of the last token of this operand
bool isImm32n_95() const
const MCExpr * getImm() const
bool isUimm5() const
bool isOffset8m8() const
void print(raw_ostream &OS, const MCAsmInfo &MAI) const override
print - Print a debug representation of the operand to the given stream.
XtensaOperand(const XtensaOperand &o)
bool isImm1_16() const
bool isB4const() const
static std::unique_ptr< XtensaOperand > createImm(const MCExpr *Val, SMLoc S, SMLoc E)
bool isOffset8m32() const
bool isShimm1_31() const
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...