poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/3] pk-utils: change pk_print_binary func to macro


From: Mohammad-Reza Nabipoor
Subject: [PATCH 1/3] pk-utils: change pk_print_binary func to macro
Date: Tue, 14 Nov 2023 02:18:53 +0100

`pk_print_binary' function needs a pointer to a `puts' function which
creates a friction for changes to the terminal interface.
Changing it to a macro makes the life easier.

2023-11-14  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * common/pk-utils.h (pk_print_binary): Remove function decl.
        (PK_PRINT_BINARY): New macro for printing in base 2 to make use of
        `pk_puts' macro easier.
        * common/pk-utils.c (pk_print_binary): Remove function.
        (pk_integral_suffix): New function.
        * libpoke/pvm-val.c (pvm_print_val_1): Change `pk_print_binary' to
        `PK_PRINT_BINARY'.
        * libpoke/pvm.jitter (wrapped-functions):
        s/pk_print_binary/pk_integral_suffix/.
        (PVM_PRINTI): s/pk_print_binary/PK_PRINT_BINARY/.
        (PVM_PRINTL): Likewise.
---
 ChangeLog          | 14 ++++++++++++++
 common/pk-utils.c  | 33 +++++++++++----------------------
 common/pk-utils.h  | 27 +++++++++++++++++++++++++--
 libpoke/pvm-val.c  | 11 ++++-------
 libpoke/pvm.jitter |  6 +++---
 5 files changed, 57 insertions(+), 34 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b580e5d5..42d1e9f8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2023-11-14  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * common/pk-utils.h (pk_print_binary): Remove function decl.
+       (PK_PRINT_BINARY): New macro for printing in base 2 to make use of
+       `pk_puts' macro easier.
+       * common/pk-utils.c (pk_print_binary): Remove function.
+       (pk_integral_suffix): New function.
+       * libpoke/pvm-val.c (pvm_print_val_1): Change `pk_print_binary' to
+       `PK_PRINT_BINARY'.
+       * libpoke/pvm.jitter (wrapped-functions):
+       s/pk_print_binary/pk_integral_suffix/.
+       (PVM_PRINTI): s/pk_print_binary/PK_PRINT_BINARY/.
+       (PVM_PRINTL): Likewise.
+
 2023-11-04  Jose E. Marchesi  <jemarch@gnu.org>
 
        * libpoke/pkl-gen.c (pkl_gen_ps_var): Fix detection of
diff --git a/common/pk-utils.c b/common/pk-utils.c
index 1134251c..d1c7cf90 100644
--- a/common/pk-utils.c
+++ b/common/pk-utils.c
@@ -82,29 +82,18 @@ PK_POW (pk_upow, uint64_t)
 
 #undef PK_POW
 
-void
-pk_print_binary (void (*puts_fn) (const char *str),
-                 uint64_t val, int size, int sign_p, int use_suffix_p)
+const char *
+pk_integral_suffix (int size, int sign_p)
 {
-  char b[65];
-
-  for (int z = 0; z < size; z++)
-    b[size-1-z] = ((val >> z) & 0x1) + '0';
-  b[size] = '\0';
-
-  puts_fn (b);
-
-  if  (use_suffix_p)
-    {
-      if (size == 64)
-        puts_fn (sign_p ? "L" : "UL");
-      else if (size == 16)
-        puts_fn (sign_p ? "H" : "UH");
-      else if (size == 8)
-        puts_fn (sign_p ? "B" : "UB");
-      else if (size == 4)
-        puts_fn (sign_p ? "N" : "UN");
-    }
+  if (size == 64)
+    return sign_p ? "L" : "UL";
+  if (size == 16)
+    return sign_p ? "H" : "UH";
+  if (size == 8)
+    return sign_p ? "B" : "UB";
+  if (size == 4)
+    return sign_p ? "N" : "UN";
+  return "";
 }
 
 int
diff --git a/common/pk-utils.h b/common/pk-utils.h
index 97f5dc0a..78e49a5c 100644
--- a/common/pk-utils.h
+++ b/common/pk-utils.h
@@ -49,9 +49,32 @@ char *pk_file_readable (const char *filename);
 int64_t pk_ipow (int64_t base, uint32_t exp);
 uint64_t pk_upow (uint64_t base, uint32_t exp);
 
+/* Return one of the following strings based on the SIZE and SIGN_P:
+     "N", "UN", "B", "UB", "H", "UH", "L", "UL"
+   which are valid suffix for integral values in Poke.  */
+
+const char *pk_integral_suffix (int size, int sign_p);
+
 /* Print the given unsigned 64-bit integer in binary. */
-void pk_print_binary (void (*puts_fn) (const char *str), uint64_t val,
-                      int size, int sign_p, int use_suffix_p);
+
+#define PK_PRINT_BINARY(val, size, sign_p, use_suffix_p)                      \
+  do                                                                          \
+    {                                                                         \
+      char _pkpb_buf[65];                                                     \
+      uint64_t _pkpb_val = (val);                                             \
+      int _pkpb_sz = (size);                                                  \
+                                                                              \
+      for (int _pkpb_z = 0; _pkpb_z < _pkpb_sz; _pkpb_z++)                    \
+        _pkpb_buf[_pkpb_sz - 1 - _pkpb_z]                                     \
+            = ((_pkpb_val >> _pkpb_z) & 0x1) + '0';                           \
+      _pkpb_buf[_pkpb_sz] = '\0';                                             \
+                                                                              \
+      pk_puts (_pkpb_buf);                                                    \
+                                                                              \
+      if (use_suffix_p)                                                       \
+        pk_puts (pk_integral_suffix (_pkpb_sz, (sign_p)));                    \
+    }                                                                         \
+  while (0)
 
 /* Format the given unsigned 64-bit integer in binary. */
 int pk_format_binary (char* out, size_t outlen, uint64_t val, int size,
diff --git a/libpoke/pvm-val.c b/libpoke/pvm-val.c
index 07e66b2c..e8091c50 100644
--- a/libpoke/pvm-val.c
+++ b/libpoke/pvm-val.c
@@ -1168,8 +1168,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, 
int indent,
       if (base == 2)
         {
           pk_puts ("0b");
-          pk_print_binary (pk_puts, ulongval, size, /*signed_p*/ 1,
-                           /*use_suffix_p*/ 1);
+          PK_PRINT_BINARY (ulongval, size, /*signed_p*/ 1, /*use_suffix_p*/ 1);
         }
       else
         {
@@ -1198,7 +1197,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, 
int indent,
       if (base == 2)
         {
           pk_puts ("0b");
-          pk_print_binary (pk_puts, (uint64_t) uintval, size, /*signed*/ 1,
+          PK_PRINT_BINARY ((uint64_t) uintval, size, /*signed*/ 1,
                            /*use_suffix_p*/ 1);
         }
       else
@@ -1228,8 +1227,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, 
int indent,
       if (base == 2)
         {
           pk_puts ("0b");
-          pk_print_binary (pk_puts, ulongval, size, /*signed_p*/ 0,
-                           /*use_suffix_p*/ 1);
+          PK_PRINT_BINARY (ulongval, size, /*signed_p*/ 0, /*use_suffix_p*/ 1);
         }
       else
         {
@@ -1251,8 +1249,7 @@ pvm_print_val_1 (pvm vm, int depth, int mode, int base, 
int indent,
       if (base == 2)
         {
           pk_puts ("0b");
-          pk_print_binary (pk_puts, uintval, size, /*signed_p*/ 0,
-                           /*use_suffix_p*/ 1);
+          PK_PRINT_BINARY (uintval, size, /*signed_p*/ 0, /*use_suffix_p*/ 1);
         }
       else
         {
diff --git a/libpoke/pvm.jitter b/libpoke/pvm.jitter
index 8d44872f..3954345a 100644
--- a/libpoke/pvm.jitter
+++ b/libpoke/pvm.jitter
@@ -90,7 +90,7 @@ wrapped-functions
   pvm_make_offset_type
   pvm_make_array_type
   pk_upow
-  pk_print_binary
+  pk_integral_suffix
   pk_format_binary
   pvm_alloc
   pvm_allocate_struct_attrs
@@ -517,7 +517,7 @@ late-header-c
       }                                                                     \
       else if ((BASE) == 2)                                                 \
       {                                                                     \
-        pk_print_binary (pk_puts, val, JITTER_ARGN0, 1, 0);                 \
+        PK_PRINT_BINARY (val, JITTER_ARGN0, 1, 0);                          \
         JITTER_DROP_STACK ();                                               \
         JITTER_DROP_STACK ();                                               \
         break;                                                              \
@@ -571,7 +571,7 @@ late-header-c
       }                                                                     \
       else if ((BASE) == 2)                                                 \
       {                                                                     \
-        pk_print_binary (pk_puts, val, JITTER_ARGN0, 1, 0);                 \
+        PK_PRINT_BINARY (val, JITTER_ARGN0, 1, 0);                          \
         JITTER_DROP_STACK ();                                               \
         JITTER_DROP_STACK ();                                               \
         break;                                                              \
-- 
2.42.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]