]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
Move platform-specific c-type initialization out of cpu.* vocabularies and into alien...
[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 : >c-bool ( ? -- int ) 1 0 ? ; inline
234
235 : c-bool> ( int -- ? ) 0 = not ; inline
236
237 MIXIN: value-type
238
239 : c-getter ( name -- quot )
240     c-type-getter [
241         [ "Cannot read struct fields with this type" throw ]
242     ] unless* ;
243
244 : c-type-getter-boxer ( name -- quot )
245     [ c-getter ] [ c-type-boxer-quot ] bi append ;
246
247 : c-setter ( name -- quot )
248     c-type-setter [
249         [ "Cannot write struct fields with this type" throw ]
250     ] unless* ;
251
252 : array-accessor ( c-type quot -- def )
253     [
254         \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
255     ] [ ] make ;
256
257 GENERIC: typedef ( old new -- )
258
259 PREDICATE: typedef-word < c-type-word
260     "c-type" word-prop c-type-name? ;
261
262 M: string typedef ( old new -- ) c-types get set-at ;
263
264 M: word typedef ( old new -- )
265     {
266         [ nip define-symbol ]
267         [ name>> typedef ]
268         [ swap "c-type" set-word-prop ]
269         [
270             swap dup c-type-name? [
271                 resolve-pointer-type
272                 "pointer-c-type" set-word-prop
273             ] [ 2drop ] if
274         ]
275     } 2cleave ;
276
277 TUPLE: long-long-type < c-type ;
278
279 : <long-long-type> ( -- c-type )
280     long-long-type new ;
281
282 M: long-long-type unbox-parameter ( n c-type -- )
283     c-type-unboxer %unbox-long-long ;
284
285 M: long-long-type unbox-return ( c-type -- )
286     f swap unbox-parameter ;
287
288 M: long-long-type box-parameter ( n c-type -- )
289     c-type-boxer %box-long-long ;
290
291 M: long-long-type box-return ( c-type -- )
292     f swap box-parameter ;
293
294 : define-deref ( name -- )
295     [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
296     (( c-ptr -- value )) define-inline ;
297
298 : define-out ( name -- )
299     [ "alien.c-types" constructor-word ]
300     [ dup c-setter '[ _ heap-size (byte-array) [ 0 @ ] keep ] ] bi
301     (( value -- c-ptr )) define-inline ;
302
303 : define-primitive-type ( c-type name -- )
304     [ typedef ]
305     [ name>> define-deref ]
306     [ name>> define-out ]
307     tri ;
308
309 : if-void ( c-type true false -- )
310     pick void? [ drop nip call ] [ nip call ] if ; inline
311
312 CONSTANT: primitive-types
313     {
314         char uchar
315         short ushort
316         int uint
317         long ulong
318         longlong ulonglong
319         float double
320         void* bool
321     }
322
323 SYMBOLS:
324     ptrdiff_t intptr_t uintptr_t size_t
325     char* uchar* ;
326
327 [
328     <c-type>
329         c-ptr >>class
330         c-ptr >>boxed-class
331         [ alien-cell ] >>getter
332         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
333         bootstrap-cell >>size
334         bootstrap-cell >>align
335         [ >c-ptr ] >>unboxer-quot
336         "box_alien" >>boxer
337         "alien_offset" >>unboxer
338     \ void* define-primitive-type
339
340     <long-long-type>
341         integer >>class
342         integer >>boxed-class
343         [ alien-signed-8 ] >>getter
344         [ set-alien-signed-8 ] >>setter
345         8 >>size
346         cpu x86.32? os windows? not and 4 8 ? >>align
347         "box_signed_8" >>boxer
348         "to_signed_8" >>unboxer
349     \ longlong define-primitive-type
350
351     <long-long-type>
352         integer >>class
353         integer >>boxed-class
354         [ alien-unsigned-8 ] >>getter
355         [ set-alien-unsigned-8 ] >>setter
356         8 >>size
357         cpu x86.32? os windows? not and 4 8 ? >>align
358         "box_unsigned_8" >>boxer
359         "to_unsigned_8" >>unboxer
360     \ ulonglong define-primitive-type
361
362     <c-type>
363         integer >>class
364         integer >>boxed-class
365         [ alien-signed-cell ] >>getter
366         [ set-alien-signed-cell ] >>setter
367         bootstrap-cell >>size
368         bootstrap-cell >>align
369         "box_signed_cell" >>boxer
370         "to_fixnum" >>unboxer
371     \ long define-primitive-type
372
373     <c-type>
374         integer >>class
375         integer >>boxed-class
376         [ alien-unsigned-cell ] >>getter
377         [ set-alien-unsigned-cell ] >>setter
378         bootstrap-cell >>size
379         bootstrap-cell >>align
380         "box_unsigned_cell" >>boxer
381         "to_cell" >>unboxer
382     \ ulong define-primitive-type
383
384     <c-type>
385         integer >>class
386         integer >>boxed-class
387         [ alien-signed-4 ] >>getter
388         [ set-alien-signed-4 ] >>setter
389         4 >>size
390         4 >>align
391         "box_signed_4" >>boxer
392         "to_fixnum" >>unboxer
393     \ int define-primitive-type
394
395     <c-type>
396         integer >>class
397         integer >>boxed-class
398         [ alien-unsigned-4 ] >>getter
399         [ set-alien-unsigned-4 ] >>setter
400         4 >>size
401         4 >>align
402         "box_unsigned_4" >>boxer
403         "to_cell" >>unboxer
404     \ uint define-primitive-type
405
406     <c-type>
407         fixnum >>class
408         fixnum >>boxed-class
409         [ alien-signed-2 ] >>getter
410         [ set-alien-signed-2 ] >>setter
411         2 >>size
412         2 >>align
413         "box_signed_2" >>boxer
414         "to_fixnum" >>unboxer
415     \ short define-primitive-type
416
417     <c-type>
418         fixnum >>class
419         fixnum >>boxed-class
420         [ alien-unsigned-2 ] >>getter
421         [ set-alien-unsigned-2 ] >>setter
422         2 >>size
423         2 >>align
424         "box_unsigned_2" >>boxer
425         "to_cell" >>unboxer
426     \ ushort define-primitive-type
427
428     <c-type>
429         fixnum >>class
430         fixnum >>boxed-class
431         [ alien-signed-1 ] >>getter
432         [ set-alien-signed-1 ] >>setter
433         1 >>size
434         1 >>align
435         "box_signed_1" >>boxer
436         "to_fixnum" >>unboxer
437     \ char define-primitive-type
438
439     <c-type>
440         fixnum >>class
441         fixnum >>boxed-class
442         [ alien-unsigned-1 ] >>getter
443         [ set-alien-unsigned-1 ] >>setter
444         1 >>size
445         1 >>align
446         "box_unsigned_1" >>boxer
447         "to_cell" >>unboxer
448     \ uchar define-primitive-type
449
450     cpu ppc? [
451         <c-type>
452             [ alien-unsigned-4 c-bool> ] >>getter
453             [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
454             4 >>size
455             4 >>align
456             "box_boolean" >>boxer
457             "to_boolean" >>unboxer
458     ] [
459         <c-type>
460             [ alien-unsigned-1 c-bool> ] >>getter
461             [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
462             1 >>size
463             1 >>align
464             "box_boolean" >>boxer
465             "to_boolean" >>unboxer
466         \ bool define-primitive-type
467     ] if
468
469     <c-type>
470         math:float >>class
471         math:float >>boxed-class
472         [ alien-float ] >>getter
473         [ [ >float ] 2dip set-alien-float ] >>setter
474         4 >>size
475         4 >>align
476         "box_float" >>boxer
477         "to_float" >>unboxer
478         float-rep >>rep
479         [ >float ] >>unboxer-quot
480     \ float define-primitive-type
481
482     <c-type>
483         math:float >>class
484         math:float >>boxed-class
485         [ alien-double ] >>getter
486         [ [ >float ] 2dip set-alien-double ] >>setter
487         8 >>size
488         cpu x86.32? os windows? not and 4 8 ? >>align
489         "box_double" >>boxer
490         "to_double" >>unboxer
491         double-rep >>rep
492         [ >float ] >>unboxer-quot
493     \ double define-primitive-type
494
495     cpu x86.64? os windows? and [
496         \ longlong c-type \ ptrdiff_t typedef
497         \ longlong c-type \ intptr_t typedef
498         \ ulonglong c-type \ uintptr_t typedef
499         \ ulonglong c-type \ size_t typedef
500     ] [
501         \ long c-type \ ptrdiff_t typedef
502         \ long c-type \ intptr_t typedef
503         \ ulong c-type \ uintptr_t typedef
504         \ ulong c-type \ size_t typedef
505     ] if
506 ] with-compilation-unit
507
508 M: char-16-rep rep-component-type drop char ;
509 M: uchar-16-rep rep-component-type drop uchar ;
510 M: short-8-rep rep-component-type drop short ;
511 M: ushort-8-rep rep-component-type drop ushort ;
512 M: int-4-rep rep-component-type drop int ;
513 M: uint-4-rep rep-component-type drop uint ;
514 M: longlong-2-rep rep-component-type drop longlong ;
515 M: ulonglong-2-rep rep-component-type drop ulonglong ;
516 M: float-4-rep rep-component-type drop float ;
517 M: double-2-rep rep-component-type drop double ;
518
519 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
520 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
521 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
522 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
523
524 : c-type-interval ( c-type -- from to )
525     {
526         { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
527         { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
528         { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
529     } cond ; foldable
530
531 : c-type-clamp ( value c-type -- value' ) c-type-interval clamp ; inline