1. 程式人生 > >系統技術非業餘研究 » Erlang原始碼彙編格式

系統技術非業餘研究 » Erlang原始碼彙編格式

我們在編碼的時候, 通常會好奇, 這時候需要觀察erl原始碼生成的VM opcode. Erlang的VM是register based的VM, 生產的opcode很容易理解.
生成彙編格式有2種方式:
1. 從原始碼生成抽象碼. erlc +”‘S'” mod.erl, 生成mod.S
2. 從beam生成Opcode. 未公開的功能. erts_debug:df 引數M或者 M, F, 生成mod.dis

來吧,實踐下:

[email protected]:~/exam# ls
eg.erl
[email protected]:~/exam# erlc +"'S'" eg.erl
[email protected]
:~/exam# erlc eg.erl [email protected]:~/exam# erl Erlang R14A (erts-5.8) [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] [lock-counting] Eshell V5.8 (abort with ^G) 1> erts_debug:df(eg). ok 2> User switch command --> q [email protected]
:~/exam# ls eg* eg.beam eg.dis eg.erl eg.S

我們得到了eg.S, eg.dis這2個反彙編的結果. 我們再來參觀下.
先看原始碼:
[email protected]:~/exam# cat eg.erl

-module(eg).
-import(lists).
-import(lists,[sum/1]).
-compile(export_all).


kilo_byte() ->
    kilo_byte(10, [42]).
kilo_byte(0, Acc) ->
    Acc;
kilo_byte(N, Acc) ->
    kilo_byte(N-1, [Acc|Acc]).


loop()->
    sum(lists:seq(1,100)),
    loop().

中間彙編碼, 供transform進行處理和編譯器進一步生成opcode.
[email protected]:~/exam# cat eg.S

{module, eg}.  %% version = 0

{exports, [{kilo_byte,0},
           {kilo_byte,2},
           {loop,0},
           {module_info,0},
           {module_info,1}]}.

{attributes, []}.

{labels, 12}.


{function, kilo_byte, 0, 2}.
  {label,1}.
    {func_info,{atom,eg},{atom,kilo_byte},0}.
  {label,2}.
    {move,{literal,"*"},{x,1}}.
    {move,{integer,10},{x,0}}.
    {call_only,2,{f,4}}.


{function, kilo_byte, 2, 4}.
  {label,3}.
    {func_info,{atom,eg},{atom,kilo_byte},2}.
  {label,4}.
    {test,is_eq_exact,{f,5},[{x,0},{integer,0}]}.
    {move,{x,1},{x,0}}.
    return.
  {label,5}.
    {gc_bif,'-',{f,0},2,[{x,0},{integer,1}],{x,0}}.
    {test_heap,2,2}.
    {put_list,{x,1},{x,1},{x,1}}.
    {call_only,2,{f,4}}.


{function, loop, 0, 7}.
  {label,6}.
    {func_info,{atom,eg},{atom,loop},0}.
  {label,7}.
    {allocate,0,0}.
    {move,{integer,100},{x,1}}.
    {move,{integer,1},{x,0}}.
    {call_ext,2,{extfunc,lists,seq,2}}.
    {call_ext,1,{extfunc,lists,sum,1}}.
    {call_last,0,{f,7},0}.


{function, module_info, 0, 9}.
  {label,8}.
    {func_info,{atom,eg},{atom,module_info},0}.
  {label,9}.
    {move,{atom,eg},{x,0}}.
    {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.


{function, module_info, 1, 11}.
  {label,10}.
    {func_info,{atom,eg},{atom,module_info},1}.
  {label,11}.
    {move,{x,0},{x,1}}.
    {move,{atom,eg},{x,0}}.
    {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.

VM opcode形式, VM就是來解釋執行這些code的

 
[email protected]:~/exam# cat eg.dis
B5146074: i_func_info_IaaI 0 eg kilo_byte 0 
B5146088: move_cx "*" x(1) 
B5146094: i_move_call_only_fcr eg:kilo_byte/2 10 x(0) 

B51460A0: i_func_info_IaaI 0 eg kilo_byte 2 
B51460B4: i_is_eq_immed_frc f(B51460C8) x(0) 0 
B51460C0: move_return_xr x(1) x(0) 
B51460C8: i_fetch_rc x(0) 1 
B51460D0: i_minus_jId j(00000000) 2 x(0) 
B51460E0: test_heap_II 2 2 
B51460EC: put_list_xxx x(1) x(1) x(1) 
B51460F4: i_call_only_f eg:kilo_byte/2 

B51460FC: i_func_info_IaaI 0 eg loop 0 
B5146110: allocate_tt 0 0 
B5146118: move_cx 100 x(1) 
B5146124: i_move_call_ext_cre 1 x(0) lists:seq/2 
B5146130: i_call_ext_e lists:sum/1 
B5146138: i_call_last_fP eg:loop/0 0 

B5146144: i_func_info_IaaI 0 eg module_info 0 
B5146158: move_cr eg x(0) 
B5146160: allocate_tt 0 1 
B5146168: call_bif1_e erlang:get_module_info/1 
B5146170: deallocate_return_P 0 

B5146178: i_func_info_IaaI 0 eg module_info 1 
B514618C: move_rx x(0) x(1) 
B5146194: move_cr eg x(0) 
B514619C: allocate_tt 0 2 
B51461A4: call_bif2_e erlang:get_module_info/2 
B51461AC: deallocate_return_P 0 

收工!

Post Footer automatically generated by wp-posturl plugin for wordpress.