Skip to content

Commit

Permalink
test for block parent bug fix, change to DSL for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
l-kent committed Jun 5, 2024
1 parent 0e429af commit 6f3231c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
11 changes: 6 additions & 5 deletions src/main/scala/ir/dsl/DSL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ case class EventuallyIndirectCall(target: Variable, fallthrough: Option[DelayNam

case class EventuallyCall(target: DelayNameResolve, fallthrough: Option[DelayNameResolve]) extends EventuallyJump {
override def resolve(p: Program): DirectCall = {
val t = target.resolveProc(p).get
val t = target.resolveProc(p) match {
case Some(x) => x
case None => throw Exception("can't resolve proc " + p)
}
val ft = fallthrough.flatMap(_.resolveBlock(p))
DirectCall(t, ft)
}
Expand All @@ -70,11 +73,9 @@ def goto(targets: List[String]): EventuallyGoto = {
EventuallyGoto(targets.map(p => DelayNameResolve(p)))
}

def indirectCall(tgt: String, fallthrough: Option[String]): EventuallyCall = EventuallyCall(DelayNameResolve(tgt), fallthrough.map(x => DelayNameResolve(x)))
def directCall(tgt: String, fallthrough: Option[String]): EventuallyCall = EventuallyCall(DelayNameResolve(tgt), fallthrough.map(x => DelayNameResolve(x)))

def call(tgt: String, fallthrough: Option[String]): EventuallyCall = EventuallyCall(DelayNameResolve(tgt), fallthrough.map(x => DelayNameResolve(x)))

def call(tgt: Variable, fallthrough: Option[String]): EventuallyIndirectCall = EventuallyIndirectCall(tgt, fallthrough.map(x => DelayNameResolve(x)))
def indirectCall(tgt: Variable, fallthrough: Option[String]): EventuallyIndirectCall = EventuallyIndirectCall(tgt, fallthrough.map(x => DelayNameResolve(x)))
// def directcall(tgt: String) = EventuallyCall(DelayNameResolve(tgt), None)


Expand Down
26 changes: 13 additions & 13 deletions src/test/scala/LiveVarsAnalysisTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
block("first_call",
r0ConstantAssign,
r1ConstantAssign,
call("callee1", Some("second_call"))
directCall("callee1", Some("second_call"))
),
block("second_call",
call("callee2", Some("returnBlock"))
directCall("callee2", Some("returnBlock"))
),
block("returnBlock",
ret
Expand Down Expand Up @@ -69,10 +69,10 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
block("first_call",
r0ConstantAssign,
r1ConstantAssign,
call("callee1", Some("second_call"))
directCall("callee1", Some("second_call"))
),
block("second_call",
call("callee2", Some("returnBlock"))
directCall("callee2", Some("returnBlock"))
),
block("returnBlock",
ret
Expand Down Expand Up @@ -104,10 +104,10 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
var program = prog(
proc("main",
block("main_first_call",
call("wrapper1", Some("main_second_call"))
directCall("wrapper1", Some("main_second_call"))
),
block("main_second_call",
call("wrapper2", Some("main_return"))
directCall("wrapper2", Some("main_return"))
),
block("main_return", ret)
),
Expand All @@ -117,19 +117,19 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
proc("wrapper1",
block("wrapper1_first_call",
LocalAssign(R1, constant1),
call("callee", Some("wrapper1_second_call"))
directCall("callee", Some("wrapper1_second_call"))
),
block("wrapper1_second_call",
call("callee2", Some("wrapper1_return"))),
directCall("callee2", Some("wrapper1_return"))),
block("wrapper1_return", ret)
),
proc("wrapper2",
block("wrapper2_first_call",
LocalAssign(R2, constant1),
call("callee", Some("wrapper2_second_call"))
directCall("callee", Some("wrapper2_second_call"))
),
block("wrapper2_second_call",
call("callee3", Some("wrapper2_return"))),
directCall("callee3", Some("wrapper2_return"))),
block("wrapper2_return", ret)
)
)
Expand All @@ -148,7 +148,7 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
var program = prog(
proc("main",
block("lmain",
call("killer", Some("aftercall"))
directCall("killer", Some("aftercall"))
),
block("aftercall",
LocalAssign(R0, R1),
Expand Down Expand Up @@ -212,7 +212,7 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
block(
"lmain",
LocalAssign(R0, R1),
call("main", Some("return"))
directCall("main", Some("return"))
),
block("return",
LocalAssign(R0, R2),
Expand Down Expand Up @@ -240,7 +240,7 @@ class LiveVarsAnalysisTests extends AnyFunSuite, TestUtil {
),
block(
"recursion",
call("main", Some("assign"))
directCall("main", Some("assign"))
),
block("assign",
LocalAssign(R0, R2),
Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/PointsToTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class PointsToTest extends AnyFunSuite with OneInstancePerTest with BeforeAndAft
goto("0x1")
),
block("0x1",
call("p2", Some("returntarget"))
directCall("p2", Some("returntarget"))
),
block("returntarget",
ret
Expand Down Expand Up @@ -217,7 +217,7 @@ class PointsToTest extends AnyFunSuite with OneInstancePerTest with BeforeAndAft
goto("0x1")
),
block("0x1",
call("p2", Some("returntarget"))
directCall("p2", Some("returntarget"))
),
block("returntarget",
ret
Expand All @@ -227,7 +227,7 @@ class PointsToTest extends AnyFunSuite with OneInstancePerTest with BeforeAndAft
block("l_foo",
LocalAssign(getRegister("R0"), MemoryLoad(mem, BinaryExpr(BVADD, getRegister("R31"), bv64(6)), LittleEndian, 64)),
LocalAssign(getRegister("R1"), BinaryExpr(BVADD, getRegister("R31"), bv64(10))),
call("p2", Some("l_foo_1"))
directCall("p2", Some("l_foo_1"))
),
block("l_foo_1",
ret,
Expand Down
44 changes: 40 additions & 4 deletions src/test/scala/ir/IRTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class IRTest extends AnyFunSuite {
),
block("l_main_1",
LocalAssign(R0, bv64(22)),
call("p2", Some("returntarget"))
directCall("p2", Some("returntarget"))
),
block("returntarget",
ret
Expand Down Expand Up @@ -246,7 +246,7 @@ class IRTest extends AnyFunSuite {
LocalAssign(R0, bv64(22)),
LocalAssign(R0, bv64(22)),
LocalAssign(R0, bv64(22)),
call("main", None)
directCall("main", None)
).resolve(p)
val b2 = block("newblock1",
LocalAssign(R0, bv64(22)),
Expand All @@ -271,7 +271,7 @@ class IRTest extends AnyFunSuite {
assert(called.incomingCalls().isEmpty)
val b3 = block("newblock3",
LocalAssign(R0, bv64(22)),
call("called", None)
directCall("called", None)
).resolve(p)

assert(b3.calls.toSet == Set(p.procs("called")))
Expand Down Expand Up @@ -333,7 +333,7 @@ class IRTest extends AnyFunSuite {
proc("main",
block("l_main",
LocalAssign(R0, bv64(10)),
call("p1", Some("returntarget"))
directCall("p1", Some("returntarget"))
),
block("returntarget",
ret
Expand Down Expand Up @@ -365,5 +365,41 @@ class IRTest extends AnyFunSuite {

}

test("replace jump") {
val p = prog(
proc("p1",
block("b1",
LocalAssign(R0, bv64(10)),
ret
)
),
proc("main",
block("l_main",
LocalAssign(R0, bv64(10)),
indirectCall(R1, Some("returntarget"))
),
block("block2",
directCall("p1", Some("returntarget"))
),
block("returntarget",
ret
)
),
)

val main = p.blocks("l_main")
val p1 = p.procs("p1")
val block2 = p.blocks("block2")

val oldJump = main.jump
val newJump = block2.jump

main.replaceJump(newJump)

assert(newJump.parent == main)
assert(block2.jump.isInstanceOf[GoTo])
assert(block2.jump.asInstanceOf[GoTo].targets.isEmpty)
}

}

0 comments on commit 6f3231c

Please sign in to comment.