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