[PATCH] D32792: [LLVM][inline-asm] Altmacro string escape character '!'

michael zuckerman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 3 04:05:11 PDT 2017


m_zuckerman created this revision.

This patch is the fourth patch in a series of reviews for the Altmacro feature. This patch introduces a new escape character '!' and it depends on https://reviews.llvm.org/D32701.

according to https://sourceware.org/binutils/docs/as/Altmacro.html:
**"single-character string escape**
To include any single character literally in a string (even if the character would otherwise have some special meaning), you can prefix the character with `!' (an exclamation mark). For example, you can write `<4.3 !> 5.4!!>' to get the literal text `4.3 > 5.4!'. "


https://reviews.llvm.org/D32792

Files:
  lib/MC/MCParser/AsmParser.cpp
  test/MC/AsmParser/altmacro_string_escape.s


Index: test/MC/AsmParser/altmacro_string_escape.s
===================================================================
--- /dev/null
+++ test/MC/AsmParser/altmacro_string_escape.s
@@ -0,0 +1,29 @@
+# RUN: llvm-mc -triple=i386 | FileCheck %s
+# CHECK: workForFun:
+# CHECK: workForFun2:
+
+.altmacro
+# single-character string escape
+# To include any single character literally in a string 
+# (even if the character would otherwise have some special meaning), 
+# you can prefix the character with `!'. 
+# For example, you can write `<4.3 !> 5.4!!>' to get the literal text `4.3 > 5.4!'. 
+
+.macro fun1 number
+  .if \number=5
+    lableNotWork:
+  .else
+    workForFun:
+  .endif
+.endm
+
+.macro fun2 string
+  .if \string
+    workForFun2:
+  .else
+    notworkForFun2:
+  .endif
+.endm
+
+fun1 <5!!>
+fun2 <5!>4>
Index: lib/MC/MCParser/AsmParser.cpp
===================================================================
--- lib/MC/MCParser/AsmParser.cpp
+++ lib/MC/MCParser/AsmParser.cpp
@@ -288,6 +288,7 @@
 
 private:
   bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc);
+  void altMacroString(StringRef AltMacroStr, std::string &Res);
   bool parseStatement(ParseStatementInfo &Info,
                       MCAsmParserSemaCallback *SI);
   bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
@@ -1204,14 +1205,24 @@
   const char *CharPtr = StrLoc.getPointer();
   while ((*CharPtr != '>') && (*CharPtr != '\'') && (*CharPtr != '\n') &&
 	     (*CharPtr != '\r') && (*CharPtr != '\0')){
+	  if(*CharPtr == '!')
+		  CharPtr++;
     CharPtr++;
   }
   if (*CharPtr == '>') {
     EndLoc = StrLoc.getFromPointer(CharPtr + 1);
     return true;
   }
   return false;
 }
+/// \brief creating a string without the escape characters '!'. 
+void AsmParser::altMacroString(StringRef AltMacroStr,std::string &Res) {
+  for (int Pos = 0; Pos < AltMacroStr.size(); Pos++) {
+    if (AltMacroStr[Pos] == '!')
+      Pos++;
+    Res += AltMacroStr[Pos];
+  }
+}
 
 /// \brief Parse an expression and return it.
 ///
@@ -2304,6 +2315,13 @@
                  (*(Token.getString().begin()) == '%') && Token.is(AsmToken::Integer))
               // Emit an integer value to the buffer.
               OS << Token.getIntVal();
+            else if ((Lexer.IsaAltMacroMode()) &&
+                     (*(Token.getString().begin()) == '<') &&
+                     Token.is(AsmToken::String)) {
+              std::string Res;
+              altMacroString(Token.getStringContents(), Res);
+              OS << Res;
+            }
             // We expect no quotes around the string's contents when
             // parsing for varargs.
             else if (Token.isNot(AsmToken::String) || VarargParameter)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32792.97588.patch
Type: text/x-patch
Size: 2721 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170503/68bf34b9/attachment.bin>


More information about the llvm-commits mailing list