]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
use reject instead of [ ... not ] filter.
[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 classes.private
6 classes.struct.bit-accessors classes.tuple classes.tuple.parser
7 combinators combinators.smart cpu.architecture definitions fry
8 functors.backend generalizations generic generic.parser io 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.loader
12 vocabs.parser words ;
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 ] same? [
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 : read-struct ( class -- struct )
75     [ heap-size read ] [ memory>struct ] bi ;
76
77 <PRIVATE
78 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
79     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
80 PRIVATE>
81
82 : (malloc-struct) ( class -- struct )
83     [ heap-size malloc ] keep memory>struct ; inline
84
85 : malloc-struct ( class -- struct )
86     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
87
88 : (struct) ( class -- struct )
89     [ heap-size (byte-array) ] keep memory>struct ; inline
90
91 : <struct> ( class -- struct )
92     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
93
94 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
95     [
96         [ <wrapper> \ (struct) [ ] 2sequence ]
97         [
98             struct-slots
99             [ length \ ndip ]
100             [ [ name>> setter-word 1quotation ] map \ spread ] bi
101         ] bi
102     ] [ ] output>sequence ;
103
104 <PRIVATE
105 : pad-struct-slots ( values class -- values' class )
106     [ struct-slots [ initial>> ] map over length tail append ] keep ;
107
108 : sign-extend ( n bits -- n' )
109     ! formula from:
110     ! http://guru.multimedia.cx/fast-sign-extension/
111     1 - -1 swap shift [ + ] keep bitxor ; inline
112
113 : sign-extender ( signed? bits -- quot )
114     '[ _ [ _ sign-extend ] when ] ;
115
116 GENERIC: (reader-quot) ( slot -- quot )
117
118 M: struct-slot-spec (reader-quot)
119     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
120
121 M: struct-bit-slot-spec (reader-quot)
122     [ [ offset>> ] [ bits>> ] bi bit-reader ]
123     [ [ signed?>> ] [ bits>> ] bi sign-extender ]
124     bi compose
125     [ >c-ptr ] prepose ;
126
127 GENERIC: (writer-quot) ( slot -- quot )
128
129 M: struct-slot-spec (writer-quot)
130     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
131
132 M: struct-bit-slot-spec (writer-quot)
133     [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
134
135 : (boxer-quot) ( class -- quot )
136     '[ _ memory>struct ] ;
137
138 : (unboxer-quot) ( class -- quot )
139     drop [ >c-ptr ] ;
140
141 MACRO: read-struct-slot ( slot -- )
142     dup type>> add-depends-on-c-type
143     (reader-quot) ;
144
145 MACRO: write-struct-slot ( slot -- )
146     dup type>> add-depends-on-c-type
147     (writer-quot) ;
148 PRIVATE>
149
150 M: struct-class boa>object
151     swap pad-struct-slots
152     [ <struct> ] [ struct-slots ] bi
153     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
154
155 M: struct-class initial-value* <struct> t ; inline
156
157 ! Struct slot accessors
158
159 GENERIC: struct-slot-values ( struct -- sequence )
160
161 M: struct-class reader-quot
162     dup type>> array? [ dup type>> first define-array-vocab drop ] when
163     nip '[ _ read-struct-slot ] ;
164
165 M: struct-class writer-quot
166     nip '[ _ write-struct-slot ] ;
167
168 : offset-of ( field struct -- offset )
169     struct-slots slot-named offset>> ; inline
170
171 ! c-types
172
173 TUPLE: struct-c-type < abstract-c-type
174     fields
175     return-in-registers? ;
176
177 INSTANCE: struct-c-type value-type
178
179 M: struct-c-type lookup-c-type ;
180
181 M: struct-c-type base-type ;
182
183 : large-struct? ( type -- ? )
184     {
185         { [ dup void? ] [ drop f ] }
186         { [ dup base-type struct-c-type? not ] [ drop f ] }
187         [ return-struct-in-registers? not ]
188     } cond ;
189
190 <PRIVATE
191 : struct-slot-values-quot ( class -- quot )
192     struct-slots
193     [ name>> reader-word 1quotation ] map
194     \ cleave [ ] 2sequence
195     \ output>array [ ] 2sequence ;
196
197 : (define-struct-slot-values-method) ( class -- )
198     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
199     define-inline-method ;
200
201 : forget-struct-slot-values-method ( class -- )
202     \ struct-slot-values ?lookup-method forget ;
203
204 : clone-underlying ( struct -- byte-array )
205     binary-object memory>byte-array ; inline
206
207 : (define-clone-method) ( class -- )
208     [ \ clone ]
209     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
210     define-inline-method ;
211
212 : forget-clone-method ( class -- )
213     \ clone ?lookup-method forget ;
214
215 :: c-type-for-class ( class slots size align -- c-type )
216     struct-c-type new
217         byte-array >>class
218         class >>boxed-class
219         slots >>fields
220         size >>size
221         align >>align
222         align >>align-first
223         class (unboxer-quot) >>unboxer-quot
224         class (boxer-quot) >>boxer-quot ;
225
226 GENERIC: compute-slot-offset ( offset class -- offset' )
227
228 : c-type-align-at ( slot-spec offset -- n )
229     over packed?>> [ 2drop 1 ] [
230         [ type>> ] dip
231         0 = [ c-type-align-first ] [ c-type-align ] if
232     ] if ;
233
234 M: struct-slot-spec compute-slot-offset
235     [ over c-type-align-at 8 * align ] keep
236     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
237
238 M: struct-bit-slot-spec compute-slot-offset
239     [ offset<< ] [ bits>> + ] 2bi ;
240
241 : compute-struct-offsets ( slots -- size )
242     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
243
244 : compute-union-offsets ( slots -- size )
245     1 [ 0 >>offset type>> heap-size max ] reduce ;
246
247 : struct-alignment ( slots -- align )
248     [ struct-bit-slot-spec? ] reject
249     1 [ dup offset>> c-type-align-at max ] reduce ;
250
251 PRIVATE>
252
253 : struct-size ( class -- n ) "struct-size" word-prop ; inline
254
255 M: struct byte-length class-of struct-size ; inline foldable
256 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
257
258 ! class definition
259
260 <PRIVATE
261 : struct-needs-prototype? ( class -- ? )
262     struct-slots [ initial>> binary-zero? ] all? not ;
263
264 : make-struct-prototype ( class -- prototype )
265     dup struct-needs-prototype? [
266         [ "c-type" word-prop size>> <byte-array> ]
267         [ memory>struct ]
268         [ struct-slots ] tri
269         [
270             [ initial>> ]
271             [ (writer-quot) ] bi
272             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
273         ] each
274     ] [ drop f ] if ;
275
276 : (struct-methods) ( class -- )
277     [ (define-struct-slot-values-method) ]
278     [ (define-clone-method) ]
279     bi ;
280
281 : check-struct-slots ( slots -- )
282     [ type>> lookup-c-type drop ] each ;
283
284 : redefine-struct-tuple-class ( class -- )
285     [ struct f define-tuple-class ] [ make-final ] bi ;
286
287 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
288     slot-specs check-struct-slots
289     slot-specs empty? [ struct-must-have-slots ] when
290     class redefine-struct-tuple-class
291     slot-specs offsets-quot call :> unaligned-size
292     slot-specs alignment-quot call :> alignment
293     unaligned-size alignment align :> size
294
295     class slot-specs size alignment c-type-for-class :> c-type
296
297     c-type class typedef
298     class slot-specs define-accessors
299     class size "struct-size" set-word-prop
300     class dup make-struct-prototype "prototype" set-word-prop
301     class (struct-methods) ; inline
302
303 : make-packed-slots ( slots -- slot-specs )
304     make-slots [ t >>packed? ] map! ;
305
306 PRIVATE>
307
308 : define-struct-class ( class slots -- )
309     make-slots
310     [ compute-struct-offsets ] [ struct-alignment ]
311     (define-struct-class) ;
312
313 : define-packed-struct-class ( class slots -- )
314     make-packed-slots
315     [ compute-struct-offsets ] [ drop 1 ]
316     (define-struct-class) ;
317
318 : define-union-struct-class ( class slots -- )
319     make-slots
320     [ compute-union-offsets ] [ struct-alignment ]
321     (define-struct-class) ;
322
323 ERROR: invalid-struct-slot token ;
324
325 : struct-slot-class ( c-type -- class' )
326     lookup-c-type c-type-boxed-class
327     dup \ byte-array = [ drop \ c-ptr ] when ;
328
329 M: struct-class reset-class
330     {
331         [ dup "c-type" word-prop fields>> forget-slot-accessors ]
332         [
333             [ forget-struct-slot-values-method ]
334             [ forget-clone-method ] bi
335         ]
336         [ { "c-type" "layout" "struct-size" } reset-props ]
337         [ call-next-method ]
338     } cleave ;
339
340 SYMBOL: bits:
341
342 <PRIVATE
343
344 :: set-bits ( slot-spec n -- slot-spec )
345     struct-bit-slot-spec new
346         n >>bits
347         slot-spec type>> c-type-signed >>signed?
348         slot-spec name>> >>name
349         slot-spec class>> >>class
350         slot-spec type>> >>type
351         slot-spec read-only>> >>read-only
352         slot-spec initial>> >>initial ;
353
354 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
355     dup empty? [
356         unclip {
357             { initial: [ [ first >>initial ] [ rest ] bi ] }
358             { read-only [ [ t >>read-only ] dip ] }
359             { bits: [ [ first set-bits ] [ rest ] bi ] }
360             [ bad-slot-attribute ]
361         } case
362     ] unless ;
363
364 PRIVATE>
365
366 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
367     [ struct-slot-spec new ] 3dip
368     [ >>name ]
369     [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
370     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
371
372 <PRIVATE
373 : parse-struct-slot ( -- slot )
374     scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
375
376 : parse-struct-slots ( slots -- slots' more? )
377     scan-token {
378         { ";" [ f ] }
379         { "{" [ parse-struct-slot suffix! t ] }
380         [ invalid-struct-slot ]
381     } case ;
382
383 : parse-struct-definition ( -- class slots )
384     scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
385     dup [ name>> ] map check-duplicate-slots ;
386 PRIVATE>
387
388 SYNTAX: STRUCT:
389     parse-struct-definition define-struct-class ;
390
391 SYNTAX: PACKED-STRUCT:
392     parse-struct-definition define-packed-struct-class ;
393
394 SYNTAX: UNION-STRUCT:
395     parse-struct-definition define-union-struct-class ;
396
397 SYNTAX: S{
398     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
399
400 SYNTAX: S@
401     scan-word scan-object swap memory>struct suffix! ;
402
403 ! functor support
404
405 <PRIVATE
406 : scan-c-type` ( -- c-type/param )
407     scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
408
409 : parse-struct-slot` ( accum -- accum )
410     scan-string-param scan-c-type` \ } parse-until
411     [ <struct-slot-spec> suffix! ] 3curry append! ;
412
413 : parse-struct-slots` ( accum -- accum more? )
414     scan-token {
415         { ";" [ f ] }
416         { "{" [ parse-struct-slot` t ] }
417         [ invalid-struct-slot ]
418     } case ;
419
420 PRIVATE>
421
422 FUNCTOR-SYNTAX: STRUCT:
423     scan-param suffix!
424     [ 8 <vector> ] append!
425     [ parse-struct-slots` ] [ ] while
426     [ >array define-struct-class ] append! ;
427
428 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when