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

Side by Side Diff: lib/compiler/implementation/ssa/builder.dart

Issue 11273121: Support closures inside initializers. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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) 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 ssa; 5 part of ssa;
6 6
7 class Interceptors { 7 class Interceptors {
8 Compiler compiler; 8 Compiler compiler;
9 Interceptors(Compiler this.compiler); 9 Interceptors(Compiler this.compiler);
10 10
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 const LiteralDartString('Object'), 315 const LiteralDartString('Object'),
316 <HInstruction>[]); 316 <HInstruction>[]);
317 builder.add(box); 317 builder.add(box);
318 return box; 318 return box;
319 } 319 }
320 320
321 /** 321 /**
322 * If the scope (function or loop) [node] has captured variables then this 322 * If the scope (function or loop) [node] has captured variables then this
323 * method creates a box and sets up the redirections. 323 * method creates a box and sets up the redirections.
324 */ 324 */
325 void enterScope(Node node) { 325 void enterScope(Node node, Element element) {
326 // See if any variable in the top-scope of the function is captured. If yes 326 // See if any variable in the top-scope of the function is captured. If yes
327 // we need to create a box-object. 327 // we need to create a box-object.
328 ClosureScope scopeData = closureData.capturingScopes[node]; 328 ClosureScope scopeData = closureData.capturingScopes[node];
329 if (scopeData != null) { 329 if (scopeData != null) {
330 // The scope has captured variables. Create a box. 330 HInstruction box;
331 // TODO(floitsch): Clean up this hack. Should we create a box-object by 331 // The scope has captured variables.
332 // just creating an empty object literal? 332 if (element != null && element.isGenerativeConstructorBody()) {
333 HInstruction box = createBox(); 333 // The box is passed as a parameter to a generative
334 // constructor body.
335 box = new HParameterValue(scopeData.boxElement);
336 builder.add(box);
337 } else {
338 // TODO(floitsch): Clean up this hack. Should we create a box-object by
339 // just creating an empty object literal?
340 box = createBox();
341 }
334 // Add the box to the known locals. 342 // Add the box to the known locals.
335 directLocals[scopeData.boxElement] = box; 343 directLocals[scopeData.boxElement] = box;
336 // Make sure that accesses to the boxed locals go into the box. We also 344 // Make sure that accesses to the boxed locals go into the box. We also
337 // need to make sure that parameters are copied into the box if necessary. 345 // need to make sure that parameters are copied into the box if necessary.
338 scopeData.capturedVariableMapping.forEach((Element from, Element to) { 346 scopeData.capturedVariableMapping.forEach((Element from, Element to) {
339 // The [from] can only be a parameter for function-scopes and not 347 // The [from] can only be a parameter for function-scopes and not
340 // loop scopes. 348 // loop scopes.
341 if (from.isParameter()) { 349 if (from.isParameter()) {
342 // Store the captured parameter in the box. Get the current value 350 // Store the captured parameter in the box. Get the current value
343 // before we put the redirection in place. 351 // before we put the redirection in place.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 HInstruction parameter = new HParameterValue(parameterElement); 399 HInstruction parameter = new HParameterValue(parameterElement);
392 builder.add(parameter); 400 builder.add(parameter);
393 builder.parameters[parameterElement] = parameter; 401 builder.parameters[parameterElement] = parameter;
394 directLocals[parameterElement] = parameter; 402 directLocals[parameterElement] = parameter;
395 parameter.guaranteedType = 403 parameter.guaranteedType =
396 builder.mapInferredType( 404 builder.mapInferredType(
397 typesTask.getGuaranteedTypeOfElement(parameterElement)); 405 typesTask.getGuaranteedTypeOfElement(parameterElement));
398 }); 406 });
399 } 407 }
400 408
401 enterScope(node); 409 enterScope(node, element);
402 410
403 // If the freeVariableMapping is not empty, then this function was a 411 // If the freeVariableMapping is not empty, then this function was a
404 // nested closure that captures variables. Redirect the captured 412 // nested closure that captures variables. Redirect the captured
405 // variables to fields in the closure. 413 // variables to fields in the closure.
406 closureData.freeVariableMapping.forEach((Element from, Element to) { 414 closureData.freeVariableMapping.forEach((Element from, Element to) {
407 redirectElement(from, to); 415 redirectElement(from, to);
408 }); 416 });
409 if (closureData.isClosure()) { 417 if (closureData.isClosure()) {
410 // Inside closure redirect references to itself to [:this:]. 418 // Inside closure redirect references to itself to [:this:].
411 HInstruction thisInstruction = new HThis(); 419 HInstruction thisInstruction = new HThis();
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 * loop-exit: 611 * loop-exit:
604 */ 612 */
605 void startLoop(Node node) { 613 void startLoop(Node node) {
606 ClosureScope scopeData = closureData.capturingScopes[node]; 614 ClosureScope scopeData = closureData.capturingScopes[node];
607 if (scopeData == null) return; 615 if (scopeData == null) return;
608 if (scopeData.hasBoxedLoopVariables()) { 616 if (scopeData.hasBoxedLoopVariables()) {
609 // If there are boxed loop variables then we set up the box and 617 // If there are boxed loop variables then we set up the box and
610 // redirections already now. This way the initializer can write its 618 // redirections already now. This way the initializer can write its
611 // values into the box. 619 // values into the box.
612 // For other loops the box will be created when entering the body. 620 // For other loops the box will be created when entering the body.
613 enterScope(node); 621 enterScope(node, null);
614 } 622 }
615 } 623 }
616 624
617 void beginLoopHeader(Node node, HBasicBlock loopEntry) { 625 void beginLoopHeader(Node node, HBasicBlock loopEntry) {
618 // Create a copy because we modify the map while iterating over 626 // Create a copy because we modify the map while iterating over
619 // it. 627 // it.
620 Map<Element, HInstruction> saved = 628 Map<Element, HInstruction> saved =
621 new Map<Element, HInstruction>.from(directLocals); 629 new Map<Element, HInstruction>.from(directLocals);
622 630
623 // Create phis for all elements in the definitions environment. 631 // Create phis for all elements in the definitions environment.
(...skipping 10 matching lines...) Expand all
634 } 642 }
635 }); 643 });
636 } 644 }
637 645
638 void enterLoopBody(Node node) { 646 void enterLoopBody(Node node) {
639 ClosureScope scopeData = closureData.capturingScopes[node]; 647 ClosureScope scopeData = closureData.capturingScopes[node];
640 if (scopeData == null) return; 648 if (scopeData == null) return;
641 // If there are no declared boxed loop variables then we did not create the 649 // If there are no declared boxed loop variables then we did not create the
642 // box before the initializer and we have to create the box now. 650 // box before the initializer and we have to create the box now.
643 if (!scopeData.hasBoxedLoopVariables()) { 651 if (!scopeData.hasBoxedLoopVariables()) {
644 enterScope(node); 652 enterScope(node, null);
645 } 653 }
646 } 654 }
647 655
648 void enterLoopUpdates(Loop node) { 656 void enterLoopUpdates(Loop node) {
649 // If there are declared boxed loop variables then the updates might have 657 // If there are declared boxed loop variables then the updates might have
650 // access to the box and we must switch to a new box before executing the 658 // access to the box and we must switch to a new box before executing the
651 // updates. 659 // updates.
652 // In all other cases a new box will be created when entering the body of 660 // In all other cases a new box will be created when entering the body of
653 // the next iteration. 661 // the next iteration.
654 ClosureScope scopeData = closureData.capturingScopes[node]; 662 ClosureScope scopeData = closureData.capturingScopes[node];
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
1165 fieldValues[fieldParameterElement.fieldElement] = argument; 1173 fieldValues[fieldParameterElement.fieldElement] = argument;
1166 } 1174 }
1167 }); 1175 });
1168 1176
1169 // Build the initializers in the context of the new constructor. 1177 // Build the initializers in the context of the new constructor.
1170 TreeElements oldElements = elements; 1178 TreeElements oldElements = elements;
1171 elements = 1179 elements =
1172 compiler.enqueuer.resolution.getCachedElements(constructor); 1180 compiler.enqueuer.resolution.getCachedElements(constructor);
1173 1181
1174 ClosureClassMap oldClosureData = localsHandler.closureData; 1182 ClosureClassMap oldClosureData = localsHandler.closureData;
1183 Node node = constructor.parseNode(compiler);
1175 localsHandler.closureData = 1184 localsHandler.closureData =
1176 compiler.closureToClassMapper.computeClosureToClassMapping( 1185 compiler.closureToClassMapper.computeClosureToClassMapping(
1177 constructor, constructor.parseNode(compiler), elements); 1186 constructor, node, elements);
1178 1187
1179 params.orderedForEachParameter((Element parameterElement) { 1188 params.orderedForEachParameter((Element parameterElement) {
1180 if (elements.isParameterChecked(parameterElement)) { 1189 if (elements.isParameterChecked(parameterElement)) {
1181 addParameterCheckInstruction(parameterElement); 1190 addParameterCheckInstruction(parameterElement);
1182 } 1191 }
1183 }); 1192 });
1184 1193 localsHandler.enterScope(node, constructor);
1185 buildInitializers(constructor, constructors, fieldValues); 1194 buildInitializers(constructor, constructors, fieldValues);
1186 localsHandler.closureData = oldClosureData; 1195 localsHandler.closureData = oldClosureData;
1187 elements = oldElements; 1196 elements = oldElements;
1188 }); 1197 });
1189 } 1198 }
1190 1199
1191 /** 1200 /**
1192 * Run through the initializers and inline all field initializers. Recursively 1201 * Run through the initializers and inline all field initializers. Recursively
1193 * inlines super initializers. 1202 * inlines super initializers.
1194 * 1203 *
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
1364 for (int index = constructors.length - 1; index >= 0; index--) { 1373 for (int index = constructors.length - 1; index >= 0; index--) {
1365 FunctionElement constructor = constructors[index]; 1374 FunctionElement constructor = constructors[index];
1366 assert(invariant(functionElement, constructor.isImplementation)); 1375 assert(invariant(functionElement, constructor.isImplementation));
1367 ConstructorBodyElement body = getConstructorBody(constructor); 1376 ConstructorBodyElement body = getConstructorBody(constructor);
1368 if (body == null) continue; 1377 if (body == null) continue;
1369 List bodyCallInputs = <HInstruction>[]; 1378 List bodyCallInputs = <HInstruction>[];
1370 bodyCallInputs.add(newObject); 1379 bodyCallInputs.add(newObject);
1371 FunctionSignature functionSignature = body.computeSignature(compiler); 1380 FunctionSignature functionSignature = body.computeSignature(compiler);
1372 int arity = functionSignature.parameterCount; 1381 int arity = functionSignature.parameterCount;
1373 functionSignature.orderedForEachParameter((parameter) { 1382 functionSignature.orderedForEachParameter((parameter) {
1383 // TODO(ngeoffray): No need to pass the parameters that are
1384 // captured and stored in a box. Because this information is
1385 // not trivial to get in codegen.dart, we just pass the
1386 // parameters anyway. We need to update both codegen.dart and
1387 // builder.dart on how parameters are being passed.
1374 bodyCallInputs.add(localsHandler.readLocal(parameter)); 1388 bodyCallInputs.add(localsHandler.readLocal(parameter));
1375 }); 1389 });
1376 1390
1377 // If parameters are checked, we pass the already computed 1391 // If parameters are checked, we pass the already computed
1378 // boolean to the constructor body. 1392 // boolean to the constructor body.
1379 TreeElements elements = 1393 TreeElements elements =
1380 compiler.enqueuer.resolution.getCachedElements(constructor); 1394 compiler.enqueuer.resolution.getCachedElements(constructor);
1381 Node node = constructor.parseNode(compiler); 1395 Node node = constructor.parseNode(compiler);
1382 ClosureClassMap parameterClosureData = 1396 ClosureClassMap parameterClosureData =
1383 compiler.closureToClassMapper.getMappingForNestedFunction(node); 1397 compiler.closureToClassMapper.getMappingForNestedFunction(node);
1384 functionSignature.orderedForEachParameter((parameter) { 1398 functionSignature.orderedForEachParameter((parameter) {
1385 if (elements.isParameterChecked(parameter)) { 1399 if (elements.isParameterChecked(parameter)) {
1386 Element fieldCheck = 1400 Element fieldCheck =
1387 parameterClosureData.parametersWithSentinel[parameter]; 1401 parameterClosureData.parametersWithSentinel[parameter];
1388 bodyCallInputs.add(localsHandler.readLocal(fieldCheck)); 1402 bodyCallInputs.add(localsHandler.readLocal(fieldCheck));
1389 } 1403 }
1390 }); 1404 });
1391 1405
1406 // If there are locals that escape (ie used in closures), we
1407 // pass the box to the constructor.
1408 ClosureScope scopeData = parameterClosureData.capturingScopes[node];
1409 if (scopeData != null) {
1410 bodyCallInputs.add(localsHandler.readLocal(scopeData.boxElement));
1411 }
1412
1392 // TODO(ahe): The constructor name is statically resolved. See 1413 // TODO(ahe): The constructor name is statically resolved. See
1393 // SsaCodeGenerator.visitInvokeDynamicMethod. Is there a cleaner 1414 // SsaCodeGenerator.visitInvokeDynamicMethod. Is there a cleaner
1394 // way to do this? 1415 // way to do this?
1395 SourceString name = 1416 SourceString name =
1396 new SourceString(backend.namer.getName(body.declaration)); 1417 new SourceString(backend.namer.getName(body.declaration));
1397 // TODO(kasperl): This seems fishy. We shouldn't be inventing all 1418 // TODO(kasperl): This seems fishy. We shouldn't be inventing all
1398 // these selectors. Maybe the resolver can do more of the work 1419 // these selectors. Maybe the resolver can do more of the work
1399 // for us here? 1420 // for us here?
1400 LibraryElement library = body.getLibrary(); 1421 LibraryElement library = body.getLibrary();
1401 Selector selector = new Selector.call(name, library, arity); 1422 Selector selector = new Selector.call(name, library, arity);
(...skipping 3184 matching lines...) Expand 10 before | Expand all | Expand 10 after
4586 new HSubGraphBlockInformation(elseBranch.graph)); 4607 new HSubGraphBlockInformation(elseBranch.graph));
4587 4608
4588 HBasicBlock conditionStartBlock = conditionBranch.block; 4609 HBasicBlock conditionStartBlock = conditionBranch.block;
4589 conditionStartBlock.setBlockFlow(info, joinBlock); 4610 conditionStartBlock.setBlockFlow(info, joinBlock);
4590 SubGraph conditionGraph = conditionBranch.graph; 4611 SubGraph conditionGraph = conditionBranch.graph;
4591 HIf branch = conditionGraph.end.last; 4612 HIf branch = conditionGraph.end.last;
4592 assert(branch is HIf); 4613 assert(branch is HIf);
4593 branch.blockInformation = conditionStartBlock.blockFlow; 4614 branch.blockInformation = conditionStartBlock.blockFlow;
4594 } 4615 }
4595 } 4616 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698