erlang源码分析

来源:互联网 发布:电信运营商大数据应用 编辑:程序博客网 时间:2024/04/25 20:20

参考网址

http://www.cs-lab.org/historical_beam_instruction_set.html

http://erlangonxen.org/more/beam

http://www.cnblogs.com/me-sa/p/erlang_notes_129.html

一、寄存器

The BEAM Virtual Machine uses the following registers:

   HTOP - top-of-heap pointer

   E - top-of-stack pointer   

   CP - return-address pointer (where to go when a function is ready executed)

   I - next-instruction pointer

   x(N) - argument registers (to pass function parameters), x(N) are also used to hold temporal variables

   y(N) - local variables (y(N) are not real registers, they reside in a local frame and are accessed by integer offset from a top-of-stack pointer)

   fcalls - number of reductions done (to check for suspension while doing function call)


在这种格式中的opcode_value小于255的占用一个字节,大于255的将占用两个字节。

字节码语法图形式比较复杂,操作数按他们的类型大致可以分为以下几种:

8-bit long

  • magic number
  • function arity
  • type of arithmetic
  • type of operand
  • type of bif
  • sign of bignumber value

16-bit long

  • atom length
  • float length
  • code length
  • register number
  • frame size
  • heap requirement

24-bit long

  • relative address

32-bit long

  • integer value
  • code size
  • tuple arity
  • tuple index
  • string length
  • bignum arity
  • bignum value

除此之外,其它的操作数都是16位长。

然后字节码文件不能被直接执行,需要由erlang虚拟机解释执行。Erlang虚拟机加载字节码文件后会将字节码映射成虚拟机能识别的指令集,虚拟机指令集是使用C代码来定义的,称为threaded-code。其格式如下:




0 0
原创粉丝点击