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 25 matching lines...) Expand all Loading... |
149 * | 116 * |
150 * Invariant: [element] must be a declaration element. | 117 * Invariant: [element] must be a declaration element. |
151 */ | 118 */ |
152 void eagerRecompile(Element element) { | 119 void eagerRecompile(Element element) { |
153 assert(invariant(element, element.isDeclaration)); | 120 assert(invariant(element, element.isDeclaration)); |
154 universe.generatedCode.remove(element); | 121 universe.generatedCode.remove(element); |
155 universe.generatedBailoutCode.remove(element); | 122 universe.generatedBailoutCode.remove(element); |
156 addToWorkList(element); | 123 addToWorkList(element); |
157 } | 124 } |
158 | 125 |
159 void registerRecompilationCandidate(Element element, | |
160 [TreeElements elements]) { | |
161 if (queueIsClosed) { | |
162 compiler.internalErrorOnElement(element, "$name work list is closed."); | |
163 } | |
164 recompilationCandidates.add(element, elements); | |
165 } | |
166 | |
167 void registerInstantiatedClass(ClassElement cls) { | 126 void registerInstantiatedClass(ClassElement cls) { |
168 if (cls.isInterface()) { | 127 if (cls.isInterface()) { |
169 compiler.internalErrorOnElement( | 128 compiler.internalErrorOnElement( |
170 // Use the current element, as this is where cls is referenced from. | 129 // Use the current element, as this is where cls is referenced from. |
171 compiler.currentElement, | 130 compiler.currentElement, |
172 'Expected a class, but $cls is an interface.'); | 131 'Expected a class, but $cls is an interface.'); |
173 } | 132 } |
174 universe.instantiatedClasses.add(cls); | 133 universe.instantiatedClasses.add(cls); |
175 onRegisterInstantiatedClass(cls); | 134 onRegisterInstantiatedClass(cls); |
176 } | 135 } |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 } | 352 } |
394 | 353 |
395 void forEach(f(WorkItem work)) { | 354 void forEach(f(WorkItem work)) { |
396 while (!queue.isEmpty()) { | 355 while (!queue.isEmpty()) { |
397 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? | 356 f(queue.removeLast()); // TODO(kasperl): Why isn't this removeFirst? |
398 } | 357 } |
399 } | 358 } |
400 | 359 |
401 String toString() => 'Enqueuer($name)'; | 360 String toString() => 'Enqueuer($name)'; |
402 } | 361 } |
OLD | NEW |