aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCParser/MasmParser.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2022-07-03 14:10:23 +0000
committerDimitry Andric <dim@FreeBSD.org>2022-07-03 14:10:23 +0000
commit145449b1e420787bb99721a429341fa6be3adfb6 (patch)
tree1d56ae694a6de602e348dd80165cf881a36600ed /llvm/lib/MC/MCParser/MasmParser.cpp
parentecbca9f5fb7d7613d2b94982c4825eb0d33d6842 (diff)
Diffstat (limited to 'llvm/lib/MC/MCParser/MasmParser.cpp')
-rw-r--r--llvm/lib/MC/MCParser/MasmParser.cpp139
1 files changed, 75 insertions, 64 deletions
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index f9433240743d..8c582d225e30 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -14,7 +14,6 @@
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
-#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
@@ -36,21 +35,19 @@
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCInstrInfo.h"
-#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCParser/AsmCond.h"
#include "llvm/MC/MCParser/AsmLexer.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
-#include "llvm/MC/MCParser/MCAsmParserUtils.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
+#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCTargetOptions.h"
-#include "llvm/MC/MCValue.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
@@ -64,7 +61,6 @@
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
-#include <cctype>
#include <climits>
#include <cstddef>
#include <cstdint>
@@ -980,6 +976,8 @@ private:
bool parseDirectiveEnds(StringRef Name, SMLoc NameLoc);
bool parseDirectiveNestedEnds();
+ bool parseDirectiveExtern();
+
/// Parse a directive like ".globl" which accepts a single symbol (which
/// should be a label or an external).
bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
@@ -1192,7 +1190,7 @@ bool MasmParser::expandMacros() {
}
}
- if (!ExpandedValue.hasValue())
+ if (!ExpandedValue)
return true;
std::unique_ptr<MemoryBuffer> Instantiation =
MemoryBuffer::getMemBufferCopy(*ExpandedValue, "<instantiation>");
@@ -1431,7 +1429,7 @@ bool MasmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
// Finalize the output stream if there are no errors and if the client wants
// us to.
if (!HadError && !NoFinalize)
- Out.Finish(Lexer.getLoc());
+ Out.finish(Lexer.getLoc());
return HadError || getContext().hadError();
}
@@ -2094,7 +2092,7 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
// If this is a line comment we can drop it safely.
if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
getTok().getString().front() == '\n')
- Out.AddBlankLine();
+ Out.addBlankLine();
Lex();
return false;
}
@@ -2283,7 +2281,7 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
}
// Consume any end of statement token, if present, to avoid spurious
- // AddBlankLine calls().
+ // addBlankLine calls().
if (getTok().is(AsmToken::EndOfStatement)) {
Lex();
}
@@ -2409,8 +2407,7 @@ bool MasmParser::parseStatement(ParseStatementInfo &Info,
case DK_ORG:
return parseDirectiveOrg();
case DK_EXTERN:
- eatToEndOfStatement(); // .extern is the default, ignore it.
- return false;
+ return parseDirectiveExtern();
case DK_PUBLIC:
return parseDirectiveSymbolAttribute(MCSA_Global);
case DK_COMM:
@@ -2905,7 +2902,7 @@ bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
if (Body[Pos] == '&')
break;
if (isMacroParameterChar(Body[Pos])) {
- if (!CurrentQuote.hasValue())
+ if (!CurrentQuote)
break;
if (IdentifierPos == End)
IdentifierPos = Pos;
@@ -2914,7 +2911,7 @@ bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
}
// Track quotation status
- if (!CurrentQuote.hasValue()) {
+ if (!CurrentQuote) {
if (Body[Pos] == '\'' || Body[Pos] == '"')
CurrentQuote = Body[Pos];
} else if (Body[Pos] == CurrentQuote) {
@@ -3333,7 +3330,7 @@ bool MasmParser::handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc) {
ParseStatementInfo Info(&AsmStrRewrites);
bool Parsed = parseStatement(Info, nullptr);
- if (!Parsed && Info.ExitValue.hasValue()) {
+ if (!Parsed && Info.ExitValue) {
ExitValue = std::move(*Info.ExitValue);
break;
}
@@ -3628,7 +3625,7 @@ bool MasmParser::parseTextItem(std::string &Data) {
if (BuiltinIt != BuiltinSymbolMap.end()) {
llvm::Optional<std::string> BuiltinText =
evaluateBuiltinTextMacro(BuiltinIt->getValue(), StartLoc);
- if (!BuiltinText.hasValue()) {
+ if (!BuiltinText) {
// Not a text macro; break without substituting
break;
}
@@ -4242,7 +4239,7 @@ bool MasmParser::parseStructInitializer(const StructInfo &Structure,
auto &FieldInitializers = Initializer.FieldInitializers;
size_t FieldIndex = 0;
- if (EndToken.hasValue()) {
+ if (EndToken) {
// Initialize all fields with given initializers.
while (getTok().isNot(EndToken.getValue()) &&
FieldIndex < Structure.Fields.size()) {
@@ -4275,7 +4272,7 @@ bool MasmParser::parseStructInitializer(const StructInfo &Structure,
FieldInitializers.push_back(Field.Contents);
}
- if (EndToken.hasValue()) {
+ if (EndToken) {
if (EndToken.getValue() == AsmToken::Greater)
return parseAngleBracketClose();
@@ -4763,7 +4760,7 @@ bool MasmParser::emitAlignTo(int64_t Alignment) {
// directive.
const MCSection *Section = getStreamer().getCurrentSectionOnly();
assert(Section && "must have section to emit alignment");
- if (Section->UseCodeAlign()) {
+ if (Section->useCodeAlign()) {
getStreamer().emitCodeAlignment(Alignment, &getTargetParser().getSTI(),
/*MaxBytesToEmit=*/0);
} else {
@@ -4911,8 +4908,8 @@ bool MasmParser::parseDirectiveFile(SMLoc DirectiveLoc) {
if (HasMD5) {
MD5::MD5Result Sum;
for (unsigned i = 0; i != 8; ++i) {
- Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
- Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
+ Sum[i] = uint8_t(MD5Hi >> ((7 - i) * 8));
+ Sum[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8));
}
CKMem = Sum;
}
@@ -4952,8 +4949,7 @@ bool MasmParser::parseDirectiveLine() {
(void)LineNumber;
// FIXME: Do something with the .line.
}
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.line' directive"))
+ if (parseEOL())
return true;
return false;
@@ -5086,8 +5082,7 @@ bool MasmParser::parseDirectiveCVFile() {
parseEscapedString(Checksum) ||
parseIntToken(ChecksumKind,
"expected checksum kind in '.cv_file' directive") ||
- parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.cv_file' directive"))
+ parseEOL())
return true;
}
@@ -5097,7 +5092,7 @@ bool MasmParser::parseDirectiveCVFile() {
ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem),
Checksum.size());
- if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
+ if (!getStreamer().emitCVFileDirective(FileNumber, Filename, ChecksumAsBytes,
static_cast<uint8_t>(ChecksumKind)))
return Error(FileNumberLoc, "file number already allocated");
@@ -5133,12 +5128,10 @@ bool MasmParser::parseDirectiveCVFuncId() {
SMLoc FunctionIdLoc = getTok().getLoc();
int64_t FunctionId;
- if (parseCVFunctionId(FunctionId, ".cv_func_id") ||
- parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.cv_func_id' directive"))
+ if (parseCVFunctionId(FunctionId, ".cv_func_id") || parseEOL())
return true;
- if (!getStreamer().EmitCVFuncIdDirective(FunctionId))
+ if (!getStreamer().emitCVFuncIdDirective(FunctionId))
return Error(FunctionIdLoc, "function id already allocated");
return false;
@@ -5194,11 +5187,10 @@ bool MasmParser::parseDirectiveCVInlineSiteId() {
Lex();
}
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.cv_inline_site_id' directive"))
+ if (parseEOL())
return true;
- if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
+ if (!getStreamer().emitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile,
IALine, IACol, FunctionIdLoc))
return Error(FunctionIdLoc, "function id already allocated");
@@ -5321,7 +5313,7 @@ bool MasmParser::parseDirectiveCVInlineLinetable() {
"expected identifier in directive"))
return true;
- if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
+ if (parseEOL())
return true;
MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName);
@@ -5482,7 +5474,7 @@ bool MasmParser::parseDirectiveCVFileChecksumOffset() {
int64_t FileNo;
if (parseIntToken(FileNo, "expected identifier in directive"))
return true;
- if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement"))
+ if (parseEOL())
return true;
getStreamer().emitCVFileChecksumOffsetDirective(FileNo);
return false;
@@ -5498,7 +5490,7 @@ bool MasmParser::parseDirectiveCVFPOData() {
if (parseEOL("unexpected tokens"))
return addErrorSuffix(" in '.cv_fpo_data' directive");
MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName);
- getStreamer().EmitCVFPOData(ProcSym, DirLoc);
+ getStreamer().emitCVFPOData(ProcSym, DirLoc);
return false;
}
@@ -5791,8 +5783,7 @@ bool MasmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) {
/// parseDirectiveCFISignalFrame
/// ::= .cfi_signal_frame
bool MasmParser::parseDirectiveCFISignalFrame() {
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.cfi_signal_frame'"))
+ if (parseEOL())
return true;
getStreamer().emitCFISignalFrame();
@@ -6023,6 +6014,39 @@ bool MasmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
return false;
}
+bool MasmParser::parseDirectiveExtern() {
+ // .extern is the default - but we still need to take any provided type info.
+ auto parseOp = [&]() -> bool {
+ StringRef Name;
+ SMLoc NameLoc = getTok().getLoc();
+ if (parseIdentifier(Name))
+ return Error(NameLoc, "expected name");
+ if (parseToken(AsmToken::Colon))
+ return true;
+
+ StringRef TypeName;
+ SMLoc TypeLoc = getTok().getLoc();
+ if (parseIdentifier(TypeName))
+ return Error(TypeLoc, "expected type");
+ if (!TypeName.equals_insensitive("proc")) {
+ AsmTypeInfo Type;
+ if (lookUpType(TypeName, Type))
+ return Error(TypeLoc, "unrecognized type");
+ KnownType[Name.lower()] = Type;
+ }
+
+ MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
+ Sym->setExternal(true);
+ getStreamer().emitSymbolAttribute(Sym, MCSA_Extern);
+
+ return false;
+ };
+
+ if (parseMany(parseOp))
+ return addErrorSuffix(" in directive 'extern'");
+ return false;
+}
+
/// parseDirectiveSymbolAttribute
/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
bool MasmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
@@ -6091,8 +6115,7 @@ bool MasmParser::parseDirectiveComm(bool IsLocal) {
}
}
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.comm' or '.lcomm' directive"))
+ if (parseEOL())
return true;
// NOTE: a size of zero for a .comm should create a undefined symbol
@@ -6138,8 +6161,7 @@ bool MasmParser::parseDirectiveComment(SMLoc DirectiveLoc) {
Lex(); // eat end of statement
} while (
!StringRef(parseStringTo(AsmToken::EndOfStatement)).contains(Delimiter));
- return parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'comment' directive");
+ return parseEOL();
}
/// parseDirectiveInclude
@@ -6173,9 +6195,7 @@ bool MasmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
eatToEndOfStatement();
} else {
int64_t ExprValue;
- if (parseAbsoluteExpression(ExprValue) ||
- parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.if' directive"))
+ if (parseAbsoluteExpression(ExprValue) || parseEOL())
return true;
switch (DirKind) {
@@ -6208,8 +6228,7 @@ bool MasmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
if (parseTextItem(Str))
return TokError("expected text item parameter for 'ifb' directive");
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'ifb' directive"))
+ if (parseEOL())
return true;
TheCondState.CondMet = ExpectBlank == Str.empty();
@@ -6275,7 +6294,7 @@ bool MasmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
if (!is_defined) {
StringRef Name;
if (check(parseIdentifier(Name), "expected identifier after 'ifdef'") ||
- parseToken(AsmToken::EndOfStatement, "unexpected token in 'ifdef'"))
+ parseEOL())
return true;
if (BuiltinSymbolMap.find(Name.lower()) != BuiltinSymbolMap.end()) {
@@ -6316,8 +6335,7 @@ bool MasmParser::parseDirectiveElseIf(SMLoc DirectiveLoc,
if (parseAbsoluteExpression(ExprValue))
return true;
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.elseif' directive"))
+ if (parseEOL())
return true;
switch (DirKind) {
@@ -6360,8 +6378,7 @@ bool MasmParser::parseDirectiveElseIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
return TokError("expected text item parameter for 'elseifnb' directive");
}
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'elseifb' directive"))
+ if (parseEOL())
return true;
TheCondState.CondMet = ExpectBlank == Str.empty();
@@ -6398,8 +6415,7 @@ bool MasmParser::parseDirectiveElseIfdef(SMLoc DirectiveLoc,
StringRef Name;
if (check(parseIdentifier(Name),
"expected identifier after 'elseifdef'") ||
- parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'elseifdef'"))
+ parseEOL())
return true;
if (BuiltinSymbolMap.find(Name.lower()) != BuiltinSymbolMap.end()) {
@@ -6475,8 +6491,7 @@ bool MasmParser::parseDirectiveElseIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
/// parseDirectiveElse
/// ::= else
bool MasmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'else' directive"))
+ if (parseEOL())
return true;
if (TheCondState.TheCond != AsmCond::IfCond &&
@@ -6498,8 +6513,7 @@ bool MasmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
/// parseDirectiveEnd
/// ::= end
bool MasmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in 'end' directive"))
+ if (parseEOL())
return true;
while (Lexer.isNot(AsmToken::Eof))
@@ -6687,8 +6701,7 @@ bool MasmParser::parseDirectiveErrorIfe(SMLoc DirectiveLoc, bool ExpectZero) {
/// parseDirectiveEndIf
/// ::= .endif
bool MasmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
- if (parseToken(AsmToken::EndOfStatement,
- "unexpected token in '.endif' directive"))
+ if (parseEOL())
return true;
if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
@@ -6982,9 +6995,7 @@ bool MasmParser::parseDirectiveRepeat(SMLoc DirectiveLoc, StringRef Dir) {
return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
}
- if (check(Count < 0, CountLoc, "Count is negative") ||
- parseToken(AsmToken::EndOfStatement,
- "unexpected token in '" + Dir + "' directive"))
+ if (check(Count < 0, CountLoc, "Count is negative") || parseEOL())
return true;
// Lex the repeat definition.
@@ -7099,7 +7110,7 @@ bool MasmParser::parseDirectiveFor(SMLoc DirectiveLoc, StringRef Dir) {
if (parseToken(AsmToken::Greater,
"values in '" + Dir +
"' directive must be enclosed in angle brackets") ||
- parseToken(AsmToken::EndOfStatement, "expected End of Statement"))
+ parseEOL())
return true;
// Lex the for definition.
@@ -7149,7 +7160,7 @@ bool MasmParser::parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive) {
}
Argument.resize(End);
}
- if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
+ if (parseEOL())
return true;
// Lex the irpc definition.