commit aae2ffe77a3cc79a16182f5e89ef572531d3ab23
parent a915064f143bcbfbdff7d65ac0957204cf9904aa
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 20 Jun 2022 13:45:02 -0400
cc: make pointer arithmetics a bit more solid
Diffstat:
2 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/fs/cc/vm.fs b/fs/cc/vm.fs
@@ -117,7 +117,7 @@ operands value 'curop
\ Force current operand to be copied to a register
: op>reg
- hasop# optype VM_REGISTER = not if
+ hasop# optype $f and VM_REGISTER = not if
regallot dup r! ( regid ) opAsm mov, oparg! VM_REGISTER optype! then ;
\ Resolve any referencing into a "simple" result. A VM_STACKFRAME goes into a
@@ -222,7 +222,9 @@ operands value 'curop
reglvl 4 >= if edx pop, then ;
\ Code generation - Unary ops
-: unaryopprep selop2 noop# selop1 op>reg opAsm ;
+\ Unary operations are performed on the selected op, which can be either op1 or
+\ op2.
+: unaryopprep op>reg opAsm ;
: vmneg, unaryopprep neg, ;
: vmnot, ( ~ ) unaryopprep not, ;
: vmboolnot, unaryopprep
@@ -232,13 +234,15 @@ operands value 'curop
\ pre-inc/dec op1
\ TODO: *opAsm goes below, not opAsm. We must inc the reference, not the result.
-: vm++op, selop2 noop# selop1 opAsm inc, ;
-: vm--op, selop2 noop# selop1 opAsm dec, ;
+: vm++op, opAsm inc, ;
+: vm--op, opAsm dec, ;
\ post-inc/dec op1
\ It's a bit complicated here. Before we inc/dec, we need a copy of the current
\ value in a new register, which will be our result.
\ For now, we only support op1=VM_STACKFRAME
+\ TODO: don't use both ops for this and thus allow post-inc and post-dec to run
+\ on either on the 2 ops
: _ ( 'w -- )
selop1 optype VM_STACKFRAME = _assert
selop2 noop# selop1 optype oparg selop2 oparg! optype!
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -59,11 +59,14 @@ int exprparens() {
return (1 + 2) * 3;
}
void cnoop() {return;}
+int* ptrari(int *x) {
+ return x + 1;
+}
int array() {
int a[3];
+ int *b;
*a = 42;
- return *a;
-}
-int* ptrari(int *x) {
- return x + 1;
+ b = a+1;
+ *b = 12;
+ return *a + *(a+1);
}