factor/basis/compiler/tree/tree.factor

169 lines
4.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2010 Slava Pestov.
2008-07-20 05:24:37 -04:00
! See http://factorcode.org/license.txt for BSD license.
2014-12-13 19:10:21 -05:00
USING: accessors arrays assocs kernel namespaces sequences
stack-checker.visitor vectors ;
2008-07-20 05:24:37 -04:00
IN: compiler.tree
TUPLE: node < identity-tuple ;
2008-07-20 05:24:37 -04:00
TUPLE: introduce# < node out-d ;
2008-07-20 05:24:37 -04:00
: <introduce#> ( out-d -- node )
introduce# new swap >>out-d ;
2008-07-20 05:24:37 -04:00
TUPLE: call# < node word in-d out-d body method class info ;
2008-07-20 05:24:37 -04:00
: <call#> ( inputs outputs word -- node )
call# new
2008-07-20 05:24:37 -04:00
swap >>word
swap >>out-d
swap >>in-d ;
TUPLE: call-recursive# < node label in-d out-d info ;
2008-07-20 05:24:37 -04:00
: <call-recursive#> ( inputs outputs label -- node )
call-recursive# new
2008-07-20 05:24:37 -04:00
swap >>label
swap >>out-d
swap >>in-d ;
TUPLE: push# < node literal out-d ;
2008-07-20 05:24:37 -04:00
: <push#> ( literal value -- node )
push# new
2008-07-20 05:24:37 -04:00
swap 1array >>out-d
swap >>literal ;
TUPLE: renaming# < node ;
2008-08-07 07:34:28 -04:00
TUPLE: shuffle# < renaming# mapping in-d out-d in-r out-r ;
2008-07-20 05:24:37 -04:00
: <shuffle#> ( in-d out-d in-r out-r mapping -- node )
shuffle# new
2008-07-20 05:24:37 -04:00
swap >>mapping
swap >>out-r
swap >>in-r
swap >>out-d
2008-07-20 05:24:37 -04:00
swap >>in-d ;
: <data-shuffle#> ( in-d out-d mapping -- node )
[ f f ] dip <shuffle#> ; inline
2008-07-20 05:24:37 -04:00
: <drop#> ( inputs -- node )
{ } { } <data-shuffle#> ;
2008-07-20 05:24:37 -04:00
TUPLE: terminate# < node in-d in-r ;
2008-07-20 05:24:37 -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# ;
2008-07-20 05:24:37 -04:00
: <if#> ( ? true false -- node )
2array if# new-branch ;
2008-07-20 05:24:37 -04:00
TUPLE: dispatch# < branch# ;
2008-07-20 05:24:37 -04:00
: <dispatch#> ( n branches -- node )
dispatch# new-branch ;
2008-07-20 05:24:37 -04:00
TUPLE: phi# < node phi-in-d phi-info-d out-d terminated ;
2008-07-20 05:24:37 -04:00
: <phi#> ( d-phi-in d-phi-out terminated -- node )
phi# new
swap >>terminated
2008-07-20 05:24:37 -04:00
swap >>out-d
swap >>phi-in-d ;
TUPLE: declare# < node declaration ;
2008-07-20 05:24:37 -04:00
: <declare#> ( declaration -- node )
declare# new
swap >>declaration ;
2008-07-20 05:24:37 -04:00
TUPLE: return# < node in-d info ;
2008-07-20 05:24:37 -04:00
: <return#> ( stack -- node )
return# new
2008-07-27 03:32:40 -04:00
swap >>in-d ;
2008-07-20 05:24:37 -04:00
TUPLE: recursive# < node in-d word label loop? child ;
2008-07-20 05:24:37 -04:00
: <recursive#> ( label inputs child -- node )
recursive# new
swap >>child
2008-07-20 05:24:37 -04:00
swap >>in-d
swap >>label ;
2008-07-20 05:24:37 -04:00
TUPLE: enter-recursive# < node in-d out-d label info ;
2008-07-27 03:32:40 -04:00
: <enter-recursive#> ( label inputs outputs -- node )
enter-recursive# new
2008-07-27 03:32:40 -04:00
swap >>out-d
swap >>in-d
swap >>label ;
TUPLE: return-recursive# < renaming# in-d out-d label info ;
2008-07-27 03:32:40 -04:00
: <return-recursive#> ( label inputs outputs -- node )
return-recursive# new
2008-07-27 03:32:40 -04:00
swap >>out-d
swap >>in-d
swap >>label ;
TUPLE: copy# < renaming# in-d out-d ;
2008-07-20 05:24:37 -04:00
: <copy#> ( inputs outputs -- node )
copy# new
2008-07-20 05:24:37 -04:00
swap >>out-d
swap >>in-d ;
TUPLE: alien-node# < node params in-d out-d ;
2008-08-12 03:41:18 -04:00
TUPLE: alien-invoke# < alien-node# ;
2008-08-12 03:41:18 -04:00
TUPLE: alien-indirect# < alien-node# ;
2008-08-12 03:41:18 -04:00
TUPLE: alien-assembly# < alien-node# ;
TUPLE: alien-callback# < node params child ;
2008-08-12 03:41:18 -04:00
: node, ( node -- ) stack-visitor get push ;
GENERIC: inputs/outputs ( renaming# -- inputs outputs )
2008-08-07 07:34:28 -04:00
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-07 07:34:28 -04:00
2008-08-18 22:30:10 -04:00
: ends-with-terminate? ( nodes -- ? )
[ f ] [ last terminate#? ] if-empty ;
2008-08-18 22:30:10 -04:00
M: vector child-visitor V{ } clone ;
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, ;
M: vector >r#, [ [ f f ] dip ] [ swap zip ] 2bi shuffle#, ;
M: vector r>#, [ swap [ f swap ] dip f ] [ swap zip ] 2bi shuffle#, ;
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# boa 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# boa node, ;
M: vector alien-indirect#, alien-indirect# boa node, ;
M: vector alien-assembly#, alien-assembly# boa node, ;
M: vector alien-callback#, alien-callback# boa node, ;