| 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 | 5 |
| 6 /** | 6 /** |
| 7 * If true, print a warning for each method that was resolved, but not | 7 * If true, print a warning for each method that was resolved, but not |
| 8 * compiled. | 8 * compiled. |
| 9 */ | 9 */ |
| 10 final bool REPORT_EXCESS_RESOLUTION = false; | 10 final bool REPORT_EXCESS_RESOLUTION = false; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 Collection<LibraryElement> libraries); | 49 Collection<LibraryElement> libraries); |
| 50 abstract void assembleProgram(); | 50 abstract void assembleProgram(); |
| 51 abstract List<CompilerTask> get tasks(); | 51 abstract List<CompilerTask> get tasks(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 class JavaScriptBackend extends Backend { | 54 class JavaScriptBackend extends Backend { |
| 55 SsaBuilderTask builder; | 55 SsaBuilderTask builder; |
| 56 SsaOptimizerTask optimizer; | 56 SsaOptimizerTask optimizer; |
| 57 SsaCodeGeneratorTask generator; | 57 SsaCodeGeneratorTask generator; |
| 58 CodeEmitterTask emitter; | 58 CodeEmitterTask emitter; |
| 59 final Map<Element, Map<Element, HType>> fieldInitializers; |
| 60 final Map<Element, Map<Element, bool>> fieldIntegerSetters; |
| 59 | 61 |
| 60 List<CompilerTask> get tasks() { | 62 List<CompilerTask> get tasks() { |
| 61 return <CompilerTask>[builder, optimizer, generator, emitter]; | 63 return <CompilerTask>[builder, optimizer, generator, emitter]; |
| 62 } | 64 } |
| 63 | 65 |
| 64 JavaScriptBackend(Compiler compiler) | 66 JavaScriptBackend(Compiler compiler) |
| 65 : emitter = new CodeEmitterTask(compiler), | 67 : emitter = new CodeEmitterTask(compiler), |
| 68 fieldInitializers = new Map<Element, Map<Element, HType>>(), |
| 69 fieldIntegerSetters = new Map<Element, Map<Element, bool>>(), |
| 66 super(compiler) { | 70 super(compiler) { |
| 67 builder = new SsaBuilderTask(this); | 71 builder = new SsaBuilderTask(this); |
| 68 optimizer = new SsaOptimizerTask(this); | 72 optimizer = new SsaOptimizerTask(this); |
| 69 generator = new SsaCodeGeneratorTask(this); | 73 generator = new SsaCodeGeneratorTask(this); |
| 70 } | 74 } |
| 71 | 75 |
| 72 void enqueueHelpers(Enqueuer world) { | 76 void enqueueHelpers(Enqueuer world) { |
| 73 enqueueAllTopLevelFunctions(compiler.jsHelperLibrary, world); | 77 enqueueAllTopLevelFunctions(compiler.jsHelperLibrary, world); |
| 74 enqueueAllTopLevelFunctions(compiler.interceptorsLibrary, world); | 78 enqueueAllTopLevelFunctions(compiler.interceptorsLibrary, world); |
| 75 for (var helper in [const SourceString('Closure'), | 79 for (var helper in [const SourceString('Closure'), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 94 } | 98 } |
| 95 | 99 |
| 96 void processNativeClasses(Enqueuer world, | 100 void processNativeClasses(Enqueuer world, |
| 97 Collection<LibraryElement> libraries) { | 101 Collection<LibraryElement> libraries) { |
| 98 native.processNativeClasses(world, emitter, libraries); | 102 native.processNativeClasses(world, emitter, libraries); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void assembleProgram() { | 105 void assembleProgram() { |
| 102 emitter.assembleProgram(); | 106 emitter.assembleProgram(); |
| 103 } | 107 } |
| 108 |
| 109 void updateFieldInitializers(Element field, HType propagatedType) { |
| 110 assert(field.isField()); |
| 111 assert(field.enclosingElement.isClass()); |
| 112 Map<Element, HType> fields = |
| 113 fieldInitializers.putIfAbsent( |
| 114 field.enclosingElement, () => new Map<Element, HType>()); |
| 115 if (!fields.containsKey(field)) { |
| 116 fields[field] = propagatedType; |
| 117 } else { |
| 118 fields[field] = fields[field].union(propagatedType); |
| 119 } |
| 120 } |
| 121 |
| 122 bool couldHaveFieldSingleTypeInitializers(Element field, |
| 123 HType requestedType) { |
| 124 assert(field.isField()); |
| 125 assert(field.enclosingElement.isClass()); |
| 126 // If there is no information on the initializer it might still be |
| 127 // initialized to integers only. |
| 128 if (!fieldInitializers.containsKey(field.enclosingElement)) return true; |
| 129 Map<Element, HType> fields = fieldInitializers[field.enclosingElement]; |
| 130 HType propagatedType = fields[field]; |
| 131 if (propagatedType == null) return true; |
| 132 return propagatedType == requestedType; |
| 133 } |
| 134 |
| 135 bool hasFieldSingleTypeInitializers(Element field, HType requestedType) { |
| 136 assert(field.isField()); |
| 137 assert(field.enclosingElement.isClass()); |
| 138 if (!fieldInitializers.containsKey(field.enclosingElement)) return false; |
| 139 Map<Element, HType> fields = fieldInitializers[field.enclosingElement]; |
| 140 HType propagatedType = fields[field]; |
| 141 if (propagatedType == null) return false; |
| 142 return propagatedType == requestedType; |
| 143 } |
| 144 |
| 145 void updateFieldIntegerSetters(Element field, bool isInteger) { |
| 146 assert(field.isField()); |
| 147 assert(field.enclosingElement.isClass()); |
| 148 Map<Element, bool> fields = |
| 149 fieldIntegerSetters.putIfAbsent( |
| 150 field.enclosingElement, () => new Map<Element, bool>()); |
| 151 if (!fields.containsKey(field)) { |
| 152 fields[field] = isInteger; |
| 153 } else { |
| 154 fields[field] = fields[field] && isInteger; |
| 155 } |
| 156 } |
| 157 |
| 158 // Returns whether nothing but setters setting the field to an integer have |
| 159 // been seen during compilation so far. |
| 160 bool onlyFieldIntegerSettersSoFar(Element field) { |
| 161 assert(field.isField()); |
| 162 assert(field.enclosingElement.isClass()); |
| 163 if (!fieldIntegerSetters.containsKey(field.enclosingElement)) return true; |
| 164 Map<Element, bool> fields = fieldIntegerSetters[field.enclosingElement]; |
| 165 if (!fields.containsKey(field)) return false; |
| 166 return fields[field]; |
| 167 } |
| 104 } | 168 } |
| 105 | 169 |
| 106 class Compiler implements DiagnosticListener { | 170 class Compiler implements DiagnosticListener { |
| 107 final Map<String, LibraryElement> libraries; | 171 final Map<String, LibraryElement> libraries; |
| 108 int nextFreeClassId = 0; | 172 int nextFreeClassId = 0; |
| 109 World world; | 173 World world; |
| 110 String assembledCode; | 174 String assembledCode; |
| 111 Namer namer; | 175 Namer namer; |
| 112 Types types; | 176 Types types; |
| 113 final bool enableTypeAssertions; | 177 final bool enableTypeAssertions; |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 checker.check(tree, elements); | 606 checker.check(tree, elements); |
| 543 return elements; | 607 return elements; |
| 544 } | 608 } |
| 545 | 609 |
| 546 TreeElements analyze(WorkItem work, Enqueuer world) { | 610 TreeElements analyze(WorkItem work, Enqueuer world) { |
| 547 if (work.isAnalyzed()) return work.resolutionTree; | 611 if (work.isAnalyzed()) return work.resolutionTree; |
| 548 if (progress.elapsedInMs() > 500) { | 612 if (progress.elapsedInMs() > 500) { |
| 549 // TODO(ahe): Add structured diagnostics to the compiler API and | 613 // TODO(ahe): Add structured diagnostics to the compiler API and |
| 550 // use it to separate this from the --verbose option. | 614 // use it to separate this from the --verbose option. |
| 551 if (phase == PHASE_RESOLVING) { | 615 if (phase == PHASE_RESOLVING) { |
| 552 log('Resolved ${enqueuer.resolution.resolvedElements.length}' | 616 log('Resolved ${enqueuer.resolution.resolvedElements.length} ' |
| 553 'elements.'); | 617 'elements.'); |
| 554 progress.reset(); | 618 progress.reset(); |
| 555 } | 619 } |
| 556 } | 620 } |
| 557 Element element = work.element; | 621 Element element = work.element; |
| 558 TreeElements result = world.getCachedElements(element); | 622 TreeElements result = world.getCachedElements(element); |
| 559 if (result !== null) return result; | 623 if (result !== null) return result; |
| 560 if (world !== enqueuer.resolution) { | 624 if (world !== enqueuer.resolution) { |
| 561 internalErrorOnElement(element, | 625 internalErrorOnElement(element, |
| 562 'Internal error: unresolved element: $element.'); | 626 'Internal error: unresolved element: $element.'); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 // invariant that endOffset > beginOffset, but for EOF the | 828 // invariant that endOffset > beginOffset, but for EOF the |
| 765 // charoffset of the next token may be [beginOffset]. This can | 829 // charoffset of the next token may be [beginOffset]. This can |
| 766 // also happen for synthetized tokens that are produced during | 830 // also happen for synthetized tokens that are produced during |
| 767 // error handling. | 831 // error handling. |
| 768 final endOffset = | 832 final endOffset = |
| 769 Math.max((end.next !== null) ? end.next.charOffset : 0, beginOffset + 1); | 833 Math.max((end.next !== null) ? end.next.charOffset : 0, beginOffset + 1); |
| 770 assert(endOffset > beginOffset); | 834 assert(endOffset > beginOffset); |
| 771 return f(beginOffset, endOffset); | 835 return f(beginOffset, endOffset); |
| 772 } | 836 } |
| 773 } | 837 } |
| OLD | NEW |