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