| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library dart2js.ir_builder; | 5 library dart2js.ir_builder; |
| 6 | 6 |
| 7 import '../compile_time_constants.dart' show BackendConstantEnvironment; |
| 7 import '../constants/constant_system.dart'; | 8 import '../constants/constant_system.dart'; |
| 8 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
| 9 import '../constants/values.dart' show PrimitiveConstantValue; | 10 import '../constants/values.dart' show ConstantValue, PrimitiveConstantValue; |
| 10 import '../dart_types.dart'; | 11 import '../dart_types.dart'; |
| 11 import '../dart2jslib.dart'; | 12 import '../dart2jslib.dart'; |
| 12 import '../elements/elements.dart'; | 13 import '../elements/elements.dart'; |
| 13 import '../io/source_information.dart'; | 14 import '../io/source_information.dart'; |
| 14 import '../tree/tree.dart' as ast; | 15 import '../tree/tree.dart' as ast; |
| 15 import '../closure.dart' hide ClosureScope; | 16 import '../closure.dart' hide ClosureScope; |
| 16 import '../universe/universe.dart' show SelectorKind; | 17 import '../universe/universe.dart' show SelectorKind; |
| 17 import 'cps_ir_nodes.dart' as ir; | 18 import 'cps_ir_nodes.dart' as ir; |
| 18 import 'cps_ir_builder_task.dart' show DartCapturedVariables, | 19 import 'cps_ir_builder_task.dart' show DartCapturedVariables, |
| 19 GlobalProgramInformation; | 20 GlobalProgramInformation; |
| (...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 // `List` instead of `Link`. | 378 // `List` instead of `Link`. |
| 378 SubbuildFunction subbuildSequence(/*Iterable<N>*/ nodes) { | 379 SubbuildFunction subbuildSequence(/*Iterable<N>*/ nodes) { |
| 379 return (IrBuilder builder) { | 380 return (IrBuilder builder) { |
| 380 return withBuilder(builder, () => builder.buildSequence(nodes, build)); | 381 return withBuilder(builder, () => builder.buildSequence(nodes, build)); |
| 381 }; | 382 }; |
| 382 } | 383 } |
| 383 } | 384 } |
| 384 | 385 |
| 385 /// Shared state between delimited IrBuilders within the same function. | 386 /// Shared state between delimited IrBuilders within the same function. |
| 386 class IrBuilderSharedState { | 387 class IrBuilderSharedState { |
| 387 final ConstantSystem constantSystem; | 388 final BackendConstantEnvironment constants; |
| 389 |
| 390 ConstantSystem get constantSystem => constants.constantSystem; |
| 388 | 391 |
| 389 /// A stack of collectors for breaks. | 392 /// A stack of collectors for breaks. |
| 390 final List<JumpCollector> breakCollectors = <JumpCollector>[]; | 393 final List<JumpCollector> breakCollectors = <JumpCollector>[]; |
| 391 | 394 |
| 392 /// A stack of collectors for continues. | 395 /// A stack of collectors for continues. |
| 393 final List<JumpCollector> continueCollectors = <JumpCollector>[]; | 396 final List<JumpCollector> continueCollectors = <JumpCollector>[]; |
| 394 | 397 |
| 395 final List<ConstDeclaration> localConstants = <ConstDeclaration>[]; | 398 final List<ConstDeclaration> localConstants = <ConstDeclaration>[]; |
| 396 | 399 |
| 397 final ExecutableElement currentElement; | 400 final ExecutableElement currentElement; |
| 398 | 401 |
| 399 final ir.Continuation returnContinuation = new ir.Continuation.retrn(); | 402 final ir.Continuation returnContinuation = new ir.Continuation.retrn(); |
| 400 ir.Parameter _thisParameter; | 403 ir.Parameter _thisParameter; |
| 401 ir.Parameter enclosingMethodThisParameter; | 404 ir.Parameter enclosingMethodThisParameter; |
| 402 | 405 |
| 403 final List<ir.Parameter> functionParameters = <ir.Parameter>[]; | 406 final List<ir.Parameter> functionParameters = <ir.Parameter>[]; |
| 404 | 407 |
| 405 IrBuilderSharedState(this.constantSystem, this.currentElement); | 408 IrBuilderSharedState(this.constants, this.currentElement); |
| 406 | 409 |
| 407 ir.Parameter get thisParameter => _thisParameter; | 410 ir.Parameter get thisParameter => _thisParameter; |
| 408 void set thisParameter(ir.Parameter value) { | 411 void set thisParameter(ir.Parameter value) { |
| 409 assert(_thisParameter == null); | 412 assert(_thisParameter == null); |
| 410 _thisParameter = value; | 413 _thisParameter = value; |
| 411 } | 414 } |
| 412 } | 415 } |
| 413 | 416 |
| 414 class ThisParameterLocal implements Local { | 417 class ThisParameterLocal implements Local { |
| 415 final ExecutableElement executableContext; | 418 final ExecutableElement executableContext; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 // | 544 // |
| 542 // We do not pass contexts as arguments or return them. Rather we use the | 545 // We do not pass contexts as arguments or return them. Rather we use the |
| 543 // current context (root, current) as the visitor state and mutate current. | 546 // current context (root, current) as the visitor state and mutate current. |
| 544 // Visiting a statement returns null; visiting an expression returns the | 547 // Visiting a statement returns null; visiting an expression returns the |
| 545 // primitive denoting its value. | 548 // primitive denoting its value. |
| 546 | 549 |
| 547 ir.Expression _root = null; | 550 ir.Expression _root = null; |
| 548 ir.Expression _current = null; | 551 ir.Expression _current = null; |
| 549 | 552 |
| 550 /// Initialize a new top-level IR builder. | 553 /// Initialize a new top-level IR builder. |
| 551 void _init(ConstantSystem constantSystem, ExecutableElement currentElement) { | 554 void _init(BackendConstantEnvironment constants, |
| 552 state = new IrBuilderSharedState(constantSystem, currentElement); | 555 ExecutableElement currentElement) { |
| 556 state = new IrBuilderSharedState(constants, currentElement); |
| 553 environment = new Environment.empty(); | 557 environment = new Environment.empty(); |
| 554 mutableVariables = <Local, ir.MutableVariable>{}; | 558 mutableVariables = <Local, ir.MutableVariable>{}; |
| 555 } | 559 } |
| 556 | 560 |
| 557 /// Construct a delimited visitor for visiting a subtree. | 561 /// Construct a delimited visitor for visiting a subtree. |
| 558 /// | 562 /// |
| 559 /// Build a subterm that is not (yet) connected to the CPS term. The | 563 /// Build a subterm that is not (yet) connected to the CPS term. The |
| 560 /// delimited visitor has its own has its own context for building an IR | 564 /// delimited visitor has its own has its own context for building an IR |
| 561 /// expression, so the built expression is not plugged into the parent's | 565 /// expression, so the built expression is not plugged into the parent's |
| 562 /// context. It has its own compile-time environment mapping local | 566 /// context. It has its own compile-time environment mapping local |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 CallStructure callStructure, | 665 CallStructure callStructure, |
| 662 List<ir.Definition> arguments, | 666 List<ir.Definition> arguments, |
| 663 {SourceInformation sourceInformation}) { | 667 {SourceInformation sourceInformation}) { |
| 664 Selector selector = callStructure.callSelector; | 668 Selector selector = callStructure.callSelector; |
| 665 return _buildInvokeDynamic(target, selector, arguments, | 669 return _buildInvokeDynamic(target, selector, arguments, |
| 666 sourceInformation: sourceInformation); | 670 sourceInformation: sourceInformation); |
| 667 } | 671 } |
| 668 | 672 |
| 669 | 673 |
| 670 /// Create a [ir.Constant] from [constant] and add it to the CPS term. | 674 /// Create a [ir.Constant] from [constant] and add it to the CPS term. |
| 671 ir.Constant buildConstant(ConstantExpression constant) { | 675 // TODO(johnniwinther): Remove [value] when [ConstantValue] can be computed |
| 676 // directly from [constant]. |
| 677 ir.Constant buildConstant(ConstantExpression constant, ConstantValue value) { |
| 672 assert(isOpen); | 678 assert(isOpen); |
| 673 return addPrimitive(new ir.Constant(constant)); | 679 return addPrimitive(new ir.Constant(constant, value)); |
| 674 } | 680 } |
| 675 | 681 |
| 676 /// Create an integer constant and add it to the CPS term. | 682 /// Create an integer constant and add it to the CPS term. |
| 677 ir.Constant buildIntegerConstant(int value) { | 683 ir.Constant buildIntegerConstant(int value) { |
| 678 return buildConstant(new IntConstantExpression( | 684 return buildConstant( |
| 679 value, | 685 new IntConstantExpression(value), |
| 680 state.constantSystem.createInt(value))); | 686 state.constantSystem.createInt(value)); |
| 681 } | 687 } |
| 682 | 688 |
| 683 /// Create a double constant and add it to the CPS term. | 689 /// Create a double constant and add it to the CPS term. |
| 684 ir.Constant buildDoubleConstant(double value) { | 690 ir.Constant buildDoubleConstant(double value) { |
| 685 return buildConstant(new DoubleConstantExpression( | 691 return buildConstant( |
| 686 value, | 692 new DoubleConstantExpression(value), |
| 687 state.constantSystem.createDouble(value))); | 693 state.constantSystem.createDouble(value)); |
| 688 } | 694 } |
| 689 | 695 |
| 690 /// Create a Boolean constant and add it to the CPS term. | 696 /// Create a Boolean constant and add it to the CPS term. |
| 691 ir.Constant buildBooleanConstant(bool value) { | 697 ir.Constant buildBooleanConstant(bool value) { |
| 692 return buildConstant(new BoolConstantExpression( | 698 return buildConstant( |
| 693 value, | 699 new BoolConstantExpression(value), |
| 694 state.constantSystem.createBool(value))); | 700 state.constantSystem.createBool(value)); |
| 695 } | 701 } |
| 696 | 702 |
| 697 /// Create a null constant and add it to the CPS term. | 703 /// Create a null constant and add it to the CPS term. |
| 698 ir.Constant buildNullConstant() { | 704 ir.Constant buildNullConstant() { |
| 699 return buildConstant(new NullConstantExpression( | 705 return buildConstant( |
| 700 state.constantSystem.createNull())); | 706 new NullConstantExpression(), |
| 707 state.constantSystem.createNull()); |
| 701 } | 708 } |
| 702 | 709 |
| 703 /// Create a string constant and add it to the CPS term. | 710 /// Create a string constant and add it to the CPS term. |
| 704 ir.Constant buildStringConstant(String value) { | 711 ir.Constant buildStringConstant(String value) { |
| 705 return buildConstant(new StringConstantExpression( | 712 return buildConstant( |
| 706 value, | 713 new StringConstantExpression(value), |
| 707 state.constantSystem.createString(new ast.DartString.literal(value)))); | 714 state.constantSystem.createString(new ast.DartString.literal(value))); |
| 708 } | 715 } |
| 709 | 716 |
| 710 /// Create a string constant and add it to the CPS term. | 717 /// Create a string constant and add it to the CPS term. |
| 711 ir.Constant buildDartStringConstant(ast.DartString value) { | 718 ir.Constant buildDartStringConstant(ast.DartString value) { |
| 712 return buildConstant(new StringConstantExpression( | 719 return buildConstant( |
| 713 value.slowToString(), | 720 new StringConstantExpression(value.slowToString()), |
| 714 state.constantSystem.createString(value))); | 721 state.constantSystem.createString(value)); |
| 715 } | 722 } |
| 716 | 723 |
| 717 /// Creates a non-constant list literal of the provided [type] and with the | 724 /// Creates a non-constant list literal of the provided [type] and with the |
| 718 /// provided [values]. | 725 /// provided [values]. |
| 719 ir.Primitive buildListLiteral(InterfaceType type, | 726 ir.Primitive buildListLiteral(InterfaceType type, |
| 720 Iterable<ir.Primitive> values) { | 727 Iterable<ir.Primitive> values) { |
| 721 assert(isOpen); | 728 assert(isOpen); |
| 722 return addPrimitive(new ir.LiteralList(type, values.toList())); | 729 return addPrimitive(new ir.LiteralList(type, values.toList())); |
| 723 } | 730 } |
| 724 | 731 |
| (...skipping 1186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1911 // ! e is translated as e ? false : true | 1918 // ! e is translated as e ? false : true |
| 1912 | 1919 |
| 1913 // Add a continuation parameter for the result of the expression. | 1920 // Add a continuation parameter for the result of the expression. |
| 1914 ir.Parameter resultParameter = new ir.Parameter(null); | 1921 ir.Parameter resultParameter = new ir.Parameter(null); |
| 1915 | 1922 |
| 1916 ir.Continuation joinContinuation = new ir.Continuation([resultParameter]); | 1923 ir.Continuation joinContinuation = new ir.Continuation([resultParameter]); |
| 1917 ir.Continuation thenContinuation = new ir.Continuation([]); | 1924 ir.Continuation thenContinuation = new ir.Continuation([]); |
| 1918 ir.Continuation elseContinuation = new ir.Continuation([]); | 1925 ir.Continuation elseContinuation = new ir.Continuation([]); |
| 1919 | 1926 |
| 1920 ir.Constant makeBoolConstant(bool value) { | 1927 ir.Constant makeBoolConstant(bool value) { |
| 1921 return new ir.Constant(new BoolConstantExpression( | 1928 return new ir.Constant( |
| 1922 value, | 1929 new BoolConstantExpression(value), |
| 1923 state.constantSystem.createBool(value))); | 1930 state.constantSystem.createBool(value)); |
| 1924 } | 1931 } |
| 1925 | 1932 |
| 1926 ir.Constant trueConstant = makeBoolConstant(true); | 1933 ir.Constant trueConstant = makeBoolConstant(true); |
| 1927 ir.Constant falseConstant = makeBoolConstant(false); | 1934 ir.Constant falseConstant = makeBoolConstant(false); |
| 1928 | 1935 |
| 1929 thenContinuation.body = new ir.LetPrim(falseConstant) | 1936 thenContinuation.body = new ir.LetPrim(falseConstant) |
| 1930 ..plug(new ir.InvokeContinuation(joinContinuation, [falseConstant])); | 1937 ..plug(new ir.InvokeContinuation(joinContinuation, [falseConstant])); |
| 1931 elseContinuation.body = new ir.LetPrim(trueConstant) | 1938 elseContinuation.body = new ir.LetPrim(trueConstant) |
| 1932 ..plug(new ir.InvokeContinuation(joinContinuation, [trueConstant])); | 1939 ..plug(new ir.InvokeContinuation(joinContinuation, [trueConstant])); |
| 1933 | 1940 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2048 /// | 2055 /// |
| 2049 /// Inner functions are represented by a [ClosureClassElement], and captured | 2056 /// Inner functions are represented by a [ClosureClassElement], and captured |
| 2050 /// variables are boxed as necessary using [CreateBox], [GetField], [SetField]. | 2057 /// variables are boxed as necessary using [CreateBox], [GetField], [SetField]. |
| 2051 class JsIrBuilder extends IrBuilder { | 2058 class JsIrBuilder extends IrBuilder { |
| 2052 final JsIrBuilderSharedState jsState; | 2059 final JsIrBuilderSharedState jsState; |
| 2053 final GlobalProgramInformation program; | 2060 final GlobalProgramInformation program; |
| 2054 | 2061 |
| 2055 IrBuilder _makeInstance() => new JsIrBuilder._blank(program, jsState); | 2062 IrBuilder _makeInstance() => new JsIrBuilder._blank(program, jsState); |
| 2056 JsIrBuilder._blank(this.program, this.jsState); | 2063 JsIrBuilder._blank(this.program, this.jsState); |
| 2057 | 2064 |
| 2058 JsIrBuilder(this.program, ConstantSystem constantSystem, | 2065 JsIrBuilder(this.program, BackendConstantEnvironment constants, |
| 2059 ExecutableElement currentElement) | 2066 ExecutableElement currentElement) |
| 2060 : jsState = new JsIrBuilderSharedState() { | 2067 : jsState = new JsIrBuilderSharedState() { |
| 2061 _init(constantSystem, currentElement); | 2068 _init(constants, currentElement); |
| 2062 } | 2069 } |
| 2063 | 2070 |
| 2064 void enterInitializers() { | 2071 void enterInitializers() { |
| 2065 assert(jsState.inInitializers == false); | 2072 assert(jsState.inInitializers == false); |
| 2066 jsState.inInitializers = true; | 2073 jsState.inInitializers = true; |
| 2067 } | 2074 } |
| 2068 | 2075 |
| 2069 void leaveInitializers() { | 2076 void leaveInitializers() { |
| 2070 assert(jsState.inInitializers == true); | 2077 assert(jsState.inInitializers == true); |
| 2071 jsState.inInitializers = false; | 2078 jsState.inInitializers = false; |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2511 final DartType type; | 2518 final DartType type; |
| 2512 final LocalVariableElement exceptionVariable; | 2519 final LocalVariableElement exceptionVariable; |
| 2513 final LocalVariableElement stackTraceVariable; | 2520 final LocalVariableElement stackTraceVariable; |
| 2514 final SubbuildFunction buildCatchBlock; | 2521 final SubbuildFunction buildCatchBlock; |
| 2515 | 2522 |
| 2516 CatchClauseInfo({this.type, | 2523 CatchClauseInfo({this.type, |
| 2517 this.exceptionVariable, | 2524 this.exceptionVariable, |
| 2518 this.stackTraceVariable, | 2525 this.stackTraceVariable, |
| 2519 this.buildCatchBlock}); | 2526 this.buildCatchBlock}); |
| 2520 } | 2527 } |
| OLD | NEW |