factor/unfinished/compiler/tree/propagation/simple/simple.factor

106 lines
2.9 KiB
Factor
Raw Normal View History

2008-07-20 05:24:37 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: fry accessors kernel sequences assocs words namespaces
classes.algebra combinators classes continuations
2008-07-22 05:45:03 -04:00
compiler.tree
compiler.tree.def-use
2008-07-22 05:45:03 -04:00
compiler.tree.propagation.info
compiler.tree.propagation.nodes
2008-07-20 05:24:37 -04:00
compiler.tree.propagation.constraints ;
IN: compiler.tree.propagation.simple
M: #introduce propagate-before
2008-07-22 05:45:03 -04:00
object <class-info> swap values>> [ set-value-info ] with each ;
2008-07-20 05:24:37 -04:00
M: #push propagate-before
2008-07-22 05:45:03 -04:00
[ literal>> value>> <literal-info> ] [ out-d>> first ] bi
set-value-info ;
: refine-value-infos ( classes values -- )
[ refine-value-info ] 2each ;
: class-infos ( classes -- infos )
[ <class-info> ] map ;
: set-value-infos ( infos values -- )
[ set-value-info ] 2each ;
2008-07-20 05:24:37 -04:00
M: #declare propagate-before
declaration>> [ <class-info> swap refine-value-info ] assoc-each ;
2008-07-20 05:24:37 -04:00
2008-07-22 05:45:03 -04:00
: predicate-constraints ( value class boolean-value -- constraint )
[ [ is-instance-of ] dip t--> ]
[ [ class-not is-instance-of ] dip f--> ]
3bi /\ ;
2008-07-20 05:24:37 -04:00
: custom-constraints ( #call quot -- )
[ [ in-d>> ] [ out-d>> ] bi append ] dip
with-datastack first assume ;
: compute-constraints ( #call -- )
dup word>> +constraints+ word-prop [ custom-constraints ] [
dup word>> predicate? [
2008-07-22 05:45:03 -04:00
[ in-d>> first ]
[ word>> "predicating" word-prop ]
[ out-d>> first ]
tri predicate-constraints assume
] [ drop ] if
2008-07-20 05:24:37 -04:00
] if* ;
: call-outputs-quot ( node -- infos )
[ in-d>> [ value-info ] map ]
[ word>> +outputs+ word-prop ]
bi with-datastack ;
: foldable-call? ( #call -- ? )
dup word>> "foldable" word-prop [
in-d>> [ value-info literal?>> ] all?
] [
drop f
] if ;
: fold-call ( #call -- infos )
[ in-d>> [ value-info literal>> ] map ]
[ word>> [ execute ] curry ]
bi with-datastack
[ <literal-info> ] map ;
2008-07-22 05:45:03 -04:00
: default-output-value-infos ( node -- infos )
dup word>> "default-output-classes" word-prop [
class-infos
] [
out-d>> length object <class-info> <repetition>
] ?if ;
2008-07-20 05:24:37 -04:00
: output-value-infos ( node -- infos )
{
{ [ dup foldable-call? ] [ fold-call ] }
{ [ dup word>> +outputs+ word-prop ] [ call-outputs-quot ] }
[ default-output-value-infos ]
} cond ;
2008-07-20 05:24:37 -04:00
M: #call propagate-before
[ [ output-value-infos ] [ out-d>> ] bi set-value-infos ]
2008-07-20 05:24:37 -04:00
[ compute-constraints ]
bi ;
2008-07-20 05:24:37 -04:00
M: node propagate-before drop ;
M: #call propagate-after
2008-07-22 05:45:03 -04:00
dup word>> "input-classes" word-prop dup [
class-infos swap in-d>> refine-value-infos
] [
2drop
] if ;
2008-07-20 05:24:37 -04:00
M: node propagate-after drop ;
: annotate-node ( node -- )
dup
[ node-defs-values ] [ node-uses-values ] bi append
[ dup value-info ] H{ } map>assoc
>>info drop ;
2008-07-22 05:45:03 -04:00
M: node propagate-around
[ propagate-before ] [ annotate-node ] [ propagate-after ] tri ;