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
|
2004-11-27 00:33:17 -05:00
|
|
|
|
|
|
|
! We build a dataflow graph for the compiler.
|
|
|
|
SYMBOL: dataflow-graph
|
|
|
|
|
2004-12-10 18:23:50 -05:00
|
|
|
! Label nodes have the node-label variable set.
|
2004-12-03 22:12:58 -05:00
|
|
|
SYMBOL: #label
|
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
SYMBOL: #call ( non-tail call )
|
2004-12-03 22:12:58 -05:00
|
|
|
SYMBOL: #call-label
|
2004-12-01 19:48:08 -05:00
|
|
|
SYMBOL: #push ( literal )
|
2005-05-15 21:17:56 -04:00
|
|
|
SYMBOL: #drop
|
2004-11-27 00:33:17 -05:00
|
|
|
|
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
|
|
|
|
|
2004-11-28 19:07:24 -05:00
|
|
|
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
|
2004-11-28 19:07:24 -05:00
|
|
|
|
2004-12-01 19:48:08 -05:00
|
|
|
! #push nodes have this field set to the value being pushed.
|
|
|
|
! #call nodes have this as the word being called
|
2004-11-28 19:07:24 -05:00
|
|
|
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
|
2004-11-28 19:07:24 -05:00
|
|
|
] extend ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2004-11-28 19:07:24 -05:00
|
|
|
: 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 ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2004-11-28 19:07:24 -05:00
|
|
|
: 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 ;
|
2004-11-28 19:07:24 -05:00
|
|
|
|
|
|
|
: get-dataflow ( -- IR )
|
|
|
|
dataflow-graph get reverse ;
|
2004-11-27 22:26:05 -05:00
|
|
|
|
2004-11-28 19:07:24 -05:00
|
|
|
: 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 ).
|
2005-03-05 14:45:23 -05:00
|
|
|
>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 ;
|