MCAsmParser.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/MC/MCParser/MCAsmParser.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/ADT/Twine.h"
  11. #include "llvm/Config/llvm-config.h"
  12. #include "llvm/MC/MCParser/MCAsmLexer.h"
  13. #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
  14. #include "llvm/MC/MCParser/MCTargetAsmParser.h"
  15. #include "llvm/Support/CommandLine.h"
  16. #include "llvm/Support/Debug.h"
  17. #include "llvm/Support/SMLoc.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <cassert>
  20. using namespace llvm;
  21. cl::opt<unsigned> AsmMacroMaxNestingDepth(
  22. "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
  23. cl::desc("The maximum nesting depth allowed for assembly macros."));
  24. MCAsmParser::MCAsmParser() {}
  25. MCAsmParser::~MCAsmParser() = default;
  26. void MCAsmParser::setTargetParser(MCTargetAsmParser &P) {
  27. assert(!TargetParser && "Target parser is already initialized!");
  28. TargetParser = &P;
  29. TargetParser->Initialize(*this);
  30. }
  31. const AsmToken &MCAsmParser::getTok() const {
  32. return getLexer().getTok();
  33. }
  34. bool MCAsmParser::parseTokenLoc(SMLoc &Loc) {
  35. Loc = getTok().getLoc();
  36. return false;
  37. }
  38. bool MCAsmParser::parseEOL() {
  39. if (getTok().getKind() != AsmToken::EndOfStatement)
  40. return Error(getTok().getLoc(), "expected newline");
  41. Lex();
  42. return false;
  43. }
  44. bool MCAsmParser::parseEOL(const Twine &Msg) {
  45. if (getTok().getKind() != AsmToken::EndOfStatement)
  46. return Error(getTok().getLoc(), Msg);
  47. Lex();
  48. return false;
  49. }
  50. bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) {
  51. if (T == AsmToken::EndOfStatement)
  52. return parseEOL(Msg);
  53. if (getTok().getKind() != T)
  54. return Error(getTok().getLoc(), Msg);
  55. Lex();
  56. return false;
  57. }
  58. bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
  59. if (getTok().getKind() != AsmToken::Integer)
  60. return TokError(Msg);
  61. V = getTok().getIntVal();
  62. Lex();
  63. return false;
  64. }
  65. bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T) {
  66. bool Present = (getTok().getKind() == T);
  67. if (Present)
  68. parseToken(T);
  69. return Present;
  70. }
  71. bool MCAsmParser::check(bool P, const Twine &Msg) {
  72. return check(P, getTok().getLoc(), Msg);
  73. }
  74. bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
  75. if (P)
  76. return Error(Loc, Msg);
  77. return false;
  78. }
  79. bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
  80. return Error(getLexer().getLoc(), Msg, Range);
  81. }
  82. bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
  83. MCPendingError PErr;
  84. PErr.Loc = L;
  85. Msg.toVector(PErr.Msg);
  86. PErr.Range = Range;
  87. PendingErrors.push_back(PErr);
  88. // If we threw this parsing error after a lexing error, this should
  89. // supercede the lexing error and so we remove it from the Lexer
  90. // before it can propagate
  91. if (getTok().is(AsmToken::Error))
  92. getLexer().Lex();
  93. return true;
  94. }
  95. bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
  96. // Make sure lexing errors have propagated to the parser.
  97. if (getTok().is(AsmToken::Error))
  98. Lex();
  99. for (auto &PErr : PendingErrors)
  100. Suffix.toVector(PErr.Msg);
  101. return true;
  102. }
  103. bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
  104. if (parseOptionalToken(AsmToken::EndOfStatement))
  105. return false;
  106. while (true) {
  107. if (parseOne())
  108. return true;
  109. if (parseOptionalToken(AsmToken::EndOfStatement))
  110. return false;
  111. if (hasComma && parseToken(AsmToken::Comma))
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool MCAsmParser::parseExpression(const MCExpr *&Res) {
  117. SMLoc L;
  118. return parseExpression(Res, L);
  119. }
  120. bool MCAsmParser::parseGNUAttribute(SMLoc L, int64_t &Tag,
  121. int64_t &IntegerValue) {
  122. // Parse a .gnu_attribute with numerical tag and value.
  123. StringRef S(L.getPointer());
  124. SMLoc TagLoc;
  125. TagLoc = getTok().getLoc();
  126. const AsmToken &Tok = getTok();
  127. if (Tok.isNot(AsmToken::Integer))
  128. return false;
  129. Tag = Tok.getIntVal();
  130. Lex(); // Eat the Tag
  131. Lex(); // Eat the comma
  132. if (Tok.isNot(AsmToken::Integer))
  133. return false;
  134. IntegerValue = Tok.getIntVal();
  135. Lex(); // Eat the IntegerValue
  136. return true;
  137. }
  138. void MCParsedAsmOperand::dump() const {
  139. // Cannot completely remove virtual function even in release mode.
  140. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  141. dbgs() << " " << *this;
  142. #endif
  143. }