[llvm] 15ec0ad - [ms] [llvm-ml] Fix case-sensitivity for variables and textmacros

Eric Astor via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 2 11:08:27 PDT 2021


Author: Eric Astor
Date: 2021-04-02T14:08:02-04:00
New Revision: 15ec0ad77ae3b65e791567854da5b4984030d080

URL: https://github.com/llvm/llvm-project/commit/15ec0ad77ae3b65e791567854da5b4984030d080
DIFF: https://github.com/llvm/llvm-project/commit/15ec0ad77ae3b65e791567854da5b4984030d080.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/MC/MCParser/MasmParser.cpp
    llvm/test/tools/llvm-ml/variable.asm

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 18114b10d5686..b3c854c136d19 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -1547,8 +1547,14 @@ bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
     }
 
     MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
-    if (!Sym)
+    if (!Sym) {
+      // Variables use case-insensitive symbol names; if this is a variable, we
+      // find the symbol using its canonical name.
+      auto VarIt = Variables.find(SymbolName.lower());
+      if (VarIt != Variables.end())
+        SymbolName = VarIt->second.Name;
       Sym = getContext().getOrCreateSymbol(SymbolName);
+    }
 
     // If this is an absolute variable reference, substitute it now to preserve
     // semantics in the face of reassignment.
@@ -3268,7 +3274,7 @@ bool MasmParser::parseIdentifier(StringRef &Res) {
 ///    | name "textequ" text-list
 bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
                                       DirectiveKind DirKind) {
-  Variable &Var = Variables[Name];
+  Variable &Var = Variables[Name.lower()];
   if (Var.Name.empty()) {
     Var.Name = Name;
   } else if (!Var.Redefinable) {
@@ -3276,6 +3282,7 @@ bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
   }
   Var.Redefinable = (DirKind != DK_EQU);
 
+  SMLoc StartLoc = Lexer.getLoc();
   if (DirKind == DK_EQU || DirKind == DK_TEXTEQU) {
     // "equ" and "textequ" both allow text expressions.
     std::string Value;
@@ -3301,7 +3308,7 @@ bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
 
   // Parse as expression assignment.
   const MCExpr *Expr;
-  SMLoc EndLoc, StartLoc = Lexer.getLoc();
+  SMLoc EndLoc;
   if (parseExpression(Expr, EndLoc))
     return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
   MCSymbol *Sym = getContext().getOrCreateSymbol(Var.Name);
@@ -3378,21 +3385,30 @@ bool MasmParser::parseTextItem(std::string &Data) {
   case AsmToken::LessGreater:
     return parseAngleBracketString(Data);
   case AsmToken::Identifier: {
+    // This must be a text macro; we need to expand it accordingly.
     StringRef ID;
     if (parseIdentifier(ID))
       return true;
     Data = ID.str();
 
-    auto it = Variables.find(ID);
-    if (it == Variables.end())
+    auto it = Variables.find(ID.lower());
+    if (it == Variables.end()) {
+      // Not a variable; since we haven't used the token, put it back for better
+      // error recovery.
+      getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
       return true;
+    }
 
     while (it != Variables.end()) {
       const Variable &Var = it->second;
-      if (!Var.IsText)
+      if (!Var.IsText) {
+        // Not a text macro; not usable in TextItem context. Since we haven't
+        // used the token, put it back for better error recovery.
+        getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
         return true;
+      }
       Data = Var.TextValue;
-      it = Variables.find(Data);
+      it = Variables.find(StringRef(Data).lower());
     }
     return false;
   }
@@ -5945,10 +5961,10 @@ bool MasmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
           parseToken(AsmToken::EndOfStatement, "unexpected token in 'ifdef'"))
         return true;
 
-      if (Variables.find(Name) != Variables.end()) {
+      if (Variables.find(Name.lower()) != Variables.end()) {
         is_defined = true;
       } else {
-        MCSymbol *Sym = getContext().lookupSymbol(Name);
+        MCSymbol *Sym = getContext().lookupSymbol(Name.lower());
         is_defined = (Sym && !Sym->isUndefined(false));
       }
     }
@@ -6067,7 +6083,7 @@ bool MasmParser::parseDirectiveElseIfdef(SMLoc DirectiveLoc,
                      "unexpected token in 'elseifdef'"))
         return true;
 
-      if (Variables.find(Name) != Variables.end()) {
+      if (Variables.find(Name.lower()) != Variables.end()) {
         is_defined = true;
       } else {
         MCSymbol *Sym = getContext().lookupSymbol(Name);
@@ -6237,7 +6253,7 @@ bool MasmParser::parseDirectiveErrorIfdef(SMLoc DirectiveLoc,
     if (check(parseIdentifier(Name), "expected identifier after '.errdef'"))
       return true;
 
-    if (Variables.find(Name) != Variables.end()) {
+    if (Variables.find(Name.lower()) != Variables.end()) {
       IsDefined = true;
     } else {
       MCSymbol *Sym = getContext().lookupSymbol(Name);

diff  --git a/llvm/test/tools/llvm-ml/variable.asm b/llvm/test/tools/llvm-ml/variable.asm
index b31879fc28aad..268a03d608fd4 100644
--- a/llvm/test/tools/llvm-ml/variable.asm
+++ b/llvm/test/tools/llvm-ml/variable.asm
@@ -3,11 +3,21 @@
 .data
 t1_value equ 1 or 2
 
-t1 BYTE t1_value DUP (0)
-; CHECK: t1:
+t1 BYTE t1_VALUE DUP (0)
+; CHECK-LABEL: t1:
 ; CHECK-NEXT: .byte 0
 ; CHECK-NEXT: .byte 0
 ; CHECK-NEXT: .byte 0
 ; CHECK-NOT: .byte 0
 
+t2_value equ 4 or t1_value
+t2 BYTE t2_VALUE
+; CHECK-LABEL: t2:
+; CHECK-NEXT: .byte 7
+
+t3_value equ t1_VALUE or 8
+t3 BYTE t3_VALUE
+; CHECK-LABEL: t3:
+; CHECK-NEXT: .byte 11
+
 END


        


More information about the llvm-commits mailing list