YunQiang Su <yunqiang...@cipunited.com> writes:
> 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.

This seems like a nice feature to have.  However, the current (historical,
more lisp-like) syntax for define_constants uses:

  (NAME VALUE)

instead of:

  NAME = VALUE

I think we should do the same here for consistency.

Thanks,
Richard

> ---
>  gcc/doc/md.texi |  4 ++++
>  gcc/read-md.c   | 15 +++++++++++++--
>  2 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
> index f8047aefc..1c1282c4c 100644
> --- a/gcc/doc/md.texi
> +++ b/gcc/doc/md.texi
> @@ -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}
>  @};
> diff --git a/gcc/read-md.c b/gcc/read-md.c
> index bb419e0f6..43dfbe264 100644
> --- a/gcc/read-md.c
> +++ b/gcc/read-md.c
> @@ -901,7 +901,8 @@ md_decimal_string (int number)
>  void
>  md_reader::handle_enum (file_location loc, bool md_p)
>  {
> -  char *enum_name, *value_name;
> +  char *enum_name, *value_name, *token;
> +  unsigned int cur_value;
>    struct md_name name;
>    struct enum_type *def;
>    struct enum_value *ev;
> @@ -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 ()) != ']')
> @@ -945,20 +947,29 @@ md_reader::handle_enum (file_location loc, bool md_p)
>        if (md_p)
>       {
>         value_name = concat (def->name, "_", name.string, NULL);
> +       value_name = strtok (value_name, "=");
> +       token = strtok (NULL, "=");
> +       if (token)
> +         cur_value = atoi (token);
>         upcase_string (value_name);
>         ev->name = xstrdup (name.string);
>       }
>        else
>       {
>         value_name = xstrdup (name.string);
> +       value_name = strtok (value_name, "=");
> +       token = strtok (NULL, "=");
> +       if (token)
> +         cur_value = atoi (token);
>         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++;
>      }
>  }

Reply via email to