BYQMDP2IKXLPX4G3AXE26QVFRRKFU665REQCT7L2P37B4DLGI2TAC
int identity(int a) {
return a;
}
defp compile_function(%{code: %{expr: exprs, locals: []}}) do
function_body = compile_function_body(exprs)
function_desc = [{:clause, 4, [], [], function_body}]
defp compile_function(%{code: %{expr: exprs, locals: []}}, {params, _} = _type) do
function_params = compile_function_params(params)
function_body = compile_function_body(exprs, function_params)
function_desc = [{:clause, 4, function_params, [], function_body}]
defp compile_function_body([instr | rest], acc) do
beam_instr = compile_instruction(instr)
compile_function_body(rest, [beam_instr | acc])
defp compile_function_params([param | rest], acc) do
compile_function_params(rest, [compile_function_param(param) | acc])
end
defp compile_function_param(:i32) do
suf = :crypto.strong_rand_bytes(2) |> Base.encode16()
var_name = "V_#{suf}" |> String.to_atom()
{:var, 4, var_name}
defp compile_instruction({:"i32.const", val}) do
defp compile_function_body(exprs, params) do
compile_function_body(exprs, params, [])
end
defp compile_function_body([], _, acc) do
Enum.reverse(acc)
end
defp compile_function_body([instr | rest], params, acc) do
beam_instr = compile_instruction(instr, params)
compile_function_body(rest, params, [beam_instr | acc])
end
defp compile_instruction({:"i32.const", val}, _) do