]> gitweb.factorcode.org Git - factor.git/blob - basis/hints/hints.factor
math.vectors.specialization: first attempt at some call site splitting for vector...
[factor.git] / basis / hints / hints.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs byte-arrays byte-vectors classes
4 combinators definitions fry generic generic.single
5 generic.standard hashtables io.binary io.streams.string kernel
6 kernel.private math math.parser namespaces parser sbufs
7 sequences splitting splitting.private strings vectors words ;
8 IN: hints
9
10 GENERIC: specializer-predicate ( spec -- quot )
11
12 M: class specializer-predicate "predicate" word-prop ;
13
14 M: object specializer-predicate '[ _ eq? ] ;
15
16 GENERIC: specializer-declaration ( spec -- class )
17
18 M: class specializer-declaration ;
19
20 M: object specializer-declaration class ;
21
22 : make-specializer ( specs -- quot )
23     dup length <reversed>
24     [ (picker) 2array ] 2map
25     [ drop object eq? not ] assoc-filter
26     [ [ t ] ] [
27         [ swap specializer-predicate append ] { } assoc>map
28         [ ] [ swap [ f ] \ if 3array append [ ] like ] map-reduce
29     ] if-empty ;
30
31 : specializer-cases ( quot word -- default alist )
32     dup [ array? ] all? [ 1array ] unless [
33         [ make-specializer ] keep
34         [ specializer-declaration ] map '[ _ declare ] pick append
35     ] { } map>assoc ;
36
37 : specialize-quot ( quot specializer -- quot' )
38     specializer-cases alist>quot ;
39
40 ! compiler.tree.propagation.inlining sets this to f
41 SYMBOL: specialize-method?
42
43 t specialize-method? set-global
44
45 : method-declaration ( method -- quot )
46     [ "method-generic" word-prop dispatch# object <array> ]
47     [ "method-class" word-prop ]
48     bi prefix [ declare ] curry [ ] like ;
49
50 : specialize-method ( quot method -- quot' )
51     [ specialize-method? get [ method-declaration prepend ] [ drop ] if ]
52     [ "method-generic" word-prop "specializer" word-prop ] bi
53     [ specialize-quot ] when* ;
54
55 : standard-method? ( method -- ? )
56     dup method-body? [
57         "method-generic" word-prop standard-generic?
58     ] [ drop f ] if ;
59
60 : specialized-def ( word -- quot )
61     [ def>> ] keep
62     dup generic? [ drop ] [
63         [ dup standard-method? [ specialize-method ] [ drop ] if ]
64         [ "specializer" word-prop [ specialize-quot ] when* ]
65         bi
66     ] if ;
67
68 : specialized-length ( specializer -- n )
69     dup [ array? ] all? [ first ] when length ;
70
71 SYNTAX: HINTS:
72     scan-object dup wrapper? [ wrapped>> ] when
73     [ changed-definition ]
74     [ parse-definition { } like "specializer" set-word-prop ] bi ;
75
76 ! Default specializers
77 { first first2 first3 first4 }
78 [ { array } "specializer" set-word-prop ] each
79
80 { last pop* pop } [
81     { vector } "specializer" set-word-prop
82 ] each
83
84 \ push { { vector } { sbuf } } "specializer" set-word-prop
85
86 \ last { { vector } } "specializer" set-word-prop
87
88 \ set-last { { object vector } } "specializer" set-word-prop
89
90 \ push-all
91 { { string sbuf } { array vector } { byte-array byte-vector } }
92 "specializer" set-word-prop
93
94 \ append
95 { { string string } { array array } }
96 "specializer" set-word-prop
97
98 \ subseq
99 { { fixnum fixnum string } { fixnum fixnum array } }
100 "specializer" set-word-prop
101
102 \ reverse-here
103 { { string } { array } }
104 "specializer" set-word-prop
105
106 \ mismatch
107 { string string }
108 "specializer" set-word-prop
109
110 \ >string { sbuf } "specializer" set-word-prop
111
112 \ >array { { vector } } "specializer" set-word-prop
113
114 \ >vector { { array } { vector } } "specializer" set-word-prop
115
116 \ >sbuf { string } "specializer" set-word-prop
117
118 \ split, { string string } "specializer" set-word-prop
119
120 \ memq? { array } "specializer" set-word-prop
121
122 \ member? { array } "specializer" set-word-prop
123
124 \ assoc-stack { vector } "specializer" set-word-prop
125
126 \ >le { { fixnum fixnum } { bignum fixnum } } "specializer" set-word-prop
127
128 \ >be { { bignum fixnum } { fixnum fixnum } } "specializer" set-word-prop
129
130 \ base> { string fixnum } "specializer" set-word-prop
131
132 M\ hashtable at* { { fixnum object } { word object } } "specializer" set-word-prop
133
134 M\ hashtable set-at { { object fixnum object } { object word object } } "specializer" set-word-prop