1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel namespaces arrays sequences io
4 words fry continuations vocabs assocs dlists definitions math
5 graphs generic combinators deques search-deques io
6 stack-checker stack-checker.state stack-checker.inlining
7 compiler.errors compiler.units compiler.tree.builder
8 compiler.tree.optimizer compiler.cfg.builder
9 compiler.cfg.optimizer compiler.cfg.linearization
10 compiler.cfg.two-operand compiler.cfg.linear-scan
11 compiler.cfg.stack-frame compiler.codegen compiler.utilities ;
17 : queue-compile ( word -- )
19 { [ dup "forgotten" word-prop ] [ ] }
20 { [ dup compiled get key? ] [ ] }
21 { [ dup inlined-block? ] [ ] }
22 { [ dup primitive? ] [ ] }
23 [ dup compile-queue get push-front ]
26 : maybe-compile ( word -- )
27 dup compiled>> [ drop ] [ queue-compile ] if ;
31 : ripple-up ( words -- )
32 dup "compiled-effect" word-prop +failed+ eq?
33 [ usage [ word? ] filter ] [ compiled-usage keys ] if
34 [ queue-compile ] each ;
36 : ripple-up? ( word effect -- ? )
37 #! If the word has previously been compiled and had a
38 #! different stack effect, we have to recompile any callers.
39 swap "compiled-effect" word-prop [ = not ] keep and ;
41 : save-effect ( word effect -- )
42 [ dupd ripple-up? [ ripple-up ] [ drop ] if ]
43 [ "compiled-effect" set-word-prop ]
47 "trace-compilation" get [ dup name>> print flush ] when
48 H{ } clone dependencies set
49 H{ } clone generic-dependencies set
50 f swap compiler-error ;
52 : fail ( word error -- )
53 [ swap compiler-error ]
57 [ f swap compiled get set-at ]
58 [ +failed+ save-effect ]
63 : frontend ( word -- effect nodes )
64 [ build-tree-from-word ] [ fail ] recover optimize-tree ;
66 ! Only switch this off for debugging.
67 SYMBOL: compile-dependencies?
69 t compile-dependencies? set-global
72 [ [ code>> ] [ label>> ] bi compiled get set-at ]
73 [ compile-dependencies? get [ calls>> [ maybe-compile ] each ] [ drop ] if ]
76 : backend ( nodes word -- )
87 : finish ( effect word -- )
94 generic-dependencies get
99 : (compile) ( word -- )
109 : compile-loop ( deque -- )
110 [ (compile) yield-hook get call ] slurp-deque ;
112 : decompile ( word -- )
113 f 2array 1array t modify-code-heap ;
115 : optimized-recompile-hook ( words -- alist )
117 <hashed-dlist> compile-queue set
118 H{ } clone compiled set
119 [ queue-compile ] each
120 compile-queue get compile-loop
124 : enable-compiler ( -- )
125 [ optimized-recompile-hook ] recompile-hook set-global ;
127 : disable-compiler ( -- )
128 [ default-recompile-hook ] recompile-hook set-global ;
130 : recompile-all ( -- )
131 forget-errors all-words compile ;