factor/library/inference/dataflow.factor

88 lines
2.3 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-15 21:17:56 -04:00
USING: interpreter kernel lists namespaces sequences vectors
words ;
! Recursive state. An alist, mapping words to labels.
SYMBOL: recursive-state
! We build a dataflow graph for the compiler.
SYMBOL: dataflow-graph
! Label nodes have the node-label variable set.
2004-12-03 22:12:58 -05:00
SYMBOL: #label
SYMBOL: #call ( non-tail call )
2004-12-03 22:12:58 -05:00
SYMBOL: #call-label
SYMBOL: #push ( literal )
2005-05-15 21:17:56 -04:00
SYMBOL: #drop
2004-12-04 23:45:41 -05:00
! This is purely a marker for values we retain after a
! conditional. It does not generate code, but merely alerts the
! dataflow optimizer to the fact these values must be retained.
SYMBOL: #values
2004-12-02 22:44:36 -05:00
SYMBOL: #return
SYMBOL: node-consume-d
SYMBOL: node-produce-d
SYMBOL: node-consume-r
SYMBOL: node-produce-r
SYMBOL: node-op
2004-12-03 22:12:58 -05:00
SYMBOL: node-label
! #push nodes have this field set to the value being pushed.
! #call nodes have this as the word being called
SYMBOL: node-param
: <dataflow-node> ( param op -- node )
<namespace> [
node-op set
node-param set
2004-12-02 22:44:36 -05:00
[ ] node-consume-d set
[ ] node-produce-d set
[ ] node-consume-r set
[ ] node-produce-r set
] extend ;
: node-inputs ( d-count r-count -- )
#! Execute in the node's namespace.
meta-r get vector-tail* node-consume-r set
meta-d get vector-tail* node-consume-d set ;
2004-11-27 23:09:32 -05:00
2004-12-23 01:14:07 -05:00
: dataflow-inputs ( in node -- )
2004-12-24 17:29:16 -05:00
[ length 0 node-inputs ] bind ;
: node-outputs ( d-count r-count -- )
#! Execute in the node's namespace.
meta-r get vector-tail* node-produce-r set
meta-d get vector-tail* node-produce-d set ;
2004-12-23 01:14:07 -05:00
: dataflow-outputs ( out node -- )
2004-12-24 17:29:16 -05:00
[ length 0 node-outputs ] bind ;
: get-dataflow ( -- IR )
dataflow-graph get reverse ;
: dataflow, ( param op -- node )
#! Add a node to the dataflow IR.
2005-04-16 00:23:27 -04:00
<dataflow-node> dup dataflow-graph [ cons ] change ;
2004-11-28 21:56:58 -05:00
2005-05-15 21:17:56 -04:00
: dataflow-drop, ( n -- )
f #drop dataflow, [ 0 node-inputs ] bind ;
: dataflow-push, ( n -- )
f #push dataflow, [ 0 node-outputs ] bind ;
2004-12-02 22:44:36 -05:00
: apply-dataflow ( dataflow name default -- )
#! For the dataflow node, look up named word property,
#! if its not defined, apply default quotation to
2004-12-03 22:12:58 -05:00
#! ( node ) otherwise apply property quotation to
#! ( node ).
>r >r dup [ node-op get ] bind r> word-prop dup [
2004-12-03 22:12:58 -05:00
call r> drop
2004-12-02 22:44:36 -05:00
] [
drop r> call
] ifte ;