commit ea6c69886642d5c8794702485900a222ef769736
parent e550d3ac2ee9d7e16c72b259291d47367c7f5971
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Thu, 3 Nov 2022 10:10:54 -0400
cc: code consolidation
Diffstat:
2 files changed, 22 insertions(+), 26 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -23,10 +23,10 @@ Arena :new structbind Arena _arena
\ This dictionary below contain global symbols of the current unit
create symbols 0 , 0 c, \ this is a dict link
-: addSymbol ( node name -- )
- symbols swap dup c@ ( node 'dict name len )
+: addSymbol ( ctype name -- )
+ symbols swap dup c@ ( ctype 'dict name len )
ENTRYSZ + 8 + _arena :[ entry , _arena :] drop ;
-: findSymbol ( name -- node-or-0 ) symbols find dup if @ then ;
+: findSymbol ( name -- ctype-or-0 ) symbols find dup if @ then ;
: ccast$ _arena :reset 0 to curunit 0 symbols ! ;
\ Unary operators
@@ -132,12 +132,12 @@ extends ASTNode struct[ Declarations
\ Find, in a node that contains AST_DECLARE nodes, the first one with a matching
\ name, or 0 if none is found.
-: findDecl ( name node -- dnode-or-0 )
+: findDecl ( name node -- ctype-or-0 )
Node firstchild begin ( name node )
?dup while ( name node )
dup Node id AST_DECLARE = if
over over Declare :name s= if ( name node )
- nip exit then then
+ nip Declare ctype exit then then
Node nextsibling repeat ( name ) drop 0 ;
extends ASTNode struct[ FuncSig
@@ -146,21 +146,20 @@ extends ASTNode struct[ FuncSig
extends ASTNode struct[ Function
sfield ctype \ the signature of the function.
- \ for now, it's only a placeholder to its address.
- \ code consolidation in progress.
sfield flags \ b0=static
: :new ( type name -- node )
swap CType :new ( ctype )
+ STORAGE_MEM over to CType storage
+ 2 ( funcsig ) over to CType flags
AST_FUNCTION ASTNode :new swap ( node ctype )
- STORAGE_MEM over to CType storage _arena :, ( node )
- curstatic ( flags ) _arena :, ;
+ ( ctype ) _arena :, curstatic ( flags ) _arena :, ;
: name ctype CType name ;
: :sig ( self -- anode ) firstchild dup id AST_FUNCSIG = _assert ;
: :stmts ( self -- snode ) :sig nextsibling dup id AST_STATEMENTS = _assert ;
: :rettype ctype CType type ;
- : :finddecl ( name self -- dnode-or-0 )
+ : :finddecl ( name self -- ctype-or-0 )
2dup :sig findDecl ?dup if nip nip else :stmts findDecl then ;
: :argssize ( self -- size-in-bytes ) :sig Declarations :totsize ;
: :locsize ( self -- size-in-bytes ) :stmts Declarations :totsize ;
@@ -178,10 +177,10 @@ extends ASTNode struct[ Ident
: :new ( name -- node ) AST_IDENT ASTNode :new swap _arena :s, ;
- : :finddecl ( self -- dnode-or-fnode-or-0 )
+ : :finddecl ( self -- ctype-or-0 )
dup name dup rot AST_FUNCTION swap Node :findparent ( name name fnode )
- dup if Function :finddecl else nip then ?dup if ( name d-or-fnode )
- nip else ( name ) findSymbol then dup to lastidentfound ;
+ dup if Function :finddecl else nip then ( name ctype-or-0 )
+ ?dup if nip else ( name ) findSymbol then ( ctype ) dup to lastidentfound ;
]struct
extends ASTNode struct[ Op
@@ -204,7 +203,7 @@ struct+[ ASTNode
: :type dup id case ( self )
AST_IDENT of =
- Ident :finddecl ?dup _assert Declare ctype CType :type endof
+ Ident :finddecl ?dup _assert CType :type endof
AST_UNARYOP of =
dup firstchild :type swap Op opid case ( type )
UOP& of = type*lvl+ endof
@@ -516,13 +515,13 @@ current to parseStatement
\ returntype, name and '(' have already been parsed, parse the rest
: parseFuncDef ( unitnode type name -- fnode )
Function :new ( unode fnode )
- dup dup Function name addSymbol ( unode fnode )
+ dup Function ctype over Function name addSymbol ( unode fnode )
parseFuncArgs over Node :add ( unode fnode )
dup rot Node :add ( fnode ) dup parseStatement ;
: parseGlobalDecl ( unitnode ctype -- dnode )
Declare :new ( unode dnode ) dup rot Node :add ( dnode )
- dup dup Declare :name addSymbol
+ dup Declare ctype over Declare :name addSymbol
STORAGE_MEM over Declare ctype to CType storage ( dnode )
dup nextt parseDeclareInit read; ;
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -170,13 +170,9 @@ ASTIDCNT wordtbl gentbl ( node -- )
'w drop ( ArgSpecs )
:w ( Ident )
_ccdebug if ." ident: " dup printast nl> then
- dup Ident :finddecl ?dup if ( inode dnode-or-fnode )
- nip dup Node id AST_FUNCTION = if
- \ Sometimes, we get a Function as dnode. In these cases, it's a global
- \ address and its type is "void"
- TYPE_VOID to vmop type
- Function ctype CType offset const>op
- else Declare ctype ctype>op then
+ dup Ident :finddecl ?dup if ( inode ctype )
+ nip dup CType :funcsig? if
+ TYPE_VOID to vmop type CType offset const>op else ctype>op then
else ( inode )
Ident name sysdict @ find ?dup _assert TYPE_VOID to vmop type const>op then
_ccdebug if .ops then ;
@@ -225,9 +221,10 @@ ASTIDCNT wordtbl gentbl ( node -- )
\ Resolve address node
0 to lastidentfound
dup Node firstchild gennode \ op has call address
- lastidentfound ?dup if
- dup Node id AST_FUNCTION = if Function :rettype
- else Declare ctype CType type dup CType :funcsig? _assert CType type then
+ lastidentfound ?dup if ( ctype )
+ \ We either have a direct function signature or a pointer to it.
+ dup CType :funcsig? not if CType type dup CType :funcsig? _assert then
+ CType type
else
vmop loc VM_CONSTANT = if vmop arg wordfunctype else TYPE_VOID then then
( node type ) vmop :push rot ( type 'copy node )