commit f40f3e9f6ddb1c0d02bc29844dcd4cd9251d2820
parent 180bb94f0f1e24cffbb5772abd8b2261a874e804
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sat, 5 Nov 2022 13:29:25 -0400
cc: fold constant macro expressions when necessary
Diffstat:
4 files changed, 34 insertions(+), 22 deletions(-)
diff --git a/fs/app/cos/cvm.c b/fs/app/cos/cvm.c
@@ -4,8 +4,7 @@
#define SP_ADDR $fffa
#define RS_ADDR $ff00
#define SYSVARS $fe00
-// TODO: allow constant expressions in macros to allow stuff like SYSVARS + $18
-#define SYSVARS_TOPTR $fe18
+#define SYSVARS_TOPTR SYSVARS+$18
/* Port for block reads. Each read or write has to be done in 5 IO writes:
* 1 - r/w. 1 for read, 2 for write.
* 2 - blkid MSB
diff --git a/fs/cc/macro.fs b/fs/cc/macro.fs
@@ -1,14 +1,13 @@
\ CC macros (high part)
?f<< /cc/macrolo.fs
?f<< /cc/ast.fs
+?f<< /cc/ttr.fs
: _err ( -- ) abort" macro error" ;
: _assert ( f -- ) not if _err then ;
struct+[ Macro
- : _asnum ( self -- n )
- ast dup Node id AST_CONSTANT = _assert
- Constant value ;
+ : _asnum ( self -- n ) ast ASTNode :?asnum _assert ;
current to :asnum
struct+[ MacroOps
diff --git a/fs/cc/ttr.fs b/fs/cc/ttr.fs
@@ -49,6 +49,33 @@ BOPSCNT wordtbl bopconsttbl ( -- )
'w _err
'w _err
+struct+[ ASTNode
+ \ Try to transform this node into a constant number.
+ \ f=1 and n=number if it's possible, otherwise f=0.
+ : :?asnum ( self -- n? f ) dup id case ( self )
+ AST_CONSTANT of = Constant value 1 endof
+ AST_UNARYOP of =
+ dup firstchild :?asnum if ( self n )
+ swap uopconsttbl swap UnaryOp opid wexec 1
+ else drop 0 then endof
+ AST_BINARYOP of =
+ dup firstchild dup :?asnum if ( self child n1 )
+ swap nextsibling :?asnum if ( self n1 n2 )
+ rot bopconsttbl swap BinaryOp opid wexec 1
+ else 2drop 0 then
+ else 2drop 0 then endof
+ drop 0
+ endcase ;
+]struct
+
+struct+[ Op
+ \ If possible, fold this node into a Constant.
+ : :?fold ( self -- )
+ dup >r :?asnum if
+ AST_CONSTANT to r@ id ( n ) to r@ Constant value 0 to r@ firstchild then
+ rdrop ;
+]struct
+
alias noop trnode ( node -- ) \ forward declaration
: trchildren ( node -- )
@@ -64,23 +91,9 @@ ASTIDCNT wordtbl trtbl ( node -- )
'w trchildren ( Statements )
'w _err ( unused )
'w drop ( Ident )
-:w ( UnaryOp )
- dup >r trchildren
- r@ Node firstchild dup Node id AST_CONSTANT = if
- Constant value uopconsttbl r@ UnaryOp opid wexec ( n )
- AST_CONSTANT to r@ Node id ( n ) to r@ Constant value
- 0 to r@ Node firstchild else drop then
- rdrop ;
+:w ( UnaryOp ) dup trchildren UnaryOp :?fold ;
'w trchildren ( PostfixOp )
-:w ( BinaryOp )
- dup >r BinaryOp :*ari r@ trchildren
- r@ Node firstchild dup Node nextsibling ( n1 n2 )
- over Node id AST_CONSTANT = over Node id AST_CONSTANT = and if
- Constant value swap Constant value swap
- bopconsttbl r@ BinaryOp opid wexec ( n )
- AST_CONSTANT to r@ Node id ( n ) to r@ Constant value
- 0 to r@ Node firstchild else 2drop then
- rdrop ;
+:w ( BinaryOp ) dup BinaryOp :*ari dup trchildren BinaryOp :?fold ;
'w trchildren ( List )
'w trchildren ( If )
'w drop ( StrLit )
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -1,6 +1,7 @@
/* test a few simple C constructs */
-#define MYCONST 42
+#define FOUR 4
+#define MYCONST 40+FOUR-2
// just return a constant
short retconst() {