OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 class EnqueueTask extends CompilerTask { | 5 class EnqueueTask extends CompilerTask { |
6 final Enqueuer codegen; | 6 final Enqueuer codegen; |
7 final Enqueuer resolution; | 7 final Enqueuer resolution; |
8 | 8 |
9 String get name => 'Enqueue'; | 9 String get name => 'Enqueue'; |
10 | 10 |
11 EnqueueTask(Compiler compiler) | 11 EnqueueTask(Compiler compiler) |
12 : codegen = new Enqueuer('codegen enqueuer', compiler, | 12 : codegen = new Enqueuer('codegen enqueuer', compiler, |
13 compiler.backend.createItemCompilationContext), | 13 compiler.backend.createItemCompilationContext), |
14 resolution = new Enqueuer('resolution enqueuer', compiler, | 14 resolution = new Enqueuer('resolution enqueuer', compiler, |
15 compiler.backend.createItemCompilationContext), | 15 compiler.backend.createItemCompilationContext), |
16 super(compiler) { | 16 super(compiler) { |
17 codegen.task = this; | 17 codegen.task = this; |
18 resolution.task = this; | 18 resolution.task = this; |
19 } | 19 } |
20 } | 20 } |
21 | 21 |
22 class RecompilationQueue { | |
23 final Function itemCompilationContextCreator; | |
24 final Queue<WorkItem> queue; | |
25 final Set<Element> queueElements; | |
26 int processed = 0; | |
27 | |
28 RecompilationQueue(ItemCompilationContext itemCompilationContextCreator()) | |
29 : this.itemCompilationContextCreator = itemCompilationContextCreator, | |
30 queue = new Queue<WorkItem>(), | |
31 queueElements = new Set<Element>(); | |
32 | |
33 void add(Element element, TreeElements elements) { | |
34 if (queueElements.contains(element)) return; | |
35 queueElements.add(element); | |
36 queue.add(new WorkItem(element, elements, itemCompilationContextCreator())); | |
37 } | |
38 | |
39 int get length => queue.length; | |
40 | |
41 bool isEmpty() => queue.isEmpty(); | |
42 | |
43 WorkItem next() { | |
44 WorkItem item = queue.removeLast(); | |
45 queueElements.remove(item.element); | |
46 processed++; | |
47 return item; | |
48 } | |
49 } | |
50 | |
51 class Enqueuer { | 22 class Enqueuer { |
52 final String name; | 23 final String name; |
53 final Compiler compiler; // TODO(ahe): Remove this dependency. | 24 final Compiler compiler; // TODO(ahe): Remove this dependency. |
54 final Function itemCompilationContextCreator; | 25 final Function itemCompilationContextCreator; |
55 final Map<String, Link<Element>> instanceMembersByName; | 26 final Map<String, Link<Element>> instanceMembersByName; |
56 final Set<ClassElement> seenClasses; | 27 final Set<ClassElement> seenClasses; |
57 final Universe universe; | 28 final Universe universe; |
58 final Queue<WorkItem> queue; | 29 final Queue<WorkItem> queue; |
59 | 30 |
60 /** | 31 /** |
61 * Map from declaration elements to the [TreeElements] object holding the | 32 * Map from declaration elements to the [TreeElements] object holding the |
62 * resolution mapping for the element implementation. | 33 * resolution mapping for the element implementation. |
63 * | 34 * |
64 * Invariant: Key elements are declaration elements. | 35 * Invariant: Key elements are declaration elements. |
65 */ | 36 */ |
66 final Map<Element, TreeElements> resolvedElements; | 37 final Map<Element, TreeElements> resolvedElements; |
67 final RecompilationQueue recompilationCandidates; | |
68 | 38 |
69 bool queueIsClosed = false; | 39 bool queueIsClosed = false; |
70 EnqueueTask task; | 40 EnqueueTask task; |
71 | 41 |
72 Enqueuer(this.name, this.compiler, | 42 Enqueuer(this.name, this.compiler, |
73 ItemCompilationContext itemCompilationContextCreator()) | 43 ItemCompilationContext itemCompilationContextCreator()) |
74 : this.itemCompilationContextCreator = itemCompilationContextCreator, | 44 : this.itemCompilationContextCreator = itemCompilationContextCreator, |
75 instanceMembersByName = new Map<String, Link<Element>>(), | 45 instanceMembersByName = new Map<String, Link<Element>>(), |
76 seenClasses = new Set<ClassElement>(), | 46 seenClasses = new Set<ClassElement>(), |
77 universe = new Universe(), | 47 universe = new Universe(), |
78 queue = new Queue<WorkItem>(), | 48 queue = new Queue<WorkItem>(), |
79 resolvedElements = new Map<Element, TreeElements>(), | 49 resolvedElements = new Map<Element, TreeElements>(); |
80 recompilationCandidates = | |
81 new RecompilationQueue(itemCompilationContextCreator); | |
82 | 50 |
83 bool get isResolutionQueue => compiler.enqueuer.resolution === this; | 51 bool get isResolutionQueue => compiler.enqueuer.resolution === this; |
84 | 52 |
85 TreeElements getCachedElements(Element element) { | 53 TreeElements getCachedElements(Element element) { |
86 // TODO(ngeoffray): Get rid of this check. | 54 // TODO(ngeoffray): Get rid of this check. |
87 if (element.enclosingElement.isClosure()) { | 55 if (element.enclosingElement.isClosure()) { |
88 closureMapping.ClosureClassElement cls = element.enclosingElement; | 56 closureMapping.ClosureClassElement cls = element.enclosingElement; |
89 element = cls.methodElement; | 57 element = cls.methodElement; |
90 } | 58 } |
91 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); | 59 Element owner = element.getOutermostEnclosingMemberOrTopLevel(); |
(...skipping 11 matching lines...) Expand all Loading... | |
103 } | 71 } |
104 | 72 |
105 /** | 73 /** |
106 * Documentation wanted -- johnniwinther | 74 * Documentation wanted -- johnniwinther |
107 * | 75 * |
108 * Invariant: [element] must be a declaration element. | 76 * Invariant: [element] must be a declaration element. |
109 */ | 77 */ |
110 void addToWorkList(Element element, [TreeElements elements]) { | 78 void addToWorkList(Element element, [TreeElements elements]) { |
111 assert(invariant(element, element.isDeclaration)); | 79 assert(invariant(element, element.isDeclaration)); |
112 if (element.isForeign()) return; | 80 if (element.isForeign()) return; |
113 if (compiler.phase == Compiler.PHASE_RECOMPILING) return; | |
114 if (queueIsClosed) { | 81 if (queueIsClosed) { |
115 if (isResolutionQueue && getCachedElements(element) !== null) return; | 82 if (isResolutionQueue && getCachedElements(element) !== null) return; |
116 compiler.internalErrorOnElement(element, "Work list is closed."); | 83 compiler.internalErrorOnElement(element, "Work list is closed."); |
117 } | 84 } |
118 if (!isResolutionQueue && | 85 if (!isResolutionQueue && |
119 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { | 86 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR) { |
120 registerInstantiatedClass(element.getEnclosingClass()); | 87 registerInstantiatedClass(element.getEnclosingClass()); |
121 } | 88 } |
122 if (elements === null) { | 89 if (elements === null) { |
123 elements = getCachedElements(element); | 90 elements = getCachedElements(element); |
(...skipping 23 matching lines...) Expand all Loading... | |
147 * | 114 * |
148 * Invariant: [element] must be an declaration element. | 115 * Invariant: [element] must be an declaration element. |
149 */ | 116 */ |
150 void eagerRecompile(Element element) { | 117 void eagerRecompile(Element element) { |
151 assert(invariant(element, element.isDeclaration)); | 118 assert(invariant(element, element.isDeclaration)); |
152 universe.generatedCode.remove(element); | 119 universe.generatedCode.remove(element); |
153 universe.generatedBailoutCode.remove(element); | 120 universe.generatedBailoutCode.remove(element); |
154 addToWorkList(element); | 121 addToWorkList(element); |
155 } | 122 } |
156 | 123 |
157 void registerRecompilationCandidate(Element element, | |
158 [TreeElements elements]) { | |
159 if (queueIsClosed) { | |
160 compiler.internalErrorOnElement(element, "$name work list is closed."); | |
161 } | |
162 recompilationCandidates.add(element, elements); | |
163 } | |
164 | |
165 void registerInstantiatedClass(ClassElement cls) { | 124 void registerInstantiatedClass(ClassElement cls) { |
166 if (cls.isInterface()) { | 125 if (cls.isInterface()) { |
167 compiler.internalErrorOnElement( | 126 compiler.internalErrorOnElement( |
168 // Use the current element, as this is where cls is referenced from. | 127 // Use the current element, as this is where cls is referenced from. |
169 compiler.currentElement, | 128 compiler.currentElement, |
170 'Expected a class, but $cls is an interface.'); | 129 'Expected a class, but $cls is an interface.'); |
171 } | 130 } |
172 universe.instantiatedClasses.add(cls); | 131 universe.instantiatedClasses.add(cls); |
173 onRegisterInstantiatedClass(cls); | 132 onRegisterInstantiatedClass(cls); |
174 } | 133 } |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 } | 281 } |
323 | 282 |
324 void handleUnseenSelector(SourceString methodName, Selector selector) { | 283 void handleUnseenSelector(SourceString methodName, Selector selector) { |
325 processInstanceMembers(methodName, (Element member) { | 284 processInstanceMembers(methodName, (Element member) { |
326 if (selector.applies(member, compiler)) { | 285 if (selector.applies(member, compiler)) { |
327 addToWorkList(member); | 286 addToWorkList(member); |
328 return true; | 287 return true; |
329 } | 288 } |
330 return false; | 289 return false; |
331 }); | 290 }); |
291 // TODO(sgjesse): Add subscription to the enqueuer to deliver this | |
292 // information to the JavaScript backend instead of this hardcoded hack. | |
293 if (compiler.backend is js_backend.JavaScriptBackend) { | |
ngeoffray
2012/09/24 08:15:49
So this has two effects: cancel previous optimizat
Søren Gjesse
2012/09/24 15:06:50
Moved to codegen.
| |
294 compiler.backend.addedDynamicSetter(selector); | |
295 } | |
332 } | 296 } |
333 | 297 |
334 /** | 298 /** |
335 * Documentation wanted -- johnniwinther | 299 * Documentation wanted -- johnniwinther |
336 * | 300 * |
337 * Invariant: [element] must be a declaration element. | 301 * Invariant: [element] must be a declaration element. |
338 */ | 302 */ |
339 void registerStaticUse(Element element) { | 303 void registerStaticUse(Element element) { |
340 if (element == null) return; | 304 if (element == null) return; |
341 assert(invariant(element, element.isDeclaration)); | 305 assert(invariant(element, element.isDeclaration)); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 } | 355 } |
392 | 356 |
393 void forEach(f(WorkItem work)) { | 357 void forEach(f(WorkItem work)) { |
394 while (!queue.isEmpty()) { | 358 while (!queue.isEmpty()) { |
395 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? | 359 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? |
396 } | 360 } |
397 } | 361 } |
398 | 362 |
399 String toString() => 'Enqueuer($name)'; | 363 String toString() => 'Enqueuer($name)'; |
400 } | 364 } |
OLD | NEW |