duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 76d42a4ac80a7ae522510bf4063e72308b794071
parent b1a125d4573ed6d1f76c38ea46ad229f85d01fe5
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Mon, 27 Jun 2022 12:57:07 -0400

cc: remove variable map

Keep all mapping info in AST_DECLARE itself and make variable finding iterate
the AST directly.

It *does* make sense to have some kind of map for this kind of info, but the
weight in terms of structures and code is too big to be worth it. Let's just
iterate dumbly.

The same treatment is coming for the function map.

Diffstat:
Mfs/cc/ast.fs | 20++++++++++++++++++++
Mfs/cc/gen.fs | 4++--
Mfs/cc/map.fs | 38+++++++++++++-------------------------
3 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs @@ -73,6 +73,26 @@ ASTIDCNT stringlist astidnames : idname ( id -- str ) astidnames slistiter ; +\ Number of bytes required to hold this variable declaration in memory. +: ast.decl.totsize ( dnode -- size-in-bytes ) + dup ast.decl.type ( dnode type ) + typesize swap ast.decl.nbelem ( nbelem ) + 1 max * ; + +: ast.decl.isarg? ( dnode -- f ) parentnode nodeid AST_ARGSPECS = ; + +: _ ( name args-or-stmts -- dnode-or-0 ) + firstchild begin ( name node ) + ?dup while ( name node ) + dup nodeid AST_DECLARE = if + over over ast.decl.name s= if ( name node ) + nip exit then then + nextsibling repeat ( name ) drop 0 ; + +: ast.func.finddecl ( name fnode -- dnode-or-0 ) + firstchild 2dup _ ?dup if + nip nip else nextsibling _ then ; + : _[ '[' emit ; : _] ']' emit ; diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs @@ -54,9 +54,9 @@ alias noop gennode ( node -- ) \ forward declaration firstchild ?dup if begin dup gennode nextsibling ?dup not until then ; : spit ( a u -- ) A>r >r >A begin Ac@+ .x1 next r>A ; -: getfuncmap ( node -- funcentry ) AST_FUNCTION parentnodeid ast.func.fmap ; : lv>decl ( lvnode -- dnode ) - dup ast.lvalue.name swap getfuncmap ( name funcentry ) findvarinmap ; + dup ast.lvalue.name swap AST_FUNCTION parentnodeid ( fnode ) + ast.func.finddecl dup _assert ; \ Multiply the value of "node" by a factor of "n" \ TODO: support lvalues and expressions diff --git a/fs/cc/map.fs b/fs/cc/map.fs @@ -14,58 +14,46 @@ \ 4b address \ 4b variable declaration xdict -\ Then, each entry in the variable declaration xdict is: -\ 4b SF offset -\ 4b link to AST_DECLARE node -newxdict curmap +\ 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 -16 'ufield fmap.vmap : _err ( -- ) abort" mapping error" ; -\ print curmap in reverse order of parsing +\ print funcmap in reverse order of parsing : printmap ( -- ) - curmap @ ?dup not if exit then begin ( w ) + funcmap @ ?dup not if exit then begin ( w ) dup wordname[] rtype spc> dup fmap.sfsize .x spc> dup fmap.argsize .x nl> - dup fmap.vmap @ ?dup if begin ( w vmap ) - spc> spc> dup @ printast nl> - prevword ?dup not until then ( w ) prevword ?dup not until ; : Function ( astnode -- entry ) - dup ast.func.name ( name ) curmap xentry ( astnode ) + dup ast.func.name ( name ) funcmap xentry ( astnode ) here swap , 16 allot0 ( entry ) ; : Variable ( dnode -- ) - curmap @ fmap.sfsize over to ast.decl.sfoff ( dnode ) - dup ast.decl.name curmap @ fmap.vmap xentry ( dnode ) - dup , ( dnode ) - dup ast.decl.type ( dnode type ) - typesize swap ast.decl.nbelem ( nbelem ) - 1 max * ( sfsize ) - curmap @ to+ fmap.sfsize ; + funcmap @ fmap.sfsize over to ast.decl.sfoff ( dnode ) + ast.decl.totsize ( sfsize ) funcmap @ to+ fmap.sfsize ; -: findvarinmap ( name funcentry -- dnode ) - fmap.vmap xfind not if _err then @ ; - -: findfuncinmap ( name -- funcentry ) curmap xfind not if _err then ; +: 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 parentnode nodeid AST_ARGSPECS = if \ inc argssize field - 4 curmap @ to+ fmap.argsize then + 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 curmap ! + 0 funcmap ! firstchild ?dup not if exit then begin ( astnode ) dup nodeid AST_FUNCTION = if dup mapfunction then nextsibling ?dup not until ;