]> gitweb.factorcode.org Git - factor.git/blob - core/compiler/units/units.factor
core/basis: trim down using lists
[factor.git] / core / compiler / units / units.factor
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 init kernel kernel.private math namespaces sequences
6 sets source-files.errors vocabs words ;
7 FROM: namespaces => set ;
8 IN: compiler.units
9
10 SYMBOL: old-definitions
11 SYMBOL: new-definitions
12
13 TUPLE: redefine-error def ;
14
15 : redefine-error ( definition -- )
16     \ redefine-error boa throw-continue ;
17
18 <PRIVATE
19
20 : add-once ( key assoc -- )
21     2dup key? [ over redefine-error ] when conjoin ;
22
23 : (remember-definition) ( definition loc assoc -- )
24     [ over set-where ] dip add-once ;
25
26 PRIVATE>
27
28 : remember-definition ( definition loc -- )
29     new-definitions get first (remember-definition) ;
30
31 : fake-definition ( definition -- )
32     old-definitions get [ delete-at ] with each ;
33
34 : remember-class ( class loc -- )
35     [ dup new-definitions get first key? [ dup redefine-error ] when ] dip
36     new-definitions get second (remember-definition) ;
37
38 : forward-reference? ( word -- ? )
39     dup old-definitions get assoc-stack
40     [ new-definitions get assoc-stack not ]
41     [ drop f ] if ;
42
43 SYMBOL: compiler-impl
44
45 HOOK: update-call-sites compiler-impl ( class generic -- words )
46
47 : changed-call-sites ( class generic -- )
48     update-call-sites [ changed-definition ] each ;
49
50 M: generic update-generic ( class generic -- )
51     [ changed-call-sites ]
52     [ remake-generic drop ]
53     [ changed-conditionally drop ]
54     2tri ;
55
56 M: sequence update-methods ( class seq -- )
57     implementors [ update-generic ] with each ;
58
59 HOOK: recompile compiler-impl ( words -- alist )
60
61 HOOK: to-recompile compiler-impl ( -- words )
62
63 HOOK: process-forgotten-words compiler-impl ( words -- )
64
65 : compile ( words -- )
66     recompile t f modify-code-heap ;
67
68 ! Non-optimizing compiler
69 M: f update-call-sites
70     2drop { } ;
71
72 M: f to-recompile
73     changed-definitions get [ drop word? ] assoc-filter keys ;
74
75 M: f recompile
76     [ dup def>> ] { } map>assoc ;
77
78 M: f process-forgotten-words drop ;
79
80 : without-optimizer ( quot -- )
81     [ f compiler-impl ] dip with-variable ; inline
82
83 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
84
85 SYMBOL: definition-observers
86
87 GENERIC: definitions-changed ( assoc obj -- )
88
89 [ V{ } clone definition-observers set-global ]
90 "compiler.units" add-startup-hook
91
92 ! This goes here because vocabs cannot depend on init
93 [ V{ } clone vocab-observers set-global ]
94 "vocabs" add-startup-hook
95
96 : add-definition-observer ( obj -- )
97     definition-observers get push ;
98
99 : remove-definition-observer ( obj -- )
100     definition-observers get remove-eq! drop ;
101
102 : notify-definition-observers ( assoc -- )
103     definition-observers get
104     [ definitions-changed ] with each ;
105
106 ! Incremented each time stack effects potentially changed, used
107 ! by compiler.tree.propagation.call-effect for call( and execute(
108 ! inline caching
109 : effect-counter ( -- n ) REDEFINITION-COUNTER special-object ; inline
110
111 GENERIC: always-bump-effect-counter? ( defspec -- ? )
112
113 M: object always-bump-effect-counter? drop f ;
114
115 <PRIVATE
116
117 : changed-vocabs ( assoc -- vocabs )
118     [ drop word? ] assoc-filter
119     [ drop vocabulary>> dup [ lookup-vocab ] when dup ] assoc-map ;
120
121 : updated-definitions ( -- assoc )
122     H{ } clone
123     forgotten-definitions get assoc-union!
124     new-definitions get first assoc-union!
125     new-definitions get second assoc-union!
126     changed-definitions get assoc-union!
127     maybe-changed get assoc-union!
128     dup changed-vocabs assoc-union! ;
129
130 : process-forgotten-definitions ( -- )
131     forgotten-definitions get keys
132     [ [ word? ] filter process-forgotten-words ]
133     [ [ delete-definition-errors ] each ]
134     bi ;
135
136 : bump-effect-counter? ( -- ? )
137     changed-effects get
138     maybe-changed get
139     changed-definitions get [ drop always-bump-effect-counter? ] assoc-filter
140     3array assoc-combine new-words get assoc-diff assoc-empty? not ;
141
142 : bump-effect-counter ( -- )
143     bump-effect-counter? [
144         REDEFINITION-COUNTER special-object 0 or
145         1 + REDEFINITION-COUNTER set-special-object
146     ] when ;
147
148 : notify-observers ( -- )
149     updated-definitions dup assoc-empty?
150     [ drop ] [ notify-definition-observers notify-error-observers ] if ;
151
152 : update-existing? ( defs -- ? )
153     new-words get keys diff empty? not ;
154
155 : reset-pics? ( -- ? )
156     outdated-generics get assoc-empty? not ;
157
158 : finish-compilation-unit ( -- )
159     [ ] [
160         remake-generics
161         to-recompile [
162             recompile
163             update-tuples
164             process-forgotten-definitions
165         ] keep update-existing? reset-pics? modify-code-heap
166         bump-effect-counter
167         notify-observers
168     ] if-bootstrapping ;
169
170 TUPLE: nesting-observer new-words ;
171
172 M: nesting-observer definitions-changed new-words>> swap assoc-diff! drop ;
173
174 : add-nesting-observer ( -- )
175     new-words get nesting-observer boa
176     [ nesting-observer set ] [ add-definition-observer ] bi ;
177
178 : remove-nesting-observer ( -- )
179     nesting-observer get remove-definition-observer ;
180
181 PRIVATE>
182
183 : with-nested-compilation-unit ( quot -- )
184     [
185         H{ } clone changed-definitions set
186         H{ } clone maybe-changed set
187         H{ } clone changed-effects set
188         H{ } clone outdated-generics set
189         H{ } clone outdated-tuples set
190         H{ } clone new-words set
191         add-nesting-observer
192         [
193             remove-nesting-observer
194             finish-compilation-unit
195         ] [ ] cleanup
196     ] with-scope ; inline
197
198 : with-compilation-unit ( quot -- )
199     [
200         <definitions> new-definitions set
201         <definitions> old-definitions set
202         H{ } clone forgotten-definitions set
203         with-nested-compilation-unit
204     ] with-scope ; inline