Skip to content

Commit bab6adc

Browse files
ericastormemfrob
authored andcommitted
[ms] [llvm-ml] Fix case-sensitivity for variables and textmacros
Make variables and text-macro references case-insensitive, to match ml.exe. Also improve error handling for text-macro expansion. Reviewed By: thakis Differential Revision: https://reviews.llvm.org/D92503
1 parent de45866 commit bab6adc

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,8 +1547,14 @@ bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
15471547
}
15481548

15491549
MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
1550-
if (!Sym)
1550+
if (!Sym) {
1551+
// Variables use case-insensitive symbol names; if this is a variable, we
1552+
// find the symbol using its canonical name.
1553+
auto VarIt = Variables.find(SymbolName.lower());
1554+
if (VarIt != Variables.end())
1555+
SymbolName = VarIt->second.Name;
15511556
Sym = getContext().getOrCreateSymbol(SymbolName);
1557+
}
15521558

15531559
// If this is an absolute variable reference, substitute it now to preserve
15541560
// semantics in the face of reassignment.
@@ -3268,14 +3274,15 @@ bool MasmParser::parseIdentifier(StringRef &Res) {
32683274
/// | name "textequ" text-list
32693275
bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
32703276
DirectiveKind DirKind) {
3271-
Variable &Var = Variables[Name];
3277+
Variable &Var = Variables[Name.lower()];
32723278
if (Var.Name.empty()) {
32733279
Var.Name = Name;
32743280
} else if (!Var.Redefinable) {
32753281
return TokError("invalid variable redefinition");
32763282
}
32773283
Var.Redefinable = (DirKind != DK_EQU);
32783284

3285+
SMLoc StartLoc = Lexer.getLoc();
32793286
if (DirKind == DK_EQU || DirKind == DK_TEXTEQU) {
32803287
// "equ" and "textequ" both allow text expressions.
32813288
std::string Value;
@@ -3301,7 +3308,7 @@ bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
33013308

33023309
// Parse as expression assignment.
33033310
const MCExpr *Expr;
3304-
SMLoc EndLoc, StartLoc = Lexer.getLoc();
3311+
SMLoc EndLoc;
33053312
if (parseExpression(Expr, EndLoc))
33063313
return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
33073314
MCSymbol *Sym = getContext().getOrCreateSymbol(Var.Name);
@@ -3378,21 +3385,30 @@ bool MasmParser::parseTextItem(std::string &Data) {
33783385
case AsmToken::LessGreater:
33793386
return parseAngleBracketString(Data);
33803387
case AsmToken::Identifier: {
3388+
// This must be a text macro; we need to expand it accordingly.
33813389
StringRef ID;
33823390
if (parseIdentifier(ID))
33833391
return true;
33843392
Data = ID.str();
33853393

3386-
auto it = Variables.find(ID);
3387-
if (it == Variables.end())
3394+
auto it = Variables.find(ID.lower());
3395+
if (it == Variables.end()) {
3396+
// Not a variable; since we haven't used the token, put it back for better
3397+
// error recovery.
3398+
getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
33883399
return true;
3400+
}
33893401

33903402
while (it != Variables.end()) {
33913403
const Variable &Var = it->second;
3392-
if (!Var.IsText)
3404+
if (!Var.IsText) {
3405+
// Not a text macro; not usable in TextItem context. Since we haven't
3406+
// used the token, put it back for better error recovery.
3407+
getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
33933408
return true;
3409+
}
33943410
Data = Var.TextValue;
3395-
it = Variables.find(Data);
3411+
it = Variables.find(StringRef(Data).lower());
33963412
}
33973413
return false;
33983414
}
@@ -5945,10 +5961,10 @@ bool MasmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
59455961
parseToken(AsmToken::EndOfStatement, "unexpected token in 'ifdef'"))
59465962
return true;
59475963

5948-
if (Variables.find(Name) != Variables.end()) {
5964+
if (Variables.find(Name.lower()) != Variables.end()) {
59495965
is_defined = true;
59505966
} else {
5951-
MCSymbol *Sym = getContext().lookupSymbol(Name);
5967+
MCSymbol *Sym = getContext().lookupSymbol(Name.lower());
59525968
is_defined = (Sym && !Sym->isUndefined(false));
59535969
}
59545970
}
@@ -6067,7 +6083,7 @@ bool MasmParser::parseDirectiveElseIfdef(SMLoc DirectiveLoc,
60676083
"unexpected token in 'elseifdef'"))
60686084
return true;
60696085

6070-
if (Variables.find(Name) != Variables.end()) {
6086+
if (Variables.find(Name.lower()) != Variables.end()) {
60716087
is_defined = true;
60726088
} else {
60736089
MCSymbol *Sym = getContext().lookupSymbol(Name);
@@ -6237,7 +6253,7 @@ bool MasmParser::parseDirectiveErrorIfdef(SMLoc DirectiveLoc,
62376253
if (check(parseIdentifier(Name), "expected identifier after '.errdef'"))
62386254
return true;
62396255

6240-
if (Variables.find(Name) != Variables.end()) {
6256+
if (Variables.find(Name.lower()) != Variables.end()) {
62416257
IsDefined = true;
62426258
} else {
62436259
MCSymbol *Sym = getContext().lookupSymbol(Name);

llvm/test/tools/llvm-ml/variable.asm

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
.data
44
t1_value equ 1 or 2
55

6-
t1 BYTE t1_value DUP (0)
7-
; CHECK: t1:
6+
t1 BYTE t1_VALUE DUP (0)
7+
; CHECK-LABEL: t1:
88
; CHECK-NEXT: .byte 0
99
; CHECK-NEXT: .byte 0
1010
; CHECK-NEXT: .byte 0
1111
; CHECK-NOT: .byte 0
1212

13+
t2_value equ 4 or t1_value
14+
t2 BYTE t2_VALUE
15+
; CHECK-LABEL: t2:
16+
; CHECK-NEXT: .byte 7
17+
18+
t3_value equ t1_VALUE or 8
19+
t3 BYTE t3_VALUE
20+
; CHECK-LABEL: t3:
21+
; CHECK-NEXT: .byte 11
22+
1323
END

0 commit comments

Comments
 (0)