factor/examples/print-dataflow.factor

94 lines
2.4 KiB
Factor
Raw Normal View History

2006-09-19 02:53:14 -04:00
IN: print-dataflow
USING: generic hashtables inference io kernel kernel-internals
2006-09-19 02:53:14 -04:00
math namespaces prettyprint sequences styles vectors words
test optimizer ;
2005-07-27 01:46:06 -04:00
! A simple tool for turning dataflow IR into quotations, for
! debugging purposes.
2006-08-15 04:57:12 -04:00
GENERIC: node>quot ( ? node -- )
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
TUPLE: comment node text ;
2005-07-27 01:46:06 -04:00
M: comment pprint*
2005-08-02 06:32:48 -04:00
"( " over comment-text " )" append3
2006-05-29 06:09:31 -04:00
swap comment-node presented associate
2006-08-06 22:30:52 -04:00
styled-text ;
2005-08-02 06:32:48 -04:00
: comment, ( ? node text -- )
2005-09-24 15:21:17 -04:00
rot [ <comment> , ] [ 2drop ] if ;
2005-07-27 01:46:06 -04:00
: values% ( prefix values -- )
[
swap %
dup value? [
value-literal unparse %
2006-01-21 16:16:49 -05:00
] [
"@" % #
2006-01-21 16:16:49 -05:00
] if
] each-with ;
2005-07-27 01:46:06 -04:00
: effect-str ( node -- str )
[
" " over node-in-d values%
" r: " over node-in-r values%
" --" %
" " over node-out-d values%
" r: " swap node-out-r values%
] "" make 1 tail ;
2005-07-27 01:46:06 -04:00
M: #shuffle node>quot
2005-09-04 17:07:59 -04:00
>r drop t r> dup effect-str "#shuffle: " swap append comment, ;
2005-07-27 01:46:06 -04:00
M: #push node>quot nip >#push< % ;
2006-04-17 17:17:34 -04:00
2005-07-27 01:46:06 -04:00
DEFER: dataflow>quot
: #call>quot ( ? node -- )
dup node-param dup
2005-09-24 15:21:17 -04:00
[ , dup effect-str comment, ] [ 3drop ] if ;
2005-07-27 01:46:06 -04:00
M: #call node>quot #call>quot ;
M: #call-label node>quot #call>quot ;
2005-07-27 01:46:06 -04:00
M: #label node>quot
2005-08-02 06:32:48 -04:00
[ "#label: " over node-param word-name append comment, ] 2keep
2005-09-04 01:09:46 -04:00
node-child swap dataflow>quot , \ call , ;
2005-07-27 01:46:06 -04:00
M: #if node>quot
2005-09-24 15:21:17 -04:00
[ "#if" comment, ] 2keep
node-children [ swap dataflow>quot ] map-with % \ if , ;
2005-07-27 01:46:06 -04:00
M: #dispatch node>quot
2005-08-02 06:32:48 -04:00
[ "#dispatch" comment, ] 2keep
node-children [ swap dataflow>quot ] map-with , \ dispatch , ;
2005-07-27 01:46:06 -04:00
M: #return node>quot
2005-09-10 00:55:46 -04:00
dup node-param unparse "#return " swap append comment, ;
2005-07-27 01:46:06 -04:00
M: object node>quot dup class word-name comment, ;
2005-09-18 01:37:28 -04:00
2005-08-02 06:32:48 -04:00
: (dataflow>quot) ( ? node -- )
dup [
2dup node>quot node-successor (dataflow>quot)
] [
2drop
2005-09-24 15:21:17 -04:00
] if ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
: dataflow>quot ( node ? -- quot )
2005-08-25 15:27:38 -04:00
[ swap (dataflow>quot) ] [ ] make ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
: dataflow. ( quot ? -- )
#! Print dataflow IR for a quotation. Flag indicates if
#! annotations should be printed or not.
2005-08-21 14:40:12 -04:00
>r dataflow optimize r> dataflow>quot . ;
2006-09-19 02:53:14 -04:00
[ ] [ [ 2 ] t dataflow. ] unit-test
[ ] [ [ 3 + ] t dataflow. ] unit-test
[ ] [ [ drop ] t dataflow. ] unit-test
[ ] [ [ [ sq ] [ abs ] if ] t dataflow. ] unit-test
[ ] [ [ { [ sq ] [ abs ] } dispatch ] t dataflow. ] unit-test
[ ] [ \ unify-values word-def t dataflow. ] unit-test
[ ] [ [ 0 0 / ] t dataflow. ] unit-test