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 21 matching lines...) Expand all Loading... |
32 import '../../tree_ir/tree_ir_integrity.dart'; | 32 import '../../tree_ir/tree_ir_integrity.dart'; |
33 import '../../cps_ir/cps_ir_nodes_sexpr.dart'; | 33 import '../../cps_ir/cps_ir_nodes_sexpr.dart'; |
34 import 'js_tree_builder.dart'; | 34 import 'js_tree_builder.dart'; |
35 | 35 |
36 class CpsFunctionCompiler implements FunctionCompiler { | 36 class CpsFunctionCompiler implements FunctionCompiler { |
37 final ConstantSystem constantSystem; | 37 final ConstantSystem constantSystem; |
38 final Compiler compiler; | 38 final Compiler compiler; |
39 final Glue glue; | 39 final Glue glue; |
40 final SourceInformationFactory sourceInformationFactory; | 40 final SourceInformationFactory sourceInformationFactory; |
41 | 41 |
42 TypeSystem types; | |
43 | |
44 // TODO(karlklose,sigurm): remove and update dart-doc of [compile]. | 42 // TODO(karlklose,sigurm): remove and update dart-doc of [compile]. |
45 final FunctionCompiler fallbackCompiler; | 43 final FunctionCompiler fallbackCompiler; |
46 | 44 |
47 Tracer get tracer => compiler.tracer; | 45 Tracer get tracer => compiler.tracer; |
48 | 46 |
49 IrBuilderTask get irBuilderTask => compiler.irBuilder; | 47 IrBuilderTask get irBuilderTask => compiler.irBuilder; |
50 | 48 |
51 CpsFunctionCompiler(Compiler compiler, JavaScriptBackend backend, | 49 CpsFunctionCompiler(Compiler compiler, JavaScriptBackend backend, |
52 SourceInformationFactory sourceInformationFactory) | 50 SourceInformationFactory sourceInformationFactory) |
53 : fallbackCompiler = | 51 : fallbackCompiler = |
54 new ssa.SsaFunctionCompiler(backend, sourceInformationFactory), | 52 new ssa.SsaFunctionCompiler(backend, sourceInformationFactory), |
55 this.sourceInformationFactory = sourceInformationFactory, | 53 this.sourceInformationFactory = sourceInformationFactory, |
56 constantSystem = backend.constantSystem, | 54 constantSystem = backend.constantSystem, |
57 compiler = compiler, | 55 compiler = compiler, |
58 glue = new Glue(compiler); | 56 glue = new Glue(compiler); |
59 | 57 |
60 String get name => 'CPS Ir pipeline'; | 58 String get name => 'CPS Ir pipeline'; |
61 | 59 |
62 /// Generates JavaScript code for `work.element`. First tries to use the | 60 /// Generates JavaScript code for `work.element`. First tries to use the |
63 /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language | 61 /// Cps Ir -> tree ir -> js pipeline, and if that fails due to language |
64 /// features not implemented it will fall back to the ssa pipeline (for | 62 /// features not implemented it will fall back to the ssa pipeline (for |
65 /// platform code) or will cancel compilation (for user code). | 63 /// platform code) or will cancel compilation (for user code). |
66 js.Fun compile(CodegenWorkItem work) { | 64 js.Fun compile(CodegenWorkItem work) { |
67 types = new TypeMaskSystem(compiler); | |
68 AstElement element = work.element; | 65 AstElement element = work.element; |
69 JavaScriptBackend backend = compiler.backend; | 66 JavaScriptBackend backend = compiler.backend; |
70 return compiler.withCurrentElement(element, () { | 67 return compiler.withCurrentElement(element, () { |
71 if (element.library.isPlatformLibrary || | 68 if (element.library.isPlatformLibrary || |
72 element.library == backend.interceptorsLibrary) { | 69 element.library == backend.interceptorsLibrary) { |
73 compiler.log('Using SSA compiler for platform element $element'); | 70 compiler.log('Using SSA compiler for platform element $element'); |
74 return fallbackCompiler.compile(work); | 71 return fallbackCompiler.compile(work); |
75 } | 72 } |
76 try { | 73 try { |
77 if (tracer != null) { | 74 if (tracer != null) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 } | 128 } |
132 String suffix = (type.isExact ? "" : "+") + (type.isNullable ? "?" : "!"); | 129 String suffix = (type.isExact ? "" : "+") + (type.isNullable ? "?" : "!"); |
133 return '${type.base.name}$suffix'; | 130 return '${type.base.name}$suffix'; |
134 } else if (type is ForwardingTypeMask) { | 131 } else if (type is ForwardingTypeMask) { |
135 return formatTypeMask(type.forwardTo); | 132 return formatTypeMask(type.forwardTo); |
136 } | 133 } |
137 throw 'unsupported: $type'; | 134 throw 'unsupported: $type'; |
138 } | 135 } |
139 | 136 |
140 void dumpTypedIR(cps.FunctionDefinition cpsNode, | 137 void dumpTypedIR(cps.FunctionDefinition cpsNode, |
141 TypePropagator<TypeMask> typePropagator) { | 138 TypePropagator typePropagator) { |
142 if (PRINT_TYPED_IR_FILTER != null && | 139 if (PRINT_TYPED_IR_FILTER != null && |
143 PRINT_TYPED_IR_FILTER.matchAsPrefix(cpsNode.element.name) != null) { | 140 PRINT_TYPED_IR_FILTER.matchAsPrefix(cpsNode.element.name) != null) { |
144 String printType(nodeOrRef, String s) { | 141 String printType(nodeOrRef, String s) { |
145 cps.Node node = nodeOrRef is cps.Reference | 142 cps.Node node = nodeOrRef is cps.Reference |
146 ? nodeOrRef.definition | 143 ? nodeOrRef.definition |
147 : nodeOrRef; | 144 : nodeOrRef; |
148 var type = typePropagator.getType(node); | 145 var type = typePropagator.getType(node); |
149 return type == null ? s : "$s:${formatTypeMask(type.type)}"; | 146 return type == null ? s : "$s:${formatTypeMask(type.type)}"; |
150 } | 147 } |
151 DEBUG_MODE = true; | 148 DEBUG_MODE = true; |
152 print(new SExpressionStringifier(printType).visit(cpsNode)); | 149 print(new SExpressionStringifier(printType).visit(cpsNode)); |
153 } | 150 } |
154 } | 151 } |
155 | 152 |
156 static bool checkCpsIntegrity(cps.FunctionDefinition node) { | 153 static bool checkCpsIntegrity(cps.FunctionDefinition node) { |
157 new CheckCpsIntegrity().check(node); | 154 new CheckCpsIntegrity().check(node); |
158 return true; // So this can be used from assert(). | 155 return true; // So this can be used from assert(). |
159 } | 156 } |
160 | 157 |
161 cps.FunctionDefinition optimizeCpsIR(cps.FunctionDefinition cpsNode) { | 158 cps.FunctionDefinition optimizeCpsIR(cps.FunctionDefinition cpsNode) { |
162 // Transformations on the CPS IR. | 159 // Transformations on the CPS IR. |
163 void applyCpsPass(cps_opt.Pass pass) { | 160 void applyCpsPass(cps_opt.Pass pass) { |
164 pass.rewrite(cpsNode); | 161 pass.rewrite(cpsNode); |
165 traceGraph(pass.passName, cpsNode); | 162 traceGraph(pass.passName, cpsNode); |
166 assert(checkCpsIntegrity(cpsNode)); | 163 assert(checkCpsIntegrity(cpsNode)); |
167 } | 164 } |
168 | 165 |
169 TypePropagator typePropagator = new TypePropagator<TypeMask>( | 166 TypePropagator typePropagator = new TypePropagator(compiler); |
170 compiler.types, | |
171 constantSystem, | |
172 new TypeMaskSystem(compiler), | |
173 compiler.internalError); | |
174 applyCpsPass(typePropagator); | 167 applyCpsPass(typePropagator); |
175 dumpTypedIR(cpsNode, typePropagator); | 168 dumpTypedIR(cpsNode, typePropagator); |
176 applyCpsPass(new RedundantPhiEliminator()); | 169 applyCpsPass(new RedundantPhiEliminator()); |
177 applyCpsPass(new ShrinkingReducer()); | 170 applyCpsPass(new ShrinkingReducer()); |
178 | 171 |
179 return cpsNode; | 172 return cpsNode; |
180 } | 173 } |
181 | 174 |
182 tree_ir.FunctionDefinition compileToTreeIR(cps.FunctionDefinition cpsNode) { | 175 tree_ir.FunctionDefinition compileToTreeIR(cps.FunctionDefinition cpsNode) { |
183 tree_builder.Builder builder = new JsTreeBuilder( | 176 tree_builder.Builder builder = new JsTreeBuilder( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 // TODO(sigurdm): Make a better list of tasks. | 213 // TODO(sigurdm): Make a better list of tasks. |
221 return <CompilerTask>[irBuilderTask]..addAll(fallbackCompiler.tasks); | 214 return <CompilerTask>[irBuilderTask]..addAll(fallbackCompiler.tasks); |
222 } | 215 } |
223 | 216 |
224 js.Node attachPosition(js.Node node, AstElement element) { | 217 js.Node attachPosition(js.Node node, AstElement element) { |
225 return node.withSourceInformation( | 218 return node.withSourceInformation( |
226 sourceInformationFactory.forContext(element) | 219 sourceInformationFactory.forContext(element) |
227 .buildDeclaration(element)); | 220 .buildDeclaration(element)); |
228 } | 221 } |
229 } | 222 } |
OLD | NEW |