2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
31 ------------------------------------------------------------------------
32 Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
33 ------------------------------------------------------------------------
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
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
57 - No automatic register allocation, and temporary results are
58 not stored on the stack. (hence the name comes)
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
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)
70 - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
73 #if (defined SLJIT_HAVE_CONFIG_PRE && SLJIT_HAVE_CONFIG_PRE)
74 #include "sljitConfigPre.h"
75 #endif /* SLJIT_HAVE_CONFIG_PRE */
77 #include "sljitConfig.h"
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 */
83 #include "sljitConfigInternal.h"
85 #if (defined SLJIT_HAVE_CONFIG_POST && SLJIT_HAVE_CONFIG_POST)
86 #include "sljitConfigPost.h"
87 #endif /* SLJIT_HAVE_CONFIG_POST */
93 /* --------------------------------------------------------------------- */
95 /* --------------------------------------------------------------------- */
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
115 /* --------------------------------------------------------------------- */
117 /* --------------------------------------------------------------------- */
120 Scratch (R) registers: registers whose may not preserve their values
121 across function calls.
123 Saved (S) registers: registers whose preserve their values across
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
130 If an architecture provides two scratch and three saved registers,
131 its scratch and saved register sets are the following:
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
139 Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
140 SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
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.
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.
155 Note: To emphasize that registers assigned to R2-R4 are saved
156 registers, they are enclosed by square brackets.
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.
166 /* Scratch registers. */
170 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
171 are allocated on the stack). These registers are called virtual
172 and cannot be used for memory addressing (cannot be part of
173 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
174 limitation on other CPUs. See sljit_get_register_index(). */
182 /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
183 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
184 #define SLJIT_R(i) (1 + (i))
186 /* Saved registers. */
187 #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
188 #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
189 #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
190 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
191 are allocated on the stack). These registers are called virtual
192 and cannot be used for memory addressing (cannot be part of
193 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
194 limitation on other CPUs. See sljit_get_register_index(). */
195 #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
196 #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
197 #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
198 #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
199 #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
200 #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
201 #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
202 /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
203 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
204 #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
206 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
207 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
209 /* The SLJIT_SP provides direct access to the linear stack space allocated by
210 sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
211 The immediate offset is extended by the relative stack offset automatically.
212 The sljit_get_local_base can be used to obtain the absolute offset. */
213 #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
215 /* Return with machine word. */
217 #define SLJIT_RETURN_REG SLJIT_R0
219 /* --------------------------------------------------------------------- */
220 /* Floating point registers */
221 /* --------------------------------------------------------------------- */
223 /* Each floating point register can store a 32 or a 64 bit precision
224 value. The FR and FS register sets are overlap in the same way as R
225 and S register sets. See above. */
227 /* Floating point scratch registers. */
234 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
235 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
236 #define SLJIT_FR(i) (1 + (i))
238 /* Floating point saved registers. */
239 #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
240 #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
241 #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
242 #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
243 #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
244 #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
245 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
246 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
247 #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
249 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
250 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
252 /* --------------------------------------------------------------------- */
253 /* Argument type definitions */
254 /* --------------------------------------------------------------------- */
256 /* The following argument type definitions are used by sljit_emit_enter,
257 sljit_set_context, sljit_emit_call, and sljit_emit_icall functions.
259 As for sljit_emit_call and sljit_emit_icall, the first integer argument
260 must be placed into SLJIT_R0, the second one into SLJIT_R1, and so on.
261 Similarly the first floating point argument must be placed into SLJIT_FR0,
262 the second one into SLJIT_FR1, and so on.
264 As for sljit_emit_enter, the integer arguments can be stored in scratch
265 or saved registers. The first integer argument without _R postfix is
266 stored in SLJIT_S0, the next one in SLJIT_S1, and so on. The integer
267 arguments with _R postfix are placed into scratch registers. The index
268 of the scratch register is the count of the previous integer arguments
269 starting from SLJIT_R0. The floating point arguments are always placed
270 into SLJIT_FR0, SLJIT_FR1, and so on.
272 Note: if a function is called by sljit_emit_call/sljit_emit_icall and
273 an argument is stored in a scratch register by sljit_emit_enter,
274 that argument uses the same scratch register index for both
275 integer and floating point arguments.
277 Example function definition:
278 sljit_f32 SLJIT_FUNC example_c_callback(void *arg_a,
279 sljit_f64 arg_b, sljit_u32 arg_c, sljit_f32 arg_d);
281 Argument type definition:
282 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_F32)
283 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_P, 1) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F64, 2)
284 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_32, 3) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 4)
286 Short form of argument type definition:
287 SLJIT_ARGS4(32, P, F64, 32, F32)
290 arg_a must be placed in SLJIT_R0
291 arg_c must be placed in SLJIT_R1
292 arg_b must be placed in SLJIT_FR0
293 arg_d must be placed in SLJIT_FR1
295 Examples for argument processing by sljit_emit_enter:
296 SLJIT_ARGS4(VOID, P, 32_R, F32, W)
297 Arguments are placed into: SLJIT_S0, SLJIT_R1, SLJIT_FR0, SLJIT_S1
299 SLJIT_ARGS4(VOID, W, W_R, W, W_R)
300 Arguments are placed into: SLJIT_S0, SLJIT_R1, SLJIT_S1, SLJIT_R3
302 SLJIT_ARGS4(VOID, F64, W, F32, W_R)
303 Arguments are placed into: SLJIT_FR0, SLJIT_S0, SLJIT_FR1, SLJIT_R1
305 Note: it is recommended to pass the scratch arguments first
306 followed by the saved arguments:
308 SLJIT_ARGS4(VOID, W_R, W_R, W, W)
309 Arguments are placed into: SLJIT_R0, SLJIT_R1, SLJIT_S0, SLJIT_S1
312 /* The following flag is only allowed for the integer arguments of
313 sljit_emit_enter. When the flag is set, the integer argument is
314 stored in a scratch register instead of a saved register. */
315 #define SLJIT_ARG_TYPE_SCRATCH_REG 0x8
317 /* Void result, can only be used by SLJIT_ARG_RETURN. */
318 #define SLJIT_ARG_TYPE_VOID 0
319 /* Machine word sized integer argument or result. */
320 #define SLJIT_ARG_TYPE_W 1
321 #define SLJIT_ARG_TYPE_W_R (SLJIT_ARG_TYPE_W | SLJIT_ARG_TYPE_SCRATCH_REG)
322 /* 32 bit integer argument or result. */
323 #define SLJIT_ARG_TYPE_32 2
324 #define SLJIT_ARG_TYPE_32_R (SLJIT_ARG_TYPE_32 | SLJIT_ARG_TYPE_SCRATCH_REG)
325 /* Pointer sized integer argument or result. */
326 #define SLJIT_ARG_TYPE_P 3
327 #define SLJIT_ARG_TYPE_P_R (SLJIT_ARG_TYPE_P | SLJIT_ARG_TYPE_SCRATCH_REG)
328 /* 64 bit floating point argument or result. */
329 #define SLJIT_ARG_TYPE_F64 4
330 /* 32 bit floating point argument or result. */
331 #define SLJIT_ARG_TYPE_F32 5
333 #define SLJIT_ARG_SHIFT 4
334 #define SLJIT_ARG_RETURN(type) (type)
335 #define SLJIT_ARG_VALUE(type, idx) ((type) << ((idx) * SLJIT_ARG_SHIFT))
337 /* Simplified argument list definitions.
339 The following definition:
340 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_W) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 1)
346 #define SLJIT_ARG_TO_TYPE(type) SLJIT_ARG_TYPE_ ## type
348 #define SLJIT_ARGS0(ret) \
349 SLJIT_ARG_RETURN(SLJIT_ARG_TO_TYPE(ret))
351 #define SLJIT_ARGS1(ret, arg1) \
352 (SLJIT_ARGS0(ret) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg1), 1))
354 #define SLJIT_ARGS2(ret, arg1, arg2) \
355 (SLJIT_ARGS1(ret, arg1) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg2), 2))
357 #define SLJIT_ARGS3(ret, arg1, arg2, arg3) \
358 (SLJIT_ARGS2(ret, arg1, arg2) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg3), 3))
360 #define SLJIT_ARGS4(ret, arg1, arg2, arg3, arg4) \
361 (SLJIT_ARGS3(ret, arg1, arg2, arg3) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg4), 4))
363 /* --------------------------------------------------------------------- */
364 /* Main structures and functions */
365 /* --------------------------------------------------------------------- */
368 The following structures are private, and can be changed in the
369 future. Keeping them here allows code inlining.
372 struct sljit_memory_fragment
{
373 struct sljit_memory_fragment
*next
;
375 /* Must be aligned to sljit_sw. */
380 struct sljit_label
*next
;
382 /* The maximum size difference. */
387 struct sljit_jump
*next
;
392 struct sljit_label
*label
;
396 struct sljit_put_label
{
397 struct sljit_put_label
*next
;
398 struct sljit_label
*label
;
404 struct sljit_const
*next
;
408 struct sljit_compiler
{
412 struct sljit_label
*labels
;
413 struct sljit_jump
*jumps
;
414 struct sljit_put_label
*put_labels
;
415 struct sljit_const
*consts
;
416 struct sljit_label
*last_label
;
417 struct sljit_jump
*last_jump
;
418 struct sljit_const
*last_const
;
419 struct sljit_put_label
*last_put_label
;
421 void *allocator_data
;
422 void *exec_allocator_data
;
423 struct sljit_memory_fragment
*buf
;
424 struct sljit_memory_fragment
*abuf
;
426 /* Used scratch registers. */
428 /* Used saved registers. */
430 /* Used float scratch registers. */
431 sljit_s32 fscratches
;
432 /* Used float saved registers. */
434 /* Local stack size. */
435 sljit_s32 local_size
;
438 /* Relative offset of the executable mapping from the writable mapping. */
439 sljit_sw executable_offset
;
440 /* Executable size for statistical purposes. */
441 sljit_uw executable_size
;
443 #if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
444 sljit_s32 status_flags_state
;
447 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
451 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
455 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
456 /* Constant pool handling. */
458 sljit_u8
*cpool_unique
;
462 /* Contains pointer, "ldr pc, [...]" pairs. */
466 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
467 /* Temporary fields. */
469 #endif /* SLJIT_CONFIG_ARM_V5 || SLJIT_CONFIG_ARM_V7 */
471 #if (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)
475 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
479 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
480 sljit_s32 delay_slot
;
485 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
489 #if (defined SLJIT_CONFIG_RISCV && SLJIT_CONFIG_RISCV)
494 #if (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
495 /* Need to allocate register save area to make calls. */
499 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
503 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
504 || (defined SLJIT_DEBUG && SLJIT_DEBUG)
505 /* Flags specified by the last arithmetic instruction.
506 It contains the type of the variable flag. */
507 sljit_s32 last_flags
;
508 /* Return value type set by entry functions. */
509 sljit_s32 last_return
;
510 /* Local size passed to entry functions. */
511 sljit_s32 logical_local_size
;
514 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
515 || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
516 || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
517 /* Trust arguments when the API function is called. */
518 sljit_s32 skip_checks
;
522 /* --------------------------------------------------------------------- */
524 /* --------------------------------------------------------------------- */
526 /* Creates an sljit compiler. The allocator_data is required by some
527 custom memory managers. This pointer is passed to SLJIT_MALLOC
528 and SLJIT_FREE macros. Most allocators (including the default
529 one) ignores this value, and it is recommended to pass NULL
530 as a dummy value for allocator_data. The exec_allocator_data
531 has the same purpose but this one is passed to SLJIT_MALLOC_EXEC /
532 SLJIT_MALLOC_FREE functions.
534 Returns NULL if failed. */
535 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_compiler
* sljit_create_compiler(void *allocator_data
, void *exec_allocator_data
);
537 /* Frees everything except the compiled machine code. */
538 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_compiler(struct sljit_compiler
*compiler
);
540 /* Returns the current error code. If an error is occurred, future sljit
541 calls which uses the same compiler argument returns early with the same
542 error code. Thus there is no need for checking the error after every
543 call, it is enough to do it before the code is compiled. Removing
544 these checks increases the performance of the compiling process. */
545 static SLJIT_INLINE sljit_s32
sljit_get_compiler_error(struct sljit_compiler
*compiler
) { return compiler
->error
; }
547 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
548 if an error was detected before. After the error code is set
549 the compiler behaves as if the allocation failure happened
550 during an sljit function call. This can greatly simplify error
551 checking, since only the compiler status needs to be checked
552 after the compilation. */
553 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_compiler_memory_error(struct sljit_compiler
*compiler
);
556 Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
557 and <= 128 bytes on 64 bit architectures. The memory area is owned by the
558 compiler, and freed by sljit_free_compiler. The returned pointer is
559 sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
560 the compiling, and no need to worry about freeing them. The size is
561 enough to contain at most 16 pointers. If the size is outside of the range,
562 the function will return with NULL. However, this return value does not
563 indicate that there is no more memory (does not set the current error code
564 of the compiler to out-of-memory status).
566 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_alloc_memory(struct sljit_compiler
*compiler
, sljit_s32 size
);
568 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
569 /* Passing NULL disables verbose. */
570 SLJIT_API_FUNC_ATTRIBUTE
void sljit_compiler_verbose(struct sljit_compiler
*compiler
, FILE* verbose
);
574 Create executable code from the sljit instruction stream. This is the final step
575 of the code generation so no more instructions can be added after this call.
578 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
);
580 /* Free executable code. */
582 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_code(void* code
, void *exec_allocator_data
);
585 When the protected executable allocator is used the JIT code is mapped
586 twice. The first mapping has read/write and the second mapping has read/exec
587 permissions. This function returns with the relative offset of the executable
588 mapping using the writable mapping as the base after the machine code is
589 successfully generated. The returned value is always 0 for the normal executable
590 allocator, since it uses only one mapping with read/write/exec permissions.
591 Dynamic code modifications requires this value.
593 Before a successful code generation, this function returns with 0.
595 static SLJIT_INLINE sljit_sw
sljit_get_executable_offset(struct sljit_compiler
*compiler
) { return compiler
->executable_offset
; }
598 The executable memory consumption of the generated code can be retrieved by
599 this function. The returned value can be used for statistical purposes.
601 Before a successful code generation, this function returns with 0.
603 static SLJIT_INLINE sljit_uw
sljit_get_generated_code_size(struct sljit_compiler
*compiler
) { return compiler
->executable_size
; }
605 /* Returns with non-zero if the feature or limitation type passed as its
606 argument is present on the current CPU.
608 Some features (e.g. floating point operations) require hardware (CPU)
609 support while others (e.g. move with update) are emulated if not available.
610 However even if a feature is emulated, specialized code paths can be faster
611 than the emulation. Some limitations are emulated as well so their general
612 case is supported but it has extra performance costs. */
614 /* [Not emulated] Floating-point support is available. */
615 #define SLJIT_HAS_FPU 0
616 /* [Limitation] Some registers are virtual registers. */
617 #define SLJIT_HAS_VIRTUAL_REGISTERS 1
618 /* [Emulated] Has zero register (setting a memory location to zero is efficient). */
619 #define SLJIT_HAS_ZERO_REGISTER 2
620 /* [Emulated] Count leading zero is supported. */
621 #define SLJIT_HAS_CLZ 3
622 /* [Emulated] Conditional move is supported. */
623 #define SLJIT_HAS_CMOV 4
624 /* [Emulated] Conditional move is supported. */
625 #define SLJIT_HAS_PREFETCH 5
627 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
628 /* [Not emulated] SSE2 support is available on x86. */
629 #define SLJIT_HAS_SSE2 100
632 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
);
634 /* If type is between SLJIT_ORDERED_EQUAL and SLJIT_ORDERED_LESS_EQUAL,
635 sljit_cmp_info returns one, if the cpu supports the passed floating
636 point comparison type.
638 If type is SLJIT_UNORDERED or SLJIT_ORDERED, sljit_cmp_info returns
639 one, if the cpu supports checking the unordered comparison result
640 regardless of the comparison type passed to the comparison instruction.
641 The returned value is always one, if there is at least one type between
642 SLJIT_ORDERED_EQUAL and SLJIT_ORDERED_LESS_EQUAL where sljit_cmp_info
643 returns with a zero value.
645 Otherwise it returns zero. */
646 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_cmp_info(sljit_s32 type
);
648 /* Instruction generation. Returns with any error code. If there is no
649 error, they return with SLJIT_SUCCESS. */
652 The executable code is a function from the viewpoint of the C
653 language. The function calls must obey to the ABI (Application
654 Binary Interface) of the platform, which specify the purpose of
655 machine registers and stack handling among other things. The
656 sljit_emit_enter function emits the necessary instructions for
657 setting up a new context for the executable code and moves function
658 arguments to the saved registers. Furthermore the options argument
659 can be used to pass configuration options to the compiler. The
660 available options are listed before sljit_emit_enter.
662 The function argument list is the combination of SLJIT_ARGx
663 (SLJIT_DEF_ARG1) macros. Currently maximum 4 arguments are
664 supported. The first integer argument is loaded into SLJIT_S0,
665 the second one is loaded into SLJIT_S1, and so on. Similarly,
666 the first floating point argument is loaded into SLJIT_FR0,
667 the second one is loaded into SLJIT_FR1, and so on. Furthermore
668 the register set used by the function must be declared as well.
669 The number of scratch and saved registers used by the function
670 must be passed to sljit_emit_enter. Only R registers between R0
671 and "scratches" argument can be used later. E.g. if "scratches"
672 is set to 2, the scratch register set will be limited to SLJIT_R0
673 and SLJIT_R1. The S registers and the floating point registers
674 ("fscratches" and "fsaveds") are specified in a similar manner.
675 The sljit_emit_enter is also capable of allocating a stack space
676 for local variables. The "local_size" argument contains the size
677 in bytes of this local area and its staring address is stored
678 in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
679 SLJIT_SP + local_size (exclusive) can be modified freely until
680 the function returns. The stack space is not initialized.
682 Note: the following conditions must met:
683 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
684 0 <= saveds <= SLJIT_NUMBER_OF_SAVED_REGISTERS
685 scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
686 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
687 0 <= fsaveds <= SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
688 fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
690 Note: the compiler can use saved registers as scratch registers,
691 but the opposite is not supported
693 Note: every call of sljit_emit_enter and sljit_set_context
694 overwrites the previous context.
697 /* Saved registers between SLJIT_S0 and SLJIT_S(n - 1) (inclusive)
698 are not saved / restored on function enter / return. Instead,
699 these registers can be used to pass / return data (such as
700 global / local context pointers) across function calls. The
701 value of n must be between 1 and 3. Furthermore, this option
702 is only supported by register argument calling convention, so
703 SLJIT_ENTER_REG_ARG (see below) must be specified as well. */
704 #define SLJIT_ENTER_KEEP(n) (n)
706 /* The compiled function uses an sljit specific register argument
707 * calling convention. This is a lightweight function call type where
708 * both the caller and called function must be compiled with sljit.
709 * The jump type of the function call must be SLJIT_CALL_REG_ARG
710 * and the called function must store all arguments in registers. */
711 #define SLJIT_ENTER_REG_ARG 0x00000004
713 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
714 #define SLJIT_MAX_LOCAL_SIZE 65536
716 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
717 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
718 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
);
720 /* The machine code has a context (which contains the local stack space size,
721 number of used registers, etc.) which initialized by sljit_emit_enter. Several
722 functions (such as sljit_emit_return) requres this context to be able to generate
723 the appropriate code. However, some code fragments (like inline cache) may have
724 no normal entry point so their context is unknown for the compiler. Their context
725 can be provided to the compiler by the sljit_set_context function.
727 Note: every call of sljit_emit_enter and sljit_set_context overwrites
728 the previous context. */
730 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
731 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
732 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
);
734 /* Return from machine code. The sljit_emit_return_void function does not return with
735 any value. The sljit_emit_return function returns with a single value which stores
736 the result of a data move instruction. The instruction is specified by the op
737 argument, and must be between SLJIT_MOV and SLJIT_MOV_P (see sljit_emit_op1). */
739 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
);
741 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
,
742 sljit_s32 src
, sljit_sw srcw
);
744 /* Generating entry and exit points for fast call functions (see SLJIT_FAST_CALL).
745 Both sljit_emit_fast_enter and SLJIT_FAST_RETURN operations preserve the
746 values of all registers and stack frame. The return address is stored in the
747 dst argument of sljit_emit_fast_enter, and this return address can be passed
748 to SLJIT_FAST_RETURN to continue the execution after the fast call.
750 Fast calls are cheap operations (usually only a single call instruction is
751 emitted) but they do not preserve any registers. However the callee function
752 can freely use / update any registers and stack values which can be
753 efficiently exploited by various optimizations. Registers can be saved
754 manually by the callee function if needed.
756 Although returning to different address by SLJIT_FAST_RETURN is possible,
757 this address usually cannot be predicted by the return address predictor of
758 modern CPUs which may reduce performance. Furthermore certain security
759 enhancement technologies such as Intel Control-flow Enforcement Technology
760 (CET) may disallow returning to a different address.
762 Flags: - (does not modify flags). */
764 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
);
767 Source and destination operands for arithmetical instructions
768 imm - a simple immediate value (cannot be used as a destination)
769 reg - any of the registers (immediate argument must be 0)
770 [imm] - absolute immediate memory address
771 [reg+imm] - indirect memory address
772 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
773 useful for (byte, half, int, sljit_sw) array access
774 (fully supported by both x86 and ARM architectures, and cheap operation on others)
778 IMPORTANT NOTE: memory access MUST be naturally aligned unless
779 SLJIT_UNALIGNED macro is defined and its value is 1.
782 ---------+-----------
783 byte | 1 byte (any physical_address is accepted)
784 half | 2 byte (physical_address & 0x1 == 0)
785 int | 4 byte (physical_address & 0x3 == 0)
786 word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
787 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
788 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
789 | on 64 bit machines)
791 Note: Different architectures have different addressing limitations.
792 A single instruction is enough for the following addressing
793 modes. Other adrressing modes are emulated by instruction
794 sequences. This information could help to improve those code
795 generators which focuses only a few architectures.
797 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
798 [reg+(reg<<imm)] is supported
799 [imm], -2^32+1 <= imm <= 2^32-1 is supported
800 Write-back is not supported
801 arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
802 bytes, any halfs or floating point values)
803 [reg+(reg<<imm)] is supported
804 Write-back is supported
805 arm-t2: [reg+imm], -255 <= imm <= 4095
806 [reg+(reg<<imm)] is supported
807 Write back is supported only for [reg+imm], where -255 <= imm <= 255
808 arm64: [reg+imm], -256 <= imm <= 255, 0 <= aligned imm <= 4095 * alignment
809 [reg+(reg<<imm)] is supported
810 Write back is supported only for [reg+imm], where -256 <= imm <= 255
811 ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
812 signed load on 64 bit requires immediates divisible by 4.
813 [reg+imm] is not supported for signed 8 bit values.
814 [reg+reg] is supported
815 Write-back is supported except for one instruction: 32 bit signed
816 load with [reg+imm] addressing mode on 64 bit.
817 mips: [reg+imm], -65536 <= imm <= 65535
818 Write-back is not supported
819 riscv: [reg+imm], -2048 <= imm <= 2047
820 Write-back is not supported
821 s390x: [reg+imm], -2^19 <= imm < 2^19
822 [reg+reg] is supported
823 Write-back is not supported
826 /* Macros for specifying operand types. */
827 #define SLJIT_MEM 0x80
828 #define SLJIT_MEM0() (SLJIT_MEM)
829 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
830 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
831 #define SLJIT_IMM 0x40
833 /* Sets 32 bit operation mode on 64 bit CPUs. This option is ignored on
834 32 bit CPUs. When this option is set for an arithmetic operation, only
835 the lower 32 bit of the input registers are used, and the CPU status
836 flags are set according to the 32 bit result. Although the higher 32 bit
837 of the input and the result registers are not defined by SLJIT, it might
838 be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
839 requirements all source registers must be the result of those operations
840 where this option was also set. Memory loads read 32 bit values rather
841 than 64 bit ones. In other words 32 bit and 64 bit operations cannot be
842 mixed. The only exception is SLJIT_MOV32 whose source register can hold
843 any 32 or 64 bit value, and it is converted to a 32 bit compatible format
844 first. This conversion is free (no instructions are emitted) on most CPUs.
845 A 32 bit value can also be converted to a 64 bit value by SLJIT_MOV_S32
846 (sign extension) or SLJIT_MOV_U32 (zero extension).
848 As for floating-point operations, this option sets 32 bit single
849 precision mode. Similar to the integer operations, all register arguments
850 must be the result of those operations where this option was also set.
852 Note: memory addressing always uses 64 bit values on 64 bit systems so
853 the result of a 32 bit operation must not be used with SLJIT_MEMx
856 This option is part of the instruction name, so there is no need to
857 manually set it. E.g:
859 SLJIT_ADD32 == (SLJIT_ADD | SLJIT_32) */
860 #define SLJIT_32 0x100
862 /* Many CPUs (x86, ARM, PPC) have status flags which can be set according
863 to the result of an operation. Other CPUs (MIPS) do not have status
864 flags, and results must be stored in registers. To cover both architecture
865 types efficiently only two flags are defined by SLJIT:
867 * Zero (equal) flag: it is set if the result is zero
868 * Variable flag: its value is defined by the last arithmetic operation
870 SLJIT instructions can set any or both of these flags. The value of
871 these flags is undefined if the instruction does not specify their value.
872 The description of each instruction contains the list of allowed flag
875 Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
877 sljit_op2(..., SLJIT_ADD, ...)
878 Both the zero and variable flags are undefined so they can
879 have any value after the operation is completed.
881 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
882 Sets the zero flag if the result is zero, clears it otherwise.
883 The variable flag is undefined.
885 sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
886 Sets the variable flag if an integer overflow occurs, clears
887 it otherwise. The zero flag is undefined.
889 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
890 Sets the zero flag if the result is zero, clears it otherwise.
891 Sets the variable flag if unsigned overflow (carry) occurs,
894 If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are
897 Using these flags can reduce the number of emitted instructions. E.g. a
898 fast loop can be implemented by decreasing a counter register and set the
899 zero flag to jump back if the counter register has not reached zero.
901 Motivation: although CPUs can set a large number of flags, usually their
902 values are ignored or only one of them is used. Emulating a large number
903 of flags on systems without flag register is complicated so SLJIT
904 instructions must specify the flag they want to use and only that flag
905 will be emulated. The last arithmetic instruction can be repeated if
906 multiple flags need to be checked.
909 /* Set Zero status flag. */
910 #define SLJIT_SET_Z 0x0200
911 /* Set the variable status flag if condition is true.
912 See comparison types. */
913 #define SLJIT_SET(condition) ((condition) << 10)
916 - you cannot postpone conditional jump instructions except if noted that
917 the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
918 - flag combinations: '|' means 'logical or'. */
920 /* Starting index of opcodes for sljit_emit_op0. */
921 #define SLJIT_OP0_BASE 0
923 /* Flags: - (does not modify flags)
924 Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
925 It falls back to SLJIT_NOP in those cases. */
926 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
927 /* Flags: - (does not modify flags)
928 Note: may or may not cause an extra cycle wait
929 it can even decrease the runtime in a few cases. */
930 #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
931 /* Flags: - (may destroy flags)
932 Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
933 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
934 #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2)
935 /* Flags: - (may destroy flags)
936 Signed multiplication of SLJIT_R0 and SLJIT_R1.
937 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
938 #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3)
939 /* Flags: - (may destroy flags)
940 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
941 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
942 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
943 #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4)
944 #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_32)
945 /* Flags: - (may destroy flags)
946 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
947 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
948 Note: if SLJIT_R1 is 0, the behaviour is undefined.
949 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
950 the behaviour is undefined. */
951 #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5)
952 #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_32)
953 /* Flags: - (may destroy flags)
954 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
955 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
956 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
957 #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6)
958 #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_32)
959 /* Flags: - (may destroy flags)
960 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
961 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
962 Note: if SLJIT_R1 is 0, the behaviour is undefined.
963 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
964 the behaviour is undefined. */
965 #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7)
966 #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_32)
967 /* Flags: - (does not modify flags)
968 ENDBR32 instruction for x86-32 and ENDBR64 instruction for x86-64
969 when Intel Control-flow Enforcement Technology (CET) is enabled.
970 No instruction for other architectures. */
971 #define SLJIT_ENDBR (SLJIT_OP0_BASE + 8)
972 /* Flags: - (may destroy flags)
973 Skip stack frames before return. */
974 #define SLJIT_SKIP_FRAMES_BEFORE_RETURN (SLJIT_OP0_BASE + 9)
976 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
);
978 /* Starting index of opcodes for sljit_emit_op1. */
979 #define SLJIT_OP1_BASE 32
981 /* The MOV instruction transfers data from source to destination.
983 MOV instruction suffixes:
985 U8 - unsigned 8 bit data transfer
986 S8 - signed 8 bit data transfer
987 U16 - unsigned 16 bit data transfer
988 S16 - signed 16 bit data transfer
989 U32 - unsigned int (32 bit) data transfer
990 S32 - signed int (32 bit) data transfer
991 P - pointer (sljit_p) data transfer
994 /* Flags: - (does not modify flags) */
995 #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
996 /* Flags: - (does not modify flags) */
997 #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1)
998 #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_32)
999 /* Flags: - (does not modify flags) */
1000 #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2)
1001 #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_32)
1002 /* Flags: - (does not modify flags) */
1003 #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3)
1004 #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_32)
1005 /* Flags: - (does not modify flags) */
1006 #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4)
1007 #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_32)
1008 /* Flags: - (does not modify flags)
1009 Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
1010 #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5)
1011 /* Flags: - (does not modify flags)
1012 Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
1013 #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6)
1014 /* Flags: - (does not modify flags) */
1015 #define SLJIT_MOV32 (SLJIT_OP1_BASE + 7)
1016 /* Flags: - (does not modify flags)
1017 Note: load a pointer sized data, useful on x32 (a 32 bit mode on x86-64
1018 where all x64 features are available, e.g. 16 register) or similar
1020 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 8)
1022 Note: immediate source argument is not supported */
1023 #define SLJIT_NOT (SLJIT_OP1_BASE + 9)
1024 #define SLJIT_NOT32 (SLJIT_NOT | SLJIT_32)
1025 /* Count leading zeroes
1026 Flags: - (may destroy flags)
1027 Note: immediate source argument is not supported */
1028 #define SLJIT_CLZ (SLJIT_OP1_BASE + 10)
1029 #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_32)
1031 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1032 sljit_s32 dst
, sljit_sw dstw
,
1033 sljit_s32 src
, sljit_sw srcw
);
1035 /* Starting index of opcodes for sljit_emit_op2. */
1036 #define SLJIT_OP2_BASE 96
1038 /* Flags: Z | OVERFLOW | CARRY */
1039 #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
1040 #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_32)
1042 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
1043 #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_32)
1044 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
1045 SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
1046 SIG_LESS_EQUAL | CARRY */
1047 #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
1048 #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_32)
1050 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
1051 #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_32)
1052 /* Note: integer mul
1054 #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
1055 #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_32)
1057 #define SLJIT_AND (SLJIT_OP2_BASE + 5)
1058 #define SLJIT_AND32 (SLJIT_AND | SLJIT_32)
1060 #define SLJIT_OR (SLJIT_OP2_BASE + 6)
1061 #define SLJIT_OR32 (SLJIT_OR | SLJIT_32)
1063 #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
1064 #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_32)
1066 Let bit_length be the length of the shift operation: 32 or 64.
1067 If src2 is immediate, src2w is masked by (bit_length - 1).
1068 Otherwise, if the content of src2 is outside the range from 0
1069 to bit_length - 1, the result is undefined. */
1070 #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
1071 #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_32)
1073 Let bit_length be the length of the shift operation: 32 or 64.
1074 If src2 is immediate, src2w is masked by (bit_length - 1).
1075 Otherwise, if the content of src2 is outside the range from 0
1076 to bit_length - 1, the result is undefined. */
1077 #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
1078 #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_32)
1080 Let bit_length be the length of the shift operation: 32 or 64.
1081 If src2 is immediate, src2w is masked by (bit_length - 1).
1082 Otherwise, if the content of src2 is outside the range from 0
1083 to bit_length - 1, the result is undefined. */
1084 #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
1085 #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_32)
1087 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1088 sljit_s32 dst
, sljit_sw dstw
,
1089 sljit_s32 src1
, sljit_sw src1w
,
1090 sljit_s32 src2
, sljit_sw src2w
);
1092 /* The sljit_emit_op2u function is the same as sljit_emit_op2 except the result is discarded. */
1094 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
1095 sljit_s32 src1
, sljit_sw src1w
,
1096 sljit_s32 src2
, sljit_sw src2w
);
1098 /* Starting index of opcodes for sljit_emit_op2. */
1099 #define SLJIT_OP_SRC_BASE 128
1101 /* Note: src cannot be an immedate value
1102 Flags: - (does not modify flags) */
1103 #define SLJIT_FAST_RETURN (SLJIT_OP_SRC_BASE + 0)
1104 /* Skip stack frames before fast return.
1105 Note: src cannot be an immedate value
1106 Flags: may destroy flags. */
1107 #define SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN (SLJIT_OP_SRC_BASE + 1)
1108 /* Prefetch value into the level 1 data cache
1109 Note: if the target CPU does not support data prefetch,
1110 no instructions are emitted.
1111 Note: this instruction never fails, even if the memory address is invalid.
1112 Flags: - (does not modify flags) */
1113 #define SLJIT_PREFETCH_L1 (SLJIT_OP_SRC_BASE + 2)
1114 /* Prefetch value into the level 2 data cache
1115 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1116 does not support this instruction form.
1117 Note: this instruction never fails, even if the memory address is invalid.
1118 Flags: - (does not modify flags) */
1119 #define SLJIT_PREFETCH_L2 (SLJIT_OP_SRC_BASE + 3)
1120 /* Prefetch value into the level 3 data cache
1121 Note: same as SLJIT_PREFETCH_L2 if the target CPU
1122 does not support this instruction form.
1123 Note: this instruction never fails, even if the memory address is invalid.
1124 Flags: - (does not modify flags) */
1125 #define SLJIT_PREFETCH_L3 (SLJIT_OP_SRC_BASE + 4)
1126 /* Prefetch a value which is only used once (and can be discarded afterwards)
1127 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1128 does not support this instruction form.
1129 Note: this instruction never fails, even if the memory address is invalid.
1130 Flags: - (does not modify flags) */
1131 #define SLJIT_PREFETCH_ONCE (SLJIT_OP_SRC_BASE + 5)
1133 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1134 sljit_s32 src
, sljit_sw srcw
);
1136 /* Starting index of opcodes for sljit_emit_fop1. */
1137 #define SLJIT_FOP1_BASE 160
1139 /* Flags: - (does not modify flags) */
1140 #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0)
1141 #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_32)
1142 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
1143 SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
1144 Rounding mode when the destination is W or I: round towards zero. */
1145 /* Flags: - (may destroy flags) */
1146 #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1)
1147 #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_32)
1148 /* Flags: - (may destroy flags) */
1149 #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2)
1150 #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_32)
1151 /* Flags: - (may destroy flags) */
1152 #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3)
1153 #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_32)
1154 /* Flags: - (may destroy flags) */
1155 #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4)
1156 #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_32)
1157 /* Flags: - (may destroy flags) */
1158 #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5)
1159 #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_32)
1160 /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
1161 Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
1162 #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 6)
1163 #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_32)
1164 /* Flags: - (may destroy flags) */
1165 #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 7)
1166 #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_32)
1167 /* Flags: - (may destroy flags) */
1168 #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 8)
1169 #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_32)
1171 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1172 sljit_s32 dst
, sljit_sw dstw
,
1173 sljit_s32 src
, sljit_sw srcw
);
1175 /* Starting index of opcodes for sljit_emit_fop2. */
1176 #define SLJIT_FOP2_BASE 192
1178 /* Flags: - (may destroy flags) */
1179 #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0)
1180 #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_32)
1181 /* Flags: - (may destroy flags) */
1182 #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1)
1183 #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_32)
1184 /* Flags: - (may destroy flags) */
1185 #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2)
1186 #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_32)
1187 /* Flags: - (may destroy flags) */
1188 #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3)
1189 #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_32)
1191 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1192 sljit_s32 dst
, sljit_sw dstw
,
1193 sljit_s32 src1
, sljit_sw src1w
,
1194 sljit_s32 src2
, sljit_sw src2w
);
1196 /* Label and jump instructions. */
1198 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
);
1200 /* Invert (negate) conditional type: xor (^) with 0x1 */
1202 /* Integer comparison types. */
1203 #define SLJIT_EQUAL 0
1204 #define SLJIT_ZERO SLJIT_EQUAL
1205 #define SLJIT_NOT_EQUAL 1
1206 #define SLJIT_NOT_ZERO SLJIT_NOT_EQUAL
1208 #define SLJIT_LESS 2
1209 #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS)
1210 #define SLJIT_GREATER_EQUAL 3
1211 #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_GREATER_EQUAL)
1212 #define SLJIT_GREATER 4
1213 #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER)
1214 #define SLJIT_LESS_EQUAL 5
1215 #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_LESS_EQUAL)
1216 #define SLJIT_SIG_LESS 6
1217 #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS)
1218 #define SLJIT_SIG_GREATER_EQUAL 7
1219 #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SIG_GREATER_EQUAL)
1220 #define SLJIT_SIG_GREATER 8
1221 #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER)
1222 #define SLJIT_SIG_LESS_EQUAL 9
1223 #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_LESS_EQUAL)
1225 #define SLJIT_OVERFLOW 10
1226 #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW)
1227 #define SLJIT_NOT_OVERFLOW 11
1229 /* Unlike other flags, sljit_emit_jump may destroy this flag. */
1230 #define SLJIT_CARRY 12
1231 #define SLJIT_SET_CARRY SLJIT_SET(SLJIT_CARRY)
1232 #define SLJIT_NOT_CARRY 13
1234 /* Basic floating point comparison types.
1236 Note: when the comparison result is unordered, their behaviour is unspecified. */
1238 #define SLJIT_F_EQUAL 14
1239 #define SLJIT_SET_F_EQUAL SLJIT_SET(SLJIT_F_EQUAL)
1240 #define SLJIT_F_NOT_EQUAL 15
1241 #define SLJIT_SET_F_NOT_EQUAL SLJIT_SET(SLJIT_F_NOT_EQUAL)
1242 #define SLJIT_F_LESS 16
1243 #define SLJIT_SET_F_LESS SLJIT_SET(SLJIT_F_LESS)
1244 #define SLJIT_F_GREATER_EQUAL 17
1245 #define SLJIT_SET_F_GREATER_EQUAL SLJIT_SET(SLJIT_F_GREATER_EQUAL)
1246 #define SLJIT_F_GREATER 18
1247 #define SLJIT_SET_F_GREATER SLJIT_SET(SLJIT_F_GREATER)
1248 #define SLJIT_F_LESS_EQUAL 19
1249 #define SLJIT_SET_F_LESS_EQUAL SLJIT_SET(SLJIT_F_LESS_EQUAL)
1251 /* Jumps when either argument contains a NaN value. */
1252 #define SLJIT_UNORDERED 20
1253 #define SLJIT_SET_UNORDERED SLJIT_SET(SLJIT_UNORDERED)
1254 /* Jumps when neither argument contains a NaN value. */
1255 #define SLJIT_ORDERED 21
1256 #define SLJIT_SET_ORDERED SLJIT_SET(SLJIT_ORDERED)
1258 /* Ordered / unordered floating point comparison types.
1260 Note: each comparison type has an ordered and unordered form. Some
1261 architectures supports only either of them (see: sljit_cmp_info). */
1263 #define SLJIT_ORDERED_EQUAL 22
1264 #define SLJIT_SET_ORDERED_EQUAL SLJIT_SET(SLJIT_ORDERED_EQUAL)
1265 #define SLJIT_UNORDERED_OR_NOT_EQUAL 23
1266 #define SLJIT_SET_UNORDERED_OR_NOT_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_NOT_EQUAL)
1267 #define SLJIT_ORDERED_LESS 24
1268 #define SLJIT_SET_ORDERED_LESS SLJIT_SET(SLJIT_ORDERED_LESS)
1269 #define SLJIT_UNORDERED_OR_GREATER_EQUAL 25
1270 #define SLJIT_SET_UNORDERED_OR_GREATER_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_GREATER_EQUAL)
1271 #define SLJIT_ORDERED_GREATER 26
1272 #define SLJIT_SET_ORDERED_GREATER SLJIT_SET(SLJIT_ORDERED_GREATER)
1273 #define SLJIT_UNORDERED_OR_LESS_EQUAL 27
1274 #define SLJIT_SET_UNORDERED_OR_LESS_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_LESS_EQUAL)
1276 #define SLJIT_UNORDERED_OR_EQUAL 28
1277 #define SLJIT_SET_UNORDERED_OR_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_EQUAL)
1278 #define SLJIT_ORDERED_NOT_EQUAL 29
1279 #define SLJIT_SET_ORDERED_NOT_EQUAL SLJIT_SET(SLJIT_ORDERED_NOT_EQUAL)
1280 #define SLJIT_UNORDERED_OR_LESS 30
1281 #define SLJIT_SET_UNORDERED_OR_LESS SLJIT_SET(SLJIT_UNORDERED_OR_LESS)
1282 #define SLJIT_ORDERED_GREATER_EQUAL 31
1283 #define SLJIT_SET_ORDERED_GREATER_EQUAL SLJIT_SET(SLJIT_ORDERED_GREATER_EQUAL)
1284 #define SLJIT_UNORDERED_OR_GREATER 32
1285 #define SLJIT_SET_UNORDERED_OR_GREATER SLJIT_SET(SLJIT_UNORDERED_OR_GREATER)
1286 #define SLJIT_ORDERED_LESS_EQUAL 33
1287 #define SLJIT_SET_ORDERED_LESS_EQUAL SLJIT_SET(SLJIT_ORDERED_LESS_EQUAL)
1289 /* Unconditional jump types. */
1290 #define SLJIT_JUMP 34
1291 /* Fast calling method. See sljit_emit_fast_enter / SLJIT_FAST_RETURN. */
1292 #define SLJIT_FAST_CALL 35
1293 /* Default C calling convention. */
1294 #define SLJIT_CALL 36
1295 /* Called function must be an sljit compiled function.
1296 See SLJIT_ENTER_REG_ARG option. */
1297 #define SLJIT_CALL_REG_ARG 37
1299 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1300 #define SLJIT_REWRITABLE_JUMP 0x1000
1301 /* When this flag is passed, the execution of the current function ends and
1302 the called function returns to the caller of the current function. The
1303 stack usage is reduced before the call, but it is not necessarily reduced
1304 to zero. In the latter case the compiler needs to allocate space for some
1305 arguments and the return address must be stored on the stack as well. */
1306 #define SLJIT_CALL_RETURN 0x2000
1308 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1309 type must be between SLJIT_EQUAL and SLJIT_FAST_CALL
1310 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1312 Flags: does not modify flags. */
1313 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
);
1315 /* Emit a C compiler (ABI) compatible function call.
1316 type must be SLJIT_CALL or SLJIT_CALL_CDECL
1317 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP and SLJIT_CALL_RETURN
1318 arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1320 Flags: destroy all flags. */
1321 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 arg_types
);
1323 /* Basic arithmetic comparison. In most architectures it is implemented as
1324 an compare operation followed by a sljit_emit_jump. However some
1325 architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
1326 It is suggested to use this comparison form when appropriate.
1327 type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
1328 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1330 Flags: may destroy flags. */
1331 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_cmp(struct sljit_compiler
*compiler
, sljit_s32 type
,
1332 sljit_s32 src1
, sljit_sw src1w
,
1333 sljit_s32 src2
, sljit_sw src2w
);
1335 /* Basic floating point comparison. In most architectures it is implemented as
1336 an SLJIT_FCMP operation (setting appropriate flags) followed by a
1337 sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1338 special optimizations here. It is suggested to use this comparison form
1340 type must be between SLJIT_F_EQUAL and SLJIT_ORDERED_LESS_EQUAL
1341 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1342 Flags: destroy flags.
1343 Note: if either operand is NaN, the behaviour is undefined for
1344 types up to SLJIT_S_LESS_EQUAL. */
1345 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_fcmp(struct sljit_compiler
*compiler
, sljit_s32 type
,
1346 sljit_s32 src1
, sljit_sw src1w
,
1347 sljit_s32 src2
, sljit_sw src2w
);
1349 /* Set the destination of the jump to this label. */
1350 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_label(struct sljit_jump
*jump
, struct sljit_label
* label
);
1351 /* Set the destination address of the jump to this label. */
1352 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_target(struct sljit_jump
*jump
, sljit_uw target
);
1354 /* Emit an indirect jump or fast call.
1355 Direct form: set src to SLJIT_IMM() and srcw to the address
1356 Indirect form: any other valid addressing mode
1357 type must be between SLJIT_JUMP and SLJIT_FAST_CALL
1359 Flags: does not modify flags. */
1360 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
);
1362 /* Emit a C compiler (ABI) compatible function call.
1363 Direct form: set src to SLJIT_IMM() and srcw to the address
1364 Indirect form: any other valid addressing mode
1365 type must be SLJIT_CALL or SLJIT_CALL_CDECL
1366 type can be combined (or'ed) with SLJIT_CALL_RETURN
1367 arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1369 Flags: destroy all flags. */
1370 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
);
1372 /* Perform the operation using the conditional flags as the second argument.
1373 Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_LESS_EQUAL. The value
1374 represented by the type is 1, if the condition represented by the type
1375 is fulfilled, and 0 otherwise.
1377 If op == SLJIT_MOV, SLJIT_MOV32:
1378 Set dst to the value represented by the type (0 or 1).
1379 Flags: - (does not modify flags)
1380 If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1381 Performs the binary operation using dst as the first, and the value
1382 represented by type as the second argument. Result is written into dst.
1383 Flags: Z (may destroy flags) */
1384 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
1385 sljit_s32 dst
, sljit_sw dstw
,
1388 /* Emit a conditional mov instruction which moves source to destination,
1389 if the condition is satisfied. Unlike other arithmetic operations this
1390 instruction does not support memory access.
1392 type must be between SLJIT_EQUAL and SLJIT_ORDERED_LESS_EQUAL
1393 dst_reg must be a valid register and it can be combined
1394 with SLJIT_32 to perform a 32 bit arithmetic operation
1395 src must be register or immediate (SLJIT_IMM)
1397 Flags: - (does not modify flags) */
1398 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
1400 sljit_s32 src
, sljit_sw srcw
);
1402 /* The following flags are used by sljit_emit_mem() and sljit_emit_fmem(). */
1404 /* Memory load operation. This is the default. */
1405 #define SLJIT_MEM_LOAD 0x000000
1406 /* Memory store operation. */
1407 #define SLJIT_MEM_STORE 0x000200
1409 /* Load or stora data from an unaligned address. */
1410 #define SLJIT_MEM_UNALIGNED 0x000400
1411 /* Load or store data and update the base address with a single operation. */
1412 /* Base register is updated before the memory access. */
1413 #define SLJIT_MEM_PRE 0x000800
1414 /* Base register is updated after the memory access. */
1415 #define SLJIT_MEM_POST 0x001000
1417 /* The following flags are supported when SLJIT_MEM_UNALIGNED is specified: */
1419 /* Defines 16 bit alignment for unaligned accesses. */
1420 #define SLJIT_MEM_ALIGNED_16 0x010000
1421 /* Defines 32 bit alignment for unaligned accesses. */
1422 #define SLJIT_MEM_ALIGNED_32 0x020000
1424 /* The following flags are supported when SLJIT_MEM_PRE or
1425 SLJIT_MEM_POST is specified: */
1427 /* When SLJIT_MEM_SUPP is passed, no instructions are emitted.
1428 Instead the function returns with SLJIT_SUCCESS if the instruction
1429 form is supported and SLJIT_ERR_UNSUPPORTED otherwise. This flag
1430 allows runtime checking of available instruction forms. */
1431 #define SLJIT_MEM_SUPP 0x010000
1433 /* The sljit_emit_mem emits instructions for various memory operations:
1435 When SLJIT_MEM_UNALIGNED is set in type argument:
1436 Emit instructions for unaligned memory loads or stores. When
1437 SLJIT_UNALIGNED is not defined, the only way to access unaligned
1438 memory data is using sljit_emit_mem. Otherwise all operations (e.g.
1439 sljit_emit_op1/2, or sljit_emit_fop1/2) supports unaligned access.
1440 In general, the performance of unaligned memory accesses are often
1441 lower than aligned and should be avoided.
1443 When SLJIT_MEM_PRE or SLJIT_MEM_POST is set in type argument:
1444 Emit a single memory load or store with update instruction.
1445 When the requested instruction form is not supported by the CPU,
1446 it returns with SLJIT_ERR_UNSUPPORTED instead of emulating the
1447 instruction. This allows specializing tight loops based on
1448 the supported instruction forms (see SLJIT_MEM_SUPP flag).
1450 type must be between SLJIT_MOV and SLJIT_MOV_P and can be
1451 combined with SLJIT_MEM_* flags.
1452 reg is the source or destination register, and must be
1453 different from the base register of the mem operand
1454 when SLJIT_MEM_PRE or SLJIT_MEM_POST is passed
1455 mem must be a memory operand
1457 Flags: - (does not modify flags) */
1458 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1460 sljit_s32 mem
, sljit_sw memw
);
1462 /* Same as sljit_emit_mem except the followings:
1464 type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
1465 combined with SLJIT_MEM_* flags.
1466 freg is the source or destination floating point register
1467 mem must be a memory operand
1469 Flags: - (does not modify flags) */
1471 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fmem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1473 sljit_s32 mem
, sljit_sw memw
);
1475 /* Copies the base address of SLJIT_SP + offset to dst. The offset can be
1476 anything to negate the effect of relative addressing. For example if an
1477 array of sljit_sw values is stored on the stack from offset 0x40, and R0
1478 contains the offset of an array item plus 0x120, this item can be
1479 overwritten by two SLJIT instructions:
1481 sljit_get_local_base(compiler, SLJIT_R1, 0, 0x40 - 0x120);
1482 sljit_emit_op1(compiler, SLJIT_MOV, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_IMM, 0x5);
1484 Flags: - (may destroy flags) */
1485 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_local_base(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw offset
);
1487 /* Store a value that can be changed runtime (see: sljit_get_const_addr / sljit_set_const)
1488 Flags: - (does not modify flags) */
1489 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
);
1491 /* Store the value of a label (see: sljit_set_put_label)
1492 Flags: - (does not modify flags) */
1493 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
);
1495 /* Set the value stored by put_label to this label. */
1496 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_put_label(struct sljit_put_label
*put_label
, struct sljit_label
*label
);
1498 /* After the code generation the address for label, jump and const instructions
1499 are computed. Since these structures are freed by sljit_free_compiler, the
1500 addresses must be preserved by the user program elsewere. */
1501 static SLJIT_INLINE sljit_uw
sljit_get_label_addr(struct sljit_label
*label
) { return label
->addr
; }
1502 static SLJIT_INLINE sljit_uw
sljit_get_jump_addr(struct sljit_jump
*jump
) { return jump
->addr
; }
1503 static SLJIT_INLINE sljit_uw
sljit_get_const_addr(struct sljit_const
*const_
) { return const_
->addr
; }
1505 /* Only the address and executable offset are required to perform dynamic
1506 code modifications. See sljit_get_executable_offset function. */
1507 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
);
1508 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
);
1510 /* --------------------------------------------------------------------- */
1511 /* Miscellaneous utility functions */
1512 /* --------------------------------------------------------------------- */
1514 #define SLJIT_MAJOR_VERSION 0
1515 #define SLJIT_MINOR_VERSION 94
1517 /* Get the human readable name of the platform. Can be useful on platforms
1518 like ARM, where ARM and Thumb2 functions can be mixed, and
1519 it is useful to know the type of the code generator. */
1520 SLJIT_API_FUNC_ATTRIBUTE
const char* sljit_get_platform_name(void);
1522 /* Portable helper function to get an offset of a member. */
1523 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1525 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1527 /* The sljit_stack structure and its manipulation functions provides
1528 an implementation for a top-down stack. The stack top is stored
1529 in the end field of the sljit_stack structure and the stack goes
1530 down to the min_start field, so the memory region reserved for
1531 this stack is between min_start (inclusive) and end (exclusive)
1532 fields. However the application can only use the region between
1533 start (inclusive) and end (exclusive) fields. The sljit_stack_resize
1534 function can be used to extend this region up to min_start.
1536 This feature uses the "address space reserve" feature of modern
1537 operating systems. Instead of allocating a large memory block
1538 applications can allocate a small memory region and extend it
1539 later without moving the content of the memory area. Therefore
1540 after a successful resize by sljit_stack_resize all pointers into
1541 this region are still valid.
1544 this structure may not be supported by all operating systems.
1545 end and max_limit fields are aligned to PAGE_SIZE bytes (usually
1547 stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */
1549 struct sljit_stack
{
1550 /* User data, anything can be stored here.
1551 Initialized to the same value as the end field. */
1553 /* These members are read only. */
1554 /* End address of the stack */
1556 /* Current start address of the stack. */
1558 /* Lowest start address of the stack. */
1559 sljit_u8
*min_start
;
1562 /* Allocates a new stack. Returns NULL if unsuccessful.
1563 Note: see sljit_create_compiler for the explanation of allocator_data. */
1564 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_stack
* SLJIT_FUNC
sljit_allocate_stack(sljit_uw start_size
, sljit_uw max_size
, void *allocator_data
);
1565 SLJIT_API_FUNC_ATTRIBUTE
void SLJIT_FUNC
sljit_free_stack(struct sljit_stack
*stack
, void *allocator_data
);
1567 /* Can be used to increase (extend) or decrease (shrink) the stack
1568 memory area. Returns with new_start if successful and NULL otherwise.
1569 It always fails if new_start is less than min_start or greater or equal
1570 than end fields. The fields of the stack are not changed if the returned
1571 value is NULL (the current memory content is never lost). */
1572 SLJIT_API_FUNC_ATTRIBUTE sljit_u8
*SLJIT_FUNC
sljit_stack_resize(struct sljit_stack
*stack
, sljit_u8
*new_start
);
1574 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1576 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1578 /* Get the entry address of a given function (signed, unsigned result). */
1579 #define SLJIT_FUNC_ADDR(func_name) ((sljit_sw)func_name)
1580 #define SLJIT_FUNC_UADDR(func_name) ((sljit_uw)func_name)
1582 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1584 /* All JIT related code should be placed in the same context (library, binary, etc.). */
1586 /* Get the entry address of a given function (signed, unsigned result). */
1587 #define SLJIT_FUNC_ADDR(func_name) (*(sljit_sw*)(void*)func_name)
1588 #define SLJIT_FUNC_UADDR(func_name) (*(sljit_uw*)(void*)func_name)
1590 /* For powerpc64, the function pointers point to a context descriptor. */
1591 struct sljit_function_context
{
1597 /* Fill the context arguments using the addr and the function.
1598 If func_ptr is NULL, it will not be set to the address of context
1599 If addr is NULL, the function address also comes from the func pointer. */
1600 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_function_context(void** func_ptr
, struct sljit_function_context
* context
, sljit_uw addr
, void* func
);
1602 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1604 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
1605 /* Free unused executable memory. The allocator keeps some free memory
1606 around to reduce the number of OS executable memory allocations.
1607 This improves performance since these calls are costly. However
1608 it is sometimes desired to free all unused memory regions, e.g.
1609 before the application terminates. */
1610 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_unused_memory_exec(void);
1613 /* --------------------------------------------------------------------- */
1614 /* CPU specific functions */
1615 /* --------------------------------------------------------------------- */
1617 /* The following function is a helper function for sljit_emit_op_custom.
1618 It returns with the real machine register index ( >=0 ) of any SLJIT_R,
1619 SLJIT_S and SLJIT_SP registers.
1621 Note: it returns with -1 for virtual registers (only on x86-32). */
1623 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
);
1625 /* The following function is a helper function for sljit_emit_op_custom.
1626 It returns with the real machine register index of any SLJIT_FLOAT register.
1628 Note: the index is always an even number on ARM-32, MIPS. */
1630 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
);
1632 /* Any instruction can be inserted into the instruction stream by
1633 sljit_emit_op_custom. It has a similar purpose as inline assembly.
1634 The size parameter must match to the instruction size of the target
1637 x86: 0 < size <= 15. The instruction argument can be byte aligned.
1638 Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
1639 if size == 4, the instruction argument must be 4 byte aligned.
1640 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
1642 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1643 void *instruction
, sljit_u32 size
);
1645 /* Flags were set by a 32 bit operation. */
1646 #define SLJIT_CURRENT_FLAGS_32 SLJIT_32
1648 /* Flags were set by an ADD or ADDC operations. */
1649 #define SLJIT_CURRENT_FLAGS_ADD 0x01
1650 /* Flags were set by a SUB, SUBC, or NEG operation. */
1651 #define SLJIT_CURRENT_FLAGS_SUB 0x02
1653 /* Flags were set by sljit_emit_op2u with SLJIT_SUB opcode.
1654 Must be combined with SLJIT_CURRENT_FLAGS_SUB. */
1655 #define SLJIT_CURRENT_FLAGS_COMPARE 0x04
1657 /* Define the currently available CPU status flags. It is usually used after
1658 an sljit_emit_label or sljit_emit_op_custom operations to define which CPU
1659 status flags are available.
1661 The current_flags must be a valid combination of SLJIT_SET_* and
1662 SLJIT_CURRENT_FLAGS_* constants. */
1664 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_current_flags(struct sljit_compiler
*compiler
,
1665 sljit_s32 current_flags
);
1671 #endif /* SLJIT_LIR_H_ */