转载请注明,来自:http://blog.csdn.net/skyman_2001
JAM即Joe's Abstract Machine.下面是摘录自http://www.erlang.org/faq/academic.html的关于JAM的说明:
First I designed an abstract machine to execute Erlang. This was called the JAM machine; JAM = Joe's Abstract Machine.
Then I wrote a compiler from Erlang to JAM and an emulator to see if the machine worked. Both these were written in prolog.
At the same time Mike Williams wrote a C emulator for the JAM.
Then I rewrote the erlang-to-jam compiler in Erlang and used the prolog compiler to compile it. The resultant object code was run in the C emulator. Then we threw away prolog.
Some of this is described in an old paper
了解JAM的内部结构对理解Erlang VM很有帮助,Erik Johansson和Christer Jonsson合写的硕士论文《Native Code Compilation for Erlang》里对JAM有比较详细的介绍。这里只捡重要的作为笔记,想详细了解的可以阅读该论文。
1. 寄存器
PC - Pointer to the next instruction to execute;
STOP - Pointer to the stack top of the active process;
HTOP - Pointer to the heap top of the active process;
ARGS - Pointer to the first argument (on the stack) of the current function;
VARS - Pointer to the first local variable on the stack;
FAIL_PC - Address where execution should continue after a failure;
FAIL_REASON - The reason for the failure;
P - Pointer to the process control block (PCB).
2. 数据表示
3. 代码
The JAM instruction set is implemented with byte codes, making it compact. Code is patched at load time (for example with indexes into the atom table). A module in Erlang is a collection of functions sharing the same name space. Code is loaded one module at the time, when needed.
4. 函数调用
When the function returns, the frame is popped from the stack and the return value is pushed (in the same stack position as the first argument).
5. 并发
每个进程有自己的process control block(PCB), stack和heap。当进程没被执行时,JAM的寄存器存储在PCB中。