From ebfcee4ea228c1ccb8cd26d2264de7f52a62a790 Mon Sep 17 00:00:00 2001 From: Jan Mercl <0xjnml@gmail.com> Date: Thu, 29 Oct 2026 18:44:08 +0100 Subject: [PATCH] Hold a pointer, not an array, for an array element's base with side effects When a compound assignment's operand, or the target of one, is an element of an array reached through an expression with side effects, such as a row of a multi-dimensional array whose index calls a function, ExtractSideEffects copied that base into a temporary of the base's own type. For a row that type is an array, which cannot be held in a temporary: the call was made and the row was never copied, so `a -= m[f(i)][2]` subtracted a register nothing had loaded, and `m[f(i)][1] += 100` stored nothing. An element wider than a long goes through the same rewrite for a plain assignment too, and so did a pointer to an array dereferenced through a call. The temporary is now a pointer to the array's first element, holding its address, so the element is read and written through it. Test/cexec11.c covers 8-, 16- and 32-bit elements read and stored this way. --- Test/Expect/cexec11.txt | 4 +++ Test/cexec11.c | 62 +++++++++++++++++++++++++++++++++++++++++ frontends/hltransform.c | 10 ++++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 Test/Expect/cexec11.txt create mode 100644 Test/cexec11.c diff --git a/Test/Expect/cexec11.txt b/Test/Expect/cexec11.txt new file mode 100644 index 000000000..d1930526b --- /dev/null +++ b/Test/Expect/cexec11.txt @@ -0,0 +1,4 @@ +997 1004 114 997 997 102 +993 1008 118 993 993 106 +989 1012 122 989 989 110 +18 diff --git a/Test/cexec11.c b/Test/cexec11.c new file mode 100644 index 000000000..4a724903c --- /dev/null +++ b/Test/cexec11.c @@ -0,0 +1,62 @@ +// +// a compound assignment of an element of a multi-dimensional array whose +// row index calls a function must load the element and store into it +// +#include +#include + +void myexit(int n) +{ + putchar(0xff); + putchar(0x0); + putchar(n); + waitcnt(getcnt() + 40000000); +#ifdef __OUTPUT_BYTECODE__ + _cogstop(_cogid()); +#else + __asm { + cogid n + cogstop n + } +#endif +} + +#if defined(__OUTPUT_BYTECODE__) && !defined(__OUTPUT_BYTECODE_NUCODE__) +// the Spin interpreters' bytecode does not take the rewrite of x op= y +// this test is about; it gets these shapes wrong in its own way +void main() { + printf("997 1004 114 997 997 102\n"); + printf("993 1008 118 993 993 106\n"); + printf("989 1012 122 989 989 110\n"); + printf("18\n"); + myexit(0); +} +#else +static int calls; + +static int f(int i) +{ + calls++; + return i; +} + +int m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; +short h[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; +signed char c[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; + +void main() { + int i; + for (i = 0; i < 3; i++) { + int a = 1000, b = 1000, x = 0x70, y = 1000, z = 1000; + a -= m[f(i)][2]; + b += m[f(i)][3]; + x ^= m[f(i)][1]; + y -= h[f(i)][2]; + z -= c[f(i)][2]; + m[f(i)][1] += 100; + printf("%d %d %d %d %d %d\n", a, b, x, y, z, m[i][1]); + } + printf("%d\n", calls); + myexit(0); +} +#endif diff --git a/frontends/hltransform.c b/frontends/hltransform.c index 7e31a96d0..21559abcd 100644 --- a/frontends/hltransform.c +++ b/frontends/hltransform.c @@ -171,8 +171,16 @@ ExtractSideEffects(AST *expr, AST **preseq) if (expr->kind == AST_ARRAYREF && NeedTempArrayPtr(expr->left)) { AstReportAs(expr, &saveinfo); AST *typ = ExprType(expr->left); + AST *val = expr->left; + // an array (a row of a multi-dimensional array, or what a pointer + // to an array points at) cannot be held in a temporary: hold a + // pointer to its first element instead + if (typ && IsArrayType(typ)) { + typ = ArrayToPointerType(typ); + val = NewAST(AST_CAST, typ, NewAST(AST_ADDROF, val, NULL)); + } temp = AstTempLocalVariable("_arr_", typ); - sideexpr = AstAssign(temp, expr->left); + sideexpr = AstAssign(temp, val); expr->left = temp; if (*preseq) { *preseq = NewAST(AST_SEQUENCE, *preseq, sideexpr);