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