diff --git a/clang/test/CodeGen/RISCV/riscv-inline-asm-gcc-commenting.c b/clang/test/CodeGen/RISCV/riscv-inline-asm-gcc-commenting.c new file mode 100644 --- /dev/null +++ b/clang/test/CodeGen/RISCV/riscv-inline-asm-gcc-commenting.c @@ -0,0 +1,31 @@ +// RUN: %clang -fPIC --target=riscv64-unknown-elf -mabi=lp64f -O3 -o - -S %s | FileCheck %s + +unsigned long int f1() { + unsigned long int dst; + __asm__ __volatile__("li %[dst], 0x1234 /* this is fine */ \n" + "add zero, %[dst], %[dst]\n" + : [ dst ] "=r"(dst) + : + :); + return dst; +} + +unsigned long int f2() { + unsigned long int dst; + __asm__ __volatile__("li /* this is fine */ %[dst], /* this should also be fine */ 0x1234\n" + "add zero, %[dst], %[dst]\n" + : [ dst ] "=r"(dst) + : + :); + return dst; +} +// CHECK: f1: +// CHECK: lui a0, 1 +// CHECK-NEXT: addiw a0, a0, 564 +// CHECK-NEXT: add zero, a0, a0 +// CHECK: ret +// CHECK: f2: +// CHECK: lui a0, 1 +// CHECK-NEXT: addiw a0, a0, 564 +// CHECK-NEXT: add zero, a0, a0 +// CHECK: ret diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp --- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -2573,14 +2573,26 @@ if (parseOperand(Operands, Name)) return true; + // Silently ignore comments after the first operand for compatibility with gcc + while (getLexer().is(AsmToken::Comment)) + getLexer().Lex(); + // Parse until end of statement, consuming commas between operands while (getLexer().is(AsmToken::Comma)) { // Consume comma token getLexer().Lex(); + // Silently ignore comments before operand for compatibility with gcc + while (getLexer().is(AsmToken::Comment)) + getLexer().Lex(); + // Parse next operand if (parseOperand(Operands, Name)) return true; + + // Silently ignore comments after operand for compatibility with gcc + while (getLexer().is(AsmToken::Comment)) + getLexer().Lex(); } if (getParser().parseEOL("unexpected token")) {