1 ! Copyright (C) 2005, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 IN: optimizer.known-words
4 USING: alien arrays generic hashtables inference.dataflow
5 inference.class kernel assocs math math.private kernel.private
6 sequences words parser vectors strings sbufs io namespaces
7 assocs quotations sequences.private io.binary io.crc32
8 io.buffers io.streams.string layouts splitting math.intervals
9 math.floats.private math.vectors tuples tuples.private classes
10 optimizer.def-use optimizer.backend optimizer.pattern-match
11 float-arrays combinators.private ;
13 ! the output of <tuple> and <tuple-boa> has the class which is
14 ! its second-to-last input
15 { <tuple> <tuple-boa> } [
17 node-in-d dup length 2 - swap nth dup value?
18 [ value-literal ] [ drop tuple ] if 1array f
19 ] "output-classes" set-word-prop
22 ! the output of clone has the same type as the input
25 node-in-d [ value-class* ] map f
26 ] "output-classes" set-word-prop
29 ! not [ A ] [ B ] if ==> [ B ] [ A ] if
30 : flip-branches? ( #call -- ? ) sole-consumer #if? ;
32 : (flip-branches) ( #if -- )
33 dup node-children reverse swap set-node-children ;
35 : flip-branches ( #call -- #if )
36 #! If a not is followed by an #if, flip branches and
38 dup sole-consumer (flip-branches) [ ] splice-quot ;
41 { [ dup flip-branches? ] [ flip-branches ] }
44 ! eq? on objects of disjoint types is always f
45 : disjoint-eq? ( node -- ? )
46 node-input-classes first2 2dup and
47 [ classes-intersect? not ] [ 2drop f ] if ;
50 { [ dup disjoint-eq? ] [ [ f ] inline-literals ] }
53 ! if the result of eq? is t and the second input is a literal,
54 ! the first input is equal to the second
56 dup node-in-d second dup value? [
58 value-literal 0 `input literal,
59 general-t 0 `output class,
64 ] "constraints" set-word-prop
66 ! eq? on the same object is always t
67 { eq? bignum= float= number= = } {
68 { { @ @ } [ 2drop t ] }
71 ! type applied to an object of a known type can be folded
72 : known-type? ( node -- ? )
73 node-class-first types length 1 number= ;
75 : fold-known-type ( node -- node )
76 dup node-class-first types inline-literals ;
79 { [ dup known-type? ] [ fold-known-type ] }
82 ! if the result of type is n, then the object has type n
87 [ type>class 0 `input class, ] keep
91 ] "constraints" set-word-prop
95 { 1+ 1- sq neg recip sgn truncate } [
96 { number } "specializer" set-word-prop
99 \ 2/ { fixnum } "specializer" set-word-prop
102 { number number } "specializer" set-word-prop
105 { vneg norm-sq norm normalize } [
106 { { float-array array } } "specializer" set-word-prop
109 \ n*v { * { float-array array } } "specializer" set-word-prop
110 \ v*n { { float-array array } * } "specializer" set-word-prop
111 \ n/v { * { float-array array } } "specializer" set-word-prop
112 \ v/n { { float-array array } * } "specializer" set-word-prop
114 { v+ v- v* v/ vmax vmin v. } [
115 { { float-array array } { float-array array } }
116 "specializer" set-word-prop
119 { first first2 first3 first4 }
120 [ { array } "specializer" set-word-prop ] each
122 { peek pop* pop push } [
123 { vector } "specializer" set-word-prop
127 { { string array } { sbuf vector } }
128 "specializer" set-word-prop
131 { { string array } { string array } }
132 "specializer" set-word-prop
135 { fixnum fixnum { string array } }
136 "specializer" set-word-prop
140 "specializer" set-word-prop
144 "specializer" set-word-prop
146 \ find-last-sep { string sbuf } "specializer" set-word-prop
148 \ >string { sbuf } "specializer" set-word-prop
150 \ >array { { string vector } } "specializer" set-word-prop
152 \ crc32 { string } "specializer" set-word-prop
154 \ split, { string string } "specializer" set-word-prop
156 \ memq? { array } "specializer" set-word-prop
158 \ member? { fixnum string } "specializer" set-word-prop
160 \ assoc-stack { vector } "specializer" set-word-prop
162 \ >le { { fixnum bignum } fixnum } "specializer" set-word-prop
164 \ >be { { fixnum bignum } fixnum } "specializer" set-word-prop
166 \ (buffer-until) { fixnum fixnum simple-alien string } "specializer" set-word-prop