]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
Merge branch 'irc-fix' of git://tiodante.com/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
32 : <c-type> ( -- type )
33     \ c-type new ;
34
35 SYMBOL: c-types
36
37 global [
38     c-types [ H{ } assoc-like ] change
39 ] bind
40
41 ERROR: no-c-type name ;
42
43 : (c-type) ( name -- type/f )
44     c-types get-global at dup [
45         dup string? [ (c-type) ] when
46     ] when ;
47
48 ! C type protocol
49 GENERIC: c-type ( name -- type ) foldable
50
51 : resolve-pointer-type ( name -- name )
52     c-types get at dup string?
53     [ "*" append ] [ drop "void*" ] if
54     c-type ;
55
56 : resolve-typedef ( name -- type )
57     dup string? [ c-type ] when ;
58
59 : parse-array-type ( name -- array )
60     "[" split unclip
61     [ [ "]" ?tail drop string>number ] map ] dip prefix ;
62
63 M: string c-type ( name -- type )
64     CHAR: ] over member? [
65         parse-array-type
66     ] [
67         dup c-types get at [
68             resolve-typedef
69         ] [
70             "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if
71         ] ?if
72     ] if ;
73
74 GENERIC: c-struct? ( type -- ? )
75
76 M: object c-struct?
77     drop f ;
78 M: string c-struct?
79     dup "void" = [ drop f ] [ c-type c-struct? ] if ;
80
81 ! These words being foldable means that words need to be
82 ! recompiled if a C type is redefined. Even so, folding the
83 ! size facilitates some optimizations.
84 GENERIC: heap-size ( type -- size ) foldable
85
86 M: string heap-size c-type heap-size ;
87
88 M: abstract-c-type heap-size size>> ;
89
90 GENERIC: require-c-array ( c-type -- )
91
92 M: array require-c-array first require-c-array ;
93
94 GENERIC: c-array-constructor ( c-type -- word )
95
96 GENERIC: c-(array)-constructor ( c-type -- word )
97
98 GENERIC: c-direct-array-constructor ( c-type -- word )
99
100 GENERIC: <c-array> ( len c-type -- array )
101
102 M: string <c-array>
103     c-array-constructor execute( len -- array ) ; inline
104
105 GENERIC: (c-array) ( len c-type -- array )
106
107 M: string (c-array)
108     c-(array)-constructor execute( len -- array ) ; inline
109
110 GENERIC: <c-direct-array> ( alien len c-type -- array )
111
112 M: string <c-direct-array>
113     c-direct-array-constructor execute( alien len -- array ) ; inline
114
115 : malloc-array ( n type -- alien )
116     [ heap-size calloc ] [ <c-direct-array> ] 2bi ; inline
117
118 : (malloc-array) ( n type -- alien )
119     [ heap-size * malloc ] [ <c-direct-array> ] 2bi ; 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 GENERIC: stack-size ( type -- size ) foldable
220
221 M: string stack-size c-type stack-size ;
222
223 M: c-type stack-size size>> cell align ;
224
225 MIXIN: value-type
226
227 M: value-type c-type-rep drop int-rep ;
228
229 M: value-type c-type-getter
230     drop [ swap <displaced-alien> ] ;
231
232 M: value-type c-type-setter ( type -- quot )
233     [ c-type-getter ] [ c-type-unboxer-quot ] [ heap-size ] tri
234     '[ @ swap @ _ memcpy ] ;
235
236 GENERIC: byte-length ( seq -- n ) flushable
237
238 M: byte-array byte-length length ; inline
239
240 M: f byte-length drop 0 ; inline
241
242 : c-getter ( name -- quot )
243     c-type-getter [
244         [ "Cannot read struct fields with this type" throw ]
245     ] unless* ;
246
247 : c-type-getter-boxer ( name -- quot )
248     [ c-getter ] [ c-type-boxer-quot ] bi append ;
249
250 : c-setter ( name -- quot )
251     c-type-setter [
252         [ "Cannot write struct fields with this type" throw ]
253     ] unless* ;
254
255 : <c-object> ( type -- array )
256     heap-size <byte-array> ; inline
257
258 : (c-object) ( type -- array )
259     heap-size (byte-array) ; inline
260
261 : malloc-object ( type -- alien )
262     1 swap heap-size calloc ; inline
263
264 : (malloc-object) ( type -- alien )
265     heap-size malloc ; inline
266
267 : malloc-byte-array ( byte-array -- alien )
268     dup byte-length [ nip malloc dup ] 2keep memcpy ;
269
270 : memory>byte-array ( alien len -- byte-array )
271     [ nip (byte-array) dup ] 2keep memcpy ;
272
273 : malloc-string ( string encoding -- alien )
274     string>alien malloc-byte-array ;
275
276 M: memory-stream stream-read
277     [
278         [ index>> ] [ alien>> ] bi <displaced-alien>
279         swap memory>byte-array
280     ] [ [ + ] change-index drop ] 2bi ;
281
282 : byte-array>memory ( byte-array base -- )
283     swap dup byte-length memcpy ; inline
284
285 : array-accessor ( type quot -- def )
286     [
287         \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
288     ] [ ] make ;
289
290 : typedef ( old new -- ) c-types get set-at ;
291
292 TUPLE: long-long-type < c-type ;
293
294 : <long-long-type> ( -- type )
295     long-long-type new ;
296
297 M: long-long-type unbox-parameter ( n type -- )
298     c-type-unboxer %unbox-long-long ;
299
300 M: long-long-type unbox-return ( type -- )
301     f swap unbox-parameter ;
302
303 M: long-long-type box-parameter ( n type -- )
304     c-type-boxer %box-long-long ;
305
306 M: long-long-type box-return ( type -- )
307     f swap box-parameter ;
308
309 : define-deref ( name -- )
310     [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
311     (( c-ptr -- value )) define-inline ;
312
313 : define-out ( name -- )
314     [ "alien.c-types" constructor-word ]
315     [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ] bi
316     (( value -- c-ptr )) define-inline ;
317
318 : >c-bool ( ? -- int ) 1 0 ? ; inline
319
320 : c-bool> ( int -- ? ) 0 = not ; inline
321
322 : define-primitive-type ( type name -- )
323     [ typedef ]
324     [ define-deref ]
325     [ define-out ]
326     tri ;
327
328 : malloc-file-contents ( path -- alien len )
329     binary file-contents [ malloc-byte-array ] [ length ] bi ;
330
331 : if-void ( type true false -- )
332     pick "void" = [ drop nip call ] [ nip call ] if ; inline
333
334 CONSTANT: primitive-types
335     {
336         "char" "uchar"
337         "short" "ushort"
338         "int" "uint"
339         "long" "ulong"
340         "longlong" "ulonglong"
341         "float" "double"
342         "void*" "bool"
343     }
344
345 [
346     <c-type>
347         c-ptr >>class
348         c-ptr >>boxed-class
349         [ alien-cell ] >>getter
350         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
351         bootstrap-cell >>size
352         bootstrap-cell >>align
353         [ >c-ptr ] >>unboxer-quot
354         "box_alien" >>boxer
355         "alien_offset" >>unboxer
356     "void*" define-primitive-type
357
358     <long-long-type>
359         integer >>class
360         integer >>boxed-class
361         [ alien-signed-8 ] >>getter
362         [ set-alien-signed-8 ] >>setter
363         8 >>size
364         8 >>align
365         "box_signed_8" >>boxer
366         "to_signed_8" >>unboxer
367     "longlong" define-primitive-type
368
369     <long-long-type>
370         integer >>class
371         integer >>boxed-class
372         [ alien-unsigned-8 ] >>getter
373         [ set-alien-unsigned-8 ] >>setter
374         8 >>size
375         8 >>align
376         "box_unsigned_8" >>boxer
377         "to_unsigned_8" >>unboxer
378     "ulonglong" define-primitive-type
379
380     <c-type>
381         integer >>class
382         integer >>boxed-class
383         [ alien-signed-cell ] >>getter
384         [ set-alien-signed-cell ] >>setter
385         bootstrap-cell >>size
386         bootstrap-cell >>align
387         "box_signed_cell" >>boxer
388         "to_fixnum" >>unboxer
389     "long" define-primitive-type
390
391     <c-type>
392         integer >>class
393         integer >>boxed-class
394         [ alien-unsigned-cell ] >>getter
395         [ set-alien-unsigned-cell ] >>setter
396         bootstrap-cell >>size
397         bootstrap-cell >>align
398         "box_unsigned_cell" >>boxer
399         "to_cell" >>unboxer
400     "ulong" define-primitive-type
401
402     <c-type>
403         integer >>class
404         integer >>boxed-class
405         [ alien-signed-4 ] >>getter
406         [ set-alien-signed-4 ] >>setter
407         4 >>size
408         4 >>align
409         "box_signed_4" >>boxer
410         "to_fixnum" >>unboxer
411     "int" define-primitive-type
412
413     <c-type>
414         integer >>class
415         integer >>boxed-class
416         [ alien-unsigned-4 ] >>getter
417         [ set-alien-unsigned-4 ] >>setter
418         4 >>size
419         4 >>align
420         "box_unsigned_4" >>boxer
421         "to_cell" >>unboxer
422     "uint" define-primitive-type
423
424     <c-type>
425         fixnum >>class
426         fixnum >>boxed-class
427         [ alien-signed-2 ] >>getter
428         [ set-alien-signed-2 ] >>setter
429         2 >>size
430         2 >>align
431         "box_signed_2" >>boxer
432         "to_fixnum" >>unboxer
433     "short" define-primitive-type
434
435     <c-type>
436         fixnum >>class
437         fixnum >>boxed-class
438         [ alien-unsigned-2 ] >>getter
439         [ set-alien-unsigned-2 ] >>setter
440         2 >>size
441         2 >>align
442         "box_unsigned_2" >>boxer
443         "to_cell" >>unboxer
444     "ushort" define-primitive-type
445
446     <c-type>
447         fixnum >>class
448         fixnum >>boxed-class
449         [ alien-signed-1 ] >>getter
450         [ set-alien-signed-1 ] >>setter
451         1 >>size
452         1 >>align
453         "box_signed_1" >>boxer
454         "to_fixnum" >>unboxer
455     "char" define-primitive-type
456
457     <c-type>
458         fixnum >>class
459         fixnum >>boxed-class
460         [ alien-unsigned-1 ] >>getter
461         [ set-alien-unsigned-1 ] >>setter
462         1 >>size
463         1 >>align
464         "box_unsigned_1" >>boxer
465         "to_cell" >>unboxer
466     "uchar" define-primitive-type
467
468     <c-type>
469         [ alien-unsigned-1 c-bool> ] >>getter
470         [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
471         1 >>size
472         1 >>align
473         "box_boolean" >>boxer
474         "to_boolean" >>unboxer
475     "bool" define-primitive-type
476
477     <c-type>
478         float >>class
479         float >>boxed-class
480         [ alien-float ] >>getter
481         [ [ >float ] 2dip set-alien-float ] >>setter
482         4 >>size
483         4 >>align
484         "box_float" >>boxer
485         "to_float" >>unboxer
486         float-rep >>rep
487         [ >float ] >>unboxer-quot
488     "float" define-primitive-type
489
490     <c-type>
491         float >>class
492         float >>boxed-class
493         [ alien-double ] >>getter
494         [ [ >float ] 2dip set-alien-double ] >>setter
495         8 >>size
496         8 >>align
497         "box_double" >>boxer
498         "to_double" >>unboxer
499         double-rep >>rep
500         [ >float ] >>unboxer-quot
501     "double" define-primitive-type
502
503     "long" "ptrdiff_t" typedef
504     "long" "intptr_t" typedef
505     "ulong" "size_t" typedef
506 ] with-compilation-unit
507