Erlang bytecode

http://mryufeng.iteye.com

yufeng在一篇文章中提到可以从erlang源码生成中间码,下来想想对理解VM的机制是大有帮助的,记一下,在此,对yufeng在Erlang方面的丰功伟绩表示感谢。

-module(tut1).
-export([fac/1]).

fac(0) -> 1;
fac(N) -> N * fac(N-1).

erlc+"'S'"tut1.erl

{module, tut1}.  %% version = 0

{exports, [{fac,1},{module_info,0},{module_info,1}]}.

{attributes, []}.

{labels, 8}.


{function, fac, 1, 2}.
  {label,1}.
    {func_info,{atom,tut1},{atom,fac},1}.
  {label,2}.
    {test,is_eq_exact,{f,3},[{x,0},{integer,0}]}.
    {move,{integer,1},{x,0}}.
    return.
  {label,3}.
    {allocate_zero,1,1}.
    {gc_bif,'-',{f,0},1,[{x,0},{integer,1}],{x,1}}.
    {move,{x,0},{y,0}}.
    {move,{x,1},{x,0}}.
    {call,1,{f,2}}.
    {gc_bif,'*',{f,0},1,[{y,0},{x,0}],{x,0}}.
    {deallocate,1}.
    return.


{function, module_info, 0, 5}.
  {label,4}.
    {func_info,{atom,tut1},{atom,module_info},0}.
  {label,5}.
    {move,{atom,tut1},{x,0}}.
    {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.


{function, module_info, 1, 7}.
  {label,6}.
    {func_info,{atom,tut1},{atom,module_info},1}.
  {label,7}.
    {move,{x,0},{x,1}}.
    {move,{atom,tut1},{x,0}}.
    {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.

相关推荐