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