duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 3c2d8e25bd89d32c7312fcd5bc7328d87157b2c4
parent d15f3d5b34d43f255136f897ee0cde301c9dc30f
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Sun, 13 Nov 2022 13:27:50 -0500

cc: fix bug in pointer arithmetics

Diffstat:
Mfs/cc/vm/commonhi.fs | 1+
Mfs/tests/cc/cc.fs | 1+
Mfs/tests/cc/test.c | 5+++++
3 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/fs/cc/vm/commonhi.fs b/fs/cc/vm/commonhi.fs @@ -52,6 +52,7 @@ ARIOPCNT wordtbl constops ( n n -- n ) \ vm-, is special because it handles the special "pointer-pointer" situation. : vm-, _ptrariadj vmop :typeptr? vmop^ :typeptr? and if vmop :*arisz typesize>pow2 1 ( - ) _ariop, ( shr ) vmop :>>n + TYPE_INT to vmop type else 1 ( - ) _ariop, then ; : _assignop, vmop :isconst? not _assert assignop, ; diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs @@ -32,6 +32,7 @@ exprparens 9 #eq cnoop ( no result! ) scnt 0 #eq 42 ptrari 50 #eq 42 50 ptrari2 2 #eq +42 50 ptrari3 46 #eq array 52 #eq global 1234 #eq globalinc 1236 #eq diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c @@ -127,6 +127,11 @@ int* ptrari(int *x) { int ptrari2(int *lo, int *hi) { return hi-lo; } +// when subtracting 2 pointers, the result is considered a scalar for the +// remainder of the expression. +int* ptrari3(int *lo, int *hi) { + return lo+((hi-lo)/2); +} int array() { int a[3] = {42, 12, 2}; return *a + a[1] - *(a+2);