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

Unified Diff: sdk/lib/_internal/compiler/implementation/ssa/codegen.dart

Issue 11364134: Merge libv1. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error 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 side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/compiler/implementation/ssa/codegen.dart
diff --git a/sdk/lib/_internal/compiler/implementation/ssa/codegen.dart b/sdk/lib/_internal/compiler/implementation/ssa/codegen.dart
index 17c6ba599ec73cc61d22cab7cc390a4f76df11a9..040337d13748f7ffdb63165b01ca7d5117872bab 100644
--- a/sdk/lib/_internal/compiler/implementation/ssa/codegen.dart
+++ b/sdk/lib/_internal/compiler/implementation/ssa/codegen.dart
@@ -54,12 +54,11 @@ class SsaCodeGeneratorTask extends CompilerTask {
CodeBuffer generateLazyInitializer(work, graph) {
return measure(() {
compiler.tracer.traceGraph("codegen", graph);
- List<js.Parameter> parameters = <js.Parameter>[];
- SsaOptimizedCodeGenerator codegen = new SsaOptimizedCodeGenerator(
- backend, work, parameters, new Map<Element, String>());
+ SsaOptimizedCodeGenerator codegen =
+ new SsaOptimizedCodeGenerator(backend, work);
codegen.visitGraph(graph);
js.Block body = codegen.body;
- js.Fun fun = new js.Fun(parameters, body);
+ js.Fun fun = new js.Fun(codegen.parameters, body);
return prettyPrint(fun);
});
}
@@ -77,21 +76,8 @@ class SsaCodeGeneratorTask extends CompilerTask {
}
});
compiler.tracer.traceGraph("codegen", graph);
- Map<Element, String> parameterNames = getParameterNames(work);
- // Use [work.element] to ensure that the parameter element come from
- // the declaration.
- FunctionElement function = work.element;
- function.computeSignature(compiler).forEachParameter((element) {
- compiler.enqueuer.codegen.addToWorkList(element, work.resolutionTree);
- });
- List<js.Parameter> parameters = <js.Parameter>[];
- parameterNames.forEach((element, name) {
- parameters.add(new js.Parameter(name));
- });
- addBackendParameters(work.element, parameters, parameterNames);
- String parametersString = Strings.join(parameterNames.values, ", ");
- SsaOptimizedCodeGenerator codegen = new SsaOptimizedCodeGenerator(
- backend, work, parameters, parameterNames);
+ SsaOptimizedCodeGenerator codegen =
+ new SsaOptimizedCodeGenerator(backend, work);
codegen.visitGraph(graph);
FunctionElement element = work.element;
@@ -108,6 +94,8 @@ class SsaCodeGeneratorTask extends CompilerTask {
nativeEmitter.overriddenMethods.add(element);
StringBuffer buffer = new StringBuffer();
String codeString = prettyPrint(codegen.body).toString();
+ String parametersString =
+ Strings.join(codegen.parameterNames.values, ", ");
native.generateMethodWithPrototypeCheckForElement(
compiler, buffer, element, codeString, parametersString);
js.Node nativeCode = new js.LiteralStatement(buffer.toString());
@@ -117,75 +105,18 @@ class SsaCodeGeneratorTask extends CompilerTask {
body = codegen.body;
allowVariableMinification = !codegen.visitedForeignCode;
}
- js.Fun fun = buildJavaScriptFunction(element, parameters, body);
+ js.Fun fun = buildJavaScriptFunction(element, codegen.parameters, body);
return prettyPrint(fun,
allowVariableMinification: allowVariableMinification);
});
}
- void addBackendParameter(Element element,
- List<js.Parameter> parameters,
- Map<Element, String> parameterNames) {
- String name = element.name.slowToString();
- String prefix = '';
- // Avoid collisions with real parameters of the method.
- do {
- name = JsNames.getValid('$prefix$name');
- prefix = '\$$prefix';
- } while (parameterNames.containsValue(name));
- parameterNames[element] = name;
- parameters.add(new js.Parameter(name));
- }
-
- void addBackendParameters(Element element,
- List<js.Parameter> parameters,
- Map<Element, String> parameterNames) {
- // TODO(ngeoffray): We should infer this information from the
- // graph, instead of recomputing what the builder did.
- if (element.isConstructor()) {
- // Put the type parameters.
- ClassElement cls = element.enclosingElement;
- if (!compiler.world.needsRti(cls)) return;
- cls.typeVariables.forEach((TypeVariableType typeVariable) {
- addBackendParameter(typeVariable.element, parameters, parameterNames);
- });
- } else if (element.isGenerativeConstructorBody()) {
- // Put the parameter checks parameters.
- Node node = element.implementation.parseNode(compiler);
- ClosureClassMap closureData =
- compiler.closureToClassMapper.getMappingForNestedFunction(node);
- FunctionElement functionElement = element;
- FunctionSignature params = functionElement.computeSignature(compiler);
- TreeElements elements =
- compiler.enqueuer.resolution.getCachedElements(element);
- params.orderedForEachParameter((Element element) {
- if (elements.isParameterChecked(element)) {
- Element checkResultElement =
- closureData.parametersWithSentinel[element];
- addBackendParameter(checkResultElement, parameters, parameterNames);
- }
- });
- // Put the box parameter.
- ClosureScope scopeData = closureData.capturingScopes[node];
- if (scopeData != null) {
- addBackendParameter(scopeData.boxElement, parameters, parameterNames);
- }
- }
- }
-
CodeBuffer generateBailoutMethod(WorkItem work, HGraph graph) {
return measure(() {
compiler.tracer.traceGraph("codegen-bailout", graph);
- Map<Element, String> parameterNames = getParameterNames(work);
- List<js.Parameter> parameters = <js.Parameter>[];
- parameterNames.forEach((element, name) {
- parameters.add(new js.Parameter(name));
- });
- addBackendParameters(work.element, parameters, parameterNames);
-
- SsaUnoptimizedCodeGenerator codegen = new SsaUnoptimizedCodeGenerator(
- backend, work, parameters, parameterNames);
+ SsaUnoptimizedCodeGenerator codegen =
+ new SsaUnoptimizedCodeGenerator(backend, work);
codegen.visitGraph(graph);
js.Block body = new js.Block(<js.Statement>[]);
@@ -196,24 +127,6 @@ class SsaCodeGeneratorTask extends CompilerTask {
return prettyPrint(fun);
});
}
-
- Map<Element, String> getParameterNames(WorkItem work) {
- // Make sure the map preserves insertion order, so that fetching
- // the values will keep the order of parameters.
- Map<Element, String> parameterNames = new LinkedHashMap<Element, String>();
- FunctionElement function = work.element.implementation;
-
- // The dom/html libraries have inline JS code that reference
- // parameter names directly. Long-term such code will be rejected.
- // Now, just don't mangle the parameter name.
- FunctionSignature signature = function.computeSignature(compiler);
- signature.orderedForEachParameter((Element element) {
- parameterNames[element] = function.isNative()
- ? element.name.slowToString()
- : JsNames.getValid('${element.name.slowToString()}');
- });
- return parameterNames;
- }
}
// Stop-gap until the core classes have such a class.
@@ -275,6 +188,7 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
final Map<Element, ElementAction> breakAction;
final Map<Element, ElementAction> continueAction;
final Map<Element, String> parameterNames;
+ final List<js.Parameter> parameters;
js.Block currentContainer;
js.Block get body => currentContainer;
@@ -314,15 +228,15 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
// if branches.
SubGraph subGraph;
- SsaCodeGenerator(this.backend,
- WorkItem work,
- this.parameterNames)
+ SsaCodeGenerator(this.backend, WorkItem work)
: this.work = work,
this.types =
(work.compilationContext as JavaScriptItemCompilationContext).types,
+ parameterNames = new LinkedHashMap<Element, String>(),
declaredLocals = new Set<String>(),
collectedVariableDeclarations = new OrderedSet<String>(),
currentContainer = new js.Block.empty(),
+ parameters = <js.Parameter>[],
expressionStack = <js.Expression>[],
oldContainerStack = <js.Block>[],
generateAtUseSite = new Set<HInstruction>(),
@@ -917,7 +831,7 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
bool visitLoopInfo(HLoopBlockInformation info) {
HExpressionInformation condition = info.condition;
- bool isConditionExpression = isJSCondition(condition);
+ bool isConditionExpression = condition == null || isJSCondition(condition);
js.Loop loop;
@@ -1029,7 +943,11 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
}
break;
case HLoopBlockInformation.DO_WHILE_LOOP:
- // Generate do-while loop in all cases.
+ // If there are phi copies after the condition, we cannot emit
+ // a pretty do/while loop, se we fallback to the generic
+ // emission of a loop.
+ CopyHandler handler = variableNames.getCopyHandler(info.end);
+ if (handler != null && !handler.isEmpty) return false;
if (info.initializer != null) {
generateStatements(info.initializer);
}
@@ -1044,7 +962,9 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
if (info.updates != null) {
generateStatements(info.updates);
}
- if (isConditionExpression) {
+ if (condition == null) {
+ push(newLiteralBool(false));
+ } else if (isConditionExpression) {
push(generateExpression(condition));
} else {
generateStatements(condition);
@@ -1324,7 +1244,16 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
}
instruction = instruction.next;
}
- assignPhisOfSuccessors(node);
+ if (instruction is HLoopBranch) {
+ HLoopBranch branch = instruction;
+ // If the loop is a do/while loop, the phi updates must happen
+ // after the evaluation of the condition.
+ if (!branch.isDoWhile()) {
+ assignPhisOfSuccessors(node);
+ }
+ } else {
+ assignPhisOfSuccessors(node);
+ }
visit(instruction);
}
@@ -1500,6 +1429,15 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
}
}
+ visitExitTry(HExitTry node) {
+ // An [HExitTry] is used to represent the control flow graph of a
+ // try/catch block, ie the try body is always a predecessor
+ // of the catch and finally. Here, we continue visiting the try
+ // body by visiting the block that contains the user-level control
+ // flow instruction.
+ visitBasicBlock(node.bodyTrySuccessor);
+ }
+
visitTry(HTry node) {
// We should never get here. Try/catch/finally is always handled using block
// information in [visitTryInfo], or not at all, in the case of the bailout
@@ -1907,8 +1845,12 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
HBasicBlock branchBlock = currentBlock;
handleLoopCondition(node);
List<HBasicBlock> dominated = currentBlock.dominatedBlocks;
- // For a do while loop, the body has already been visited.
- if (!node.isDoWhile()) {
+ if (node.isDoWhile()) {
+ // Now that the condition has been evaluated, we can update the
+ // phis of a do/while loop.
+ assignPhisOfSuccessors(node.block);
+ } else {
+ // For a do while loop, the body has already been visited.
visitBasicBlock(dominated[0]);
}
endLoop(node.block);
@@ -2614,18 +2556,20 @@ abstract class SsaCodeGenerator implements HVisitor, HBlockInformationVisitor {
}
class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
- SsaOptimizedCodeGenerator(backend, work, parameters, parameterNames)
- : super(backend, work, parameterNames) {
+ SsaOptimizedCodeGenerator(backend, work) : super(backend, work);
+
+ int maxBailoutParameters;
+
+ HBasicBlock beginGraph(HGraph graph) {
// Declare the parameter names only for the optimized version. The
// unoptimized version has different parameters.
parameterNames.forEach((Element element, String name) {
+ parameters.add(new js.Parameter(name));
declaredLocals.add(name);
});
+ return graph.entry;
}
- int maxBailoutParameters;
-
- HBasicBlock beginGraph(HGraph graph) => graph.entry;
void endGraph(HGraph graph) {}
js.Statement bailout(HTypeGuard guard, String reason) {
@@ -2770,7 +2714,8 @@ class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
body = unwrapStatement(body);
js.While loop = new js.While(newLiteralBool(true), body);
- HLoopInformation info = block.loopInformation;
+ HBasicBlock header = block.isLoopHeader() ? block : block.parentLoopHeader;
+ HLoopInformation info = header.loopInformation;
attachLocationRange(loop,
info.loopBlockInformation.sourcePosition,
info.loopBlockInformation.endSourcePosition);
@@ -2779,7 +2724,9 @@ class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
void handleLoopCondition(HLoopBranch node) {
use(node.inputs[0]);
- pushStatement(new js.If.noElse(pop(), new js.Break(null)), node);
+ js.Expression test = new js.Prefix('!', pop());
+ js.Statement then = new js.Break(null);
+ pushStatement(new js.If.noElse(test, then), node);
}
@@ -2811,8 +2758,8 @@ class SsaUnoptimizedCodeGenerator extends SsaCodeGenerator {
SsaBailoutPropagator propagator;
HInstruction savedFirstInstruction;
- SsaUnoptimizedCodeGenerator(backend, work, parameters, parameterNames)
- : super(backend, work, parameterNames),
+ SsaUnoptimizedCodeGenerator(backend, work)
+ : super(backend, work),
oldBailoutSwitches = <js.Switch>[],
newParameters = <js.Parameter>[],
labels = <String>[],
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/ssa/builder.dart ('k') | sdk/lib/_internal/compiler/implementation/ssa/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698