2006-04-03 01:33:52 -04:00
|
|
|
! Copyright (C) 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-04-03 02:18:56 -04:00
|
|
|
IN: compiler
|
|
|
|
USING: arrays generic inference kernel math
|
2006-04-03 01:33:52 -04:00
|
|
|
namespaces sequences vectors words ;
|
|
|
|
|
2006-04-08 16:46:47 -04:00
|
|
|
! TUPLE: phantom-stack height elements ;
|
|
|
|
!
|
|
|
|
! GENERIC: <loc> ( n stack -- loc )
|
|
|
|
!
|
|
|
|
! TUPLE: phantom-datastack ;
|
|
|
|
!
|
|
|
|
! C: phantom-datastack [ >r <phantom-stack> r> ] set-delegate ;
|
|
|
|
!
|
|
|
|
! M: phantom-datastack <loc> drop <ds-loc> ;
|
|
|
|
!
|
|
|
|
! TUPLE: phantom-callstack ;
|
|
|
|
!
|
|
|
|
! C: phantom-callstack [ >r <phantom-stack> r> ] set-delegate ;
|
|
|
|
!
|
|
|
|
! M: phantom-callstack <loc> drop <cs-loc> ;
|
|
|
|
|
2006-04-03 02:18:56 -04:00
|
|
|
SYMBOL: d-height
|
|
|
|
SYMBOL: r-height
|
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
! Uncomitted values
|
|
|
|
SYMBOL: phantom-d
|
|
|
|
SYMBOL: phantom-r
|
|
|
|
|
|
|
|
: init-templates
|
|
|
|
0 d-height set 0 r-height set
|
|
|
|
V{ } clone phantom-d set V{ } clone phantom-r set ;
|
|
|
|
|
2006-04-03 02:18:56 -04:00
|
|
|
! A data stack location.
|
|
|
|
TUPLE: ds-loc n ;
|
2006-04-05 02:43:37 -04:00
|
|
|
C: ds-loc [ >r d-height get - r> set-ds-loc-n ] keep ;
|
2006-04-03 02:18:56 -04:00
|
|
|
|
|
|
|
! A call stack location.
|
|
|
|
TUPLE: cs-loc n ;
|
2006-04-05 02:43:37 -04:00
|
|
|
C: cs-loc [ >r r-height get - r> set-cs-loc-n ] keep ;
|
2006-04-03 02:18:56 -04:00
|
|
|
|
|
|
|
: adjust-stacks ( inc-d inc-r -- )
|
|
|
|
r-height [ + ] change d-height [ + ] change ;
|
|
|
|
|
2006-04-03 01:33:52 -04:00
|
|
|
: immediate? ( obj -- ? )
|
|
|
|
#! fixnums and f have a pointerless representation, and
|
|
|
|
#! are compiled immediately. Everything else can be moved
|
|
|
|
#! by GC, and is indexed through a table.
|
|
|
|
dup fixnum? swap f eq? or ;
|
|
|
|
|
|
|
|
: load-literal ( obj vreg -- )
|
|
|
|
over immediate? [ %immediate ] [ %indirect ] if , ;
|
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
G: vreg>stack ( value loc -- ) 1 standard-combination ;
|
|
|
|
|
|
|
|
M: f vreg>stack ( value loc -- ) 2drop ;
|
|
|
|
|
|
|
|
M: value vreg>stack ( value loc -- )
|
|
|
|
swap value-literal fixnum-imm? over immediate? and
|
|
|
|
[ T{ vreg f 0 } load-literal T{ vreg f 0 } ] unless
|
|
|
|
swap %replace , ;
|
|
|
|
|
|
|
|
M: object vreg>stack ( value loc -- )
|
|
|
|
%replace , ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: vregs>stack ( values quot literals -- )
|
|
|
|
-rot >r [ dup value? rot eq? [ drop f ] unless ] map-with
|
|
|
|
dup reverse-slice swap length r> map
|
|
|
|
[ vreg>stack ] 2each ; inline
|
|
|
|
|
|
|
|
: finalize-height ( word symbol -- )
|
|
|
|
[ dup zero? [ 2drop ] [ swap execute , ] if 0 ] change ;
|
|
|
|
inline
|
|
|
|
|
2006-04-08 03:13:01 -04:00
|
|
|
: reset-stack ( vector -- )
|
|
|
|
0 swap set-length ;
|
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: end-basic-block ( -- )
|
|
|
|
\ %inc-d d-height finalize-height
|
|
|
|
\ %inc-r r-height finalize-height
|
|
|
|
phantom-d get [ <ds-loc> ] f vregs>stack
|
|
|
|
phantom-r get [ <cs-loc> ] f vregs>stack
|
|
|
|
phantom-d get [ <ds-loc> ] t vregs>stack
|
|
|
|
phantom-r get [ <cs-loc> ] t vregs>stack
|
2006-04-08 03:13:01 -04:00
|
|
|
phantom-d get reset-stack
|
|
|
|
phantom-r get reset-stack ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
G: stack>vreg ( value vreg loc -- operand )
|
|
|
|
2 standard-combination ;
|
|
|
|
|
|
|
|
M: f stack>vreg ( value vreg loc -- operand ) 2drop ;
|
|
|
|
|
|
|
|
M: object stack>vreg ( value vreg loc -- operand )
|
|
|
|
>r <vreg> dup r> %peek , nip ;
|
|
|
|
|
|
|
|
M: value stack>vreg ( value vreg loc -- operand )
|
2006-04-08 16:46:47 -04:00
|
|
|
drop dup value eq? [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
>r value-literal r> <vreg> [ load-literal ] keep
|
|
|
|
] if ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
|
|
|
SYMBOL: vreg-allocator
|
|
|
|
|
|
|
|
SYMBOL: any-reg
|
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: alloc-reg ( template -- template )
|
|
|
|
dup any-reg eq? [
|
|
|
|
drop vreg-allocator dup get swap inc
|
|
|
|
] when ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: alloc-regs ( template -- template ) [ alloc-reg ] map ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: (stack>vregs) ( values template locs -- inputs )
|
|
|
|
3array flip
|
|
|
|
[ first3 over [ stack>vreg ] [ 3drop f ] if ] map ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-08 03:13:01 -04:00
|
|
|
: phantom-vregs ( phantom template -- )
|
2006-04-08 16:46:47 -04:00
|
|
|
>r [ dup value? [ value-literal ] when ] map r>
|
|
|
|
[ second ] map
|
|
|
|
[ set ] 2each ;
|
2006-04-08 03:13:01 -04:00
|
|
|
|
2006-04-03 01:33:52 -04:00
|
|
|
: stack>vregs ( stack template quot -- )
|
2006-04-08 03:13:01 -04:00
|
|
|
>r dup [ first ] map swapd alloc-regs
|
|
|
|
dup length reverse r> map
|
|
|
|
(stack>vregs) swap phantom-vregs ; inline
|
|
|
|
|
|
|
|
: compatible-vreg?
|
2006-04-08 16:46:47 -04:00
|
|
|
swap dup value? [ 2drop f ] [ vreg-n = ] if ;
|
2006-04-08 03:13:01 -04:00
|
|
|
|
|
|
|
: compatible-values? ( value template -- ? )
|
|
|
|
{
|
2006-04-08 16:46:47 -04:00
|
|
|
{ [ dup any-reg eq? ] [ drop vreg? ] }
|
2006-04-08 03:13:01 -04:00
|
|
|
{ [ dup integer? ] [ compatible-vreg? ] }
|
|
|
|
{ [ dup value eq? ] [ drop value? ] }
|
2006-04-08 16:46:47 -04:00
|
|
|
{ [ dup not ] [ 2drop t ] }
|
2006-04-08 03:13:01 -04:00
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: template-match? ( phantom template -- ? )
|
|
|
|
2dup [ length ] 2apply = [
|
2006-04-08 16:46:47 -04:00
|
|
|
t [ first compatible-values? and ] 2reduce
|
2006-04-08 03:13:01 -04:00
|
|
|
] [
|
|
|
|
2drop f
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: template-input ( values template phantom quot -- )
|
|
|
|
>r swap [ template-match? ] 2keep rot [
|
|
|
|
rot r> 2drop over >r phantom-vregs r> reset-stack
|
|
|
|
] [
|
|
|
|
nip end-basic-block r> stack>vregs
|
|
|
|
] if ; inline
|
2006-04-03 01:33:52 -04:00
|
|
|
|
|
|
|
: template-inputs ( stack template stack template -- )
|
2006-04-08 03:13:01 -04:00
|
|
|
over >r phantom-r get [ <cs-loc> ] template-input
|
|
|
|
over >r phantom-d get [ <ds-loc> ] template-input
|
2006-04-05 02:43:37 -04:00
|
|
|
r> r> [ length neg ] 2apply adjust-stacks ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
2006-04-05 02:43:37 -04:00
|
|
|
: >phantom ( seq stack -- )
|
|
|
|
get swap [ dup value? [ get ] unless ] map nappend ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
|
|
|
: template-outputs ( stack stack -- )
|
2006-04-05 02:43:37 -04:00
|
|
|
2dup [ length ] 2apply adjust-stacks
|
|
|
|
phantom-r >phantom phantom-d >phantom ;
|
2006-04-03 01:33:52 -04:00
|
|
|
|
|
|
|
: with-template ( node in out quot -- )
|
2006-04-05 02:43:37 -04:00
|
|
|
swap >r >r >r dup node-in-d r> { } { } template-inputs
|
2006-04-03 03:22:33 -04:00
|
|
|
node set r> call r> { } template-outputs ; inline
|