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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1499 - (hide annotations) (download)
Mon Aug 11 06:42:28 2014 UTC (3 weeks, 1 day ago) by zherczeg
File MIME type: text/plain
File size: 50847 byte(s)
JIT compiler update.
1 ph10 662 /*
2     * Stack-less Just-In-Time compiler
3     *
4 ph10 836 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 ph10 662 *
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 zherczeg 1216 - 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 ph10 662 - although it is not suggested to do it frequently
45 zherczeg 1216 - 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 ph10 662 - A fixed stack space can be allocated for local variables
51     - The compiler is thread-safe
52 zherczeg 951 - 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 ph10 662 Disadvantages:
57 zherczeg 1216 - No automatic register allocation, and temporary results are
58     not stored on the stack. (hence the name comes)
59 ph10 662 In practice:
60     - This approach is very effective for interpreters
61 zherczeg 880 - One of the saved registers typically points to a stack interface
62 zherczeg 1216 - 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 ph10 662 of the fastest execution path of the dynamic language
66     - SLJIT supports complex memory addressing modes
67 zherczeg 1216 - mainly position and context independent code (except some cases)
68 zherczeg 742
69     For valgrind users:
70     - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
71 ph10 662 */
72    
73     #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
74     #include "sljitConfig.h"
75     #endif
76 zherczeg 839
77     /* The following header file defines useful macros for fine tuning
78 ph10 1325 sljit based code generators. They are listed in the beginning
79 zherczeg 839 of sljitConfigInternal.h */
80    
81 ph10 662 #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 empty architecture. */
99     #define SLJIT_ERR_UNSUPPORTED 4
100    
101     /* --------------------------------------------------------------------- */
102     /* Registers */
103     /* --------------------------------------------------------------------- */
104    
105 zherczeg 1491 /*
106     Scratch (R) registers: registers whose may not preserve their values
107     across function calls.
108    
109     Saved (S) registers: registers whose preserve their values across
110     function calls.
111    
112     The scratch and saved register sets are overlap. The last scratch register
113     is the first saved register, the one before the last is the second saved
114     register, and so on.
115    
116     If an architecture provides two scratch and three saved registers,
117     its scratch and saved register sets are the following:
118    
119     R0 | [S4] | R0 and S4 represent the same physical register
120     R1 | [S3] | R1 and S3 represent the same physical register
121     [R2] | S2 | R2 and S2 represent the same physical register
122     [R3] | S1 | R3 and S1 represent the same physical register
123     [R4] | S0 | R4 and S0 represent the same physical register
124    
125     Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
126     SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
127    
128     Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 10
129     and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 5. However, 4 registers
130     are virtual on x86-32. See below.
131    
132     The purpose of this definition is convenience. Although a register
133     is either scratch register or saved register, SLJIT allows accessing
134     them from the other set. For example, four registers can be used as
135     scratch registers and the fifth one as saved register on the architecture
136     above. Of course the last two scratch registers (R2 and R3) from this
137     four will be saved on the stack, because they are defined as saved
138     registers in the application binary interface. Still R2 and R3 can be
139     used for referencing to these registers instead of S2 and S1, which
140     makes easier to write platform independent code. Scratch registers
141     can be saved registers in a similar way, but these extra saved
142     registers will not be preserved across function calls! Hence the
143     application must save them on those platforms, where the number of
144     saved registers is too low. This can be done by copy them onto
145     the stack and restore them after a function call.
146    
147     Note: To emphasize that registers assigned to R2-R4 are saved
148     registers, they are enclosed by square brackets. S3-S4
149     are marked in a similar way.
150    
151     Note: sljit_emit_enter and sljit_set_context defines whether a register
152     is S or R register. E.g: when 3 scratches and 1 saved is mapped
153     by sljit_emit_enter, the allowed register set will be: R0-R2 and
154     S0. Although S2 is mapped to the same position as R2, it does not
155     available in the current configuration. Furthermore the R3 (S1)
156     register does not available as well.
157     */
158    
159     /* When SLJIT_UNUSED is specified as destination, the result is discarded. */
160 ph10 662 #define SLJIT_UNUSED 0
161    
162 zherczeg 1491 /* Scratch registers. */
163     #define SLJIT_R0 1
164     #define SLJIT_R1 2
165     #define SLJIT_R2 3
166 zherczeg 1493 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
167     are allocated on the stack). These registers are called virtual
168     and cannot be used for memory addressing (cannot be part of
169     any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
170     limitation on other CPUs. See sljit_get_register_index(). */
171 zherczeg 1491 #define SLJIT_R3 4
172     #define SLJIT_R4 5
173     #define SLJIT_R5 6
174     #define SLJIT_R6 7
175     #define SLJIT_R7 8
176     #define SLJIT_R8 9
177     #define SLJIT_R9 10
178     /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
179     The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
180     #define SLJIT_R(i) (1 + (i))
181 ph10 662
182 zherczeg 1491 /* Saved registers. */
183     #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
184     #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
185     #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
186 zherczeg 1493 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
187     are allocated on the stack). These registers are called virtual
188     and cannot be used for memory addressing (cannot be part of
189     any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
190     limitation on other CPUs. See sljit_get_register_index(). */
191 zherczeg 1491 #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
192     #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
193     #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
194     #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
195     #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
196     #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
197     #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
198     /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
199     The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
200     #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
201 ph10 662
202 zherczeg 1491 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
203     #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
204 ph10 662
205 zherczeg 1491 /* The SLJIT_SP provides direct access to the linear stack space allocated by
206     sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
207     The immediate offset is extended by the relative stack offset automatically.
208     The sljit_get_local_base can be used to obtain the absolute offset. */
209     #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
210 ph10 662
211     /* Return with machine word. */
212    
213 zherczeg 1491 #define SLJIT_RETURN_REG SLJIT_R0
214 ph10 662
215 zherczeg 847 /* x86 prefers specific registers for special purposes. In case of shift
216 zherczeg 1491 by register it supports only SLJIT_R2 for shift argument
217 zherczeg 847 (which is the src2 argument of sljit_emit_op2). If another register is
218     used, sljit must exchange data between registers which cause a minor
219     slowdown. Other architectures has no such limitation. */
220 ph10 662
221 zherczeg 1491 #define SLJIT_PREF_SHIFT_REG SLJIT_R2
222 ph10 662
223     /* --------------------------------------------------------------------- */
224     /* Floating point registers */
225     /* --------------------------------------------------------------------- */
226    
227 zherczeg 1491 /* Each floating point register can store a double or single precision
228     value. The FR and FS register sets are overlap in the same way as R
229     and S register sets. See above. */
230    
231 ph10 662 /* Note: SLJIT_UNUSED as destination is not valid for floating point
232 zherczeg 1491 operations, since they cannot be used for setting flags. */
233 ph10 662
234 zherczeg 1491 /* Floating point scratch registers. */
235     #define SLJIT_FR0 1
236     #define SLJIT_FR1 2
237     #define SLJIT_FR2 3
238     #define SLJIT_FR3 4
239     #define SLJIT_FR4 5
240     #define SLJIT_FR5 6
241     /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
242     The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
243     #define SLJIT_FR(i) (1 + (i))
244 ph10 662
245 zherczeg 1491 /* Floating point saved registers. */
246     #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
247     #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
248     #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
249     #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
250     #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
251     #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
252     /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
253     The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
254     #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
255 ph10 662
256 zherczeg 1491 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
257     #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
258 zherczeg 1280
259 ph10 662 /* --------------------------------------------------------------------- */
260     /* Main structures and functions */
261     /* --------------------------------------------------------------------- */
262    
263 zherczeg 1499 /*
264     The following structures are private, and can be changed in the
265     future. Keeping them here allows code inlining.
266     */
267    
268 ph10 662 struct sljit_memory_fragment {
269     struct sljit_memory_fragment *next;
270     sljit_uw used_size;
271 zherczeg 1195 /* Must be aligned to sljit_sw. */
272 ph10 662 sljit_ub memory[1];
273     };
274    
275     struct sljit_label {
276     struct sljit_label *next;
277     sljit_uw addr;
278     /* The maximum size difference. */
279     sljit_uw size;
280     };
281    
282     struct sljit_jump {
283     struct sljit_jump *next;
284     sljit_uw addr;
285 zherczeg 1195 sljit_sw flags;
286 ph10 662 union {
287     sljit_uw target;
288     struct sljit_label* label;
289     } u;
290     };
291    
292     struct sljit_const {
293     struct sljit_const *next;
294     sljit_uw addr;
295     };
296    
297     struct sljit_compiler {
298 zherczeg 1195 sljit_si error;
299 zherczeg 1499 sljit_si options;
300 ph10 662
301     struct sljit_label *labels;
302     struct sljit_jump *jumps;
303     struct sljit_const *consts;
304     struct sljit_label *last_label;
305     struct sljit_jump *last_jump;
306     struct sljit_const *last_const;
307    
308     struct sljit_memory_fragment *buf;
309     struct sljit_memory_fragment *abuf;
310    
311 zherczeg 1491 /* Used scratch registers. */
312 zherczeg 1215 sljit_si scratches;
313 zherczeg 880 /* Used saved registers. */
314 zherczeg 1195 sljit_si saveds;
315 zherczeg 1491 /* Used float scratch registers. */
316     sljit_si fscratches;
317     /* Used float saved registers. */
318     sljit_si fsaveds;
319 ph10 662 /* Local stack size. */
320 zherczeg 1195 sljit_si local_size;
321 ph10 662 /* Code size. */
322     sljit_uw size;
323 ph10 836 /* For statistical purposes. */
324     sljit_uw executable_size;
325 ph10 662
326     #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
327 zherczeg 1195 sljit_si args;
328 ph10 662 #endif
329    
330     #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
331 zherczeg 1195 sljit_si mode32;
332 ph10 662 #endif
333    
334 zherczeg 1483 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
335 zherczeg 1195 sljit_si flags_saved;
336 ph10 662 #endif
337    
338     #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
339     /* Constant pool handling. */
340     sljit_uw *cpool;
341     sljit_ub *cpool_unique;
342     sljit_uw cpool_diff;
343     sljit_uw cpool_fill;
344 zherczeg 880 /* Other members. */
345 ph10 662 /* Contains pointer, "ldr pc, [...]" pairs. */
346     sljit_uw patches;
347     #endif
348    
349     #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
350     /* Temporary fields. */
351     sljit_uw shift_imm;
352 zherczeg 1195 sljit_si cache_arg;
353     sljit_sw cache_argw;
354 ph10 662 #endif
355    
356     #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
357 zherczeg 1195 sljit_si cache_arg;
358     sljit_sw cache_argw;
359 ph10 662 #endif
360    
361 zherczeg 1453 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
362     sljit_si locals_offset;
363     sljit_si cache_arg;
364     sljit_sw cache_argw;
365     #endif
366    
367 zherczeg 1483 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
368 zherczeg 1195 sljit_sw imm;
369     sljit_si cache_arg;
370     sljit_sw cache_argw;
371 ph10 662 #endif
372    
373 zherczeg 1483 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
374 zherczeg 1195 sljit_si delay_slot;
375     sljit_si cache_arg;
376     sljit_sw cache_argw;
377 ph10 662 #endif
378    
379 zherczeg 1149 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
380 zherczeg 1195 sljit_si delay_slot;
381     sljit_si cache_arg;
382     sljit_sw cache_argw;
383 zherczeg 1149 #endif
384    
385 zherczeg 1378 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
386     sljit_si cache_arg;
387     sljit_sw cache_argw;
388     #endif
389    
390 ph10 662 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
391     FILE* verbose;
392     #endif
393    
394 zherczeg 955 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
395     /* Local size passed to the functions. */
396 zherczeg 1195 sljit_si logical_local_size;
397 zherczeg 955 #endif
398    
399 ph10 662 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
400 zherczeg 1195 sljit_si skip_checks;
401 ph10 662 #endif
402     };
403    
404     /* --------------------------------------------------------------------- */
405     /* Main functions */
406     /* --------------------------------------------------------------------- */
407    
408     /* Creates an sljit compiler.
409     Returns NULL if failed. */
410 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void);
411 zherczeg 1216
412     /* Free everything except the compiled machine code. */
413 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
414 ph10 662
415 ph10 1325 /* Returns the current error code. If an error is occurred, future sljit
416 zherczeg 1216 calls which uses the same compiler argument returns early with the same
417     error code. Thus there is no need for checking the error after every
418     call, it is enough to do it before the code is compiled. Removing
419     these checks increases the performance of the compiling process. */
420 zherczeg 1195 static SLJIT_INLINE sljit_si sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
421 ph10 662
422     /*
423     Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
424 zherczeg 1216 and <= 128 bytes on 64 bit architectures. The memory area is owned by the
425     compiler, and freed by sljit_free_compiler. The returned pointer is
426     sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
427     the compiling, and no need to worry about freeing them. The size is
428     enough to contain at most 16 pointers. If the size is outside of the range,
429     the function will return with NULL. However, this return value does not
430     indicate that there is no more memory (does not set the current error code
431     of the compiler to out-of-memory status).
432 ph10 662 */
433 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size);
434 ph10 662
435     #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
436     /* Passing NULL disables verbose. */
437 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
438 ph10 662 #endif
439    
440 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
441     SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
442 ph10 662
443 ph10 836 /*
444 zherczeg 1216 After the machine code generation is finished we can retrieve the allocated
445     executable memory size, although this area may not be fully filled with
446     instructions depending on some optimizations. This function is useful only
447     for statistical purposes.
448 ph10 836
449     Before a successful code generation, this function returns with 0.
450     */
451     static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
452    
453 zherczeg 1216 /* Instruction generation. Returns with any error code. If there is no
454     error, they return with SLJIT_SUCCESS. */
455 ph10 662
456     /*
457 zherczeg 1491 The executable code is a function call from the viewpoint of the C
458     language. The function calls must obey to the ABI (Application
459     Binary Interface) of the platform, which specify the purpose of
460     all machine registers and stack handling among other things. The
461     sljit_emit_enter function emits the necessary instructions for
462     setting up a new context for the executable code and moves function
463 zherczeg 1499 arguments to the saved registers. Furthermore the options argument
464     can be used to pass configuration options to the compiler. Currently
465     there are no options, so it must be set to 0.
466 ph10 662
467 zherczeg 1499 The number of sljit_sw arguments passed to the generated function
468     are specified in the "args" parameter. The number of arguments must
469     be less than or equal to 3. The first argument goes to SLJIT_S0,
470     the second goes to SLJIT_S1 and so on. The register set used by
471     the function must be declared as well. The number of scratch and
472     saved registers used by the function must be passed to sljit_emit_enter.
473     Only R registers between R0 and "scratches" argument can be used
474     later. E.g. if "scratches" is set to 2, the register set will be
475     limited to R0 and R1. The S registers and the floating point
476     registers ("fscratches" and "fsaveds") are specified in a similar
477     way. The sljit_emit_enter is also capable of allocating a stack
478     space for local variables. The "local_size" argument contains the
479     size in bytes of this local area and its staring address is stored
480     in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
481     SLJIT_SP + local_size (exclusive) can be modified freely until
482     the function returns. The stack space is not initialized.
483    
484 zherczeg 1491 Note: the following conditions must met:
485     0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
486     0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
487     scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
488     0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
489     0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
490     fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
491    
492 zherczeg 1216 Note: every call of sljit_emit_enter and sljit_set_context
493 zherczeg 1491 overwrites the previous context.
494     */
495 ph10 662
496     #define SLJIT_MAX_LOCAL_SIZE 65536
497    
498 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
499 zherczeg 1499 sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
500 zherczeg 1491 sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
501 ph10 662
502 zherczeg 860 /* The machine code has a context (which contains the local stack space size,
503     number of used registers, etc.) which initialized by sljit_emit_enter. Several
504     functions (like sljit_emit_return) requres this context to be able to generate
505     the appropriate code. However, some code fragments (like inline cache) may have
506 zherczeg 1491 no normal entry point so their context is unknown for the compiler. Their context
507     can be provided to the compiler by the sljit_set_context function.
508 ph10 662
509 zherczeg 860 Note: every call of sljit_emit_enter and sljit_set_context overwrites
510     the previous context. */
511    
512 zherczeg 875 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler,
513 zherczeg 1499 sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
514 zherczeg 1491 sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
515 ph10 662
516 zherczeg 875 /* Return from machine code. The op argument can be SLJIT_UNUSED which means the
517     function does not return with anything or any opcode between SLJIT_MOV and
518 zherczeg 1182 SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
519 zherczeg 875 is SLJIT_UNUSED, otherwise see below the description about source and
520     destination arguments. */
521 zherczeg 1216
522 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op,
523     sljit_si src, sljit_sw srcw);
524 ph10 662
525 zherczeg 1216 /* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and
526     even the stack frame is passed to the callee. The return address is preserved in
527     dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function
528     is sljit_p), and sljit_emit_fast_return can use this as a return value later. */
529 ph10 662
530 zherczeg 1182 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine
531     instructions are needed. Excellent for small uility functions, where saving registers
532     and setting up a new stack frame would cost too much performance. However, it is still
533     possible to return to the address of the caller (or anywhere else). */
534 ph10 662
535     /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
536    
537     /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
538     since many architectures do clever branch prediction on call / return instruction pairs. */
539    
540 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw);
541     SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw);
542 ph10 662
543     /*
544     Source and destination values for arithmetical instructions
545     imm - a simple immediate value (cannot be used as a destination)
546     reg - any of the registers (immediate argument must be 0)
547     [imm] - absolute immediate memory address
548     [reg+imm] - indirect memory address
549     [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
550 zherczeg 1195 useful for (byte, half, int, sljit_sw) array access
551 ph10 662 (fully supported by both x86 and ARM architectures, and cheap operation on others)
552     */
553    
554     /*
555 zherczeg 847 IMPORATNT NOTE: memory access MUST be naturally aligned except
556     SLJIT_UNALIGNED macro is defined and its value is 1.
557    
558 ph10 662 length | alignment
559     ---------+-----------
560 zherczeg 1195 byte | 1 byte (any physical_address is accepted)
561     half | 2 byte (physical_address & 0x1 == 0)
562     int | 4 byte (physical_address & 0x3 == 0)
563     word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
564 zherczeg 847 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
565 zherczeg 1195 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
566     | on 64 bit machines)
567 ph10 662
568 zherczeg 1195 Note: Different architectures have different addressing limitations.
569     A single instruction is enough for the following addressing
570     modes. Other adrressing modes are emulated by instruction
571     sequences. This information could help to improve those code
572     generators which focuses only a few architectures.
573    
574 ph10 1325 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
575 zherczeg 1195 [reg+(reg<<imm)] is supported
576     [imm], -2^32+1 <= imm <= 2^32-1 is supported
577     Write-back is not supported
578     arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
579     bytes, any halfs or floating point values)
580     [reg+(reg<<imm)] is supported
581     Write-back is supported
582     arm-t2: [reg+imm], -255 <= imm <= 4095
583     [reg+(reg<<imm)] is supported
584     Write back is supported only for [reg+imm], where -255 <= imm <= 255
585     ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
586     signed load on 64 bit requires immediates divisible by 4.
587     [reg+imm] is not supported for signed 8 bit values.
588     [reg+reg] is supported
589     Write-back is supported except for one instruction: 32 bit signed
590     load with [reg+imm] addressing mode on 64 bit.
591     mips: [reg+imm], -65536 <= imm <= 65535
592     sparc: [reg+imm], -4096 <= imm <= 4095
593     [reg+reg] is supported
594 ph10 662 */
595    
596     /* Register output: simply the name of the register.
597     For destination, you can use SLJIT_UNUSED as well. */
598 zherczeg 1453 #define SLJIT_MEM 0x80
599 ph10 662 #define SLJIT_MEM0() (SLJIT_MEM)
600     #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
601 zherczeg 1453 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
602     #define SLJIT_IMM 0x40
603 ph10 662
604     /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
605 zherczeg 1195 32 bit CPUs. If this flag is set for an arithmetic operation, it uses only the
606     lower 32 bit of the input register(s), and set the CPU status flags according
607     to the 32 bit result. The higher 32 bits are undefined for both the input and
608     output. However, the CPU might not ignore those higher 32 bits, like MIPS, which
609     expects it to be the sign extension of the lower 32 bit. All 32 bit operations
610     are undefined, if this condition is not fulfilled. Therefore, when SLJIT_INT_OP
611     is specified, all register arguments must be the result of other operations with
612     the same SLJIT_INT_OP flag. In other words, although a register can hold either
613     a 64 or 32 bit value, these values cannot be mixed. The only exceptions are
614 zherczeg 1460 SLJIT_IMOV and SLJIT_IMOVU (SLJIT_MOV_SI/SLJIT_MOVU_SI with SLJIT_INT_OP flag)
615     which can convert any source argument to SLJIT_INT_OP compatible result. This
616     conversion might be unnecessary on some CPUs like x86-64, since the upper 32
617     bit is always ignored. In this case SLJIT is clever enough to not generate any
618     instructions if the source and destination operands are the same registers.
619     Affects sljit_emit_op0, sljit_emit_op1 and sljit_emit_op2. */
620 ph10 662 #define SLJIT_INT_OP 0x100
621    
622 zherczeg 1195 /* Single precision mode (SP). This flag is similar to SLJIT_INT_OP, just
623     it applies to floating point registers (it is even the same bit). When
624     this flag is passed, the CPU performs single precision floating point
625     operations. Similar to SLJIT_INT_OP, all register arguments must be the
626     result of other floating point operations with this flag. Affects
627     sljit_emit_fop1, sljit_emit_fop2 and sljit_emit_fcmp. */
628     #define SLJIT_SINGLE_OP 0x100
629    
630 ph10 662 /* Common CPU status flags for all architectures (x86, ARM, PPC)
631     - carry flag
632     - overflow flag
633     - zero flag
634     - negative/positive flag (depends on arc)
635     On mips, these flags are emulated by software. */
636    
637     /* By default, the instructions may, or may not set the CPU status flags.
638     Forcing to set or keep status flags can be done with the following flags: */
639    
640     /* Note: sljit tries to emit the minimum number of instructions. Using these
641     flags can increase them, so use them wisely to avoid unnecessary code generation. */
642    
643     /* Set Equal (Zero) status flag (E). */
644     #define SLJIT_SET_E 0x0200
645 zherczeg 1460 /* Set unsigned status flag (U). */
646     #define SLJIT_SET_U 0x0400
647 ph10 662 /* Set signed status flag (S). */
648 zherczeg 1460 #define SLJIT_SET_S 0x0800
649 ph10 662 /* Set signed overflow flag (O). */
650     #define SLJIT_SET_O 0x1000
651     /* Set carry flag (C).
652     Note: Kinda unsigned overflow, but behaves differently on various cpus. */
653     #define SLJIT_SET_C 0x2000
654     /* Do not modify the flags (K).
655     Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
656     #define SLJIT_KEEP_FLAGS 0x4000
657    
658     /* Notes:
659     - you cannot postpone conditional jump instructions except if noted that
660     the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
661     - flag combinations: '|' means 'logical or'. */
662    
663 zherczeg 1499 /* Starting index of opcodes for sljit_emit_op0. */
664     #define SLJIT_OP0_BASE 0
665    
666 ph10 662 /* Flags: - (never set any flags)
667     Note: breakpoint instruction is not supported by all architectures (namely ppc)
668     It falls back to SLJIT_NOP in those cases. */
669 zherczeg 1499 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
670 ph10 662 /* Flags: - (never set any flags)
671     Note: may or may not cause an extra cycle wait
672     it can even decrease the runtime in a few cases. */
673 zherczeg 1499 #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
674 zherczeg 1195 /* Flags: - (may destroy flags)
675 zherczeg 1491 Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
676     Result goes to SLJIT_R1:SLJIT_R0 (high:low) word */
677 zherczeg 1499 #define SLJIT_UMUL (SLJIT_OP0_BASE + 2)
678 zherczeg 1195 /* Flags: - (may destroy flags)
679 zherczeg 1491 Signed multiplication of SLJIT_R0 and SLJIT_R1.
680     Result goes to SLJIT_R1:SLJIT_R0 (high:low) word */
681 zherczeg 1499 #define SLJIT_SMUL (SLJIT_OP0_BASE + 3)
682 zherczeg 1195 /* Flags: I - (may destroy flags)
683 zherczeg 1491 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
684     The result is placed in SLJIT_R0 and the remainder goes to SLJIT_R1.
685     Note: if SLJIT_R1 contains 0, the behaviour is undefined. */
686 zherczeg 1499 #define SLJIT_UDIV (SLJIT_OP0_BASE + 4)
687 zherczeg 1195 #define SLJIT_IUDIV (SLJIT_UDIV | SLJIT_INT_OP)
688     /* Flags: I - (may destroy flags)
689 zherczeg 1491 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
690     The result is placed in SLJIT_R0 and the remainder goes to SLJIT_R1.
691     Note: if SLJIT_R1 contains 0, the behaviour is undefined. */
692 zherczeg 1499 #define SLJIT_SDIV (SLJIT_OP0_BASE + 5)
693 zherczeg 1195 #define SLJIT_ISDIV (SLJIT_SDIV | SLJIT_INT_OP)
694 ph10 662
695 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op);
696 ph10 662
697 zherczeg 1499 /* Starting index of opcodes for sljit_emit_op1. */
698     #define SLJIT_OP1_BASE 32
699    
700 ph10 662 /* Notes for MOV instructions:
701 zherczeg 1453 U = Mov with update (pre form). If source or destination defined as SLJIT_MEM1(r1)
702 ph10 662 or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
703     UB = unsigned byte (8 bit)
704     SB = signed byte (8 bit)
705 zherczeg 1182 UH = unsigned half (16 bit)
706     SH = signed half (16 bit)
707     UI = unsigned int (32 bit)
708     SI = signed int (32 bit)
709     P = pointer (sljit_p) size */
710 ph10 662
711     /* Flags: - (never set any flags) */
712 zherczeg 1499 #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
713 zherczeg 1195 /* Flags: I - (never set any flags) */
714 zherczeg 1499 #define SLJIT_MOV_UB (SLJIT_OP1_BASE + 1)
715 zherczeg 1195 #define SLJIT_IMOV_UB (SLJIT_MOV_UB | SLJIT_INT_OP)
716     /* Flags: I - (never set any flags) */
717 zherczeg 1499 #define SLJIT_MOV_SB (SLJIT_OP1_BASE + 2)
718 zherczeg 1195 #define SLJIT_IMOV_SB (SLJIT_MOV_SB | SLJIT_INT_OP)
719     /* Flags: I - (never set any flags) */
720 zherczeg 1499 #define SLJIT_MOV_UH (SLJIT_OP1_BASE + 3)
721 zherczeg 1195 #define SLJIT_IMOV_UH (SLJIT_MOV_UH | SLJIT_INT_OP)
722     /* Flags: I - (never set any flags) */
723 zherczeg 1499 #define SLJIT_MOV_SH (SLJIT_OP1_BASE + 4)
724 zherczeg 1195 #define SLJIT_IMOV_SH (SLJIT_MOV_SH | SLJIT_INT_OP)
725     /* Flags: I - (never set any flags)
726     Note: see SLJIT_INT_OP for further details. */
727 zherczeg 1499 #define SLJIT_MOV_UI (SLJIT_OP1_BASE + 5)
728 zherczeg 1460 /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOV. */
729 zherczeg 1195 /* Flags: I - (never set any flags)
730     Note: see SLJIT_INT_OP for further details. */
731 zherczeg 1499 #define SLJIT_MOV_SI (SLJIT_OP1_BASE + 6)
732 zherczeg 1195 #define SLJIT_IMOV (SLJIT_MOV_SI | SLJIT_INT_OP)
733 ph10 662 /* Flags: - (never set any flags) */
734 zherczeg 1499 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 7)
735 ph10 662 /* Flags: - (never set any flags) */
736 zherczeg 1499 #define SLJIT_MOVU (SLJIT_OP1_BASE + 8)
737 zherczeg 1195 /* Flags: I - (never set any flags) */
738 zherczeg 1499 #define SLJIT_MOVU_UB (SLJIT_OP1_BASE + 9)
739 zherczeg 1195 #define SLJIT_IMOVU_UB (SLJIT_MOVU_UB | SLJIT_INT_OP)
740     /* Flags: I - (never set any flags) */
741 zherczeg 1499 #define SLJIT_MOVU_SB (SLJIT_OP1_BASE + 10)
742 zherczeg 1195 #define SLJIT_IMOVU_SB (SLJIT_MOVU_SB | SLJIT_INT_OP)
743     /* Flags: I - (never set any flags) */
744 zherczeg 1499 #define SLJIT_MOVU_UH (SLJIT_OP1_BASE + 11)
745 zherczeg 1195 #define SLJIT_IMOVU_UH (SLJIT_MOVU_UH | SLJIT_INT_OP)
746     /* Flags: I - (never set any flags) */
747 zherczeg 1499 #define SLJIT_MOVU_SH (SLJIT_OP1_BASE + 12)
748 zherczeg 1195 #define SLJIT_IMOVU_SH (SLJIT_MOVU_SH | SLJIT_INT_OP)
749     /* Flags: I - (never set any flags)
750     Note: see SLJIT_INT_OP for further details. */
751 zherczeg 1499 #define SLJIT_MOVU_UI (SLJIT_OP1_BASE + 13)
752 zherczeg 1460 /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOVU. */
753 zherczeg 1195 /* Flags: I - (never set any flags)
754     Note: see SLJIT_INT_OP for further details. */
755 zherczeg 1499 #define SLJIT_MOVU_SI (SLJIT_OP1_BASE + 14)
756 zherczeg 1195 #define SLJIT_IMOVU (SLJIT_MOVU_SI | SLJIT_INT_OP)
757 zherczeg 1182 /* Flags: - (never set any flags) */
758 zherczeg 1499 #define SLJIT_MOVU_P (SLJIT_OP1_BASE + 15)
759 ph10 662 /* Flags: I | E | K */
760 zherczeg 1499 #define SLJIT_NOT (SLJIT_OP1_BASE + 16)
761 zherczeg 1195 #define SLJIT_INOT (SLJIT_NOT | SLJIT_INT_OP)
762 ph10 662 /* Flags: I | E | O | K */
763 zherczeg 1499 #define SLJIT_NEG (SLJIT_OP1_BASE + 17)
764 zherczeg 1195 #define SLJIT_INEG (SLJIT_NEG | SLJIT_INT_OP)
765 ph10 662 /* Count leading zeroes
766 zherczeg 1149 Flags: I | E | K
767     Important note! Sparc 32 does not support K flag, since
768     the required popc instruction is introduced only in sparc 64. */
769 zherczeg 1499 #define SLJIT_CLZ (SLJIT_OP1_BASE + 18)
770 zherczeg 1195 #define SLJIT_ICLZ (SLJIT_CLZ | SLJIT_INT_OP)
771 ph10 662
772 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
773     sljit_si dst, sljit_sw dstw,
774     sljit_si src, sljit_sw srcw);
775 ph10 662
776 zherczeg 1499 /* Starting index of opcodes for sljit_emit_op2. */
777     #define SLJIT_OP2_BASE 96
778    
779 ph10 662 /* Flags: I | E | O | C | K */
780 zherczeg 1499 #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
781 zherczeg 1195 #define SLJIT_IADD (SLJIT_ADD | SLJIT_INT_OP)
782 ph10 662 /* Flags: I | C | K */
783 zherczeg 1499 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
784 zherczeg 1195 #define SLJIT_IADDC (SLJIT_ADDC | SLJIT_INT_OP)
785 zherczeg 1460 /* Flags: I | E | U | S | O | C | K */
786 zherczeg 1499 #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
787 zherczeg 1195 #define SLJIT_ISUB (SLJIT_SUB | SLJIT_INT_OP)
788 ph10 662 /* Flags: I | C | K */
789 zherczeg 1499 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
790 zherczeg 1195 #define SLJIT_ISUBC (SLJIT_SUBC | SLJIT_INT_OP)
791 zherczeg 860 /* Note: integer mul
792     Flags: I | O (see SLJIT_C_MUL_*) | K */
793 zherczeg 1499 #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
794 zherczeg 1195 #define SLJIT_IMUL (SLJIT_MUL | SLJIT_INT_OP)
795 ph10 662 /* Flags: I | E | K */
796 zherczeg 1499 #define SLJIT_AND (SLJIT_OP2_BASE + 5)
797 zherczeg 1195 #define SLJIT_IAND (SLJIT_AND | SLJIT_INT_OP)
798 ph10 662 /* Flags: I | E | K */
799 zherczeg 1499 #define SLJIT_OR (SLJIT_OP2_BASE + 6)
800 zherczeg 1195 #define SLJIT_IOR (SLJIT_OR | SLJIT_INT_OP)
801 ph10 662 /* Flags: I | E | K */
802 zherczeg 1499 #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
803 zherczeg 1195 #define SLJIT_IXOR (SLJIT_XOR | SLJIT_INT_OP)
804 zherczeg 860 /* Flags: I | E | K
805     Let bit_length be the length of the shift operation: 32 or 64.
806     If src2 is immediate, src2w is masked by (bit_length - 1).
807     Otherwise, if the content of src2 is outside the range from 0
808 zherczeg 1499 to bit_length - 1, the result is undefined. */
809     #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
810 zherczeg 1195 #define SLJIT_ISHL (SLJIT_SHL | SLJIT_INT_OP)
811 zherczeg 860 /* Flags: I | E | K
812     Let bit_length be the length of the shift operation: 32 or 64.
813     If src2 is immediate, src2w is masked by (bit_length - 1).
814     Otherwise, if the content of src2 is outside the range from 0
815 zherczeg 1499 to bit_length - 1, the result is undefined. */
816     #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
817 zherczeg 1195 #define SLJIT_ILSHR (SLJIT_LSHR | SLJIT_INT_OP)
818 zherczeg 860 /* Flags: I | E | K
819     Let bit_length be the length of the shift operation: 32 or 64.
820     If src2 is immediate, src2w is masked by (bit_length - 1).
821     Otherwise, if the content of src2 is outside the range from 0
822 zherczeg 1499 to bit_length - 1, the result is undefined. */
823     #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
824 zherczeg 1195 #define SLJIT_IASHR (SLJIT_ASHR | SLJIT_INT_OP)
825 ph10 662
826 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
827     sljit_si dst, sljit_sw dstw,
828     sljit_si src1, sljit_sw src1w,
829     sljit_si src2, sljit_sw src2w);
830 ph10 662
831 zherczeg 839 /* The following function is a helper function for sljit_emit_op_custom.
832 zherczeg 1493 It returns with the real machine register index ( >=0 ) of any SLJIT_R,
833     SLJIT_S and SLJIT_SP registers.
834 zherczeg 839
835 zherczeg 1493 Note: it returns with -1 for virtual registers (only on x86-32). */
836    
837 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg);
838 zherczeg 839
839 zherczeg 1280 /* The following function is a helper function for sljit_emit_op_custom.
840     It returns with the real machine register index of any SLJIT_FLOAT register.
841 zherczeg 1493
842     Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
843 zherczeg 1280
844     SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg);
845    
846 zherczeg 839 /* Any instruction can be inserted into the instruction stream by
847     sljit_emit_op_custom. It has a similar purpose as inline assembly.
848     The size parameter must match to the instruction size of the target
849     architecture:
850    
851     x86: 0 < size <= 15. The instruction argument can be byte aligned.
852     Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
853     if size == 4, the instruction argument must be 4 byte aligned.
854     Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
855    
856 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
857     void *instruction, sljit_si size);
858 zherczeg 839
859     /* Returns with non-zero if fpu is available. */
860    
861 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void);
862 ph10 662
863 zherczeg 1499 /* Starting index of opcodes for sljit_emit_fop1. */
864     #define SLJIT_FOP1_BASE 128
865    
866 zherczeg 1195 /* Flags: SP - (never set any flags) */
867 zherczeg 1499 #define SLJIT_MOVD (SLJIT_FOP1_BASE + 0)
868 zherczeg 1195 #define SLJIT_MOVS (SLJIT_MOVD | SLJIT_SINGLE_OP)
869 zherczeg 1483 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
870     SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
871     Rounding mode when the destination is W or I: round towards zero. */
872     /* Flags: SP - (never set any flags) */
873 zherczeg 1499 #define SLJIT_CONVD_FROMS (SLJIT_FOP1_BASE + 1)
874 zherczeg 1483 #define SLJIT_CONVS_FROMD (SLJIT_CONVD_FROMS | SLJIT_SINGLE_OP)
875     /* Flags: SP - (never set any flags) */
876 zherczeg 1499 #define SLJIT_CONVW_FROMD (SLJIT_FOP1_BASE + 2)
877 zherczeg 1483 #define SLJIT_CONVW_FROMS (SLJIT_CONVW_FROMD | SLJIT_SINGLE_OP)
878     /* Flags: SP - (never set any flags) */
879 zherczeg 1499 #define SLJIT_CONVI_FROMD (SLJIT_FOP1_BASE + 3)
880 zherczeg 1483 #define SLJIT_CONVI_FROMS (SLJIT_CONVI_FROMD | SLJIT_SINGLE_OP)
881     /* Flags: SP - (never set any flags) */
882 zherczeg 1499 #define SLJIT_CONVD_FROMW (SLJIT_FOP1_BASE + 4)
883 zherczeg 1483 #define SLJIT_CONVS_FROMW (SLJIT_CONVD_FROMW | SLJIT_SINGLE_OP)
884     /* Flags: SP - (never set any flags) */
885 zherczeg 1499 #define SLJIT_CONVD_FROMI (SLJIT_FOP1_BASE + 5)
886 zherczeg 1483 #define SLJIT_CONVS_FROMI (SLJIT_CONVD_FROMI | SLJIT_SINGLE_OP)
887     /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
888     Note: NaN check is always performed. If SLJIT_C_FLOAT_UNORDERED flag
889     is set, the comparison result is unpredictable.
890     Flags: SP | E | S (see SLJIT_C_FLOAT_*) */
891 zherczeg 1499 #define SLJIT_CMPD (SLJIT_FOP1_BASE + 6)
892 zherczeg 1483 #define SLJIT_CMPS (SLJIT_CMPD | SLJIT_SINGLE_OP)
893 zherczeg 1499 /* Flags: SP - (never set any flags) */
894     #define SLJIT_NEGD (SLJIT_FOP1_BASE + 7)
895     #define SLJIT_NEGS (SLJIT_NEGD | SLJIT_SINGLE_OP)
896     /* Flags: SP - (never set any flags) */
897     #define SLJIT_ABSD (SLJIT_FOP1_BASE + 8)
898     #define SLJIT_ABSS (SLJIT_ABSD | SLJIT_SINGLE_OP)
899 ph10 662
900 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
901     sljit_si dst, sljit_sw dstw,
902     sljit_si src, sljit_sw srcw);
903 ph10 662
904 zherczeg 1499 /* Starting index of opcodes for sljit_emit_fop2. */
905     #define SLJIT_FOP2_BASE 160
906    
907 zherczeg 1195 /* Flags: SP - (never set any flags) */
908 zherczeg 1499 #define SLJIT_ADDD (SLJIT_FOP2_BASE + 0)
909 zherczeg 1195 #define SLJIT_ADDS (SLJIT_ADDD | SLJIT_SINGLE_OP)
910     /* Flags: SP - (never set any flags) */
911 zherczeg 1499 #define SLJIT_SUBD (SLJIT_FOP2_BASE + 1)
912 zherczeg 1195 #define SLJIT_SUBS (SLJIT_SUBD | SLJIT_SINGLE_OP)
913     /* Flags: SP - (never set any flags) */
914 zherczeg 1499 #define SLJIT_MULD (SLJIT_FOP2_BASE + 2)
915 zherczeg 1195 #define SLJIT_MULS (SLJIT_MULD | SLJIT_SINGLE_OP)
916     /* Flags: SP - (never set any flags) */
917 zherczeg 1499 #define SLJIT_DIVD (SLJIT_FOP2_BASE + 3)
918 zherczeg 1195 #define SLJIT_DIVS (SLJIT_DIVD | SLJIT_SINGLE_OP)
919 ph10 662
920 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
921     sljit_si dst, sljit_sw dstw,
922     sljit_si src1, sljit_sw src1w,
923     sljit_si src2, sljit_sw src2w);
924 ph10 662
925     /* Label and jump instructions. */
926    
927 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
928 ph10 662
929     /* Invert conditional instruction: xor (^) with 0x1 */
930     #define SLJIT_C_EQUAL 0
931     #define SLJIT_C_ZERO 0
932     #define SLJIT_C_NOT_EQUAL 1
933     #define SLJIT_C_NOT_ZERO 1
934    
935     #define SLJIT_C_LESS 2
936     #define SLJIT_C_GREATER_EQUAL 3
937     #define SLJIT_C_GREATER 4
938     #define SLJIT_C_LESS_EQUAL 5
939     #define SLJIT_C_SIG_LESS 6
940     #define SLJIT_C_SIG_GREATER_EQUAL 7
941     #define SLJIT_C_SIG_GREATER 8
942     #define SLJIT_C_SIG_LESS_EQUAL 9
943    
944     #define SLJIT_C_OVERFLOW 10
945     #define SLJIT_C_NOT_OVERFLOW 11
946    
947     #define SLJIT_C_MUL_OVERFLOW 12
948     #define SLJIT_C_MUL_NOT_OVERFLOW 13
949    
950     #define SLJIT_C_FLOAT_EQUAL 14
951     #define SLJIT_C_FLOAT_NOT_EQUAL 15
952     #define SLJIT_C_FLOAT_LESS 16
953     #define SLJIT_C_FLOAT_GREATER_EQUAL 17
954     #define SLJIT_C_FLOAT_GREATER 18
955     #define SLJIT_C_FLOAT_LESS_EQUAL 19
956 zherczeg 1149 #define SLJIT_C_FLOAT_UNORDERED 20
957     #define SLJIT_C_FLOAT_ORDERED 21
958 ph10 662
959     #define SLJIT_JUMP 22
960 zherczeg 722 #define SLJIT_FAST_CALL 23
961     #define SLJIT_CALL0 24
962     #define SLJIT_CALL1 25
963     #define SLJIT_CALL2 26
964     #define SLJIT_CALL3 27
965 ph10 662
966     /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
967    
968     /* The target can be changed during runtime (see: sljit_set_jump_addr). */
969     #define SLJIT_REWRITABLE_JUMP 0x1000
970    
971     /* Emit a jump instruction. The destination is not set, only the type of the jump.
972     type must be between SLJIT_C_EQUAL and SLJIT_CALL3
973     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
974     Flags: - (never set any flags) for both conditional and unconditional jumps.
975     Flags: destroy all flags for calls. */
976 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type);
977 ph10 662
978 zherczeg 867 /* Basic arithmetic comparison. In most architectures it is implemented as
979     an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
980     appropriate flags) followed by a sljit_emit_jump. However some
981     architectures (i.e: MIPS) may employ special optimizations here. It is
982     suggested to use this comparison form when appropriate.
983 ph10 662 type must be between SLJIT_C_EQUAL and SLJIT_C_SIG_LESS_EQUAL
984     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP or SLJIT_INT_OP
985     Flags: destroy flags. */
986 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
987     sljit_si src1, sljit_sw src1w,
988     sljit_si src2, sljit_sw src2w);
989 ph10 662
990 zherczeg 867 /* Basic floating point comparison. In most architectures it is implemented as
991     an SLJIT_FCMP operation (setting appropriate flags) followed by a
992     sljit_emit_jump. However some architectures (i.e: MIPS) may employ
993     special optimizations here. It is suggested to use this comparison form
994     when appropriate.
995 zherczeg 1149 type must be between SLJIT_C_FLOAT_EQUAL and SLJIT_C_FLOAT_ORDERED
996 zherczeg 1195 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP and SLJIT_SINGLE_OP
997 zherczeg 867 Flags: destroy flags.
998     Note: if either operand is NaN, the behaviour is undefined for
999     type <= SLJIT_C_FLOAT_LESS_EQUAL. */
1000 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1001     sljit_si src1, sljit_sw src1w,
1002     sljit_si src2, sljit_sw src2w);
1003 zherczeg 867
1004 ph10 662 /* Set the destination of the jump to this label. */
1005 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
1006 zherczeg 1460 /* Set the destination address of the jump to this label. */
1007 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
1008 ph10 662
1009     /* Call function or jump anywhere. Both direct and indirect form
1010     type must be between SLJIT_JUMP and SLJIT_CALL3
1011     Direct form: set src to SLJIT_IMM() and srcw to the address
1012     Indirect form: any other valid addressing mode
1013     Flags: - (never set any flags) for unconditional jumps.
1014     Flags: destroy all flags for calls. */
1015 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw);
1016 ph10 662
1017 zherczeg 1209 /* Perform the operation using the conditional flags as the second argument.
1018     Type must always be between SLJIT_C_EQUAL and SLJIT_C_FLOAT_ORDERED. The
1019     value represented by the type is 1, if the condition represented by the type
1020     is fulfilled, and 0 otherwise.
1021    
1022     If op == SLJIT_MOV, SLJIT_MOV_SI, SLJIT_MOV_UI:
1023     Set dst to the value represented by the type (0 or 1).
1024     Src must be SLJIT_UNUSED, and srcw must be 0
1025 ph10 662 Flags: - (never set any flags)
1026 zherczeg 1209 If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
1027     Performs the binary operation using src as the first, and the value
1028     represented by type as the second argument.
1029     Important note: only dst=src and dstw=srcw is supported at the moment!
1030 zherczeg 1203 Flags: I | E | K
1031 zherczeg 1209 Note: sljit_emit_op_flags does nothing, if dst is SLJIT_UNUSED (regardless of op). */
1032     SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1033     sljit_si dst, sljit_sw dstw,
1034     sljit_si src, sljit_sw srcw,
1035     sljit_si type);
1036 ph10 662
1037 zherczeg 1491 /* Copies the base address of SLJIT_SP + offset to dst.
1038 zherczeg 955 Flags: - (never set any flags) */
1039 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset);
1040 zherczeg 955
1041 ph10 662 /* The constant can be changed runtime (see: sljit_set_const)
1042     Flags: - (never set any flags) */
1043 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value);
1044 ph10 662
1045     /* After the code generation the address for label, jump and const instructions
1046 zherczeg 1222 are computed. Since these structures are freed by sljit_free_compiler, the
1047 ph10 662 addresses must be preserved by the user program elsewere. */
1048     static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
1049     static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
1050     static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
1051    
1052     /* Only the address is required to rewrite the code. */
1053 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
1054 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant);
1055 ph10 662
1056     /* --------------------------------------------------------------------- */
1057     /* Miscellaneous utility functions */
1058     /* --------------------------------------------------------------------- */
1059    
1060     #define SLJIT_MAJOR_VERSION 0
1061 zherczeg 1483 #define SLJIT_MINOR_VERSION 92
1062 ph10 662
1063 zherczeg 1222 /* Get the human readable name of the platform. Can be useful on platforms
1064 zherczeg 1216 like ARM, where ARM and Thumb2 functions can be mixed, and
1065     it is useful to know the type of the code generator. */
1066 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
1067 ph10 662
1068 zherczeg 1222 /* Portable helper function to get an offset of a member. */
1069 zherczeg 1195 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
1070 ph10 662
1071     #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
1072     /* This global lock is useful to compile common functions. */
1073 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
1074     SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
1075 ph10 662 #endif
1076    
1077     #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
1078    
1079     /* The sljit_stack is a utiliy feature of sljit, which allocates a
1080     writable memory region between base (inclusive) and limit (exclusive).
1081     Both base and limit is a pointer, and base is always <= than limit.
1082     This feature uses the "address space reserve" feature
1083     of modern operating systems. Basically we don't need to allocate a
1084     huge memory block in one step for the worst case, we can start with
1085     a smaller chunk and extend it later. Since the address space is
1086     reserved, the data never copied to other regions, thus it is safe
1087     to store pointers here. */
1088    
1089     /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
1090     Note: stack growing should not happen in small steps: 4k, 16k or even
1091     bigger growth is better.
1092     Note: this structure may not be supported by all operating systems.
1093     Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
1094     is not defined. */
1095    
1096     struct sljit_stack {
1097     /* User data, anything can be stored here.
1098     Starting with the same value as base. */
1099     sljit_uw top;
1100     /* These members are read only. */
1101     sljit_uw base;
1102     sljit_uw limit;
1103     sljit_uw max_limit;
1104     };
1105    
1106     /* Returns NULL if unsuccessful.
1107     Note: limit and max_limit contains the size for stack allocation
1108     Note: the top field is initialized to base. */
1109 zherczeg 740 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit);
1110     SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack* stack);
1111 ph10 662
1112     /* Can be used to increase (allocate) or decrease (free) the memory area.
1113     Returns with a non-zero value if unsuccessful. If new_limit is greater than
1114     max_limit, it will fail. It is very easy to implement a stack data structure,
1115     since the growth ratio can be added to the current limit, and sljit_stack_resize
1116     will do all the necessary checks. The fields of the stack are not changed if
1117     sljit_stack_resize fails. */
1118 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit);
1119 ph10 662
1120     #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
1121    
1122     #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
1123    
1124     /* Get the entry address of a given function. */
1125 zherczeg 1195 #define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name)
1126 ph10 662
1127     #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1128    
1129     /* All JIT related code should be placed in the same context (library, binary, etc.). */
1130    
1131 zherczeg 1222 #define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name)
1132 ph10 662
1133     /* For powerpc64, the function pointers point to a context descriptor. */
1134     struct sljit_function_context {
1135 zherczeg 1195 sljit_sw addr;
1136     sljit_sw r2;
1137     sljit_sw r11;
1138 ph10 662 };
1139    
1140     /* Fill the context arguments using the addr and the function.
1141     If func_ptr is NULL, it will not be set to the address of context
1142     If addr is NULL, the function address also comes from the func pointer. */
1143 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
1144 ph10 662
1145     #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
1146    
1147     #endif /* _SLJIT_LIR_H_ */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12