| OLD | NEW |
| 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 SsaCodeGeneratorTask extends CompilerTask { | 7 class SsaCodeGeneratorTask extends CompilerTask { |
| 8 | 8 |
| 9 final JavaScriptBackend backend; | 9 final JavaScriptBackend backend; |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // Use [work.element] to ensure that the parameter element come from | 79 // Use [work.element] to ensure that the parameter element come from |
| 80 // the declaration. | 80 // the declaration. |
| 81 FunctionElement function = work.element; | 81 FunctionElement function = work.element; |
| 82 function.computeSignature(compiler).forEachParameter((element) { | 82 function.computeSignature(compiler).forEachParameter((element) { |
| 83 compiler.enqueuer.codegen.addToWorkList(element, work.resolutionTree); | 83 compiler.enqueuer.codegen.addToWorkList(element, work.resolutionTree); |
| 84 }); | 84 }); |
| 85 List<js.Parameter> parameters = <js.Parameter>[]; | 85 List<js.Parameter> parameters = <js.Parameter>[]; |
| 86 parameterNames.forEach((element, name) { | 86 parameterNames.forEach((element, name) { |
| 87 parameters.add(new js.Parameter(name)); | 87 parameters.add(new js.Parameter(name)); |
| 88 }); | 88 }); |
| 89 addTypeParameters(work.element, parameters, parameterNames); | 89 addBackendParameters(work.element, parameters, parameterNames); |
| 90 String parametersString = Strings.join(parameterNames.values, ", "); | 90 String parametersString = Strings.join(parameterNames.values, ", "); |
| 91 SsaOptimizedCodeGenerator codegen = new SsaOptimizedCodeGenerator( | 91 SsaOptimizedCodeGenerator codegen = new SsaOptimizedCodeGenerator( |
| 92 backend, work, parameters, parameterNames); | 92 backend, work, parameters, parameterNames); |
| 93 codegen.visitGraph(graph); | 93 codegen.visitGraph(graph); |
| 94 | 94 |
| 95 FunctionElement element = work.element; | 95 FunctionElement element = work.element; |
| 96 js.Block body; | 96 js.Block body; |
| 97 ClassElement enclosingClass = element.getEnclosingClass(); | 97 ClassElement enclosingClass = element.getEnclosingClass(); |
| 98 if (element.isInstanceMember() | 98 if (element.isInstanceMember() |
| 99 && enclosingClass.isNative() | 99 && enclosingClass.isNative() |
| (...skipping 10 matching lines...) Expand all Loading... |
| 110 js.Node nativeCode = new js.LiteralStatement(buffer.toString()); | 110 js.Node nativeCode = new js.LiteralStatement(buffer.toString()); |
| 111 body = new js.Block(<js.Statement>[nativeCode]); | 111 body = new js.Block(<js.Statement>[nativeCode]); |
| 112 } else { | 112 } else { |
| 113 body = codegen.body; | 113 body = codegen.body; |
| 114 } | 114 } |
| 115 js.Fun fun = buildJavaScriptFunction(element, parameters, body); | 115 js.Fun fun = buildJavaScriptFunction(element, parameters, body); |
| 116 return prettyPrint(fun); | 116 return prettyPrint(fun); |
| 117 }); | 117 }); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void addTypeParameters(Element element, | 120 void addBackendParameter(Element element, |
| 121 List<js.Parameter> parameters, | 121 List<js.Parameter> parameters, |
| 122 Map<Element, String> parameterNames) { | 122 Map<Element, String> parameterNames) { |
| 123 if (!element.isConstructor()) return; | 123 String name = element.name.slowToString(); |
| 124 ClassElement cls = element.enclosingElement; | 124 String prefix = ''; |
| 125 if (!compiler.world.needsRti(cls)) return; | 125 // Avoid collisions with real parameters of the method. |
| 126 cls.typeVariables.forEach((TypeVariableType typeVariable) { | 126 do { |
| 127 String name = typeVariable.element.name.slowToString(); | 127 name = JsNames.getValid('$prefix$name'); |
| 128 String prefix = ''; | 128 prefix = '\$$prefix'; |
| 129 // Avoid collisions with real parameters of the method. | 129 } while (parameterNames.containsValue(name)); |
| 130 do { | 130 parameterNames[element] = name; |
| 131 name = JsNames.getValid('$prefix$name'); | 131 parameters.add(new js.Parameter(name)); |
| 132 prefix = '\$$prefix'; | 132 } |
| 133 } while (parameterNames.containsValue(name)); | 133 |
| 134 parameterNames[typeVariable.element] = name; | 134 void addBackendParameters(Element element, |
| 135 parameters.add(new js.Parameter(name)); | 135 List<js.Parameter> parameters, |
| 136 }); | 136 Map<Element, String> parameterNames) { |
| 137 // TODO(ngeoffray): We should infer this information from the |
| 138 // graph, instead of recomputing what the builder did. |
| 139 if (element.isConstructor()) { |
| 140 // Put the type parameters. |
| 141 ClassElement cls = element.enclosingElement; |
| 142 if (!compiler.world.needsRti(cls)) return; |
| 143 cls.typeVariables.forEach((TypeVariableType typeVariable) { |
| 144 addBackendParameter(typeVariable.element, parameters, parameterNames); |
| 145 }); |
| 146 } else if (element.isGenerativeConstructorBody()) { |
| 147 // Put the parameter checks parameters. |
| 148 Node node = element.implementation.parseNode(compiler); |
| 149 ClosureClassMap closureData = |
| 150 compiler.closureToClassMapper.getMappingForNestedFunction(node); |
| 151 FunctionElement functionElement = element; |
| 152 FunctionSignature params = functionElement.computeSignature(compiler); |
| 153 TreeElements elements = |
| 154 compiler.enqueuer.resolution.getCachedElements(element); |
| 155 params.orderedForEachParameter((Element element) { |
| 156 if (elements.isParameterChecked(element)) { |
| 157 Element checkResultElement = |
| 158 closureData.parametersWithSentinel[element]; |
| 159 addBackendParameter(checkResultElement, parameters, parameterNames); |
| 160 } |
| 161 }); |
| 162 } |
| 137 } | 163 } |
| 138 | 164 |
| 139 CodeBuffer generateBailoutMethod(WorkItem work, HGraph graph) { | 165 CodeBuffer generateBailoutMethod(WorkItem work, HGraph graph) { |
| 140 return measure(() { | 166 return measure(() { |
| 141 compiler.tracer.traceGraph("codegen-bailout", graph); | 167 compiler.tracer.traceGraph("codegen-bailout", graph); |
| 142 | 168 |
| 143 Map<Element, String> parameterNames = getParameterNames(work); | 169 Map<Element, String> parameterNames = getParameterNames(work); |
| 144 List<js.Parameter> parameters = <js.Parameter>[]; | 170 List<js.Parameter> parameters = <js.Parameter>[]; |
| 145 parameterNames.forEach((element, name) { | 171 parameterNames.forEach((element, name) { |
| 146 parameters.add(new js.Parameter(name)); | 172 parameters.add(new js.Parameter(name)); |
| 147 }); | 173 }); |
| 148 addTypeParameters(work.element, parameters, parameterNames); | 174 addBackendParameters(work.element, parameters, parameterNames); |
| 149 | 175 |
| 150 SsaUnoptimizedCodeGenerator codegen = new SsaUnoptimizedCodeGenerator( | 176 SsaUnoptimizedCodeGenerator codegen = new SsaUnoptimizedCodeGenerator( |
| 151 backend, work, parameters, parameterNames); | 177 backend, work, parameters, parameterNames); |
| 152 codegen.visitGraph(graph); | 178 codegen.visitGraph(graph); |
| 153 | 179 |
| 154 js.Block body = new js.Block(<js.Statement>[]); | 180 js.Block body = new js.Block(<js.Statement>[]); |
| 155 if (codegen.setup != null) body.statements.add(codegen.setup); | 181 if (codegen.setup != null) body.statements.add(codegen.setup); |
| 156 body.statements.add(codegen.body); | 182 body.statements.add(codegen.body); |
| 157 js.Fun fun = | 183 js.Fun fun = |
| 158 buildJavaScriptFunction(work.element, codegen.newParameters, body); | 184 buildJavaScriptFunction(work.element, codegen.newParameters, body); |
| (...skipping 2833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2992 if (leftType.canBeNull() && rightType.canBeNull()) { | 3018 if (leftType.canBeNull() && rightType.canBeNull()) { |
| 2993 if (left.isConstantNull() || right.isConstantNull() || | 3019 if (left.isConstantNull() || right.isConstantNull() || |
| 2994 (leftType.isPrimitive() && leftType == rightType)) { | 3020 (leftType.isPrimitive() && leftType == rightType)) { |
| 2995 return '=='; | 3021 return '=='; |
| 2996 } | 3022 } |
| 2997 return null; | 3023 return null; |
| 2998 } else { | 3024 } else { |
| 2999 return '==='; | 3025 return '==='; |
| 3000 } | 3026 } |
| 3001 } | 3027 } |
| OLD | NEW |