]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
Rename c-type to lookup-c-type. Fixes #230.
[factor.git] / basis / classes / struct / struct.factor
1 ! Copyright (C) 2010, 2011 Joe Groff, Daniel Ehrenberg,
2 ! John Benediktsson, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors alien alien.c-types alien.data alien.parser
5 arrays byte-arrays classes classes.parser
6 classes.struct.bit-accessors classes.tuple classes.tuple.parser
7 combinators combinators.smart cpu.architecture fry
8 functors.backend generalizations generic.parser kernel
9 kernel.private lexer libc locals macros math math.order parser
10 quotations sequences slots slots.private specialized-arrays
11 stack-checker.dependencies summary vectors vocabs.parser words
12 classes.private generic definitions ;
13 FROM: delegate.private => group-words slot-group-words ;
14 QUALIFIED: math
15 IN: classes.struct
16
17 SPECIALIZED-ARRAY: uchar
18
19 ERROR: struct-must-have-slots ;
20
21 M: struct-must-have-slots summary
22     drop "Struct definitions must have slots" ;
23
24 TUPLE: struct
25     { (underlying) c-ptr read-only } ;
26
27 ! We hijack the core slots vocab's slot-spec type for struct
28 ! fields. Note that 'offset' is in bits, not bytes, to support
29 ! bitfields.
30 TUPLE: struct-slot-spec < slot-spec
31     type packed? ;
32
33 ! For a struct-bit-slot-spec, offset is in bits, not bytes
34 TUPLE: struct-bit-slot-spec < struct-slot-spec
35     bits signed? ;
36
37 PREDICATE: struct-class < tuple-class
38     superclass \ struct eq? ;
39
40 SLOT: fields
41
42 : struct-slots ( struct-class -- slots )
43     "c-type" word-prop fields>> ;
44
45 M: struct-class group-words
46     struct-slots slot-group-words ;
47
48 ! struct allocation
49
50 M: struct >c-ptr
51     2 slot { c-ptr } declare ; inline
52
53 M: struct equal?
54     over struct? [
55         2dup [ class-of ] bi@ = [
56             2dup [ >c-ptr ] both?
57             [ [ >c-ptr ] [ binary-object ] bi* memory= ]
58             [ [ >c-ptr not ] both? ]
59             if
60         ] [ 2drop f ] if
61     ] [ 2drop f ] if ; inline
62
63 M: struct hashcode*
64     binary-object over
65     [ uchar <c-direct-array> hashcode* ] [ 3drop 0 ] if ; inline
66
67 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
68
69 : memory>struct ( ptr class -- struct )
70     ! This is sub-optimal if the class is not literal, but gets
71     ! optimized down to efficient code if it is.
72     '[ _ boa ] call( ptr -- struct ) ; inline
73
74 <PRIVATE
75 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
76     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
77 PRIVATE>
78
79 : (malloc-struct) ( class -- struct )
80     [ heap-size malloc ] keep memory>struct ; inline
81
82 : malloc-struct ( class -- struct )
83     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
84
85 : (struct) ( class -- struct )
86     [ heap-size (byte-array) ] keep memory>struct ; inline
87
88 : <struct> ( class -- struct )
89     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
90
91 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
92     [
93         [ <wrapper> \ (struct) [ ] 2sequence ]
94         [
95             struct-slots
96             [ length \ ndip ]
97             [ [ name>> setter-word 1quotation ] map \ spread ] bi
98         ] bi
99     ] [ ] output>sequence ;
100
101 <PRIVATE
102 : pad-struct-slots ( values class -- values' class )
103     [ struct-slots [ initial>> ] map over length tail append ] keep ;
104
105 : sign-extend ( n bits -- n' )
106     ! formula from:
107     ! http://guru.multimedia.cx/fast-sign-extension/
108     1 - -1 swap shift [ + ] keep bitxor ; inline
109
110 : sign-extender ( signed? bits -- quot )
111     '[ _ [ _ sign-extend ] when ] ;
112
113 GENERIC: (reader-quot) ( slot -- quot )
114
115 M: struct-slot-spec (reader-quot)
116     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
117
118 M: struct-bit-slot-spec (reader-quot)
119     [ [ offset>> ] [ bits>> ] bi bit-reader ]
120     [ [ signed?>> ] [ bits>> ] bi sign-extender ]
121     bi compose
122     [ >c-ptr ] prepose ;
123
124 GENERIC: (writer-quot) ( slot -- quot )
125
126 M: struct-slot-spec (writer-quot)
127     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
128
129 M: struct-bit-slot-spec (writer-quot)
130     [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
131
132 : (boxer-quot) ( class -- quot )
133     '[ _ memory>struct ] ;
134
135 : (unboxer-quot) ( class -- quot )
136     drop [ >c-ptr ] ;
137
138 MACRO: read-struct-slot ( slot -- )
139     dup type>> depends-on-c-type
140     (reader-quot) ;
141
142 MACRO: write-struct-slot ( slot -- )
143     dup type>> depends-on-c-type
144     (writer-quot) ;
145 PRIVATE>
146
147 M: struct-class boa>object
148     swap pad-struct-slots
149     [ <struct> ] [ struct-slots ] bi
150     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
151
152 M: struct-class initial-value* <struct> ; inline
153
154 ! Struct slot accessors
155
156 GENERIC: struct-slot-values ( struct -- sequence )
157
158 M: struct-class reader-quot
159     dup type>> array? [ dup type>> first define-array-vocab drop ] when
160     nip '[ _ read-struct-slot ] ;
161
162 M: struct-class writer-quot
163     nip '[ _ write-struct-slot ] ;
164
165 : offset-of ( field struct -- offset )
166     struct-slots slot-named offset>> ; inline
167
168 ! c-types
169
170 TUPLE: struct-c-type < abstract-c-type
171     fields
172     return-in-registers? ;
173
174 INSTANCE: struct-c-type value-type
175
176 ! M: struct-c-type c-type ;
177 M: struct-c-type lookup-c-type ;
178
179 M: struct-c-type base-type ;
180
181 : large-struct? ( type -- ? )
182     {
183         { [ dup void? ] [ drop f ] }
184         { [ dup base-type struct-c-type? not ] [ drop f ] }
185         [ return-struct-in-registers? not ]
186     } cond ;
187
188 <PRIVATE
189 : struct-slot-values-quot ( class -- quot )
190     struct-slots
191     [ name>> reader-word 1quotation ] map
192     \ cleave [ ] 2sequence
193     \ output>array [ ] 2sequence ;
194
195 : (define-struct-slot-values-method) ( class -- )
196     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
197     define-inline-method ;
198
199 : forget-struct-slot-values-method ( class -- )
200     \ struct-slot-values ?lookup-method forget ;
201
202 : clone-underlying ( struct -- byte-array )
203     binary-object memory>byte-array ; inline
204
205 : (define-clone-method) ( class -- )
206     [ \ clone ]
207     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
208     define-inline-method ;
209
210 : forget-clone-method ( class -- )
211     \ clone ?lookup-method forget ;
212
213 :: c-type-for-class ( class slots size align -- c-type )
214     struct-c-type new
215         byte-array >>class
216         class >>boxed-class
217         slots >>fields
218         size >>size
219         align >>align
220         align >>align-first
221         class (unboxer-quot) >>unboxer-quot
222         class (boxer-quot) >>boxer-quot ;
223
224 GENERIC: compute-slot-offset ( offset class -- offset' )
225
226 : c-type-align-at ( slot-spec offset -- n )
227     over packed?>> [ 2drop 1 ] [
228         [ type>> ] dip
229         0 = [ c-type-align-first ] [ c-type-align ] if
230     ] if ;
231
232 M: struct-slot-spec compute-slot-offset
233     [ over c-type-align-at 8 * align ] keep
234     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
235
236 M: struct-bit-slot-spec compute-slot-offset
237     [ offset<< ] [ bits>> + ] 2bi ;
238
239 : compute-struct-offsets ( slots -- size )
240     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
241
242 : compute-union-offsets ( slots -- size )
243     1 [ 0 >>offset type>> heap-size max ] reduce ;
244
245 : struct-alignment ( slots -- align )
246     [ struct-bit-slot-spec? not ] filter
247     1 [ dup offset>> c-type-align-at max ] reduce ;
248
249 PRIVATE>
250
251 M: struct byte-length class-of "struct-size" word-prop ; foldable
252 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
253
254 ! class definition
255
256 <PRIVATE
257 : struct-needs-prototype? ( class -- ? )
258     struct-slots [ initial>> binary-zero? ] all? not ;
259
260 : make-struct-prototype ( class -- prototype )
261     dup struct-needs-prototype? [
262         [ "c-type" word-prop size>> <byte-array> ]
263         [ memory>struct ]
264         [ struct-slots ] tri
265         [
266             [ initial>> ]
267             [ (writer-quot) ] bi
268             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
269         ] each
270     ] [ drop f ] if ;
271
272 : (struct-methods) ( class -- )
273     [ (define-struct-slot-values-method) ]
274     [ (define-clone-method) ]
275     bi ;
276
277 : check-struct-slots ( slots -- )
278     [ type>> lookup-c-type drop ] each ;
279
280 : redefine-struct-tuple-class ( class -- )
281     [ struct f define-tuple-class ] [ make-final ] bi ;
282
283 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
284     slot-specs check-struct-slots
285     slot-specs empty? [ struct-must-have-slots ] when
286     class redefine-struct-tuple-class
287     slot-specs offsets-quot call :> unaligned-size
288     slot-specs alignment-quot call :> alignment
289     unaligned-size alignment align :> size
290
291     class slot-specs size alignment c-type-for-class :> c-type
292
293     c-type class typedef
294     class slot-specs define-accessors
295     class size "struct-size" set-word-prop
296     class dup make-struct-prototype "prototype" set-word-prop
297     class (struct-methods) ; inline
298
299 : make-packed-slots ( slots -- slot-specs )
300     make-slots [ t >>packed? ] map! ;
301
302 PRIVATE>
303
304 : define-struct-class ( class slots -- )
305     make-slots
306     [ compute-struct-offsets ] [ struct-alignment ]
307     (define-struct-class) ;
308
309 : define-packed-struct-class ( class slots -- )
310     make-packed-slots
311     [ compute-struct-offsets ] [ drop 1 ]
312     (define-struct-class) ;
313
314 : define-union-struct-class ( class slots -- )
315     make-slots
316     [ compute-union-offsets ] [ struct-alignment ]
317     (define-struct-class) ;
318
319 ERROR: invalid-struct-slot token ;
320
321 : struct-slot-class ( c-type -- class' )
322     lookup-c-type c-type-boxed-class
323     dup \ byte-array = [ drop \ c-ptr ] when ;
324
325 M: struct-class reset-class
326     {
327         [ dup "c-type" word-prop fields>> forget-slot-accessors ]
328         [
329             [ forget-struct-slot-values-method ]
330             [ forget-clone-method ] bi
331         ]
332         [ { "c-type" "layout" "struct-size" } reset-props ]
333         [ call-next-method ]
334     } cleave ;
335
336 SYMBOL: bits:
337
338 <PRIVATE
339
340 ERROR: bad-type-for-bits type ;
341
342 :: set-bits ( slot-spec n -- slot-spec )
343     struct-bit-slot-spec new
344         n >>bits
345         slot-spec type>> {
346             { int [ t ] }
347             { uint [ f ] }
348             [ bad-type-for-bits ]
349         } case >>signed?
350         slot-spec name>> >>name
351         slot-spec class>> >>class
352         slot-spec type>> >>type
353         slot-spec read-only>> >>read-only
354         slot-spec initial>> >>initial ;
355
356 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
357     dup empty? [
358         unclip {
359             { initial: [ [ first >>initial ] [ rest ] bi ] }
360             { read-only [ [ t >>read-only ] dip ] }
361             { bits: [ [ first set-bits ] [ rest ] bi ] }
362             [ bad-slot-attribute ]
363         } case
364     ] unless ;
365
366 PRIVATE>
367
368 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
369     [ struct-slot-spec new ] 3dip
370     [ >>name ]
371     [ [ >>type ] [ struct-slot-class >>class ] bi ]
372     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
373
374 <PRIVATE
375 : parse-struct-slot ( -- slot )
376     scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
377
378 : parse-struct-slots ( slots -- slots' more? )
379     scan-token {
380         { ";" [ f ] }
381         { "{" [ parse-struct-slot suffix! t ] }
382         [ invalid-struct-slot ]
383     } case ;
384
385 : parse-struct-definition ( -- class slots )
386     scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
387     dup [ name>> ] map check-duplicate-slots ;
388 PRIVATE>
389
390 SYNTAX: STRUCT:
391     parse-struct-definition define-struct-class ;
392
393 SYNTAX: PACKED-STRUCT:
394     parse-struct-definition define-packed-struct-class ;
395
396 SYNTAX: UNION-STRUCT:
397     parse-struct-definition define-union-struct-class ;
398
399 SYNTAX: S{
400     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
401
402 SYNTAX: S@
403     scan-word scan-object swap memory>struct suffix! ;
404
405 ! functor support
406
407 <PRIVATE
408 : scan-c-type` ( -- c-type/param )
409     scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
410
411 : parse-struct-slot` ( accum -- accum )
412     scan-string-param scan-c-type` \ } parse-until
413     [ <struct-slot-spec> suffix! ] 3curry append! ;
414
415 : parse-struct-slots` ( accum -- accum more? )
416     scan-token {
417         { ";" [ f ] }
418         { "{" [ parse-struct-slot` t ] }
419         [ invalid-struct-slot ]
420     } case ;
421
422 PRIVATE>
423
424 FUNCTOR-SYNTAX: STRUCT:
425     scan-param suffix!
426     [ 8 <vector> ] append!
427     [ parse-struct-slots` ] [ ] while
428     [ >array define-struct-class ] append! ;
429
430 USING: vocabs vocabs.loader ;
431
432 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when