factor/library/inference/print-dataflow.factor

87 lines
2.3 KiB
Factor
Raw Normal View History

2005-07-27 01:46:06 -04:00
IN: inference
USING: generic hashtables inference io kernel kernel-internals
math namespaces prettyprint sequences vectors words ;
2005-07-27 01:46:06 -04:00
! A simple tool for turning dataflow IR into quotations, for
! debugging purposes.
GENERIC: node>quot ( node -- )
2005-08-02 06:32:48 -04:00
TUPLE: comment node text ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: comment prettyprint* ( ann -- )
"( " over comment-text " )" append3
swap comment-node object. ;
: comment, ( ? node text -- )
rot [ <comment> , ] [ 2drop ] ifte ;
2005-07-27 01:46:06 -04:00
: value-str ( classes values -- str )
2005-07-28 18:20:31 -04:00
[ swap ?hash [ object ] unless* ] map-with
[ word-name ] map
" " join ;
2005-07-27 01:46:06 -04:00
: effect-str ( node -- str )
[
dup node-classes swap
2dup node-in-d value-str %
"--" %
2005-07-27 01:46:06 -04:00
node-out-d value-str %
] make-string ;
2005-08-02 06:32:48 -04:00
M: #push node>quot ( ? node -- )
2005-08-04 00:48:07 -04:00
node-out-d [ literal-value literalize ] map % drop ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #drop node>quot ( ? node -- )
2005-07-27 01:46:06 -04:00
node-in-d length dup 3 > [
\ drop <repeated>
2005-07-27 01:46:06 -04:00
] [
{ f [ drop ] [ 2drop ] [ 3drop ] } nth
2005-08-02 06:32:48 -04:00
] ifte % drop ;
2005-07-27 01:46:06 -04:00
DEFER: dataflow>quot
2005-08-02 06:32:48 -04:00
M: #call node>quot ( ? node -- )
dup node-param , dup effect-str comment, ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #call-label node>quot ( ? node -- )
#! Even if the flag is off, we still output the annotation.
>r drop t r>
"#call-label: " over node-param word-name append comment, ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #label node>quot ( ? node -- )
[ "#label: " over node-param word-name append comment, ] 2keep
node-children first swap dataflow>quot , \ call , ;
2005-07-27 01:46:06 -04:00
M: #simple-label node>quot ( ? node -- )
node-children first swap dataflow>quot % ;
2005-08-02 06:32:48 -04:00
M: #ifte node>quot ( ? node -- )
[ "#ifte" comment, ] 2keep
node-children [ swap dataflow>quot ] map-with % \ ifte , ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #dispatch node>quot ( ? node -- )
[ "#dispatch" comment, ] 2keep
node-children [ swap dataflow>quot ] map-with , \ dispatch , ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #return node>quot ( ? node -- ) "#return" comment, ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #values node>quot ( ? node -- ) "#values" comment, ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
M: #merge node>quot ( ? node -- ) "#merge" comment, ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
: (dataflow>quot) ( ? node -- )
dup [
2dup node>quot node-successor (dataflow>quot)
] [
2drop
] ifte ;
2005-07-27 01:46:06 -04:00
2005-08-02 06:32:48 -04:00
: dataflow>quot ( node ? -- quot )
[ swap (dataflow>quot) ] make-list ;
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.
>r dataflow optimize r> dataflow>quot prettyprint ;