factor/unfinished/compiler/cfg/builder/builder.factor

298 lines
7.3 KiB
Factor
Raw Normal View History

2008-09-10 23:11:03 -04:00
! Copyright (C) 2004, 2008 Slava Pestov.
2008-07-20 05:24:37 -04:00
! See http://factorcode.org/license.txt for BSD license.
2008-09-10 23:11:03 -04:00
USING: accessors arrays assocs combinators hashtables kernel
math fry namespaces make sequences words stack-checker.inlining
2008-08-11 03:49:37 -04:00
compiler.tree
2008-09-10 23:11:03 -04:00
compiler.tree.builder
2008-08-11 03:49:37 -04:00
compiler.tree.combinators
compiler.tree.propagation.info
compiler.cfg
2008-09-10 23:11:03 -04:00
compiler.cfg.stacks
compiler.cfg.templates
compiler.cfg.iterator
2008-09-15 02:54:48 -04:00
compiler.cfg.instructions
compiler.cfg.registers
compiler.alien ;
2008-07-20 05:24:37 -04:00
IN: compiler.cfg.builder
2008-09-10 23:11:03 -04:00
! Convert tree SSA IR to CFG (not quite SSA yet) IR.
2008-07-20 05:24:37 -04:00
: set-basic-block ( basic-block -- )
[ basic-block set ] [ instructions>> building set ] bi ;
: begin-basic-block ( -- )
2008-09-10 23:11:03 -04:00
<basic-block> basic-block get [
2008-07-20 05:24:37 -04:00
dupd successors>> push
] when*
set-basic-block ;
2008-09-10 23:11:03 -04:00
: end-basic-block ( -- )
building off
basic-block off ;
2008-07-20 05:24:37 -04:00
2008-09-11 03:05:22 -04:00
: stop-iterating ( -- next ) end-basic-block f ;
2008-09-10 23:11:03 -04:00
USE: qualified
FROM: compiler.generator.registers => +input+ ;
FROM: compiler.generator.registers => +output+ ;
FROM: compiler.generator.registers => +scratch+ ;
FROM: compiler.generator.registers => +clobber+ ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
SYMBOL: procedures
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
SYMBOL: current-word
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
SYMBOL: current-label
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
SYMBOL: loops
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
! Basic block after prologue, makes recursion faster
SYMBOL: current-label-start
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: add-procedure ( -- )
basic-block get current-word get current-label get
2008-09-11 03:05:22 -04:00
<cfg> procedures get push ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: begin-procedure ( word label -- )
end-basic-block
begin-basic-block
H{ } clone loops set
current-label set
current-word set
add-procedure ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: with-cfg-builder ( nodes word label quot -- )
'[ begin-procedure @ ] with-scope ; inline
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
GENERIC: emit-node ( node -- next )
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: check-basic-block ( node -- node' )
basic-block get [ drop f ] unless ; inline
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: emit-nodes ( nodes -- )
[ current-node emit-node check-basic-block ] iterate-nodes
finalize-phantoms ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: remember-loop ( label -- )
basic-block get swap loops get set-at ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: begin-word ( -- )
#! We store the basic block after the prologue as a loop
#! labelled by the current word, so that self-recursive
#! calls can skip an epilogue/prologue.
init-phantoms
%prologue
%branch
begin-basic-block
current-label get remember-loop ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: (build-cfg) ( nodes word label -- )
2008-07-20 05:24:37 -04:00
[
2008-09-10 23:11:03 -04:00
begin-word
[ emit-nodes ] with-node-iterator
] with-cfg-builder ;
: build-cfg ( nodes word label -- procedures )
V{ } clone [
procedures [
(build-cfg)
] with-variable
] keep ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: if-intrinsics ( #call -- quot )
word>> "if-intrinsics" word-prop ;
2008-09-11 03:05:22 -04:00
: local-recursive-call ( basic-block -- next )
2008-09-10 23:11:03 -04:00
%branch
basic-block get successors>> push
2008-09-11 03:05:22 -04:00
stop-iterating ;
2008-09-10 23:11:03 -04:00
: emit-call ( word -- next )
finalize-phantoms
{
{ [ tail-call? not ] [ 0 %frame-required %call iterate-next ] }
2008-09-11 03:05:22 -04:00
{ [ dup loops get key? ] [ loops get at local-recursive-call ] }
[ %epilogue %jump stop-iterating ]
2008-09-10 23:11:03 -04:00
} cond ;
! #recursive
: compile-recursive ( node -- next )
[ label>> id>> emit-call ]
[ [ child>> ] [ label>> word>> ] [ label>> id>> ] tri (build-cfg) ] bi ;
: compile-loop ( node -- next )
finalize-phantoms
begin-basic-block
[ label>> id>> remember-loop ] [ child>> emit-nodes ] bi
iterate-next ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
M: #recursive emit-node
dup label>> loop?>> [ compile-loop ] [ compile-recursive ] if ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
! #if
: emit-branch ( nodes -- final-bb )
[
begin-basic-block copy-phantoms
emit-nodes
basic-block get dup [ %branch ] when
] with-scope ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: emit-if ( node -- next )
children>> [ emit-branch ] map
2008-08-11 03:49:37 -04:00
end-basic-block
2008-09-10 23:11:03 -04:00
begin-basic-block
basic-block get '[ [ _ swap successors>> push ] when* ] each
init-phantoms
iterate-next ;
M: #if emit-node
{ { f "flag" } } lazy-load first %branch-t
emit-if ;
! #dispatch
: dispatch-branch ( nodes word -- label )
gensym [
[
copy-phantoms
%prologue
[ emit-nodes ] with-node-iterator
%epilogue
%return
] with-cfg-builder
] keep ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: dispatch-branches ( node -- )
children>> [
current-word get dispatch-branch
%dispatch-label
] each ;
: emit-dispatch ( node -- )
%dispatch dispatch-branches init-phantoms ;
M: #dispatch emit-node
#! The order here is important, dispatch-branches must
#! run after %dispatch, so that each branch gets the
#! correct register state
tail-call? [
emit-dispatch iterate-next
] [
current-word get gensym [
[
begin-word
emit-dispatch
] with-cfg-builder
] keep emit-call
] if ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
! #call
: define-intrinsics ( word intrinsics -- )
"intrinsics" set-word-prop ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: define-intrinsic ( word quot assoc -- )
2array 1array define-intrinsics ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: define-if-intrinsics ( word intrinsics -- )
[ +input+ associate ] assoc-map
"if-intrinsics" set-word-prop ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: define-if-intrinsic ( word quot inputs -- )
2array 1array define-if-intrinsics ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
: find-intrinsic ( #call -- pair/f )
word>> "intrinsics" word-prop find-template ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: find-boolean-intrinsic ( #call -- pair/f )
word>> "if-intrinsics" word-prop find-template ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: find-if-intrinsic ( #call -- pair/f )
node@ {
{ [ dup length 2 < ] [ 2drop f ] }
{ [ dup second #if? ] [ drop find-boolean-intrinsic ] }
[ 2drop f ]
} cond ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: do-if-intrinsic ( pair -- next )
[ %if-intrinsic ] apply-template skip-next emit-if ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
: do-boolean-intrinsic ( pair -- next )
2008-07-20 05:24:37 -04:00
[
2008-09-10 23:11:03 -04:00
f alloc-vreg [ %boolean-intrinsic ] keep phantom-push
] apply-template iterate-next ;
: do-intrinsic ( pair -- next )
[ %intrinsic ] apply-template iterate-next ;
: setup-operand-classes ( #call -- )
node-input-infos [ class>> ] map set-operand-classes ;
M: #call emit-node
dup setup-operand-classes
dup find-if-intrinsic [ do-if-intrinsic ] [
dup find-boolean-intrinsic [ do-boolean-intrinsic ] [
dup find-intrinsic [ do-intrinsic ] [
word>> emit-call
] ?if
] ?if
] ?if ;
! #call-recursive
M: #call-recursive emit-node label>> id>> emit-call ;
! #push
M: #push emit-node
literal>> <constant> phantom-push iterate-next ;
! #shuffle
M: #shuffle emit-node
shuffle-effect phantom-shuffle iterate-next ;
M: #>r emit-node
[ in-d>> length ] [ out-r>> empty? ] bi
[ phantom-drop ] [ phantom->r ] if
iterate-next ;
M: #r> emit-node
[ in-r>> length ] [ out-d>> empty? ] bi
[ phantom-rdrop ] [ phantom-r> ] if
iterate-next ;
! #return
M: #return emit-node
drop finalize-phantoms %epilogue %return f ;
M: #return-recursive emit-node
finalize-phantoms
label>> id>> loops get key?
[ %epilogue %return ] unless f ;
! #terminate
2008-09-11 03:05:22 -04:00
M: #terminate emit-node drop stop-iterating ;
2008-09-10 23:11:03 -04:00
! FFI
M: #alien-invoke emit-node
params>>
[ alien-invoke-frame %frame-required ]
[ %alien-invoke iterate-next ]
2008-07-20 05:24:37 -04:00
bi ;
2008-09-10 23:11:03 -04:00
M: #alien-indirect emit-node
params>>
[ alien-invoke-frame %frame-required ]
[ %alien-indirect iterate-next ]
2008-08-11 03:49:37 -04:00
bi ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
M: #alien-callback emit-node
params>> dup xt>> dup
[ init-phantoms %alien-callback ] with-cfg-builder
iterate-next ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
! No-op nodes
M: #introduce emit-node drop iterate-next ;
2008-07-20 05:24:37 -04:00
2008-09-10 23:11:03 -04:00
M: #copy emit-node drop iterate-next ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
M: #enter-recursive emit-node drop iterate-next ;
2008-08-11 03:49:37 -04:00
2008-09-10 23:11:03 -04:00
M: #phi emit-node drop iterate-next ;