Skip to content

Commit

Permalink
fibonacci (#48)
Browse files Browse the repository at this point in the history
* add test file

* support br_table instruction (#49)

* add java options to Test (#50)

---------

Co-authored-by: butterunderflow <[email protected]>
  • Loading branch information
Kraks and butterunderflow authored Oct 5, 2024
1 parent f0e2046 commit a8781d4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
12 changes: 12 additions & 0 deletions benchmarks/wasm/fib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[no_mangle]
#[inline(never)]
fn fibonacci(n: i32) -> i32 {
if n == 0 { 0 }
else if n == 1 { 1 }
else { fibonacci(n - 1) + fibonacci(n - 2) }
}

#[no_mangle]
fn real_main() -> i32 {
fibonacci(12)
}
59 changes: 59 additions & 0 deletions benchmarks/wasm/fib.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func (result i32)))
(func (;0;) (type 0) (param i32) (result i32)
(local i32)
i32.const 0
local.set 1
local.get 0
local.set 0
block ;; label = @1
loop ;; label = @2
local.get 1
local.set 1
block ;; label = @3
block ;; label = @4
block ;; label = @5
local.get 0
local.tee 0
br_table 0 (;@5;) 1 (;@4;) 2 (;@3;)
end
local.get 0
local.set 0
br 3 (;@1;)
end
i32.const 1
local.set 0
br 2 (;@1;)
end
local.get 1
local.get 0
i32.const -1
i32.add
call 0
i32.add
local.set 1
local.get 0
i32.const -2
i32.add
local.set 0
br 0 (;@2;)
end
end
local.get 1
local.get 0
i32.add)
(func (;1;) (type 1) (result i32)
i32.const 12
call 0)
(start 1)
(table (;0;) 1 1 funcref)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "fibonacci" (func 0))
(export "real_main" (func 1))
(export "__data_end" (global 1))
(export "__heap_base" (global 2)))
4 changes: 3 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ autoCompilerPlugins := true

//https://www.scala-sbt.org/release/docs/Running-Project-Code.html
fork := true
run / javaOptions ++= Seq(
val commonJavaOptions = Seq(
"-Xms4G",
"-Xmx32G",
"-Xss1024M",
"-XX:MaxMetaspaceSize=8G",
"-XX:ReservedCodeCacheSize=2048M"
)
run / javaOptions ++= commonJavaOptions
Test / javaOptions ++= commonJavaOptions

val paradiseVersion = "2.1.0"
addCompilerPlugin("org.scalamacros" % "paradise" % paradiseVersion cross CrossVersion.full)
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/wasm/MiniWasm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ object Evaluator {
val I32V(cond) :: newStack = stack
if (cond != 0) trail(label)(newStack)
else eval(rest, newStack, frame, kont, trail, ret)
case BrTable(labels, default) =>
val I32V(cond) :: newStack = stack
val goto = if (cond < labels.length) labels(cond) else default
trail(goto)(newStack)
case Return => trail(ret)(stack)
case Call(f) if frame.module.funcs(f).isInstanceOf[FuncDef] =>
val FuncDef(_, FuncBodyDef(ty, _, locals, body)) = frame.module.funcs(f)
Expand Down
2 changes: 2 additions & 0 deletions src/test/scala/genwasym/TestEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class TestEval extends FunSuite {
test("even-odd") { testFile("./benchmarks/wasm/even_odd.wat", None, Some(1)) }
test("return") { testFile("./benchmarks/wasm/return.wat", None, None) }

test("fib") { testFile("./benchmarks/wasm/fib.wat", None, Some(144)) }

// Parser works, but the memory issue remains
// test("btree") { testFile("./benchmarks/wasm/btree/2o1u-no-label-for-real.wat") }

Expand Down

0 comments on commit a8781d4

Please sign in to comment.