forked from area9innovation/flow9
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstrument.hx
More file actions
91 lines (81 loc) · 2.32 KB
/
Copy pathInstrument.hx
File metadata and controls
91 lines (81 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import FlowUtil;
import Flow;
import FlowArray;
class Instrument {
// A facility to add debug tracing for the listed names
public static function instrument(p : Program, variables : String) : Void {
// A fast way to see if a name is hit
var vars = new Map();
for (v in variables.split(",")) {
vars.set(v, true);
}
var traceFound = false;
var toStringFound = false;
// First, register all strings and find the duplicated ones
for (d in p.declsOrder) {
traceFound = traceFound || d == "trace";
toStringFound = toStringFound || d == "toString";
if (vars.exists(d)) {
var e = p.topdecs.get(d);
switch (e) {
case Lambda(arguments, type, body, _, pos): {
var instrumented = instrumentFunction(d, body, pos);
p.topdecs.set(d, FlowUtil.lambda(arguments, type, instrumented, pos));
}
default: {
trace("Does not support instrumenting " + e + " yet");
}
}
}
}
if (!traceFound) {
Errors.print("--instrument requires \"trace = ref false;\" to be defined. Try \"import runtime;\"");
}
if (!toStringFound) {
Errors.print("--instrument requires \"native toString : (flow) -> string = Native.toString;\" to be defined. Try \"import runtime;\"");
}
}
/*
fn(args) -> ret {
body
}
should be changed to
fn(args) -> ret {
if (trace) {
println("fn called");
}
r = body;
if (trace) {
println("fn gave" + toString(r));
}
r;
}
*/
static function instrumentFunction(name : String, body : Flow, pos : Position) : Flow {
var p : Position = { f: pos.f, l: pos.l, s: pos.s, e: pos.e, type: null, type2: null };
var sp : Position = { f: pos.f, l: pos.l, s: pos.s, e: pos.e, type: TString, type2: null };
var instrumented = Sequence(FlowArrayUtil.fromArray([
If( Deref(VarRef("trace", p), p),
Call(VarRef("println", p), FlowArrayUtil.one(ConstantString(name, p)), p),
ConstantVoid(p),
p),
Let( "$r", null, body,
Sequence(FlowArrayUtil.two(
If( Deref(VarRef("trace", p), p),
Call(VarRef("println", p),
FlowArrayUtil.one(
Plus(
ConstantString(name + "=", sp),
Call(VarRef("toString", p), FlowArrayUtil.one(VarRef("$r", p)), p),
sp
)
), p),
ConstantVoid(p),
p),
VarRef("$r", p)
), p)
, p)
]), p);
return instrumented;
}
}