[PATCH] gas: correctly deal with line comments when not preprocessing

Jan Beulich jbeulich@suse.com
Thu Jul 25 10:32:36 GMT 2024


Internal naming of functions / data as well as commentary mixes lines
and statements. It is presumably this confusion which has led to the
wrong use of ignore_rest_of_line() when dealing with line comments in
read_a_source_file(). We shall not (silently) produce different output
depending on whether -f is passed (for suitable input).

Introduce two new helper macros, intended to be used in favor of open-
coded accesses to is_end_of_line[]. To emphasize the difference, convert
ignore_rest_of_line() right away, including adjustments to its comments.

Since most targets have # in line_comment_chars[], add a target-
independent test for that, plus an x86-only one also checking for non-#
to work as intended.
---
I'm a little puzzled by the bounds checking in ignore_rest_of_line():
read_a_source_file() is handed full lines by input_scrub_next_buffer()
anyway, so there got to be an end-of-statement character and even an EOL
one ahead of the buffer limit. Yet to be on the safe side I copied this
behavior.

--- a/gas/read.c
+++ b/gas/read.c
@@ -1324,8 +1324,11 @@ read_a_source_file (const char *name)
 	      s = input_line_pointer;
 	      if (!startswith (s, "APP\n"))
 		{
-		  /* We ignore it.  */
-		  ignore_rest_of_line ();
+		  /* We ignore it.  Note: Not ignore_rest_of_line ()!  */
+		  while (s <= buffer_limit)
+		    if (is_end_of_line (*s++))
+		      break;
+		  input_line_pointer = s;
 		  continue;
 		}
 	      bump_line_counters ();
@@ -3974,7 +3977,7 @@ demand_empty_rest_of_line (void)
   /* Return pointing just after end-of-line.  */
 }
 
-/* Silently advance to the end of line.  Use this after already having
+/* Silently advance to the end of a statement.  Use this after already having
    issued an error about something bad.  Like demand_empty_rest_of_line,
    this function may leave input_line_pointer one after buffer_limit;
    Don't call it from within expression parsing code in an attempt to
@@ -3984,9 +3987,9 @@ void
 ignore_rest_of_line (void)
 {
   while (input_line_pointer <= buffer_limit)
-    if (is_end_of_line[(unsigned char) *input_line_pointer++])
+    if (is_end_of_stmt (*input_line_pointer++))
       break;
-  /* Return pointing just after end-of-line.  */
+  /* Return pointing just after end-of-statement.  */
 }
 
 /* Sets frag for given symbol to zero_address_frag, except when the
--- a/gas/read.h
+++ b/gas/read.h
@@ -58,6 +58,14 @@ extern bool input_from_string;
 #define is_name_ender(c) \
   ( lex_type[(unsigned char) (c)] & LEX_END_NAME   )
 
+/* The distinction of "line" and "statement" sadly is blurred by unhelpful
+   naming of e.g. the underlying array.  Most users really mean "end of
+   statement".  Going forward only these wrappers are supposed to be used.  */
+#define is_end_of_stmt(c) \
+  (is_end_of_line[(unsigned char) (c)])
+#define is_end_of_line(c) \
+  (is_end_of_line[(unsigned char) (c)] == 1)
+
 #ifndef is_a_char
 #define CHAR_MASK	(0xff)
 #define NOT_A_CHAR	(CHAR_MASK+1)
--- a/gas/testsuite/gas/all/gas.exp
+++ b/gas/testsuite/gas/all/gas.exp
@@ -490,6 +490,10 @@ if { ![istarget "tic4x*-*-*"] && ![istar
     run_list_test const-1 "-alm"
 }
 
+if { ![istarget "tic30-*-*"] } {
+    run_dump_test "line-comment"
+}
+
 run_list_test macro "-alm"
 
 run_list_test pr20312
--- /dev/null
+++ b/gas/testsuite/gas/all/line-comment.d
@@ -0,0 +1,12 @@
+#as:
+#as: -f
+#objdump: -dw --disassemble-zeroes
+#name: generic line comment
+
+.*: +file format .*
+
+Disassembly of section .text:
+
+0+ <(line_comment|\.text)>:
+ +0+:	.*
+#pass
--- /dev/null
+++ b/gas/testsuite/gas/all/line-comment.s
@@ -0,0 +1,4 @@
+	.text
+line_comment:
+	#.warning;$@!|.error
+	.nop
--- a/gas/testsuite/gas/i386/i386.exp
+++ b/gas/testsuite/gas/i386/i386.exp
@@ -105,6 +105,7 @@ if [gas_32_check] then {
     run_dump_test "equ"
     run_list_test "equ-2" "-al"
     run_list_test "equ-bad"
+    run_dump_test "line-comment"
     run_dump_test "curly"
     run_dump_test "divide"
     run_dump_test "quoted"
--- /dev/null
+++ b/gas/testsuite/gas/i386/line-comment.d
@@ -0,0 +1,13 @@
+#as:
+#as: -f
+#objdump: -dw
+#name: i386 line comment
+
+.*: +file format .*
+
+Disassembly of section .text:
+
+0+ <line_comment>:
+ +[a-f0-9]+:	90[ 	]+nop
+ +[a-f0-9]+:	c3[ 	]+ret
+#pass
--- /dev/null
+++ b/gas/testsuite/gas/i386/line-comment.s
@@ -0,0 +1,6 @@
+	.text
+line_comment:
+	nop
+	#aaa;aas
+	/aad;aam
+	ret


More information about the Binutils mailing list