35 #define DEBUG_TYPE "avr-asm-parser" 45 const std::string GENERATE_STUBS =
"gs";
47 #define GET_ASSEMBLER_HEADER 48 #include "AVRGenAsmMatcher.inc" 50 bool MatchAndEmitInstruction(
SMLoc IDLoc,
unsigned &Opcode,
53 bool MatchingInlineAsm)
override;
55 bool ParseRegister(
unsigned &RegNo,
SMLoc &StartLoc,
SMLoc &EndLoc)
override;
60 bool ParseDirective(
AsmToken DirectiveID)
override;
65 int parseRegisterName(
unsigned (*matchFn)(
StringRef));
66 int parseRegisterName();
74 unsigned Kind)
override;
76 unsigned toDREG(
unsigned Reg,
unsigned From = AVR::sub_lo) {
77 MCRegisterClass const *Class = &AVRMCRegisterClasses[AVR::DREGSRegClassID];
86 bool parseLiteralValues(
unsigned SizeInBytes,
SMLoc L);
93 MRI = getContext().getRegisterInfo();
95 setAvailableFeatures(ComputeAvailableFeatures(STI.
getFeatureBits()));
105 enum KindTy { k_Immediate, k_Register, k_Token, k_Memri }
Kind;
109 : Base(), Kind(k_Token), Tok(Tok), Start(S), End(S) {}
110 AVROperand(
unsigned Reg,
SMLoc const &S,
SMLoc const &
E)
111 : Base(), Kind(k_Register), RegImm({
Reg,
nullptr}), Start(S), End(
E) {}
113 : Base(), Kind(k_Immediate), RegImm({0, Imm}), Start(S), End(
E) {}
115 : Base(), Kind(k_Memri), RegImm({
Reg, Imm}), Start(S), End(
E) {}
117 struct RegisterImmediate {
123 RegisterImmediate RegImm;
129 void addRegOperands(
MCInst &Inst,
unsigned N)
const {
130 assert(Kind == k_Register &&
"Unexpected operand kind");
131 assert(N == 1 &&
"Invalid number of operands!");
140 else if (
const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
146 void addImmOperands(
MCInst &Inst,
unsigned N)
const {
147 assert(Kind == k_Immediate &&
"Unexpected operand kind");
148 assert(N == 1 &&
"Invalid number of operands!");
150 const MCExpr *Expr = getImm();
155 void addMemriOperands(
MCInst &Inst,
unsigned N)
const {
156 assert(Kind == k_Memri &&
"Unexpected operand kind");
157 assert(N == 2 &&
"Invalid number of operands");
160 addExpr(Inst, getImm());
163 void addImmCom8Operands(
MCInst &Inst,
unsigned N)
const {
164 assert(N == 1 &&
"Invalid number of operands!");
171 bool isImmCom8()
const {
172 if (!isImm())
return false;
174 if (!CE)
return false;
179 bool isReg()
const {
return Kind == k_Register; }
180 bool isImm()
const {
return Kind == k_Immediate; }
181 bool isToken()
const {
return Kind == k_Token; }
182 bool isMem()
const {
return Kind == k_Memri; }
183 bool isMemri()
const {
return Kind == k_Memri; }
186 assert(Kind == k_Token &&
"Invalid access!");
191 assert((Kind == k_Register || Kind == k_Memri) &&
"Invalid access!");
196 const MCExpr *getImm()
const {
197 assert((Kind == k_Immediate || Kind == k_Memri) &&
"Invalid access!");
201 static std::unique_ptr<AVROperand> CreateToken(
StringRef Str,
SMLoc S) {
202 return std::make_unique<AVROperand>(Str, S);
205 static std::unique_ptr<AVROperand> CreateReg(
unsigned RegNum,
SMLoc S,
207 return std::make_unique<AVROperand>(RegNum, S,
E);
210 static std::unique_ptr<AVROperand> CreateImm(
const MCExpr *Val,
SMLoc S,
212 return std::make_unique<AVROperand>(Val, S,
E);
215 static std::unique_ptr<AVROperand>
217 return std::make_unique<AVROperand>(RegNum, Val, S,
E);
225 void makeReg(
unsigned RegNo) {
227 RegImm = {RegNo,
nullptr};
230 void makeImm(
MCExpr const *Ex) {
235 void makeMemri(
unsigned RegNo,
MCExpr const *Imm) {
237 RegImm = {RegNo, Imm};
240 SMLoc getStartLoc()
const {
return Start; }
241 SMLoc getEndLoc()
const {
return End; }
246 O <<
"Token: \"" <<
getToken() <<
"\"";
249 O <<
"Register: " <<
getReg();
252 O <<
"Immediate: \"" << *getImm() <<
"\"";
257 O <<
"Memri: \"" <<
getReg() <<
'+' << *getImm() <<
"\"";
277 bool AVRAsmParser::invalidOperand(
SMLoc const &Loc,
280 SMLoc ErrorLoc = Loc;
281 char const *Diag = 0;
283 if (ErrorInfo != ~0U) {
284 if (ErrorInfo >= Operands.
size()) {
285 Diag =
"too few operands for instruction.";
287 AVROperand
const &
Op = (AVROperand
const &)*Operands[ErrorInfo];
290 if (Op.getStartLoc() !=
SMLoc()) {
291 ErrorLoc = Op.getStartLoc();
297 Diag =
"invalid operand for instruction";
300 return Error(ErrorLoc, Diag);
303 bool AVRAsmParser::missingFeature(
llvm::SMLoc const &Loc,
304 uint64_t
const &ErrorInfo) {
305 return Error(Loc,
"instruction requires a CPU feature not currently enabled");
315 bool AVRAsmParser::MatchAndEmitInstruction(
SMLoc Loc,
unsigned &Opcode,
318 bool MatchingInlineAsm) {
320 unsigned MatchResult =
321 MatchInstructionImpl(Operands, Inst, ErrorInfo, MatchingInlineAsm);
323 switch (MatchResult) {
324 case Match_Success:
return emit(Inst, Loc, Out);
325 case Match_MissingFeature:
return missingFeature(Loc, ErrorInfo);
326 case Match_InvalidOperand:
return invalidOperand(Loc, Operands, ErrorInfo);
327 case Match_MnemonicFail:
return Error(Loc,
"invalid instruction");
328 default:
return true;
334 int AVRAsmParser::parseRegisterName(
unsigned (*matchFn)(
StringRef)) {
337 int RegNum = matchFn(Name);
343 if (RegNum == AVR::NoRegister) {
344 RegNum = matchFn(Name.
lower());
346 if (RegNum == AVR::NoRegister) {
347 RegNum = matchFn(Name.
upper());
353 int AVRAsmParser::parseRegisterName() {
356 if (RegNum == AVR::NoRegister)
362 int AVRAsmParser::parseRegister() {
363 int RegNum = AVR::NoRegister;
373 RegNum = toDREG(parseRegisterName());
376 RegNum = parseRegisterName();
382 bool AVRAsmParser::tryParseRegisterOperand(
OperandVector &Operands) {
383 int RegNo = parseRegister();
385 if (RegNo == AVR::NoRegister)
395 bool AVRAsmParser::tryParseExpression(
OperandVector &Operands) {
398 if (!tryParseRelocExpression(Operands))
411 if (getParser().parseExpression(Expression))
415 Operands.
push_back(AVROperand::CreateImm(Expression, S, E));
419 bool AVRAsmParser::tryParseRelocExpression(
OperandVector &Operands) {
420 bool isNegated =
false;
429 if (ReadCount == 2) {
464 std::string GSModName = ModifierName.
str() +
"_" + GENERATE_STUBS;
480 MCExpr const *InnerExpression;
481 if (getParser().parseExpression(InnerExpression))
495 isNegated, getContext());
498 Operands.
push_back(AVROperand::CreateImm(Expression, S, E));
506 switch (getLexer().getKind()) {
513 if (!tryParseRegisterOperand(Operands)) {
520 return tryParseExpression(Operands);
525 switch (getLexer().peekTok().getKind()) {
530 if (!tryParseExpression(Operands))
558 RegNo = parseRegister();
560 if (RegNo == AVR::NoRegister)
569 if (getParser().parseExpression(Expression))
575 Operands.
push_back(AVROperand::CreateMemri(RegNo, Expression, S, E));
580 bool AVRAsmParser::ParseRegister(
unsigned &RegNo,
SMLoc &StartLoc,
583 RegNo = parseRegister();
586 return (RegNo == AVR::NoRegister);
589 void AVRAsmParser::eatComma() {
600 Operands.
push_back(AVROperand::CreateToken(Mnemonic, NameLoc));
604 if (!first) eatComma();
608 auto MatchResult = MatchOperandParserImpl(Operands, Mnemonic);
615 SMLoc Loc = getLexer().getLoc();
618 return Error(Loc,
"failed to parse register and immediate pair");
621 if (parseOperand(Operands)) {
622 SMLoc Loc = getLexer().getLoc();
624 return Error(Loc,
"unexpected token in argument list");
633 if (IDVal.
lower() ==
".long") {
635 }
else if (IDVal.
lower() ==
".word" || IDVal.
lower() ==
".short") {
637 }
else if (IDVal.
lower() ==
".byte") {
638 parseLiteralValues(1, DirectiveID.
getLoc());
643 bool AVRAsmParser::parseLiteralValues(
unsigned SizeInBytes,
SMLoc L) {
675 auto parseOne = [&]() ->
bool {
682 return (parseMany(parseOne));
689 #define GET_REGISTER_MATCHER 690 #define GET_MATCHER_IMPLEMENTATION 691 #include "AVRGenAsmMatcher.inc" 695 unsigned ExpectedKind) {
696 AVROperand &Op =
static_cast<AVROperand &
>(AsmOp);
697 MatchClassKind
Expected =
static_cast<MatchClassKind
>(ExpectedKind);
702 if (
MCConstantExpr const *Const = dyn_cast<MCConstantExpr>(Op.getImm())) {
703 int64_t RegNum = Const->getValue();
704 std::ostringstream RegName;
705 RegName <<
"r" << RegNum;
707 if (RegNum != AVR::NoRegister) {
709 if (validateOperandClass(Op, Expected) == Match_Success) {
710 return Match_Success;
720 if (isSubclass(Expected, MCK_DREGS)) {
721 unsigned correspondingDREG = toDREG(Op.getReg());
723 if (correspondingDREG != AVR::NoRegister) {
724 Op.makeReg(correspondingDREG);
725 return validateOperandClass(Op, Expected);
729 return Match_InvalidOperand;
static bool isReg(const MCInst &MI, unsigned OpNo)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmParser()
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
This class represents lattice values for constants.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Generic assembler parser interface, for use by target specific assembly parsers.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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.
void push_back(const T &Elt)
virtual const AsmToken & Lex()=0
Get the next AsmToken in the stream, possibly handling file inclusion first.
const AsmToken & getTok() const
Get the current AsmToken from the stream.
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
Target & getTheAVRTarget()
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string...
VariantKind
Specifies the type of an expression.
static MCOperand createReg(unsigned Reg)
const FeatureBitset & getFeatureBits() const
Generic assembler lexer interface, for use by target specific assembly lexers.
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.
Tagged union holding either a T or a Error.
mir Rename Register Operands
static bool isMem(const MachineInstr &MI, unsigned Op)
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.
MCRegisterClass - Base class of TargetRegisterClass.
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.
Analysis containing CSE Info
virtual void eatToEndOfStatement()=0
Skip to the end of the current statement, for error recovery.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
const char * getPointer() const
Streaming machine code generation interface.
constexpr bool isUInt< 8 >(uint64_t x)
unsigned const MachineRegisterInfo * MRI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Interface to description of machine instruction set.
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...
virtual MCAsmLexer & getLexer()=0
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
static unsigned MatchRegisterAltName(StringRef Name)
Maps from the set of all alternative registernames to a register number.
AsmToken::TokenKind getKind() const
Get the kind of current token.
BlockVerifier::State From
virtual MCStreamer & getStreamer()=0
Return the output streamer for the assembler.
#define LLVM_EXTERNAL_VISIBILITY
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)
Base class for user error types.
static unsigned MatchRegisterName(StringRef Name)
Maps from the set of all register names to a register number.
static SMLoc getFromPointer(const char *Ptr)
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.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static VariantKind getKindByName(StringRef Name)
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())
LLVM Value Representation.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
This class implements an extremely fast bulk output stream that can only output to a stream...
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
virtual size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)=0
Look ahead an arbitrary number of tokens.
Represents a location in source code.
static MCOperand createImm(int64_t Val)
TokenKind getKind() const