Skip to content

Commit edc9f19

Browse files
committed
make DOT ir generator dependency free
1 parent da616c4 commit edc9f19

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

compiler/src/flow/visualize.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { ICompilerContext } from "../CompilerContext";
2-
import { digraph } from "../graphviz";
3-
import { Node } from "../graphviz/node";
42
import { Block } from "./block";
53
import { traverse } from "./graph";
64
import { ValueId } from "./id";
75
import { TBlockInstruction, TBlockEndInstruction } from "./instructions";
86

7+
/**
8+
* Debug function that you can use to visualize the compiler's intermediate
9+
* representation during compilation as a directed graph in DOT notation.
10+
*/
911
export function generateGraphVizDOTString(c: ICompilerContext, entry: Block) {
12+
let result = "digraph mlogjs_cfg {\n";
1013
const ids = new Map<Block, string>();
11-
const nodes = new Map<Block, Node>();
12-
const graph = digraph("G");
14+
15+
traverse(entry, block => {
16+
const id = `block${ids.size}`;
17+
ids.set(block, id);
18+
});
1319

1420
const n = (id: ValueId) => c.getValueOrTemp(id)?.toMlogString();
1521
function instToString(
@@ -51,38 +57,35 @@ export function generateGraphVizDOTString(c: ICompilerContext, entry: Block) {
5157
return "end";
5258
case "stop":
5359
return "stop";
60+
case "end-if":
61+
return `end-if ${n(inst.condition)} ${ids.get(inst.alternate.block)}`;
5462
default:
5563
throw new Error(
5664
`Missing representation for instruction of type ${
65+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
5766
(inst as any).type
5867
}`,
5968
);
6069
}
6170
}
6271

63-
traverse(entry, block => {
64-
const id = `block${ids.size}`;
65-
ids.set(block, id);
66-
nodes.set(block, graph.addNode(id));
67-
});
68-
6972
traverse(entry, block => {
7073
const id = ids.get(block)!;
71-
const node = nodes.get(block)!;
72-
node.set(
73-
"label",
74-
`${id}\n\n${[...block.instructions, block.endInstruction]
75-
.filter((value): value is NonNullable<typeof value> => !!value)
76-
.map(instToString)
77-
.join("\n")
78-
.replace(/"/g, "'")}`,
79-
);
74+
result += `${id} [label="${id}\n\n${[
75+
...block.instructions,
76+
block.endInstruction,
77+
]
78+
.filter((value): value is NonNullable<typeof value> => !!value)
79+
.map(instToString)
80+
.join("\n")
81+
.replace(/"/g, "'")}"];\n`;
82+
8083
for (const edge of block.childEdges) {
81-
const target = ids.get(edge.block);
82-
console.log(target);
83-
graph.addEdge(id, ids.get(edge.block)!, undefined);
84+
result += `${id} -> ${ids.get(edge.block)};\n`;
8485
}
8586
});
8687

87-
return graph.to_dot();
88+
result += "}\n";
89+
90+
return result;
8891
}

0 commit comments

Comments
 (0)