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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5