! Copyright (C) 2004, 2010 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: accessors arrays assocs kernel namespaces sequences stack-checker.visitor vectors ; IN: compiler.tree TUPLE: node < identity-tuple ; TUPLE: introduce# < node out-d ; : ( out-d -- node ) introduce# new swap >>out-d ; TUPLE: call# < node word in-d out-d body method class info ; : ( inputs outputs word -- node ) call# new swap >>word swap >>out-d swap >>in-d ; TUPLE: call-recursive# < node label in-d out-d info ; : ( inputs outputs label -- node ) call-recursive# new swap >>label swap >>out-d swap >>in-d ; TUPLE: push# < node literal out-d ; : ( literal value -- node ) push# new swap 1array >>out-d swap >>literal ; TUPLE: renaming# < node ; TUPLE: shuffle# < renaming# mapping in-d out-d in-r out-r ; : ( in-d out-d in-r out-r mapping -- node ) shuffle# new swap >>mapping swap >>out-r swap >>in-r swap >>out-d swap >>in-d ; : ( in-d out-d mapping -- node ) [ f f ] dip ; inline : ( inputs -- node ) { } { } ; TUPLE: terminate# < node in-d in-r ; TUPLE: branch# < node in-d children live-branches ; : new-branch ( value children class -- node ) new swap >>children swap 1array >>in-d ; inline TUPLE: if# < branch# ; : ( ? true false -- node ) 2array if# new-branch ; TUPLE: dispatch# < branch# ; : ( n branches -- node ) dispatch# new-branch ; TUPLE: phi# < node phi-in-d phi-info-d out-d terminated ; : ( d-phi-in d-phi-out terminated -- node ) phi# new swap >>terminated swap >>out-d swap >>phi-in-d ; TUPLE: declare# < node declaration ; : ( declaration -- node ) declare# new swap >>declaration ; TUPLE: return# < node in-d info ; : ( stack -- node ) return# new swap >>in-d ; TUPLE: recursive# < node in-d word label loop? child ; : ( label inputs child -- node ) recursive# new swap >>child swap >>in-d swap >>label ; TUPLE: enter-recursive# < node in-d out-d label info ; : ( label inputs outputs -- node ) enter-recursive# new swap >>out-d swap >>in-d swap >>label ; TUPLE: return-recursive# < renaming# in-d out-d label info ; : ( label inputs outputs -- node ) return-recursive# new swap >>out-d swap >>in-d swap >>label ; TUPLE: copy# < renaming# in-d out-d ; : ( inputs outputs -- node ) copy# new swap >>out-d swap >>in-d ; TUPLE: alien-node# < node params in-d out-d ; TUPLE: alien-invoke# < alien-node# ; TUPLE: alien-indirect# < alien-node# ; TUPLE: alien-assembly# < alien-node# ; TUPLE: alien-callback# < node params child ; : node, ( node -- ) stack-visitor get push ; 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 ; : ends-with-terminate? ( nodes -- ? ) [ f ] [ last terminate#? ] if-empty ; M: vector child-visitor V{ } clone ; M: vector introduce#, node, ; M: vector call#, node, ; M: vector push#, node, ; M: vector shuffle#, node, ; M: vector 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#, node, ; M: vector enter-recursive#, node, ; M: vector return-recursive#, node, ; M: vector call-recursive#, node, ; M: vector terminate#, terminate# boa node, ; M: vector if#, node, ; M: vector dispatch#, node, ; M: vector phi#, node, ; M: vector declare#, node, ; M: vector recursive#, node, ; M: vector 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, ;