1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef SLJIT_LIR_H_
28 #define SLJIT_LIR_H_
29
30 /*
31 ------------------------------------------------------------------------
32 Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
33 ------------------------------------------------------------------------
34
35 Short description
36 Advantages:
37 - The execution can be continued from any LIR instruction. In other
38 words, it is possible to jump to any label from anywhere, even from
39 a code fragment, which is compiled later, as long as the compiling
40 context is the same. See sljit_emit_enter for more details.
41 - Supports self modifying code: target of any jump and call
42 instructions and some constant values can be dynamically modified
43 during runtime. See SLJIT_REWRITABLE_JUMP.
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 - A fixed stack space can be allocated for local variables
48 - The compiler is thread-safe
49 - The compiler is highly configurable through preprocessor macros.
50 You can disable unneeded features (multithreading in single
51 threaded applications), and you can use your own system functions
52 (including memory allocators). See sljitConfig.h.
53 Disadvantages:
54 - The compiler is more like a platform independent assembler, so
55 there is no built-in variable management. Registers and stack must
56 be managed manually (the name of the compiler refers to this).
57 In practice:
58 - This approach is very effective for interpreters
59 - One of the saved registers typically points to a stack interface
60 - It can jump to any exception handler anytime (even if it belongs
61 to another function)
62 - Hot paths can be modified during runtime reflecting the changes
63 of the fastest execution path of the dynamic language
64 - SLJIT supports complex memory addressing modes
65 - mainly position and context independent code (except some cases)
66
67 For valgrind users:
68 - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
69 */
70
71 #if (defined SLJIT_HAVE_CONFIG_PRE && SLJIT_HAVE_CONFIG_PRE)
72 #include "sljitConfigPre.h"
73 #endif /* SLJIT_HAVE_CONFIG_PRE */
74
75 #include "sljitConfigCPU.h"
76 #include "sljitConfig.h"
77
78 /* The following header file defines useful macros for fine tuning
79 SLJIT based code generators. They are listed in the beginning
80 of sljitConfigInternal.h */
81
82 #include "sljitConfigInternal.h"
83
84 #if (defined SLJIT_HAVE_CONFIG_POST && SLJIT_HAVE_CONFIG_POST)
85 #include "sljitConfigPost.h"
86 #endif /* SLJIT_HAVE_CONFIG_POST */
87
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91
92 /* Version numbers. */
93 #define SLJIT_MAJOR_VERSION 0
94 #define SLJIT_MINOR_VERSION 95
95
96 /* --------------------------------------------------------------------- */
97 /* Error codes */
98 /* --------------------------------------------------------------------- */
99
100 /* Indicates no error. */
101 #define SLJIT_SUCCESS 0
102 /* After the call of sljit_generate_code(), the error code of the compiler
103 is set to this value to avoid further code generation.
104 The complier should be freed after sljit_generate_code(). */
105 #define SLJIT_ERR_COMPILED 1
106 /* Cannot allocate non-executable memory. */
107 #define SLJIT_ERR_ALLOC_FAILED 2
108 /* Cannot allocate executable memory.
109 Only sljit_generate_code() returns with this error code. */
110 #define SLJIT_ERR_EX_ALLOC_FAILED 3
111 /* Unsupported instruction form. */
112 #define SLJIT_ERR_UNSUPPORTED 4
113 /* An invalid argument is passed to any SLJIT function. */
114 #define SLJIT_ERR_BAD_ARGUMENT 5
115
116 /* --------------------------------------------------------------------- */
117 /* Registers */
118 /* --------------------------------------------------------------------- */
119
120 /*
121 Scratch (R) registers: registers which may not preserve their values
122 across function calls.
123
124 Saved (S) registers: registers which preserve their values across
125 function calls.
126
127 The scratch and saved register sets overlap. The last scratch register
128 is the first saved register, the one before the last is the second saved
129 register, and so on.
130
131 For example, in an architecture with only five registers (A-E), if two
132 are scratch and three saved registers, they will be defined as follows:
133
134 A | R0 | | R0 always represent scratch register A
135 B | R1 | | R1 always represent scratch register B
136 C | [R2] | S2 | R2 and S2 represent the same physical register C
137 D | [R3] | S1 | R3 and S1 represent the same physical register D
138 E | [R4] | S0 | R4 and S0 represent the same physical register E
139
140 Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS will be 2 and
141 SLJIT_NUMBER_OF_SAVED_REGISTERS will be 3.
142
143 Note: For all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 12
144 and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 6. However, 6 registers
145 are virtual on x86-32. See below.
146
147 The purpose of this definition is convenience: saved registers can
148 be used as extra scratch registers. For example, building in the
149 previous example, four registers can be specified as scratch registers
150 and the fifth one as saved register, allowing any user code which requires
151 four scratch registers to run unmodified. The SLJIT compiler automatically
152 saves the content of the two extra scratch register on the stack. Scratch
153 registers can also be preserved by saving their value on the stack but
154 that needs to be done manually.
155
156 Note: To emphasize that registers assigned to R2-R4 are saved
157 registers, they are enclosed by square brackets.
158
159 Note: sljit_emit_enter and sljit_set_context define whether a register
160 is S or R register. E.g: if in the previous example 3 scratches and
161 1 saved are mapped by sljit_emit_enter, the allowed register set
162 will be: R0-R2 and S0. Although S2 is mapped to the same register
163 than R2, it is not available in that configuration. Furthermore
164 the S1 register cannot be used at all.
165 */
166
167 /* Scratch registers. */
168 #define SLJIT_R0 1
169 #define SLJIT_R1 2
170 #define SLJIT_R2 3
171 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
172 are allocated on the stack). These registers are called virtual
173 and cannot be used for memory addressing (cannot be part of
174 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
175 limitation on other CPUs. See sljit_get_register_index(). */
176 #define SLJIT_R3 4
177 #define SLJIT_R4 5
178 #define SLJIT_R5 6
179 #define SLJIT_R6 7
180 #define SLJIT_R7 8
181 #define SLJIT_R8 9
182 #define SLJIT_R9 10
183 /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
184 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
185 #define SLJIT_R(i) (1 + (i))
186
187 /* Saved registers. */
188 #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
189 #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
190 #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
191 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
192 are allocated on the stack). These registers are called virtual
193 and cannot be used for memory addressing (cannot be part of
194 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
195 limitation on other CPUs. See sljit_get_register_index(). */
196 #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
197 #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
198 #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
199 #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
200 #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
201 #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
202 #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
203 /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
204 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
205 #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
206
207 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
208 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
209
210 /* The SLJIT_SP provides direct access to the linear stack space allocated by
211 sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
212 The immediate offset is extended by the relative stack offset automatically.
213 sljit_get_local_base can be used to obtain the real address of a value. */
214 #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
215
216 /* Return with machine word. */
217
218 #define SLJIT_RETURN_REG SLJIT_R0
219
220 /* --------------------------------------------------------------------- */
221 /* Floating point registers */
222 /* --------------------------------------------------------------------- */
223
224 /* Each floating point register can store a 32 or a 64 bit precision
225 value. The FR and FS register sets overlap in the same way as R
226 and S register sets. See above. */
227
228 /* Floating point scratch registers. */
229 #define SLJIT_FR0 1
230 #define SLJIT_FR1 2
231 #define SLJIT_FR2 3
232 #define SLJIT_FR3 4
233 #define SLJIT_FR4 5
234 #define SLJIT_FR5 6
235 #define SLJIT_FR6 7
236 #define SLJIT_FR7 8
237 #define SLJIT_FR8 9
238 #define SLJIT_FR9 10
239 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
240 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
241 #define SLJIT_FR(i) (1 + (i))
242
243 /* Floating point saved registers. */
244 #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
245 #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
246 #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
247 #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
248 #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
249 #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
250 #define SLJIT_FS6 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 6)
251 #define SLJIT_FS7 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 7)
252 #define SLJIT_FS8 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 8)
253 #define SLJIT_FS9 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 9)
254 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
255 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
256 #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
257
258 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
259 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
260
261 /* Return with floating point arg. */
262
263 #define SLJIT_RETURN_FREG SLJIT_FR0
264
265 /* --------------------------------------------------------------------- */
266 /* Argument type definitions */
267 /* --------------------------------------------------------------------- */
268
269 /* The following argument type definitions are used by sljit_emit_enter,
270 sljit_set_context, sljit_emit_call, and sljit_emit_icall functions.
271
272 For sljit_emit_call and sljit_emit_icall, the first integer argument
273 must be placed into SLJIT_R0, the second one into SLJIT_R1, and so on.
274 Similarly the first floating point argument must be placed into SLJIT_FR0,
275 the second one into SLJIT_FR1, and so on.
276
277 For sljit_emit_enter, the integer arguments can be stored in scratch
278 or saved registers. Scratch registers are identified by a _R suffix.
279
280 If only saved registers are used, then the allocation mirrors what is
281 done for the "call" functions but using saved registers, meaning that
282 the first integer argument goes to SLJIT_S0, the second one goes into
283 SLJIT_S1, and so on.
284
285 If scratch registers are used, then the way the integer registers are
286 allocated changes so that SLJIT_S0, SLJIT_S1, etc; will be assigned
287 only for the arguments not using scratch registers, while SLJIT_R<n>
288 will be used for the ones using scratch registers.
289
290 Furthermore, the index (shown as "n" above) that will be used for the
291 scratch register depends on how many previous integer registers
292 (scratch or saved) were used already, starting with SLJIT_R0.
293 Eventhough some indexes will be likely skipped, they still need to be
294 accounted for in the scratches parameter of sljit_emit_enter. See below
295 for some examples.
296
297 The floating point arguments always use scratch registers (but not the
298 _R suffix like the integer arguments) and must use SLJIT_FR0, SLJIT_FR1,
299 just like in the "call" functions.
300
301 Note: the mapping for scratch registers is part of the compiler context
302 and therefore a new context after sljit_emit_call/sljit_emit_icall
303 could remove access to some scratch registers that were used as
304 arguments.
305
306 Example function definition:
307 sljit_f32 SLJIT_FUNC example_c_callback(void *arg_a,
308 sljit_f64 arg_b, sljit_u32 arg_c, sljit_f32 arg_d);
309
310 Argument type definition:
311 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_F32)
312 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_P, 1) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F64, 2)
313 | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_32, 3) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 4)
314
315 Short form of argument type definition:
316 SLJIT_ARGS4(F32, P, F64, 32, F32)
317
318 Argument passing:
319 arg_a must be placed in SLJIT_R0
320 arg_b must be placed in SLJIT_FR0
321 arg_c must be placed in SLJIT_R1
322 arg_d must be placed in SLJIT_FR1
323
324 Examples for argument processing by sljit_emit_enter:
325 SLJIT_ARGS4V(P, 32_R, F32, W)
326 Arguments are placed into: SLJIT_S0, SLJIT_R1, SLJIT_FR0, SLJIT_S1
327 The type of the result is void.
328
329 SLJIT_ARGS4(F32, W, W_R, W, W_R)
330 Arguments are placed into: SLJIT_S0, SLJIT_R1, SLJIT_S1, SLJIT_R3
331 The type of the result is sljit_f32.
332
333 SLJIT_ARGS4(P, W, F32, P_R)
334 Arguments are placed into: SLJIT_FR0, SLJIT_S0, SLJIT_FR1, SLJIT_R1
335 The type of the result is pointer.
336
337 Note: it is recommended to pass the scratch arguments first
338 followed by the saved arguments:
339
340 SLJIT_ARGS4(W, W_R, W_R, W, W)
341 Arguments are placed into: SLJIT_R0, SLJIT_R1, SLJIT_S0, SLJIT_S1
342 The type of the result is sljit_sw / sljit_uw.
343 */
344
345 /* The following flag is only allowed for the integer arguments of
346 sljit_emit_enter. When the flag is set, the integer argument is
347 stored in a scratch register instead of a saved register. */
348 #define SLJIT_ARG_TYPE_SCRATCH_REG 0x8
349
350 /* No return value, only supported by SLJIT_ARG_RETURN. */
351 #define SLJIT_ARG_TYPE_RET_VOID 0
352 /* Machine word sized integer argument or result. */
353 #define SLJIT_ARG_TYPE_W 1
354 #define SLJIT_ARG_TYPE_W_R (SLJIT_ARG_TYPE_W | SLJIT_ARG_TYPE_SCRATCH_REG)
355 /* 32 bit integer argument or result. */
356 #define SLJIT_ARG_TYPE_32 2
357 #define SLJIT_ARG_TYPE_32_R (SLJIT_ARG_TYPE_32 | SLJIT_ARG_TYPE_SCRATCH_REG)
358 /* Pointer sized integer argument or result. */
359 #define SLJIT_ARG_TYPE_P 3
360 #define SLJIT_ARG_TYPE_P_R (SLJIT_ARG_TYPE_P | SLJIT_ARG_TYPE_SCRATCH_REG)
361 /* 64 bit floating point argument or result. */
362 #define SLJIT_ARG_TYPE_F64 4
363 /* 32 bit floating point argument or result. */
364 #define SLJIT_ARG_TYPE_F32 5
365
366 #define SLJIT_ARG_SHIFT 4
367 #define SLJIT_ARG_RETURN(type) (type)
368 #define SLJIT_ARG_VALUE(type, idx) ((type) << ((idx) * SLJIT_ARG_SHIFT))
369
370 /* Simplified argument list definitions.
371
372 The following definition:
373 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_W) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_F32, 1)
374
375 can be shortened to:
376 SLJIT_ARGS1(W, F32)
377
378 Another example where no value is returned:
379 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_RET_VOID) | SLJIT_ARG_VALUE(SLJIT_ARG_TYPE_W_R, 1)
380
381 can be shortened to:
382 SLJIT_ARGS1V(W_R)
383 */
384
385 #define SLJIT_ARG_TO_TYPE(type) SLJIT_ARG_TYPE_ ## type
386
387 #define SLJIT_ARGS0(ret) \
388 SLJIT_ARG_RETURN(SLJIT_ARG_TO_TYPE(ret))
389 #define SLJIT_ARGS0V() \
390 SLJIT_ARG_RETURN(SLJIT_ARG_TYPE_RET_VOID)
391
392 #define SLJIT_ARGS1(ret, arg1) \
393 (SLJIT_ARGS0(ret) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg1), 1))
394 #define SLJIT_ARGS1V(arg1) \
395 (SLJIT_ARGS0V() | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg1), 1))
396
397 #define SLJIT_ARGS2(ret, arg1, arg2) \
398 (SLJIT_ARGS1(ret, arg1) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg2), 2))
399 #define SLJIT_ARGS2V(arg1, arg2) \
400 (SLJIT_ARGS1V(arg1) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg2), 2))
401
402 #define SLJIT_ARGS3(ret, arg1, arg2, arg3) \
403 (SLJIT_ARGS2(ret, arg1, arg2) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg3), 3))
404 #define SLJIT_ARGS3V(arg1, arg2, arg3) \
405 (SLJIT_ARGS2V(arg1, arg2) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg3), 3))
406
407 #define SLJIT_ARGS4(ret, arg1, arg2, arg3, arg4) \
408 (SLJIT_ARGS3(ret, arg1, arg2, arg3) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg4), 4))
409 #define SLJIT_ARGS4V(arg1, arg2, arg3, arg4) \
410 (SLJIT_ARGS3V(arg1, arg2, arg3) | SLJIT_ARG_VALUE(SLJIT_ARG_TO_TYPE(arg4), 4))
411
412 /* --------------------------------------------------------------------- */
413 /* Main structures and functions */
414 /* --------------------------------------------------------------------- */
415
416 /*
417 The following structures are private, and can be changed in the
418 future. Keeping them here allows code inlining.
419 */
420
421 struct sljit_memory_fragment {
422 struct sljit_memory_fragment *next;
423 sljit_uw used_size;
424 /* Must be aligned to sljit_sw. */
425 sljit_u8 memory[1];
426 };
427
428 struct sljit_label {
429 struct sljit_label *next;
430 sljit_uw addr;
431 /* The maximum size difference. */
432 sljit_uw size;
433 };
434
435 struct sljit_jump {
436 struct sljit_jump *next;
437 sljit_uw addr;
438 /* Architecture dependent flags. */
439 sljit_uw flags;
440 union {
441 sljit_uw target;
442 struct sljit_label *label;
443 } u;
444 };
445
446 struct sljit_put_label {
447 struct sljit_put_label *next;
448 struct sljit_label *label;
449 sljit_uw addr;
450 sljit_uw flags;
451 };
452
453 struct sljit_const {
454 struct sljit_const *next;
455 sljit_uw addr;
456 };
457
458 struct sljit_compiler {
459 sljit_s32 error;
460 sljit_s32 options;
461
462 struct sljit_label *labels;
463 struct sljit_jump *jumps;
464 struct sljit_put_label *put_labels;
465 struct sljit_const *consts;
466 struct sljit_label *last_label;
467 struct sljit_jump *last_jump;
468 struct sljit_const *last_const;
469 struct sljit_put_label *last_put_label;
470
471 void *allocator_data;
472 void *exec_allocator_data;
473 struct sljit_memory_fragment *buf;
474 struct sljit_memory_fragment *abuf;
475
476 /* Available scratch registers. */
477 sljit_s32 scratches;
478 /* Available saved registers. */
479 sljit_s32 saveds;
480 /* Available float scratch registers. */
481 sljit_s32 fscratches;
482 /* Available float saved registers. */
483 sljit_s32 fsaveds;
484 /* Local stack size. */
485 sljit_s32 local_size;
486 /* Maximum code size. */
487 sljit_uw size;
488 /* Relative offset of the executable mapping from the writable mapping. */
489 sljit_sw executable_offset;
490 /* Executable size for statistical purposes. */
491 sljit_uw executable_size;
492
493 #if (defined SLJIT_HAS_STATUS_FLAGS_STATE && SLJIT_HAS_STATUS_FLAGS_STATE)
494 sljit_s32 status_flags_state;
495 #endif
496
497 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
498 sljit_s32 args_size;
499 #endif
500
501 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
502 sljit_s32 mode32;
503 #endif
504
505 #if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6)
506 /* Constant pool handling. */
507 sljit_uw *cpool;
508 sljit_u8 *cpool_unique;
509 sljit_uw cpool_diff;
510 sljit_uw cpool_fill;
511 /* Other members. */
512 /* Contains pointer, "ldr pc, [...]" pairs. */
513 sljit_uw patches;
514 #endif
515
516 #if (defined SLJIT_CONFIG_ARM_V6 && SLJIT_CONFIG_ARM_V6) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
517 /* Temporary fields. */
518 sljit_uw shift_imm;
519 #endif /* SLJIT_CONFIG_ARM_V6 || SLJIT_CONFIG_ARM_V6 */
520
521 #if (defined SLJIT_CONFIG_ARM_32 && SLJIT_CONFIG_ARM_32) && (defined __SOFTFP__)
522 sljit_uw args_size;
523 #endif
524
525 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
526 sljit_u32 imm;
527 #endif
528
529 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
530 sljit_s32 delay_slot;
531 sljit_s32 cache_arg;
532 sljit_sw cache_argw;
533 #endif
534
535 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
536 sljit_uw args_size;
537 #endif
538
539 #if (defined SLJIT_CONFIG_RISCV && SLJIT_CONFIG_RISCV)
540 sljit_s32 cache_arg;
541 sljit_sw cache_argw;
542 #endif
543
544 #if (defined SLJIT_CONFIG_S390X && SLJIT_CONFIG_S390X)
545 /* Need to allocate register save area to make calls. */
546 sljit_s32 mode;
547 #endif
548
549 #if (defined SLJIT_CONFIG_LOONGARCH && SLJIT_CONFIG_LOONGARCH)
550 sljit_s32 cache_arg;
551 sljit_sw cache_argw;
552 #endif
553
554 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
555 FILE* verbose;
556 #endif
557
558 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
559 || (defined SLJIT_DEBUG && SLJIT_DEBUG)
560 /* Flags specified by the last arithmetic instruction.
561 It contains the type of the variable flag. */
562 sljit_s32 last_flags;
563 /* Return value type set by entry functions. */
564 sljit_s32 last_return;
565 /* Local size passed to entry functions. */
566 sljit_s32 logical_local_size;
567 #endif
568
569 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
570 || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
571 || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
572 /* Trust arguments when an API function is called.
573 Used internally for calling API functions. */
574 sljit_s32 skip_checks;
575 #endif
576 };
577
578 /* --------------------------------------------------------------------- */
579 /* Main functions */
580 /* --------------------------------------------------------------------- */
581
582 /* Creates an SLJIT compiler. The allocator_data is required by some
583 custom memory managers. This pointer is passed to SLJIT_MALLOC
584 and SLJIT_FREE macros. Most allocators (including the default
585 one) ignores this value, and it is recommended to pass NULL
586 as a dummy value for allocator_data. The exec_allocator_data
587 has the same purpose but this one is passed to SLJIT_MALLOC_EXEC /
588 SLJIT_MALLOC_FREE functions.
589
590 Returns NULL if failed. */
591 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data, void *exec_allocator_data);
592
593 /* Frees everything except the compiled machine code. */
594 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
595
596 /* Returns the current error code. If an error occurres, future calls
597 which uses the same compiler argument returns early with the same
598 error code. Thus there is no need for checking the error after every
599 call, it is enough to do it after the code is compiled. Removing
600 these checks increases the performance of the compiling process. */
sljit_get_compiler_error(struct sljit_compiler * compiler)601 static SLJIT_INLINE sljit_s32 sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
602
603 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
604 if an error was detected before. After the error code is set
605 the compiler behaves as if the allocation failure happened
606 during an SLJIT function call. This can greatly simplify error
607 checking, since it is enough to check the compiler status
608 after the code is compiled. */
609 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
610
611 /* Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
612 and <= 128 bytes on 64 bit architectures. The memory area is owned by the
613 compiler, and freed by sljit_free_compiler. The returned pointer is
614 sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
615 compiling, and no need to worry about freeing them. The size is enough
616 to contain at most 16 pointers. If the size is outside of the range,
617 the function will return with NULL. However, this return value does not
618 indicate that there is no more memory (does not set the current error code
619 of the compiler to out-of-memory status). */
620 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_s32 size);
621
622 /* Returns the allocator data passed to sljit_create_compiler. These pointers
623 may contain context data even if the normal/exec allocator ignores it. */
sljit_get_allocator_data(struct sljit_compiler * compiler)624 static SLJIT_INLINE void* sljit_get_allocator_data(struct sljit_compiler *compiler) { return compiler->allocator_data; }
sljit_get_exec_allocator_data(struct sljit_compiler * compiler)625 static SLJIT_INLINE void* sljit_get_exec_allocator_data(struct sljit_compiler *compiler) { return compiler->exec_allocator_data; }
626
627 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
628 /* Passing NULL disables verbose. */
629 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
630 #endif
631
632 /* Create executable code from the instruction stream. This is the final step
633 of the code generation so no more instructions can be emitted after this call. */
634
635 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
636
637 /* Free executable code. */
638
639 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code, void *exec_allocator_data);
640
641 /* When the protected executable allocator is used the JIT code is mapped
642 twice. The first mapping has read/write and the second mapping has read/exec
643 permissions. This function returns with the relative offset of the executable
644 mapping using the writable mapping as the base after the machine code is
645 successfully generated. The returned value is always 0 for the normal executable
646 allocator, since it uses only one mapping with read/write/exec permissions.
647 Dynamic code modifications requires this value.
648
649 Before a successful code generation, this function returns with 0. */
sljit_get_executable_offset(struct sljit_compiler * compiler)650 static SLJIT_INLINE sljit_sw sljit_get_executable_offset(struct sljit_compiler *compiler) { return compiler->executable_offset; }
651
652 /* The executable memory consumption of the generated code can be retrieved by
653 this function. The returned value can be used for statistical purposes.
654
655 Before a successful code generation, this function returns with 0. */
sljit_get_generated_code_size(struct sljit_compiler * compiler)656 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
657
658 /* Returns with non-zero if the feature or limitation type passed as its
659 argument is present on the current CPU. The return value is one, if a
660 feature is fully supported, and it is two, if partially supported.
661
662 Some features (e.g. floating point operations) require hardware (CPU)
663 support while others (e.g. move with update) are emulated if not available.
664 However, even when a feature is emulated, specialized code paths may be
665 faster than the emulation. Some limitations are emulated as well so their
666 general case is supported but it has extra performance costs. */
667
668 /* [Not emulated] Floating-point support is available. */
669 #define SLJIT_HAS_FPU 0
670 /* [Limitation] Some registers are virtual registers. */
671 #define SLJIT_HAS_VIRTUAL_REGISTERS 1
672 /* [Emulated] Has zero register (setting a memory location to zero is efficient). */
673 #define SLJIT_HAS_ZERO_REGISTER 2
674 /* [Emulated] Count leading zero is supported. */
675 #define SLJIT_HAS_CLZ 3
676 /* [Emulated] Count trailing zero is supported. */
677 #define SLJIT_HAS_CTZ 4
678 /* [Emulated] Reverse the order of bytes is supported. */
679 #define SLJIT_HAS_REV 5
680 /* [Emulated] Rotate left/right is supported. */
681 #define SLJIT_HAS_ROT 6
682 /* [Emulated] Conditional move is supported. */
683 #define SLJIT_HAS_CMOV 7
684 /* [Emulated] Prefetch instruction is available (emulated as a nop). */
685 #define SLJIT_HAS_PREFETCH 8
686 /* [Emulated] Copy from/to f32 operation is available (see sljit_emit_fcopy). */
687 #define SLJIT_HAS_COPY_F32 9
688 /* [Emulated] Copy from/to f64 operation is available (see sljit_emit_fcopy). */
689 #define SLJIT_HAS_COPY_F64 10
690 /* [Not emulated] The 64 bit floating point registers can be used as
691 two separate 32 bit floating point registers (e.g. ARM32). The
692 second 32 bit part can be accessed by SLJIT_F64_SECOND. */
693 #define SLJIT_HAS_F64_AS_F32_PAIR 11
694 /* [Not emulated] Some SIMD operations are supported by the compiler. */
695 #define SLJIT_HAS_SIMD 12
696 /* [Not emulated] SIMD registers are mapped to a pair of double precision
697 floating point registers. E.g. passing either SLJIT_FR0 or SLJIT_FR1 to
698 a simd operation represents the same 128 bit register, and both SLJIT_FR0
699 and SLJIT_FR1 are overwritten. */
700 #define SLJIT_SIMD_REGS_ARE_PAIRS 13
701 /* [Not emulated] Atomic support is available (fine-grained). */
702 #define SLJIT_HAS_ATOMIC 14
703
704 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
705 /* [Not emulated] AVX support is available on x86. */
706 #define SLJIT_HAS_AVX 100
707 /* [Not emulated] AVX2 support is available on x86. */
708 #define SLJIT_HAS_AVX2 101
709 #endif
710
711 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type);
712
713 /* If type is between SLJIT_ORDERED_EQUAL and SLJIT_ORDERED_LESS_EQUAL,
714 sljit_cmp_info returns with:
715 zero - if the cpu supports the floating point comparison type
716 one - if the comparison requires two machine instructions
717 two - if the comparison requires more than two machine instructions
718
719 When the result is non-zero, it is recommended to avoid
720 using the specified comparison type if it is easy to do so.
721
722 Otherwise it returns zero. */
723 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_cmp_info(sljit_s32 type);
724
725 /* The following functions generate machine code. If there is no
726 error, they return with SLJIT_SUCCESS, otherwise they return
727 with an error code. */
728
729 /*
730 The executable code is a function from the viewpoint of the C
731 language. The function calls must conform to the ABI (Application
732 Binary Interface) of the platform, which specify the purpose of
733 machine registers and stack handling among other things. The
734 sljit_emit_enter function emits the necessary instructions for
735 setting up a new context for the executable code. This is often
736 called as function prologue. Furthermore the options argument
737 can be used to pass configuration options to the compiler. The
738 available options are listed before sljit_emit_enter.
739
740 The function argument list is specified by the SLJIT_ARGSx
741 (SLJIT_ARGS0 .. SLJIT_ARGS4) macros. Currently maximum four
742 arguments are supported. See the description of SLJIT_ARGSx
743 macros about argument passing. Furthermore the register set
744 used by the function must be declared as well. The number of
745 scratch and saved registers available to the function must
746 be passed to sljit_emit_enter. Only R registers between R0
747 and "scratches" argument can be used later. E.g. if "scratches"
748 is set to two, the scratch register set will be limited to
749 SLJIT_R0 and SLJIT_R1. The S registers and the floating point
750 registers ("fscratches" and "fsaveds") are specified in a
751 similar manner. The sljit_emit_enter is also capable of
752 allocating a stack space for local data. The "local_size"
753 argument contains the size in bytes of this local area, and
754 it can be accessed using SLJIT_MEM1(SLJIT_SP). The memory
755 area between SLJIT_SP (inclusive) and SLJIT_SP + local_size
756 (exclusive) can be modified freely until the function returns.
757 The stack space is not initialized to zero.
758
759 Note: the following conditions must met:
760 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
761 0 <= saveds <= SLJIT_NUMBER_OF_SAVED_REGISTERS
762 scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
763 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
764 0 <= fsaveds <= SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS
765 fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
766
767 Note: the compiler can use saved registers as scratch registers,
768 but the opposite is not supported
769
770 Note: every call of sljit_emit_enter and sljit_set_context
771 overwrites the previous context.
772 */
773
774 /* Saved registers between SLJIT_S0 and SLJIT_S(n - 1) (inclusive)
775 are not saved / restored on function enter / return. Instead,
776 these registers can be used to pass / return data (such as
777 global / local context pointers) across function calls. The
778 value of n must be between 1 and 3. This option is only
779 supported by SLJIT_ENTER_REG_ARG calling convention. */
780 #define SLJIT_ENTER_KEEP(n) (n)
781
782 /* The compiled function uses an SLJIT specific register argument
783 calling convention. This is a lightweight function call type where
784 both the caller and the called functions must be compiled by
785 SLJIT. The type argument of the call must be SLJIT_CALL_REG_ARG
786 and all arguments must be stored in scratch registers. */
787 #define SLJIT_ENTER_REG_ARG 0x00000004
788
789 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
790 #define SLJIT_MAX_LOCAL_SIZE 1048576
791
792 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
793 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
794 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
795
796 /* The SLJIT compiler has a current context (which contains the local
797 stack space size, number of used registers, etc.) which is initialized
798 by sljit_emit_enter. Several functions (such as sljit_emit_return)
799 requires this context to be able to generate the appropriate code.
800 However, some code fragments (compiled separately) may have no
801 normal entry point so their context is unknown to the compiler.
802
803 sljit_set_context and sljit_emit_enter have the same arguments,
804 but sljit_set_context does not generate any machine code.
805
806 Note: every call of sljit_emit_enter and sljit_set_context overwrites
807 the previous context. */
808
809 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
810 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
811 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
812
813 /* Return to the caller function. The sljit_emit_return_void function
814 does not return with any value. The sljit_emit_return function returns
815 with a single value loaded from its source operand. The load operation
816 can be between SLJIT_MOV and SLJIT_MOV_P (see sljit_emit_op1) and
817 SLJIT_MOV_F32/SLJIT_MOV_F64 (see sljit_emit_fop1) depending on the
818 return value specified by sljit_emit_enter/sljit_set_context. */
819
820 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler);
821
822 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op,
823 sljit_s32 src, sljit_sw srcw);
824
825 /* Restores the saved registers and free the stack area, then the execution
826 continues from the address specified by the source operand. This
827 operation is similar to sljit_emit_return, but it ignores the return
828 address. The code where the exection continues should use the same context
829 as the caller function (see sljit_set_context). A word (pointer) value
830 can be passed in the SLJIT_RETURN_REG register. This function can be used
831 to jump to exception handlers. */
832
833 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_to(struct sljit_compiler *compiler,
834 sljit_s32 src, sljit_sw srcw);
835
836 /*
837 Source and destination operands for arithmetical instructions
838 imm - a simple immediate value (cannot be used as a destination)
839 reg - any of the available registers (immediate argument must be 0)
840 [imm] - absolute memory address
841 [reg+imm] - indirect memory address
842 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
843 useful for accessing arrays (fully supported by both x86 and
844 ARM architectures, and cheap operation on others)
845 */
846
847 /*
848 IMPORTANT NOTE: memory accesses MUST be naturally aligned unless
849 SLJIT_UNALIGNED macro is defined and its value is 1.
850
851 length | alignment
852 ---------+-----------
853 byte | 1 byte (any physical_address is accepted)
854 half | 2 byte (physical_address & 0x1 == 0)
855 int | 4 byte (physical_address & 0x3 == 0)
856 word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
857 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
858 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
859 | on 64 bit machines)
860
861 Note: Different architectures have different addressing limitations.
862 A single instruction is enough for the following addressing
863 modes. Other addressing modes are emulated by instruction
864 sequences. This information could help to improve those code
865 generators which focuses only a few architectures.
866
867 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
868 [reg+(reg<<imm)] is supported
869 [imm], -2^32+1 <= imm <= 2^32-1 is supported
870 Write-back is not supported
871 arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
872 bytes, any halfs or floating point values)
873 [reg+(reg<<imm)] is supported
874 Write-back is supported
875 arm-t2: [reg+imm], -255 <= imm <= 4095
876 [reg+(reg<<imm)] is supported
877 Write back is supported only for [reg+imm], where -255 <= imm <= 255
878 arm64: [reg+imm], -256 <= imm <= 255, 0 <= aligned imm <= 4095 * alignment
879 [reg+(reg<<imm)] is supported
880 Write back is supported only for [reg+imm], where -256 <= imm <= 255
881 ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
882 signed load on 64 bit requires immediates divisible by 4.
883 [reg+imm] is not supported for signed 8 bit values.
884 [reg+reg] is supported
885 Write-back is supported except for one instruction: 32 bit signed
886 load with [reg+imm] addressing mode on 64 bit.
887 mips: [reg+imm], -65536 <= imm <= 65535
888 Write-back is not supported
889 riscv: [reg+imm], -2048 <= imm <= 2047
890 Write-back is not supported
891 s390x: [reg+imm], -2^19 <= imm < 2^19
892 [reg+reg] is supported
893 Write-back is not supported
894 loongarch: [reg+imm], -2048 <= imm <= 2047
895 [reg+reg] is supported
896 Write-back is not supported
897 */
898
899 /* Macros for specifying operand types. */
900 #define SLJIT_MEM 0x80
901 #define SLJIT_MEM0() (SLJIT_MEM)
902 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
903 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
904 #define SLJIT_IMM 0x7f
905 #define SLJIT_REG_PAIR(r1, r2) ((r1) | ((r2) << 8))
906
907 /* Macros for checking operand types (only for valid arguments). */
908 #define SLJIT_IS_REG(arg) ((arg) > 0 && (arg) < SLJIT_IMM)
909 #define SLJIT_IS_MEM(arg) ((arg) & SLJIT_MEM)
910 #define SLJIT_IS_MEM0(arg) ((arg) == SLJIT_MEM)
911 #define SLJIT_IS_MEM1(arg) ((arg) > SLJIT_MEM && (arg) < (SLJIT_MEM << 1))
912 #define SLJIT_IS_MEM2(arg) (((arg) & SLJIT_MEM) && (arg) >= (SLJIT_MEM << 1))
913 #define SLJIT_IS_IMM(arg) ((arg) == SLJIT_IMM)
914 #define SLJIT_IS_REG_PAIR(arg) (!((arg) & SLJIT_MEM) && (arg) >= (SLJIT_MEM << 1))
915
916 /* Sets 32 bit operation mode on 64 bit CPUs. This option is ignored on
917 32 bit CPUs. When this option is set for an arithmetic operation, only
918 the lower 32 bits of the input registers are used, and the CPU status
919 flags are set according to the 32 bit result. Although the higher 32 bit
920 of the input and the result registers are not defined by SLJIT, it might
921 be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
922 requirements all source registers must be the result of those operations
923 where this option was also set. Memory loads read 32 bit values rather
924 than 64 bit ones. In other words 32 bit and 64 bit operations cannot be
925 mixed. The only exception is SLJIT_MOV32 which source register can hold
926 any 32 or 64 bit value, and it is converted to a 32 bit compatible format
927 first. When the source and destination registers are the same, this
928 conversion is free (no instructions are emitted) on most CPUs. A 32 bit
929 value can also be converted to a 64 bit value by SLJIT_MOV_S32
930 (sign extension) or SLJIT_MOV_U32 (zero extension).
931
932 As for floating-point operations, this option sets 32 bit single
933 precision mode. Similar to the integer operations, all register arguments
934 must be the result of those operations where this option was also set.
935
936 Note: memory addressing always uses 64 bit values on 64 bit systems so
937 the result of a 32 bit operation must not be used with SLJIT_MEMx
938 macros.
939
940 This option is part of the instruction name, so there is no need to
941 manually set it. E.g:
942
943 SLJIT_ADD32 == (SLJIT_ADD | SLJIT_32) */
944 #define SLJIT_32 0x100
945
946 /* Many CPUs (x86, ARM, PPC) have status flag bits which can be set according
947 to the result of an operation. Other CPUs (MIPS) do not have status
948 flag bits, and results must be stored in registers. To cover both
949 architecture types efficiently only two flags are defined by SLJIT:
950
951 * Zero (equal) flag: it is set if the result is zero
952 * Variable flag: its value is defined by the arithmetic operation
953
954 SLJIT instructions can set any or both of these flags. The value of
955 these flags is undefined if the instruction does not specify their
956 value. The description of each instruction contains the list of
957 allowed flag types.
958
959 Note: the logical or operation can be used to set flags.
960
961 Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
962
963 sljit_op2(..., SLJIT_ADD, ...)
964 Both the zero and variable flags are undefined so they can
965 have any value after the operation is completed.
966
967 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
968 Sets the zero flag if the result is zero, clears it otherwise.
969 The variable flag is undefined.
970
971 sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
972 Sets the variable flag if an integer overflow occurs, clears
973 it otherwise. The zero flag is undefined.
974
975 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
976 Sets the zero flag if the result is zero, clears it otherwise.
977 Sets the variable flag if unsigned overflow (carry) occurs,
978 clears it otherwise.
979
980 Certain instructions (e.g. SLJIT_MOV) does not modify flags, so
981 status flags are unchanged.
982
983 Example:
984
985 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
986 sljit_op1(..., SLJIT_MOV, ...)
987 Zero flag is set according to the result of SLJIT_ADD.
988
989 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
990 sljit_op2(..., SLJIT_ADD, ...)
991 Zero flag has unknown value.
992
993 These flags can be used for code optimization. E.g. a fast loop can be
994 implemented by decreasing a counter register and set the zero flag
995 using a single instruction. The zero register can be used by a
996 conditional jump to restart the loop. A single comparison can set a
997 zero and less flags to check if a value is less, equal, or greater
998 than another value.
999
1000 Motivation: although some CPUs can set a large number of flag bits,
1001 usually their values are ignored or only a few of them are used. Emulating
1002 a large number of flags on systems without a flag register is complicated
1003 so SLJIT instructions must specify the flag they want to use and only
1004 that flag is computed. The last arithmetic instruction can be repeated if
1005 multiple flags need to be checked.
1006 */
1007
1008 /* Set Zero status flag. */
1009 #define SLJIT_SET_Z 0x0200
1010 /* Set the variable status flag if condition is true.
1011 See comparison types (e.g. SLJIT_SET_LESS, SLJIT_SET_F_EQUAL). */
1012 #define SLJIT_SET(condition) ((condition) << 10)
1013
1014 /* Starting index of opcodes for sljit_emit_op0. */
1015 #define SLJIT_OP0_BASE 0
1016
1017 /* Flags: - (does not modify flags)
1018 Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
1019 It falls back to SLJIT_NOP in those cases. */
1020 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
1021 /* Flags: - (does not modify flags)
1022 Note: may or may not cause an extra cycle wait
1023 it can even decrease the runtime in a few cases. */
1024 #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
1025 /* Flags: - (may destroy flags)
1026 Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
1027 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
1028 #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2)
1029 /* Flags: - (may destroy flags)
1030 Signed multiplication of SLJIT_R0 and SLJIT_R1.
1031 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
1032 #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3)
1033 /* Flags: - (may destroy flags)
1034 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
1035 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
1036 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
1037 #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4)
1038 #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_32)
1039 /* Flags: - (may destroy flags)
1040 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
1041 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
1042 Note: if SLJIT_R1 is 0, the behaviour is undefined.
1043 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
1044 the behaviour is undefined. */
1045 #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5)
1046 #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_32)
1047 /* Flags: - (may destroy flags)
1048 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
1049 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
1050 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
1051 #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6)
1052 #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_32)
1053 /* Flags: - (may destroy flags)
1054 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
1055 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
1056 Note: if SLJIT_R1 is 0, the behaviour is undefined.
1057 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
1058 the behaviour is undefined. */
1059 #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7)
1060 #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_32)
1061 /* Flags: - (does not modify flags)
1062 ENDBR32 instruction for x86-32 and ENDBR64 instruction for x86-64
1063 when Intel Control-flow Enforcement Technology (CET) is enabled.
1064 No instructions are emitted for other architectures. */
1065 #define SLJIT_ENDBR (SLJIT_OP0_BASE + 8)
1066 /* Flags: - (may destroy flags)
1067 Skip stack frames before return when Intel Control-flow
1068 Enforcement Technology (CET) is enabled. No instructions
1069 are emitted for other architectures. */
1070 #define SLJIT_SKIP_FRAMES_BEFORE_RETURN (SLJIT_OP0_BASE + 9)
1071
1072 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op);
1073
1074 /* Starting index of opcodes for sljit_emit_op1. */
1075 #define SLJIT_OP1_BASE 32
1076
1077 /* The MOV instruction transfers data from source to destination.
1078
1079 MOV instruction suffixes:
1080
1081 U8 - unsigned 8 bit data transfer
1082 S8 - signed 8 bit data transfer
1083 U16 - unsigned 16 bit data transfer
1084 S16 - signed 16 bit data transfer
1085 U32 - unsigned int (32 bit) data transfer
1086 S32 - signed int (32 bit) data transfer
1087 P - pointer (sljit_p) data transfer
1088 */
1089
1090 /* Flags: - (does not modify flags) */
1091 #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
1092 /* Flags: - (does not modify flags) */
1093 #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1)
1094 #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_32)
1095 /* Flags: - (does not modify flags) */
1096 #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2)
1097 #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_32)
1098 /* Flags: - (does not modify flags) */
1099 #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3)
1100 #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_32)
1101 /* Flags: - (does not modify flags) */
1102 #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4)
1103 #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_32)
1104 /* Flags: - (does not modify flags)
1105 Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
1106 #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5)
1107 /* Flags: - (does not modify flags)
1108 Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
1109 #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6)
1110 /* Flags: - (does not modify flags) */
1111 #define SLJIT_MOV32 (SLJIT_OP1_BASE + 7)
1112 /* Flags: - (does not modify flags)
1113 Note: loads a pointer sized data, useful on x32 mode (a 64 bit mode
1114 on x86-64 which uses 32 bit pointers) or similar compiling modes */
1115 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 8)
1116 /* Count leading zeroes
1117 Flags: - (may destroy flags)
1118 Note: immediate source argument is not supported */
1119 #define SLJIT_CLZ (SLJIT_OP1_BASE + 9)
1120 #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_32)
1121 /* Count trailing zeroes
1122 Flags: - (may destroy flags)
1123 Note: immediate source argument is not supported */
1124 #define SLJIT_CTZ (SLJIT_OP1_BASE + 10)
1125 #define SLJIT_CTZ32 (SLJIT_CTZ | SLJIT_32)
1126 /* Reverse the order of bytes
1127 Flags: - (may destroy flags)
1128 Note: converts between little and big endian formats
1129 Note: immediate source argument is not supported */
1130 #define SLJIT_REV (SLJIT_OP1_BASE + 11)
1131 #define SLJIT_REV32 (SLJIT_REV | SLJIT_32)
1132 /* Reverse the order of bytes in the lower 16 bit and extend as unsigned
1133 Flags: - (may destroy flags)
1134 Note: converts between little and big endian formats
1135 Note: immediate source argument is not supported */
1136 #define SLJIT_REV_U16 (SLJIT_OP1_BASE + 12)
1137 #define SLJIT_REV32_U16 (SLJIT_REV_U16 | SLJIT_32)
1138 /* Reverse the order of bytes in the lower 16 bit and extend as signed
1139 Flags: - (may destroy flags)
1140 Note: converts between little and big endian formats
1141 Note: immediate source argument is not supported */
1142 #define SLJIT_REV_S16 (SLJIT_OP1_BASE + 13)
1143 #define SLJIT_REV32_S16 (SLJIT_REV_S16 | SLJIT_32)
1144 /* Reverse the order of bytes in the lower 32 bit and extend as unsigned
1145 Flags: - (may destroy flags)
1146 Note: converts between little and big endian formats
1147 Note: immediate source argument is not supported */
1148 #define SLJIT_REV_U32 (SLJIT_OP1_BASE + 14)
1149 /* Reverse the order of bytes in the lower 32 bit and extend as signed
1150 Flags: - (may destroy flags)
1151 Note: converts between little and big endian formats
1152 Note: immediate source argument is not supported */
1153 #define SLJIT_REV_S32 (SLJIT_OP1_BASE + 15)
1154
1155 /* The following unary operations are supported by using sljit_emit_op2:
1156 - binary not: SLJIT_XOR with immedate -1 as src1 or src2
1157 - negate: SLJIT_SUB with immedate 0 as src1
1158 Note: these operations are optimized by the compiler if the
1159 target CPU has specialized instruction forms for them. */
1160
1161 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1162 sljit_s32 dst, sljit_sw dstw,
1163 sljit_s32 src, sljit_sw srcw);
1164
1165 /* Starting index of opcodes for sljit_emit_op2. */
1166 #define SLJIT_OP2_BASE 64
1167
1168 /* Flags: Z | OVERFLOW | CARRY */
1169 #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
1170 #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_32)
1171 /* Flags: CARRY */
1172 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
1173 #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_32)
1174 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
1175 SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
1176 SIG_LESS_EQUAL | OVERFLOW | CARRY */
1177 #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
1178 #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_32)
1179 /* Flags: CARRY */
1180 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
1181 #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_32)
1182 /* Note: integer mul
1183 Flags: OVERFLOW */
1184 #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
1185 #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_32)
1186 /* Flags: Z */
1187 #define SLJIT_AND (SLJIT_OP2_BASE + 5)
1188 #define SLJIT_AND32 (SLJIT_AND | SLJIT_32)
1189 /* Flags: Z */
1190 #define SLJIT_OR (SLJIT_OP2_BASE + 6)
1191 #define SLJIT_OR32 (SLJIT_OR | SLJIT_32)
1192 /* Flags: Z */
1193 #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
1194 #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_32)
1195 /* Flags: Z
1196 Let bit_length be the length of the shift operation: 32 or 64.
1197 If src2 is immediate, src2w is masked by (bit_length - 1).
1198 Otherwise, if the content of src2 is outside the range from 0
1199 to bit_length - 1, the result is undefined. */
1200 #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
1201 #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_32)
1202 /* Flags: Z
1203 Same as SLJIT_SHL, except the the second operand is
1204 always masked by the length of the shift operation. */
1205 #define SLJIT_MSHL (SLJIT_OP2_BASE + 9)
1206 #define SLJIT_MSHL32 (SLJIT_MSHL | SLJIT_32)
1207 /* Flags: Z
1208 Let bit_length be the length of the shift operation: 32 or 64.
1209 If src2 is immediate, src2w is masked by (bit_length - 1).
1210 Otherwise, if the content of src2 is outside the range from 0
1211 to bit_length - 1, the result is undefined. */
1212 #define SLJIT_LSHR (SLJIT_OP2_BASE + 10)
1213 #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_32)
1214 /* Flags: Z
1215 Same as SLJIT_LSHR, except the the second operand is
1216 always masked by the length of the shift operation. */
1217 #define SLJIT_MLSHR (SLJIT_OP2_BASE + 11)
1218 #define SLJIT_MLSHR32 (SLJIT_MLSHR | SLJIT_32)
1219 /* Flags: Z
1220 Let bit_length be the length of the shift operation: 32 or 64.
1221 If src2 is immediate, src2w is masked by (bit_length - 1).
1222 Otherwise, if the content of src2 is outside the range from 0
1223 to bit_length - 1, the result is undefined. */
1224 #define SLJIT_ASHR (SLJIT_OP2_BASE + 12)
1225 #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_32)
1226 /* Flags: Z
1227 Same as SLJIT_ASHR, except the the second operand is
1228 always masked by the length of the shift operation. */
1229 #define SLJIT_MASHR (SLJIT_OP2_BASE + 13)
1230 #define SLJIT_MASHR32 (SLJIT_MASHR | SLJIT_32)
1231 /* Flags: - (may destroy flags)
1232 Let bit_length be the length of the rotate operation: 32 or 64.
1233 The second operand is always masked by (bit_length - 1). */
1234 #define SLJIT_ROTL (SLJIT_OP2_BASE + 14)
1235 #define SLJIT_ROTL32 (SLJIT_ROTL | SLJIT_32)
1236 /* Flags: - (may destroy flags)
1237 Let bit_length be the length of the rotate operation: 32 or 64.
1238 The second operand is always masked by (bit_length - 1). */
1239 #define SLJIT_ROTR (SLJIT_OP2_BASE + 15)
1240 #define SLJIT_ROTR32 (SLJIT_ROTR | SLJIT_32)
1241
1242 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1243 sljit_s32 dst, sljit_sw dstw,
1244 sljit_s32 src1, sljit_sw src1w,
1245 sljit_s32 src2, sljit_sw src2w);
1246
1247 /* The sljit_emit_op2u function is the same as sljit_emit_op2
1248 except the result is discarded. */
1249
1250 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1251 sljit_s32 src1, sljit_sw src1w,
1252 sljit_s32 src2, sljit_sw src2w);
1253
1254 /* Emit a left or right shift operation, where the bits shifted
1255 in comes from a separate source operand. All operands are
1256 interpreted as unsigned integers.
1257
1258 In the followings the value_mask variable is 31 for 32 bit
1259 operations and word_size - 1 otherwise.
1260
1261 op must be one of the following operations:
1262 SLJIT_SHL or SLJIT_SHL32:
1263 dst_reg = src1_reg << src3_reg
1264 dst_reg |= ((src2_reg >> 1) >> (src3 ^ value_mask))
1265 SLJIT_MSHL or SLJIT_MSHL32:
1266 src3 &= value_mask
1267 perform the SLJIT_SHL or SLJIT_SHL32 operation
1268 SLJIT_LSHR or SLJIT_LSHR32:
1269 dst_reg = src1_reg >> src3_reg
1270 dst_reg |= ((src2_reg << 1) << (src3 ^ value_mask))
1271 SLJIT_MLSHR or SLJIT_MLSHR32:
1272 src3 &= value_mask
1273 perform the SLJIT_LSHR or SLJIT_LSHR32 operation
1274
1275 op can be combined (or'ed) with SLJIT_SHIFT_INTO_NON_ZERO
1276
1277 dst_reg specifies the destination register, where dst_reg
1278 and src2_reg cannot be the same registers
1279 src1_reg specifies the source register
1280 src2_reg specifies the register which is shifted into src1_reg
1281 src3 / src3w contains the shift amount
1282
1283 Note: a rotate operation is performed if src1_reg and
1284 src2_reg are the same registers
1285
1286 Flags: - (may destroy flags) */
1287
1288 /* The src3 operand contains a non-zero value. Improves
1289 the generated code on certain architectures, which
1290 provides a small performance improvement. */
1291 #define SLJIT_SHIFT_INTO_NON_ZERO 0x200
1292
1293 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_shift_into(struct sljit_compiler *compiler, sljit_s32 op,
1294 sljit_s32 dst_reg,
1295 sljit_s32 src1_reg,
1296 sljit_s32 src2_reg,
1297 sljit_s32 src3, sljit_sw src3w);
1298
1299 /* Starting index of opcodes for sljit_emit_op_src
1300 and sljit_emit_op_dst. */
1301 #define SLJIT_OP_SRC_DST_BASE 96
1302
1303 /* Fast return, see SLJIT_FAST_CALL for more details.
1304 Note: src cannot be an immedate value
1305 Flags: - (does not modify flags) */
1306 #define SLJIT_FAST_RETURN (SLJIT_OP_SRC_DST_BASE + 0)
1307 /* Skip stack frames before fast return.
1308 Note: src cannot be an immedate value
1309 Flags: may destroy flags. */
1310 #define SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN (SLJIT_OP_SRC_DST_BASE + 1)
1311 /* Prefetch value into the level 1 data cache
1312 Note: if the target CPU does not support data prefetch,
1313 no instructions are emitted.
1314 Note: this instruction never fails, even if the memory address is invalid.
1315 Flags: - (does not modify flags) */
1316 #define SLJIT_PREFETCH_L1 (SLJIT_OP_SRC_DST_BASE + 2)
1317 /* Prefetch value into the level 2 data cache
1318 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1319 does not support this instruction form.
1320 Note: this instruction never fails, even if the memory address is invalid.
1321 Flags: - (does not modify flags) */
1322 #define SLJIT_PREFETCH_L2 (SLJIT_OP_SRC_DST_BASE + 3)
1323 /* Prefetch value into the level 3 data cache
1324 Note: same as SLJIT_PREFETCH_L2 if the target CPU
1325 does not support this instruction form.
1326 Note: this instruction never fails, even if the memory address is invalid.
1327 Flags: - (does not modify flags) */
1328 #define SLJIT_PREFETCH_L3 (SLJIT_OP_SRC_DST_BASE + 4)
1329 /* Prefetch a value which is only used once (and can be discarded afterwards)
1330 Note: same as SLJIT_PREFETCH_L1 if the target CPU
1331 does not support this instruction form.
1332 Note: this instruction never fails, even if the memory address is invalid.
1333 Flags: - (does not modify flags) */
1334 #define SLJIT_PREFETCH_ONCE (SLJIT_OP_SRC_DST_BASE + 5)
1335
1336 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1337 sljit_s32 src, sljit_sw srcw);
1338
1339 /* Fast enter, see SLJIT_FAST_CALL for more details.
1340 Flags: - (does not modify flags) */
1341 #define SLJIT_FAST_ENTER (SLJIT_OP_SRC_DST_BASE + 6)
1342
1343 /* Copies the return address into dst. The return address is the
1344 address where the execution continues after the called function
1345 returns (see: sljit_emit_return / sljit_emit_return_void).
1346 Flags: - (does not modify flags) */
1347 #define SLJIT_GET_RETURN_ADDRESS (SLJIT_OP_SRC_DST_BASE + 7)
1348
1349 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_dst(struct sljit_compiler *compiler, sljit_s32 op,
1350 sljit_s32 dst, sljit_sw dstw);
1351
1352 /* Starting index of opcodes for sljit_emit_fop1. */
1353 #define SLJIT_FOP1_BASE 128
1354
1355 /* Flags: - (does not modify flags) */
1356 #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0)
1357 #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_32)
1358 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
1359 SRC/DST TYPE can be: F64, F32, S32, SW
1360 Rounding mode when the destination is SW or S32: round towards zero. */
1361 /* Flags: - (may destroy flags) */
1362 #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1)
1363 #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_32)
1364 /* Flags: - (may destroy flags) */
1365 #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2)
1366 #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_32)
1367 /* Flags: - (may destroy flags) */
1368 #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3)
1369 #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_32)
1370 /* Flags: - (may destroy flags) */
1371 #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4)
1372 #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_32)
1373 /* Flags: - (may destroy flags) */
1374 #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5)
1375 #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_32)
1376 /* Flags: - (may destroy flags) */
1377 #define SLJIT_CONV_F64_FROM_UW (SLJIT_FOP1_BASE + 6)
1378 #define SLJIT_CONV_F32_FROM_UW (SLJIT_CONV_F64_FROM_UW | SLJIT_32)
1379 /* Flags: - (may destroy flags) */
1380 #define SLJIT_CONV_F64_FROM_U32 (SLJIT_FOP1_BASE + 7)
1381 #define SLJIT_CONV_F32_FROM_U32 (SLJIT_CONV_F64_FROM_U32 | SLJIT_32)
1382 /* Note: dst is the left and src is the right operand for SLJIT_CMP_F32/64.
1383 Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
1384 #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 8)
1385 #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_32)
1386 /* Flags: - (may destroy flags) */
1387 #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 9)
1388 #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_32)
1389 /* Flags: - (may destroy flags) */
1390 #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 10)
1391 #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_32)
1392
1393 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1394 sljit_s32 dst, sljit_sw dstw,
1395 sljit_s32 src, sljit_sw srcw);
1396
1397 /* Starting index of opcodes for sljit_emit_fop2. */
1398 #define SLJIT_FOP2_BASE 160
1399
1400 /* Flags: - (may destroy flags) */
1401 #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0)
1402 #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_32)
1403 /* Flags: - (may destroy flags) */
1404 #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1)
1405 #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_32)
1406 /* Flags: - (may destroy flags) */
1407 #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2)
1408 #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_32)
1409 /* Flags: - (may destroy flags) */
1410 #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3)
1411 #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_32)
1412
1413 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1414 sljit_s32 dst, sljit_sw dstw,
1415 sljit_s32 src1, sljit_sw src1w,
1416 sljit_s32 src2, sljit_sw src2w);
1417
1418 /* Starting index of opcodes for sljit_emit_fop2r. */
1419 #define SLJIT_FOP2R_BASE 168
1420
1421 /* Flags: - (may destroy flags) */
1422 #define SLJIT_COPYSIGN_F64 (SLJIT_FOP2R_BASE + 0)
1423 #define SLJIT_COPYSIGN_F32 (SLJIT_COPYSIGN_F64 | SLJIT_32)
1424
1425 /* Similar to sljit_emit_fop2, except the destination is always a register. */
1426 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2r(struct sljit_compiler *compiler, sljit_s32 op,
1427 sljit_s32 dst_freg,
1428 sljit_s32 src1, sljit_sw src1w,
1429 sljit_s32 src2, sljit_sw src2w);
1430
1431 /* Sets a floating point register to an immediate value. */
1432
1433 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset32(struct sljit_compiler *compiler,
1434 sljit_s32 freg, sljit_f32 value);
1435 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
1436 sljit_s32 freg, sljit_f64 value);
1437
1438 /* The following opcodes are used by sljit_emit_fcopy(). */
1439
1440 /* 64 bit: copy a 64 bit value from an integer register into a
1441 64 bit floating point register without any modifications.
1442 32 bit: copy a 32 bit register or register pair into a 64 bit
1443 floating point register without any modifications. The
1444 register, or the first register of the register pair
1445 replaces the high order 32 bit of the floating point
1446 register. If a register pair is passed, the low
1447 order 32 bit is replaced by the second register.
1448 Otherwise, the low order 32 bit is unchanged. */
1449 #define SLJIT_COPY_TO_F64 1
1450 /* Copy a 32 bit value from an integer register into a 32 bit
1451 floating point register without any modifications. */
1452 #define SLJIT_COPY32_TO_F32 (SLJIT_COPY_TO_F64 | SLJIT_32)
1453 /* 64 bit: copy the value of a 64 bit floating point register into
1454 an integer register without any modifications.
1455 32 bit: copy a 64 bit floating point register into a 32 bit register
1456 or a 32 bit register pair without any modifications. The
1457 high order 32 bit of the floating point register is copied
1458 into the register, or the first register of the register
1459 pair. If a register pair is passed, the low order 32 bit
1460 is copied into the second register. */
1461 #define SLJIT_COPY_FROM_F64 2
1462 /* Copy the value of a 32 bit floating point register into an integer
1463 register without any modifications. The register should be processed
1464 with 32 bit operations later. */
1465 #define SLJIT_COPY32_FROM_F32 (SLJIT_COPY_FROM_F64 | SLJIT_32)
1466
1467 /* Special data copy which involves floating point registers.
1468
1469 op must be between SLJIT_COPY_TO_F64 and SLJIT_COPY32_FROM_F32
1470 freg must be a floating point register
1471 reg must be a register or register pair */
1472
1473 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
1474 sljit_s32 freg, sljit_s32 reg);
1475
1476 /* Label and jump instructions. */
1477
1478 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
1479
1480 /* The SLJIT_FAST_CALL is a calling method for creating lightweight function
1481 calls. This type of calls preserve the values of all registers and stack
1482 frame. Unlike normal function calls, the enter and return operations must
1483 be performed by the SLJIT_FAST_ENTER and SLJIT_FAST_RETURN operations
1484 respectively. The return address is stored in the dst argument of the
1485 SLJIT_FAST_ENTER operation, and this return address should be passed as
1486 the src argument for the SLJIT_FAST_RETURN operation to return from the
1487 called function.
1488
1489 Fast calls are cheap operations (usually only a single call instruction is
1490 emitted) but they do not preserve any registers. However the callee function
1491 can freely use / update any registers and the locals area which can be
1492 efficiently exploited by various optimizations. Registers can be saved
1493 and restored manually if needed.
1494
1495 Although returning to different address by SLJIT_FAST_RETURN is possible,
1496 this address usually cannot be predicted by the return address predictor of
1497 modern CPUs which may reduce performance. Furthermore certain security
1498 enhancement technologies such as Intel Control-flow Enforcement Technology
1499 (CET) may disallow returning to a different address (indirect jumps
1500 can be used instead, see SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN). */
1501
1502 /* Invert (negate) conditional type: xor (^) with 0x1 */
1503
1504 /* Integer comparison types. */
1505 #define SLJIT_EQUAL 0
1506 #define SLJIT_ZERO SLJIT_EQUAL
1507 #define SLJIT_NOT_EQUAL 1
1508 #define SLJIT_NOT_ZERO SLJIT_NOT_EQUAL
1509
1510 #define SLJIT_LESS 2
1511 #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS)
1512 #define SLJIT_GREATER_EQUAL 3
1513 #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_LESS)
1514 #define SLJIT_GREATER 4
1515 #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER)
1516 #define SLJIT_LESS_EQUAL 5
1517 #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_GREATER)
1518 #define SLJIT_SIG_LESS 6
1519 #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS)
1520 #define SLJIT_SIG_GREATER_EQUAL 7
1521 #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SIG_LESS)
1522 #define SLJIT_SIG_GREATER 8
1523 #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER)
1524 #define SLJIT_SIG_LESS_EQUAL 9
1525 #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_GREATER)
1526
1527 #define SLJIT_OVERFLOW 10
1528 #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW)
1529 #define SLJIT_NOT_OVERFLOW 11
1530
1531 /* Unlike other flags, sljit_emit_jump may destroy the carry flag. */
1532 #define SLJIT_CARRY 12
1533 #define SLJIT_SET_CARRY SLJIT_SET(SLJIT_CARRY)
1534 #define SLJIT_NOT_CARRY 13
1535
1536 #define SLJIT_ATOMIC_STORED 14
1537 #define SLJIT_SET_ATOMIC_STORED SLJIT_SET(SLJIT_ATOMIC_STORED)
1538 #define SLJIT_ATOMIC_NOT_STORED 15
1539
1540 /* Basic floating point comparison types.
1541
1542 Note: when the comparison result is unordered, their behaviour is unspecified. */
1543
1544 #define SLJIT_F_EQUAL 16
1545 #define SLJIT_SET_F_EQUAL SLJIT_SET(SLJIT_F_EQUAL)
1546 #define SLJIT_F_NOT_EQUAL 17
1547 #define SLJIT_SET_F_NOT_EQUAL SLJIT_SET(SLJIT_F_EQUAL)
1548 #define SLJIT_F_LESS 18
1549 #define SLJIT_SET_F_LESS SLJIT_SET(SLJIT_F_LESS)
1550 #define SLJIT_F_GREATER_EQUAL 19
1551 #define SLJIT_SET_F_GREATER_EQUAL SLJIT_SET(SLJIT_F_LESS)
1552 #define SLJIT_F_GREATER 20
1553 #define SLJIT_SET_F_GREATER SLJIT_SET(SLJIT_F_GREATER)
1554 #define SLJIT_F_LESS_EQUAL 21
1555 #define SLJIT_SET_F_LESS_EQUAL SLJIT_SET(SLJIT_F_GREATER)
1556
1557 /* Jumps when either argument contains a NaN value. */
1558 #define SLJIT_UNORDERED 22
1559 #define SLJIT_SET_UNORDERED SLJIT_SET(SLJIT_UNORDERED)
1560 /* Jumps when neither argument contains a NaN value. */
1561 #define SLJIT_ORDERED 23
1562 #define SLJIT_SET_ORDERED SLJIT_SET(SLJIT_UNORDERED)
1563
1564 /* Ordered / unordered floating point comparison types.
1565
1566 Note: each comparison type has an ordered and unordered form. Some
1567 architectures supports only either of them (see: sljit_cmp_info). */
1568
1569 #define SLJIT_ORDERED_EQUAL 24
1570 #define SLJIT_SET_ORDERED_EQUAL SLJIT_SET(SLJIT_ORDERED_EQUAL)
1571 #define SLJIT_UNORDERED_OR_NOT_EQUAL 25
1572 #define SLJIT_SET_UNORDERED_OR_NOT_EQUAL SLJIT_SET(SLJIT_ORDERED_EQUAL)
1573 #define SLJIT_ORDERED_LESS 26
1574 #define SLJIT_SET_ORDERED_LESS SLJIT_SET(SLJIT_ORDERED_LESS)
1575 #define SLJIT_UNORDERED_OR_GREATER_EQUAL 27
1576 #define SLJIT_SET_UNORDERED_OR_GREATER_EQUAL SLJIT_SET(SLJIT_ORDERED_LESS)
1577 #define SLJIT_ORDERED_GREATER 28
1578 #define SLJIT_SET_ORDERED_GREATER SLJIT_SET(SLJIT_ORDERED_GREATER)
1579 #define SLJIT_UNORDERED_OR_LESS_EQUAL 29
1580 #define SLJIT_SET_UNORDERED_OR_LESS_EQUAL SLJIT_SET(SLJIT_ORDERED_GREATER)
1581
1582 #define SLJIT_UNORDERED_OR_EQUAL 30
1583 #define SLJIT_SET_UNORDERED_OR_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_EQUAL)
1584 #define SLJIT_ORDERED_NOT_EQUAL 31
1585 #define SLJIT_SET_ORDERED_NOT_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_EQUAL)
1586 #define SLJIT_UNORDERED_OR_LESS 32
1587 #define SLJIT_SET_UNORDERED_OR_LESS SLJIT_SET(SLJIT_UNORDERED_OR_LESS)
1588 #define SLJIT_ORDERED_GREATER_EQUAL 33
1589 #define SLJIT_SET_ORDERED_GREATER_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_LESS)
1590 #define SLJIT_UNORDERED_OR_GREATER 34
1591 #define SLJIT_SET_UNORDERED_OR_GREATER SLJIT_SET(SLJIT_UNORDERED_OR_GREATER)
1592 #define SLJIT_ORDERED_LESS_EQUAL 35
1593 #define SLJIT_SET_ORDERED_LESS_EQUAL SLJIT_SET(SLJIT_UNORDERED_OR_GREATER)
1594
1595 /* Unconditional jump types. */
1596 #define SLJIT_JUMP 36
1597 /* Fast calling method. See the description above. */
1598 #define SLJIT_FAST_CALL 37
1599 /* Default C calling convention. */
1600 #define SLJIT_CALL 38
1601 /* Called function must be compiled by SLJIT.
1602 See SLJIT_ENTER_REG_ARG option. */
1603 #define SLJIT_CALL_REG_ARG 39
1604
1605 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1606 #define SLJIT_REWRITABLE_JUMP 0x1000
1607 /* When this flag is passed, the execution of the current function ends and
1608 the called function returns to the caller of the current function. The
1609 stack usage is reduced before the call, but it is not necessarily reduced
1610 to zero. In the latter case the compiler needs to allocate space for some
1611 arguments and the return address must be stored on the stack as well. */
1612 #define SLJIT_CALL_RETURN 0x2000
1613
1614 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1615 type must be between SLJIT_EQUAL and SLJIT_FAST_CALL
1616 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1617
1618 Flags: does not modify flags. */
1619 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type);
1620
1621 /* Emit a C compiler (ABI) compatible function call.
1622 type must be SLJIT_CALL or SLJIT_CALL_REG_ARG
1623 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP and/or SLJIT_CALL_RETURN
1624 arg_types can be specified by SLJIT_ARGSx (SLJIT_ARG_RETURN / SLJIT_ARG_VALUE) macros
1625
1626 Flags: destroy all flags. */
1627 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 arg_types);
1628
1629 /* Basic arithmetic comparison. In most architectures it is implemented as
1630 a compare operation followed by a sljit_emit_jump. However some
1631 architectures (i.e: ARM64 or MIPS) may employ special optimizations
1632 here. It is suggested to use this comparison form when appropriate.
1633 type must be between SLJIT_EQUAL and SLJIT_SIG_LESS_EQUAL
1634 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1635
1636 Flags: may destroy flags. */
1637 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
1638 sljit_s32 src1, sljit_sw src1w,
1639 sljit_s32 src2, sljit_sw src2w);
1640
1641 /* Basic floating point comparison. In most architectures it is implemented as
1642 a SLJIT_CMP_F32/64 operation (setting appropriate flags) followed by a
1643 sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1644 special optimizations here. It is suggested to use this comparison form
1645 when appropriate.
1646 type must be between SLJIT_F_EQUAL and SLJIT_ORDERED_LESS_EQUAL
1647 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1648 Flags: destroy flags.
1649 Note: when an operand is NaN the behaviour depends on the comparison type. */
1650 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_s32 type,
1651 sljit_s32 src1, sljit_sw src1w,
1652 sljit_s32 src2, sljit_sw src2w);
1653
1654 /* Set the destination of the jump to this label. */
1655 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
1656 /* Set the destination address of the jump to this label. */
1657 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
1658
1659 /* Emit an indirect jump or fast call.
1660 Direct form: set src to SLJIT_IMM() and srcw to the address
1661 Indirect form: any other valid addressing mode
1662 type must be between SLJIT_JUMP and SLJIT_FAST_CALL
1663
1664 Flags: does not modify flags. */
1665 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw);
1666
1667 /* Emit a C compiler (ABI) compatible function call.
1668 Direct form: set src to SLJIT_IMM() and srcw to the address
1669 Indirect form: any other valid addressing mode
1670 type must be SLJIT_CALL or SLJIT_CALL_REG_ARG
1671 type can be combined (or'ed) with SLJIT_CALL_RETURN
1672 arg_types can be specified by SLJIT_ARGSx (SLJIT_ARG_RETURN / SLJIT_ARG_VALUE) macros
1673
1674 Flags: destroy all flags. */
1675 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);
1676
1677 /* Perform an operation using the conditional flags as the second argument.
1678 Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_LESS_EQUAL.
1679 The value represented by the type is 1, if the condition represented
1680 by the type is fulfilled, and 0 otherwise.
1681
1682 When op is SLJIT_MOV or SLJIT_MOV32:
1683 Set dst to the value represented by the type (0 or 1).
1684 Flags: - (does not modify flags)
1685 When op is SLJIT_AND, SLJIT_AND32, SLJIT_OR, SLJIT_OR32, SLJIT_XOR, or SLJIT_XOR32
1686 Performs the binary operation using dst as the first, and the value
1687 represented by type as the second argument. Result is written into dst.
1688 Flags: Z (may destroy flags) */
1689 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1690 sljit_s32 dst, sljit_sw dstw,
1691 sljit_s32 type);
1692
1693 /* Emit a conditional select instruction which moves src1 to dst_reg,
1694 if the condition is satisfied, or src2_reg to dst_reg otherwise.
1695
1696 type must be between SLJIT_EQUAL and SLJIT_ORDERED_LESS_EQUAL
1697 type can be combined (or'ed) with SLJIT_32 to move 32 bit
1698 register values instead of word sized ones
1699 dst_reg and src2_reg must be valid registers
1700 src1 must be valid operand
1701
1702 Note: if src1 is a memory operand, its value
1703 might be loaded even if the condition is false.
1704
1705 Flags: - (does not modify flags) */
1706 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_select(struct sljit_compiler *compiler, sljit_s32 type,
1707 sljit_s32 dst_reg,
1708 sljit_s32 src1, sljit_sw src1w,
1709 sljit_s32 src2_reg);
1710
1711 /* Emit a conditional floating point select instruction which moves
1712 src1 to dst_reg, if the condition is satisfied, or src2_reg to
1713 dst_reg otherwise.
1714
1715 type must be between SLJIT_EQUAL and SLJIT_ORDERED_LESS_EQUAL
1716 type can be combined (or'ed) with SLJIT_32 to move 32 bit
1717 floating point values instead of 64 bit ones
1718 dst_freg and src2_freg must be valid floating point registers
1719 src1 must be valid operand
1720
1721 Note: if src1 is a memory operand, its value
1722 might be loaded even if the condition is false.
1723
1724 Flags: - (does not modify flags) */
1725 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fselect(struct sljit_compiler *compiler, sljit_s32 type,
1726 sljit_s32 dst_freg,
1727 sljit_s32 src1, sljit_sw src1w,
1728 sljit_s32 src2_freg);
1729
1730 /* The following flags are used by sljit_emit_mem(), sljit_emit_mem_update(),
1731 sljit_emit_fmem(), and sljit_emit_fmem_update(). */
1732
1733 /* Memory load operation. This is the default. */
1734 #define SLJIT_MEM_LOAD 0x000000
1735 /* Memory store operation. */
1736 #define SLJIT_MEM_STORE 0x000200
1737
1738 /* The following flags are used by sljit_emit_mem() and sljit_emit_fmem(). */
1739
1740 /* Load or stora data from an unaligned (byte aligned) address. */
1741 #define SLJIT_MEM_UNALIGNED 0x000400
1742 /* Load or stora data from a 16 bit aligned address. */
1743 #define SLJIT_MEM_ALIGNED_16 0x000800
1744 /* Load or stora data from a 32 bit aligned address. */
1745 #define SLJIT_MEM_ALIGNED_32 0x001000
1746
1747 /* The following flags are used by sljit_emit_mem_update(),
1748 and sljit_emit_fmem_update(). */
1749
1750 /* Base register is updated before the memory access (default). */
1751 #define SLJIT_MEM_PRE 0x000000
1752 /* Base register is updated after the memory access. */
1753 #define SLJIT_MEM_POST 0x000400
1754
1755 /* When SLJIT_MEM_SUPP is passed, no instructions are emitted.
1756 Instead the function returns with SLJIT_SUCCESS if the instruction
1757 form is supported and SLJIT_ERR_UNSUPPORTED otherwise. This flag
1758 allows runtime checking of available instruction forms. */
1759 #define SLJIT_MEM_SUPP 0x000800
1760
1761 /* The sljit_emit_mem emits instructions for various memory operations:
1762
1763 When SLJIT_MEM_UNALIGNED / SLJIT_MEM_ALIGNED_16 /
1764 SLJIT_MEM_ALIGNED_32 is set in type argument:
1765 Emit instructions for unaligned memory loads or stores. When
1766 SLJIT_UNALIGNED is not defined, the only way to access unaligned
1767 memory data is using sljit_emit_mem. Otherwise all operations (e.g.
1768 sljit_emit_op1/2, or sljit_emit_fop1/2) supports unaligned access.
1769 In general, the performance of unaligned memory accesses are often
1770 lower than aligned and should be avoided.
1771
1772 When a pair of registers is passed in reg argument:
1773 Emit instructions for moving data between a register pair and
1774 memory. The register pair can be specified by the SLJIT_REG_PAIR
1775 macro. The first register is loaded from or stored into the
1776 location specified by the mem/memw arguments, and the end address
1777 of this operation is the starting address of the data transfer
1778 between the second register and memory. The type argument must
1779 be SLJIT_MOV. The SLJIT_MEM_UNALIGNED / SLJIT_MEM_ALIGNED_*
1780 options are allowed for this operation.
1781
1782 type must be between SLJIT_MOV and SLJIT_MOV_P and can be
1783 combined (or'ed) with SLJIT_MEM_* flags
1784 reg is a register or register pair, which is the source or
1785 destination of the operation
1786 mem must be a memory operand
1787
1788 Flags: - (does not modify flags) */
1789 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1790 sljit_s32 reg,
1791 sljit_s32 mem, sljit_sw memw);
1792
1793 /* Emit a single memory load or store with update instruction.
1794 When the requested instruction form is not supported by the CPU,
1795 it returns with SLJIT_ERR_UNSUPPORTED instead of emulating the
1796 instruction. This allows specializing tight loops based on
1797 the supported instruction forms (see SLJIT_MEM_SUPP flag).
1798 Absolute address (SLJIT_MEM0) forms are never supported
1799 and the base (first) register specified by the mem argument
1800 must not be SLJIT_SP and must also be different from the
1801 register specified by the reg argument.
1802
1803 type must be between SLJIT_MOV and SLJIT_MOV_P and can be
1804 combined (or'ed) with SLJIT_MEM_* flags
1805 reg is the source or destination register of the operation
1806 mem must be a memory operand
1807
1808 Flags: - (does not modify flags) */
1809
1810 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem_update(struct sljit_compiler *compiler, sljit_s32 type,
1811 sljit_s32 reg,
1812 sljit_s32 mem, sljit_sw memw);
1813
1814 /* Same as sljit_emit_mem except the followings:
1815
1816 Loading or storing a pair of registers is not supported.
1817
1818 type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
1819 combined (or'ed) with SLJIT_MEM_* flags.
1820 freg is the source or destination floating point register
1821 of the operation
1822 mem must be a memory operand
1823
1824 Flags: - (does not modify flags) */
1825
1826 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
1827 sljit_s32 freg,
1828 sljit_s32 mem, sljit_sw memw);
1829
1830 /* Same as sljit_emit_mem_update except the followings:
1831
1832 type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
1833 combined (or'ed) with SLJIT_MEM_* flags
1834 freg is the source or destination floating point register
1835 of the operation
1836 mem must be a memory operand
1837
1838 Flags: - (does not modify flags) */
1839
1840 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem_update(struct sljit_compiler *compiler, sljit_s32 type,
1841 sljit_s32 freg,
1842 sljit_s32 mem, sljit_sw memw);
1843
1844 /* The following options are used by several simd operations. */
1845
1846 /* Load data into a simd register, this is the default */
1847 #define SLJIT_SIMD_LOAD 0x000000
1848 /* Store data from a simd register */
1849 #define SLJIT_SIMD_STORE 0x000001
1850 /* The simd register contains floating point values */
1851 #define SLJIT_SIMD_FLOAT 0x000400
1852 /* Tests whether the operation is available */
1853 #define SLJIT_SIMD_TEST 0x000800
1854 /* Move data to/from a 64 bit (8 byte) long SIMD register */
1855 #define SLJIT_SIMD_REG_64 (3 << 12)
1856 /* Move data to/from a 128 bit (16 byte) long SIMD register */
1857 #define SLJIT_SIMD_REG_128 (4 << 12)
1858 /* Move data to/from a 256 bit (32 byte) long SIMD register */
1859 #define SLJIT_SIMD_REG_256 (5 << 12)
1860 /* Move data to/from a 512 bit (64 byte) long SIMD register */
1861 #define SLJIT_SIMD_REG_512 (6 << 12)
1862 /* Element size is 8 bit long (this is the default), usually cannot be combined with SLJIT_SIMD_FLOAT */
1863 #define SLJIT_SIMD_ELEM_8 (0 << 18)
1864 /* Element size is 16 bit long, usually cannot be combined with SLJIT_SIMD_FLOAT */
1865 #define SLJIT_SIMD_ELEM_16 (1 << 18)
1866 /* Element size is 32 bit long */
1867 #define SLJIT_SIMD_ELEM_32 (2 << 18)
1868 /* Element size is 64 bit long */
1869 #define SLJIT_SIMD_ELEM_64 (3 << 18)
1870 /* Element size is 128 bit long */
1871 #define SLJIT_SIMD_ELEM_128 (4 << 18)
1872 /* Element size is 256 bit long */
1873 #define SLJIT_SIMD_ELEM_256 (5 << 18)
1874
1875 /* The following options are used by sljit_emit_simd_mov(). */
1876
1877 /* Memory address is unaligned (this is the default) */
1878 #define SLJIT_SIMD_MEM_UNALIGNED (0 << 24)
1879 /* Memory address is 16 bit aligned */
1880 #define SLJIT_SIMD_MEM_ALIGNED_16 (1 << 24)
1881 /* Memory address is 32 bit aligned */
1882 #define SLJIT_SIMD_MEM_ALIGNED_32 (2 << 24)
1883 /* Memory address is 64 bit aligned */
1884 #define SLJIT_SIMD_MEM_ALIGNED_64 (3 << 24)
1885 /* Memory address is 128 bit aligned */
1886 #define SLJIT_SIMD_MEM_ALIGNED_128 (4 << 24)
1887 /* Memory address is 256 bit aligned */
1888 #define SLJIT_SIMD_MEM_ALIGNED_256 (5 << 24)
1889 /* Memory address is 512 bit aligned */
1890 #define SLJIT_SIMD_MEM_ALIGNED_512 (6 << 24)
1891
1892 /* Moves data between a simd register and memory.
1893
1894 If the operation is not supported, it returns with
1895 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
1896 it does not emit any instructions.
1897
1898 type must be a combination of SLJIT_SIMD_* and
1899 SLJIT_SIMD_MEM_* options
1900 freg is the source or destination simd register
1901 of the operation
1902 srcdst must be a memory operand or a simd register
1903
1904 Note:
1905 The alignment and element size must be
1906 less or equal than simd register size.
1907
1908 Flags: - (does not modify flags) */
1909
1910 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_mov(struct sljit_compiler *compiler, sljit_s32 type,
1911 sljit_s32 freg,
1912 sljit_s32 srcdst, sljit_sw srcdstw);
1913
1914 /* Replicates a scalar value to all lanes of a simd
1915 register.
1916
1917 If the operation is not supported, it returns with
1918 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
1919 it does not emit any instructions.
1920
1921 type must be a combination of SLJIT_SIMD_* options
1922 except SLJIT_SIMD_STORE.
1923 freg is the destination simd register of the operation
1924 src is the value which is replicated
1925
1926 Note:
1927 The src == SLJIT_IMM and srcw == 0 can be used to
1928 clear a register even when SLJIT_SIMD_FLOAT is set.
1929
1930 Flags: - (does not modify flags) */
1931
1932 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_replicate(struct sljit_compiler *compiler, sljit_s32 type,
1933 sljit_s32 freg,
1934 sljit_s32 src, sljit_sw srcw);
1935
1936 /* The following options are used by sljit_emit_simd_lane_mov(). */
1937
1938 /* Clear all bits of the simd register before loading the lane. */
1939 #define SLJIT_SIMD_LANE_ZERO 0x000002
1940 /* Sign extend the integer value stored from the lane. */
1941 #define SLJIT_SIMD_LANE_SIGNED 0x000004
1942
1943 /* Moves data between a simd register lane and a register or
1944 memory. If the srcdst argument is a register, it must be
1945 a floating point register when SLJIT_SIMD_FLOAT is specified,
1946 or a general purpose register otherwise.
1947
1948 If the operation is not supported, it returns with
1949 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
1950 it does not emit any instructions.
1951
1952 type must be a combination of SLJIT_SIMD_* options
1953 Further options:
1954 SLJIT_32 - when SLJIT_SIMD_FLOAT is not set
1955 SLJIT_SIMD_LANE_SIGNED - when SLJIT_SIMD_STORE
1956 is set and SLJIT_SIMD_FLOAT is not set
1957 SLJIT_SIMD_LANE_ZERO - when SLJIT_SIMD_LOAD
1958 is specified
1959 freg is the source or destination simd register
1960 of the operation
1961 lane_index is the index of the lane
1962 srcdst is the destination operand for loads, and
1963 source operand for stores
1964
1965 Note:
1966 The elem size must be lower than register size.
1967
1968 Flags: - (does not modify flags) */
1969
1970 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_lane_mov(struct sljit_compiler *compiler, sljit_s32 type,
1971 sljit_s32 freg, sljit_s32 lane_index,
1972 sljit_s32 srcdst, sljit_sw srcdstw);
1973
1974 /* Replicates a scalar value from a lane to all lanes
1975 of a simd register.
1976
1977 If the operation is not supported, it returns with
1978 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
1979 it does not emit any instructions.
1980
1981 type must be a combination of SLJIT_SIMD_* options
1982 except SLJIT_SIMD_STORE.
1983 freg is the destination simd register of the operation
1984 src is the simd register which lane is replicated
1985 src_lane_index is the lane index of the src register
1986
1987 Flags: - (does not modify flags) */
1988
1989 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_lane_replicate(struct sljit_compiler *compiler, sljit_s32 type,
1990 sljit_s32 freg,
1991 sljit_s32 src, sljit_s32 src_lane_index);
1992
1993 /* The following options are used by sljit_emit_simd_load_extend(). */
1994
1995 /* Sign extend the integer elements */
1996 #define SLJIT_SIMD_EXTEND_SIGNED 0x000002
1997 /* Extend data to 16 bit */
1998 #define SLJIT_SIMD_EXTEND_16 (1 << 24)
1999 /* Extend data to 32 bit */
2000 #define SLJIT_SIMD_EXTEND_32 (2 << 24)
2001 /* Extend data to 64 bit */
2002 #define SLJIT_SIMD_EXTEND_64 (3 << 24)
2003
2004 /* Extend elements and stores them in a simd register.
2005 The extension operation increases the size of the
2006 elements (e.g. from 16 bit to 64 bit). For integer
2007 values, the extension can be signed or unsigned.
2008
2009 If the operation is not supported, it returns with
2010 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
2011 it does not emit any instructions.
2012
2013 type must be a combination of SLJIT_SIMD_*, and
2014 SLJIT_SIMD_EXTEND_* options except SLJIT_SIMD_STORE
2015 freg is the destination simd register of the operation
2016 src must be a memory operand or a simd register.
2017 In the latter case, the source elements are stored
2018 in the lower half of the register.
2019
2020 Flags: - (does not modify flags) */
2021
2022 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_extend(struct sljit_compiler *compiler, sljit_s32 type,
2023 sljit_s32 freg,
2024 sljit_s32 src, sljit_sw srcw);
2025
2026 /* Extract the highest bit (usually the sign bit) from
2027 each elements of a vector.
2028
2029 If the operation is not supported, it returns with
2030 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
2031 it does not emit any instructions.
2032
2033 type must be a combination of SLJIT_SIMD_* and SLJIT_32
2034 options except SLJIT_SIMD_LOAD
2035 freg is the source simd register of the operation
2036 dst is the destination operand
2037
2038 Flags: - (does not modify flags) */
2039
2040 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_sign(struct sljit_compiler *compiler, sljit_s32 type,
2041 sljit_s32 freg,
2042 sljit_s32 dst, sljit_sw dstw);
2043
2044 /* The following options are used by sljit_emit_simd_op2(). */
2045
2046 /* Binary 'and' operation */
2047 #define SLJIT_SIMD_OP2_AND 0x000001
2048 /* Binary 'or' operation */
2049 #define SLJIT_SIMD_OP2_OR 0x000002
2050 /* Binary 'xor' operation */
2051 #define SLJIT_SIMD_OP2_XOR 0x000003
2052
2053 /* Perform simd operations using simd registers.
2054
2055 If the operation is not supported, it returns with
2056 SLJIT_ERR_UNSUPPORTED. If SLJIT_SIMD_TEST is passed,
2057 it does not emit any instructions.
2058
2059 type must be a combination of SLJIT_SIMD_* and SLJIT_SIMD_OP2_
2060 options except SLJIT_SIMD_LOAD and SLJIT_SIMD_STORE
2061 dst_freg is the destination register of the operation
2062 src1_freg is the first source register of the operation
2063 src1_freg is the second source register of the operation
2064
2065 Flags: - (does not modify flags) */
2066
2067 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_simd_op2(struct sljit_compiler *compiler, sljit_s32 type,
2068 sljit_s32 dst_freg, sljit_s32 src1_freg, sljit_s32 src2_freg);
2069
2070 /* The sljit_emit_atomic_load and sljit_emit_atomic_store operation pair
2071 can perform an atomic read-modify-write operation. First, an unsigned
2072 value must be loaded from memory using sljit_emit_atomic_load. Then,
2073 the updated value must be written back to the same memory location by
2074 sljit_emit_atomic_store. A thread can only perform a single atomic
2075 operation at a time.
2076
2077 Note: atomic operations are experimental, and not implemented
2078 for all cpus.
2079
2080 The following conditions must be satisfied, or the operation
2081 is undefined:
2082 - the address provided in mem_reg must be divisible by the size of
2083 the value (only naturally aligned updates are supported)
2084 - no memory writes are allowed between the load and store operations
2085 regardless of its target address (currently read operations are
2086 allowed, but this might change in the future)
2087 - the memory operation (op) and the base address (stored in mem_reg)
2088 passed to the load/store operations must be the same (the mem_reg
2089 can be a different register, only its value must be the same)
2090 - an store must always follow a load for the same transaction.
2091
2092 op must be between SLJIT_MOV and SLJIT_MOV_P, excluding all
2093 signed loads such as SLJIT_MOV32_S16
2094 dst_reg is the register where the data will be loaded into
2095 mem_reg is the base address of the memory load (it cannot be
2096 SLJIT_SP or a virtual register on x86-32)
2097
2098 Flags: - (does not modify flags) */
2099 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_atomic_load(struct sljit_compiler *compiler, sljit_s32 op,
2100 sljit_s32 dst_reg,
2101 sljit_s32 mem_reg);
2102
2103 /* The sljit_emit_atomic_load and sljit_emit_atomic_store operations
2104 allows performing an atomic read-modify-write operation. See the
2105 description of sljit_emit_atomic_load.
2106
2107 op must be between SLJIT_MOV and SLJIT_MOV_P, excluding all signed
2108 loads such as SLJIT_MOV32_S16
2109 src_reg is the register which value is stored into the memory
2110 mem_reg is the base address of the memory store (it cannot be
2111 SLJIT_SP or a virtual register on x86-32)
2112 temp_reg is a not preserved scratch register, which must be
2113 initialized with the value loaded into the dst_reg during the
2114 corresponding sljit_emit_atomic_load operation, or the operation
2115 is undefined
2116
2117 Flags: ATOMIC_STORED is set if the operation is successful,
2118 otherwise the memory remains unchanged. */
2119 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_atomic_store(struct sljit_compiler *compiler, sljit_s32 op,
2120 sljit_s32 src_reg,
2121 sljit_s32 mem_reg,
2122 sljit_s32 temp_reg);
2123
2124 /* Copies the base address of SLJIT_SP + offset to dst. The offset can
2125 represent the starting address of a value in the local data (stack).
2126 The offset is not limited by the local data limits, it can be any value.
2127 For example if an array of bytes are stored on the stack from
2128 offset 0x40, and R0 contains the offset of an array item plus 0x120,
2129 this item can be changed by two SLJIT instructions:
2130
2131 sljit_get_local_base(compiler, SLJIT_R1, 0, 0x40 - 0x120);
2132 sljit_emit_op1(compiler, SLJIT_MOV_U8, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_IMM, 0x5);
2133
2134 Flags: - (may destroy flags) */
2135 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset);
2136
2137 /* Store a value that can be changed runtime (see: sljit_get_const_addr / sljit_set_const)
2138 Flags: - (does not modify flags) */
2139 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value);
2140
2141 /* Store the value of a label (see: sljit_set_put_label)
2142 Flags: - (does not modify flags) */
2143 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw);
2144
2145 /* Set the value stored by put_label to this label. */
2146 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_put_label(struct sljit_put_label *put_label, struct sljit_label *label);
2147
2148 /* After the code generation the address for label, jump and const instructions
2149 are computed. Since these structures are freed by sljit_free_compiler, the
2150 addresses must be preserved by the user program elsewere. */
sljit_get_label_addr(struct sljit_label * label)2151 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
sljit_get_jump_addr(struct sljit_jump * jump)2152 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
sljit_get_const_addr(struct sljit_const * const_)2153 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
2154
2155 /* Only the address and executable offset are required to perform dynamic
2156 code modifications. See sljit_get_executable_offset function. */
2157 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset);
2158 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset);
2159
2160 /* --------------------------------------------------------------------- */
2161 /* CPU specific functions */
2162 /* --------------------------------------------------------------------- */
2163
2164 /* Types for sljit_get_register_index */
2165
2166 /* General purpose (integer) registers. */
2167 #define SLJIT_GP_REGISTER 0
2168 /* Floating point registers. */
2169 #define SLJIT_FLOAT_REGISTER 1
2170
2171 /* The following function is a helper function for sljit_emit_op_custom.
2172 It returns with the real machine register index ( >=0 ) of any registers.
2173
2174 When type is SLJIT_GP_REGISTER:
2175 reg must be an SLJIT_R(i), SLJIT_S(i), or SLJIT_SP register
2176
2177 When type is SLJIT_FLOAT_REGISTER:
2178 reg must be an SLJIT_FR(i) or SLJIT_FS(i) register
2179
2180 When type is SLJIT_SIMD_REG_64 / 128 / 256 / 512 :
2181 reg must be an SLJIT_FR(i) or SLJIT_FS(i) register
2182
2183 Note: it returns with -1 for unknown registers, such as virtual
2184 registers on x86-32 or unsupported simd registers. */
2185
2186 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 type, sljit_s32 reg);
2187
2188 /* Any instruction can be inserted into the instruction stream by
2189 sljit_emit_op_custom. It has a similar purpose as inline assembly.
2190 The size parameter must match to the instruction size of the target
2191 architecture:
2192
2193 x86: 0 < size <= 15, the instruction argument can be byte aligned.
2194 Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
2195 if size == 4, the instruction argument must be 4 byte aligned.
2196 s390x: size can be 2, 4, or 6, the instruction argument can be byte aligned.
2197 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
2198
2199 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
2200 void *instruction, sljit_u32 size);
2201
2202 /* Flags were set by a 32 bit operation. */
2203 #define SLJIT_CURRENT_FLAGS_32 SLJIT_32
2204
2205 /* Flags were set by an ADD or ADDC operations. */
2206 #define SLJIT_CURRENT_FLAGS_ADD 0x01
2207 /* Flags were set by a SUB, SUBC, or NEG operation. */
2208 #define SLJIT_CURRENT_FLAGS_SUB 0x02
2209
2210 /* Flags were set by sljit_emit_op2u with SLJIT_SUB opcode.
2211 Must be combined with SLJIT_CURRENT_FLAGS_SUB. */
2212 #define SLJIT_CURRENT_FLAGS_COMPARE 0x04
2213
2214 /* Define the currently available CPU status flags. It is usually used after
2215 an sljit_emit_label or sljit_emit_op_custom operations to define which CPU
2216 status flags are available.
2217
2218 The current_flags must be a valid combination of SLJIT_SET_* and
2219 SLJIT_CURRENT_FLAGS_* constants. */
2220
2221 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_current_flags(struct sljit_compiler *compiler,
2222 sljit_s32 current_flags);
2223
2224 /* --------------------------------------------------------------------- */
2225 /* Miscellaneous utility functions */
2226 /* --------------------------------------------------------------------- */
2227
2228 /* Get the human readable name of the platform. Can be useful on platforms
2229 like ARM, where ARM and Thumb2 functions can be mixed, and it is useful
2230 to know the type of the code generator. */
2231 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void);
2232
2233 /* Portable helper function to get an offset of a member.
2234 Same as offsetof() macro defined in stddef.h */
2235 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
2236
2237 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
2238
2239 /* The sljit_stack structure and its manipulation functions provides
2240 an implementation for a top-down stack. The stack top is stored
2241 in the end field of the sljit_stack structure and the stack goes
2242 down to the min_start field, so the memory region reserved for
2243 this stack is between min_start (inclusive) and end (exclusive)
2244 fields. However the application can only use the region between
2245 start (inclusive) and end (exclusive) fields. The sljit_stack_resize
2246 function can be used to extend this region up to min_start.
2247
2248 This feature uses the "address space reserve" feature of modern
2249 operating systems. Instead of allocating a large memory block
2250 applications can allocate a small memory region and extend it
2251 later without moving the content of the memory area. Therefore
2252 after a successful resize by sljit_stack_resize all pointers into
2253 this region are still valid.
2254
2255 Note:
2256 this structure may not be supported by all operating systems.
2257 end and max_limit fields are aligned to PAGE_SIZE bytes (usually
2258 4 Kbyte or more).
2259 stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */
2260
2261 struct sljit_stack {
2262 /* User data, anything can be stored here.
2263 Initialized to the same value as the end field. */
2264 sljit_u8 *top;
2265 /* These members are read only. */
2266 /* End address of the stack */
2267 sljit_u8 *end;
2268 /* Current start address of the stack. */
2269 sljit_u8 *start;
2270 /* Lowest start address of the stack. */
2271 sljit_u8 *min_start;
2272 };
2273
2274 /* Allocates a new stack. Returns NULL if unsuccessful.
2275 Note: see sljit_create_compiler for the explanation of allocator_data. */
2276 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data);
2277 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
2278
2279 /* Can be used to increase (extend) or decrease (shrink) the stack
2280 memory area. Returns with new_start if successful and NULL otherwise.
2281 It always fails if new_start is less than min_start or greater or equal
2282 than end fields. The fields of the stack are not changed if the returned
2283 value is NULL (the current memory content is never lost). */
2284 SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start);
2285
2286 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
2287
2288 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
2289
2290 /* Get the entry address of a given function (signed, unsigned result). */
2291 #define SLJIT_FUNC_ADDR(func_name) ((sljit_sw)func_name)
2292 #define SLJIT_FUNC_UADDR(func_name) ((sljit_uw)func_name)
2293
2294 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
2295
2296 /* All JIT related code should be placed in the same context (library, binary, etc.). */
2297
2298 /* Get the entry address of a given function (signed, unsigned result). */
2299 #define SLJIT_FUNC_ADDR(func_name) (*(sljit_sw*)(void*)func_name)
2300 #define SLJIT_FUNC_UADDR(func_name) (*(sljit_uw*)(void*)func_name)
2301
2302 /* For powerpc64, the function pointers point to a context descriptor. */
2303 struct sljit_function_context {
2304 sljit_uw addr;
2305 sljit_uw r2;
2306 sljit_uw r11;
2307 };
2308
2309 /* Fill the context arguments using the addr and the function.
2310 If func_ptr is NULL, it will not be set to the address of context
2311 If addr is NULL, the function address also comes from the func pointer. */
2312 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_uw addr, void* func);
2313
2314 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
2315
2316 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
2317 /* Free unused executable memory. The allocator keeps some free memory
2318 around to reduce the number of OS executable memory allocations.
2319 This improves performance since these calls are costly. However
2320 it is sometimes desired to free all unused memory regions, e.g.
2321 before the application terminates. */
2322 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
2323 #endif
2324
2325 #ifdef __cplusplus
2326 } /* extern "C" */
2327 #endif
2328
2329 #endif /* SLJIT_LIR_H_ */
2330