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 library tracer; | 5 library tracer; |
6 | 6 |
7 import 'dart:async' show EventSink; | 7 import 'dart:async' show EventSink; |
8 import '../compiler.dart' as api; | 8 import '../compiler.dart' as api; |
9 import 'common/work.dart' show | 9 import 'common/work.dart' show ItemCompilationContext; |
10 ItemCompilationContext; | 10 import 'compiler.dart' show Compiler; |
11 import 'compiler.dart' show | 11 import 'ssa/nodes.dart' as ssa show HGraph; |
12 Compiler; | 12 import 'ssa/ssa_tracer.dart' show HTracer; |
13 import 'ssa/nodes.dart' as ssa show | |
14 HGraph; | |
15 import 'ssa/ssa_tracer.dart' show | |
16 HTracer; | |
17 import 'cps_ir/cps_ir_nodes.dart' as cps_ir; | 13 import 'cps_ir/cps_ir_nodes.dart' as cps_ir; |
18 import 'cps_ir/cps_ir_tracer.dart' show | 14 import 'cps_ir/cps_ir_tracer.dart' show IRTracer; |
19 IRTracer; | |
20 import 'tree_ir/tree_ir_nodes.dart' as tree_ir; | 15 import 'tree_ir/tree_ir_nodes.dart' as tree_ir; |
21 import 'tree_ir/tree_ir_tracer.dart' show | 16 import 'tree_ir/tree_ir_tracer.dart' show TreeTracer; |
22 TreeTracer; | 17 import 'util/util.dart' show Indentation; |
23 import 'util/util.dart' show | |
24 Indentation; | |
25 | 18 |
26 /** | 19 /** |
27 * If non-null, we only trace methods whose name match the regexp defined by the | 20 * If non-null, we only trace methods whose name match the regexp defined by the |
28 * given pattern. | 21 * given pattern. |
29 */ | 22 */ |
30 const String TRACE_FILTER_PATTERN = const String.fromEnvironment("DUMP_IR"); | 23 const String TRACE_FILTER_PATTERN = const String.fromEnvironment("DUMP_IR"); |
31 | 24 |
32 final RegExp TRACE_FILTER = | 25 final RegExp TRACE_FILTER = |
33 TRACE_FILTER_PATTERN == null ? null : new RegExp(TRACE_FILTER_PATTERN); | 26 TRACE_FILTER_PATTERN == null ? null : new RegExp(TRACE_FILTER_PATTERN); |
34 | 27 |
35 /** | 28 /** |
36 * Dumps the intermediate representation after each phase in a format | 29 * Dumps the intermediate representation after each phase in a format |
37 * readable by IR Hydra. | 30 * readable by IR Hydra. |
38 */ | 31 */ |
39 class Tracer extends TracerUtil { | 32 class Tracer extends TracerUtil { |
40 final Compiler compiler; | 33 final Compiler compiler; |
41 ItemCompilationContext context; | 34 ItemCompilationContext context; |
42 bool traceActive = false; | 35 bool traceActive = false; |
43 final EventSink<String> output; | 36 final EventSink<String> output; |
44 final bool isEnabled = TRACE_FILTER != null; | 37 final bool isEnabled = TRACE_FILTER != null; |
45 | 38 |
46 Tracer(Compiler compiler, api.CompilerOutputProvider outputProvider) | 39 Tracer(Compiler compiler, api.CompilerOutputProvider outputProvider) |
47 : this.compiler = compiler, | 40 : this.compiler = compiler, |
48 output = TRACE_FILTER != null ? outputProvider('dart', 'cfg') : null; | 41 output = TRACE_FILTER != null ? outputProvider('dart', 'cfg') : null; |
49 | 42 |
50 void traceCompilation(String methodName, | 43 void traceCompilation( |
51 ItemCompilationContext compilationContext) { | 44 String methodName, ItemCompilationContext compilationContext) { |
52 if (!isEnabled) return; | 45 if (!isEnabled) return; |
53 traceActive = TRACE_FILTER.hasMatch(methodName); | 46 traceActive = TRACE_FILTER.hasMatch(methodName); |
54 if (!traceActive) return; | 47 if (!traceActive) return; |
55 this.context = compilationContext; | 48 this.context = compilationContext; |
56 tag("compilation", () { | 49 tag("compilation", () { |
57 printProperty("name", methodName); | 50 printProperty("name", methodName); |
58 printProperty("method", methodName); | 51 printProperty("method", methodName); |
59 printProperty("date", new DateTime.now().millisecondsSinceEpoch); | 52 printProperty("date", new DateTime.now().millisecondsSinceEpoch); |
60 }); | 53 }); |
61 } | 54 } |
62 | 55 |
63 void traceGraph(String name, var irObject) { | 56 void traceGraph(String name, var irObject) { |
64 if (!traceActive) return; | 57 if (!traceActive) return; |
65 if (irObject is ssa.HGraph) { | 58 if (irObject is ssa.HGraph) { |
66 new HTracer(output, compiler, context).traceGraph(name, irObject); | 59 new HTracer(output, compiler, context).traceGraph(name, irObject); |
67 } | 60 } else if (irObject is cps_ir.FunctionDefinition) { |
68 else if (irObject is cps_ir.FunctionDefinition) { | |
69 new IRTracer(output).traceGraph(name, irObject); | 61 new IRTracer(output).traceGraph(name, irObject); |
70 } | 62 } else if (irObject is tree_ir.FunctionDefinition) { |
71 else if (irObject is tree_ir.FunctionDefinition) { | |
72 new TreeTracer(output).traceGraph(name, irObject); | 63 new TreeTracer(output).traceGraph(name, irObject); |
73 } | 64 } |
74 } | 65 } |
75 | 66 |
76 void close() { | 67 void close() { |
77 if (output != null) { | 68 if (output != null) { |
78 output.close(); | 69 output.close(); |
79 } | 70 } |
80 } | 71 } |
81 } | 72 } |
82 | 73 |
83 | |
84 abstract class TracerUtil { | 74 abstract class TracerUtil { |
85 EventSink<String> get output; | 75 EventSink<String> get output; |
86 final Indentation _ind = new Indentation(); | 76 final Indentation _ind = new Indentation(); |
87 | 77 |
88 void tag(String tagName, Function f) { | 78 void tag(String tagName, Function f) { |
89 println("begin_$tagName"); | 79 println("begin_$tagName"); |
90 _ind.indentBlock(f); | 80 _ind.indentBlock(f); |
91 println("end_$tagName"); | 81 println("end_$tagName"); |
92 } | 82 } |
93 | 83 |
(...skipping 24 matching lines...) Expand all Loading... |
118 } | 108 } |
119 | 109 |
120 void add(String string) { | 110 void add(String string) { |
121 output.add(string); | 111 output.add(string); |
122 } | 112 } |
123 | 113 |
124 void addIndent() { | 114 void addIndent() { |
125 add(_ind.indentation); | 115 add(_ind.indentation); |
126 } | 116 } |
127 } | 117 } |
OLD | NEW |