commit 2566055e5df724f0b4e9725211fbdbddfcd7e021
parent 67bc87acde9da34b8bb4ba11baf0856c290a13db
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Thu, 3 Nov 2022 14:34:47 -0400
cc: code consolidation
Diffstat:
4 files changed, 22 insertions(+), 22 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -102,11 +102,10 @@ extends Node struct[ ASTNode
extends ASTNode struct[ Declare
sfield ctype
- sfield flags
: :new ( ctype -- node )
- AST_DECLARE ASTNode :new ( ctype node )
- swap ( ctype ) _arena :, curstatic ( flags ) _arena :, ;
+ curstatic if dup CType :static! then
+ AST_DECLARE ASTNode :new ( ctype node ) swap _arena :, ;
: :name ctype CType name ;
\ Number of bytes required to hold this variable declaration in memory.
@@ -115,7 +114,7 @@ extends ASTNode struct[ Declare
]struct
\ a node that contains Declare children (among others)
-extends ASTNode struct[ Declarations
+extends ASTNode struct[ Statements
: :totsize ( fnode -- size-in-bytes )
firstchild ( node ) 0 begin ( node r )
over while ( node r )
@@ -143,11 +142,10 @@ extends ASTNode struct[ Declarations
extends ASTNode struct[ Function
sfield ctype \ the signature of the function.
- sfield flags \ b0=static
: :new ( ctype -- node )
- AST_FUNCTION ASTNode :new swap ( node ctype )
- ( ctype ) _arena :, curstatic ( flags ) _arena :, ;
+ curstatic if dup CType :static! then
+ AST_FUNCTION ASTNode :new swap ( node ctype ) _arena :, ;
: name ctype CType name ;
: :stmts ( self -- snode ) firstchild dup id AST_STATEMENTS = _assert ;
@@ -155,7 +153,7 @@ extends ASTNode struct[ Function
: :finddecl ( name self -- ctype-or-0 )
2dup ctype CType :find ?dup if nip nip else :stmts findDecl then ;
: :argssize ( self -- size-in-bytes ) ctype CType :argssize ;
- : :locsize ( self -- size-in-bytes ) :stmts Declarations :totsize ;
+ : :locsize ( self -- size-in-bytes ) :stmts Statements :totsize ;
]struct
extends ASTNode struct[ Constant
@@ -250,7 +248,7 @@ extends ASTNode struct[ StrLit
: i]# Ident :new ]# ;
ASTIDCNT stringlist astidnames
-"declare" "unit" "function" "return" "constant" "stmts" "funcsig" "ident"
+"declare" "unit" "function" "return" "constant" "stmts" "unused" "ident"
"unaryop" "postop" "binop" "list" "if" "str" "call" "for" "push" "pop" "break"
"while" "do" "arrow"
@@ -262,9 +260,7 @@ ASTIDCNT stringlist astidnames
ASTIDCNT wordtbl astdatatbl ( node -- node )
:w ( Declare ) _[ dup Declare ctype CType :. _] ;
'w noop ( Unit )
-:w ( Function ) _[
- dup Function ctype printtype spc>
- dup Function flags .x1 _] ;
+:w ( Function ) _[ dup Function ctype printtype _] ;
'w noop ( Return )
:w ( Constant ) _[ dup Constant value .x _] ;
'w noop ( Statements )
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -128,9 +128,9 @@ BOPSCNT wordtbl bopgentblpost ( -- )
ASTIDCNT wordtbl gentbl ( node -- )
:w ( Declare ) dup Declare ctype CType :isglobal? if
- dup Declare flags 1 and not if \ not static
- dup Declare ctype CType name NEXTWORD ! create then ( dnode )
- here over Declare ctype to CType offset
+ dup Declare ctype dup CType :static? not if \ not static
+ dup CType name NEXTWORD ! create then ( dnode ctype )
+ here swap to CType offset ( dnode )
dup Node firstchild ?dup if ( dnode inode )
nip gennode \ initialization value in vmop
\ TODO: support Ident during initialization
@@ -149,9 +149,9 @@ ASTIDCNT wordtbl gentbl ( node -- )
:w ( Function )
_ccdebug if ." debugging: " dup Function name stype nl> then
ops$
- dup Function flags 1 and not if \ not static
- sysdict over Function name entry then ( fnode )
- here over Function ctype to CType offset
+ dup Function ctype dup CType :static? not if \ not static
+ sysdict over CType name entry then ( fnode ctype )
+ here swap to CType offset ( fnode )
dup Function :argssize over
Function :locsize ( argsz locsz ) vmprelude, dup genchildren
nobreaks# \ no dangling "break" markers
diff --git a/fs/cc/ttr.fs b/fs/cc/ttr.fs
@@ -61,7 +61,7 @@ ASTIDCNT wordtbl trtbl ( node -- )
'w trchildren ( Function )
'w trchildren ( Return )
'w drop ( Constant )
-:w ( Statements ) dup Declarations :computeDeclAddrs trchildren ;
+:w ( Statements ) dup Statements :computeDeclAddrs trchildren ;
'w _err ( unused )
'w drop ( Ident )
:w ( UnaryOp )
diff --git a/fs/cc/type.fs b/fs/cc/type.fs
@@ -67,6 +67,7 @@ struct[ CType
\ of the struct. First field is nexttype.
\ b1=is a funcsig? if 1, type is func return type and name is
\ the name of the sig. arguments follow in nexttype.
+ \ b2=has static storage?
sfield offset \ offset, in bytes, of this element within its list
\ if this ctype is a function, offset contains its address.
sfield nbelem \ number of elements in array. 0 if not an array.
@@ -78,7 +79,11 @@ struct[ CType
_globalmode if _ else $100 SZ + _arena :[ _ _arena :] drop then ;
: :struct? flags 1 and ;
+ : :struct! dup flags 1 or swap to flags ;
: :funcsig? flags 2 and bool ;
+ : :funcsig! dup flags 2 or swap to flags ;
+ : :static? flags 4 and bool ;
+ : :static! dup flags 4 or swap to flags ;
: :funcptr?
type dup ctype? if
dup type*lvl 1 = swap ctype' :funcsig? and
@@ -206,8 +211,7 @@ alias _err parseDeclarator ( type -- ctype ) \ forward declaration
nextt ']' expectChar ( ctype nbelem )
over to CType nbelem endof
'(' of isChar?^
- 2 over to CType flags \ func
- STORAGE_MEM over to CType storage
+ dup CType :funcsig! STORAGE_MEM over to CType storage
nextt dup ')' isChar? if drop
else ( ctype tok ) over swap _arg ( ctype offset ) drop then endof
r> to nexttputback exit
@@ -249,7 +253,7 @@ current to parseDeclarator
dup S" struct" s= if
drop nextt dup isIdent? if nextt 1 to _globalmode else NULLSTR swap then
'{' expectChar ( name ) TYPE_VOID CType :new ( res )
- 1 over to CType flags
+ dup CType :struct!
_globalmode if dup addGlobalTypedef then
0 >r dup begin ( res prev ) \ V1=offset
nextt dup '}' isChar? not while ( res prev tok )