Re: compiler optimisation of constant expressions

Björn Gustavsson <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <CA+yh78TM8AYmOk9G6EfipcEqPC5equBiF3pHU-6rfe5UPZUg-Q@mail.gmail.com>
On Thu, Sep 16, 2021 at 11:15 AM Andreas Schultz
<[email protected]> wrote:
> When I tested that on OTP-23.3.4.5 it turned out that the compiled beam file still contained the atom_to_list and integer_to_list calls, e.g. this code snippet:
>
> test() ->
>     ?FUNCTION_STRING.
>
>
> Is contained in the beam file as:
>
> test() ->
>     "test_mod" ++ ":" ++ atom_to_list(test) ++ "/" ++ integer_to_list(0).

If by "contained in the beam file" you mean the debug info, it has not
been optimized. We intentionally stay as close to the source code as
possible.

Later optimization passes will optimize the code. So if I have this module:

-module(t).
-compile([export_all, nowarn_export_all]).

-define(FUNCTION_STRING, atom_to_list(?MODULE) ++ ":" ++
atom_to_list(?FUNCTION_NAME) ++ "/" ++
          integer_to_list(?FUNCTION_ARITY)).

test() ->
    ?FUNCTION_STRING.

the BEAM code that I'll get if I compile like this:

erlc -S t.erl

will look this this:

{function, test, 0, 2}.
  {label,1}.
    {line,[{location,"t.erl",7}]}.
    {func_info,{atom,t},{atom,test},0}.
  {label,2}.
    {move,{literal,"t:test/0"},{x,0}}.
    return.

/Björn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.