1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef _SLJIT_LIR_H_
28 #define _SLJIT_LIR_H_
29
30 /*
31 ------------------------------------------------------------------------
32 Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
33 ------------------------------------------------------------------------
34
35 Short description
36 Advantages:
37 - The execution can be continued from any LIR instruction. In other
38 words, it is possible to jump to any label from anywhere, even from
39 a code fragment, which is compiled later, if both compiled code
40 shares the same context. See sljit_emit_enter for more details
41 - Supports self modifying code: target of (conditional) jump and call
42 instructions and some constant values can be dynamically modified
43 during runtime
44 - although it is not suggested to do it frequently
45 - can be used for inline caching: save an important value once
46 in the instruction stream
47 - since this feature limits the optimization possibilities, a
48 special flag must be passed at compile time when these
49 instructions are emitted
50 - A fixed stack space can be allocated for local variables
51 - The compiler is thread-safe
52 - The compiler is highly configurable through preprocessor macros.
53 You can disable unneeded features (multithreading in single
54 threaded applications), and you can use your own system functions
55 (including memory allocators). See sljitConfig.h
56 Disadvantages:
57 - No automatic register allocation, and temporary results are
58 not stored on the stack. (hence the name comes)
59 In practice:
60 - This approach is very effective for interpreters
61 - One of the saved registers typically points to a stack interface
62 - It can jump to any exception handler anytime (even if it belongs
63 to another function)
64 - Hot paths can be modified during runtime reflecting the changes
65 of the fastest execution path of the dynamic language
66 - SLJIT supports complex memory addressing modes
67 - mainly position and context independent code (except some cases)
68
69 For valgrind users:
70 - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
71 */
72
73 #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
74 #include "sljitConfig.h"
75 #endif
76
77 /* The following header file defines useful macros for fine tuning
78 sljit based code generators. They are listed in the beginning
79 of sljitConfigInternal.h */
80
81 #include "sljitConfigInternal.h"
82
83 /* --------------------------------------------------------------------- */
84 /* Error codes */
85 /* --------------------------------------------------------------------- */
86
87 /* Indicates no error. */
88 #define SLJIT_SUCCESS 0
89 /* After the call of sljit_generate_code(), the error code of the compiler
90 is set to this value to avoid future sljit calls (in debug mode at least).
91 The complier should be freed after sljit_generate_code(). */
92 #define SLJIT_ERR_COMPILED 1
93 /* Cannot allocate non executable memory. */
94 #define SLJIT_ERR_ALLOC_FAILED 2
95 /* Cannot allocate executable memory.
96 Only for sljit_generate_code() */
97 #define SLJIT_ERR_EX_ALLOC_FAILED 3
98 /* Return value for SLJIT_CONFIG_UNSUPPORTED placeholder architecture. */
99 #define SLJIT_ERR_UNSUPPORTED 4
100 /* An ivalid argument is passed to any SLJIT function. */
101 #define SLJIT_ERR_BAD_ARGUMENT 5
102 /* Dynamic code modification is not enabled. */
103 #define SLJIT_ERR_DYN_CODE_MOD 6
104
105 /* --------------------------------------------------------------------- */
106 /* Registers */
107 /* --------------------------------------------------------------------- */
108
109 /*
110 Scratch (R) registers: registers whose may not preserve their values
111 across function calls.
112
113 Saved (S) registers: registers whose preserve their values across
114 function calls.
115
116 The scratch and saved register sets are overlap. The last scratch register
117 is the first saved register, the one before the last is the second saved
118 register, and so on.
119
120 If an architecture provides two scratch and three saved registers,
121 its scratch and saved register sets are the following:
122
123 R0 | | R0 is always a scratch register
124 R1 | | R1 is always a scratch register
125 [R2] | S2 | R2 and S2 represent the same physical register
126 [R3] | S1 | R3 and S1 represent the same physical register
127 [R4] | S0 | R4 and S0 represent the same physical register
128
129 Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
130 SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
131
132 Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 12
133 and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 6. However, 6 registers
134 are virtual on x86-32. See below.
135
136 The purpose of this definition is convenience: saved registers can
137 be used as extra scratch registers. For example four registers can
138 be specified as scratch registers and the fifth one as saved register
139 on the CPU above and any user code which requires four scratch
140 registers can run unmodified. The SLJIT compiler automatically saves
141 the content of the two extra scrath register on the stack. Scratch
142 registers can also be preserved by saving their value on the stack
143 but this needs to be done manually.
144
145 Note: To emphasize that registers assigned to R2-R4 are saved
146 registers, they are enclosed by square brackets.
147
148 Note: sljit_emit_enter and sljit_set_context defines whether a register
149 is S or R register. E.g: when 3 scratches and 1 saved is mapped
150 by sljit_emit_enter, the allowed register set will be: R0-R2 and
151 S0. Although S2 is mapped to the same position as R2, it does not
152 available in the current configuration. Furthermore the S1 register
153 is not available at all.
154 */
155
156 /* When SLJIT_UNUSED is specified as the destination of sljit_emit_op1 and
157 and sljit_emit_op2 operations the result is discarded. If no status
158 flags are set, no instructions are emitted for these operations. Data
159 prefetch is a special exception, see SLJIT_MOV operation. Other SLJIT
160 operations do not support SLJIT_UNUSED as a destination operand. */
161 #define SLJIT_UNUSED 0
162
163 /* Scratch registers. */
164 #define SLJIT_R0 1
165 #define SLJIT_R1 2
166 #define SLJIT_R2 3
167 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
168 are allocated on the stack). These registers are called virtual
169 and cannot be used for memory addressing (cannot be part of
170 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
171 limitation on other CPUs. See sljit_get_register_index(). */
172 #define SLJIT_R3 4
173 #define SLJIT_R4 5
174 #define SLJIT_R5 6
175 #define SLJIT_R6 7
176 #define SLJIT_R7 8
177 #define SLJIT_R8 9
178 #define SLJIT_R9 10
179 /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
180 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
181 #define SLJIT_R(i) (1 + (i))
182
183 /* Saved registers. */
184 #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
185 #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
186 #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
187 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
188 are allocated on the stack). These registers are called virtual
189 and cannot be used for memory addressing (cannot be part of
190 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
191 limitation on other CPUs. See sljit_get_register_index(). */
192 #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
193 #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
194 #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
195 #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
196 #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
197 #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
198 #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
199 /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
200 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
201 #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
202
203 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
204 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
205
206 /* The SLJIT_SP provides direct access to the linear stack space allocated by
207 sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
208 The immediate offset is extended by the relative stack offset automatically.
209 The sljit_get_local_base can be used to obtain the absolute offset. */
210 #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
211
212 /* Return with machine word. */
213
214 #define SLJIT_RETURN_REG SLJIT_R0
215
216 /* x86 prefers specific registers for special purposes. In case of shift
217 by register it supports only SLJIT_R2 for shift argument
218 (which is the src2 argument of sljit_emit_op2). If another register is
219 used, sljit must exchange data between registers which cause a minor
220 slowdown. Other architectures has no such limitation. */
221
222 #define SLJIT_PREF_SHIFT_REG SLJIT_R2
223
224 /* --------------------------------------------------------------------- */
225 /* Floating point registers */
226 /* --------------------------------------------------------------------- */
227
228 /* Each floating point register can store a 32 or a 64 bit precision
229 value. The FR and FS register sets are overlap in the same way as R
230 and S register sets. See above. */
231
232 /* Note: SLJIT_UNUSED as destination is not valid for floating point
233 operations, since they cannot be used for setting flags. */
234
235 /* Floating point scratch registers. */
236 #define SLJIT_FR0 1
237 #define SLJIT_FR1 2
238 #define SLJIT_FR2 3
239 #define SLJIT_FR3 4
240 #define SLJIT_FR4 5
241 #define SLJIT_FR5 6
242 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
243 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
244 #define SLJIT_FR(i) (1 + (i))
245
246 /* Floating point saved registers. */
247 #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
248 #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
249 #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
250 #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
251 #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
252 #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
253 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
254 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
255 #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
256
257 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
258 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
259
260 /* --------------------------------------------------------------------- */
261 /* Main structures and functions */
262 /* --------------------------------------------------------------------- */
263
264 /*
265 The following structures are private, and can be changed in the
266 future. Keeping them here allows code inlining.
267 */
268
269 struct sljit_memory_fragment {
270 struct sljit_memory_fragment *next;
271 sljit_uw used_size;
272 /* Must be aligned to sljit_sw. */
273 sljit_u8 memory[1];
274 };
275
276 struct sljit_label {
277 struct sljit_label *next;
278 sljit_uw addr;
279 /* The maximum size difference. */
280 sljit_uw size;
281 };
282
283 struct sljit_jump {
284 struct sljit_jump *next;
285 sljit_uw addr;
286 sljit_sw flags;
287 union {
288 sljit_uw target;
289 struct sljit_label* label;
290 } u;
291 };
292
293 struct sljit_const {
294 struct sljit_const *next;
295 sljit_uw addr;
296 };
297
298 struct sljit_compiler {
299 sljit_s32 error;
300 sljit_s32 options;
301
302 struct sljit_label *labels;
303 struct sljit_jump *jumps;
304 struct sljit_const *consts;
305 struct sljit_label *last_label;
306 struct sljit_jump *last_jump;
307 struct sljit_const *last_const;
308
309 void *allocator_data;
310 struct sljit_memory_fragment *buf;
311 struct sljit_memory_fragment *abuf;
312
313 /* Used scratch registers. */
314 sljit_s32 scratches;
315 /* Used saved registers. */
316 sljit_s32 saveds;
317 /* Used float scratch registers. */
318 sljit_s32 fscratches;
319 /* Used float saved registers. */
320 sljit_s32 fsaveds;
321 /* Local stack size. */
322 sljit_s32 local_size;
323 /* Code size. */
324 sljit_uw size;
325 /* Relative offset of the executable mapping from the writable mapping. */
326 sljit_uw executable_offset;
327 /* Executable size for statistical purposes. */
328 sljit_uw executable_size;
329
330 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
331 sljit_s32 args;
332 sljit_s32 locals_offset;
333 sljit_s32 saveds_offset;
334 #endif
335
336 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
337 sljit_s32 mode32;
338 #ifdef _WIN64
339 sljit_s32 locals_offset;
340 #endif
341 #endif
342
343 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
344 /* Constant pool handling. */
345 sljit_uw *cpool;
346 sljit_u8 *cpool_unique;
347 sljit_uw cpool_diff;
348 sljit_uw cpool_fill;
349 /* Other members. */
350 /* Contains pointer, "ldr pc, [...]" pairs. */
351 sljit_uw patches;
352 #endif
353
354 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
355 /* Temporary fields. */
356 sljit_uw shift_imm;
357 #endif
358
359 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
360 sljit_s32 cache_arg;
361 sljit_sw cache_argw;
362 #endif
363
364 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
365 sljit_sw imm;
366 sljit_s32 cache_arg;
367 sljit_sw cache_argw;
368 #endif
369
370 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
371 sljit_s32 delay_slot;
372 sljit_s32 cache_arg;
373 sljit_sw cache_argw;
374 #endif
375
376 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
377 sljit_s32 delay_slot;
378 sljit_s32 cache_arg;
379 sljit_sw cache_argw;
380 #endif
381
382 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
383 sljit_s32 cache_arg;
384 sljit_sw cache_argw;
385 #endif
386
387 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
388 FILE* verbose;
389 #endif
390
391 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
392 || (defined SLJIT_DEBUG && SLJIT_DEBUG)
393 /* Flags specified by the last arithmetic instruction.
394 It contains the type of the variable flag. */
395 sljit_s32 last_flags;
396 /* Local size passed to the functions. */
397 sljit_s32 logical_local_size;
398 #endif
399
400 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
401 || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
402 || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
403 /* Trust arguments when the API function is called. */
404 sljit_s32 skip_checks;
405 #endif
406 };
407
408 /* --------------------------------------------------------------------- */
409 /* Main functions */
410 /* --------------------------------------------------------------------- */
411
412 /* Creates an sljit compiler. The allocator_data is required by some
413 custom memory managers. This pointer is passed to SLJIT_MALLOC
414 and SLJIT_FREE macros. Most allocators (including the default
415 one) ignores this value, and it is recommended to pass NULL
416 as a dummy value for allocator_data.
417
418 Returns NULL if failed. */
419 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data);
420
421 /* Frees everything except the compiled machine code. */
422 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
423
424 /* Returns the current error code. If an error is occurred, future sljit
425 calls which uses the same compiler argument returns early with the same
426 error code. Thus there is no need for checking the error after every
427 call, it is enough to do it before the code is compiled. Removing
428 these checks increases the performance of the compiling process. */
sljit_get_compiler_error(struct sljit_compiler * compiler)429 static SLJIT_INLINE sljit_s32 sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
430
431 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
432 if an error was detected before. After the error code is set
433 the compiler behaves as if the allocation failure happened
434 during an sljit function call. This can greatly simplify error
435 checking, since only the compiler status needs to be checked
436 after the compilation. */
437 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
438
439 /*
440 Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
441 and <= 128 bytes on 64 bit architectures. The memory area is owned by the
442 compiler, and freed by sljit_free_compiler. The returned pointer is
443 sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
444 the compiling, and no need to worry about freeing them. The size is
445 enough to contain at most 16 pointers. If the size is outside of the range,
446 the function will return with NULL. However, this return value does not
447 indicate that there is no more memory (does not set the current error code
448 of the compiler to out-of-memory status).
449 */
450 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_s32 size);
451
452 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
453 /* Passing NULL disables verbose. */
454 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
455 #endif
456
457 /*
458 Create executable code from the sljit instruction stream. This is the final step
459 of the code generation so no more instructions can be added after this call.
460 */
461
462 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
463
464 /* Free executable code. */
465
466 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
467
468 /*
469 When the protected executable allocator is used the JIT code is mapped
470 twice. The first mapping has read/write and the second mapping has read/exec
471 permissions. This function returns with the relative offset of the executable
472 mapping using the writable mapping as the base after the machine code is
473 successfully generated. The returned value is always 0 for the normal executable
474 allocator, since it uses only one mapping with read/write/exec permissions.
475 Dynamic code modifications requires this value.
476
477 Before a successful code generation, this function returns with 0.
478 */
sljit_get_executable_offset(struct sljit_compiler * compiler)479 static SLJIT_INLINE sljit_sw sljit_get_executable_offset(struct sljit_compiler *compiler) { return compiler->executable_offset; }
480
481 /*
482 The executable memory consumption of the generated code can be retrieved by
483 this function. The returned value can be used for statistical purposes.
484
485 Before a successful code generation, this function returns with 0.
486 */
sljit_get_generated_code_size(struct sljit_compiler * compiler)487 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
488
489 /* Returns with non-zero if the feature or limitation type passed as its
490 argument is present on the current CPU.
491
492 Some features (e.g. floating point operations) require hardware (CPU)
493 support while others (e.g. move with update) are emulated if not available.
494 However even if a feature is emulated, specialized code paths can be faster
495 than the emulation. Some limitations are emulated as well so their general
496 case is supported but it has extra performance costs. */
497
498 /* [Not emulated] Floating-point support is available. */
499 #define SLJIT_HAS_FPU 0
500 /* [Limitation] Some registers are virtual registers. */
501 #define SLJIT_HAS_VIRTUAL_REGISTERS 1
502 /* [Emulated] Some forms of move with pre update is supported. */
503 #define SLJIT_HAS_PRE_UPDATE 2
504 /* [Emulated] Count leading zero is supported. */
505 #define SLJIT_HAS_CLZ 3
506 /* [Emulated] Conditional move is supported. */
507 #define SLJIT_HAS_CMOV 4
508 /* [Limitation] [Emulated] Shifting with register is limited to SLJIT_PREF_SHIFT_REG. */
509 #define SLJIT_HAS_PREF_SHIFT_REG 5
510
511 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
512 /* [Not emulated] SSE2 support is available on x86. */
513 #define SLJIT_HAS_SSE2 100
514 #endif
515
516 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type);
517
518 /* Instruction generation. Returns with any error code. If there is no
519 error, they return with SLJIT_SUCCESS. */
520
521 /*
522 The executable code is a function call from the viewpoint of the C
523 language. The function calls must obey to the ABI (Application
524 Binary Interface) of the platform, which specify the purpose of
525 all machine registers and stack handling among other things. The
526 sljit_emit_enter function emits the necessary instructions for
527 setting up a new context for the executable code and moves function
528 arguments to the saved registers. Furthermore the options argument
529 can be used to pass configuration options to the compiler. The
530 available options are listed before sljit_emit_enter.
531
532 The number of sljit_sw arguments passed to the generated function
533 are specified in the "args" parameter. The number of arguments must
534 be less than or equal to 3. The first argument goes to SLJIT_S0,
535 the second goes to SLJIT_S1 and so on. The register set used by
536 the function must be declared as well. The number of scratch and
537 saved registers used by the function must be passed to sljit_emit_enter.
538 Only R registers between R0 and "scratches" argument can be used
539 later. E.g. if "scratches" is set to 2, the register set will be
540 limited to R0 and R1. The S registers and the floating point
541 registers ("fscratches" and "fsaveds") are specified in a similar
542 way. The sljit_emit_enter is also capable of allocating a stack
543 space for local variables. The "local_size" argument contains the
544 size in bytes of this local area and its staring address is stored
545 in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
546 SLJIT_SP + local_size (exclusive) can be modified freely until
547 the function returns. The stack space is not initialized.
548
549 Note: the following conditions must met:
550 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
551 0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
552 scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
553 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
554 0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
555 fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
556
557 Note: every call of sljit_emit_enter and sljit_set_context
558 overwrites the previous context.
559 */
560
561 /* The absolute address returned by sljit_get_local_base with
562 offset 0 is aligned to sljit_f64. Otherwise it is aligned to sljit_sw. */
563 #define SLJIT_F64_ALIGNMENT 0x00000001
564
565 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
566 #define SLJIT_MAX_LOCAL_SIZE 65536
567
568 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
569 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
570 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
571
572 /* The machine code has a context (which contains the local stack space size,
573 number of used registers, etc.) which initialized by sljit_emit_enter. Several
574 functions (like sljit_emit_return) requres this context to be able to generate
575 the appropriate code. However, some code fragments (like inline cache) may have
576 no normal entry point so their context is unknown for the compiler. Their context
577 can be provided to the compiler by the sljit_set_context function.
578
579 Note: every call of sljit_emit_enter and sljit_set_context overwrites
580 the previous context. */
581
582 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
583 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
584 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
585
586 /* Return from machine code. The op argument can be SLJIT_UNUSED which means the
587 function does not return with anything or any opcode between SLJIT_MOV and
588 SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
589 is SLJIT_UNUSED, otherwise see below the description about source and
590 destination arguments. */
591
592 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op,
593 sljit_s32 src, sljit_sw srcw);
594
595 /* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and
596 even the stack frame is passed to the callee. The return address is preserved in
597 dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function
598 is sljit_p), and sljit_emit_fast_return can use this as a return value later. */
599
600 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine
601 instructions are needed. Excellent for small uility functions, where saving registers
602 and setting up a new stack frame would cost too much performance. However, it is still
603 possible to return to the address of the caller (or anywhere else). */
604
605 /* Note: may destroy flags. */
606
607 /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
608 since many architectures do clever branch prediction on call / return instruction pairs. */
609
610 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw);
611 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw);
612
613 /*
614 Source and destination values for arithmetical instructions
615 imm - a simple immediate value (cannot be used as a destination)
616 reg - any of the registers (immediate argument must be 0)
617 [imm] - absolute immediate memory address
618 [reg+imm] - indirect memory address
619 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
620 useful for (byte, half, int, sljit_sw) array access
621 (fully supported by both x86 and ARM architectures, and cheap operation on others)
622 */
623
624 /*
625 IMPORATNT NOTE: memory access MUST be naturally aligned except
626 SLJIT_UNALIGNED macro is defined and its value is 1.
627
628 length | alignment
629 ---------+-----------
630 byte | 1 byte (any physical_address is accepted)
631 half | 2 byte (physical_address & 0x1 == 0)
632 int | 4 byte (physical_address & 0x3 == 0)
633 word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
634 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
635 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
636 | on 64 bit machines)
637
638 Note: Different architectures have different addressing limitations.
639 A single instruction is enough for the following addressing
640 modes. Other adrressing modes are emulated by instruction
641 sequences. This information could help to improve those code
642 generators which focuses only a few architectures.
643
644 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
645 [reg+(reg<<imm)] is supported
646 [imm], -2^32+1 <= imm <= 2^32-1 is supported
647 Write-back is not supported
648 arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
649 bytes, any halfs or floating point values)
650 [reg+(reg<<imm)] is supported
651 Write-back is supported
652 arm-t2: [reg+imm], -255 <= imm <= 4095
653 [reg+(reg<<imm)] is supported
654 Write back is supported only for [reg+imm], where -255 <= imm <= 255
655 ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
656 signed load on 64 bit requires immediates divisible by 4.
657 [reg+imm] is not supported for signed 8 bit values.
658 [reg+reg] is supported
659 Write-back is supported except for one instruction: 32 bit signed
660 load with [reg+imm] addressing mode on 64 bit.
661 mips: [reg+imm], -65536 <= imm <= 65535
662 sparc: [reg+imm], -4096 <= imm <= 4095
663 [reg+reg] is supported
664 */
665
666 /* Register output: simply the name of the register.
667 For destination, you can use SLJIT_UNUSED as well. */
668 #define SLJIT_MEM 0x80
669 #define SLJIT_MEM0() (SLJIT_MEM)
670 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
671 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
672 #define SLJIT_IMM 0x40
673
674 /* Set 32 bit operation mode (I) on 64 bit CPUs. This option is ignored on
675 32 bit CPUs. When this option is set for an arithmetic operation, only
676 the lower 32 bit of the input registers are used, and the CPU status
677 flags are set according to the 32 bit result. Although the higher 32 bit
678 of the input and the result registers are not defined by SLJIT, it might
679 be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
680 requirements all source registers must be the result of those operations
681 where this option was also set. Memory loads read 32 bit values rather
682 than 64 bit ones. In other words 32 bit and 64 bit operations cannot
683 be mixed. The only exception is SLJIT_MOV32 and SLJIT_MOVU32 whose source
684 register can hold any 32 or 64 bit value, and it is converted to a 32 bit
685 compatible format first. This conversion is free (no instructions are
686 emitted) on most CPUs. A 32 bit value can also be coverted to a 64 bit
687 value by SLJIT_MOV_S32 (sign extension) or SLJIT_MOV_U32 (zero extension).
688
689 Note: memory addressing always uses 64 bit values on 64 bit systems so
690 the result of a 32 bit operation must not be used with SLJIT_MEMx
691 macros.
692
693 This option is part of the instruction name, so there is no need to
694 manually set it. E.g:
695
696 SLJIT_ADD32 == (SLJIT_ADD | SLJIT_I32_OP) */
697 #define SLJIT_I32_OP 0x100
698
699 /* Set F32 (single) precision mode for floating-point computation. This
700 option is similar to SLJIT_I32_OP, it just applies to floating point
701 registers. When this option is passed, the CPU performs 32 bit floating
702 point operations, rather than 64 bit one. Similar to SLJIT_I32_OP, all
703 register arguments must be the result of those operations where this
704 option was also set.
705
706 This option is part of the instruction name, so there is no need to
707 manually set it. E.g:
708
709 SLJIT_MOV_F32 = (SLJIT_MOV_F64 | SLJIT_F32_OP)
710 */
711 #define SLJIT_F32_OP SLJIT_I32_OP
712
713 /* Many CPUs (x86, ARM, PPC) has status flags which can be set according
714 to the result of an operation. Other CPUs (MIPS) does not have status
715 flags, and results must be stored in registers. To cover both architecture
716 types efficiently only two flags are defined by SLJIT:
717
718 * Zero (equal) flag: it is set if the result is zero
719 * Variable flag: its value is defined by the last arithmetic operation
720
721 SLJIT instructions can set any or both of these flags. The value of
722 these flags is undefined if the instruction does not specify their value.
723 The description of each instruction contains the list of allowed flag
724 types.
725
726 Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
727
728 sljit_op2(..., SLJIT_ADD, ...)
729 Both the zero and variable flags are undefined so they can
730 have any value after the operation is completed.
731
732 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
733 Sets the zero flag if the result is zero, clears it otherwise.
734 The variable flag is undefined.
735
736 sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
737 Sets the variable flag if an integer overflow occurs, clears
738 it otherwise. The zero flag is undefined.
739
740 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
741 Sets the zero flag if the result is zero, clears it otherwise.
742 Sets the variable flag if unsigned overflow (carry) occurs,
743 clears it otherwise.
744
745 If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are
746 unchanged.
747
748 Using these flags can reduce the number of emitted instructions. E.g. a
749 fast loop can be implemented by decreasing a counter register and set the
750 zero flag to jump back if the counter register is not reached zero.
751
752 Motivation: although CPUs can set a large number of flags, usually their
753 values are ignored or only one of them is used. Emulating a large number
754 of flags on systems without flag register is complicated so SLJIT
755 instructions must specify the flag they want to use and only that flag
756 will be emulated. The last arithmetic instruction can be repeated if
757 multiple flags needs to be checked.
758 */
759
760 /* Set Zero status flag. */
761 #define SLJIT_SET_Z 0x0200
762 /* Set the variable status flag if condition is true.
763 See comparison types. */
764 #define SLJIT_SET(condition) ((condition) << 10)
765
766 /* Notes:
767 - you cannot postpone conditional jump instructions except if noted that
768 the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
769 - flag combinations: '|' means 'logical or'. */
770
771 /* Starting index of opcodes for sljit_emit_op0. */
772 #define SLJIT_OP0_BASE 0
773
774 /* Flags: - (does not modify flags)
775 Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
776 It falls back to SLJIT_NOP in those cases. */
777 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
778 /* Flags: - (does not modify flags)
779 Note: may or may not cause an extra cycle wait
780 it can even decrease the runtime in a few cases. */
781 #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
782 /* Flags: - (may destroy flags)
783 Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
784 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
785 #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2)
786 /* Flags: - (may destroy flags)
787 Signed multiplication of SLJIT_R0 and SLJIT_R1.
788 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
789 #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3)
790 /* Flags: - (may destroy flags)
791 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
792 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
793 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
794 #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4)
795 #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_I32_OP)
796 /* Flags: - (may destroy flags)
797 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
798 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
799 Note: if SLJIT_R1 is 0, the behaviour is undefined.
800 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
801 the behaviour is undefined. */
802 #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5)
803 #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_I32_OP)
804 /* Flags: - (may destroy flags)
805 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
806 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
807 Note: if SLJIT_R1 is 0, the behaviour is undefined. */
808 #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6)
809 #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_I32_OP)
810 /* Flags: - (may destroy flags)
811 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
812 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
813 Note: if SLJIT_R1 is 0, the behaviour is undefined.
814 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
815 the behaviour is undefined. */
816 #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7)
817 #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_I32_OP)
818
819 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op);
820
821 /* Starting index of opcodes for sljit_emit_op1. */
822 #define SLJIT_OP1_BASE 32
823
824 /* The MOV instruction transfer data from source to destination.
825
826 MOV instruction suffixes:
827
828 U8 - unsigned 8 bit data transfer
829 S8 - signed 8 bit data transfer
830 U16 - unsigned 16 bit data transfer
831 S16 - signed 16 bit data transfer
832 U32 - unsigned int (32 bit) data transfer
833 S32 - signed int (32 bit) data transfer
834 P - pointer (sljit_p) data transfer
835
836 U = move with update (pre form). If source or destination defined as
837 SLJIT_MEM1(r1) or SLJIT_MEM2(r1, r2), r1 is increased by the
838 offset part of the address.
839
840 Register arguments and base registers can only be used once for move
841 with update instructions. The shift value of SLJIT_MEM2 addressing
842 mode must also be 0. Reason: SLJIT_MOVU instructions are expected to
843 be in high-performance loops where complex instruction emulation
844 would be too costly.
845
846 Examples for invalid move with update instructions:
847
848 sljit_emit_op1(..., SLJIT_MOVU_U8,
849 SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), 8);
850 sljit_emit_op1(..., SLJIT_MOVU_U8,
851 SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_R0, 0);
852 sljit_emit_op1(..., SLJIT_MOVU_U8,
853 SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 0, SLJIT_MEM1(SLJIT_R0), 8);
854 sljit_emit_op1(..., SLJIT_MOVU_U8,
855 SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 0, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0);
856 sljit_emit_op1(..., SLJIT_MOVU_U8,
857 SLJIT_R2, 0, SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 1);
858
859 The following example is valid, since only the offset register is
860 used multiple times:
861
862 sljit_emit_op1(..., SLJIT_MOVU_U8,
863 SLJIT_MEM2(SLJIT_R0, SLJIT_R2), 0, SLJIT_MEM2(SLJIT_R1, SLJIT_R2), 0);
864
865 If the destination of a MOV without update instruction is SLJIT_UNUSED
866 and the source operand is a memory address the compiler emits a prefetch
867 instruction if this instruction is supported by the current CPU.
868 Higher data sizes bring the data closer to the core: a MOV with word
869 size loads the data into a higher level cache than a byte size. Otherwise
870 the type does not affect the prefetch instruction. Furthermore a prefetch
871 instruction never fails, so it can be used to prefetch a data from an
872 address and check whether that address is NULL afterwards.
873 */
874
875 /* Flags: - (does not modify flags) */
876 #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
877 /* Flags: - (does not modify flags) */
878 #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1)
879 #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_I32_OP)
880 /* Flags: - (does not modify flags) */
881 #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2)
882 #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_I32_OP)
883 /* Flags: - (does not modify flags) */
884 #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3)
885 #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_I32_OP)
886 /* Flags: - (does not modify flags) */
887 #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4)
888 #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_I32_OP)
889 /* Flags: - (does not modify flags)
890 Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
891 #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5)
892 /* Flags: - (does not modify flags)
893 Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
894 #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6)
895 /* Flags: - (does not modify flags) */
896 #define SLJIT_MOV32 (SLJIT_MOV_S32 | SLJIT_I32_OP)
897 /* Flags: - (does not modify flags) */
898 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 7)
899 /* Flags: - (may destroy flags) */
900 #define SLJIT_MOVU (SLJIT_OP1_BASE + 8)
901 /* Flags: - (may destroy flags) */
902 #define SLJIT_MOVU_U8 (SLJIT_OP1_BASE + 9)
903 #define SLJIT_MOVU32_U8 (SLJIT_MOVU_U8 | SLJIT_I32_OP)
904 /* Flags: - (may destroy flags) */
905 #define SLJIT_MOVU_S8 (SLJIT_OP1_BASE + 10)
906 #define SLJIT_MOVU32_S8 (SLJIT_MOVU_S8 | SLJIT_I32_OP)
907 /* Flags: - (may destroy flags) */
908 #define SLJIT_MOVU_U16 (SLJIT_OP1_BASE + 11)
909 #define SLJIT_MOVU32_U16 (SLJIT_MOVU_U16 | SLJIT_I32_OP)
910 /* Flags: - (may destroy flags) */
911 #define SLJIT_MOVU_S16 (SLJIT_OP1_BASE + 12)
912 #define SLJIT_MOVU32_S16 (SLJIT_MOVU_S16 | SLJIT_I32_OP)
913 /* Flags: - (may destroy flags)
914 Note: no SLJIT_MOVU32_U32 form, since it is the same as SLJIT_MOVU32 */
915 #define SLJIT_MOVU_U32 (SLJIT_OP1_BASE + 13)
916 /* Flags: - (may destroy flags)
917 Note: no SLJIT_MOVU32_S32 form, since it is the same as SLJIT_MOVU32 */
918 #define SLJIT_MOVU_S32 (SLJIT_OP1_BASE + 14)
919 /* Flags: - (may destroy flags) */
920 #define SLJIT_MOVU32 (SLJIT_MOVU_S32 | SLJIT_I32_OP)
921 /* Flags: - (may destroy flags) */
922 #define SLJIT_MOVU_P (SLJIT_OP1_BASE + 15)
923 /* Flags: Z */
924 #define SLJIT_NOT (SLJIT_OP1_BASE + 16)
925 #define SLJIT_NOT32 (SLJIT_NOT | SLJIT_I32_OP)
926 /* Flags: Z | OVERFLOW */
927 #define SLJIT_NEG (SLJIT_OP1_BASE + 17)
928 #define SLJIT_NEG32 (SLJIT_NEG | SLJIT_I32_OP)
929 /* Count leading zeroes
930 Flags: - (may destroy flags) */
931 #define SLJIT_CLZ (SLJIT_OP1_BASE + 18)
932 #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_I32_OP)
933
934 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
935 sljit_s32 dst, sljit_sw dstw,
936 sljit_s32 src, sljit_sw srcw);
937
938 /* Starting index of opcodes for sljit_emit_op2. */
939 #define SLJIT_OP2_BASE 96
940
941 /* Flags: Z | OVERFLOW | CARRY */
942 #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
943 #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_I32_OP)
944 /* Flags: CARRY */
945 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
946 #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_I32_OP)
947 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
948 SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
949 SIG_LESS_EQUAL | CARRY */
950 #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
951 #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_I32_OP)
952 /* Flags: CARRY */
953 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
954 #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_I32_OP)
955 /* Note: integer mul
956 Flags: MUL_OVERFLOW */
957 #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
958 #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_I32_OP)
959 /* Flags: Z */
960 #define SLJIT_AND (SLJIT_OP2_BASE + 5)
961 #define SLJIT_AND32 (SLJIT_AND | SLJIT_I32_OP)
962 /* Flags: Z */
963 #define SLJIT_OR (SLJIT_OP2_BASE + 6)
964 #define SLJIT_OR32 (SLJIT_OR | SLJIT_I32_OP)
965 /* Flags: Z */
966 #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
967 #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_I32_OP)
968 /* Flags: Z
969 Let bit_length be the length of the shift operation: 32 or 64.
970 If src2 is immediate, src2w is masked by (bit_length - 1).
971 Otherwise, if the content of src2 is outside the range from 0
972 to bit_length - 1, the result is undefined. */
973 #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
974 #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_I32_OP)
975 /* Flags: Z
976 Let bit_length be the length of the shift operation: 32 or 64.
977 If src2 is immediate, src2w is masked by (bit_length - 1).
978 Otherwise, if the content of src2 is outside the range from 0
979 to bit_length - 1, the result is undefined. */
980 #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
981 #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_I32_OP)
982 /* Flags: Z
983 Let bit_length be the length of the shift operation: 32 or 64.
984 If src2 is immediate, src2w is masked by (bit_length - 1).
985 Otherwise, if the content of src2 is outside the range from 0
986 to bit_length - 1, the result is undefined. */
987 #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
988 #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_I32_OP)
989
990 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
991 sljit_s32 dst, sljit_sw dstw,
992 sljit_s32 src1, sljit_sw src1w,
993 sljit_s32 src2, sljit_sw src2w);
994
995 /* Starting index of opcodes for sljit_emit_fop1. */
996 #define SLJIT_FOP1_BASE 128
997
998 /* Flags: - (does not modify flags) */
999 #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0)
1000 #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_F32_OP)
1001 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
1002 SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
1003 Rounding mode when the destination is W or I: round towards zero. */
1004 /* Flags: - (does not modify flags) */
1005 #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1)
1006 #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_F32_OP)
1007 /* Flags: - (does not modify flags) */
1008 #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2)
1009 #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_F32_OP)
1010 /* Flags: - (does not modify flags) */
1011 #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3)
1012 #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_F32_OP)
1013 /* Flags: - (does not modify flags) */
1014 #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4)
1015 #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_F32_OP)
1016 /* Flags: - (does not modify flags) */
1017 #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5)
1018 #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_F32_OP)
1019 /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
1020 Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
1021 #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 6)
1022 #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_F32_OP)
1023 /* Flags: - (does not modify flags) */
1024 #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 7)
1025 #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_F32_OP)
1026 /* Flags: - (does not modify flags) */
1027 #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 8)
1028 #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_F32_OP)
1029
1030 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1031 sljit_s32 dst, sljit_sw dstw,
1032 sljit_s32 src, sljit_sw srcw);
1033
1034 /* Starting index of opcodes for sljit_emit_fop2. */
1035 #define SLJIT_FOP2_BASE 160
1036
1037 /* Flags: - (does not modify flags) */
1038 #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0)
1039 #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_F32_OP)
1040 /* Flags: - (does not modify flags) */
1041 #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1)
1042 #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_F32_OP)
1043 /* Flags: - (does not modify flags) */
1044 #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2)
1045 #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_F32_OP)
1046 /* Flags: - (does not modify flags) */
1047 #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3)
1048 #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_F32_OP)
1049
1050 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1051 sljit_s32 dst, sljit_sw dstw,
1052 sljit_s32 src1, sljit_sw src1w,
1053 sljit_s32 src2, sljit_sw src2w);
1054
1055 /* Label and jump instructions. */
1056
1057 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
1058
1059 /* Invert (negate) conditional type: xor (^) with 0x1 */
1060
1061 /* Integer comparison types. */
1062 #define SLJIT_EQUAL 0
1063 #define SLJIT_EQUAL32 (SLJIT_EQUAL | SLJIT_I32_OP)
1064 #define SLJIT_ZERO 0
1065 #define SLJIT_ZERO32 (SLJIT_ZERO | SLJIT_I32_OP)
1066 #define SLJIT_NOT_EQUAL 1
1067 #define SLJIT_NOT_EQUAL32 (SLJIT_NOT_EQUAL | SLJIT_I32_OP)
1068 #define SLJIT_NOT_ZERO 1
1069 #define SLJIT_NOT_ZERO32 (SLJIT_NOT_ZERO | SLJIT_I32_OP)
1070
1071 #define SLJIT_LESS 2
1072 #define SLJIT_LESS32 (SLJIT_LESS | SLJIT_I32_OP)
1073 #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS)
1074 #define SLJIT_GREATER_EQUAL 3
1075 #define SLJIT_GREATER_EQUAL32 (SLJIT_GREATER_EQUAL | SLJIT_I32_OP)
1076 #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_GREATER_EQUAL)
1077 #define SLJIT_GREATER 4
1078 #define SLJIT_GREATER32 (SLJIT_GREATER | SLJIT_I32_OP)
1079 #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER)
1080 #define SLJIT_LESS_EQUAL 5
1081 #define SLJIT_LESS_EQUAL32 (SLJIT_LESS_EQUAL | SLJIT_I32_OP)
1082 #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_LESS_EQUAL)
1083 #define SLJIT_SIG_LESS 6
1084 #define SLJIT_SIG_LESS32 (SLJIT_SIG_LESS | SLJIT_I32_OP)
1085 #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS)
1086 #define SLJIT_SIG_GREATER_EQUAL 7
1087 #define SLJIT_SIG_GREATER_EQUAL32 (SLJIT_SIG_GREATER_EQUAL | SLJIT_I32_OP)
1088 #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SIG_GREATER_EQUAL)
1089 #define SLJIT_SIG_GREATER 8
1090 #define SLJIT_SIG_GREATER32 (SLJIT_SIG_GREATER | SLJIT_I32_OP)
1091 #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER)
1092 #define SLJIT_SIG_LESS_EQUAL 9
1093 #define SLJIT_SIG_LESS_EQUAL32 (SLJIT_SIG_LESS_EQUAL | SLJIT_I32_OP)
1094 #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_LESS_EQUAL)
1095
1096 #define SLJIT_OVERFLOW 10
1097 #define SLJIT_OVERFLOW32 (SLJIT_OVERFLOW | SLJIT_I32_OP)
1098 #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW)
1099 #define SLJIT_NOT_OVERFLOW 11
1100 #define SLJIT_NOT_OVERFLOW32 (SLJIT_NOT_OVERFLOW | SLJIT_I32_OP)
1101
1102 #define SLJIT_MUL_OVERFLOW 12
1103 #define SLJIT_MUL_OVERFLOW32 (SLJIT_MUL_OVERFLOW | SLJIT_I32_OP)
1104 #define SLJIT_SET_MUL_OVERFLOW SLJIT_SET(SLJIT_MUL_OVERFLOW)
1105 #define SLJIT_MUL_NOT_OVERFLOW 13
1106 #define SLJIT_MUL_NOT_OVERFLOW32 (SLJIT_MUL_NOT_OVERFLOW | SLJIT_I32_OP)
1107
1108 /* There is no SLJIT_CARRY or SLJIT_NOT_CARRY. */
1109 #define SLJIT_SET_CARRY SLJIT_SET(14)
1110
1111 /* Floating point comparison types. */
1112 #define SLJIT_EQUAL_F64 16
1113 #define SLJIT_EQUAL_F32 (SLJIT_EQUAL_F64 | SLJIT_F32_OP)
1114 #define SLJIT_SET_EQUAL_F SLJIT_SET(SLJIT_EQUAL_F64)
1115 #define SLJIT_NOT_EQUAL_F64 17
1116 #define SLJIT_NOT_EQUAL_F32 (SLJIT_NOT_EQUAL_F64 | SLJIT_F32_OP)
1117 #define SLJIT_SET_NOT_EQUAL_F SLJIT_SET(SLJIT_NOT_EQUAL_F64)
1118 #define SLJIT_LESS_F64 18
1119 #define SLJIT_LESS_F32 (SLJIT_LESS_F64 | SLJIT_F32_OP)
1120 #define SLJIT_SET_LESS_F SLJIT_SET(SLJIT_LESS_F64)
1121 #define SLJIT_GREATER_EQUAL_F64 19
1122 #define SLJIT_GREATER_EQUAL_F32 (SLJIT_GREATER_EQUAL_F64 | SLJIT_F32_OP)
1123 #define SLJIT_SET_GREATER_EQUAL_F SLJIT_SET(SLJIT_GREATER_EQUAL_F64)
1124 #define SLJIT_GREATER_F64 20
1125 #define SLJIT_GREATER_F32 (SLJIT_GREATER_F64 | SLJIT_F32_OP)
1126 #define SLJIT_SET_GREATER_F SLJIT_SET(SLJIT_GREATER_F64)
1127 #define SLJIT_LESS_EQUAL_F64 21
1128 #define SLJIT_LESS_EQUAL_F32 (SLJIT_LESS_EQUAL_F64 | SLJIT_F32_OP)
1129 #define SLJIT_SET_LESS_EQUAL_F SLJIT_SET(SLJIT_LESS_EQUAL_F64)
1130 #define SLJIT_UNORDERED_F64 22
1131 #define SLJIT_UNORDERED_F32 (SLJIT_UNORDERED_F64 | SLJIT_F32_OP)
1132 #define SLJIT_SET_UNORDERED_F SLJIT_SET(SLJIT_UNORDERED_F64)
1133 #define SLJIT_ORDERED_F64 23
1134 #define SLJIT_ORDERED_F32 (SLJIT_ORDERED_F64 | SLJIT_F32_OP)
1135 #define SLJIT_SET_ORDERED_F SLJIT_SET(SLJIT_ORDERED_F64)
1136
1137 /* Unconditional jump types. */
1138 #define SLJIT_JUMP 24
1139 #define SLJIT_FAST_CALL 25
1140 #define SLJIT_CALL0 26
1141 #define SLJIT_CALL1 27
1142 #define SLJIT_CALL2 28
1143 #define SLJIT_CALL3 29
1144
1145 /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
1146
1147 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
1148 #define SLJIT_REWRITABLE_JUMP 0x1000
1149
1150 /* Emit a jump instruction. The destination is not set, only the type of the jump.
1151 type must be between SLJIT_EQUAL and SLJIT_CALL3
1152 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1153
1154 Flags: does not modify flags for conditional and unconditional
1155 jumps but destroy all flags for calls. */
1156 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type);
1157
1158 /* Basic arithmetic comparison. In most architectures it is implemented as
1159 an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
1160 appropriate flags) followed by a sljit_emit_jump. However some
1161 architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
1162 It is suggested to use this comparison form when appropriate.
1163 type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
1164 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1165 Flags: may destroy flags. */
1166 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
1167 sljit_s32 src1, sljit_sw src1w,
1168 sljit_s32 src2, sljit_sw src2w);
1169
1170 /* Basic floating point comparison. In most architectures it is implemented as
1171 an SLJIT_FCMP operation (setting appropriate flags) followed by a
1172 sljit_emit_jump. However some architectures (i.e: MIPS) may employ
1173 special optimizations here. It is suggested to use this comparison form
1174 when appropriate.
1175 type must be between SLJIT_EQUAL_F64 and SLJIT_ORDERED_F32
1176 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
1177 Flags: destroy flags.
1178 Note: if either operand is NaN, the behaviour is undefined for
1179 types up to SLJIT_S_LESS_EQUAL. */
1180 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_s32 type,
1181 sljit_s32 src1, sljit_sw src1w,
1182 sljit_s32 src2, sljit_sw src2w);
1183
1184 /* Set the destination of the jump to this label. */
1185 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
1186 /* Set the destination address of the jump to this label. */
1187 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
1188
1189 /* Call function or jump anywhere. Both direct and indirect form
1190 type must be between SLJIT_JUMP and SLJIT_CALL3
1191 Direct form: set src to SLJIT_IMM() and srcw to the address
1192 Indirect form: any other valid addressing mode
1193
1194 Flags: does not modify flags for unconditional jumps but
1195 destroy all flags for calls. */
1196 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw);
1197
1198 /* Perform the operation using the conditional flags as the second argument.
1199 Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_F64. The value
1200 represented by the type is 1, if the condition represented by the type
1201 is fulfilled, and 0 otherwise.
1202
1203 If op == SLJIT_MOV, SLJIT_MOV32:
1204 Set dst to the value represented by the type (0 or 1).
1205 Flags: - (does not modify flags)
1206 If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1207 Performs the binary operation using dst as the first, and the value
1208 represented by type as the second argument. Result is written into dst.
1209 Flags: Z (may destroy flags) */
1210 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1211 sljit_s32 dst, sljit_sw dstw,
1212 sljit_s32 type);
1213
1214 /* Emit a conditional mov instruction which moves source to destination,
1215 if the condition is satisfied. Unlike other arithmetic operations this
1216 instruction does not support memory accesses.
1217
1218 type must be between SLJIT_EQUAL and SLJIT_ORDERED_F64
1219 dst_reg must be a valid register and it can be combined
1220 with SLJIT_I32_OP to perform a 32 bit arithmetic operation
1221 src must be register or immediate (SLJIT_IMM)
1222
1223 Flags: - (does not modify flags) */
1224 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1225 sljit_s32 dst_reg,
1226 sljit_s32 src, sljit_sw srcw);
1227
1228 /* Copies the base address of SLJIT_SP + offset to dst.
1229 Flags: - (may destroy flags) */
1230 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset);
1231
1232 /* The constant can be changed runtime (see: sljit_set_const)
1233 Flags: - (does not modify flags) */
1234 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value);
1235
1236 /* After the code generation the address for label, jump and const instructions
1237 are computed. Since these structures are freed by sljit_free_compiler, the
1238 addresses must be preserved by the user program elsewere. */
sljit_get_label_addr(struct sljit_label * label)1239 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
sljit_get_jump_addr(struct sljit_jump * jump)1240 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
sljit_get_const_addr(struct sljit_const * const_)1241 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
1242
1243 /* Only the address and executable offset are required to perform dynamic
1244 code modifications. See sljit_get_executable_offset function. */
1245 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset);
1246 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset);
1247
1248 /* --------------------------------------------------------------------- */
1249 /* Miscellaneous utility functions */
1250 /* --------------------------------------------------------------------- */
1251
1252 #define SLJIT_MAJOR_VERSION 0
1253 #define SLJIT_MINOR_VERSION 94
1254
1255 /* Get the human readable name of the platform. Can be useful on platforms
1256 like ARM, where ARM and Thumb2 functions can be mixed, and
1257 it is useful to know the type of the code generator. */
1258 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void);
1259
1260 /* Portable helper function to get an offset of a member. */
1261 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1262
1263 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
1264 /* This global lock is useful to compile common functions. */
1265 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
1266 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
1267 #endif
1268
1269 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1270
1271 /* The sljit_stack is a utility extension of sljit, which provides
1272 a top-down stack. The stack starts at base and goes down to
1273 max_limit, so the memory region for this stack is between
1274 max_limit (inclusive) and base (exclusive). However the
1275 application can only use the region between limit (inclusive)
1276 and base (exclusive). The sljit_stack_resize can be used to
1277 extend this region up to max_limit.
1278
1279 This feature uses the "address space reserve" feature of modern
1280 operating systems, so instead of allocating a huge memory block
1281 applications can allocate a small region and extend it later
1282 without moving the memory area. Hence pointers can be stored
1283 in this area. */
1284
1285 /* Note: base and max_limit fields are aligned to PAGE_SIZE bytes
1286 (usually 4 Kbyte or more).
1287 Note: stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more.
1288 Note: this structure may not be supported by all operating systems.
1289 Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
1290 is not defined. */
1291
1292 struct sljit_stack {
1293 /* User data, anything can be stored here.
1294 Starting with the same value as base. */
1295 sljit_u8 *top;
1296 /* These members are read only. */
1297 sljit_u8 *base;
1298 sljit_u8 *limit;
1299 sljit_u8 *max_limit;
1300 };
1301
1302 /* Returns NULL if unsuccessful.
1303 Note: max_limit contains the maximum stack size in bytes.
1304 Note: limit contains the starting stack size in bytes.
1305 Note: the top field is initialized to base.
1306 Note: see sljit_create_compiler for the explanation of allocator_data. */
1307 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data);
1308 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
1309
1310 /* Can be used to increase (allocate) or decrease (free) the memory area.
1311 Returns with a non-zero value if unsuccessful. If new_limit is greater than
1312 max_limit, it will fail. It is very easy to implement a stack data structure,
1313 since the growth ratio can be added to the current limit, and sljit_stack_resize
1314 will do all the necessary checks. The fields of the stack are not changed if
1315 sljit_stack_resize fails. */
1316 SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_limit);
1317
1318 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1319
1320 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1321
1322 /* Get the entry address of a given function. */
1323 #define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name)
1324
1325 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1326
1327 /* All JIT related code should be placed in the same context (library, binary, etc.). */
1328
1329 #define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name)
1330
1331 /* For powerpc64, the function pointers point to a context descriptor. */
1332 struct sljit_function_context {
1333 sljit_sw addr;
1334 sljit_sw r2;
1335 sljit_sw r11;
1336 };
1337
1338 /* Fill the context arguments using the addr and the function.
1339 If func_ptr is NULL, it will not be set to the address of context
1340 If addr is NULL, the function address also comes from the func pointer. */
1341 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
1342
1343 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1344
1345 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
1346 /* Free unused executable memory. The allocator keeps some free memory
1347 around to reduce the number of OS executable memory allocations.
1348 This improves performance since these calls are costly. However
1349 it is sometimes desired to free all unused memory regions, e.g.
1350 before the application terminates. */
1351 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
1352 #endif
1353
1354 /* --------------------------------------------------------------------- */
1355 /* CPU specific functions */
1356 /* --------------------------------------------------------------------- */
1357
1358 /* The following function is a helper function for sljit_emit_op_custom.
1359 It returns with the real machine register index ( >=0 ) of any SLJIT_R,
1360 SLJIT_S and SLJIT_SP registers.
1361
1362 Note: it returns with -1 for virtual registers (only on x86-32). */
1363
1364 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg);
1365
1366 /* The following function is a helper function for sljit_emit_op_custom.
1367 It returns with the real machine register index of any SLJIT_FLOAT register.
1368
1369 Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
1370
1371 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg);
1372
1373 /* Any instruction can be inserted into the instruction stream by
1374 sljit_emit_op_custom. It has a similar purpose as inline assembly.
1375 The size parameter must match to the instruction size of the target
1376 architecture:
1377
1378 x86: 0 < size <= 15. The instruction argument can be byte aligned.
1379 Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
1380 if size == 4, the instruction argument must be 4 byte aligned.
1381 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
1382
1383 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1384 void *instruction, sljit_s32 size);
1385
1386 /* Define the currently available CPU status flags. It is usually used after an
1387 sljit_emit_op_custom call to define which flags are set. */
1388
1389 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_current_flags(struct sljit_compiler *compiler,
1390 sljit_s32 current_flags);
1391
1392 #endif /* _SLJIT_LIR_H_ */
1393