[llvm] 6313626 - [AsmParser][SystemZ][z/OS] Support for emitting labels in upper case

Anirudh Prasad via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 24 09:50:18 PDT 2021


Author: Anirudh Prasad
Date: 2021-06-24T12:50:11-04:00
New Revision: 631362665c3b3271736fee070f6b553c099250d8

URL: https://github.com/llvm/llvm-project/commit/631362665c3b3271736fee070f6b553c099250d8
DIFF: https://github.com/llvm/llvm-project/commit/631362665c3b3271736fee070f6b553c099250d8.diff

LOG: [AsmParser][SystemZ][z/OS] Support for emitting labels in upper case

- Currently, the emitting of labels in the parsePrimaryExpr function is case independent. It just takes the identifier and emits it.
- However, for HLASM the emitting of labels is case independent. We are emitting them in the upper case only, to enforce case independency. So we need to ensure that at the time of parsing the label we are emitting the upper case (in `parseAsHLASMLabel`), but also, when we are processing a PC-relative relocatable expression, we need to ensure we emit it in upper case (in `parsePrimaryExpr`)
- To achieve this a new MCAsmInfo attribute has been introduced which corresponding targets can override if needed.

Reviewed By: abhina.sreeskantharajan, uweigand

Differential Revision: https://reviews.llvm.org/D104715

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAsmInfo.h
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
    llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index e1283c4f75137..f5efd0dee21fa 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -153,6 +153,9 @@ class MCAsmInfo {
   /// This is appended to emitted labels.  Defaults to ":"
   const char *LabelSuffix;
 
+  /// Emit labels in purely upper case. Defaults to false.
+  bool EmitLabelsInUpperCase = false;
+
   // Print the EH begin symbol with an assignment. Defaults to false.
   bool UseAssignmentForEHBegin = false;
 
@@ -637,6 +640,7 @@ class MCAsmInfo {
     return EmitGNUAsmStartIndentationMarker;
   }
   const char *getLabelSuffix() const { return LabelSuffix; }
+  bool shouldEmitLabelsInUpperCase() const { return EmitLabelsInUpperCase; }
 
   bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
   bool needsLocalForSize() const { return NeedsLocalForSize; }

diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 8e4752c4b10d3..17e845ddefc69 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1231,7 +1231,8 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
 
     MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
     if (!Sym)
-      Sym = getContext().getOrCreateSymbol(SymbolName);
+      Sym = getContext().getOrCreateSymbol(
+          MAI.shouldEmitLabelsInUpperCase() ? SymbolName.upper() : SymbolName);
 
     // If this is an absolute variable reference, substitute it now to preserve
     // semantics in the face of reassignment.
@@ -6212,8 +6213,10 @@ bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info,
     return Error(LabelLoc,
                  "Cannot have just a label for an HLASM inline asm statement");
 
-  // FIXME: Later on, ensure emitted labels are case-insensitive.
-  MCSymbol *Sym = getContext().getOrCreateSymbol(LabelVal);
+  MCSymbol *Sym = getContext().getOrCreateSymbol(
+      getContext().getAsmInfo()->shouldEmitLabelsInUpperCase()
+          ? LabelVal.upper()
+          : LabelVal);
 
   getTargetParser().doBeforeLabelEmit(Sym);
 

diff  --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
index 2d62558539121..fa48642995869 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp
@@ -31,6 +31,7 @@ SystemZMCAsmInfo::SystemZMCAsmInfo(const Triple &TT) {
   StarIsPC = (AssemblerDialect == AD_HLASM);
   EmitGNUAsmStartIndentationMarker = (AssemblerDialect == AD_ATT);
   AllowAtInName = (AssemblerDialect == AD_HLASM);
+  EmitLabelsInUpperCase = (AssemblerDialect == AD_HLASM);
 
   ZeroDirective = "\t.space\t";
   Data64bitsDirective = "\t.quad\t";

diff  --git a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
index caf1dccb06577..63d6b52deb913 100644
--- a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
+++ b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
@@ -49,6 +49,7 @@ class MockedUpMCAsmInfo : public MCAsmInfo {
   }
   void setAllowDotIsPC(bool Value) { DotIsPC = Value; }
   void setAssemblerDialect(unsigned Value) { AssemblerDialect = Value; }
+  void setEmitLabelsInUpperCase(bool Value) { EmitLabelsInUpperCase = Value; }
 };
 
 // Setup a testing class that the GTest framework can call.
@@ -750,4 +751,36 @@ TEST_F(SystemZAsmLexerTest, CheckPrintAcceptableSymbol2) {
   AsmStr += "#";
   EXPECT_EQ(true, MUPMAI->isValidUnquotedName(AsmStr));
 }
+
+TEST_F(SystemZAsmLexerTest, CheckLabelCaseUpperCase2) {
+  StringRef AsmStr = "label\nlabel";
+
+  // Setup.
+  setupCallToAsmParser(AsmStr);
+
+  // Lex initially to get the string.
+  Parser->getLexer().Lex();
+
+  const MCExpr *Expr;
+  bool ParsePrimaryExpr = Parser->parseExpression(Expr);
+  EXPECT_EQ(ParsePrimaryExpr, false);
+
+  const MCSymbolRefExpr *SymbolExpr = dyn_cast<MCSymbolRefExpr>(Expr);
+  EXPECT_NE(SymbolExpr, nullptr);
+  EXPECT_NE(&SymbolExpr->getSymbol(), nullptr);
+  EXPECT_EQ((&SymbolExpr->getSymbol())->getName(), StringRef("label"));
+
+  // Lex the end of statement token.
+  Parser->getLexer().Lex();
+
+  MUPMAI->setEmitLabelsInUpperCase(true);
+
+  ParsePrimaryExpr = Parser->parseExpression(Expr);
+  EXPECT_EQ(ParsePrimaryExpr, false);
+
+  SymbolExpr = dyn_cast<MCSymbolRefExpr>(Expr);
+  EXPECT_NE(SymbolExpr, nullptr);
+  EXPECT_NE(&SymbolExpr->getSymbol(), nullptr);
+  EXPECT_EQ((&SymbolExpr->getSymbol())->getName(), StringRef("LABEL"));
+}
 } // end anonymous namespace


        


More information about the llvm-commits mailing list