Compiling Elixir to Erlang assembler

230 views
Skip to first unread message

Peter Minten

unread,
Feb 17, 2014, 1:04:29 PM2/17/14
to elixir-l...@googlegroups.com
Hi all,

I have a module which I'd like to compile to Erlang assembler, but I
haven't found a way to do so. Ericmj pointed me in the direction of
:elixir.quoted_to_erl but this doesn't work ("vague internal error in
transform_module"):

File.read!("/tmp/foo.ex") |> Code.string_to_quoted!() |>
:elixir.quoted_to_erl(:elixir.env_for_eval([])) |> :compile.forms()

Could anyone tell me how to make Elixir compile to assembler like Erlang
does with compile:file(Filename, ['S'])?

Greetings,

Peter

PS: For the curious, this is what I'm trying to compile (wanting to see
whether case2 and case3 are more efficient than case1):

defmodule Foo do
def case1(_, _, 0), do: true
def case1([ha|ta], [hb|tb], len) do
if ha === hb, do: case1(ta, tb, len-1), else: false
end

def case2(_, _, 0), do: true
def case2([ha|ta], [hb|tb], len) when ha === hb, do:
case2(ta, tb, len-1)
def case2(_, _, _), do: false

def case3(_, _, 0), do: true
def case3([x|ta], [x|tb], len), do: case2(ta, tb, len-1)
def case3(_, _, _), do: false
end

José Valim

unread,
Feb 17, 2014, 1:30:05 PM2/17/14
to elixir-l...@googlegroups.com
Hello Peter,

:compile.forms/2 expects an erlang module form so passing Elixir scripts is not going to do it. One option is to compile the Elixir module and then load its abstract format (as shown below) and then feed it into :compile.forms/2:

:beam_lib.chunks('lib/elixir/ebin/Elixir.URI.beam', [:abstract_code])


José Valim
Skype: jv.ptec
Founder and Lead Developer



--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter Minten

unread,
Feb 18, 2014, 12:58:54 PM2/18/14
to elixir-l...@googlegroups.com
Thanks. That worked.

For the curious, turns out all the caseN functions are equally efficient.

On 17/02/14 19:30, José Valim wrote:
> Hello Peter,
>
> :compile.forms/2 expects an erlang module form so passing Elixir scripts is
> not going to do it. One option is to compile the Elixir module and then
> load its abstract format (as shown below) and then feed it into
> :compile.forms/2:
>
> :beam_lib.chunks('lib/elixir/ebin/Elixir.URI.beam', [:abstract_code])
>
>
>
> *José Valim*

Lars Hesel Christensen

unread,
Feb 27, 2015, 6:38:36 AM2/27/15
to elixir-l...@googlegroups.com, peter....@online.nl
Hi

I tried you suggestion José, but didn't get it to work. I have the following code:

defmodule Example do
  def f("ale"), do: 1
  def f("alabama"), do: 2
  def f("alhambra"), do: 3
end

I compile it then successfully load the abstract code:

{:ok, x} = :beam_lib.chunks('/home/lhc/dev/temp/Elixir.Example', [:abstract_code])

But running  :compile.forms(x) gives me this:

: internal error in transform_module;
crash reason: function_clause

  in function  compile:compile_options/1
     called as compile:compile_options({'Elixir.Example',
  [{abstract_code,
    {raw_abstract_v1,
     [{attribute,0,compile,no_auto_import},
      {attribute,1,file,{"example.exs",1}},
      {attribute,1,module,'Elixir.Example'},
....
        {clause,4,
         [{bin,0,[{bin_element,0,{string,0,"alhambra"},default,default}]}],
         [],
         [{integer,0,3}]}]}]}}]})
  in call from compile:transform_module/1 (compile.erl, line 897)
  in call from compile:'-internal_comp/4-anonymous-1-'/2 (compile.erl, line 292)
  in call from compile:fold_comp/3 (compile.erl, line 310)
  in call from compile:internal_comp/4 (compile.erl, line 294)
  in call from compile:'-do_compile/2-anonymous-0-'/2 (compile.erl, line 153)
:error


Any ideas what I'm doing wrong? I was wondering if maybe I need to pass some options and use :compile.forms/2, but didn't find any options that seemed appropriate.

Cheers,
Lars

José Valim

unread,
Feb 27, 2015, 6:41:45 AM2/27/15
to elixir-l...@googlegroups.com
You need to match on what is returned by :beam_lib.chunks/2 and extract the abstract_code out:

{'Elixir.Example',
  [{:abstract_code,
    {:raw_abstract_v1, abstract_code}}]} = :beam_lib.chunks(Example, [:abstract_code])
:compile.forms(abstract_code)



José Valim
Skype: jv.ptec
Founder and Lead Developer

Lars Hesel Christensen

unread,
Feb 27, 2015, 7:05:01 AM2/27/15
to elixir-l...@googlegroups.com
Thanks, that and feeding it into :compile.forms with the :S atom option
gave me exactly what I needed.

Cheers,
Lars

On 02/27/2015 12:41 PM, José Valim wrote:
> You need to match on what is returned by :beam_lib.chunks/2 and extract
> the abstract_code out:
>
> {'Elixir.Example',
> [{:abstract_code,
> {:raw_abstract_v1, abstract_code}}]} = :beam_lib.chunks(Example,
> [:abstract_code])
> :compile.forms(abstract_code)
>
>
>
> *José Valim*
> www.plataformatec.com.br <http://www.plataformatec.com.br/>
> > :beam_lib.chunks('lib/elixir/__ebin/Elixir.URI.beam',
> [:abstract_code])
> >
> >
> >
> > *José Valim*
> > www.plataformatec.com.br <http://www.plataformatec.com.br>
> > Skype: jv.ptec
> > Founder and Lead Developer
> >
> >
> > On Mon, Feb 17, 2014 at 7:04 PM, Peter Minten
> <peter....@online.nl>wrote:
> >
> >> Hi all,
> >>
> >> I have a module which I'd like to compile to Erlang
> assembler, but I
> >> haven't found a way to do so. Ericmj pointed me in the
> direction of
> >> :elixir.quoted_to_erl but this doesn't work ("vague internal
> error in
> >> transform_module"):
> >>
> >> File.read!("/tmp/foo.ex") |> Code.string_to_quoted!() |>
> >> :elixir.quoted_to_erl(:elixir.__env_for_eval([])) |>
> >> email to elixir-lang-ta...@__googlegroups.com.
> >> For more options, visit
> https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
> >>
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to elixir-lang-ta...@googlegroups.com
> <mailto:elixir-lang-ta...@googlegroups.com>.
> <https://groups.google.com/d/msgid/elixir-lang-talk/0e7b4515-264b-4af5-9166-b9bc328f8b00%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to elixir-lang-ta...@googlegroups.com
> <mailto:elixir-lang-ta...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4%2BF4PPnVeY9PYvo8QzzZvzP0EOfGGjcVMXZ7B3%3Dvdnsvg%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4%2BF4PPnVeY9PYvo8QzzZvzP0EOfGGjcVMXZ7B3%3Dvdnsvg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages