factor/library/compiler/linearizer.factor

120 lines
3.4 KiB
Factor
Raw Normal View History

2006-03-02 01:12:32 -05:00
! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays compiler-backend hashtables inference kernel
namespaces sequences words ;
2005-05-09 02:34:15 -04:00
IN: compiler-frontend
2006-03-02 01:12:32 -05:00
DEFER: #terminal?
PREDICATE: #merge #terminal-merge node-successor #terminal? ;
UNION: #terminal POSTPONE: f #return #values #terminal-merge ;
: tail-call? ( -- ? )
node-stack get [ node-successor ] map [ #terminal? ] all? ;
GENERIC: linearize* ( node -- next )
: linearize-child ( node -- )
[ node@ linearize* ] iterate-nodes ;
2005-12-04 19:56:42 -05:00
2005-12-21 02:43:41 -05:00
! A map from words to linear IR.
SYMBOL: linearized
! Renamed labels. To avoid problems with labels with the same
! name in different scopes.
SYMBOL: renamed-labels
2006-02-11 02:30:18 -05:00
: make-linear ( word quot -- )
2006-03-02 01:12:32 -05:00
[
swap >r [ %prologue , call ] { } make r>
linearized get set-hash
] with-node-iterator ; inline
2006-02-11 02:30:18 -05:00
2006-03-02 01:12:32 -05:00
: linearize-1 ( word node -- )
swap [ linearize-child ] make-linear ;
2005-12-21 02:43:41 -05:00
: init-linearizer ( -- )
H{ } clone linearized set
H{ } clone renamed-labels set ;
: linearize ( word dataflow -- linearized )
#! Outputs a hashtable mapping from labels to their
#! respective linear IR.
init-linearizer linearize-1 linearized get ;
2005-05-17 16:13:08 -04:00
2006-03-02 01:12:32 -05:00
M: node linearize* ( node -- next ) drop iterate-next ;
2004-12-05 23:00:52 -05:00
2006-03-02 01:12:32 -05:00
: linearize-call ( label -- next )
tail-call? [
%jump , f
] [
%call , iterate-next
] if ;
2005-05-17 16:13:08 -04:00
2006-03-02 01:12:32 -05:00
: rename-label ( label -- label )
<label> dup rot renamed-labels get set-hash ;
2005-05-17 16:13:08 -04:00
2006-03-02 01:12:32 -05:00
: renamed-label ( label -- label )
renamed-labels get hash ;
2005-12-21 02:43:41 -05:00
2006-03-02 01:12:32 -05:00
: linearize-call-label ( label -- next )
rename-label linearize-call ;
2005-12-21 02:43:41 -05:00
2006-03-02 01:12:32 -05:00
M: #label linearize* ( node -- next )
#! We remap the IR node's label to a new label object here,
#! to avoid problems with two IR #label nodes having the
#! same label in different lexical scopes.
2006-03-02 01:12:32 -05:00
dup node-param dup linearize-call-label >r
renamed-label swap node-child linearize-1
r> ;
: in-1 0 0 %peek-d , ;
: in-2 0 1 %peek-d , 1 0 %peek-d , ;
: in-3 0 2 %peek-d , 1 1 %peek-d , 2 0 %peek-d , ;
: out-1 T{ vreg f 0 } 0 %replace-d , ;
2005-06-08 04:49:05 -04:00
2005-09-08 22:23:54 -04:00
: intrinsic ( #call -- quot ) node-param "intrinsic" word-prop ;
2005-06-08 04:49:05 -04:00
2005-09-24 15:21:17 -04:00
: if-intrinsic ( #call -- quot )
dup node-successor #if?
[ node-param "if-intrinsic" word-prop ] [ drop f ] if ;
2006-03-02 01:12:32 -05:00
: linearize-if ( node label -- next )
<label> dup >r >r >r node-children first2 linearize-child
r> r> %jump-label , %label , linearize-child r> %label ,
iterate-next ;
2005-09-08 22:23:54 -04:00
M: #call linearize* ( node -- )
2005-09-24 15:21:17 -04:00
dup if-intrinsic [
>r <label> 2dup r> call
2005-09-24 15:21:17 -04:00
>r node-successor r> linearize-if
2005-09-08 22:23:54 -04:00
] [
2006-03-02 01:12:32 -05:00
dup intrinsic
[ call iterate-next ] [ node-param linearize-call ] if*
2005-09-24 15:21:17 -04:00
] if* ;
2005-05-06 18:33:40 -04:00
2006-03-02 01:12:32 -05:00
M: #call-label linearize* ( node -- next )
node-param renamed-label linearize-call ;
2005-06-09 19:49:31 -04:00
2006-03-02 01:12:32 -05:00
M: #if linearize* ( node -- next )
2006-02-28 00:26:45 -05:00
in-1 -1 %inc-d , <label> dup 0 %jump-t , linearize-if ;
2006-03-02 01:12:32 -05:00
: dispatch-head ( vtable -- label/node )
2004-12-05 21:17:09 -05:00
#! Output the jump table insn and return a list of
#! label/branch pairs.
2006-03-02 01:12:32 -05:00
in-1 -1 %inc-d , 0 %dispatch ,
2006-02-28 00:26:45 -05:00
[ <label> dup %target-label , 2array ] map ;
2004-12-05 21:17:09 -05:00
2006-03-02 01:12:32 -05:00
: dispatch-body ( label/node -- )
2006-02-28 00:26:45 -05:00
<label> swap [
2006-03-02 01:12:32 -05:00
first2 %label , linearize-child dup %jump-label ,
2006-02-28 00:26:45 -05:00
] each %label , ;
2004-12-13 16:28:28 -05:00
2006-03-02 01:12:32 -05:00
M: #dispatch linearize* ( node -- next )
2005-09-08 22:23:54 -04:00
#! The parameter is a list of nodes, each one is a branch to
2004-12-05 21:17:09 -05:00
#! take in case the top of stack has that type.
2006-03-02 01:12:32 -05:00
node-children dispatch-head dispatch-body iterate-next ;
2006-03-02 01:12:32 -05:00
M: #return linearize* drop %return , f ;