sim: mips: rework dynamic printf logic to avoid compiler warnings
authorMike Frysinger <vapier@gentoo.org>
Tue, 15 Jun 2021 04:41:22 +0000 (00:41 -0400)
committerMike Frysinger <vapier@gentoo.org>
Wed, 16 Jun 2021 05:55:31 +0000 (01:55 -0400)
The compiler doesn't like passing non-constant strings to printf
functions, so tweak the code to always pass one in.  This code is
a little more verbose, but it's probably the same performance.

The macro usage is a bit ugly, but maybe less than copying &
pasting the extended conditional format logic.

sim/mips/ChangeLog
sim/mips/interp.c

index 28a33c78995a7678b80ea489a84aab5c772513c2..c36e01fa19eb47286dad2ecea3ffea4977030f62 100644 (file)
@@ -1,3 +1,8 @@
+2021-06-16  Mike Frysinger  <vapier@gentoo.org>
+
+       * interp.c (sim_monitor): Change ap type to address_word*.
+       (_P, P): New macros.  Rewrite dynamic printf logic to use these.
+
 2021-06-16  Mike Frysinger  <vapier@gentoo.org>
 
        * dv-tx3904sio.c (tx3904sio_fifo_push): Change next_buf to
index 38338fbfa7f9508c87da8be649978d7e1084d14e..5645f01ed2251c54e8dee4bc56ff1841f5448d10 100644 (file)
@@ -1388,7 +1388,7 @@ sim_monitor (SIM_DESC sd,
       {
        address_word s = A0;
        unsigned char c;
-       signed_word *ap = &A1; /* 1st argument */
+       address_word *ap = &A1; /* 1st argument */
         /* This isn't the quickest way, since we call the host print
            routine for every character almost. But it does avoid
            having to allocate and manage a temporary string buffer. */
@@ -1471,18 +1471,43 @@ sim_monitor (SIM_DESC sd,
                          sim_io_printf(sd,"<binary not supported>");
                        else
                          {
-                           sprintf (tmp, "%%%s%c", longlong ? "ll" : "", c);
-                           if (longlong)
-                             sim_io_printf(sd, tmp, lv);
-                           else
-                             sim_io_printf(sd, tmp, (int)lv);
+#define _P(c, fmt64, fmt32) \
+  case c: \
+    if (longlong) \
+      sim_io_printf (sd, "%" fmt64, lv); \
+    else \
+      sim_io_printf (sd, "%" fmt32, (int)lv); \
+    break;
+#define P(c, fmtc) _P(c, PRI##fmtc##64, PRI##fmtc##32)
+                           switch (c)
+                             {
+                             P('d', d)
+                             P('o', o)
+                             P('x', x)
+                             P('X', X)
+                             P('u', u)
+                             }
                          }
+#undef P
+#undef _P
                      }
                    else if (strchr ("eEfgG", c))
                      {
                        double dbl = *(double*)(ap++);
-                       sprintf (tmp, "%%%d.%d%c", width, trunc, c);
-                       sim_io_printf (sd, tmp, dbl);
+
+#define P(c, fmtc) \
+  case c: \
+    sim_io_printf (sd, "%*.*" #fmtc, width, trunc, dbl); \
+    break;
+                       switch (c)
+                         {
+                         P('e', e)
+                         P('E', E)
+                         P('f', f)
+                         P('g', g)
+                         P('G', G)
+                         }
+#undef P
                        trunc = 0;
                      }
                  }
This page took 0.029379 seconds and 4 git commands to generate.