]> gitweb.factorcode.org Git - factor.git/blob - core/compiler/units/units.factor
More changes so that mixins trigger even less recompilation
[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 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
7 kernel.private ;
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
17     { { "Continue" t } } throw-restarts drop ;
18
19 <PRIVATE
20
21 : add-once ( key assoc -- )
22     2dup key? [ over redefine-error ] when conjoin ;
23
24 : (remember-definition) ( definition loc assoc -- )
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-at ] with each ;
34
35 : remember-class ( class loc -- )
36     [ dup new-definitions get first key? [ dup redefine-error ] when ] dip
37     new-definitions get second (remember-definition) ;
38
39 : forward-reference? ( word -- ? )
40     dup old-definitions get assoc-stack
41     [ new-definitions get assoc-stack not ]
42     [ drop f ] if ;
43
44 SYMBOL: compiler-impl
45
46 HOOK: update-call-sites compiler-impl ( class generic -- words )
47
48 M: generic update-generic ( class generic -- )
49     [ update-call-sites [ changed-definition ] each ]
50     [ remake-generic drop ]
51     2bi ;
52
53 M: sequence update-methods ( class seq -- )
54     implementors [ update-generic ] with each ;
55
56 HOOK: recompile compiler-impl ( words -- alist )
57
58 HOOK: to-recompile compiler-impl ( -- words )
59
60 HOOK: process-forgotten-words compiler-impl ( words -- )
61
62 : compile ( words -- ) recompile modify-code-heap ;
63
64 ! Non-optimizing compiler
65 M: f update-call-sites
66     2drop { } ;
67
68 M: f to-recompile
69     changed-definitions get [ drop word? ] assoc-filter keys ;
70
71 M: f recompile
72     [ dup def>> ] { } map>assoc ;
73
74 M: f process-forgotten-words drop ;
75
76 : without-optimizer ( quot -- )
77     [ f compiler-impl ] dip with-variable ; inline
78
79 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
80
81 SYMBOL: definition-observers
82
83 GENERIC: definitions-changed ( assoc obj -- )
84
85 [ V{ } clone definition-observers set-global ]
86 "compiler.units" add-startup-hook
87
88 ! This goes here because vocabs cannot depend on init
89 [ V{ } clone vocab-observers set-global ]
90 "vocabs" add-startup-hook
91
92 : add-definition-observer ( obj -- )
93     definition-observers get push ;
94
95 : remove-definition-observer ( obj -- )
96     definition-observers get remove-eq! drop ;
97
98 : notify-definition-observers ( assoc -- )
99     definition-observers get
100     [ definitions-changed ] with each ;
101
102 ! Incremented each time stack effects potentially changed, used
103 ! by compiler.tree.propagation.call-effect for call( and execute(
104 ! inline caching
105 : effect-counter ( -- n ) 47 special-object ; inline
106
107 GENERIC: bump-effect-counter* ( defspec -- ? )
108
109 M: object bump-effect-counter* drop f ;
110
111 <PRIVATE
112
113 : changed-vocabs ( assoc -- vocabs )
114     [ drop word? ] assoc-filter
115     [ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
116
117 : updated-definitions ( -- assoc )
118     H{ } clone
119     dup forgotten-definitions get update
120     dup new-definitions get first update
121     dup new-definitions get second update
122     dup changed-definitions get update
123     dup dup changed-vocabs update ;
124
125 : process-forgotten-definitions ( -- )
126     forgotten-definitions get keys
127     [ [ word? ] filter process-forgotten-words ]
128     [ [ delete-definition-errors ] each ]
129     bi ;
130
131 : bump-effect-counter? ( -- ? )
132     changed-effects get new-words get assoc-diff assoc-empty? not
133     changed-definitions get [ drop bump-effect-counter* ] assoc-any?
134     or ;
135
136 : bump-effect-counter ( -- )
137     bump-effect-counter? [
138         47 special-object 0 or
139         1 +
140         47 set-special-object
141     ] when ;
142
143 : notify-observers ( -- )
144     updated-definitions dup assoc-empty?
145     [ drop ] [ notify-definition-observers notify-error-observers ] if ;
146
147 : finish-compilation-unit ( -- )
148     [ ] [
149         remake-generics
150         to-recompile recompile
151         update-tuples
152         process-forgotten-definitions
153         modify-code-heap
154         bump-effect-counter
155         notify-observers
156     ] if-bootstrapping ;
157
158 PRIVATE>
159
160 : with-nested-compilation-unit ( quot -- )
161     [
162         H{ } clone changed-definitions set
163         H{ } clone changed-effects set
164         H{ } clone outdated-generics set
165         H{ } clone outdated-tuples set
166         H{ } clone new-words set
167         [ finish-compilation-unit ] [ ] cleanup
168     ] with-scope ; inline
169
170 : with-compilation-unit ( quot -- )
171     [
172         H{ } clone changed-definitions set
173         H{ } clone changed-effects set
174         H{ } clone outdated-generics set
175         H{ } clone forgotten-definitions set
176         H{ } clone outdated-tuples set
177         H{ } clone new-words set
178         <definitions> new-definitions set
179         <definitions> old-definitions set
180         [ finish-compilation-unit ] [ ] cleanup
181     ] with-scope ; inline