IR to CFG: when implementation in function generator, some when tests
This commit is contained in:
committed by
Dmitry Petrov
parent
eaf10a4675
commit
979f2231a1
@@ -102,6 +102,30 @@ class FunctionGenerator(val function: IrFunction) {
|
||||
return expression
|
||||
}
|
||||
|
||||
override fun visitWhen(expression: IrWhen, data: Boolean): IrElement? {
|
||||
if (data) {
|
||||
builder.add(expression)
|
||||
}
|
||||
val whenExit = MergeCfgElement(expression, "When exit")
|
||||
val branches = expression.branches
|
||||
for (branch in branches) {
|
||||
val condition = branch.condition
|
||||
condition.process(includeSelf = false)
|
||||
builder.jump(condition)
|
||||
}
|
||||
for (branch in branches) {
|
||||
val result = branch.result
|
||||
builder.move(branch.condition)
|
||||
if (!result.process().isNothing()) {
|
||||
builder.jump(whenExit)
|
||||
}
|
||||
else {
|
||||
builder.move(branch.condition)
|
||||
}
|
||||
}
|
||||
return whenExit
|
||||
}
|
||||
|
||||
override fun visitElement(element: IrElement, data: Boolean): IrElement? {
|
||||
TODO("not implemented")
|
||||
}
|
||||
|
||||
+2
-2
@@ -36,9 +36,9 @@ class GeneralConnectorBuilder(private val element: IrElement) : BlockConnectorBu
|
||||
}
|
||||
|
||||
override fun build() = when {
|
||||
next.size == 1 -> JoinBlockConnector(previous.toReadOnlyList(), element, next.single())
|
||||
next.size <= 1 -> JoinBlockConnector(previous.toReadOnlyList(), element, next.firstOrNull())
|
||||
previous.size == 1 -> SplitBlockConnector(previous.single(), element, next.toReadOnlyList())
|
||||
else -> throw AssertionError("Connector should have either exactly one previous block or exactly one next block, " +
|
||||
else -> throw AssertionError("Connector should have either exactly one previous block or no more than one next block, " +
|
||||
"actual previous = ${previous.size}, next = ${next.size}")
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -23,8 +23,8 @@ import org.jetbrains.kotlin.ir2cfg.graph.BlockConnector
|
||||
class JoinBlockConnector(
|
||||
override val previousBlocks: List<BasicBlock>,
|
||||
override val element: IrElement,
|
||||
next: BasicBlock
|
||||
next: BasicBlock?
|
||||
) : BlockConnector {
|
||||
|
||||
override val nextBlocks = listOf(next)
|
||||
override val nextBlocks = listOfNotNull(next)
|
||||
}
|
||||
Reference in New Issue
Block a user