commit 6a32185a183a9e337f0cbe3c524db58539ed24da
parent fec173c6461d5be65897383b89b9bb04e7f4af71
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 13 Jul 2022 06:54:38 -0400
cc: add pspush() and pspop()
Diffstat:
6 files changed, 34 insertions(+), 6 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -37,7 +37,7 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 3 c, 3 c, 3 c, 3 c,
: boptoken ( opid -- tok ) BOPTlist slistiter ;
\ AST node types
-16 const ASTIDCNT
+18 const ASTIDCNT
0 const AST_DECLARE
1 const AST_UNIT
2 const AST_FUNCTION
@@ -54,6 +54,8 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 3 c, 3 c, 3 c, 3 c,
13 const AST_STRLIT
14 const AST_FUNCALL \ first elem is addr to call, the rest are args
15 const AST_FOR \ for (child0; child1; child2) { child3 }
+16 const AST_PSPUSH
+17 const AST_PSPOP
\ It's important that decl.name and func.name have the same offset. Poor man's
\ polymorphism...
@@ -74,7 +76,7 @@ NODESZ ufield ast.bop.opid
NODESZ 'ufield ast.strlit.value
ASTIDCNT stringlist astidnames
"declare" "unit" "function" "return" "constant" "stmts" "args" "ident"
-"unaryop" "postop" "binop" "list" "if" "str" "call" "for"
+"unaryop" "postop" "binop" "list" "if" "str" "call" "for" "push" "pop"
0 value curunit \ points to current Unit, the beginning of the AST
@@ -145,6 +147,8 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
:w ( StrLit ) _[ dup ast.strlit.value stype _] ;
'w noop ( FunCall )
'w noop ( For )
+'w noop ( PSPush )
+'w noop ( PSPop )
: printast ( node -- )
?dup not if ." null" exit then
@@ -231,6 +235,7 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
\ 4. A function call
\ 5. An expression inside () parens.
\ 6. A string literal
+\ 7. pspop()
: parseFactor ( tok -- node )
case
'(' of isChar?^ ( )
@@ -240,6 +245,10 @@ alias noop parseExpression ( tok -- node ) \ forward declaration
_cc< dup '"' = not while c, 1+ repeat ( node cnt c )
drop over NODESZ + c! ( node )
endof
+ S" pspop" of s= ( )
+ nextt '(' expectChar nextt ')' expectChar
+ AST_PSPOP createnode
+ endof
of uopid ( opid )
AST_UNARYOP createnode swap , ( opnode )
nextt parseFactor over addnode ( opnode ) endof
@@ -317,8 +326,8 @@ current to parseExpression
alias noop parseStatements ( funcnode -- ) \ forward declaration
-3 stringlist statementnames "return" "if" "for"
-3 wordtbl statementhandler ( snode -- snode )
+4 stringlist statementnames "return" "if" "for" "pspush"
+4 wordtbl statementhandler ( snode -- snode )
:w ( return )
dup AST_RETURN newnode ( snode rnode )
nextt dup S" ;" s= if \ empty returns are allowed
@@ -342,6 +351,10 @@ alias noop parseStatements ( funcnode -- ) \ forward declaration
nextt parseExpression ( sn forn expr ) over addnode
nextt ')' expectChar
parseStatements ( snode ifnode ) ;
+:w ( pspush ) dup AST_PSPUSH newnode ( snode pushnode )
+ nextt '(' expectChar
+ nextt parseExpression swap addnode
+ nextt ')' expectChar read; ;
: _ ( parentnode -- ) \ parseStatements
nextt '{' expectChar AST_STATEMENTS newnode nextt
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -258,6 +258,8 @@ ASTIDCNT wordtbl gentbl ( node -- )
nextsibling dup _assert dup gennode ops$ \ adjustment
nextsibling dup _assert gennode ( loop' cond' ) \ body
swap vmjmp, ( cond' ) ]vmjmp ;
+:w ( PSPush ) firstchild dup _assert gennode vmpspush, ;
+:w ( PSPop ) drop vmpspop, ;
: _ ( node -- ) gentbl over nodeid wexec ;
current to gennode
diff --git a/fs/cc/vm.fs b/fs/cc/vm.fs
@@ -188,7 +188,7 @@ operands value 'curop
selop2 oparg swap oparg! optype rot optype!
selop1 optype! oparg! ;
-\ Code generation - Functions, calls, ret
+\ Code generation - Functions, calls, ret, pspush, pspop
\ generate function prelude code by allocating "locsz" bytes on PS.
: vmprelude, ( argsz locsz -- )
@@ -216,6 +216,13 @@ operands value 'curop
VM_REGISTER optype! regallot dup oparg! r! [ebp] mov,
ebp 4 i32 add, 0 to callsz ;
+\ Allocate a new register for active op and pop 4b from PS into it.
+: vmpspop,
+ noop# VM_REGISTER optype! regallot dup oparg! r! [ebp] mov,
+ ebp CELLSZ i32 add, ;
+
+\ Push active op to PS.
+: vmpspush, opderef ebp CELLSZ i32 sub, [ebp] opAsm mov, opdeinit ;
\ Code generation - Binary ops
: binopprep ( -- ) \ prepare ops for the binop
diff --git a/fs/sys/scratch.fs b/fs/sys/scratch.fs
@@ -7,5 +7,7 @@
\ these cursor live in the system scratchpad, they'd be overwritten by faster-
\ paced data.
-$4000 scratchpad$ syspad
+\ TODO: investigate why CC has memory corruption when running tests with a $4000
+\ syspad.
+$8000 scratchpad$ syspad
syspad to Scratchpad
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -16,6 +16,7 @@ binopshr 10 #eq
boolops 0 #eq
variables 82 #eq
funcall 42 #eq
+42 pushpop 42 #eq
2 3 adder 5 #eq
3 2 subber 1 #eq
42 plusone 43 #eq
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -45,6 +45,9 @@ int variables() {
int funcall() {
return retconst();
}
+void pushpop() {
+ pspush(pspop());
+}
int adder(int a, int b) {
return a + b;
}