• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef _SLJIT_LIR_H_
28 #define _SLJIT_LIR_H_
29 
30 /*
31    ------------------------------------------------------------------------
32     Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
33    ------------------------------------------------------------------------
34 
35    Short description
36     Advantages:
37       - The execution can be continued from any LIR instruction. In other
38         words, it is possible to jump to any label from anywhere, even from
39         a code fragment, which is compiled later, if both compiled code
40         shares the same context. See sljit_emit_enter for more details
41       - Supports self modifying code: target of (conditional) jump and call
42         instructions and some constant values can be dynamically modified
43         during runtime
44         - although it is not suggested to do it frequently
45         - can be used for inline caching: save an important value once
46           in the instruction stream
47         - since this feature limits the optimization possibilities, a
48           special flag must be passed at compile time when these
49           instructions are emitted
50       - A fixed stack space can be allocated for local variables
51       - The compiler is thread-safe
52       - The compiler is highly configurable through preprocessor macros.
53         You can disable unneeded features (multithreading in single
54         threaded applications), and you can use your own system functions
55         (including memory allocators). See sljitConfig.h
56     Disadvantages:
57       - No automatic register allocation, and temporary results are
58         not stored on the stack. (hence the name comes)
59     In practice:
60       - This approach is very effective for interpreters
61         - One of the saved registers typically points to a stack interface
62         - It can jump to any exception handler anytime (even if it belongs
63           to another function)
64         - Hot paths can be modified during runtime reflecting the changes
65           of the fastest execution path of the dynamic language
66         - SLJIT supports complex memory addressing modes
67         - mainly position and context independent code (except some cases)
68 
69     For valgrind users:
70       - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
71 */
72 
73 #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
74 #include "sljitConfig.h"
75 #endif
76 
77 /* The following header file defines useful macros for fine tuning
78 sljit based code generators. They are listed in the beginning
79 of sljitConfigInternal.h */
80 
81 #include "sljitConfigInternal.h"
82 
83 /* --------------------------------------------------------------------- */
84 /*  Error codes                                                          */
85 /* --------------------------------------------------------------------- */
86 
87 /* Indicates no error. */
88 #define SLJIT_SUCCESS			0
89 /* After the call of sljit_generate_code(), the error code of the compiler
90    is set to this value to avoid future sljit calls (in debug mode at least).
91    The complier should be freed after sljit_generate_code(). */
92 #define SLJIT_ERR_COMPILED		1
93 /* Cannot allocate non executable memory. */
94 #define SLJIT_ERR_ALLOC_FAILED		2
95 /* Cannot allocate executable memory.
96    Only for sljit_generate_code() */
97 #define SLJIT_ERR_EX_ALLOC_FAILED	3
98 /* Return value for SLJIT_CONFIG_UNSUPPORTED placeholder architecture. */
99 #define SLJIT_ERR_UNSUPPORTED		4
100 /* An ivalid argument is passed to any SLJIT function. */
101 #define SLJIT_ERR_BAD_ARGUMENT		5
102 /* Dynamic code modification is not enabled. */
103 #define SLJIT_ERR_DYN_CODE_MOD		6
104 
105 /* --------------------------------------------------------------------- */
106 /*  Registers                                                            */
107 /* --------------------------------------------------------------------- */
108 
109 /*
110   Scratch (R) registers: registers whose may not preserve their values
111   across function calls.
112 
113   Saved (S) registers: registers whose preserve their values across
114   function calls.
115 
116   The scratch and saved register sets are overlap. The last scratch register
117   is the first saved register, the one before the last is the second saved
118   register, and so on.
119 
120   If an architecture provides two scratch and three saved registers,
121   its scratch and saved register sets are the following:
122 
123      R0   |        |   R0 is always a scratch register
124      R1   |        |   R1 is always a scratch register
125     [R2]  |   S2   |   R2 and S2 represent the same physical register
126     [R3]  |   S1   |   R3 and S1 represent the same physical register
127     [R4]  |   S0   |   R4 and S0 represent the same physical register
128 
129   Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
130         SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
131 
132   Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 12
133         and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 6. However, 6 registers
134         are virtual on x86-32. See below.
135 
136   The purpose of this definition is convenience: saved registers can
137   be used as extra scratch registers. For example four registers can
138   be specified as scratch registers and the fifth one as saved register
139   on the CPU above and any user code which requires four scratch
140   registers can run unmodified. The SLJIT compiler automatically saves
141   the content of the two extra scratch register on the stack. Scratch
142   registers can also be preserved by saving their value on the stack
143   but this needs to be done manually.
144 
145   Note: To emphasize that registers assigned to R2-R4 are saved
146         registers, they are enclosed by square brackets.
147 
148   Note: sljit_emit_enter and sljit_set_context defines whether a register
149         is S or R register. E.g: when 3 scratches and 1 saved is mapped
150         by sljit_emit_enter, the allowed register set will be: R0-R2 and
151         S0. Although S2 is mapped to the same position as R2, it does not
152         available in the current configuration. Furthermore the S1 register
153         is not available at all.
154 */
155 
156 /* When SLJIT_UNUSED is specified as the destination of sljit_emit_op1
157    or sljit_emit_op2 operations the result is discarded. If no status
158    flags are set, no instructions are emitted for these operations. Data
159    prefetch is a special exception, see SLJIT_MOV operation. Other SLJIT
160    operations do not support SLJIT_UNUSED as a destination operand. */
161 #define SLJIT_UNUSED		0
162 
163 /* Scratch registers. */
164 #define SLJIT_R0	1
165 #define SLJIT_R1	2
166 #define SLJIT_R2	3
167 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
168    are allocated on the stack). These registers are called virtual
169    and cannot be used for memory addressing (cannot be part of
170    any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
171    limitation on other CPUs. See sljit_get_register_index(). */
172 #define SLJIT_R3	4
173 #define SLJIT_R4	5
174 #define SLJIT_R5	6
175 #define SLJIT_R6	7
176 #define SLJIT_R7	8
177 #define SLJIT_R8	9
178 #define SLJIT_R9	10
179 /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
180    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
181 #define SLJIT_R(i)	(1 + (i))
182 
183 /* Saved registers. */
184 #define SLJIT_S0	(SLJIT_NUMBER_OF_REGISTERS)
185 #define SLJIT_S1	(SLJIT_NUMBER_OF_REGISTERS - 1)
186 #define SLJIT_S2	(SLJIT_NUMBER_OF_REGISTERS - 2)
187 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
188    are allocated on the stack). These registers are called virtual
189    and cannot be used for memory addressing (cannot be part of
190    any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
191    limitation on other CPUs. See sljit_get_register_index(). */
192 #define SLJIT_S3	(SLJIT_NUMBER_OF_REGISTERS - 3)
193 #define SLJIT_S4	(SLJIT_NUMBER_OF_REGISTERS - 4)
194 #define SLJIT_S5	(SLJIT_NUMBER_OF_REGISTERS - 5)
195 #define SLJIT_S6	(SLJIT_NUMBER_OF_REGISTERS - 6)
196 #define SLJIT_S7	(SLJIT_NUMBER_OF_REGISTERS - 7)
197 #define SLJIT_S8	(SLJIT_NUMBER_OF_REGISTERS - 8)
198 #define SLJIT_S9	(SLJIT_NUMBER_OF_REGISTERS - 9)
199 /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
200    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
201 #define SLJIT_S(i)	(SLJIT_NUMBER_OF_REGISTERS - (i))
202 
203 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
204 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
205 
206 /* The SLJIT_SP provides direct access to the linear stack space allocated by
207    sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
208    The immediate offset is extended by the relative stack offset automatically.
209    The sljit_get_local_base can be used to obtain the absolute offset. */
210 #define SLJIT_SP	(SLJIT_NUMBER_OF_REGISTERS + 1)
211 
212 /* Return with machine word. */
213 
214 #define SLJIT_RETURN_REG	SLJIT_R0
215 
216 /* --------------------------------------------------------------------- */
217 /*  Floating point registers                                             */
218 /* --------------------------------------------------------------------- */
219 
220 /* Each floating point register can store a 32 or a 64 bit precision
221    value. The FR and FS register sets are overlap in the same way as R
222    and S register sets. See above. */
223 
224 /* Note: SLJIT_UNUSED as destination is not valid for floating point
225    operations, since they cannot be used for setting flags. */
226 
227 /* Floating point scratch registers. */
228 #define SLJIT_FR0	1
229 #define SLJIT_FR1	2
230 #define SLJIT_FR2	3
231 #define SLJIT_FR3	4
232 #define SLJIT_FR4	5
233 #define SLJIT_FR5	6
234 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
235    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
236 #define SLJIT_FR(i)	(1 + (i))
237 
238 /* Floating point saved registers. */
239 #define SLJIT_FS0	(SLJIT_NUMBER_OF_FLOAT_REGISTERS)
240 #define SLJIT_FS1	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
241 #define SLJIT_FS2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
242 #define SLJIT_FS3	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
243 #define SLJIT_FS4	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
244 #define SLJIT_FS5	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
245 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
246    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
247 #define SLJIT_FS(i)	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
248 
249 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
250 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
251 
252 /* --------------------------------------------------------------------- */
253 /*  Argument type definitions                                            */
254 /* --------------------------------------------------------------------- */
255 
256 /* Argument type definitions.
257    Used by SLJIT_[DEF_]ARGx and SLJIT_[DEF]_RET macros. */
258 
259 #define SLJIT_ARG_TYPE_VOID 0
260 #define SLJIT_ARG_TYPE_SW 1
261 #define SLJIT_ARG_TYPE_UW 2
262 #define SLJIT_ARG_TYPE_S32 3
263 #define SLJIT_ARG_TYPE_U32 4
264 #define SLJIT_ARG_TYPE_F32 5
265 #define SLJIT_ARG_TYPE_F64 6
266 
267 /* The following argument type definitions are used by sljit_emit_enter,
268    sljit_set_context, sljit_emit_call, and sljit_emit_icall functions.
269    The following return type definitions are used by sljit_emit_call
270    and sljit_emit_icall functions.
271 
272    When a function is called, the first integer argument must be placed
273    in SLJIT_R0, the second in SLJIT_R1, and so on. Similarly the first
274    floating point argument must be placed in SLJIT_FR0, the second in
275    SLJIT_FR1, and so on.
276 
277    Example function definition:
278      sljit_f32 SLJIT_FUNC example_c_callback(sljit_sw arg_a,
279          sljit_f64 arg_b, sljit_u32 arg_c, sljit_f32 arg_d);
280 
281    Argument type definition:
282      SLJIT_DEF_RET(SLJIT_ARG_TYPE_F32)
283         | SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_SW) | SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_F64)
284         | SLJIT_DEF_ARG3(SLJIT_ARG_TYPE_U32) | SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_F32)
285 
286    Short form of argument type definition:
287      SLJIT_RET(F32) | SLJIT_ARG1(SW) | SLJIT_ARG2(F64)
288         | SLJIT_ARG3(S32) | SLJIT_ARG4(F32)
289 
290    Argument passing:
291      arg_a must be placed in SLJIT_R0
292      arg_c must be placed in SLJIT_R1
293      arg_b must be placed in SLJIT_FR0
294      arg_d must be placed in SLJIT_FR1
295 
296 Note:
297    The SLJIT_ARG_TYPE_VOID type is only supported by
298    SLJIT_DEF_RET, and SLJIT_ARG_TYPE_VOID is also the
299    default value when SLJIT_DEF_RET is not specified. */
300 #define SLJIT_DEF_SHIFT 4
301 #define SLJIT_DEF_RET(type) (type)
302 #define SLJIT_DEF_ARG1(type) ((type) << SLJIT_DEF_SHIFT)
303 #define SLJIT_DEF_ARG2(type) ((type) << (2 * SLJIT_DEF_SHIFT))
304 #define SLJIT_DEF_ARG3(type) ((type) << (3 * SLJIT_DEF_SHIFT))
305 #define SLJIT_DEF_ARG4(type) ((type) << (4 * SLJIT_DEF_SHIFT))
306 
307 /* Short form of the macros above.
308 
309    For example the following definition:
310    SLJIT_DEF_RET(SLJIT_ARG_TYPE_SW) | SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_F32)
311 
312    can be shortened to:
313    SLJIT_RET(SW) | SLJIT_ARG1(F32)
314 
315 Note:
316    The VOID type is only supported by SLJIT_RET, and
317    VOID is also the default value when SLJIT_RET is
318    not specified. */
319 #define SLJIT_RET(type) SLJIT_DEF_RET(SLJIT_ARG_TYPE_ ## type)
320 #define SLJIT_ARG1(type) SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_ ## type)
321 #define SLJIT_ARG2(type) SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_ ## type)
322 #define SLJIT_ARG3(type) SLJIT_DEF_ARG3(SLJIT_ARG_TYPE_ ## type)
323 #define SLJIT_ARG4(type) SLJIT_DEF_ARG4(SLJIT_ARG_TYPE_ ## type)
324 
325 /* --------------------------------------------------------------------- */
326 /*  Main structures and functions                                        */
327 /* --------------------------------------------------------------------- */
328 
329 /*
330 	The following structures are private, and can be changed in the
331 	future. Keeping them here allows code inlining.
332 */
333 
334 struct sljit_memory_fragment {
335 	struct sljit_memory_fragment *next;
336 	sljit_uw used_size;
337 	/* Must be aligned to sljit_sw. */
338 	sljit_u8 memory[1];
339 };
340 
341 struct sljit_label {
342 	struct sljit_label *next;
343 	sljit_uw addr;
344 	/* The maximum size difference. */
345 	sljit_uw size;
346 };
347 
348 struct sljit_jump {
349 	struct sljit_jump *next;
350 	sljit_uw addr;
351 	sljit_sw flags;
352 	union {
353 		sljit_uw target;
354 		struct sljit_label* label;
355 	} u;
356 };
357 
358 struct sljit_const {
359 	struct sljit_const *next;
360 	sljit_uw addr;
361 };
362 
363 struct sljit_compiler {
364 	sljit_s32 error;
365 	sljit_s32 options;
366 
367 	struct sljit_label *labels;
368 	struct sljit_jump *jumps;
369 	struct sljit_const *consts;
370 	struct sljit_label *last_label;
371 	struct sljit_jump *last_jump;
372 	struct sljit_const *last_const;
373 
374 	void *allocator_data;
375 	struct sljit_memory_fragment *buf;
376 	struct sljit_memory_fragment *abuf;
377 
378 	/* Used scratch registers. */
379 	sljit_s32 scratches;
380 	/* Used saved registers. */
381 	sljit_s32 saveds;
382 	/* Used float scratch registers. */
383 	sljit_s32 fscratches;
384 	/* Used float saved registers. */
385 	sljit_s32 fsaveds;
386 	/* Local stack size. */
387 	sljit_s32 local_size;
388 	/* Code size. */
389 	sljit_uw size;
390 	/* Relative offset of the executable mapping from the writable mapping. */
391 	sljit_uw executable_offset;
392 	/* Executable size for statistical purposes. */
393 	sljit_uw executable_size;
394 
395 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
396 	sljit_s32 args;
397 	sljit_s32 locals_offset;
398 	sljit_s32 saveds_offset;
399 	sljit_s32 stack_tmp_size;
400 #endif
401 
402 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
403 	sljit_s32 mode32;
404 #ifdef _WIN64
405 	sljit_s32 locals_offset;
406 #endif
407 #endif
408 
409 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
410 	/* Constant pool handling. */
411 	sljit_uw *cpool;
412 	sljit_u8 *cpool_unique;
413 	sljit_uw cpool_diff;
414 	sljit_uw cpool_fill;
415 	/* Other members. */
416 	/* Contains pointer, "ldr pc, [...]" pairs. */
417 	sljit_uw patches;
418 #endif
419 
420 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
421 	/* Temporary fields. */
422 	sljit_uw shift_imm;
423 #endif
424 
425 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
426 	sljit_sw imm;
427 #endif
428 
429 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
430 	sljit_s32 delay_slot;
431 	sljit_s32 cache_arg;
432 	sljit_sw cache_argw;
433 #endif
434 
435 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
436 	sljit_s32 delay_slot;
437 	sljit_s32 cache_arg;
438 	sljit_sw cache_argw;
439 #endif
440 
441 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
442 	sljit_s32 cache_arg;
443 	sljit_sw cache_argw;
444 #endif
445 
446 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
447 	FILE* verbose;
448 #endif
449 
450 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
451 		|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
452 	/* Flags specified by the last arithmetic instruction.
453 	   It contains the type of the variable flag. */
454 	sljit_s32 last_flags;
455 	/* Local size passed to the functions. */
456 	sljit_s32 logical_local_size;
457 #endif
458 
459 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
460 		|| (defined SLJIT_DEBUG && SLJIT_DEBUG) \
461 		|| (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
462 	/* Trust arguments when the API function is called. */
463 	sljit_s32 skip_checks;
464 #endif
465 };
466 
467 /* --------------------------------------------------------------------- */
468 /*  Main functions                                                       */
469 /* --------------------------------------------------------------------- */
470 
471 /* Creates an sljit compiler. The allocator_data is required by some
472    custom memory managers. This pointer is passed to SLJIT_MALLOC
473    and SLJIT_FREE macros. Most allocators (including the default
474    one) ignores this value, and it is recommended to pass NULL
475    as a dummy value for allocator_data.
476 
477    Returns NULL if failed. */
478 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data);
479 
480 /* Frees everything except the compiled machine code. */
481 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
482 
483 /* Returns the current error code. If an error is occurred, future sljit
484    calls which uses the same compiler argument returns early with the same
485    error code. Thus there is no need for checking the error after every
486    call, it is enough to do it before the code is compiled. Removing
487    these checks increases the performance of the compiling process. */
sljit_get_compiler_error(struct sljit_compiler * compiler)488 static SLJIT_INLINE sljit_s32 sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
489 
490 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
491    if an error was detected before. After the error code is set
492    the compiler behaves as if the allocation failure happened
493    during an sljit function call. This can greatly simplify error
494    checking, since only the compiler status needs to be checked
495    after the compilation. */
496 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
497 
498 /*
499    Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
500    and <= 128 bytes on 64 bit architectures. The memory area is owned by the
501    compiler, and freed by sljit_free_compiler. The returned pointer is
502    sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
503    the compiling, and no need to worry about freeing them. The size is
504    enough to contain at most 16 pointers. If the size is outside of the range,
505    the function will return with NULL. However, this return value does not
506    indicate that there is no more memory (does not set the current error code
507    of the compiler to out-of-memory status).
508 */
509 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_s32 size);
510 
511 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
512 /* Passing NULL disables verbose. */
513 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
514 #endif
515 
516 /*
517    Create executable code from the sljit instruction stream. This is the final step
518    of the code generation so no more instructions can be added after this call.
519 */
520 
521 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
522 
523 /* Free executable code. */
524 
525 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
526 
527 /*
528    When the protected executable allocator is used the JIT code is mapped
529    twice. The first mapping has read/write and the second mapping has read/exec
530    permissions. This function returns with the relative offset of the executable
531    mapping using the writable mapping as the base after the machine code is
532    successfully generated. The returned value is always 0 for the normal executable
533    allocator, since it uses only one mapping with read/write/exec permissions.
534    Dynamic code modifications requires this value.
535 
536    Before a successful code generation, this function returns with 0.
537 */
sljit_get_executable_offset(struct sljit_compiler * compiler)538 static SLJIT_INLINE sljit_sw sljit_get_executable_offset(struct sljit_compiler *compiler) { return compiler->executable_offset; }
539 
540 /*
541    The executable memory consumption of the generated code can be retrieved by
542    this function. The returned value can be used for statistical purposes.
543 
544    Before a successful code generation, this function returns with 0.
545 */
sljit_get_generated_code_size(struct sljit_compiler * compiler)546 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
547 
548 /* Returns with non-zero if the feature or limitation type passed as its
549    argument is present on the current CPU.
550 
551    Some features (e.g. floating point operations) require hardware (CPU)
552    support while others (e.g. move with update) are emulated if not available.
553    However even if a feature is emulated, specialized code paths can be faster
554    than the emulation. Some limitations are emulated as well so their general
555    case is supported but it has extra performance costs. */
556 
557 /* [Not emulated] Floating-point support is available. */
558 #define SLJIT_HAS_FPU			0
559 /* [Limitation] Some registers are virtual registers. */
560 #define SLJIT_HAS_VIRTUAL_REGISTERS	1
561 /* [Emulated] Count leading zero is supported. */
562 #define SLJIT_HAS_CLZ			2
563 /* [Emulated] Conditional move is supported. */
564 #define SLJIT_HAS_CMOV			3
565 
566 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
567 /* [Not emulated] SSE2 support is available on x86. */
568 #define SLJIT_HAS_SSE2			100
569 #endif
570 
571 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type);
572 
573 /* Instruction generation. Returns with any error code. If there is no
574    error, they return with SLJIT_SUCCESS. */
575 
576 /*
577    The executable code is a function from the viewpoint of the C
578    language. The function calls must obey to the ABI (Application
579    Binary Interface) of the platform, which specify the purpose of
580    machine registers and stack handling among other things. The
581    sljit_emit_enter function emits the necessary instructions for
582    setting up a new context for the executable code and moves function
583    arguments to the saved registers. Furthermore the options argument
584    can be used to pass configuration options to the compiler. The
585    available options are listed before sljit_emit_enter.
586 
587    The function argument list is the combination of SLJIT_ARGx
588    (SLJIT_DEF_ARG1) macros. Currently maximum 3 SW / UW
589    (SLJIT_ARG_TYPE_SW / LJIT_ARG_TYPE_UW) arguments are supported.
590    The first argument goes to SLJIT_S0, the second goes to SLJIT_S1
591    and so on. The register set used by the function must be declared
592    as well. The number of scratch and saved registers used by the
593    function must be passed to sljit_emit_enter. Only R registers
594    between R0 and "scratches" argument can be used later. E.g. if
595    "scratches" is set to 2, the scratch register set will be limited
596    to SLJIT_R0 and SLJIT_R1. The S registers and the floating point
597    registers ("fscratches" and "fsaveds") are specified in a similar
598    manner. The sljit_emit_enter is also capable of allocating a stack
599    space for local variables. The "local_size" argument contains the
600    size in bytes of this local area and its staring address is stored
601    in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
602    SLJIT_SP + local_size (exclusive) can be modified freely until
603    the function returns. The stack space is not initialized.
604 
605    Note: the following conditions must met:
606          0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
607          0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
608          scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
609          0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
610          0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
611          fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
612 
613    Note: every call of sljit_emit_enter and sljit_set_context
614          overwrites the previous context.
615 */
616 
617 /* The absolute address returned by sljit_get_local_base with
618 offset 0 is aligned to sljit_f64. Otherwise it is aligned to sljit_sw. */
619 #define SLJIT_F64_ALIGNMENT 0x00000001
620 
621 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
622 #define SLJIT_MAX_LOCAL_SIZE	65536
623 
624 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
625 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
626 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
627 
628 /* The machine code has a context (which contains the local stack space size,
629    number of used registers, etc.) which initialized by sljit_emit_enter. Several
630    functions (like sljit_emit_return) requres this context to be able to generate
631    the appropriate code. However, some code fragments (like inline cache) may have
632    no normal entry point so their context is unknown for the compiler. Their context
633    can be provided to the compiler by the sljit_set_context function.
634 
635    Note: every call of sljit_emit_enter and sljit_set_context overwrites
636          the previous context. */
637 
638 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
639 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
640 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
641 
642 /* Return from machine code.  The op argument can be SLJIT_UNUSED which means the
643    function does not return with anything or any opcode between SLJIT_MOV and
644    SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
645    is SLJIT_UNUSED, otherwise see below the description about source and
646    destination arguments. */
647 
648 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op,
649 	sljit_s32 src, sljit_sw srcw);
650 
651 /* Generating entry and exit points for fast call functions (see SLJIT_FAST_CALL).
652    Both sljit_emit_fast_enter and sljit_emit_fast_return functions preserve the
653    values of all registers and stack frame. The return address is stored in the
654    dst argument of sljit_emit_fast_enter, and this return address can be passed
655    to sljit_emit_fast_return to continue the execution after the fast call.
656 
657    Fast calls are cheap operations (usually only a single call instruction is
658    emitted) but they do not preserve any registers. However the callee function
659    can freely use / update any registers and stack values which can be
660    efficiently exploited by various optimizations. Registers can be saved
661    manually by the callee function if needed.
662 
663    Although returning to different address by sljit_emit_fast_return is possible,
664    this address usually cannot be predicted by the return address predictor of
665    modern CPUs which may reduce performance. Furthermore using sljit_emit_ijump
666    to return is also inefficient since return address prediction is usually
667    triggered by a specific form of ijump.
668 
669    Flags: - (does not modify flags). */
670 
671 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw);
672 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw);
673 
674 /*
675    Source and destination operands for arithmetical instructions
676     imm              - a simple immediate value (cannot be used as a destination)
677     reg              - any of the registers (immediate argument must be 0)
678     [imm]            - absolute immediate memory address
679     [reg+imm]        - indirect memory address
680     [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
681                        useful for (byte, half, int, sljit_sw) array access
682                        (fully supported by both x86 and ARM architectures, and cheap operation on others)
683 */
684 
685 /*
686    IMPORATNT NOTE: memory access MUST be naturally aligned except
687                    SLJIT_UNALIGNED macro is defined and its value is 1.
688 
689      length | alignment
690    ---------+-----------
691      byte   | 1 byte (any physical_address is accepted)
692      half   | 2 byte (physical_address & 0x1 == 0)
693      int    | 4 byte (physical_address & 0x3 == 0)
694      word   | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
695             | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
696     pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
697             | on 64 bit machines)
698 
699    Note:   Different architectures have different addressing limitations.
700            A single instruction is enough for the following addressing
701            modes. Other adrressing modes are emulated by instruction
702            sequences. This information could help to improve those code
703            generators which focuses only a few architectures.
704 
705    x86:    [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
706            [reg+(reg<<imm)] is supported
707            [imm], -2^32+1 <= imm <= 2^32-1 is supported
708            Write-back is not supported
709    arm:    [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
710                 bytes, any halfs or floating point values)
711            [reg+(reg<<imm)] is supported
712            Write-back is supported
713    arm-t2: [reg+imm], -255 <= imm <= 4095
714            [reg+(reg<<imm)] is supported
715            Write back is supported only for [reg+imm], where -255 <= imm <= 255
716    arm64:  [reg+imm], -256 <= imm <= 255, 0 <= aligned imm <= 4095 * alignment
717            [reg+(reg<<imm)] is supported
718            Write back is supported only for [reg+imm], where -256 <= imm <= 255
719    ppc:    [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
720                 signed load on 64 bit requires immediates divisible by 4.
721                 [reg+imm] is not supported for signed 8 bit values.
722            [reg+reg] is supported
723            Write-back is supported except for one instruction: 32 bit signed
724                 load with [reg+imm] addressing mode on 64 bit.
725    mips:   [reg+imm], -65536 <= imm <= 65535
726    sparc:  [reg+imm], -4096 <= imm <= 4095
727            [reg+reg] is supported
728 */
729 
730 /* Macros for specifying operand types. */
731 #define SLJIT_MEM		0x80
732 #define SLJIT_MEM0()		(SLJIT_MEM)
733 #define SLJIT_MEM1(r1)		(SLJIT_MEM | (r1))
734 #define SLJIT_MEM2(r1, r2)	(SLJIT_MEM | (r1) | ((r2) << 8))
735 #define SLJIT_IMM		0x40
736 
737 /* Set 32 bit operation mode (I) on 64 bit CPUs. This option is ignored on
738    32 bit CPUs. When this option is set for an arithmetic operation, only
739    the lower 32 bit of the input registers are used, and the CPU status
740    flags are set according to the 32 bit result. Although the higher 32 bit
741    of the input and the result registers are not defined by SLJIT, it might
742    be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
743    requirements all source registers must be the result of those operations
744    where this option was also set. Memory loads read 32 bit values rather
745    than 64 bit ones. In other words 32 bit and 64 bit operations cannot
746    be mixed. The only exception is SLJIT_MOV32 and SLJIT_MOVU32 whose source
747    register can hold any 32 or 64 bit value, and it is converted to a 32 bit
748    compatible format first. This conversion is free (no instructions are
749    emitted) on most CPUs. A 32 bit value can also be converted to a 64 bit
750    value by SLJIT_MOV_S32 (sign extension) or SLJIT_MOV_U32 (zero extension).
751 
752    Note: memory addressing always uses 64 bit values on 64 bit systems so
753          the result of a 32 bit operation must not be used with SLJIT_MEMx
754          macros.
755 
756    This option is part of the instruction name, so there is no need to
757    manually set it. E.g:
758 
759      SLJIT_ADD32 == (SLJIT_ADD | SLJIT_I32_OP) */
760 #define SLJIT_I32_OP		0x100
761 
762 /* Set F32 (single) precision mode for floating-point computation. This
763    option is similar to SLJIT_I32_OP, it just applies to floating point
764    registers. When this option is passed, the CPU performs 32 bit floating
765    point operations, rather than 64 bit one. Similar to SLJIT_I32_OP, all
766    register arguments must be the result of those operations where this
767    option was also set.
768 
769    This option is part of the instruction name, so there is no need to
770    manually set it. E.g:
771 
772      SLJIT_MOV_F32 = (SLJIT_MOV_F64 | SLJIT_F32_OP)
773  */
774 #define SLJIT_F32_OP		SLJIT_I32_OP
775 
776 /* Many CPUs (x86, ARM, PPC) have status flags which can be set according
777    to the result of an operation. Other CPUs (MIPS) do not have status
778    flags, and results must be stored in registers. To cover both architecture
779    types efficiently only two flags are defined by SLJIT:
780 
781     * Zero (equal) flag: it is set if the result is zero
782     * Variable flag: its value is defined by the last arithmetic operation
783 
784    SLJIT instructions can set any or both of these flags. The value of
785    these flags is undefined if the instruction does not specify their value.
786    The description of each instruction contains the list of allowed flag
787    types.
788 
789    Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
790 
791      sljit_op2(..., SLJIT_ADD, ...)
792        Both the zero and variable flags are undefined so they can
793        have any value after the operation is completed.
794 
795      sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
796        Sets the zero flag if the result is zero, clears it otherwise.
797        The variable flag is undefined.
798 
799      sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
800        Sets the variable flag if an integer overflow occurs, clears
801        it otherwise. The zero flag is undefined.
802 
803      sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
804        Sets the zero flag if the result is zero, clears it otherwise.
805        Sets the variable flag if unsigned overflow (carry) occurs,
806        clears it otherwise.
807 
808    If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are
809    unchanged.
810 
811    Using these flags can reduce the number of emitted instructions. E.g. a
812    fast loop can be implemented by decreasing a counter register and set the
813    zero flag to jump back if the counter register has not reached zero.
814 
815    Motivation: although CPUs can set a large number of flags, usually their
816    values are ignored or only one of them is used. Emulating a large number
817    of flags on systems without flag register is complicated so SLJIT
818    instructions must specify the flag they want to use and only that flag
819    will be emulated. The last arithmetic instruction can be repeated if
820    multiple flags need to be checked.
821 */
822 
823 /* Set Zero status flag. */
824 #define SLJIT_SET_Z			0x0200
825 /* Set the variable status flag if condition is true.
826    See comparison types. */
827 #define SLJIT_SET(condition)			((condition) << 10)
828 
829 /* Notes:
830      - you cannot postpone conditional jump instructions except if noted that
831        the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
832      - flag combinations: '|' means 'logical or'. */
833 
834 /* Starting index of opcodes for sljit_emit_op0. */
835 #define SLJIT_OP0_BASE			0
836 
837 /* Flags: - (does not modify flags)
838    Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
839          It falls back to SLJIT_NOP in those cases. */
840 #define SLJIT_BREAKPOINT		(SLJIT_OP0_BASE + 0)
841 /* Flags: - (does not modify flags)
842    Note: may or may not cause an extra cycle wait
843          it can even decrease the runtime in a few cases. */
844 #define SLJIT_NOP			(SLJIT_OP0_BASE + 1)
845 /* Flags: - (may destroy flags)
846    Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
847    Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
848 #define SLJIT_LMUL_UW			(SLJIT_OP0_BASE + 2)
849 /* Flags: - (may destroy flags)
850    Signed multiplication of SLJIT_R0 and SLJIT_R1.
851    Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
852 #define SLJIT_LMUL_SW			(SLJIT_OP0_BASE + 3)
853 /* Flags: - (may destroy flags)
854    Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
855    The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
856    Note: if SLJIT_R1 is 0, the behaviour is undefined. */
857 #define SLJIT_DIVMOD_UW			(SLJIT_OP0_BASE + 4)
858 #define SLJIT_DIVMOD_U32		(SLJIT_DIVMOD_UW | SLJIT_I32_OP)
859 /* Flags: - (may destroy flags)
860    Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
861    The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
862    Note: if SLJIT_R1 is 0, the behaviour is undefined.
863    Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
864          the behaviour is undefined. */
865 #define SLJIT_DIVMOD_SW			(SLJIT_OP0_BASE + 5)
866 #define SLJIT_DIVMOD_S32		(SLJIT_DIVMOD_SW | SLJIT_I32_OP)
867 /* Flags: - (may destroy flags)
868    Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
869    The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
870    Note: if SLJIT_R1 is 0, the behaviour is undefined. */
871 #define SLJIT_DIV_UW			(SLJIT_OP0_BASE + 6)
872 #define SLJIT_DIV_U32			(SLJIT_DIV_UW | SLJIT_I32_OP)
873 /* Flags: - (may destroy flags)
874    Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
875    The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
876    Note: if SLJIT_R1 is 0, the behaviour is undefined.
877    Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
878          the behaviour is undefined. */
879 #define SLJIT_DIV_SW			(SLJIT_OP0_BASE + 7)
880 #define SLJIT_DIV_S32			(SLJIT_DIV_SW | SLJIT_I32_OP)
881 
882 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op);
883 
884 /* Starting index of opcodes for sljit_emit_op1. */
885 #define SLJIT_OP1_BASE			32
886 
887 /* The MOV instruction transfers data from source to destination.
888 
889    MOV instruction suffixes:
890 
891    U8  - unsigned 8 bit data transfer
892    S8  - signed 8 bit data transfer
893    U16 - unsigned 16 bit data transfer
894    S16 - signed 16 bit data transfer
895    U32 - unsigned int (32 bit) data transfer
896    S32 - signed int (32 bit) data transfer
897    P   - pointer (sljit_p) data transfer
898 
899    If the destination of a MOV instruction is SLJIT_UNUSED and the source
900    operand is a memory address the compiler emits a prefetch instruction
901    if this instruction is supported by the current CPU. Higher data sizes
902    bring the data closer to the core: a MOV with word size loads the data
903    into a higher level cache than a byte size. Otherwise the type does not
904    affect the prefetch instruction. Furthermore a prefetch instruction
905    never fails, so it can be used to prefetch a data from an address and
906    check whether that address is NULL afterwards.
907 */
908 
909 /* Flags: - (does not modify flags) */
910 #define SLJIT_MOV			(SLJIT_OP1_BASE + 0)
911 /* Flags: - (does not modify flags) */
912 #define SLJIT_MOV_U8			(SLJIT_OP1_BASE + 1)
913 #define SLJIT_MOV32_U8			(SLJIT_MOV_U8 | SLJIT_I32_OP)
914 /* Flags: - (does not modify flags) */
915 #define SLJIT_MOV_S8			(SLJIT_OP1_BASE + 2)
916 #define SLJIT_MOV32_S8			(SLJIT_MOV_S8 | SLJIT_I32_OP)
917 /* Flags: - (does not modify flags) */
918 #define SLJIT_MOV_U16			(SLJIT_OP1_BASE + 3)
919 #define SLJIT_MOV32_U16			(SLJIT_MOV_U16 | SLJIT_I32_OP)
920 /* Flags: - (does not modify flags) */
921 #define SLJIT_MOV_S16			(SLJIT_OP1_BASE + 4)
922 #define SLJIT_MOV32_S16			(SLJIT_MOV_S16 | SLJIT_I32_OP)
923 /* Flags: - (does not modify flags)
924    Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
925 #define SLJIT_MOV_U32			(SLJIT_OP1_BASE + 5)
926 /* Flags: - (does not modify flags)
927    Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
928 #define SLJIT_MOV_S32			(SLJIT_OP1_BASE + 6)
929 /* Flags: - (does not modify flags) */
930 #define SLJIT_MOV32			(SLJIT_MOV_S32 | SLJIT_I32_OP)
931 /* Flags: - (does not modify flags)
932    Note: load a pointer sized data, useful on x32 (a 32 bit mode on x86-64
933          where all x64 features are available, e.g. 16 register) or similar
934          compiling modes */
935 #define SLJIT_MOV_P			(SLJIT_OP1_BASE + 7)
936 /* Flags: Z
937    Note: immediate source argument is not supported */
938 #define SLJIT_NOT			(SLJIT_OP1_BASE + 8)
939 #define SLJIT_NOT32			(SLJIT_NOT | SLJIT_I32_OP)
940 /* Flags: Z | OVERFLOW
941    Note: immediate source argument is not supported */
942 #define SLJIT_NEG			(SLJIT_OP1_BASE + 9)
943 #define SLJIT_NEG32			(SLJIT_NEG | SLJIT_I32_OP)
944 /* Count leading zeroes
945    Flags: - (may destroy flags)
946    Note: immediate source argument is not supported */
947 #define SLJIT_CLZ			(SLJIT_OP1_BASE + 10)
948 #define SLJIT_CLZ32			(SLJIT_CLZ | SLJIT_I32_OP)
949 
950 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
951 	sljit_s32 dst, sljit_sw dstw,
952 	sljit_s32 src, sljit_sw srcw);
953 
954 /* Starting index of opcodes for sljit_emit_op2. */
955 #define SLJIT_OP2_BASE			96
956 
957 /* Flags: Z | OVERFLOW | CARRY */
958 #define SLJIT_ADD			(SLJIT_OP2_BASE + 0)
959 #define SLJIT_ADD32			(SLJIT_ADD | SLJIT_I32_OP)
960 /* Flags: CARRY */
961 #define SLJIT_ADDC			(SLJIT_OP2_BASE + 1)
962 #define SLJIT_ADDC32			(SLJIT_ADDC | SLJIT_I32_OP)
963 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
964           SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
965           SIG_LESS_EQUAL | CARRY */
966 #define SLJIT_SUB			(SLJIT_OP2_BASE + 2)
967 #define SLJIT_SUB32			(SLJIT_SUB | SLJIT_I32_OP)
968 /* Flags: CARRY */
969 #define SLJIT_SUBC			(SLJIT_OP2_BASE + 3)
970 #define SLJIT_SUBC32			(SLJIT_SUBC | SLJIT_I32_OP)
971 /* Note: integer mul
972    Flags: MUL_OVERFLOW */
973 #define SLJIT_MUL			(SLJIT_OP2_BASE + 4)
974 #define SLJIT_MUL32			(SLJIT_MUL | SLJIT_I32_OP)
975 /* Flags: Z */
976 #define SLJIT_AND			(SLJIT_OP2_BASE + 5)
977 #define SLJIT_AND32			(SLJIT_AND | SLJIT_I32_OP)
978 /* Flags: Z */
979 #define SLJIT_OR			(SLJIT_OP2_BASE + 6)
980 #define SLJIT_OR32			(SLJIT_OR | SLJIT_I32_OP)
981 /* Flags: Z */
982 #define SLJIT_XOR			(SLJIT_OP2_BASE + 7)
983 #define SLJIT_XOR32			(SLJIT_XOR | SLJIT_I32_OP)
984 /* Flags: Z
985    Let bit_length be the length of the shift operation: 32 or 64.
986    If src2 is immediate, src2w is masked by (bit_length - 1).
987    Otherwise, if the content of src2 is outside the range from 0
988    to bit_length - 1, the result is undefined. */
989 #define SLJIT_SHL			(SLJIT_OP2_BASE + 8)
990 #define SLJIT_SHL32			(SLJIT_SHL | SLJIT_I32_OP)
991 /* Flags: Z
992    Let bit_length be the length of the shift operation: 32 or 64.
993    If src2 is immediate, src2w is masked by (bit_length - 1).
994    Otherwise, if the content of src2 is outside the range from 0
995    to bit_length - 1, the result is undefined. */
996 #define SLJIT_LSHR			(SLJIT_OP2_BASE + 9)
997 #define SLJIT_LSHR32			(SLJIT_LSHR | SLJIT_I32_OP)
998 /* Flags: Z
999    Let bit_length be the length of the shift operation: 32 or 64.
1000    If src2 is immediate, src2w is masked by (bit_length - 1).
1001    Otherwise, if the content of src2 is outside the range from 0
1002    to bit_length - 1, the result is undefined. */
1003 #define SLJIT_ASHR			(SLJIT_OP2_BASE + 10)
1004 #define SLJIT_ASHR32			(SLJIT_ASHR | SLJIT_I32_OP)
1005 
1006 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1007 	sljit_s32 dst, sljit_sw dstw,
1008 	sljit_s32 src1, sljit_sw src1w,
1009 	sljit_s32 src2, sljit_sw src2w);
1010 
1011 /* Starting index of opcodes for sljit_emit_fop1. */
1012 #define SLJIT_FOP1_BASE			128
1013 
1014 /* Flags: - (does not modify flags) */
1015 #define SLJIT_MOV_F64			(SLJIT_FOP1_BASE + 0)
1016 #define SLJIT_MOV_F32			(SLJIT_MOV_F64 | SLJIT_F32_OP)
1017 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
1018    SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
1019    Rounding mode when the destination is W or I: round towards zero. */
1020 /* Flags: - (does not modify flags) */
1021 #define SLJIT_CONV_F64_FROM_F32		(SLJIT_FOP1_BASE + 1)
1022 #define SLJIT_CONV_F32_FROM_F64		(SLJIT_CONV_F64_FROM_F32 | SLJIT_F32_OP)
1023 /* Flags: - (does not modify flags) */
1024 #define SLJIT_CONV_SW_FROM_F64		(SLJIT_FOP1_BASE + 2)
1025 #define SLJIT_CONV_SW_FROM_F32		(SLJIT_CONV_SW_FROM_F64 | SLJIT_F32_OP)
1026 /* Flags: - (does not modify flags) */
1027 #define SLJIT_CONV_S32_FROM_F64		(SLJIT_FOP1_BASE + 3)
1028 #define SLJIT_CONV_S32_FROM_F32		(SLJIT_CONV_S32_FROM_F64 | SLJIT_F32_OP)
1029 /* Flags: - (does not modify flags) */
1030 #define SLJIT_CONV_F64_FROM_SW		(SLJIT_FOP1_BASE + 4)
1031 #define SLJIT_CONV_F32_FROM_SW		(SLJIT_CONV_F64_FROM_SW | SLJIT_F32_OP)
1032 /* Flags: - (does not modify flags) */
1033 #define SLJIT_CONV_F64_FROM_S32		(SLJIT_FOP1_BASE + 5)
1034 #define SLJIT_CONV_F32_FROM_S32		(SLJIT_CONV_F64_FROM_S32 | SLJIT_F32_OP)
1035 /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
1036    Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
1037 #define SLJIT_CMP_F64			(SLJIT_FOP1_BASE + 6)
1038 #define SLJIT_CMP_F32			(SLJIT_CMP_F64 | SLJIT_F32_OP)
1039 /* Flags: - (does not modify flags) */
1040 #define SLJIT_NEG_F64			(SLJIT_FOP1_BASE + 7)
1041 #define SLJIT_NEG_F32			(SLJIT_NEG_F64 | SLJIT_F32_OP)
1042 /* Flags: - (does not modify flags) */
1043 #define SLJIT_ABS_F64			(SLJIT_FOP1_BASE + 8)
1044 #define SLJIT_ABS_F32			(SLJIT_ABS_F64 | SLJIT_F32_OP)
1045 
1046 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1047 	sljit_s32 dst, sljit_sw dstw,
1048 	sljit_s32 src, sljit_sw srcw);
1049 
1050 /* Starting index of opcodes for sljit_emit_fop2. */
1051 #define SLJIT_FOP2_BASE			160
1052 
1053 /* Flags: - (does not modify flags) */
1054 #define SLJIT_ADD_F64			(SLJIT_FOP2_BASE + 0)
1055 #define SLJIT_ADD_F32			(SLJIT_ADD_F64 | SLJIT_F32_OP)
1056 /* Flags: - (does not modify flags) */
1057 #define SLJIT_SUB_F64			(SLJIT_FOP2_BASE + 1)
1058 #define SLJIT_SUB_F32			(SLJIT_SUB_F64 | SLJIT_F32_OP)
1059 /* Flags: - (does not modify flags) */
1060 #define SLJIT_MUL_F64			(SLJIT_FOP2_BASE + 2)
1061 #define SLJIT_MUL_F32			(SLJIT_MUL_F64 | SLJIT_F32_OP)
1062 /* Flags: - (does not modify flags) */
1063 #define SLJIT_DIV_F64			(SLJIT_FOP2_BASE + 3)
1064 #define SLJIT_DIV_F32			(SLJIT_DIV_F64 | SLJIT_F32_OP)
1065 
1066 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1067 	sljit_s32 dst, sljit_sw dstw,
1068 	sljit_s32 src1, sljit_sw src1w,
1069 	sljit_s32 src2, sljit_sw src2w);
1070 
1071 /* Label and jump instructions. */
1072 
1073 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
1074 
1075 /* Invert (negate) conditional type: xor (^) with 0x1 */
1076 
1077 /* Integer comparison types. */
1078 #define SLJIT_EQUAL			0
1079 #define SLJIT_EQUAL32			(SLJIT_EQUAL | SLJIT_I32_OP)
1080 #define SLJIT_ZERO			0
1081 #define SLJIT_ZERO32			(SLJIT_ZERO | SLJIT_I32_OP)
1082 #define SLJIT_NOT_EQUAL			1
1083 #define SLJIT_NOT_EQUAL32		(SLJIT_NOT_EQUAL | SLJIT_I32_OP)
1084 #define SLJIT_NOT_ZERO			1
1085 #define SLJIT_NOT_ZERO32		(SLJIT_NOT_ZERO | SLJIT_I32_OP)
1086 
1087 #define SLJIT_LESS			2
1088 #define SLJIT_LESS32			(SLJIT_LESS | SLJIT_I32_OP)
1089 #define SLJIT_SET_LESS			SLJIT_SET(SLJIT_LESS)
1090 #define SLJIT_GREATER_EQUAL		3
1091 #define SLJIT_GREATER_EQUAL32		(SLJIT_GREATER_EQUAL | SLJIT_I32_OP)
1092 #define SLJIT_SET_GREATER_EQUAL		SLJIT_SET(SLJIT_GREATER_EQUAL)
1093 #define SLJIT_GREATER			4
1094 #define SLJIT_GREATER32			(SLJIT_GREATER | SLJIT_I32_OP)
1095 #define SLJIT_SET_GREATER		SLJIT_SET(SLJIT_GREATER)
1096 #define SLJIT_LESS_EQUAL		5
1097 #define SLJIT_LESS_EQUAL32		(SLJIT_LESS_EQUAL | SLJIT_I32_OP)
1098 #define SLJIT_SET_LESS_EQUAL		SLJIT_SET(SLJIT_LESS_EQUAL)
1099 #define SLJIT_SIG_LESS			6
1100 #define SLJIT_SIG_LESS32		(SLJIT_SIG_LESS | SLJIT_I32_OP)
1101 #define SLJIT_SET_SIG_LESS		SLJIT_SET(SLJIT_SIG_LESS)
1102 #define SLJIT_SIG_GREATER_EQUAL		7
1103 #define SLJIT_SIG_GREATER_EQUAL32	(SLJIT_SIG_GREATER_EQUAL | SLJIT_I32_OP)
1104 #define SLJIT_SET_SIG_GREATER_EQUAL	SLJIT_SET(SLJIT_SIG_GREATER_EQUAL)
1105 #define SLJIT_SIG_GREATER		8
1106 #define SLJIT_SIG_GREATER32		(SLJIT_SIG_GREATER | SLJIT_I32_OP)
1107 #define SLJIT_SET_SIG_GREATER		SLJIT_SET(SLJIT_SIG_GREATER)
1108 #define SLJIT_SIG_LESS_EQUAL		9
1109 #define SLJIT_SIG_LESS_EQUAL32		(SLJIT_SIG_LESS_EQUAL | SLJIT_I32_OP)
1110 #define SLJIT_SET_SIG_LESS_EQUAL	SLJIT_SET(SLJIT_SIG_LESS_EQUAL)
1111 
1112 #define SLJIT_OVERFLOW			10
1113 #define SLJIT_OVERFLOW32		(SLJIT_OVERFLOW | SLJIT_I32_OP)
1114 #define SLJIT_SET_OVERFLOW		SLJIT_SET(SLJIT_OVERFLOW)
1115 #define SLJIT_NOT_OVERFLOW		11
1116 #define SLJIT_NOT_OVERFLOW32		(SLJIT_NOT_OVERFLOW | SLJIT_I32_OP)
1117 
1118 #define SLJIT_MUL_OVERFLOW		12
1119 #define SLJIT_MUL_OVERFLOW32		(SLJIT_MUL_OVERFLOW | SLJIT_I32_OP)
1120 #define SLJIT_SET_MUL_OVERFLOW		SLJIT_SET(SLJIT_MUL_OVERFLOW)
1121 #define SLJIT_MUL_NOT_OVERFLOW		13
1122 #define SLJIT_MUL_NOT_OVERFLOW32	(SLJIT_MUL_NOT_OVERFLOW | SLJIT_I32_OP)
1123 
1124 /* There is no SLJIT_CARRY or SLJIT_NOT_CARRY. */
1125 #define SLJIT_SET_CARRY			SLJIT_SET(14)
1126 
1127 /* Floating point comparison types. */
1128 #define SLJIT_EQUAL_F64			16
1129 #define SLJIT_EQUAL_F32			(SLJIT_EQUAL_F64 | SLJIT_F32_OP)
1130 #define SLJIT_SET_EQUAL_F		SLJIT_SET(SLJIT_EQUAL_F64)
1131 #define SLJIT_NOT_EQUAL_F64		17
1132 #define SLJIT_NOT_EQUAL_F32		(SLJIT_NOT_EQUAL_F64 | SLJIT_F32_OP)
1133 #define SLJIT_SET_NOT_EQUAL_F		SLJIT_SET(SLJIT_NOT_EQUAL_F64)
1134 #define SLJIT_LESS_F64			18
1135 #define SLJIT_LESS_F32			(SLJIT_LESS_F64 | SLJIT_F32_OP)
1136 #define SLJIT_SET_LESS_F		SLJIT_SET(SLJIT_LESS_F64)
1137 #define SLJIT_GREATER_EQUAL_F64		19
1138 #define SLJIT_GREATER_EQUAL_F32		(SLJIT_GREATER_EQUAL_F64 | SLJIT_F32_OP)
1139 #define SLJIT_SET_GREATER_EQUAL_F	SLJIT_SET(SLJIT_GREATER_EQUAL_F64)
1140 #define SLJIT_GREATER_F64		20
1141 #define SLJIT_GREATER_F32		(SLJIT_GREATER_F64 | SLJIT_F32_OP)
1142 #define SLJIT_SET_GREATER_F		SLJIT_SET(SLJIT_GREATER_F64)
1143 #define SLJIT_LESS_EQUAL_F64		21
1144 #define SLJIT_LESS_EQUAL_F32		(SLJIT_LESS_EQUAL_F64 | SLJIT_F32_OP)
1145 #define SLJIT_SET_LESS_EQUAL_F		SLJIT_SET(SLJIT_LESS_EQUAL_F64)
1146 #define SLJIT_UNORDERED_F64		22
1147 #define SLJIT_UNORDERED_F32		(SLJIT_UNORDERED_F64 | SLJIT_F32_OP)
1148 #define SLJIT_SET_UNORDERED_F		SLJIT_SET(SLJIT_UNORDERED_F64)
1149 #define SLJIT_ORDERED_F64		23
1150 #define SLJIT_ORDERED_F32		(SLJIT_ORDERED_F64 | SLJIT_F32_OP)
1151 #define SLJIT_SET_ORDERED_F		SLJIT_SET(SLJIT_ORDERED_F64)
1152 
1153 /* Unconditional jump types. */
1154 #define SLJIT_JUMP			24
1155 	/* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
1156 #define SLJIT_FAST_CALL			25
1157 	/* Called function must be declared with the SLJIT_FUNC attribute. */
1158 #define SLJIT_CALL			26
1159 	/* Called function must be declared with cdecl attribute.
1160 	   This is the default attribute for C functions. */
1161 #define SLJIT_CALL_CDECL		27
1162 
1163 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1164 #define SLJIT_REWRITABLE_JUMP		0x1000
1165 
1166 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1167     type must be between SLJIT_EQUAL and SLJIT_FAST_CALL
1168     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1169 
1170    Flags: does not modify flags. */
1171 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type);
1172 
1173 /* Emit a C compiler (ABI) compatible function call.
1174     type must be SLJIT_CALL or SLJIT_CALL_CDECL
1175     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1176     arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1177 
1178    Flags: destroy all flags. */
1179 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 arg_types);
1180 
1181 /* Basic arithmetic comparison. In most architectures it is implemented as
1182    an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
1183    appropriate flags) followed by a sljit_emit_jump. However some
1184    architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
1185    It is suggested to use this comparison form when appropriate.
1186     type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
1187     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1188 
1189    Flags: may destroy flags. */
1190 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
1191 	sljit_s32 src1, sljit_sw src1w,
1192 	sljit_s32 src2, sljit_sw src2w);
1193 
1194 /* Basic floating point comparison. In most architectures it is implemented as
1195    an SLJIT_FCMP operation (setting appropriate flags) followed by a
1196    sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1197    special optimizations here. It is suggested to use this comparison form
1198    when appropriate.
1199     type must be between SLJIT_EQUAL_F64 and SLJIT_ORDERED_F32
1200     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1201    Flags: destroy flags.
1202    Note: if either operand is NaN, the behaviour is undefined for
1203          types up to SLJIT_S_LESS_EQUAL. */
1204 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_s32 type,
1205 	sljit_s32 src1, sljit_sw src1w,
1206 	sljit_s32 src2, sljit_sw src2w);
1207 
1208 /* Set the destination of the jump to this label. */
1209 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
1210 /* Set the destination address of the jump to this label. */
1211 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
1212 
1213 /* Emit an indirect jump or fast call.
1214    Direct form: set src to SLJIT_IMM() and srcw to the address
1215    Indirect form: any other valid addressing mode
1216     type must be between SLJIT_JUMP and SLJIT_FAST_CALL
1217 
1218    Flags: does not modify flags. */
1219 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw);
1220 
1221 /* Emit a C compiler (ABI) compatible function call.
1222    Direct form: set src to SLJIT_IMM() and srcw to the address
1223    Indirect form: any other valid addressing mode
1224     type must be SLJIT_CALL or SLJIT_CALL_CDECL
1225     arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1226 
1227    Flags: destroy all flags. */
1228 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 arg_types, sljit_s32 src, sljit_sw srcw);
1229 
1230 /* Perform the operation using the conditional flags as the second argument.
1231    Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_F64. The value
1232    represented by the type is 1, if the condition represented by the type
1233    is fulfilled, and 0 otherwise.
1234 
1235    If op == SLJIT_MOV, SLJIT_MOV32:
1236      Set dst to the value represented by the type (0 or 1).
1237      Flags: - (does not modify flags)
1238    If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1239      Performs the binary operation using dst as the first, and the value
1240      represented by type as the second argument. Result is written into dst.
1241      Flags: Z (may destroy flags) */
1242 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1243 	sljit_s32 dst, sljit_sw dstw,
1244 	sljit_s32 type);
1245 
1246 /* Emit a conditional mov instruction which moves source to destination,
1247    if the condition is satisfied. Unlike other arithmetic operations this
1248    instruction does not support memory access.
1249 
1250    type must be between SLJIT_EQUAL and SLJIT_ORDERED_F64
1251    dst_reg must be a valid register and it can be combined
1252       with SLJIT_I32_OP to perform a 32 bit arithmetic operation
1253    src must be register or immediate (SLJIT_IMM)
1254 
1255    Flags: - (does not modify flags) */
1256 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1257 	sljit_s32 dst_reg,
1258 	sljit_s32 src, sljit_sw srcw);
1259 
1260 /* The following flags are used by sljit_emit_mem() and sljit_emit_fmem(). */
1261 
1262 /* When SLJIT_MEM_SUPP is passed, no instructions are emitted.
1263    Instead the function returns with SLJIT_SUCCESS if the instruction
1264    form is supported and SLJIT_ERR_UNSUPPORTED otherwise. This flag
1265    allows runtime checking of available instruction forms. */
1266 #define SLJIT_MEM_SUPP		0x0200
1267 /* Memory load operation. This is the default. */
1268 #define SLJIT_MEM_LOAD		0x0000
1269 /* Memory store operation. */
1270 #define SLJIT_MEM_STORE		0x0400
1271 /* Base register is updated before the memory access. */
1272 #define SLJIT_MEM_PRE		0x0800
1273 /* Base register is updated after the memory access. */
1274 #define SLJIT_MEM_POST		0x1000
1275 
1276 /* Emit a single memory load or store with update instruction. When the
1277    requested instruction form is not supported by the CPU, it returns
1278    with SLJIT_ERR_UNSUPPORTED instead of emulating the instruction. This
1279    allows specializing tight loops based on the supported instruction
1280    forms (see SLJIT_MEM_SUPP flag).
1281 
1282    type must be between SLJIT_MOV and SLJIT_MOV_P and can be
1283      combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
1284      or SLJIT_MEM_POST must be specified.
1285    reg is the source or destination register, and must be
1286      different from the base register of the mem operand
1287    mem must be a SLJIT_MEM1() or SLJIT_MEM2() operand
1288 
1289    Flags: - (does not modify flags) */
1290 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1291 	sljit_s32 reg,
1292 	sljit_s32 mem, sljit_sw memw);
1293 
1294 /* Same as sljit_emit_mem except the followings:
1295 
1296    type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
1297      combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
1298      or SLJIT_MEM_POST must be specified.
1299    freg is the source or destination floating point register */
1300 
1301 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
1302 	sljit_s32 freg,
1303 	sljit_s32 mem, sljit_sw memw);
1304 
1305 /* Copies the base address of SLJIT_SP + offset to dst. The offset can be
1306    anything to negate the effect of relative addressing. For example if an
1307    array of sljit_sw values is stored on the stack from offset 0x40, and R0
1308    contains the offset of an array item plus 0x120, this item can be
1309    overwritten by two SLJIT instructions:
1310 
1311    sljit_get_local_base(compiler, SLJIT_R1, 0, 0x40 - 0x120);
1312    sljit_emit_op1(compiler, SLJIT_MOV, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_IMM, 0x5);
1313 
1314    Flags: - (may destroy flags) */
1315 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset);
1316 
1317 /* The constant can be changed runtime (see: sljit_set_const)
1318    Flags: - (does not modify flags) */
1319 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value);
1320 
1321 /* After the code generation the address for label, jump and const instructions
1322    are computed. Since these structures are freed by sljit_free_compiler, the
1323    addresses must be preserved by the user program elsewere. */
sljit_get_label_addr(struct sljit_label * label)1324 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
sljit_get_jump_addr(struct sljit_jump * jump)1325 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
sljit_get_const_addr(struct sljit_const * const_)1326 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
1327 
1328 /* Only the address and executable offset are required to perform dynamic
1329    code modifications. See sljit_get_executable_offset function. */
1330 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset);
1331 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset);
1332 
1333 /* --------------------------------------------------------------------- */
1334 /*  Miscellaneous utility functions                                      */
1335 /* --------------------------------------------------------------------- */
1336 
1337 #define SLJIT_MAJOR_VERSION	0
1338 #define SLJIT_MINOR_VERSION	94
1339 
1340 /* Get the human readable name of the platform. Can be useful on platforms
1341    like ARM, where ARM and Thumb2 functions can be mixed, and
1342    it is useful to know the type of the code generator. */
1343 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void);
1344 
1345 /* Portable helper function to get an offset of a member. */
1346 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1347 
1348 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
1349 /* This global lock is useful to compile common functions. */
1350 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void);
1351 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_release_lock(void);
1352 #endif
1353 
1354 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1355 
1356 /* The sljit_stack structure and its manipulation functions provides
1357    an implementation for a top-down stack. The stack top is stored
1358    in the end field of the sljit_stack structure and the stack goes
1359    down to the min_start field, so the memory region reserved for
1360    this stack is between min_start (inclusive) and end (exclusive)
1361    fields. However the application can only use the region between
1362    start (inclusive) and end (exclusive) fields. The sljit_stack_resize
1363    function can be used to extend this region up to min_start.
1364 
1365    This feature uses the "address space reserve" feature of modern
1366    operating systems. Instead of allocating a large memory block
1367    applications can allocate a small memory region and extend it
1368    later without moving the content of the memory area. Therefore
1369    after a successful resize by sljit_stack_resize all pointers into
1370    this region are still valid.
1371 
1372    Note:
1373      this structure may not be supported by all operating systems.
1374      end and max_limit fields are aligned to PAGE_SIZE bytes (usually
1375          4 Kbyte or more).
1376      stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */
1377 
1378 struct sljit_stack {
1379 	/* User data, anything can be stored here.
1380 	   Initialized to the same value as the end field. */
1381 	sljit_u8 *top;
1382 /* These members are read only. */
1383 	/* End address of the stack */
1384 	sljit_u8 *end;
1385 	/* Current start address of the stack. */
1386 	sljit_u8 *start;
1387 	/* Lowest start address of the stack. */
1388 	sljit_u8 *min_start;
1389 };
1390 
1391 /* Allocates a new stack. Returns NULL if unsuccessful.
1392    Note: see sljit_create_compiler for the explanation of allocator_data. */
1393 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data);
1394 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
1395 
1396 /* Can be used to increase (extend) or decrease (shrink) the stack
1397    memory area. Returns with new_start if successful and NULL otherwise.
1398    It always fails if new_start is less than min_start or greater or equal
1399    than end fields. The fields of the stack are not changed if the returned
1400    value is NULL (the current memory content is never lost). */
1401 SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start);
1402 
1403 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1404 
1405 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1406 
1407 /* Get the entry address of a given function. */
1408 #define SLJIT_FUNC_OFFSET(func_name)	((sljit_sw)func_name)
1409 
1410 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1411 
1412 /* All JIT related code should be placed in the same context (library, binary, etc.). */
1413 
1414 #define SLJIT_FUNC_OFFSET(func_name)	(*(sljit_sw*)(void*)func_name)
1415 
1416 /* For powerpc64, the function pointers point to a context descriptor. */
1417 struct sljit_function_context {
1418 	sljit_sw addr;
1419 	sljit_sw r2;
1420 	sljit_sw r11;
1421 };
1422 
1423 /* Fill the context arguments using the addr and the function.
1424    If func_ptr is NULL, it will not be set to the address of context
1425    If addr is NULL, the function address also comes from the func pointer. */
1426 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
1427 
1428 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1429 
1430 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
1431 /* Free unused executable memory. The allocator keeps some free memory
1432    around to reduce the number of OS executable memory allocations.
1433    This improves performance since these calls are costly. However
1434    it is sometimes desired to free all unused memory regions, e.g.
1435    before the application terminates. */
1436 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
1437 #endif
1438 
1439 /* --------------------------------------------------------------------- */
1440 /*  CPU specific functions                                               */
1441 /* --------------------------------------------------------------------- */
1442 
1443 /* The following function is a helper function for sljit_emit_op_custom.
1444    It returns with the real machine register index ( >=0 ) of any SLJIT_R,
1445    SLJIT_S and SLJIT_SP registers.
1446 
1447    Note: it returns with -1 for virtual registers (only on x86-32). */
1448 
1449 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg);
1450 
1451 /* The following function is a helper function for sljit_emit_op_custom.
1452    It returns with the real machine register index of any SLJIT_FLOAT register.
1453 
1454    Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
1455 
1456 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg);
1457 
1458 /* Any instruction can be inserted into the instruction stream by
1459    sljit_emit_op_custom. It has a similar purpose as inline assembly.
1460    The size parameter must match to the instruction size of the target
1461    architecture:
1462 
1463          x86: 0 < size <= 15. The instruction argument can be byte aligned.
1464       Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
1465               if size == 4, the instruction argument must be 4 byte aligned.
1466    Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
1467 
1468 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1469 	void *instruction, sljit_s32 size);
1470 
1471 /* Define the currently available CPU status flags. It is usually used after an
1472    sljit_emit_op_custom call to define which flags are set. */
1473 
1474 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_current_flags(struct sljit_compiler *compiler,
1475 	sljit_s32 current_flags);
1476 
1477 #endif /* _SLJIT_LIR_H_ */
1478