| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Generate code using the cps-based IR pipeline. | 5 /// Generate code using the cps-based IR pipeline. |
| 6 library code_generator_task; | 6 library code_generator_task; |
| 7 | 7 |
| 8 import 'glue.dart'; | 8 import 'glue.dart'; |
| 9 import 'codegen.dart'; | 9 import 'codegen.dart'; |
| 10 import 'unsugar.dart'; | 10 import 'unsugar.dart'; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 return reporter.withCurrentElement(element, () { | 96 return reporter.withCurrentElement(element, () { |
| 97 try { | 97 try { |
| 98 // TODO(karlklose): remove this fallback when we do not need it for | 98 // TODO(karlklose): remove this fallback when we do not need it for |
| 99 // testing anymore. | 99 // testing anymore. |
| 100 if (false) { | 100 if (false) { |
| 101 reporter.log('Using SSA compiler for platform element $element'); | 101 reporter.log('Using SSA compiler for platform element $element'); |
| 102 return fallbackCompiler.compile(work); | 102 return fallbackCompiler.compile(work); |
| 103 } | 103 } |
| 104 | 104 |
| 105 if (tracer != null) { | 105 if (tracer != null) { |
| 106 tracer.traceCompilation(element.name, null); | 106 tracer.traceCompilation('$element', null); |
| 107 } | 107 } |
| 108 cps.FunctionDefinition cpsFunction = compileToCpsIr(element); | 108 cps.FunctionDefinition cpsFunction = compileToCpsIr(element); |
| 109 optimizeCpsBeforeInlining(cpsFunction); | 109 optimizeCpsBeforeInlining(cpsFunction); |
| 110 applyCpsPass(inliner, cpsFunction); | 110 applyCpsPass(inliner, cpsFunction); |
| 111 optimizeCpsAfterInlining(cpsFunction); | 111 optimizeCpsAfterInlining(cpsFunction); |
| 112 cpsIntegrityChecker = null; | 112 cpsIntegrityChecker = null; |
| 113 tree_ir.FunctionDefinition treeFunction = compileToTreeIr(cpsFunction); | 113 tree_ir.FunctionDefinition treeFunction = compileToTreeIr(cpsFunction); |
| 114 treeFunction = optimizeTreeIr(treeFunction); | 114 treeFunction = optimizeTreeIr(treeFunction); |
| 115 return compileToJavaScript(work, treeFunction); | 115 return compileToJavaScript(work, treeFunction); |
| 116 } on CodegenBailout catch (e) { | 116 } on CodegenBailout catch (e) { |
| 117 String message = "Unable to compile $element with the new compiler.\n" | 117 String message = "Unable to compile $element with the new compiler.\n" |
| 118 " Reason: ${e.message}"; | 118 " Reason: ${e.message}"; |
| 119 reporter.internalError(element, message); | 119 reporter.internalError(element, message); |
| 120 } | 120 } |
| 121 }); | 121 }); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void giveUp(String reason) { | 124 void giveUp(String reason) { |
| 125 throw new CodegenBailout(null, reason); | 125 throw new CodegenBailout(null, reason); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void traceGraph(String title, var irObject) { | 128 void traceGraph(String title, var irObject) { |
| 129 if (tracer != null) { | 129 if (tracer != null) { |
| 130 tracer.traceGraph(title, irObject); | 130 tracer.traceGraph(title, irObject); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 String stringify(cps.FunctionDefinition node) { |
| 135 return new SExpressionStringifier().withTypes().visit(node); |
| 136 } |
| 137 |
| 138 /// For debugging purposes, replace a call to [applyCpsPass] with a call |
| 139 /// to [debugCpsPass] to check that this pass is idempotent. |
| 140 /// |
| 141 /// This runs [pass] followed by shrinking reductions, and then checks that |
| 142 /// one more run of [pass] does not change the IR. The intermediate shrinking |
| 143 /// reductions pass is omitted if [pass] itself is shrinking reductions. |
| 144 /// |
| 145 /// If [targetName] is given, functions whose name contains that substring |
| 146 /// will be dumped out if the idempotency test fails. |
| 147 void debugCpsPass(cps_opt.Pass makePass(), |
| 148 cps.FunctionDefinition cpsFunction, |
| 149 [String targetName]) { |
| 150 String original = stringify(cpsFunction); |
| 151 cps_opt.Pass pass = makePass(); |
| 152 pass.rewrite(cpsFunction); |
| 153 assert(checkCpsIntegrity(cpsFunction, pass.passName)); |
| 154 if (pass is! ShrinkingReducer) { |
| 155 new ShrinkingReducer().rewrite(cpsFunction); |
| 156 } |
| 157 String before = stringify(cpsFunction); |
| 158 makePass().rewrite(cpsFunction); |
| 159 String after = stringify(cpsFunction); |
| 160 if (before != after) { |
| 161 print('SExpression changed for ${cpsFunction.element}'); |
| 162 if (targetName != null && '${cpsFunction.element}'.contains(targetName)) { |
| 163 print(original); |
| 164 print('\n-->\n'); |
| 165 print(before); |
| 166 print('\n-->\n'); |
| 167 print(after); |
| 168 compiler.outputProvider('original', 'dump')..add(original)..close(); |
| 169 compiler.outputProvider('before', 'dump')..add(before)..close(); |
| 170 compiler.outputProvider('after', 'dump')..add(after)..close(); |
| 171 } |
| 172 } |
| 173 traceGraph(pass.passName, cpsFunction); |
| 174 dumpTypedIr(pass.passName, cpsFunction); |
| 175 } |
| 176 |
| 134 void applyCpsPass(cps_opt.Pass pass, cps.FunctionDefinition cpsFunction) { | 177 void applyCpsPass(cps_opt.Pass pass, cps.FunctionDefinition cpsFunction) { |
| 135 cpsOptimizationTask.measureSubtask(pass.passName, () { | 178 cpsOptimizationTask.measureSubtask(pass.passName, () { |
| 136 pass.rewrite(cpsFunction); | 179 pass.rewrite(cpsFunction); |
| 137 }); | 180 }); |
| 138 traceGraph(pass.passName, cpsFunction); | 181 traceGraph(pass.passName, cpsFunction); |
| 139 dumpTypedIr(pass.passName, cpsFunction); | 182 dumpTypedIr(pass.passName, cpsFunction); |
| 140 assert(checkCpsIntegrity(cpsFunction, pass.passName)); | 183 assert(checkCpsIntegrity(cpsFunction, pass.passName)); |
| 141 } | 184 } |
| 142 | 185 |
| 143 cps.FunctionDefinition compileToCpsIr(AstElement element) { | 186 cps.FunctionDefinition compileToCpsIr(AstElement element) { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 treeOptimizationTask] | 351 treeOptimizationTask] |
| 309 ..addAll(fallbackCompiler.tasks); | 352 ..addAll(fallbackCompiler.tasks); |
| 310 } | 353 } |
| 311 | 354 |
| 312 js.Node attachPosition(js.Node node, AstElement element) { | 355 js.Node attachPosition(js.Node node, AstElement element) { |
| 313 return node.withSourceInformation( | 356 return node.withSourceInformation( |
| 314 sourceInformationFactory.createBuilderForContext(element) | 357 sourceInformationFactory.createBuilderForContext(element) |
| 315 .buildDeclaration(element)); | 358 .buildDeclaration(element)); |
| 316 } | 359 } |
| 317 } | 360 } |
| OLD | NEW |