Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 272 additions & 0 deletions GasCosts.tex

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions lib/wasp_vm.ex → lib/alchemy_vm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,35 @@ defmodule AlchemyVM do

@spec execute_func(AlchemyVM, integer, list, :infinity | integer, String.t(), list) :: tuple
defp execute_func(vm, addr, args, gas_limit, fname, opts) do
stack = Enum.reduce(args, [], & [&1 | &2])
args = Enum.reverse(args)

# Conditional for Trace
if opts[:trace], do: create_log_timestamp(fname)

{vm, gas, stack} = Executor.create_frame_and_execute(vm, addr, gas_limit, opts, 0, stack)
# We'll have to update this when we allow multiple return values post-MVP
{return_type, {vm, gas, stack}} = Executor.create_frame_and_execute(vm, addr, gas_limit, opts, 0, [], args)

case vm do
tuple when is_tuple(tuple) -> tuple
_ -> {{:ok, gas, List.first(stack)}, vm}
_ ->
return_val =
case return_type do
{:i32} ->
[<<value::integer-32-little-signed>> | _] = stack
value
{:i64} ->
[<<value::integer-64-little-signed>> | _] = stack
value
{:f32} ->
[<<value::float-32-little>> | _] = stack
value
{:f64} ->
[<<value::float-64-little>> | _] = stack
value
{} -> nil
end

{{:ok, gas, return_val}, vm}
end
end

Expand Down
70 changes: 70 additions & 0 deletions lib/execution/dsl.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
defmodule AlchemyVM.DSL do
@moduledoc false

defmacro __using__(_opts) do
quote do
import AlchemyVM.DSL
end
end

@doc """
This allows us to internally write OpCode definition instructions in a more
concise format.

We can write the following definition:

defop i32_add(a, b) do
stack = [a + b | stack]
{ctx, gas + Gas.cost(:i32_add), stack}
end

and it will generate the following code:

defp instruction(ctx, gas, [a, b | stack], _opts, :i32_add) do
stack = [a + b | stack]
{ctx, gas + Gas.cost(:i32_add), stack}
end

In the above example, the i32_add(a, b) will implicitly pull values off the
stack and assign them to their respective variables.

When we have opcodes that are tuples rather than just atoms (opcodes that
have immediates, like i32_const), we can specify their immediates like so:

defop i32_const(immediates: [i32]) do
...
end

This gets translated to

defp instruction(ctx, gas, stack, _opts, {:i32_const, i32}) do
...
end
"""
defmacro defop(head, do: block) do
{opname, args_ast} = Macro.decompose_call(head)

{opname, args_ast} =
case List.last(args_ast) do
[immediates: immediates] ->
op = {:{}, [], [opname | immediates]}
[_ | args] = Enum.reverse(args_ast)
args = Enum.reverse(args)

{op, args}

_ -> {opname, args_ast}
end

num_args = length(args_ast)

quote generated: true do
defp instruction({var!(frame), var!(vm), var!(ip)} = var!(ctx), var!(gas), s, var!(opts), unquote(opname)) do
{unquote(args_ast), var!(stack)} = Enum.split(s, unquote(num_args))

unquote(block)
end
end
end

end
1,477 changes: 938 additions & 539 deletions lib/execution/executor.ex

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/execution/module_instance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ defmodule AlchemyVM.ModuleInstance do
typeidx = Enum.at(module.function_types, idx)
type = Enum.at(module.types, typeidx)

locals = Enum.flat_map(func.locals, & List.duplicate(0, &1.count))

locals = Enum.flat_map(func.locals, & List.duplicate(<<0::integer-32-little>>, &1.count))

{type, ref, func.body, locals}
end)
Expand Down
Loading