| 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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 const USE_LAZY_EMITTER = const bool.fromEnvironment("dart2js.use.lazy.emitter"); | 7 const USE_LAZY_EMITTER = const bool.fromEnvironment("dart2js.use.lazy.emitter"); |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Generates the code for all used classes in the program. Static fields (even | 10 * Generates the code for all used classes in the program. Static fields (even |
| 11 * in classes) are ignored, since they can be treated as non-class elements. | 11 * in classes) are ignored, since they can be treated as non-class elements. |
| 12 * | 12 * |
| 13 * The code for the containing (used) methods must exist in the `universe`. | 13 * The code for the containing (used) methods must exist in the `universe`. |
| 14 */ | 14 */ |
| 15 class CodeEmitterTask extends CompilerTask { | 15 class CodeEmitterTask extends CompilerTask { |
| 16 // TODO(floitsch): the code-emitter task should not need a namer. | 16 TypeTestRegistry typeTestRegistry; |
| 17 final Namer namer; | |
| 18 final TypeTestRegistry typeTestRegistry; | |
| 19 NativeEmitter nativeEmitter; | 17 NativeEmitter nativeEmitter; |
| 20 MetadataCollector metadataCollector; | 18 MetadataCollector metadataCollector; |
| 21 Emitter emitter; | 19 EmitterFactory _emitterFactory; |
| 20 Emitter _emitter; |
| 22 final Compiler compiler; | 21 final Compiler compiler; |
| 23 | 22 |
| 24 /// Records if a type variable is read dynamically for type tests. | 23 /// Records if a type variable is read dynamically for type tests. |
| 25 final Set<TypeVariableElement> readTypeVariables = | 24 final Set<TypeVariableElement> readTypeVariables = |
| 26 new Set<TypeVariableElement>(); | 25 new Set<TypeVariableElement>(); |
| 27 | 26 |
| 28 JavaScriptBackend get backend => compiler.backend; | 27 JavaScriptBackend get backend => compiler.backend; |
| 29 | 28 |
| 30 @deprecated | 29 @deprecated |
| 31 // This field should be removed. It's currently only needed for dump-info and | 30 // This field should be removed. It's currently only needed for dump-info and |
| 32 // tests. | 31 // tests. |
| 33 // The field is set after the program has been emitted. | 32 // The field is set after the program has been emitted. |
| 34 /// Contains a list of all classes that are emitted. | 33 /// Contains a list of all classes that are emitted. |
| 35 Set<ClassElement> neededClasses; | 34 Set<ClassElement> neededClasses; |
| 36 | 35 |
| 37 CodeEmitterTask(Compiler compiler, Namer namer, bool generateSourceMap, | 36 CodeEmitterTask( |
| 38 bool useStartupEmitter) | 37 Compiler compiler, bool generateSourceMap, bool useStartupEmitter) |
| 39 : compiler = compiler, | 38 : compiler = compiler, |
| 40 this.namer = namer, | |
| 41 this.typeTestRegistry = new TypeTestRegistry(compiler), | |
| 42 super(compiler.measurer) { | 39 super(compiler.measurer) { |
| 43 nativeEmitter = new NativeEmitter(this); | 40 nativeEmitter = new NativeEmitter(this); |
| 44 if (USE_LAZY_EMITTER) { | 41 if (USE_LAZY_EMITTER) { |
| 45 emitter = new lazy_js_emitter.Emitter(compiler, namer, nativeEmitter); | 42 _emitterFactory = new lazy_js_emitter.EmitterFactory(); |
| 46 } else if (useStartupEmitter) { | 43 } else if (useStartupEmitter) { |
| 47 emitter = new startup_js_emitter.Emitter( | 44 _emitterFactory = new startup_js_emitter.EmitterFactory( |
| 48 compiler, namer, nativeEmitter, generateSourceMap); | 45 generateSourceMap: generateSourceMap); |
| 49 } else { | 46 } else { |
| 50 emitter = | 47 _emitterFactory = new full_js_emitter.EmitterFactory( |
| 51 new full_js_emitter.Emitter(compiler, namer, generateSourceMap, this); | 48 generateSourceMap: generateSourceMap); |
| 52 } | 49 } |
| 53 metadataCollector = new MetadataCollector(compiler, emitter); | 50 } |
| 51 |
| 52 Emitter get emitter { |
| 53 assert(invariant(NO_LOCATION_SPANNABLE, _emitter != null, |
| 54 message: "Emitter has not been created yet.")); |
| 55 return _emitter; |
| 54 } | 56 } |
| 55 | 57 |
| 56 String get name => 'Code emitter'; | 58 String get name => 'Code emitter'; |
| 57 | 59 |
| 58 /// Returns the string that is used to find library patches that are | 60 /// Returns the string that is used to find library patches that are |
| 59 /// specialized for the emitter. | 61 /// specialized for the emitter. |
| 60 String get patchVersion => emitter.patchVersion; | 62 String get patchVersion => _emitterFactory.patchVersion; |
| 63 |
| 64 /// Returns true, if the emitter supports reflection. |
| 65 bool get supportsReflection => _emitterFactory.supportsReflection; |
| 61 | 66 |
| 62 /// Returns the closure expression of a static function. | 67 /// Returns the closure expression of a static function. |
| 63 jsAst.Expression isolateStaticClosureAccess(MethodElement element) { | 68 jsAst.Expression isolateStaticClosureAccess(MethodElement element) { |
| 64 return emitter.isolateStaticClosureAccess(element); | 69 return emitter.isolateStaticClosureAccess(element); |
| 65 } | 70 } |
| 66 | 71 |
| 67 /// Returns the JS function that must be invoked to get the value of the | 72 /// Returns the JS function that must be invoked to get the value of the |
| 68 /// lazily initialized static. | 73 /// lazily initialized static. |
| 69 jsAst.Expression isolateLazyInitializerAccess(FieldElement element) { | 74 jsAst.Expression isolateLazyInitializerAccess(FieldElement element) { |
| 70 return emitter.isolateLazyInitializerAccess(element); | 75 return emitter.isolateLazyInitializerAccess(element); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 136 } |
| 132 | 137 |
| 133 Set<ClassElement> _finalizeRti() { | 138 Set<ClassElement> _finalizeRti() { |
| 134 // Compute the required type checks to know which classes need a | 139 // Compute the required type checks to know which classes need a |
| 135 // 'is$' method. | 140 // 'is$' method. |
| 136 typeTestRegistry.computeRequiredTypeChecks(); | 141 typeTestRegistry.computeRequiredTypeChecks(); |
| 137 // Compute the classes needed by RTI. | 142 // Compute the classes needed by RTI. |
| 138 return typeTestRegistry.computeRtiNeededClasses(); | 143 return typeTestRegistry.computeRtiNeededClasses(); |
| 139 } | 144 } |
| 140 | 145 |
| 141 int assembleProgram() { | 146 /// Creates the [Emitter] for this task. |
| 147 void createEmitter(Namer namer, ClosedWorld closedWorld) { |
| 148 measure(() { |
| 149 _emitter = _emitterFactory.createEmitter(this, namer, closedWorld); |
| 150 metadataCollector = new MetadataCollector(compiler, _emitter); |
| 151 typeTestRegistry = new TypeTestRegistry(compiler, closedWorld); |
| 152 }); |
| 153 } |
| 154 |
| 155 int assembleProgram(Namer namer, ClosedWorld closedWorld) { |
| 142 return measure(() { | 156 return measure(() { |
| 143 emitter.invalidateCaches(); | 157 emitter.invalidateCaches(); |
| 144 | 158 |
| 145 Set<ClassElement> rtiNeededClasses = _finalizeRti(); | 159 Set<ClassElement> rtiNeededClasses = _finalizeRti(); |
| 146 ProgramBuilder programBuilder = | 160 ProgramBuilder programBuilder = new ProgramBuilder( |
| 147 new ProgramBuilder(compiler, namer, this, emitter, rtiNeededClasses); | 161 compiler, namer, this, emitter, closedWorld, rtiNeededClasses); |
| 148 int size = emitter.emitProgram(programBuilder); | 162 int size = emitter.emitProgram(programBuilder); |
| 149 // TODO(floitsch): we shouldn't need the `neededClasses` anymore. | 163 // TODO(floitsch): we shouldn't need the `neededClasses` anymore. |
| 150 neededClasses = programBuilder.collector.neededClasses; | 164 neededClasses = programBuilder.collector.neededClasses; |
| 151 return size; | 165 return size; |
| 152 }); | 166 }); |
| 153 } | 167 } |
| 154 } | 168 } |
| 155 | 169 |
| 156 abstract class Emitter { | 170 abstract class EmitterFactory { |
| 157 /// Returns the string that is used to find library patches that are | 171 /// Returns the string that is used to find library patches that are |
| 158 /// specialized for this emitter. | 172 /// specialized for this emitter. |
| 159 String get patchVersion; | 173 String get patchVersion; |
| 160 | 174 |
| 175 /// Returns true, if the emitter supports reflection. |
| 176 bool get supportsReflection; |
| 177 |
| 178 /// Create the [Emitter] for the emitter [task] that uses the given [namer]. |
| 179 Emitter createEmitter( |
| 180 CodeEmitterTask task, Namer namer, ClosedWorld closedWorld); |
| 181 } |
| 182 |
| 183 abstract class Emitter { |
| 161 /// Uses the [programBuilder] to generate a model of the program, emits | 184 /// Uses the [programBuilder] to generate a model of the program, emits |
| 162 /// the program, and returns the size of the generated output. | 185 /// the program, and returns the size of the generated output. |
| 163 int emitProgram(ProgramBuilder programBuilder); | 186 int emitProgram(ProgramBuilder programBuilder); |
| 164 | 187 |
| 165 /// Returns true, if the emitter supports reflection. | |
| 166 bool get supportsReflection; | |
| 167 | |
| 168 /// Returns the JS function that must be invoked to get the value of the | 188 /// Returns the JS function that must be invoked to get the value of the |
| 169 /// lazily initialized static. | 189 /// lazily initialized static. |
| 170 jsAst.Expression isolateLazyInitializerAccess(FieldElement element); | 190 jsAst.Expression isolateLazyInitializerAccess(FieldElement element); |
| 171 | 191 |
| 172 /// Returns the closure expression of a static function. | 192 /// Returns the closure expression of a static function. |
| 173 jsAst.Expression isolateStaticClosureAccess(FunctionElement element); | 193 jsAst.Expression isolateStaticClosureAccess(FunctionElement element); |
| 174 | 194 |
| 175 /// Returns the JS code for accessing the embedded [global]. | 195 /// Returns the JS code for accessing the embedded [global]. |
| 176 jsAst.Expression generateEmbeddedGlobalAccess(String global); | 196 jsAst.Expression generateEmbeddedGlobalAccess(String global); |
| 177 | 197 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 203 bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant); | 223 bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant); |
| 204 | 224 |
| 205 /// Returns the JS code for accessing the given [constant]. | 225 /// Returns the JS code for accessing the given [constant]. |
| 206 jsAst.Expression constantReference(ConstantValue constant); | 226 jsAst.Expression constantReference(ConstantValue constant); |
| 207 | 227 |
| 208 /// Returns the JS template for the given [builtin]. | 228 /// Returns the JS template for the given [builtin]. |
| 209 jsAst.Template templateForBuiltin(JsBuiltin builtin); | 229 jsAst.Template templateForBuiltin(JsBuiltin builtin); |
| 210 | 230 |
| 211 void invalidateCaches(); | 231 void invalidateCaches(); |
| 212 } | 232 } |
| OLD | NEW |