Document jit.status().
[luajit-2.0/celess22.git] / doc / api.html
blob2d596677af8deebc0c679fd0d5ed596a53aa7924
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <title>API Extensions</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-2010, 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>API Extensions</h1>
18 </div>
19 <div id="nav">
20 <ul><li>
21 <a href="luajit.html">LuaJIT</a>
22 <ul><li>
23 <a href="install.html">Installation</a>
24 </li><li>
25 <a href="running.html">Running</a>
26 </li><li>
27 <a class="current" href="api.html">API Extensions</a>
28 </li></ul>
29 </li><li>
30 <a href="status.html">Status</a>
31 <ul><li>
32 <a href="changes.html">Changes</a>
33 </li></ul>
34 </li><li>
35 <a href="faq.html">FAQ</a>
36 </li><li>
37 <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
38 </li></ul>
39 </div>
40 <div id="main">
41 <p>
42 LuaJIT is fully upwards-compatible with Lua 5.1. It supports all
43 <a href="http://www.lua.org/manual/5.1/manual.html#5"><span class="ext">&raquo;</span>&nbsp;standard Lua
44 library functions</a> and the full set of
45 <a href="http://www.lua.org/manual/5.1/manual.html#3"><span class="ext">&raquo;</span>&nbsp;Lua/C API
46 functions</a>.
47 </p>
48 <p>
49 LuaJIT is also fully ABI-compatible to Lua 5.1 at the linker/dynamic
50 loader level. This means you can compile a C&nbsp;module against the
51 standard Lua headers and load the same shared library from either Lua
52 or LuaJIT.
53 </p>
55 <h2 id="bit"><tt>bit.*</tt> &mdash; Bitwise Operations</h2>
56 <p>
57 LuaJIT supports all bitwise operations as defined by
58 <a href="http://bitop.luajit.org"><span class="ext">&raquo;</span>&nbsp;Lua BitOp</a>:
59 </p>
60 <pre class="code">
61 bit.tobit bit.tohex bit.bnot bit.band bit.bor bit.bxor
62 bit.lshift bit.rshift bit.arshift bit.rol bit.ror bit.bswap
63 </pre>
64 <p>
65 This module is a LuaJIT built-in &mdash; you don't need to download or
66 install Lua BitOp. The Lua BitOp site has full documentation for all
67 <a href="http://bitop.luajit.org/api.html"><span class="ext">&raquo;</span>&nbsp;Lua BitOp API functions</a>.
68 </p>
69 <p>
70 Please make sure to <tt>require</tt> the module before using any of
71 its functions:
72 </p>
73 <pre class="code">
74 local bit = require("bit")
75 </pre>
76 <p>
77 An already installed Lua BitOp module is ignored by LuaJIT.
78 This way you can use bit operations from both Lua and LuaJIT on a
79 shared installation.
80 </p>
82 <h2 id="jit"><tt>jit.*</tt> &mdash; JIT compiler control</h2>
83 <p>
84 The functions in this built-in module control the behavior
85 of the JIT compiler engine.
86 </p>
88 <h3 id="jit_onoff"><tt>jit.on()<br>
89 jit.off()</tt></h3>
90 <p>
91 Turns the whole JIT compiler on (default) or off.
92 </p>
93 <p>
94 These functions are typically used with the command line options
95 <tt>-j on</tt> or <tt>-j off</tt>.
96 </p>
98 <h3 id="jit_flush"><tt>jit.flush()</tt></h3>
99 <p>
100 Flushes the whole cache of compiled code.
101 </p>
103 <h3 id="jit_onoff_func"><tt>jit.on(func|true [,true|false])<br>
104 jit.off(func|true [,true|false])<br>
105 jit.flush(func|true [,true|false])</tt></h3>
107 <tt>jit.on</tt> enables JIT compilation for a Lua function (this is
108 the default).
109 </p>
111 <tt>jit.off</tt> disables JIT compilation for a Lua function and
112 flushes any already compiled code from the code cache.
113 </p>
115 <tt>jit.flush</tt> flushes the code, but doesn't affect the
116 enable/disable status.
117 </p>
119 The current function, i.e. the Lua function calling this library
120 function, can also be specified by passing <tt>true</tt> as the first
121 argument.
122 </p>
124 If the second argument is <tt>true</tt>, JIT compilation is also
125 enabled, disabled or flushed recursively for all subfunctions of a
126 function. With <tt>false</tt> only the subfunctions are affected.
127 </p>
129 The <tt>jit.on</tt> and <tt>jit.off</tt> functions only set a flag
130 which is checked when the function is about to be compiled. They do
131 not trigger immediate compilation.
132 </p>
134 Typical usage is <tt>jit.off(true, true)</tt> in the main chunk
135 of a module to turn off JIT compilation for the whole module for
136 debugging purposes.
137 </p>
139 <h3 id="jit_flush_tr"><tt>status = jit.flush(tr)</tt></h3>
141 Tries to flush the code for the specified trace and all of its
142 side traces from the cache. Returns <tt>true</tt> on success.
143 Returns <tt>false</tt> if there are still links to this trace.
144 </p>
146 <h3 id="jit_status"><tt>status, ... = jit.status()</tt></h3>
148 Returns the current status of the JIT compiler. The first result is
149 either <tt>true</tt> or <tt>false</tt> if the JIT compiler is turned
150 on or off. The remaining results are strings for CPU-specific features
151 and enabled optimizations.
152 </p>
154 <h3 id="jit_version"><tt>jit.version</tt></h3>
156 Contains the LuaJIT version string.
157 </p>
159 <h3 id="jit_version_num"><tt>jit.version_num</tt></h3>
161 Contains the version number of the LuaJIT core. Version xx.yy.zz
162 is represented by the decimal number xxyyzz.
163 </p>
165 <h3 id="jit_arch"><tt>jit.arch</tt></h3>
167 Contains the target architecture name (CPU and optional ABI).
168 </p>
170 <h2 id="jit_opt"><tt>jit.opt.*</tt> &mdash; JIT compiler optimization control</h2>
172 This module provides the backend for the <tt>-O</tt> command line
173 option.
174 </p>
176 You can also use it programmatically, e.g.:
177 </p>
178 <pre class="code">
179 jit.opt.start(2) -- same as -O2
180 jit.opt.start("-dce")
181 jit.opt.start("hotloop=10", "hotexit=2")
182 </pre>
184 Unlike in LuaJIT 1.x, the module is built-in and
185 <b>optimization is turned on by default!</b>
186 It's no longer necessary to run <tt>require("jit.opt").start()</tt>,
187 which was one of the ways to enable optimization.
188 </p>
190 <h2 id="jit_util"><tt>jit.util.*</tt> &mdash; JIT compiler introspection</h2>
192 This module holds functions to introspect the bytecode, generated
193 traces, the IR and the generated machine code. The functionality
194 provided by this module is still in flux and therefore undocumented.
195 </p>
197 The debug modules <tt>-jbc</tt>, <tt>-jv</tt> and <tt>-jdump</tt> make
198 extensive use of these functions. Please check out their source code,
199 if you want to know more.
200 </p>
202 <h2 id="c_api">C API extensions</h2>
204 LuaJIT adds some extensions to the Lua/C API. The LuaJIT include
205 directory must be in the compiler search path (<tt>-I<i>path</i></tt>)
206 to be able to include the required header for C code:
207 </p>
208 <pre class="code">
209 #include "luajit.h"
210 </pre>
212 Or for C++ code:
213 </p>
214 <pre class="code">
215 #include "lua.hpp"
216 </pre>
218 <h2 id="luaJIT_setmode"><tt>luaJIT_setmode(L, idx, mode)</tt>
219 &mdash; Control VM</h2>
221 This is a C API extension to allow control of the VM from C code. The
222 full prototype of <tt>LuaJIT_setmode</tt> is:
223 </p>
224 <pre class="code">
225 LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
226 </pre>
228 The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>).
229 The second argument is either <tt>0</tt> or a stack index (similar to the
230 other Lua/C API functions).
231 </p>
233 The third argument specifies the mode, which is 'or'ed with a flag.
234 The flag can be <tt>LUAJIT_MODE_OFF</tt> to turn a feature on,
235 <tt>LUAJIT_MODE_ON</tt> to turn a feature off, or
236 <tt>LUAJIT_MODE_FLUSH</tt> to flush cached code.
237 </p>
239 The following modes are defined:
240 </p>
242 <h3 id="mode_engine"><tt>luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)</tt></h3>
244 Turn the whole JIT compiler on or off or flush the whole cache of compiled code.
245 </p>
247 <h3 id="mode_func"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)</tt><br>
248 <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)</tt><br>
249 <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)</tt></h3>
251 This sets the mode for the function at the stack index <tt>idx</tt> or
252 the parent of the calling function (<tt>idx = 0</tt>). It either
253 enables JIT compilation for a function, disables it and flushes any
254 already compiled code or only flushes already compiled code. This
255 applies recursively to all subfunctions of the function with
256 <tt>LUAJIT_MODE_ALLFUNC</tt> or only to the subfunctions with
257 <tt>LUAJIT_MODE_ALLSUBFUNC</tt>.
258 </p>
260 <h3 id="mode_engine"><tt>luaJIT_setmode(L, trace,<br>
261 &nbsp;&nbsp;LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3>
263 Tries to flush the code for the specified trace and all of its
264 side traces from the cache.
265 </p>
267 <h3 id="mode_engine"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)</tt></h3>
269 This mode defines a wrapper function for calls to C functions. If
270 called with <tt>LUAJIT_MODE_ON</tt>, the stack index at <tt>idx</tt>
271 must be a <tt>lightuserdata</tt> object holding a pointer to the wrapper
272 function. From now on all C functions are called through the wrapper
273 function. If called with <tt>LUAJIT_MODE_OFF</tt> this mode is turned
274 off and all C functions are directly called.
275 </p>
277 The wrapper function can be used for debugging purposes or to catch
278 and convert foreign exceptions. Recommended usage can be seen in this
279 C++ code excerpt:
280 </p>
281 <pre class="code">
282 #include &lt;exception&gt;
283 #include "lua.hpp"
285 // Catch C++ exceptions and convert them to Lua error messages.
286 // Customize as needed for your own exception classes.
287 static int wrap_exceptions(lua_State *L, lua_CFunction f)
289 try {
290 return f(L); // Call wrapped function and return result.
291 } catch (const char *s) { // Catch and convert exceptions.
292 lua_pushstring(L, s);
293 } catch (std::exception& e) {
294 lua_pushstring(L, e.what());
295 } catch (...) {
296 lua_pushliteral(L, "caught (...)");
298 return lua_error(L); // Rethrow as a Lua error.
301 static int myinit(lua_State *L)
304 // Define wrapper function and enable it.
305 lua_pushlightuserdata(L, (void *)wrap_exceptions);
306 luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
307 lua_pop(L, 1);
310 </pre>
312 Note that you can only define <b>a single global wrapper function</b>,
313 so be careful when using this mechanism from multiple C++ modules.
314 Also note that this mechanism is not without overhead.
315 </p>
317 LuaJIT already intercepts exception handling for systems using DWARF2
318 stack unwinding (e.g. Linux or OSX) and for Windows/x64 (but <b>not</b>
319 for Windows/x86). This is a zero-cost mechanism and always enabled.
320 You don't need to use any wrapper functions, except when you want to get
321 a more specific error message than <tt>"C++&nbsp;exception"</tt>.
322 </p>
323 <br class="flush">
324 </div>
325 <div id="foot">
326 <hr class="hide">
327 Copyright &copy; 2005-2010 Mike Pall
328 <span class="noprint">
329 &middot;
330 <a href="contact.html">Contact</a>
331 </span>
332 </div>
333 </body>
334 </html>