| 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 '../js_backend.dart';import '../../dart2jslib.dart'; | 8 import '../js_backend.dart';import '../../dart2jslib.dart'; |
| 9 import '../../source_file.dart'; | 9 import '../../source_file.dart'; |
| 10 import '../../cps_ir/cps_ir_nodes.dart' as cps; | 10 import '../../cps_ir/cps_ir_nodes.dart' as cps; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 String get name => 'CPS Ir pipeline'; | 35 String get name => 'CPS Ir pipeline'; |
| 36 | 36 |
| 37 final IrBuilderTask irBuilderTask; | 37 final IrBuilderTask irBuilderTask; |
| 38 | 38 |
| 39 final ConstantSystem constantSystem; | 39 final ConstantSystem constantSystem; |
| 40 | 40 |
| 41 final Compiler compiler; | 41 final Compiler compiler; |
| 42 | 42 |
| 43 // Remember to update dart-doc of [compile] when this field is removed. | 43 // Remember to update dart-doc of [compile] when this field is removed. |
| 44 FunctionCompiler fallbackCompiler; | 44 final FunctionCompiler fallbackCompiler; |
| 45 | 45 |
| 46 CspFunctionCompiler(Compiler compiler, JavaScriptBackend backend) | 46 CspFunctionCompiler(Compiler compiler, JavaScriptBackend backend) |
| 47 : irBuilderTask = new IrBuilderTask(compiler), | 47 : irBuilderTask = new IrBuilderTask(compiler), |
| 48 fallbackCompiler = new ssa.SsaFunctionCompiler(backend), | 48 fallbackCompiler = new ssa.SsaFunctionCompiler(backend), |
| 49 constantSystem = backend.constantSystem, | 49 constantSystem = backend.constantSystem, |
| 50 compiler = compiler; | 50 compiler = compiler; |
| 51 | 51 |
| 52 /// Generates JavaScript code for `work.element`. First tries to use the | 52 /// Generates JavaScript code for `work.element`. First tries to use the |
| 53 /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language | 53 /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language |
| 54 /// features not implemented it will fall back to the ssa pipeline. | 54 /// features not implemented it will fall back to the ssa pipeline. |
| 55 js.Fun compile(CodegenWorkItem work) { | 55 js.Fun compile(CodegenWorkItem work) { |
| 56 AstElement element = work.element; | 56 AstElement element = work.element; |
| 57 return compiler.withCurrentElement(element, () { | 57 return compiler.withCurrentElement(element, () { |
| 58 try { | 58 try { |
| 59 // TODO(sigurdm): Support these constructs. | |
| 60 if(work.element.isGenerativeConstructorBody || | |
| 61 work.element.enclosingClass is ClosureClassElement || | |
| 62 work.element.isNative) { | |
| 63 throw CodeGenerator.UNIMPLEMENTED; | |
| 64 } | |
| 65 | |
| 66 void traceGraph(String title, var irObject) { | |
| 67 if (tracer != null) { | |
| 68 tracer.traceGraph(title, irObject); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 cps.FunctionDefinition cps_definition = | |
| 73 irBuilderTask.buildNode(element); | |
| 74 if (cps_definition == null) throw CodeGenerator.UNIMPLEMENTED; | |
| 75 if (tracer != null) { | 59 if (tracer != null) { |
| 76 tracer.traceCompilation(element.name, null); | 60 tracer.traceCompilation(element.name, null); |
| 77 } | 61 } |
| 78 // Transformations on the CPS IR. | 62 cps.FunctionDefinition cpsFunction = compileToCpsIR(element); |
| 79 traceGraph("IR Builder", cps_definition); | 63 cpsFunction = optimizeCpsIR(cpsFunction); |
| 80 new ConstantPropagator(compiler, constantSystem) | 64 tree_ir.FunctionDefinition treeFunction = compileToTreeIR(cpsFunction); |
| 81 .rewrite(cps_definition); | 65 treeFunction = optimizeTreeIR(treeFunction); |
| 82 traceGraph("Sparse constant propagation", cps_definition); | 66 return compileToJavaScript(work, treeFunction); |
| 83 new RedundantPhiEliminator().rewrite(cps_definition); | 67 } on CodegenBailout catch (e) { |
| 84 traceGraph("Redundant phi elimination", cps_definition); | 68 compiler.log('Falling back to SSA compiler for $element' |
| 85 new ShrinkingReducer().rewrite(cps_definition); | 69 ' (${e.message})'); |
| 86 traceGraph("Shrinking reductions", cps_definition); | 70 return fallbackCompiler.compile(work); |
| 87 | |
| 88 // Do not rewrite the IR after variable allocation. Allocation | |
| 89 // makes decisions based on an approximation of IR variable live | |
| 90 // ranges that can be invalidated by transforming the IR. | |
| 91 new cps.RegisterAllocator().visit(cps_definition); | |
| 92 | |
| 93 tree_builder.Builder builder = new tree_builder.Builder(compiler); | |
| 94 tree_ir.FunctionDefinition definition = builder.build(cps_definition); | |
| 95 assert(definition != null); | |
| 96 traceGraph('Tree builder', definition); | |
| 97 | |
| 98 // Transformations on the Tree IR. | |
| 99 new StatementRewriter().rewrite(definition); | |
| 100 traceGraph('Statement rewriter', definition); | |
| 101 new CopyPropagator().rewrite(definition); | |
| 102 traceGraph('Copy propagation', definition); | |
| 103 new LoopRewriter().rewrite(definition); | |
| 104 traceGraph('Loop rewriter', definition); | |
| 105 new LogicalRewriter().rewrite(definition); | |
| 106 traceGraph('Logical rewriter', definition); | |
| 107 new backend_ast_emitter.UnshadowParameters().unshadow(definition); | |
| 108 traceGraph('Unshadow parameters', definition); | |
| 109 | |
| 110 CodeGenerator codeGen = new CodeGenerator(); | |
| 111 | |
| 112 codeGen.buildFunction(definition); | |
| 113 return buildJavaScriptFunction(work.element, | |
| 114 codeGen.parameters, | |
| 115 codeGen.body); | |
| 116 } catch (e, tr) { | |
| 117 if (e == CodeGenerator.UNIMPLEMENTED) { | |
| 118 return fallbackCompiler.compile(work); | |
| 119 } else { | |
| 120 rethrow; | |
| 121 } | |
| 122 } | 71 } |
| 123 }); | 72 }); |
| 124 } | 73 } |
| 125 | 74 |
| 75 void giveUp(String reason) { |
| 76 throw new CodegenBailout(null, reason); |
| 77 } |
| 78 |
| 79 void traceGraph(String title, var irObject) { |
| 80 if (tracer != null) { |
| 81 tracer.traceGraph(title, irObject); |
| 82 } |
| 83 } |
| 84 |
| 85 cps.FunctionDefinition compileToCpsIR(AstElement element) { |
| 86 // TODO(sigurdm): Support these constructs. |
| 87 if (element.isGenerativeConstructorBody || |
| 88 element.enclosingClass is ClosureClassElement || |
| 89 element.isNative) { |
| 90 giveUp('unsupported element kind: ${element.name}:${element.kind}'); |
| 91 } |
| 92 |
| 93 cps.FunctionDefinition cpsNode = irBuilderTask.buildNode(element); |
| 94 if (cpsNode == null) { |
| 95 giveUp('unable to build cps definition of $element'); |
| 96 } |
| 97 return cpsNode; |
| 98 } |
| 99 |
| 100 cps.FunctionDefinition optimizeCpsIR(cps.FunctionDefinition cpsNode) { |
| 101 // Transformations on the CPS IR. |
| 102 traceGraph("IR Builder", cpsNode); |
| 103 new ConstantPropagator(compiler, constantSystem) |
| 104 .rewrite(cpsNode); |
| 105 traceGraph("Sparse constant propagation", cpsNode); |
| 106 new RedundantPhiEliminator().rewrite(cpsNode); |
| 107 traceGraph("Redundant phi elimination", cpsNode); |
| 108 new ShrinkingReducer().rewrite(cpsNode); |
| 109 traceGraph("Shrinking reductions", cpsNode); |
| 110 |
| 111 // Do not rewrite the IR after variable allocation. Allocation |
| 112 // makes decisions based on an approximation of IR variable live |
| 113 // ranges that can be invalidated by transforming the IR. |
| 114 new cps.RegisterAllocator().visit(cpsNode); |
| 115 return cpsNode; |
| 116 } |
| 117 |
| 118 tree_ir.FunctionDefinition compileToTreeIR(cps.FunctionDefinition cpsNode) { |
| 119 tree_builder.Builder builder = new tree_builder.Builder(compiler); |
| 120 tree_ir.FunctionDefinition treeNode = builder.build(cpsNode); |
| 121 assert(treeNode != null); |
| 122 traceGraph('Tree builder', treeNode); |
| 123 return treeNode; |
| 124 } |
| 125 |
| 126 tree_ir.FunctionDefinition optimizeTreeIR( |
| 127 tree_ir.FunctionDefinition treeNode) { |
| 128 // Transformations on the Tree IR. |
| 129 new StatementRewriter().rewrite(treeNode); |
| 130 traceGraph('Statement rewriter', treeNode); |
| 131 new CopyPropagator().rewrite(treeNode); |
| 132 traceGraph('Copy propagation', treeNode); |
| 133 new LoopRewriter().rewrite(treeNode); |
| 134 traceGraph('Loop rewriter', treeNode); |
| 135 new LogicalRewriter().rewrite(treeNode); |
| 136 traceGraph('Logical rewriter', treeNode); |
| 137 new backend_ast_emitter.UnshadowParameters().unshadow(treeNode); |
| 138 traceGraph('Unshadow parameters', treeNode); |
| 139 return treeNode; |
| 140 } |
| 141 |
| 142 js.Fun compileToJavaScript(CodegenWorkItem work, |
| 143 tree_ir.FunctionDefinition definition) { |
| 144 CodeGenerator codeGen = new CodeGenerator(); |
| 145 |
| 146 codeGen.buildFunction(definition); |
| 147 return buildJavaScriptFunction(work.element, |
| 148 codeGen.parameters, |
| 149 codeGen.body); |
| 150 } |
| 151 |
| 126 Iterable<CompilerTask> get tasks { | 152 Iterable<CompilerTask> get tasks { |
| 127 // TODO(sigurdm): Make a better list of tasks. | 153 // TODO(sigurdm): Make a better list of tasks. |
| 128 return <CompilerTask>[irBuilderTask]..addAll(fallbackCompiler.tasks); | 154 return <CompilerTask>[irBuilderTask]..addAll(fallbackCompiler.tasks); |
| 129 } | 155 } |
| 130 | 156 |
| 131 js.Node attachPosition(js.Node node, AstElement element) { | 157 js.Node attachPosition(js.Node node, AstElement element) { |
| 132 // TODO(sra): Attaching positions might be cleaner if the source position | 158 // TODO(sra): Attaching positions might be cleaner if the source position |
| 133 // was on a wrapping node. | 159 // was on a wrapping node. |
| 134 SourceFile sourceFile = sourceFileOfElement(element); | 160 SourceFile sourceFile = sourceFileOfElement(element); |
| 135 String name = element.name; | 161 String name = element.name; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 161 SourceFile sourceFileOfElement(Element element) { | 187 SourceFile sourceFileOfElement(Element element) { |
| 162 return element.implementation.compilationUnit.script.file; | 188 return element.implementation.compilationUnit.script.file; |
| 163 } | 189 } |
| 164 | 190 |
| 165 js.Fun buildJavaScriptFunction(FunctionElement element, | 191 js.Fun buildJavaScriptFunction(FunctionElement element, |
| 166 List<js.Parameter> parameters, | 192 List<js.Parameter> parameters, |
| 167 js.Block body) { | 193 js.Block body) { |
| 168 return attachPosition(new js.Fun(parameters, body), element); | 194 return attachPosition(new js.Fun(parameters, body), element); |
| 169 } | 195 } |
| 170 } | 196 } |
| OLD | NEW |