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