]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
Make M\ throw an error upon lookup failure. Change method -> ?lookup-method, lookup...
[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 ] 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
178 M: struct-c-type base-type ;
179
180 : large-struct? ( type -- ? )
181     {
182         { [ dup void? ] [ drop f ] }
183         { [ dup base-type struct-c-type? not ] [ drop f ] }
184         [ return-struct-in-registers? not ]
185     } cond ;
186
187 <PRIVATE
188 : struct-slot-values-quot ( class -- quot )
189     struct-slots
190     [ name>> reader-word 1quotation ] map
191     \ cleave [ ] 2sequence
192     \ output>array [ ] 2sequence ;
193
194 : (define-struct-slot-values-method) ( class -- )
195     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
196     define-inline-method ;
197
198 : forget-struct-slot-values-method ( class -- )
199     \ struct-slot-values ?lookup-method forget ;
200
201 : clone-underlying ( struct -- byte-array )
202     binary-object memory>byte-array ; inline
203
204 : (define-clone-method) ( class -- )
205     [ \ clone ]
206     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
207     define-inline-method ;
208
209 : forget-clone-method ( class -- )
210     \ clone ?lookup-method forget ;
211
212 :: c-type-for-class ( class slots size align -- c-type )
213     struct-c-type new
214         byte-array >>class
215         class >>boxed-class
216         slots >>fields
217         size >>size
218         align >>align
219         align >>align-first
220         class (unboxer-quot) >>unboxer-quot
221         class (boxer-quot) >>boxer-quot ;
222
223 GENERIC: compute-slot-offset ( offset class -- offset' )
224
225 : c-type-align-at ( slot-spec offset -- n )
226     over packed?>> [ 2drop 1 ] [
227         [ type>> ] dip
228         0 = [ c-type-align-first ] [ c-type-align ] if
229     ] if ;
230
231 M: struct-slot-spec compute-slot-offset
232     [ over c-type-align-at 8 * align ] keep
233     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
234
235 M: struct-bit-slot-spec compute-slot-offset
236     [ offset<< ] [ bits>> + ] 2bi ;
237
238 : compute-struct-offsets ( slots -- size )
239     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
240
241 : compute-union-offsets ( slots -- size )
242     1 [ 0 >>offset type>> heap-size max ] reduce ;
243
244 : struct-alignment ( slots -- align )
245     [ struct-bit-slot-spec? not ] filter
246     1 [ dup offset>> c-type-align-at max ] reduce ;
247
248 PRIVATE>
249
250 M: struct byte-length class "struct-size" word-prop ; foldable
251 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
252
253 ! class definition
254
255 <PRIVATE
256 : struct-needs-prototype? ( class -- ? )
257     struct-slots [ initial>> binary-zero? ] all? not ;
258
259 : make-struct-prototype ( class -- prototype )
260     dup struct-needs-prototype? [
261         [ "c-type" word-prop size>> <byte-array> ]
262         [ memory>struct ]
263         [ struct-slots ] tri
264         [
265             [ initial>> ]
266             [ (writer-quot) ] bi
267             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
268         ] each
269     ] [ drop f ] if ;
270
271 : (struct-methods) ( class -- )
272     [ (define-struct-slot-values-method) ]
273     [ (define-clone-method) ]
274     bi ;
275
276 : check-struct-slots ( slots -- )
277     [ type>> c-type drop ] each ;
278
279 : redefine-struct-tuple-class ( class -- )
280     [ struct f define-tuple-class ] [ make-final ] bi ;
281
282 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
283     slot-specs check-struct-slots
284     slot-specs empty? [ struct-must-have-slots ] when
285     class redefine-struct-tuple-class
286     slot-specs offsets-quot call :> unaligned-size
287     slot-specs alignment-quot call :> alignment
288     unaligned-size alignment align :> size
289
290     class slot-specs size alignment c-type-for-class :> c-type
291
292     c-type class typedef
293     class slot-specs define-accessors
294     class size "struct-size" set-word-prop
295     class dup make-struct-prototype "prototype" set-word-prop
296     class (struct-methods) ; inline
297
298 : make-packed-slots ( slots -- slot-specs )
299     make-slots [ t >>packed? ] map! ;
300
301 PRIVATE>
302
303 : define-struct-class ( class slots -- )
304     make-slots
305     [ compute-struct-offsets ] [ struct-alignment ]
306     (define-struct-class) ;
307
308 : define-packed-struct-class ( class slots -- )
309     make-packed-slots
310     [ compute-struct-offsets ] [ drop 1 ]
311     (define-struct-class) ;
312
313 : define-union-struct-class ( class slots -- )
314     make-slots
315     [ compute-union-offsets ] [ struct-alignment ]
316     (define-struct-class) ;
317
318 ERROR: invalid-struct-slot token ;
319
320 : struct-slot-class ( c-type -- class' )
321     c-type c-type-boxed-class
322     dup \ byte-array = [ drop \ c-ptr ] when ;
323
324 M: struct-class reset-class
325     [ call-next-method ]
326     [
327         [ forget-struct-slot-values-method ]
328         [ forget-clone-method ] bi
329     ]
330     [ { "c-type" "layout" "struct-size" } reset-props ] tri ;
331
332 SYMBOL: bits:
333
334 <PRIVATE
335
336 ERROR: bad-type-for-bits type ;
337
338 :: set-bits ( slot-spec n -- slot-spec )
339     struct-bit-slot-spec new
340         n >>bits
341         slot-spec type>> {
342             { int [ t ] }
343             { uint [ f ] }
344             [ bad-type-for-bits ]
345         } case >>signed?
346         slot-spec name>> >>name
347         slot-spec class>> >>class
348         slot-spec type>> >>type
349         slot-spec read-only>> >>read-only
350         slot-spec initial>> >>initial ;
351
352 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
353     dup empty? [
354         unclip {
355             { initial: [ [ first >>initial ] [ rest ] bi ] }
356             { read-only [ [ t >>read-only ] dip ] }
357             { bits: [ [ first set-bits ] [ rest ] bi ] }
358             [ bad-slot-attribute ]
359         } case
360     ] unless ;
361
362 PRIVATE>
363
364 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
365     [ struct-slot-spec new ] 3dip
366     [ >>name ]
367     [ [ >>type ] [ struct-slot-class >>class ] bi ]
368     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
369
370 <PRIVATE
371 : parse-struct-slot ( -- slot )
372     scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
373
374 : parse-struct-slots ( slots -- slots' more? )
375     scan-token {
376         { ";" [ f ] }
377         { "{" [ parse-struct-slot suffix! t ] }
378         [ invalid-struct-slot ]
379     } case ;
380
381 : parse-struct-definition ( -- class slots )
382     scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
383     dup [ name>> ] map check-duplicate-slots ;
384 PRIVATE>
385
386 SYNTAX: STRUCT:
387     parse-struct-definition define-struct-class ;
388
389 SYNTAX: PACKED-STRUCT:
390     parse-struct-definition define-packed-struct-class ;
391
392 SYNTAX: UNION-STRUCT:
393     parse-struct-definition define-union-struct-class ;
394
395 SYNTAX: S{
396     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
397
398 SYNTAX: S@
399     scan-word scan-object swap memory>struct suffix! ;
400
401 ! functor support
402
403 <PRIVATE
404 : scan-c-type` ( -- c-type/param )
405     scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
406
407 : parse-struct-slot` ( accum -- accum )
408     scan-string-param scan-c-type` \ } parse-until
409     [ <struct-slot-spec> suffix! ] 3curry append! ;
410
411 : parse-struct-slots` ( accum -- accum more? )
412     scan-token {
413         { ";" [ f ] }
414         { "{" [ parse-struct-slot` t ] }
415         [ invalid-struct-slot ]
416     } case ;
417
418 PRIVATE>
419
420 FUNCTOR-SYNTAX: STRUCT:
421     scan-param suffix!
422     [ 8 <vector> ] append!
423     [ parse-struct-slots` ] [ ] while
424     [ >array define-struct-class ] append! ;
425
426 USING: vocabs vocabs.loader ;
427
428 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when