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