2010-01-06 23:39:22 -05:00
|
|
|
! Copyright (C) 2004, 2010 Slava Pestov.
|
2008-07-20 05:24:37 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-08-13 15:17:04 -04:00
|
|
|
USING: fry arrays generic assocs kernel math namespaces parser
|
2008-11-11 19:46:31 -05:00
|
|
|
sequences words vectors math.intervals classes
|
2008-08-22 04:12:15 -04:00
|
|
|
accessors combinators stack-checker.state stack-checker.visitor
|
|
|
|
stack-checker.inlining ;
|
2008-07-20 05:24:37 -04:00
|
|
|
IN: compiler.tree
|
|
|
|
|
|
|
|
! High-level tree SSA form.
|
|
|
|
|
2008-08-24 15:02:33 -04:00
|
|
|
TUPLE: node < identity-tuple ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-14 00:52:49 -04:00
|
|
|
TUPLE: #introduce < node out-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#introduce> ( out-d -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#introduce new swap >>out-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-30 03:31:27 -04:00
|
|
|
TUPLE: #call < node word in-d out-d body method class info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#call> ( inputs outputs word -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#call new
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>word
|
|
|
|
swap >>out-d
|
|
|
|
swap >>in-d ;
|
|
|
|
|
2008-08-24 15:02:33 -04:00
|
|
|
TUPLE: #call-recursive < node label in-d out-d info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#call-recursive> ( inputs outputs label -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#call-recursive new
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>label
|
|
|
|
swap >>out-d
|
|
|
|
swap >>in-d ;
|
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
TUPLE: #push < node literal out-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#push> ( literal value -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#push new
|
2008-07-20 05:24:37 -04:00
|
|
|
swap 1array >>out-d
|
|
|
|
swap >>literal ;
|
|
|
|
|
2008-08-07 07:34:28 -04:00
|
|
|
TUPLE: #renaming < node ;
|
|
|
|
|
2008-11-11 19:46:31 -05:00
|
|
|
TUPLE: #shuffle < #renaming mapping in-d out-d in-r out-r ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#shuffle> ( in-d out-d in-r out-r mapping -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#shuffle new
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>mapping
|
|
|
|
swap >>out-r
|
2008-11-11 19:46:31 -05:00
|
|
|
swap >>in-r
|
|
|
|
swap >>out-d
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>in-d ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#data-shuffle> ( in-d out-d mapping -- node )
|
|
|
|
[ f f ] dip <#shuffle> ; inline
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#drop> ( inputs -- node )
|
|
|
|
{ } { } <#data-shuffle> ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-15 00:35:19 -04:00
|
|
|
TUPLE: #terminate < node in-d in-r ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#terminate> ( in-d in-r -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#terminate new
|
2008-08-15 00:35:19 -04:00
|
|
|
swap >>in-r
|
2008-07-27 03:32:40 -04:00
|
|
|
swap >>in-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-30 04:38:10 -04:00
|
|
|
TUPLE: #branch < node in-d children live-branches ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
: new-branch ( value children class -- node )
|
|
|
|
new
|
|
|
|
swap >>children
|
|
|
|
swap 1array >>in-d ; inline
|
|
|
|
|
|
|
|
TUPLE: #if < #branch ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#if> ( ? true false -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
2array #if new-branch ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
|
|
|
TUPLE: #dispatch < #branch ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#dispatch> ( n branches -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#dispatch new-branch ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-18 21:49:03 -04:00
|
|
|
TUPLE: #phi < node phi-in-d phi-info-d out-d terminated ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#phi> ( d-phi-in d-phi-out terminated -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#phi new
|
2008-08-01 21:04:36 -04:00
|
|
|
swap >>terminated
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>out-d
|
|
|
|
swap >>phi-in-d ;
|
|
|
|
|
|
|
|
TUPLE: #declare < node declaration ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#declare> ( declaration -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#declare new
|
2008-07-24 00:50:21 -04:00
|
|
|
swap >>declaration ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-24 15:02:33 -04:00
|
|
|
TUPLE: #return < node in-d info ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#return> ( stack -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#return new
|
2008-07-27 03:32:40 -04:00
|
|
|
swap >>in-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-15 05:09:23 -04:00
|
|
|
TUPLE: #recursive < node in-d word label loop? child ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#recursive> ( label inputs child -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#recursive new
|
2008-07-27 21:25:42 -04:00
|
|
|
swap >>child
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>in-d
|
2008-08-15 05:09:23 -04:00
|
|
|
swap >>label ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-08-24 15:02:33 -04:00
|
|
|
TUPLE: #enter-recursive < node in-d out-d label info ;
|
2008-07-27 03:32:40 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#enter-recursive> ( label inputs outputs -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#enter-recursive new
|
2008-07-27 03:32:40 -04:00
|
|
|
swap >>out-d
|
|
|
|
swap >>in-d
|
|
|
|
swap >>label ;
|
|
|
|
|
2008-08-24 15:02:33 -04:00
|
|
|
TUPLE: #return-recursive < #renaming in-d out-d label info ;
|
2008-07-27 03:32:40 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#return-recursive> ( label inputs outputs -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#return-recursive new
|
2008-07-27 03:32:40 -04:00
|
|
|
swap >>out-d
|
|
|
|
swap >>in-d
|
|
|
|
swap >>label ;
|
|
|
|
|
2008-08-07 07:34:28 -04:00
|
|
|
TUPLE: #copy < #renaming in-d out-d ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#copy> ( inputs outputs -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#copy new
|
2008-07-20 05:24:37 -04:00
|
|
|
swap >>out-d
|
|
|
|
swap >>in-d ;
|
|
|
|
|
2008-08-12 03:41:18 -04:00
|
|
|
TUPLE: #alien-node < node params ;
|
|
|
|
|
|
|
|
: new-alien-node ( params class -- node )
|
|
|
|
new
|
|
|
|
over in-d>> >>in-d
|
|
|
|
over out-d>> >>out-d
|
|
|
|
swap >>params ; inline
|
|
|
|
|
|
|
|
TUPLE: #alien-invoke < #alien-node in-d out-d ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#alien-invoke> ( params -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#alien-invoke new-alien-node ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
|
|
|
TUPLE: #alien-indirect < #alien-node in-d out-d ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#alien-indirect> ( params -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#alien-indirect new-alien-node ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
2010-01-06 23:39:22 -05:00
|
|
|
TUPLE: #alien-assembly < #alien-node in-d out-d ;
|
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#alien-assembly> ( params -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#alien-assembly new-alien-node ;
|
2010-01-06 23:39:22 -05:00
|
|
|
|
2010-07-28 00:49:26 -04:00
|
|
|
TUPLE: #alien-callback < node params child ;
|
2008-08-12 03:41:18 -04:00
|
|
|
|
2011-11-06 23:41:31 -05:00
|
|
|
: <#alien-callback> ( params child -- node )
|
2011-11-13 01:04:26 -05:00
|
|
|
#alien-callback new
|
2010-07-28 00:49:26 -04:00
|
|
|
swap >>child
|
2008-08-12 03:41:18 -04:00
|
|
|
swap >>params ;
|
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
: node, ( node -- ) stack-visitor get push ;
|
|
|
|
|
2008-08-07 07:34:28 -04:00
|
|
|
GENERIC: inputs/outputs ( #renaming -- inputs outputs )
|
|
|
|
|
|
|
|
M: #shuffle inputs/outputs mapping>> unzip swap ;
|
|
|
|
M: #copy inputs/outputs [ in-d>> ] [ out-d>> ] bi ;
|
|
|
|
M: #return-recursive inputs/outputs [ in-d>> ] [ out-d>> ] bi ;
|
|
|
|
|
2008-08-18 22:30:10 -04:00
|
|
|
: ends-with-terminate? ( nodes -- ? )
|
2009-05-25 17:38:33 -04:00
|
|
|
[ f ] [ last #terminate? ] if-empty ;
|
2008-08-18 22:30:10 -04:00
|
|
|
|
2008-07-27 21:25:42 -04:00
|
|
|
M: vector child-visitor V{ } clone ;
|
2011-11-06 23:41:31 -05:00
|
|
|
M: vector #introduce, <#introduce> node, ;
|
|
|
|
M: vector #call, <#call> node, ;
|
|
|
|
M: vector #push, <#push> node, ;
|
|
|
|
M: vector #shuffle, <#shuffle> node, ;
|
|
|
|
M: vector #drop, <#drop> node, ;
|
2008-11-11 19:46:31 -05:00
|
|
|
M: vector #>r, [ [ f f ] dip ] [ swap zip ] 2bi #shuffle, ;
|
|
|
|
M: vector #r>, [ swap [ f swap ] dip f ] [ swap zip ] 2bi #shuffle, ;
|
2011-11-06 23:41:31 -05:00
|
|
|
M: vector #return, <#return> node, ;
|
|
|
|
M: vector #enter-recursive, <#enter-recursive> node, ;
|
|
|
|
M: vector #return-recursive, <#return-recursive> node, ;
|
|
|
|
M: vector #call-recursive, <#call-recursive> node, ;
|
|
|
|
M: vector #terminate, <#terminate> node, ;
|
|
|
|
M: vector #if, <#if> node, ;
|
|
|
|
M: vector #dispatch, <#dispatch> node, ;
|
|
|
|
M: vector #phi, <#phi> node, ;
|
|
|
|
M: vector #declare, <#declare> node, ;
|
|
|
|
M: vector #recursive, <#recursive> node, ;
|
|
|
|
M: vector #copy, <#copy> node, ;
|
|
|
|
M: vector #alien-invoke, <#alien-invoke> node, ;
|
|
|
|
M: vector #alien-indirect, <#alien-indirect> node, ;
|
|
|
|
M: vector #alien-assembly, <#alien-assembly> node, ;
|
|
|
|
M: vector #alien-callback, <#alien-callback> node, ;
|