]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/slots/slots.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / compiler / tree / propagation / slots / slots.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry assocs arrays byte-arrays strings accessors sequences
4 kernel slots classes.algebra classes.tuple classes.tuple.private
5 words math math.private combinators sequences.private namespaces
6 slots.private classes compiler.tree.propagation.info ;
7 IN: compiler.tree.propagation.slots
8
9 ! Propagation of immutable slots and array lengths
10
11 ! Revisit this code when delegation is removed and when complex
12 ! numbers become tuples.
13
14 UNION: fixed-length-sequence array byte-array string ;
15
16 : sequence-constructor? ( word -- ? )
17     { <array> <byte-array> (byte-array) <string> } memq? ;
18
19 : constructor-output-class ( word -- class )
20     {
21         { <array> array }
22         { <byte-array> byte-array }
23         { (byte-array) byte-array }
24         { <string> string }
25     } at ;
26
27 : propagate-sequence-constructor ( #call word -- infos )
28     [ in-d>> first <sequence-info> ]
29     [ constructor-output-class <class-info> ]
30     bi* value-info-intersect 1array ;
31
32 : tuple-constructor? ( word -- ? )
33     { <tuple-boa> <complex> } memq? ;
34
35 : fold-<tuple-boa> ( values class -- info )
36     [ [ literal>> ] map ] dip prefix >tuple
37     <literal-info> ;
38
39 : (propagate-tuple-constructor) ( values class -- info )
40     [ [ value-info ] map ] dip [ read-only-slots ] keep
41     over rest-slice [ dup [ literal?>> ] when ] all? [
42         [ rest-slice ] dip fold-<tuple-boa>
43     ] [
44         <tuple-info>
45     ] if ;
46
47 : propagate-<tuple-boa> ( #call -- info )
48     in-d>> unclip-last
49     value-info literal>> first (propagate-tuple-constructor) ;
50
51 : propagate-<complex> ( #call -- info )
52     in-d>> [ value-info ] map complex <tuple-info> ;
53
54 : propagate-tuple-constructor ( #call word -- infos )
55     {
56         { \ <tuple-boa> [ propagate-<tuple-boa> ] }
57         { \ <complex> [ propagate-<complex> ] }
58     } case 1array ;
59
60 : read-only-slot? ( n class -- ? )
61     all-slots [ offset>> = ] with find nip
62     dup [ read-only>> ] when ;
63
64 : literal-info-slot ( slot object -- info/f )
65     2dup class read-only-slot?
66     [ swap slot <literal-info> ] [ 2drop f ] if ;
67
68 : length-accessor? ( slot info -- ? )
69     [ 1 = ] [ length>> ] bi* and ;
70
71 : value-info-slot ( slot info -- info' )
72     {
73         { [ over 0 = ] [ 2drop fixnum <class-info> ] }
74         { [ 2dup length-accessor? ] [ nip length>> ] }
75         { [ dup literal?>> ] [ literal>> literal-info-slot ] }
76         [ [ 1- ] [ slots>> ] bi* ?nth ]
77     } cond [ object-info ] unless* ;