LLVM 24.0.0git
AVRAsmParser.cpp
Go to the documentation of this file.
1//===---- AVRAsmParser.cpp - Parse AVR 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
13
14#include "llvm/ADT/APInt.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCStreamer.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCValue.h"
28#include "llvm/Support/Debug.h"
30
31#include <array>
32#include <sstream>
33
34#define DEBUG_TYPE "avr-asm-parser"
35
36using namespace llvm;
37
38namespace {
39/// Parses AVR assembly from a stream.
40class AVRAsmParser : public MCTargetAsmParser {
41 MCAsmParser &Parser;
42 const MCRegisterInfo *MRI;
43 const std::string GENERATE_STUBS = "gs";
44
45 enum AVRMatchResultTy {
46 Match_InvalidRegisterOnTiny = FIRST_TARGET_MATCH_RESULT_TY + 1,
47 };
48
49#define GET_ASSEMBLER_HEADER
50#include "AVRGenAsmMatcher.inc"
51
52 bool matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
55 bool MatchingInlineAsm) override;
56
57 bool parseRegister(MCRegister &Reg, SMLoc &StartLoc, SMLoc &EndLoc) override;
58 ParseStatus tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
59 SMLoc &EndLoc) override;
60
61 bool parseInstruction(ParseInstructionInfo &Info, StringRef Name,
62 SMLoc NameLoc, OperandVector &Operands) override;
63
64 ParseStatus parseDirective(AsmToken DirectiveID) override;
65
66 ParseStatus parseMemriOperand(OperandVector &Operands);
67
68 bool parseOperand(OperandVector &Operands, bool maybeReg);
69 MCRegister parseRegisterName(MCRegister (*matchFn)(StringRef));
70 MCRegister parseRegisterName();
71 MCRegister parseRegister(bool RestoreOnFailure = false);
72 bool tryParseRegisterOperand(OperandVector &Operands);
73 bool tryParseExpression(OperandVector &Operands, int64_t offset);
74 bool tryParseRelocExpression(OperandVector &Operands);
75 void eatComma();
76
77 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op,
78 unsigned Kind) override;
79
80 MCRegister toDREG(MCRegister Reg, unsigned From = AVR::sub_lo) {
81 MCRegisterClass const *Class = &getAVRMCRegisterClass(AVR::DREGSRegClassID);
82 return MRI->getMatchingSuperReg(Reg, From, Class);
83 }
84
85 bool emit(MCInst &Instruction, SMLoc Loc, MCStreamer &Out) const;
86 bool invalidOperand(SMLoc Loc, OperandVector const &Operands,
87 uint64_t const &ErrorInfo);
88 bool missingFeature(SMLoc Loc, uint64_t const &ErrorInfo);
89
90 ParseStatus parseLiteralValues(unsigned SizeInBytes, SMLoc L);
91
92public:
93 AVRAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser,
94 const MCInstrInfo &MII)
95 : MCTargetAsmParser(STI, MII), Parser(Parser) {
97 MRI = getContext().getRegisterInfo();
98
99 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
100 }
101
102 MCAsmParser &getParser() const { return Parser; }
103 AsmLexer &getLexer() const { return Parser.getLexer(); }
104};
105
106/// An parsed AVR assembly operand.
107class AVROperand : public MCParsedAsmOperand {
108 typedef MCParsedAsmOperand Base;
109 enum KindTy { k_Immediate, k_Register, k_Token, k_Memri } Kind;
110
111public:
112 AVROperand(StringRef Tok, SMLoc S)
113 : Kind(k_Token), Tok(Tok), Start(S), End(S) {}
114 AVROperand(MCRegister Reg, SMLoc S, SMLoc E)
115 : Kind(k_Register), RegImm({Reg, nullptr}), Start(S), End(E) {}
116 AVROperand(MCExpr const *Imm, SMLoc S, SMLoc E)
117 : Kind(k_Immediate), RegImm({0, Imm}), Start(S), End(E) {}
118 AVROperand(MCRegister Reg, MCExpr const *Imm, SMLoc S, SMLoc E)
119 : Kind(k_Memri), RegImm({Reg, Imm}), Start(S), End(E) {}
120
121 struct RegisterImmediate {
122 MCRegister Reg;
123 MCExpr const *Imm;
124 };
125 union {
126 StringRef Tok;
127 RegisterImmediate RegImm;
128 };
129
130 SMLoc Start, End;
131
132public:
133 void addRegOperands(MCInst &Inst, unsigned N) const {
134 assert(Kind == k_Register && "Unexpected operand kind");
135 assert(N == 1 && "Invalid number of operands!");
136
138 }
139
140 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
141 // Add as immediate when possible
142 if (!Expr)
144 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
145 Inst.addOperand(MCOperand::createImm(CE->getValue()));
146 else
148 }
149
150 void addImmOperands(MCInst &Inst, unsigned N) const {
151 assert(Kind == k_Immediate && "Unexpected operand kind");
152 assert(N == 1 && "Invalid number of operands!");
153
154 const MCExpr *Expr = getImm();
155 addExpr(Inst, Expr);
156 }
157
158 /// Adds the contained reg+imm operand to an instruction.
159 void addMemriOperands(MCInst &Inst, unsigned N) const {
160 assert(Kind == k_Memri && "Unexpected operand kind");
161 assert(N == 2 && "Invalid number of operands");
162
164 addExpr(Inst, getImm());
165 }
166
167 void addImmCom8Operands(MCInst &Inst, unsigned N) const {
168 assert(N == 1 && "Invalid number of operands!");
169 // The operand is actually a imm8, but we have its bitwise
170 // negation in the assembly source, so twiddle it here.
171 const auto *CE = cast<MCConstantExpr>(getImm());
172 Inst.addOperand(MCOperand::createImm(~(uint8_t)CE->getValue()));
173 }
174
175 bool isImmCom8() const {
176 if (!isImm())
177 return false;
178 const auto *CE = dyn_cast<MCConstantExpr>(getImm());
179 if (!CE)
180 return false;
181 int64_t Value = CE->getValue();
182 return isUInt<8>(Value);
183 }
184
185 bool isReg() const override { return Kind == k_Register; }
186 bool isImm() const override { return Kind == k_Immediate; }
187 bool isToken() const override { return Kind == k_Token; }
188 bool isMem() const override { return Kind == k_Memri; }
189 bool isMemri() const { return Kind == k_Memri; }
190
191 StringRef getToken() const {
192 assert(Kind == k_Token && "Invalid access!");
193 return Tok;
194 }
195
196 MCRegister getReg() const override {
197 assert((Kind == k_Register || Kind == k_Memri) && "Invalid access!");
198
199 return RegImm.Reg;
200 }
201
202 const MCExpr *getImm() const {
203 assert((Kind == k_Immediate || Kind == k_Memri) && "Invalid access!");
204 return RegImm.Imm;
205 }
206
207 static std::unique_ptr<AVROperand> CreateToken(StringRef Str, SMLoc S) {
208 return std::make_unique<AVROperand>(Str, S);
209 }
210
211 static std::unique_ptr<AVROperand> CreateReg(MCRegister Reg, SMLoc S,
212 SMLoc E) {
213 return std::make_unique<AVROperand>(Reg, S, E);
214 }
215
216 static std::unique_ptr<AVROperand> CreateImm(const MCExpr *Val, SMLoc S,
217 SMLoc E) {
218 return std::make_unique<AVROperand>(Val, S, E);
219 }
220
221 static std::unique_ptr<AVROperand>
222 CreateMemri(MCRegister Reg, const MCExpr *Val, SMLoc S, SMLoc E) {
223 return std::make_unique<AVROperand>(Reg, Val, S, E);
224 }
225
226 void makeToken(StringRef Token) {
227 Kind = k_Token;
228 Tok = Token;
229 }
230
231 void makeReg(MCRegister Reg) {
232 Kind = k_Register;
233 RegImm = {Reg, nullptr};
234 }
235
236 void makeImm(MCExpr const *Ex) {
237 Kind = k_Immediate;
238 RegImm = {0, Ex};
239 }
240
241 void makeMemri(MCRegister Reg, MCExpr const *Imm) {
242 Kind = k_Memri;
243 RegImm = {Reg, Imm};
244 }
245
246 SMLoc getStartLoc() const override { return Start; }
247 SMLoc getEndLoc() const override { return End; }
248
249 void print(raw_ostream &O, const MCAsmInfo &MAI) const override {
250 switch (Kind) {
251 case k_Token:
252 O << "Token: \"" << getToken() << "\"";
253 break;
254 case k_Register:
255 O << "Register: " << getReg().id();
256 break;
257 case k_Immediate:
258 O << "Immediate: \"";
259 MAI.printExpr(O, *getImm());
260 O << "\"";
261 break;
262 case k_Memri: {
263 // only manually print the size for non-negative values,
264 // as the sign is inserted automatically.
265 O << "Memri: \"" << getReg().id() << '+';
266 MAI.printExpr(O, *getImm());
267 O << "\"";
268 break;
269 }
270 }
271 O << "\n";
272 }
273};
274
275} // end anonymous namespace.
276
277// Auto-generated Match Functions
278
279/// Maps from the set of all register names to a register number.
280/// \note Generated by TableGen.
282
283/// Maps from the set of all alternative registernames to a register number.
284/// \note Generated by TableGen.
286
287bool AVRAsmParser::invalidOperand(SMLoc Loc, OperandVector const &Operands,
288 uint64_t const &ErrorInfo) {
289 SMLoc ErrorLoc = Loc;
290 char const *Diag = nullptr;
291
292 if (ErrorInfo != ~0U) {
293 if (ErrorInfo >= Operands.size()) {
294 Diag = "too few operands for instruction.";
295 } else {
296 AVROperand const &Op = (AVROperand const &)*Operands[ErrorInfo];
297
298 // TODO: See if we can do a better error than just "invalid ...".
299 if (Op.getStartLoc() != SMLoc()) {
300 ErrorLoc = Op.getStartLoc();
301 }
302 }
303 }
304
305 if (!Diag) {
306 Diag = "invalid operand for instruction";
307 }
308
309 return Error(ErrorLoc, Diag);
310}
311
312bool AVRAsmParser::missingFeature(SMLoc Loc, uint64_t const &ErrorInfo) {
313 return Error(Loc, "instruction requires a CPU feature not currently enabled");
314}
315
316bool AVRAsmParser::emit(MCInst &Inst, SMLoc Loc, MCStreamer &Out) const {
317 Inst.setLoc(Loc);
318 Out.emitInstruction(Inst, *STI);
319
320 return false;
321}
322
323bool AVRAsmParser::matchAndEmitInstruction(SMLoc Loc, unsigned &Opcode,
325 MCStreamer &Out, uint64_t &ErrorInfo,
326 bool MatchingInlineAsm) {
327 MCInst Inst;
328 unsigned MatchResult =
329 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
330
331 switch (MatchResult) {
332 case Match_Success:
333 return emit(Inst, Loc, Out);
334 case Match_MissingFeature:
335 return missingFeature(Loc, ErrorInfo);
336 case Match_InvalidOperand:
337 return invalidOperand(Loc, Operands, ErrorInfo);
338 case Match_MnemonicFail:
339 return Error(Loc, "invalid instruction");
340 case Match_InvalidRegisterOnTiny:
341 return Error(Loc, "invalid register on avrtiny");
342 default:
343 return true;
344 }
345}
346
347/// Parses a register name using a given matching function.
348/// Checks for lowercase or uppercase if necessary.
349MCRegister AVRAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
350 StringRef Name = Parser.getTok().getString();
351
352 MCRegister Reg = matchFn(Name);
353
354 // GCC supports case insensitive register names. Some of the AVR registers
355 // are all lower case, some are all upper case but non are mixed. We prefer
356 // to use the original names in the register definitions. That is why we
357 // have to test both upper and lower case here.
358 if (!Reg) {
359 Reg = matchFn(Name.lower());
360 }
361 if (!Reg) {
362 Reg = matchFn(Name.upper());
363 }
364
365 return Reg;
366}
367
368MCRegister AVRAsmParser::parseRegisterName() {
369 MCRegister Reg = parseRegisterName(&MatchRegisterName);
370
371 if (!Reg)
372 Reg = parseRegisterName(&MatchRegisterAltName);
373
374 return Reg;
375}
376
377MCRegister AVRAsmParser::parseRegister(bool RestoreOnFailure) {
378 MCRegister Reg;
379
380 if (Parser.getTok().is(AsmToken::Identifier)) {
381 // Check for register pair syntax
382 if (Parser.getLexer().peekTok().is(AsmToken::Colon)) {
383 AsmToken HighTok = Parser.getTok();
384 Parser.Lex();
385 AsmToken ColonTok = Parser.getTok();
386 Parser.Lex(); // Eat high (odd) register and colon
387
388 if (Parser.getTok().is(AsmToken::Identifier)) {
389 // Convert lower (even) register to DREG
390 Reg = toDREG(parseRegisterName());
391 }
392 if (!Reg && RestoreOnFailure) {
393 getLexer().UnLex(std::move(ColonTok));
394 getLexer().UnLex(std::move(HighTok));
395 }
396 } else {
397 Reg = parseRegisterName();
398 }
399 }
400 return Reg;
401}
402
403bool AVRAsmParser::tryParseRegisterOperand(OperandVector &Operands) {
404 MCRegister Reg = parseRegister();
405
406 if (!Reg)
407 return true;
408
409 // Reject R0~R15 on avrtiny.
410 if (AVR::R0 <= Reg && Reg <= AVR::R15 &&
411 STI->hasFeature(AVR::FeatureTinyEncoding))
412 return Error(Parser.getTok().getLoc(), "invalid register on avrtiny");
413
414 AsmToken const &T = Parser.getTok();
415 Operands.push_back(AVROperand::CreateReg(Reg, T.getLoc(), T.getEndLoc()));
416 Parser.Lex(); // Eat register token.
417
418 return false;
419}
420
421bool AVRAsmParser::tryParseExpression(OperandVector &Operands, int64_t offset) {
422 SMLoc S = Parser.getTok().getLoc();
423
424 if (!tryParseRelocExpression(Operands))
425 return false;
426
427 if ((Parser.getTok().getKind() == AsmToken::Plus ||
428 Parser.getTok().getKind() == AsmToken::Minus) &&
430 // Don't handle this case - it should be split into two
431 // separate tokens.
432 return true;
433 }
434
435 // Parse (potentially inner) expression
436 MCExpr const *Expression;
437 if (getParser().parseExpression(Expression))
438 return true;
439
440 if (offset) {
441 Expression = MCBinaryExpr::createAdd(
442 Expression, MCConstantExpr::create(offset, getContext()), getContext());
443 }
444
445 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
446 Operands.push_back(AVROperand::CreateImm(Expression, S, E));
447 return false;
448}
449
450bool AVRAsmParser::tryParseRelocExpression(OperandVector &Operands) {
451 bool isNegated = false;
452 AVR::Specifier ModifierKind = AVR::S_AVR_NONE;
453
454 SMLoc S = Parser.getTok().getLoc();
455
456 // Reject the form in which sign comes first. This behaviour is
457 // in accordance with avr-gcc.
458 AsmToken::TokenKind CurTok = Parser.getLexer().getKind();
459 if (CurTok == AsmToken::Minus || CurTok == AsmToken::Plus)
460 return true;
461
462 // Check for sign.
463 AsmToken tokens[2];
464 if (Parser.getLexer().peekTokens(tokens) == 2)
465 if (tokens[0].getKind() == AsmToken::LParen &&
466 tokens[1].getKind() == AsmToken::Minus)
467 isNegated = true;
468
469 // Check if we have a target specific modifier (lo8, hi8, &c)
470 if (CurTok != AsmToken::Identifier ||
471 Parser.getLexer().peekTok().getKind() != AsmToken::LParen) {
472 // Not a reloc expr
473 return true;
474 }
475 StringRef ModifierName = Parser.getTok().getString();
476 ModifierKind = AVRMCExpr::parseSpecifier(ModifierName);
477
478 if (ModifierKind != AVR::S_AVR_NONE) {
479 Parser.Lex();
480 Parser.Lex(); // Eat modifier name and parenthesis
481 if (Parser.getTok().getString() == GENERATE_STUBS &&
482 Parser.getTok().getKind() == AsmToken::Identifier) {
483 std::string GSModName = ModifierName.str() + "_" + GENERATE_STUBS;
484 ModifierKind = AVRMCExpr::parseSpecifier(GSModName);
485 if (ModifierKind != AVR::S_AVR_NONE)
486 Parser.Lex(); // Eat gs modifier name
487 }
488 } else {
489 return Error(Parser.getTok().getLoc(), "unknown modifier");
490 }
491
492 if (tokens[1].getKind() == AsmToken::Minus ||
493 tokens[1].getKind() == AsmToken::Plus) {
494 Parser.Lex();
495 assert(Parser.getTok().getKind() == AsmToken::LParen);
496 Parser.Lex(); // Eat the sign and parenthesis
497 }
498
499 MCExpr const *InnerExpression;
500 if (getParser().parseExpression(InnerExpression))
501 return true;
502
503 if (tokens[1].getKind() == AsmToken::Minus ||
504 tokens[1].getKind() == AsmToken::Plus) {
505 assert(Parser.getTok().getKind() == AsmToken::RParen);
506 Parser.Lex(); // Eat closing parenthesis
507 }
508
509 // If we have a modifier wrap the inner expression
510 assert(Parser.getTok().getKind() == AsmToken::RParen);
511 Parser.Lex(); // Eat closing parenthesis
512
513 MCExpr const *Expression =
514 AVRMCExpr::create(ModifierKind, InnerExpression, isNegated, getContext());
515
516 SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
517 Operands.push_back(AVROperand::CreateImm(Expression, S, E));
518
519 return false;
520}
521
522bool AVRAsmParser::parseOperand(OperandVector &Operands, bool maybeReg) {
523 LLVM_DEBUG(dbgs() << "parseOperand\n");
524
525 switch (getLexer().getKind()) {
526 default:
527 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
528
530 // Try to parse a register, fall through to the next case if it fails.
531 if (maybeReg && !tryParseRegisterOperand(Operands)) {
532 return false;
533 }
534 [[fallthrough]];
535 case AsmToken::LParen:
537 return tryParseExpression(Operands, 0);
538 case AsmToken::Dot:
539 return tryParseExpression(Operands, 2);
540 case AsmToken::Plus:
541 case AsmToken::Minus: {
542 // If the sign preceeds a number, parse the number,
543 // otherwise treat the sign a an independent token.
544 switch (getLexer().peekTok().getKind()) {
546 case AsmToken::BigNum:
548 case AsmToken::Real:
549 if (!tryParseExpression(Operands, 0))
550 return false;
551 break;
552 default:
553 break;
554 }
555 // Treat the token as an independent token.
556 Operands.push_back(AVROperand::CreateToken(Parser.getTok().getString(),
557 Parser.getTok().getLoc()));
558 Parser.Lex(); // Eat the token.
559 return false;
560 }
561 }
562
563 // Could not parse operand
564 return true;
565}
566
567ParseStatus AVRAsmParser::parseMemriOperand(OperandVector &Operands) {
568 LLVM_DEBUG(dbgs() << "parseMemriOperand()\n");
569
570 SMLoc E, S;
571 MCExpr const *Expression;
572 MCRegister Reg;
573
574 // Parse register.
575 {
576 Reg = parseRegister();
577
578 if (!Reg)
580
581 S = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
582 Parser.Lex(); // Eat register token.
583 }
584
585 // Parse immediate;
586 {
587 if (getParser().parseExpression(Expression))
589
590 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
591 }
592
593 Operands.push_back(AVROperand::CreateMemri(Reg, Expression, S, E));
594
596}
597
598bool AVRAsmParser::parseRegister(MCRegister &Reg, SMLoc &StartLoc,
599 SMLoc &EndLoc) {
600 StartLoc = Parser.getTok().getLoc();
601 Reg = parseRegister(/*RestoreOnFailure=*/false);
602 EndLoc = Parser.getTok().getLoc();
603
604 return Reg == AVR::NoRegister;
605}
606
607ParseStatus AVRAsmParser::tryParseRegister(MCRegister &Reg, SMLoc &StartLoc,
608 SMLoc &EndLoc) {
609 StartLoc = Parser.getTok().getLoc();
610 Reg = parseRegister(/*RestoreOnFailure=*/true);
611 EndLoc = Parser.getTok().getLoc();
612
613 if (Reg == AVR::NoRegister)
616}
617
618void AVRAsmParser::eatComma() {
619 if (getLexer().is(AsmToken::Comma)) {
620 Parser.Lex();
621 } else {
622 // GCC allows commas to be omitted.
623 }
624}
625
626bool AVRAsmParser::parseInstruction(ParseInstructionInfo &Info,
627 StringRef Mnemonic, SMLoc NameLoc,
629 Operands.push_back(AVROperand::CreateToken(Mnemonic, NameLoc));
630
631 int OperandNum = -1;
632 while (getLexer().isNot(AsmToken::EndOfStatement)) {
633 OperandNum++;
634 if (OperandNum > 0)
635 eatComma();
636
637 ParseStatus ParseRes = MatchOperandParserImpl(Operands, Mnemonic);
638
639 if (ParseRes.isSuccess())
640 continue;
641
642 if (ParseRes.isFailure()) {
643 SMLoc Loc = getLexer().getLoc();
644 Parser.eatToEndOfStatement();
645
646 return Error(Loc, "failed to parse register and immediate pair");
647 }
648
649 // These specific operands should be treated as addresses/symbols/labels,
650 // other than registers.
651 bool maybeReg = true;
652
653 if (OperandNum == 1) {
654 std::array<StringRef, 8> Insts = {"lds", "adiw", "sbiw", "ldi"};
655 for (auto Inst : Insts) {
656 if (Inst == Mnemonic) {
657 maybeReg = false;
658 break;
659 }
660 }
661 } else if (OperandNum == 0) {
662 std::array<StringRef, 8> Insts = {"sts", "call", "rcall", "rjmp", "jmp"};
663 for (auto Inst : Insts) {
664 if (Inst == Mnemonic) {
665 maybeReg = false;
666 break;
667 }
668 }
669 }
670
671 if (parseOperand(Operands, maybeReg)) {
672 SMLoc Loc = getLexer().getLoc();
673 Parser.eatToEndOfStatement();
674 return Error(Loc, "unexpected token in argument list");
675 }
676 }
677 Parser.Lex(); // Consume the EndOfStatement
678 return false;
679}
680
681ParseStatus AVRAsmParser::parseDirective(llvm::AsmToken DirectiveID) {
682 StringRef IDVal = DirectiveID.getIdentifier();
683 if (IDVal.lower() == ".long")
684 return parseLiteralValues(SIZE_LONG, DirectiveID.getLoc());
685 if (IDVal.lower() == ".word" || IDVal.lower() == ".short")
686 return parseLiteralValues(SIZE_WORD, DirectiveID.getLoc());
687 if (IDVal.lower() == ".byte")
688 return parseLiteralValues(1, DirectiveID.getLoc());
690}
691
692ParseStatus AVRAsmParser::parseLiteralValues(unsigned SizeInBytes, SMLoc L) {
693 MCAsmParser &Parser = getParser();
694 AVRMCELFStreamer &AVRStreamer =
695 static_cast<AVRMCELFStreamer &>(Parser.getStreamer());
696 AsmToken Tokens[2];
697 size_t ReadCount = Parser.getLexer().peekTokens(Tokens);
698 if (ReadCount == 2 && Parser.getTok().getKind() == AsmToken::Identifier &&
699 Tokens[0].getKind() == AsmToken::Minus &&
700 Tokens[1].getKind() == AsmToken::Identifier) {
701 MCSymbol *Symbol = getContext().getOrCreateSymbol(".text");
702 AVRStreamer.emitValueForModiferKind(Symbol, SizeInBytes, L,
705 }
706
707 if (Parser.getTok().getKind() == AsmToken::Identifier &&
708 Parser.getLexer().peekTok().getKind() == AsmToken::LParen) {
709 StringRef ModifierName = Parser.getTok().getString();
710 AVR::Specifier Spec = AVRMCExpr::parseSpecifier(ModifierName);
711 if (Spec != AVR::S_AVR_NONE) {
712 Parser.Lex();
713 Parser.Lex(); // Eat the modifier and parenthesis
714 } else {
715 return Error(Parser.getTok().getLoc(), "unknown modifier");
716 }
718 getContext().getOrCreateSymbol(Parser.getTok().getString());
719 AVRStreamer.emitValueForModiferKind(Symbol, SizeInBytes, L, Spec);
720 Lex(); // Eat the symbol name.
721 if (parseToken(AsmToken::RParen))
723 return parseEOL();
724 }
725
726 auto parseOne = [&]() -> bool {
727 const MCExpr *Value;
728 if (Parser.parseExpression(Value))
729 return true;
730 Parser.getStreamer().emitValue(Value, SizeInBytes, L);
731 return false;
732 };
733 return (parseMany(parseOne));
734}
735
739
740#define GET_REGISTER_MATCHER
741#define GET_MATCHER_IMPLEMENTATION
742#include "AVRGenAsmMatcher.inc"
743
744// Uses enums defined in AVRGenAsmMatcher.inc
745unsigned AVRAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
746 unsigned ExpectedKind) {
747 AVROperand &Op = static_cast<AVROperand &>(AsmOp);
748 MatchClassKind Expected = static_cast<MatchClassKind>(ExpectedKind);
749
750 // If need be, GCC converts bare numbers to register names
751 // It's ugly, but GCC supports it.
752 if (Op.isImm()) {
753 if (MCConstantExpr const *Const = dyn_cast<MCConstantExpr>(Op.getImm())) {
754 int64_t RegNum = Const->getValue();
755
756 // Reject R0~R15 on avrtiny.
757 if (0 <= RegNum && RegNum <= 15 &&
758 STI->hasFeature(AVR::FeatureTinyEncoding))
759 return Match_InvalidRegisterOnTiny;
760
761 std::ostringstream RegName;
762 RegName << "r" << RegNum;
764 Op.makeReg(Reg);
765 if (validateOperandClass(Op, Expected, *STI) == Match_Success) {
766 return Match_Success;
767 }
768 }
769 // Let the other quirks try their magic.
770 }
771 }
772
773 if (Op.isReg()) {
774 // If the instruction uses a register pair but we got a single, lower
775 // register we perform a "class cast".
776 if (isSubclass(Expected, MCK_DREGS)) {
777 MCRegister correspondingDREG = toDREG(Op.getReg());
778
779 if (correspondingDREG) {
780 Op.makeReg(correspondingDREG);
781 return validateOperandClass(Op, Expected, *STI);
782 }
783 }
784 }
785 return Match_InvalidOperand;
786}
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)
This file implements a class to represent arbitrary precision integral constant values and operations...
static MCRegister MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
static MCRegister MatchRegisterName(StringRef Name)
Maps from the set of all register names to a register number.
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmParser()
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
#define RegName(no)
Register Reg
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
MachineRegisterInfo & MRI
SI Fold Operands
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
#define LLVM_DEBUG(...)
Definition Debug.h:119
void emitValueForModiferKind(const MCSymbol *Sym, unsigned SizeInBytes, SMLoc Loc=SMLoc(), AVRMCExpr::Specifier ModifierKind=AVR::S_AVR_NONE)
static const AVRMCExpr * create(Specifier S, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Specifies the type of an expression.
Definition AVRMCExpr.cpp:17
static Specifier parseSpecifier(StringRef Name)
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
Definition AsmLexer.h:122
AsmToken::TokenKind getKind() const
Get the kind of current token.
Definition AsmLexer.h:145
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
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
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
Tagged union holding either a T or a Error.
Definition Error.h:485
void printExpr(raw_ostream &, const MCExpr &) const
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
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.
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
AsmLexer & getLexer()
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
MCStreamer & getStreamer()
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:342
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
void setLoc(SMLoc loc)
Definition MCInst.h:207
void addOperand(const MCOperand Op)
Definition MCInst.h:215
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:166
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand.
MCRegisterClass - Base class of TargetRegisterClass.
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
constexpr unsigned id() const
Definition MCRegister.h:82
Streaming machine code generation interface.
Definition MCStreamer.h:222
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
bool hasFeature(unsigned Feature) const
const FeatureBitset & getFeatureBits() const
MCTargetAsmParser - Generic interface to target specific assembly parsers.
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
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
LLVM_ABI std::string lower() const
uint16_t Specifier
@ 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)
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
Target & getTheAVRTarget()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
DWARFExpression::Operation Op
const int SIZE_WORD
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
const int SIZE_LONG
#define N
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...