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

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

Issue 2916393002: Compute constants from IR nodes (Closed)
Patch Set: 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 print('$staticGet');
Siggi Cherem (dart-lang) 2017/06/02 19:26:03 delete?
2006 FieldEntity field = _elementMap.getField(staticTarget);
2007 push(
2008 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field)));
2009 }
2001 } else { 2010 } else {
2002 if (_isLazyStatic(staticTarget)) { 2011 if (_isLazyStatic(staticTarget)) {
2003 FieldEntity field = _elementMap.getField(staticTarget); 2012 FieldEntity field = _elementMap.getField(staticTarget);
2004 push( 2013 push(
2005 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field))); 2014 new HLazyStatic(field, _typeInferenceMap.getInferredTypeOf(field)));
2006 } else { 2015 } else {
2007 MemberEntity member = _elementMap.getMember(staticTarget); 2016 MemberEntity member = _elementMap.getMember(staticTarget);
2008 push(new HStatic(member, _typeInferenceMap.getInferredTypeOf(member))); 2017 push(new HStatic(member, _typeInferenceMap.getInferredTypeOf(member)));
2009 } 2018 }
2010 } 2019 }
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
2221 for (ir.DartType type in arguments.types) { 2230 for (ir.DartType type in arguments.types) {
2222 values.add(typeBuilder.analyzeTypeArgument( 2231 values.add(typeBuilder.analyzeTypeArgument(
2223 _elementMap.getDartType(type), sourceElement)); 2232 _elementMap.getDartType(type), sourceElement));
2224 } 2233 }
2225 } 2234 }
2226 2235
2227 HInstruction _defaultValueForParameter(ir.VariableDeclaration parameter) { 2236 HInstruction _defaultValueForParameter(ir.VariableDeclaration parameter) {
2228 ir.Expression initializer = parameter.initializer; 2237 ir.Expression initializer = parameter.initializer;
2229 if (initializer == null) return graph.addConstantNull(closedWorld); 2238 if (initializer == null) return graph.addConstantNull(closedWorld);
2230 // TODO(sra): Evaluate constant in ir.Node domain. 2239 // TODO(sra): Evaluate constant in ir.Node domain.
2231 ConstantValue constant = 2240 ConstantValue constant = _elementMap.getConstantValue(initializer);
2232 astAdapter.getConstantForParameterDefaultValue(initializer);
2233 if (constant == null) return graph.addConstantNull(closedWorld); 2241 if (constant == null) return graph.addConstantNull(closedWorld);
2234 return graph.addConstant(constant, closedWorld); 2242 return graph.addConstant(constant, closedWorld);
2235 } 2243 }
2236 2244
2237 @override 2245 @override
2238 void visitStaticInvocation(ir.StaticInvocation invocation) { 2246 void visitStaticInvocation(ir.StaticInvocation invocation) {
2239 ir.Procedure target = invocation.target; 2247 ir.Procedure target = invocation.target;
2240 if (_elementMap.isForeignLibrary(target.enclosingLibrary)) { 2248 if (_elementMap.isForeignLibrary(target.enclosingLibrary)) {
2241 handleInvokeStaticForeign(invocation, target); 2249 handleInvokeStaticForeign(invocation, target);
2242 return; 2250 return;
2243 } 2251 }
2244 FunctionEntity function = _elementMap.getMember(target); 2252 FunctionEntity function = _elementMap.getMember(target);
2245 TypeMask typeMask = _typeInferenceMap.getReturnTypeOf(function); 2253 TypeMask typeMask = _typeInferenceMap.getReturnTypeOf(function);
2246 2254
2247 // TODO(sra): For JS interop external functions, use a different function to 2255 // TODO(sra): For JS interop external functions, use a different function to
2248 // build arguments. 2256 // build arguments.
2249 List<HInstruction> arguments = 2257 List<HInstruction> arguments =
2250 _visitArgumentsForStaticTarget(target.function, invocation.arguments); 2258 _visitArgumentsForStaticTarget(target.function, invocation.arguments);
2251 2259
2252 if (function is ConstructorEntity && function.isFactoryConstructor) { 2260 if (function is ConstructorEntity && function.isFactoryConstructor) {
2253 if (function.isExternal && function.isFromEnvironmentConstructor) { 2261 if (function.isExternal && function.isFromEnvironmentConstructor) {
2254 if (invocation.isConst) { 2262 if (invocation.isConst) {
2255 // Just like all const constructors (see visitConstructorInvocation). 2263 // Just like all const constructors (see visitConstructorInvocation).
2256 stack.add(graph.addConstant( 2264 stack.add(graph.addConstant(
2257 astAdapter.getConstantFor(invocation), closedWorld)); 2265 _elementMap.getConstantValue(invocation), closedWorld));
2258 } else { 2266 } else {
2259 generateUnsupportedError( 2267 generateUnsupportedError(
2260 invocation, 2268 invocation,
2261 '${function.enclosingClass.name}.${function.name} ' 2269 '${function.enclosingClass.name}.${function.name} '
2262 'can only be used as a const constructor'); 2270 'can only be used as a const constructor');
2263 } 2271 }
2264 return; 2272 return;
2265 } 2273 }
2266 2274
2267 // Factory constructors take type parameters; other static methods ignore 2275 // 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), 2961 _elementMap.getSelector(invocation),
2954 _elementMap.getClass(_containingClass(invocation)), 2962 _elementMap.getClass(_containingClass(invocation)),
2955 _elementMap.getMethod(invocation.interfaceTarget), 2963 _elementMap.getMethod(invocation.interfaceTarget),
2956 arguments); 2964 arguments);
2957 } 2965 }
2958 2966
2959 @override 2967 @override
2960 void visitConstructorInvocation(ir.ConstructorInvocation invocation) { 2968 void visitConstructorInvocation(ir.ConstructorInvocation invocation) {
2961 ir.Constructor target = invocation.target; 2969 ir.Constructor target = invocation.target;
2962 if (invocation.isConst) { 2970 if (invocation.isConst) {
2963 ConstantValue constant = astAdapter.getConstantFor(invocation); 2971 ConstantValue constant = _elementMap.getConstantValue(invocation);
2964 stack.add(graph.addConstant(constant, closedWorld)); 2972 stack.add(graph.addConstant(constant, closedWorld));
2965 return; 2973 return;
2966 } 2974 }
2967 2975
2968 // TODO(sra): For JS-interop targets, process arguments differently. 2976 // TODO(sra): For JS-interop targets, process arguments differently.
2969 List<HInstruction> arguments = 2977 List<HInstruction> arguments =
2970 _visitArgumentsForStaticTarget(target.function, invocation.arguments); 2978 _visitArgumentsForStaticTarget(target.function, invocation.arguments);
2971 ConstructorEntity constructor = _elementMap.getConstructor(target); 2979 ConstructorEntity constructor = _elementMap.getConstructor(target);
2972 ClassEntity cls = constructor.enclosingClass; 2980 ClassEntity cls = constructor.enclosingClass;
2973 if (backend.rtiNeed.classNeedsRti(cls)) { 2981 if (backend.rtiNeed.classNeedsRti(cls)) {
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
3406 enterBlock.setBlockFlow( 3414 enterBlock.setBlockFlow(
3407 new HTryBlockInformation( 3415 new HTryBlockInformation(
3408 kernelBuilder.wrapStatementGraph(bodyGraph), 3416 kernelBuilder.wrapStatementGraph(bodyGraph),
3409 exception, 3417 exception,
3410 kernelBuilder.wrapStatementGraph(catchGraph), 3418 kernelBuilder.wrapStatementGraph(catchGraph),
3411 kernelBuilder.wrapStatementGraph(finallyGraph)), 3419 kernelBuilder.wrapStatementGraph(finallyGraph)),
3412 exitBlock); 3420 exitBlock);
3413 kernelBuilder.inTryStatement = previouslyInTryStatement; 3421 kernelBuilder.inTryStatement = previouslyInTryStatement;
3414 } 3422 }
3415 } 3423 }
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