Skip to content

Instantly share code, notes, and snippets.

@sasa1977
Created August 24, 2015 10:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasa1977/73274c2be733b5321ace to your computer and use it in GitHub Desktop.
Save sasa1977/73274c2be733b5321ace to your computer and use it in GitHub Desktop.
[{_, beam}] = Code.compile_string("""
defmodule Test do
def tail_call(x) do
tail_call(x)
end
def non_tail_call(x) do
1 + tail_call(x)
end
end
""")
{:ok,{_,[{:abstract_code,{_,abstract_code}}]}} = :beam_lib.chunks(beam,[:abstract_code])
{:ok, module, asm} = :compile.forms(abstract_code, [:to_asm])
IO.inspect(asm)
...
{:function, :non_tail_call, 1, 8,
[{:label, 7}, {:line, [{:location, 'nofile', 10}]},
{:func_info, {:atom, Test}, {:atom, :non_tail_call}, 1}, {:label, 8},
{:allocate, 0, 1}, {:line, [{:location, 'nofile', 11}]},
{:call, 1, {:f, 10}}, {:line, [{:location, 'nofile', 11}]}, <- non_tail call
{:gc_bif, :+, {:f, 0}, 1, [integer: 1, x: 0], {:x, 0}}, {:deallocate, 0},
:return]},
...
{:function, :tail_call, 1, 10,
[{:label, 9}, {:line, [{:location, 'nofile', 2}]},
{:func_info, {:atom, Test}, {:atom, :tail_call}, 1}, {:label, 10},
{:test, :is_lt, {:f, 11}, [integer: 0, x: 0]}, {:call_only, 1, {:f, 10}},
{:label, 11}, {:call_only, 1, {:f, 10}}]}, <- tail call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment