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