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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 867 - (show annotations)
Fri Jan 13 13:34:07 2012 UTC (3 years, 7 months ago) by zherczeg
File MIME type: text/plain
File size: 35488 byte(s)
Error occurred while calculating annotation data.
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
38 In other words, jump into and out of the code is safe
39 - Both target of (conditional) jump and call instructions
40 and constants can be dynamically modified during runtime
41 - although it is not suggested to do it frequently
42 - very effective to cache an important value once
43 - A fixed stack space can be allocated for local variables
44 - The compiler is thread-safe
45 Disadvantages:
46 - Limited number of registers (only 6+4 integer registers, max 3+2
47 temporary and max 3+2 general, and 4 floating point registers)
48 In practice:
49 - This approach is very effective for interpreters
50 - One of the general registers typically points to a stack interface
51 - It can jump to any exception handler anytime (even for another
52 function. It is safe for SLJIT.)
53 - Fast paths can be modified during runtime reflecting the changes
54 of the fastest execution path of the dynamic language
55 - SLJIT supports complex memory addressing modes
56 - mainly position independent code
57 - Optimizations (perhaps later)
58 - Only for basic blocks (when no labels inserted between LIR instructions)
59
60 For valgrind users:
61 - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
62 */
63
64 #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
65 #include "sljitConfig.h"
66 #endif
67
68 /* The following header file defines useful macros for fine tuning
69 sljit based code generators. They are listed in the begining
70 of sljitConfigInternal.h */
71
72 #include "sljitConfigInternal.h"
73
74 /* --------------------------------------------------------------------- */
75 /* Error codes */
76 /* --------------------------------------------------------------------- */
77
78 /* Indicates no error. */
79 #define SLJIT_SUCCESS 0
80 /* After the call of sljit_generate_code(), the error code of the compiler
81 is set to this value to avoid future sljit calls (in debug mode at least).
82 The complier should be freed after sljit_generate_code(). */
83 #define SLJIT_ERR_COMPILED 1
84 /* Cannot allocate non executable memory. */
85 #define SLJIT_ERR_ALLOC_FAILED 2
86 /* Cannot allocate executable memory.
87 Only for sljit_generate_code() */
88 #define SLJIT_ERR_EX_ALLOC_FAILED 3
89 /* return value for SLJIT_CONFIG_UNSUPPORTED empty architecture. */
90 #define SLJIT_ERR_UNSUPPORTED 4
91
92 /* --------------------------------------------------------------------- */
93 /* Registers */
94 /* --------------------------------------------------------------------- */
95
96 #define SLJIT_UNUSED 0
97
98 /* Temporary (scratch) registers may not preserve their values across function calls. */
99 #define SLJIT_TEMPORARY_REG1 1
100 #define SLJIT_TEMPORARY_REG2 2
101 #define SLJIT_TEMPORARY_REG3 3
102 /* Note: Extra Registers cannot be used for memory addressing. */
103 /* Note: on x86-32, these registers are emulated (using stack loads & stores). */
104 #define SLJIT_TEMPORARY_EREG1 4
105 #define SLJIT_TEMPORARY_EREG2 5
106
107 /* General (saved) registers preserve their values across function calls. */
108 #define SLJIT_GENERAL_REG1 6
109 #define SLJIT_GENERAL_REG2 7
110 #define SLJIT_GENERAL_REG3 8
111 /* Note: Extra Registers cannot be used for memory addressing. */
112 /* Note: on x86-32, these registers are emulated (using stack loads & stores). */
113 #define SLJIT_GENERAL_EREG1 9
114 #define SLJIT_GENERAL_EREG2 10
115
116 /* Read-only register (cannot be the destination of an operation). */
117 /* Note: SLJIT_MEM2( ... , SLJIT_LOCALS_REG) is not supported (x86 limitation). */
118 /* Note: SLJIT_LOCALS_REG is not necessary the real stack pointer. See sljit_emit_enter. */
119 #define SLJIT_LOCALS_REG 11
120
121 /* Number of registers. */
122 #define SLJIT_NO_TMP_REGISTERS 5
123 #define SLJIT_NO_GEN_REGISTERS 5
124 #define SLJIT_NO_REGISTERS 11
125
126 /* Return with machine word. */
127
128 #define SLJIT_RETURN_REG SLJIT_TEMPORARY_REG1
129
130 /* x86 prefers specific registers for special purposes. In case of shift
131 by register it supports only SLJIT_TEMPORARY_REG3 for shift argument
132 (which is the src2 argument of sljit_emit_op2). If another register is
133 used, sljit must exchange data between registers which cause a minor
134 slowdown. Other architectures has no such limitation. */
135
136 #define SLJIT_PREF_SHIFT_REG SLJIT_TEMPORARY_REG3
137
138 /* --------------------------------------------------------------------- */
139 /* Floating point registers */
140 /* --------------------------------------------------------------------- */
141
142 /* Note: SLJIT_UNUSED as destination is not valid for floating point
143 operations, since they cannot be used for setting flags. */
144
145 /* Floating point operations are performed on double precision values. */
146
147 #define SLJIT_FLOAT_REG1 1
148 #define SLJIT_FLOAT_REG2 2
149 #define SLJIT_FLOAT_REG3 3
150 #define SLJIT_FLOAT_REG4 4
151
152 /* --------------------------------------------------------------------- */
153 /* Main structures and functions */
154 /* --------------------------------------------------------------------- */
155
156 struct sljit_memory_fragment {
157 struct sljit_memory_fragment *next;
158 sljit_uw used_size;
159 sljit_ub memory[1];
160 };
161
162 struct sljit_label {
163 struct sljit_label *next;
164 sljit_uw addr;
165 /* The maximum size difference. */
166 sljit_uw size;
167 };
168
169 struct sljit_jump {
170 struct sljit_jump *next;
171 sljit_uw addr;
172 sljit_w flags;
173 union {
174 sljit_uw target;
175 struct sljit_label* label;
176 } u;
177 };
178
179 struct sljit_const {
180 struct sljit_const *next;
181 sljit_uw addr;
182 };
183
184 struct sljit_compiler {
185 int error;
186
187 struct sljit_label *labels;
188 struct sljit_jump *jumps;
189 struct sljit_const *consts;
190 struct sljit_label *last_label;
191 struct sljit_jump *last_jump;
192 struct sljit_const *last_const;
193
194 struct sljit_memory_fragment *buf;
195 struct sljit_memory_fragment *abuf;
196
197 /* Used local registers. */
198 int temporaries;
199 /* Used general registers. */
200 int generals;
201 /* Local stack size. */
202 int local_size;
203 /* Code size. */
204 sljit_uw size;
205 /* For statistical purposes. */
206 sljit_uw executable_size;
207
208 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
209 int args;
210 int temporaries_start;
211 int generals_start;
212 #endif
213
214 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
215 int mode32;
216 #ifdef _WIN64
217 int has_locals;
218 #endif
219 #endif
220
221 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
222 int flags_saved;
223 #endif
224
225 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
226 /* Constant pool handling. */
227 sljit_uw *cpool;
228 sljit_ub *cpool_unique;
229 sljit_uw cpool_diff;
230 sljit_uw cpool_fill;
231 /* General fields. */
232 /* Contains pointer, "ldr pc, [...]" pairs. */
233 sljit_uw patches;
234 #endif
235
236 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
237 /* Temporary fields. */
238 sljit_uw shift_imm;
239 int cache_arg;
240 sljit_w cache_argw;
241 #endif
242
243 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
244 int cache_arg;
245 sljit_w cache_argw;
246 #endif
247
248 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
249 int has_locals;
250 sljit_w imm;
251 int cache_arg;
252 sljit_w cache_argw;
253 #endif
254
255 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
256 int has_locals;
257 int delay_slot;
258 int cache_arg;
259 sljit_w cache_argw;
260 #endif
261
262 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
263 FILE* verbose;
264 #endif
265
266 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
267 int skip_checks;
268 #endif
269 };
270
271 /* --------------------------------------------------------------------- */
272 /* Main functions */
273 /* --------------------------------------------------------------------- */
274
275 /* Creates an sljit compiler.
276 Returns NULL if failed. */
277 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void);
278 /* Free everything except the codes. */
279 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
280
281 static SLJIT_INLINE int sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
282
283 /*
284 Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
285 and <= 128 bytes on 64 bit architectures. The memory area is owned by the compiler,
286 and freed by sljit_free_compiler. The returned pointer is sizeof(sljit_w) aligned.
287 Excellent for allocating small blocks during the compiling, and no need to worry
288 about freeing them. The size is enough to contain at most 16 pointers.
289 If the size is outside of the range, the function will return with NULL,
290 but this return value does not indicate that there is no more memory (does
291 not set the compiler to out-of-memory status).
292 */
293 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size);
294
295 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
296 /* Passing NULL disables verbose. */
297 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
298 #endif
299
300 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
301 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
302
303 /*
304 After the code generation we can retrieve the allocated executable memory size,
305 although this area may not be fully filled with instructions depending on some
306 optimizations. This function is useful only for statistical purposes.
307
308 Before a successful code generation, this function returns with 0.
309 */
310 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
311
312 /* Instruction generation. Returns with error code. */
313
314 /*
315 The executable code is basically a function call from the viewpoint of
316 the C language. The function calls must obey to the ABI (Application
317 Binary Interface) of the platform, which specify the purpose of machine
318 registers and stack handling among other things. The sljit_emit_enter
319 function emits the necessary instructions for setting up a new context
320 for the executable code and moves function arguments to the general
321 registers. The number of arguments are specified in the "args"
322 parameter and the first argument goes to SLJIT_GENERAL_REG1, the second
323 goes to SLJIT_GENERAL_REG2 and so on. The number of temporary and
324 general registers are passed in "temporaries" and "generals" arguments
325 respectively. Since the general registers contains the arguments,
326 "args" must be less or equal than "generals". The sljit_emit_enter
327 is also capable of allocating a stack space for local variables. The
328 "local_size" argument contains the size in bytes of this local area
329 and its staring address is stored in SLJIT_LOCALS_REG. However
330 the SLJIT_LOCALS_REG is not necessary the machine stack pointer.
331 The memory bytes between SLJIT_LOCALS_REG (inclusive) and
332 SLJIT_LOCALS_REG + local_size (exclusive) can be modified freely
333 until the function returns. The stack space is uninitialized.
334
335 Note: every call of sljit_emit_enter and sljit_set_context overwrites
336 the previous context. */
337
338 #define SLJIT_MAX_LOCAL_SIZE 65536
339
340 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size);
341
342 /* The machine code has a context (which contains the local stack space size,
343 number of used registers, etc.) which initialized by sljit_emit_enter. Several
344 functions (like sljit_emit_return) requres this context to be able to generate
345 the appropriate code. However, some code fragments (like inline cache) may have
346 no normal entry point so their context is unknown for the compiler. Using the
347 function below we can specify thir context.
348
349 Note: every call of sljit_emit_enter and sljit_set_context overwrites
350 the previous context. */
351
352 /* Note: multiple calls of this function overwrites the previous call. */
353
354 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size);
355
356 /* Return from jit. See below the possible values for src and srcw. */
357 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int src, sljit_w srcw);
358
359 /* Really fast calling method for utility functions inside sljit (see SLJIT_FAST_CALL).
360 All registers and even the stack frame is passed to the callee. The return address is
361 preserved in dst/dstw by sljit_emit_fast_enter, and sljit_emit_fast_return can
362 use this as a return value later. */
363
364 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine instructions
365 are needed. Excellent for small uility functions, where saving general registers and setting up
366 a new stack frame would cost too much performance. However, it is still possible to return
367 to the address of the caller (or anywhere else). */
368
369 /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
370
371 /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
372 since many architectures do clever branch prediction on call / return instruction pairs. */
373
374 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int generals, int local_size);
375 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw);
376
377 /*
378 Source and destination values for arithmetical instructions
379 imm - a simple immediate value (cannot be used as a destination)
380 reg - any of the registers (immediate argument must be 0)
381 [imm] - absolute immediate memory address
382 [reg+imm] - indirect memory address
383 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
384 useful for (byte, half, int, sljit_w) array access
385 (fully supported by both x86 and ARM architectures, and cheap operation on others)
386 */
387
388 /*
389 IMPORATNT NOTE: memory access MUST be naturally aligned except
390 SLJIT_UNALIGNED macro is defined and its value is 1.
391
392 length | alignment
393 ---------+-----------
394 byte | 1 byte (not aligned)
395 half | 2 byte (real_address & 0x1 == 0)
396 int | 4 byte (real_address & 0x3 == 0)
397 sljit_w | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
398 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
399
400 Note: different architectures have different addressing limitations
401 Thus sljit may generate several instructions for other addressing modes
402 x86: all addressing modes supported, but write-back is not supported
403 (requires an extra instruction). On x86-64 only 32 bit signed
404 integers are supported by the architecture.
405 arm: [reg+imm] supported for small immediates (-4095 <= imm <= 4095
406 or -255 <= imm <= 255 for loading signed bytes, any halfs or doubles)
407 [reg+(reg<<imm)] are supported or requires only two instructions
408 Write back is limited to small immediates on thumb2
409 ppc: [reg+imm], -65535 <= imm <= 65535. 64 bit moves requires immediates
410 divisible by 4. [reg+reg] supported, write-back supported
411 [reg+(reg<<imm)] (imm != 0) is cheap (requires two instructions)
412 */
413
414 /* Register output: simply the name of the register.
415 For destination, you can use SLJIT_UNUSED as well. */
416 #define SLJIT_MEM 0x100
417 #define SLJIT_MEM0() (SLJIT_MEM)
418 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
419 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 4))
420 #define SLJIT_IMM 0x200
421
422 /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
423 32 bit CPUs. The arithmetic instruction uses only the lower 32 bit of the
424 input register(s), and set the flags according to the 32 bit result. If the
425 destination is a register, the higher 32 bit of the result is undefined.
426 The addressing modes (SLJIT_MEM1/SLJIT_MEM2 macros) are unaffected by this flag. */
427 #define SLJIT_INT_OP 0x100
428
429 /* Common CPU status flags for all architectures (x86, ARM, PPC)
430 - carry flag
431 - overflow flag
432 - zero flag
433 - negative/positive flag (depends on arc)
434 On mips, these flags are emulated by software. */
435
436 /* By default, the instructions may, or may not set the CPU status flags.
437 Forcing to set or keep status flags can be done with the following flags: */
438
439 /* Note: sljit tries to emit the minimum number of instructions. Using these
440 flags can increase them, so use them wisely to avoid unnecessary code generation. */
441
442 /* Set Equal (Zero) status flag (E). */
443 #define SLJIT_SET_E 0x0200
444 /* Set signed status flag (S). */
445 #define SLJIT_SET_S 0x0400
446 /* Set unsgined status flag (U). */
447 #define SLJIT_SET_U 0x0800
448 /* Set signed overflow flag (O). */
449 #define SLJIT_SET_O 0x1000
450 /* Set carry flag (C).
451 Note: Kinda unsigned overflow, but behaves differently on various cpus. */
452 #define SLJIT_SET_C 0x2000
453 /* Do not modify the flags (K).
454 Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
455 #define SLJIT_KEEP_FLAGS 0x4000
456
457 /* Notes:
458 - you cannot postpone conditional jump instructions except if noted that
459 the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
460 - flag combinations: '|' means 'logical or'. */
461
462 /* Flags: - (never set any flags)
463 Note: breakpoint instruction is not supported by all architectures (namely ppc)
464 It falls back to SLJIT_NOP in those cases. */
465 #define SLJIT_BREAKPOINT 0
466 /* Flags: - (never set any flags)
467 Note: may or may not cause an extra cycle wait
468 it can even decrease the runtime in a few cases. */
469 #define SLJIT_NOP 1
470 /* Flags: may destroy flags
471 Unsigned multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
472 Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
473 #define SLJIT_UMUL 2
474 /* Flags: may destroy flags
475 Signed multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
476 Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
477 #define SLJIT_SMUL 3
478 /* Flags: I | may destroy flags
479 Unsigned divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
480 The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
481 Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
482 #define SLJIT_UDIV 4
483 /* Flags: I | may destroy flags
484 Signed divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
485 The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
486 Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
487 #define SLJIT_SDIV 5
488
489 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op);
490
491 /* Notes for MOV instructions:
492 U = Mov with update (post form). If source or destination defined as SLJIT_MEM1(r1)
493 or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
494 UB = unsigned byte (8 bit)
495 SB = signed byte (8 bit)
496 UH = unsgined half (16 bit)
497 SH = unsgined half (16 bit) */
498
499 /* Flags: - (never set any flags) */
500 #define SLJIT_MOV 6
501 /* Flags: - (never set any flags) */
502 #define SLJIT_MOV_UB 7
503 /* Flags: - (never set any flags) */
504 #define SLJIT_MOV_SB 8
505 /* Flags: - (never set any flags) */
506 #define SLJIT_MOV_UH 9
507 /* Flags: - (never set any flags) */
508 #define SLJIT_MOV_SH 10
509 /* Flags: - (never set any flags) */
510 #define SLJIT_MOV_UI 11
511 /* Flags: - (never set any flags) */
512 #define SLJIT_MOV_SI 12
513 /* Flags: - (never set any flags) */
514 #define SLJIT_MOVU 13
515 /* Flags: - (never set any flags) */
516 #define SLJIT_MOVU_UB 14
517 /* Flags: - (never set any flags) */
518 #define SLJIT_MOVU_SB 15
519 /* Flags: - (never set any flags) */
520 #define SLJIT_MOVU_UH 16
521 /* Flags: - (never set any flags) */
522 #define SLJIT_MOVU_SH 17
523 /* Flags: - (never set any flags) */
524 #define SLJIT_MOVU_UI 18
525 /* Flags: - (never set any flags) */
526 #define SLJIT_MOVU_SI 19
527 /* Flags: I | E | K */
528 #define SLJIT_NOT 20
529 /* Flags: I | E | O | K */
530 #define SLJIT_NEG 21
531 /* Count leading zeroes
532 Flags: I | E | K */
533 #define SLJIT_CLZ 22
534
535 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
536 int dst, sljit_w dstw,
537 int src, sljit_w srcw);
538
539 /* Flags: I | E | O | C | K */
540 #define SLJIT_ADD 23
541 /* Flags: I | C | K */
542 #define SLJIT_ADDC 24
543 /* Flags: I | E | S | U | O | C | K */
544 #define SLJIT_SUB 25
545 /* Flags: I | C | K */
546 #define SLJIT_SUBC 26
547 /* Note: integer mul
548 Flags: I | O (see SLJIT_C_MUL_*) | K */
549 #define SLJIT_MUL 27
550 /* Flags: I | E | K */
551 #define SLJIT_AND 28
552 /* Flags: I | E | K */
553 #define SLJIT_OR 29
554 /* Flags: I | E | K */
555 #define SLJIT_XOR 30
556 /* Flags: I | E | K
557 Let bit_length be the length of the shift operation: 32 or 64.
558 If src2 is immediate, src2w is masked by (bit_length - 1).
559 Otherwise, if the content of src2 is outside the range from 0
560 to bit_length - 1, the operation is undefined. */
561 #define SLJIT_SHL 31
562 /* Flags: I | E | K
563 Let bit_length be the length of the shift operation: 32 or 64.
564 If src2 is immediate, src2w is masked by (bit_length - 1).
565 Otherwise, if the content of src2 is outside the range from 0
566 to bit_length - 1, the operation is undefined. */
567 #define SLJIT_LSHR 32
568 /* Flags: I | E | K
569 Let bit_length be the length of the shift operation: 32 or 64.
570 If src2 is immediate, src2w is masked by (bit_length - 1).
571 Otherwise, if the content of src2 is outside the range from 0
572 to bit_length - 1, the operation is undefined. */
573 #define SLJIT_ASHR 33
574
575 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
576 int dst, sljit_w dstw,
577 int src1, sljit_w src1w,
578 int src2, sljit_w src2w);
579
580 /* The following function is a helper function for sljit_emit_op_custom.
581 It returns with the real machine register index of any SLJIT_TEMPORARY
582 SLJIT_GENERAL or SLJIT_LOCALS register.
583 Note: it returns with -1 for virtual registers (all EREGs on x86-32).
584 Note: register returned by SLJIT_LOCALS_REG is not necessary the real
585 stack pointer register of the target architecture. */
586
587 SLJIT_API_FUNC_ATTRIBUTE int sljit_get_register_index(int reg);
588
589 /* Any instruction can be inserted into the instruction stream by
590 sljit_emit_op_custom. It has a similar purpose as inline assembly.
591 The size parameter must match to the instruction size of the target
592 architecture:
593
594 x86: 0 < size <= 15. The instruction argument can be byte aligned.
595 Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
596 if size == 4, the instruction argument must be 4 byte aligned.
597 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
598
599 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op_custom(struct sljit_compiler *compiler,
600 void *instruction, int size);
601
602 /* Returns with non-zero if fpu is available. */
603
604 SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void);
605
606 /* Note: dst is the left and src is the right operand for SLJIT_FCMP.
607 Note: NaN check is always performed. If SLJIT_C_FLOAT_NAN is set,
608 the comparison result is unpredictable.
609 Flags: E | S (see SLJIT_C_FLOAT_*) */
610 #define SLJIT_FCMP 34
611 /* Flags: - (never set any flags) */
612 #define SLJIT_FMOV 35
613 /* Flags: - (never set any flags) */
614 #define SLJIT_FNEG 36
615 /* Flags: - (never set any flags) */
616 #define SLJIT_FABS 37
617
618 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
619 int dst, sljit_w dstw,
620 int src, sljit_w srcw);
621
622 /* Flags: - (never set any flags) */
623 #define SLJIT_FADD 38
624 /* Flags: - (never set any flags) */
625 #define SLJIT_FSUB 39
626 /* Flags: - (never set any flags) */
627 #define SLJIT_FMUL 40
628 /* Flags: - (never set any flags) */
629 #define SLJIT_FDIV 41
630
631 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
632 int dst, sljit_w dstw,
633 int src1, sljit_w src1w,
634 int src2, sljit_w src2w);
635
636 /* Label and jump instructions. */
637
638 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
639
640 /* Invert conditional instruction: xor (^) with 0x1 */
641 #define SLJIT_C_EQUAL 0
642 #define SLJIT_C_ZERO 0
643 #define SLJIT_C_NOT_EQUAL 1
644 #define SLJIT_C_NOT_ZERO 1
645
646 #define SLJIT_C_LESS 2
647 #define SLJIT_C_GREATER_EQUAL 3
648 #define SLJIT_C_GREATER 4
649 #define SLJIT_C_LESS_EQUAL 5
650 #define SLJIT_C_SIG_LESS 6
651 #define SLJIT_C_SIG_GREATER_EQUAL 7
652 #define SLJIT_C_SIG_GREATER 8
653 #define SLJIT_C_SIG_LESS_EQUAL 9
654
655 #define SLJIT_C_OVERFLOW 10
656 #define SLJIT_C_NOT_OVERFLOW 11
657
658 #define SLJIT_C_MUL_OVERFLOW 12
659 #define SLJIT_C_MUL_NOT_OVERFLOW 13
660
661 #define SLJIT_C_FLOAT_EQUAL 14
662 #define SLJIT_C_FLOAT_NOT_EQUAL 15
663 #define SLJIT_C_FLOAT_LESS 16
664 #define SLJIT_C_FLOAT_GREATER_EQUAL 17
665 #define SLJIT_C_FLOAT_GREATER 18
666 #define SLJIT_C_FLOAT_LESS_EQUAL 19
667 #define SLJIT_C_FLOAT_NAN 20
668 #define SLJIT_C_FLOAT_NOT_NAN 21
669
670 #define SLJIT_JUMP 22
671 #define SLJIT_FAST_CALL 23
672 #define SLJIT_CALL0 24
673 #define SLJIT_CALL1 25
674 #define SLJIT_CALL2 26
675 #define SLJIT_CALL3 27
676
677 /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
678
679 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
680 #define SLJIT_REWRITABLE_JUMP 0x1000
681
682 /* Emit a jump instruction. The destination is not set, only the type of the jump.
683 type must be between SLJIT_C_EQUAL and SLJIT_CALL3
684 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
685 Flags: - (never set any flags) for both conditional and unconditional jumps.
686 Flags: destroy all flags for calls. */
687 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type);
688
689 /* Basic arithmetic comparison. In most architectures it is implemented as
690 an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
691 appropriate flags) followed by a sljit_emit_jump. However some
692 architectures (i.e: MIPS) may employ special optimizations here. It is
693 suggested to use this comparison form when appropriate.
694 type must be between SLJIT_C_EQUAL and SLJIT_C_SIG_LESS_EQUAL
695 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP or SLJIT_INT_OP
696 Flags: destroy flags. */
697 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
698 int src1, sljit_w src1w,
699 int src2, sljit_w src2w);
700
701 /* Basic floating point comparison. In most architectures it is implemented as
702 an SLJIT_FCMP operation (setting appropriate flags) followed by a
703 sljit_emit_jump. However some architectures (i.e: MIPS) may employ
704 special optimizations here. It is suggested to use this comparison form
705 when appropriate.
706 type must be between SLJIT_C_FLOAT_EQUAL and SLJIT_C_FLOAT_NOT_NAN
707 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
708 Flags: destroy flags.
709 Note: if either operand is NaN, the behaviour is undefined for
710 type <= SLJIT_C_FLOAT_LESS_EQUAL. */
711 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, int type,
712 int src1, sljit_w src1w,
713 int src2, sljit_w src2w);
714
715 /* Set the destination of the jump to this label. */
716 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
717 /* Only for jumps defined with SLJIT_REWRITABLE_JUMP flag.
718 Note: use sljit_emit_ijump for fixed jumps. */
719 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
720
721 /* Call function or jump anywhere. Both direct and indirect form
722 type must be between SLJIT_JUMP and SLJIT_CALL3
723 Direct form: set src to SLJIT_IMM() and srcw to the address
724 Indirect form: any other valid addressing mode
725 Flags: - (never set any flags) for unconditional jumps.
726 Flags: destroy all flags for calls. */
727 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw);
728
729 /* If op == SLJIT_MOV:
730 Set dst to 1 if condition is fulfilled, 0 otherwise
731 type must be between SLJIT_C_EQUAL and SLJIT_C_FLOAT_NOT_NAN
732 Flags: - (never set any flags)
733 If op == SLJIT_OR
734 Dst is used as src as well, and set its lowest bit to 1 if
735 the condition is fulfilled. Otherwise it does nothing.
736 Flags: E | K
737 Note: sljit_emit_cond_value does nothing, if dst is SLJIT_UNUSED (regardless of op). */
738 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type);
739
740 /* The constant can be changed runtime (see: sljit_set_const)
741 Flags: - (never set any flags) */
742 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value);
743
744 /* After the code generation the address for label, jump and const instructions
745 are computed. Since these structures are freed sljit_free_compiler, the
746 addresses must be preserved by the user program elsewere. */
747 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
748 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
749 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
750
751 /* Only the address is required to rewrite the code. */
752 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
753 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant);
754
755 /* --------------------------------------------------------------------- */
756 /* Miscellaneous utility functions */
757 /* --------------------------------------------------------------------- */
758
759 #define SLJIT_MAJOR_VERSION 0
760 #define SLJIT_MINOR_VERSION 87
761
762 /* Get the human readable name of the platfrom.
763 Can be useful for debugging on platforms like ARM, where ARM and
764 Thumb2 functions can be mixed. */
765 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
766
767 /* Portble helper function to get an offset of a member. */
768 #define SLJIT_OFFSETOF(base, member) ((sljit_w)(&((base*)0x10)->member) - 0x10)
769
770 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
771 /* This global lock is useful to compile common functions. */
772 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
773 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
774 #endif
775
776 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
777
778 /* The sljit_stack is a utiliy feature of sljit, which allocates a
779 writable memory region between base (inclusive) and limit (exclusive).
780 Both base and limit is a pointer, and base is always <= than limit.
781 This feature uses the "address space reserve" feature
782 of modern operating systems. Basically we don't need to allocate a
783 huge memory block in one step for the worst case, we can start with
784 a smaller chunk and extend it later. Since the address space is
785 reserved, the data never copied to other regions, thus it is safe
786 to store pointers here. */
787
788 /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
789 Note: stack growing should not happen in small steps: 4k, 16k or even
790 bigger growth is better.
791 Note: this structure may not be supported by all operating systems.
792 Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
793 is not defined. */
794
795 struct sljit_stack {
796 /* User data, anything can be stored here.
797 Starting with the same value as base. */
798 sljit_uw top;
799 /* These members are read only. */
800 sljit_uw base;
801 sljit_uw limit;
802 sljit_uw max_limit;
803 };
804
805 /* Returns NULL if unsuccessful.
806 Note: limit and max_limit contains the size for stack allocation
807 Note: the top field is initialized to base. */
808 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit);
809 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack* stack);
810
811 /* Can be used to increase (allocate) or decrease (free) the memory area.
812 Returns with a non-zero value if unsuccessful. If new_limit is greater than
813 max_limit, it will fail. It is very easy to implement a stack data structure,
814 since the growth ratio can be added to the current limit, and sljit_stack_resize
815 will do all the necessary checks. The fields of the stack are not changed if
816 sljit_stack_resize fails. */
817 SLJIT_API_FUNC_ATTRIBUTE sljit_w SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit);
818
819 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
820
821 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
822
823 /* Get the entry address of a given function. */
824 #define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)func_name)
825
826 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
827
828 /* All JIT related code should be placed in the same context (library, binary, etc.). */
829
830 #define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)*(void**)func_name)
831
832 /* For powerpc64, the function pointers point to a context descriptor. */
833 struct sljit_function_context {
834 sljit_w addr;
835 sljit_w r2;
836 sljit_w r11;
837 };
838
839 /* Fill the context arguments using the addr and the function.
840 If func_ptr is NULL, it will not be set to the address of context
841 If addr is NULL, the function address also comes from the func pointer. */
842 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func);
843
844 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
845
846 #endif /* _SLJIT_LIR_H_ */

  ViewVC Help
Powered by ViewVC 1.1.5