-
-
Save sasa1977/73274c2be733b5321ace to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{_, 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
{: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