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