]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
s390: Do not erroneously use base operand value for length operand
authorJens Remus <jremus@linux.ibm.com>
Fri, 1 Mar 2024 11:45:14 +0000 (12:45 +0100)
committerJens Remus <jremus@linux.ibm.com>
Fri, 1 Mar 2024 11:45:14 +0000 (12:45 +0100)
The base register operand B may optionally be omitted in D(B) by coding
D and in D(L,B) by coding D(L). The index register operand X may
optionally be omitted in D(X,B) by coding D(,B) or D(B). Both base and
index register operands may optionally be omitted in D(X,B) by coding D.
In any case the omitted base and/or index register operand value
defaults to zero.

When parsing an erroneously omitted length L operand in D(L,B) by coding
D(,B) the base register operand B was erroneously consumed as length
operand. When using a register name for the base register operand this
was detected and reported as error. But when not using a register name
the base register operand value was erroneously used as length operand
value.

Correct the parsing of an omitted optional base or index register to not
erroneously use the base register operand value as length, when
erroneously omitting the length operand.

While at it rename the variable used to remember whether the base or
index register operand was omitted to enhance code readability.
Additionally add test cases for the optional omission of base and/or
index register operands.

Example assembler source:
mvc 16(1,%r1),32(%r2)
mvc 16(1),32(%r2)
mvc 16(,1),32(%r2) # undetected syntax error

Disassembly of bad assembly without commit shows the base register
operand value was erroneously used as length operand value:
   0:   d2 00 10 10 20 20       mvc     16(1,%r1),32(%r2)
   6:   d2 00 00 10 20 20       mvc     16(1,%r0),32(%r2)
   c:   d2 00 00 10 20 20       mvc     16(1,%r0),32(%r2)

Assembler messages with commit:
3: Error: operand 1: missing operand

gas/
* config/tc-s390.c: Correct parsing of omitted base register.
* testsuite/gas/s390/s390.exp: Add test cases for omitted base
and/or index register.
* testsuite/gas/s390/zarch-omitted-base-index.s: Test cases for
omitted optional base or index register.
* testsuite/gas/s390/zarch-omitted-base-index.d: Likewise.
* testsuite/gas/s390/zarch-omitted-base-index-err.s: Test cases
for omitted base and/or index register.
* testsuite/gas/s390/zarch-omitted-base-index-err.l: Likewise.

Reviewed-by: Andreas Krebbel <krebbel@linux.ibm.com>
Signed-off-by: Jens Remus <jremus@linux.ibm.com>
gas/config/tc-s390.c
gas/testsuite/gas/s390/s390.exp
gas/testsuite/gas/s390/zarch-omitted-base-index-err.l [new file with mode: 0644]
gas/testsuite/gas/s390/zarch-omitted-base-index-err.s [new file with mode: 0644]
gas/testsuite/gas/s390/zarch-omitted-base-index.d [new file with mode: 0644]
gas/testsuite/gas/s390/zarch-omitted-base-index.s [new file with mode: 0644]

index 019f26b2f4abf3506653706293e34ec70e34adc0..cfe98b5e94bec3ec18b5f5009c85bfe53e72c92a 100644 (file)
@@ -1268,16 +1268,15 @@ md_gather_operands (char *str,
   expressionS ex;
   elf_suffix_type suffix;
   bfd_reloc_code_real_type reloc;
-  int skip_optional;
+  int omitted_base_or_index;
   char *f;
   int fc, i;
 
   while (ISSPACE (*str))
     str++;
 
-  skip_optional = 0;
-
   /* Gather the operands.  */
+  omitted_base_or_index = 0;   /* Whether B in D(L,B) or X in D(X,B) were omitted.  */
   fc = 0;
   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
     {
@@ -1294,13 +1293,13 @@ md_gather_operands (char *str,
          break;
        }
 
-      if (skip_optional && (operand->flags & S390_OPERAND_INDEX))
+      if (omitted_base_or_index && (operand->flags & S390_OPERAND_INDEX))
        {
-         /* We do an early skip. For D(X,B) constructions the index
-            register is skipped (X is optional). For D(L,B) the base
-            register will be the skipped operand, because L is NOT
-            optional.  */
-         skip_optional = 0;
+         /* Skip omitted optional index register operand in D(X,B) due to
+            D(,B) or D(B). Skip comma, if D(,B).  */
+         if (*str == ',')
+           str++;
+         omitted_base_or_index = 0;
          continue;
        }
 
@@ -1510,17 +1509,19 @@ md_gather_operands (char *str,
              for (f = str; *f != '\0'; f++)
                if (*f == ',' || *f == ')')
                  break;
-             /* If there is no comma until the closing parentheses OR
-                there is a comma right after the opening parentheses,
-                we have to skip optional operands.  */
+             /* If there is no comma until the closing parenthesis ')' or
+                there is a comma right after the opening parenthesis '(',
+                we have to skip the omitted optional index or base register
+                operand:
+                - Index X in D(X,B), when D(,B) or D(B)
+                - Base B in D(L,B), when D(L)  */
              if (*f == ',' && f == str)
                {
-                 /* comma directly after '(' ? */
-                 skip_optional = 1;
-                 str++;
+                 /* Comma directly after opening parenthesis '(' ? */
+                 omitted_base_or_index = 1;
                }
              else
-               skip_optional = (*f != ',');
+               omitted_base_or_index = (*f != ',');
            }
        }
       else if (operand->flags & S390_OPERAND_BASE)
@@ -1530,7 +1531,7 @@ md_gather_operands (char *str,
            as_bad (_("syntax error; missing ')' after base register"));
          else
            str++;
-         skip_optional = 0;
+         omitted_base_or_index = 0;
 
          if (*str == '\0' && skip_optargs_p (opcode->flags, &opindex_ptr[1]))
            continue;
index 86e2dd492cd12bde5a50348c57a027fd151cabe8..7fbc7f8a7515d8967544d179e8a702831051730b 100644 (file)
@@ -44,4 +44,6 @@ if [expr [istarget "s390-*-*"] ||  [istarget "s390x-*-*"]]  then {
     run_list_test "machine-parsing-4" ""
     run_list_test "machine-parsing-5" ""
     run_list_test "machine-parsing-6" ""
+    run_dump_test "zarch-omitted-base-index" "{as -m64}"
+    run_list_test "zarch-omitted-base-index-err" ""
 }
diff --git a/gas/testsuite/gas/s390/zarch-omitted-base-index-err.l b/gas/testsuite/gas/s390/zarch-omitted-base-index-err.l
new file mode 100644 (file)
index 0000000..fe13f95
--- /dev/null
@@ -0,0 +1,24 @@
+.*: Assembler messages:
+.*:5: Error: bad expression
+.*:5: Error: syntax error; missing '\)' after base register
+.*:8: Error: bad expression
+.*:8: Error: syntax error; missing '\)' after base register
+.*:9: Error: bad expression
+.*:9: Error: syntax error; missing '\)' after base register
+.*:12: Error: bad expression
+.*:12: Error: syntax error; missing '\)' after base register
+.*:13: Error: bad expression
+.*:13: Error: syntax error; missing '\)' after base register
+.*:16: Error: missing operand
+.*:17: Error: missing operand
+.*:18: Error: invalid length field specified
+.*:19: Error: bad expression
+.*:19: Error: operand out of range \(0 is not between 1 and 256\)
+.*:19: Error: operand out of range \(32 is not between 0 and 15\)
+.*:19: Error: syntax error; missing '\)' after base register
+.*:19: Error: syntax error; expected ','
+.*:19: Error: bad expression
+.*:19: Error: found 'r', expected: '\)'
+.*:19: Error: syntax error; missing '\)' after base register
+.*:19: Error: junk at end of line: `r2\)'
+.*:20: Error: syntax error; missing '\(' after displacement
diff --git a/gas/testsuite/gas/s390/zarch-omitted-base-index-err.s b/gas/testsuite/gas/s390/zarch-omitted-base-index-err.s
new file mode 100644 (file)
index 0000000..65ad739
--- /dev/null
@@ -0,0 +1,20 @@
+.text
+foo:
+
+#              R1,D2(B2)
+       clm     %r1,0b1000,16()
+
+#              R1,D2(X2,B2)
+       a       %r1,16(%r2,)
+       a       %r1,16()
+
+#              V1,D2(VX2,B2),M3
+       vgef    %v1,16(%v2,),0
+       vgef    %v1,16(),0
+
+#              D1(L1,B1),D2(B2)
+       mvc     16(,%r1),32(%r2)
+       mvc     16(,1),32(%r2)
+       mvc     16(%r1),32(%r2)
+       mvc     16(),32(%r2)
+       mvc     16,32(%r2)
diff --git a/gas/testsuite/gas/s390/zarch-omitted-base-index.d b/gas/testsuite/gas/s390/zarch-omitted-base-index.d
new file mode 100644 (file)
index 0000000..b2ff292
--- /dev/null
@@ -0,0 +1,22 @@
+#name: s390x omit base/index register
+#objdump: -dr
+
+.*: +file format .*
+
+Disassembly of section .text:
+
+.* <foo>:
+.*:    bd 18 20 10 [    ]*clm  %r1,8,16\(%r2\)
+.*:    bd 18 00 10 [    ]*clm  %r1,8,16
+.*:    5a 12 30 10 [    ]*a    %r1,16\(%r2,%r3\)
+.*:    5a 10 30 10 [    ]*a    %r1,16\(%r3\)
+.*:    5a 10 30 10 [    ]*a    %r1,16\(%r3\)
+.*:    5a 10 00 10 [    ]*a    %r1,16
+.*:    e7 12 30 10 00 13 [      ]*vgef %v1,16\(%v2,%r3\),0
+.*:    e7 10 30 10 00 13 [      ]*vgef %v1,16\(%r3\),0
+.*:    e7 10 30 10 00 13 [      ]*vgef %v1,16\(%r3\),0
+.*:    e7 10 00 10 00 13 [      ]*vgef %v1,16,0
+.*:    d2 00 10 10 20 20 [      ]*mvc  16\(1,%r1\),32\(%r2\)
+.*:    d2 00 10 10 00 20 [      ]*mvc  16\(1,%r1\),32
+.*:    d2 00 00 10 20 20 [      ]*mvc  16\(1,%r0\),32\(%r2\)
+.*:    d2 00 00 10 00 20 [      ]*mvc  16\(1,%r0\),32
diff --git a/gas/testsuite/gas/s390/zarch-omitted-base-index.s b/gas/testsuite/gas/s390/zarch-omitted-base-index.s
new file mode 100644 (file)
index 0000000..8381319
--- /dev/null
@@ -0,0 +1,24 @@
+.text
+foo:
+
+#              R1,D2(B2)
+       clm     %r1,0b1000,16(%r2)
+       clm     %r1,0b1000,16
+
+#              R1,D1(X2,B2)
+       a       %r1,16(%r2,%r3)
+       a       %r1,16(,%r3)
+       a       %r1,16(%r3)
+       a       %r1,16
+
+#              V1,D2(VX2,B2),M3
+       vgef    %v1,16(%v2,%r3),0
+       vgef    %v1,16(,%r3),0
+       vgef    %v1,16(%r3),0
+       vgef    %v1,16,0
+
+#              D1(L1,B1),D2(B2)
+       mvc     16(1,%r1),32(%r2)
+       mvc     16(1,%r1),32
+       mvc     16(1),32(%r2)
+       mvc     16(1),32