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