From: Michael Zuckerman Date: Wed, 10 May 2017 13:08:11 +0000 (+0000) Subject: [LLVM][inline-asm] Altmacro string escape character '!' X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6361a0b8e9444f5c112e21264fd2a798c311a205;p=llvm [LLVM][inline-asm] Altmacro string escape character '!' 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 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!'. " Differential Revision: https://reviews.llvm.org/D32792 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302652 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/MC/MCParser/AsmParser.cpp b/lib/MC/MCParser/AsmParser.cpp index 66ba853da2f..d213bf3fa3a 100644 --- a/lib/MC/MCParser/AsmParser.cpp +++ b/lib/MC/MCParser/AsmParser.cpp @@ -288,6 +288,7 @@ public: private: bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc); + void altMacroString(StringRef AltMacroStr, std::string &Res); bool parseStatement(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI); bool parseCurlyBlockScope(SmallVectorImpl& AsmStrRewrites); @@ -1209,6 +1210,8 @@ bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { const char *CharPtr = StrLoc.getPointer(); while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && (*CharPtr != '\0')){ + if(*CharPtr == '!') + CharPtr++; CharPtr++; } if (*CharPtr == '>') { @@ -1218,6 +1221,15 @@ bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { 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. /// /// expr ::= expr &&,|| expr -> lowest. @@ -2309,6 +2321,15 @@ bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, (*(Token.getString().begin()) == '%') && Token.is(AsmToken::Integer)) // Emit an integer value to the buffer. OS << Token.getIntVal(); + // Only Token that was validated as a string and begins with '<' + // is considered altMacroString!!! + 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) diff --git a/test/MC/AsmParser/altmacro_string_escape.s b/test/MC/AsmParser/altmacro_string_escape.s new file mode 100644 index 00000000000..bcc9e845953 --- /dev/null +++ b/test/MC/AsmParser/altmacro_string_escape.s @@ -0,0 +1,29 @@ +# RUN: llvm-mc -triple i386-linux-gnu %s| FileCheck %s + +.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!'. + +# CHECK: workForFun: +.macro fun1 number + .if \number=5 + lableNotWork: + .else + workForFun: + .endif +.endm + +# CHECK: workForFun2: +.macro fun2 string + .if \string + workForFun2: + .else + notworkForFun2: + .endif +.endm + +fun1 <5!!> +fun2 <5!>4>