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