1 ! Copyright (C) 2005, 2008 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 ;
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 ;
25 : annotate ( word quot -- )
26 over "unannotated-def" word-prop [
27 over cannot-annotate-twice
30 over dup def>> "unannotated-def" set-word-prop
31 [ dup def>> ] dip call define
32 ] with-compilation-unit ; inline
34 : word-inputs ( word -- seq )
36 [ datastack ] dip in>> length tail*
42 "/-- Entering: " write dup .
46 : word-outputs ( word -- seq )
48 [ datastack ] dip out>> length tail*
54 "/-- Leaving: " write dup .
58 : (watch) ( word def -- def )
59 over '[ _ entering @ _ leaving ] ;
62 dup [ (watch) ] annotate ;
64 : (watch-vars) ( word vars quot -- newquot )
66 "--- Entering: " write _ .
67 "--- Variable values:" print _ [ dup get ] H{ } map>assoc describe
71 : watch-vars ( word vars -- )
72 dupd '[ [ _ _ ] dip (watch-vars) ] annotate ;
74 GENERIC# annotate-methods 1 ( word quot -- )
76 M: generic annotate-methods
77 [ "methods" word-prop values ] dip [ annotate ] curry each ;
79 M: word annotate-methods
82 : breakpoint ( word -- )
83 [ add-breakpoint ] annotate-methods ;
85 : breakpoint-if ( word quot -- )
86 '[ [ _ [ [ break ] when ] ] dip 3append ] annotate-methods ;
90 word-timing global [ H{ } clone or ] change-at
92 : reset-word-timing ( -- )
93 word-timing get clear-assoc ;
95 : (add-timing) ( def word -- def' )
96 '[ _ benchmark _ word-timing get at+ ] ;
98 : add-timing ( word -- )
99 dup '[ _ (add-timing) ] annotate ;
101 : word-timing. ( -- )
103 >alist [ 1000000 /f ] assoc-map sort-values