--- embedaddon/pcre/sljit/sljitLir.h 2012/10/09 09:19:18 1.1.1.3 +++ embedaddon/pcre/sljit/sljitLir.h 2013/07/22 08:25:57 1.1.1.4 @@ -34,12 +34,19 @@ Short description Advantages: - - The execution can be continued from any LIR instruction - In other words, jump into and out of the code is safe - - Both target of (conditional) jump and call instructions - and constants can be dynamically modified during runtime + - The execution can be continued from any LIR instruction. In other + words, it is possible to jump to any label from anywhere, even from + a code fragment, which is compiled later, if both compiled code + shares the same context. See sljit_emit_enter for more details + - Supports self modifying code: target of (conditional) jump and call + instructions and some constant values can be dynamically modified + during runtime - although it is not suggested to do it frequently - - very effective to cache an important value once + - can be used for inline caching: save an important value once + in the instruction stream + - since this feature limits the optimization possibilities, a + special flag must be passed at compile time when these + instructions are emitted - A fixed stack space can be allocated for local variables - The compiler is thread-safe - The compiler is highly configurable through preprocessor macros. @@ -47,19 +54,19 @@ threaded applications), and you can use your own system functions (including memory allocators). See sljitConfig.h Disadvantages: + - No automatic register allocation, and temporary results are + not stored on the stack. (hence the name comes) - Limited number of registers (only 6+4 integer registers, max 3+2 - temporary, max 3+2 saved and 4 floating point registers) + scratch, max 3+2 saved and 6 floating point registers) In practice: - This approach is very effective for interpreters - One of the saved registers typically points to a stack interface - - It can jump to any exception handler anytime (even for another - function. It is safe for SLJIT.) - - Fast paths can be modified during runtime reflecting the changes + - It can jump to any exception handler anytime (even if it belongs + to another function) + - Hot paths can be modified during runtime reflecting the changes of the fastest execution path of the dynamic language - SLJIT supports complex memory addressing modes - - mainly position independent code - - Optimizations (perhaps later) - - Only for basic blocks (when no labels inserted between LIR instructions) + - mainly position and context independent code (except some cases) For valgrind users: - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code" @@ -70,7 +77,7 @@ #endif /* The following header file defines useful macros for fine tuning -sljit based code generators. They are listed in the begining +sljit based code generators. They are listed in the beginning of sljitConfigInternal.h */ #include "sljitConfigInternal.h" @@ -99,12 +106,14 @@ of sljitConfigInternal.h */ #define SLJIT_UNUSED 0 -/* Temporary (scratch) registers may not preserve their values across function calls. */ -#define SLJIT_TEMPORARY_REG1 1 -#define SLJIT_TEMPORARY_REG2 2 -#define SLJIT_TEMPORARY_REG3 3 -/* Note: Extra Registers cannot be used for memory addressing. */ -/* Note: on x86-32, these registers are emulated (using stack loads & stores). */ +/* Scratch (temporary) registers whose may not preserve their values + across function calls. */ +#define SLJIT_SCRATCH_REG1 1 +#define SLJIT_SCRATCH_REG2 2 +#define SLJIT_SCRATCH_REG3 3 +/* Note: extra registers cannot be used for memory addressing. */ +/* Note: on x86-32, these registers are emulated (using stack + loads & stores). */ #define SLJIT_TEMPORARY_EREG1 4 #define SLJIT_TEMPORARY_EREG2 5 @@ -112,15 +121,17 @@ of sljitConfigInternal.h */ #define SLJIT_SAVED_REG1 6 #define SLJIT_SAVED_REG2 7 #define SLJIT_SAVED_REG3 8 -/* Note: Extra Registers cannot be used for memory addressing. */ -/* Note: on x86-32, these registers are emulated (using stack loads & stores). */ +/* Note: extra registers cannot be used for memory addressing. */ +/* Note: on x86-32, these registers are emulated (using stack + loads & stores). */ #define SLJIT_SAVED_EREG1 9 #define SLJIT_SAVED_EREG2 10 /* Read-only register (cannot be the destination of an operation). Only SLJIT_MEM1(SLJIT_LOCALS_REG) addressing mode is allowed since several ABIs has certain limitations about the stack layout. However - sljit_get_local_base() can be used to obtain the offset of a value. */ + sljit_get_local_base() can be used to obtain the offset of a value + on the stack. */ #define SLJIT_LOCALS_REG 11 /* Number of registers. */ @@ -130,15 +141,15 @@ of sljitConfigInternal.h */ /* Return with machine word. */ -#define SLJIT_RETURN_REG SLJIT_TEMPORARY_REG1 +#define SLJIT_RETURN_REG SLJIT_SCRATCH_REG1 /* x86 prefers specific registers for special purposes. In case of shift - by register it supports only SLJIT_TEMPORARY_REG3 for shift argument + by register it supports only SLJIT_SCRATCH_REG3 for shift argument (which is the src2 argument of sljit_emit_op2). If another register is used, sljit must exchange data between registers which cause a minor slowdown. Other architectures has no such limitation. */ -#define SLJIT_PREF_SHIFT_REG SLJIT_TEMPORARY_REG3 +#define SLJIT_PREF_SHIFT_REG SLJIT_SCRATCH_REG3 /* --------------------------------------------------------------------- */ /* Floating point registers */ @@ -147,13 +158,18 @@ of sljitConfigInternal.h */ /* Note: SLJIT_UNUSED as destination is not valid for floating point operations, since they cannot be used for setting flags. */ -/* Floating point operations are performed on double precision values. */ +/* Floating point operations are performed on double or + single precision values. */ -#define SLJIT_FLOAT_REG1 1 -#define SLJIT_FLOAT_REG2 2 -#define SLJIT_FLOAT_REG3 3 -#define SLJIT_FLOAT_REG4 4 +#define SLJIT_FLOAT_REG1 1 +#define SLJIT_FLOAT_REG2 2 +#define SLJIT_FLOAT_REG3 3 +#define SLJIT_FLOAT_REG4 4 +#define SLJIT_FLOAT_REG5 5 +#define SLJIT_FLOAT_REG6 6 +#define SLJIT_NO_FLOAT_REGISTERS 6 + /* --------------------------------------------------------------------- */ /* Main structures and functions */ /* --------------------------------------------------------------------- */ @@ -161,6 +177,7 @@ of sljitConfigInternal.h */ struct sljit_memory_fragment { struct sljit_memory_fragment *next; sljit_uw used_size; + /* Must be aligned to sljit_sw. */ sljit_ub memory[1]; }; @@ -174,7 +191,7 @@ struct sljit_label { struct sljit_jump { struct sljit_jump *next; sljit_uw addr; - sljit_w flags; + sljit_sw flags; union { sljit_uw target; struct sljit_label* label; @@ -187,7 +204,7 @@ struct sljit_const { }; struct sljit_compiler { - int error; + sljit_si error; struct sljit_label *labels; struct sljit_jump *jumps; @@ -200,29 +217,29 @@ struct sljit_compiler { struct sljit_memory_fragment *abuf; /* Used local registers. */ - int temporaries; + sljit_si scratches; /* Used saved registers. */ - int saveds; + sljit_si saveds; /* Local stack size. */ - int local_size; + sljit_si local_size; /* Code size. */ sljit_uw size; /* For statistical purposes. */ sljit_uw executable_size; #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) - int args; - int locals_offset; - int temporaries_start; - int saveds_start; + sljit_si args; + sljit_si locals_offset; + sljit_si scratches_start; + sljit_si saveds_start; #endif #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) - int mode32; + sljit_si mode32; #endif #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) - int flags_saved; + sljit_si flags_saved; #endif #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) @@ -239,38 +256,44 @@ struct sljit_compiler { #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) /* Temporary fields. */ sljit_uw shift_imm; - int cache_arg; - sljit_w cache_argw; + sljit_si cache_arg; + sljit_sw cache_argw; #endif #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2) - int cache_arg; - sljit_w cache_argw; + sljit_si cache_arg; + sljit_sw cache_argw; #endif #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64) - sljit_w imm; - int cache_arg; - sljit_w cache_argw; + sljit_sw imm; + sljit_si cache_arg; + sljit_sw cache_argw; #endif #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) - int delay_slot; - int cache_arg; - sljit_w cache_argw; + sljit_si delay_slot; + sljit_si cache_arg; + sljit_sw cache_argw; #endif +#if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) + sljit_si delay_slot; + sljit_si cache_arg; + sljit_sw cache_argw; +#endif + #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) FILE* verbose; #endif #if (defined SLJIT_DEBUG && SLJIT_DEBUG) /* Local size passed to the functions. */ - int logical_local_size; + sljit_si logical_local_size; #endif #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG) - int skip_checks; + sljit_si skip_checks; #endif }; @@ -281,22 +304,29 @@ struct sljit_compiler { /* Creates an sljit compiler. Returns NULL if failed. */ SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void); -/* Free everything except the codes. */ + +/* Free everything except the compiled machine code. */ SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler); -static SLJIT_INLINE int sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; } +/* Returns the current error code. If an error is occurred, future sljit + calls which uses the same compiler argument returns early with the same + error code. Thus there is no need for checking the error after every + call, it is enough to do it before the code is compiled. Removing + these checks increases the performance of the compiling process. */ +static SLJIT_INLINE sljit_si sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; } /* Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit, - and <= 128 bytes on 64 bit architectures. The memory area is owned by the compiler, - and freed by sljit_free_compiler. The returned pointer is sizeof(sljit_w) aligned. - Excellent for allocating small blocks during the compiling, and no need to worry - about freeing them. The size is enough to contain at most 16 pointers. - If the size is outside of the range, the function will return with NULL, - but this return value does not indicate that there is no more memory (does - not set the compiler to out-of-memory status). + and <= 128 bytes on 64 bit architectures. The memory area is owned by the + compiler, and freed by sljit_free_compiler. The returned pointer is + sizeof(sljit_sw) aligned. Excellent for allocating small blocks during + the compiling, and no need to worry about freeing them. The size is + enough to contain at most 16 pointers. If the size is outside of the range, + the function will return with NULL. However, this return value does not + indicate that there is no more memory (does not set the current error code + of the compiler to out-of-memory status). */ -SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size); +SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size); #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) /* Passing NULL disables verbose. */ @@ -307,15 +337,17 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(str SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code); /* - After the code generation we can retrieve the allocated executable memory size, - although this area may not be fully filled with instructions depending on some - optimizations. This function is useful only for statistical purposes. + After the machine code generation is finished we can retrieve the allocated + executable memory size, although this area may not be fully filled with + instructions depending on some optimizations. This function is useful only + for statistical purposes. Before a successful code generation, this function returns with 0. */ static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; } -/* Instruction generation. Returns with error code. */ +/* Instruction generation. Returns with any error code. If there is no + error, they return with SLJIT_SUCCESS. */ /* The executable code is basically a function call from the viewpoint of @@ -326,8 +358,8 @@ static SLJIT_INLINE sljit_uw sljit_get_generated_code_ for the executable code and moves function arguments to the saved registers. The number of arguments are specified in the "args" parameter and the first argument goes to SLJIT_SAVED_REG1, the second - goes to SLJIT_SAVED_REG2 and so on. The number of temporary and - saved registers are passed in "temporaries" and "saveds" arguments + goes to SLJIT_SAVED_REG2 and so on. The number of scratch and + saved registers are passed in "scratches" and "saveds" arguments respectively. Since the saved registers contains the arguments, "args" must be less or equal than "saveds". The sljit_emit_enter is also capable of allocating a stack space for local variables. The @@ -338,54 +370,53 @@ static SLJIT_INLINE sljit_uw sljit_get_generated_code_ SLJIT_LOCALS_REG + local_size (exclusive) can be modified freely until the function returns. The stack space is uninitialized. - Note: every call of sljit_emit_enter and sljit_set_context overwrites - the previous context. */ + Note: every call of sljit_emit_enter and sljit_set_context + overwrites the previous context. */ #define SLJIT_MAX_LOCAL_SIZE 65536 -SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler, - int args, int temporaries, int saveds, int local_size); +SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, + sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size); /* The machine code has a context (which contains the local stack space size, number of used registers, etc.) which initialized by sljit_emit_enter. Several functions (like sljit_emit_return) requres this context to be able to generate the appropriate code. However, some code fragments (like inline cache) may have no normal entry point so their context is unknown for the compiler. Using the - function below we can specify thir context. + function below we can specify their context. Note: every call of sljit_emit_enter and sljit_set_context overwrites the previous context. */ -/* Note: multiple calls of this function overwrites the previous call. */ - SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, - int args, int temporaries, int saveds, int local_size); + sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size); /* Return from machine code. The op argument can be SLJIT_UNUSED which means the function does not return with anything or any opcode between SLJIT_MOV and - SLJIT_MOV_SI (see sljit_emit_op1). As for src and srcw they must be 0 if op + SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op is SLJIT_UNUSED, otherwise see below the description about source and destination arguments. */ -SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int op, - int src, sljit_w srcw); -/* Really fast calling method for utility functions inside sljit (see SLJIT_FAST_CALL). - All registers and even the stack frame is passed to the callee. The return address is - preserved in dst/dstw by sljit_emit_fast_enter, and sljit_emit_fast_return can - use this as a return value later. */ +SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, + sljit_si src, sljit_sw srcw); -/* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine instructions - are needed. Excellent for small uility functions, where saving registers and setting up - a new stack frame would cost too much performance. However, it is still possible to return - to the address of the caller (or anywhere else). */ +/* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and + even the stack frame is passed to the callee. The return address is preserved in + dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function + is sljit_p), and sljit_emit_fast_return can use this as a return value later. */ +/* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine + instructions are needed. Excellent for small uility functions, where saving registers + and setting up a new stack frame would cost too much performance. However, it is still + possible to return to the address of the caller (or anywhere else). */ + /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */ /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested, since many architectures do clever branch prediction on call / return instruction pairs. */ -SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw); -SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw); +SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw); +SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw); /* Source and destination values for arithmetical instructions @@ -394,7 +425,7 @@ SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(st [imm] - absolute immediate memory address [reg+imm] - indirect memory address [reg+(reg<addr; } static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; } @@ -767,22 +895,22 @@ static SLJIT_INLINE sljit_uw sljit_get_const_addr(stru /* Only the address is required to rewrite the code. */ SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr); -SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant); +SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant); /* --------------------------------------------------------------------- */ /* Miscellaneous utility functions */ /* --------------------------------------------------------------------- */ #define SLJIT_MAJOR_VERSION 0 -#define SLJIT_MINOR_VERSION 88 +#define SLJIT_MINOR_VERSION 91 -/* Get the human readable name of the platfrom. - Can be useful for debugging on platforms like ARM, where ARM and - Thumb2 functions can be mixed. */ +/* Get the human readable name of the platform. Can be useful on platforms + like ARM, where ARM and Thumb2 functions can be mixed, and + it is useful to know the type of the code generator. */ SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void); -/* Portble helper function to get an offset of a member. */ -#define SLJIT_OFFSETOF(base, member) ((sljit_w)(&((base*)0x10)->member) - 0x10) +/* Portable helper function to get an offset of a member. */ +#define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10) #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK) /* This global lock is useful to compile common functions. */ @@ -831,32 +959,32 @@ SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_st since the growth ratio can be added to the current limit, and sljit_stack_resize will do all the necessary checks. The fields of the stack are not changed if sljit_stack_resize fails. */ -SLJIT_API_FUNC_ATTRIBUTE sljit_w SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit); +SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit); #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */ #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) /* Get the entry address of a given function. */ -#define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)func_name) +#define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name) #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */ /* All JIT related code should be placed in the same context (library, binary, etc.). */ -#define SLJIT_FUNC_OFFSET(func_name) ((sljit_w)*(void**)func_name) +#define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name) /* For powerpc64, the function pointers point to a context descriptor. */ struct sljit_function_context { - sljit_w addr; - sljit_w r2; - sljit_w r11; + sljit_sw addr; + sljit_sw r2; + sljit_sw r11; }; /* Fill the context arguments using the addr and the function. If func_ptr is NULL, it will not be set to the address of context If addr is NULL, the function address also comes from the func pointer. */ -SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func); +SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func); #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */