md/define_c_enum: support value assignation
authorYunQiang Su <yunqiang.su@cipunited.com>
Tue, 31 Aug 2021 11:19:49 +0000 (07:19 -0400)
committerYunQiang Su <syq@debian.org>
Wed, 1 Sep 2021 10:12:17 +0000 (18:12 +0800)
Currently, the enums from define_c_enum and define_enum can only
has values one by one from 0.

In fact we can support the behaviour just like C, aka like
  (define_enum "mips_isa" [(mips1 1) mips2 (mips32 32) mips32r2]),
then we can get
  enum mips_isa {
    MIPS_ISA_MIPS1 = 1,
    MIPS_ISA_MIPS2 = 2,
    MIPS_ISA_MIPS32 = 32,
    MIPS_ISA_MIPS32R2 = 33
  };

gcc/ChangeLog:
* read-md.c (md_reader::handle_enum): support value assignation.
* doc/md.texi: record define_c_enum value assignation support.

gcc/doc/md.texi
gcc/read-md.c

index f8047ae..2b41cb7 100644 (file)
@@ -11074,6 +11074,8 @@ The syntax is as follows:
 (define_c_enum "@var{name}" [
   @var{value0}
   @var{value1}
+  (@var{value32} 32)
+  @var{value33}
   @dots{}
   @var{valuen}
 ])
@@ -11086,6 +11088,8 @@ in @file{insn-constants.h}:
 enum @var{name} @{
   @var{value0} = 0,
   @var{value1} = 1,
+  @var{value32} = 32,
+  @var{value33} = 33,
   @dots{}
   @var{valuen} = @var{n}
 @};
index bb419e0..8fda401 100644 (file)
@@ -902,7 +902,8 @@ void
 md_reader::handle_enum (file_location loc, bool md_p)
 {
   char *enum_name, *value_name;
-  struct md_name name;
+  unsigned int cur_value;
+  struct md_name name, value;
   struct enum_type *def;
   struct enum_value *ev;
   void **slot;
@@ -928,6 +929,7 @@ md_reader::handle_enum (file_location loc, bool md_p)
       *slot = def;
     }
 
+  cur_value = def->num_values;
   require_char_ws ('[');
 
   while ((c = read_skip_spaces ()) != ']')
@@ -937,8 +939,18 @@ md_reader::handle_enum (file_location loc, bool md_p)
          error_at (loc, "unterminated construct");
          exit (1);
        }
-      unread_char (c);
-      read_name (&name);
+      if (c == '(')
+       {
+         read_name (&name);
+         read_name (&value);
+         require_char_ws (')');
+         cur_value = atoi (value.string);
+       }
+      else
+       {
+         unread_char (c);
+         read_name (&name);
+       }
 
       ev = XNEW (struct enum_value);
       ev->next = 0;
@@ -954,11 +966,12 @@ md_reader::handle_enum (file_location loc, bool md_p)
          ev->name = value_name;
        }
       ev->def = add_constant (get_md_constants (), value_name,
-                             md_decimal_string (def->num_values), def);
+                             md_decimal_string (cur_value), def);
 
       *def->tail_ptr = ev;
       def->tail_ptr = &ev->next;
       def->num_values++;
+      cur_value++;
     }
 }