sljitLir.h revision 1.1 1 1.1 alnsn /*
2 1.1 alnsn * Stack-less Just-In-Time compiler
3 1.1 alnsn *
4 1.1 alnsn * Copyright 2009-2012 Zoltan Herczeg (hzmester (at) freemail.hu). All rights reserved.
5 1.1 alnsn *
6 1.1 alnsn * Redistribution and use in source and binary forms, with or without modification, are
7 1.1 alnsn * permitted provided that the following conditions are met:
8 1.1 alnsn *
9 1.1 alnsn * 1. Redistributions of source code must retain the above copyright notice, this list of
10 1.1 alnsn * conditions and the following disclaimer.
11 1.1 alnsn *
12 1.1 alnsn * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 1.1 alnsn * of conditions and the following disclaimer in the documentation and/or other materials
14 1.1 alnsn * provided with the distribution.
15 1.1 alnsn *
16 1.1 alnsn * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 1.1 alnsn * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 alnsn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 1.1 alnsn * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 alnsn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 1.1 alnsn * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 1.1 alnsn * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 1.1 alnsn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 1.1 alnsn * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 alnsn */
26 1.1 alnsn
27 1.1 alnsn #ifndef _SLJIT_LIR_H_
28 1.1 alnsn #define _SLJIT_LIR_H_
29 1.1 alnsn
30 1.1 alnsn /*
31 1.1 alnsn ------------------------------------------------------------------------
32 1.1 alnsn Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
33 1.1 alnsn ------------------------------------------------------------------------
34 1.1 alnsn
35 1.1 alnsn Short description
36 1.1 alnsn Advantages:
37 1.1 alnsn - The execution can be continued from any LIR instruction
38 1.1 alnsn In other words, jump into and out of the code is safe
39 1.1 alnsn - Both target of (conditional) jump and call instructions
40 1.1 alnsn and constants can be dynamically modified during runtime
41 1.1 alnsn - although it is not suggested to do it frequently
42 1.1 alnsn - very effective to cache an important value once
43 1.1 alnsn - A fixed stack space can be allocated for local variables
44 1.1 alnsn - The compiler is thread-safe
45 1.1 alnsn - The compiler is highly configurable through preprocessor macros.
46 1.1 alnsn You can disable unneeded features (multithreading in single
47 1.1 alnsn threaded applications), and you can use your own system functions
48 1.1 alnsn (including memory allocators). See sljitConfig.h
49 1.1 alnsn Disadvantages:
50 1.1 alnsn - Limited number of registers (only 6+4 integer registers, max 3+2
51 1.1 alnsn temporary, max 3+2 saved and 4 floating point registers)
52 1.1 alnsn In practice:
53 1.1 alnsn - This approach is very effective for interpreters
54 1.1 alnsn - One of the saved registers typically points to a stack interface
55 1.1 alnsn - It can jump to any exception handler anytime (even for another
56 1.1 alnsn function. It is safe for SLJIT.)
57 1.1 alnsn - Fast paths can be modified during runtime reflecting the changes
58 1.1 alnsn of the fastest execution path of the dynamic language
59 1.1 alnsn - SLJIT supports complex memory addressing modes
60 1.1 alnsn - mainly position independent code
61 1.1 alnsn - Optimizations (perhaps later)
62 1.1 alnsn - Only for basic blocks (when no labels inserted between LIR instructions)
63 1.1 alnsn
64 1.1 alnsn For valgrind users:
65 1.1 alnsn - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
66 1.1 alnsn */
67 1.1 alnsn
68 1.1 alnsn #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
69 1.1 alnsn #include "sljitConfig.h"
70 1.1 alnsn #endif
71 1.1 alnsn
72 1.1 alnsn /* The following header file defines useful macros for fine tuning
73 1.1 alnsn sljit based code generators. They are listed in the begining
74 1.1 alnsn of sljitConfigInternal.h */
75 1.1 alnsn
76 1.1 alnsn #include "sljitConfigInternal.h"
77 1.1 alnsn
78 1.1 alnsn /* --------------------------------------------------------------------- */
79 1.1 alnsn /* Error codes */
80 1.1 alnsn /* --------------------------------------------------------------------- */
81 1.1 alnsn
82 1.1 alnsn /* Indicates no error. */
83 1.1 alnsn #define SLJIT_SUCCESS 0
84 1.1 alnsn /* After the call of sljit_generate_code(), the error code of the compiler
85 1.1 alnsn is set to this value to avoid future sljit calls (in debug mode at least).
86 1.1 alnsn The complier should be freed after sljit_generate_code(). */
87 1.1 alnsn #define SLJIT_ERR_COMPILED 1
88 1.1 alnsn /* Cannot allocate non executable memory. */
89 1.1 alnsn #define SLJIT_ERR_ALLOC_FAILED 2
90 1.1 alnsn /* Cannot allocate executable memory.
91 1.1 alnsn Only for sljit_generate_code() */
92 1.1 alnsn #define SLJIT_ERR_EX_ALLOC_FAILED 3
93 1.1 alnsn /* return value for SLJIT_CONFIG_UNSUPPORTED empty architecture. */
94 1.1 alnsn #define SLJIT_ERR_UNSUPPORTED 4
95 1.1 alnsn
96 1.1 alnsn /* --------------------------------------------------------------------- */
97 1.1 alnsn /* Registers */
98 1.1 alnsn /* --------------------------------------------------------------------- */
99 1.1 alnsn
100 1.1 alnsn #define SLJIT_UNUSED 0
101 1.1 alnsn
102 1.1 alnsn /* Temporary (scratch) registers may not preserve their values across function calls. */
103 1.1 alnsn #define SLJIT_TEMPORARY_REG1 1
104 1.1 alnsn #define SLJIT_TEMPORARY_REG2 2
105 1.1 alnsn #define SLJIT_TEMPORARY_REG3 3
106 1.1 alnsn /* Note: Extra Registers cannot be used for memory addressing. */
107 1.1 alnsn /* Note: on x86-32, these registers are emulated (using stack loads & stores). */
108 1.1 alnsn #define SLJIT_TEMPORARY_EREG1 4
109 1.1 alnsn #define SLJIT_TEMPORARY_EREG2 5
110 1.1 alnsn
111 1.1 alnsn /* Saved registers whose preserve their values across function calls. */
112 1.1 alnsn #define SLJIT_SAVED_REG1 6
113 1.1 alnsn #define SLJIT_SAVED_REG2 7
114 1.1 alnsn #define SLJIT_SAVED_REG3 8
115 1.1 alnsn /* Note: Extra Registers cannot be used for memory addressing. */
116 1.1 alnsn /* Note: on x86-32, these registers are emulated (using stack loads & stores). */
117 1.1 alnsn #define SLJIT_SAVED_EREG1 9
118 1.1 alnsn #define SLJIT_SAVED_EREG2 10
119 1.1 alnsn
120 1.1 alnsn /* Read-only register (cannot be the destination of an operation).
121 1.1 alnsn Only SLJIT_MEM1(SLJIT_LOCALS_REG) addressing mode is allowed since
122 1.1 alnsn several ABIs has certain limitations about the stack layout. However
123 1.1 alnsn sljit_get_local_base() can be used to obtain the offset of a value. */
124 1.1 alnsn #define SLJIT_LOCALS_REG 11
125 1.1 alnsn
126 1.1 alnsn /* Number of registers. */
127 1.1 alnsn #define SLJIT_NO_TMP_REGISTERS 5
128 1.1 alnsn #define SLJIT_NO_GEN_REGISTERS 5
129 1.1 alnsn #define SLJIT_NO_REGISTERS 11
130 1.1 alnsn
131 1.1 alnsn /* Return with machine word. */
132 1.1 alnsn
133 1.1 alnsn #define SLJIT_RETURN_REG SLJIT_TEMPORARY_REG1
134 1.1 alnsn
135 1.1 alnsn /* x86 prefers specific registers for special purposes. In case of shift
136 1.1 alnsn by register it supports only SLJIT_TEMPORARY_REG3 for shift argument
137 1.1 alnsn (which is the src2 argument of sljit_emit_op2). If another register is
138 1.1 alnsn used, sljit must exchange data between registers which cause a minor
139 1.1 alnsn slowdown. Other architectures has no such limitation. */
140 1.1 alnsn
141 1.1 alnsn #define SLJIT_PREF_SHIFT_REG SLJIT_TEMPORARY_REG3
142 1.1 alnsn
143 1.1 alnsn /* --------------------------------------------------------------------- */
144 1.1 alnsn /* Floating point registers */
145 1.1 alnsn /* --------------------------------------------------------------------- */
146 1.1 alnsn
147 1.1 alnsn /* Note: SLJIT_UNUSED as destination is not valid for floating point
148 1.1 alnsn operations, since they cannot be used for setting flags. */
149 1.1 alnsn
150 1.1 alnsn /* Floating point operations are performed on double precision values. */
151 1.1 alnsn
152 1.1 alnsn #define SLJIT_FLOAT_REG1 1
153 1.1 alnsn #define SLJIT_FLOAT_REG2 2
154 1.1 alnsn #define SLJIT_FLOAT_REG3 3
155 1.1 alnsn #define SLJIT_FLOAT_REG4 4
156 1.1 alnsn
157 1.1 alnsn /* --------------------------------------------------------------------- */
158 1.1 alnsn /* Main structures and functions */
159 1.1 alnsn /* --------------------------------------------------------------------- */
160 1.1 alnsn
161 1.1 alnsn struct sljit_memory_fragment {
162 1.1 alnsn struct sljit_memory_fragment *next;
163 1.1 alnsn sljit_uw used_size;
164 1.1 alnsn sljit_ub memory[1];
165 1.1 alnsn };
166 1.1 alnsn
167 1.1 alnsn struct sljit_label {
168 1.1 alnsn struct sljit_label *next;
169 1.1 alnsn sljit_uw addr;
170 1.1 alnsn /* The maximum size difference. */
171 1.1 alnsn sljit_uw size;
172 1.1 alnsn };
173 1.1 alnsn
174 1.1 alnsn struct sljit_jump {
175 1.1 alnsn struct sljit_jump *next;
176 1.1 alnsn sljit_uw addr;
177 1.1 alnsn sljit_w flags;
178 1.1 alnsn union {
179 1.1 alnsn sljit_uw target;
180 1.1 alnsn struct sljit_label* label;
181 1.1 alnsn } u;
182 1.1 alnsn };
183 1.1 alnsn
184 1.1 alnsn struct sljit_const {
185 1.1 alnsn struct sljit_const *next;
186 1.1 alnsn sljit_uw addr;
187 1.1 alnsn };
188 1.1 alnsn
189 1.1 alnsn struct sljit_compiler {
190 1.1 alnsn int error;
191 1.1 alnsn
192 1.1 alnsn struct sljit_label *labels;
193 1.1 alnsn struct sljit_jump *jumps;
194 1.1 alnsn struct sljit_const *consts;
195 1.1 alnsn struct sljit_label *last_label;
196 1.1 alnsn struct sljit_jump *last_jump;
197 1.1 alnsn struct sljit_const *last_const;
198 1.1 alnsn
199 1.1 alnsn struct sljit_memory_fragment *buf;
200 1.1 alnsn struct sljit_memory_fragment *abuf;
201 1.1 alnsn
202 1.1 alnsn /* Used local registers. */
203 1.1 alnsn int temporaries;
204 1.1 alnsn /* Used saved registers. */
205 1.1 alnsn int saveds;
206 1.1 alnsn /* Local stack size. */
207 1.1 alnsn int local_size;
208 1.1 alnsn /* Code size. */
209 1.1 alnsn sljit_uw size;
210 1.1 alnsn /* For statistical purposes. */
211 1.1 alnsn sljit_uw executable_size;
212 1.1 alnsn
213 1.1 alnsn #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
214 1.1 alnsn int args;
215 1.1 alnsn int locals_offset;
216 1.1 alnsn int temporaries_start;
217 1.1 alnsn int saveds_start;
218 1.1 alnsn #endif
219 1.1 alnsn
220 1.1 alnsn #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
221 1.1 alnsn int mode32;
222 1.1 alnsn #endif
223 1.1 alnsn
224 1.1 alnsn #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
225 1.1 alnsn int flags_saved;
226 1.1 alnsn #endif
227 1.1 alnsn
228 1.1 alnsn #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
229 1.1 alnsn /* Constant pool handling. */
230 1.1 alnsn sljit_uw *cpool;
231 1.1 alnsn sljit_ub *cpool_unique;
232 1.1 alnsn sljit_uw cpool_diff;
233 1.1 alnsn sljit_uw cpool_fill;
234 1.1 alnsn /* Other members. */
235 1.1 alnsn /* Contains pointer, "ldr pc, [...]" pairs. */
236 1.1 alnsn sljit_uw patches;
237 1.1 alnsn #endif
238 1.1 alnsn
239 1.1 alnsn #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
240 1.1 alnsn /* Temporary fields. */
241 1.1 alnsn sljit_uw shift_imm;
242 1.1 alnsn int cache_arg;
243 1.1 alnsn sljit_w cache_argw;
244 1.1 alnsn #endif
245 1.1 alnsn
246 1.1 alnsn #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
247 1.1 alnsn int cache_arg;
248 1.1 alnsn sljit_w cache_argw;
249 1.1 alnsn #endif
250 1.1 alnsn
251 1.1 alnsn #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
252 1.1 alnsn sljit_w imm;
253 1.1 alnsn int cache_arg;
254 1.1 alnsn sljit_w cache_argw;
255 1.1 alnsn #endif
256 1.1 alnsn
257 1.1 alnsn #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
258 1.1 alnsn int delay_slot;
259 1.1 alnsn int cache_arg;
260 1.1 alnsn sljit_w cache_argw;
261 1.1 alnsn #endif
262 1.1 alnsn
263 1.1 alnsn #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
264 1.1 alnsn FILE* verbose;
265 1.1 alnsn #endif
266 1.1 alnsn
267 1.1 alnsn #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
268 1.1 alnsn /* Local size passed to the functions. */
269 1.1 alnsn int logical_local_size;
270 1.1 alnsn #endif
271 1.1 alnsn
272 1.1 alnsn #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
273 1.1 alnsn int skip_checks;
274 1.1 alnsn #endif
275 1.1 alnsn };
276 1.1 alnsn
277 1.1 alnsn /* --------------------------------------------------------------------- */
278 1.1 alnsn /* Main functions */
279 1.1 alnsn /* --------------------------------------------------------------------- */
280 1.1 alnsn
281 1.1 alnsn /* Creates an sljit compiler.
282 1.1 alnsn Returns NULL if failed. */
283 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void);
284 1.1 alnsn /* Free everything except the codes. */
285 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
286 1.1 alnsn
287 1.1 alnsn static SLJIT_INLINE int sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
288 1.1 alnsn
289 1.1 alnsn /*
290 1.1 alnsn Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
291 1.1 alnsn and <= 128 bytes on 64 bit architectures. The memory area is owned by the compiler,
292 1.1 alnsn and freed by sljit_free_compiler. The returned pointer is sizeof(sljit_w) aligned.
293 1.1 alnsn Excellent for allocating small blocks during the compiling, and no need to worry
294 1.1 alnsn about freeing them. The size is enough to contain at most 16 pointers.
295 1.1 alnsn If the size is outside of the range, the function will return with NULL,
296 1.1 alnsn but this return value does not indicate that there is no more memory (does
297 1.1 alnsn not set the compiler to out-of-memory status).
298 1.1 alnsn */
299 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size);
300 1.1 alnsn
301 1.1 alnsn #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
302 1.1 alnsn /* Passing NULL disables verbose. */
303 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
304 1.1 alnsn #endif
305 1.1 alnsn
306 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
307 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
308 1.1 alnsn
309 1.1 alnsn /*
310 1.1 alnsn After the code generation we can retrieve the allocated executable memory size,
311 1.1 alnsn although this area may not be fully filled with instructions depending on some
312 1.1 alnsn optimizations. This function is useful only for statistical purposes.
313 1.1 alnsn
314 1.1 alnsn Before a successful code generation, this function returns with 0.
315 1.1 alnsn */
316 1.1 alnsn static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
317 1.1 alnsn
318 1.1 alnsn /* Instruction generation. Returns with error code. */
319 1.1 alnsn
320 1.1 alnsn /*
321 1.1 alnsn The executable code is basically a function call from the viewpoint of
322 1.1 alnsn the C language. The function calls must obey to the ABI (Application
323 1.1 alnsn Binary Interface) of the platform, which specify the purpose of machine
324 1.1 alnsn registers and stack handling among other things. The sljit_emit_enter
325 1.1 alnsn function emits the necessary instructions for setting up a new context
326 1.1 alnsn for the executable code and moves function arguments to the saved
327 1.1 alnsn registers. The number of arguments are specified in the "args"
328 1.1 alnsn parameter and the first argument goes to SLJIT_SAVED_REG1, the second
329 1.1 alnsn goes to SLJIT_SAVED_REG2 and so on. The number of temporary and
330 1.1 alnsn saved registers are passed in "temporaries" and "saveds" arguments
331 1.1 alnsn respectively. Since the saved registers contains the arguments,
332 1.1 alnsn "args" must be less or equal than "saveds". The sljit_emit_enter
333 1.1 alnsn is also capable of allocating a stack space for local variables. The
334 1.1 alnsn "local_size" argument contains the size in bytes of this local area
335 1.1 alnsn and its staring address is stored in SLJIT_LOCALS_REG. However
336 1.1 alnsn the SLJIT_LOCALS_REG is not necessary the machine stack pointer.
337 1.1 alnsn The memory bytes between SLJIT_LOCALS_REG (inclusive) and
338 1.1 alnsn SLJIT_LOCALS_REG + local_size (exclusive) can be modified freely
339 1.1 alnsn until the function returns. The stack space is uninitialized.
340 1.1 alnsn
341 1.1 alnsn Note: every call of sljit_emit_enter and sljit_set_context overwrites
342 1.1 alnsn the previous context. */
343 1.1 alnsn
344 1.1 alnsn #define SLJIT_MAX_LOCAL_SIZE 65536
345 1.1 alnsn
346 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler,
347 1.1 alnsn int args, int temporaries, int saveds, int local_size);
348 1.1 alnsn
349 1.1 alnsn /* The machine code has a context (which contains the local stack space size,
350 1.1 alnsn number of used registers, etc.) which initialized by sljit_emit_enter. Several
351 1.1 alnsn functions (like sljit_emit_return) requres this context to be able to generate
352 1.1 alnsn the appropriate code. However, some code fragments (like inline cache) may have
353 1.1 alnsn no normal entry point so their context is unknown for the compiler. Using the
354 1.1 alnsn function below we can specify thir context.
355 1.1 alnsn
356 1.1 alnsn Note: every call of sljit_emit_enter and sljit_set_context overwrites
357 1.1 alnsn the previous context. */
358 1.1 alnsn
359 1.1 alnsn /* Note: multiple calls of this function overwrites the previous call. */
360 1.1 alnsn
361 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler,
362 1.1 alnsn int args, int temporaries, int saveds, int local_size);
363 1.1 alnsn
364 1.1 alnsn /* Return from machine code. The op argument can be SLJIT_UNUSED which means the
365 1.1 alnsn function does not return with anything or any opcode between SLJIT_MOV and
366 1.1 alnsn SLJIT_MOV_SI (see sljit_emit_op1). As for src and srcw they must be 0 if op
367 1.1 alnsn is SLJIT_UNUSED, otherwise see below the description about source and
368 1.1 alnsn destination arguments. */
369 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int op,
370 1.1 alnsn int src, sljit_w srcw);
371 1.1 alnsn
372 1.1 alnsn /* Really fast calling method for utility functions inside sljit (see SLJIT_FAST_CALL).
373 1.1 alnsn All registers and even the stack frame is passed to the callee. The return address is
374 1.1 alnsn preserved in dst/dstw by sljit_emit_fast_enter, and sljit_emit_fast_return can
375 1.1 alnsn use this as a return value later. */
376 1.1 alnsn
377 1.1 alnsn /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine instructions
378 1.1 alnsn are needed. Excellent for small uility functions, where saving registers and setting up
379 1.1 alnsn a new stack frame would cost too much performance. However, it is still possible to return
380 1.1 alnsn to the address of the caller (or anywhere else). */
381 1.1 alnsn
382 1.1 alnsn /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
383 1.1 alnsn
384 1.1 alnsn /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
385 1.1 alnsn since many architectures do clever branch prediction on call / return instruction pairs. */
386 1.1 alnsn
387 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw);
388 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw);
389 1.1 alnsn
390 1.1 alnsn /*
391 1.1 alnsn Source and destination values for arithmetical instructions
392 1.1 alnsn imm - a simple immediate value (cannot be used as a destination)
393 1.1 alnsn reg - any of the registers (immediate argument must be 0)
394 1.1 alnsn [imm] - absolute immediate memory address
395 1.1 alnsn [reg+imm] - indirect memory address
396 1.1 alnsn [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
397 1.1 alnsn useful for (byte, half, int, sljit_w) array access
398 1.1 alnsn (fully supported by both x86 and ARM architectures, and cheap operation on others)
399 1.1 alnsn */
400 1.1 alnsn
401 1.1 alnsn /*
402 1.1 alnsn IMPORATNT NOTE: memory access MUST be naturally aligned except
403 1.1 alnsn SLJIT_UNALIGNED macro is defined and its value is 1.
404 1.1 alnsn
405 1.1 alnsn length | alignment
406 1.1 alnsn ---------+-----------
407 1.1 alnsn byte | 1 byte (not aligned)
408 1.1 alnsn half | 2 byte (real_address & 0x1 == 0)
409 1.1 alnsn int | 4 byte (real_address & 0x3 == 0)
410 1.1 alnsn sljit_w | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
411 1.1 alnsn | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
412 1.1 alnsn
413 1.1 alnsn Note: different architectures have different addressing limitations
414 1.1 alnsn Thus sljit may generate several instructions for other addressing modes
415 1.1 alnsn x86: all addressing modes supported, but write-back is not supported
416 1.1 alnsn (requires an extra instruction). On x86-64 only 32 bit signed
417 1.1 alnsn integers are supported by the architecture.
418 1.1 alnsn arm: [reg+imm] supported for small immediates (-4095 <= imm <= 4095
419 1.1 alnsn or -255 <= imm <= 255 for loading signed bytes, any halfs or doubles)
420 1.1 alnsn [reg+(reg<<imm)] are supported or requires only two instructions
421 1.1 alnsn Write back is limited to small immediates on thumb2
422 1.1 alnsn ppc: [reg+imm], -65535 <= imm <= 65535. 64 bit moves requires immediates
423 1.1 alnsn divisible by 4. [reg+reg] supported, write-back supported
424 1.1 alnsn [reg+(reg<<imm)] (imm != 0) is cheap (requires two instructions)
425 1.1 alnsn */
426 1.1 alnsn
427 1.1 alnsn /* Register output: simply the name of the register.
428 1.1 alnsn For destination, you can use SLJIT_UNUSED as well. */
429 1.1 alnsn #define SLJIT_MEM 0x100
430 1.1 alnsn #define SLJIT_MEM0() (SLJIT_MEM)
431 1.1 alnsn #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
432 1.1 alnsn #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 4))
433 1.1 alnsn #define SLJIT_IMM 0x200
434 1.1 alnsn
435 1.1 alnsn /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
436 1.1 alnsn 32 bit CPUs. The arithmetic instruction uses only the lower 32 bit of the
437 1.1 alnsn input register(s), and set the flags according to the 32 bit result. If the
438 1.1 alnsn destination is a register, the higher 32 bit of the result is undefined.
439 1.1 alnsn The addressing modes (SLJIT_MEM1/SLJIT_MEM2 macros) are unaffected by this flag. */
440 1.1 alnsn #define SLJIT_INT_OP 0x100
441 1.1 alnsn
442 1.1 alnsn /* Common CPU status flags for all architectures (x86, ARM, PPC)
443 1.1 alnsn - carry flag
444 1.1 alnsn - overflow flag
445 1.1 alnsn - zero flag
446 1.1 alnsn - negative/positive flag (depends on arc)
447 1.1 alnsn On mips, these flags are emulated by software. */
448 1.1 alnsn
449 1.1 alnsn /* By default, the instructions may, or may not set the CPU status flags.
450 1.1 alnsn Forcing to set or keep status flags can be done with the following flags: */
451 1.1 alnsn
452 1.1 alnsn /* Note: sljit tries to emit the minimum number of instructions. Using these
453 1.1 alnsn flags can increase them, so use them wisely to avoid unnecessary code generation. */
454 1.1 alnsn
455 1.1 alnsn /* Set Equal (Zero) status flag (E). */
456 1.1 alnsn #define SLJIT_SET_E 0x0200
457 1.1 alnsn /* Set signed status flag (S). */
458 1.1 alnsn #define SLJIT_SET_S 0x0400
459 1.1 alnsn /* Set unsgined status flag (U). */
460 1.1 alnsn #define SLJIT_SET_U 0x0800
461 1.1 alnsn /* Set signed overflow flag (O). */
462 1.1 alnsn #define SLJIT_SET_O 0x1000
463 1.1 alnsn /* Set carry flag (C).
464 1.1 alnsn Note: Kinda unsigned overflow, but behaves differently on various cpus. */
465 1.1 alnsn #define SLJIT_SET_C 0x2000
466 1.1 alnsn /* Do not modify the flags (K).
467 1.1 alnsn Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
468 1.1 alnsn #define SLJIT_KEEP_FLAGS 0x4000
469 1.1 alnsn
470 1.1 alnsn /* Notes:
471 1.1 alnsn - you cannot postpone conditional jump instructions except if noted that
472 1.1 alnsn the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
473 1.1 alnsn - flag combinations: '|' means 'logical or'. */
474 1.1 alnsn
475 1.1 alnsn /* Flags: - (never set any flags)
476 1.1 alnsn Note: breakpoint instruction is not supported by all architectures (namely ppc)
477 1.1 alnsn It falls back to SLJIT_NOP in those cases. */
478 1.1 alnsn #define SLJIT_BREAKPOINT 0
479 1.1 alnsn /* Flags: - (never set any flags)
480 1.1 alnsn Note: may or may not cause an extra cycle wait
481 1.1 alnsn it can even decrease the runtime in a few cases. */
482 1.1 alnsn #define SLJIT_NOP 1
483 1.1 alnsn /* Flags: may destroy flags
484 1.1 alnsn Unsigned multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
485 1.1 alnsn Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
486 1.1 alnsn #define SLJIT_UMUL 2
487 1.1 alnsn /* Flags: may destroy flags
488 1.1 alnsn Signed multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
489 1.1 alnsn Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
490 1.1 alnsn #define SLJIT_SMUL 3
491 1.1 alnsn /* Flags: I | may destroy flags
492 1.1 alnsn Unsigned divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
493 1.1 alnsn The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
494 1.1 alnsn Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
495 1.1 alnsn #define SLJIT_UDIV 4
496 1.1 alnsn /* Flags: I | may destroy flags
497 1.1 alnsn Signed divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
498 1.1 alnsn The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
499 1.1 alnsn Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
500 1.1 alnsn #define SLJIT_SDIV 5
501 1.1 alnsn
502 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op);
503 1.1 alnsn
504 1.1 alnsn /* Notes for MOV instructions:
505 1.1 alnsn U = Mov with update (post form). If source or destination defined as SLJIT_MEM1(r1)
506 1.1 alnsn or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
507 1.1 alnsn UB = unsigned byte (8 bit)
508 1.1 alnsn SB = signed byte (8 bit)
509 1.1 alnsn UH = unsgined half (16 bit)
510 1.1 alnsn SH = unsgined half (16 bit) */
511 1.1 alnsn
512 1.1 alnsn /* Flags: - (never set any flags) */
513 1.1 alnsn #define SLJIT_MOV 6
514 1.1 alnsn /* Flags: - (never set any flags) */
515 1.1 alnsn #define SLJIT_MOV_UB 7
516 1.1 alnsn /* Flags: - (never set any flags) */
517 1.1 alnsn #define SLJIT_MOV_SB 8
518 1.1 alnsn /* Flags: - (never set any flags) */
519 1.1 alnsn #define SLJIT_MOV_UH 9
520 1.1 alnsn /* Flags: - (never set any flags) */
521 1.1 alnsn #define SLJIT_MOV_SH 10
522 1.1 alnsn /* Flags: - (never set any flags) */
523 1.1 alnsn #define SLJIT_MOV_UI 11
524 1.1 alnsn /* Flags: - (never set any flags) */
525 1.1 alnsn #define SLJIT_MOV_SI 12
526 1.1 alnsn /* Flags: - (never set any flags) */
527 1.1 alnsn #define SLJIT_MOVU 13
528 1.1 alnsn /* Flags: - (never set any flags) */
529 1.1 alnsn #define SLJIT_MOVU_UB 14
530 1.1 alnsn /* Flags: - (never set any flags) */
531 1.1 alnsn #define SLJIT_MOVU_SB 15
532 1.1 alnsn /* Flags: - (never set any flags) */
533 1.1 alnsn #define SLJIT_MOVU_UH 16
534 1.1 alnsn /* Flags: - (never set any flags) */
535 1.1 alnsn #define SLJIT_MOVU_SH 17
536 1.1 alnsn /* Flags: - (never set any flags) */
537 1.1 alnsn #define SLJIT_MOVU_UI 18
538 1.1 alnsn /* Flags: - (never set any flags) */
539 1.1 alnsn #define SLJIT_MOVU_SI 19
540 1.1 alnsn /* Flags: I | E | K */
541 1.1 alnsn #define SLJIT_NOT 20
542 1.1 alnsn /* Flags: I | E | O | K */
543 1.1 alnsn #define SLJIT_NEG 21
544 1.1 alnsn /* Count leading zeroes
545 1.1 alnsn Flags: I | E | K */
546 1.1 alnsn #define SLJIT_CLZ 22
547 1.1 alnsn
548 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
549 1.1 alnsn int dst, sljit_w dstw,
550 1.1 alnsn int src, sljit_w srcw);
551 1.1 alnsn
552 1.1 alnsn /* Flags: I | E | O | C | K */
553 1.1 alnsn #define SLJIT_ADD 23
554 1.1 alnsn /* Flags: I | C | K */
555 1.1 alnsn #define SLJIT_ADDC 24
556 1.1 alnsn /* Flags: I | E | S | U | O | C | K */
557 1.1 alnsn #define SLJIT_SUB 25
558 1.1 alnsn /* Flags: I | C | K */
559 1.1 alnsn #define SLJIT_SUBC 26
560 1.1 alnsn /* Note: integer mul
561 1.1 alnsn Flags: I | O (see SLJIT_C_MUL_*) | K */
562 1.1 alnsn #define SLJIT_MUL 27
563 1.1 alnsn /* Flags: I | E | K */
564 1.1 alnsn #define SLJIT_AND 28
565 1.1 alnsn /* Flags: I | E | K */
566 1.1 alnsn #define SLJIT_OR 29
567 1.1 alnsn /* Flags: I | E | K */
568 1.1 alnsn #define SLJIT_XOR 30
569 1.1 alnsn /* Flags: I | E | K
570 1.1 alnsn Let bit_length be the length of the shift operation: 32 or 64.
571 1.1 alnsn If src2 is immediate, src2w is masked by (bit_length - 1).
572 1.1 alnsn Otherwise, if the content of src2 is outside the range from 0
573 1.1 alnsn to bit_length - 1, the operation is undefined. */
574 1.1 alnsn #define SLJIT_SHL 31
575 1.1 alnsn /* Flags: I | E | K
576 1.1 alnsn Let bit_length be the length of the shift operation: 32 or 64.
577 1.1 alnsn If src2 is immediate, src2w is masked by (bit_length - 1).
578 1.1 alnsn Otherwise, if the content of src2 is outside the range from 0
579 1.1 alnsn to bit_length - 1, the operation is undefined. */
580 1.1 alnsn #define SLJIT_LSHR 32
581 1.1 alnsn /* Flags: I | E | K
582 1.1 alnsn Let bit_length be the length of the shift operation: 32 or 64.
583 1.1 alnsn If src2 is immediate, src2w is masked by (bit_length - 1).
584 1.1 alnsn Otherwise, if the content of src2 is outside the range from 0
585 1.1 alnsn to bit_length - 1, the operation is undefined. */
586 1.1 alnsn #define SLJIT_ASHR 33
587 1.1 alnsn
588 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
589 1.1 alnsn int dst, sljit_w dstw,
590 1.1 alnsn int src1, sljit_w src1w,
591 1.1 alnsn int src2, sljit_w src2w);
592 1.1 alnsn
593 1.1 alnsn /* The following function is a helper function for sljit_emit_op_custom.
594 1.1 alnsn It returns with the real machine register index of any SLJIT_TEMPORARY
595 1.1 alnsn SLJIT_SAVED or SLJIT_LOCALS register.
596 1.1 alnsn Note: it returns with -1 for virtual registers (all EREGs on x86-32).
597 1.1 alnsn Note: register returned by SLJIT_LOCALS_REG is not necessary the real
598 1.1 alnsn stack pointer register of the target architecture. */
599 1.1 alnsn
600 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_get_register_index(int reg);
601 1.1 alnsn
602 1.1 alnsn /* Any instruction can be inserted into the instruction stream by
603 1.1 alnsn sljit_emit_op_custom. It has a similar purpose as inline assembly.
604 1.1 alnsn The size parameter must match to the instruction size of the target
605 1.1 alnsn architecture:
606 1.1 alnsn
607 1.1 alnsn x86: 0 < size <= 15. The instruction argument can be byte aligned.
608 1.1 alnsn Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
609 1.1 alnsn if size == 4, the instruction argument must be 4 byte aligned.
610 1.1 alnsn Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
611 1.1 alnsn
612 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op_custom(struct sljit_compiler *compiler,
613 1.1 alnsn void *instruction, int size);
614 1.1 alnsn
615 1.1 alnsn /* Returns with non-zero if fpu is available. */
616 1.1 alnsn
617 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void);
618 1.1 alnsn
619 1.1 alnsn /* Note: dst is the left and src is the right operand for SLJIT_FCMP.
620 1.1 alnsn Note: NaN check is always performed. If SLJIT_C_FLOAT_NAN is set,
621 1.1 alnsn the comparison result is unpredictable.
622 1.1 alnsn Flags: E | S (see SLJIT_C_FLOAT_*) */
623 1.1 alnsn #define SLJIT_FCMP 34
624 1.1 alnsn /* Flags: - (never set any flags) */
625 1.1 alnsn #define SLJIT_FMOV 35
626 1.1 alnsn /* Flags: - (never set any flags) */
627 1.1 alnsn #define SLJIT_FNEG 36
628 1.1 alnsn /* Flags: - (never set any flags) */
629 1.1 alnsn #define SLJIT_FABS 37
630 1.1 alnsn
631 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
632 1.1 alnsn int dst, sljit_w dstw,
633 1.1 alnsn int src, sljit_w srcw);
634 1.1 alnsn
635 1.1 alnsn /* Flags: - (never set any flags) */
636 1.1 alnsn #define SLJIT_FADD 38
637 1.1 alnsn /* Flags: - (never set any flags) */
638 1.1 alnsn #define SLJIT_FSUB 39
639 1.1 alnsn /* Flags: - (never set any flags) */
640 1.1 alnsn #define SLJIT_FMUL 40
641 1.1 alnsn /* Flags: - (never set any flags) */
642 1.1 alnsn #define SLJIT_FDIV 41
643 1.1 alnsn
644 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
645 1.1 alnsn int dst, sljit_w dstw,
646 1.1 alnsn int src1, sljit_w src1w,
647 1.1 alnsn int src2, sljit_w src2w);
648 1.1 alnsn
649 1.1 alnsn /* Label and jump instructions. */
650 1.1 alnsn
651 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
652 1.1 alnsn
653 1.1 alnsn /* Invert conditional instruction: xor (^) with 0x1 */
654 1.1 alnsn #define SLJIT_C_EQUAL 0
655 1.1 alnsn #define SLJIT_C_ZERO 0
656 1.1 alnsn #define SLJIT_C_NOT_EQUAL 1
657 1.1 alnsn #define SLJIT_C_NOT_ZERO 1
658 1.1 alnsn
659 1.1 alnsn #define SLJIT_C_LESS 2
660 1.1 alnsn #define SLJIT_C_GREATER_EQUAL 3
661 1.1 alnsn #define SLJIT_C_GREATER 4
662 1.1 alnsn #define SLJIT_C_LESS_EQUAL 5
663 1.1 alnsn #define SLJIT_C_SIG_LESS 6
664 1.1 alnsn #define SLJIT_C_SIG_GREATER_EQUAL 7
665 1.1 alnsn #define SLJIT_C_SIG_GREATER 8
666 1.1 alnsn #define SLJIT_C_SIG_LESS_EQUAL 9
667 1.1 alnsn
668 1.1 alnsn #define SLJIT_C_OVERFLOW 10
669 1.1 alnsn #define SLJIT_C_NOT_OVERFLOW 11
670 1.1 alnsn
671 1.1 alnsn #define SLJIT_C_MUL_OVERFLOW 12
672 1.1 alnsn #define SLJIT_C_MUL_NOT_OVERFLOW 13
673 1.1 alnsn
674 1.1 alnsn #define SLJIT_C_FLOAT_EQUAL 14
675 1.1 alnsn #define SLJIT_C_FLOAT_NOT_EQUAL 15
676 1.1 alnsn #define SLJIT_C_FLOAT_LESS 16
677 1.1 alnsn #define SLJIT_C_FLOAT_GREATER_EQUAL 17
678 1.1 alnsn #define SLJIT_C_FLOAT_GREATER 18
679 1.1 alnsn #define SLJIT_C_FLOAT_LESS_EQUAL 19
680 1.1 alnsn #define SLJIT_C_FLOAT_NAN 20
681 1.1 alnsn #define SLJIT_C_FLOAT_NOT_NAN 21
682 1.1 alnsn
683 1.1 alnsn #define SLJIT_JUMP 22
684 1.1 alnsn #define SLJIT_FAST_CALL 23
685 1.1 alnsn #define SLJIT_CALL0 24
686 1.1 alnsn #define SLJIT_CALL1 25
687 1.1 alnsn #define SLJIT_CALL2 26
688 1.1 alnsn #define SLJIT_CALL3 27
689 1.1 alnsn
690 1.1 alnsn /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
691 1.1 alnsn
692 1.1 alnsn /* The target can be changed during runtime (see: sljit_set_jump_addr). */
693 1.1 alnsn #define SLJIT_REWRITABLE_JUMP 0x1000
694 1.1 alnsn
695 1.1 alnsn /* Emit a jump instruction. The destination is not set, only the type of the jump.
696 1.1 alnsn type must be between SLJIT_C_EQUAL and SLJIT_CALL3
697 1.1 alnsn type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
698 1.1 alnsn Flags: - (never set any flags) for both conditional and unconditional jumps.
699 1.1 alnsn Flags: destroy all flags for calls. */
700 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type);
701 1.1 alnsn
702 1.1 alnsn /* Basic arithmetic comparison. In most architectures it is implemented as
703 1.1 alnsn an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
704 1.1 alnsn appropriate flags) followed by a sljit_emit_jump. However some
705 1.1 alnsn architectures (i.e: MIPS) may employ special optimizations here. It is
706 1.1 alnsn suggested to use this comparison form when appropriate.
707 1.1 alnsn type must be between SLJIT_C_EQUAL and SLJIT_C_SIG_LESS_EQUAL
708 1.1 alnsn type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP or SLJIT_INT_OP
709 1.1 alnsn Flags: destroy flags. */
710 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
711 1.1 alnsn int src1, sljit_w src1w,
712 1.1 alnsn int src2, sljit_w src2w);
713 1.1 alnsn
714 1.1 alnsn /* Basic floating point comparison. In most architectures it is implemented as
715 1.1 alnsn an SLJIT_FCMP operation (setting appropriate flags) followed by a
716 1.1 alnsn sljit_emit_jump. However some architectures (i.e: MIPS) may employ
717 1.1 alnsn special optimizations here. It is suggested to use this comparison form
718 1.1 alnsn when appropriate.
719 1.1 alnsn type must be between SLJIT_C_FLOAT_EQUAL and SLJIT_C_FLOAT_NOT_NAN
720 1.1 alnsn type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
721 1.1 alnsn Flags: destroy flags.
722 1.1 alnsn Note: if either operand is NaN, the behaviour is undefined for
723 1.1 alnsn type <= SLJIT_C_FLOAT_LESS_EQUAL. */
724 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, int type,
725 1.1 alnsn int src1, sljit_w src1w,
726 1.1 alnsn int src2, sljit_w src2w);
727 1.1 alnsn
728 1.1 alnsn /* Set the destination of the jump to this label. */
729 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
730 1.1 alnsn /* Only for jumps defined with SLJIT_REWRITABLE_JUMP flag.
731 1.1 alnsn Note: use sljit_emit_ijump for fixed jumps. */
732 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
733 1.1 alnsn
734 1.1 alnsn /* Call function or jump anywhere. Both direct and indirect form
735 1.1 alnsn type must be between SLJIT_JUMP and SLJIT_CALL3
736 1.1 alnsn Direct form: set src to SLJIT_IMM() and srcw to the address
737 1.1 alnsn Indirect form: any other valid addressing mode
738 1.1 alnsn Flags: - (never set any flags) for unconditional jumps.
739 1.1 alnsn Flags: destroy all flags for calls. */
740 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw);
741 1.1 alnsn
742 1.1 alnsn /* If op == SLJIT_MOV:
743 1.1 alnsn Set dst to 1 if condition is fulfilled, 0 otherwise
744 1.1 alnsn type must be between SLJIT_C_EQUAL and SLJIT_C_FLOAT_NOT_NAN
745 1.1 alnsn Flags: - (never set any flags)
746 1.1 alnsn If op == SLJIT_OR
747 1.1 alnsn Dst is used as src as well, and set its lowest bit to 1 if
748 1.1 alnsn the condition is fulfilled. Otherwise it does nothing.
749 1.1 alnsn Flags: E | K
750 1.1 alnsn Note: sljit_emit_cond_value does nothing, if dst is SLJIT_UNUSED (regardless of op). */
751 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type);
752 1.1 alnsn
753 1.1 alnsn /* Copies the base address of SLJIT_MEM1(SLJIT_LOCALS_REG)+offset to dst.
754 1.1 alnsn Flags: - (never set any flags) */
755 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE int sljit_get_local_base(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w offset);
756 1.1 alnsn
757 1.1 alnsn /* The constant can be changed runtime (see: sljit_set_const)
758 1.1 alnsn Flags: - (never set any flags) */
759 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value);
760 1.1 alnsn
761 1.1 alnsn /* After the code generation the address for label, jump and const instructions
762 1.1 alnsn are computed. Since these structures are freed sljit_free_compiler, the
763 1.1 alnsn addresses must be preserved by the user program elsewere. */
764 1.1 alnsn static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
765 1.1 alnsn static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
766 1.1 alnsn static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
767 1.1 alnsn
768 1.1 alnsn /* Only the address is required to rewrite the code. */
769 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
770 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant);
771 1.1 alnsn
772 1.1 alnsn /* --------------------------------------------------------------------- */
773 1.1 alnsn /* Miscellaneous utility functions */
774 1.1 alnsn /* --------------------------------------------------------------------- */
775 1.1 alnsn
776 1.1 alnsn #define SLJIT_MAJOR_VERSION 0
777 1.1 alnsn #define SLJIT_MINOR_VERSION 88
778 1.1 alnsn
779 1.1 alnsn /* Get the human readable name of the platfrom.
780 1.1 alnsn Can be useful for debugging on platforms like ARM, where ARM and
781 1.1 alnsn Thumb2 functions can be mixed. */
782 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
783 1.1 alnsn
784 1.1 alnsn /* Portble helper function to get an offset of a member. */
785 1.1 alnsn #define SLJIT_OFFSETOF(base, member) ((sljit_w)(&((base*)0x10)->member) - 0x10)
786 1.1 alnsn
787 1.1 alnsn #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
788 1.1 alnsn /* This global lock is useful to compile common functions. */
789 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
790 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
791 1.1 alnsn #endif
792 1.1 alnsn
793 1.1 alnsn #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
794 1.1 alnsn
795 1.1 alnsn /* The sljit_stack is a utiliy feature of sljit, which allocates a
796 1.1 alnsn writable memory region between base (inclusive) and limit (exclusive).
797 1.1 alnsn Both base and limit is a pointer, and base is always <= than limit.
798 1.1 alnsn This feature uses the "address space reserve" feature
799 1.1 alnsn of modern operating systems. Basically we don't need to allocate a
800 1.1 alnsn huge memory block in one step for the worst case, we can start with
801 1.1 alnsn a smaller chunk and extend it later. Since the address space is
802 1.1 alnsn reserved, the data never copied to other regions, thus it is safe
803 1.1 alnsn to store pointers here. */
804 1.1 alnsn
805 1.1 alnsn /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
806 1.1 alnsn Note: stack growing should not happen in small steps: 4k, 16k or even
807 1.1 alnsn bigger growth is better.
808 1.1 alnsn Note: this structure may not be supported by all operating systems.
809 1.1 alnsn Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
810 1.1 alnsn is not defined. */
811 1.1 alnsn
812 1.1 alnsn struct sljit_stack {
813 1.1 alnsn /* User data, anything can be stored here.
814 1.1 alnsn Starting with the same value as base. */
815 1.1 alnsn sljit_uw top;
816 1.1 alnsn /* These members are read only. */
817 1.1 alnsn sljit_uw base;
818 1.1 alnsn sljit_uw limit;
819 1.1 alnsn sljit_uw max_limit;
820 1.1 alnsn };
821 1.1 alnsn
822 1.1 alnsn /* Returns NULL if unsuccessful.
823 1.1 alnsn Note: limit and max_limit contains the size for stack allocation
824 1.1 alnsn Note: the top field is initialized to base. */
825 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit);
826 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack* stack);
827 1.1 alnsn
828 1.1 alnsn /* Can be used to increase (allocate) or decrease (free) the memory area.
829 1.1 alnsn Returns with a non-zero value if unsuccessful. If new_limit is greater than
830 1.1 alnsn max_limit, it will fail. It is very easy to implement a stack data structure,
831 1.1 alnsn since the growth ratio can be added to the current limit, and sljit_stack_resize
832 1.1 alnsn will do all the necessary checks. The fields of the stack are not changed if
833 1.1 alnsn sljit_stack_resize fails. */
834 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE sljit_w SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit);
835 1.1 alnsn
836 1.1 alnsn #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
837 1.1 alnsn
838 1.1 alnsn #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
839 1.1 alnsn
840 1.1 alnsn /* Get the entry address of a given function. */
841 1.1 alnsn #define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)func_name)
842 1.1 alnsn
843 1.1 alnsn #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
844 1.1 alnsn
845 1.1 alnsn /* All JIT related code should be placed in the same context (library, binary, etc.). */
846 1.1 alnsn
847 1.1 alnsn #define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)*(void**)func_name)
848 1.1 alnsn
849 1.1 alnsn /* For powerpc64, the function pointers point to a context descriptor. */
850 1.1 alnsn struct sljit_function_context {
851 1.1 alnsn sljit_w addr;
852 1.1 alnsn sljit_w r2;
853 1.1 alnsn sljit_w r11;
854 1.1 alnsn };
855 1.1 alnsn
856 1.1 alnsn /* Fill the context arguments using the addr and the function.
857 1.1 alnsn If func_ptr is NULL, it will not be set to the address of context
858 1.1 alnsn If addr is NULL, the function address also comes from the func pointer. */
859 1.1 alnsn SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func);
860 1.1 alnsn
861 1.1 alnsn #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
862 1.1 alnsn
863 1.1 alnsn #endif /* _SLJIT_LIR_H_ */
864