File indexing completed on 2026-05-10 08:44:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_MC_MCPARSER_MCASMPARSER_H
0010 #define LLVM_MC_MCPARSER_MCASMPARSER_H
0011
0012 #include "llvm/ADT/STLFunctionalExtras.h"
0013 #include "llvm/ADT/SmallString.h"
0014 #include "llvm/ADT/SmallVector.h"
0015 #include "llvm/ADT/StringRef.h"
0016 #include "llvm/ADT/Twine.h"
0017 #include "llvm/MC/MCAsmMacro.h"
0018 #include "llvm/Support/SMLoc.h"
0019 #include <cstdint>
0020 #include <string>
0021 #include <utility>
0022
0023 namespace llvm {
0024
0025 class MCAsmLexer;
0026 class MCAsmInfo;
0027 class MCAsmParserExtension;
0028 class MCContext;
0029 class MCExpr;
0030 class MCInstPrinter;
0031 class MCInstrInfo;
0032 class MCStreamer;
0033 class MCTargetAsmParser;
0034 class SourceMgr;
0035
0036 struct InlineAsmIdentifierInfo {
0037 enum IdKind {
0038 IK_Invalid,
0039 IK_Label,
0040 IK_EnumVal,
0041 IK_Var
0042 };
0043
0044 struct EnumIdentifier {
0045 int64_t EnumVal;
0046 };
0047
0048 struct LabelIdentifier {
0049 void *Decl;
0050 };
0051
0052 struct VariableIdentifier {
0053 void *Decl;
0054 bool IsGlobalLV;
0055 unsigned Length;
0056 unsigned Size;
0057 unsigned Type;
0058 };
0059
0060 union {
0061 EnumIdentifier Enum;
0062 LabelIdentifier Label;
0063 VariableIdentifier Var;
0064 };
0065 bool isKind(IdKind kind) const { return Kind == kind; }
0066
0067 void setEnum(int64_t enumVal) {
0068 assert(isKind(IK_Invalid) && "should be initialized only once");
0069 Kind = IK_EnumVal;
0070 Enum.EnumVal = enumVal;
0071 }
0072 void setLabel(void *decl) {
0073 assert(isKind(IK_Invalid) && "should be initialized only once");
0074 Kind = IK_Label;
0075 Label.Decl = decl;
0076 }
0077 void setVar(void *decl, bool isGlobalLV, unsigned size, unsigned type) {
0078 assert(isKind(IK_Invalid) && "should be initialized only once");
0079 Kind = IK_Var;
0080 Var.Decl = decl;
0081 Var.IsGlobalLV = isGlobalLV;
0082 Var.Size = size;
0083 Var.Type = type;
0084 Var.Length = size / type;
0085 }
0086 InlineAsmIdentifierInfo() = default;
0087
0088 private:
0089
0090 IdKind Kind = IK_Invalid;
0091 };
0092
0093
0094
0095 struct AsmTypeInfo {
0096 StringRef Name;
0097 unsigned Size = 0;
0098 unsigned ElementSize = 0;
0099 unsigned Length = 0;
0100 };
0101
0102 struct AsmFieldInfo {
0103 AsmTypeInfo Type;
0104 unsigned Offset = 0;
0105 };
0106
0107
0108 class MCAsmParserSemaCallback {
0109 public:
0110 virtual ~MCAsmParserSemaCallback();
0111
0112 virtual void LookupInlineAsmIdentifier(StringRef &LineBuf,
0113 InlineAsmIdentifierInfo &Info,
0114 bool IsUnevaluatedContext) = 0;
0115 virtual StringRef LookupInlineAsmLabel(StringRef Identifier, SourceMgr &SM,
0116 SMLoc Location, bool Create) = 0;
0117 virtual bool LookupInlineAsmField(StringRef Base, StringRef Member,
0118 unsigned &Offset) = 0;
0119 };
0120
0121
0122
0123 class MCAsmParser {
0124 public:
0125 using DirectiveHandler = bool (*)(MCAsmParserExtension*, StringRef, SMLoc);
0126 using ExtensionDirectiveHandler =
0127 std::pair<MCAsmParserExtension*, DirectiveHandler>;
0128
0129 struct MCPendingError {
0130 SMLoc Loc;
0131 SmallString<64> Msg;
0132 SMRange Range;
0133 };
0134
0135 private:
0136 MCTargetAsmParser *TargetParser = nullptr;
0137
0138 protected:
0139 MCAsmParser();
0140
0141 SmallVector<MCPendingError, 0> PendingErrors;
0142
0143
0144 bool HadError = false;
0145
0146 bool ShowParsedOperands = false;
0147
0148 public:
0149 MCAsmParser(const MCAsmParser &) = delete;
0150 MCAsmParser &operator=(const MCAsmParser &) = delete;
0151 virtual ~MCAsmParser();
0152
0153 virtual void addDirectiveHandler(StringRef Directive,
0154 ExtensionDirectiveHandler Handler) = 0;
0155
0156 virtual void addAliasForDirective(StringRef Directive, StringRef Alias) = 0;
0157
0158 virtual SourceMgr &getSourceManager() = 0;
0159
0160 virtual MCAsmLexer &getLexer() = 0;
0161 const MCAsmLexer &getLexer() const {
0162 return const_cast<MCAsmParser*>(this)->getLexer();
0163 }
0164
0165 virtual MCContext &getContext() = 0;
0166
0167
0168 virtual MCStreamer &getStreamer() = 0;
0169
0170 MCTargetAsmParser &getTargetParser() const { return *TargetParser; }
0171 void setTargetParser(MCTargetAsmParser &P);
0172
0173 virtual unsigned getAssemblerDialect() { return 0;}
0174 virtual void setAssemblerDialect(unsigned i) { }
0175
0176 bool getShowParsedOperands() const { return ShowParsedOperands; }
0177 void setShowParsedOperands(bool Value) { ShowParsedOperands = Value; }
0178
0179
0180 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false) = 0;
0181
0182 virtual void setParsingMSInlineAsm(bool V) = 0;
0183 virtual bool isParsingMSInlineAsm() = 0;
0184
0185 virtual bool discardLTOSymbol(StringRef) const { return false; }
0186
0187 virtual bool isParsingMasm() const { return false; }
0188
0189 virtual bool defineMacro(StringRef Name, StringRef Value) { return true; }
0190
0191 virtual bool lookUpField(StringRef Name, AsmFieldInfo &Info) const {
0192 return true;
0193 }
0194 virtual bool lookUpField(StringRef Base, StringRef Member,
0195 AsmFieldInfo &Info) const {
0196 return true;
0197 }
0198
0199 virtual bool lookUpType(StringRef Name, AsmTypeInfo &Info) const {
0200 return true;
0201 }
0202
0203
0204 virtual bool parseMSInlineAsm(
0205 std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
0206 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
0207 SmallVectorImpl<std::string> &Constraints,
0208 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
0209 MCInstPrinter *IP, MCAsmParserSemaCallback &SI) = 0;
0210
0211
0212 virtual void Note(SMLoc L, const Twine &Msg,
0213 SMRange Range = std::nullopt) = 0;
0214
0215
0216
0217
0218 virtual bool Warning(SMLoc L, const Twine &Msg,
0219 SMRange Range = std::nullopt) = 0;
0220
0221
0222
0223
0224
0225
0226 bool Error(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt);
0227
0228
0229
0230
0231
0232 virtual bool printError(SMLoc L, const Twine &Msg,
0233 SMRange Range = std::nullopt) = 0;
0234
0235 bool hasPendingError() { return !PendingErrors.empty(); }
0236
0237 bool printPendingErrors() {
0238 bool rv = !PendingErrors.empty();
0239 for (auto &Err : PendingErrors) {
0240 printError(Err.Loc, Twine(Err.Msg), Err.Range);
0241 }
0242 PendingErrors.clear();
0243 return rv;
0244 }
0245
0246 void clearPendingErrors() { PendingErrors.clear(); }
0247
0248 bool addErrorSuffix(const Twine &Suffix);
0249
0250
0251
0252 virtual const AsmToken &Lex() = 0;
0253
0254
0255 const AsmToken &getTok() const;
0256
0257
0258 bool TokError(const Twine &Msg, SMRange Range = std::nullopt);
0259
0260 bool parseTokenLoc(SMLoc &Loc);
0261 bool parseToken(AsmToken::TokenKind T, const Twine &Msg = "unexpected token");
0262
0263
0264 bool parseOptionalToken(AsmToken::TokenKind T);
0265
0266 bool parseComma() { return parseToken(AsmToken::Comma, "expected comma"); }
0267 bool parseRParen() { return parseToken(AsmToken::RParen, "expected ')'"); }
0268 bool parseEOL();
0269 bool parseEOL(const Twine &ErrMsg);
0270
0271 bool parseMany(function_ref<bool()> parseOne, bool hasComma = true);
0272
0273 bool parseIntToken(int64_t &V, const Twine &ErrMsg);
0274
0275 bool check(bool P, const Twine &Msg);
0276 bool check(bool P, SMLoc Loc, const Twine &Msg);
0277
0278
0279
0280 virtual bool parseIdentifier(StringRef &Res) = 0;
0281
0282
0283
0284
0285 virtual StringRef parseStringToEndOfStatement() = 0;
0286
0287
0288
0289 virtual bool parseEscapedString(std::string &Data) = 0;
0290
0291
0292
0293 virtual bool parseAngleBracketString(std::string &Data) = 0;
0294
0295
0296 virtual void eatToEndOfStatement() = 0;
0297
0298
0299
0300
0301
0302
0303 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
0304 bool parseExpression(const MCExpr *&Res);
0305
0306
0307
0308
0309
0310
0311 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
0312 AsmTypeInfo *TypeInfo) = 0;
0313
0314
0315
0316
0317
0318
0319
0320 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) = 0;
0321
0322
0323
0324
0325
0326
0327 virtual bool parseAbsoluteExpression(int64_t &Res) = 0;
0328
0329
0330
0331
0332 virtual bool checkForValidSection() = 0;
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 virtual bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
0343 SMLoc &EndLoc) = 0;
0344
0345
0346 bool parseGNUAttribute(SMLoc L, int64_t &Tag, int64_t &IntegerValue);
0347 };
0348
0349
0350 MCAsmParser *createMCAsmParser(SourceMgr &, MCContext &, MCStreamer &,
0351 const MCAsmInfo &, unsigned CB = 0);
0352
0353
0354 MCAsmParser *createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &,
0355 const MCAsmInfo &, struct tm, unsigned CB = 0);
0356
0357 }
0358
0359 #endif