]> gitweb.factorcode.org Git - factor.git/blob - core/compiler/units/units.factor
core: fix bad interaction between stage1 bootstrap and manifest update code
[factor.git] / core / compiler / units / units.factor
1 ! Copyright (C) 2008, 2009 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: recompile compiler-impl ( words -- alist )
47
48 HOOK: to-recompile compiler-impl ( -- words )
49
50 HOOK: process-forgotten-words compiler-impl ( words -- )
51
52 : compile ( words -- ) recompile modify-code-heap ;
53
54 ! Non-optimizing compiler
55 M: f recompile
56     [ dup def>> ] { } map>assoc ;
57
58 M: f to-recompile
59     changed-definitions get [ drop word? ] assoc-filter
60     changed-generics get assoc-union keys ;
61
62 M: f process-forgotten-words drop ;
63
64 : without-optimizer ( quot -- )
65     [ f compiler-impl ] dip with-variable ; inline
66
67 : <definitions> ( -- pair ) { H{ } H{ } } [ clone ] map ;
68
69 SYMBOL: definition-observers
70
71 GENERIC: definitions-changed ( assoc obj -- )
72
73 [ V{ } clone definition-observers set-global ]
74 "compiler.units" add-startup-hook
75
76 ! This goes here because vocabs cannot depend on init
77 [ V{ } clone vocab-observers set-global ]
78 "vocabs" add-startup-hook
79
80 : add-definition-observer ( obj -- )
81     definition-observers get push ;
82
83 : remove-definition-observer ( obj -- )
84     definition-observers get remove-eq! drop ;
85
86 : notify-definition-observers ( assoc -- )
87     definition-observers get
88     [ definitions-changed ] with each ;
89
90 ! Incremented each time stack effects potentially changed, used
91 ! by compiler.tree.propagation.call-effect for call( and execute(
92 ! inline caching
93 : effect-counter ( -- n ) 47 special-object ; inline
94
95 GENERIC: bump-effect-counter* ( defspec -- ? )
96
97 M: object bump-effect-counter* drop f ;
98
99 <PRIVATE
100
101 : changed-vocabs ( assoc -- vocabs )
102     [ drop word? ] assoc-filter
103     [ drop vocabulary>> dup [ vocab ] when dup ] assoc-map ;
104
105 : updated-definitions ( -- assoc )
106     H{ } clone
107     dup forgotten-definitions get update
108     dup new-definitions get first update
109     dup new-definitions get second update
110     dup changed-definitions get update
111     dup dup changed-vocabs update ;
112
113 : process-forgotten-definitions ( -- )
114     forgotten-definitions get keys
115     [ [ word? ] filter process-forgotten-words ]
116     [ [ delete-definition-errors ] each ]
117     bi ;
118
119 : bump-effect-counter? ( -- ? )
120     changed-effects get new-words get assoc-diff assoc-empty? not
121     changed-definitions get [ drop bump-effect-counter* ] assoc-any?
122     or ;
123
124 : bump-effect-counter ( -- )
125     bump-effect-counter? [
126         47 special-object 0 or
127         1 +
128         47 set-special-object
129     ] when ;
130
131 : notify-observers ( -- )
132     updated-definitions dup assoc-empty?
133     [ drop ] [ notify-definition-observers notify-error-observers ] if ;
134
135 : finish-compilation-unit ( -- )
136     [ ] [
137         remake-generics
138         to-recompile recompile
139         update-tuples
140         process-forgotten-definitions
141         modify-code-heap
142         bump-effect-counter
143         notify-observers
144     ] if-bootstrapping ;
145
146 PRIVATE>
147
148 : with-nested-compilation-unit ( quot -- )
149     [
150         H{ } clone changed-definitions set
151         H{ } clone changed-generics set
152         H{ } clone changed-effects set
153         H{ } clone outdated-generics set
154         H{ } clone outdated-tuples set
155         H{ } clone new-words set
156         H{ } clone new-classes set
157         [ finish-compilation-unit ] [ ] cleanup
158     ] with-scope ; inline
159
160 : with-compilation-unit ( quot -- )
161     [
162         H{ } clone changed-definitions set
163         H{ } clone changed-generics set
164         H{ } clone changed-effects set
165         H{ } clone outdated-generics set
166         H{ } clone forgotten-definitions set
167         H{ } clone outdated-tuples set
168         H{ } clone new-words set
169         H{ } clone new-classes set
170         <definitions> new-definitions set
171         <definitions> old-definitions set
172         [ finish-compilation-unit ] [ ] cleanup
173     ] with-scope ; inline