Skip to content

Commit 212d7c8

Browse files
committed
Support custom EtsStmt in CFG DSL
1 parent 0a50288 commit 212d7c8

10 files changed

Lines changed: 86 additions & 1 deletion

File tree

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/BlockCfg.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ fun Program.toBlockCfg(): BlockCfg {
9494
is Assign -> BlockAssign(node.target, node.expr)
9595
is Return -> BlockReturn(node.expr)
9696
is If -> BlockIf(node.condition)
97+
is CustomEts -> BlockCustomEts(node.toEts)
9798
else -> error("Unexpected node: $node")
9899
}
99100
nodeToStmt[node] = stmt

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/BlockStmt.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.jacodb.ets.dsl
1818

19+
import org.jacodb.ets.model.EtsStmt
20+
import org.jacodb.ets.model.EtsStmtLocation
21+
1922
sealed interface BlockStmt
2023

2124
data object BlockNop : BlockStmt
@@ -32,3 +35,7 @@ data class BlockReturn(
3235
data class BlockIf(
3336
val condition: Expr,
3437
) : BlockStmt
38+
39+
class BlockCustomEts(
40+
val toEts: (EtsStmtLocation) -> EtsStmt,
41+
) : BlockStmt

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/DSL.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,68 @@
1616

1717
package org.jacodb.ets.dsl
1818

19+
import org.jacodb.ets.model.EtsAssignStmt
20+
import org.jacodb.ets.model.EtsClassSignature
21+
import org.jacodb.ets.model.EtsFieldSignature
22+
import org.jacodb.ets.model.EtsInstanceFieldRef
23+
import org.jacodb.ets.model.EtsLocal
24+
import org.jacodb.ets.model.EtsNumberConstant
25+
import org.jacodb.ets.model.EtsUnknownType
1926
import org.jacodb.ets.utils.view
2027

2128
private fun main() {
2229
val prog = program {
30+
// i := arg(0)
2331
assign(local("i"), param(0))
2432

33+
// if (i > 10)
2534
ifStmt(gt(local("i"), const(10))) {
2635
ifStmt(eq(local("i"), const(42))) {
36+
// if (i == 42) {
37+
// return i
2738
ret(local("i"))
2839
}.elseIf(eq(local("i"), const(20))) {
40+
// } else if (i == 20) {
41+
// return i
2942
ret(local("i"))
3043
}.`else` {
44+
// } else {
45+
// i := 10
3146
assign(local("i"), const(10))
3247
}
3348
nop()
3449
}
3550

3651
label("loop")
3752
ifStmt(gt(local("i"), const(0))) {
53+
// if (i > 0) {
54+
// i := i - 1
55+
// goto @loop
3856
assign(local("i"), sub(local("i"), const(1)))
3957
goto("loop")
4058
}.`else` {
59+
// } else {
60+
// return i
4161
ret(local("i"))
4262
}
4363

64+
// x.foo := 35
65+
customStmt { loc ->
66+
EtsAssignStmt(
67+
location = loc,
68+
lhv = EtsInstanceFieldRef(
69+
instance = EtsLocal("x", EtsUnknownType),
70+
field = EtsFieldSignature(
71+
enclosingClass = EtsClassSignature.UNKNOWN,
72+
name = "foo",
73+
type = EtsUnknownType,
74+
),
75+
type = EtsUnknownType
76+
),
77+
rhv = EtsNumberConstant(35.0)
78+
)
79+
}
80+
4481
ret(const(100)) // unreachable
4582
}
4683

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/LinearizedCfg.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ fun BlockCfg.linearize(): LinearizedCfg {
4646
is BlockIf -> {
4747
processed += IfStmt(loc++, stmt.condition)
4848
}
49+
50+
is BlockCustomEts -> {
51+
processed += CustomEtsStmt(loc++, stmt.toEts)
52+
}
4953
}
5054
}
5155
if (processed.isEmpty()) {
@@ -60,7 +64,7 @@ fun BlockCfg.linearize(): LinearizedCfg {
6064

6165
for ((id, statements) in linearizedBlocks) {
6266
for ((stmt, next) in statements.zipWithNext()) {
63-
check(next !is ReturnStmt) { "Return statement in the middle of the block: $next" }
67+
check(stmt !is ReturnStmt) { "Return statement in the middle of the block: $stmt" }
6468
check(stmt !is IfStmt) { "If statement in the middle of the block: $stmt" }
6569
successors[stmt.location] = listOf(next.location)
6670
}

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Node.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.jacodb.ets.dsl
1818

19+
import org.jacodb.ets.model.EtsStmt
20+
import org.jacodb.ets.model.EtsStmtLocation
21+
1922
sealed interface Node
2023

2124
data object Nop : Node
@@ -42,3 +45,7 @@ data class Label(
4245
data class Goto(
4346
val targetLabel: String,
4447
) : Node
48+
49+
class CustomEts(
50+
val toEts: (EtsStmtLocation) -> EtsStmt,
51+
) : Node

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Program.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ data class Program(
4444

4545
is Label -> line("label ${node.name}")
4646
is Goto -> line("goto ${node.targetLabel}")
47+
48+
is CustomEts -> line("???")
4749
}
4850
}
4951
}

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/ProgramBuilder.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.jacodb.ets.dsl
2020

2121
import org.jacodb.ets.model.EtsEntity
22+
import org.jacodb.ets.model.EtsStmt
23+
import org.jacodb.ets.model.EtsStmtLocation
2224

2325
interface ProgramBuilder {
2426
fun nop()
@@ -27,6 +29,8 @@ interface ProgramBuilder {
2729
fun label(name: String)
2830
fun goto(label: String)
2931
fun ifStmt(condition: Expr, block: ProgramBuilder.() -> Unit): IfBuilder
32+
33+
fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt)
3034
}
3135

3236
fun ProgramBuilder.local(name: String) = Local(name)
@@ -106,6 +110,10 @@ class ProgramBuilderImpl : ProgramBuilder {
106110
_nodes += If(condition, builder.thenNodes, builder.elseNodes)
107111
return builder
108112
}
113+
114+
override fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt) {
115+
_nodes += CustomEts(toEts)
116+
}
109117
}
110118

111119
class IfBuilder : ProgramBuilder {
@@ -136,4 +144,6 @@ class IfBuilder : ProgramBuilder {
136144
override fun ret(expr: Expr) = thenBuilder.ret(expr)
137145
override fun label(name: String) = thenBuilder.label(name)
138146
override fun goto(label: String) = thenBuilder.goto(label)
147+
148+
override fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt) = thenBuilder.customStmt(toEts)
139149
}

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Stmt.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.jacodb.ets.dsl
1818

19+
import org.jacodb.ets.model.EtsStmt
20+
import org.jacodb.ets.model.EtsStmtLocation
21+
1922
typealias StmtLocation = Int
2023

2124
sealed interface Stmt {
@@ -41,3 +44,8 @@ data class IfStmt(
4144
override val location: StmtLocation,
4245
val condition: Expr,
4346
) : Stmt
47+
48+
class CustomEtsStmt(
49+
override val location: StmtLocation,
50+
val toEts: (EtsStmtLocation) -> EtsStmt,
51+
) : Stmt

jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/ToDot.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.jacodb.ets.dsl
1818

19+
import org.jacodb.ets.utils.toDotLabel
1920
import java.util.IdentityHashMap
2021

2122
private fun Node.toDotLabel() = when (this) {
@@ -25,6 +26,7 @@ private fun Node.toDotLabel() = when (this) {
2526
is If -> "if ($condition)"
2627
is Label -> "label $name"
2728
is Goto -> "goto $targetLabel"
29+
is CustomEts -> "???"
2830
}
2931

3032
fun Program.toDot(): String {
@@ -102,6 +104,7 @@ private fun BlockStmt.toDotLabel() = when (this) {
102104
is BlockReturn -> "return $expr"
103105
is BlockIf -> "if ($condition)"
104106
is BlockNop -> "nop"
107+
is BlockCustomEts -> "???"
105108
}
106109

107110
fun BlockCfg.toDot(): String {
@@ -138,6 +141,7 @@ private fun Stmt.toDotLabel() = when (this) {
138141
is AssignStmt -> "$target := $expr"
139142
is ReturnStmt -> "return $expr"
140143
is IfStmt -> "if ($condition)"
144+
is CustomEtsStmt -> "???"
141145
}
142146

143147
fun LinearizedCfg.toDot(): String {

jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/BlockCfgBuilder.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.jacodb.ets.dsl.BinaryOperator
2121
import org.jacodb.ets.dsl.Block
2222
import org.jacodb.ets.dsl.BlockAssign
2323
import org.jacodb.ets.dsl.BlockCfg
24+
import org.jacodb.ets.dsl.BlockCustomEts
2425
import org.jacodb.ets.dsl.BlockIf
2526
import org.jacodb.ets.dsl.BlockNop
2627
import org.jacodb.ets.dsl.BlockReturn
@@ -301,6 +302,10 @@ class EtsBlockCfgBuilder(
301302
condition = condition,
302303
)
303304
}
305+
306+
is BlockCustomEts -> {
307+
etsStatements += stmt.toEts(stub)
308+
}
304309
}
305310
}
306311

0 commit comments

Comments
 (0)