Index: include/llvm/MC/MCParser/AsmLexer.h =================================================================== --- include/llvm/MC/MCParser/AsmLexer.h +++ include/llvm/MC/MCParser/AsmLexer.h @@ -64,7 +64,7 @@ AsmToken LexLineComment(); AsmToken LexDigit(); AsmToken LexSingleQuote(); - AsmToken LexQuote(); + AsmToken LexQuote(char Dilimiter = '"'); AsmToken LexFloatLiteral(); AsmToken LexHexFloatLiteral(bool NoIntDigits); Index: lib/MC/MCParser/AsmLexer.cpp =================================================================== --- lib/MC/MCParser/AsmLexer.cpp +++ lib/MC/MCParser/AsmLexer.cpp @@ -429,6 +429,9 @@ if (CurChar == '\\') CurChar = getNextChar(); + if (IsaAltMacroMode()) + return LexQuote('\''); + if (CurChar == EOF) return ReturnError(TokStart, "unterminated single quote"); @@ -458,10 +461,10 @@ } /// LexQuote: String: "..." -AsmToken AsmLexer::LexQuote() { +AsmToken AsmLexer::LexQuote(char Dilimiter) { int CurChar = getNextChar(); // TODO: does gas allow multiline string constants? - while (CurChar != '"') { + while (CurChar != Dilimiter) { if (CurChar == '\\') { // Allow \", etc. CurChar = getNextChar(); Index: lib/MC/MCParser/AsmParser.cpp =================================================================== --- lib/MC/MCParser/AsmParser.cpp +++ lib/MC/MCParser/AsmParser.cpp @@ -287,6 +287,7 @@ /// } private: + bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc); bool parseStatement(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI); bool parseCurlyBlockScope(SmallVectorImpl& AsmStrRewrites); @@ -1191,6 +1192,27 @@ llvm_unreachable("Invalid expression kind!"); } +/// This function checks if the next token is type or arithmetic. +/// string that begin with character '<' must end with character '>'. +/// otherwise it is arithmetics. +/// If the function returns a 'true' value, +/// the End argument will be filled with the last location pointed to the '>' +/// character. +bool AsmParser::isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { + assert((StrLoc.getPointer() != NULL) && + "Argument to the function cannot be a NULL value"); + const char *CharPtr = StrLoc.getPointer(); + while ((*CharPtr != '>') && (*CharPtr != '\n') && + (*CharPtr != '\r') && (*CharPtr != '\0')){ + CharPtr++; + } + if (*CharPtr == '>') { + EndLoc = StrLoc.getFromPointer(CharPtr + 1); + return true; + } + return false; +} + /// \brief Parse an expression and return it. /// /// expr ::= expr &&,|| expr -> lowest. @@ -2460,9 +2482,9 @@ if (NamedParametersFound && FA.Name.empty()) return Error(IDLoc, "cannot mix positional and keyword arguments"); + SMLoc StrLoc = Lexer.getLoc(); + SMLoc EndLoc; if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Percent)) { - SMLoc StrLoc = Lexer.getLoc(); - SMLoc EndLoc; const MCExpr *AbsoluteExp; int64_t Value; /// Eat '%' @@ -2475,8 +2497,16 @@ const char *EndChar = EndLoc.getPointer(); AsmToken newToken(AsmToken::Integer, StringRef(StrChar , EndChar - StrChar), Value); FA.Value.push_back(newToken); - } - else if(parseMacroArgument(FA.Value, Vararg)) + } else if (Lexer.IsaAltMacroMode() && Lexer.is(AsmToken::Less) && + isAltmacroString(StrLoc, EndLoc)) { + const char *StrChar = StrLoc.getPointer(); + const char *EndChar = EndLoc.getPointer(); + jumpToLoc(EndLoc, CurBuffer); + /// Eat from '<' to '>' + Lex(); + AsmToken newToken(AsmToken::String, StringRef(StrChar, EndChar - StrChar)); + FA.Value.push_back(newToken); + } else if(parseMacroArgument(FA.Value, Vararg)) return true; unsigned PI = Parameter; Index: test/MC/AsmParser/altmacro_string.s =================================================================== --- /dev/null +++ test/MC/AsmParser/altmacro_string.s @@ -0,0 +1,83 @@ +# RUN: llvm-mc %s| FileCheck %s + +# This test checks the altmacro string delimiter '<' and '>'. + +.altmacro + +# Test #1: +# You can delimit strings with matching angle brackets '<' '>' +# or with matching '\''. +# If an argument begins with '<' and ends with '>'. +# Or an argument begins with '\'' and ends with '\''. +# The argument is considered as a string. + +# CHECK: simpleCheck: +# CHECK: simpleCheck0: +.macro simple_check_0 name + \name: + addl $5,%eax +.endm + +simple_check_0 +simple_check_0 'simpleCheck0' + +# Test #2: +# Except adding new string marks '<..>', a regular macro behavior is expected. + +# CHECK: simpleCheck1: +# CHECK: addl $0, %eax +.macro concat string1 string2 string3 + \string1\string2\string3: + addl $\string3, %eax +.endm + +concat ,,<1> + +# Test #3: +# The altmacro cannot affect the regular less/greater behavior. + +# CHECK: addl $1, %eax +# CHECK: addl $0, %eax + +.macro fun3 arg1 arg2 + addl $\arg1,%eax + addl $\arg2,%eax +.endm + +fun3 5<6 , 5>8 + +# Test #4: +# If a comma is present inside an angle brackets, +# the comma considered as a character and not as a separator. +# This check checks the ability to split the string to different +# arguments according to the use of the comma. +# Fun2 sees the comma as a character. +# Fun3 sees the comma as a separator. + +# CHECK: addl $5, %eax +# CHECK: addl $6, %eax +# CHECK: addl $6, %eax +# CHECK: addl $7, %eax +.macro fun2 arg + fun3 \arg +.endm + +fun2 <5,6> +fun2 '6,7' + +# Test #5: +# If argument begin with '<' and there is no '>' to close it. +# A regular macro behavior is expected. + +# CHECK: addl $4, %eax +# CHECK: addl $3, %eax +.macro fun4 arg1 arg2 + .if \arg2\arg1 + addl $\arg2,%eax + .endif +.endm + +fun4 <5,4 +fun4 '<5',3 + +.noaltmacro \ No newline at end of file