commit 159d6feaa5d52600a805af44f33eaa36878743f9
parent 76d42a4ac80a7ae522510bf4063e72308b794071
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 27 Jun 2022 14:06:34 -0400
cc: remove cc/map
In the grand scheme of things, these structures are noise.
Diffstat:
5 files changed, 31 insertions(+), 78 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -56,15 +56,15 @@ NODESZ 4 + ufield ast.decl.type
NODESZ 8 + ufield ast.decl.nbelem
NODESZ 12 + ufield ast.decl.sfoff
NODESZ ufield ast.func.name
-NODESZ 4 + ufield ast.func.fmap
+NODESZ 4 + ufield ast.func.sfsize
NODESZ 8 + ufield ast.func.type
+NODESZ 12 + ufield ast.func.address
NODESZ ufield ast.const.value
NODESZ ufield ast.lvalue.name
NODESZ ufield ast.uop.opid
NODESZ ufield ast.pop.opid
NODESZ ufield ast.bop.opid
NODESZ ufield ast.funcall.funcname
-NODESZ 4 + ufield ast.funcall.fmap
ASTIDCNT stringlist astidnames
"declare" "unit" "function" "return" "constant" "stmts" "args" "lvalue"
"unaryop" "postop" "binop" "list" "if" "_" "call"
@@ -73,6 +73,14 @@ ASTIDCNT stringlist astidnames
: idname ( id -- str ) astidnames slistiter ;
+: ast.unit.findfunc ( name -- fnode-or-0 )
+ curunit firstchild begin ( name node )
+ ?dup while ( name node )
+ dup nodeid AST_FUNCTION = if
+ over over ast.func.name s= if ( name node )
+ nip exit then then
+ nextsibling repeat ( name ) drop 0 ;
+
\ Number of bytes required to hold this variable declaration in memory.
: ast.decl.totsize ( dnode -- size-in-bytes )
dup ast.decl.type ( dnode type )
@@ -90,8 +98,15 @@ ASTIDCNT stringlist astidnames
nextsibling repeat ( name ) drop 0 ;
: ast.func.finddecl ( name fnode -- dnode-or-0 )
- firstchild 2dup _ ?dup if
- nip nip else nextsibling _ then ;
+ firstchild ( args ) 2dup _ ?dup if
+ nip nip else nextsibling ( stmts ) _ then ;
+
+: ast.func.argsize ( fnode -- size-in-bytes )
+ firstchild ( args ) firstchild ( node ) 0 begin ( node r )
+ over while ( node r )
+ over nodeid AST_DECLARE = _assert
+ over ast.decl.totsize +
+ swap nextsibling swap repeat ( node r ) nip ;
: _[ '[' emit ;
: _] ']' emit ;
@@ -252,11 +267,14 @@ current to parseExpression
drop nextt parse _assert nextt ']' expectChar ( nbelem ) else
to nexttputback 0 ( nbelem ) then ;
+\ Parse a variable declaration from within a function
: parseDeclare ( type parentnode -- dnode )
AST_DECLARE newnode ( type dnode )
swap parseType* ( pnode type tok )
expectIdent ( dnode type name ) , , ( dnode )
- nextt parseNbelem , 0 ( sfoff ) , ;
+ nextt parseNbelem , ( dnode )
+ dup AST_FUNCTION parentnodeid ?dup _assert ( dnode fnode )
+ dup ast.func.sfsize , over ast.decl.totsize swap to+ ast.func.sfsize ;
: parseDeclareInit ( dnode tok -- node )
dup S" =" s= not if ';' expectChar drop exit then
@@ -265,9 +283,6 @@ current to parseExpression
parseExpression then
read; ( dnode expr-or-list ) swap addnode ;
-: parseDeclarationList ( type stmtsnode -- )
- parseDeclare nextt parseDeclareInit ;
-
: parseArgSpecs ( funcnode -- )
\ First '(' is already parsed
AST_ARGSPECS newnode nextt ( argsnode tok )
@@ -302,7 +317,7 @@ alias noop parseStatements ( funcnode -- ) \ forward declaration
dup S" }" s= not while
dup statementnames sfind dup 0< if ( snode tok -1 )
drop dup parseType if ( snode tok type )
- nip over parseDeclarationList else ( snode tok )
+ nip over parseDeclare nextt parseDeclareInit else ( snode tok )
parseExpression over addnode read; then ( snode )
else ( snode tok idx ) nip statementhandler swap wexec then ( snode )
nextt repeat ( snode tok ) 2drop ;
@@ -313,7 +328,7 @@ current to parseStatements
parseType _assert ( unode type ) parseType* ( unode type tok )
expectIdent ( unode type name ) rot nextt case ( type name unode )
S" (" of s=
- AST_FUNCTION newnode ( type name fnode ) rot> , 0 , ,
+ AST_FUNCTION newnode ( type name fnode ) rot> , 0 , , 0 ( address ) ,
dup parseArgSpecs parseStatements
endof
AST_DECLARE newnode ( type name dnode ) rot> , , ( dnode )
diff --git a/fs/cc/cc.fs b/fs/cc/cc.fs
@@ -9,6 +9,5 @@
?f<< cc/type.fs
?f<< cc/tree.fs
?f<< cc/ast.fs
-?f<< cc/map.fs
?f<< cc/gen.fs
?f<< cc/cc1.fs
diff --git a/fs/cc/cc1.fs b/fs/cc/cc1.fs
@@ -4,6 +4,5 @@
\ Compiles input coming from the cc< alias (defaulting to in<) and writes the
\ result to here. Aborts on error.
: cc1, ( -- )
- xhere$ xhere[ parseast curunit _debug if dup printast nl> then
- dup mapunit _debug if printmap then ]xhere
+ xhere$ xhere[ parseast curunit _debug if dup printast nl> then ]xhere
gennode ;
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -148,10 +148,9 @@ ASTIDCNT wordtbl gentbl ( node -- )
:w ( Function )
_debug if ." debugging: " dup ast.func.name stype nl> then
ops$
- dup ast.func.name entry
- dup ast.func.fmap ( astfunc mapfunc )
- here over to fmap.address \ set address
- dup fmap.argsize swap fmap.sfsize over - ( argsz locsz ) vmprelude,
+ dup ast.func.name entry ( fnode )
+ here over to ast.func.address
+ over ast.func.argsize over ast.func.sfsize over - ( argsz locsz ) vmprelude,
genchildren
_debug if current here current - spit nl> then ;
:w ( Return )
@@ -207,8 +206,8 @@ ASTIDCNT wordtbl gentbl ( node -- )
dup selop1 gennode swap dup selop2 sf+>op op1<>op2 vmmov, ops$
4 - swap nextsibling ?dup not until drop then
\ find and call
- ( node ) ast.funcall.funcname ( name ) findfuncinmap ( mapfunc )
- fmap.address vmcall>op1, ;
+ ( node ) ast.funcall.funcname ( name ) ast.unit.findfunc dup _assert
+ ast.func.address vmcall>op1, ;
: _ ( node -- ) gentbl over nodeid wexec ;
current to gennode
diff --git a/fs/cc/map.fs b/fs/cc/map.fs
@@ -1,59 +0,0 @@
-\ C compiler local variable map
-\ these maps are generated after AST parsing, before code generation to map
-\ some names to some numbers, namely:
-\ 1. Functions address
-\ 2. Local variable stack frame (SF) offsets
-
-\ Those maps are xdicts. The first level is a dictionary of functions. It is
-\ located at "curmap". Each function in the AST results in an entry in this
-\ map. Each entry has this structure:
-
-\ 4b link to AST_FUNCTION node
-\ 4b SF size, which *includes* args size. SF-args=local vars
-\ 4b args size
-\ 4b address
-\ 4b variable declaration xdict
-
-\ Then, each entry in the variable declaration xdict contains a 4b pointer to
-\ an AST_DECLARE node.
-newxdict funcmap
-
-\ Funcmap. Unbounded fields because the full struct generates more noise in the
-\ code.
-0 ufield fmap.astfunc
-4 ufield fmap.sfsize
-8 ufield fmap.argsize
-12 ufield fmap.address
-
-: _err ( -- ) abort" mapping error" ;
-\ print funcmap in reverse order of parsing
-: printmap ( -- )
- funcmap @ ?dup not if exit then begin ( w )
- dup wordname[] rtype spc>
- dup fmap.sfsize .x spc> dup fmap.argsize .x nl>
- prevword ?dup not until ;
-
-: Function ( astnode -- entry )
- dup ast.func.name ( name ) funcmap xentry ( astnode )
- here swap , 16 allot0 ( entry ) ;
-
-: Variable ( dnode -- )
- funcmap @ fmap.sfsize over to ast.decl.sfoff ( dnode )
- ast.decl.totsize ( sfsize ) funcmap @ to+ fmap.sfsize ;
-
-: findfuncinmap ( name -- funcentry ) funcmap xfind not if _err then ;
-
-: mapfunction ( astfunction -- )
- dup Function ( astfunc fmap ) over to ast.funcall.fmap ( astfunc ) begin ( curnode )
- AST_DECLARE nextnodeid dup if ( astdecl )
- dup ast.decl.isarg? if \ inc argssize field
- 4 funcmap @ to+ fmap.argsize then
- dup Variable 0 else 1 then
- until ( curnode ) drop ;
-
-\ create a new map from "astunit"
-: mapunit ( astunit -- )
- 0 funcmap !
- firstchild ?dup not if exit then begin ( astnode )
- dup nodeid AST_FUNCTION = if dup mapfunction then
- nextsibling ?dup not until ;