2008-07-20 05:24:37 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-07-24 18:34:08 -04:00
|
|
|
USING: fry accessors kernel sequences sequences.private assocs
|
|
|
|
words namespaces classes.algebra combinators classes
|
|
|
|
continuations arrays byte-arrays strings
|
2008-07-22 05:45:03 -04:00
|
|
|
compiler.tree
|
2008-07-24 00:50:21 -04:00
|
|
|
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
|
2008-07-24 00:50:21 -04:00
|
|
|
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 )
|
2008-07-24 00:50:21 -04:00
|
|
|
[ [ is-instance-of ] dip t--> ]
|
|
|
|
[ [ class-not is-instance-of ] dip f--> ]
|
|
|
|
3bi /\ ;
|
2008-07-20 05:24:37 -04:00
|
|
|
|
2008-07-23 01:17:08 -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* ;
|
|
|
|
|
2008-07-24 00:50:21 -04:00
|
|
|
: 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
|
|
|
|
2008-07-24 18:34:08 -04:00
|
|
|
UNION: fixed-length-sequence array byte-array string ;
|
|
|
|
|
|
|
|
: sequence-constructor? ( node -- ? )
|
|
|
|
word>> { <array> <byte-array> <string> } memq? ;
|
|
|
|
|
|
|
|
: propagate-sequence-constructor ( node -- infos )
|
|
|
|
[ default-output-value-infos first ]
|
|
|
|
[ in-d>> first <sequence-info> ]
|
|
|
|
bi value-info-intersect 1array ;
|
|
|
|
|
|
|
|
: length-accessor? ( node -- ? )
|
|
|
|
dup in-d>> first fixed-length-sequence value-is?
|
|
|
|
[ word>> \ length eq? ] [ drop f ] if ;
|
|
|
|
|
|
|
|
: propagate-length ( node -- infos )
|
|
|
|
in-d>> first value-info length>>
|
|
|
|
[ array-capacity <class-info> ] unless* 1array ;
|
|
|
|
|
2008-07-23 01:17:08 -04:00
|
|
|
: output-value-infos ( node -- infos )
|
2008-07-24 00:50:21 -04:00
|
|
|
{
|
|
|
|
{ [ dup foldable-call? ] [ fold-call ] }
|
2008-07-24 18:34:08 -04:00
|
|
|
{ [ dup sequence-constructor? ] [ propagate-sequence-constructor ] }
|
|
|
|
{ [ dup length-accessor? ] [ propagate-length ] }
|
2008-07-24 00:50:21 -04:00
|
|
|
{ [ 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
|
2008-07-23 01:17:08 -04:00
|
|
|
[ [ output-value-infos ] [ out-d>> ] bi set-value-infos ]
|
2008-07-20 05:24:37 -04:00
|
|
|
[ compute-constraints ]
|
2008-07-23 01:17:08 -04:00
|
|
|
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 -- )
|
2008-07-24 00:50:21 -04:00
|
|
|
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 ;
|