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 library ssa; | 5 library ssa; |
6 | 6 |
7 import '../common/codegen.dart' show CodegenWorkItem; | 7 import '../common/codegen.dart' show CodegenWorkItem; |
8 import '../common/tasks.dart' show CompilerTask, Measurer; | 8 import '../common/tasks.dart' show CompilerTask, Measurer; |
9 import '../elements/elements.dart' show MethodElement; | 9 import '../elements/elements.dart' show MethodElement; |
10 import '../elements/entities.dart' show MemberEntity; | 10 import '../elements/entities.dart' show MemberEntity; |
11 import '../io/source_information.dart'; | 11 import '../io/source_information.dart'; |
12 import '../js/js.dart' as js; | 12 import '../js/js.dart' as js; |
13 import '../js_backend/backend.dart' show JavaScriptBackend, FunctionCompiler; | 13 import '../js_backend/backend.dart' show JavaScriptBackend, FunctionCompiler; |
14 import '../world.dart' show ClosedWorld; | 14 import '../world.dart' show ClosedWorld; |
15 | 15 |
16 import 'codegen.dart'; | 16 import 'codegen.dart'; |
17 import 'nodes.dart'; | 17 import 'nodes.dart'; |
18 import 'optimize.dart'; | 18 import 'optimize.dart'; |
19 | 19 |
20 class SsaFunctionCompiler implements FunctionCompiler { | 20 class SsaFunctionCompiler implements FunctionCompiler { |
21 final SsaCodeGeneratorTask generator; | 21 final SsaCodeGeneratorTask generator; |
22 final SsaBuilderTask _builder; | 22 final SsaBuilderTask _builder; |
23 final SsaOptimizerTask optimizer; | 23 final SsaOptimizerTask optimizer; |
24 final JavaScriptBackend backend; | 24 final JavaScriptBackend backend; |
25 | 25 |
26 SsaFunctionCompiler(JavaScriptBackend backend, Measurer measurer, | 26 SsaFunctionCompiler(JavaScriptBackend backend, Measurer measurer, |
27 SourceInformationStrategy sourceInformationFactory) | 27 SourceInformationStrategy sourceInformationFactory) |
28 : generator = new SsaCodeGeneratorTask(backend, sourceInformationFactory), | 28 : generator = new SsaCodeGeneratorTask(backend, sourceInformationFactory), |
29 _builder = backend.compiler.backendStrategy | 29 _builder = new SsaBuilderTask(backend, sourceInformationFactory), |
30 .createSsaBuilderTask(backend, sourceInformationFactory), | |
31 optimizer = new SsaOptimizerTask(backend), | 30 optimizer = new SsaOptimizerTask(backend), |
32 backend = backend; | 31 backend = backend; |
33 | 32 |
| 33 void onCodegenStart() { |
| 34 _builder.onCodegenStart(); |
| 35 } |
| 36 |
34 /// Generates JavaScript code for `work.element`. | 37 /// Generates JavaScript code for `work.element`. |
35 /// Using the ssa builder, optimizer and codegenerator. | 38 /// Using the ssa builder, optimizer and codegenerator. |
36 js.Fun compile(CodegenWorkItem work, ClosedWorld closedWorld) { | 39 js.Fun compile(CodegenWorkItem work, ClosedWorld closedWorld) { |
37 HGraph graph = _builder.build(work, closedWorld); | 40 HGraph graph = _builder.build(work, closedWorld); |
38 if (graph == null) return null; | 41 if (graph == null) return null; |
39 optimizer.optimize(work, graph, closedWorld); | 42 optimizer.optimize(work, graph, closedWorld); |
40 MemberEntity element = work.element; | 43 MemberEntity element = work.element; |
41 js.Expression result = generator.generateCode(work, graph, closedWorld); | 44 js.Expression result = generator.generateCode(work, graph, closedWorld); |
42 if (element is MethodElement) { | 45 if (element is MethodElement) { |
43 // TODO(sigmund): replace by kernel transformer when `useKernel` is true. | 46 // TODO(sigmund): replace by kernel transformer when `useKernel` is true. |
44 result = | 47 result = |
45 backend.rewriteAsync(closedWorld.commonElements, element, result); | 48 backend.rewriteAsync(closedWorld.commonElements, element, result); |
46 } | 49 } |
47 return result; | 50 return result; |
48 } | 51 } |
49 | 52 |
50 Iterable<CompilerTask> get tasks { | 53 Iterable<CompilerTask> get tasks { |
51 return <CompilerTask>[_builder, optimizer, generator]; | 54 return <CompilerTask>[_builder, optimizer, generator]; |
52 } | 55 } |
53 } | 56 } |
54 | 57 |
55 abstract class SsaBuilderTask implements CompilerTask { | 58 abstract class SsaBuilder { |
56 /// Creates the [HGraph] for [work] or returns `null` if no code is needed | 59 /// Creates the [HGraph] for [work] or returns `null` if no code is needed |
57 /// for [work]. | 60 /// for [work]. |
58 HGraph build(CodegenWorkItem work, ClosedWorld closedWorld); | 61 HGraph build(CodegenWorkItem work, ClosedWorld closedWorld); |
59 } | 62 } |
| 63 |
| 64 class SsaBuilderTask extends CompilerTask { |
| 65 final JavaScriptBackend _backend; |
| 66 final SourceInformationStrategy _sourceInformationFactory; |
| 67 SsaBuilder _builder; |
| 68 |
| 69 SsaBuilderTask(this._backend, this._sourceInformationFactory) |
| 70 : super(_backend.compiler.measurer); |
| 71 |
| 72 void onCodegenStart() { |
| 73 _builder = _backend.compiler.backendStrategy |
| 74 .createSsaBuilder(this, _backend, _sourceInformationFactory); |
| 75 } |
| 76 |
| 77 /// Creates the [HGraph] for [work] or returns `null` if no code is needed |
| 78 /// for [work]. |
| 79 HGraph build(CodegenWorkItem work, ClosedWorld closedWorld) { |
| 80 return _builder.build(work, closedWorld); |
| 81 } |
| 82 } |
OLD | NEW |