1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[AsmParser][SystemZ][z/OS] Add in support to allow use of additional comment strings.

- Currently, MCAsmInfo provides a CommentString attribute, that various targets can set, so that the AsmLexer can appropriately lex a string as a comment based on the set value of the attribute.
- However, AsmLexer also supports a few additional comment syntaxes, in addition to what's specified as a CommentString attribute. This includes regular C-style block comments (/* ... */), regular C-style line comments (// .... ) and #. While I'm not sure as to why this behaviour exists, I am assuming it does to maintain backward compatibility with GNU AS (see https://sourceware.org/binutils/docs/as/Comments.html#Comments for reference)
For example:
Consider a target which sets the CommentString attribute to '*'.
The following strings are all lexed as comments.

```
"# abc" -> comment
"// abc" -> comment
"/* abc */ -> comment
"* abc" -> comment
```

- In HLASM however, only "*" is accepted as a comment string, and nothing else.
- To achieve this, an additional attribute (`AllowAdditionalComments`) has been added to MCAsmInfo. If this attribute is set to false, then only the string specified by the CommentString attribute is used as a possible comment string to be lexed by the AsmLexer. The regular C-style block comments, line comments and "#" are disabled. As a final note, "#" will still be treated as a comment, if the CommentString attribute is set to "#".

Depends on https://reviews.llvm.org/D99277

Reviewed By: abhina.sreeskantharajan, myiwanch

Differential Revision: https://reviews.llvm.org/D99286
This commit is contained in:
Anirudh Prasad 2021-04-13 11:07:46 -04:00
parent 88511b573a
commit d7da6c0d43
4 changed files with 173 additions and 1 deletions

View File

@ -130,6 +130,14 @@ protected:
/// at the beginning of statements. Defaults to false.
bool RestrictCommentStringToStartOfStatement = false;
/// This indicates whether to allow additional "comment strings" to be lexed
/// as a comment. Setting this attribute to true, will ensure that C-style
/// line comments (// ..), C-style block comments (/* .. */), and "#" are
/// all treated as comments in addition to the string specified by the
/// CommentString attribute.
/// Default is true.
bool AllowAdditionalComments = true;
/// This is appended to emitted labels. Defaults to ":"
const char *LabelSuffix;
@ -567,6 +575,7 @@ public:
bool getRestrictCommentStringToStartOfStatement() const {
return RestrictCommentStringToStartOfStatement;
}
bool shouldAllowAdditionalComments() const { return AllowAdditionalComments; }
const char *getLabelSuffix() const { return LabelSuffix; }
bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }

View File

@ -175,7 +175,13 @@ AsmToken AsmLexer::LexIdentifier() {
/// LexSlash: Slash: /
/// C-Style Comment: /* ... */
/// C-style Comment: // ...
AsmToken AsmLexer::LexSlash() {
if (!MAI.shouldAllowAdditionalComments()) {
IsAtStartOfStatement = false;
return AsmToken(AsmToken::Slash, StringRef(TokStart, 1));
}
switch (*CurPtr) {
case '*':
IsAtStartOfStatement = false;
@ -729,7 +735,9 @@ AsmToken AsmLexer::LexToken() {
UnLex(TokenBuf[0]);
return AsmToken(AsmToken::HashDirective, s);
}
return LexLineComment();
if (MAI.shouldAllowAdditionalComments())
return LexLineComment();
}
if (isAtStartOfComment(TokStart))

View File

@ -23,6 +23,7 @@ SystemZMCAsmInfo::SystemZMCAsmInfo(const Triple &TT) {
CommentString = AssemblerDialect == AD_HLASM ? "*" : "#";
RestrictCommentStringToStartOfStatement = (AssemblerDialect == AD_HLASM);
AllowAdditionalComments = (AssemblerDialect == AD_ATT);
ZeroDirective = "\t.space\t";
Data64bitsDirective = "\t.quad\t";
UsesELFSectionDirectiveForBSS = true;

View File

@ -32,6 +32,9 @@ public:
RestrictCommentStringToStartOfStatement = Value;
}
void setCommentString(StringRef Value) { CommentString = Value; }
void setAllowAdditionalComments(bool Value) {
AllowAdditionalComments = Value;
}
};
// Setup a testing class that the GTest framework can call.
@ -213,4 +216,155 @@ TEST_F(SystemZAsmLexerTest, CheckAllowHashInIdentifier2) {
AsmToken::EndOfStatement, AsmToken::Eof});
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, DontCheckStrictCommentString) {
StringRef AsmStr = "# abc\n/* def */// xyz";
// Setup.
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
SmallVector<AsmToken::TokenKind> ExpectedTokens(
{AsmToken::EndOfStatement, AsmToken::Comment, AsmToken::EndOfStatement,
AsmToken::Eof});
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, DontCheckStrictCommentString2) {
StringRef AsmStr = "# abc\n/* def */// xyz\n* rst";
// Setup.
MUPMAI->setCommentString("*");
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
SmallVector<AsmToken::TokenKind> ExpectedTokens(
{AsmToken::EndOfStatement, AsmToken::Comment, AsmToken::EndOfStatement,
AsmToken::EndOfStatement, AsmToken::Eof});
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, CheckStrictCommentString) {
StringRef AsmStr = "# abc\n/* def */// xyz";
// Setup.
MUPMAI->setAllowAdditionalComments(false);
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
// "# abc" -> still treated as a comment, since CommentString
// is set to "#"
SmallVector<AsmToken::TokenKind> ExpectedTokens;
ExpectedTokens.push_back(AsmToken::EndOfStatement); // "# abc\n"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Star); // "*"
ExpectedTokens.push_back(AsmToken::Identifier); // "def"
ExpectedTokens.push_back(AsmToken::Star); // "*"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Identifier); // "xyz"
ExpectedTokens.push_back(AsmToken::EndOfStatement);
ExpectedTokens.push_back(AsmToken::Eof);
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, CheckStrictCommentString2) {
StringRef AsmStr = "// abc";
// Setup.
MUPMAI->setAllowAdditionalComments(false);
MUPMAI->setCommentString("//");
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
// "// abc" -> will still be treated as a comment because "//" is the
// CommentString
SmallVector<AsmToken::TokenKind> ExpectedTokens(
{AsmToken::EndOfStatement, AsmToken::Eof});
lexAndCheckTokens(AsmStr /* "// abc" */, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, CheckStrictCommentString3) {
StringRef AsmStr = "/* abc */";
// Setup.
MUPMAI->setAllowAdditionalComments(false);
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
SmallVector<AsmToken::TokenKind> ExpectedTokens;
ExpectedTokens.push_back(AsmToken::Slash);
ExpectedTokens.push_back(AsmToken::Star);
ExpectedTokens.push_back(AsmToken::Identifier);
ExpectedTokens.push_back(AsmToken::Star);
ExpectedTokens.push_back(AsmToken::Slash);
ExpectedTokens.push_back(AsmToken::EndOfStatement);
ExpectedTokens.push_back(AsmToken::Eof);
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, CheckStrictCommentString4) {
StringRef AsmStr = "# abc\n/* def */// xyz";
// Setup.
MUPMAI->setCommentString("*");
MUPMAI->setAllowAdditionalComments(false);
MUPMAI->setRestrictCommentStringToStartOfStatement(true);
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
SmallVector<AsmToken::TokenKind> ExpectedTokens;
ExpectedTokens.push_back(AsmToken::Hash); // "#"
ExpectedTokens.push_back(AsmToken::Identifier); // "abc"
ExpectedTokens.push_back(AsmToken::EndOfStatement); // "\n"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Star); // "*"
ExpectedTokens.push_back(AsmToken::Identifier); // "def"
ExpectedTokens.push_back(AsmToken::Star); // "*"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::Identifier); // "xyz"
ExpectedTokens.push_back(AsmToken::EndOfStatement);
ExpectedTokens.push_back(AsmToken::Eof);
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
TEST_F(SystemZAsmLexerTest, CheckStrictCommentString5) {
StringRef AsmStr = "#abc\n/* def */// xyz";
// Setup.
MUPMAI->setCommentString("*");
MUPMAI->setAllowAdditionalComments(false);
setupCallToAsmParser(AsmStr);
// Lex initially to get the string.
Parser->getLexer().Lex();
SmallVector<AsmToken::TokenKind> ExpectedTokens;
ExpectedTokens.push_back(AsmToken::Hash); // "#"
ExpectedTokens.push_back(AsmToken::Identifier); // "abc"
ExpectedTokens.push_back(AsmToken::EndOfStatement); // "\n"
ExpectedTokens.push_back(AsmToken::Slash); // "/"
ExpectedTokens.push_back(AsmToken::EndOfStatement); // "* def */// xyz"
ExpectedTokens.push_back(AsmToken::Eof);
lexAndCheckTokens(AsmStr, ExpectedTokens);
}
} // end anonymous namespace