Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang Opcodes and their meaning

Some of the opcodes used by the Erlang VM have an obvious meaning, but others are mysterious and they seem to differ each other by subtle differences.

As an example:

  1. What's the difference between the call_ext and call_ext_only opcodes?
  2. What's the difference between the allocate and the allocate_zero opcodes?
  3. What is the test_heap opcode used for? Is it some kind of check or it actually allocate some space in the heap? It is somehow linked to the usage of tuples and lists, but in which terms?
  4. What do the arguments of the allocate stands for?

If anyone could point me to any piece of documentation available for the opcodes used by the Erlang VM or if he could at least enlighten me on the above points, it would be very appreciated.

like image 596
Roberto Aloi Avatar asked Nov 15 '12 12:11

Roberto Aloi


1 Answers

As stated in a similar SO-question and the erlang-documentation:

Note that the format of assembler files is not documented, and may change between releases - this option is primarily for internal debugging use.

If you realy want to know what's happening, it seems you have to track it down in the source-code. Most of the work is done in erts/emulator/beam/beam_emu.c (I've looked into otp_src_R15B02):

  1. call_ext: set the continuation pointer to current instruction+2 and dispatch/ call external. call_ext_only: dont touch CP, just dispatch. (~line 1520)
  2. both allocate memory but allocate_zero also initializes it to 0x00 (~line 334).
  3. test_heap: Check if Nh words of heap are available; if not, do a garbage collection. (~line 390)
  4. allocate(StackNeeded, NumberOfRegistersToPreserve) (~line 316)

The whole file is a compound of #defines and gotos, some macros are defined inside ops.tab in the same folder. I'm not an expert in erlang-asm either and might have missed something. Please keep that in mind and cross-check my statements before you start to work with them.

To quote TamasNagy from the linked SO-awnser:

I'm not sure what are you trying to achieve with this, but core erlang might be a better level to do code manipulation.

Please look there for further information. Erlang have it strengths but documentation is not one of them.

regards.

like image 88
Peter Schneider Avatar answered Oct 22 '22 15:10

Peter Schneider