LLVM 24.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() {
44 assert(getParser().getStreamer().getTargetStreamer() &&
45 "do not have a target streamer");
47 return static_cast<XtensaTargetStreamer &>(TS);
48 }
49
50 ParseStatus parseDirective(AsmToken DirectiveID) override;
51 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
52 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
53 SMLoc NameLoc, OperandVector &Operands) override;
54 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
56 uint64_t &ErrorInfo,
57 bool MatchingInlineAsm) 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
66 MCRegister getRegisterByName(StringRef RegName);
69 parseRegister(OperandVector &Operands, bool AllowParens = false,
70 XtensaRegisterType SR = Xtensa_Generic,
72 ParseStatus parseOperandWithModifier(OperandVector &Operands);
73 bool
74 parseOperand(OperandVector &Operands, StringRef Mnemonic,
75 XtensaRegisterType SR = Xtensa_Generic,
77 bool ParseInstructionWithSR(ParseInstructionInfo &Info, StringRef Name,
78 SMLoc NameLoc, OperandVector &Operands);
79 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
80 SMLoc &EndLoc) override {
82 }
83
84 ParseStatus parsePCRelTarget(OperandVector &Operands);
85 bool parseLiteralDirective(SMLoc L);
86
87public:
90#define GET_OPERAND_DIAGNOSTIC_TYPES
91#include "XtensaGenAsmMatcher.inc"
92#undef GET_OPERAND_DIAGNOSTIC_TYPES
93 };
94
96 const MCInstrInfo &MII)
98 MRI(*Parser.getContext().getRegisterInfo()) {
99 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
100 }
101
102 bool hasWindowed() const {
103 return getSTI().getFeatureBits()[Xtensa::FeatureWindowed];
104 };
105};
106
107// Return true if Expr is in the range [MinValue, MaxValue].
108static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
109 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
110 int64_t Value = CE->getValue();
111 return Value >= MinValue && Value <= MaxValue;
112 }
113 return false;
114}
115
117
123
124 struct RegOp {
125 unsigned RegNum;
126 };
127
128 struct ImmOp {
129 const MCExpr *Val;
130 };
131
133 union {
137 };
138
140
141public:
143 Kind = o.Kind;
144 StartLoc = o.StartLoc;
145 EndLoc = o.EndLoc;
146 switch (Kind) {
147 case Register:
148 Reg = o.Reg;
149 break;
150 case Immediate:
151 Imm = o.Imm;
152 break;
153 case Token:
154 Tok = o.Tok;
155 break;
156 }
157 }
158
159 bool isToken() const override { return Kind == Token; }
160 bool isReg() const override { return Kind == Register; }
161 bool isImm() const override { return Kind == Immediate; }
162 bool isMem() const override { return false; }
163
164 bool isImm(int64_t MinValue, int64_t MaxValue) const {
165 return Kind == Immediate && inRange(getImm(), MinValue, MaxValue);
166 }
167
168 bool isImm8() const { return isImm(-128, 127); }
169
170 bool isImm8_sh8() const {
171 return isImm(-32768, 32512) &&
172 ((cast<MCConstantExpr>(getImm())->getValue() & 0xFF) == 0);
173 }
174
175 bool isImm12() const { return isImm(-2048, 2047); }
176
177 // Convert MOVI to literal load, when immediate is not in range (-2048, 2047)
178 bool isImm12m() const { return Kind == Immediate; }
179
180 bool isOffset4m32() const {
181 return isImm(0, 60) &&
182 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
183 }
184
185 bool isOffset8m8() const { return isImm(0, 255); }
186
187 bool isOffset8m16() const {
188 return isImm(0, 510) &&
189 ((cast<MCConstantExpr>(getImm())->getValue() & 0x1) == 0);
190 }
191
192 bool isOffset8m32() const {
193 return isImm(0, 1020) &&
194 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
195 }
196
197 bool isentry_imm12() const {
198 return isImm(0, 32760) &&
199 ((cast<MCConstantExpr>(getImm())->getValue() % 8) == 0);
200 }
201
202 bool isUimm4() const { return isImm(0, 15); }
203
204 bool isUimm5() const { return isImm(0, 31); }
205
206 bool isImm8n_7() const { return isImm(-8, 7); }
207
208 bool isShimm1_31() const { return isImm(1, 31); }
209
210 bool isImm16_31() const { return isImm(16, 31); }
211
212 bool isImm1_16() const { return isImm(1, 16); }
213
214 // Check that value is either equals (-1) or from [1,15] range.
215 bool isImm1n_15() const { return isImm(1, 15) || isImm(-1, -1); }
216
217 bool isImm32n_95() const { return isImm(-32, 95); }
218
219 bool isImm64n_4n() const {
220 return isImm(-64, -4) &&
221 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
222 }
223
224 bool isB4const() const {
225 if (Kind != Immediate)
226 return false;
227 if (auto *CE = dyn_cast<MCConstantExpr>(getImm())) {
228 int64_t Value = CE->getValue();
229 switch (Value) {
230 case -1:
231 case 1:
232 case 2:
233 case 3:
234 case 4:
235 case 5:
236 case 6:
237 case 7:
238 case 8:
239 case 10:
240 case 12:
241 case 16:
242 case 32:
243 case 64:
244 case 128:
245 case 256:
246 return true;
247 default:
248 return false;
249 }
250 }
251 return false;
252 }
253
254 bool isB4constu() const {
255 if (Kind != Immediate)
256 return false;
257 if (auto *CE = dyn_cast<MCConstantExpr>(getImm())) {
258 int64_t Value = CE->getValue();
259 switch (Value) {
260 case 32768:
261 case 65536:
262 case 2:
263 case 3:
264 case 4:
265 case 5:
266 case 6:
267 case 7:
268 case 8:
269 case 10:
270 case 12:
271 case 16:
272 case 32:
273 case 64:
274 case 128:
275 case 256:
276 return true;
277 default:
278 return false;
279 }
280 }
281 return false;
282 }
283
284 bool isimm7_22() const { return isImm(7, 22); }
285
286 bool isSelect_2() const { return isImm(0, 1); }
287
288 bool isSelect_4() const { return isImm(0, 3); }
289
290 bool isSelect_8() const { return isImm(0, 7); }
291
292 bool isSelect_16() const { return isImm(0, 15); }
293
294 bool isSelect_256() const { return isImm(0, 255); }
295
296 bool isOffset_16_16() const {
297 return isImm(-128, 112) &&
298 ((cast<MCConstantExpr>(getImm())->getValue() & 0xf) == 0);
299 }
300
301 bool isOffset_256_8() const {
302 return isImm(-1024, 1016) &&
303 ((cast<MCConstantExpr>(getImm())->getValue() & 0x7) == 0);
304 }
305
306 bool isOffset_256_16() const {
307 return isImm(-2048, 2032) &&
308 ((cast<MCConstantExpr>(getImm())->getValue() & 0xf) == 0);
309 }
310
311 bool isOffset_256_4() const {
312 return isImm(-512, 508) &&
313 ((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
314 }
315
316 bool isOffset_128_2() const {
317 return isImm(0, 254) &&
318 ((cast<MCConstantExpr>(getImm())->getValue() & 0x1) == 0);
319 }
320
321 bool isOffset_128_1() const { return isImm(0, 127); }
322
323 bool isOffset_64_16() const {
324 return isImm(-512, 496) &&
325 ((cast<MCConstantExpr>(getImm())->getValue() & 0xf) == 0);
326 }
327
328 /// getStartLoc - Gets location of the first token of this operand
329 SMLoc getStartLoc() const override { return StartLoc; }
330 /// getEndLoc - Gets location of the last token of this operand
331 SMLoc getEndLoc() const override { return EndLoc; }
332
333 MCRegister getReg() const override {
334 assert(Kind == Register && "Invalid type access!");
335 return Reg.RegNum;
336 }
337
338 const MCExpr *getImm() const {
339 assert(Kind == Immediate && "Invalid type access!");
340 return Imm.Val;
341 }
342
344 assert(Kind == Token && "Invalid type access!");
345 return Tok;
346 }
347
348 void print(raw_ostream &OS, const MCAsmInfo &MAI) const override {
349 switch (Kind) {
350 case Immediate:
351 MAI.printExpr(OS, *getImm());
352 break;
353 case Register:
354 OS << "<register x";
355 OS << getReg() << ">";
356 break;
357 case Token:
358 OS << "'" << getToken() << "'";
359 break;
360 }
361 }
362
363 static std::unique_ptr<XtensaOperand> createToken(StringRef Str, SMLoc S) {
364 auto Op = std::make_unique<XtensaOperand>(Token);
365 Op->Tok = Str;
366 Op->StartLoc = S;
367 Op->EndLoc = S;
368 return Op;
369 }
370
371 static std::unique_ptr<XtensaOperand> createReg(unsigned RegNo, SMLoc S,
372 SMLoc E) {
373 auto Op = std::make_unique<XtensaOperand>(Register);
374 Op->Reg.RegNum = RegNo;
375 Op->StartLoc = S;
376 Op->EndLoc = E;
377 return Op;
378 }
379
380 static std::unique_ptr<XtensaOperand> createImm(const MCExpr *Val, SMLoc S,
381 SMLoc E) {
382 auto Op = std::make_unique<XtensaOperand>(Immediate);
383 Op->Imm.Val = Val;
384 Op->StartLoc = S;
385 Op->EndLoc = E;
386 return Op;
387 }
388
389 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
390 assert(Expr && "Expr shouldn't be null!");
391 int64_t Imm = 0;
392 bool IsConstant = false;
393
394 if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
395 IsConstant = true;
396 Imm = CE->getValue();
397 }
398
399 if (IsConstant)
401 else
403 }
404
405 // Used by the TableGen Code
406 void addRegOperands(MCInst &Inst, unsigned N) const {
407 assert(N == 1 && "Invalid number of operands!");
409 }
410
411 void addImmOperands(MCInst &Inst, unsigned N) const {
412 assert(N == 1 && "Invalid number of operands!");
413 addExpr(Inst, getImm());
414 }
415};
416
417#define GET_REGISTER_MATCHER
418#define GET_MATCHER_IMPLEMENTATION
419#include "XtensaGenAsmMatcher.inc"
420
423 if (ErrorInfo != ~0ULL && ErrorInfo < Operands.size()) {
424 SMLoc ErrorLoc = Operands[ErrorInfo]->getStartLoc();
425 if (ErrorLoc == SMLoc())
426 return Loc;
427 return ErrorLoc;
428 }
429 return Loc;
430}
431
432bool XtensaAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
433 MCStreamer &Out,
434 const MCSubtargetInfo *STI) {
435 Inst.setLoc(IDLoc);
436 const unsigned Opcode = Inst.getOpcode();
437 switch (Opcode) {
438 case Xtensa::L32R: {
439 const MCSymbolRefExpr *OpExpr =
440 static_cast<const MCSymbolRefExpr *>(Inst.getOperand(1).getExpr());
441 Inst.getOperand(1).setExpr(OpExpr);
442 break;
443 }
444 case Xtensa::MOVI: {
445 XtensaTargetStreamer &TS = this->getTargetStreamer();
446
447 // Expand MOVI operand
448 if (!Inst.getOperand(1).isExpr()) {
449 uint64_t ImmOp64 = Inst.getOperand(1).getImm();
450 int32_t Imm = ImmOp64;
451 if (!isInt<12>(Imm)) {
452 XtensaTargetStreamer &TS = this->getTargetStreamer();
453 MCInst TmpInst;
454 TmpInst.setLoc(IDLoc);
455 TmpInst.setOpcode(Xtensa::L32R);
456 const MCExpr *Value = MCConstantExpr::create(ImmOp64, getContext());
458 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
459 TmpInst.addOperand(Inst.getOperand(0));
460 MCOperand Op1 = MCOperand::createExpr(Expr);
461 TmpInst.addOperand(Op1);
462 TS.emitLiteral(Sym, Value, true, IDLoc);
463 Inst = TmpInst;
464 }
465 } else {
466 MCInst TmpInst;
467 TmpInst.setLoc(IDLoc);
468 TmpInst.setOpcode(Xtensa::L32R);
469 const MCExpr *Value = Inst.getOperand(1).getExpr();
471 const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
472 TmpInst.addOperand(Inst.getOperand(0));
473 MCOperand Op1 = MCOperand::createExpr(Expr);
474 TmpInst.addOperand(Op1);
475 Inst = TmpInst;
476 TS.emitLiteral(Sym, Value, true, IDLoc);
477 }
478 break;
479 }
480 default:
481 break;
482 }
483
484 return true;
485}
486
487bool XtensaAsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
489 MCStreamer &Out,
491 bool MatchingInlineAsm) {
492 MCInst Inst;
493 auto Result =
494 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
495
496 switch (Result) {
497 default:
498 break;
499 case Match_Success:
500 processInstruction(Inst, IDLoc, Out, STI);
501 Inst.setLoc(IDLoc);
502 Out.emitInstruction(Inst, getSTI());
503 return false;
505 return Error(IDLoc, "instruction use requires an option to be enabled");
507 return Error(IDLoc, "unrecognized instruction mnemonic");
509 SMLoc ErrorLoc = IDLoc;
510 if (ErrorInfo != ~0U) {
511 if (ErrorInfo >= Operands.size())
512 return Error(ErrorLoc, "too few operands for instruction");
513
514 ErrorLoc = ((XtensaOperand &)*Operands[ErrorInfo]).getStartLoc();
515 if (ErrorLoc == SMLoc())
516 ErrorLoc = IDLoc;
517 }
518 return Error(ErrorLoc, "invalid operand for instruction");
519 }
520 case Match_InvalidImm8:
521 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
522 "expected immediate in range [-128, 127]");
523 case Match_InvalidImm8_sh8:
524 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
525 "expected immediate in range [-32768, 32512], first 8 bits "
526 "should be zero");
527 case Match_InvalidB4const:
528 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
529 "expected b4const immediate");
530 case Match_InvalidB4constu:
531 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
532 "expected b4constu immediate");
533 case Match_InvalidImm12:
534 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
535 "expected immediate in range [-2048, 2047]");
536 case Match_InvalidImm12m:
537 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
538 "expected immediate in range [-2048, 2047]");
539 case Match_InvalidImm1_16:
540 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
541 "expected immediate in range [1, 16]");
542 case Match_InvalidImm1n_15:
543 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
544 "expected immediate in range [-1, 15] except 0");
545 case Match_InvalidImm32n_95:
546 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
547 "expected immediate in range [-32, 95]");
548 case Match_InvalidImm64n_4n:
549 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
550 "expected immediate in range [-64, -4]");
551 case Match_InvalidImm8n_7:
552 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
553 "expected immediate in range [-8, 7]");
554 case Match_InvalidShimm1_31:
555 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
556 "expected immediate in range [1, 31]");
557 case Match_InvalidUimm4:
558 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
559 "expected immediate in range [0, 15]");
560 case Match_InvalidUimm5:
561 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
562 "expected immediate in range [0, 31]");
563 case Match_InvalidOffset8m8:
564 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
565 "expected immediate in range [0, 255]");
566 case Match_InvalidOffset8m16:
567 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
568 "expected immediate in range [0, 510], first bit "
569 "should be zero");
570 case Match_InvalidOffset8m32:
571 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
572 "expected immediate in range [0, 1020], first 2 bits "
573 "should be zero");
574 case Match_InvalidOffset4m32:
575 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
576 "expected immediate in range [0, 60], first 2 bits "
577 "should be zero");
578 case Match_Invalidentry_imm12:
579 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
580 "expected immediate in range [0, 32760], first 3 bits "
581 "should be zero");
582 case Match_Invalidimm7_22:
583 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
584 "expected immediate in range [7, 22]");
585 case Match_InvalidSelect_2:
586 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
587 "expected immediate in range [0, 1]");
588 case Match_InvalidSelect_4:
589 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
590 "expected immediate in range [0, 3]");
591 case Match_InvalidSelect_8:
592 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
593 "expected immediate in range [0, 7]");
594 case Match_InvalidSelect_16:
595 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
596 "expected immediate in range [0, 15]");
597 case Match_InvalidSelect_256:
598 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
599 "expected immediate in range [0, 255]");
600 case Match_InvalidOffset_16_16:
601 return Error(
602 RefineErrorLoc(IDLoc, Operands, ErrorInfo),
603 "expected immediate in range [-128, 112], first 4 bits should be zero");
604 case Match_InvalidOffset_256_8:
605 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
606 "expected immediate in range [-1024, 1016], first 3 bits "
607 "should be zero");
608 case Match_InvalidOffset_256_16:
609 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
610 "expected immediate in range [-2048, 2032], first 4 bits "
611 "should be zero");
612 case Match_InvalidOffset_256_4:
613 return Error(
614 RefineErrorLoc(IDLoc, Operands, ErrorInfo),
615 "expected immediate in range [-512, 508], first 2 bits should be zero");
616 case Match_InvalidOffset_128_2:
617 return Error(
618 RefineErrorLoc(IDLoc, Operands, ErrorInfo),
619 "expected immediate in range [0, 254], first bit should be zero");
620 case Match_InvalidOffset_128_1:
621 return Error(RefineErrorLoc(IDLoc, Operands, ErrorInfo),
622 "expected immediate in range [0, 127]");
623 case Match_InvalidOffset_64_16:
624 return Error(
625 RefineErrorLoc(IDLoc, Operands, ErrorInfo),
626 "expected immediate in range [-512, 496], first 4 bits should be zero");
627 }
628
629 report_fatal_error("Unknown match type detected!");
630}
631
632ParseStatus XtensaAsmParser::parsePCRelTarget(OperandVector &Operands) {
633 MCAsmParser &Parser = getParser();
634 LLVM_DEBUG(dbgs() << "parsePCRelTarget\n");
635
636 SMLoc S = getLexer().getLoc();
637
638 // Expressions are acceptable
639 const MCExpr *Expr = nullptr;
640 if (Parser.parseExpression(Expr)) {
641 // We have no way of knowing if a symbol was consumed so we must ParseFail
643 }
644
645 // Currently not support constants
646 if (Expr->getKind() == MCExpr::ExprKind::Constant)
647 return Error(getLoc(), "unknown operand");
648
649 Operands.push_back(XtensaOperand::createImm(Expr, S, getLexer().getLoc()));
651}
652
653MCRegister XtensaAsmParser::getRegisterByName(StringRef RegName) {
654 MCRegister RegNo = MatchRegisterName(RegName);
655 if (RegNo == 0)
657
658 if (RegNo == Xtensa::GPIO_OUT_S2 &&
659 getSTI().getFeatureBits()[Xtensa::FeatureESP32S3Ops])
660 RegNo = Xtensa::GPIO_OUT_S3;
661
662 return RegNo;
663}
664
665bool XtensaAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
666 SMLoc &EndLoc) {
667 const AsmToken &Tok = getParser().getTok();
668 StartLoc = Tok.getLoc();
669 EndLoc = Tok.getEndLoc();
670 Reg = Xtensa::NoRegister;
671 StringRef Name = getLexer().getTok().getIdentifier();
672
673 if (!MatchRegisterName(Name) && !MatchRegisterAltName(Name)) {
674 getParser().Lex(); // Eat identifier token.
675 return false;
676 }
677
678 return Error(StartLoc, "invalid register name");
679}
680
681ParseStatus XtensaAsmParser::parseRegister(OperandVector &Operands,
682 bool AllowParens,
683 XtensaRegisterType RegType,
685 SMLoc FirstS = getLoc();
686 bool HadParens = false;
687 AsmToken Buf[2];
688 StringRef RegName;
689
690 // If this a parenthesised register name is allowed, parse it atomically
691 if (AllowParens && getLexer().is(AsmToken::LParen)) {
692 size_t ReadCount = getLexer().peekTokens(Buf);
693 if (ReadCount == 2 && Buf[1].getKind() == AsmToken::RParen) {
694 if (Buf[0].getKind() == AsmToken::Integer && RegType == Xtensa_Generic)
696 HadParens = true;
697 getParser().Lex(); // Eat '('
698 }
699 }
700
701 MCRegister RegNo = 0;
702
703 switch (getLexer().getKind()) {
704 default:
707 if (RegType == Xtensa_Generic)
709
710 // Parse case when we expect UR register code as special case,
711 // because SR and UR registers may have the same number
712 // and such situation may lead to confilct
713 if (RegType == Xtensa_UR) {
714 int64_t RegCode = getLexer().getTok().getIntVal();
715 RegNo = Xtensa::getUserRegister(RegCode, MRI);
716 } else {
719 }
720 break;
723 RegNo = getRegisterByName(RegName);
724 break;
725 }
726
727 if (RegNo == 0) {
728 if (HadParens)
729 getLexer().UnLex(Buf[0]);
731 }
732
733 if (!Xtensa::checkRegister(RegNo, getSTI().getFeatureBits(), RAType))
735
736 if (HadParens)
737 Operands.push_back(XtensaOperand::createToken("(", FirstS));
738 SMLoc S = getLoc();
739 SMLoc E = getParser().getTok().getEndLoc();
740 getLexer().Lex();
741 Operands.push_back(XtensaOperand::createReg(RegNo, S, E));
742
743 if (HadParens) {
744 getParser().Lex(); // Eat ')'
745 Operands.push_back(XtensaOperand::createToken(")", getLoc()));
746 }
747
749}
750
751ParseStatus XtensaAsmParser::parseImmediate(OperandVector &Operands) {
752 SMLoc S = getLoc();
753 SMLoc E;
754 const MCExpr *Res;
755
756 switch (getLexer().getKind()) {
757 default:
759 case AsmToken::LParen:
760 case AsmToken::Minus:
761 case AsmToken::Plus:
762 case AsmToken::Tilde:
764 case AsmToken::String:
765 if (getParser().parseExpression(Res))
767 break;
769 if (getParser().parseExpression(Res))
771 break;
772 }
774 return parseOperandWithModifier(Operands);
775 }
776
778 Operands.push_back(XtensaOperand::createImm(Res, S, E));
780}
781
782ParseStatus XtensaAsmParser::parseOperandWithModifier(OperandVector &Operands) {
784}
785
786/// Looks at a token type and creates the relevant operand
787/// from this information, adding to Operands.
788/// If operand was parsed, returns false, else true.
789bool XtensaAsmParser::parseOperand(OperandVector &Operands, StringRef Mnemonic,
790 XtensaRegisterType RegType,
792 // Check if the current operand has a custom associated parser, if so, try to
793 // custom parse the operand, or fallback to the general approach.
794 ParseStatus Res = MatchOperandParserImpl(Operands, Mnemonic);
795 if (Res.isSuccess())
796 return false;
797
798 // If there wasn't a custom match, try the generic matcher below. Otherwise,
799 // there was a match, but an error occurred, in which case, just return that
800 // the operand parsing failed.
801 if (Res.isFailure())
802 return true;
803
804 // Attempt to parse token as register
805 if (parseRegister(Operands, true, RegType, RAType).isSuccess())
806 return false;
807
808 // Attempt to parse token as an immediate
809 if (parseImmediate(Operands).isSuccess())
810 return false;
811
812 // Finally we have exhausted all options and must declare defeat.
813 return Error(getLoc(), "unknown operand");
814}
815
816bool XtensaAsmParser::ParseInstructionWithSR(ParseInstructionInfo &Info,
817 StringRef Name, SMLoc NameLoc,
820 Name[0] == 'w' ? Xtensa::REGISTER_WRITE
821 : (Name[0] == 'r' ? Xtensa::REGISTER_READ
823
824 if ((Name.size() > 4) && Name[3] == '.') {
825 // Parse case when instruction name is concatenated with SR/UR register
826 // name, like "wsr.sar a1" or "wur.fcr a1"
827
828 // First operand is token for instruction
829 Operands.push_back(XtensaOperand::createToken(Name.take_front(3), NameLoc));
830
831 StringRef RegName = Name.drop_front(4);
832 MCRegister RegNo = getRegisterByName(RegName);
833
834 if (!Xtensa::checkRegister(RegNo, getSTI().getFeatureBits(), RAType))
835 return Error(NameLoc, "invalid register name");
836
837 // Parse operand
838 if (parseOperand(Operands, Name))
839 return true;
840
841 SMLoc S = getLoc();
842 SMLoc E = SMLoc::getFromPointer(S.getPointer() - 1);
843 Operands.push_back(XtensaOperand::createReg(RegNo, S, E));
844 } else {
845 // First operand is token for instruction
846 Operands.push_back(XtensaOperand::createToken(Name, NameLoc));
847
848 // Parse first operand
849 if (parseOperand(Operands, Name))
850 return true;
851
853 SMLoc Loc = getLexer().getLoc();
855 return Error(Loc, "unexpected token");
856 }
857
858 // Parse second operand
859 if (parseOperand(Operands, Name, Name[1] == 's' ? Xtensa_SR : Xtensa_UR,
860 RAType))
861 return true;
862 }
863
865 SMLoc Loc = getLexer().getLoc();
867 return Error(Loc, "unexpected token");
868 }
869
870 getParser().Lex(); // Consume the EndOfStatement.
871 return false;
872}
873
874bool XtensaAsmParser::parseInstruction(ParseInstructionInfo &Info,
875 StringRef Name, SMLoc NameLoc,
877 if (Name.starts_with("wsr") || Name.starts_with("rsr") ||
878 Name.starts_with("xsr") || Name.starts_with("rur") ||
879 Name.starts_with("wur")) {
880 return ParseInstructionWithSR(Info, Name, NameLoc, Operands);
881 }
882
883 // First operand is token for instruction
884 Operands.push_back(XtensaOperand::createToken(Name, NameLoc));
885
886 // If there are no more operands, then finish
888 return false;
889
890 // Parse first operand
891 if (parseOperand(Operands, Name))
892 return true;
893
894 // Parse until end of statement, consuming commas between operands
896 if (parseOperand(Operands, Name))
897 return true;
898
900 SMLoc Loc = getLexer().getLoc();
902 return Error(Loc, "unexpected token");
903 }
904
905 getParser().Lex(); // Consume the EndOfStatement.
906 return false;
907}
908
909bool XtensaAsmParser::parseLiteralDirective(SMLoc L) {
910 MCAsmParser &Parser = getParser();
911 const MCExpr *Value;
912 SMLoc LiteralLoc = getLexer().getLoc();
913 XtensaTargetStreamer &TS = this->getTargetStreamer();
914
915 if (Parser.parseExpression(Value))
916 return true;
917
918 const MCSymbolRefExpr *SE = dyn_cast<MCSymbolRefExpr>(Value);
919
920 if (!SE)
921 return Error(LiteralLoc, "literal label must be a symbol");
922
923 if (Parser.parseComma())
924 return true;
925
926 SMLoc OpcodeLoc = getLexer().getLoc();
928 return Error(OpcodeLoc, "expected value");
929
930 if (Parser.parseExpression(Value))
931 return true;
932
933 if (parseEOL())
934 return true;
935
937
938 TS.emitLiteral(Sym, Value, true, LiteralLoc);
939
940 return false;
941}
942
943ParseStatus XtensaAsmParser::parseDirective(AsmToken DirectiveID) {
944 StringRef IDVal = DirectiveID.getString();
945 SMLoc Loc = getLexer().getLoc();
946
947 if (IDVal == ".literal_position") {
948 XtensaTargetStreamer &TS = this->getTargetStreamer();
950 return parseEOL();
951 }
952
953 if (IDVal == ".literal") {
954 return parseLiteralDirective(Loc);
955 }
956
958}
959
960// Force static initialization.
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
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:665
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
Register Reg
SI Fold Operands
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:116
void UnLex(AsmToken const &Token)
Definition AsmLexer.h:107
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition AsmLexer.h:119
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition AsmLexer.h:93
LLVM_ABI size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)
Look ahead an arbitrary number of tokens.
Definition AsmLexer.cpp:762
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:69
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:31
ExprKind getKind() const
Definition MCExpr.h:82
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
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
Interface to description of machine instruction set.
Definition MCInstrInfo.h:27
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
void setExpr(const MCExpr *Val)
Definition MCInst.h:122
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
const MCExpr * getExpr() const
Definition MCInst.h:117
bool isExpr() const
Definition MCInst.h:68
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Streaming machine code generation interface.
Definition MCStreamer.h:221
MCTargetStreamer * getTargetStreamer()
Definition MCStreamer.h:339
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
const MCSymbol & getSymbol() const
Definition MCExpr.h:223
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:210
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:94
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
virtual void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, bool SwitchLiteralSection, SMLoc L=SMLoc())
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.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
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 isSelect_4() const
bool isImm12() const
bool isOffset_256_8() 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
bool isOffset_64_16() const
void addExpr(MCInst &Inst, const MCExpr *Expr) const
void addImmOperands(MCInst &Inst, unsigned N) const
StringRef getToken() const
bool isOffset_256_16() 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 isSelect_16() 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 isSelect_8() const
bool isimm7_22() const
SMLoc getEndLoc() const override
getEndLoc - Gets location of the last token of this operand
bool isOffset_16_16() const
bool isImm32n_95() const
bool isSelect_2() const
const MCExpr * getImm() const
bool isUimm5() const
bool isOffset_256_4() 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 isOffset_128_1() const
bool isOffset_128_2() 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,...