#ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
#define LLVM_MC_MCPARSER_MCASMPARSER_H
#include "llvm/ADT/None.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAsmMacro.h"
#include "llvm/Support/SMLoc.h"
#include <cstdint>
#include <string>
#include <utility>
namespace llvm {
class MCAsmLexer;
class MCAsmInfo;
class MCAsmParserExtension;
class MCContext;
class MCExpr;
class MCInstPrinter;
class MCInstrInfo;
class MCStreamer;
class MCTargetAsmParser;
class SourceMgr;
struct InlineAsmIdentifierInfo {
enum IdKind {
IK_Invalid, IK_Label, IK_EnumVal, IK_Var };
struct EnumIdentifier {
int64_t EnumVal;
};
struct LabelIdentifier {
void *Decl;
};
struct VariableIdentifier {
void *Decl;
bool IsGlobalLV;
unsigned Length;
unsigned Size;
unsigned Type;
};
union {
EnumIdentifier Enum;
LabelIdentifier Label;
VariableIdentifier Var;
};
bool isKind(IdKind kind) const { return Kind == kind; }
void setEnum(int64_t enumVal) {
assert(isKind(IK_Invalid) && "should be initialized only once");
Kind = IK_EnumVal;
Enum.EnumVal = enumVal;
}
void setLabel(void *decl) {
assert(isKind(IK_Invalid) && "should be initialized only once");
Kind = IK_Label;
Label.Decl = decl;
}
void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
assert(isKind(IK_Invalid) && "should be initialized only once");
Kind = IK_Var;
Var.Decl = decl;
Var.IsGlobalLV = isGlobalLV;
Var.Size = size;
Var.Type = type;
Var.Length = size / type;
}
InlineAsmIdentifierInfo() : Kind(IK_Invalid) {}
private:
IdKind Kind;
};
struct AsmTypeInfo {
StringRef Name;
unsigned Size = 0;
unsigned ElementSize = 0;
unsigned Length = 0;
};
struct AsmFieldInfo {
AsmTypeInfo Type;
unsigned Offset = 0;
};
class MCAsmParserSemaCallback {
public:
virtual ~MCAsmParserSemaCallback();
virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
InlineAsmIdentifierInfo &Info,
bool IsUnevaluatedContext) = 0;
virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
SMLoc Location, bool Create) = 0;
virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
unsigned &Offset) = 0;
};
class MCAsmParser {
public:
using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc);
using ExtensionDirectiveHandler =
std::pair<MCAsmParserExtension*, DirectiveHandler>;
struct MCPendingError {
SMLoc Loc;
SmallString<64> Msg;
SMRange Range;
};
private:
MCTargetAsmParser *TargetParser = nullptr;
protected: MCAsmParser();
SmallVector<MCPendingError, 0> PendingErrors;
bool HadError = false;
bool ShowParsedOperands = false;
public:
MCAsmParser(const MCAsmParser &) = delete;
MCAsmParser &operator=(const MCAsmParser &) = delete;
virtual ~MCAsmParser();
virtual void addDirectiveHandler(StringRef Directive,
ExtensionDirectiveHandler Handler) = 0;
virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
virtual SourceMgr &getSourceManager() = 0;
virtual MCAsmLexer &getLexer() = 0;
const MCAsmLexer &getLexer() const {
return const_cast<MCAsmParser*>(this)->getLexer();
}
virtual MCContext &getContext() = 0;
virtual MCStreamer &getStreamer() = 0;
MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
void setTargetParser(MCTargetAsmParser &P);
virtual unsigned getAssemblerDialect() { return 0;}
virtual void setAssemblerDialect(unsigned i) { }
bool getShowParsedOperands() const { return ShowParsedOperands; }
void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
virtual void setParsingMSInlineAsm(bool V) = 0;
virtual bool isParsingMSInlineAsm() = 0;
virtual bool discardLTOSymbol(StringRef) const { return false; }
virtual bool isParsingMasm() const { return false; }
virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
return true;
}
virtual bool lookUpField(StringRef Base, StringRef Member,
AsmFieldInfo &Info) const {
return true;
}
virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
return true;
}
virtual bool parseMSInlineAsm(
std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
SmallVectorImpl<std::string> &Constraints,
SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
virtual void Note(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
virtual bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
bool Error(SMLoc L, const Twine &Msg, SMRange Range = None);
virtual bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) = 0;
bool hasPendingError() { return !PendingErrors.empty(); }
bool printPendingErrors() {
bool rv = !PendingErrors.empty();
for (auto Err : PendingErrors) {
printError(Err.Loc, Twine(Err.Msg), Err.Range);
}
PendingErrors.clear();
return rv;
}
void clearPendingErrors() { PendingErrors.clear(); }
bool addErrorSuffix(const Twine &Suffix);
virtual const AsmToken &Lex() = 0;
const AsmToken &getTok() const;
bool TokError(const Twine &Msg, SMRange Range = None);
bool parseTokenLoc(SMLoc &Loc);
bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
bool parseOptionalToken(AsmToken::TokenKind T);
bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
bool parseEOL();
bool parseEOL(const Twine &ErrMsg);
bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
bool parseIntToken(int64_t &V, const Twine &ErrMsg);
bool check(bool P, const Twine &Msg);
bool check(bool P, SMLoc Loc, const Twine &Msg);
virtual bool parseIdentifier(StringRef &Res) = 0;
virtual StringRef parseStringToEndOfStatement() = 0;
virtual bool parseEscapedString(std::string &Data) = 0;
virtual bool parseAngleBracketString(std::string &Data) = 0;
virtual void eatToEndOfStatement() = 0;
virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
bool parseExpression(const MCExpr *&Res);
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
AsmTypeInfo *TypeInfo) = 0;
virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
virtual bool checkForValidSection() = 0;
virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
SMLoc &EndLoc) = 0;
bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
};
MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
const MCAsmInfo &, unsigned CB = 0);
MCAsmParser *createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &,
const MCAsmInfo &, struct tm, unsigned CB = 0);
}
#endif