duskos

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

commit 31a0c40b81418fc9f4381238c1dce30d010ca9e7
parent 3bf9b0863d0c8f1519e9c17b1601b412371818be
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Thu,  9 Jun 2022 16:38:00 -0400

cc: add function calls!

Diffstat:
Mfs/cc/ast.fs | 17++++++++++++++---
Mfs/cc/gen.fs | 10+++++++++-
Mfs/cc/map.fs | 10++++++++--
Mfs/cc/tree.fs | 1+
Mfs/tests/cc.fs | 1+
Mfs/tests/test.c | 3+++
6 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs @@ -39,7 +39,7 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c, : boptoken ( opid -- tok ) BOPTlist slistiter ; \ AST node types -14 value ASTIDCNT +15 value ASTIDCNT 0 value AST_DECLARE \ data1=name 1 value AST_UNIT 2 value AST_FUNCTION \ data1=name data2=MAP_FUNCTION @@ -54,12 +54,13 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c, 11 value AST_ASSIGN \ data1=name 12 value AST_DECLLIST 13 value AST_VARIABLE \ data1=name +14 value AST_FUNCALL \ data1=name data2=MAP_FUNCTION create astidnames 7 c, ," declare" 4 c, ," unit" 8 c, ," function" 6 c, ," return" 8 c, ," constant" 5 c, ," stmts" 4 c, ," args" 4 c, ," expr" 7 c, ," unaryop" 6 c, ," factor" 5 c, ," binop" 6 c, ," assign" - 8 c, ," decllist" 3 c, ," var" + 8 c, ," decllist" 3 c, ," var" 4 c, ," call" 0 c, 0 value curunit \ points to current Unit, the beginning of the AST @@ -99,6 +100,7 @@ ASTIDCNT wordtbl astdatatbl ( node -- node ) 'w _s ( Assign ) 'w noop ( DeclarationList ) 'w _s ( Variable ) +'w _s ( FunCall ) : printast ( node -- ) ?dup not if ." null" exit then @@ -129,6 +131,7 @@ ASTIDCNT wordtbl astdatatbl ( node -- node ) : Assign ( name -- ) 1 AST_ASSIGN newnode , ; : DeclarationList ( -- ) -1 AST_DECLLIST newnode ; : Variable ( name -- ) 0 AST_VARIABLE newnode , ; +: FunCall ( name -- ) 0 AST_FUNCALL newnode , ; : _err ( tok -- ) stype spc> @@ -208,12 +211,20 @@ ASTIDCNT wordtbl astparsetbl skipnext Factor ; :w ( UnaryOp ) skipnext Factor ; :w ( Factor ) - dup isIdent? if Variable else expectConst Constant then seqclose ; + dup isIdent? if \ Variable or FunCall + _nextt ( prevtok newtok ) dup S" (" s= if \ FunCall + drop FunCall _nextt ')' expectChar + else \ Variable + skipnext swap Variable then + else \ Constant + expectConst Constant then + seqclose ; :w ( BinaryOp ) skipnext Factor ; 'w _err ( Assign ) :w ( DeclarationList ) expectIdent Declare ; 'w _err ( Variable ) +'w _err ( FunCall ) : parseast ( -- ) Unit begin _skip if 0 to _skip else nextt ?dup not if exit then then diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs @@ -69,7 +69,9 @@ ASTIDCNT wordtbl gentbl ( node -- ) :w ( Function ) _debug if ." debugging: " dup data1 stype nl> then dup data1 entry - dup data2 ( MAP_FUNCTION ) data2 ?dup if + dup data2 ( astfunc mapfunc ) + here over data3! \ set address + data2 ?dup if \ has SF ebp i32 sub, then genchildren _debug if current here current - spit nl> then ; @@ -101,6 +103,12 @@ ASTIDCNT wordtbl gentbl ( node -- ) dup data1 ( node name ) swap getfuncnode findvarinmap ( varnode ) data2 ( offset ) eax [ebp]+ mov, ; +:w ( FunCall ) + data1 ( name ) + curmap findfuncinmap ?dup not if _err then ( mapfunc ) + data3 ( addr ) call, + eax [ebp] mov, + ebp 4 i32 add, ; : _ ( node -- ) gentbl over astid wexec ; current to gennode diff --git a/fs/cc/map.fs b/fs/cc/map.fs @@ -6,7 +6,7 @@ 3 value MAPIDCNT 0 value MAP_UNIT -1 value MAP_FUNCTION \ data1=AST_FUNCTION data2=address +1 value MAP_FUNCTION \ data1=AST_FUNCTION data2=SF size data3=address 2 value MAP_VARIABLE \ data1=name data2=SF offset create mapidnames 4 c, ," unit" 8 c, ," function" 3 c, ," var" @@ -21,7 +21,7 @@ create mapidnames 4 c, ," unit" 8 c, ," function" 3 c, ," var" dup nodeclosed? not if to activenode else drop then ; : Unit ( -- ) -1 MAP_UNIT createnode dup to curmap to activenode ; -: Function ( name -- ) -1 MAP_FUNCTION newnode , 0 , ; +: Function ( name -- ) -1 MAP_FUNCTION newnode , 0 , 0 , ; : Variable ( offset name -- ) 0 MAP_VARIABLE newnode , , ; : _[ '[' emit ; @@ -52,6 +52,12 @@ MAPIDCNT wordtbl mapdatatbl ( node -- node ) 2dup data1 s= if nip exit then nextsibling dup not until then ( name node ) nip ; +: findfuncinmap ( name node -- funcnode ) + dup nodeid MAP_UNIT = not if _err then + firstchild dup if begin ( name node ) + 2dup data1 data1 s= if nip exit then nextsibling dup not + until then ( name node ) nip ; + : mapfunction ( astfunction -- ) dup Function activenode over data2! dup begin ( astfunc astfunc ) AST_DECLARE nextnodeid dup if diff --git a/fs/cc/tree.fs b/fs/cc/tree.fs @@ -17,6 +17,7 @@ \ children is added, the slot is decreased. When 0 is reached, we close it. : nodeid ( node -- id ) c@ ; +: nodeid! ( id node -- ) c! ; : cslots ( node -- slots ) 1+ 1+ c@ ; : cslots! ( slots node -- ) 1+ 1+ c! ; : cslots- ( node -- ) dup cslots 1- swap cslots! ; diff --git a/fs/tests/cc.fs b/fs/tests/cc.fs @@ -10,4 +10,5 @@ bwnot $ffffffd5 #eq exprbinops 7 #eq boolops 0 #eq variables 42 #eq +funcall 42 #eq testend diff --git a/fs/tests/test.c b/fs/tests/test.c @@ -16,3 +16,6 @@ int variables() { int bar = 2; return foo + bar; } +int funcall() { + return retconst(); +}