Skip to content

Commit a7b5460

Browse files
committed
Fixed bug with break and exit nodes
1 parent de8b218 commit a7b5460

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

  • src/main/kotlin/deep/decaf/low

src/main/kotlin/deep/decaf/low/CFG.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,27 @@ fun constructCFG(statement: IRStatement, info: CFGCreationInfo): CFG {
328328

329329
// Re-wire the break and continue properly
330330
val done = mutableMapOf<String, Boolean>()
331+
var blockDepth = 0
331332
fun dfs(node: CFGNode) {
332333
val isDone = done[node.uuid] ?: false
333334
if (!isDone) {
335+
if (node is BlockEntryNode) {
336+
blockDepth++
337+
}
338+
if (node is BlockExitNode) {
339+
blockDepth--
340+
}
334341
if (node is BreakNode && node.loopId == info.loopId()) {
335342
val next = node.next
336-
node.next = exitNoOp
337-
exitNoOp.prevs.add(node)
343+
val exits = Array(blockDepth) { BlockExitNode(null, null) }
344+
for (i in 0 until blockDepth - 1) {
345+
exits[i].next = exits[i + 1]
346+
exits[i + 1].prev = exits[i]
347+
}
348+
node.next = exits[0]
349+
exits[0].prev = node
350+
exits[blockDepth - 1].next = exitNoOp
351+
exitNoOp.prevs.add(exits[blockDepth - 1])
338352
if (next != null) {
339353
if (next is SingleInput) {
340354
next.prev = null
@@ -345,8 +359,15 @@ fun constructCFG(statement: IRStatement, info: CFGCreationInfo): CFG {
345359
}
346360
if (node is ContinueNode && node.loopId == info.loopId()) {
347361
val next = node.next
348-
node.next = noOp
349-
noOp.prevs.add(node)
362+
val exits = Array(blockDepth) { BlockExitNode(null, null) }
363+
for (i in 0 until blockDepth - 1) {
364+
exits[i].next = exits[i + 1]
365+
exits[i + 1].prev = exits[i]
366+
}
367+
node.next = exits[0]
368+
exits[0].prev = node
369+
exits[blockDepth - 1].next = noOp
370+
noOp.prevs.add(exits[blockDepth - 1])
350371
if (next != null) {
351372
if (next is SingleInput) {
352373
next.prev = null

0 commit comments

Comments
 (0)