Skip to content

Commit

Permalink
Remove more wasm do usage
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Jan 29, 2024
1 parent 969bae2 commit 61ab351
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 111 deletions.
198 changes: 94 additions & 104 deletions test/examples/memory.exs
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,14 @@ defmodule Examples.Memory do
@wasm_use_bump_allocator true
end

Orb.wasm do
unquote(__MODULE__).funcp(:bump_alloc)
end

Orb.include(unquote(__MODULE__))
import unquote(__MODULE__)
end
end

defmacro export_alloc() do
quote do
Orb.wasm do
unquote(__MODULE__)._func(:alloc)
unquote(__MODULE__)._func(:free_all)
end
Orb.include(unquote(__MODULE__))
end
end

Expand All @@ -139,27 +133,25 @@ defmodule Examples.Memory do
bump_mark: 0
)

wasm do
funcp bump_alloc(size: I32), I32, [] do
# TODO: check if we have allocated too much
# and if so, either err or increase the available memory.
# TODO: Need better maths than this to round up to aligned memory?

push(@bump_offset) do
@bump_offset = I32.add(@bump_offset, size)
end
defwp bump_alloc(size: I32), I32, [] do
# TODO: check if we have allocated too much
# and if so, either err or increase the available memory.
# TODO: Need better maths than this to round up to aligned memory?

# Better syntax?
# pop(push: @bump_offset) do
# @bump_offset = I32.add(@bump_offset, size)
# end
push(@bump_offset) do
@bump_offset = I32.add(@bump_offset, size)
end

func(alloc(size: I32), I32, do: typed_call(I32, :bump_alloc, [size]))
# Better syntax?
# pop(push: @bump_offset) do
# @bump_offset = I32.add(@bump_offset, size)
# end
end

func free_all() do
@bump_offset = Constants.bump_init_offset()
end
defw(alloc(size: I32), I32, do: typed_call(I32, :bump_alloc, [size]))

defw free_all() do
@bump_offset = Constants.bump_init_offset()
end

# def alloc(byte_count) do
Expand Down Expand Up @@ -191,95 +183,93 @@ defmodule Examples.Memory do
# increase_memory pages: 2
# @wasm_memory 2

wasm do
func cons(hd: I32.UnsafePointer, tl: I32.UnsafePointer), I32.UnsafePointer,
ptr: I32.UnsafePointer do
ptr = typed_call(I32.UnsafePointer, :bump_alloc, [8])
ptr[at!: 0] = hd
ptr[at!: 1] = tl
ptr
end
defw cons(hd: I32.UnsafePointer, tl: I32.UnsafePointer), I32.UnsafePointer,
ptr: I32.UnsafePointer do
ptr = typed_call(I32.UnsafePointer, :bump_alloc, [8])
ptr[at!: 0] = hd
ptr[at!: 1] = tl
ptr
end

func hd(ptr: I32.UnsafePointer), I32.UnsafePointer do
# Zig: https://godbolt.org/z/bG5zj6bzx
# ptr[at: 0, fallback: 0x0]
# ptr |> I32.eqz?(do: 0x0, else: ptr[at!: 0])
I32.eqz(ptr) |> I32.when?(do: 0x0, else: ptr[at!: 0])
# ptr &&& ptr[at!: 0]
# ptr[at!: 0]
end
defw hd(ptr: I32.UnsafePointer), I32.UnsafePointer do
# Zig: https://godbolt.org/z/bG5zj6bzx
# ptr[at: 0, fallback: 0x0]
# ptr |> I32.eqz?(do: 0x0, else: ptr[at!: 0])
I32.eqz(ptr) |> I32.when?(do: 0x0, else: ptr[at!: 0])
# ptr &&& ptr[at!: 0]
# ptr[at!: 0]
end

func tl(ptr: I32.UnsafePointer), I32.UnsafePointer do
# ptr.unwrap[at!: 1]
# ptr |> I32.eqz?(do: :unreachable, else: ptr[at!: 1])
I32.eqz(ptr) |> I32.when?(do: 0x0, else: ptr[at!: 1])
# ptr[at!: 1]
end
defw tl(ptr: I32.UnsafePointer), I32.UnsafePointer do
# ptr.unwrap[at!: 1]
# ptr |> I32.eqz?(do: :unreachable, else: ptr[at!: 1])
I32.eqz(ptr) |> I32.when?(do: 0x0, else: ptr[at!: 1])
# ptr[at!: 1]
end

func reverse_in_place(node: I32.UnsafePointer), I32.UnsafePointer,
prev: I32.UnsafePointer,
current: I32.UnsafePointer,
next: I32.UnsafePointer do
current = node
defw reverse_in_place(node: I32.UnsafePointer), I32.UnsafePointer,
prev: I32.UnsafePointer,
current: I32.UnsafePointer,
next: I32.UnsafePointer do
current = node

# loop current, result: I32 do
# 0 ->
# {:halt, prev}
#
# current ->
# next = current[at!: 1]
# current[at!: 1] = prev
# prev = current
# {:cont, next}
# end

loop Iterate, result: I32 do
# return(prev, if: I32.eqz(current))
if I32.eqz(current), do: return(prev)

next = current[at!: 1]
current[at!: 1] = prev
prev = current
current = next
Iterate.continue()
end
# loop current, result: I32 do
# 0 ->
# {:halt, prev}
#
# current ->
# next = current[at!: 1]
# current[at!: 1] = prev
# prev = current
# {:cont, next}
# end

loop Iterate, result: I32 do
# return(prev, if: I32.eqz(current))
if I32.eqz(current), do: return(prev)

next = current[at!: 1]
current[at!: 1] = prev
prev = current
current = next
Iterate.continue()
end
end

func list_count(ptr: I32.UnsafePointer), I32, count: I32 do
loop Iterate, result: I32 do
# I32.match ptr do
# 0 ->
# return(count)
#
# _ ->
# ptr = call(:tl, ptr)
# count = I32.add(count, 1)
# Iterate.continue()
# end

if I32.eqz(ptr), do: return(count)
# if I32.eqz(ptr), return: count
# return(count, if: I32.eqz(ptr))
# guard ptr, else_return: count
# I32.unless ptr, return: count
# I32.when? ptr, else_return: count
# I32.when? ptr, else: return(count)

ptr = typed_call(I32.UnsafePointer, :tl, [ptr])
count = count + 1
Iterate.continue()
end
defw list_count(ptr: I32.UnsafePointer), I32, count: I32 do
loop Iterate, result: I32 do
# I32.match ptr do
# 0 ->
# return(count)
#
# _ ->
# ptr = call(:tl, ptr)
# count = I32.add(count, 1)
# Iterate.continue()
# end

if I32.eqz(ptr), do: return(count)
# if I32.eqz(ptr), return: count
# return(count, if: I32.eqz(ptr))
# guard ptr, else_return: count
# I32.unless ptr, return: count
# I32.when? ptr, else_return: count
# I32.when? ptr, else: return(count)

ptr = typed_call(I32.UnsafePointer, :tl, [ptr])
count = count + 1
Iterate.continue()
end
end

func list32_sum(ptr: I32), I32, sum: I32 do
loop Iterate, result: I32 do
if I32.eqz(ptr), do: return(sum)
defw list32_sum(ptr: I32), I32, sum: I32 do
loop Iterate, result: I32 do
if I32.eqz(ptr), do: return(sum)

sum = sum + typed_call(I32.UnsafePointer, :hd, [ptr])
ptr = typed_call(I32.UnsafePointer, :tl, [ptr])
sum = sum + typed_call(I32.UnsafePointer, :hd, [ptr])
ptr = typed_call(I32.UnsafePointer, :tl, [ptr])

Iterate.continue()
end
Iterate.continue()
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/examples/memory_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ defmodule Examples.MemoryTest do

test "wasm size" do
wasm = Wasm.to_wasm(LinkedLists)
assert byte_size(wasm) == 326
assert byte_size(wasm) == 347
end

test "multiple allocations" do
Expand Down
12 changes: 6 additions & 6 deletions test/orb_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ defmodule OrbTest do

Memory.pages(1)

defw get_int32(), I32 do
Memory.load!(I32, 0x100)
end
defw get_int32(), I32 do
Memory.load!(I32, 0x100)
end

defw set_int32(value: I32) do
Memory.store!(I32, 0x100, value)
end
defw set_int32(value: I32) do
Memory.store!(I32, 0x100, value)
end
end

assert to_wat(MemoryLoadStore) == """
Expand Down

0 comments on commit 61ab351

Please sign in to comment.