-
Notifications
You must be signed in to change notification settings - Fork 1
/
disfun.html
219 lines (196 loc) · 21.4 KB
/
disfun.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE html>
<html lang="vi">
<head>
<title>Tin tức Python PyMI.vn</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" />
<link href="https://n.pymi.vn/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Tin tức Python PyMI.vn Full Atom Feed" />
<!-- twitter card metadata -->
<meta name="twitter:site" content="">
<meta name="twitter:title" content="Dịch ngược mã máy Python: function/lambda">
<meta name="twitter:description" content="tiếp tục tìm hiểu bytecode với function">
<!-- OG Tags -->
<meta property="og:url" content="./disfun.html"/>
<meta property="og:title" content="Dịch ngược mã máy Python: function/lambda | Tin tức Python PyMI.vn" />
<meta property="og:description" content="tiếp tục tìm hiểu bytecode với function" />
<!-- favicon -->
<!-- moment.js for date formatting -->
<script src="./theme/js/moment.js"></script>
<!-- css -->
<link rel="stylesheet" type="text/css" href="./theme/css/main.css" />
<script>
/*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
(function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
grunticon(["./theme/css/icons.data.svg.css", "./theme/css/icons.data.png.css", "./theme/css/icons.fallback.css"]);
</script>
<noscript><link href="./theme/css/icons.fallback.css" rel="stylesheet"></noscript>
<!-- menu toggle javascript -->
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", initMenu);
function initMenu(){
var menu = document.getElementById("menu");
var menulink = document.getElementById("menu-link");
menulink.addEventListener("click", function toggleMenu(){
window.event.preventDefault();
menulink.classList.toggle('active');
menu.classList.toggle('active');
});
};
</script>
<meta name="description" content="tiếp tục tìm hiểu bytecode với function" />
<meta name="tags" content="dis" />
<meta name="tags" content="disassembly" />
<meta name="tags" content="compile" />
<meta name="tags" content="internal" />
<meta name="tags" content="bytecode" />
<meta name="tags" content="function" />
</head>
<body>
<div role="banner" id="masthead">
<header>
<h1><a href="/">Pymiers's Blog</a></h1>
<a href="#menu" id="menu-link">more stuff</a>
<nav id="menu">
<ul>
<li class="active"><a href="./category/features.html">features</a></li>
<li><a href="./category/news.html">news</a></li>
<li><a href="./category/pymivn.html">pymi.vn</a></li>
</ul>
</nav>
</header>
</div>
<div class="page" role="main">
<div class="article" role="article">
<article>
<footer>
<a name="top"></a>
<p>
<time datetime=" 2021-09-04 00:00:00+07:00">
<script>document.write(moment('2021-09-04 00:00:00+07:00').format('LL'));</script>
</time>
</p>
</footer>
<header>
<h2>
Dịch ngược mã máy Python: function/lambda
</h2>
<center>
<h4>
by Pymier0
</h4>
</center>
</header>
<div class="content">
<p>Tiếp loạt bài về <a href="./compile.html">CPython compiler</a>
<img alt="img" src="https://images.unsplash.com/photo-1601100521677-598dc05293c5?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMzI1MzN8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MzA3MjU5MTM&ixlib=rb-1.2.1&q=80&w=600"></p>
<p>Code định nghĩa 2 function dùng def và lambda:</p>
<div class="highlight"><pre><span></span><code><span class="c1"># $ cat -n fun.py</span>
<span class="mi">1</span> <span class="k">def</span> <span class="nf">sum_two</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="mi">2</span> <span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="mi">3</span> <span class="k">return</span> <span class="n">z</span>
<span class="mi">4</span>
<span class="mi">5</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>
<span class="mi">6</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">7</span>
<span class="mi">7</span> <span class="n">sum_two</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="mi">8</span>
<span class="mi">9</span> <span class="n">double</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">*</span> <span class="mi">2</span>
<span class="mi">10</span> <span class="n">double</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
</code></pre></div>
<p>Chạy dis. Việc tạo function dùng def hay lambda đều sử dụng
<span class="caps">BYTECODE</span> <code>MAKE_FUNCTION</code> sau đó <code>STORE_NAME</code></p>
<div class="highlight"><pre><span></span><code> <span class="mi">1</span> <span class="mi">0</span> <span class="n">LOAD_CONST</span> <span class="mi">0</span> <span class="p">(</span><span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="n">sum_two</span> <span class="n">at</span> <span class="mh">0x7fc64dcca190</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="o">></span><span class="p">)</span>
<span class="mi">2</span> <span class="n">LOAD_CONST</span> <span class="mi">1</span> <span class="p">(</span><span class="s1">'sum_two'</span><span class="p">)</span>
<span class="mi">4</span> <span class="n">MAKE_FUNCTION</span> <span class="mi">0</span>
<span class="mi">6</span> <span class="n">STORE_NAME</span> <span class="mi">0</span> <span class="p">(</span><span class="n">sum_two</span><span class="p">)</span>
<span class="mi">5</span> <span class="mi">8</span> <span class="n">LOAD_CONST</span> <span class="mi">2</span> <span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="mi">10</span> <span class="n">STORE_NAME</span> <span class="mi">1</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="mi">6</span> <span class="mi">12</span> <span class="n">LOAD_CONST</span> <span class="mi">3</span> <span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="mi">14</span> <span class="n">STORE_NAME</span> <span class="mi">2</span> <span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="mi">7</span> <span class="mi">16</span> <span class="n">LOAD_NAME</span> <span class="mi">0</span> <span class="p">(</span><span class="n">sum_two</span><span class="p">)</span>
<span class="mi">18</span> <span class="n">LOAD_NAME</span> <span class="mi">1</span> <span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="mi">20</span> <span class="n">LOAD_NAME</span> <span class="mi">2</span> <span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="mi">22</span> <span class="n">CALL_FUNCTION</span> <span class="mi">2</span>
<span class="mi">24</span> <span class="n">POP_TOP</span>
<span class="mi">9</span> <span class="mi">26</span> <span class="n">LOAD_CONST</span> <span class="mi">4</span> <span class="p">(</span><span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="o"><</span><span class="k">lambda</span><span class="o">></span> <span class="n">at</span> <span class="mh">0x7fc64dcca2f0</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">9</span><span class="o">></span><span class="p">)</span>
<span class="mi">28</span> <span class="n">LOAD_CONST</span> <span class="mi">5</span> <span class="p">(</span><span class="s1">'<lambda>'</span><span class="p">)</span>
<span class="mi">30</span> <span class="n">MAKE_FUNCTION</span> <span class="mi">0</span>
<span class="mi">32</span> <span class="n">STORE_NAME</span> <span class="mi">3</span> <span class="p">(</span><span class="n">double</span><span class="p">)</span>
<span class="mi">10</span> <span class="mi">34</span> <span class="n">LOAD_NAME</span> <span class="mi">3</span> <span class="p">(</span><span class="n">double</span><span class="p">)</span>
<span class="mi">36</span> <span class="n">LOAD_CONST</span> <span class="mi">2</span> <span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="mi">38</span> <span class="n">CALL_FUNCTION</span> <span class="mi">1</span>
<span class="mi">40</span> <span class="n">POP_TOP</span>
<span class="mi">42</span> <span class="n">LOAD_CONST</span> <span class="mi">6</span> <span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="mi">44</span> <span class="n">RETURN_VALUE</span>
<span class="n">Disassembly</span> <span class="n">of</span> <span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="n">sum_two</span> <span class="n">at</span> <span class="mh">0x7fc64dcca190</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="o">></span><span class="p">:</span>
<span class="mi">2</span> <span class="mi">0</span> <span class="n">LOAD_FAST</span> <span class="mi">0</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">2</span> <span class="n">LOAD_FAST</span> <span class="mi">1</span> <span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="mi">4</span> <span class="n">BINARY_ADD</span>
<span class="mi">6</span> <span class="n">STORE_FAST</span> <span class="mi">2</span> <span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="mi">3</span> <span class="mi">8</span> <span class="n">LOAD_FAST</span> <span class="mi">2</span> <span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="mi">10</span> <span class="n">RETURN_VALUE</span>
<span class="n">Disassembly</span> <span class="n">of</span> <span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="o"><</span><span class="k">lambda</span><span class="o">></span> <span class="n">at</span> <span class="mh">0x7fc64dcca2f0</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">9</span><span class="o">></span><span class="p">:</span>
<span class="mi">9</span> <span class="mi">0</span> <span class="n">LOAD_FAST</span> <span class="mi">0</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">2</span> <span class="n">LOAD_CONST</span> <span class="mi">1</span> <span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">4</span> <span class="n">BINARY_MULTIPLY</span>
<span class="mi">6</span> <span class="n">RETURN_VALUE</span>
</code></pre></div>
<p>khác với các khái niệm trước, khi viết function sẽ thấy python dis riêng ra từng mục cho từng function.</p>
<p>Với function sum_two tại dòng 1, có riêng mục</p>
<p><code>Disassembly of <code object sum_two at 0x7fc64dcca190, file "fun.py", line 1>:</code></p>
<p>chú ý thêm sự khác biệt khi dùng biến trong function sử dụng <code>LOAD_FAST</code> thay vì <code>LOAD_NAME</code>, tạo biến sử dụng <code>STORE_FAST</code> thay <code>STORE_NAME</code>. function kết thúc bằng việc <code>RETURN_VALUE</code>.</p>
<p>Thế nhưng.. return cái gì? câu <code>RETURN_VALUE</code> không thấy ghi thêm gì sau, làm
sao biết nó return gì?
<code>RETURN_VALUE</code> sẽ return giá trị cuối cùng tính toán được. Như trong <code>sum_two</code>
sẽ return giá trị của z sau khi <code>LOAD_FAST</code>. Với <code>lambda</code> function, return
kết quả của phép nhân <code>BINARY_MULTIPLY</code>. Cụ thể hơn, trong Python <span class="caps">VM</span> gọi đây là
<span class="caps">TOP</span> <span class="caps">OF</span> <span class="caps">STACK</span> (<span class="caps">TOS</span>), kết quả của giá trị tính toán xong sẽ luôn nằm ở đây.
Vậy nếu muốn return giá trị không phải là tính cuối cùng thì sao?</p>
<p>Thử nghiệm với 2 function không trả về giá trị vừa tính xong:</p>
<div class="highlight"><pre><span></span><code> <span class="mi">1</span> <span class="k">def</span> <span class="nf">r_none</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="mi">2</span> <span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="mi">3</span> <span class="k">return</span>
<span class="mi">4</span>
<span class="mi">5</span>
<span class="mi">6</span> <span class="k">def</span> <span class="nf">r_i</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="mi">7</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">8</span>
<span class="mi">8</span> <span class="n">z</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="mi">9</span> <span class="k">return</span> <span class="n">i</span>
</code></pre></div>
<p>Kết quả dis</p>
<div class="highlight"><pre><span></span><code><span class="n">Disassembly</span> <span class="n">of</span> <span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="n">r_none</span> <span class="n">at</span> <span class="mh">0x7fdd4b47bdf0</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="o">></span><span class="p">:</span>
<span class="mi">2</span> <span class="mi">0</span> <span class="n">LOAD_FAST</span> <span class="mi">0</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">2</span> <span class="n">LOAD_FAST</span> <span class="mi">1</span> <span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="mi">4</span> <span class="n">BINARY_ADD</span>
<span class="mi">6</span> <span class="n">STORE_FAST</span> <span class="mi">2</span> <span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="mi">3</span> <span class="mi">8</span> <span class="n">LOAD_CONST</span> <span class="mi">0</span> <span class="p">(</span><span class="kc">None</span><span class="p">)</span>
<span class="mi">10</span> <span class="n">RETURN_VALUE</span>
<span class="n">Disassembly</span> <span class="n">of</span> <span class="o"><</span><span class="n">code</span> <span class="nb">object</span> <span class="n">r_i</span> <span class="n">at</span> <span class="mh">0x7fdd4b47f030</span><span class="p">,</span> <span class="n">file</span> <span class="s2">"fun.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">6</span><span class="o">></span><span class="p">:</span>
<span class="mi">7</span> <span class="mi">0</span> <span class="n">LOAD_CONST</span> <span class="mi">1</span> <span class="p">(</span><span class="mi">8</span><span class="p">)</span>
<span class="mi">2</span> <span class="n">STORE_FAST</span> <span class="mi">2</span> <span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="mi">8</span> <span class="mi">4</span> <span class="n">LOAD_FAST</span> <span class="mi">0</span> <span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="mi">6</span> <span class="n">LOAD_FAST</span> <span class="mi">1</span> <span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="mi">8</span> <span class="n">BINARY_ADD</span>
<span class="mi">10</span> <span class="n">STORE_FAST</span> <span class="mi">3</span> <span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="mi">9</span> <span class="mi">12</span> <span class="n">LOAD_FAST</span> <span class="mi">2</span> <span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="mi">14</span> <span class="n">RETURN_VALUE</span>
</code></pre></div>
<p>function <code>r_none</code> load giá trị None để return vì code chỉ ghi return không gì cả.
function <code>r_i</code> sẽ <code>LOAD_FAST</code> giá trị <code>i</code> rồi return i.</p>
<p>Đăng ký ngay tại <a href="https://pymi.vn">PyMI.vn</a> để học Python tại Hà Nội <span class="caps">TP</span> <span class="caps">HCM</span> (Sài Gòn),
trở thành lập trình viên #python chuyên nghiệp ngay sau khóa học.</p>
</div>
<div class="back-to-top">
<a href="#top">back to top</a>
</div>
</article>
</div>
<!-- end article -->
<footer>
<div class="icons">
<a href="https://github.com/pymivn" target="_blank"><div class="icon-github icon"></div></a>
</div>
<p>© <script>document.write(moment().format('YYYY'));</script> Pymiers</p>
</footer>
</div>
</body>
</html>