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