2005-02-22 23:07:47 -05:00
|
|
|
! 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
|
2005-08-03 18:47:32 -04:00
|
|
|
USING: compiler-backend errors generic lists inference kernel
|
2005-08-12 18:02:03 -04:00
|
|
|
math namespaces prettyprint sequences
|
2005-06-09 19:49:31 -04:00
|
|
|
strings words ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
GENERIC: linearize-node* ( node -- )
|
2005-08-04 17:39:39 -04:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
M: f linearize-node* ( f -- ) drop ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
2005-08-04 17:39:39 -04:00
|
|
|
M: node linearize-node* ( node -- ) drop ;
|
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
: linearize-node ( node -- )
|
|
|
|
[
|
|
|
|
dup linearize-node* node-successor linearize-node
|
|
|
|
] when* ;
|
2004-12-01 19:48:08 -05:00
|
|
|
|
|
|
|
: 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-08-25 15:27:38 -04:00
|
|
|
[ %prologue , linearize-node ] [ ] make ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
|
|
|
M: #label linearize-node* ( node -- )
|
2005-05-06 18:33:40 -04:00
|
|
|
<label> dup %return-to , >r
|
2005-05-17 16:13:08 -04:00
|
|
|
dup node-param %label ,
|
2005-09-04 01:09:46 -04:00
|
|
|
node-child linearize-node
|
2005-05-06 18:33:40 -04:00
|
|
|
r> %label , ;
|
2004-12-05 23:00:52 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
M: #call linearize-node* ( node -- )
|
|
|
|
dup node-param
|
2005-08-11 19:08:22 -04:00
|
|
|
dup "intrinsic" word-prop [ call ] [ %call , drop ] ?ifte ;
|
2005-05-17 16:13:08 -04:00
|
|
|
|
|
|
|
M: #call-label linearize-node* ( node -- )
|
|
|
|
node-param %call-label , ;
|
|
|
|
|
|
|
|
: 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 ;
|
2004-12-05 23:00:52 -05:00
|
|
|
|
2005-06-08 04:49:05 -04:00
|
|
|
GENERIC: load-value ( vreg n value -- )
|
|
|
|
|
2005-06-09 19:49:31 -04:00
|
|
|
M: object load-value ( vreg n value -- )
|
2005-06-08 04:49:05 -04:00
|
|
|
drop %peek-d , ;
|
|
|
|
|
2005-06-09 19:49:31 -04:00
|
|
|
: push-literal ( vreg value -- )
|
|
|
|
literal-value dup
|
2005-05-17 16:13:08 -04:00
|
|
|
immediate? [ %immediate ] [ %indirect ] ifte , ;
|
2005-05-06 18:33:40 -04:00
|
|
|
|
2005-08-07 00:00:57 -04:00
|
|
|
M: literal load-value ( vreg n value -- )
|
2005-06-09 19:49:31 -04:00
|
|
|
nip push-literal ;
|
|
|
|
|
2005-05-08 00:21:00 -04:00
|
|
|
: ifte-head ( label -- )
|
2005-09-04 17:07:59 -04:00
|
|
|
in-1 -1 %inc-d, 0 %jump-t , ;
|
2005-05-06 18:33:40 -04:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
M: #ifte linearize-node* ( node -- )
|
2005-09-02 23:44:23 -04:00
|
|
|
node-children first2
|
2005-08-11 19:08:22 -04:00
|
|
|
<label> dup ifte-head
|
|
|
|
swap linearize-node ( false branch )
|
|
|
|
%label , ( branch target of BRANCH-T )
|
|
|
|
linearize-node ( 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.
|
2005-05-06 19:49:07 -04:00
|
|
|
in-1
|
2005-09-04 17:07:59 -04:00
|
|
|
-1 %inc-d,
|
2005-05-08 00:21:00 -04:00
|
|
|
0 %untag-fixnum ,
|
2005-05-06 19:49:07 -04:00
|
|
|
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
|
|
|
|
2005-08-11 19:08:22 -04:00
|
|
|
: dispatch-body ( label/param -- )
|
2004-12-05 21:17:09 -05:00
|
|
|
#! Output each branch, with a jump to the end label.
|
2005-08-11 19:08:22 -04:00
|
|
|
[ uncons %label , linearize-node ] each ;
|
2004-12-13 16:28:28 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
M: #dispatch linearize-node* ( vtable -- )
|
2004-12-05 21:17:09 -05:00
|
|
|
#! The parameter is a list of lists, each one is a branch to
|
|
|
|
#! take in case the top of stack has that type.
|
2005-08-11 19:08:22 -04:00
|
|
|
node-children dispatch-head dispatch-body ;
|
2004-12-08 18:39:36 -05:00
|
|
|
|
2005-05-17 16:13:08 -04:00
|
|
|
M: #return linearize-node* ( node -- )
|
|
|
|
drop f %return , ;
|