factor/library/inference/dataflow.factor

154 lines
4.0 KiB
Factor
Raw Normal View History

2005-05-15 21:17:56 -04:00
! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-12-02 22:44:36 -05:00
IN: inference
2005-05-17 16:13:08 -04:00
USING: generic interpreter kernel lists namespaces parser
sequences vectors words ;
2005-05-15 21:17:56 -04:00
2005-05-17 16:13:08 -04:00
! The dataflow IR is the first of the two intermediate
! representations used by Factor. It annotates concatenative
! code with stack flow information and types.
2005-07-27 01:46:06 -04:00
TUPLE: node param in-d out-d in-r out-r
2005-07-28 18:20:31 -04:00
classes literals successor children ;
2005-05-17 16:13:08 -04:00
M: node = eq? ;
2005-05-17 16:13:08 -04:00
: make-node ( param in-d out-d in-r out-r node -- node )
2005-07-28 18:20:31 -04:00
[ >r f f f f <node> r> set-delegate ] keep ;
: param-node ( label) f f f f ;
: in-d-node ( inputs) >r f r> f f f ;
: out-d-node ( outputs) >r f f r> f f ;
2005-05-17 16:13:08 -04:00
: d-tail ( n -- list ) meta-d get tail* >vector ;
: r-tail ( n -- list ) meta-r get tail* >vector ;
2005-05-17 16:13:08 -04:00
TUPLE: #label ;
C: #label make-node ;
2005-05-17 16:13:08 -04:00
: #label ( label -- node ) param-node <#label> ;
TUPLE: #simple-label ;
C: #simple-label make-node ;
: #simple-label ( label -- node ) param-node <#simple-label> ;
TUPLE: #entry ;
C: #entry make-node ;
: #entry ( -- node ) meta-d get clone in-d-node <#entry> ;
TUPLE: #call ;
C: #call make-node ;
2005-05-17 16:13:08 -04:00
: #call ( word -- node ) param-node <#call> ;
TUPLE: #call-label ;
C: #call-label make-node ;
2005-06-08 04:49:05 -04:00
: #call-label ( label -- node ) param-node <#call-label> ;
2005-05-17 16:13:08 -04:00
TUPLE: #push ;
C: #push make-node ;
2005-05-17 16:13:08 -04:00
: #push ( outputs -- node ) d-tail out-d-node <#push> ;
TUPLE: #drop ;
C: #drop make-node ;
2005-05-17 16:13:08 -04:00
: #drop ( inputs -- node ) d-tail in-d-node <#drop> ;
TUPLE: #values ;
C: #values make-node ;
: #values ( -- node ) meta-d get in-d-node <#values> ;
TUPLE: #return ;
C: #return make-node ;
: #return ( -- node ) meta-d get in-d-node <#return> ;
2005-05-17 16:13:08 -04:00
TUPLE: #ifte ;
C: #ifte make-node ;
2005-05-17 16:13:08 -04:00
: #ifte ( in -- node ) 1 d-tail in-d-node <#ifte> ;
TUPLE: #dispatch ;
C: #dispatch make-node ;
2005-05-17 16:13:08 -04:00
: #dispatch ( in -- node ) 1 d-tail in-d-node <#dispatch> ;
2005-07-27 01:46:06 -04:00
TUPLE: #merge ;
C: #merge make-node ;
: #merge ( values -- node ) in-d-node <#merge> ;
2005-05-17 16:13:08 -04:00
: node-inputs ( d-count r-count node -- )
tuck
>r r-tail r> set-node-in-r
>r d-tail r> set-node-in-d ;
: node-outputs ( d-count r-count node -- )
tuck
>r r-tail r> set-node-out-r
>r d-tail r> set-node-out-d ;
! Variable holding dataflow graph being built.
SYMBOL: dataflow-graph
2005-05-17 16:13:08 -04:00
! The most recently added node.
SYMBOL: current-node
2005-05-17 16:13:08 -04:00
: node, ( node -- )
dataflow-graph get [
dup current-node [ set-node-successor ] change
2004-12-02 22:44:36 -05:00
] [
2005-05-17 16:13:08 -04:00
! first node
dup dataflow-graph set current-node set
2004-12-02 22:44:36 -05:00
] ifte ;
2005-05-17 16:13:08 -04:00
: nest-node ( -- dataflow current )
dataflow-graph get dataflow-graph off
current-node get current-node off ;
: unnest-node ( new-node dataflow current -- new-node )
>r >r dataflow-graph get unit over set-node-children
r> dataflow-graph set
r> current-node set ;
: with-nesting ( quot -- new-node | quot: -- new-node )
nest-node 2slip unnest-node ; inline
: copy-effect ( from to -- )
over node-in-d over set-node-in-d
over node-in-r over set-node-in-r
over node-out-d over set-node-out-d
swap node-out-r swap set-node-out-r ;
: node-effect ( node -- [[ d-in meta-d ]] )
dup node-in-d swap node-out-d cons ;
: node-values ( node -- values )
[
dup node-in-d % dup node-out-d %
dup node-in-r % node-out-r %
] make-vector ;
2005-05-22 02:35:38 -04:00
: uses-value? ( value node -- ? ) node-values memq? ;
2005-05-22 02:35:38 -04:00
: last-node ( node -- last )
dup node-successor [ last-node ] [ ] ?ifte ;
: penultimate-node ( node -- penultimate )
dup node-successor dup [
dup node-successor
[ nip penultimate-node ] [ drop ] ifte
] [
2drop f
] ifte ;
: drop-inputs ( node -- #drop )
node-in-d in-d-node <#drop> ;
2005-05-17 16:13:08 -04:00
! Recursive state. An alist, mapping words to labels.
SYMBOL: recursive-state
: each-node ( node quot -- )
over [
[ call ] 2keep swap
[ node-children [ swap each-node ] each-with ] 2keep
node-successor swap each-node
] [
2drop
] ifte ; inline
: each-node-with ( obj node quot -- | quot: obj node -- )
swap [ with ] each-node 2drop ; inline