Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/BlockCfg.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fun Program.toBlockCfg(): BlockCfg {
is Assign -> BlockAssign(node.target, node.expr)
is Return -> BlockReturn(node.expr)
is If -> BlockIf(node.condition)
is CustomEts -> BlockCustomEts(node.toEts)
else -> error("Unexpected node: $node")
}
nodeToStmt[node] = stmt
Expand Down
7 changes: 7 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/BlockStmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.jacodb.ets.dsl

import org.jacodb.ets.model.EtsStmt
import org.jacodb.ets.model.EtsStmtLocation

sealed interface BlockStmt

data object BlockNop : BlockStmt
Expand All @@ -32,3 +35,7 @@ data class BlockReturn(
data class BlockIf(
val condition: Expr,
) : BlockStmt

class BlockCustomEts(
val toEts: (EtsStmtLocation) -> EtsStmt,
) : BlockStmt
37 changes: 37 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/DSL.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,68 @@

package org.jacodb.ets.dsl

import org.jacodb.ets.model.EtsAssignStmt
import org.jacodb.ets.model.EtsClassSignature
import org.jacodb.ets.model.EtsFieldSignature
import org.jacodb.ets.model.EtsInstanceFieldRef
import org.jacodb.ets.model.EtsLocal
import org.jacodb.ets.model.EtsNumberConstant
import org.jacodb.ets.model.EtsUnknownType
import org.jacodb.ets.utils.view

private fun main() {
val prog = program {
// i := arg(0)
assign(local("i"), param(0))

// if (i > 10)
ifStmt(gt(local("i"), const(10))) {
ifStmt(eq(local("i"), const(42))) {
// if (i == 42) {
// return i
ret(local("i"))
}.elseIf(eq(local("i"), const(20))) {
// } else if (i == 20) {
// return i
ret(local("i"))
}.`else` {
// } else {
// i := 10
assign(local("i"), const(10))
}
nop()
}

label("loop")
ifStmt(gt(local("i"), const(0))) {
// if (i > 0) {
// i := i - 1
// goto @loop
assign(local("i"), sub(local("i"), const(1)))
goto("loop")
}.`else` {
// } else {
// return i
ret(local("i"))
}

// x.foo := 35
customStmt { loc ->
EtsAssignStmt(
location = loc,
lhv = EtsInstanceFieldRef(
instance = EtsLocal("x", EtsUnknownType),
field = EtsFieldSignature(
enclosingClass = EtsClassSignature.UNKNOWN,
name = "foo",
type = EtsUnknownType,
),
type = EtsUnknownType
),
rhv = EtsNumberConstant(35.0)
)
}

ret(const(100)) // unreachable
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ fun BlockCfg.linearize(): LinearizedCfg {
is BlockIf -> {
processed += IfStmt(loc++, stmt.condition)
}

is BlockCustomEts -> {
processed += CustomEtsStmt(loc++, stmt.toEts)
}
}
}
if (processed.isEmpty()) {
Expand All @@ -60,7 +64,7 @@ fun BlockCfg.linearize(): LinearizedCfg {

for ((id, statements) in linearizedBlocks) {
for ((stmt, next) in statements.zipWithNext()) {
check(next !is ReturnStmt) { "Return statement in the middle of the block: $next" }
check(stmt !is ReturnStmt) { "Return statement in the middle of the block: $stmt" }
Comment thread
Lipen marked this conversation as resolved.
check(stmt !is IfStmt) { "If statement in the middle of the block: $stmt" }
successors[stmt.location] = listOf(next.location)
}
Expand Down
7 changes: 7 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Node.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.jacodb.ets.dsl

import org.jacodb.ets.model.EtsStmt
import org.jacodb.ets.model.EtsStmtLocation

sealed interface Node

data object Nop : Node
Expand All @@ -42,3 +45,7 @@ data class Label(
data class Goto(
val targetLabel: String,
) : Node

class CustomEts(
val toEts: (EtsStmtLocation) -> EtsStmt,
) : Node
2 changes: 2 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Program.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ data class Program(

is Label -> line("label ${node.name}")
is Goto -> line("goto ${node.targetLabel}")

is CustomEts -> line("???")
Comment thread
Lipen marked this conversation as resolved.
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/ProgramBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.jacodb.ets.dsl

import org.jacodb.ets.model.EtsEntity
import org.jacodb.ets.model.EtsStmt
import org.jacodb.ets.model.EtsStmtLocation

interface ProgramBuilder {
fun nop()
Expand All @@ -27,6 +29,8 @@ interface ProgramBuilder {
fun label(name: String)
fun goto(label: String)
fun ifStmt(condition: Expr, block: ProgramBuilder.() -> Unit): IfBuilder

fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt)
}

fun ProgramBuilder.local(name: String) = Local(name)
Expand Down Expand Up @@ -106,6 +110,10 @@ class ProgramBuilderImpl : ProgramBuilder {
_nodes += If(condition, builder.thenNodes, builder.elseNodes)
return builder
}

override fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt) {
_nodes += CustomEts(toEts)
}
}

class IfBuilder : ProgramBuilder {
Expand Down Expand Up @@ -136,4 +144,6 @@ class IfBuilder : ProgramBuilder {
override fun ret(expr: Expr) = thenBuilder.ret(expr)
override fun label(name: String) = thenBuilder.label(name)
override fun goto(label: String) = thenBuilder.goto(label)

override fun customStmt(toEts: (EtsStmtLocation) -> EtsStmt) = thenBuilder.customStmt(toEts)
}
8 changes: 8 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/Stmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.jacodb.ets.dsl

import org.jacodb.ets.model.EtsStmt
import org.jacodb.ets.model.EtsStmtLocation

typealias StmtLocation = Int

sealed interface Stmt {
Expand All @@ -41,3 +44,8 @@ data class IfStmt(
override val location: StmtLocation,
val condition: Expr,
) : Stmt

class CustomEtsStmt(
override val location: StmtLocation,
val toEts: (EtsStmtLocation) -> EtsStmt,
) : Stmt
4 changes: 4 additions & 0 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dsl/ToDot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.jacodb.ets.dsl

import org.jacodb.ets.utils.toDotLabel
import java.util.IdentityHashMap

private fun Node.toDotLabel() = when (this) {
Expand All @@ -25,6 +26,7 @@ private fun Node.toDotLabel() = when (this) {
is If -> "if ($condition)"
is Label -> "label $name"
is Goto -> "goto $targetLabel"
is CustomEts -> "???"
Comment thread
Lipen marked this conversation as resolved.
}

fun Program.toDot(): String {
Expand Down Expand Up @@ -102,6 +104,7 @@ private fun BlockStmt.toDotLabel() = when (this) {
is BlockReturn -> "return $expr"
is BlockIf -> "if ($condition)"
is BlockNop -> "nop"
is BlockCustomEts -> "???"
Comment thread
Lipen marked this conversation as resolved.
}

fun BlockCfg.toDot(): String {
Expand Down Expand Up @@ -138,6 +141,7 @@ private fun Stmt.toDotLabel() = when (this) {
is AssignStmt -> "$target := $expr"
is ReturnStmt -> "return $expr"
is IfStmt -> "if ($condition)"
is CustomEtsStmt -> "???"
Comment thread
Lipen marked this conversation as resolved.
}

fun LinearizedCfg.toDot(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.jacodb.ets.dsl.BinaryOperator
import org.jacodb.ets.dsl.Block
import org.jacodb.ets.dsl.BlockAssign
import org.jacodb.ets.dsl.BlockCfg
import org.jacodb.ets.dsl.BlockCustomEts
import org.jacodb.ets.dsl.BlockIf
import org.jacodb.ets.dsl.BlockNop
import org.jacodb.ets.dsl.BlockReturn
Expand Down Expand Up @@ -301,6 +302,10 @@ class EtsBlockCfgBuilder(
condition = condition,
)
}

is BlockCustomEts -> {
etsStatements += stmt.toEts(stub)
}
}
}

Expand Down
Loading