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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5