| 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 js_backend; | 5 part of js_backend; |
| 6 | 6 |
| 7 const VERBOSE_OPTIMIZER_HINTS = false; | 7 const VERBOSE_OPTIMIZER_HINTS = false; |
| 8 | 8 |
| 9 const bool USE_CPS_IR = const bool.fromEnvironment("USE_CPS_IR"); |
| 10 |
| 9 class JavaScriptItemCompilationContext extends ItemCompilationContext { | 11 class JavaScriptItemCompilationContext extends ItemCompilationContext { |
| 10 final Set<HInstruction> boundsChecked = new Set<HInstruction>(); | 12 final Set<HInstruction> boundsChecked = new Set<HInstruction>(); |
| 11 final Set<HInstruction> allocatedFixedLists = new Set<HInstruction>(); | 13 final Set<HInstruction> allocatedFixedLists = new Set<HInstruction>(); |
| 12 } | 14 } |
| 13 | 15 |
| 16 abstract class FunctionCompiler { |
| 17 /// Generates JavaScript code for `work.element`. |
| 18 jsAst.Fun compile(CodegenWorkItem work); |
| 19 |
| 20 Iterable get tasks; |
| 21 } |
| 22 |
| 14 /* | 23 /* |
| 15 * Invariants: | 24 * Invariants: |
| 16 * canInline(function) implies canInline(function, insideLoop:true) | 25 * canInline(function) implies canInline(function, insideLoop:true) |
| 17 * !canInline(function, insideLoop: true) implies !canInline(function) | 26 * !canInline(function, insideLoop: true) implies !canInline(function) |
| 18 */ | 27 */ |
| 19 class FunctionInlineCache { | 28 class FunctionInlineCache { |
| 20 final Map<FunctionElement, bool> canBeInlined = | 29 final Map<FunctionElement, bool> canBeInlined = |
| 21 new Map<FunctionElement, bool>(); | 30 new Map<FunctionElement, bool>(); |
| 22 | 31 |
| 23 final Map<FunctionElement, bool> canBeInlinedInsideLoop = | 32 final Map<FunctionElement, bool> canBeInlinedInsideLoop = |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 LibraryElement internalLibrary; | 99 LibraryElement internalLibrary; |
| 91 | 100 |
| 92 | 101 |
| 93 /// Set of classes that need to be considered for reflection although not | 102 /// Set of classes that need to be considered for reflection although not |
| 94 /// otherwise visible during resolution. | 103 /// otherwise visible during resolution. |
| 95 Iterable<ClassElement> get classesRequiredForReflection { | 104 Iterable<ClassElement> get classesRequiredForReflection { |
| 96 // TODO(herhut): Clean this up when classes needed for rti are tracked. | 105 // TODO(herhut): Clean this up when classes needed for rti are tracked. |
| 97 return [closureClass, jsIndexableClass]; | 106 return [closureClass, jsIndexableClass]; |
| 98 } | 107 } |
| 99 | 108 |
| 100 SsaBuilderTask builder; | 109 FunctionCompiler functionCompiler; |
| 101 SsaOptimizerTask optimizer; | 110 |
| 102 SsaCodeGeneratorTask generator; | |
| 103 CodeEmitterTask emitter; | 111 CodeEmitterTask emitter; |
| 104 | 112 |
| 105 /** | 113 /** |
| 106 * The generated code as a js AST for compiled methods. | 114 * The generated code as a js AST for compiled methods. |
| 107 */ | 115 */ |
| 108 Map<Element, jsAst.Expression> get generatedCode { | 116 Map<Element, jsAst.Expression> get generatedCode { |
| 109 return compiler.enqueuer.codegen.generatedCode; | 117 return compiler.enqueuer.codegen.generatedCode; |
| 110 } | 118 } |
| 111 | 119 |
| 112 FunctionInlineCache inlineCache = new FunctionInlineCache(); | 120 FunctionInlineCache inlineCache = new FunctionInlineCache(); |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 */ | 331 */ |
| 324 final Set<ClassElement> specialOperatorEqClasses = new Set<ClassElement>(); | 332 final Set<ClassElement> specialOperatorEqClasses = new Set<ClassElement>(); |
| 325 | 333 |
| 326 /** | 334 /** |
| 327 * A set of members that are called from subclasses via super. | 335 * A set of members that are called from subclasses via super. |
| 328 */ | 336 */ |
| 329 final Set<FunctionElement> aliasedSuperMembers = | 337 final Set<FunctionElement> aliasedSuperMembers = |
| 330 new Setlet<FunctionElement>(); | 338 new Setlet<FunctionElement>(); |
| 331 | 339 |
| 332 List<CompilerTask> get tasks { | 340 List<CompilerTask> get tasks { |
| 333 return <CompilerTask>[builder, optimizer, generator, emitter]; | 341 List<CompilerTask> result = functionCompiler.tasks; |
| 342 result.add(emitter); |
| 343 return result; |
| 334 } | 344 } |
| 335 | 345 |
| 336 final RuntimeTypes rti; | 346 final RuntimeTypes rti; |
| 337 | 347 |
| 338 /// Holds the method "disableTreeShaking" in js_mirrors when | 348 /// Holds the method "disableTreeShaking" in js_mirrors when |
| 339 /// dart:mirrors has been loaded. | 349 /// dart:mirrors has been loaded. |
| 340 FunctionElement disableTreeShakingMarker; | 350 FunctionElement disableTreeShakingMarker; |
| 341 | 351 |
| 342 /// Holds the method "preserveNames" in js_mirrors when | 352 /// Holds the method "preserveNames" in js_mirrors when |
| 343 /// dart:mirrors has been loaded. | 353 /// dart:mirrors has been loaded. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 JavaScriptResolutionCallbacks resolutionCallbacks; | 448 JavaScriptResolutionCallbacks resolutionCallbacks; |
| 439 | 449 |
| 440 JavaScriptBackend(Compiler compiler, bool generateSourceMap) | 450 JavaScriptBackend(Compiler compiler, bool generateSourceMap) |
| 441 : namer = determineNamer(compiler), | 451 : namer = determineNamer(compiler), |
| 442 oneShotInterceptors = new Map<String, Selector>(), | 452 oneShotInterceptors = new Map<String, Selector>(), |
| 443 interceptedElements = new Map<String, Set<Element>>(), | 453 interceptedElements = new Map<String, Set<Element>>(), |
| 444 rti = new RuntimeTypes(compiler), | 454 rti = new RuntimeTypes(compiler), |
| 445 specializedGetInterceptors = new Map<String, Set<ClassElement>>(), | 455 specializedGetInterceptors = new Map<String, Set<ClassElement>>(), |
| 446 super(compiler) { | 456 super(compiler) { |
| 447 emitter = new CodeEmitterTask(compiler, namer, generateSourceMap); | 457 emitter = new CodeEmitterTask(compiler, namer, generateSourceMap); |
| 448 builder = new SsaBuilderTask(this); | |
| 449 optimizer = new SsaOptimizerTask(this); | |
| 450 generator = new SsaCodeGeneratorTask(this); | |
| 451 typeVariableHandler = new TypeVariableHandler(this); | 458 typeVariableHandler = new TypeVariableHandler(this); |
| 452 customElementsAnalysis = new CustomElementsAnalysis(this); | 459 customElementsAnalysis = new CustomElementsAnalysis(this); |
| 453 constantCompilerTask = new JavaScriptConstantTask(compiler); | 460 constantCompilerTask = new JavaScriptConstantTask(compiler); |
| 454 resolutionCallbacks = new JavaScriptResolutionCallbacks(this); | 461 resolutionCallbacks = new JavaScriptResolutionCallbacks(this); |
| 462 functionCompiler = USE_CPS_IR |
| 463 ? new CspFunctionCompiler(compiler, this) |
| 464 : new SsaFunctionCompiler(this); |
| 455 } | 465 } |
| 456 | 466 |
| 457 ConstantSystem get constantSystem => constants.constantSystem; | 467 ConstantSystem get constantSystem => constants.constantSystem; |
| 458 | 468 |
| 459 /// Returns constant environment for the JavaScript interpretation of the | 469 /// Returns constant environment for the JavaScript interpretation of the |
| 460 /// constants. | 470 /// constants. |
| 461 JavaScriptConstantCompiler get constants { | 471 JavaScriptConstantCompiler get constants { |
| 462 return constantCompilerTask.jsConstantCompiler; | 472 return constantCompilerTask.jsConstantCompiler; |
| 463 } | 473 } |
| 464 | 474 |
| (...skipping 715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1180 | 1190 |
| 1181 void codegen(CodegenWorkItem work) { | 1191 void codegen(CodegenWorkItem work) { |
| 1182 Element element = work.element; | 1192 Element element = work.element; |
| 1183 var kind = element.kind; | 1193 var kind = element.kind; |
| 1184 if (kind == ElementKind.TYPEDEF) return; | 1194 if (kind == ElementKind.TYPEDEF) return; |
| 1185 if (element.isConstructor && element.enclosingClass == jsNullClass) { | 1195 if (element.isConstructor && element.enclosingClass == jsNullClass) { |
| 1186 // Work around a problem compiling JSNull's constructor. | 1196 // Work around a problem compiling JSNull's constructor. |
| 1187 return; | 1197 return; |
| 1188 } | 1198 } |
| 1189 if (kind.category == ElementCategory.VARIABLE) { | 1199 if (kind.category == ElementCategory.VARIABLE) { |
| 1190 ConstantExpression initialValue = constants.getConstantForVariable(element
); | 1200 ConstantExpression initialValue = |
| 1201 constants.getConstantForVariable(element); |
| 1191 if (initialValue != null) { | 1202 if (initialValue != null) { |
| 1192 registerCompileTimeConstant(initialValue.value, work.registry); | 1203 registerCompileTimeConstant(initialValue.value, work.registry); |
| 1193 constants.addCompileTimeConstantForEmission(initialValue.value); | 1204 constants.addCompileTimeConstantForEmission(initialValue.value); |
| 1194 // We don't need to generate code for static or top-level | 1205 // We don't need to generate code for static or top-level |
| 1195 // variables. For instance variables, we may need to generate | 1206 // variables. For instance variables, we may need to generate |
| 1196 // the checked setter. | 1207 // the checked setter. |
| 1197 if (Elements.isStaticOrTopLevel(element)) return; | 1208 if (Elements.isStaticOrTopLevel(element)) return; |
| 1198 } else { | 1209 } else { |
| 1199 // If the constant-handler was not able to produce a result we have to | 1210 // If the constant-handler was not able to produce a result we have to |
| 1200 // go through the builder (below) to generate the lazy initializer for | 1211 // go through the builder (below) to generate the lazy initializer for |
| 1201 // the static variable. | 1212 // the static variable. |
| 1202 // We also need to register the use of the cyclic-error helper. | 1213 // We also need to register the use of the cyclic-error helper. |
| 1203 compiler.enqueuer.codegen.registerStaticUse(getCyclicThrowHelper()); | 1214 compiler.enqueuer.codegen.registerStaticUse(getCyclicThrowHelper()); |
| 1204 } | 1215 } |
| 1205 } | 1216 } |
| 1206 HGraph graph = builder.build(work); | 1217 generatedCode[element] = functionCompiler.compile(work); |
| 1207 optimizer.optimize(work, graph); | |
| 1208 jsAst.Expression code = generator.generateCode(work, graph); | |
| 1209 generatedCode[element] = code; | |
| 1210 } | 1218 } |
| 1211 | 1219 |
| 1212 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) { | 1220 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) { |
| 1213 return new native.NativeResolutionEnqueuer(world, compiler); | 1221 return new native.NativeResolutionEnqueuer(world, compiler); |
| 1214 } | 1222 } |
| 1215 | 1223 |
| 1216 native.NativeEnqueuer nativeCodegenEnqueuer(Enqueuer world) { | 1224 native.NativeEnqueuer nativeCodegenEnqueuer(Enqueuer world) { |
| 1217 return new native.NativeCodegenEnqueuer(world, compiler, emitter); | 1225 return new native.NativeCodegenEnqueuer(world, compiler, emitter); |
| 1218 } | 1226 } |
| 1219 | 1227 |
| (...skipping 1241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2461 } | 2469 } |
| 2462 } | 2470 } |
| 2463 | 2471 |
| 2464 /// Records that [constant] is used by the element behind [registry]. | 2472 /// Records that [constant] is used by the element behind [registry]. |
| 2465 class Dependency { | 2473 class Dependency { |
| 2466 final ConstantValue constant; | 2474 final ConstantValue constant; |
| 2467 final Element annotatedElement; | 2475 final Element annotatedElement; |
| 2468 | 2476 |
| 2469 const Dependency(this.constant, this.annotatedElement); | 2477 const Dependency(this.constant, this.annotatedElement); |
| 2470 } | 2478 } |
| OLD | NEW |