[10/25] libcpu: Remove the need of NMNES by using enum

Message ID 20221020182603.815-11-luoyonggang@gmail.com
State Superseded
Headers
Series Patches for building with mingw/gcc msvc/clang-cl |

Commit Message

Yonggang Luo Oct. 20, 2022, 6:25 p.m. UTC
  Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
---
 libcpu/Makefile.am  |  2 +-
 libcpu/i386_parse.y | 13 +++++++++----
 2 files changed, 10 insertions(+), 5 deletions(-)
  

Comments

Mark Wielaard Dec. 12, 2022, 12:37 p.m. UTC | #1
Hi,

On Fri, 2022-10-21 at 02:25 +0800, Yonggang Luo via Elfutils-devel
wrote:
> Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
> ---
>  libcpu/Makefile.am  |  2 +-
>  libcpu/i386_parse.y | 13 +++++++++----
>  2 files changed, 10 insertions(+), 5 deletions(-)
> 
> diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
> index 57d0a164..259ed838 100644
> --- a/libcpu/Makefile.am
> +++ b/libcpu/Makefile.am
> @@ -92,7 +92,7 @@ libeu = ../lib/libeu.a
>  i386_lex_CFLAGS = -Wno-unused-label -Wno-unused-function -Wno-sign-
> compare \
>  		  -Wno-implicit-fallthrough
>  i386_parse.o: i386_parse.c i386.mnemonics
> -i386_parse_CFLAGS = -DNMNES="`wc -l < i386.mnemonics`"
> +i386_parse_CFLAGS =
>  i386_lex.o: i386_parse.h
>  i386_gendis_LDADD = $(libeu) -lm $(obstack_LIBS)
>  
> diff --git a/libcpu/i386_parse.y b/libcpu/i386_parse.y
> index d2236d59..5f31484c 100644
> --- a/libcpu/i386_parse.y
> +++ b/libcpu/i386_parse.y
> @@ -1108,9 +1108,14 @@ print_op_fct (const void *nodep, VISIT value,
>  }
>  
>  
> -#if NMNES < 2
> -# error "bogus NMNES value"
> -#endif
> +/* The index can be stored in the instrtab.  */
> +enum
> +  {
> +#define MNE(name) MNE_##name,
> +#include "i386.mnemonics"
> +#undef MNE
> +    MNE_COUNT
> +  };

Since almost the same enum is defined in i386_disasm.c, just with
MNE_COUNT = MNE_INVALID, can we define and use them in one place?

>  static void
>  instrtable_out (void)
> @@ -1123,7 +1128,7 @@ instrtable_out (void)
>    fprintf (outfile, "#define MNEMONIC_BITS %zu\n",
> best_mnemonic_bits);
>  #else
>    fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
> -	   lrint (ceil (log2 (NMNES))));
> +	   lrint (ceil (log2 (MNE_COUNT))));
>  #endif
>    fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
>    for (int i = 0; i < 3; ++i)
  

Patch

diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 57d0a164..259ed838 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -92,7 +92,7 @@  libeu = ../lib/libeu.a
 i386_lex_CFLAGS = -Wno-unused-label -Wno-unused-function -Wno-sign-compare \
 		  -Wno-implicit-fallthrough
 i386_parse.o: i386_parse.c i386.mnemonics
-i386_parse_CFLAGS = -DNMNES="`wc -l < i386.mnemonics`"
+i386_parse_CFLAGS =
 i386_lex.o: i386_parse.h
 i386_gendis_LDADD = $(libeu) -lm $(obstack_LIBS)
 
diff --git a/libcpu/i386_parse.y b/libcpu/i386_parse.y
index d2236d59..5f31484c 100644
--- a/libcpu/i386_parse.y
+++ b/libcpu/i386_parse.y
@@ -1108,9 +1108,14 @@  print_op_fct (const void *nodep, VISIT value,
 }
 
 
-#if NMNES < 2
-# error "bogus NMNES value"
-#endif
+/* The index can be stored in the instrtab.  */
+enum
+  {
+#define MNE(name) MNE_##name,
+#include "i386.mnemonics"
+#undef MNE
+    MNE_COUNT
+  };
 
 static void
 instrtable_out (void)
@@ -1123,7 +1128,7 @@  instrtable_out (void)
   fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
 #else
   fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
-	   lrint (ceil (log2 (NMNES))));
+	   lrint (ceil (log2 (MNE_COUNT))));
 #endif
   fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
   for (int i = 0; i < 3; ++i)