]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/intrinsics/intrinsics.factor
11d063c43037a6ac908ba267e94087e28ee7bfd8
[factor.git] / basis / compiler / cfg / intrinsics / intrinsics.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: words sequences kernel combinators cpu.architecture assocs
4 compiler.cfg.hats
5 compiler.cfg.stacks
6 compiler.cfg.instructions
7 compiler.cfg.intrinsics.alien
8 compiler.cfg.intrinsics.allot
9 compiler.cfg.intrinsics.fixnum
10 compiler.cfg.intrinsics.float
11 compiler.cfg.intrinsics.slots
12 compiler.cfg.intrinsics.strings
13 compiler.cfg.intrinsics.misc
14 compiler.cfg.comparisons ;
15 QUALIFIED: alien
16 QUALIFIED: alien.accessors
17 QUALIFIED: alien.data.private
18 QUALIFIED: alien.c-types
19 QUALIFIED: kernel
20 QUALIFIED: arrays
21 QUALIFIED: byte-arrays
22 QUALIFIED: kernel.private
23 QUALIFIED: slots.private
24 QUALIFIED: strings.private
25 QUALIFIED: classes.tuple.private
26 QUALIFIED: math.private
27 QUALIFIED: math.bitwise.private
28 QUALIFIED: math.integers.private
29 QUALIFIED: math.floats.private
30 QUALIFIED: math.libm
31 IN: compiler.cfg.intrinsics
32
33 : enable-intrinsics ( alist -- )
34     [ "intrinsic" set-word-prop ] assoc-each ;
35
36 {
37     { kernel.private:tag [ drop emit-tag ] }
38     { kernel.private:context-object [ emit-context-object ] }
39     { kernel.private:special-object [ emit-special-object ] }
40     { kernel.private:set-special-object [ emit-set-special-object ] }
41     { kernel.private:(identity-hashcode) [ drop emit-identity-hashcode ] }
42     { math.private:both-fixnums? [ drop emit-both-fixnums? ] }
43     { math.private:fixnum+ [ drop emit-fixnum+ ] }
44     { math.private:fixnum- [ drop emit-fixnum- ] }
45     { math.private:fixnum* [ drop emit-fixnum* ] }
46     { math.private:fixnum+fast [ drop [ ^^add ] binary-op ] }
47     { math.private:fixnum-fast [ drop [ ^^sub ] binary-op ] }
48     { math.private:fixnum*fast [ drop [ ^^mul ] binary-op ] }
49     { math.private:fixnum-bitand [ drop [ ^^and ] binary-op ] }
50     { math.private:fixnum-bitor [ drop [ ^^or ] binary-op ] }
51     { math.private:fixnum-bitxor [ drop [ ^^xor ] binary-op ] }
52     { math.private:fixnum-shift-fast [ emit-fixnum-shift-fast ] }
53     { math.private:fixnum-bitnot [ drop [ ^^not ] unary-op ] }
54     { math.private:fixnum< [ drop cc< emit-fixnum-comparison ] }
55     { math.private:fixnum<= [ drop cc<= emit-fixnum-comparison ] }
56     { math.private:fixnum>= [ drop cc>= emit-fixnum-comparison ] }
57     { math.private:fixnum> [ drop cc> emit-fixnum-comparison ] }
58     { kernel:eq? [ emit-eq ] }
59     { slots.private:slot [ emit-slot ] }
60     { slots.private:set-slot [ emit-set-slot ] }
61     { strings.private:string-nth-fast [ drop emit-string-nth-fast ] }
62     { strings.private:set-string-nth-fast [ drop emit-set-string-nth-fast ] }
63     { classes.tuple.private:<tuple-boa> [ emit-<tuple-boa> ] }
64     { arrays:<array> [ emit-<array> ] }
65     { byte-arrays:<byte-array> [ emit-<byte-array> ] }
66     { byte-arrays:(byte-array) [ emit-(byte-array) ] }
67     { kernel:<wrapper> [ emit-simple-allot ] }
68     { alien.data.private:(local-allot) [ emit-local-allot ] }
69     { alien:<displaced-alien> [ emit-<displaced-alien> ] }
70     { alien.accessors:alien-unsigned-1 [ int-rep alien.c-types:uchar emit-load-memory ] }
71     { alien.accessors:set-alien-unsigned-1 [ int-rep alien.c-types:uchar emit-store-memory ] }
72     { alien.accessors:alien-signed-1 [ int-rep alien.c-types:char emit-load-memory ] }
73     { alien.accessors:set-alien-signed-1 [ int-rep alien.c-types:char emit-store-memory ] }
74     { alien.accessors:alien-unsigned-2 [ int-rep alien.c-types:ushort emit-load-memory ] }
75     { alien.accessors:set-alien-unsigned-2 [ int-rep alien.c-types:ushort emit-store-memory ] }
76     { alien.accessors:alien-signed-2 [ int-rep alien.c-types:short emit-load-memory ] }
77     { alien.accessors:set-alien-signed-2 [ int-rep alien.c-types:short emit-store-memory ] }
78     { alien.accessors:alien-cell [ emit-alien-cell ] }
79     { alien.accessors:set-alien-cell [ emit-set-alien-cell ] }
80 } enable-intrinsics
81
82 : enable-alien-4-intrinsics ( -- )
83     {
84         { alien.accessors:alien-signed-4 [ int-rep alien.c-types:int emit-load-memory ] }
85         { alien.accessors:set-alien-signed-4 [ int-rep alien.c-types:int emit-store-memory ] }
86         { alien.accessors:alien-unsigned-4 [ int-rep alien.c-types:uint emit-load-memory ] }
87         { alien.accessors:set-alien-unsigned-4 [ int-rep alien.c-types:uint emit-store-memory ] }
88     } enable-intrinsics ;
89
90 : enable-float-intrinsics ( -- )
91     {
92         { math.private:float+ [ drop [ ^^add-float ] binary-op ] }
93         { math.private:float- [ drop [ ^^sub-float ] binary-op ] }
94         { math.private:float* [ drop [ ^^mul-float ] binary-op ] }
95         { math.private:float/f [ drop [ ^^div-float ] binary-op ] }
96         { math.private:float< [ drop cc< emit-float-ordered-comparison ] }
97         { math.private:float<= [ drop cc<= emit-float-ordered-comparison ] }
98         { math.private:float>= [ drop cc>= emit-float-ordered-comparison ] }
99         { math.private:float> [ drop cc> emit-float-ordered-comparison ] }
100         { math.private:float-u< [ drop cc< emit-float-unordered-comparison ] }
101         { math.private:float-u<= [ drop cc<= emit-float-unordered-comparison ] }
102         { math.private:float-u>= [ drop cc>= emit-float-unordered-comparison ] }
103         { math.private:float-u> [ drop cc> emit-float-unordered-comparison ] }
104         { math.private:float= [ drop cc= emit-float-unordered-comparison ] }
105         { math.private:float>fixnum [ drop [ ^^float>integer ] unary-op ] }
106         { math.private:fixnum>float [ drop [ ^^integer>float ] unary-op ] }
107         { math.floats.private:float-unordered? [ drop cc/<>= emit-float-unordered-comparison ] }
108         { alien.accessors:alien-float [ float-rep f emit-load-memory ] }
109         { alien.accessors:set-alien-float [ float-rep f emit-store-memory ] }
110         { alien.accessors:alien-double [ double-rep f emit-load-memory ] }
111         { alien.accessors:set-alien-double [ double-rep f emit-store-memory ] }
112     } enable-intrinsics ;
113
114 : enable-fsqrt ( -- )
115     {
116         { math.libm:fsqrt [ drop [ ^^sqrt ] unary-op ] }
117     } enable-intrinsics ;
118
119 : enable-float-min/max ( -- )
120     {
121         { math.floats.private:float-min [ drop [ ^^min-float ] binary-op ] }
122         { math.floats.private:float-max [ drop [ ^^max-float ] binary-op ] }
123     } enable-intrinsics ;
124
125 : enable-float-functions ( -- )
126     {
127         { math.libm:facos [ drop "acos" emit-unary-float-function ] }
128         { math.libm:fasin [ drop "asin" emit-unary-float-function ] }
129         { math.libm:fatan [ drop "atan" emit-unary-float-function ] }
130         { math.libm:fatan2 [ drop "atan2" emit-binary-float-function ] }
131         { math.libm:fcos [ drop "cos" emit-unary-float-function ] }
132         { math.libm:fsin [ drop "sin" emit-unary-float-function ] }
133         { math.libm:ftan [ drop "tan" emit-unary-float-function ] }
134         { math.libm:fcosh [ drop "cosh" emit-unary-float-function ] }
135         { math.libm:fsinh [ drop "sinh" emit-unary-float-function ] }
136         { math.libm:ftanh [ drop "tanh" emit-unary-float-function ] }
137         { math.libm:fexp [ drop "exp" emit-unary-float-function ] }
138         { math.libm:flog [ drop "log" emit-unary-float-function ] }
139         { math.libm:flog10 [ drop "log10" emit-unary-float-function ] }
140         { math.libm:fpow [ drop "pow" emit-binary-float-function ] }
141         { math.libm:facosh [ drop "acosh" emit-unary-float-function ] }
142         { math.libm:fasinh [ drop "asinh" emit-unary-float-function ] }
143         { math.libm:fatanh [ drop "atanh" emit-unary-float-function ] }
144         { math.libm:fsqrt [ drop "sqrt" emit-unary-float-function ] }
145         { math.floats.private:float-min [ drop "fmin" emit-binary-float-function ] }
146         { math.floats.private:float-max [ drop "fmax" emit-binary-float-function ] }
147         { math.private:float-mod [ drop "fmod" emit-binary-float-function ] }
148     } enable-intrinsics ;
149
150 : enable-min/max ( -- )
151     {
152         { math.integers.private:fixnum-min [ drop [ ^^min ] binary-op ] }
153         { math.integers.private:fixnum-max [ drop [ ^^max ] binary-op ] }
154     } enable-intrinsics ;
155
156 : enable-log2 ( -- )
157     {
158         { math.integers.private:fixnum-log2 [ drop [ ^^log2 ] unary-op ] }
159     } enable-intrinsics ;
160
161 : enable-bit-count ( -- )
162     {
163         { math.bitwise.private:fixnum-bit-count [ drop [ ^^bit-count ] unary-op ] }
164     } enable-intrinsics ;
165
166 : emit-intrinsic ( node word -- )
167     "intrinsic" word-prop call( node -- ) ;