As part of debugging an issue in Benchee recently, we needed to look at
BEAM instructions for some code to see what exactly was being generated on
OTP 20 and OTP 21. I also know of several other people in the community
that need to look at the BEAM instructions from time to time, either for
debugging purposes or for performance analysis purposes. Each of these
people have their own little script or function stored somewhere that
generates this code for them. I think, in the spirit of adding things to
Elixir Core that help in the development of the language itself, it would
be nice to have a function to take care of generating BEAM instructions in
Elixir Core.
That's why I am proposing a new function called `Code.beam_instructions/1`.
It would accept a string of Elixir code and would return a human readable
binary representation of the BEAM instructions for the given code.
For reference, here's the implementation that I'm currently using to
inspect BEAM instructions (which I got from Tobias Peiffer):
```
defmodule ASM do
def for(code) do
code = """
defmodule Test do
def test do
#{code}
end
end
"""
[{_, beam}] = Code.compile_string(code)
{:ok,{_,[{:abstract_code,{_,abstract_code}}]}} =
:beam_lib.chunks(beam,[:abstract_code])
{:ok, _module, asm} = :compile.forms(abstract_code, [:to_asm])
asm
end
end
```
and here's a link to Saša Jurić's gist on which that's based:
https://gist.github.com/sasa1977/73274c2be733b5321ace
The reason it's helpful to have in Elixir core instead of in a separate
file somewhere is that you can drop this function in as a debugging
statement in the code you're working on instead of having to pull out the
code you're working on and paste it into an IEX session somewhere else.
It's a convenience for sure, but it would help make the development
experience nicer.
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/b3e26fb3-9034-42d7-bb25-fd04d5686b6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.