]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
Remove <uint> *uint and friends. Hopefully remove the last usages of these words
[factor.git] / basis / alien / c-types / c-types.factor
1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.accessors arrays byte-arrays
4 classes combinators compiler.units cpu.architecture delegate
5 fry kernel layouts locals macros math math.order quotations
6 sequences system words words.symbol ;
7 QUALIFIED: math
8 IN: alien.c-types
9
10 SYMBOLS:
11     char uchar
12     short ushort
13     int uint
14     long ulong
15     longlong ulonglong
16     float double
17     void* bool ;
18
19 SINGLETON: void
20
21 DEFER: <ref>
22 DEFER: deref
23
24 TUPLE: abstract-c-type
25 { class class initial: object }
26 { boxed-class class initial: object }
27 { boxer-quot callable }
28 { unboxer-quot callable }
29 { getter callable }
30 { setter callable }
31 { size integer }
32 { align integer }
33 { align-first integer } ;
34
35 TUPLE: c-type < abstract-c-type
36 boxer
37 unboxer
38 { rep initial: int-rep } ;
39
40 : <c-type> ( -- c-type )
41     \ c-type new ; inline
42
43 ERROR: no-c-type name ;
44
45 ! C type protocol
46 GENERIC: c-type ( name -- c-type ) foldable
47
48 PREDICATE: c-type-word < word
49     "c-type" word-prop ;
50
51 TUPLE: pointer { to initial: void read-only } ;
52 C: <pointer> pointer
53
54 UNION: c-type-name
55     c-type-word pointer ;
56
57 : resolve-typedef ( name -- c-type )
58     dup void? [ no-c-type ] when
59     dup c-type-name? [ c-type ] when ;
60
61 M: word c-type
62     dup "c-type" word-prop resolve-typedef
63     [ ] [ no-c-type ] ?if ;
64
65 GENERIC: c-type-class ( name -- class )
66
67 M: abstract-c-type c-type-class class>> ;
68
69 GENERIC: c-type-boxed-class ( name -- class )
70
71 M: abstract-c-type c-type-boxed-class boxed-class>> ;
72
73 GENERIC: c-type-boxer-quot ( name -- quot )
74
75 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
76
77 GENERIC: c-type-unboxer-quot ( name -- quot )
78
79 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
80
81 GENERIC: c-type-rep ( name -- rep )
82
83 M: c-type c-type-rep rep>> ;
84
85 GENERIC: c-type-getter ( name -- quot )
86
87 M: c-type c-type-getter getter>> ;
88
89 GENERIC: c-type-copier ( name -- quot )
90
91 M: c-type c-type-copier drop [ ] ;
92
93 GENERIC: c-type-setter ( name -- quot )
94
95 M: c-type c-type-setter setter>> ;
96
97 GENERIC: c-type-align ( name -- n ) foldable
98
99 M: abstract-c-type c-type-align align>> ;
100
101 GENERIC: c-type-align-first ( name -- n )
102
103 M: abstract-c-type c-type-align-first align-first>> ;
104
105 GENERIC: base-type ( c-type -- c-type )
106
107 M: c-type-name base-type c-type ;
108
109 M: c-type base-type ;
110
111 : little-endian? ( -- ? ) 1 int <ref> char deref 1 = ; foldable
112
113 GENERIC: heap-size ( name -- size )
114
115 M: abstract-c-type heap-size size>> ;
116
117 MIXIN: value-type
118
119 MACRO: alien-value ( c-type -- quot: ( c-ptr offset -- value ) )
120     [ c-type-getter ] [ c-type-boxer-quot ] bi append ;
121
122 MACRO: alien-copy-value ( c-type -- quot: ( c-ptr offset -- value ) )
123     [ c-type-getter ] [ c-type-copier ] [ c-type-boxer-quot ] tri 3append ;
124
125 MACRO: set-alien-value ( c-type -- quot: ( value c-ptr offset -- ) )
126     [ c-type-unboxer-quot [ [ ] ] [ '[ _ 2dip ] ] if-empty ]
127     [ c-type-setter ]
128     bi append ;
129
130 : array-accessor ( n c-ptr c-type -- c-ptr offset c-type )
131     [ swapd heap-size * >fixnum ] keep ; inline
132
133 : alien-element ( n c-ptr c-type -- value )
134     array-accessor alien-value ; inline
135
136 : set-alien-element ( value n c-ptr c-type -- )
137     array-accessor set-alien-value ; inline
138
139 PROTOCOL: c-type-protocol 
140     c-type-class
141     c-type-boxed-class
142     c-type-boxer-quot
143     c-type-unboxer-quot
144     c-type-rep
145     c-type-getter
146     c-type-copier
147     c-type-setter
148     c-type-align
149     c-type-align-first
150     base-type
151     heap-size ;
152
153 CONSULT: c-type-protocol c-type-name
154     c-type ;
155
156 PREDICATE: typedef-word < c-type-word
157     "c-type" word-prop [ c-type-name? ] [ array? ] bi or ;
158
159 : typedef ( old new -- )
160     {
161         [ nip define-symbol ]
162         [ swap "c-type" set-word-prop ]
163     } 2cleave ;
164
165 TUPLE: long-long-type < c-type ;
166
167 : <long-long-type> ( -- c-type )
168     long-long-type new ;
169
170 : if-void ( c-type true false -- )
171     pick void? [ drop nip call ] [ nip call ] if ; inline
172
173 SYMBOLS:
174     ptrdiff_t intptr_t uintptr_t size_t
175     c-string ;
176
177 CONSTANT: primitive-types
178     {
179         char uchar
180         short ushort
181         int uint
182         long ulong
183         longlong ulonglong
184         float double
185         void* bool
186         c-string
187     }
188
189 : >c-bool ( ? -- int ) 1 0 ? ; inline
190
191 : c-bool> ( int -- ? ) 0 = not ; inline
192
193 <PRIVATE
194
195 : 8-byte-alignment ( c-type -- c-type )
196     {
197         { [ cpu ppc? os macosx? and ] [ 4 >>align 8 >>align-first ] }
198         { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
199         [ 8 >>align 8 >>align-first ]
200     } cond ;
201
202 : resolve-pointer-typedef ( type -- base-type )
203     dup "c-type" word-prop dup word?
204     [ nip resolve-pointer-typedef ] [
205         pointer? [ drop void* ] when
206     ] if ;
207
208 : primitive-pointer-type? ( type -- ? )
209     dup c-type-word? [
210         resolve-pointer-typedef [ void? ] [ primitive-types member? ] bi or
211     ] [ drop t ] if ;
212
213 : (pointer-c-type) ( void* type -- void*' )
214     [ clone ] dip c-type-boxer-quot '[ _ [ f ] if* ] >>boxer-quot ;
215
216 PRIVATE>
217
218 M: pointer c-type
219     [ \ void* c-type ] dip
220     to>> dup primitive-pointer-type? [ drop ] [ (pointer-c-type) ] if ;
221
222 [
223     <c-type>
224         c-ptr >>class
225         c-ptr >>boxed-class
226         [ alien-cell ] >>getter
227         [ set-alien-cell ] >>setter
228         bootstrap-cell >>size
229         bootstrap-cell >>align
230         bootstrap-cell >>align-first
231         [ >c-ptr ] >>unboxer-quot
232         "allot_alien" >>boxer
233         "alien_offset" >>unboxer
234     \ void* typedef
235
236     <c-type>
237         fixnum >>class
238         fixnum >>boxed-class
239         [ alien-signed-2 ] >>getter
240         [ set-alien-signed-2 ] >>setter
241         2 >>size
242         2 >>align
243         2 >>align-first
244         "from_signed_2" >>boxer
245         "to_signed_2" >>unboxer
246         [ >fixnum ] >>unboxer-quot
247     \ short typedef
248
249     <c-type>
250         fixnum >>class
251         fixnum >>boxed-class
252         [ alien-unsigned-2 ] >>getter
253         [ set-alien-unsigned-2 ] >>setter
254         2 >>size
255         2 >>align
256         2 >>align-first
257         "from_unsigned_2" >>boxer
258         "to_unsigned_2" >>unboxer
259         [ >fixnum ] >>unboxer-quot
260     \ ushort typedef
261
262     <c-type>
263         fixnum >>class
264         fixnum >>boxed-class
265         [ alien-signed-1 ] >>getter
266         [ set-alien-signed-1 ] >>setter
267         1 >>size
268         1 >>align
269         1 >>align-first
270         "from_signed_1" >>boxer
271         "to_signed_1" >>unboxer
272         [ >fixnum ] >>unboxer-quot
273     \ char typedef
274
275     <c-type>
276         fixnum >>class
277         fixnum >>boxed-class
278         [ alien-unsigned-1 ] >>getter
279         [ set-alien-unsigned-1 ] >>setter
280         1 >>size
281         1 >>align
282         1 >>align-first
283         "from_unsigned_1" >>boxer
284         "to_unsigned_1" >>unboxer
285         [ >fixnum ] >>unboxer-quot
286     \ uchar typedef
287
288     <c-type>
289         math:float >>class
290         math:float >>boxed-class
291         [ alien-float ] >>getter
292         [ set-alien-float ] >>setter
293         4 >>size
294         4 >>align
295         4 >>align-first
296         "from_float" >>boxer
297         "to_float" >>unboxer
298         float-rep >>rep
299         [ >float ] >>unboxer-quot
300     \ float typedef
301
302     <c-type>
303         math:float >>class
304         math:float >>boxed-class
305         [ alien-double ] >>getter
306         [ set-alien-double ] >>setter
307         8 >>size
308         8-byte-alignment
309         "from_double" >>boxer
310         "to_double" >>unboxer
311         double-rep >>rep
312         [ >float ] >>unboxer-quot
313     \ double typedef
314
315     cell 8 = [
316         <c-type>
317             fixnum >>class
318             fixnum >>boxed-class
319             [ alien-signed-4 ] >>getter
320             [ set-alien-signed-4 ] >>setter
321             4 >>size
322             4 >>align
323             4 >>align-first
324             "from_signed_4" >>boxer
325             "to_signed_4" >>unboxer
326             [ >fixnum ] >>unboxer-quot
327         \ int typedef
328     
329         <c-type>
330             fixnum >>class
331             fixnum >>boxed-class
332             [ alien-unsigned-4 ] >>getter
333             [ set-alien-unsigned-4 ] >>setter
334             4 >>size
335             4 >>align
336             4 >>align-first
337             "from_unsigned_4" >>boxer
338             "to_unsigned_4" >>unboxer
339             [ >fixnum ] >>unboxer-quot
340         \ uint typedef
341
342         <c-type>
343             integer >>class
344             integer >>boxed-class
345             [ alien-signed-cell ] >>getter
346             [ set-alien-signed-cell ] >>setter
347             8 >>size
348             8 >>align
349             8 >>align-first
350             "from_signed_cell" >>boxer
351             "to_fixnum" >>unboxer
352             [ >integer ] >>unboxer-quot
353         \ longlong typedef
354
355         <c-type>
356             integer >>class
357             integer >>boxed-class
358             [ alien-unsigned-cell ] >>getter
359             [ set-alien-unsigned-cell ] >>setter
360             8 >>size
361             8 >>align
362             8 >>align-first
363             "from_unsigned_cell" >>boxer
364             "to_cell" >>unboxer
365             [ >integer ] >>unboxer-quot
366         \ ulonglong typedef
367
368         os windows? [
369             \ int c-type \ long typedef
370             \ uint c-type \ ulong typedef
371         ] [
372             \ longlong c-type \ long typedef
373             \ ulonglong c-type \ ulong typedef
374         ] if
375
376         \ longlong c-type \ ptrdiff_t typedef
377         \ longlong c-type \ intptr_t typedef
378
379         \ ulonglong c-type \ uintptr_t typedef
380         \ ulonglong c-type \ size_t typedef
381     ] [
382         <c-type>
383             integer >>class
384             integer >>boxed-class
385             [ alien-signed-cell ] >>getter
386             [ set-alien-signed-cell ] >>setter
387             4 >>size
388             4 >>align
389             4 >>align-first
390             "from_signed_cell" >>boxer
391             "to_fixnum" >>unboxer
392             [ >integer ] >>unboxer-quot
393         \ int typedef
394     
395         <c-type>
396             integer >>class
397             integer >>boxed-class
398             [ alien-unsigned-cell ] >>getter
399             [ set-alien-unsigned-cell ] >>setter
400             4 >>size
401             4 >>align
402             4 >>align-first
403             "from_unsigned_cell" >>boxer
404             "to_cell" >>unboxer
405             [ >integer ] >>unboxer-quot
406         \ uint typedef
407
408         <long-long-type>
409             integer >>class
410             integer >>boxed-class
411             [ alien-signed-8 ] >>getter
412             [ set-alien-signed-8 ] >>setter
413             8 >>size
414             8-byte-alignment
415             "from_signed_8" >>boxer
416             "to_signed_8" >>unboxer
417             [ >integer ] >>unboxer-quot
418         \ longlong typedef
419
420         <long-long-type>
421             integer >>class
422             integer >>boxed-class
423             [ alien-unsigned-8 ] >>getter
424             [ set-alien-unsigned-8 ] >>setter
425             8 >>size
426             8-byte-alignment
427             "from_unsigned_8" >>boxer
428             "to_unsigned_8" >>unboxer
429             [ >integer ] >>unboxer-quot
430         \ ulonglong typedef
431
432         \ int c-type \ long typedef
433         \ uint c-type \ ulong typedef
434
435         \ int c-type \ ptrdiff_t typedef
436         \ int c-type \ intptr_t typedef
437
438         \ uint c-type \ uintptr_t typedef
439         \ uint c-type \ size_t typedef
440     ] if
441
442     cpu ppc? \ uint \ uchar ? c-type clone
443         [ >c-bool ] >>unboxer-quot
444         [ c-bool> ] >>boxer-quot
445         object >>boxed-class
446     \ bool typedef
447
448 ] with-compilation-unit
449
450 M: char-16-rep rep-component-type drop char ;
451 M: uchar-16-rep rep-component-type drop uchar ;
452 M: short-8-rep rep-component-type drop short ;
453 M: ushort-8-rep rep-component-type drop ushort ;
454 M: int-4-rep rep-component-type drop int ;
455 M: uint-4-rep rep-component-type drop uint ;
456 M: longlong-2-rep rep-component-type drop longlong ;
457 M: ulonglong-2-rep rep-component-type drop ulonglong ;
458 M: float-4-rep rep-component-type drop float ;
459 M: double-2-rep rep-component-type drop double ;
460
461 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
462 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
463 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
464 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
465
466 : c-type-interval ( c-type -- from to )
467     {
468         { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
469         { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
470         { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
471     } cond ; foldable
472
473 : c-type-clamp ( value c-type -- value' )
474     dup { float double } member-eq?
475     [ drop ] [ c-type-interval clamp ] if ; inline
476
477 :: <ref> ( value c-type -- c-ptr )
478     c-type heap-size <byte-array> :> c-ptr
479     value c-ptr 0 c-type set-alien-value
480     c-ptr ; inline
481
482 : deref ( c-ptr c-type -- value )
483     [ 0 ] dip alien-value ; inline