factor/library/inference/print-dataflow.factor

86 lines
2.2 KiB
Factor
Raw Normal View History

2005-07-27 01:46:06 -04:00
IN: inference
USING: generic hashtables inference io kernel kernel-internals
2005-08-21 01:17:37 -04:00
lists math namespaces prettyprint sequences styles 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-21 01:17:37 -04:00
M: comment pprint* ( ann -- )
2005-08-02 06:32:48 -04:00
"( " over comment-text " )" append3
2005-08-21 01:17:37 -04:00
swap comment-node presented swons unit format ;
2005-08-02 06:32:48 -04:00
: comment, ( ? node text -- )
rot [ <comment> , ] [ 2drop ] ifte ;
2005-07-27 01:46:06 -04:00
: value-str ( classes values -- str )
[ 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
: #call>quot ( ? node -- )
dup node-param dup
[ , dup effect-str comment, ] [ 3drop ] ifte ;
2005-07-27 01:46:06 -04:00
M: #call node>quot ( ? node -- ) #call>quot ;
M: #call-label node>quot ( ? node -- ) #call>quot ;
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
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
M: #entry node>quot ( ? node -- ) "#entry" comment, ;
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.
2005-08-21 14:40:12 -04:00
>r dataflow optimize r> dataflow>quot . ;