Understanding BEAM bytecode [Code section]

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding BEAM bytecode [Code section]

Gabriel Rivas
Hello all,

I am trying to understand BEAM files most specifically the Code section.
For that objective I wrote a simple program consisting of just a subtraction function 
taking 2 parameters:

-module(ex3).
-export([sub/2]).

sub(X,Y) ->
    X-Y.

From that file I created the .S version,  and I can see that the assembly
version for the subtraction is:

{gc_bif,'-',{f,0},2,[{x,0},{x,1}],{x,0}}. 
    return.

In the BEAM file I can see that the bytecode part that correlated with this
function is (in hex numbers):

7D 05 20 00 03 13 03 13

After some reading of the BEAM book and some analysis, I could translate
these hex numbers to:

7D 05 20 = {gc_bif2,
                    Lbl = 05,
                    Live X registers = 2,
                    Bif = 00,

03 13 03 13 =  args: X0,X1
                        return: X0
                        13 -> k_return

But I still don't see how the subtraction happens. Can anybody shed some light
on the internals of calling a BIF in the erlang module?

Regards,
Gabriel

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Dmytro Lytovchenko
Similar to bif0...bif3 gc_bifN uses import index from the import table. Import table contains triples {Mod, Fun, Arity} for external functions used in the current module:

So Bif=00 is the 0th index in the import table.


2017-09-14 23:16 GMT+02:00 Gabriel Rivas <[hidden email]>:
Hello all,

I am trying to understand BEAM files most specifically the Code section.
For that objective I wrote a simple program consisting of just a subtraction function 
taking 2 parameters:

-module(ex3).
-export([sub/2]).

sub(X,Y) ->
    X-Y.

From that file I created the .S version,  and I can see that the assembly
version for the subtraction is:

{gc_bif,'-',{f,0},2,[{x,0},{x,1}],{x,0}}. 
    return.

In the BEAM file I can see that the bytecode part that correlated with this
function is (in hex numbers):

7D 05 20 00 03 13 03 13

After some reading of the BEAM book and some analysis, I could translate
these hex numbers to:

7D 05 20 = {gc_bif2,
                    Lbl = 05,
                    Live X registers = 2,
                    Bif = 00,

03 13 03 13 =  args: X0,X1
                        return: X0
                        13 -> k_return

But I still don't see how the subtraction happens. Can anybody shed some light
on the internals of calling a BIF in the erlang module?

Regards,
Gabriel

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Björn Gustavsson-4
In reply to this post by Gabriel Rivas
On Thu, Sep 14, 2017 at 11:16 PM, Gabriel Rivas
<[hidden email]> wrote:

>
> But I still don't see how the subtraction happens. Can anybody shed some
> light
> on the internals of calling a BIF in the erlang module?

Not sure that you are aware of it, but there is a disassembler that is
used like this:

  erts_debug:df(ex3).

It creates a file named ex3.dis. In it, you can see that the loaded
code looks this:

00007F9C8F953AD0: i_func_info_IaaI 0 ex3 sub 2
00007F9C8F953AF8: i_minus_jIxxd j(0000000000000000) 2 x(0) x(1) x(0)
00007F9C8F953B28: return

In this particular case, you can see that the BIF call has been
translated to a special instruction.

The translations that the loader makes can be found in
erts/emulator/beam/ops.tab.

/Bjorn
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Gabriel Rivas
In reply to this post by Dmytro Lytovchenko
Thanks Dmytro, I understand it now, you are doing a great job with BEAM Wisdoms. 

On Fri, Sep 15, 2017 at 4:24 AM, Dmytro Lytovchenko <[hidden email]> wrote:
Similar to bif0...bif3 gc_bifN uses import index from the import table. Import table contains triples {Mod, Fun, Arity} for external functions used in the current module:

So Bif=00 is the 0th index in the import table.


2017-09-14 23:16 GMT+02:00 Gabriel Rivas <[hidden email]>:
Hello all,

I am trying to understand BEAM files most specifically the Code section.
For that objective I wrote a simple program consisting of just a subtraction function 
taking 2 parameters:

-module(ex3).
-export([sub/2]).

sub(X,Y) ->
    X-Y.

From that file I created the .S version,  and I can see that the assembly
version for the subtraction is:

{gc_bif,'-',{f,0},2,[{x,0},{x,1}],{x,0}}. 
    return.

In the BEAM file I can see that the bytecode part that correlated with this
function is (in hex numbers):

7D 05 20 00 03 13 03 13

After some reading of the BEAM book and some analysis, I could translate
these hex numbers to:

7D 05 20 = {gc_bif2,
                    Lbl = 05,
                    Live X registers = 2,
                    Bif = 00,

03 13 03 13 =  args: X0,X1
                        return: X0
                        13 -> k_return

But I still don't see how the subtraction happens. Can anybody shed some light
on the internals of calling a BIF in the erlang module?

Regards,
Gabriel

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions




_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Gabriel Rivas
In reply to this post by Björn Gustavsson-4
Hello Bjorn, 

I was not aware of it, I am new to Erlang. I started doing development on Elixir but 
I would like to learn and understand what Erlang does under the hood. 

Thanks for the tip, it is very useful.

Gabriel

On Fri, Sep 15, 2017 at 4:46 AM, Björn Gustavsson <[hidden email]> wrote:
On Thu, Sep 14, 2017 at 11:16 PM, Gabriel Rivas
<[hidden email]> wrote:

>
> But I still don't see how the subtraction happens. Can anybody shed some
> light
> on the internals of calling a BIF in the erlang module?

Not sure that you are aware of it, but there is a disassembler that is
used like this:

  erts_debug:df(ex3).

It creates a file named ex3.dis. In it, you can see that the loaded
code looks this:

00007F9C8F953AD0: i_func_info_IaaI 0 ex3 sub 2
00007F9C8F953AF8: i_minus_jIxxd j(0000000000000000) 2 x(0) x(1) x(0)
00007F9C8F953B28: return

In this particular case, you can see that the BIF call has been
translated to a special instruction.

The translations that the loader makes can be found in
erts/emulator/beam/ops.tab.

/Bjorn


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Сергей Прохоров-2
In reply to this post by Gabriel Rivas
You might be interested in this online tool to inspect BEAM assembler and bytecode:


Try 'Compile to: Erlang assembler (.S)' or 'Compile to: beam VM assembler (.dis)'.

Regards,
Sergey

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Understanding BEAM bytecode [Code section]

Gabriel Rivas
Thank you, I will check it out. 

On Sep 17, 2017 9:05 AM, "Сергей Прохоров" <[hidden email]> wrote:
You might be interested in this online tool to inspect BEAM assembler and bytecode:


Try 'Compile to: Erlang assembler (.S)' or 'Compile to: beam VM assembler (.dis)'.

Regards,
Sergey

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions