1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math sorting words parser io summary
4 quotations sequences prettyprint continuations effects
5 definitions compiler.units namespaces assocs tools.walker
6 tools.time generic inspector fry tools.continuations ;
9 GENERIC: reset ( word -- )
13 [ subwords [ reset ] each ] bi ;
16 dup "unannotated-def" word-prop [
18 dup dup "unannotated-def" word-prop define
19 ] with-compilation-unit
20 f "unannotated-def" set-word-prop
23 ERROR: cannot-annotate-twice word ;
27 : check-annotate-twice ( word -- word )
28 dup "unannotated-def" word-prop [
32 : save-unannotated-def ( word -- )
33 dup def>> "unannotated-def" set-word-prop ;
35 : (annotate) ( word quot -- )
36 [ dup def>> ] dip call( old -- new ) define ;
40 : annotate ( word quot -- )
41 [ check-annotate-twice ] dip
42 [ over save-unannotated-def (annotate) ] with-compilation-unit ;
46 : stack-values ( names -- alist )
47 [ datastack ] dip [ nip ] [ length tail* ] 2bi zip ;
49 : trace-message ( word quot str -- )
50 "--- " write write bl over .
51 [ stack-effect ] dip '[ @ stack-values ] [ f ] if*
52 [ simple-table. ] unless-empty flush ; inline
54 : entering ( str -- ) [ in>> ] "Entering" trace-message ;
56 : leaving ( str -- ) [ out>> ] "Leaving" trace-message ;
58 : (watch) ( word def -- def )
59 over '[ _ entering @ _ leaving ] ;
64 dup '[ [ _ ] dip (watch) ] annotate ;
68 : (watch-vars) ( word vars quot -- newquot )
70 "--- Entering: " write _ .
71 "--- Variable values:" print _ [ dup get ] H{ } map>assoc describe
77 : watch-vars ( word vars -- )
78 dupd '[ [ _ _ ] dip (watch-vars) ] annotate ;
80 GENERIC# annotate-methods 1 ( word quot -- )
82 M: generic annotate-methods
83 [ "methods" word-prop values ] dip [ annotate ] curry each ;
85 M: word annotate-methods
88 : breakpoint ( word -- )
89 [ add-breakpoint ] annotate-methods ;
91 : breakpoint-if ( word quot -- )
92 '[ [ _ [ [ break ] when ] ] dip 3append ] annotate-methods ;
96 word-timing [ H{ } clone ] initialize
98 : reset-word-timing ( -- )
99 word-timing get clear-assoc ;
103 : (add-timing) ( def word -- def' )
104 '[ _ benchmark _ word-timing get at+ ] ;
108 : add-timing ( word -- )
109 dup '[ _ (add-timing) ] annotate ;
111 : word-timing. ( -- )
113 >alist [ 1000000 /f ] assoc-map sort-values