1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays kernel continuations assocs namespaces
4 sequences words vocabs definitions hashtables init sets
5 math math.order classes classes.algebra classes.tuple
6 classes.tuple.private generic source-files.errors
10 SYMBOL: old-definitions
11 SYMBOL: new-definitions
13 TUPLE: redefine-error def ;
15 : redefine-error ( definition -- )
17 { { "Continue" t } } throw-restarts drop ;
21 : add-once ( key assoc -- )
22 2dup key? [ over redefine-error ] when conjoin ;
24 : (remember-definition) ( definition loc assoc -- )
25 [ over set-where ] dip add-once ;
29 : remember-definition ( definition loc -- )
30 new-definitions get first (remember-definition) ;
32 : fake-definition ( definition -- )
33 old-definitions get [ delete-at ] with each ;
35 : remember-class ( class loc -- )
36 [ dup new-definitions get first key? [ dup redefine-error ] when ] dip
37 new-definitions get second (remember-definition) ;
39 : forward-reference? ( word -- ? )
40 dup old-definitions get assoc-stack
41 [ new-definitions get assoc-stack not ]
46 HOOK: recompile compiler-impl ( words -- alist )
48 HOOK: to-recompile compiler-impl ( -- words )
50 HOOK: process-forgotten-words compiler-impl ( words -- )
52 : compile ( words -- ) recompile modify-code-heap ;
54 ! Non-optimizing compiler
56 [ dup def>> ] { } map>assoc ;
59 changed-definitions get [ drop word? ] assoc-filter
60 changed-generics get assoc-union keys ;
62 M: f process-forgotten-words drop ;
64 : without-optimizer ( quot -- )
65 [ f compiler-impl ] dip with-variable ; inline
67 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
69 SYMBOL: definition-observers
71 GENERIC: definitions-changed ( assoc obj -- )
73 [ V{ } clone definition-observers set-global ]
74 "compiler.units" add-startup-hook
76 ! This goes here because vocabs cannot depend on init
77 [ V{ } clone vocab-observers set-global ]
78 "vocabs" add-startup-hook
80 : add-definition-observer ( obj -- )
81 definition-observers get push ;
83 : remove-definition-observer ( obj -- )
84 definition-observers get remove-eq! drop ;
86 : notify-definition-observers ( assoc -- )
87 definition-observers get
88 [ definitions-changed ] with each ;
90 ! Incremented each time stack effects potentially changed, used
91 ! by compiler.tree.propagation.call-effect for call( and execute(
93 : effect-counter ( -- n ) 47 special-object ; inline
95 GENERIC: bump-effect-counter* ( defspec -- ? )
97 M: object bump-effect-counter* drop f ;
101 : changed-vocabs ( assoc -- vocabs )
102 [ drop word? ] assoc-filter
103 [ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
105 : updated-definitions ( -- assoc )
107 dup forgotten-definitions get update
108 dup new-definitions get first update
109 dup new-definitions get second update
110 dup changed-definitions get update
111 dup dup changed-vocabs update ;
113 : process-forgotten-definitions ( -- )
114 forgotten-definitions get keys
115 [ [ word? ] filter process-forgotten-words ]
116 [ [ delete-definition-errors ] each ]
119 : bump-effect-counter? ( -- ? )
120 changed-effects get new-words get assoc-diff assoc-empty? not
121 changed-definitions get [ drop bump-effect-counter* ] assoc-any?
124 : bump-effect-counter ( -- )
125 bump-effect-counter? [
126 47 special-object 0 or
128 47 set-special-object
131 : notify-observers ( -- )
132 updated-definitions dup assoc-empty?
133 [ drop ] [ notify-definition-observers notify-error-observers ] if ;
135 : finish-compilation-unit ( -- )
138 to-recompile recompile
140 process-forgotten-definitions
148 : with-nested-compilation-unit ( quot -- )
150 H{ } clone changed-definitions set
151 H{ } clone changed-generics set
152 H{ } clone changed-effects set
153 H{ } clone outdated-generics set
154 H{ } clone outdated-tuples set
155 H{ } clone new-words set
156 H{ } clone new-classes set
157 [ finish-compilation-unit ] [ ] cleanup
158 ] with-scope ; inline
160 : with-compilation-unit ( quot -- )
162 H{ } clone changed-definitions set
163 H{ } clone changed-generics set
164 H{ } clone changed-effects set
165 H{ } clone outdated-generics set
166 H{ } clone forgotten-definitions set
167 H{ } clone outdated-tuples set
168 H{ } clone new-words set
169 H{ } clone new-classes set
170 <definitions> new-definitions set
171 <definitions> old-definitions set
172 [ finish-compilation-unit ] [ ] cleanup
173 ] with-scope ; inline