<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erlang非业余研究 &#187; opcode</title>
	<atom:link href="http://blog.yufeng.info/archives/tag/opcode/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.yufeng.info</link>
	<description>Erlang系统深度探索和应用</description>
	<lastBuildDate>Tue, 17 Jan 2012 06:05:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Erlang源码汇编格式</title>
		<link>http://blog.yufeng.info/archives/498</link>
		<comments>http://blog.yufeng.info/archives/498#comments</comments>
		<pubDate>Fri, 16 Apr 2010 02:40:40 +0000</pubDate>
		<dc:creator>Yu Feng</dc:creator>
				<category><![CDATA[Erlang探索]]></category>
		<category><![CDATA['s']]></category>
		<category><![CDATA[erlc]]></category>
		<category><![CDATA[erts_dbug:df]]></category>
		<category><![CDATA[opcode]]></category>
		<category><![CDATA[VM]]></category>

		<guid isPermaLink="false">http://blog.yufeng.info/?p=498</guid>
		<description><![CDATA[原创文章，转载请注明： 转载自Erlang非业余研究 本文链接地址: Erlang源码汇编格式 我们在编码的时候, 通常会好奇, 这时候需要观察erl源码生成的VM opcode. Erlang的VM是register based的VM, 生产的opcode很容易理解. 生成汇编格式有2种方式: 1. 从源码生成抽象码. erlc +&#8221;&#8216;S&#8217;&#8221; mod.erl, 生成mod.S 2. 从beam生成Opcode. 未公开的功能. erts_debug:df 参数M或者 M, F, 生成mod.dis 来吧,实践下: 我们得到了eg.S, eg.dis这2个反汇编的结果. 我们再来参观下. 先看源码: root@ubuntu:~/exam# cat eg.erl 中间汇编码, 供transform进行处理和编译器进一步生成opcode. root@ubuntu:~/exam# cat eg.S VM opcode形式, VM就是来解释运行这些code的 收工! Post Footer automatically generated by wp-posturl plugin for wordpress.]]></description>
			<content:encoded><![CDATA[<div style="margin-top: 15px; font-style: italic">
<p><strong>原创文章，转载请注明：</strong> 转载自<a href="http://blog.yufeng.info/">Erlang非业余研究</a></p>
<p><strong>本文链接地址:</strong> <a href="http://blog.yufeng.info/archives/498">Erlang源码汇编格式</a></p>
</div>
<p>我们在编码的时候, 通常会好奇, 这时候需要观察erl源码生成的VM opcode.  Erlang的VM是register based的VM, 生产的opcode很容易理解.<br />
生成汇编格式有2种方式:<br />
1. 从源码生成抽象码. erlc +&#8221;&#8216;S&#8217;&#8221; mod.erl, 生成mod.S<br />
2. 从beam生成Opcode. 未公开的功能. erts_debug:df   参数M或者 M, F, 生成mod.dis</p>
<p>来吧,实践下:</p>
<pre class="brush: bash; title: ; notranslate">
root@ubuntu:~/exam# ls
eg.erl
root@ubuntu:~/exam# erlc +&quot;'S'&quot; eg.erl
root@ubuntu:~/exam# erlc eg.erl
root@ubuntu:~/exam# erl
Erlang R14A (erts-5.8) 1 [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false] [lock-counting]

Eshell V5.8  (abort with ^G)
1&gt;  erts_debug:df(eg).
ok
2&gt;
User switch command
 --&gt; q
root@ubuntu:~/exam# ls eg*
eg.beam  eg.dis  eg.erl  eg.S
</pre>
<p>我们得到了eg.S, eg.dis这2个反汇编的结果. 我们再来参观下.<br />
先看源码:<br />
root@ubuntu:~/exam# cat eg.erl</p>
<pre class="brush: erlang; title: ; notranslate">
-module(eg).
-import(lists).
-import(lists,[sum/1]).
-compile(export_all).

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

loop()-&gt;
    sum(lists:seq(1,100)),
    loop().
</pre>
<p>中间汇编码, 供transform进行处理和编译器进一步生成opcode.<br />
root@ubuntu:~/exam# cat eg.S</p>
<pre class="brush: erlang; title: ; notranslate">
{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,&quot;*&quot;},{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}}.
</pre>
<p>VM opcode形式, VM就是来解释运行这些code的</p>
<pre class="brush: bash; title: ; notranslate">
root@ubuntu:~/exam# cat eg.dis
B5146074: i_func_info_IaaI 0 eg kilo_byte 0
B5146088: move_cx &quot;*&quot; 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
</pre>
<p>收工!</p>
<div style="margin-top: 0; margin-bottom: 15px; color: #888888; font-size: 80%; font-style: italic">
<p>Post Footer automatically generated by <a href="http://easwy.com/blog/wordpress/wp-posturl/" style="color: #8888FF; text-decoration: underline;">wp-posturl plugin</a> for wordpress.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.yufeng.info/archives/498/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

