xref: /PHP-5.5/ext/pcre/pcrelib/sljit/sljitLir.h (revision ca02d9c2)
1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright 2009-2012 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 
103 /* --------------------------------------------------------------------- */
104 /*  Registers                                                            */
105 /* --------------------------------------------------------------------- */
106 
107 /*
108   Scratch (R) registers: registers whose may not preserve their values
109   across function calls.
110 
111   Saved (S) registers: registers whose preserve their values across
112   function calls.
113 
114   The scratch and saved register sets are overlap. The last scratch register
115   is the first saved register, the one before the last is the second saved
116   register, and so on.
117 
118   If an architecture provides two scratch and three saved registers,
119   its scratch and saved register sets are the following:
120 
121      R0   |  [S4]  |   R0 and S4 represent the same physical register
122      R1   |  [S3]  |   R1 and S3 represent the same physical register
123     [R2]  |   S2   |   R2 and S2 represent the same physical register
124     [R3]  |   S1   |   R3 and S1 represent the same physical register
125     [R4]  |   S0   |   R4 and S0 represent the same physical register
126 
127   Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
128         SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
129 
130   Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 10
131         and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 5. However, 4 registers
132         are virtual on x86-32. See below.
133 
134   The purpose of this definition is convenience. Although a register
135   is either scratch register or saved register, SLJIT allows accessing
136   them from the other set. For example, four registers can be used as
137   scratch registers and the fifth one as saved register on the architecture
138   above. Of course the last two scratch registers (R2 and R3) from this
139   four will be saved on the stack, because they are defined as saved
140   registers in the application binary interface. Still R2 and R3 can be
141   used for referencing to these registers instead of S2 and S1, which
142   makes easier to write platform independent code. Scratch registers
143   can be saved registers in a similar way, but these extra saved
144   registers will not be preserved across function calls! Hence the
145   application must save them on those platforms, where the number of
146   saved registers is too low. This can be done by copy them onto
147   the stack and restore them after a function call.
148 
149   Note: To emphasize that registers assigned to R2-R4 are saved
150         registers, they are enclosed by square brackets. S3-S4
151         are marked in a similar way.
152 
153   Note: sljit_emit_enter and sljit_set_context defines whether a register
154         is S or R register. E.g: when 3 scratches and 1 saved is mapped
155         by sljit_emit_enter, the allowed register set will be: R0-R2 and
156         S0. Although S2 is mapped to the same position as R2, it does not
157         available in the current configuration. Furthermore the R3 (S1)
158         register does not available as well.
159 */
160 
161 /* When SLJIT_UNUSED is specified as destination, the result is discarded. */
162 #define SLJIT_UNUSED		0
163 
164 /* Scratch registers. */
165 #define SLJIT_R0	1
166 #define SLJIT_R1	2
167 #define SLJIT_R2	3
168 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
169    are allocated on the stack). These registers are called virtual
170    and cannot be used for memory addressing (cannot be part of
171    any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
172    limitation on other CPUs. See sljit_get_register_index(). */
173 #define SLJIT_R3	4
174 #define SLJIT_R4	5
175 #define SLJIT_R5	6
176 #define SLJIT_R6	7
177 #define SLJIT_R7	8
178 #define SLJIT_R8	9
179 #define SLJIT_R9	10
180 /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
181    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
182 #define SLJIT_R(i)	(1 + (i))
183 
184 /* Saved registers. */
185 #define SLJIT_S0	(SLJIT_NUMBER_OF_REGISTERS)
186 #define SLJIT_S1	(SLJIT_NUMBER_OF_REGISTERS - 1)
187 #define SLJIT_S2	(SLJIT_NUMBER_OF_REGISTERS - 2)
188 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
189    are allocated on the stack). These registers are called virtual
190    and cannot be used for memory addressing (cannot be part of
191    any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
192    limitation on other CPUs. See sljit_get_register_index(). */
193 #define SLJIT_S3	(SLJIT_NUMBER_OF_REGISTERS - 3)
194 #define SLJIT_S4	(SLJIT_NUMBER_OF_REGISTERS - 4)
195 #define SLJIT_S5	(SLJIT_NUMBER_OF_REGISTERS - 5)
196 #define SLJIT_S6	(SLJIT_NUMBER_OF_REGISTERS - 6)
197 #define SLJIT_S7	(SLJIT_NUMBER_OF_REGISTERS - 7)
198 #define SLJIT_S8	(SLJIT_NUMBER_OF_REGISTERS - 8)
199 #define SLJIT_S9	(SLJIT_NUMBER_OF_REGISTERS - 9)
200 /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
201    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
202 #define SLJIT_S(i)	(SLJIT_NUMBER_OF_REGISTERS - (i))
203 
204 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
205 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
206 
207 /* The SLJIT_SP provides direct access to the linear stack space allocated by
208    sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
209    The immediate offset is extended by the relative stack offset automatically.
210    The sljit_get_local_base can be used to obtain the absolute offset. */
211 #define SLJIT_SP	(SLJIT_NUMBER_OF_REGISTERS + 1)
212 
213 /* Return with machine word. */
214 
215 #define SLJIT_RETURN_REG	SLJIT_R0
216 
217 /* x86 prefers specific registers for special purposes. In case of shift
218    by register it supports only SLJIT_R2 for shift argument
219    (which is the src2 argument of sljit_emit_op2). If another register is
220    used, sljit must exchange data between registers which cause a minor
221    slowdown. Other architectures has no such limitation. */
222 
223 #define SLJIT_PREF_SHIFT_REG	SLJIT_R2
224 
225 /* --------------------------------------------------------------------- */
226 /*  Floating point registers                                             */
227 /* --------------------------------------------------------------------- */
228 
229 /* Each floating point register can store a double or single precision
230    value. The FR and FS register sets are overlap in the same way as R
231    and S register sets. See above. */
232 
233 /* Note: SLJIT_UNUSED as destination is not valid for floating point
234    operations, since they cannot be used for setting flags. */
235 
236 /* Floating point scratch registers. */
237 #define SLJIT_FR0	1
238 #define SLJIT_FR1	2
239 #define SLJIT_FR2	3
240 #define SLJIT_FR3	4
241 #define SLJIT_FR4	5
242 #define SLJIT_FR5	6
243 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
244    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
245 #define SLJIT_FR(i)	(1 + (i))
246 
247 /* Floating point saved registers. */
248 #define SLJIT_FS0	(SLJIT_NUMBER_OF_FLOAT_REGISTERS)
249 #define SLJIT_FS1	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
250 #define SLJIT_FS2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
251 #define SLJIT_FS3	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
252 #define SLJIT_FS4	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
253 #define SLJIT_FS5	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
254 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
255    The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
256 #define SLJIT_FS(i)	(SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
257 
258 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
259 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
260 
261 /* --------------------------------------------------------------------- */
262 /*  Main structures and functions                                        */
263 /* --------------------------------------------------------------------- */
264 
265 /*
266 	The following structures are private, and can be changed in the
267 	future. Keeping them here allows code inlining.
268 */
269 
270 struct sljit_memory_fragment {
271 	struct sljit_memory_fragment *next;
272 	sljit_uw used_size;
273 	/* Must be aligned to sljit_sw. */
274 	sljit_ub memory[1];
275 };
276 
277 struct sljit_label {
278 	struct sljit_label *next;
279 	sljit_uw addr;
280 	/* The maximum size difference. */
281 	sljit_uw size;
282 };
283 
284 struct sljit_jump {
285 	struct sljit_jump *next;
286 	sljit_uw addr;
287 	sljit_sw flags;
288 	union {
289 		sljit_uw target;
290 		struct sljit_label* label;
291 	} u;
292 };
293 
294 struct sljit_const {
295 	struct sljit_const *next;
296 	sljit_uw addr;
297 };
298 
299 struct sljit_compiler {
300 	sljit_si error;
301 	sljit_si options;
302 
303 	struct sljit_label *labels;
304 	struct sljit_jump *jumps;
305 	struct sljit_const *consts;
306 	struct sljit_label *last_label;
307 	struct sljit_jump *last_jump;
308 	struct sljit_const *last_const;
309 
310 	void *allocator_data;
311 	struct sljit_memory_fragment *buf;
312 	struct sljit_memory_fragment *abuf;
313 
314 	/* Used scratch registers. */
315 	sljit_si scratches;
316 	/* Used saved registers. */
317 	sljit_si saveds;
318 	/* Used float scratch registers. */
319 	sljit_si fscratches;
320 	/* Used float saved registers. */
321 	sljit_si fsaveds;
322 	/* Local stack size. */
323 	sljit_si local_size;
324 	/* Code size. */
325 	sljit_uw size;
326 	/* For statistical purposes. */
327 	sljit_uw executable_size;
328 
329 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
330 	sljit_si args;
331 #endif
332 
333 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
334 	sljit_si mode32;
335 #endif
336 
337 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
338 	sljit_si flags_saved;
339 #endif
340 
341 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
342 	/* Constant pool handling. */
343 	sljit_uw *cpool;
344 	sljit_ub *cpool_unique;
345 	sljit_uw cpool_diff;
346 	sljit_uw cpool_fill;
347 	/* Other members. */
348 	/* Contains pointer, "ldr pc, [...]" pairs. */
349 	sljit_uw patches;
350 #endif
351 
352 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
353 	/* Temporary fields. */
354 	sljit_uw shift_imm;
355 	sljit_si cache_arg;
356 	sljit_sw cache_argw;
357 #endif
358 
359 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
360 	sljit_si cache_arg;
361 	sljit_sw cache_argw;
362 #endif
363 
364 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
365 	sljit_si cache_arg;
366 	sljit_sw cache_argw;
367 #endif
368 
369 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
370 	sljit_sw imm;
371 	sljit_si cache_arg;
372 	sljit_sw cache_argw;
373 #endif
374 
375 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
376 	sljit_si delay_slot;
377 	sljit_si cache_arg;
378 	sljit_sw cache_argw;
379 #endif
380 
381 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
382 	sljit_si delay_slot;
383 	sljit_si cache_arg;
384 	sljit_sw cache_argw;
385 #endif
386 
387 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
388 	sljit_si cache_arg;
389 	sljit_sw cache_argw;
390 #endif
391 
392 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
393 	FILE* verbose;
394 #endif
395 
396 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
397 		|| (defined SLJIT_DEBUG && SLJIT_DEBUG)
398 	/* Local size passed to the functions. */
399 	sljit_si logical_local_size;
400 #endif
401 
402 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
403 		|| (defined SLJIT_DEBUG && SLJIT_DEBUG) \
404 		|| (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
405 	sljit_si skip_checks;
406 #endif
407 };
408 
409 /* --------------------------------------------------------------------- */
410 /*  Main functions                                                       */
411 /* --------------------------------------------------------------------- */
412 
413 /* Creates an sljit compiler. The allocator_data is required by some
414    custom memory managers. This pointer is passed to SLJIT_MALLOC
415    and SLJIT_FREE macros. Most allocators (including the default
416    one) ignores this value, and it is recommended to pass NULL
417    as a dummy value for allocator_data.
418 
419    Returns NULL if failed. */
420 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data);
421 
422 /* Frees everything except the compiled machine code. */
423 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
424 
425 /* Returns the current error code. If an error is occurred, future sljit
426    calls which uses the same compiler argument returns early with the same
427    error code. Thus there is no need for checking the error after every
428    call, it is enough to do it before the code is compiled. Removing
429    these checks increases the performance of the compiling process. */
sljit_get_compiler_error(struct sljit_compiler * compiler)430 static SLJIT_INLINE sljit_si sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
431 
432 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
433    if an error was detected before. After the error code is set
434    the compiler behaves as if the allocation failure happened
435    during an sljit function call. This can greatly simplify error
436    checking, since only the compiler status needs to be checked
437    after the compilation. */
438 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
439 
440 /*
441    Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
442    and <= 128 bytes on 64 bit architectures. The memory area is owned by the
443    compiler, and freed by sljit_free_compiler. The returned pointer is
444    sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
445    the compiling, and no need to worry about freeing them. The size is
446    enough to contain at most 16 pointers. If the size is outside of the range,
447    the function will return with NULL. However, this return value does not
448    indicate that there is no more memory (does not set the current error code
449    of the compiler to out-of-memory status).
450 */
451 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size);
452 
453 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
454 /* Passing NULL disables verbose. */
455 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
456 #endif
457 
458 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
459 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
460 
461 /*
462    After the machine code generation is finished we can retrieve the allocated
463    executable memory size, although this area may not be fully filled with
464    instructions depending on some optimizations. This function is useful only
465    for statistical purposes.
466 
467    Before a successful code generation, this function returns with 0.
468 */
sljit_get_generated_code_size(struct sljit_compiler * compiler)469 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
470 
471 /* Instruction generation. Returns with any error code. If there is no
472    error, they return with SLJIT_SUCCESS. */
473 
474 /*
475    The executable code is a function call from the viewpoint of the C
476    language. The function calls must obey to the ABI (Application
477    Binary Interface) of the platform, which specify the purpose of
478    all machine registers and stack handling among other things. The
479    sljit_emit_enter function emits the necessary instructions for
480    setting up a new context for the executable code and moves function
481    arguments to the saved registers. Furthermore the options argument
482    can be used to pass configuration options to the compiler. The
483    available options are listed before sljit_emit_enter.
484 
485    The number of sljit_sw arguments passed to the generated function
486    are specified in the "args" parameter. The number of arguments must
487    be less than or equal to 3. The first argument goes to SLJIT_S0,
488    the second goes to SLJIT_S1 and so on. The register set used by
489    the function must be declared as well. The number of scratch and
490    saved registers used by the function must be passed to sljit_emit_enter.
491    Only R registers between R0 and "scratches" argument can be used
492    later. E.g. if "scratches" is set to 2, the register set will be
493    limited to R0 and R1. The S registers and the floating point
494    registers ("fscratches" and "fsaveds") are specified in a similar
495    way. The sljit_emit_enter is also capable of allocating a stack
496    space for local variables. The "local_size" argument contains the
497    size in bytes of this local area and its staring address is stored
498    in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
499    SLJIT_SP + local_size (exclusive) can be modified freely until
500    the function returns. The stack space is not initialized.
501 
502    Note: the following conditions must met:
503          0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
504          0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
505          scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
506          0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
507          0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
508          fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
509 
510    Note: every call of sljit_emit_enter and sljit_set_context
511          overwrites the previous context.
512 */
513 
514 /* The absolute address returned by sljit_get_local_base with
515 offset 0 is aligned to sljit_d. Otherwise it is aligned to sljit_uw. */
516 #define SLJIT_DOUBLE_ALIGNMENT 0x00000001
517 
518 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
519 #define SLJIT_MAX_LOCAL_SIZE	65536
520 
521 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
522 	sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
523 	sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
524 
525 /* The machine code has a context (which contains the local stack space size,
526    number of used registers, etc.) which initialized by sljit_emit_enter. Several
527    functions (like sljit_emit_return) requres this context to be able to generate
528    the appropriate code. However, some code fragments (like inline cache) may have
529    no normal entry point so their context is unknown for the compiler. Their context
530    can be provided to the compiler by the sljit_set_context function.
531 
532    Note: every call of sljit_emit_enter and sljit_set_context overwrites
533          the previous context. */
534 
535 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_set_context(struct sljit_compiler *compiler,
536 	sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
537 	sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
538 
539 /* Return from machine code.  The op argument can be SLJIT_UNUSED which means the
540    function does not return with anything or any opcode between SLJIT_MOV and
541    SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
542    is SLJIT_UNUSED, otherwise see below the description about source and
543    destination arguments. */
544 
545 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op,
546 	sljit_si src, sljit_sw srcw);
547 
548 /* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and
549    even the stack frame is passed to the callee. The return address is preserved in
550    dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function
551    is sljit_p), and sljit_emit_fast_return can use this as a return value later. */
552 
553 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine
554    instructions are needed. Excellent for small uility functions, where saving registers
555    and setting up a new stack frame would cost too much performance. However, it is still
556    possible to return to the address of the caller (or anywhere else). */
557 
558 /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
559 
560 /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
561    since many architectures do clever branch prediction on call / return instruction pairs. */
562 
563 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw);
564 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw);
565 
566 /*
567    Source and destination values for arithmetical instructions
568     imm              - a simple immediate value (cannot be used as a destination)
569     reg              - any of the registers (immediate argument must be 0)
570     [imm]            - absolute immediate memory address
571     [reg+imm]        - indirect memory address
572     [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
573                        useful for (byte, half, int, sljit_sw) array access
574                        (fully supported by both x86 and ARM architectures, and cheap operation on others)
575 */
576 
577 /*
578    IMPORATNT NOTE: memory access MUST be naturally aligned except
579                    SLJIT_UNALIGNED macro is defined and its value is 1.
580 
581      length | alignment
582    ---------+-----------
583      byte   | 1 byte (any physical_address is accepted)
584      half   | 2 byte (physical_address & 0x1 == 0)
585      int    | 4 byte (physical_address & 0x3 == 0)
586      word   | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
587             | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
588     pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
589             | on 64 bit machines)
590 
591    Note:   Different architectures have different addressing limitations.
592            A single instruction is enough for the following addressing
593            modes. Other adrressing modes are emulated by instruction
594            sequences. This information could help to improve those code
595            generators which focuses only a few architectures.
596 
597    x86:    [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
598            [reg+(reg<<imm)] is supported
599            [imm], -2^32+1 <= imm <= 2^32-1 is supported
600            Write-back is not supported
601    arm:    [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
602                 bytes, any halfs or floating point values)
603            [reg+(reg<<imm)] is supported
604            Write-back is supported
605    arm-t2: [reg+imm], -255 <= imm <= 4095
606            [reg+(reg<<imm)] is supported
607            Write back is supported only for [reg+imm], where -255 <= imm <= 255
608    ppc:    [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
609                 signed load on 64 bit requires immediates divisible by 4.
610                 [reg+imm] is not supported for signed 8 bit values.
611            [reg+reg] is supported
612            Write-back is supported except for one instruction: 32 bit signed
613                 load with [reg+imm] addressing mode on 64 bit.
614    mips:   [reg+imm], -65536 <= imm <= 65535
615    sparc:  [reg+imm], -4096 <= imm <= 4095
616            [reg+reg] is supported
617 */
618 
619 /* Register output: simply the name of the register.
620    For destination, you can use SLJIT_UNUSED as well. */
621 #define SLJIT_MEM		0x80
622 #define SLJIT_MEM0()		(SLJIT_MEM)
623 #define SLJIT_MEM1(r1)		(SLJIT_MEM | (r1))
624 #define SLJIT_MEM2(r1, r2)	(SLJIT_MEM | (r1) | ((r2) << 8))
625 #define SLJIT_IMM		0x40
626 
627 /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
628    32 bit CPUs. If this flag is set for an arithmetic operation, it uses only the
629    lower 32 bit of the input register(s), and set the CPU status flags according
630    to the 32 bit result. The higher 32 bits are undefined for both the input and
631    output. However, the CPU might not ignore those higher 32 bits, like MIPS, which
632    expects it to be the sign extension of the lower 32 bit. All 32 bit operations
633    are undefined, if this condition is not fulfilled. Therefore, when SLJIT_INT_OP
634    is specified, all register arguments must be the result of other operations with
635    the same SLJIT_INT_OP flag. In other words, although a register can hold either
636    a 64 or 32 bit value, these values cannot be mixed. The only exceptions are
637    SLJIT_IMOV and SLJIT_IMOVU (SLJIT_MOV_SI/SLJIT_MOVU_SI with SLJIT_INT_OP flag)
638    which can convert any source argument to SLJIT_INT_OP compatible result. This
639    conversion might be unnecessary on some CPUs like x86-64, since the upper 32
640    bit is always ignored. In this case SLJIT is clever enough to not generate any
641    instructions if the source and destination operands are the same registers.
642    Affects sljit_emit_op0, sljit_emit_op1 and sljit_emit_op2. */
643 #define SLJIT_INT_OP		0x100
644 
645 /* Single precision mode (SP). This flag is similar to SLJIT_INT_OP, just
646    it applies to floating point registers (it is even the same bit). When
647    this flag is passed, the CPU performs single precision floating point
648    operations. Similar to SLJIT_INT_OP, all register arguments must be the
649    result of other floating point operations with this flag. Affects
650    sljit_emit_fop1, sljit_emit_fop2 and sljit_emit_fcmp. */
651 #define SLJIT_SINGLE_OP		0x100
652 
653 /* Common CPU status flags for all architectures (x86, ARM, PPC)
654     - carry flag
655     - overflow flag
656     - zero flag
657     - negative/positive flag (depends on arc)
658    On mips, these flags are emulated by software. */
659 
660 /* By default, the instructions may, or may not set the CPU status flags.
661    Forcing to set or keep status flags can be done with the following flags: */
662 
663 /* Note: sljit tries to emit the minimum number of instructions. Using these
664    flags can increase them, so use them wisely to avoid unnecessary code generation. */
665 
666 /* Set Equal (Zero) status flag (E). */
667 #define SLJIT_SET_E			0x0200
668 /* Set unsigned status flag (U). */
669 #define SLJIT_SET_U			0x0400
670 /* Set signed status flag (S). */
671 #define SLJIT_SET_S			0x0800
672 /* Set signed overflow flag (O). */
673 #define SLJIT_SET_O			0x1000
674 /* Set carry flag (C).
675    Note: Kinda unsigned overflow, but behaves differently on various cpus. */
676 #define SLJIT_SET_C			0x2000
677 /* Do not modify the flags (K).
678    Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
679 #define SLJIT_KEEP_FLAGS		0x4000
680 
681 /* Notes:
682      - you cannot postpone conditional jump instructions except if noted that
683        the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
684      - flag combinations: '|' means 'logical or'. */
685 
686 /* Starting index of opcodes for sljit_emit_op0. */
687 #define SLJIT_OP0_BASE			0
688 
689 /* Flags: - (never set any flags)
690    Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
691          It falls back to SLJIT_NOP in those cases. */
692 #define SLJIT_BREAKPOINT		(SLJIT_OP0_BASE + 0)
693 /* Flags: - (never set any flags)
694    Note: may or may not cause an extra cycle wait
695          it can even decrease the runtime in a few cases. */
696 #define SLJIT_NOP			(SLJIT_OP0_BASE + 1)
697 /* Flags: - (may destroy flags)
698    Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
699    Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
700 #define SLJIT_LUMUL			(SLJIT_OP0_BASE + 2)
701 /* Flags: - (may destroy flags)
702    Signed multiplication of SLJIT_R0 and SLJIT_R1.
703    Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
704 #define SLJIT_LSMUL			(SLJIT_OP0_BASE + 3)
705 /* Flags: I - (may destroy flags)
706    Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
707    The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
708    Note: if SLJIT_R1 is 0, the behaviour is undefined. */
709 #define SLJIT_UDIVMOD			(SLJIT_OP0_BASE + 4)
710 #define SLJIT_IUDIVMOD			(SLJIT_UDIVMOD | SLJIT_INT_OP)
711 /* Flags: I - (may destroy flags)
712    Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
713    The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
714    Note: if SLJIT_R1 is 0, the behaviour is undefined.
715    Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
716          the behaviour is undefined. */
717 #define SLJIT_SDIVMOD			(SLJIT_OP0_BASE + 5)
718 #define SLJIT_ISDIVMOD			(SLJIT_SDIVMOD | SLJIT_INT_OP)
719 /* Flags: I - (may destroy flags)
720    Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
721    The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
722    Note: if SLJIT_R1 is 0, the behaviour is undefined.
723    Note: SLJIT_SDIV is single precision divide. */
724 #define SLJIT_UDIVI			(SLJIT_OP0_BASE + 6)
725 #define SLJIT_IUDIVI			(SLJIT_UDIVI | SLJIT_INT_OP)
726 /* Flags: I - (may destroy flags)
727    Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
728    The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
729    Note: if SLJIT_R1 is 0, the behaviour is undefined.
730    Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
731          the behaviour is undefined.
732    Note: SLJIT_SDIV is single precision divide. */
733 #define SLJIT_SDIVI			(SLJIT_OP0_BASE + 7)
734 #define SLJIT_ISDIVI			(SLJIT_SDIVI | SLJIT_INT_OP)
735 
736 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op);
737 
738 /* Starting index of opcodes for sljit_emit_op1. */
739 #define SLJIT_OP1_BASE			32
740 
741 /* Notes for MOV instructions:
742    U = Mov with update (pre form). If source or destination defined as SLJIT_MEM1(r1)
743        or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
744    UB = unsigned byte (8 bit)
745    SB = signed byte (8 bit)
746    UH = unsigned half (16 bit)
747    SH = signed half (16 bit)
748    UI = unsigned int (32 bit)
749    SI = signed int (32 bit)
750    P  = pointer (sljit_p) size */
751 
752 /* Flags: - (never set any flags) */
753 #define SLJIT_MOV			(SLJIT_OP1_BASE + 0)
754 /* Flags: I - (never set any flags) */
755 #define SLJIT_MOV_UB			(SLJIT_OP1_BASE + 1)
756 #define SLJIT_IMOV_UB			(SLJIT_MOV_UB | SLJIT_INT_OP)
757 /* Flags: I - (never set any flags) */
758 #define SLJIT_MOV_SB			(SLJIT_OP1_BASE + 2)
759 #define SLJIT_IMOV_SB			(SLJIT_MOV_SB | SLJIT_INT_OP)
760 /* Flags: I - (never set any flags) */
761 #define SLJIT_MOV_UH			(SLJIT_OP1_BASE + 3)
762 #define SLJIT_IMOV_UH			(SLJIT_MOV_UH | SLJIT_INT_OP)
763 /* Flags: I - (never set any flags) */
764 #define SLJIT_MOV_SH			(SLJIT_OP1_BASE + 4)
765 #define SLJIT_IMOV_SH			(SLJIT_MOV_SH | SLJIT_INT_OP)
766 /* Flags: I - (never set any flags)
767    Note: see SLJIT_INT_OP for further details. */
768 #define SLJIT_MOV_UI			(SLJIT_OP1_BASE + 5)
769 /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOV. */
770 /* Flags: I - (never set any flags)
771    Note: see SLJIT_INT_OP for further details. */
772 #define SLJIT_MOV_SI			(SLJIT_OP1_BASE + 6)
773 #define SLJIT_IMOV			(SLJIT_MOV_SI | SLJIT_INT_OP)
774 /* Flags: - (never set any flags) */
775 #define SLJIT_MOV_P			(SLJIT_OP1_BASE + 7)
776 /* Flags: - (never set any flags) */
777 #define SLJIT_MOVU			(SLJIT_OP1_BASE + 8)
778 /* Flags: I - (never set any flags) */
779 #define SLJIT_MOVU_UB			(SLJIT_OP1_BASE + 9)
780 #define SLJIT_IMOVU_UB			(SLJIT_MOVU_UB | SLJIT_INT_OP)
781 /* Flags: I - (never set any flags) */
782 #define SLJIT_MOVU_SB			(SLJIT_OP1_BASE + 10)
783 #define SLJIT_IMOVU_SB			(SLJIT_MOVU_SB | SLJIT_INT_OP)
784 /* Flags: I - (never set any flags) */
785 #define SLJIT_MOVU_UH			(SLJIT_OP1_BASE + 11)
786 #define SLJIT_IMOVU_UH			(SLJIT_MOVU_UH | SLJIT_INT_OP)
787 /* Flags: I - (never set any flags) */
788 #define SLJIT_MOVU_SH			(SLJIT_OP1_BASE + 12)
789 #define SLJIT_IMOVU_SH			(SLJIT_MOVU_SH | SLJIT_INT_OP)
790 /* Flags: I - (never set any flags)
791    Note: see SLJIT_INT_OP for further details. */
792 #define SLJIT_MOVU_UI			(SLJIT_OP1_BASE + 13)
793 /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOVU. */
794 /* Flags: I - (never set any flags)
795    Note: see SLJIT_INT_OP for further details. */
796 #define SLJIT_MOVU_SI			(SLJIT_OP1_BASE + 14)
797 #define SLJIT_IMOVU			(SLJIT_MOVU_SI | SLJIT_INT_OP)
798 /* Flags: - (never set any flags) */
799 #define SLJIT_MOVU_P			(SLJIT_OP1_BASE + 15)
800 /* Flags: I | E | K */
801 #define SLJIT_NOT			(SLJIT_OP1_BASE + 16)
802 #define SLJIT_INOT			(SLJIT_NOT | SLJIT_INT_OP)
803 /* Flags: I | E | O | K */
804 #define SLJIT_NEG			(SLJIT_OP1_BASE + 17)
805 #define SLJIT_INEG			(SLJIT_NEG | SLJIT_INT_OP)
806 /* Count leading zeroes
807    Flags: I | E | K
808    Important note! Sparc 32 does not support K flag, since
809    the required popc instruction is introduced only in sparc 64. */
810 #define SLJIT_CLZ			(SLJIT_OP1_BASE + 18)
811 #define SLJIT_ICLZ			(SLJIT_CLZ | SLJIT_INT_OP)
812 
813 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
814 	sljit_si dst, sljit_sw dstw,
815 	sljit_si src, sljit_sw srcw);
816 
817 /* Starting index of opcodes for sljit_emit_op2. */
818 #define SLJIT_OP2_BASE			96
819 
820 /* Flags: I | E | O | C | K */
821 #define SLJIT_ADD			(SLJIT_OP2_BASE + 0)
822 #define SLJIT_IADD			(SLJIT_ADD | SLJIT_INT_OP)
823 /* Flags: I | C | K */
824 #define SLJIT_ADDC			(SLJIT_OP2_BASE + 1)
825 #define SLJIT_IADDC			(SLJIT_ADDC | SLJIT_INT_OP)
826 /* Flags: I | E | U | S | O | C | K */
827 #define SLJIT_SUB			(SLJIT_OP2_BASE + 2)
828 #define SLJIT_ISUB			(SLJIT_SUB | SLJIT_INT_OP)
829 /* Flags: I | C | K */
830 #define SLJIT_SUBC			(SLJIT_OP2_BASE + 3)
831 #define SLJIT_ISUBC			(SLJIT_SUBC | SLJIT_INT_OP)
832 /* Note: integer mul
833    Flags: I | O (see SLJIT_C_MUL_*) | K */
834 #define SLJIT_MUL			(SLJIT_OP2_BASE + 4)
835 #define SLJIT_IMUL			(SLJIT_MUL | SLJIT_INT_OP)
836 /* Flags: I | E | K */
837 #define SLJIT_AND			(SLJIT_OP2_BASE + 5)
838 #define SLJIT_IAND			(SLJIT_AND | SLJIT_INT_OP)
839 /* Flags: I | E | K */
840 #define SLJIT_OR			(SLJIT_OP2_BASE + 6)
841 #define SLJIT_IOR			(SLJIT_OR | SLJIT_INT_OP)
842 /* Flags: I | E | K */
843 #define SLJIT_XOR			(SLJIT_OP2_BASE + 7)
844 #define SLJIT_IXOR			(SLJIT_XOR | SLJIT_INT_OP)
845 /* Flags: I | E | K
846    Let bit_length be the length of the shift operation: 32 or 64.
847    If src2 is immediate, src2w is masked by (bit_length - 1).
848    Otherwise, if the content of src2 is outside the range from 0
849    to bit_length - 1, the result is undefined. */
850 #define SLJIT_SHL			(SLJIT_OP2_BASE + 8)
851 #define SLJIT_ISHL			(SLJIT_SHL | SLJIT_INT_OP)
852 /* Flags: I | E | K
853    Let bit_length be the length of the shift operation: 32 or 64.
854    If src2 is immediate, src2w is masked by (bit_length - 1).
855    Otherwise, if the content of src2 is outside the range from 0
856    to bit_length - 1, the result is undefined. */
857 #define SLJIT_LSHR			(SLJIT_OP2_BASE + 9)
858 #define SLJIT_ILSHR			(SLJIT_LSHR | SLJIT_INT_OP)
859 /* Flags: I | E | K
860    Let bit_length be the length of the shift operation: 32 or 64.
861    If src2 is immediate, src2w is masked by (bit_length - 1).
862    Otherwise, if the content of src2 is outside the range from 0
863    to bit_length - 1, the result is undefined. */
864 #define SLJIT_ASHR			(SLJIT_OP2_BASE + 10)
865 #define SLJIT_IASHR			(SLJIT_ASHR | SLJIT_INT_OP)
866 
867 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
868 	sljit_si dst, sljit_sw dstw,
869 	sljit_si src1, sljit_sw src1w,
870 	sljit_si src2, sljit_sw src2w);
871 
872 /* Returns with non-zero if fpu is available. */
873 
874 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void);
875 
876 /* Starting index of opcodes for sljit_emit_fop1. */
877 #define SLJIT_FOP1_BASE			128
878 
879 /* Flags: SP - (never set any flags) */
880 #define SLJIT_DMOV			(SLJIT_FOP1_BASE + 0)
881 #define SLJIT_SMOV			(SLJIT_DMOV | SLJIT_SINGLE_OP)
882 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
883    SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
884    Rounding mode when the destination is W or I: round towards zero. */
885 /* Flags: SP - (never set any flags) */
886 #define SLJIT_CONVD_FROMS		(SLJIT_FOP1_BASE + 1)
887 #define SLJIT_CONVS_FROMD		(SLJIT_CONVD_FROMS | SLJIT_SINGLE_OP)
888 /* Flags: SP - (never set any flags) */
889 #define SLJIT_CONVW_FROMD		(SLJIT_FOP1_BASE + 2)
890 #define SLJIT_CONVW_FROMS		(SLJIT_CONVW_FROMD | SLJIT_SINGLE_OP)
891 /* Flags: SP - (never set any flags) */
892 #define SLJIT_CONVI_FROMD		(SLJIT_FOP1_BASE + 3)
893 #define SLJIT_CONVI_FROMS		(SLJIT_CONVI_FROMD | SLJIT_SINGLE_OP)
894 /* Flags: SP - (never set any flags) */
895 #define SLJIT_CONVD_FROMW		(SLJIT_FOP1_BASE + 4)
896 #define SLJIT_CONVS_FROMW		(SLJIT_CONVD_FROMW | SLJIT_SINGLE_OP)
897 /* Flags: SP - (never set any flags) */
898 #define SLJIT_CONVD_FROMI		(SLJIT_FOP1_BASE + 5)
899 #define SLJIT_CONVS_FROMI		(SLJIT_CONVD_FROMI | SLJIT_SINGLE_OP)
900 /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
901    Note: NaN check is always performed. If SLJIT_C_FLOAT_UNORDERED flag
902          is set, the comparison result is unpredictable.
903    Flags: SP | E | S (see SLJIT_C_FLOAT_*) */
904 #define SLJIT_DCMP			(SLJIT_FOP1_BASE + 6)
905 #define SLJIT_SCMP			(SLJIT_DCMP | SLJIT_SINGLE_OP)
906 /* Flags: SP - (never set any flags) */
907 #define SLJIT_DNEG			(SLJIT_FOP1_BASE + 7)
908 #define SLJIT_SNEG			(SLJIT_DNEG | SLJIT_SINGLE_OP)
909 /* Flags: SP - (never set any flags) */
910 #define SLJIT_DABS			(SLJIT_FOP1_BASE + 8)
911 #define SLJIT_SABS			(SLJIT_DABS | SLJIT_SINGLE_OP)
912 
913 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
914 	sljit_si dst, sljit_sw dstw,
915 	sljit_si src, sljit_sw srcw);
916 
917 /* Starting index of opcodes for sljit_emit_fop2. */
918 #define SLJIT_FOP2_BASE			160
919 
920 /* Flags: SP - (never set any flags) */
921 #define SLJIT_DADD			(SLJIT_FOP2_BASE + 0)
922 #define SLJIT_SADD			(SLJIT_DADD | SLJIT_SINGLE_OP)
923 /* Flags: SP - (never set any flags) */
924 #define SLJIT_DSUB			(SLJIT_FOP2_BASE + 1)
925 #define SLJIT_SSUB			(SLJIT_DSUB | SLJIT_SINGLE_OP)
926 /* Flags: SP - (never set any flags) */
927 #define SLJIT_DMUL			(SLJIT_FOP2_BASE + 2)
928 #define SLJIT_SMUL			(SLJIT_DMUL | SLJIT_SINGLE_OP)
929 /* Flags: SP - (never set any flags) */
930 #define SLJIT_DDIV			(SLJIT_FOP2_BASE + 3)
931 #define SLJIT_SDIV			(SLJIT_DDIV | SLJIT_SINGLE_OP)
932 
933 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
934 	sljit_si dst, sljit_sw dstw,
935 	sljit_si src1, sljit_sw src1w,
936 	sljit_si src2, sljit_sw src2w);
937 
938 /* Label and jump instructions. */
939 
940 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
941 
942 /* Invert (negate) conditional type: xor (^) with 0x1 */
943 
944 /* Integer comparison types. */
945 #define SLJIT_EQUAL			0
946 #define SLJIT_I_EQUAL			(SLJIT_EQUAL | SLJIT_INT_OP)
947 #define SLJIT_ZERO			0
948 #define SLJIT_I_ZERO			(SLJIT_ZERO | SLJIT_INT_OP)
949 #define SLJIT_NOT_EQUAL			1
950 #define SLJIT_I_NOT_EQUAL		(SLJIT_NOT_EQUAL | SLJIT_INT_OP)
951 #define SLJIT_NOT_ZERO			1
952 #define SLJIT_I_NOT_ZERO		(SLJIT_NOT_ZERO | SLJIT_INT_OP)
953 
954 #define SLJIT_LESS			2
955 #define SLJIT_I_LESS			(SLJIT_LESS | SLJIT_INT_OP)
956 #define SLJIT_GREATER_EQUAL		3
957 #define SLJIT_I_GREATER_EQUAL		(SLJIT_GREATER_EQUAL | SLJIT_INT_OP)
958 #define SLJIT_GREATER			4
959 #define SLJIT_I_GREATER			(SLJIT_GREATER | SLJIT_INT_OP)
960 #define SLJIT_LESS_EQUAL		5
961 #define SLJIT_I_LESS_EQUAL		(SLJIT_LESS_EQUAL | SLJIT_INT_OP)
962 #define SLJIT_SIG_LESS			6
963 #define SLJIT_I_SIG_LESS		(SLJIT_SIG_LESS | SLJIT_INT_OP)
964 #define SLJIT_SIG_GREATER_EQUAL		7
965 #define SLJIT_I_SIG_GREATER_EQUAL	(SLJIT_SIG_GREATER_EQUAL | SLJIT_INT_OP)
966 #define SLJIT_SIG_GREATER		8
967 #define SLJIT_I_SIG_GREATER		(SLJIT_SIG_GREATER | SLJIT_INT_OP)
968 #define SLJIT_SIG_LESS_EQUAL		9
969 #define SLJIT_I_SIG_LESS_EQUAL		(SLJIT_SIG_LESS_EQUAL | SLJIT_INT_OP)
970 
971 #define SLJIT_OVERFLOW			10
972 #define SLJIT_I_OVERFLOW		(SLJIT_OVERFLOW | SLJIT_INT_OP)
973 #define SLJIT_NOT_OVERFLOW		11
974 #define SLJIT_I_NOT_OVERFLOW		(SLJIT_NOT_OVERFLOW | SLJIT_INT_OP)
975 
976 #define SLJIT_MUL_OVERFLOW		12
977 #define SLJIT_I_MUL_OVERFLOW		(SLJIT_MUL_OVERFLOW | SLJIT_INT_OP)
978 #define SLJIT_MUL_NOT_OVERFLOW		13
979 #define SLJIT_I_MUL_NOT_OVERFLOW	(SLJIT_MUL_NOT_OVERFLOW | SLJIT_INT_OP)
980 
981 /* Floating point comparison types. */
982 #define SLJIT_D_EQUAL			14
983 #define SLJIT_S_EQUAL			(SLJIT_D_EQUAL | SLJIT_SINGLE_OP)
984 #define SLJIT_D_NOT_EQUAL		15
985 #define SLJIT_S_NOT_EQUAL		(SLJIT_D_NOT_EQUAL | SLJIT_SINGLE_OP)
986 #define SLJIT_D_LESS			16
987 #define SLJIT_S_LESS			(SLJIT_D_LESS | SLJIT_SINGLE_OP)
988 #define SLJIT_D_GREATER_EQUAL		17
989 #define SLJIT_S_GREATER_EQUAL		(SLJIT_D_GREATER_EQUAL | SLJIT_SINGLE_OP)
990 #define SLJIT_D_GREATER			18
991 #define SLJIT_S_GREATER			(SLJIT_D_GREATER | SLJIT_SINGLE_OP)
992 #define SLJIT_D_LESS_EQUAL		19
993 #define SLJIT_S_LESS_EQUAL		(SLJIT_D_LESS_EQUAL | SLJIT_SINGLE_OP)
994 #define SLJIT_D_UNORDERED		20
995 #define SLJIT_S_UNORDERED		(SLJIT_D_UNORDERED | SLJIT_SINGLE_OP)
996 #define SLJIT_D_ORDERED			21
997 #define SLJIT_S_ORDERED			(SLJIT_D_ORDERED | SLJIT_SINGLE_OP)
998 
999 /* Unconditional jump types. */
1000 #define SLJIT_JUMP			22
1001 #define SLJIT_FAST_CALL			23
1002 #define SLJIT_CALL0			24
1003 #define SLJIT_CALL1			25
1004 #define SLJIT_CALL2			26
1005 #define SLJIT_CALL3			27
1006 
1007 /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
1008 
1009 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1010 #define SLJIT_REWRITABLE_JUMP		0x1000
1011 
1012 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1013     type must be between SLJIT_EQUAL and SLJIT_CALL3
1014     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1015    Flags: - (never set any flags) for both conditional and unconditional jumps.
1016    Flags: destroy all flags for calls. */
1017 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type);
1018 
1019 /* Basic arithmetic comparison. In most architectures it is implemented as
1020    an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
1021    appropriate flags) followed by a sljit_emit_jump. However some
1022    architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
1023    It is suggested to use this comparison form when appropriate.
1024     type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
1025     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1026    Flags: destroy flags. */
1027 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
1028 	sljit_si src1, sljit_sw src1w,
1029 	sljit_si src2, sljit_sw src2w);
1030 
1031 /* Basic floating point comparison. In most architectures it is implemented as
1032    an SLJIT_FCMP operation (setting appropriate flags) followed by a
1033    sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1034    special optimizations here. It is suggested to use this comparison form
1035    when appropriate.
1036     type must be between SLJIT_D_EQUAL and SLJIT_S_ORDERED
1037     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1038    Flags: destroy flags.
1039    Note: if either operand is NaN, the behaviour is undefined for
1040          types up to SLJIT_S_LESS_EQUAL. */
1041 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1042 	sljit_si src1, sljit_sw src1w,
1043 	sljit_si src2, sljit_sw src2w);
1044 
1045 /* Set the destination of the jump to this label. */
1046 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
1047 /* Set the destination address of the jump to this label. */
1048 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
1049 
1050 /* Call function or jump anywhere. Both direct and indirect form
1051     type must be between SLJIT_JUMP and SLJIT_CALL3
1052     Direct form: set src to SLJIT_IMM() and srcw to the address
1053     Indirect form: any other valid addressing mode
1054    Flags: - (never set any flags) for unconditional jumps.
1055    Flags: destroy all flags for calls. */
1056 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw);
1057 
1058 /* Perform the operation using the conditional flags as the second argument.
1059    Type must always be between SLJIT_EQUAL and SLJIT_S_ORDERED. The value
1060    represented by the type is 1, if the condition represented by the type
1061    is fulfilled, and 0 otherwise.
1062 
1063    If op == SLJIT_MOV, SLJIT_MOV_SI, SLJIT_MOV_UI:
1064      Set dst to the value represented by the type (0 or 1).
1065      Src must be SLJIT_UNUSED, and srcw must be 0
1066      Flags: - (never set any flags)
1067    If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1068      Performs the binary operation using src as the first, and the value
1069      represented by type as the second argument.
1070      Important note: only dst=src and dstw=srcw is supported at the moment!
1071      Flags: I | E | K
1072    Note: sljit_emit_op_flags does nothing, if dst is SLJIT_UNUSED (regardless of op). */
1073 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1074 	sljit_si dst, sljit_sw dstw,
1075 	sljit_si src, sljit_sw srcw,
1076 	sljit_si type);
1077 
1078 /* Copies the base address of SLJIT_SP + offset to dst.
1079    Flags: - (never set any flags) */
1080 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset);
1081 
1082 /* The constant can be changed runtime (see: sljit_set_const)
1083    Flags: - (never set any flags) */
1084 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value);
1085 
1086 /* After the code generation the address for label, jump and const instructions
1087    are computed. Since these structures are freed by sljit_free_compiler, the
1088    addresses must be preserved by the user program elsewere. */
sljit_get_label_addr(struct sljit_label * label)1089 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
sljit_get_jump_addr(struct sljit_jump * jump)1090 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
sljit_get_const_addr(struct sljit_const * const_)1091 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
1092 
1093 /* Only the address is required to rewrite the code. */
1094 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
1095 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant);
1096 
1097 /* --------------------------------------------------------------------- */
1098 /*  Miscellaneous utility functions                                      */
1099 /* --------------------------------------------------------------------- */
1100 
1101 #define SLJIT_MAJOR_VERSION	0
1102 #define SLJIT_MINOR_VERSION	93
1103 
1104 /* Get the human readable name of the platform. Can be useful on platforms
1105    like ARM, where ARM and Thumb2 functions can be mixed, and
1106    it is useful to know the type of the code generator. */
1107 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
1108 
1109 /* Portable helper function to get an offset of a member. */
1110 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1111 
1112 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
1113 /* This global lock is useful to compile common functions. */
1114 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
1115 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
1116 #endif
1117 
1118 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1119 
1120 /* The sljit_stack is a utiliy feature of sljit, which allocates a
1121    writable memory region between base (inclusive) and limit (exclusive).
1122    Both base and limit is a pointer, and base is always <= than limit.
1123    This feature uses the "address space reserve" feature
1124    of modern operating systems. Basically we don't need to allocate a
1125    huge memory block in one step for the worst case, we can start with
1126    a smaller chunk and extend it later. Since the address space is
1127    reserved, the data never copied to other regions, thus it is safe
1128    to store pointers here. */
1129 
1130 /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
1131    Note: stack growing should not happen in small steps: 4k, 16k or even
1132      bigger growth is better.
1133    Note: this structure may not be supported by all operating systems.
1134      Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
1135      is not defined. */
1136 
1137 struct sljit_stack {
1138 	/* User data, anything can be stored here.
1139 	   Starting with the same value as base. */
1140 	sljit_uw top;
1141 	/* These members are read only. */
1142 	sljit_uw base;
1143 	sljit_uw limit;
1144 	sljit_uw max_limit;
1145 };
1146 
1147 /* Returns NULL if unsuccessful.
1148    Note: limit and max_limit contains the size for stack allocation.
1149    Note: the top field is initialized to base.
1150    Note: see sljit_create_compiler for the explanation of allocator_data. */
1151 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data);
1152 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
1153 
1154 /* Can be used to increase (allocate) or decrease (free) the memory area.
1155    Returns with a non-zero value if unsuccessful. If new_limit is greater than
1156    max_limit, it will fail. It is very easy to implement a stack data structure,
1157    since the growth ratio can be added to the current limit, and sljit_stack_resize
1158    will do all the necessary checks. The fields of the stack are not changed if
1159    sljit_stack_resize fails. */
1160 SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack *stack, sljit_uw new_limit);
1161 
1162 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1163 
1164 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1165 
1166 /* Get the entry address of a given function. */
1167 #define SLJIT_FUNC_OFFSET(func_name)	((sljit_sw)func_name)
1168 
1169 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1170 
1171 /* All JIT related code should be placed in the same context (library, binary, etc.). */
1172 
1173 #define SLJIT_FUNC_OFFSET(func_name)	(*(sljit_sw*)(void*)func_name)
1174 
1175 /* For powerpc64, the function pointers point to a context descriptor. */
1176 struct sljit_function_context {
1177 	sljit_sw addr;
1178 	sljit_sw r2;
1179 	sljit_sw r11;
1180 };
1181 
1182 /* Fill the context arguments using the addr and the function.
1183    If func_ptr is NULL, it will not be set to the address of context
1184    If addr is NULL, the function address also comes from the func pointer. */
1185 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
1186 
1187 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1188 
1189 /* --------------------------------------------------------------------- */
1190 /*  CPU specific functions                                               */
1191 /* --------------------------------------------------------------------- */
1192 
1193 /* The following function is a helper function for sljit_emit_op_custom.
1194    It returns with the real machine register index ( >=0 ) of any SLJIT_R,
1195    SLJIT_S and SLJIT_SP registers.
1196 
1197    Note: it returns with -1 for virtual registers (only on x86-32). */
1198 
1199 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg);
1200 
1201 /* The following function is a helper function for sljit_emit_op_custom.
1202    It returns with the real machine register index of any SLJIT_FLOAT register.
1203 
1204    Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
1205 
1206 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg);
1207 
1208 /* Any instruction can be inserted into the instruction stream by
1209    sljit_emit_op_custom. It has a similar purpose as inline assembly.
1210    The size parameter must match to the instruction size of the target
1211    architecture:
1212 
1213          x86: 0 < size <= 15. The instruction argument can be byte aligned.
1214       Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
1215               if size == 4, the instruction argument must be 4 byte aligned.
1216    Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
1217 
1218 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1219 	void *instruction, sljit_si size);
1220 
1221 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
1222 
1223 /* Returns with non-zero if sse2 is available. */
1224 
1225 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_is_sse2_available(void);
1226 
1227 /* Returns with non-zero if cmov instruction is available. */
1228 
1229 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_is_cmov_available(void);
1230 
1231 /* Emit a conditional mov instruction on x86 CPUs. This instruction
1232    moves src to destination, if the condition is satisfied. Unlike
1233    other arithmetic instructions, destination must be a register.
1234    Before such instructions are emitted, cmov support should be
1235    checked by sljit_x86_is_cmov_available function.
1236     type must be between SLJIT_EQUAL and SLJIT_S_ORDERED
1237     dst_reg must be a valid register and it can be combined
1238       with SLJIT_INT_OP to perform 32 bit arithmetic
1239    Flags: I - (never set any flags)
1240  */
1241 
1242 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_emit_cmov(struct sljit_compiler *compiler,
1243 	sljit_si type,
1244 	sljit_si dst_reg,
1245 	sljit_si src, sljit_sw srcw);
1246 
1247 #endif
1248 
1249 #endif /* _SLJIT_LIR_H_ */
1250