-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynasm.html
386 lines (357 loc) · 27.8 KB
/
dynasm.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title> dynasm - DynASM with Lua mode </title>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script src="jquery-tablesorter.js"></script>
<script src="strftime.js"></script>
<script src="mustache.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="templates/reset.css" />
<link rel="stylesheet" type="text/css" href="templates/hack.css" />
<link rel="stylesheet" type="text/css" id="lights_css" />
<script>
var DOCNAME = 'dynasm'
var PROJECT = 'dynasm'
//set the lights before rendering starts
set_lights()
</script>
</head>
<body>
<header>
<div class="container">
<div class="btn-container btn-lights-container">
<a href="#" class="btn btn-lights" id="lights">lights</a>
</div>
<div class="btn-container btn-home-container">
<a href="/" class="btn btn-home">luapower</a>
</div>
<table id="header_table">
<tr>
<td style="vertical-align: middle;" width="100%">
<h1> dynasm </h1>
<h2> DynASM with Lua mode </h2>
</td>
<td style="vertical-align: middle;" align="right" style="height: 150px">
<table><tr><td>
<div class="doc" id="package_info_container">
<div id="package_info"> </div>
<div id="commit_log"> </div>
</div>
<a href="https://github.com/luapower/dynasm" class="btn btn-rightside btn-github"><span class="icon"></span>View on GitHub</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/dynasm/tarball/master" class="btn btn-rightside">Download as .tar.gz</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/dynasm/zipball/master" class="btn btn-rightside">Download as .zip</a>
</td></tr></table>
</td>
</tr>
</table>
<div class="btn-container btn-discuss-container">
<a href="https://github.com/luapower/dynasm/issues/new" target="_blank"
class="btn btn-rightside btn-discuss"><span class="icon"></span>Discuss</a>
</div>
</div>
</header>
<div class="bg-container">
<div class="bg-center-container">
<div class="bg bg-dynasm" style="background-image: url('bg/dynasm.png');"></div>
</div>
</div>
<div class="under-header">
<div class="container">
<div id="toc_container" class="toc_container doc"></div>
<button id=tab1_button class="tab_button hidden">Documentation</button>
<button id=tab2_button class="tab_button hidden">Package Info</button>
<div id="tabs_cointainer">
<div id="tab1_container">
<section class="doc">
<span id="module_info"></span>
<h2 id="local-dynasm-requiredynasm"><code>local dynasm = require'dynasm'</code></h2>
<p>This is a modified version of <a href="http://luajit.org/dynasm.html">DynASM</a> that allows generating, compiling, and running x86 and x86-64 assembly code directly from Lua. It also exposes the DynASM assembler and linker to be used as Lua modules.</p>
<p>Jump To: <a href="#examples">Examples</a> | <a href="#dynasm-api">DynASM API</a> | <a href="#dasm-api">DASM API</a> | <a href="#changes-to-dynasm">Changes to DynASM</a> | <a href="http://corsix.github.io/dynasm-doc/instructions.html">List of Instructions</a> | <a href="http://corsix.github.io/dynasm-doc/reference.html#directives">List of Directives</a></p>
<h2 id="features">Features</h2>
<ul>
<li>translate, compile and run Lua/ASM code from Lua (no C glue)</li>
<li>load Lua/ASM (.dasl) files with <code>require()</code></li>
<li>works with file, string and stream inputs and outputs</li>
</ul>
<h2 id="before-you-start">Before you start</h2>
<ol style="list-style-type: decimal">
<li><p>DynASM is <a href="http://www.corsix.org/content/what-is-dynasm">not an inline assembler</a>, it's a code generator. The following code:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">function</span> codegen<span class="ot">(</span><span class="kw">Dst</span><span class="ot">)</span>
<span class="kw">for</span> <span class="kw">i</span> <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">3</span> <span class="kw">do</span>
| <span class="kw">mov</span> <span class="kw">ax</span><span class="ot">,</span> <span class="kw">i</span>
<span class="kw">end</span>
<span class="kw">end</span></code></pre>
<p>does <em>not</em> run the assembly instruction 3 times when codegen is called, instead, it merely adds the instruction sequence <code>mov ax, 1; mov ax, 2; mov ax, 3</code> to the dynasm state <code>Dst</code> when codegen is called. Mixing Lua and ASM code like this has the effect of generating code, not running it.</p></li>
<li><p>DynASM has two parts: the assembler/preprocessor, written in Lua, and the the linker/encoder, written in C. <code>dynasm.lua</code> is the preprocessor. It takes mixed C/ASM code as input (from a file, string or file-like object) and generates C code (to a file, string, or file-like object). Alternatively, it can take mixed Lua/ASM code (like the above example) and generate Lua code, which is what the "Lua mode" part is all about. <code>dasm.lua</code> is the binding to the C part of DynASM (the linker/encoder) which deals with building the code into executable memory that can be called into.</p></li>
<li><p><code>.dasl</code> files refer to Lua/ASM files, <code>.dasc</code> files refer to C/ASM files. dasl files can be used transparently as Lua modules (they are translated on-the-fly).</p></li>
</ol>
<h2 id="examples">Examples</h2>
<h3 id="self-contained-module">1. Self-contained module</h3>
<p>This simple, self-contained module publishes the function multiply(x, y) -> x * y.</p>
<h4 id="multiply_x86.dasl"><code>multiply_x86.dasl:</code></h4>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">ffi</span> <span class="ot">=</span> require<span class="st">'ffi'</span> <span class="co">--required</span>
<span class="kw">local</span> <span class="kw">dasm</span> <span class="ot">=</span> require<span class="st">'dasm'</span> <span class="co">--required</span>
|<span class="ot">.</span><span class="kw">arch</span> <span class="kw">x86</span> <span class="co">--must be the first instruction</span>
|<span class="ot">.</span><span class="kw">actionlist</span> <span class="kw">actions</span> <span class="co">--make an action list called `actions`</span>
<span class="kw">local</span> <span class="kw">Dst</span> <span class="ot">=</span> <span class="kw">dasm</span><span class="ot">.</span>new<span class="ot">(</span><span class="kw">actions</span><span class="ot">)</span> <span class="co">--make a dasm state</span>
<span class="co">-- the next chunk of asm code will be added to the action list, and a call</span>
<span class="co">-- to `dasm.put(Dst, 0)` will be generated in its place, which will be copying</span>
<span class="co">-- the code from the start of the action list into the Dst state.</span>
| <span class="kw">mov</span> <span class="kw">eax</span><span class="ot">,</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">4</span><span class="ot">]</span>
| <span class="kw">imul</span> <span class="kw">dword</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">8</span><span class="ot">]</span>
| <span class="kw">ret</span>
<span class="kw">local</span> <span class="kw">code</span> <span class="ot">=</span> <span class="kw">Dst</span>:build<span class="ot">()</span> <span class="co">--check, link and encode the code</span>
<span class="kw">local</span> <span class="kw">fptr</span> <span class="ot">=</span> <span class="kw">ffi</span><span class="ot">.</span>cast<span class="ot">(</span><span class="st">'int32_t __cdecl (*) (int32_t x, int32_t y)'</span><span class="ot">,</span> <span class="kw">code</span><span class="ot">)</span> <span class="co">--take a callable pointer to it</span>
<span class="kw">return</span> <span class="kw">function</span><span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">_</span> <span class="ot">=</span> <span class="kw">code</span> <span class="co">--pin the code buffer so it doesn't get collected</span>
<span class="kw">return</span> fptr<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">)</span>
<span class="kw">end</span></code></pre>
<p>The best way to understand how the above code is supposed to work is to translate it:</p>
<pre><code>> lua dynasm.lua multiply_x86.dasl</code></pre>
<h4 id="main.lua"><code>main.lua</code>:</h4>
<pre class="sourceCode lua"><code class="sourceCode lua">require<span class="st">'dynasm'</span> <span class="co">--hook in the `require` loader for .dasl files</span>
<span class="kw">local</span> <span class="kw">multiply</span> <span class="ot">=</span> require<span class="st">'multiply_x86'</span> <span class="co">--translate, load and run `multiply_x86.dasl`</span>
<span class="fu">assert</span><span class="ot">(</span>multiply<span class="ot">(-</span><span class="dv">7</span><span class="ot">,</span> <span class="dv">5</span><span class="ot">)</span> <span class="ot">==</span> <span class="ot">-</span><span class="dv">35</span><span class="ot">)</span></code></pre>
<h3 id="code-gen-build-split">2. Code gen / build split</h3>
<p>This is an idea on how you can keep your asm code separated from the plumbing required to build it, and also how you can make separate functions out of different asm chunks from the same dasl file.</p>
<h4 id="funcs_x86.dasl"><code>funcs_x86.dasl</code>:</h4>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">ffi</span> <span class="ot">=</span> require<span class="st">'ffi'</span>
<span class="kw">local</span> <span class="kw">dasm</span> <span class="ot">=</span> require<span class="st">'dasm'</span>
|<span class="ot">.</span><span class="kw">arch</span> <span class="kw">x86</span>
|<span class="ot">.</span><span class="kw">actionlist</span> <span class="kw">actions</span>
|<span class="ot">.</span><span class="kw">globalnames</span> <span class="kw">globalnames</span>
<span class="kw">local</span> <span class="kw">gen</span> <span class="ot">=</span> <span class="ot">{}</span>
<span class="kw">function</span> <span class="kw">gen</span><span class="ot">.</span>mul<span class="ot">(</span><span class="kw">Dst</span><span class="ot">)</span> <span class="co">--function which generates code into the dynasm state called `Dst`</span>
|<span class="ot">-></span><span class="kw">mul</span>: <span class="co">--and returns a "make" function which gets a dasm.globals() map</span>
| <span class="kw">mov</span> <span class="kw">eax</span><span class="ot">,</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">4</span><span class="ot">]</span> <span class="co">--and returns a function that knows how to call into its code.</span>
| <span class="kw">imul</span> <span class="kw">dword</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">8</span><span class="ot">]</span>
| <span class="kw">ret</span>
<span class="kw">return</span> <span class="kw">function</span><span class="ot">(</span><span class="fu">globals</span><span class="ot">)</span>
<span class="kw">return</span> <span class="kw">ffi</span><span class="ot">.</span>cast<span class="ot">(</span><span class="st">'int32_t __cdecl (*) (int32_t x, int32_t y)'</span><span class="ot">,</span> <span class="kw">globals</span><span class="ot">.</span><span class="kw">mul</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">function</span> <span class="kw">gen</span><span class="ot">.</span>add<span class="ot">(</span><span class="kw">Dst</span><span class="ot">)</span>
|<span class="ot">-></span><span class="kw">add</span>:
| <span class="kw">mov</span> <span class="kw">eax</span><span class="ot">,</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">4</span><span class="ot">]</span>
| <span class="kw">add</span> <span class="kw">eax</span><span class="ot">,</span> <span class="kw">dword</span> <span class="ot">[</span><span class="kw">esp</span><span class="ot">+</span><span class="dv">8</span><span class="ot">]</span>
| <span class="kw">ret</span>
<span class="kw">return</span> <span class="kw">function</span><span class="ot">(</span><span class="fu">globals</span><span class="ot">)</span>
<span class="kw">return</span> <span class="kw">ffi</span><span class="ot">.</span>cast<span class="ot">(</span><span class="st">'int32_t __cdecl (*) (int32_t x, int32_t y)'</span><span class="ot">,</span> <span class="kw">globals</span><span class="ot">.</span><span class="kw">add</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="kw">return</span> <span class="ot">{</span><span class="kw">gen</span> <span class="ot">=</span> <span class="kw">gen</span><span class="ot">,</span> <span class="kw">actions</span> <span class="ot">=</span> <span class="kw">actions</span><span class="ot">,</span> <span class="kw">globalnames</span> <span class="ot">=</span> <span class="kw">globalnames</span><span class="ot">}</span></code></pre>
<h4 id="funcs.lua"><code>funcs.lua</code>:</h4>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">dynasm</span> <span class="ot">=</span> require<span class="st">'dynasm'</span>
<span class="kw">local</span> <span class="kw">dasm</span> <span class="ot">=</span> require<span class="st">'dasm'</span>
<span class="kw">local</span> <span class="kw">funcs</span> <span class="ot">=</span> require<span class="st">'funcs_x86'</span>
<span class="kw">local</span> <span class="kw">state</span><span class="ot">,</span> <span class="fu">globals</span> <span class="ot">=</span> <span class="kw">dasm</span><span class="ot">.</span>new<span class="ot">(</span><span class="kw">funcs</span><span class="ot">.</span><span class="kw">actions</span><span class="ot">)</span> <span class="co">--create a dynasm state with the generated action list</span>
<span class="kw">local</span> <span class="kw">M</span> <span class="ot">=</span> <span class="ot">{}</span> <span class="co">--generate the code, collecting the make functions</span>
<span class="kw">for</span> <span class="kw">name</span><span class="ot">,</span> <span class="kw">gen</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span><span class="kw">funcs</span><span class="ot">.</span><span class="kw">gen</span><span class="ot">)</span> <span class="kw">do</span>
<span class="kw">M</span><span class="ot">[</span><span class="kw">name</span><span class="ot">]</span> <span class="ot">=</span> gen<span class="ot">(</span><span class="kw">state</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">local</span> <span class="kw">buf</span><span class="ot">,</span> <span class="kw">size</span> <span class="ot">=</span> <span class="kw">state</span>:build<span class="ot">()</span> <span class="co">--check, link and encode the code</span>
<span class="kw">local</span> <span class="fu">globals</span> <span class="ot">=</span> <span class="kw">dasm</span><span class="ot">.</span>globals<span class="ot">(</span><span class="fu">globals</span><span class="ot">,</span> <span class="kw">funcs</span><span class="ot">.</span><span class="kw">globalnames</span><span class="ot">)</span> <span class="co">--get the map of global_name -> global_addr</span>
<span class="kw">for</span> <span class="kw">name</span><span class="ot">,</span> <span class="kw">make</span> <span class="kw">in</span> <span class="fu">pairs</span><span class="ot">(</span><span class="kw">M</span><span class="ot">)</span> <span class="kw">do</span> <span class="co">--make the callable functions</span>
<span class="kw">M</span><span class="ot">[</span><span class="kw">name</span><span class="ot">]</span> <span class="ot">=</span> make<span class="ot">(</span><span class="fu">globals</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">M</span><span class="ot">.</span><span class="kw">__buf</span> <span class="ot">=</span> <span class="kw">buf</span> <span class="co">--pin buf so it doesn't get collected</span>
<span class="kw">return</span> <span class="kw">M</span></code></pre>
<h4 id="main.lua-1"><code>main.lua</code></h4>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">funcs</span> <span class="ot">=</span> require<span class="st">'funcs'</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">funcs</span><span class="ot">.</span>mul<span class="ot">(-</span><span class="dv">7</span><span class="ot">,</span> <span class="dv">5</span><span class="ot">)</span> <span class="ot">==</span> <span class="ot">-</span><span class="dv">35</span><span class="ot">)</span>
<span class="fu">assert</span><span class="ot">(</span><span class="kw">funcs</span><span class="ot">.</span>add<span class="ot">(-</span><span class="dv">7</span><span class="ot">,</span> <span class="dv">5</span><span class="ot">)</span> <span class="ot">==</span> <span class="ot">-</span><span class="dv">2</span><span class="ot">)</span></code></pre>
<h3 id="load-code-from-a-string">3. Load code from a string</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">dynasm</span> <span class="ot">=</span> require<span class="st">'dynasm'</span>
<span class="kw">local</span> <span class="kw">gencode</span><span class="ot">,</span> <span class="kw">actions</span> <span class="ot">=</span> <span class="kw">dynasm</span><span class="ot">.</span>loadstring<span class="ot">(</span><span class="st">[[</span>
<span class="st">local ffi = require'ffi'</span>
<span class="st">local dasm = require'dasm'</span>
<span class="st">|.arch x86</span>
<span class="st">|.actionlist actions</span>
<span class="st">local function gencode(Dst)</span>
<span class="st"> | mov ax, bx</span>
<span class="st">end</span>
<span class="st">return gencode, actions</span>
<span class="st">]]</span><span class="ot">)()</span></code></pre>
<h3 id="translate-from-lua">4. Translate from Lua</h3>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">dynasm</span> <span class="ot">=</span> require<span class="st">'dynasm'</span>
<span class="fu">print</span><span class="ot">(</span><span class="kw">dynasm</span><span class="ot">.</span>translate_tostring<span class="st">'multiply_x86.dasl'</span><span class="ot">)</span></code></pre>
<p>The above is equivalent to the command line:</p>
<pre><code>> lua dynasm.lua multiply_x86.dasl</code></pre>
<blockquote>
<p><strong>Tip</strong>: You can pre-assemble <code>foo.dasl</code> into <code>foo.lua</code> -- <code>require()</code> will then choose <code>foo.lua</code> over <code>foo.dasl</code>, so you basically get transparent caching for free. This speeds up app loading a bit, and you can ship your app without the assembler (you still need to ship the linker/encoder for all the platforms that you support).</p>
</blockquote>
<h3 id="included-demotutorial">5. Included demo/tutorial</h3>
<p>Check out the included <a href="https://github.com/luapower/dynasm/blob/master/dynasm_demo_x86.dasl">dynasm_demo_x86.dasl</a> and <a href="https://github.com/luapower/dynasm/blob/master/dynasm_demo.lua">dynasm_demo.lua</a> modules for more in-depth knowledge about DynASM/Lua interaction. It works on Windows, Linux and OSX, both x86 and x64.</p>
<h3 id="brainfuck-jit-compiler">6. Brainfuck JIT compiler</h3>
<p>The examples above don't do DynASM enough justice, because DynASM was after all made for building JIT compilers. The <a href="https://github.com/luapower/bf">bf project</a> contains a Lua/ASM translation of the code from Josh Haberman's <a href="http://blog.reverberate.org/2012/12/hello-jit-world-joy-of-simple-jits.html">tutorial</a> on DynASM and JITs, and probably the simplest JIT compiler you could write. It too works on Windows, Linux and OSX, x86 and x64.</p>
<h2 id="dynasm-api">DynASM API</h2>
<table>
<col width="51%" />
<col width="48%" />
<tbody>
<tr class="odd">
<td align="left"><strong>hi-level</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">dynasm.loadfile(infile[, opt]) -> chunk</td>
<td align="left">load a dasl file and return it as a Lua chunk</td>
</tr>
<tr class="odd">
<td align="left">dynasm.loadstring(s[, opt]) -> chunk</td>
<td align="left">load a dasl string and return it as a Lua chunk</td>
</tr>
<tr class="even">
<td align="left"><strong>low-level</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">dynasm.translate(infile, outfile[, opt])</td>
<td align="left">translate a dasc or dasl file</td>
</tr>
<tr class="even">
<td align="left">dynasm.string_infile(s) -> infile</td>
<td align="left">use a string as an infile to translate()</td>
</tr>
<tr class="odd">
<td align="left">dynasm.func_outfile(func) -> outfile</td>
<td align="left">make an outfile that calls func(s) for each piece</td>
</tr>
<tr class="even">
<td align="left">dynasm.table_outfile(t) -> outfile</td>
<td align="left">make an outfile that writes pieces to a table</td>
</tr>
<tr class="odd">
<td align="left">dynasm.translate_tostring(infile[, opt]) -> s</td>
<td align="left">translate to a string</td>
</tr>
<tr class="even">
<td align="left">dynasm.translate_toiter(infile[, opt]) -> iter() -> s</td>
<td align="left">translate to an iterator of string pieces</td>
</tr>
</tbody>
</table>
<h2 id="dasm-api">DASM API</h2>
<table>
<col width="51%" />
<col width="48%" />
<tbody>
<tr class="odd">
<td align="left"><strong>hi-level</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left">dasm.new(<br />actionlist,<br />[externnames],<br />[sectioncount],<br />[globalcount],<br />[externget],<br />[globals]) -> state, globals</td>
<td align="left">make a dasm state for an action list.<br />-> per <code>.actionlist</code> directive.<br />-> per <code>.externnames</code> directive.<br />-> DASM_MAXSECTION from <code>.sections</code> directive.<br />-> DASM_MAXGLOBAL from <code>.globals</code> directive.<br />-> <code>func(externname) -> addr</code>, for solving <code>extern</code>s<br />-> <code>void*[DASM_MAXGLOBAL]</code>, to hold globals</td>
</tr>
<tr class="odd">
<td align="left">state:build() -> buf, size</td>
<td align="left">check, link, alloc, encode and mprotect the code</td>
</tr>
<tr class="even">
<td align="left">dasm.dump(buf, size)</td>
<td align="left">dump the code using the included disassembler in luajit</td>
</tr>
<tr class="odd">
<td align="left">dasm.globals(globals, globalnames) -> {name -> addr}</td>
<td align="left">given the globals array returned by dasm.new() and the globalnames list per <code>.globalnames</code> directive, return a table that maps the names to their address.</td>
</tr>
<tr class="even">
<td align="left"><strong>low-level</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left">state:init(maxsection)</td>
<td align="left">init a state</td>
</tr>
<tr class="even">
<td align="left">state:free()</td>
<td align="left">free the state</td>
</tr>
<tr class="odd">
<td align="left">state:setupglobal(globals, globalcount)</td>
<td align="left">set up the globals buffer</td>
</tr>
<tr class="even">
<td align="left">state:growpc(maxpc)</td>
<td align="left">grow the number of available pc labels</td>
</tr>
<tr class="odd">
<td align="left">state:setup(actionlist)</td>
<td align="left">set up the state with an action list</td>
</tr>
<tr class="even">
<td align="left">state:put(state, ...)</td>
<td align="left">the assembler generates these calls</td>
</tr>
<tr class="odd">
<td align="left">state:link() -> size</td>
<td align="left">link the code and get its size</td>
</tr>
<tr class="even">
<td align="left">state:encode(buf)</td>
<td align="left">encode the code into a buffer</td>
</tr>
<tr class="odd">
<td align="left">state:getpclabel(pclabel[, buf])</td>
<td align="left">get pc label offset, or pointer if buf is passed</td>
</tr>
<tr class="even">
<td align="left">state:checkstep(secmatch)</td>
<td align="left">check code before encoding</td>
</tr>
<tr class="odd">
<td align="left">state:setupextern(externnames, getter)</td>
<td align="left">set up a new <code>extern</code> handler</td>
</tr>
</tbody>
</table>
<h2 id="changes-to-dynasm">Changes to DynASM</h2>
<p>The <a href="https://github.com/luapower/dynasm/compare/7d7e130...master">source code changes</a> made to DynASM were kept to a minimum to preserve DynASM semantics, and to make it easy to add the Lua mode to other architectures supported by DynASM. As for the user-facing changes, the list is again small:</p>
<ul>
<li>added <code>-l, --lang C|Lua</code> command line option (set automatically for dasl and dasc files).</li>
<li>asm comments can start with both <code>--</code> and <code>//</code> in Lua mode.</li>
<li>the defines ARCH, OS, X86, X64, WINDOWS, LINUX, OSX are available by default in Lua mode.</li>
<li>the <code>.globals</code> directive generates DASM_MAXGLOBAL in Lua mode.</li>
<li><code>.type</code> usage is limited in Lua mode: <code>FOO.field</code>, <code>FOO[expr]</code> and <code>FOO[expr].field</code> are ok, but arbitrary expressions like <code>FOO[5].bar[2].baz</code> are not.</li>
<li><code>extern foo</code> resolves to <code>ffi.C.foo</code> by default; if foo has no cdef, <code>ffi.cdef'void foo()'</code> is called (i.e. a dummy cdef is made for it - caveat emptor).</li>
</ul>
<h2 id="assembler-tutorials-ref-docs">Assembler tutorials & ref docs</h2>
<ul>
<li><a href="https://software.intel.com/en-us/articles/introduction-to-x64-assembly/">x64 tutorial</a></li>
<li><a href="http://neilkemp.us/src/sse_tutorial/sse_tutorial.html">SSE tutorial</a></li>
<li><a href="http://softpixel.com/~cwright/programming/simd/sse.php">SSE quick ref</a></li>
<li><a href="http://www.website.masmforum.com/tutorials/fptute/index.html">FPU tutorial & ref</a></li>
<li><a href="http://www.agner.org/optimize/calling_conventions.pdf">Agner Fog - Calling Conventions</a></li>
<li><a href="http://www.agner.org/optimize/microarchitecture.pdf">Agner Fog - CPU Internals</a></li>
<li><a href="http://www.agner.org/optimize/optimizing_assembly.pdf">Agner Fog - Optimization Guide</a></li>
<li><a href="http://www.agner.org/optimize/instruction_tables.pdf">Agner Fog - Instruction Tables</a></li>
</ul>
</section>
</div>
<div id="tab2_container" class="doc"></div>
</div>
</div>
<div class="container">
<footer>
<div id="disqus_thread"></div>
<div class="faint">[email protected] | <a href="http://unlicense.org/">public domain</a></div>
</footer>
</div>
</div>
<script type="text/x-mustache" id=info_tab_template>
<h3>Modules</h3>
<ul>
{{#module_array}}
<li>{{name}}</li>
{{/module_array}}
</ul>
</script>
</body>
</html>