33#define DEBUG_TYPE "avr-asm-parser"
38 unsigned From = AVR::sub_lo) {
39 MCRegisterClass const *Class = &getAVRMCRegisterClass(AVR::DREGSRegClassID);
40 return MRI.getMatchingSuperReg(
Reg, From, Class);
47 const MCRegisterInfo *MRI;
48 const std::string GENERATE_STUBS =
"gs";
50 enum AVRMatchResultTy {
51 Match_InvalidRegisterOnTiny = FIRST_TARGET_MATCH_RESULT_TY + 1,
54#define GET_ASSEMBLER_HEADER
55#include "AVRGenAsmMatcher.inc"
57 bool matchAndEmitInstruction(
SMLoc IDLoc,
unsigned &Opcode,
60 bool MatchingInlineAsm)
override;
64 SMLoc &EndLoc)
override;
76 MCRegister parseRegister(
bool RestoreOnFailure =
false);
83 unsigned Kind)
override;
99 setAvailableFeatures(ComputeAvailableFeatures(STI.
getFeatureBits()));
102 MCAsmParser &getParser()
const {
return Parser; }
103 AsmLexer &getLexer()
const {
return Parser.getLexer(); }
108 typedef MCParsedAsmOperand Base;
109 enum KindTy { k_Immediate, k_Register, k_Token, k_Memri } Kind;
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 const MCRegisterInfo *MRI =
nullptr)
117 AVROperand(MCExpr
const *
Imm, SMLoc S, SMLoc
E,
118 const MCRegisterInfo *MRI =
nullptr)
120 AVROperand(MCRegister
Reg, MCExpr
const *
Imm, SMLoc S, SMLoc
E)
123 struct RegisterImmediate {
134 const MCRegisterInfo *MRI =
nullptr;
139 MCRegister getRegOrBareNumber()
const {
140 if (Kind == k_Register)
142 if (Kind == k_Immediate)
144 if (0 <=
CE->getValue() &&
CE->getValue() <= 31)
145 return AVR::R0 +
CE->getValue();
149 void addRegOperands(MCInst &Inst,
unsigned N)
const {
150 assert(
N == 1 &&
"Invalid number of operands!");
151 MCRegister
Reg = getRegOrBareNumber();
152 assert(
Reg &&
"Unexpected operand kind");
156 void addDREGSRegOperands(MCInst &Inst,
unsigned N)
const {
157 assert(
N == 1 &&
"Invalid number of operands!");
158 MCRegister
Reg = getRegOrBareNumber();
159 assert(
Reg &&
"Unexpected operand kind");
165 void addExpr(MCInst &Inst,
const MCExpr *Expr)
const {
175 void addImmOperands(MCInst &Inst,
unsigned N)
const {
176 assert(Kind == k_Immediate &&
"Unexpected operand kind");
177 assert(
N == 1 &&
"Invalid number of operands!");
179 const MCExpr *Expr =
getImm();
184 void addMemriOperands(MCInst &Inst,
unsigned N)
const {
185 assert(Kind == k_Memri &&
"Unexpected operand kind");
186 assert(
N == 2 &&
"Invalid number of operands");
192 void addImmCom8Operands(MCInst &Inst,
unsigned N)
const {
193 assert(
N == 1 &&
"Invalid number of operands!");
200 bool isImmCom8()
const {
206 int64_t
Value =
CE->getValue();
210 bool isReg()
const override {
return Kind == k_Register; }
211 bool isImm()
const override {
return Kind == k_Immediate; }
212 bool isToken()
const override {
return Kind == k_Token; }
213 bool isMem()
const override {
return Kind == k_Memri; }
214 bool isMemri()
const {
return Kind == k_Memri; }
217 assert(Kind == k_Token &&
"Invalid access!");
221 MCRegister
getReg()
const override {
222 assert((Kind == k_Register || Kind == k_Memri) &&
"Invalid access!");
227 const MCExpr *
getImm()
const {
228 assert((Kind == k_Immediate || Kind == k_Memri) &&
"Invalid access!");
232 static std::unique_ptr<AVROperand> CreateToken(StringRef Str, SMLoc S) {
233 return std::make_unique<AVROperand>(Str, S);
236 static std::unique_ptr<AVROperand> CreateReg(MCRegister
Reg, SMLoc S, SMLoc
E,
237 const MCRegisterInfo *MRI) {
238 return std::make_unique<AVROperand>(
Reg, S,
E, MRI);
241 static std::unique_ptr<AVROperand>
242 CreateImm(
const MCExpr *Val, SMLoc S, SMLoc
E,
const MCRegisterInfo *MRI) {
243 return std::make_unique<AVROperand>(Val, S,
E, MRI);
246 static std::unique_ptr<AVROperand>
247 CreateMemri(MCRegister
Reg,
const MCExpr *Val, SMLoc S, SMLoc
E) {
248 return std::make_unique<AVROperand>(
Reg, Val, S,
E);
251 SMLoc getStartLoc()
const override {
return Start; }
252 SMLoc getEndLoc()
const override {
return End; }
254 void print(raw_ostream &O,
const MCAsmInfo &MAI)
const override {
263 O <<
"Immediate: \"";
270 O <<
"Memri: \"" <<
getReg().
id() <<
'+';
295 char const *Diag =
nullptr;
299 Diag =
"too few operands for instruction.";
301 AVROperand
const &
Op = (AVROperand
const &)*
Operands[ErrorInfo];
304 if (
Op.getStartLoc() != SMLoc()) {
305 ErrorLoc =
Op.getStartLoc();
311 Diag =
"invalid operand for instruction";
314 return Error(ErrorLoc, Diag);
317bool AVRAsmParser::missingFeature(SMLoc Loc,
uint64_t const &ErrorInfo) {
318 return Error(Loc,
"instruction requires a CPU feature not currently enabled");
321bool AVRAsmParser::emit(MCInst &Inst, SMLoc Loc, MCStreamer &Out)
const {
323 Out.emitInstruction(Inst, *STI);
328bool AVRAsmParser::matchAndEmitInstruction(SMLoc Loc,
unsigned &Opcode,
330 MCStreamer &Out,
uint64_t &ErrorInfo,
331 bool MatchingInlineAsm) {
333 unsigned MatchResult =
334 MatchInstructionImpl(
Operands, Inst, ErrorInfo, MatchingInlineAsm);
336 switch (MatchResult) {
338 return emit(Inst, Loc, Out);
339 case Match_MissingFeature:
340 return missingFeature(Loc, ErrorInfo);
341 case Match_InvalidOperand:
342 return invalidOperand(Loc,
Operands, ErrorInfo);
343 case Match_MnemonicFail:
344 return Error(Loc,
"invalid instruction");
345 case Match_InvalidRegisterOnTiny:
346 return Error(Loc,
"invalid register on avrtiny");
354MCRegister AVRAsmParser::parseRegisterName(MCRegister (*matchFn)(StringRef)) {
357 MCRegister
Reg = matchFn(Name);
373MCRegister AVRAsmParser::parseRegisterName() {
382MCRegister AVRAsmParser::parseRegister(
bool RestoreOnFailure) {
388 AsmToken HighTok = Parser.
getTok();
390 AsmToken ColonTok = Parser.
getTok();
397 if (!
Reg && RestoreOnFailure) {
398 getLexer().UnLex(std::move(ColonTok));
399 getLexer().UnLex(std::move(HighTok));
402 Reg = parseRegisterName();
409 MCRegister
Reg = parseRegister();
415 if (AVR::R0 <=
Reg &&
Reg <= AVR::R15 &&
419 AsmToken
const &
T = Parser.
getTok();
421 AVROperand::CreateReg(
Reg,
T.getLoc(),
T.getEndLoc(),
MRI));
430 if (!tryParseRelocExpression(
Operands))
442 MCExpr
const *Expression;
443 if (getParser().parseExpression(Expression))
452 Operands.push_back(AVROperand::CreateImm(Expression, S,
E,
MRI));
457 bool isNegated =
false;
489 std::string GSModName = ModifierName.
str() +
"_" + GENERATE_STUBS;
505 MCExpr
const *InnerExpression;
506 if (getParser().parseExpression(InnerExpression))
519 MCExpr
const *Expression =
523 Operands.push_back(AVROperand::CreateImm(Expression, S,
E,
MRI));
531 switch (getLexer().getKind()) {
537 if (maybeReg && !tryParseRegisterOperand(
Operands)) {
543 return tryParseExpression(
Operands, 0);
545 return tryParseExpression(
Operands, 2);
550 switch (getLexer().peekTok().getKind()) {
555 if (!tryParseExpression(
Operands, 0))
577 MCExpr
const *Expression;
582 Reg = parseRegister();
593 if (getParser().parseExpression(Expression))
599 Operands.push_back(AVROperand::CreateMemri(
Reg, Expression, S,
E));
604bool AVRAsmParser::parseRegister(MCRegister &
Reg, SMLoc &StartLoc,
607 Reg = parseRegister(
false);
610 return Reg == AVR::NoRegister;
613ParseStatus AVRAsmParser::tryParseRegister(MCRegister &
Reg, SMLoc &StartLoc,
616 Reg = parseRegister(
true);
619 if (
Reg == AVR::NoRegister)
624void AVRAsmParser::eatComma() {
632bool AVRAsmParser::parseInstruction(ParseInstructionInfo &Info,
633 StringRef Mnemonic, SMLoc NameLoc,
635 Operands.push_back(AVROperand::CreateToken(Mnemonic, NameLoc));
643 ParseStatus ParseRes = MatchOperandParserImpl(
Operands, Mnemonic);
649 SMLoc Loc = getLexer().getLoc();
652 return Error(Loc,
"failed to parse register and immediate pair");
657 bool maybeReg =
true;
659 if (OperandNum == 1) {
660 std::array<StringRef, 8> Insts = {
"lds",
"adiw",
"sbiw",
"ldi"};
661 for (
auto Inst : Insts) {
662 if (Inst == Mnemonic) {
667 }
else if (OperandNum == 0) {
668 std::array<StringRef, 8> Insts = {
"sts",
"call",
"rcall",
"rjmp",
"jmp"};
669 for (
auto Inst : Insts) {
670 if (Inst == Mnemonic) {
677 if (parseOperand(
Operands, maybeReg)) {
678 SMLoc Loc = getLexer().getLoc();
680 return Error(Loc,
"unexpected token in argument list");
687ParseStatus AVRAsmParser::parseDirective(llvm::AsmToken DirectiveID) {
689 if (IDVal.
lower() ==
".long")
691 if (IDVal.
lower() ==
".word" || IDVal.
lower() ==
".short")
693 if (IDVal.
lower() ==
".byte")
694 return parseLiteralValues(1, DirectiveID.
getLoc());
698ParseStatus AVRAsmParser::parseLiteralValues(
unsigned SizeInBytes, SMLoc L) {
699 MCAsmParser &Parser = getParser();
700 AVRMCELFStreamer &AVRStreamer =
701 static_cast<AVRMCELFStreamer &
>(Parser.
getStreamer());
732 auto parseOne = [&]() ->
bool {
739 return (parseMany(parseOne));
746#define GET_REGISTER_MATCHER
747#define GET_MATCHER_IMPLEMENTATION
748#include "AVRGenAsmMatcher.inc"
752 unsigned ExpectedKind) {
753 AVROperand &
Op =
static_cast<AVROperand &
>(AsmOp);
754 MatchClassKind
Expected =
static_cast<MatchClassKind
>(ExpectedKind);
755 if (Expected <= MCK_LAST_TOKEN || Expected > MCK_LAST_REGISTER)
756 return Match_InvalidOperand;
762 return Match_InvalidOperand;
765 if (
Reg.id() - AVR::R0 <= 15 && STI->
hasFeature(AVR::FeatureTinyEncoding))
766 return Match_InvalidRegisterOnTiny;
767 AVROperand RegOp(
Reg,
Op.getStartLoc(),
Op.getEndLoc());
768 if (validateOperandClass(RegOp,
Expected, *STI) == Match_Success)
769 return Match_Success;
774 if (isSubclass(
Expected, MCK_DREGS)) {
776 AVROperand RegOp(CorrespondingDREG,
Op.getStartLoc(),
Op.getEndLoc());
777 return validateOperandClass(RegOp,
Expected, *STI);
780 return Match_InvalidOperand;
static MCRegister MatchRegisterName(StringRef Name)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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()
static MCRegister toDREG(const MCRegisterInfo &MRI, MCRegister Reg, unsigned From=AVR::sub_lo)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_EXTERNAL_VISIBILITY
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static bool isReg(const MCInst &MI, unsigned OpNo)
MachineRegisterInfo & MRI
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
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.
static Specifier parseSpecifier(StringRef Name)
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
AsmToken::TokenKind getKind() const
Get the kind of current token.
LLVM_ABI size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)
Look ahead an arbitrary number of tokens.
Target independent representation for an assembler token.
LLVM_ABI SMLoc getLoc() const
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
bool is(TokenKind K) const
TokenKind getKind() const
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Base class for user error types.
Tagged union holding either a T or a Error.
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.
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())
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Instances of this class represent a single low-level machine instruction.
void addOperand(const MCOperand Op)
Interface to description of machine instruction set.
static MCOperand createExpr(const MCExpr *Val)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
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.
constexpr unsigned id() const
Streaming machine code generation interface.
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Generic base class for all target subtargets.
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.
static SMLoc getFromPointer(const char *Ptr)
constexpr const char * getPointer() const
Represent a constant reference to a string, i.e.
std::string str() const
Get the contents as an std::string.
LLVM_ABI std::string lower() const
@ CE
Windows NT (Windows on ARM)
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.
Target & getTheAVRTarget()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
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.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
@ End
Run the machine verifier once, at the end of the pipeline.
RegisterMCAsmParser - Helper template for registering a target specific assembly parser,...