Skip to content

Commit

Permalink
Reworked execution of branches
Browse files Browse the repository at this point in the history
  • Loading branch information
BrendoCosta committed Oct 17, 2024
1 parent 5c6293b commit d2f4345
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 185 deletions.
326 changes: 164 additions & 162 deletions src/gwr/execution/machine.gleam

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/gwr/execution/stack.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ pub type StackEntry
ValueEntry(runtime.Value)
LabelEntry(runtime.Label)
ActivationEntry(runtime.Frame)
// @NOTE: the "Jump" entry is not part of the spec and
// is intended for signaling down stack that a jump has
// occurred.
Jump(List(StackEntry))
}

pub fn create() -> Stack
Expand Down Expand Up @@ -195,6 +191,15 @@ pub fn get_current_frame(from stack: Stack) -> Result(runtime.Frame, Nil)
}
}

pub fn get_current_label(from stack: Stack) -> Result(runtime.Label, Nil)
{
pop_while(from: stack, with: fn (entry) { !is_activation_frame(entry) }).1
|> list.filter(is_label)
|> list.map(to_label)
|> result.values
|> list.first
}

pub fn replace_current_frame(from stack: Stack, with new_frame: runtime.Frame) -> Result(Stack, Nil)
{
let #(stack, upper_entries) = pop_while(from: stack, with: fn (entry) { !is_activation_frame(entry) })
Expand Down
29 changes: 29 additions & 0 deletions test/assets/rust/misc/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustc --crate-type cdylib --target wasm32-unknown-unknown -C debuginfo=none -C panic=abort -C strip=symbols -C opt-level=3 ./test/assets/rust/misc/fibonacci.rs -o ./test/assets/rust/misc/fibonacci.wasm

#![no_std]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> !
{
loop {}
}

#[no_mangle]
pub extern fn fibonacci(value: i32) -> i32
{
if value <= 0
{
return 0;
}
else
{
if value == 1
{
return 1;
}
else
{
return fibonacci(value - 1) + fibonacci(value - 2);
}
}
}
Binary file added test/assets/rust/misc/fibonacci.wasm
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;; wat2wasm ./test/assets/control/fibonacci.wat -o ./test/assets/control/fibonacci.wasm
;; wat2wasm ./test/assets/wat/misc/fibonacci.wat -o ./test/assets/wat/misc/fibonacci.wasm
(module
(func $fibonacci (export "fibonacci") (param $value i32) (result i32)
(if (result i32)
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions test/assets/sum.wat → test/assets/wat/misc/sum.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
;; sum.wat

;; wat2wasm ./test/assets/wat/misc/sum.wat -o ./test/assets/wat/misc/sum.wasm
(module
(type $t0 (func (param i32 i32) (result i32)))
(func $sum (export "sum") (type $t0) (param $p0 i32) (param $p1 i32) (result i32)
Expand Down
40 changes: 24 additions & 16 deletions test/gwr/execution/execution_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,73 @@ pub fn main()
gleeunit.main()
}

pub fn sum_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/sum.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "sum", [runtime.Integer32(4), runtime.Integer32(2)]) |> should.be_ok
result |> should.equal([runtime.Integer32(6)])
}

pub fn block_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/block.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/block.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "block_test", [runtime.Integer32(2), runtime.Integer32(3)]) |> should.be_ok
result |> should.equal([runtime.Integer32(7)])
}

pub fn if_else___if_scope___test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/if_else.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/if_else.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "if_else_test", [runtime.Integer32(0)]) |> should.be_ok
result |> should.equal([runtime.Integer32(110)])
}

pub fn if_else___else_scope___test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/if_else.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/if_else.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "if_else_test", [runtime.Integer32(1)]) |> should.be_ok
result |> should.equal([runtime.Integer32(120)])
}

pub fn loop___test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/loop.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/loop.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "loop_test", []) |> should.be_ok
result |> should.equal([runtime.Integer32(10)])
}

pub fn call_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/call.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/call.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "call_test", [runtime.Integer32(5), runtime.Integer32(2)]) |> should.be_ok
result |> should.equal([runtime.Integer32(7)])
}

pub fn recursion_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/recursion.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/wat/control/recursion.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "recursion_test", [runtime.Integer32(3), runtime.Integer32(0)]) |> should.be_ok
result |> should.equal([runtime.Integer32(6)])
}

pub fn fibonacci_test()
pub fn wat_misc_fibonacci_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/wat/misc/fibonacci.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "fibonacci", [runtime.Integer32(12)]) |> should.be_ok
result |> should.equal([runtime.Integer32(144)])
}

pub fn wat_misc_sum_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/wat/misc/sum.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "sum", [runtime.Integer32(4), runtime.Integer32(2)]) |> should.be_ok
result |> should.equal([runtime.Integer32(6)])
}

pub fn rust_misc_fibonacci_test()
{
let module_data = simplifile.read_bits(from: "./test/assets/control/fibonacci.wasm") |> should.be_ok
let module_data = simplifile.read_bits(from: "./test/assets/rust/misc/fibonacci.wasm") |> should.be_ok
let instance = gwr.create(from: module_data) |> should.be_ok
let #(_, result) = gwr.call(instance, "fibonacci", [runtime.Integer32(12)]) |> should.be_ok
result |> should.equal([runtime.Integer32(144)])
Expand Down

0 comments on commit d2f4345

Please sign in to comment.