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