]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
fix buggy simd intrinsics
[factor.git] / basis / alien / c-types / c-types.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays arrays assocs kernel kernel.private math
4 math.order math.parser namespaces make parser sequences strings
5 words splitting cpu.architecture alien alien.accessors
6 alien.strings quotations layouts system compiler.units io
7 io.files io.encodings.binary io.streams.memory accessors
8 combinators effects continuations fry classes vocabs
9 vocabs.loader words.symbol ;
10 QUALIFIED: math
11 IN: alien.c-types
12
13 SYMBOLS:
14     char uchar
15     short ushort
16     int uint
17     long ulong
18     longlong ulonglong
19     float double
20     void* bool
21     void ;
22
23 DEFER: <int>
24 DEFER: *char
25
26 TUPLE: abstract-c-type
27 { class class initial: object }
28 { boxed-class class initial: object }
29 { boxer-quot callable }
30 { unboxer-quot callable }
31 { getter callable }
32 { setter callable }
33 { size integer }
34 { align integer }
35 { align-first integer } ;
36
37 TUPLE: c-type < abstract-c-type
38 boxer
39 unboxer
40 { rep initial: int-rep }
41 stack-align? ;
42
43 : <c-type> ( -- c-type )
44     \ c-type new ; inline
45
46 SYMBOL: c-types
47
48 global [
49     c-types [ H{ } assoc-like ] change
50 ] bind
51
52 ERROR: no-c-type name ;
53
54 PREDICATE: c-type-word < word
55     "c-type" word-prop ;
56
57 UNION: c-type-name string c-type-word ;
58
59 ! C type protocol
60 GENERIC: c-type ( name -- c-type ) foldable
61
62 GENERIC: resolve-pointer-type ( name -- c-type )
63
64 << \ void \ void* "pointer-c-type" set-word-prop >>
65
66 : void? ( c-type -- ? )
67     { void "void" } member? ;
68
69 M: word resolve-pointer-type
70     dup "pointer-c-type" word-prop
71     [ ] [ drop void* ] ?if ;
72
73 M: string resolve-pointer-type
74     dup "*" append dup c-types get at
75     [ nip ] [
76         drop
77         c-types get at dup c-type-name?
78         [ resolve-pointer-type ] [ drop void* ] if
79     ] if ;
80
81 : resolve-typedef ( name -- c-type )
82     dup void? [ no-c-type ] when
83     dup c-type-name? [ c-type ] when ;
84
85 <PRIVATE
86
87 : parse-array-type ( name -- dims c-type )
88     "[" split unclip
89     [ [ "]" ?tail drop string>number ] map ] dip ;
90
91 PRIVATE>
92
93 M: string c-type ( name -- c-type )
94     CHAR: ] over member? [
95         parse-array-type prefix
96     ] [
97         dup c-types get at [ ] [
98             "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if
99         ] ?if resolve-typedef
100     ] if ;
101
102 M: word c-type
103     dup "c-type" word-prop resolve-typedef
104     [ ] [ no-c-type ] ?if ;
105
106 GENERIC: c-struct? ( c-type -- ? )
107
108 M: object c-struct? drop f ;
109
110 M: c-type-name c-struct? dup void? [ drop f ] [ c-type c-struct? ] if ;
111
112 ! These words being foldable means that words need to be
113 ! recompiled if a C type is redefined. Even so, folding the
114 ! size facilitates some optimizations.
115 GENERIC: c-type-class ( name -- class )
116
117 M: abstract-c-type c-type-class class>> ;
118
119 M: c-type-name c-type-class c-type c-type-class ;
120
121 GENERIC: c-type-boxed-class ( name -- class )
122
123 M: abstract-c-type c-type-boxed-class boxed-class>> ;
124
125 M: c-type-name c-type-boxed-class c-type c-type-boxed-class ;
126
127 GENERIC: c-type-boxer ( name -- boxer )
128
129 M: c-type c-type-boxer boxer>> ;
130
131 M: c-type-name c-type-boxer c-type c-type-boxer ;
132
133 GENERIC: c-type-boxer-quot ( name -- quot )
134
135 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
136
137 M: c-type-name c-type-boxer-quot c-type c-type-boxer-quot ;
138
139 GENERIC: c-type-unboxer ( name -- boxer )
140
141 M: c-type c-type-unboxer unboxer>> ;
142
143 M: c-type-name c-type-unboxer c-type c-type-unboxer ;
144
145 GENERIC: c-type-unboxer-quot ( name -- quot )
146
147 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
148
149 M: c-type-name c-type-unboxer-quot c-type c-type-unboxer-quot ;
150
151 GENERIC: c-type-rep ( name -- rep )
152
153 M: c-type c-type-rep rep>> ;
154
155 M: c-type-name c-type-rep c-type c-type-rep ;
156
157 GENERIC: c-type-getter ( name -- quot )
158
159 M: c-type c-type-getter getter>> ;
160
161 M: c-type-name c-type-getter c-type c-type-getter ;
162
163 GENERIC: c-type-setter ( name -- quot )
164
165 M: c-type c-type-setter setter>> ;
166
167 M: c-type-name c-type-setter c-type c-type-setter ;
168
169 GENERIC: c-type-align ( name -- n )
170
171 M: abstract-c-type c-type-align align>> ;
172
173 M: c-type-name c-type-align c-type c-type-align ;
174
175 GENERIC: c-type-align-first ( name -- n )
176
177 M: c-type-name c-type-align-first c-type c-type-align-first ;
178
179 M: abstract-c-type c-type-align-first align-first>> ;
180
181 GENERIC: c-type-stack-align? ( name -- ? )
182
183 M: c-type c-type-stack-align? stack-align?>> ;
184
185 M: c-type-name c-type-stack-align? c-type c-type-stack-align? ;
186
187 : c-type-box ( n c-type -- )
188     [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
189     %box ;
190
191 : c-type-unbox ( n c-type -- )
192     [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
193     %unbox ;
194
195 GENERIC: box-parameter ( n c-type -- )
196
197 M: c-type box-parameter c-type-box ;
198
199 M: c-type-name box-parameter c-type box-parameter ;
200
201 GENERIC: box-return ( c-type -- )
202
203 M: c-type box-return f swap c-type-box ;
204
205 M: c-type-name box-return c-type box-return ;
206
207 GENERIC: unbox-parameter ( n c-type -- )
208
209 M: c-type unbox-parameter c-type-unbox ;
210
211 M: c-type-name unbox-parameter c-type unbox-parameter ;
212
213 GENERIC: unbox-return ( c-type -- )
214
215 M: c-type unbox-return f swap c-type-unbox ;
216
217 M: c-type-name unbox-return c-type unbox-return ;
218
219 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
220
221 GENERIC: heap-size ( name -- size ) foldable
222
223 M: c-type-name heap-size c-type heap-size ;
224
225 M: abstract-c-type heap-size size>> ;
226
227 GENERIC: stack-size ( name -- size ) foldable
228
229 M: c-type-name stack-size c-type stack-size ;
230
231 M: c-type stack-size size>> cell align ;
232
233 GENERIC: byte-length ( seq -- n ) flushable
234
235 M: byte-array byte-length length ; inline
236
237 M: f byte-length drop 0 ; inline
238
239 : >c-bool ( ? -- int ) 1 0 ? ; inline
240
241 : c-bool> ( int -- ? ) 0 = not ; inline
242
243 MIXIN: value-type
244
245 : c-getter ( name -- quot )
246     c-type-getter [
247         [ "Cannot read struct fields with this type" throw ]
248     ] unless* ;
249
250 : c-type-getter-boxer ( name -- quot )
251     [ c-getter ] [ c-type-boxer-quot ] bi append ;
252
253 : c-setter ( name -- quot )
254     c-type-setter [
255         [ "Cannot write struct fields with this type" throw ]
256     ] unless* ;
257
258 : array-accessor ( c-type quot -- def )
259     [
260         \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
261     ] [ ] make ;
262
263 GENERIC: typedef ( old new -- )
264
265 PREDICATE: typedef-word < c-type-word
266     "c-type" word-prop c-type-name? ;
267
268 M: string typedef ( old new -- ) c-types get set-at ;
269
270 M: word typedef ( old new -- )
271     {
272         [ nip define-symbol ]
273         [ name>> typedef ]
274         [ swap "c-type" set-word-prop ]
275         [
276             swap dup c-type-name? [
277                 resolve-pointer-type
278                 "pointer-c-type" set-word-prop
279             ] [ 2drop ] if
280         ]
281     } 2cleave ;
282
283 TUPLE: long-long-type < c-type ;
284
285 : <long-long-type> ( -- c-type )
286     long-long-type new ;
287
288 M: long-long-type unbox-parameter ( n c-type -- )
289     c-type-unboxer %unbox-long-long ;
290
291 M: long-long-type unbox-return ( c-type -- )
292     f swap unbox-parameter ;
293
294 M: long-long-type box-parameter ( n c-type -- )
295     c-type-boxer %box-long-long ;
296
297 M: long-long-type box-return ( c-type -- )
298     f swap box-parameter ;
299
300 : define-deref ( name -- )
301     [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
302     (( c-ptr -- value )) define-inline ;
303
304 : define-out ( name -- )
305     [ "alien.c-types" constructor-word ]
306     [ dup c-setter '[ _ heap-size (byte-array) [ 0 @ ] keep ] ] bi
307     (( value -- c-ptr )) define-inline ;
308
309 : define-primitive-type ( c-type name -- )
310     [ typedef ]
311     [ name>> define-deref ]
312     [ name>> define-out ]
313     tri ;
314
315 : if-void ( c-type true false -- )
316     pick void? [ drop nip call ] [ nip call ] if ; inline
317
318 CONSTANT: primitive-types
319     {
320         char uchar
321         short ushort
322         int uint
323         long ulong
324         longlong ulonglong
325         float double
326         void* bool
327     }
328
329 SYMBOLS:
330     ptrdiff_t intptr_t uintptr_t size_t
331     char* uchar* ;
332
333 : 8-byte-alignment ( c-type -- c-type )
334     {
335         { [ cpu ppc? os macosx? and ] [ 4 >>align 8 >>align-first ] }
336         { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
337         [ 8 >>align 8 >>align-first ]
338     } cond ;
339
340 [
341     <c-type>
342         c-ptr >>class
343         c-ptr >>boxed-class
344         [ alien-cell ] >>getter
345         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
346         bootstrap-cell >>size
347         bootstrap-cell >>align
348         bootstrap-cell >>align-first
349         [ >c-ptr ] >>unboxer-quot
350         "box_alien" >>boxer
351         "alien_offset" >>unboxer
352     \ void* define-primitive-type
353
354     <long-long-type>
355         integer >>class
356         integer >>boxed-class
357         [ alien-signed-8 ] >>getter
358         [ set-alien-signed-8 ] >>setter
359         8 >>size
360         8-byte-alignment
361         "box_signed_8" >>boxer
362         "to_signed_8" >>unboxer
363     \ longlong define-primitive-type
364
365     <long-long-type>
366         integer >>class
367         integer >>boxed-class
368         [ alien-unsigned-8 ] >>getter
369         [ set-alien-unsigned-8 ] >>setter
370         8 >>size
371         8-byte-alignment
372         "box_unsigned_8" >>boxer
373         "to_unsigned_8" >>unboxer
374     \ ulonglong define-primitive-type
375
376     <c-type>
377         integer >>class
378         integer >>boxed-class
379         [ alien-signed-cell ] >>getter
380         [ set-alien-signed-cell ] >>setter
381         bootstrap-cell >>size
382         bootstrap-cell >>align
383         bootstrap-cell >>align-first
384         "box_signed_cell" >>boxer
385         "to_fixnum" >>unboxer
386     \ long define-primitive-type
387
388     <c-type>
389         integer >>class
390         integer >>boxed-class
391         [ alien-unsigned-cell ] >>getter
392         [ set-alien-unsigned-cell ] >>setter
393         bootstrap-cell >>size
394         bootstrap-cell >>align
395         bootstrap-cell >>align-first
396         "box_unsigned_cell" >>boxer
397         "to_cell" >>unboxer
398     \ ulong define-primitive-type
399
400     <c-type>
401         integer >>class
402         integer >>boxed-class
403         [ alien-signed-4 ] >>getter
404         [ set-alien-signed-4 ] >>setter
405         4 >>size
406         4 >>align
407         4 >>align-first
408         "box_signed_4" >>boxer
409         "to_fixnum" >>unboxer
410     \ int define-primitive-type
411
412     <c-type>
413         integer >>class
414         integer >>boxed-class
415         [ alien-unsigned-4 ] >>getter
416         [ set-alien-unsigned-4 ] >>setter
417         4 >>size
418         4 >>align
419         4 >>align-first
420         "box_unsigned_4" >>boxer
421         "to_cell" >>unboxer
422     \ uint define-primitive-type
423
424     <c-type>
425         fixnum >>class
426         fixnum >>boxed-class
427         [ alien-signed-2 ] >>getter
428         [ set-alien-signed-2 ] >>setter
429         2 >>size
430         2 >>align
431         2 >>align-first
432         "box_signed_2" >>boxer
433         "to_fixnum" >>unboxer
434     \ short define-primitive-type
435
436     <c-type>
437         fixnum >>class
438         fixnum >>boxed-class
439         [ alien-unsigned-2 ] >>getter
440         [ set-alien-unsigned-2 ] >>setter
441         2 >>size
442         2 >>align
443         2 >>align-first
444         "box_unsigned_2" >>boxer
445         "to_cell" >>unboxer
446     \ ushort define-primitive-type
447
448     <c-type>
449         fixnum >>class
450         fixnum >>boxed-class
451         [ alien-signed-1 ] >>getter
452         [ set-alien-signed-1 ] >>setter
453         1 >>size
454         1 >>align
455         1 >>align-first
456         "box_signed_1" >>boxer
457         "to_fixnum" >>unboxer
458     \ char define-primitive-type
459
460     <c-type>
461         fixnum >>class
462         fixnum >>boxed-class
463         [ alien-unsigned-1 ] >>getter
464         [ set-alien-unsigned-1 ] >>setter
465         1 >>size
466         1 >>align
467         1 >>align-first
468         "box_unsigned_1" >>boxer
469         "to_cell" >>unboxer
470     \ uchar define-primitive-type
471
472     cpu ppc? [
473         <c-type>
474             [ alien-unsigned-4 c-bool> ] >>getter
475             [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
476             4 >>size
477             4 >>align
478             4 >>align-first
479             "box_boolean" >>boxer
480             "to_boolean" >>unboxer
481     ] [
482         <c-type>
483             [ alien-unsigned-1 c-bool> ] >>getter
484             [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
485             1 >>size
486             1 >>align
487             1 >>align-first
488             "box_boolean" >>boxer
489             "to_boolean" >>unboxer
490     ] if
491     \ bool define-primitive-type
492
493     <c-type>
494         math:float >>class
495         math:float >>boxed-class
496         [ alien-float ] >>getter
497         [ [ >float ] 2dip set-alien-float ] >>setter
498         4 >>size
499         4 >>align
500         4 >>align-first
501         "box_float" >>boxer
502         "to_float" >>unboxer
503         float-rep >>rep
504         [ >float ] >>unboxer-quot
505     \ float define-primitive-type
506
507     <c-type>
508         math:float >>class
509         math:float >>boxed-class
510         [ alien-double ] >>getter
511         [ [ >float ] 2dip set-alien-double ] >>setter
512         8 >>size
513         8-byte-alignment
514         "box_double" >>boxer
515         "to_double" >>unboxer
516         double-rep >>rep
517         [ >float ] >>unboxer-quot
518     \ double define-primitive-type
519
520     cpu x86.64? os windows? and [
521         \ longlong c-type \ ptrdiff_t typedef
522         \ longlong c-type \ intptr_t typedef
523         \ ulonglong c-type \ uintptr_t typedef
524         \ ulonglong c-type \ size_t typedef
525     ] [
526         \ long c-type \ ptrdiff_t typedef
527         \ long c-type \ intptr_t typedef
528         \ ulong c-type \ uintptr_t typedef
529         \ ulong c-type \ size_t typedef
530     ] if
531 ] with-compilation-unit
532
533 M: char-16-rep rep-component-type drop char ;
534 M: uchar-16-rep rep-component-type drop uchar ;
535 M: short-8-rep rep-component-type drop short ;
536 M: ushort-8-rep rep-component-type drop ushort ;
537 M: int-4-rep rep-component-type drop int ;
538 M: uint-4-rep rep-component-type drop uint ;
539 M: longlong-2-rep rep-component-type drop longlong ;
540 M: ulonglong-2-rep rep-component-type drop ulonglong ;
541 M: float-4-rep rep-component-type drop float ;
542 M: double-2-rep rep-component-type drop double ;
543
544 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
545 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
546 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
547 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
548
549 : c-type-interval ( c-type -- from to )
550     {
551         { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
552         { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
553         { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
554     } cond ; foldable
555
556 : c-type-clamp ( value c-type -- value' )
557     dup { float double } member-eq?
558     [ drop ] [ c-type-interval clamp ] if ; inline