From c7d916dbd205f28627939598b85ea34e87f12b46 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 3 Oct 2024 15:44:17 -0700 Subject: [PATCH 1/2] [LLVM][AsmParser] Add support for C style comments Add support for C style comments in LLVM assembly. --- llvm/docs/LangRef.rst | 3 +- llvm/include/llvm/AsmParser/LLLexer.h | 1 + llvm/lib/AsmParser/LLLexer.cpp | 29 ++++++++++++++++++- llvm/test/Assembler/c-style-comment.ll | 22 ++++++++++++++ .../Assembler/invalid-c-style-comment0.ll | 6 ++++ .../Assembler/invalid-c-style-comment1.ll | 8 +++++ .../Assembler/invalid-c-style-comment2.ll | 7 +++++ .../Assembler/invalid-c-style-comment3.ll | 6 ++++ 8 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Assembler/c-style-comment.ll create mode 100644 llvm/test/Assembler/invalid-c-style-comment0.ll create mode 100644 llvm/test/Assembler/invalid-c-style-comment1.ll create mode 100644 llvm/test/Assembler/invalid-c-style-comment2.ll create mode 100644 llvm/test/Assembler/invalid-c-style-comment3.ll diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst index 177924dca4d17..e3c1f8697d53a 100644 --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -122,13 +122,14 @@ And the hard way: .. code-block:: llvm %0 = add i32 %X, %X ; yields i32:%0 - %1 = add i32 %0, %0 ; yields i32:%1 + %1 = add i32 %0, %0 /* yields i32:%1 */ %result = add i32 %1, %1 This last way of multiplying ``%X`` by 8 illustrates several important lexical features of LLVM: #. Comments are delimited with a '``;``' and go until the end of line. + Alternatively, comments can start with ``/*`` and terminate with ``*/``. #. Unnamed temporaries are created when the result of a computation is not assigned to a named value. #. By default, unnamed temporaries are numbered sequentially (using a diff --git a/llvm/include/llvm/AsmParser/LLLexer.h b/llvm/include/llvm/AsmParser/LLLexer.h index a9f51fb925f5d..501a7aefccd7f 100644 --- a/llvm/include/llvm/AsmParser/LLLexer.h +++ b/llvm/include/llvm/AsmParser/LLLexer.h @@ -95,6 +95,7 @@ namespace llvm { int getNextChar(); void SkipLineComment(); + bool SkipCComment(); lltok::Kind ReadString(lltok::Kind kind); bool ReadVarName(); diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index 56abd03d62354..9d28aca151036 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -200,7 +200,6 @@ lltok::Kind LLLexer::LexToken() { // Handle letters: [a-zA-Z_] if (isalpha(static_cast(CurChar)) || CurChar == '_') return LexIdentifier(); - return lltok::Error; case EOF: return lltok::Eof; case 0: @@ -251,6 +250,12 @@ lltok::Kind LLLexer::LexToken() { case ',': return lltok::comma; case '*': return lltok::star; case '|': return lltok::bar; + case '/': + if (getNextChar() != '*') + return lltok::Error; + if (SkipCComment()) + return lltok::Error; + continue; } } } @@ -262,6 +267,28 @@ void LLLexer::SkipLineComment() { } } +/// SkipCComment - This skips C-style /**/ comments. Returns true if there +/// was an error. +bool LLLexer::SkipCComment() { + while (true) { + int CurChar = getNextChar(); + switch (CurChar) { + case EOF: + LexError("unterminated comment"); + return true; + case '*': + // End of the comment? + CurChar = getNextChar(); + if (CurChar == '/') + return false; + if (CurChar == EOF) { + LexError("unterminated comment"); + return true; + } + } + } +} + /// Lex all tokens that start with an @ character. /// GlobalVar @\"[^\"]*\" /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]* diff --git a/llvm/test/Assembler/c-style-comment.ll b/llvm/test/Assembler/c-style-comment.ll new file mode 100644 index 0000000000000..9cc17ad90d0bc --- /dev/null +++ b/llvm/test/Assembler/c-style-comment.ll @@ -0,0 +1,22 @@ +; RUN: llvm-as < %s | llvm-dis | FileCheck %s + +/* Simple C style comment */ + +; CHECK: @B = external global i32 +@B = external global i32 + +/* multiline C ctyle comment at "top-level" + * This is the second line + * and this is third + */ + + +; CHECK: @foo +define <4 x i1> @foo(<4 x float> %a, <4 x float> %b) nounwind { +entry: /* inline comment */ + %cmp = fcmp olt <4 x float> %a, /* to be ignored */ %b + ret <4 x i1> %cmp /* ignore */ +} + +/* End of the assembly file */ + diff --git a/llvm/test/Assembler/invalid-c-style-comment0.ll b/llvm/test/Assembler/invalid-c-style-comment0.ll new file mode 100644 index 0000000000000..f042cdd151af9 --- /dev/null +++ b/llvm/test/Assembler/invalid-c-style-comment0.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s + +@B = external global i32 + +; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment +/* End of the assembly file diff --git a/llvm/test/Assembler/invalid-c-style-comment1.ll b/llvm/test/Assembler/invalid-c-style-comment1.ll new file mode 100644 index 0000000000000..7f2b966238cf4 --- /dev/null +++ b/llvm/test/Assembler/invalid-c-style-comment1.ll @@ -0,0 +1,8 @@ +; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s + +@B = external global i32 + +/* /* Nested comments not supported */ + +; CHECK: [[FILE]]:[[@LINE+1]]:1: error: redefinition of global '@B' +@B = external global i32 diff --git a/llvm/test/Assembler/invalid-c-style-comment2.ll b/llvm/test/Assembler/invalid-c-style-comment2.ll new file mode 100644 index 0000000000000..2a759d84682e6 --- /dev/null +++ b/llvm/test/Assembler/invalid-c-style-comment2.ll @@ -0,0 +1,7 @@ +; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s + +@B = external global i32 + +; CHECK: [[FILE]]:[[@LINE+1]]:2: error: expected top-level entity +*/ + diff --git a/llvm/test/Assembler/invalid-c-style-comment3.ll b/llvm/test/Assembler/invalid-c-style-comment3.ll new file mode 100644 index 0000000000000..767fdb6ff069e --- /dev/null +++ b/llvm/test/Assembler/invalid-c-style-comment3.ll @@ -0,0 +1,6 @@ +; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s + +@B = external global i32 + +; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment +/* End of the assembly file * From 62db773593a559db63fa9ff2e1abe5e109390492 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Fri, 1 Nov 2024 12:48:45 -0700 Subject: [PATCH 2/2] Update llvm/lib/AsmParser/LLLexer.cpp Co-authored-by: Nikita Popov --- llvm/lib/AsmParser/LLLexer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index 9d28aca151036..1b8e033134f51 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -267,7 +267,7 @@ void LLLexer::SkipLineComment() { } } -/// SkipCComment - This skips C-style /**/ comments. Returns true if there +/// This skips C-style /**/ comments. Returns true if there /// was an error. bool LLLexer::SkipCComment() { while (true) {