factor/library/compiler/linearizer.factor

77 lines
2.1 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2005-05-09 02:34:15 -04:00
IN: compiler-frontend
USING: compiler-backend errors generic lists inference kernel
math namespaces prettyprint sequences
2005-06-09 19:49:31 -04:00
strings words ;
2005-09-08 22:23:54 -04:00
GENERIC: linearize* ( node -- )
: linearize ( dataflow -- linear )
#! Transform dataflow IR into linear IR. This strips out
2005-05-17 16:13:08 -04:00
#! stack flow information, and flattens conditionals into
#! jumps and labels.
2005-09-08 22:23:54 -04:00
[ %prologue , linearize* ] { } make ;
2005-05-17 16:13:08 -04:00
2005-09-08 22:23:54 -04:00
: linearize-next node-successor linearize* ;
2004-12-05 23:00:52 -05:00
2005-09-08 22:23:54 -04:00
M: f linearize* ( f -- ) drop ;
2005-05-17 16:13:08 -04:00
2005-09-08 22:23:54 -04:00
M: node linearize* ( node -- ) linearize-next ;
2005-05-17 16:13:08 -04:00
2005-09-08 22:23:54 -04:00
M: #label linearize* ( node -- )
<label> dup %return-to , >r
dup node-param %label ,
dup node-child linearize*
r> %label ,
linearize-next ;
2004-12-05 23:00:52 -05:00
2005-09-08 22:23:54 -04:00
: ?tail-call ( node caller jumper -- next )
>r >r dup node-successor #return? [
node-param r> drop r> execute ,
] [
dup node-param r> execute , r> drop linearize-next
] ifte ; inline
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-08 22:23:54 -04:00
M: #call linearize* ( node -- )
dup intrinsic [
dupd call linearize-next
] [
\ %call \ %jump ?tail-call
] ifte* ;
2005-05-06 18:33:40 -04:00
2005-09-08 22:23:54 -04:00
M: #call-label linearize* ( node -- )
\ %call-label \ %jump-label ?tail-call ;
2005-06-09 19:49:31 -04:00
2005-09-08 22:23:54 -04:00
: ifte-head ( label -- ) in-1 -1 %inc-d , 0 %jump-t , ;
2005-05-06 18:33:40 -04:00
2005-09-08 22:23:54 -04:00
M: #ifte linearize* ( node -- )
node-children first2
<label> dup ifte-head
2005-09-08 22:23:54 -04:00
swap linearize* ( false branch )
%label , ( branch target of BRANCH-T )
2005-09-08 22:23:54 -04:00
linearize* ( true branch ) ;
: dispatch-head ( vtable -- label/code )
2004-12-05 21:17:09 -05:00
#! Output the jump table insn and return a list of
#! label/branch pairs.
in-1
2005-09-08 22:23:54 -04:00
-1 %inc-d ,
0 %untag-fixnum ,
0 %dispatch ,
2005-05-06 18:33:40 -04:00
[ <label> dup %target-label , cons ] map
%end-dispatch , ;
2004-12-05 21:17:09 -05:00
: dispatch-body ( label/param -- )
2005-09-08 22:23:54 -04:00
[ uncons %label , linearize* ] each ;
2004-12-13 16:28:28 -05:00
2005-09-08 22:23:54 -04:00
M: #dispatch linearize* ( vtable -- )
#! 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.
node-children dispatch-head dispatch-body ;
2005-09-08 22:23:54 -04:00
M: #return linearize* ( node -- )
drop f %return , ;