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