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