diff --git a/lib/orb/i32/dsl.ex b/lib/orb/i32/dsl.ex index bdb5f49..7d14950 100644 --- a/lib/orb/i32/dsl.ex +++ b/lib/orb/i32/dsl.ex @@ -19,7 +19,6 @@ defmodule Orb.I32.DSL do def left * right do Orb.Numeric.Multiply.optimized(Orb.I32, left, right) - # Orb.I32.mul(left, right) end def _left == _right do diff --git a/lib/orb/numeric/add.ex b/lib/orb/numeric/add.ex index e9dd545..b46fff1 100644 --- a/lib/orb/numeric/add.ex +++ b/lib/orb/numeric/add.ex @@ -9,6 +9,9 @@ defmodule Orb.Numeric.Add do [MDN reference](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Addition) """ + # Perform at comptime. + def optimized(_type, a, b) when is_integer(a) and is_integer(b), do: a + b + # Add anything by zero is that same thing. def optimized(_type, a, 0), do: a def optimized(_type, 0, b), do: b # def optimized(type, a, b), do: {type.wasm_type(), :add, {a, b}} diff --git a/lib/orb/numeric/multiply.ex b/lib/orb/numeric/multiply.ex index cb2af65..c51b08a 100644 --- a/lib/orb/numeric/multiply.ex +++ b/lib/orb/numeric/multiply.ex @@ -17,6 +17,6 @@ defmodule Orb.Numeric.Multiply do # Multiply anything by one is that same thing. def optimized(_type, a, 1), do: a def optimized(_type, 1, b), do: b - # Finally, spit out the instruction. + # Finally, spit out the runtime instruction. def optimized(type, a, b), do: Orb.Instruction.new(type.wasm_type(), :mul, [a, b]) end diff --git a/lib/orb/numeric/sub.ex b/lib/orb/numeric/sub.ex index 24d0f29..ae08e47 100644 --- a/lib/orb/numeric/sub.ex +++ b/lib/orb/numeric/sub.ex @@ -9,6 +9,9 @@ defmodule Orb.Numeric.Subtract do [MDN reference](https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/Subtraction) """ + # Perform at comptime. + def optimized(_type, a, b) when is_integer(a) and is_integer(b), do: a - b + # Minus zero from anything is that same thing. def optimized(_type, a, 0), do: a def optimized(type, a, b), do: Orb.Instruction.new(type.wasm_type(), :sub, [a, b]) end diff --git a/test/examples/arena.exs b/test/examples/arena.exs index 161109c..6f2e29b 100644 --- a/test/examples/arena.exs +++ b/test/examples/arena.exs @@ -12,10 +12,18 @@ defmodule Examples.Arena do Orb.snippet Orb.S32, new_ptr: I32.UnsafePointer do new_ptr = Instruction.global_get(Orb.I32, offset_global_name) + + + + if new_ptr + byte_count > end_offset * Orb.Memory.page_byte_size() do unreachable!() end + + + + Instruction.global_set(Orb.I32, offset_global_name, new_ptr + byte_count) new_ptr diff --git a/test/operators_test.exs b/test/operators_test.exs index a111bd0..93f223b 100644 --- a/test/operators_test.exs +++ b/test/operators_test.exs @@ -8,8 +8,12 @@ defmodule OperatorsTest do use Orb wasm do - func add(), I32 do + func add(a: I32) do 1 + 2 + :drop + + a + 2 + :drop end func multiply(a: I32, b: I32) do @@ -48,8 +52,11 @@ defmodule OperatorsTest do assert to_wat(D1) == """ (module $D1 - (func $add (export "add") (result i32) - (i32.add (i32.const 1) (i32.const 2)) + (func $add (export "add") (param $a i32) + (i32.const 3) + drop + (i32.add (local.get $a) (i32.const 2)) + drop ) (func $multiply (export "multiply") (param $a i32) (param $b i32) (i32.mul (local.get $a) (local.get $b)) @@ -83,8 +90,12 @@ defmodule OperatorsTest do use Orb wasm U32 do - func add(), I32 do + func add(a: I32) do 1 + 2 + :drop + + a + 2 + :drop end func multiply(a: I32, b: I32) do @@ -118,8 +129,11 @@ defmodule OperatorsTest do assert to_wat(U1) == """ (module $U1 - (func $add (export "add") (result i32) - (i32.add (i32.const 1) (i32.const 2)) + (func $add (export "add") (param $a i32) + (i32.const 3) + drop + (i32.add (local.get $a) (i32.const 2)) + drop ) (func $multiply (export "multiply") (param $a i32) (param $b i32) (i32.mul (local.get $a) (local.get $b)) diff --git a/test/orb_test.exs b/test/orb_test.exs index dcd4cc6..9c4f750 100644 --- a/test/orb_test.exs +++ b/test/orb_test.exs @@ -802,7 +802,7 @@ defmodule OrbTest do (module $SnippetUser (memory (export "memory") 1) (func $answer (export "answer") (result i32) - (i32.load (i32.add (i32.const #{0x100}) (i32.const 4))) + (i32.load (i32.const #{0x104})) ) ) """