Skip to content

Instantly share code, notes, and snippets.

@kuenishi
Created July 26, 2012 16:22
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kuenishi/3183043 to your computer and use it in GitHub Desktop.
Save kuenishi/3183043 to your computer and use it in GitHub Desktop.
Erlang BEAM memo

BEAM

assemble

$ erlc -S test.erl

registers

  • 1024 X registers
  • Y register

Registers X, Y are represented like this:

5 bit tag

27 bit value

The tags are 3=X, 4=Y, 5=r, 6=Literal

  • CP
  • "Continuation Pointer" - return address
  • PC
  • "Program Counter" - next instruction to be executed

important instructions

  • test
  • move {move, Src, Dest}

64 is the opcode for "move", 0x18000000 is the encoding of argument that means register {x, 0}

  • gc_bif
  • call {call, Arity, {f, N}}
  • set P=N, CP=Next
  • jumps to label N
  • arguments are stored in X0 ~ Xn
  • return
  • does PC=CP
  • return value is stored in X0
  • receive
  • allocate
  • deallocate {deallocate, N}
  • create and remove stack frames.
  • there also exists {allocate_zero, Ny, Ng} - Ny Y registers

other instructions

  • label {label, N}
  • Nth label in the module
  • function {function, Name, Arity, LabelID}
  • defines a function
  • bif {bif, FuncAtom, {f, 0}, [], {x, 0}}
  • calls, ?
  • gc_bif {gc_bif, '*', {f, A}, N1, [R1, R2, R3]}
  • jumps to a function named '*'
  • multiply R1 and R2, and set the result into R3. If any failure occurs jump to A. If GC is triggered include N1 X registers starting at X0 in the root set for GC.
  • send send.
  • sends the data in X0 ?
  • loop_rec {loop_rec, {f, N}, {x, 0}}
  • receive clause starts here, till loop_rec_end
  • wait_timeout {wait_timeout, {f, N}, {integer, T}}
  • test {test, is_eq_exact, {f, N}, [{x,0}, {atom, foo}]}
  • remove_message.

misc

Context Switch

No other timing than when a call or receive instruction is executed, check a reduction counter.

How to disasemble

beam_lib:chunks("hw.beam", [abstract_code]),

Reference

- A Guide to the Erlang SourceErlang source code guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment