factor/unfinished/compiler/tree/tree.factor

165 lines
3.9 KiB
Factor
Raw Normal View History

2008-07-20 05:24:37 -04:00
! Copyright (C) 2004, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays generic assocs kernel math namespaces parser
sequences words vectors math.intervals effects classes
accessors combinators stack-checker.state stack-checker.visitor ;
2008-07-20 05:24:37 -04:00
IN: compiler.tree
! High-level tree SSA form.
TUPLE: node < identity-tuple info ;
2008-07-20 05:24:37 -04:00
M: node hashcode* drop node hashcode* ;
2008-07-20 05:24:37 -04:00
TUPLE: #introduce < node value ;
2008-07-20 05:24:37 -04:00
: #introduce ( value -- node )
\ #introduce new swap >>value ;
2008-07-20 05:24:37 -04:00
TUPLE: #call < node word in-d out-d body method ;
2008-07-20 05:24:37 -04:00
: #call ( inputs outputs word -- node )
\ #call new
swap >>word
swap >>out-d
swap >>in-d ;
TUPLE: #call-recursive < node label in-d out-d ;
2008-07-20 05:24:37 -04:00
: #call-recursive ( inputs outputs label -- node )
\ #call-recursive new
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
swap 1array >>out-d
swap >>literal ;
TUPLE: #shuffle < node mapping in-d out-d ;
2008-07-20 05:24:37 -04:00
: #shuffle ( inputs outputs mapping -- node )
\ #shuffle new
swap >>mapping
swap >>out-d
swap >>in-d ;
: #drop ( inputs -- node )
{ } { } #shuffle ;
TUPLE: #>r < node in-d out-r ;
2008-07-20 05:24:37 -04:00
: #>r ( inputs outputs -- node )
\ #>r new
swap >>out-r
swap >>in-d ;
TUPLE: #r> < node in-r out-d ;
2008-07-20 05:24:37 -04:00
: #r> ( inputs outputs -- node )
\ #r> new
swap >>out-d
swap >>in-r ;
TUPLE: #terminate < node in-d ;
2008-07-20 05:24:37 -04:00
2008-07-27 03:32:40 -04:00
: #terminate ( stack -- node )
\ #terminate new
swap >>in-d ;
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 ;
: #if ( ? true false -- node )
2array \ #if new-branch ;
TUPLE: #dispatch < #branch ;
: #dispatch ( n branches -- node )
\ #dispatch new-branch ;
TUPLE: #phi < node phi-in-d phi-info-d phi-in-r phi-info-r out-d out-r terminated ;
2008-07-20 05:24:37 -04:00
: #phi ( d-phi-in d-phi-out r-phi-in r-phi-out terminated -- node )
2008-07-20 05:24:37 -04:00
\ #phi new
swap >>terminated
2008-07-20 05:24:37 -04:00
swap >>out-r
swap >>phi-in-r
swap >>out-d
swap >>phi-in-d ;
TUPLE: #declare < node declaration ;
: #declare ( declaration -- node )
2008-07-20 05:24:37 -04:00
\ #declare new
swap >>declaration ;
2008-07-20 05:24:37 -04:00
TUPLE: #return < node in-d ;
2008-07-20 05:24:37 -04:00
2008-07-27 03:32:40 -04:00
: #return ( stack -- node )
2008-07-20 05:24:37 -04:00
\ #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? returns calls child ;
2008-07-20 05:24:37 -04:00
2008-07-27 03:32:40 -04:00
: #recursive ( word label inputs child -- node )
2008-07-20 05:24:37 -04:00
\ #recursive new
swap >>child
2008-07-20 05:24:37 -04:00
swap >>in-d
swap >>label
swap >>word ;
TUPLE: #enter-recursive < node in-d out-d label ;
2008-07-27 03:32:40 -04:00
: #enter-recursive ( label inputs outputs -- node )
\ #enter-recursive new
swap >>out-d
swap >>in-d
swap >>label ;
TUPLE: #return-recursive < node in-d out-d label ;
2008-07-27 03:32:40 -04:00
: #return-recursive ( label inputs outputs -- node )
\ #return-recursive new
swap >>out-d
swap >>in-d
swap >>label ;
TUPLE: #copy < node in-d out-d ;
2008-07-20 05:24:37 -04:00
: #copy ( inputs outputs -- node )
\ #copy new
swap >>out-d
swap >>in-d ;
: node, ( node -- ) stack-visitor get push ;
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, #>r node, ;
M: vector #r>, #r> node, ;
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, ;