[v2,03/14] x86: parse special opcode modifiers for .insn

Message ID 5771df73-12d8-e880-a051-86c09d6bdb06@suse.com
State Accepted
Headers
Series x86: new .insn directive |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Jan Beulich March 10, 2023, 10:20 a.m. UTC
  So called "short form" encoding is specified by a trailing "+r", whereas
a possible extension opcode is specified by the usual "/<digit>". Take
these off the expression before handing it to get_absolute_expression().

Note that on targets where / starts a comment, --divide needs passing to
gas in order to make use of the extension opcode functionality.
---
I don't think it makes sense to further complicate things and also
consider the use of quotation inside the major opcode expression.
Ambiguities in particular with "/<digit>" can easily be resolved by
simply parenthesizing the actual expression part of the construct.
  

Patch

--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -10742,7 +10742,7 @@  signed_cons (int size)
 static void
 s_insn (int dummy ATTRIBUTE_UNUSED)
 {
-  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer;
+  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
   char *saved_ilp = find_end_of_line (line, false), saved_char;
   const char *end;
   unsigned int j;
@@ -10768,6 +10768,7 @@  s_insn (int dummy ATTRIBUTE_UNUSED)
 
   current_templates = &tt;
   i.tm.mnem_off = MN__insn;
+  i.tm.extension_opcode = None;
 
   if (startswith (line, "VEX")
       && (line[3] == '.' || is_space_char (line[3])))
@@ -10994,10 +10995,46 @@  s_insn (int dummy ATTRIBUTE_UNUSED)
       goto done;
     }
 
+  /* Before processing the opcode expression, find trailing "+r" or
+     "/<digit>" specifiers.  */
+  for (ptr = line; ; ++ptr)
+    {
+      unsigned long n;
+      char *e;
+
+      ptr = strpbrk (ptr, "+/,");
+      if (ptr == NULL || *ptr == ',')
+	break;
+
+      if (*ptr == '+' && ptr[1] == 'r'
+	  && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
+	{
+	  *ptr = ' ';
+	  ptr[1] = ' ';
+	  i.short_form = true;
+	  break;
+	}
+
+      if (*ptr == '/' && ISDIGIT (ptr[1])
+	  && (n = strtoul (ptr + 1, &e, 8)) < 8
+	  && e == ptr + 2
+	  && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
+	{
+	  *ptr = ' ';
+	  ptr[1] = ' ';
+	  i.tm.extension_opcode = n;
+	  i.tm.opcode_modifier.modrm = 1;
+	  break;
+	}
+    }
+
   input_line_pointer = line;
   val = get_absolute_expression ();
   line = input_line_pointer;
 
+  if (i.short_form && (val & 7))
+    as_warn ("`+r' assumes low three opcode bits to be clear");
+
   for (j = 1; j < sizeof(val); ++j)
     if (!(val >> (j * 8)))
       break;