]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/slots/slots.factor
Replace ratio and complex built-in types with tuples defined in the library. This...
[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 : fold-<tuple-boa> ( values class -- info )
33     [ [ literal>> ] map ] dip prefix >tuple
34     <literal-info> ;
35
36 : (propagate-tuple-constructor) ( values class -- info )
37     [ [ value-info ] map ] dip [ read-only-slots ] keep
38     over rest-slice [ dup [ literal?>> ] when ] all? [
39         [ rest-slice ] dip fold-<tuple-boa>
40     ] [
41         <tuple-info>
42     ] if ;
43
44 : propagate-<tuple-boa> ( #call -- infos )
45     in-d>> unclip-last
46     value-info literal>> first (propagate-tuple-constructor) 1array ;
47
48 : read-only-slot? ( n class -- ? )
49     all-slots [ offset>> = ] with find nip
50     dup [ read-only>> ] when ;
51
52 : literal-info-slot ( slot object -- info/f )
53     2dup class read-only-slot?
54     [ swap slot <literal-info> ] [ 2drop f ] if ;
55
56 : length-accessor? ( slot info -- ? )
57     [ 1 = ] [ length>> ] bi* and ;
58
59 : value-info-slot ( slot info -- info' )
60     {
61         { [ over 0 = ] [ 2drop fixnum <class-info> ] }
62         { [ 2dup length-accessor? ] [ nip length>> ] }
63         { [ dup literal?>> ] [ literal>> literal-info-slot ] }
64         [ [ 1- ] [ slots>> ] bi* ?nth ]
65     } cond [ object-info ] unless* ;