/[pcre]/code/trunk/sljit/sljitLir.h
ViewVC logotype

Contents of /code/trunk/sljit/sljitLir.h

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5