commit fec173c6461d5be65897383b89b9bb04e7f4af71
parent e96060d72c5b57dcef7f25da7d16bb9343cec11a
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 13 Jul 2022 06:11:58 -0400
cc: allow implicit returns in statements
Diffstat:
3 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -116,6 +116,9 @@ ASTIDCNT stringlist astidnames
over ast.decl.totsize +
swap nextsibling swap repeat ( node r ) nip ;
+\ is snode a function body?
+: ast.stmts.funcbody? ( snode -- f ) parentnode nodeid AST_FUNCTION = ;
+
: _[ '[' emit ;
: _] ']' emit ;
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -189,7 +189,11 @@ ASTIDCNT wordtbl gentbl ( node -- )
:w ( Constant ) ast.const.value const>op ;
:w ( Statements )
\ we run ops$ between each statement to discard any unused Result
- firstchild ?dup if begin dup gennode ops$ nextsibling ?dup not until then ;
+ dup firstchild begin ?dup while dup gennode ops$ nextsibling repeat ( snode )
+ dup ast.stmts.funcbody? if
+ \ emit implicit vmret, if needed
+ lastchild ?dup if nodeid AST_RETURN = not if vmret, then else vmret, then
+ else drop then ;
'w genchildren ( ArgSpecs )
:w ( Ident ) dup lv>decl ?dup if ( inode dnode )
nip decl>op else ( inode )
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -91,7 +91,7 @@ int exprparens() {
return (1 + 2) * 3;
}
// test that a void function doesn't add anything to PS
-void cnoop() {return;}
+void cnoop() {}
// test that pointer arithmetics properly multiply operands by 2 or 4.
int* ptrari(int *x) {
return x + 1;