1 ! Copyright (C) 2008, 2010 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 math
5 math.order classes classes.private classes.algebra classes.tuple
6 classes.tuple.private generic source-files.errors kernel.private ;
9 SYMBOL: old-definitions
10 SYMBOL: new-definitions
12 TUPLE: redefine-error def ;
14 : redefine-error ( definition -- )
16 { { "Continue" t } } throw-restarts drop ;
20 : add-once ( key assoc -- )
21 2dup key? [ over redefine-error ] when conjoin ;
23 : (remember-definition) ( definition loc assoc -- )
24 [ over set-where ] dip add-once ;
28 : remember-definition ( definition loc -- )
29 new-definitions get first (remember-definition) ;
31 : fake-definition ( definition -- )
32 old-definitions get [ delete-at ] with each ;
34 : remember-class ( class loc -- )
35 [ dup new-definitions get first key? [ dup redefine-error ] when ] dip
36 new-definitions get second (remember-definition) ;
38 : forward-reference? ( word -- ? )
39 dup old-definitions get assoc-stack
40 [ new-definitions get assoc-stack not ]
45 HOOK: update-call-sites compiler-impl ( class generic -- words )
47 : changed-call-sites ( class generic -- )
48 update-call-sites [ changed-definition ] each ;
50 M: generic update-generic ( class generic -- )
51 [ changed-call-sites ]
52 [ remake-generic drop ]
53 [ changed-conditionally drop ]
56 M: sequence update-methods ( class seq -- )
57 implementors [ update-generic ] with each ;
59 HOOK: recompile compiler-impl ( words -- alist )
61 HOOK: to-recompile compiler-impl ( -- words )
63 HOOK: process-forgotten-words compiler-impl ( words -- )
65 : compile ( words -- ) recompile modify-code-heap ;
67 ! Non-optimizing compiler
68 M: f update-call-sites
72 changed-definitions get [ drop word? ] assoc-filter keys ;
75 [ dup def>> ] { } map>assoc ;
77 M: f process-forgotten-words drop ;
79 : without-optimizer ( quot -- )
80 [ f compiler-impl ] dip with-variable ; inline
82 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
84 SYMBOL: definition-observers
86 GENERIC: definitions-changed ( assoc obj -- )
88 [ V{ } clone definition-observers set-global ]
89 "compiler.units" add-startup-hook
91 ! This goes here because vocabs cannot depend on init
92 [ V{ } clone vocab-observers set-global ]
93 "vocabs" add-startup-hook
95 : add-definition-observer ( obj -- )
96 definition-observers get push ;
98 : remove-definition-observer ( obj -- )
99 definition-observers get remove-eq! drop ;
101 : notify-definition-observers ( assoc -- )
102 definition-observers get
103 [ definitions-changed ] with each ;
105 ! Incremented each time stack effects potentially changed, used
106 ! by compiler.tree.propagation.call-effect for call( and execute(
108 : effect-counter ( -- n ) 47 special-object ; inline
110 GENERIC: always-bump-effect-counter? ( defspec -- ? )
112 M: object always-bump-effect-counter? drop f ;
116 : changed-vocabs ( assoc -- vocabs )
117 [ drop word? ] assoc-filter
118 [ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
120 : updated-definitions ( -- assoc )
122 dup forgotten-definitions get update
123 dup new-definitions get first update
124 dup new-definitions get second update
125 dup changed-definitions get update
126 dup maybe-changed get update
127 dup dup changed-vocabs update ;
129 : process-forgotten-definitions ( -- )
130 forgotten-definitions get keys
131 [ [ word? ] filter process-forgotten-words ]
132 [ [ delete-definition-errors ] each ]
135 : bump-effect-counter? ( -- ? )
138 changed-definitions get [ drop always-bump-effect-counter? ] assoc-filter
139 3array assoc-combine new-words get assoc-diff assoc-empty? not ;
141 : bump-effect-counter ( -- )
142 bump-effect-counter? [
143 47 special-object 0 or
145 47 set-special-object
148 : notify-observers ( -- )
149 updated-definitions dup assoc-empty?
150 [ drop ] [ notify-definition-observers notify-error-observers ] if ;
152 : finish-compilation-unit ( -- )
155 to-recompile recompile
157 process-forgotten-definitions
165 : with-nested-compilation-unit ( quot -- )
167 H{ } clone changed-definitions set
168 H{ } clone maybe-changed set
169 H{ } clone changed-effects set
170 H{ } clone outdated-generics set
171 H{ } clone outdated-tuples set
172 H{ } clone new-words set
173 [ finish-compilation-unit ] [ ] cleanup
174 ] with-scope ; inline
176 : with-compilation-unit ( quot -- )
178 H{ } clone changed-definitions set
179 H{ } clone maybe-changed set
180 H{ } clone changed-effects set
181 H{ } clone outdated-generics set
182 H{ } clone forgotten-definitions set
183 H{ } clone outdated-tuples set
184 H{ } clone new-words set
185 <definitions> new-definitions set
186 <definitions> old-definitions set
187 [ finish-compilation-unit ] [ ] cleanup
188 ] with-scope ; inline