aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html')
-rw-r--r--libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html188
1 files changed, 188 insertions, 0 deletions
diff --git a/libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html b/libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html
new file mode 100644
index 0000000..5d2331e
--- /dev/null
+++ b/libraries/LuaJIT-1.1.7/jitdoc/dynasm_examples.html
@@ -0,0 +1,188 @@
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2<html>
3<head>
4<title>DynASM Examples</title>
5<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6<meta name="Author" content="Mike Pall">
7<meta name="Copyright" content="Copyright (C) 2005-2011, Mike Pall">
8<meta name="Language" content="en">
9<link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
10<link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
11</head>
12<body>
13<div id="site">
14<a href="http://luajit.org/"><span>Lua<span id="logo">JIT</span></span></a>
15</div>
16<div id="head">
17<h1>DynASM Examples</h1>
18</div>
19<div id="nav">
20<ul><li>
21<a href="index.html">Index</a>
22</li><li>
23<a href="luajit.html">LuaJIT</a>
24<ul><li>
25<a href="luajit_features.html">Features</a>
26</li><li>
27<a href="luajit_install.html">Installation</a>
28</li><li>
29<a href="luajit_run.html">Running</a>
30</li><li>
31<a href="luajit_api.html">API Extensions</a>
32</li><li>
33<a href="luajit_intro.html">Introduction</a>
34</li><li>
35<a href="luajit_performance.html">Performance</a>
36</li><li>
37<a href="luajit_debug.html">Debugging</a>
38</li><li>
39<a href="luajit_changes.html">Changes</a>
40</li></ul>
41</li><li>
42<a href="coco.html">Coco</a>
43<ul><li>
44<a href="coco_portability.html">Portability</a>
45</li><li>
46<a href="coco_api.html">API Extensions</a>
47</li><li>
48<a href="coco_changes.html">Changes</a>
49</li></ul>
50</li><li>
51<a href="dynasm.html">DynASM</a>
52<ul><li>
53<a href="dynasm_features.html">Features</a>
54</li><li>
55<a class="current" href="dynasm_examples.html">Examples</a>
56</li></ul>
57</li><li>
58<a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
59</li></ul>
60</div>
61<div id="main">
62<h2>A Simple Example</h2>
63<p>
64To get you started, here is a simple code snippet to be pre-processed.
65The lines starting with '|' (the pipe symbol) are for DynASM:
66</p>
67<pre>
68 if (ptr != NULL) {
69 | mov eax, foo+17
70 | mov edx, [eax+esi*2+0x20]
71 | add ebx, [ecx+bar(ptr, 9)]
72 }
73</pre>
74<p>
75After pre-processing you get:
76</p>
77<pre>
78 if (ptr != NULL) {
79 dasm_put(Dst, 123, foo+17, bar(ptr, 9));
80 }
81</pre>
82<p style="font-size: 80%;">
83Note: yes, you usually get the assembler code as comments and proper
84CPP directives to match them up with the source. I've omitted
85them here for clarity. Oh and BTW: the pipe symbols probably
86line up much more nicely in your editor than in a browser.
87</p>
88<p>
89Here 123 is an offset into the action list buffer that
90holds the partially specified machine code. Without going
91into too much detail, the embedded C&nbsp;library implements a
92tiny bytecode engine that takes the action list as input and
93outputs machine code. It basically copies machine code snippets
94from the action list and merges them with the arguments
95passed in by <tt>dasm_put()</tt>.
96</p>
97<p>
98The arguments can be any kind of C&nbsp;expressions. In practical
99use most of them evaluate to constants (e.g. structure offsets).
100Your C&nbsp;compiler should generate very compact code out of it.
101</p>
102<p>
103The embedded C&nbsp;library knows only what's absolutely needed to
104generate proper machine code for the target CPU (e.g. variable
105displacement sizes, variable branch offset sizes and so on).
106It doesn't have a clue about other atrocities like x86 opcode
107encodings &mdash; and it doesn't need to. This dramatically
108reduces the minimum required code size to around 2K [sic!].
109</p>
110<p>
111The action list buffer itself has a pretty compact encoding, too.
112E.g. the whole action list buffer for an early version of LuaJIT
113needs only around 3K.
114</p>
115
116<h2>Advanced Features</h2>
117<p>
118Here's a real-life example taken from LuaJIT that shows some
119advanced features like type maps, macros and how to access
120C&nbsp;structures:
121</p>
122<pre>
123|.type L, lua_State, esi // L.
124|.type BASE, TValue, ebx // L->base.
125|.type TOP, TValue, edi // L->top.
126|.type CI, CallInfo, ecx // L->ci.
127|.type LCL, LClosure, eax // L->ci->func->value.
128|.type UPVAL, UpVal
129
130|.macro copyslot, D, S, R1, R2, R3
131| mov R1, S.value; mov R2, S.value.na[1]; mov R3, S.tt
132| mov D.value, R1; mov D.value.na[1], R2; mov D.tt, R3
133|.endmacro
134
135|.macro copyslot, D, S; copyslot D, S, ecx, edx, eax; .endmacro
136
137|.macro getLCL, reg
138||if (!J->pt->is_vararg) {
139| mov LCL:reg, BASE[-1].value
140||} else {
141| mov CI, L->ci
142| mov TOP, CI->func
143| mov LCL:reg, TOP->value
144||}
145|.endmacro
146
147|.macro getLCL; getLCL eax; .endmacro
148
149[...]
150
151static void jit_op_getupval(jit_State *J, int dest, int uvidx)
152{
153 | getLCL
154 | mov UPVAL:ecx, LCL->upvals[uvidx]
155 | mov TOP, UPVAL:ecx->v
156 | copyslot BASE[dest], TOP[0]
157}
158</pre>
159<p>
160And here is the pre-processed output (stripped a bit for clarity):
161</p>
162<pre>
163#define Dt1(_V) (int)&amp;(((lua_State *)0)_V)
164[...]
165static void jit_op_getupval(jit_State *J, int dest, int uvidx)
166{
167 if (!J->pt->is_vararg) {
168 dasm_put(Dst, 1164, Dt2([-1].value));
169 } else {
170 dasm_put(Dst, 1168, Dt1(->ci), Dt4(->func), Dt3(->value));
171 }
172 dasm_put(Dst, 1178, Dt5(->upvals[uvidx]), DtF(->v), Dt3([0].value),
173 Dt3([0].value.na[1]), Dt3([0].tt), Dt2([dest].value),
174 Dt2([dest].value.na[1]), Dt2([dest].tt));
175}
176</pre>
177<br class="flush">
178</div>
179<div id="foot">
180<hr class="hide">
181Copyright &copy; 2005-2011 Mike Pall
182<span class="noprint">
183&middot;
184<a href="contact.html">Contact</a>
185</span>
186</div>
187</body>
188</html>