2006-03-06 19:19:20 -05:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2004-12-02 22:44:36 -05:00
|
|
|
IN: inference
|
2006-05-15 01:01:47 -04:00
|
|
|
USING: arrays generic hashtables interpreter kernel math
|
2005-09-16 20:49:24 -04:00
|
|
|
namespaces parser sequences 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-09-04 19:24:24 -04:00
|
|
|
TUPLE: node param shuffle
|
2005-08-11 19:08:22 -04:00
|
|
|
classes literals history
|
|
|
|
successor children ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-07-28 15:17:31 -04:00
|
|
|
M: node = eq? ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-07-28 15:17:31 -04:00
|
|
|
: make-node ( param in-d out-d in-r out-r node -- node )
|
2005-09-23 01:22:04 -04:00
|
|
|
[ >r swapd <shuffle> f f f f f <node> r> set-delegate ] keep ;
|
2005-07-28 15:17:31 -04:00
|
|
|
|
2005-09-04 19:24:24 -04:00
|
|
|
: node-in-d node-shuffle shuffle-in-d ;
|
|
|
|
: node-in-r node-shuffle shuffle-in-r ;
|
|
|
|
: node-out-d node-shuffle shuffle-out-d ;
|
|
|
|
: node-out-r node-shuffle shuffle-out-r ;
|
|
|
|
|
|
|
|
: set-node-in-d node-shuffle set-shuffle-in-d ;
|
|
|
|
: set-node-in-r node-shuffle set-shuffle-in-r ;
|
|
|
|
: set-node-out-d node-shuffle set-shuffle-out-d ;
|
|
|
|
: set-node-out-r node-shuffle set-shuffle-out-r ;
|
|
|
|
|
2005-10-29 23:25:38 -04:00
|
|
|
: empty-node f { } { } { } { } ;
|
|
|
|
: param-node ( label) { } { } { } { } ;
|
|
|
|
: in-node ( inputs) >r f r> { } { } { } ;
|
|
|
|
: out-node ( outputs) >r f { } r> { } { } ;
|
2006-05-10 02:18:25 -04:00
|
|
|
: meta-d-node meta-d get clone in-node ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-09-04 19:24:24 -04:00
|
|
|
: d-tail ( n -- list ) meta-d get tail* ;
|
2006-05-15 01:49:07 -04:00
|
|
|
: r-tail ( n -- list ) meta-r get tail* ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-09-04 01:09:46 -04:00
|
|
|
: node-child node-children first ;
|
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
TUPLE: #label ;
|
|
|
|
C: #label make-node ;
|
2005-05-17 16:13:08 -04:00
|
|
|
: #label ( label -- node ) param-node <#label> ;
|
|
|
|
|
2005-08-04 17:39:39 -04:00
|
|
|
TUPLE: #entry ;
|
|
|
|
C: #entry make-node ;
|
2006-05-10 02:18:25 -04:00
|
|
|
|
|
|
|
: #entry ( -- node ) meta-d-node <#entry> ;
|
2005-08-04 17:39:39 -04:00
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
TUPLE: #call ;
|
|
|
|
C: #call make-node ;
|
2005-05-17 16:13:08 -04:00
|
|
|
: #call ( word -- node ) param-node <#call> ;
|
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
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
|
|
|
|
2006-04-17 17:17:34 -04:00
|
|
|
TUPLE: #push ;
|
|
|
|
C: #push make-node ;
|
2006-05-10 03:40:03 -04:00
|
|
|
: #push ( -- node ) peek-d 1array out-node <#push> ;
|
2006-04-17 17:17:34 -04:00
|
|
|
: >#push< ( node -- seq ) node-out-d [ value-literal ] map ;
|
|
|
|
|
2005-09-04 17:07:59 -04:00
|
|
|
TUPLE: #shuffle ;
|
|
|
|
C: #shuffle make-node ;
|
|
|
|
: #shuffle ( -- node ) empty-node <#shuffle> ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
TUPLE: #values ;
|
|
|
|
C: #values make-node ;
|
2006-05-10 02:18:25 -04:00
|
|
|
: #values ( -- node ) meta-d-node <#values> ;
|
2004-11-27 00:33:17 -05:00
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
TUPLE: #return ;
|
|
|
|
C: #return make-node ;
|
2005-09-07 17:21:11 -04:00
|
|
|
: #return ( label -- node )
|
|
|
|
#! The parameter is the label we are returning from, or if
|
|
|
|
#! f, this is a top-level return.
|
2006-05-10 02:18:25 -04:00
|
|
|
meta-d-node <#return> [ set-node-param ] keep ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-09-24 15:21:17 -04:00
|
|
|
TUPLE: #if ;
|
|
|
|
C: #if make-node ;
|
2006-05-10 03:40:03 -04:00
|
|
|
: #if ( in -- node ) peek-d 1array in-node <#if> ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-06-12 03:38:57 -04:00
|
|
|
TUPLE: #dispatch ;
|
|
|
|
C: #dispatch make-node ;
|
2006-05-10 03:40:03 -04:00
|
|
|
: #dispatch ( in -- node ) peek-d 1array in-node <#dispatch> ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-07-27 01:46:06 -04:00
|
|
|
TUPLE: #merge ;
|
|
|
|
C: #merge make-node ;
|
2005-09-17 22:25:18 -04:00
|
|
|
: #merge ( -- node ) meta-d get clone out-node <#merge> ;
|
|
|
|
|
|
|
|
TUPLE: #terminate ;
|
|
|
|
C: #terminate make-node ;
|
|
|
|
: #terminate ( -- node ) empty-node <#terminate> ;
|
2005-07-27 01:46:06 -04:00
|
|
|
|
2006-05-02 01:49:52 -04:00
|
|
|
TUPLE: #declare ;
|
|
|
|
C: #declare make-node ;
|
|
|
|
: #declare ( classes -- node ) param-node <#declare> ;
|
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: node-inputs ( d-count r-count node -- )
|
|
|
|
tuck
|
2006-05-15 01:49:07 -04:00
|
|
|
>r r-tail r> set-node-in-r
|
2005-05-17 16:13:08 -04:00
|
|
|
>r d-tail r> set-node-in-d ;
|
|
|
|
|
|
|
|
: node-outputs ( d-count r-count node -- )
|
|
|
|
tuck
|
2006-05-15 01:49:07 -04:00
|
|
|
>r r-tail r> set-node-out-r
|
2005-05-17 16:13:08 -04:00
|
|
|
>r d-tail r> set-node-out-d ;
|
|
|
|
|
|
|
|
! Variable holding dataflow graph being built.
|
2004-11-27 00:33:17 -05:00
|
|
|
SYMBOL: dataflow-graph
|
2005-05-17 16:13:08 -04:00
|
|
|
! The most recently added node.
|
|
|
|
SYMBOL: current-node
|
2004-11-27 00:33:17 -05:00
|
|
|
|
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
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
2005-07-28 15:17:31 -04:00
|
|
|
: node-values ( node -- values )
|
|
|
|
[
|
|
|
|
dup node-in-d % dup node-out-d %
|
|
|
|
dup node-in-r % node-out-r %
|
2005-10-29 23:25:38 -04:00
|
|
|
] { } make ;
|
2005-05-22 02:35:38 -04:00
|
|
|
|
|
|
|
: last-node ( node -- last )
|
2005-09-24 15:21:17 -04:00
|
|
|
dup node-successor [ last-node ] [ ] ?if ;
|
2005-05-22 02:35:38 -04:00
|
|
|
|
2005-08-29 21:00:39 -04:00
|
|
|
: penultimate-node ( node -- penultimate )
|
|
|
|
dup node-successor dup [
|
|
|
|
dup node-successor
|
2005-09-24 15:21:17 -04:00
|
|
|
[ nip penultimate-node ] [ drop ] if
|
2005-08-29 21:00:39 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-08-29 21:00:39 -04:00
|
|
|
|
2005-09-04 17:07:59 -04:00
|
|
|
: drop-inputs ( node -- #shuffle )
|
2005-09-17 22:25:18 -04:00
|
|
|
node-in-d clone in-node <#shuffle> ;
|
2005-09-04 17:07:59 -04:00
|
|
|
|
|
|
|
: #drop ( n -- #shuffle )
|
2005-09-17 22:25:18 -04:00
|
|
|
d-tail in-node <#shuffle> ;
|
2005-08-02 00:25:05 -04:00
|
|
|
|
2005-08-29 21:00:39 -04:00
|
|
|
: each-node ( node quot -- | quot: node -- )
|
2005-08-04 17:39:39 -04:00
|
|
|
over [
|
|
|
|
[ call ] 2keep swap
|
|
|
|
[ node-children [ swap each-node ] each-with ] 2keep
|
|
|
|
node-successor swap each-node
|
|
|
|
] [
|
|
|
|
2drop
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ; inline
|
2005-08-04 17:39:39 -04:00
|
|
|
|
|
|
|
: each-node-with ( obj node quot -- | quot: obj node -- )
|
|
|
|
swap [ with ] each-node 2drop ; inline
|
2005-08-07 00:00:57 -04:00
|
|
|
|
2005-08-29 21:00:39 -04:00
|
|
|
: all-nodes? ( node quot -- ? | quot: node -- ? )
|
|
|
|
over [
|
|
|
|
[ call ] 2keep rot [
|
|
|
|
[
|
|
|
|
swap node-children [ swap all-nodes? ] all-with?
|
|
|
|
] 2keep rot [
|
|
|
|
>r node-successor r> all-nodes?
|
|
|
|
] [
|
|
|
|
2drop f
|
2005-09-24 15:21:17 -04:00
|
|
|
] if
|
2005-08-29 21:00:39 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
2005-09-24 15:21:17 -04:00
|
|
|
] if
|
2005-08-29 21:00:39 -04:00
|
|
|
] [
|
|
|
|
2drop t
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ; inline
|
2005-08-29 21:00:39 -04:00
|
|
|
|
|
|
|
: all-nodes-with? ( obj node quot -- ? | quot: obj node -- ? )
|
|
|
|
swap [ with rot ] all-nodes? 2nip ; inline
|
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: remember-node ( word node -- )
|
|
|
|
#! Annotate each node with the fact it was inlined from
|
|
|
|
#! 'word'.
|
|
|
|
[
|
2005-09-23 01:22:04 -04:00
|
|
|
dup #call?
|
|
|
|
[ [ node-history ?push ] keep set-node-history ]
|
2005-09-24 15:21:17 -04:00
|
|
|
[ 2drop ] if
|
2005-08-11 19:08:22 -04:00
|
|
|
] each-node-with ;
|
|
|
|
|
2005-09-04 01:09:46 -04:00
|
|
|
GENERIC: calls-label* ( label node -- ? )
|
|
|
|
|
|
|
|
M: node calls-label* 2drop f ;
|
|
|
|
|
|
|
|
M: #call-label calls-label* node-param eq? ;
|
|
|
|
|
|
|
|
: calls-label? ( label node -- ? )
|
|
|
|
[ calls-label* not ] all-nodes-with? not ;
|
|
|
|
|
|
|
|
: recursive-label? ( node -- ? )
|
|
|
|
dup node-param swap calls-label? ;
|
2006-03-02 01:47:34 -05:00
|
|
|
|
|
|
|
SYMBOL: node-stack
|
|
|
|
|
|
|
|
: >node node-stack get push ;
|
|
|
|
: node> node-stack get pop ;
|
|
|
|
: node@ node-stack get peek ;
|
|
|
|
|
|
|
|
DEFER: iterate-nodes
|
|
|
|
|
|
|
|
: iterate-children ( quot -- )
|
2006-03-06 19:19:20 -05:00
|
|
|
node@ node-children [ swap iterate-nodes ] each-with ;
|
|
|
|
inline
|
2006-03-02 01:47:34 -05:00
|
|
|
|
|
|
|
: iterate-next ( -- node ) node@ node-successor ;
|
|
|
|
|
|
|
|
: iterate-nodes ( node quot -- )
|
|
|
|
over [
|
|
|
|
[ swap >node call node> drop ] keep
|
|
|
|
over [ iterate-nodes ] [ 2drop ] if
|
|
|
|
] [
|
|
|
|
2drop
|
|
|
|
] if ; inline
|
|
|
|
|
2006-03-06 19:19:20 -05:00
|
|
|
: ?set-node-successor ( next prev -- )
|
|
|
|
[ set-node-successor ] [ drop ] if* ;
|
|
|
|
|
|
|
|
: map-node ( prev quot -- )
|
|
|
|
swap >r node@ swap call dup r> ?set-node-successor
|
|
|
|
node> drop >node ; inline
|
|
|
|
|
|
|
|
DEFER: map-children
|
|
|
|
DEFER: (map-nodes)
|
|
|
|
|
|
|
|
: map-next ( quot -- )
|
|
|
|
node@ [
|
|
|
|
swap [ map-children ] keep
|
|
|
|
node> node-successor >node (map-nodes)
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] if* ; inline
|
|
|
|
|
|
|
|
: (map-nodes) ( prev quot -- | quot: node -- node )
|
|
|
|
node@
|
|
|
|
[ [ map-node ] keep map-next ]
|
|
|
|
[ drop f swap ?set-node-successor ] if ; inline
|
|
|
|
|
|
|
|
: map-first ( node quot -- node | quot: node -- node )
|
|
|
|
call node> drop dup >node ; inline
|
|
|
|
|
|
|
|
: map-nodes ( node quot -- node | quot: node -- node )
|
|
|
|
over [
|
|
|
|
over >node [ map-first ] keep map-next node>
|
|
|
|
] when drop ; inline
|
|
|
|
|
|
|
|
: map-children ( quot -- | quot: node -- node )
|
|
|
|
node@ [ node-children [ swap map-nodes ] map-with ] keep
|
|
|
|
set-node-children ; inline
|
|
|
|
|
2006-03-02 01:47:34 -05:00
|
|
|
: with-node-iterator ( quot -- )
|
2006-03-06 01:04:43 -05:00
|
|
|
[ V{ } clone node-stack set call ] with-scope ; inline
|
2006-03-06 23:35:32 -05:00
|
|
|
|
|
|
|
: (subst-values) ( new old node -- )
|
|
|
|
[
|
|
|
|
[ node-in-d subst ] 3keep [ node-in-r subst ] 3keep
|
|
|
|
[ node-out-d subst ] 3keep [ node-out-r subst ] 3keep
|
|
|
|
drop
|
|
|
|
] each-node 2drop ;
|
|
|
|
|
|
|
|
: subst-values ( new old node -- )
|
|
|
|
#! Mutates nodes.
|
|
|
|
1 node-stack get head* swap add
|
|
|
|
[ >r 2dup r> node-successor (subst-values) ] each 2drop ;
|