-
Notifications
You must be signed in to change notification settings - Fork 37
/
BytecodeCost.hx
44 lines (42 loc) · 1.24 KB
/
BytecodeCost.hx
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
import BytecodeWriter;
// A facility to evaluate the space/time cost of a given AST
class BytecodeCost {
public function new(interpreter : FlowInterpreter) {
writer = new BytecodeWriter();
debug = new DebugInfo(null);
names = new Names();
// Populate the names
var n = 0;
for (d in interpreter.order) {
names.toplevelAndOuter.set(d, TopLevel(n));
++n;
}
// This numbering is not correct, but it will suffice for cost estimation
var nstructs = 0;
for (d in interpreter.userTypeDeclarations) {
switch (d.type.type) {
case TStruct(structname, cargs, max): {
names.structs.set(structname, Struct(nstructs, structname, cargs.length));
nstructs++;
}
default:
}
}
}
// How much does this AST cost?
public function cost(e : Flow) : Int {
try {
// To find out, we simply compile to bytecode
var bytecodes = writer.encodeToBuffer(e, debug, names, false);
// At first, just use the length of the bytecode as the estimate.
// Later, make a cost model of each opcode separately
return bytecodes.length;
} catch (e : Dynamic) {
trace("Can not estimate the cost of " + Prettyprint.prettyprint(e) + ":\n" + e);
return 1;
}
}
var writer : BytecodeWriter;
var names : Names;
var debug : DebugInfo;
}