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