Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(953)

Side by Side Diff: pkg/compiler/lib/src/ssa/builder_kernel.dart

Issue 2916393002: Compute constants from IR nodes (Closed)
Patch Set: Updated cf. comments Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'package:kernel/ast.dart' as ir; 5 import 'package:kernel/ast.dart' as ir;
6 6
7 import '../closure.dart'; 7 import '../closure.dart';
8 import '../common.dart'; 8 import '../common.dart';
9 import '../common/codegen.dart' show CodegenRegistry; 9 import '../common/codegen.dart' show CodegenRegistry;
10 import '../common/names.dart'; 10 import '../common/names.dart';
(...skipping 1431 matching lines...) Expand 10 before | Expand all | Expand 10 after
1442 } 1442 }
1443 1443
1444 /// Loop through the cases in a switch and create a mapping of case 1444 /// Loop through the cases in a switch and create a mapping of case
1445 /// expressions to constants. 1445 /// expressions to constants.
1446 Map<ir.Expression, ConstantValue> _buildSwitchCaseConstants( 1446 Map<ir.Expression, ConstantValue> _buildSwitchCaseConstants(
1447 ir.SwitchStatement switchStatement) { 1447 ir.SwitchStatement switchStatement) {
1448 Map<ir.Expression, ConstantValue> constants = 1448 Map<ir.Expression, ConstantValue> constants =
1449 new Map<ir.Expression, ConstantValue>(); 1449 new Map<ir.Expression, ConstantValue>();
1450 for (ir.SwitchCase switchCase in switchStatement.cases) { 1450 for (ir.SwitchCase switchCase in switchStatement.cases) {
1451 for (ir.Expression caseExpression in switchCase.expressions) { 1451 for (ir.Expression caseExpression in switchCase.expressions) {
1452 ConstantValue constant = astAdapter.getConstantFor(caseExpression); 1452 ConstantValue constant = _elementMap.getConstantValue(caseExpression);
1453 constants[caseExpression] = constant; 1453 constants[caseExpression] = constant;
1454 } 1454 }
1455 } 1455 }
1456 return constants; 1456 return constants;
1457 } 1457 }
1458 1458
1459 @override 1459 @override
1460 void visitContinueSwitchStatement( 1460 void visitContinueSwitchStatement(
1461 ir.ContinueSwitchStatement switchStatement) { 1461 ir.ContinueSwitchStatement switchStatement) {
1462 handleInTryStatement(); 1462 handleInTryStatement();
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after
1813 } 1813 }
1814 1814
1815 @override 1815 @override
1816 void visitStringLiteral(ir.StringLiteral stringLiteral) { 1816 void visitStringLiteral(ir.StringLiteral stringLiteral) {
1817 stack.add(graph.addConstantString(stringLiteral.value, closedWorld)); 1817 stack.add(graph.addConstantString(stringLiteral.value, closedWorld));
1818 } 1818 }
1819 1819
1820 @override 1820 @override
1821 void visitSymbolLiteral(ir.SymbolLiteral symbolLiteral) { 1821 void visitSymbolLiteral(ir.SymbolLiteral symbolLiteral) {
1822 stack.add(graph.addConstant( 1822 stack.add(graph.addConstant(
1823 astAdapter.getConstantForSymbol(symbolLiteral), closedWorld)); 1823 _elementMap.getConstantValue(symbolLiteral), closedWorld));
1824 registry?.registerConstSymbol(symbolLiteral.value); 1824 registry?.registerConstSymbol(symbolLiteral.value);
1825 } 1825 }
1826 1826
1827 @override 1827 @override
1828 void visitNullLiteral(ir.NullLiteral nullLiteral) { 1828 void visitNullLiteral(ir.NullLiteral nullLiteral) {
1829 stack.add(graph.addConstantNull(closedWorld)); 1829 stack.add(graph.addConstantNull(closedWorld));
1830 } 1830 }
1831 1831
1832 /// Set the runtime type information if necessary. 1832 /// Set the runtime type information if necessary.
1833 HInstruction setListRuntimeTypeInfoIfNeeded( 1833 HInstruction setListRuntimeTypeInfoIfNeeded(
(...skipping 10 matching lines...) Expand all
1844 // TODO(15489): Register at codegen. 1844 // TODO(15489): Register at codegen.
1845 registry?.registerInstantiation(type); 1845 registry?.registerInstantiation(type);
1846 return callSetRuntimeTypeInfoWithTypeArguments(type, arguments, object); 1846 return callSetRuntimeTypeInfoWithTypeArguments(type, arguments, object);
1847 } 1847 }
1848 1848
1849 @override 1849 @override
1850 void visitListLiteral(ir.ListLiteral listLiteral) { 1850 void visitListLiteral(ir.ListLiteral listLiteral) {
1851 HInstruction listInstruction; 1851 HInstruction listInstruction;
1852 if (listLiteral.isConst) { 1852 if (listLiteral.isConst) {
1853 listInstruction = graph.addConstant( 1853 listInstruction = graph.addConstant(
1854 astAdapter.getConstantFor(listLiteral), closedWorld); 1854 _elementMap.getConstantValue(listLiteral), closedWorld);
1855 } else { 1855 } else {
1856 List<HInstruction> elements = <HInstruction>[]; 1856 List<HInstruction> elements = <HInstruction>[];
1857 for (ir.Expression element in listLiteral.expressions) { 1857 for (ir.Expression element in listLiteral.expressions) {
1858 element.accept(this); 1858 element.accept(this);
1859 elements.add(pop()); 1859 elements.add(pop());
1860 } 1860 }
1861 listInstruction = 1861 listInstruction =
1862 new HLiteralList(elements, commonMasks.extendableArrayType); 1862 new HLiteralList(elements, commonMasks.extendableArrayType);
1863 add(listInstruction); 1863 add(listInstruction);
1864 listInstruction = 1864 listInstruction =
1865 setListRuntimeTypeInfoIfNeeded(listInstruction, listLiteral); 1865 setListRuntimeTypeInfoIfNeeded(listInstruction, listLiteral);
1866 } 1866 }
1867 1867
1868 TypeMask type = _typeInferenceMap.typeOfListLiteral( 1868 TypeMask type = _typeInferenceMap.typeOfListLiteral(
1869 targetElement, listLiteral, closedWorld); 1869 targetElement, listLiteral, closedWorld);
1870 if (!type.containsAll(closedWorld)) { 1870 if (!type.containsAll(closedWorld)) {
1871 listInstruction.instructionType = type; 1871 listInstruction.instructionType = type;
1872 } 1872 }
1873 stack.add(listInstruction); 1873 stack.add(listInstruction);
1874 } 1874 }
1875 1875
1876 @override 1876 @override
1877 void visitMapLiteral(ir.MapLiteral mapLiteral) { 1877 void visitMapLiteral(ir.MapLiteral mapLiteral) {
1878 if (mapLiteral.isConst) { 1878 if (mapLiteral.isConst) {
1879 stack.add(graph.addConstant( 1879 stack.add(graph.addConstant(
1880 astAdapter.getConstantFor(mapLiteral), closedWorld)); 1880 _elementMap.getConstantValue(mapLiteral), closedWorld));
1881 return; 1881 return;
1882 } 1882 }
1883 1883
1884 // The map literal constructors take the key-value pairs as a List 1884 // The map literal constructors take the key-value pairs as a List
1885 List<HInstruction> constructorArgs = <HInstruction>[]; 1885 List<HInstruction> constructorArgs = <HInstruction>[];
1886 for (ir.MapEntry mapEntry in mapLiteral.entries) { 1886 for (ir.MapEntry mapEntry in mapLiteral.entries) {
1887 mapEntry.accept(this); 1887 mapEntry.accept(this);
1888 constructorArgs.add(pop()); 1888 constructorArgs.add(pop());
1889 constructorArgs.add(pop()); 1889 constructorArgs.add(pop());
1890 } 1890 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1956 // Visit value before the key because each will push an expression to the 1956 // Visit value before the key because each will push an expression to the
1957 // stack, so when we pop them off, the key is popped first, then the value. 1957 // stack, so when we pop them off, the key is popped first, then the value.
1958 mapEntry.value.accept(this); 1958 mapEntry.value.accept(this);
1959 mapEntry.key.accept(this); 1959 mapEntry.key.accept(this);
1960 } 1960 }
1961 1961
1962 @override 1962 @override
1963 void visitTypeLiteral(ir.TypeLiteral typeLiteral) { 1963 void visitTypeLiteral(ir.TypeLiteral typeLiteral) {
1964 ir.DartType type = typeLiteral.type; 1964 ir.DartType type = typeLiteral.type;
1965 if (type is ir.InterfaceType || type is ir.DynamicType) { 1965 if (type is ir.InterfaceType || type is ir.DynamicType) {
1966 ConstantValue constant = astAdapter.getConstantForType(type); 1966 ConstantValue constant = _elementMap.getConstantValue(typeLiteral);
1967 stack.add(graph.addConstant(constant, closedWorld)); 1967 stack.add(graph.addConstant(constant, closedWorld));
1968 return; 1968 return;
1969 } 1969 }
1970 // For other types (e.g. TypeParameterType, function types from expanded 1970 // For other types (e.g. TypeParameterType, function types from expanded
1971 // typedefs), look-up or construct a reified type representation and convert 1971 // typedefs), look-up or construct a reified type representation and convert
1972 // to a RuntimeType. 1972 // to a RuntimeType.
1973 1973
1974 // TODO(sra): Convert the type logic here to use ir.DartType. 1974 // TODO(sra): Convert the type logic here to use ir.DartType.
1975 ResolutionDartType dartType = _elementMap.getDartType(type); 1975 ResolutionDartType dartType = _elementMap.getDartType(type);
1976 dartType = localsHandler.substInContext(dartType); 1976 dartType = localsHandler.substInContext(dartType);
(...skipping 12 matching lines...) Expand all
1989 ir.Member staticTarget = staticGet.target; 1989 ir.Member staticTarget = staticGet.target;
1990 if (staticTarget is ir.Procedure && 1990 if (staticTarget is ir.Procedure &&
1991 staticTarget.kind == ir.ProcedureKind.Getter) { 1991 staticTarget.kind == ir.ProcedureKind.Getter) {
1992 FunctionEntity getter = _elementMap.getMember(staticTarget); 1992 FunctionEntity getter = _elementMap.getMember(staticTarget);
1993 // Invoke the getter 1993 // Invoke the getter
1994 _pushStaticInvocation(getter, const <HInstruction>[], 1994 _pushStaticInvocation(getter, const <HInstruction>[],
1995 _typeInferenceMap.getReturnTypeOf(getter)); 1995 _typeInferenceMap.getReturnTypeOf(getter));
1996 } else if (staticTarget is ir.Field && 1996 } else if (staticTarget is ir.Field &&
1997 (staticTarget.isConst || 1997 (staticTarget.isConst ||
1998 staticTarget.isFinal && !_isLazyStatic(staticTarget))) { 1998 staticTarget.isFinal && !_isLazyStatic(staticTarget))) {
1999 stack.add(graph.addConstant( 1999 ConstantValue value = _elementMap.getConstantValue(
2000 astAdapter.getConstantFor(staticTarget.initializer), closedWorld)); 2000 staticTarget.initializer,
2001 requireConstant: staticTarget.isConst);
2002 if (value != null) {
2003 stack.add(graph.addConstant(value, closedWorld));
2004 } else {
2005 FieldEntity field = _elementMap.getField(staticTarget);
2006 push(
2007 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field)));
2008 }
2001 } else { 2009 } else {
2002 if (_isLazyStatic(staticTarget)) { 2010 if (_isLazyStatic(staticTarget)) {
2003 FieldEntity field = _elementMap.getField(staticTarget); 2011 FieldEntity field = _elementMap.getField(staticTarget);
2004 push( 2012 push(
2005 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field))); 2013 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field)));
2006 } else { 2014 } else {
2007 MemberEntity member = _elementMap.getMember(staticTarget); 2015 MemberEntity member = _elementMap.getMember(staticTarget);
2008 push(new HStatic(member, _typeInferenceMap.getInferredTypeOf(member))); 2016 push(new HStatic(member, _typeInferenceMap.getInferredTypeOf(member)));
2009 } 2017 }
2010 } 2018 }
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
2221 for (ir.DartType type in arguments.types) { 2229 for (ir.DartType type in arguments.types) {
2222 values.add(typeBuilder.analyzeTypeArgument( 2230 values.add(typeBuilder.analyzeTypeArgument(
2223 _elementMap.getDartType(type), sourceElement)); 2231 _elementMap.getDartType(type), sourceElement));
2224 } 2232 }
2225 } 2233 }
2226 2234
2227 HInstruction _defaultValueForParameter(ir.VariableDeclaration parameter) { 2235 HInstruction _defaultValueForParameter(ir.VariableDeclaration parameter) {
2228 ir.Expression initializer = parameter.initializer; 2236 ir.Expression initializer = parameter.initializer;
2229 if (initializer == null) return graph.addConstantNull(closedWorld); 2237 if (initializer == null) return graph.addConstantNull(closedWorld);
2230 // TODO(sra): Evaluate constant in ir.Node domain. 2238 // TODO(sra): Evaluate constant in ir.Node domain.
2231 ConstantValue constant = 2239 ConstantValue constant = _elementMap.getConstantValue(initializer);
2232 astAdapter.getConstantForParameterDefaultValue(initializer);
2233 if (constant == null) return graph.addConstantNull(closedWorld); 2240 if (constant == null) return graph.addConstantNull(closedWorld);
2234 return graph.addConstant(constant, closedWorld); 2241 return graph.addConstant(constant, closedWorld);
2235 } 2242 }
2236 2243
2237 @override 2244 @override
2238 void visitStaticInvocation(ir.StaticInvocation invocation) { 2245 void visitStaticInvocation(ir.StaticInvocation invocation) {
2239 ir.Procedure target = invocation.target; 2246 ir.Procedure target = invocation.target;
2240 if (_elementMap.isForeignLibrary(target.enclosingLibrary)) { 2247 if (_elementMap.isForeignLibrary(target.enclosingLibrary)) {
2241 handleInvokeStaticForeign(invocation, target); 2248 handleInvokeStaticForeign(invocation, target);
2242 return; 2249 return;
2243 } 2250 }
2244 FunctionEntity function = _elementMap.getMember(target); 2251 FunctionEntity function = _elementMap.getMember(target);
2245 TypeMask typeMask = _typeInferenceMap.getReturnTypeOf(function); 2252 TypeMask typeMask = _typeInferenceMap.getReturnTypeOf(function);
2246 2253
2247 // TODO(sra): For JS interop external functions, use a different function to 2254 // TODO(sra): For JS interop external functions, use a different function to
2248 // build arguments. 2255 // build arguments.
2249 List<HInstruction> arguments = 2256 List<HInstruction> arguments =
2250 _visitArgumentsForStaticTarget(target.function, invocation.arguments); 2257 _visitArgumentsForStaticTarget(target.function, invocation.arguments);
2251 2258
2252 if (function is ConstructorEntity && function.isFactoryConstructor) { 2259 if (function is ConstructorEntity && function.isFactoryConstructor) {
2253 if (function.isExternal && function.isFromEnvironmentConstructor) { 2260 if (function.isExternal && function.isFromEnvironmentConstructor) {
2254 if (invocation.isConst) { 2261 if (invocation.isConst) {
2255 // Just like all const constructors (see visitConstructorInvocation). 2262 // Just like all const constructors (see visitConstructorInvocation).
2256 stack.add(graph.addConstant( 2263 stack.add(graph.addConstant(
2257 astAdapter.getConstantFor(invocation), closedWorld)); 2264 _elementMap.getConstantValue(invocation), closedWorld));
2258 } else { 2265 } else {
2259 generateUnsupportedError( 2266 generateUnsupportedError(
2260 invocation, 2267 invocation,
2261 '${function.enclosingClass.name}.${function.name} ' 2268 '${function.enclosingClass.name}.${function.name} '
2262 'can only be used as a const constructor'); 2269 'can only be used as a const constructor');
2263 } 2270 }
2264 return; 2271 return;
2265 } 2272 }
2266 2273
2267 // Factory constructors take type parameters; other static methods ignore 2274 // Factory constructors take type parameters; other static methods ignore
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
2953 _elementMap.getSelector(invocation), 2960 _elementMap.getSelector(invocation),
2954 _elementMap.getClass(_containingClass(invocation)), 2961 _elementMap.getClass(_containingClass(invocation)),
2955 _elementMap.getMethod(invocation.interfaceTarget), 2962 _elementMap.getMethod(invocation.interfaceTarget),
2956 arguments); 2963 arguments);
2957 } 2964 }
2958 2965
2959 @override 2966 @override
2960 void visitConstructorInvocation(ir.ConstructorInvocation invocation) { 2967 void visitConstructorInvocation(ir.ConstructorInvocation invocation) {
2961 ir.Constructor target = invocation.target; 2968 ir.Constructor target = invocation.target;
2962 if (invocation.isConst) { 2969 if (invocation.isConst) {
2963 ConstantValue constant = astAdapter.getConstantFor(invocation); 2970 ConstantValue constant = _elementMap.getConstantValue(invocation);
2964 stack.add(graph.addConstant(constant, closedWorld)); 2971 stack.add(graph.addConstant(constant, closedWorld));
2965 return; 2972 return;
2966 } 2973 }
2967 2974
2968 // TODO(sra): For JS-interop targets, process arguments differently. 2975 // TODO(sra): For JS-interop targets, process arguments differently.
2969 List<HInstruction> arguments = 2976 List<HInstruction> arguments =
2970 _visitArgumentsForStaticTarget(target.function, invocation.arguments); 2977 _visitArgumentsForStaticTarget(target.function, invocation.arguments);
2971 ConstructorEntity constructor = _elementMap.getConstructor(target); 2978 ConstructorEntity constructor = _elementMap.getConstructor(target);
2972 ClassEntity cls = constructor.enclosingClass; 2979 ClassEntity cls = constructor.enclosingClass;
2973 if (backend.rtiNeed.classNeedsRti(cls)) { 2980 if (backend.rtiNeed.classNeedsRti(cls)) {
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
3406 enterBlock.setBlockFlow( 3413 enterBlock.setBlockFlow(
3407 new HTryBlockInformation( 3414 new HTryBlockInformation(
3408 kernelBuilder.wrapStatementGraph(bodyGraph), 3415 kernelBuilder.wrapStatementGraph(bodyGraph),
3409 exception, 3416 exception,
3410 kernelBuilder.wrapStatementGraph(catchGraph), 3417 kernelBuilder.wrapStatementGraph(catchGraph),
3411 kernelBuilder.wrapStatementGraph(finallyGraph)), 3418 kernelBuilder.wrapStatementGraph(finallyGraph)),
3412 exitBlock); 3419 exitBlock);
3413 kernelBuilder.inTryStatement = previouslyInTryStatement; 3420 kernelBuilder.inTryStatement = previouslyInTryStatement;
3414 } 3421 }
3415 } 3422 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/kernel/element_map_impl.dart ('k') | pkg/compiler/lib/src/ssa/kernel_ast_adapter.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698