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