35 #define DEBUG_TYPE "avr-asm-parser" 44 const std::string GENERATE_STUBS =
"gs";
46 #define GET_ASSEMBLER_HEADER 47 #include "AVRGenAsmMatcher.inc" 49 bool MatchAndEmitInstruction(SMLoc IDLoc,
unsigned &Opcode,
52 bool MatchingInlineAsm)
override;
54 bool ParseRegister(
unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc)
override;
56 bool ParseInstruction(ParseInstructionInfo &Info, StringRef
Name,
59 bool ParseDirective(AsmToken DirectiveID)
override;
64 int parseRegisterName(
unsigned (*matchFn)(StringRef));
65 int parseRegisterName();
72 unsigned validateTargetOperandClass(MCParsedAsmOperand &
Op,
73 unsigned Kind)
override;
75 unsigned toDREG(
unsigned Reg,
unsigned From = AVR::sub_lo) {
76 MCRegisterClass
const *Class = &AVRMCRegisterClasses[AVR::DREGSRegClassID];
80 bool emit(MCInst &Instruction, SMLoc
const &Loc, MCStreamer &Out)
const;
81 bool invalidOperand(SMLoc
const &Loc,
OperandVector const &Operands,
82 uint64_t
const &ErrorInfo);
83 bool missingFeature(SMLoc
const &Loc, uint64_t
const &ErrorInfo);
85 bool parseLiteralValues(
unsigned SizeInBytes, SMLoc L);
104 enum KindTy { k_Immediate, k_Register, k_Token, k_Memri }
Kind;
108 : Base(), Kind(k_Token), Tok(Tok), Start(S), End(S) {}
110 : Base(), Kind(k_Register), RegImm({
Reg,
nullptr}), Start(S), End(
E) {}
112 : Base(), Kind(k_Immediate), RegImm({0, Imm}), Start(S), End(
E) {}
114 : Base(), Kind(k_Memri), RegImm({
Reg, Imm}), Start(S), End(
E) {}
129 assert(Kind == k_Register &&
"Unexpected operand kind");
130 assert(N == 1 &&
"Invalid number of operands!");
139 else if (
const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
146 assert(Kind == k_Immediate &&
"Unexpected operand kind");
147 assert(N == 1 &&
"Invalid number of operands!");
149 const MCExpr *Expr = getImm();
155 assert(Kind == k_Memri &&
"Unexpected operand kind");
156 assert(N == 2 &&
"Invalid number of operands");
159 addExpr(Inst, getImm());
162 bool isReg()
const {
return Kind == k_Register; }
163 bool isImm()
const {
return Kind == k_Immediate; }
164 bool isToken()
const {
return Kind == k_Token; }
165 bool isMem()
const {
return Kind == k_Memri; }
166 bool isMemri()
const {
return Kind == k_Memri; }
169 assert(Kind == k_Token &&
"Invalid access!");
174 assert((Kind == k_Register || Kind == k_Memri) &&
"Invalid access!");
180 assert((Kind == k_Immediate || Kind == k_Memri) &&
"Invalid access!");
185 return make_unique<AVROperand>(Str, S);
190 return make_unique<AVROperand>(RegNum, S,
E);
195 return make_unique<AVROperand>(Val, S,
E);
198 static std::unique_ptr<AVROperand>
200 return make_unique<AVROperand>(RegNum, Val, S,
E);
210 RegImm = {RegNo,
nullptr};
220 RegImm = {RegNo, Imm};
229 O <<
"Token: \"" <<
getToken() <<
"\"";
232 O <<
"Register: " <<
getReg();
235 O <<
"Immediate: \"" << *getImm() <<
"\"";
240 O <<
"Memri: \"" <<
getReg() <<
'+' << *getImm() <<
"\"";
258 bool AVRAsmParser::invalidOperand(
SMLoc const &Loc,
261 SMLoc ErrorLoc = Loc;
262 char const *Diag = 0;
264 if (ErrorInfo != ~0U) {
265 if (ErrorInfo >= Operands.
size()) {
266 Diag =
"too few operands for instruction.";
278 Diag =
"invalid operand for instruction";
281 return Error(ErrorLoc, Diag);
284 bool AVRAsmParser::missingFeature(
llvm::SMLoc const &Loc,
285 uint64_t
const &ErrorInfo) {
286 return Error(Loc,
"instruction requires a CPU feature not currently enabled");
296 bool AVRAsmParser::MatchAndEmitInstruction(
SMLoc Loc,
unsigned &Opcode,
299 bool MatchingInlineAsm) {
301 unsigned MatchResult =
302 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
304 switch (MatchResult) {
309 default:
return true;
315 int AVRAsmParser::parseRegisterName(
unsigned (*matchFn)(
StringRef)) {
318 int RegNum = matchFn(Name);
324 if (RegNum == AVR::NoRegister) {
325 RegNum = matchFn(Name.
lower());
327 if (RegNum == AVR::NoRegister) {
328 RegNum = matchFn(Name.
upper());
334 int AVRAsmParser::parseRegisterName() {
335 int RegNum = parseRegisterName(&MatchRegisterName);
337 if (RegNum == AVR::NoRegister)
338 RegNum = parseRegisterName(&MatchRegisterAltName);
343 int AVRAsmParser::parseRegister() {
344 int RegNum = AVR::NoRegister;
354 RegNum = toDREG(parseRegisterName());
357 RegNum = parseRegisterName();
363 bool AVRAsmParser::tryParseRegisterOperand(
OperandVector &Operands) {
364 int RegNo = parseRegister();
366 if (RegNo == AVR::NoRegister)
376 bool AVRAsmParser::tryParseExpression(
OperandVector &Operands) {
379 if (!tryParseRelocExpression(Operands))
400 bool AVRAsmParser::tryParseRelocExpression(
OperandVector &Operands) {
401 bool isNegated =
false;
410 if (ReadCount == 2) {
445 std::string GSModName = ModifierName.
str() +
"_" + GENERATE_STUBS;
461 MCExpr const *InnerExpression;
462 if (
getParser().parseExpression(InnerExpression))
494 if (!tryParseRegisterOperand(Operands)) {
501 return tryParseExpression(Operands);
506 switch (
getLexer().peekTok().getKind()) {
511 if (!tryParseExpression(Operands))
538 RegNo = parseRegister();
540 if (RegNo == AVR::NoRegister)
549 if (
getParser().parseExpression(Expression))
560 bool AVRAsmParser::ParseRegister(
unsigned &RegNo,
SMLoc &StartLoc,
563 RegNo = parseRegister();
566 return (RegNo == AVR::NoRegister);
569 void AVRAsmParser::eatComma() {
584 if (!first) eatComma();
588 auto MatchResult = MatchOperandParserImpl(Operands, Mnemonic);
598 return Error(Loc,
"failed to parse register and immediate pair");
601 if (parseOperand(Operands)) {
604 return Error(Loc,
"unexpected token in argument list");
613 if (IDVal.
lower() ==
".long") {
615 }
else if (IDVal.
lower() ==
".word" || IDVal.
lower() ==
".short") {
617 }
else if (IDVal.
lower() ==
".byte") {
618 parseLiteralValues(1, DirectiveID.
getLoc());
623 bool AVRAsmParser::parseLiteralValues(
unsigned SizeInBytes,
SMLoc L) {
655 auto parseOne = [&]() ->
bool {
669 #define GET_REGISTER_MATCHER 670 #define GET_MATCHER_IMPLEMENTATION 671 #include "AVRGenAsmMatcher.inc" 674 unsigned AVRAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp,
675 unsigned ExpectedKind) {
676 AVROperand &
Op =
static_cast<AVROperand &
>(AsmOp);
677 MatchClassKind Expected =
static_cast<MatchClassKind
>(ExpectedKind);
682 if (MCConstantExpr
const *Const = dyn_cast<MCConstantExpr>(Op.getImm())) {
683 int64_t RegNum = Const->getValue();
684 std::ostringstream RegName;
685 RegName <<
"r" << RegNum;
687 if (RegNum != AVR::NoRegister) {
700 if (isSubclass(Expected, MCK_DREGS)) {
701 unsigned correspondingDREG = toDREG(Op.getReg());
703 if (correspondingDREG != AVR::NoRegister) {
704 Op.makeReg(correspondingDREG);
705 return validateOperandClass(Op, Expected);
void push_back(const T &Elt)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
An parsed AVR assembly operand.
bool parseMany(function_ref< bool()> parseOne, bool hasComma=true)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Compute iterated dominance frontiers using a linear time algorithm.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
void makeReg(unsigned RegNo)
AVROperand(unsigned Reg, MCExpr const *Imm, SMLoc const &S, SMLoc const &E)
Generic assembler parser interface, for use by target specific assembly parsers.
virtual void Initialize(MCAsmParser &Parser)
Initialize the extension for parsing using the given Parser.
static MCOperand createExpr(const MCExpr *Val)
MCTargetAsmParser - Generic interface to target specific assembly parsers.
bool isToken() const
isToken - Is this a token operand?
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
bool isImm() const
isImm - Is this an immediate operand?
AVROperand(unsigned Reg, SMLoc const &S, SMLoc const &E)
const AsmToken & getTok() const
Get the current AsmToken from the stream.
void makeImm(MCExpr const *Ex)
bool isMem() const
isMem - Is this a memory operand?
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, bool PrintSchedInfo=false)
Emit the given Instruction into the current section.
Parses AVR assembly from a stream.
bool Error(SMLoc L, const Twine &Msg, SMRange Range=SMRange())
Target & getTheAVRTarget()
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string...
SMLoc getLoc() const
Get the current source location.
VariantKind
Specifies the type of an expression.
const MCExpr * getImm() const
static MCOperand createReg(unsigned Reg)
const FeatureBitset & getFeatureBits() const
Generic assembler lexer interface, for use by target specific assembly lexers.
static unsigned MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
static std::unique_ptr< AVROperand > CreateReg(unsigned RegNum, SMLoc S, SMLoc E)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Base class for the full range of assembler expressions which are needed for parsing.
Target independent representation for an assembler token.
SMLoc getStartLoc() const
getStartLoc - Get the location of the first token of this operand.
void addRegOperands(MCInst &Inst, unsigned N) const
virtual void print(raw_ostream &O) const
print - Print a debug representation of the operand to the given stream.
MCParsedAsmOperand - This abstract class represents a source-level assembly instruction operand...
This file implements a class to represent arbitrary precision integral constant values and operations...
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc)=0
Parse an arbitrary expression.
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
RegisterMCAsmParser - Helper template for registering a target specific assembly parser, for use in the target machine initialization function.
AVROperand(MCExpr const *Imm, SMLoc const &S, SMLoc const &E)
LLVM_NODISCARD std::string upper() const
Convert the given ASCII string to uppercase.
void EmitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Instances of this class represent a single low-level machine instruction.
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
void makeMemri(unsigned RegNo, MCExpr const *Imm)
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
const char * getPointer() const
Streaming machine code generation interface.
unsigned getMatchingSuperReg(unsigned 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...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static std::unique_ptr< AVROperand > CreateImm(const MCExpr *Val, SMLoc S, SMLoc E)
static std::unique_ptr< AVROperand > CreateMemri(unsigned RegNum, const MCExpr *Val, SMLoc S, SMLoc E)
Interface to description of machine instruction set.
virtual MCAsmLexer & getLexer()=0
void addMemriOperands(MCInst &Inst, unsigned N) const
Adds the contained reg+imm operand to an instruction.
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
void addImmOperands(MCInst &Inst, unsigned N) const
SmallVectorImpl< std::unique_ptr< MCParsedAsmOperand > > OperandVector
AVROperand(StringRef Tok, SMLoc const &S)
AsmToken::TokenKind getKind() const
Get the kind of current token.
bool isReg() const
isReg - Is this a register operand?
void makeToken(StringRef Token)
virtual MCStreamer & getStreamer()=0
Return the output streamer for the assembler.
SMLoc getEndLoc() const
getEndLoc - Get the location of the last token of this operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool is(TokenKind K) const
static unsigned getReg(const void *D, unsigned RC, unsigned RegNo)
static std::unique_ptr< AVROperand > CreateToken(StringRef Str, SMLoc S)
Base class for user error types.
static SMLoc getFromPointer(const char *Ptr)
StringRef getToken() const
void setAvailableFeatures(uint64_t Value)
void addExpr(MCInst &Inst, const MCExpr *Expr) const
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Generic base class for all target subtargets.
static const AVRMCExpr * create(VariantKind Kind, const MCExpr *Expr, bool isNegated, MCContext &Ctx)
Creates an AVR machine code expression.
MCAsmParser & getParser() const
static VariantKind getKindByName(StringRef Name)
MCAsmLexer & getLexer() const
LLVM_NODISCARD std::string lower() const
void EmitValueForModiferKind(const MCSymbol *Sym, unsigned SizeInBytes, SMLoc Loc=SMLoc(), AVRMCExpr::VariantKind ModifierKind=AVRMCExpr::VK_AVR_None)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
LLVM Value Representation.
static unsigned MatchRegisterName(StringRef Name)
Maps from the set of all register names to a register number.
This class implements an extremely fast bulk output stream that can only output to a stream...
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
AVRAsmParser(const MCSubtargetInfo &STI, MCAsmParser &Parser, const MCInstrInfo &MII, const MCTargetOptions &Options)
virtual size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)=0
Look ahead an arbitrary number of tokens.
Represents a location in source code.
void LLVMInitializeAVRAsmParser()
static MCOperand createImm(int64_t Val)
TokenKind getKind() const