compiler optimisation of constant expressions

Björn Gustavsson bjorn@REDACTED
Thu Sep 16 12:18:54 CEST 2021


On Thu, Sep 16, 2021 at 11:15 AM Andreas Schultz
<andreas.schultz@REDACTED> 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


More information about the erlang-questions mailing list