1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs classes classes.private
4 classes.tuple classes.tuple.private continuations definitions
5 generic hash-sets init kernel kernel.private math namespaces
6 sequences sets source-files.errors vocabs words ;
7 FROM: namespaces => set ;
8 FROM: sets => members ;
11 PRIMITIVE: modify-code-heap ( alist update-existing? reset-pics? -- )
13 SYMBOL: old-definitions
14 SYMBOL: new-definitions
16 TUPLE: redefine-error def ;
18 : throw-redefine-error ( definition -- )
19 redefine-error boa throw-continue ;
23 : add-once ( key set -- )
24 dupd ?adjoin [ drop ] [ throw-redefine-error ] if ;
26 : (remember-definition) ( definition loc set -- )
27 [ over set-where ] dip add-once ;
31 : remember-definition ( definition loc -- )
32 new-definitions get first (remember-definition) ;
34 : fake-definition ( definition -- )
35 old-definitions get [ delete ] with each ;
37 : remember-class ( class loc -- )
38 new-definitions get first2
39 [ dupd in? [ dup throw-redefine-error ] when ]
40 [ (remember-definition) ] bi-curry* bi* ;
42 : forward-reference? ( word -- ? )
43 dup old-definitions get [ in? ] with any? [
44 new-definitions get [ in? ] with any? not
49 HOOK: update-call-sites compiler-impl ( class generic -- words )
51 : changed-call-sites ( class generic -- )
52 update-call-sites [ changed-definition ] each ;
54 M: generic update-generic ( class generic -- )
55 [ changed-call-sites ]
56 [ remake-generic drop ]
57 [ changed-conditionally drop ]
60 M: sequence update-methods ( class seq -- )
61 implementors [ update-generic ] with each ;
63 HOOK: recompile compiler-impl ( words -- alist )
65 HOOK: to-recompile compiler-impl ( -- words )
67 HOOK: process-forgotten-words compiler-impl ( words -- )
69 : compile ( words -- )
70 recompile t f modify-code-heap ;
72 ! Non-optimizing compiler
73 M: f update-call-sites
77 changed-definitions get members [ word? ] filter ;
80 [ dup def>> ] { } map>assoc ;
82 M: f process-forgotten-words drop ;
84 : without-optimizer ( quot -- )
85 [ f compiler-impl ] dip with-variable ; inline
87 : <definitions> ( -- pair ) { HS{ } HS{ } } [ clone ] map ;
89 SYMBOL: definition-observers
91 GENERIC: definitions-changed ( set obj -- )
93 [ V{ } clone definition-observers set-global ]
94 "compiler.units" add-startup-hook
96 ! This goes here because vocabs cannot depend on init
97 [ V{ } clone vocab-observers set-global ]
98 "vocabs" add-startup-hook
100 : add-definition-observer ( obj -- )
101 definition-observers get push ;
103 : remove-definition-observer ( obj -- )
104 definition-observers get remove-eq! drop ;
106 : notify-definition-observers ( set -- )
107 definition-observers get
108 [ definitions-changed ] with each ;
110 ! Incremented each time stack effects potentially changed, used
111 ! by compiler.tree.propagation.call-effect for call( and execute(
113 : effect-counter ( -- n ) REDEFINITION-COUNTER special-object ; inline
115 GENERIC: always-bump-effect-counter? ( defspec -- ? )
117 M: object always-bump-effect-counter? drop f ;
121 : changed-vocabs ( set -- vocabs )
122 members [ word? ] filter
123 [ vocabulary>> dup [ lookup-vocab ] when ] map ;
125 : updated-definitions ( -- set )
127 forgotten-definitions get union!
128 new-definitions get first union!
129 new-definitions get second union!
130 changed-definitions get union!
131 maybe-changed get union!
132 dup changed-vocabs over adjoin-all ;
134 : process-forgotten-definitions ( -- )
135 forgotten-definitions get members
136 [ [ word? ] filter process-forgotten-words ]
137 [ [ delete-definition-errors ] each ]
140 : bump-effect-counter? ( -- ? )
141 changed-effects get members
142 maybe-changed get members
143 changed-definitions get members [ always-bump-effect-counter? ] filter
144 3array combine new-words get [ in? not ] curry any? ;
146 : bump-effect-counter ( -- )
147 bump-effect-counter? [
148 REDEFINITION-COUNTER special-object 0 or
149 1 + REDEFINITION-COUNTER set-special-object
152 : notify-observers ( -- )
153 updated-definitions dup null?
154 [ drop ] [ notify-definition-observers notify-error-observers ] if ;
156 : update-existing? ( defs -- ? )
157 new-words get [ in? not ] curry any? ;
159 : reset-pics? ( -- ? )
160 outdated-generics get null? not ;
162 : finish-compilation-unit ( -- )
168 process-forgotten-definitions
169 ] keep update-existing? reset-pics? modify-code-heap
174 TUPLE: nesting-observer { new-words hash-set } ;
176 M: nesting-observer definitions-changed
177 [ members ] dip new-words>> [ delete ] curry each ;
179 : add-nesting-observer ( -- )
180 new-words get nesting-observer boa
181 [ nesting-observer set ] [ add-definition-observer ] bi ;
183 : remove-nesting-observer ( -- )
184 nesting-observer get remove-definition-observer ;
188 : with-nested-compilation-unit ( quot -- )
190 HS{ } clone changed-definitions set
191 HS{ } clone maybe-changed set
192 HS{ } clone changed-effects set
193 HS{ } clone outdated-generics set
194 H{ } clone outdated-tuples set
195 HS{ } clone new-words set
198 remove-nesting-observer
199 finish-compilation-unit
201 ] with-scope ; inline
203 : with-compilation-unit ( quot -- )
205 <definitions> new-definitions set
206 <definitions> old-definitions set
207 HS{ } clone forgotten-definitions set
208 with-nested-compilation-unit
209 ] with-scope ; inline