Skip to content

Commit f51290d

Browse files
a test entry that compile btree cases
1 parent b6056d8 commit f51290d

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

src/main/scala/wasm/StagedConcolicMiniWasm.scala

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ trait StagedWasmEvaluator extends SAIOps {
152152
()
153153
}
154154

155+
def evalSymbolic(ty: ValueType,
156+
rest: List[Instr],
157+
kont: Context => Rep[Cont[Unit]],
158+
mkont: Rep[MCont[Unit]],
159+
trail: Trail[Unit])(implicit ctx: Context) = {
160+
Stack.popC(ty)
161+
val id = Stack.popS(ty)
162+
val symVal = id.makeSymbolic(ty)
163+
val num = SymEnv.read(symVal.s)
164+
Stack.pushC(StagedConcreteNum(ty, num))
165+
Stack.pushS(symVal)
166+
val newCtx = ctx.pop()._2.push(ty)
167+
eval(rest, kont, mkont, trail)(newCtx)
168+
}
169+
155170
def eval(insts: List[Instr],
156171
kont: Context => Rep[Cont[Unit]],
157172
mkont: Rep[MCont[Unit]],
@@ -174,15 +189,7 @@ trait StagedWasmEvaluator extends SAIOps {
174189
Stack.pushS(toStagedSymbolicNum(num))
175190
val newCtx = ctx.push(num.tipe(module))
176191
eval(rest, kont, mkont, trail)(newCtx)
177-
case Symbolic(ty) =>
178-
Stack.popC(ty)
179-
val id = Stack.popS(ty)
180-
val symVal = id.makeSymbolic(ty)
181-
val num = SymEnv.read(symVal.s)
182-
Stack.pushC(StagedConcreteNum(ty, num))
183-
Stack.pushS(symVal)
184-
val newCtx = ctx.pop()._2.push(ty)
185-
eval(rest, kont, mkont, trail)(newCtx)
192+
case Symbolic(ty) => evalSymbolic(ty, rest, kont, mkont, trail)(ctx)
186193
case LocalGet(i) =>
187194
Stack.pushC(Frames.getC(i))
188195
Stack.pushS(Frames.getS(i))
@@ -530,7 +537,15 @@ trait StagedWasmEvaluator extends SAIOps {
530537
val s = Stack.popS(ty)
531538
runtimeAssert(v.toInt != 0)
532539
eval(rest, kont, mkont, trail)(newCtx)
533-
case Import(_, _, _) => throw new Exception(s"Unknown import at $funcIndex")
540+
case Import("i32", "symbolic", _) =>
541+
evalSymbolic(NumType(I32Type), rest, kont, mkont, trail)(ctx)
542+
case Import("i32", "sym_assume", _) =>
543+
// TODO: implement sym_assume
544+
eval(rest, kont, mkont, trail)(ctx.pop()._2)
545+
case Import("i32", "sym_assert", _) =>
546+
// TODO: implement sym_assert
547+
eval(rest, kont, mkont, trail)(ctx.pop()._2)
548+
case Import(m, f, _) => throw new Exception(s"Unknown import $m.$f at $funcIndex")
534549
case _ => throw new Exception(s"Definition at $funcIndex is not callable")
535550
}
536551
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package gensym.wasm
2+
3+
import org.scalatest.FunSuite
4+
5+
import lms.core.stub.Adapter
6+
7+
import gensym.wasm.miniwasm.{ModuleInstance}
8+
import gensym.wasm.parser._
9+
import gensym.wasm.stagedconcolicminiwasm._
10+
11+
// This 'test file' is not intended to test functionality, but to generate compiled code for btree benchmarks
12+
class TestBenchmarkBtree extends FunSuite {
13+
def compileToCpp(filename: String,
14+
main: Option[String] = None) = {
15+
import sys.process._
16+
17+
println(s"Compiling $filename to C++")
18+
val moduleInst = ModuleInstance(Parser.parseFile(filename))
19+
val cppFile = s"$filename.cpp"
20+
val generated = WasmToCppCompiler.compile(moduleInst, main, false)
21+
22+
val code = generated.source
23+
val writer = new java.io.PrintWriter(new java.io.File(cppFile))
24+
try {
25+
writer.write(code)
26+
} finally {
27+
writer.close()
28+
}
29+
}
30+
31+
def compileDirToCpp(dir: String,
32+
main: Option[String] = None) = {
33+
import java.io.File
34+
val d = new File(dir)
35+
d.listFiles().filter(_.getName.endsWith(".wat")).foreach { file =>
36+
compileToCpp(file.getAbsolutePath, main)
37+
}
38+
}
39+
40+
// only test concrete execution and its result
41+
def testFileConcreteCpp(filename: String, main: Option[String] = None, expect: Option[List[Float]] = None) = {
42+
val moduleInst = ModuleInstance(Parser.parseFile(filename))
43+
val cppFile = s"$filename.cpp"
44+
val exe = s"$cppFile.exe"
45+
WasmToCppCompiler.compileToExe(moduleInst, main, cppFile, exe, true, optimizeLevel=0, "NO_INFO", "RUN_ONCE", "USE_IMM")
46+
47+
import sys.process._
48+
val result = s"./$exe".!!
49+
println(result)
50+
51+
expect.map(vs => {
52+
val stackValues = result
53+
.split("Stack contents: \n")(1)
54+
.split("\n")
55+
.map(_.toFloat)
56+
.toList
57+
assert(vs == stackValues)
58+
})
59+
}
60+
61+
test("compile-btree-benchmarks") { compileDirToCpp("./benchmarks/pldi2026/btree/", Some("main")) }
62+
63+
}

0 commit comments

Comments
 (0)