| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Generate code using the cps-based IR pipeline. |
| 6 library code_generator_task; |
| 7 |
| 8 import '../js_backend.dart';import '../../dart2jslib.dart'; |
| 9 import '../../source_file.dart'; |
| 10 import '../../cps_ir/cps_ir_nodes.dart' as cps; |
| 11 import '../../cps_ir/cps_ir_builder.dart'; |
| 12 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
| 13 import '../../tree/tree.dart' as ast; |
| 14 import '../../scanner/scannerlib.dart' as scanner; |
| 15 import '../../elements/elements.dart'; |
| 16 import '../../closure.dart'; |
| 17 import '../../js/js.dart' as js; |
| 18 import '../../source_map_builder.dart'; |
| 19 import '../../tree_ir/tree_ir_builder.dart' as tree_builder; |
| 20 import '../../dart_backend/backend_ast_emitter.dart' as backend_ast_emitter; |
| 21 import '../../cps_ir/optimizers.dart'; |
| 22 import '../../tracer.dart'; |
| 23 import '../../dart_backend/statement_rewriter.dart'; |
| 24 import '../../dart_backend/copy_propagator.dart'; |
| 25 import '../../dart_backend/loop_rewriter.dart'; |
| 26 import '../../dart_backend/logical_rewriter.dart'; |
| 27 import '../../js_backend/codegen/codegen.dart'; |
| 28 import '../../ssa/ssa.dart' as ssa; |
| 29 |
| 30 class CspFunctionCompiler implements FunctionCompiler { |
| 31 |
| 32 // TODO(sigurdm): Assign this. |
| 33 Tracer tracer; |
| 34 |
| 35 String get name => 'CPS Ir pipeline'; |
| 36 |
| 37 final IrBuilderTask irBuilderTask; |
| 38 |
| 39 final ConstantSystem constantSystem; |
| 40 |
| 41 final Compiler compiler; |
| 42 |
| 43 FunctionCompiler fallbackPipeline; |
| 44 |
| 45 CspFunctionCompiler(Compiler compiler, JavaScriptBackend backend) |
| 46 : irBuilderTask = new IrBuilderTask(compiler), |
| 47 fallbackPipeline = new ssa.SsaFunctionCompiler(backend), |
| 48 constantSystem = backend.constantSystem, |
| 49 compiler = compiler; |
| 50 |
| 51 /// Generates JavaScript code for `work.element`. First tries to use the |
| 52 /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language |
| 53 /// features not implemented it will fall back to the ssa pipeline. |
| 54 js.Fun compile(CodegenWorkItem work) { |
| 55 AstElement element = work.element; |
| 56 return compiler.withCurrentElement(element, () { |
| 57 try { |
| 58 // TODO(sigurdm): Support these constructs. |
| 59 if(work.element.isGenerativeConstructorBody || |
| 60 work.element.enclosingClass is ClosureClassElement || |
| 61 work.element.isNative) { |
| 62 throw CodeGenerator.UNIMPLEMENTED; |
| 63 } |
| 64 |
| 65 void traceGraph(String title, var irObject) { |
| 66 if (tracer != null) { |
| 67 tracer.traceGraph(title, irObject); |
| 68 } |
| 69 } |
| 70 |
| 71 cps.FunctionDefinition cps_definition = |
| 72 irBuilderTask.buildNode(element); |
| 73 if (cps_definition == null) throw CodeGenerator.UNIMPLEMENTED; |
| 74 if (tracer != null) { |
| 75 tracer.traceCompilation(element.name, null); |
| 76 } |
| 77 // Transformations on the CPS IR. |
| 78 traceGraph("IR Builder", cps_definition); |
| 79 new ConstantPropagator(compiler, constantSystem) |
| 80 .rewrite(cps_definition); |
| 81 traceGraph("Sparse constant propagation", cps_definition); |
| 82 new RedundantPhiEliminator().rewrite(cps_definition); |
| 83 traceGraph("Redundant phi elimination", cps_definition); |
| 84 new ShrinkingReducer().rewrite(cps_definition); |
| 85 traceGraph("Shrinking reductions", cps_definition); |
| 86 |
| 87 // Do not rewrite the IR after variable allocation. Allocation |
| 88 // makes decisions based on an approximation of IR variable live |
| 89 // ranges that can be invalidated by transforming the IR. |
| 90 new cps.RegisterAllocator().visit(cps_definition); |
| 91 |
| 92 tree_builder.Builder builder = new tree_builder.Builder(compiler); |
| 93 tree_ir.FunctionDefinition definition = builder.build(cps_definition); |
| 94 assert(definition != null); |
| 95 traceGraph('Tree builder', definition); |
| 96 |
| 97 // Transformations on the Tree IR. |
| 98 new StatementRewriter().rewrite(definition); |
| 99 traceGraph('Statement rewriter', definition); |
| 100 new CopyPropagator().rewrite(definition); |
| 101 traceGraph('Copy propagation', definition); |
| 102 new LoopRewriter().rewrite(definition); |
| 103 traceGraph('Loop rewriter', definition); |
| 104 new LogicalRewriter().rewrite(definition); |
| 105 traceGraph('Logical rewriter', definition); |
| 106 new backend_ast_emitter.UnshadowParameters().unshadow(definition); |
| 107 traceGraph('Unshadow parameters', definition); |
| 108 |
| 109 CodeGenerator codeGen = new CodeGenerator(); |
| 110 |
| 111 codeGen.buildFunction(definition); |
| 112 return buildJavaScriptFunction(work.element, |
| 113 codeGen.parameters, |
| 114 codeGen.body); |
| 115 } catch (e, tr) { |
| 116 if (e == CodeGenerator.UNIMPLEMENTED) { |
| 117 return fallbackPipeline.compile(work); |
| 118 } else { |
| 119 rethrow; |
| 120 } |
| 121 } |
| 122 }); |
| 123 } |
| 124 |
| 125 Iterable<CompilerTask> get tasks { |
| 126 // TODO(sigurdm): Make a better list of tasks. |
| 127 return <CompilerTask>[irBuilderTask]..addAll(fallbackPipeline.tasks); |
| 128 } |
| 129 |
| 130 js.Node attachPosition(js.Node node, AstElement element) { |
| 131 // TODO(sra): Attaching positions might be cleaner if the source position |
| 132 // was on a wrapping node. |
| 133 SourceFile sourceFile = sourceFileOfElement(element); |
| 134 String name = element.name; |
| 135 AstElement implementation = element.implementation; |
| 136 ast.Node expression = implementation.node; |
| 137 scanner.Token beginToken; |
| 138 scanner.Token endToken; |
| 139 if (expression == null) { |
| 140 // Synthesized node. Use the enclosing element for the location. |
| 141 beginToken = endToken = element.position; |
| 142 } else { |
| 143 beginToken = expression.getBeginToken(); |
| 144 endToken = expression.getEndToken(); |
| 145 } |
| 146 // TODO(podivilov): find the right sourceFile here and remove offset |
| 147 // checks below. |
| 148 var sourcePosition, endSourcePosition; |
| 149 if (beginToken.charOffset < sourceFile.length) { |
| 150 sourcePosition = |
| 151 new TokenSourceFileLocation(sourceFile, beginToken, name); |
| 152 } |
| 153 if (endToken.charOffset < sourceFile.length) { |
| 154 endSourcePosition = |
| 155 new TokenSourceFileLocation(sourceFile, endToken, name); |
| 156 } |
| 157 return node.withPosition(sourcePosition, endSourcePosition); |
| 158 } |
| 159 |
| 160 SourceFile sourceFileOfElement(Element element) { |
| 161 return element.implementation.compilationUnit.script.file; |
| 162 } |
| 163 |
| 164 js.Fun buildJavaScriptFunction(FunctionElement element, |
| 165 List<js.Parameter> parameters, |
| 166 js.Block body) { |
| 167 return attachPosition(new js.Fun(parameters, body), element); |
| 168 } |
| 169 } |
| OLD | NEW |