Created
July 6, 2022 16:28
-
-
Save baioc/a063d120d23d6b1566eea28be067525e to your computer and use it in GitHub Desktop.
GyreD API example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gyre.mnemonics; | |
Graph graph; | |
graph.initialize(MacroNode.ID(42)); | |
scope(exit) graph.dispose(); | |
assert(graph.exported == 0); | |
assert(graph.length == 0); | |
// imagine we're compiling something like | |
// foo(ret, x) { | |
// a = x + 1 | |
// b = 1 + x | |
// y = a / b | |
// ret(y) | |
// } | |
with (Subgraph.init) { | |
reserve(6); | |
auto foo = insert!func(2); | |
auto jmp = insert!jump(1); | |
auto y = insert!divu; | |
auto a = insert!addnos; | |
auto b = insert!addnos; | |
auto c1 = insert!const_(1); | |
auto params = foo.channels[0]; | |
auto ret = ¶ms[0]; | |
auto x = ¶ms[1]; | |
link!"control"(foo, jmp.control); | |
link!"continuation"(jmp, ret); | |
link!"arguments"(jmp, 0, y.quotient); | |
link!"dividend"(y, a.result); | |
link!"divisor"(y, b.result); | |
link!"lhs"(a, x); | |
link!"rhs"(a, c1.value); | |
link!"lhs"(b, c1.value); | |
link!"rhs"(b, x); | |
commit(graph, (node, inGraph){ | |
if (node is foo.asNode) { | |
auto fooInGraph = JoinNode.ofNode(inGraph); | |
graph.export_(fooInGraph.definition); | |
} | |
}); | |
} | |
// since `a` and `b` compute the same value, one was CSE'd away | |
assert(graph.length == 5); | |
assert(graph.exported == 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment