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_enter, the first integer argument is available in
260 SLJIT_R0, the second one in SLJIT_R1, and so on. Similarly the first
261 floating point argument is available in SLJIT_FR0, the second one in
262 SLJIT_FR1, and so on.
264 As for sljit_emit_call and sljit_emit_icall, the first integer argument
265 must be placed into SLJIT_R0, the second one into SLJIT_R1, and so on.
266 Similarly the first floating point argument must be placed into SLJIT_FR0,
267 the second one into SLJIT_FR1, and so on.
269 Example function definition:
270 sljit_f32 SLJIT_FUNC example_c_callback(void *arg_a,
271 sljit_f64 arg_b, sljit_u32 arg_c, sljit_f32 arg_d);
273 Argument type definition:
274 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_F32)
275 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_P, 1) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F64, 2)
276 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_32, 3) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 4)
278 Short form of argument type definition:
279 SLJIT_ARGS4(F32, P, F64, 32, F32)
282 arg_a must be placed in SLJIT_R0
283 arg_c must be placed in SLJIT_R1
284 arg_b must be placed in SLJIT_FR0
285 arg_d must be placed in SLJIT_FR1
288 /* Void result, can only be used by SLJIT_ARG_RETURN. */
289 #define SLJIT_ARG_TYPE_VOID 0
290 /* Machine word sized integer argument or result. */
291 #define SLJIT_ARG_TYPE_W 1
292 /* 32 bit integer argument or result. */
293 #define SLJIT_ARG_TYPE_32 2
294 /* Pointer sized integer argument or result. */
295 #define SLJIT_ARG_TYPE_P 3
296 /* 64 bit floating point argument or result. */
297 #define SLJIT_ARG_TYPE_F64 4
298 /* 32 bit floating point argument or result. */
299 #define SLJIT_ARG_TYPE_F32 5
301 #define SLJIT_ARG_SHIFT 4
302 #define SLJIT_ARG_RETURN(type) (type)
303 #define SLJIT_ARG_VALUE(type, idx) ((type) << ((idx) * SLJIT_ARG_SHIFT))
305 /* Simplified argument list definitions.
307 The following definition:
308 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_SW) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 1)
314 #define SLJIT_ARG_TO_TYPE(type) SLJIT_ARG_TYPE_ ## type
316 #define SLJIT_ARGS0(ret) \
317 SLJIT_ARG_RETURN(SLJIT_ARG_TO_TYPE(ret))
319 #define SLJIT_ARGS1(ret, arg1) \
320 (SLJIT_ARGS0(ret) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg1), 1))
322 #define SLJIT_ARGS2(ret, arg1, arg2) \
323 (SLJIT_ARGS1(ret, arg1) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg2), 2))
325 #define SLJIT_ARGS3(ret, arg1, arg2, arg3) \
326 (SLJIT_ARGS2(ret, arg1, arg2) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg3), 3))
328 #define SLJIT_ARGS4(ret, arg1, arg2, arg3, arg4) \
329 (SLJIT_ARGS3(ret, arg1, arg2, arg3) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg4), 4))
331 /* --------------------------------------------------------------------- */
332 /* Main structures and functions */
333 /* --------------------------------------------------------------------- */
336 The following structures are private, and can be changed in the
337 future. Keeping them here allows code inlining.
340 struct sljit_memory_fragment
{
341 struct sljit_memory_fragment
*next
;
343 /* Must be aligned to sljit_sw. */
348 struct sljit_label
*next
;
350 /* The maximum size difference. */
355 struct sljit_jump
*next
;
360 struct sljit_label
*label
;
364 struct sljit_put_label
{
365 struct sljit_put_label
*next
;
366 struct sljit_label
*label
;
372 struct sljit_const
*next
;
376 struct sljit_compiler
{
380 struct sljit_label
*labels
;
381 struct sljit_jump
*jumps
;
382 struct sljit_put_label
*put_labels
;
383 struct sljit_const
*consts
;
384 struct sljit_label
*last_label
;
385 struct sljit_jump
*last_jump
;
386 struct sljit_const
*last_const
;
387 struct sljit_put_label
*last_put_label
;
389 void *allocator_data
;
390 void *exec_allocator_data
;
391 struct sljit_memory_fragment
*buf
;
392 struct sljit_memory_fragment
*abuf
;
394 /* Used scratch registers. */
396 /* Used saved registers. */
398 /* Used float scratch registers. */
399 sljit_s32 fscratches
;
400 /* Used float saved registers. */
402 /* Local stack size. */
403 sljit_s32 local_size
;
406 /* Relative offset of the executable mapping from the writable mapping. */
407 sljit_sw executable_offset
;
408 /* Executable size for statistical purposes. */
409 sljit_uw executable_size
;
411 #if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
412 sljit_s32 status_flags_state
;
415 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
417 sljit_s32 locals_offset
;
418 sljit_s32 saveds_offset
;
419 sljit_s32 stack_tmp_size
;
422 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
425 sljit_s32 locals_offset
;
429 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
430 /* Constant pool handling. */
432 sljit_u8
*cpool_unique
;
436 /* Contains pointer, "ldr pc, [...]" pairs. */
440 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
441 /* Temporary fields. */
443 #endif /* SLJIT_CONFIG_ARM_V5 || SLJIT_CONFIG_ARM_V7 */
445 #if (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)
449 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
453 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
454 sljit_s32 delay_slot
;
459 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
463 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
464 sljit_s32 delay_slot
;
469 #if (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
470 /* Need to allocate register save area to make calls. */
474 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
478 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
479 || (defined SLJIT_DEBUG && SLJIT_DEBUG)
480 /* Flags specified by the last arithmetic instruction.
481 It contains the type of the variable flag. */
482 sljit_s32 last_flags
;
483 /* Return value type set by entry functions. */
484 sljit_s32 last_return
;
485 /* Local size passed to entry functions. */
486 sljit_s32 logical_local_size
;
489 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
490 || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
491 || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
492 /* Trust arguments when the API function is called. */
493 sljit_s32 skip_checks
;
497 /* --------------------------------------------------------------------- */
499 /* --------------------------------------------------------------------- */
501 /* Creates an sljit compiler. The allocator_data is required by some
502 custom memory managers. This pointer is passed to SLJIT_MALLOC
503 and SLJIT_FREE macros. Most allocators (including the default
504 one) ignores this value, and it is recommended to pass NULL
505 as a dummy value for allocator_data. The exec_allocator_data
506 has the same purpose but this one is passed to SLJIT_MALLOC_EXEC /
507 SLJIT_MALLOC_FREE functions.
509 Returns NULL if failed. */
510 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_compiler
* sljit_create_compiler(void *allocator_data
, void *exec_allocator_data
);
512 /* Frees everything except the compiled machine code. */
513 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_compiler(struct sljit_compiler
*compiler
);
515 /* Returns the current error code. If an error is occurred, future sljit
516 calls which uses the same compiler argument returns early with the same
517 error code. Thus there is no need for checking the error after every
518 call, it is enough to do it before the code is compiled. Removing
519 these checks increases the performance of the compiling process. */
520 static SLJIT_INLINE sljit_s32
sljit_get_compiler_error(struct sljit_compiler
*compiler
) { return compiler
->error
; }
522 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
523 if an error was detected before. After the error code is set
524 the compiler behaves as if the allocation failure happened
525 during an sljit function call. This can greatly simplify error
526 checking, since only the compiler status needs to be checked
527 after the compilation. */
528 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_compiler_memory_error(struct sljit_compiler
*compiler
);
531 Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
532 and <= 128 bytes on 64 bit architectures. The memory area is owned by the
533 compiler, and freed by sljit_free_compiler. The returned pointer is
534 sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
535 the compiling, and no need to worry about freeing them. The size is
536 enough to contain at most 16 pointers. If the size is outside of the range,
537 the function will return with NULL. However, this return value does not
538 indicate that there is no more memory (does not set the current error code
539 of the compiler to out-of-memory status).
541 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_alloc_memory(struct sljit_compiler
*compiler
, sljit_s32 size
);
543 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
544 /* Passing NULL disables verbose. */
545 SLJIT_API_FUNC_ATTRIBUTE
void sljit_compiler_verbose(struct sljit_compiler
*compiler
, FILE* verbose
);
549 Create executable code from the sljit instruction stream. This is the final step
550 of the code generation so no more instructions can be added after this call.
553 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
);
555 /* Free executable code. */
557 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_code(void* code
, void *exec_allocator_data
);
560 When the protected executable allocator is used the JIT code is mapped
561 twice. The first mapping has read/write and the second mapping has read/exec
562 permissions. This function returns with the relative offset of the executable
563 mapping using the writable mapping as the base after the machine code is
564 successfully generated. The returned value is always 0 for the normal executable
565 allocator, since it uses only one mapping with read/write/exec permissions.
566 Dynamic code modifications requires this value.
568 Before a successful code generation, this function returns with 0.
570 static SLJIT_INLINE sljit_sw
sljit_get_executable_offset(struct sljit_compiler
*compiler
) { return compiler
->executable_offset
; }
573 The executable memory consumption of the generated code can be retrieved by
574 this function. The returned value can be used for statistical purposes.
576 Before a successful code generation, this function returns with 0.
578 static SLJIT_INLINE sljit_uw
sljit_get_generated_code_size(struct sljit_compiler
*compiler
) { return compiler
->executable_size
; }
580 /* Returns with non-zero if the feature or limitation type passed as its
581 argument is present on the current CPU.
583 Some features (e.g. floating point operations) require hardware (CPU)
584 support while others (e.g. move with update) are emulated if not available.
585 However even if a feature is emulated, specialized code paths can be faster
586 than the emulation. Some limitations are emulated as well so their general
587 case is supported but it has extra performance costs. */
589 /* [Not emulated] Floating-point support is available. */
590 #define SLJIT_HAS_FPU 0
591 /* [Limitation] Some registers are virtual registers. */
592 #define SLJIT_HAS_VIRTUAL_REGISTERS 1
593 /* [Emulated] Has zero register (setting a memory location to zero is efficient). */
594 #define SLJIT_HAS_ZERO_REGISTER 2
595 /* [Emulated] Count leading zero is supported. */
596 #define SLJIT_HAS_CLZ 3
597 /* [Emulated] Conditional move is supported. */
598 #define SLJIT_HAS_CMOV 4
599 /* [Emulated] Conditional move is supported. */
600 #define SLJIT_HAS_PREFETCH 5
602 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
603 /* [Not emulated] SSE2 support is available on x86. */
604 #define SLJIT_HAS_SSE2 100
607 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
);
609 /* Instruction generation. Returns with any error code. If there is no
610 error, they return with SLJIT_SUCCESS. */
613 The executable code is a function from the viewpoint of the C
614 language. The function calls must obey to the ABI (Application
615 Binary Interface) of the platform, which specify the purpose of
616 machine registers and stack handling among other things. The
617 sljit_emit_enter function emits the necessary instructions for
618 setting up a new context for the executable code and moves function
619 arguments to the saved registers. Furthermore the options argument
620 can be used to pass configuration options to the compiler. The
621 available options are listed before sljit_emit_enter.
623 The function argument list is the combination of SLJIT_ARGx
624 (SLJIT_DEF_ARG1) macros. Currently maximum 4 arguments are
625 supported. The first integer argument is loaded into SLJIT_S0,
626 the second one is loaded into SLJIT_S1, and so on. Similarly,
627 the first floating point argument is loaded into SLJIT_FR0,
628 the second one is loaded into SLJIT_FR1, and so on. Furthermore
629 the register set used by the function must be declared as well.
630 The number of scratch and saved registers used by the function
631 must be passed to sljit_emit_enter. Only R registers between R0
632 and "scratches" argument can be used later. E.g. if "scratches"
633 is set to 2, the scratch register set will be limited to SLJIT_R0
634 and SLJIT_R1. The S registers and the floating point registers
635 ("fscratches" and "fsaveds") are specified in a similar manner.
636 The sljit_emit_enter is also capable of allocating a stack space
637 for local variables. The "local_size" argument contains the size
638 in bytes of this local area and its staring address is stored
639 in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
640 SLJIT_SP + local_size (exclusive) can be modified freely until
641 the function returns. The stack space is not initialized.
643 Note: the following conditions must met:
644 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
645 0 <= saveds <= SLJIT_NUMBER_OF_SAVED_REGISTERS
646 scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
647 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
648 0 <= fsaveds <= SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
649 fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
651 Note: the compiler can use saved registers as scratch registers,
652 but the opposite is not supported
654 Note: every call of sljit_emit_enter and sljit_set_context
655 overwrites the previous context.
658 /* The absolute address returned by sljit_get_local_base with
659 offset 0 is aligned to sljit_f64. Otherwise it is aligned to sljit_sw. */
660 #define SLJIT_F64_ALIGNMENT 0x00000001
662 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
663 #define SLJIT_MAX_LOCAL_SIZE 65536
665 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
666 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
667 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
);
669 /* The machine code has a context (which contains the local stack space size,
670 number of used registers, etc.) which initialized by sljit_emit_enter. Several
671 functions (such as sljit_emit_return) requres this context to be able to generate
672 the appropriate code. However, some code fragments (like inline cache) may have
673 no normal entry point so their context is unknown for the compiler. Their context
674 can be provided to the compiler by the sljit_set_context function.
676 Note: every call of sljit_emit_enter and sljit_set_context overwrites
677 the previous context. */
679 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
680 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
681 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
);
683 /* Return from machine code. The sljit_emit_return_void function does not return with
684 any value. The sljit_emit_return function returns with a single value which stores
685 the result of a data move instruction. The instruction is specified by the op
686 argument, and must be between SLJIT_MOV and SLJIT_MOV_P (see sljit_emit_op1). */
688 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return_void(struct sljit_compiler
*compiler
);
690 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
,
691 sljit_s32 src
, sljit_sw srcw
);
693 /* Generating entry and exit points for fast call functions (see SLJIT_FAST_CALL).
694 Both sljit_emit_fast_enter and SLJIT_FAST_RETURN operations preserve the
695 values of all registers and stack frame. The return address is stored in the
696 dst argument of sljit_emit_fast_enter, and this return address can be passed
697 to SLJIT_FAST_RETURN to continue the execution after the fast call.
699 Fast calls are cheap operations (usually only a single call instruction is
700 emitted) but they do not preserve any registers. However the callee function
701 can freely use / update any registers and stack values which can be
702 efficiently exploited by various optimizations. Registers can be saved
703 manually by the callee function if needed.
705 Although returning to different address by SLJIT_FAST_RETURN is possible,
706 this address usually cannot be predicted by the return address predictor of
707 modern CPUs which may reduce performance. Furthermore certain security
708 enhancement technologies such as Intel Control-flow Enforcement Technology
709 (CET) may disallow returning to a different address.
711 Flags: - (does not modify flags). */
713 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
);
716 Source and destination operands for arithmetical instructions
717 imm - a simple immediate value (cannot be used as a destination)
718 reg - any of the registers (immediate argument must be 0)
719 [imm] - absolute immediate memory address
720 [reg+imm] - indirect memory address
721 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
722 useful for (byte, half, int, sljit_sw) array access
723 (fully supported by both x86 and ARM architectures, and cheap operation on others)
727 IMPORTANT NOTE: memory access MUST be naturally aligned unless
728 SLJIT_UNALIGNED macro is defined and its value is 1.
731 ---------+-----------
732 byte | 1 byte (any physical_address is accepted)
733 half | 2 byte (physical_address & 0x1 == 0)
734 int | 4 byte (physical_address & 0x3 == 0)
735 word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
736 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
737 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
738 | on 64 bit machines)
740 Note: Different architectures have different addressing limitations.
741 A single instruction is enough for the following addressing
742 modes. Other adrressing modes are emulated by instruction
743 sequences. This information could help to improve those code
744 generators which focuses only a few architectures.
746 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
747 [reg+(reg<<imm)] is supported
748 [imm], -2^32+1 <= imm <= 2^32-1 is supported
749 Write-back is not supported
750 arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
751 bytes, any halfs or floating point values)
752 [reg+(reg<<imm)] is supported
753 Write-back is supported
754 arm-t2: [reg+imm], -255 <= imm <= 4095
755 [reg+(reg<<imm)] is supported
756 Write back is supported only for [reg+imm], where -255 <= imm <= 255
757 arm64: [reg+imm], -256 <= imm <= 255, 0 <= aligned imm <= 4095 * alignment
758 [reg+(reg<<imm)] is supported
759 Write back is supported only for [reg+imm], where -256 <= imm <= 255
760 ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
761 signed load on 64 bit requires immediates divisible by 4.
762 [reg+imm] is not supported for signed 8 bit values.
763 [reg+reg] is supported
764 Write-back is supported except for one instruction: 32 bit signed
765 load with [reg+imm] addressing mode on 64 bit.
766 mips: [reg+imm], -65536 <= imm <= 65535
767 sparc: [reg+imm], -4096 <= imm <= 4095
768 [reg+reg] is supported
769 s390x: [reg+imm], -2^19 <= imm < 2^19
770 [reg+reg] is supported
771 Write-back is not supported
774 /* Macros for specifying operand types. */
775 #define SLJIT_MEM 0x80
776 #define SLJIT_MEM0() (SLJIT_MEM)
777 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
778 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
779 #define SLJIT_IMM 0x40
781 /* Sets 32 bit operation mode on 64 bit CPUs. This option is ignored on
782 32 bit CPUs. When this option is set for an arithmetic operation, only
783 the lower 32 bit of the input registers are used, and the CPU status
784 flags are set according to the 32 bit result. Although the higher 32 bit
785 of the input and the result registers are not defined by SLJIT, it might
786 be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
787 requirements all source registers must be the result of those operations
788 where this option was also set. Memory loads read 32 bit values rather
789 than 64 bit ones. In other words 32 bit and 64 bit operations cannot be
790 mixed. The only exception is SLJIT_MOV32 whose source register can hold
791 any 32 or 64 bit value, and it is converted to a 32 bit compatible format
792 first. This conversion is free (no instructions are emitted) on most CPUs.
793 A 32 bit value can also be converted to a 64 bit value by SLJIT_MOV_S32
794 (sign extension) or SLJIT_MOV_U32 (zero extension).
796 As for floating-point operations, this option sets 32 bit single
797 precision mode. Similar to the integer operations, all register arguments
798 must be the result of those operations where this option was also set.
800 Note: memory addressing always uses 64 bit values on 64 bit systems so
801 the result of a 32 bit operation must not be used with SLJIT_MEMx
804 This option is part of the instruction name, so there is no need to
805 manually set it. E.g:
807 SLJIT_ADD32 == (SLJIT_ADD | SLJIT_32) */
808 #define SLJIT_32 0x100
810 /* Many CPUs (x86, ARM, PPC) have status flags which can be set according
811 to the result of an operation. Other CPUs (MIPS) do not have status
812 flags, and results must be stored in registers. To cover both architecture
813 types efficiently only two flags are defined by SLJIT:
815 * Zero (equal) flag: it is set if the result is zero
816 * Variable flag: its value is defined by the last arithmetic operation
818 SLJIT instructions can set any or both of these flags. The value of
819 these flags is undefined if the instruction does not specify their value.
820 The description of each instruction contains the list of allowed flag
823 Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
825 sljit_op2(..., SLJIT_ADD, ...)
826 Both the zero and variable flags are undefined so they can
827 have any value after the operation is completed.
829 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
830 Sets the zero flag if the result is zero, clears it otherwise.
831 The variable flag is undefined.
833 sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
834 Sets the variable flag if an integer overflow occurs, clears
835 it otherwise. The zero flag is undefined.
837 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
838 Sets the zero flag if the result is zero, clears it otherwise.
839 Sets the variable flag if unsigned overflow (carry) occurs,
842 If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are
845 Using these flags can reduce the number of emitted instructions. E.g. a
846 fast loop can be implemented by decreasing a counter register and set the
847 zero flag to jump back if the counter register has not reached zero.
849 Motivation: although CPUs can set a large number of flags, usually their
850 values are ignored or only one of them is used. Emulating a large number
851 of flags on systems without flag register is complicated so SLJIT
852 instructions must specify the flag they want to use and only that flag
853 will be emulated. The last arithmetic instruction can be repeated if
854 multiple flags need to be checked.
857 /* Set Zero status flag. */
858 #define SLJIT_SET_Z 0x0200
859 /* Set the variable status flag if condition is true.
860 See comparison types. */
861 #define SLJIT_SET(condition) ((condition) << 10)
864 - you cannot postpone conditional jump instructions except if noted that
865 the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
866 - flag combinations: '|' means 'logical or'. */
868 /* Starting index of opcodes for sljit_emit_op0. */
869 #define SLJIT_OP0_BASE 0
871 /* Flags: - (does not modify flags)
872 Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
873 It falls back to SLJIT_NOP in those cases. */
874 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
875 /* Flags: - (does not modify flags)
876 Note: may or may not cause an extra cycle wait
877 it can even decrease the runtime in a few cases. */
878 #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
879 /* Flags: - (may destroy flags)
880 Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
881 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
882 #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2)
883 /* Flags: - (may destroy flags)
884 Signed multiplication of SLJIT_R0 and SLJIT_R1.
885 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
886 #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3)
887 /* Flags: - (may destroy flags)
888 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
889 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
890 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
891 #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4)
892 #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_32)
893 /* Flags: - (may destroy flags)
894 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
895 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
896 Note: if SLJIT_R1 is 0, the behaviour is undefined.
897 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
898 the behaviour is undefined. */
899 #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5)
900 #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_32)
901 /* Flags: - (may destroy flags)
902 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
903 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
904 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
905 #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6)
906 #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_32)
907 /* Flags: - (may destroy flags)
908 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
909 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
910 Note: if SLJIT_R1 is 0, the behaviour is undefined.
911 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
912 the behaviour is undefined. */
913 #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7)
914 #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_32)
915 /* Flags: - (does not modify flags)
916 ENDBR32 instruction for x86-32 and ENDBR64 instruction for x86-64
917 when Intel Control-flow Enforcement Technology (CET) is enabled.
918 No instruction for other architectures. */
919 #define SLJIT_ENDBR (SLJIT_OP0_BASE + 8)
920 /* Flags: - (may destroy flags)
921 Skip stack frames before return. */
922 #define SLJIT_SKIP_FRAMES_BEFORE_RETURN (SLJIT_OP0_BASE + 9)
924 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
);
926 /* Starting index of opcodes for sljit_emit_op1. */
927 #define SLJIT_OP1_BASE 32
929 /* The MOV instruction transfers data from source to destination.
931 MOV instruction suffixes:
933 U8 - unsigned 8 bit data transfer
934 S8 - signed 8 bit data transfer
935 U16 - unsigned 16 bit data transfer
936 S16 - signed 16 bit data transfer
937 U32 - unsigned int (32 bit) data transfer
938 S32 - signed int (32 bit) data transfer
939 P - pointer (sljit_p) data transfer
942 /* Flags: - (does not modify flags) */
943 #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
944 /* Flags: - (does not modify flags) */
945 #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1)
946 #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_32)
947 /* Flags: - (does not modify flags) */
948 #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2)
949 #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_32)
950 /* Flags: - (does not modify flags) */
951 #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3)
952 #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_32)
953 /* Flags: - (does not modify flags) */
954 #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4)
955 #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_32)
956 /* Flags: - (does not modify flags)
957 Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
958 #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5)
959 /* Flags: - (does not modify flags)
960 Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
961 #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6)
962 /* Flags: - (does not modify flags) */
963 #define SLJIT_MOV32 (SLJIT_OP1_BASE + 7)
964 /* Flags: - (does not modify flags)
965 Note: load a pointer sized data, useful on x32 (a 32 bit mode on x86-64
966 where all x64 features are available, e.g. 16 register) or similar
968 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 8)
970 Note: immediate source argument is not supported */
971 #define SLJIT_NOT (SLJIT_OP1_BASE + 9)
972 #define SLJIT_NOT32 (SLJIT_NOT | SLJIT_32)
973 /* Flags: Z | OVERFLOW
974 Note: immediate source argument is not supported */
975 #define SLJIT_NEG (SLJIT_OP1_BASE + 10)
976 #define SLJIT_NEG32 (SLJIT_NEG | SLJIT_32)
977 /* Count leading zeroes
978 Flags: - (may destroy flags)
979 Note: immediate source argument is not supported */
980 #define SLJIT_CLZ (SLJIT_OP1_BASE + 11)
981 #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_32)
983 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
984 sljit_s32 dst
, sljit_sw dstw
,
985 sljit_s32 src
, sljit_sw srcw
);
987 /* Starting index of opcodes for sljit_emit_op2. */
988 #define SLJIT_OP2_BASE 96
990 /* Flags: Z | OVERFLOW | CARRY */
991 #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
992 #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_32)
994 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
995 #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_32)
996 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
997 SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
998 SIG_LESS_EQUAL | CARRY */
999 #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
1000 #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_32)
1002 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
1003 #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_32)
1004 /* Note: integer mul
1006 #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
1007 #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_32)
1009 #define SLJIT_AND (SLJIT_OP2_BASE + 5)
1010 #define SLJIT_AND32 (SLJIT_AND | SLJIT_32)
1012 #define SLJIT_OR (SLJIT_OP2_BASE + 6)
1013 #define SLJIT_OR32 (SLJIT_OR | SLJIT_32)
1015 #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
1016 #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_32)
1018 Let bit_length be the length of the shift operation: 32 or 64.
1019 If src2 is immediate, src2w is masked by (bit_length - 1).
1020 Otherwise, if the content of src2 is outside the range from 0
1021 to bit_length - 1, the result is undefined. */
1022 #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
1023 #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_32)
1025 Let bit_length be the length of the shift operation: 32 or 64.
1026 If src2 is immediate, src2w is masked by (bit_length - 1).
1027 Otherwise, if the content of src2 is outside the range from 0
1028 to bit_length - 1, the result is undefined. */
1029 #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
1030 #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_32)
1032 Let bit_length be the length of the shift operation: 32 or 64.
1033 If src2 is immediate, src2w is masked by (bit_length - 1).
1034 Otherwise, if the content of src2 is outside the range from 0
1035 to bit_length - 1, the result is undefined. */
1036 #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
1037 #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_32)
1039 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1040 sljit_s32 dst
, sljit_sw dstw
,
1041 sljit_s32 src1
, sljit_sw src1w
,
1042 sljit_s32 src2
, sljit_sw src2w
);
1044 /* The sljit_emit_op2u function is the same as sljit_emit_op2 except the result is discarded. */
1046 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2u(struct sljit_compiler
*compiler
, sljit_s32 op
,
1047 sljit_s32 src1
, sljit_sw src1w
,
1048 sljit_s32 src2
, sljit_sw src2w
);
1050 /* Starting index of opcodes for sljit_emit_op2. */
1051 #define SLJIT_OP_SRC_BASE 128
1053 /* Note: src cannot be an immedate value
1054 Flags: - (does not modify flags) */
1055 #define SLJIT_FAST_RETURN (SLJIT_OP_SRC_BASE + 0)
1056 /* Skip stack frames before fast return.
1057 Note: src cannot be an immedate value
1058 Flags: may destroy flags. */
1059 #define SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN (SLJIT_OP_SRC_BASE + 1)
1060 /* Prefetch value into the level 1 data cache
1061 Note: if the target CPU does not support data prefetch,
1062 no instructions are emitted.
1063 Note: this instruction never fails, even if the memory address is invalid.
1064 Flags: - (does not modify flags) */
1065 #define SLJIT_PREFETCH_L1 (SLJIT_OP_SRC_BASE + 2)
1066 /* Prefetch value into the level 2 data cache
1067 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1068 does not support this instruction form.
1069 Note: this instruction never fails, even if the memory address is invalid.
1070 Flags: - (does not modify flags) */
1071 #define SLJIT_PREFETCH_L2 (SLJIT_OP_SRC_BASE + 3)
1072 /* Prefetch value into the level 3 data cache
1073 Note: same as SLJIT_PREFETCH_L2 if the target CPU
1074 does not support this instruction form.
1075 Note: this instruction never fails, even if the memory address is invalid.
1076 Flags: - (does not modify flags) */
1077 #define SLJIT_PREFETCH_L3 (SLJIT_OP_SRC_BASE + 4)
1078 /* Prefetch a value which is only used once (and can be discarded afterwards)
1079 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1080 does not support this instruction form.
1081 Note: this instruction never fails, even if the memory address is invalid.
1082 Flags: - (does not modify flags) */
1083 #define SLJIT_PREFETCH_ONCE (SLJIT_OP_SRC_BASE + 5)
1085 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
1086 sljit_s32 src
, sljit_sw srcw
);
1088 /* Starting index of opcodes for sljit_emit_fop1. */
1089 #define SLJIT_FOP1_BASE 160
1091 /* Flags: - (does not modify flags) */
1092 #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0)
1093 #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_32)
1094 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
1095 SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
1096 Rounding mode when the destination is W or I: round towards zero. */
1097 /* Flags: - (does not modify flags) */
1098 #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1)
1099 #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_32)
1100 /* Flags: - (does not modify flags) */
1101 #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2)
1102 #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_32)
1103 /* Flags: - (does not modify flags) */
1104 #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3)
1105 #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_32)
1106 /* Flags: - (does not modify flags) */
1107 #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4)
1108 #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_32)
1109 /* Flags: - (does not modify flags) */
1110 #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5)
1111 #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_32)
1112 /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
1113 Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
1114 #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 6)
1115 #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_32)
1116 /* Flags: - (does not modify flags) */
1117 #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 7)
1118 #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_32)
1119 /* Flags: - (does not modify flags) */
1120 #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 8)
1121 #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_32)
1123 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1124 sljit_s32 dst
, sljit_sw dstw
,
1125 sljit_s32 src
, sljit_sw srcw
);
1127 /* Starting index of opcodes for sljit_emit_fop2. */
1128 #define SLJIT_FOP2_BASE 192
1130 /* Flags: - (does not modify flags) */
1131 #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0)
1132 #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_32)
1133 /* Flags: - (does not modify flags) */
1134 #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1)
1135 #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_32)
1136 /* Flags: - (does not modify flags) */
1137 #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2)
1138 #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_32)
1139 /* Flags: - (does not modify flags) */
1140 #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3)
1141 #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_32)
1143 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1144 sljit_s32 dst
, sljit_sw dstw
,
1145 sljit_s32 src1
, sljit_sw src1w
,
1146 sljit_s32 src2
, sljit_sw src2w
);
1148 /* Label and jump instructions. */
1150 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
);
1152 /* Invert (negate) conditional type: xor (^) with 0x1 */
1154 /* Integer comparison types. */
1155 #define SLJIT_EQUAL 0
1156 #define SLJIT_ZERO SLJIT_EQUAL
1157 #define SLJIT_NOT_EQUAL 1
1158 #define SLJIT_NOT_ZERO SLJIT_NOT_EQUAL
1160 #define SLJIT_LESS 2
1161 #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS)
1162 #define SLJIT_GREATER_EQUAL 3
1163 #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_GREATER_EQUAL)
1164 #define SLJIT_GREATER 4
1165 #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER)
1166 #define SLJIT_LESS_EQUAL 5
1167 #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_LESS_EQUAL)
1168 #define SLJIT_SIG_LESS 6
1169 #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS)
1170 #define SLJIT_SIG_GREATER_EQUAL 7
1171 #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SIG_GREATER_EQUAL)
1172 #define SLJIT_SIG_GREATER 8
1173 #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER)
1174 #define SLJIT_SIG_LESS_EQUAL 9
1175 #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_LESS_EQUAL)
1177 #define SLJIT_OVERFLOW 10
1178 #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW)
1179 #define SLJIT_NOT_OVERFLOW 11
1181 /* There is no SLJIT_CARRY or SLJIT_NOT_CARRY. */
1182 #define SLJIT_SET_CARRY SLJIT_SET(12)
1184 /* Floating point comparison types. */
1185 #define SLJIT_EQUAL_F64 14
1186 #define SLJIT_EQUAL_F32 (SLJIT_EQUAL_F64 | SLJIT_32)
1187 #define SLJIT_SET_EQUAL_F SLJIT_SET(SLJIT_EQUAL_F64)
1188 #define SLJIT_NOT_EQUAL_F64 15
1189 #define SLJIT_NOT_EQUAL_F32 (SLJIT_NOT_EQUAL_F64 | SLJIT_32)
1190 #define SLJIT_SET_NOT_EQUAL_F SLJIT_SET(SLJIT_NOT_EQUAL_F64)
1191 #define SLJIT_LESS_F64 16
1192 #define SLJIT_LESS_F32 (SLJIT_LESS_F64 | SLJIT_32)
1193 #define SLJIT_SET_LESS_F SLJIT_SET(SLJIT_LESS_F64)
1194 #define SLJIT_GREATER_EQUAL_F64 17
1195 #define SLJIT_GREATER_EQUAL_F32 (SLJIT_GREATER_EQUAL_F64 | SLJIT_32)
1196 #define SLJIT_SET_GREATER_EQUAL_F SLJIT_SET(SLJIT_GREATER_EQUAL_F64)
1197 #define SLJIT_GREATER_F64 18
1198 #define SLJIT_GREATER_F32 (SLJIT_GREATER_F64 | SLJIT_32)
1199 #define SLJIT_SET_GREATER_F SLJIT_SET(SLJIT_GREATER_F64)
1200 #define SLJIT_LESS_EQUAL_F64 19
1201 #define SLJIT_LESS_EQUAL_F32 (SLJIT_LESS_EQUAL_F64 | SLJIT_32)
1202 #define SLJIT_SET_LESS_EQUAL_F SLJIT_SET(SLJIT_LESS_EQUAL_F64)
1203 #define SLJIT_UNORDERED_F64 20
1204 #define SLJIT_UNORDERED_F32 (SLJIT_UNORDERED_F64 | SLJIT_32)
1205 #define SLJIT_SET_UNORDERED_F SLJIT_SET(SLJIT_UNORDERED_F64)
1206 #define SLJIT_ORDERED_F64 21
1207 #define SLJIT_ORDERED_F32 (SLJIT_ORDERED_F64 | SLJIT_32)
1208 #define SLJIT_SET_ORDERED_F SLJIT_SET(SLJIT_ORDERED_F64)
1210 /* Unconditional jump types. */
1211 #define SLJIT_JUMP 22
1212 /* Fast calling method. See sljit_emit_fast_enter / SLJIT_FAST_RETURN. */
1213 #define SLJIT_FAST_CALL 23
1214 /* Called function must be declared with the SLJIT_FUNC attribute. */
1215 #define SLJIT_CALL 24
1216 /* Called function must be declared with cdecl attribute.
1217 This is the default attribute for C functions. */
1218 #define SLJIT_CALL_CDECL 25
1220 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1221 #define SLJIT_REWRITABLE_JUMP 0x1000
1222 /* When this flag is passed, the execution of the current function ends and
1223 the called function returns to the caller of the current function. The
1224 stack usage is reduced before the call, but it is not necessarily reduced
1225 to zero. In the latter case the compiler needs to allocate space for some
1226 arguments and the return register must be kept as well.
1228 This feature is highly experimental and only supported on x86, ARM, PPC,
1229 and MIPS platforms at the moment. */
1230 #define SLJIT_TAIL_CALL 0x2000
1232 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1233 type must be between SLJIT_EQUAL and SLJIT_FAST_CALL
1234 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1236 Flags: does not modify flags. */
1237 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
);
1239 /* Emit a C compiler (ABI) compatible function call.
1240 type must be SLJIT_CALL or SLJIT_CALL_CDECL
1241 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP and SLJIT_TAIL_CALL
1242 arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1244 Flags: destroy all flags. */
1245 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 arg_types
);
1247 /* Basic arithmetic comparison. In most architectures it is implemented as
1248 an compare operation followed by a sljit_emit_jump. However some
1249 architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
1250 It is suggested to use this comparison form when appropriate.
1251 type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
1252 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1254 Flags: may destroy flags. */
1255 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_cmp(struct sljit_compiler
*compiler
, sljit_s32 type
,
1256 sljit_s32 src1
, sljit_sw src1w
,
1257 sljit_s32 src2
, sljit_sw src2w
);
1259 /* Basic floating point comparison. In most architectures it is implemented as
1260 an SLJIT_FCMP operation (setting appropriate flags) followed by a
1261 sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1262 special optimizations here. It is suggested to use this comparison form
1264 type must be between SLJIT_EQUAL_F64 and SLJIT_ORDERED_F32
1265 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1266 Flags: destroy flags.
1267 Note: if either operand is NaN, the behaviour is undefined for
1268 types up to SLJIT_S_LESS_EQUAL. */
1269 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_fcmp(struct sljit_compiler
*compiler
, sljit_s32 type
,
1270 sljit_s32 src1
, sljit_sw src1w
,
1271 sljit_s32 src2
, sljit_sw src2w
);
1273 /* Set the destination of the jump to this label. */
1274 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_label(struct sljit_jump
*jump
, struct sljit_label
* label
);
1275 /* Set the destination address of the jump to this label. */
1276 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_target(struct sljit_jump
*jump
, sljit_uw target
);
1278 /* Emit an indirect jump or fast call.
1279 Direct form: set src to SLJIT_IMM() and srcw to the address
1280 Indirect form: any other valid addressing mode
1281 type must be between SLJIT_JUMP and SLJIT_FAST_CALL
1283 Flags: does not modify flags. */
1284 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
);
1286 /* Emit a C compiler (ABI) compatible function call.
1287 Direct form: set src to SLJIT_IMM() and srcw to the address
1288 Indirect form: any other valid addressing mode
1289 type must be SLJIT_CALL or SLJIT_CALL_CDECL
1290 type can be combined (or'ed) with SLJIT_TAIL_CALL
1291 arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
1293 Flags: destroy all flags. */
1294 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
);
1296 /* Perform the operation using the conditional flags as the second argument.
1297 Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_F64. The value
1298 represented by the type is 1, if the condition represented by the type
1299 is fulfilled, and 0 otherwise.
1301 If op == SLJIT_MOV, SLJIT_MOV32:
1302 Set dst to the value represented by the type (0 or 1).
1303 Flags: - (does not modify flags)
1304 If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1305 Performs the binary operation using dst as the first, and the value
1306 represented by type as the second argument. Result is written into dst.
1307 Flags: Z (may destroy flags) */
1308 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
1309 sljit_s32 dst
, sljit_sw dstw
,
1312 /* Emit a conditional mov instruction which moves source to destination,
1313 if the condition is satisfied. Unlike other arithmetic operations this
1314 instruction does not support memory access.
1316 type must be between SLJIT_EQUAL and SLJIT_ORDERED_F64
1317 dst_reg must be a valid register and it can be combined
1318 with SLJIT_32 to perform a 32 bit arithmetic operation
1319 src must be register or immediate (SLJIT_IMM)
1321 Flags: - (does not modify flags) */
1322 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
1324 sljit_s32 src
, sljit_sw srcw
);
1326 /* The following flags are used by sljit_emit_mem() and sljit_emit_fmem(). */
1328 /* When SLJIT_MEM_SUPP is passed, no instructions are emitted.
1329 Instead the function returns with SLJIT_SUCCESS if the instruction
1330 form is supported and SLJIT_ERR_UNSUPPORTED otherwise. This flag
1331 allows runtime checking of available instruction forms. */
1332 #define SLJIT_MEM_SUPP 0x0200
1333 /* Memory load operation. This is the default. */
1334 #define SLJIT_MEM_LOAD 0x0000
1335 /* Memory store operation. */
1336 #define SLJIT_MEM_STORE 0x0400
1337 /* Base register is updated before the memory access. */
1338 #define SLJIT_MEM_PRE 0x0800
1339 /* Base register is updated after the memory access. */
1340 #define SLJIT_MEM_POST 0x1000
1342 /* Emit a single memory load or store with update instruction. When the
1343 requested instruction form is not supported by the CPU, it returns
1344 with SLJIT_ERR_UNSUPPORTED instead of emulating the instruction. This
1345 allows specializing tight loops based on the supported instruction
1346 forms (see SLJIT_MEM_SUPP flag).
1348 type must be between SLJIT_MOV and SLJIT_MOV_P and can be
1349 combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
1350 or SLJIT_MEM_POST must be specified.
1351 reg is the source or destination register, and must be
1352 different from the base register of the mem operand
1353 mem must be a SLJIT_MEM1() or SLJIT_MEM2() operand
1355 Flags: - (does not modify flags) */
1356 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_mem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1358 sljit_s32 mem
, sljit_sw memw
);
1360 /* Same as sljit_emit_mem except the followings:
1362 type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
1363 combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
1364 or SLJIT_MEM_POST must be specified.
1365 freg is the source or destination floating point register */
1367 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fmem(struct sljit_compiler
*compiler
, sljit_s32 type
,
1369 sljit_s32 mem
, sljit_sw memw
);
1371 /* Copies the base address of SLJIT_SP + offset to dst. The offset can be
1372 anything to negate the effect of relative addressing. For example if an
1373 array of sljit_sw values is stored on the stack from offset 0x40, and R0
1374 contains the offset of an array item plus 0x120, this item can be
1375 overwritten by two SLJIT instructions:
1377 sljit_get_local_base(compiler, SLJIT_R1, 0, 0x40 - 0x120);
1378 sljit_emit_op1(compiler, SLJIT_MOV, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_IMM, 0x5);
1380 Flags: - (may destroy flags) */
1381 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_local_base(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw offset
);
1383 /* Store a value that can be changed runtime (see: sljit_get_const_addr / sljit_set_const)
1384 Flags: - (does not modify flags) */
1385 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
);
1387 /* Store the value of a label (see: sljit_set_put_label)
1388 Flags: - (does not modify flags) */
1389 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
);
1391 /* Set the value stored by put_label to this label. */
1392 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_put_label(struct sljit_put_label
*put_label
, struct sljit_label
*label
);
1394 /* After the code generation the address for label, jump and const instructions
1395 are computed. Since these structures are freed by sljit_free_compiler, the
1396 addresses must be preserved by the user program elsewere. */
1397 static SLJIT_INLINE sljit_uw
sljit_get_label_addr(struct sljit_label
*label
) { return label
->addr
; }
1398 static SLJIT_INLINE sljit_uw
sljit_get_jump_addr(struct sljit_jump
*jump
) { return jump
->addr
; }
1399 static SLJIT_INLINE sljit_uw
sljit_get_const_addr(struct sljit_const
*const_
) { return const_
->addr
; }
1401 /* Only the address and executable offset are required to perform dynamic
1402 code modifications. See sljit_get_executable_offset function. */
1403 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_jump_addr(sljit_uw addr
, sljit_uw new_target
, sljit_sw executable_offset
);
1404 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_const(sljit_uw addr
, sljit_sw new_constant
, sljit_sw executable_offset
);
1406 /* --------------------------------------------------------------------- */
1407 /* Miscellaneous utility functions */
1408 /* --------------------------------------------------------------------- */
1410 #define SLJIT_MAJOR_VERSION 0
1411 #define SLJIT_MINOR_VERSION 94
1413 /* Get the human readable name of the platform. Can be useful on platforms
1414 like ARM, where ARM and Thumb2 functions can be mixed, and
1415 it is useful to know the type of the code generator. */
1416 SLJIT_API_FUNC_ATTRIBUTE
const char* sljit_get_platform_name(void);
1418 /* Portable helper function to get an offset of a member. */
1419 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1421 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1423 /* The sljit_stack structure and its manipulation functions provides
1424 an implementation for a top-down stack. The stack top is stored
1425 in the end field of the sljit_stack structure and the stack goes
1426 down to the min_start field, so the memory region reserved for
1427 this stack is between min_start (inclusive) and end (exclusive)
1428 fields. However the application can only use the region between
1429 start (inclusive) and end (exclusive) fields. The sljit_stack_resize
1430 function can be used to extend this region up to min_start.
1432 This feature uses the "address space reserve" feature of modern
1433 operating systems. Instead of allocating a large memory block
1434 applications can allocate a small memory region and extend it
1435 later without moving the content of the memory area. Therefore
1436 after a successful resize by sljit_stack_resize all pointers into
1437 this region are still valid.
1440 this structure may not be supported by all operating systems.
1441 end and max_limit fields are aligned to PAGE_SIZE bytes (usually
1443 stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */
1445 struct sljit_stack
{
1446 /* User data, anything can be stored here.
1447 Initialized to the same value as the end field. */
1449 /* These members are read only. */
1450 /* End address of the stack */
1452 /* Current start address of the stack. */
1454 /* Lowest start address of the stack. */
1455 sljit_u8
*min_start
;
1458 /* Allocates a new stack. Returns NULL if unsuccessful.
1459 Note: see sljit_create_compiler for the explanation of allocator_data. */
1460 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_stack
* SLJIT_FUNC
sljit_allocate_stack(sljit_uw start_size
, sljit_uw max_size
, void *allocator_data
);
1461 SLJIT_API_FUNC_ATTRIBUTE
void SLJIT_FUNC
sljit_free_stack(struct sljit_stack
*stack
, void *allocator_data
);
1463 /* Can be used to increase (extend) or decrease (shrink) the stack
1464 memory area. Returns with new_start if successful and NULL otherwise.
1465 It always fails if new_start is less than min_start or greater or equal
1466 than end fields. The fields of the stack are not changed if the returned
1467 value is NULL (the current memory content is never lost). */
1468 SLJIT_API_FUNC_ATTRIBUTE sljit_u8
*SLJIT_FUNC
sljit_stack_resize(struct sljit_stack
*stack
, sljit_u8
*new_start
);
1470 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1472 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1474 /* Get the entry address of a given function (signed, unsigned result). */
1475 #define SLJIT_FUNC_ADDR(func_name) ((sljit_sw)func_name)
1476 #define SLJIT_FUNC_UADDR(func_name) ((sljit_uw)func_name)
1478 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1480 /* All JIT related code should be placed in the same context (library, binary, etc.). */
1482 /* Get the entry address of a given function (signed, unsigned result). */
1483 #define SLJIT_FUNC_ADDR(func_name) (*(sljit_sw*)(void*)func_name)
1484 #define SLJIT_FUNC_UADDR(func_name) (*(sljit_uw*)(void*)func_name)
1486 /* For powerpc64, the function pointers point to a context descriptor. */
1487 struct sljit_function_context
{
1493 /* Fill the context arguments using the addr and the function.
1494 If func_ptr is NULL, it will not be set to the address of context
1495 If addr is NULL, the function address also comes from the func pointer. */
1496 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_function_context(void** func_ptr
, struct sljit_function_context
* context
, sljit_uw addr
, void* func
);
1498 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1500 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
1501 /* Free unused executable memory. The allocator keeps some free memory
1502 around to reduce the number of OS executable memory allocations.
1503 This improves performance since these calls are costly. However
1504 it is sometimes desired to free all unused memory regions, e.g.
1505 before the application terminates. */
1506 SLJIT_API_FUNC_ATTRIBUTE
void sljit_free_unused_memory_exec(void);
1509 /* --------------------------------------------------------------------- */
1510 /* CPU specific functions */
1511 /* --------------------------------------------------------------------- */
1513 /* The following function is a helper function for sljit_emit_op_custom.
1514 It returns with the real machine register index ( >=0 ) of any SLJIT_R,
1515 SLJIT_S and SLJIT_SP registers.
1517 Note: it returns with -1 for virtual registers (only on x86-32). */
1519 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
);
1521 /* The following function is a helper function for sljit_emit_op_custom.
1522 It returns with the real machine register index of any SLJIT_FLOAT register.
1524 Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
1526 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
);
1528 /* Any instruction can be inserted into the instruction stream by
1529 sljit_emit_op_custom. It has a similar purpose as inline assembly.
1530 The size parameter must match to the instruction size of the target
1533 x86: 0 < size <= 15. The instruction argument can be byte aligned.
1534 Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
1535 if size == 4, the instruction argument must be 4 byte aligned.
1536 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
1538 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1539 void *instruction
, sljit_u32 size
);
1541 /* Flags were set by a 32 bit operation. */
1542 #define SLJIT_CURRENT_FLAGS_32 SLJIT_32
1544 /* Flags were set by an ADD, ADDC, SUB, SUBC, or NEG operation. */
1545 #define SLJIT_CURRENT_FLAGS_ADD_SUB 0x01
1547 /* Flags were set by sljit_emit_op2u with SLJIT_SUB opcode.
1548 Must be combined with SLJIT_CURRENT_FLAGS_ADD_SUB. */
1549 #define SLJIT_CURRENT_FLAGS_COMPARE 0x02
1551 /* Define the currently available CPU status flags. It is usually used after
1552 an sljit_emit_label or sljit_emit_op_custom operations to define which CPU
1553 status flags are available.
1555 The current_flags must be a valid combination of SLJIT_SET_* and
1556 SLJIT_CURRENT_FLAGS_* constants. */
1558 SLJIT_API_FUNC_ATTRIBUTE
void sljit_set_current_flags(struct sljit_compiler
*compiler
,
1559 sljit_s32 current_flags
);
1565 #endif /* SLJIT_LIR_H_ */