Hello,

I'm Fred, the lead developer of PureBasic (www.purebasic.com) and I got
into 2 issues for ARM32 version of tinycc when integrating tinycc into the
PureBasic pipeline to have a faster compile on Raspberry PI. Here is the
Claude report, patch and tests suite, feel free to integrate them to main
branch if it fits !

*********

Claude:

Here are two fixes for the ARM VFP code generator (arm-gen.c), each
with a new test in tests/tests2.

We found them through PureBasic, whose C backend uses tcc to compile
non-optimized builds on the Raspberry Pi: both broke code that works
with gcc.

Both patches are against mob (aade7baf) and apply in order. Tested on a
Raspberry Pi running Raspberry Pi OS 32-bit (ARM eabihf, gcc 12.2):
'make test' passes ("ALL TESTS PASSED") with both applied, the same as
without them.


1/2 arm: don't fold 'x + 0.0' and '0.0 - x' when the sign of the zero
matters

tcc gives the wrong sign for some additions and subtractions with a
zero constant:

    double x = -0.0;
    double y = x + 0.0;   /* tcc: -0.0, should be +0.0 */

gen_opf() folds 'x + 0.0' and '0.0 + x' to 'x', 'x - 0.0' to 'x' and
'0.0 - x' to '-x', whatever the sign of the zero constant. Under IEEE 754
(round to nearest) these folds are only exact for one sign of the zero:

    'x + -0.0' is x, but -0.0 + +0.0 = +0.0
    'x - +0.0' is x, but -0.0 - -0.0 = +0.0
    '-0.0 - x' is -x, but +0.0 - +0.0 = +0.0

gcc gets all of these right, even with -O0. The patch only keeps the
fold for the zero that makes it exact.

The unary minus needed a change too: on ARM, gen_negf() generates
'0 - x' and relies on gen_opf() turning it into fneg. Once '+0.0 - x'
is no longer folded, -x gave +0.0 for x = +0.0, and tcctest's
signed_zeros and nantest failed. So with VFP, gen_negf is now
gen_opf(TOK_NEG), as on x86_64, i386 and arm64. It emits fneg, a real
sign flip, which is also right for +0.0 and NaNs. ARM without VFP keeps
its current gen_negf().

Test: 154_signed_zero_fold checks the four folds and -x, for float and
double. Its .expect file was generated with gcc.


2/2 arm: support unaligned float/double accesses with VFP

flds/fldd/fsts/fstd fault (SIGBUS) when the address isn't word aligned,
which happens with members of packed structures:

    #pragma pack(1)
    struct s { char c; double d; } *p;
    p->d = 1.5;   /* Bus error */

gcc handles this. When the address isn't known to be word aligned (a
pointer in a register or saved on the stack, or a local or global at an
unaligned offset), the value is now loaded and stored with ldr/str
through a core register (lr, or ip when lr holds the address) and moved
with vmov. Core register accesses support unaligned addresses on ARMv6
and above. Locals and globals at an aligned offset still use
flds/fldd/fsts/fstd, so aligned accesses generate the same code as
before.

Test: 155_unaligned_float covers float and double members of packed
structures through a pointer, in globals, in locals and in a nested
structure, at large offsets and in arrays, and as function arguments.
Its .expect file matches gcc's output.

The two patches are independent, but both touch arm-gen.c, so they're
sent together and numbered in order.

*********

Best regards,

  Fred
From 2424713f5c7d4d7defffd55d02c58fe63deb0d4c Mon Sep 17 00:00:00 2001
From: Fred <[email protected]>
Date: Wed, 11 Nov 2026 17:12:59 +0100
Subject: [PATCH 1/2] arm: don't fold 'x + 0.0' and '0.0 - x' when the sign of
 the zero matters

With VFP, gen_opf() replaced 'x + 0.0' and '0.0 + x' with 'x', 'x - 0.0'
with 'x' and '0.0 - x' with '-x', for a zero constant of any sign. It's
wrong for some signed zeros (IEEE 754, round to nearest):

  -0.0 + 0.0 = +0.0, not -0.0 ('x + +0.0' isn't 'x' when x is -0.0)
  -0.0 - -0.0 = +0.0, not -0.0 ('x - -0.0' isn't 'x' when x is -0.0)
  0.0 - 0.0 = +0.0, not -0.0 ('+0.0 - x' isn't '-x' when x is +0.0)

gcc gets them right, even with -O0. The folds are now only done with the
zero which makes them exact: 'x + -0.0', 'x - +0.0' and '-0.0 - x'.

The unary minus relied on the '0.0 - x' fold (gen_negf() generated
'0 - x'), so with VFP it now uses gen_opf(TOK_NEG) like x86 and arm64,
which emits fneg: a sign flip, also right for +0.0 and NaNs.
---
 arm-gen.c                                | 36 +++++++++++++++++++++---
 tccgen.c                                 |  3 +-
 tests/tests2/154_signed_zero_fold.c      | 21 ++++++++++++++
 tests/tests2/154_signed_zero_fold.expect |  5 ++++
 4 files changed, 60 insertions(+), 5 deletions(-)
 create mode 100644 tests/tests2/154_signed_zero_fold.c
 create mode 100644 tests/tests2/154_signed_zero_fold.expect

diff --git a/arm-gen.c b/arm-gen.c
index 3a65a7cc..d360a02f 100644
--- a/arm-gen.c
+++ b/arm-gen.c
@@ -1796,6 +1796,26 @@ static int is_zero(int i)
   return (vtop[i].c.ld == 0.l);
 }
 
+/* returns 1 if vtop[i] is the constant -0.0 (sign = 1) or +0.0 (sign = 0). Folding 'x + 0.0' or '0.0 - x'
+   depends on the sign of the zero: -0.0 + +0.0 is +0.0, so 'x + +0.0' isn't 'x' when x is -0.0 */
+static int is_signed_zero(int i, int sign)
+{
+  union { float f; uint32_t u; } f;
+  union { double d; uint64_t u; } d;
+
+  if(!is_zero(i))
+    return 0;
+  if (vtop[i].type.t == VT_FLOAT) {
+    f.f = vtop[i].c.f;
+    return (int)(f.u >> 31) == sign;
+  }
+  if (vtop[i].type.t == VT_DOUBLE)
+    d.d = vtop[i].c.d;
+  else
+    d.d = vtop[i].c.ld; /* keeps the sign of a zero */
+  return (int)(d.u >> 63) == sign;
+}
+
 /* generate a floating point operation 'v = t1 op t2' instruction. The
  *    two operands are guaranteed to have the same floating point type */
 void gen_opf(int op)
@@ -1805,9 +1825,10 @@ void gen_opf(int op)
   x=0xEE000A00|T2CPR(vtop->type.t);
   switch(op) {
     case '+':
-      if(is_zero(-1))
+      /* x + -0.0 is x, but x + +0.0 is +0.0 when x is -0.0 */
+      if(is_signed_zero(-1, 1))
         vswap();
-      if(is_zero(0)) {
+      if(is_signed_zero(0, 1)) {
         vtop--;
         return;
       }
@@ -1815,11 +1836,13 @@ void gen_opf(int op)
       break;
     case '-':
       x|=0x300040;
-      if(is_zero(0)) {
+      /* x - +0.0 is x, but x - -0.0 is +0.0 when x is -0.0 */
+      if(is_signed_zero(0, 0)) {
         vtop--;
         return;
       }
-      if(is_zero(-1)) {
+      /* -0.0 - x is -x, but +0.0 - x is +0.0 when x is +0.0 */
+      if(is_signed_zero(-1, 1)) {
         x|=0x810000; /* fsubX -> fnegX */
         vswap();
         vtop--;
@@ -1832,6 +1855,11 @@ void gen_opf(int op)
     case '/':
       x|=0x800000;
       break;
+    case TOK_NEG:
+      /* a sign flip: 0.0 - x isn't -x when x is +0.0 or a NaN */
+      x|=0xB10040; /* fnegX */
+      fneg=1;
+      break;
     default:
       if(op < TOK_ULT || op > TOK_GT) {
         tcc_error("unknown fp op %x!",op);
diff --git a/tccgen.c b/tccgen.c
index d9721a52..29e9b6e1 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -2506,7 +2506,8 @@ static void gen_opic(int op)
     }
 }
 
-#if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386 || defined TCC_TARGET_ARM64
+#if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386 || defined TCC_TARGET_ARM64 \
+    || (defined TCC_TARGET_ARM && defined TCC_ARM_VFP)
 # define gen_negf gen_opf
 #elif defined TCC_TARGET_ARM
 void gen_negf(int op)
diff --git a/tests/tests2/154_signed_zero_fold.c b/tests/tests2/154_signed_zero_fold.c
new file mode 100644
index 00000000..7f5fc7f6
--- /dev/null
+++ b/tests/tests2/154_signed_zero_fold.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <string.h>
+
+/* Adding or subtracting a zero constant can't be folded without checking the sign of the zero,
+   and -x is a sign flip, not 0.0 - x */
+
+static int sign_d(double d) { unsigned char b[sizeof d]; memcpy(b, &d, sizeof d); return b[sizeof d - 1] >> 7; }
+static int sign_f(float f) { unsigned char b[sizeof f]; memcpy(b, &f, sizeof f); return b[sizeof f - 1] >> 7; }
+
+int main(void)
+{
+    double nd = -0.0, pd = 0.0;
+    float nf = -0.0f, pf = 0.0f;
+
+    printf("%d %d %d %d\n", sign_d(nd + 0.0), sign_d(0.0 + nd), sign_d(nd + -0.0), sign_d(-0.0 + nd));
+    printf("%d %d %d %d\n", sign_d(nd - 0.0), sign_d(nd - -0.0), sign_d(0.0 - pd), sign_d(-0.0 - pd));
+    printf("%d %d %d %d\n", sign_f(nf + 0.0f), sign_f(0.0f + nf), sign_f(nf + -0.0f), sign_f(-0.0f + nf));
+    printf("%d %d %d %d\n", sign_f(nf - 0.0f), sign_f(nf - -0.0f), sign_f(0.0f - pf), sign_f(-0.0f - pf));
+    printf("%d %d %d %d\n", sign_d(-pd), sign_d(-nd), sign_f(-pf), sign_f(-nf));
+    return 0;
+}
diff --git a/tests/tests2/154_signed_zero_fold.expect b/tests/tests2/154_signed_zero_fold.expect
new file mode 100644
index 00000000..ec9f9a24
--- /dev/null
+++ b/tests/tests2/154_signed_zero_fold.expect
@@ -0,0 +1,5 @@
+0 0 1 1
+1 0 0 1
+0 0 1 1
+1 0 0 1
+1 0 1 0
-- 
2.53.0

From 26c9edee86b3f29d6d11616005c2b1fd5cc5904d Mon Sep 17 00:00:00 2001
From: Fred <[email protected]>
Date: Wed, 4 Nov 2026 10:30:51 +0100
Subject: [PATCH 2/2] arm: support unaligned float/double accesses with VFP

flds/fldd/fsts/fstd fault (SIGBUS) when the address isn't word aligned,
which happens with members of packed structures, for example:

  #pragma pack(1)
  struct s { char c; double d; } *p;
  p->d = 1.5; /* Bus error */

gcc handles it, so code which works with gcc crashed with tcc.

When the address isn't known to be word aligned (pointer in a register or
saved on the stack, local or global at an unaligned offset), the value is
now loaded/stored with ldr/str through a core register (lr, or ip when lr
holds the address) and moved with vmov. Core register accesses support
unaligned addresses on ARMv6 and above. Locals and globals at an aligned
offset still use flds/fldd/fsts/fstd.
---
 arm-gen.c                               | 81 +++++++++++++++++++++++++
 tests/tests2/155_unaligned_float.c      | 63 +++++++++++++++++++
 tests/tests2/155_unaligned_float.expect |  6 ++
 3 files changed, 150 insertions(+)
 create mode 100644 tests/tests2/155_unaligned_float.c
 create mode 100644 tests/tests2/155_unaligned_float.expect

diff --git a/arm-gen.c b/arm-gen.c
index d360a02f..f4a6916b 100644
--- a/arm-gen.c
+++ b/arm-gen.c
@@ -527,6 +527,69 @@ static int negcc(int cc)
   return TOK_NE;
 }
 
+#ifdef TCC_ARM_VFP
+/* VFP loads/stores (flds/fldd/fsts/fstd) fault on addresses which are not
+   word aligned, which happens with members of packed structures or casted
+   pointers. Tell if the lvalue 'sv' is known to be word aligned. */
+static int vfp_aligned(SValue *sv)
+{
+  int v = sv->r & VT_VALMASK, align;
+
+  if (v == VT_LOCAL)
+    return (sv->c.i & 3) == 0; /* fp is word aligned */
+  if (v == VT_CONST) {
+    if ((sv->r & VT_SYM) && !(sv->sym->type.t & VT_TLS)) {
+      type_size(&sv->sym->type, &align);
+      if (align < 4)
+        return 0;
+    }
+    return (sv->c.i & 3) == 0;
+  }
+  return 0; /* pointer in a register or saved on the stack: unknown */
+}
+
+/* ldr/str rt, [base, #off] with -4095 <= off <= 4095 */
+static void ldst_word(uint32_t op, int rt, uint32_t base, int off)
+{
+  if (off >= 0)
+    op |= 0x800000;
+  else
+    off = -off;
+  o(op | (rt << 12) | (base << 16) | off);
+}
+
+/* Load (store=0) or store (store=1) the float/double VFP register 'r' at
+   [base, +/-fc] which may be unaligned, using core registers which allow
+   unaligned accesses (ARMv6 and above). */
+static void vfp_ldst_unaligned(int store, int r, int ft, uint32_t base, int fc, int sign)
+{
+  int i, n, off, rt = 14; /* lr */
+
+  calcaddr(&base, &fc, &sign, 2047, 0);
+  off = sign ? -fc : fc;
+  if (base == 14) {
+    /* lr holds the address, so use ip and preserve it */
+    rt = 12;
+    o(0xE52DC004); /* push {ip} */
+  }
+  /* a double register dN is made of the single registers s2N (low word)
+     and s2N+1 (high word), a float is held in s2N */
+  n = (ft & VT_BTYPE) == VT_FLOAT ? 1 : 2;
+  for (i = 0; i < n; i++) {
+    uint32_t vmov = ((vfpr(r) << 16) | (i << 7) | (rt << 12));
+    if (store) {
+      o(0xEE100A10 | vmov); /* vmov rt, sX */
+      ldst_word(0xE5000000, rt, base, off + i * 4); /* str rt, [base, #off] */
+    } else {
+      ldst_word(0xE5100000, rt, base, off + i * 4); /* ldr rt, [base, #off] */
+      o(0xEE000A10 | vmov); /* vmov sX, rt */
+    }
+  }
+  if (rt == 12)
+    o(0xE49DC004); /* pop {ip} */
+}
+#endif
+
 /* Load value into register r.
    Use relative/got addressing to avoid setting DT_TEXTREL */
 static void load_value(SValue *sv, int r)
@@ -588,6 +651,9 @@ void load(int r, SValue *sv)
 
   v = fr & VT_VALMASK;
   if (fr & VT_LVAL) {
+#ifdef TCC_ARM_VFP
+    int aligned = !is_float(ft) || vfp_aligned(sv);
+#endif
 
     if ((fr & VT_SYM) && sv->sym->type.t & VT_TLS) {
         /* XXX: this does not work */
@@ -624,6 +690,12 @@ void load(int r, SValue *sv)
     }
     if(v == VT_LOCAL) {
       if(is_float(ft)) {
+#ifdef TCC_ARM_VFP
+        if (!aligned) {
+          vfp_ldst_unaligned(0, r, ft, base, fc, sign);
+          return;
+        }
+#endif
 	calcaddr(&base,&fc,&sign,1020,2);
 #ifdef TCC_ARM_VFP
         op=0xED100A00; /* flds */
@@ -729,6 +801,9 @@ void store(int r, SValue *sv)
 
   v = fr & VT_VALMASK;
   if (fr & VT_LVAL || fr == VT_LOCAL) {
+#ifdef TCC_ARM_VFP
+    int aligned = !is_float(ft) || vfp_aligned(sv);
+#endif
 
     if ((fr & VT_SYM) && sv->sym->type.t & VT_TLS) {
         /* XXX: this does not work */
@@ -757,6 +832,12 @@ void store(int r, SValue *sv)
     }
     if(v == VT_LOCAL) {
        if(is_float(ft)) {
+#ifdef TCC_ARM_VFP
+        if (!aligned) {
+          vfp_ldst_unaligned(1, r, ft, base, fc, sign);
+          return;
+        }
+#endif
 	calcaddr(&base,&fc,&sign,1020,2);
 #ifdef TCC_ARM_VFP
         op=0xED000A00; /* fsts */
diff --git a/tests/tests2/155_unaligned_float.c b/tests/tests2/155_unaligned_float.c
new file mode 100644
index 00000000..27e8c287
--- /dev/null
+++ b/tests/tests2/155_unaligned_float.c
@@ -0,0 +1,63 @@
+/* float/double members of packed structures at unaligned addresses
+   (flds/fldd/fsts/fstd fault on them on ARM) */
+#include <stdio.h>
+
+#pragma pack(1)
+struct s {
+    char c;
+    double d;
+    float f;
+    char pad[3000]; /* large offsets */
+    double far_d;
+    float farr[5];
+    double darr[3];
+};
+struct outer {
+    char c;
+    struct s inner;
+};
+#pragma pack()
+
+static char buf[2 * sizeof(struct s)];
+struct s g1;
+char gc;
+struct s g2;
+struct outer go;
+
+static double sum(double a, double b, double c, double d,
+                  double e, double f, double g, double h)
+{
+    return a + b + c + d + e + f + g + h;
+}
+
+int main(void)
+{
+    int i;
+    struct s *p = (struct s *)(buf + 1);
+    char lc; struct s l1; char lc2; struct s l2;
+    volatile double x = 1.25;
+    double a1 = x, a2 = x * 2, a3 = x * 3, a4 = x * 4, a5 = x * 5, a6 = x * 6;
+
+    p->d = a1; p->f = (float)a2; p->far_d = a3;
+    for (i = 0; i < 5; i++)
+        p->farr[i] = (float)(i + a4);
+    for (i = 0; i < 3; i++)
+        p->darr[i] = i + a5;
+    g1.d = a1; g2.d = a2; g2.f = 9.5f; g2.far_d = a3;
+    go.inner.d = a4; go.inner.darr[2] = a5;
+    l1.d = a6; l2.d = a1; l2.f = 1.5f; l2.far_d = a2; l2.darr[1] = a3;
+    lc = lc2 = gc = 0;
+
+    printf("%g %g %g\n", p->d, p->f, p->far_d);
+    for (i = 0; i < 5; i++)
+        printf("%g ", p->farr[i]);
+    for (i = 0; i < 3; i++)
+        printf(i < 2 ? "%g " : "%g", p->darr[i]);
+    printf("\n%g %g %g %g %g %g\n", g1.d, g2.d, g2.f, g2.far_d, go.inner.d, go.inner.darr[2]);
+    printf("%g %g %g %g %g\n", l1.d, l2.d, l2.f, l2.far_d, l2.darr[1]);
+    printf("%g\n", sum(p->d, g1.d, l1.d, p->f, p->far_d, l2.far_d, go.inner.d, p->darr[2]));
+
+    p->d += 1; l2.f *= 2; g2.far_d -= 0.75; go.inner.darr[2] /= 2;
+    printf("%g %g %g %g\n", p->d, l2.f, g2.far_d, go.inner.darr[2]);
+    return lc + lc2 + gc;
+}
diff --git a/tests/tests2/155_unaligned_float.expect b/tests/tests2/155_unaligned_float.expect
new file mode 100644
index 00000000..8de3a408
--- /dev/null
+++ b/tests/tests2/155_unaligned_float.expect
@@ -0,0 +1,6 @@
+1.25 2.5 3.75
+5 6 7 8 9 6.25 7.25 8.25
+1.25 2.5 9.5 3.75 5 6.25
+7.5 1.25 1.5 2.5 3.75
+32
+2.25 3 3 3.125
-- 
2.53.0

_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to