factor/basis/compiler/tree/propagation/simple/simple.factor

124 lines
4.0 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 sequences.private assocs words
namespaces classes.algebra combinators classes classes.tuple
classes.tuple.private continuations arrays byte-arrays strings
math math.partial-dispatch math.private slots generic
generic.standard generic.math
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-25 03:07:45 -04:00
compiler.tree.propagation.slots
compiler.tree.propagation.inlining
2008-07-20 05:24:37 -04:00
compiler.tree.propagation.constraints ;
IN: compiler.tree.propagation.simple
! Propagation for straight-line code.
2008-07-20 05:24:37 -04:00
M: #introduce propagate-before
2008-08-14 00:52:49 -04:00
out-d>> [ object-info swap set-value-info ] each ;
2008-07-20 05:24:37 -04:00
M: #push propagate-before
[ literal>> <literal-info> ] [ out-d>> first ] bi
2008-07-22 05:45:03 -04:00
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 word -- )
dup +constraints+ word-prop [ nip custom-constraints ] [
dup predicate? [
[ [ in-d>> first ] [ out-d>> first ] bi ]
[ "predicating" word-prop ] bi*
swap predicate-constraints assume
] [ 2drop ] if
2008-07-20 05:24:37 -04:00
] if* ;
: call-outputs-quot ( #call word -- infos )
[ in-d>> [ value-info ] map ] [ +outputs+ word-prop ] bi*
with-datastack ;
2008-07-25 03:07:45 -04:00
: foldable-call? ( #call word -- ? )
"foldable" word-prop
[ in-d>> [ value-info literal?>> ] all? ] [ drop f ] if ;
: fold-call ( #call word -- infos )
[ in-d>> [ value-info literal>> ] map ]
[ [ execute ] curry ]
bi* with-datastack
[ <literal-info> ] map ;
: predicate-output-infos ( info class -- info )
[ class>> ] dip {
{ [ 2dup class<= ] [ t <literal-info> ] }
{ [ 2dup classes-intersect? not ] [ f <literal-info> ] }
[ object-info ]
} cond 2nip ;
: propagate-predicate ( #call word -- infos )
[ in-d>> first value-info ] [ "predicating" word-prop ] bi*
predicate-output-infos 1array ;
: default-output-value-infos ( #call word -- infos )
"default-output-classes" word-prop
[ class-infos ] [ out-d>> length object-info <repetition> ] ?if ;
2008-07-20 05:24:37 -04:00
: output-value-infos ( #call word -- infos )
{
{ [ 2dup foldable-call? ] [ fold-call ] }
2008-07-25 03:07:45 -04:00
{ [ dup tuple-constructor? ] [ propagate-tuple-constructor ] }
2008-07-24 18:34:08 -04:00
{ [ dup sequence-constructor? ] [ propagate-sequence-constructor ] }
{ [ dup predicate? ] [ propagate-predicate ] }
{ [ dup +outputs+ word-prop ] [ call-outputs-quot ] }
[ default-output-value-infos ]
} cond ;
2008-07-20 05:24:37 -04:00
: do-inlining ( #call word -- ? )
2008-07-25 03:07:45 -04:00
{
2008-08-02 00:31:43 -04:00
{ [ dup always-inline-word? ] [ always-inline-word ] }
{ [ dup standard-generic? ] [ inline-standard-method ] }
{ [ dup math-generic? ] [ inline-math-method ] }
{ [ dup math-partial? ] [ inline-math-partial ] }
{ [ dup method-body? ] [ inline-method-body ] }
[ 2drop f ]
2008-07-25 03:07:45 -04:00
} cond ;
2008-07-20 05:24:37 -04:00
M: #call propagate-before
dup word>> 2dup do-inlining [ 2drop ] [
[ [ output-value-infos ] [ drop out-d>> ] 2bi set-value-infos ]
[ compute-constraints ]
2bi
] if ;
: propagate-input-classes ( node input-classes -- )
class-infos swap in-d>> refine-value-infos ;
2008-07-22 05:45:03 -04:00
M: #call propagate-after
dup word>> "input-classes" word-prop dup
[ propagate-input-classes ] [ 2drop ] if ;
2008-08-12 03:41:18 -04:00
M: #alien-invoke propagate-before
out-d>> [ object-info swap set-value-info ] each ;
M: #alien-indirect propagate-before
out-d>> [ object-info swap set-value-info ] each ;