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

Side by Side Diff: pkg/compiler/lib/src/cps_ir/cps_ir_builder.dart

Issue 862703002: Implement constructor bodies and initializers in CPS->JS backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebase again Created 5 years, 11 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 | Annotate | Revision Log
OLDNEW
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 '../constants/expressions.dart'; 7 import '../constants/expressions.dart';
8 import '../constants/values.dart' show PrimitiveConstantValue; 8 import '../constants/values.dart' show PrimitiveConstantValue;
9 import '../dart_types.dart'; 9 import '../dart_types.dart';
10 import '../dart2jslib.dart'; 10 import '../dart2jslib.dart';
11 import '../elements/elements.dart'; 11 import '../elements/elements.dart';
12 import '../io/source_file.dart'; 12 import '../io/source_file.dart';
13 import '../tree/tree.dart' as ast; 13 import '../tree/tree.dart' as ast;
14 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator; 14 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator;
15 import '../universe/universe.dart' show SelectorKind; 15 import '../universe/universe.dart' show SelectorKind;
16 import 'cps_ir_nodes.dart' as ir; 16 import 'cps_ir_nodes.dart' as ir;
17 import '../elements/modelx.dart' show SynthesizedConstructorElementX; 17 import '../elements/modelx.dart' show SynthesizedConstructorElementX,
18 import '../closure.dart'; 18 ConstructorBodyElementX, FunctionSignatureX;
19 import '../closure.dart' hide ClosureScope;
19 import '../closure.dart' as closurelib; 20 import '../closure.dart' as closurelib;
20 import '../js_backend/js_backend.dart' show JavaScriptBackend; 21 import '../js_backend/js_backend.dart' show JavaScriptBackend;
21 22
22 part 'cps_ir_builder_visitor.dart'; 23 part 'cps_ir_builder_visitor.dart';
23 24
24 /// A mapping from variable elements to their compile-time values. 25 /// A mapping from variable elements to their compile-time values.
25 /// 26 ///
26 /// Map elements denoted by parameters and local variables to the 27 /// Map elements denoted by parameters and local variables to the
27 /// [ir.Primitive] that is their value. Parameters and locals are 28 /// [ir.Primitive] that is their value. Parameters and locals are
28 /// assigned indexes which can be used to refer to them. 29 /// assigned indexes which can be used to refer to them.
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 203
203 /// A factory for building the cps IR. 204 /// A factory for building the cps IR.
204 /// 205 ///
205 /// [DartIrBuilder] and [JsIrBuilder] implement nested functions and captured 206 /// [DartIrBuilder] and [JsIrBuilder] implement nested functions and captured
206 /// variables in different ways. 207 /// variables in different ways.
207 abstract class IrBuilder { 208 abstract class IrBuilder {
208 IrBuilder _makeInstance(); 209 IrBuilder _makeInstance();
209 210
210 void declareLocalVariable(LocalVariableElement element, 211 void declareLocalVariable(LocalVariableElement element,
211 {ir.Primitive initialValue}); 212 {ir.Primitive initialValue});
212 void declareLocalFunction(LocalFunctionElement element, Object function);
213 ir.Primitive buildFunctionExpression(Object function);
214 ir.Primitive buildLocalGet(LocalElement element); 213 ir.Primitive buildLocalGet(LocalElement element);
215 ir.Primitive buildLocalSet(LocalElement element, ir.Primitive value); 214 ir.Primitive buildLocalSet(LocalElement element, ir.Primitive value);
216 215
217 /// Called when entering a nested function with free variables. 216 /// Called when entering a nested function with free variables.
218 /// 217 ///
219 /// The free variables must subsequently be accessible using [buildLocalGet] 218 /// The free variables must subsequently be accessible using [buildLocalGet]
220 /// and [buildLocalSet]. 219 /// and [buildLocalSet].
221 void _enterClosureEnvironment(ClosureEnvironment env); 220 void _enterClosureEnvironment(ClosureEnvironment env);
222 221
223 /// Called when entering a function body or loop body. 222 /// Called when entering a function body or loop body.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 ..environment = new Environment.empty(); 339 ..environment = new Environment.empty();
341 } 340 }
342 341
343 bool get isOpen => _root == null || _current != null; 342 bool get isOpen => _root == null || _current != null;
344 343
345 344
346 void buildFieldInitializerHeader({ClosureScope closureScope}) { 345 void buildFieldInitializerHeader({ClosureScope closureScope}) {
347 _enterScope(closureScope); 346 _enterScope(closureScope);
348 } 347 }
349 348
350 void buildFunctionHeader(Iterable<ParameterElement> parameters, 349 List<ir.Primitive> buildFunctionHeader(Iterable<ParameterElement> parameters,
351 {ClosureScope closureScope, 350 {ClosureScope closureScope,
352 ClosureEnvironment closureEnvironment}) { 351 ClosureEnvironment env}) {
353 _enterClosureEnvironment(closureEnvironment); 352 _enterClosureEnvironment(env);
354 _enterScope(closureScope); 353 _enterScope(closureScope);
355 parameters.forEach(_createFunctionParameter); 354 parameters.forEach(_createFunctionParameter);
355 return _parameters;
356 } 356 }
357 357
358 /// Creates a parameter for [local] and adds it to the current environment. 358 /// Creates a parameter for [local] and adds it to the current environment.
359 ir.Parameter createLocalParameter(Local local) { 359 ir.Parameter createLocalParameter(Local local) {
360 ir.Parameter parameter = new ir.Parameter(local); 360 ir.Parameter parameter = new ir.Parameter(local);
361 _parameters.add(parameter); 361 _parameters.add(parameter);
362 environment.extend(local, parameter); 362 environment.extend(local, parameter);
363 return parameter; 363 return parameter;
364 } 364 }
365 365
(...skipping 27 matching lines...) Expand all
393 } 393 }
394 394
395 ir.Primitive _buildInvokeStatic(Element element, 395 ir.Primitive _buildInvokeStatic(Element element,
396 Selector selector, 396 Selector selector,
397 List<ir.Primitive> arguments) { 397 List<ir.Primitive> arguments) {
398 assert(isOpen); 398 assert(isOpen);
399 return _continueWithExpression( 399 return _continueWithExpression(
400 (k) => new ir.InvokeStatic(element, selector, k, arguments)); 400 (k) => new ir.InvokeStatic(element, selector, k, arguments));
401 } 401 }
402 402
403 ir.Primitive _buildInvokeDirectly(Element target, 403 ir.Primitive _buildInvokeSuper(Element target,
404 Selector selector, 404 Selector selector,
405 List<ir.Primitive> arguments) { 405 List<ir.Primitive> arguments) {
406 assert(isOpen); 406 assert(isOpen);
407 return _continueWithExpression( 407 return _continueWithExpression(
408 (k) => new ir.InvokeMethodDirectly( 408 (k) => new ir.InvokeMethodDirectly(
409 buildThis(), target, selector, k, arguments)); 409 buildThis(), target, selector, k, arguments));
410 } 410 }
411 411
412 ir.Primitive _buildInvokeDynamic(ir.Primitive receiver, 412 ir.Primitive _buildInvokeDynamic(ir.Primitive receiver,
413 Selector selector, 413 Selector selector,
414 List<ir.Primitive> arguments) { 414 List<ir.Primitive> arguments) {
415 assert(isOpen); 415 assert(isOpen);
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
643 /// argument are defined by [selector] and [value], respectively. 643 /// argument are defined by [selector] and [value], respectively.
644 void buildSuperSet(Element target, Selector selector, ir.Primitive value) { 644 void buildSuperSet(Element target, Selector selector, ir.Primitive value) {
645 buildSuperInvocation(target, selector, [value]); 645 buildSuperInvocation(target, selector, [value]);
646 } 646 }
647 647
648 /// Create an index set invocation on the super class with the provided 648 /// Create an index set invocation on the super class with the provided
649 /// [index] and [value]. 649 /// [index] and [value].
650 ir.Primitive buildSuperIndexSet(Element target, 650 ir.Primitive buildSuperIndexSet(Element target,
651 ir.Primitive index, 651 ir.Primitive index,
652 ir.Primitive value) { 652 ir.Primitive value) {
653 _buildInvokeDirectly(target, new Selector.indexSet(), 653 _buildInvokeSuper(target, new Selector.indexSet(),
654 <ir.Primitive>[index, value]); 654 <ir.Primitive>[index, value]);
655 return value; 655 return value;
656 } 656 }
657 657
658 /// Create a dynamic invocation on [receiver] where the method name and 658 /// Create a dynamic invocation on [receiver] where the method name and
659 /// argument structure are defined by [selector] and the argument values are 659 /// argument structure are defined by [selector] and the argument values are
660 /// defined by [arguments]. 660 /// defined by [arguments].
661 ir.Primitive buildDynamicInvocation(ir.Primitive receiver, 661 ir.Primitive buildDynamicInvocation(ir.Primitive receiver,
662 Selector selector, 662 Selector selector,
663 List<ir.Primitive> arguments) { 663 List<ir.Primitive> arguments) {
(...skipping 866 matching lines...) Expand 10 before | Expand all | Expand 10 after
1530 /// Shared state between DartIrBuilders within the same method. 1530 /// Shared state between DartIrBuilders within the same method.
1531 class DartIrBuilderSharedState { 1531 class DartIrBuilderSharedState {
1532 /// Maps local variables to their corresponding [ClosureVariable] object. 1532 /// Maps local variables to their corresponding [ClosureVariable] object.
1533 final Map<Local, ir.ClosureVariable> local2closure = 1533 final Map<Local, ir.ClosureVariable> local2closure =
1534 <Local, ir.ClosureVariable>{}; 1534 <Local, ir.ClosureVariable>{};
1535 1535
1536 /// Maps functions to the list of closure variables declared in that function. 1536 /// Maps functions to the list of closure variables declared in that function.
1537 final Map<ExecutableElement, List<ir.ClosureVariable>> function2closures = 1537 final Map<ExecutableElement, List<ir.ClosureVariable>> function2closures =
1538 <ExecutableElement, List<ir.ClosureVariable>>{}; 1538 <ExecutableElement, List<ir.ClosureVariable>>{};
1539 1539
1540 final ClosureVariableInfo closureVariables; 1540 final DartCapturedVariableInfo closureVariables;
1541 1541
1542 /// Returns the closure variables declared in the given function. 1542 /// Returns the closure variables declared in the given function.
1543 List<ir.ClosureVariable> getClosureList(ExecutableElement element) { 1543 List<ir.ClosureVariable> getClosureList(ExecutableElement element) {
1544 return function2closures.putIfAbsent(element, () => <ir.ClosureVariable>[]); 1544 return function2closures.putIfAbsent(element, () => <ir.ClosureVariable>[]);
1545 } 1545 }
1546 1546
1547 /// Creates a closure variable for the given local. 1547 /// Creates a closure variable for the given local.
1548 void makeClosureVariable(Local local) { 1548 void makeClosureVariable(Local local) {
1549 ir.ClosureVariable variable = 1549 ir.ClosureVariable variable =
1550 new ir.ClosureVariable(local.executableContext, local); 1550 new ir.ClosureVariable(local.executableContext, local);
(...skipping 17 matching lines...) Expand all
1568 /// Captured variables are translated to ref cells (see [ClosureVariable]) 1568 /// Captured variables are translated to ref cells (see [ClosureVariable])
1569 /// using [GetClosureVariable] and [SetClosureVariable]. 1569 /// using [GetClosureVariable] and [SetClosureVariable].
1570 class DartIrBuilder extends IrBuilder { 1570 class DartIrBuilder extends IrBuilder {
1571 final DartIrBuilderSharedState dartState; 1571 final DartIrBuilderSharedState dartState;
1572 1572
1573 IrBuilder _makeInstance() => new DartIrBuilder._blank(dartState); 1573 IrBuilder _makeInstance() => new DartIrBuilder._blank(dartState);
1574 DartIrBuilder._blank(this.dartState); 1574 DartIrBuilder._blank(this.dartState);
1575 1575
1576 DartIrBuilder(ConstantSystem constantSystem, 1576 DartIrBuilder(ConstantSystem constantSystem,
1577 ExecutableElement currentElement, 1577 ExecutableElement currentElement,
1578 ClosureVariableInfo closureVariables) 1578 DartCapturedVariableInfo closureVariables)
1579 : dartState = new DartIrBuilderSharedState(closureVariables) { 1579 : dartState = new DartIrBuilderSharedState(closureVariables) {
1580 _init(constantSystem, currentElement); 1580 _init(constantSystem, currentElement);
1581 } 1581 }
1582 1582
1583 /// True if [local] should currently be accessed from a [ClosureVariable]. 1583 /// True if [local] should currently be accessed from a [ClosureVariable].
1584 bool isInClosureVariable(Local local) { 1584 bool isInClosureVariable(Local local) {
1585 return dartState.local2closure.containsKey(local) && 1585 return dartState.local2closure.containsKey(local) &&
1586 !dartState.registerizedClosureVariables.contains(local); 1586 !dartState.registerizedClosureVariables.contains(local);
1587 } 1587 }
1588 1588
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1727 1727
1728 ir.Primitive buildThis() { 1728 ir.Primitive buildThis() {
1729 ir.Primitive thisPrim = new ir.This(); 1729 ir.Primitive thisPrim = new ir.This();
1730 add(new ir.LetPrim(thisPrim)); 1730 add(new ir.LetPrim(thisPrim));
1731 return thisPrim; 1731 return thisPrim;
1732 } 1732 }
1733 1733
1734 ir.Primitive buildSuperInvocation(Element target, 1734 ir.Primitive buildSuperInvocation(Element target,
1735 Selector selector, 1735 Selector selector,
1736 List<ir.Primitive> arguments) { 1736 List<ir.Primitive> arguments) {
1737 return _buildInvokeDirectly(target, selector, arguments); 1737 return _buildInvokeSuper(target, selector, arguments);
1738 } 1738 }
1739 1739
1740 } 1740 }
1741 1741
1742 /// State shared between JsIrBuilders within the same function. 1742 /// State shared between JsIrBuilders within the same function.
1743 /// 1743 ///
1744 /// Note that this is not shared between builders of nested functions. 1744 /// Note that this is not shared between builders of nested functions.
1745 class JsIrBuilderSharedState { 1745 class JsIrBuilderSharedState {
1746 /// Maps boxed locals to their location. These locals are not part of 1746 /// Maps boxed locals to their location. These locals are not part of
1747 /// the environment. 1747 /// the environment.
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1849 ClosureClassElement classElement) { 1849 ClosureClassElement classElement) {
1850 ir.Primitive closure = buildFunctionExpression(classElement); 1850 ir.Primitive closure = buildFunctionExpression(classElement);
1851 declareLocalVariable(functionElement, initialValue: closure); 1851 declareLocalVariable(functionElement, initialValue: closure);
1852 } 1852 }
1853 1853
1854 ir.Primitive buildFunctionExpression(ClosureClassElement classElement) { 1854 ir.Primitive buildFunctionExpression(ClosureClassElement classElement) {
1855 List<ir.Primitive> arguments = <ir.Primitive>[]; 1855 List<ir.Primitive> arguments = <ir.Primitive>[];
1856 for (ClosureFieldElement field in classElement.closureFields) { 1856 for (ClosureFieldElement field in classElement.closureFields) {
1857 arguments.add(environment.lookup(field.local)); 1857 arguments.add(environment.lookup(field.local));
1858 } 1858 }
1859 ir.Primitive closure = new ir.CreateClosureClass(classElement, arguments); 1859 ir.Primitive closure = new ir.CreateInstance(classElement, arguments);
1860 add(new ir.LetPrim(closure)); 1860 add(new ir.LetPrim(closure));
1861 return closure; 1861 return closure;
1862 } 1862 }
1863 1863
1864 /// Create a read access of [local]. 1864 /// Create a read access of [local].
1865 ir.Primitive buildLocalGet(LocalElement local) { 1865 ir.Primitive buildLocalGet(LocalElement local) {
1866 assert(isOpen); 1866 assert(isOpen);
1867 ClosureLocation location = jsState.boxedVariables[local]; 1867 ClosureLocation location = jsState.boxedVariables[local];
1868 if (location != null) { 1868 if (location != null) {
1869 ir.Primitive result = new ir.GetField(environment.lookup(location.box), 1869 ir.Primitive result = new ir.GetField(environment.lookup(location.box),
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 if (selector.isGetter) { 1952 if (selector.isGetter) {
1953 ir.Primitive get = new ir.GetField(buildThis(), target); 1953 ir.Primitive get = new ir.GetField(buildThis(), target);
1954 add(new ir.LetPrim(get)); 1954 add(new ir.LetPrim(get));
1955 return get; 1955 return get;
1956 } else { 1956 } else {
1957 assert(selector.isSetter); 1957 assert(selector.isSetter);
1958 add(new ir.SetField(buildThis(), target, arguments.single)); 1958 add(new ir.SetField(buildThis(), target, arguments.single));
1959 return arguments.single; 1959 return arguments.single;
1960 } 1960 }
1961 } else { 1961 } else {
1962 return _buildInvokeDirectly(target, selector, arguments); 1962 return _buildInvokeSuper(target, selector, arguments);
1963 }
1964 }
1965
1966 ir.Primitive buildInvokeDirectly(FunctionElement target,
1967 ir.Primitive receiver,
1968 List<ir.Primitive> arguments) {
1969 assert(isOpen);
1970 Selector selector =
1971 new Selector.call(target.name, target.library, arguments.length);
1972 return _continueWithExpression(
1973 (k) => new ir.InvokeMethodDirectly(
1974 receiver, target, selector, k, arguments));
1975 }
1976
1977 /// Loads parameters to a constructor body into the environment.
1978 ///
1979 /// The header for a constructor body differs from other functions in that
1980 /// some parameters are already boxed, and the box is passed as an argument
1981 /// instead of being created in the header.
1982 void buildConstructorBodyHeader(Iterable<Local> parameters,
1983 ClosureScope closureScope) {
1984 for (Local param in parameters) {
1985 ir.Parameter parameter = createLocalParameter(param);
1986 state.functionParameters.add(parameter);
1987 }
1988 if (closureScope != null) {
1989 jsState.boxedVariables.addAll(closureScope.capturedVariables);
1963 } 1990 }
1964 } 1991 }
1965 } 1992 }
1966 1993
1967 1994
1968 /// Location of a variable relative to a given closure. 1995 /// Location of a variable relative to a given closure.
1969 class ClosureLocation { 1996 class ClosureLocation {
1970 /// If not `null`, this location is [box].[field]. 1997 /// If not `null`, this location is [box].[field].
1971 /// The location of [box] can be obtained separately from an 1998 /// The location of [box] can be obtained separately from an
1972 /// enclosing [ClosureEnvironment] or [ClosureScope]. 1999 /// enclosing [ClosureEnvironment] or [ClosureScope].
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2010 /// If non-null, [thisLocal] has an entry in [freeVariables] describing where 2037 /// If non-null, [thisLocal] has an entry in [freeVariables] describing where
2011 /// to find the captured value of `this`. 2038 /// to find the captured value of `this`.
2012 final ThisLocal thisLocal; 2039 final ThisLocal thisLocal;
2013 2040
2014 /// Maps [LocalElement]s, [BoxLocal]s and [ThisLocal] to their location. 2041 /// Maps [LocalElement]s, [BoxLocal]s and [ThisLocal] to their location.
2015 final Map<Local, ClosureLocation> freeVariables; 2042 final Map<Local, ClosureLocation> freeVariables;
2016 2043
2017 ClosureEnvironment(this.selfReference, this.thisLocal, this.freeVariables); 2044 ClosureEnvironment(this.selfReference, this.thisLocal, this.freeVariables);
2018 } 2045 }
2019 2046
2020 /// Information about which variables are captured in a closure. 2047 /// Information about which variables are captured by a nested function.
2021 /// 2048 ///
2022 /// This is used by the [DartIrBuilder] instead of [ClosureScope] and 2049 /// This is used by the [DartIrBuilder] instead of [ClosureScope] and
2023 /// [ClosureEnvironment]. 2050 /// [ClosureEnvironment].
2024 abstract class ClosureVariableInfo { 2051 abstract class DartCapturedVariableInfo {
2025 Iterable<Local> get capturedVariables; 2052 Iterable<Local> get capturedVariables;
2026 } 2053 }
OLDNEW
« no previous file with comments | « pkg/analyzer2dart/lib/src/cps_generator.dart ('k') | pkg/compiler/lib/src/cps_ir/cps_ir_builder_visitor.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698