2008-08-02 00:31:43 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-08-02 21:21:25 -04:00
|
|
|
USING: assocs namespaces sequences kernel math combinators sets
|
2008-08-03 22:32:12 -04:00
|
|
|
disjoint-sets fry stack-checker.state compiler.tree.copy-equiv ;
|
2008-08-02 00:31:43 -04:00
|
|
|
IN: compiler.tree.escape-analysis.allocations
|
|
|
|
|
2008-08-03 22:32:12 -04:00
|
|
|
! A map from values to sequences of values
|
2008-08-02 00:31:43 -04:00
|
|
|
SYMBOL: allocations
|
|
|
|
|
|
|
|
: allocation ( value -- allocation )
|
|
|
|
resolve-copy allocations get at ;
|
|
|
|
|
|
|
|
: record-allocation ( allocation value -- )
|
2008-08-02 21:21:25 -04:00
|
|
|
{
|
|
|
|
{ [ dup not ] [ 2drop ] }
|
|
|
|
{ [ over not ] [ allocations get delete-at drop ] }
|
|
|
|
[ allocations get set-at ]
|
|
|
|
} cond ;
|
2008-08-02 00:31:43 -04:00
|
|
|
|
|
|
|
: record-allocations ( allocations values -- )
|
|
|
|
[ record-allocation ] 2each ;
|
|
|
|
|
2008-08-03 22:32:12 -04:00
|
|
|
! We track escaping values with a disjoint set.
|
|
|
|
SYMBOL: escaping-values
|
|
|
|
|
|
|
|
SYMBOL: +escaping+
|
|
|
|
|
|
|
|
: <escaping-values> ( -- disjoint-set )
|
|
|
|
<disjoint-set> +escaping+ over add-atom ;
|
|
|
|
|
|
|
|
: init-escaping-values ( -- )
|
|
|
|
copies get <escaping-values>
|
|
|
|
[ '[ drop , add-atom ] assoc-each ]
|
|
|
|
[ '[ , equate ] assoc-each ]
|
|
|
|
[ nip escaping-values set ]
|
|
|
|
2tri ;
|
|
|
|
|
|
|
|
: <slot-value> ( -- value )
|
|
|
|
<value>
|
|
|
|
[ introduce-value ]
|
|
|
|
[ escaping-values get add-atom ]
|
|
|
|
[ ]
|
|
|
|
tri ;
|
|
|
|
|
|
|
|
: same-value ( in-value out-value -- )
|
|
|
|
over [
|
|
|
|
[ is-copy-of ] [ escaping-values get equate ] 2bi
|
|
|
|
] [ 2drop ] if ;
|
|
|
|
|
2008-08-02 00:31:43 -04:00
|
|
|
: record-slot-access ( out slot# in -- )
|
2008-08-03 22:32:12 -04:00
|
|
|
over zero? [ 3drop ] [ allocation ?nth swap same-value ] if ;
|
2008-08-02 00:31:43 -04:00
|
|
|
|
2008-08-03 22:32:12 -04:00
|
|
|
: merge-values ( in-values out-value -- )
|
|
|
|
escaping-values get '[ , , equate ] each ;
|
2008-08-02 00:31:43 -04:00
|
|
|
|
|
|
|
: merge-slots ( values -- value )
|
2008-08-02 21:21:25 -04:00
|
|
|
dup [ ] contains? [
|
2008-08-03 22:32:12 -04:00
|
|
|
<slot-value> [ merge-values ] keep
|
2008-08-02 21:21:25 -04:00
|
|
|
] [ drop f ] if ;
|
|
|
|
|
2008-08-03 22:32:12 -04:00
|
|
|
: add-escaping-values ( values -- )
|
|
|
|
escaping-values get
|
|
|
|
'[ +escaping+ , equate ] each ;
|
2008-08-02 21:21:25 -04:00
|
|
|
|
2008-08-03 22:32:12 -04:00
|
|
|
: escaping-value? ( value -- ? )
|
|
|
|
+escaping+ escaping-values get equiv? ;
|
2008-08-03 06:01:05 -04:00
|
|
|
|
|
|
|
SYMBOL: escaping-allocations
|
|
|
|
|
|
|
|
: compute-escaping-allocations ( -- )
|
|
|
|
allocations get
|
2008-08-03 22:32:12 -04:00
|
|
|
[ drop escaping-value? ] assoc-filter
|
2008-08-03 06:01:05 -04:00
|
|
|
escaping-allocations set ;
|
2008-08-02 21:21:25 -04:00
|
|
|
|
|
|
|
: escaping-allocation? ( value -- ? )
|
2008-08-03 06:01:05 -04:00
|
|
|
escaping-allocations get key? ;
|