1 ! Copyright (C) 2006, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs classes classes.private classes.algebra
4 combinators hashtables kernel layouts math namespaces make
5 quotations sequences system vectors words effects alien
6 byte-arrays accessors sets math.order cpu.architecture
7 compiler.generator.fixup ;
8 IN: compiler.generator.registers
19 GENERIC: set-operand-class ( class obj -- )
20 GENERIC: operand-class* ( operand -- class )
21 GENERIC: move-spec ( obj -- spec )
22 GENERIC: live-vregs* ( obj -- )
23 GENERIC: live-loc? ( actual current -- ? )
24 GENERIC# (lazy-load) 1 ( value spec -- value )
25 GENERIC: lazy-store ( dst src -- )
26 GENERIC: minimal-ds-loc* ( min obj -- min )
28 ! This will be a multimethod soon
35 : operand-class ( operand -- class )
36 operand-class* object or ;
38 ! Default implementation
39 M: value set-operand-class 2drop ;
40 M: value operand-class* drop f ;
41 M: value live-vregs* drop ;
42 M: value live-loc? 2drop f ;
43 M: value minimal-ds-loc* drop ;
44 M: value lazy-store 2drop ;
46 ! A scratch register for computations
47 TUPLE: vreg n reg-class ;
49 C: <vreg> vreg ( n reg-class -- vreg )
51 M: vreg v>operand [ n>> ] [ reg-class>> ] bi vregs nth ;
52 M: vreg live-vregs* , ;
56 { [ dup int-regs? ] [ f ] }
57 { [ dup float-regs? ] [ float ] }
60 M: vreg operand-class*
62 { [ dup int-regs? ] [ f ] }
63 { [ dup float-regs? ] [ float ] }
68 ! Temporary register for stack shuffling
71 M: temp-reg move-spec drop f ;
73 INSTANCE: temp-reg value
75 ! A data stack location.
76 TUPLE: ds-loc n class ;
78 : <ds-loc> ( n -- loc ) f ds-loc boa ;
80 M: ds-loc minimal-ds-loc* n>> min ;
82 over ds-loc? [ [ n>> ] bi@ = not ] [ 2drop t ] if ;
84 ! A retain stack location.
85 TUPLE: rs-loc n class ;
87 : <rs-loc> ( n -- loc ) f rs-loc boa ;
89 over rs-loc? [ [ n>> ] bi@ = not ] [ 2drop t ] if ;
91 UNION: loc ds-loc rs-loc ;
93 M: loc operand-class* class>> ;
94 M: loc set-operand-class (>>class) ;
95 M: loc move-spec drop loc ;
99 M: f move-spec drop loc ;
100 M: f operand-class* ;
102 ! A stack location which has been loaded into a register. To
103 ! read the location, we just read the register, but when time
104 ! comes to save it back to the stack, we know the register just
105 ! contains a stack value so we don't have to redundantly write
107 TUPLE: cached loc vreg ;
111 M: cached set-operand-class vreg>> set-operand-class ;
112 M: cached operand-class* vreg>> operand-class* ;
113 M: cached move-spec drop cached ;
114 M: cached live-vregs* vreg>> live-vregs* ;
115 M: cached live-loc? loc>> live-loc? ;
116 M: cached (lazy-load) >r vreg>> r> (lazy-load) ;
119 [ "live-locs" get at %move ] [ 2drop ] if ;
120 M: cached minimal-ds-loc* loc>> minimal-ds-loc* ;
122 INSTANCE: cached value
125 TUPLE: tagged vreg class ;
127 : <tagged> ( vreg -- tagged )
130 M: tagged v>operand vreg>> v>operand ;
131 M: tagged set-operand-class (>>class) ;
132 M: tagged operand-class* class>> ;
133 M: tagged move-spec drop f ;
134 M: tagged live-vregs* vreg>> , ;
136 INSTANCE: tagged value
138 ! Unboxed alien pointers
139 TUPLE: unboxed-alien vreg ;
140 C: <unboxed-alien> unboxed-alien
141 M: unboxed-alien v>operand vreg>> v>operand ;
142 M: unboxed-alien operand-class* drop simple-alien ;
143 M: unboxed-alien move-spec class ;
144 M: unboxed-alien live-vregs* vreg>> , ;
146 INSTANCE: unboxed-alien value
148 TUPLE: unboxed-byte-array vreg ;
149 C: <unboxed-byte-array> unboxed-byte-array
150 M: unboxed-byte-array v>operand vreg>> v>operand ;
151 M: unboxed-byte-array operand-class* drop c-ptr ;
152 M: unboxed-byte-array move-spec class ;
153 M: unboxed-byte-array live-vregs* vreg>> , ;
155 INSTANCE: unboxed-byte-array value
157 TUPLE: unboxed-f vreg ;
158 C: <unboxed-f> unboxed-f
159 M: unboxed-f v>operand vreg>> v>operand ;
160 M: unboxed-f operand-class* drop \ f ;
161 M: unboxed-f move-spec class ;
162 M: unboxed-f live-vregs* vreg>> , ;
164 INSTANCE: unboxed-f value
166 TUPLE: unboxed-c-ptr vreg ;
167 C: <unboxed-c-ptr> unboxed-c-ptr
168 M: unboxed-c-ptr v>operand vreg>> v>operand ;
169 M: unboxed-c-ptr operand-class* drop c-ptr ;
170 M: unboxed-c-ptr move-spec class ;
171 M: unboxed-c-ptr live-vregs* vreg>> , ;
173 INSTANCE: unboxed-c-ptr value
176 TUPLE: constant value ;
177 C: <constant> constant
178 M: constant operand-class* value>> class ;
179 M: constant move-spec class ;
181 INSTANCE: constant value
185 ! Moving values between locations and registers
186 : %move-bug ( -- * ) "Bug in generator.registers" throw ;
188 : %unbox-c-ptr ( dst src -- )
190 { [ dup \ f class<= ] [ drop %unbox-f ] }
191 { [ dup simple-alien class<= ] [ drop %unbox-alien ] }
192 { [ dup byte-array class<= ] [ drop %unbox-byte-array ] }
193 [ drop %unbox-any-c-ptr ]
196 : %move-via-temp ( dst src -- )
197 #! For many transfers, such as loc to unboxed-alien, we
198 #! don't have an intrinsic, so we transfer the source to
199 #! temp then temp to the destination.
201 operand-class temp-reg
207 : %move ( dst src -- )
208 2dup [ move-spec ] bi@ 2array {
209 { { f f } [ %move-bug ] }
210 { { f unboxed-c-ptr } [ %move-bug ] }
211 { { f unboxed-byte-array } [ %move-bug ] }
213 { { f constant } [ value>> swap load-literal ] }
215 { { f float } [ %box-float ] }
216 { { f unboxed-alien } [ %box-alien ] }
217 { { f loc } [ %peek ] }
219 { { float f } [ %unbox-float ] }
220 { { unboxed-alien f } [ %unbox-alien ] }
221 { { unboxed-byte-array f } [ %unbox-byte-array ] }
222 { { unboxed-f f } [ %unbox-f ] }
223 { { unboxed-c-ptr f } [ %unbox-c-ptr ] }
224 { { loc f } [ swap %replace ] }
226 [ drop %move-via-temp ]
229 ! A compile-time stack
230 TUPLE: phantom-stack height stack ;
232 M: phantom-stack clone
233 call-next-method [ clone ] change-stack ;
235 GENERIC: finalize-height ( stack -- )
237 : new-phantom-stack ( class -- stack )
238 >r 0 V{ } clone r> boa ; inline
240 : (loc) ( m stack -- n )
241 #! Utility for methods on <loc>
244 : (finalize-height) ( stack word -- )
245 #! We consolidate multiple stack height changes until the
246 #! last moment, and we emit the final height changing
249 over zero? [ 2drop ] [ execute ] if 0
250 ] curry change-height drop ; inline
252 GENERIC: <loc> ( n stack -- loc )
254 TUPLE: phantom-datastack < phantom-stack ;
256 : <phantom-datastack> ( -- stack )
257 phantom-datastack new-phantom-stack ;
259 M: phantom-datastack <loc> (loc) <ds-loc> ;
261 M: phantom-datastack finalize-height
262 \ %inc-d (finalize-height) ;
264 TUPLE: phantom-retainstack < phantom-stack ;
266 : <phantom-retainstack> ( -- stack )
267 phantom-retainstack new-phantom-stack ;
269 M: phantom-retainstack <loc> (loc) <rs-loc> ;
271 M: phantom-retainstack finalize-height
272 \ %inc-r (finalize-height) ;
274 : phantom-locs ( n phantom -- locs )
275 #! A sequence of n ds-locs or rs-locs indexing the stack.
276 >r <reversed> r> [ <loc> ] curry map ;
278 : phantom-locs* ( phantom -- locs )
279 [ stack>> length ] keep phantom-locs ;
281 : phantoms ( -- phantom phantom )
282 phantom-datastack get phantom-retainstack get ;
284 : (each-loc) ( phantom quot -- )
285 >r [ phantom-locs* ] [ stack>> ] bi r> 2each ; inline
287 : each-loc ( quot -- )
288 phantoms 2array swap [ (each-loc) ] curry each ; inline
290 : adjust-phantom ( n phantom -- )
291 swap [ + ] curry change-height drop ;
293 : cut-phantom ( n phantom -- seq )
294 swap [ cut* swap ] curry change-stack drop ;
296 : phantom-append ( seq stack -- )
297 over length over adjust-phantom stack>> push-all ;
299 : add-locs ( n phantom -- )
300 2dup stack>> length <= [
303 [ phantom-locs ] keep
304 [ stack>> length head-slice* ] keep
305 [ append >vector ] change-stack drop
308 : phantom-input ( n phantom -- seq )
311 >r >r neg r> adjust-phantom r> ;
313 : each-phantom ( quot -- ) phantoms rot bi@ ; inline
315 : finalize-heights ( -- ) [ finalize-height ] each-phantom ;
317 : live-vregs ( -- seq )
318 [ [ stack>> [ live-vregs* ] each ] each-phantom ] { } make ;
320 : (live-locs) ( phantom -- seq )
321 #! Discard locs which haven't moved
322 [ phantom-locs* ] [ stack>> ] bi zip
323 [ live-loc? ] assoc-filter
326 : live-locs ( -- seq )
327 [ (live-locs) ] each-phantom append prune ;
329 ! Operands holding pointers to freshly-allocated objects which
330 ! are guaranteed to be in the nursery
331 SYMBOL: fresh-objects
333 ! Computing free registers and initializing allocator
334 : reg-spec>class ( spec -- class )
335 float eq? double-float-regs int-regs ? ;
337 : free-vregs ( reg-class -- seq )
338 #! Free vregs in a given register class
339 \ free-vregs get at ;
341 : alloc-vreg ( spec -- reg )
342 [ reg-spec>class free-vregs pop ] keep {
344 { unboxed-alien [ <unboxed-alien> ] }
345 { unboxed-byte-array [ <unboxed-byte-array> ] }
346 { unboxed-f [ <unboxed-f> ] }
347 { unboxed-c-ptr [ <unboxed-c-ptr> ] }
351 : compatible? ( value spec -- ? )
354 { [ dup unboxed-c-ptr eq? ] [
355 over { unboxed-byte-array unboxed-alien } member?
360 : allocation ( value spec -- reg-class )
362 { [ dup quotation? ] [ 2drop f ] }
363 { [ 2dup compatible? ] [ 2drop f ] }
364 [ nip reg-spec>class ]
367 : alloc-vreg-for ( value spec -- vreg )
368 alloc-vreg swap operand-class
369 over tagged? [ >>class ] [ drop ] if ;
373 dupd alloc-vreg-for dup rot %move
378 : (compute-free-vregs) ( used class -- vector )
379 #! Find all vregs in 'class' which are not in 'used'.
380 [ vregs length reverse ] keep
381 [ <vreg> ] curry map swap diff
384 : compute-free-vregs ( -- )
385 #! Create a new hashtable for thee free-vregs variable.
387 { int-regs double-float-regs }
388 [ 2dup (compute-free-vregs) ] H{ } map>assoc
393 2dup live-loc? [ "live-locs" get at %move ] [ 2drop ] if ;
395 : do-shuffle ( hash -- )
400 [ lazy-store ] each-loc
403 : fast-shuffle ( locs -- )
404 #! We have enough free registers to load all shuffle inputs
406 [ dup f (lazy-load) ] H{ } map>assoc do-shuffle ;
408 : minimal-ds-loc ( phantom -- n )
409 #! When shuffling more values than can fit in registers, we
410 #! need to find an area on the data stack which isn't in
412 [ stack>> ] [ height>> neg ] bi [ minimal-ds-loc* ] reduce ;
414 : find-tmp-loc ( -- n )
415 #! Find an area of the data stack which is not referenced
416 #! from the phantom stacks. We can clobber there all we want
417 [ minimal-ds-loc ] each-phantom min 1- ;
419 : slow-shuffle-mapping ( locs tmp -- pairs )
421 [ swap - <ds-loc> ] curry map zip ;
423 : slow-shuffle ( locs -- )
424 #! We don't have enough free registers to load all shuffle
425 #! inputs, so we use a single temporary register, together
426 #! with the area of the data stack above the stack pointer
427 find-tmp-loc slow-shuffle-mapping [
429 swap dup cached? [ vreg>> ] when %move
431 ] keep >hashtable do-shuffle ;
433 : fast-shuffle? ( live-locs -- ? )
434 #! Test if we have enough free registers to load all
435 #! shuffle inputs at once.
436 int-regs free-vregs [ length ] bi@ <= ;
438 : finalize-locs ( -- )
439 #! Perform any deferred stack shuffling.
441 \ free-vregs [ [ clone ] assoc-map ] change
442 live-locs dup fast-shuffle?
443 [ fast-shuffle ] [ slow-shuffle ] if
446 : finalize-vregs ( -- )
447 #! Store any vregs to their final stack locations.
449 dup loc? over cached? or [ 2drop ] [ %move ] if
452 : reset-phantom ( phantom -- )
453 #! Kill register assignments but preserve constants and
454 #! class information.
457 dup constant? [ nip ] [
458 operand-class over set-operand-class
461 over stack>> delete-all
462 swap stack>> push-all ;
464 : reset-phantoms ( -- )
465 [ reset-phantom ] each-phantom ;
467 : finalize-contents ( -- )
468 finalize-locs finalize-vregs reset-phantoms ;
470 ! Loading stacks to vregs
471 : free-vregs? ( int# float# -- ? )
472 double-float-regs free-vregs length <=
473 >r int-regs free-vregs length <= r> and ;
475 : phantom&spec ( phantom spec -- phantom' spec' )
477 [ length f pad-left ] keep
478 [ <reversed> ] bi@ ; inline
480 : phantom&spec-agree? ( phantom spec quot -- ? )
481 >r phantom&spec r> 2all? ; inline
483 : vreg-substitution ( value vreg -- pair )
484 dupd <cached> 2array ;
486 : substitute-vreg? ( old new -- ? )
487 #! We don't substitute locs for float or alien vregs,
488 #! since in those cases the boxing overhead might kill us.
489 vreg>> tagged? >r loc? r> and ;
491 : substitute-vregs ( values vregs -- )
492 [ vreg-substitution ] 2map
493 [ substitute-vreg? ] assoc-filter >hashtable
494 [ >r stack>> r> substitute-here ] curry each-phantom ;
496 : set-operand ( value var -- )
497 >r dup constant? [ value>> ] when r> set ;
499 : lazy-load ( values template -- )
500 #! Set operand vars here.
501 2dup [ first (lazy-load) ] 2map
502 dup rot [ second set-operand ] 2each
507 [ length phantom-datastack get phantom-input ] keep
510 : output-vregs ( -- seq seq )
511 +output+ +clobber+ [ get [ get ] map ] bi@ ;
513 : clash? ( seq -- ? )
514 phantoms [ stack>> ] bi@ append [
515 dup cached? [ vreg>> ] when swap member?
518 : outputs-clash? ( -- ? )
519 output-vregs append clash? ;
521 : count-vregs ( reg-classes -- ) [ [ inc ] when* ] each ;
523 : count-input-vregs ( phantom spec -- )
525 >r dup cached? [ vreg>> ] when r> first allocation
528 : count-scratch-regs ( spec -- )
529 [ first reg-spec>class ] map count-vregs ;
531 : guess-vregs ( dinput rinput scratch -- int# float# )
534 0 double-float-regs set
536 phantom-retainstack get swap count-input-vregs
537 phantom-datastack get swap count-input-vregs
538 int-regs get double-float-regs get
541 : alloc-scratch ( -- )
542 +scratch+ get [ >r alloc-vreg r> set ] assoc-each ;
544 : guess-template-vregs ( -- int# float# )
545 +input+ get { } +scratch+ get guess-vregs ;
547 : template-inputs ( -- )
548 ! Load input values into registers
550 ! Allocate scratch registers
552 ! If outputs clash, we write values back to the stack
553 outputs-clash? [ finalize-contents ] when ;
555 : template-outputs ( -- )
556 +output+ get [ get ] map phantom-datastack get phantom-append ;
558 : value-matches? ( value spec -- ? )
559 #! If the spec is a quotation and the value is a literal
560 #! fixnum, see if the quotation yields true when applied
561 #! to the fixnum. Otherwise, the values don't match. If the
562 #! spec is not a quotation, its a reg-class, in which case
563 #! the value is always good.
566 [ >r value>> r> call ] [ 2drop f ] if
571 : class-matches? ( actual expected -- ? )
574 { known-tag [ dup [ class-tag >boolean ] when ] }
578 : spec-matches? ( value spec -- ? )
579 2dup first value-matches?
580 >r >r operand-class 2 r> ?nth class-matches? r> and ;
582 : template-matches? ( spec -- ? )
583 phantom-datastack get +input+ rot at
584 [ spec-matches? ] phantom&spec-agree? ;
586 : ensure-template-vregs ( -- )
587 guess-template-vregs free-vregs? [
588 finalize-contents compute-free-vregs
591 : clear-phantoms ( -- )
592 [ stack>> delete-all ] each-phantom ;
596 : set-operand-classes ( classes -- )
597 phantom-datastack get
598 over length over add-locs
599 stack>> [ set-operand-class ] 2reverse-each ;
601 : end-basic-block ( -- )
602 #! Commit all deferred stacking shuffling, and ensure the
603 #! in-memory data and retain stacks are up to date with
604 #! respect to the compiler's current picture.
608 fresh-objects get [ empty? [ %gc ] unless ] [ delete-all ] bi ;
610 : with-template ( quot hash -- )
612 ensure-template-vregs
613 template-inputs call template-outputs
615 compute-free-vregs ; inline
617 : do-template ( pair -- )
618 #! Use with return value from find-template
619 first2 with-template ;
621 : fresh-object ( obj -- ) fresh-objects get push ;
623 : fresh-object? ( obj -- ? ) fresh-objects get memq? ;
625 : init-templates ( -- )
626 #! Initialize register allocator.
627 V{ } clone fresh-objects set
628 <phantom-datastack> phantom-datastack set
629 <phantom-retainstack> phantom-retainstack set
632 : copy-templates ( -- )
633 #! Copies register allocator state, used when compiling
635 fresh-objects [ clone ] change
636 phantom-datastack [ clone ] change
637 phantom-retainstack [ clone ] change
640 : find-template ( templates -- pair/f )
641 #! Pair has shape { quot hash }
642 [ second template-matches? ] find nip ;
644 : operand-tag ( operand -- tag/f )
645 operand-class dup [ class-tag ] when ;
647 UNION: immediate fixnum POSTPONE: f ;
649 : operand-immediate? ( operand -- ? )
650 operand-class immediate class<= ;
652 : phantom-push ( obj -- )
653 1 phantom-datastack get adjust-phantom
654 phantom-datastack get stack>> push ;
656 : phantom-shuffle ( shuffle -- )
657 [ in>> length phantom-datastack get phantom-input ] keep
658 shuffle phantom-datastack get phantom-append ;
660 : phantom->r ( n -- )
661 phantom-datastack get phantom-input
662 phantom-retainstack get phantom-append ;
664 : phantom-r> ( n -- )
665 phantom-retainstack get phantom-input
666 phantom-datastack get phantom-append ;
668 : phantom-drop ( n -- )
669 phantom-datastack get phantom-input drop ;
671 : phantom-rdrop ( n -- )
672 phantom-retainstack get phantom-input drop ;