| 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 class JavaScriptItemCompilationContext extends ItemCompilationContext { | 9 class JavaScriptItemCompilationContext extends ItemCompilationContext { |
| 10 final Set<HInstruction> boundsChecked = new Set<HInstruction>(); | 10 final Set<HInstruction> boundsChecked = new Set<HInstruction>(); |
| (...skipping 1030 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 constants.addCompileTimeConstantForEmission(constant); | 1041 constants.addCompileTimeConstantForEmission(constant); |
| 1042 } | 1042 } |
| 1043 | 1043 |
| 1044 void registerCompileTimeConstantInternal(ConstantValue constant, | 1044 void registerCompileTimeConstantInternal(ConstantValue constant, |
| 1045 Registry registry) { | 1045 Registry registry) { |
| 1046 DartType type = constant.getType(compiler.coreTypes); | 1046 DartType type = constant.getType(compiler.coreTypes); |
| 1047 registerInstantiatedConstantType(type, registry); | 1047 registerInstantiatedConstantType(type, registry); |
| 1048 | 1048 |
| 1049 if (constant.isFunction) { | 1049 if (constant.isFunction) { |
| 1050 FunctionConstantValue function = constant; | 1050 FunctionConstantValue function = constant; |
| 1051 registry.registerGetOfStaticFunction(function.element); | 1051 registry.registerStaticUse(new StaticUse.staticTearOff(function.element)); |
| 1052 } else if (constant.isInterceptor) { | 1052 } else if (constant.isInterceptor) { |
| 1053 // An interceptor constant references the class's prototype chain. | 1053 // An interceptor constant references the class's prototype chain. |
| 1054 InterceptorConstantValue interceptor = constant; | 1054 InterceptorConstantValue interceptor = constant; |
| 1055 registerInstantiatedConstantType(interceptor.dispatchedType, registry); | 1055 registerInstantiatedConstantType(interceptor.dispatchedType, registry); |
| 1056 } else if (constant.isType) { | 1056 } else if (constant.isType) { |
| 1057 enqueueInResolution(helpers.createRuntimeType, registry); | 1057 enqueueInResolution(helpers.createRuntimeType, registry); |
| 1058 registry.registerInstantiation(typeImplementation.rawType); | 1058 registry.registerInstantiation(typeImplementation.rawType); |
| 1059 } | 1059 } |
| 1060 lookupMapAnalysis.registerConstantKey(constant); | 1060 lookupMapAnalysis.registerConstantKey(constant); |
| 1061 } | 1061 } |
| 1062 | 1062 |
| 1063 void registerInstantiatedConstantType(DartType type, Registry registry) { | 1063 void registerInstantiatedConstantType(DartType type, Registry registry) { |
| 1064 DartType instantiatedType = | 1064 DartType instantiatedType = |
| 1065 type.isFunctionType ? coreTypes.functionType : type; | 1065 type.isFunctionType ? coreTypes.functionType : type; |
| 1066 if (type is InterfaceType) { | 1066 if (type is InterfaceType) { |
| 1067 registry.registerInstantiation(instantiatedType); | 1067 registry.registerInstantiation(instantiatedType); |
| 1068 if (!type.treatAsRaw && classNeedsRti(type.element)) { | 1068 if (!type.treatAsRaw && classNeedsRti(type.element)) { |
| 1069 registry.registerStaticInvocation(helpers.setRuntimeTypeInfo); | 1069 registry.registerStaticUse( |
| 1070 new StaticUse.staticInvoke( |
| 1071 // TODO(johnniwinther): Find the right [CallStructure]. |
| 1072 helpers.setRuntimeTypeInfo, null)); |
| 1070 } | 1073 } |
| 1071 if (type.element == typeImplementation) { | 1074 if (type.element == typeImplementation) { |
| 1072 // If we use a type literal in a constant, the compile time | 1075 // If we use a type literal in a constant, the compile time |
| 1073 // constant emitter will generate a call to the createRuntimeType | 1076 // constant emitter will generate a call to the createRuntimeType |
| 1074 // helper so we register a use of that. | 1077 // helper so we register a use of that. |
| 1075 registry.registerStaticInvocation(helpers.createRuntimeType); | 1078 registry.registerStaticUse( |
| 1079 new StaticUse.staticInvoke( |
| 1080 // TODO(johnniwinther): Find the right [CallStructure]. |
| 1081 |
| 1082 helpers.createRuntimeType, null)); |
| 1076 } | 1083 } |
| 1077 } | 1084 } |
| 1078 } | 1085 } |
| 1079 | 1086 |
| 1080 void registerMetadataConstant(MetadataAnnotation metadata, | 1087 void registerMetadataConstant(MetadataAnnotation metadata, |
| 1081 Element annotatedElement, | 1088 Element annotatedElement, |
| 1082 Registry registry) { | 1089 Registry registry) { |
| 1083 assert(registry.isForResolution); | 1090 assert(registry.isForResolution); |
| 1084 ConstantValue constant = constants.getConstantValueForMetadata(metadata); | 1091 ConstantValue constant = constants.getConstantValueForMetadata(metadata); |
| 1085 registerCompileTimeConstant(constant, registry); | 1092 registerCompileTimeConstant(constant, registry); |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1410 } | 1417 } |
| 1411 } | 1418 } |
| 1412 reporter.reportErrorMessage( | 1419 reporter.reportErrorMessage( |
| 1413 node, | 1420 node, |
| 1414 MessageKind.WRONG_ARGUMENT_FOR_JS_INTERCEPTOR_CONSTANT); | 1421 MessageKind.WRONG_ARGUMENT_FOR_JS_INTERCEPTOR_CONSTANT); |
| 1415 } | 1422 } |
| 1416 } | 1423 } |
| 1417 | 1424 |
| 1418 void enableNoSuchMethod(Enqueuer world) { | 1425 void enableNoSuchMethod(Enqueuer world) { |
| 1419 enqueue(world, helpers.createInvocationMirror, compiler.globalDependencies); | 1426 enqueue(world, helpers.createInvocationMirror, compiler.globalDependencies); |
| 1420 world.registerInvocation( | 1427 world.registerDynamicUse( |
| 1421 new UniverseSelector(Selectors.noSuchMethod_, null)); | 1428 new UniverseSelector(Selectors.noSuchMethod_, null)); |
| 1422 } | 1429 } |
| 1423 | 1430 |
| 1424 void enableIsolateSupport(Enqueuer enqueuer) { | 1431 void enableIsolateSupport(Enqueuer enqueuer) { |
| 1425 // TODO(floitsch): We should also ensure that the class IsolateMessage is | 1432 // TODO(floitsch): We should also ensure that the class IsolateMessage is |
| 1426 // instantiated. Currently, just enabling isolate support works. | 1433 // instantiated. Currently, just enabling isolate support works. |
| 1427 if (compiler.mainFunction != null) { | 1434 if (compiler.mainFunction != null) { |
| 1428 // The JavaScript backend implements [Isolate.spawn] by looking up | 1435 // The JavaScript backend implements [Isolate.spawn] by looking up |
| 1429 // top-level functions by name. So all top-level function tear-off | 1436 // top-level functions by name. So all top-level function tear-off |
| 1430 // closures have a private name field. | 1437 // closures have a private name field. |
| 1431 // | 1438 // |
| 1432 // The JavaScript backend of [Isolate.spawnUri] uses the same internal | 1439 // The JavaScript backend of [Isolate.spawnUri] uses the same internal |
| 1433 // implementation as [Isolate.spawn], and fails if it cannot look main up | 1440 // implementation as [Isolate.spawn], and fails if it cannot look main up |
| 1434 // by name. | 1441 // by name. |
| 1435 enqueuer.registerGetOfStaticFunction(compiler.mainFunction); | 1442 enqueuer.registerStaticUse( |
| 1443 new StaticUse.staticTearOff(compiler.mainFunction)); |
| 1436 } | 1444 } |
| 1437 if (enqueuer.isResolutionQueue) { | 1445 if (enqueuer.isResolutionQueue) { |
| 1438 | 1446 |
| 1439 void enqueue(Element element) { | 1447 void enqueue(Element element) { |
| 1440 enqueuer.addToWorkList(element); | 1448 enqueuer.addToWorkList(element); |
| 1441 compiler.globalDependencies.registerDependency(element); | 1449 compiler.globalDependencies.registerDependency(element); |
| 1442 helpersUsed.add(element.declaration); | 1450 helpersUsed.add(element.declaration); |
| 1443 } | 1451 } |
| 1444 | 1452 |
| 1445 enqueue(helpers.startRootIsolate); | 1453 enqueue(helpers.startRootIsolate); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1581 if (Elements.isStaticOrTopLevel(element)) { | 1589 if (Elements.isStaticOrTopLevel(element)) { |
| 1582 return impactTransformer.transformCodegenImpact( | 1590 return impactTransformer.transformCodegenImpact( |
| 1583 work.registry.worldImpact); | 1591 work.registry.worldImpact); |
| 1584 } | 1592 } |
| 1585 } else { | 1593 } else { |
| 1586 // If the constant-handler was not able to produce a result we have to | 1594 // If the constant-handler was not able to produce a result we have to |
| 1587 // go through the builder (below) to generate the lazy initializer for | 1595 // go through the builder (below) to generate the lazy initializer for |
| 1588 // the static variable. | 1596 // the static variable. |
| 1589 // We also need to register the use of the cyclic-error helper. | 1597 // We also need to register the use of the cyclic-error helper. |
| 1590 compiler.enqueuer.codegen.registerStaticUse( | 1598 compiler.enqueuer.codegen.registerStaticUse( |
| 1591 helpers.cyclicThrowHelper); | 1599 new StaticUse.staticInvoke( |
| 1600 helpers.cyclicThrowHelper, CallStructure.ONE_ARG)); |
| 1592 } | 1601 } |
| 1593 } | 1602 } |
| 1594 | 1603 |
| 1595 generatedCode[element] = functionCompiler.compile(work); | 1604 generatedCode[element] = functionCompiler.compile(work); |
| 1596 return impactTransformer.transformCodegenImpact(work.registry.worldImpact); | 1605 return impactTransformer.transformCodegenImpact(work.registry.worldImpact); |
| 1597 } | 1606 } |
| 1598 | 1607 |
| 1599 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) { | 1608 native.NativeEnqueuer nativeResolutionEnqueuer(Enqueuer world) { |
| 1600 return new native.NativeResolutionEnqueuer(world, compiler); | 1609 return new native.NativeResolutionEnqueuer(world, compiler); |
| 1601 } | 1610 } |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1819 } | 1828 } |
| 1820 } | 1829 } |
| 1821 } | 1830 } |
| 1822 } | 1831 } |
| 1823 } | 1832 } |
| 1824 | 1833 |
| 1825 void registerCheckedModeHelpers(Registry registry) { | 1834 void registerCheckedModeHelpers(Registry registry) { |
| 1826 // We register all the helpers in the resolution queue. | 1835 // We register all the helpers in the resolution queue. |
| 1827 // TODO(13155): Find a way to register fewer helpers. | 1836 // TODO(13155): Find a way to register fewer helpers. |
| 1828 for (CheckedModeHelper helper in checkedModeHelpers) { | 1837 for (CheckedModeHelper helper in checkedModeHelpers) { |
| 1829 enqueueInResolution(helper.getElement(compiler), registry); | 1838 enqueueInResolution(helper.getStaticUse(compiler).element, registry); |
| 1830 } | 1839 } |
| 1831 } | 1840 } |
| 1832 | 1841 |
| 1833 /** | 1842 /** |
| 1834 * Returns [:true:] if the checking of [type] is performed directly on the | 1843 * Returns [:true:] if the checking of [type] is performed directly on the |
| 1835 * object and not on an interceptor. | 1844 * object and not on an interceptor. |
| 1836 */ | 1845 */ |
| 1837 bool hasDirectCheckFor(DartType type) { | 1846 bool hasDirectCheckFor(DartType type) { |
| 1838 Element element = type.element; | 1847 Element element = type.element; |
| 1839 return element == coreClasses.stringClass || | 1848 return element == coreClasses.stringClass || |
| (...skipping 974 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2814 } | 2823 } |
| 2815 | 2824 |
| 2816 return transformed; | 2825 return transformed; |
| 2817 } | 2826 } |
| 2818 | 2827 |
| 2819 void registerBackendImpact(TransformedWorldImpact worldImpact, | 2828 void registerBackendImpact(TransformedWorldImpact worldImpact, |
| 2820 BackendImpact backendImpact) { | 2829 BackendImpact backendImpact) { |
| 2821 for (Element staticUse in backendImpact.staticUses) { | 2830 for (Element staticUse in backendImpact.staticUses) { |
| 2822 assert(staticUse != null); | 2831 assert(staticUse != null); |
| 2823 backend.registerBackendUse(staticUse); | 2832 backend.registerBackendUse(staticUse); |
| 2824 worldImpact.registerStaticUse(staticUse); | 2833 worldImpact.registerStaticUse( |
| 2834 // TODO(johnniwinther): Store the correct use in impacts. |
| 2835 new StaticUse.foreignUse(staticUse)); |
| 2825 } | 2836 } |
| 2826 for (InterfaceType instantiatedType in backendImpact.instantiatedTypes) { | 2837 for (InterfaceType instantiatedType in backendImpact.instantiatedTypes) { |
| 2827 backend.registerBackendUse(instantiatedType.element); | 2838 backend.registerBackendUse(instantiatedType.element); |
| 2828 worldImpact.registerInstantiatedType(instantiatedType); | 2839 worldImpact.registerInstantiatedType(instantiatedType); |
| 2829 } | 2840 } |
| 2830 for (ClassElement cls in backendImpact.instantiatedClasses) { | 2841 for (ClassElement cls in backendImpact.instantiatedClasses) { |
| 2831 cls.ensureResolved(backend.resolution); | 2842 cls.ensureResolved(backend.resolution); |
| 2832 backend.registerBackendUse(cls); | 2843 backend.registerBackendUse(cls); |
| 2833 worldImpact.registerInstantiatedType(cls.rawType); | 2844 worldImpact.registerInstantiatedType(cls.rawType); |
| 2834 } | 2845 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2893 // [registerIsCheck] is also called for checked mode checks, so we | 2904 // [registerIsCheck] is also called for checked mode checks, so we |
| 2894 // need to register checked mode helpers. | 2905 // need to register checked mode helpers. |
| 2895 if (inCheckedMode) { | 2906 if (inCheckedMode) { |
| 2896 // All helpers are added to resolution queue in enqueueHelpers. These | 2907 // All helpers are added to resolution queue in enqueueHelpers. These |
| 2897 // calls to enqueueInResolution serve as assertions that the helper was | 2908 // calls to enqueueInResolution serve as assertions that the helper was |
| 2898 // in fact added. | 2909 // in fact added. |
| 2899 // TODO(13155): Find a way to enqueue helpers lazily. | 2910 // TODO(13155): Find a way to enqueue helpers lazily. |
| 2900 CheckedModeHelper helper = | 2911 CheckedModeHelper helper = |
| 2901 backend.getCheckedModeHelper(type, typeCast: false); | 2912 backend.getCheckedModeHelper(type, typeCast: false); |
| 2902 if (helper != null) { | 2913 if (helper != null) { |
| 2903 Element helperElement = helper.getElement(backend.compiler); | 2914 StaticUse staticUse = helper.getStaticUse(backend.compiler); |
| 2904 transformed.registerStaticUse(helperElement); | 2915 transformed.registerStaticUse(staticUse); |
| 2905 backend.registerBackendUse(helperElement); | 2916 backend.registerBackendUse(staticUse.element); |
| 2906 } | 2917 } |
| 2907 // We also need the native variant of the check (for DOM types). | 2918 // We also need the native variant of the check (for DOM types). |
| 2908 helper = backend.getNativeCheckedModeHelper(type, typeCast: false); | 2919 helper = backend.getNativeCheckedModeHelper(type, typeCast: false); |
| 2909 if (helper != null) { | 2920 if (helper != null) { |
| 2910 Element helperElement = helper.getElement(backend.compiler); | 2921 StaticUse staticUse = helper.getStaticUse(backend.compiler); |
| 2911 transformed.registerStaticUse(helperElement); | 2922 transformed.registerStaticUse(staticUse); |
| 2912 backend.registerBackendUse(helperElement); | 2923 backend.registerBackendUse(staticUse.element); |
| 2913 } | 2924 } |
| 2914 } | 2925 } |
| 2915 if (!type.treatAsRaw || type.containsTypeVariables) { | 2926 if (!type.treatAsRaw || type.containsTypeVariables) { |
| 2916 registerBackendImpact(transformed, impacts.genericIsCheck); | 2927 registerBackendImpact(transformed, impacts.genericIsCheck); |
| 2917 } | 2928 } |
| 2918 if (type.element != null && backend.isNative(type.element)) { | 2929 if (type.element != null && backend.isNative(type.element)) { |
| 2919 // We will neeed to add the "$is" and "$as" properties on the | 2930 // We will neeed to add the "$is" and "$as" properties on the |
| 2920 // JavaScript object prototype, so we make sure | 2931 // JavaScript object prototype, so we make sure |
| 2921 // [:defineProperty:] is compiled. | 2932 // [:defineProperty:] is compiled. |
| 2922 registerBackendImpact(transformed, impacts.nativeTypeCheck); | 2933 registerBackendImpact(transformed, impacts.nativeTypeCheck); |
| 2923 } | 2934 } |
| 2924 } | 2935 } |
| 2925 | 2936 |
| 2926 @override | 2937 @override |
| 2927 WorldImpact transformCodegenImpact(CodegenImpact impact) { | 2938 WorldImpact transformCodegenImpact(CodegenImpact impact) { |
| 2928 TransformedWorldImpact transformed = new TransformedWorldImpact(impact); | 2939 TransformedWorldImpact transformed = new TransformedWorldImpact(impact); |
| 2929 EagerRegistry registry = impact.registry; | 2940 EagerRegistry registry = impact.registry; |
| 2930 Enqueuer world = registry.world; | 2941 Enqueuer world = registry.world; |
| 2931 | 2942 |
| 2932 for (InterfaceType type in impact.instantiatedTypes) { | 2943 for (InterfaceType type in impact.instantiatedTypes) { |
| 2933 backend.lookupMapAnalysis.registerInstantiatedType(type, registry); | 2944 backend.lookupMapAnalysis.registerInstantiatedType(type, registry); |
| 2934 } | 2945 } |
| 2935 | 2946 |
| 2936 for (Element element in impact.getterForSuperElements) { | |
| 2937 world.registerGetterForSuperMethod(element); | |
| 2938 } | |
| 2939 | |
| 2940 for (Element element in impact.fieldGetters) { | |
| 2941 world.registerFieldGetter(element); | |
| 2942 } | |
| 2943 | |
| 2944 for (Element element in impact.fieldSetters) { | |
| 2945 world.registerFieldSetter(element); | |
| 2946 } | |
| 2947 | |
| 2948 for (DartType type in impact.isChecks) { | 2947 for (DartType type in impact.isChecks) { |
| 2949 onIsCheckForCodegen(type, transformed); | 2948 onIsCheckForCodegen(type, transformed); |
| 2950 } | 2949 } |
| 2951 | 2950 |
| 2952 for (ConstantValue constant in impact.compileTimeConstants) { | 2951 for (ConstantValue constant in impact.compileTimeConstants) { |
| 2953 backend.registerCompileTimeConstant(constant, registry); | 2952 backend.registerCompileTimeConstant(constant, registry); |
| 2954 backend.addCompileTimeConstantForEmission(constant); | 2953 backend.addCompileTimeConstantForEmission(constant); |
| 2955 } | 2954 } |
| 2956 | 2955 |
| 2957 for (Pair<DartType, DartType> check in | 2956 for (Pair<DartType, DartType> check in |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3001 } | 3000 } |
| 3002 } | 3001 } |
| 3003 | 3002 |
| 3004 /// Records that [constant] is used by the element behind [registry]. | 3003 /// Records that [constant] is used by the element behind [registry]. |
| 3005 class Dependency { | 3004 class Dependency { |
| 3006 final ConstantValue constant; | 3005 final ConstantValue constant; |
| 3007 final Element annotatedElement; | 3006 final Element annotatedElement; |
| 3008 | 3007 |
| 3009 const Dependency(this.constant, this.annotatedElement); | 3008 const Dependency(this.constant, this.annotatedElement); |
| 3010 } | 3009 } |
| OLD | NEW |