--- gforth/Attic/prims2y.fs 2003/10/02 18:58:47 1.5 +++ gforth/Attic/prims2y.fs 2003/10/08 13:17:10 1.7 @@ -290,6 +290,8 @@ variable in-part \ true if processing a in-part off 0 value state-in \ state on entering prim 0 value state-out \ state on exiting prim +0 value state-in-default \ state on entering prim +0 value state-out-default \ state on exiting prim : prim-context ( ... p xt -- ... ) \ execute xt with prim set to p @@ -1307,6 +1309,82 @@ variable tail-nextp2 \ xt to execute for compute-max-back-depths output-combined perform ; +\ reprocessing (typically to generate versions for another cache states) +\ !! use prim-context + +variable reprocessed-num 0 reprocessed-num ! + +: new-name ( -- c-addr u ) + reprocessed-num @ 0 + 1 reprocessed-num +! + <# #s 'p hold '_ hold #> save-mem ; + +: reprocess-simple ( prim -- ) + to prim + new-name prim prim-c-name 2! + output @ execute ; + +: lookup-prim ( c-addr u -- prim ) + primitives search-wordlist 0= -13 and throw execute ; + +: state-prim1 { in-state out-state prim -- } + in-state out-state state-in-default state-out-default d= ?EXIT + in-state to state-in + out-state to state-out + prim reprocess-simple ; + +: state-prim ( in-state out-state "name" -- ) + parse-word lookup-prim state-prim1 ; + +\ reprocessing with default states + +\ This is a simple scheme and should be generalized +\ assumes we only cache one stack and use simple states for that + +0 value cache-stack \ stack that we cache +2variable cache-states \ states of the cache, starting with the empty state + +: compute-default-state-out ( n-in -- n-out ) + \ for the current prim + cache-stack stack-in @ - 0 max + cache-stack stack-out @ + cache-states 2@ nip 1- min ; + +: gen-prim-states ( prim -- ) + to prim + cache-states 2@ swap { states } ( nstates ) + cache-stack stack-in @ +do + states i th @ + states i compute-default-state-out th @ + prim state-prim1 + loop ; + +: prim-states ( "name" -- ) + parse-word lookup-prim gen-prim-states ; + +: gen-branch-states ( out-state prim -- ) + \ generate versions that produce out-state; useful for branches + to prim { out-state } + cache-states 2@ swap { states } ( nstates ) + cache-stack stack-in @ +do + states i th @ out-state prim state-prim1 + loop ; + +: branch-states ( out-state "name" -- ) + parse-word lookup-prim gen-branch-states ; + +\ producing state transitions + +: gen-transitions ( "name" -- ) + parse-word lookup-prim { prim } + cache-states 2@ { states nstates } + nstates 0 +do + nstates 0 +do + i j <> if + states i th @ states j th @ prim state-prim1 + endif + loop + loop ; + \ C output : print-item { n stack -- }