Home | History | Annotate | Line # | Download | only in sljit_src
sljitLir.h revision 1.1
      1 /*
      2  *    Stack-less Just-In-Time compiler
      3  *
      4  *    Copyright 2009-2012 Zoltan Herczeg (hzmester (at) 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_VERBOSE && SLJIT_VERBOSE)
    264 	FILE* verbose;
    265 #endif
    266 
    267 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
    268 	/* Local size passed to the functions. */
    269 	int logical_local_size;
    270 #endif
    271 
    272 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
    273 	int skip_checks;
    274 #endif
    275 };
    276 
    277 /* --------------------------------------------------------------------- */
    278 /*  Main functions                                                       */
    279 /* --------------------------------------------------------------------- */
    280 
    281 /* Creates an sljit compiler.
    282    Returns NULL if failed. */
    283 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void);
    284 /* Free everything except the codes. */
    285 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
    286 
    287 static SLJIT_INLINE int sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
    288 
    289 /*
    290    Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
    291    and <= 128 bytes on 64 bit architectures. The memory area is owned by the compiler,
    292    and freed by sljit_free_compiler. The returned pointer is sizeof(sljit_w) aligned.
    293    Excellent for allocating small blocks during the compiling, and no need to worry
    294    about freeing them. The size is enough to contain at most 16 pointers.
    295    If the size is outside of the range, the function will return with NULL,
    296    but this return value does not indicate that there is no more memory (does
    297    not set the compiler to out-of-memory status).
    298 */
    299 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size);
    300 
    301 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
    302 /* Passing NULL disables verbose. */
    303 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
    304 #endif
    305 
    306 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
    307 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
    308 
    309 /*
    310    After the code generation we can retrieve the allocated executable memory size,
    311    although this area may not be fully filled with instructions depending on some
    312    optimizations. This function is useful only for statistical purposes.
    313 
    314    Before a successful code generation, this function returns with 0.
    315 */
    316 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
    317 
    318 /* Instruction generation. Returns with error code. */
    319 
    320 /*
    321    The executable code is basically a function call from the viewpoint of
    322    the C language. The function calls must obey to the ABI (Application
    323    Binary Interface) of the platform, which specify the purpose of machine
    324    registers and stack handling among other things. The sljit_emit_enter
    325    function emits the necessary instructions for setting up a new context
    326    for the executable code and moves function arguments to the saved
    327    registers. The number of arguments are specified in the "args"
    328    parameter and the first argument goes to SLJIT_SAVED_REG1, the second
    329    goes to SLJIT_SAVED_REG2 and so on. The number of temporary and
    330    saved registers are passed in "temporaries" and "saveds" arguments
    331    respectively. Since the saved registers contains the arguments,
    332    "args" must be less or equal than "saveds". The sljit_emit_enter
    333    is also capable of allocating a stack space for local variables. The
    334    "local_size" argument contains the size in bytes of this local area
    335    and its staring address is stored in SLJIT_LOCALS_REG. However
    336    the SLJIT_LOCALS_REG is not necessary the machine stack pointer.
    337    The memory bytes between SLJIT_LOCALS_REG (inclusive) and
    338    SLJIT_LOCALS_REG + local_size (exclusive) can be modified freely
    339    until the function returns. The stack space is uninitialized.
    340 
    341    Note: every call of sljit_emit_enter and sljit_set_context overwrites
    342          the previous context. */
    343 
    344 #define SLJIT_MAX_LOCAL_SIZE	65536
    345 
    346 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler,
    347 	int args, int temporaries, int saveds, int local_size);
    348 
    349 /* The machine code has a context (which contains the local stack space size,
    350    number of used registers, etc.) which initialized by sljit_emit_enter. Several
    351    functions (like sljit_emit_return) requres this context to be able to generate
    352    the appropriate code. However, some code fragments (like inline cache) may have
    353    no normal entry point so their context is unknown for the compiler. Using the
    354    function below we can specify thir context.
    355 
    356    Note: every call of sljit_emit_enter and sljit_set_context overwrites
    357          the previous context. */
    358 
    359 /* Note: multiple calls of this function overwrites the previous call. */
    360 
    361 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler,
    362 	int args, int temporaries, int saveds, int local_size);
    363 
    364 /* Return from machine code.  The op argument can be SLJIT_UNUSED which means the
    365    function does not return with anything or any opcode between SLJIT_MOV and
    366    SLJIT_MOV_SI (see sljit_emit_op1). As for src and srcw they must be 0 if op
    367    is SLJIT_UNUSED, otherwise see below the description about source and
    368    destination arguments. */
    369 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int op,
    370 	int src, sljit_w srcw);
    371 
    372 /* Really fast calling method for utility functions inside sljit (see SLJIT_FAST_CALL).
    373    All registers and even the stack frame is passed to the callee. The return address is
    374    preserved in dst/dstw by sljit_emit_fast_enter, and sljit_emit_fast_return can
    375    use this as a return value later. */
    376 
    377 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine instructions
    378    are needed. Excellent for small uility functions, where saving registers and setting up
    379    a new stack frame would cost too much performance. However, it is still possible to return
    380    to the address of the caller (or anywhere else). */
    381 
    382 /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
    383 
    384 /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
    385    since many architectures do clever branch prediction on call / return instruction pairs. */
    386 
    387 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw);
    388 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw);
    389 
    390 /*
    391    Source and destination values for arithmetical instructions
    392     imm              - a simple immediate value (cannot be used as a destination)
    393     reg              - any of the registers (immediate argument must be 0)
    394     [imm]            - absolute immediate memory address
    395     [reg+imm]        - indirect memory address
    396     [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
    397                        useful for (byte, half, int, sljit_w) array access
    398                        (fully supported by both x86 and ARM architectures, and cheap operation on others)
    399 */
    400 
    401 /*
    402    IMPORATNT NOTE: memory access MUST be naturally aligned except
    403                    SLJIT_UNALIGNED macro is defined and its value is 1.
    404 
    405      length | alignment
    406    ---------+-----------
    407      byte   | 1 byte (not aligned)
    408      half   | 2 byte (real_address & 0x1 == 0)
    409      int    | 4 byte (real_address & 0x3 == 0)
    410     sljit_w | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
    411             | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
    412 
    413    Note: different architectures have different addressing limitations
    414          Thus sljit may generate several instructions for other addressing modes
    415    x86:  all addressing modes supported, but write-back is not supported
    416          (requires an extra instruction). On x86-64 only 32 bit signed
    417          integers are supported by the architecture.
    418    arm:  [reg+imm] supported for small immediates (-4095 <= imm <= 4095
    419          or -255 <= imm <= 255 for loading signed bytes, any halfs or doubles)
    420          [reg+(reg<<imm)] are supported or requires only two instructions
    421          Write back is limited to small immediates on thumb2
    422    ppc:  [reg+imm], -65535 <= imm <= 65535. 64 bit moves requires immediates
    423          divisible by 4. [reg+reg] supported, write-back supported
    424          [reg+(reg<<imm)] (imm != 0) is cheap (requires two instructions)
    425 */
    426 
    427 /* Register output: simply the name of the register.
    428    For destination, you can use SLJIT_UNUSED as well. */
    429 #define SLJIT_MEM		0x100
    430 #define SLJIT_MEM0()		(SLJIT_MEM)
    431 #define SLJIT_MEM1(r1)		(SLJIT_MEM | (r1))
    432 #define SLJIT_MEM2(r1, r2)	(SLJIT_MEM | (r1) | ((r2) << 4))
    433 #define SLJIT_IMM		0x200
    434 
    435 /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
    436    32 bit CPUs. The arithmetic instruction uses only the lower 32 bit of the
    437    input register(s), and set the flags according to the 32 bit result. If the
    438    destination is a register, the higher 32 bit of the result is undefined.
    439    The addressing modes (SLJIT_MEM1/SLJIT_MEM2 macros) are unaffected by this flag. */
    440 #define SLJIT_INT_OP		0x100
    441 
    442 /* Common CPU status flags for all architectures (x86, ARM, PPC)
    443     - carry flag
    444     - overflow flag
    445     - zero flag
    446     - negative/positive flag (depends on arc)
    447    On mips, these flags are emulated by software. */
    448 
    449 /* By default, the instructions may, or may not set the CPU status flags.
    450    Forcing to set or keep status flags can be done with the following flags: */
    451 
    452 /* Note: sljit tries to emit the minimum number of instructions. Using these
    453    flags can increase them, so use them wisely to avoid unnecessary code generation. */
    454 
    455 /* Set Equal (Zero) status flag (E). */
    456 #define SLJIT_SET_E			0x0200
    457 /* Set signed status flag (S). */
    458 #define SLJIT_SET_S			0x0400
    459 /* Set unsgined status flag (U). */
    460 #define SLJIT_SET_U			0x0800
    461 /* Set signed overflow flag (O). */
    462 #define SLJIT_SET_O			0x1000
    463 /* Set carry flag (C).
    464    Note: Kinda unsigned overflow, but behaves differently on various cpus. */
    465 #define SLJIT_SET_C			0x2000
    466 /* Do not modify the flags (K).
    467    Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
    468 #define SLJIT_KEEP_FLAGS		0x4000
    469 
    470 /* Notes:
    471      - you cannot postpone conditional jump instructions except if noted that
    472        the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
    473      - flag combinations: '|' means 'logical or'. */
    474 
    475 /* Flags: - (never set any flags)
    476    Note: breakpoint instruction is not supported by all architectures (namely ppc)
    477          It falls back to SLJIT_NOP in those cases. */
    478 #define SLJIT_BREAKPOINT		0
    479 /* Flags: - (never set any flags)
    480    Note: may or may not cause an extra cycle wait
    481          it can even decrease the runtime in a few cases. */
    482 #define SLJIT_NOP			1
    483 /* Flags: may destroy flags
    484    Unsigned multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
    485    Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
    486 #define SLJIT_UMUL			2
    487 /* Flags: may destroy flags
    488    Signed multiplication of SLJIT_TEMPORARY_REG1 and SLJIT_TEMPORARY_REG2.
    489    Result goes to SLJIT_TEMPORARY_REG2:SLJIT_TEMPORARY_REG1 (high:low) word */
    490 #define SLJIT_SMUL			3
    491 /* Flags: I | may destroy flags
    492    Unsigned divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
    493    The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
    494    Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
    495 #define SLJIT_UDIV			4
    496 /* Flags: I | may destroy flags
    497    Signed divide of the value in SLJIT_TEMPORARY_REG1 by the value in SLJIT_TEMPORARY_REG2.
    498    The result is placed in SLJIT_TEMPORARY_REG1 and the remainder goes to SLJIT_TEMPORARY_REG2.
    499    Note: if SLJIT_TEMPORARY_REG2 contains 0, the behaviour is undefined. */
    500 #define SLJIT_SDIV			5
    501 
    502 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op);
    503 
    504 /* Notes for MOV instructions:
    505    U = Mov with update (post form). If source or destination defined as SLJIT_MEM1(r1)
    506        or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
    507    UB = unsigned byte (8 bit)
    508    SB = signed byte (8 bit)
    509    UH = unsgined half (16 bit)
    510    SH = unsgined half (16 bit) */
    511 
    512 /* Flags: - (never set any flags) */
    513 #define SLJIT_MOV			6
    514 /* Flags: - (never set any flags) */
    515 #define SLJIT_MOV_UB			7
    516 /* Flags: - (never set any flags) */
    517 #define SLJIT_MOV_SB			8
    518 /* Flags: - (never set any flags) */
    519 #define SLJIT_MOV_UH			9
    520 /* Flags: - (never set any flags) */
    521 #define SLJIT_MOV_SH			10
    522 /* Flags: - (never set any flags) */
    523 #define SLJIT_MOV_UI			11
    524 /* Flags: - (never set any flags) */
    525 #define SLJIT_MOV_SI			12
    526 /* Flags: - (never set any flags) */
    527 #define SLJIT_MOVU			13
    528 /* Flags: - (never set any flags) */
    529 #define SLJIT_MOVU_UB			14
    530 /* Flags: - (never set any flags) */
    531 #define SLJIT_MOVU_SB			15
    532 /* Flags: - (never set any flags) */
    533 #define SLJIT_MOVU_UH			16
    534 /* Flags: - (never set any flags) */
    535 #define SLJIT_MOVU_SH			17
    536 /* Flags: - (never set any flags) */
    537 #define SLJIT_MOVU_UI			18
    538 /* Flags: - (never set any flags) */
    539 #define SLJIT_MOVU_SI			19
    540 /* Flags: I | E | K */
    541 #define SLJIT_NOT			20
    542 /* Flags: I | E | O | K */
    543 #define SLJIT_NEG			21
    544 /* Count leading zeroes
    545    Flags: I | E | K */
    546 #define SLJIT_CLZ			22
    547 
    548 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
    549 	int dst, sljit_w dstw,
    550 	int src, sljit_w srcw);
    551 
    552 /* Flags: I | E | O | C | K */
    553 #define SLJIT_ADD			23
    554 /* Flags: I | C | K */
    555 #define SLJIT_ADDC			24
    556 /* Flags: I | E | S | U | O | C | K */
    557 #define SLJIT_SUB			25
    558 /* Flags: I | C | K */
    559 #define SLJIT_SUBC			26
    560 /* Note: integer mul
    561    Flags: I | O (see SLJIT_C_MUL_*) | K */
    562 #define SLJIT_MUL			27
    563 /* Flags: I | E | K */
    564 #define SLJIT_AND			28
    565 /* Flags: I | E | K */
    566 #define SLJIT_OR			29
    567 /* Flags: I | E | K */
    568 #define SLJIT_XOR			30
    569 /* Flags: I | E | K
    570    Let bit_length be the length of the shift operation: 32 or 64.
    571    If src2 is immediate, src2w is masked by (bit_length - 1).
    572    Otherwise, if the content of src2 is outside the range from 0
    573    to bit_length - 1, the operation is undefined. */
    574 #define SLJIT_SHL			31
    575 /* Flags: I | E | K
    576    Let bit_length be the length of the shift operation: 32 or 64.
    577    If src2 is immediate, src2w is masked by (bit_length - 1).
    578    Otherwise, if the content of src2 is outside the range from 0
    579    to bit_length - 1, the operation is undefined. */
    580 #define SLJIT_LSHR			32
    581 /* Flags: I | E | K
    582    Let bit_length be the length of the shift operation: 32 or 64.
    583    If src2 is immediate, src2w is masked by (bit_length - 1).
    584    Otherwise, if the content of src2 is outside the range from 0
    585    to bit_length - 1, the operation is undefined. */
    586 #define SLJIT_ASHR			33
    587 
    588 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
    589 	int dst, sljit_w dstw,
    590 	int src1, sljit_w src1w,
    591 	int src2, sljit_w src2w);
    592 
    593 /* The following function is a helper function for sljit_emit_op_custom.
    594    It returns with the real machine register index of any SLJIT_TEMPORARY
    595    SLJIT_SAVED or SLJIT_LOCALS register.
    596    Note: it returns with -1 for virtual registers (all EREGs on x86-32).
    597    Note: register returned by SLJIT_LOCALS_REG is not necessary the real
    598          stack pointer register of the target architecture. */
    599 
    600 SLJIT_API_FUNC_ATTRIBUTE int sljit_get_register_index(int reg);
    601 
    602 /* Any instruction can be inserted into the instruction stream by
    603    sljit_emit_op_custom. It has a similar purpose as inline assembly.
    604    The size parameter must match to the instruction size of the target
    605    architecture:
    606 
    607          x86: 0 < size <= 15. The instruction argument can be byte aligned.
    608       Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
    609               if size == 4, the instruction argument must be 4 byte aligned.
    610    Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
    611 
    612 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op_custom(struct sljit_compiler *compiler,
    613 	void *instruction, int size);
    614 
    615 /* Returns with non-zero if fpu is available. */
    616 
    617 SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void);
    618 
    619 /* Note: dst is the left and src is the right operand for SLJIT_FCMP.
    620    Note: NaN check is always performed. If SLJIT_C_FLOAT_NAN is set,
    621          the comparison result is unpredictable.
    622    Flags: E | S (see SLJIT_C_FLOAT_*) */
    623 #define SLJIT_FCMP			34
    624 /* Flags: - (never set any flags) */
    625 #define SLJIT_FMOV			35
    626 /* Flags: - (never set any flags) */
    627 #define SLJIT_FNEG			36
    628 /* Flags: - (never set any flags) */
    629 #define SLJIT_FABS			37
    630 
    631 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
    632 	int dst, sljit_w dstw,
    633 	int src, sljit_w srcw);
    634 
    635 /* Flags: - (never set any flags) */
    636 #define SLJIT_FADD			38
    637 /* Flags: - (never set any flags) */
    638 #define SLJIT_FSUB			39
    639 /* Flags: - (never set any flags) */
    640 #define SLJIT_FMUL			40
    641 /* Flags: - (never set any flags) */
    642 #define SLJIT_FDIV			41
    643 
    644 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
    645 	int dst, sljit_w dstw,
    646 	int src1, sljit_w src1w,
    647 	int src2, sljit_w src2w);
    648 
    649 /* Label and jump instructions. */
    650 
    651 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
    652 
    653 /* Invert conditional instruction: xor (^) with 0x1 */
    654 #define SLJIT_C_EQUAL			0
    655 #define SLJIT_C_ZERO			0
    656 #define SLJIT_C_NOT_EQUAL		1
    657 #define SLJIT_C_NOT_ZERO		1
    658 
    659 #define SLJIT_C_LESS			2
    660 #define SLJIT_C_GREATER_EQUAL		3
    661 #define SLJIT_C_GREATER			4
    662 #define SLJIT_C_LESS_EQUAL		5
    663 #define SLJIT_C_SIG_LESS		6
    664 #define SLJIT_C_SIG_GREATER_EQUAL	7
    665 #define SLJIT_C_SIG_GREATER		8
    666 #define SLJIT_C_SIG_LESS_EQUAL		9
    667 
    668 #define SLJIT_C_OVERFLOW		10
    669 #define SLJIT_C_NOT_OVERFLOW		11
    670 
    671 #define SLJIT_C_MUL_OVERFLOW		12
    672 #define SLJIT_C_MUL_NOT_OVERFLOW	13
    673 
    674 #define SLJIT_C_FLOAT_EQUAL		14
    675 #define SLJIT_C_FLOAT_NOT_EQUAL		15
    676 #define SLJIT_C_FLOAT_LESS		16
    677 #define SLJIT_C_FLOAT_GREATER_EQUAL	17
    678 #define SLJIT_C_FLOAT_GREATER		18
    679 #define SLJIT_C_FLOAT_LESS_EQUAL	19
    680 #define SLJIT_C_FLOAT_NAN		20
    681 #define SLJIT_C_FLOAT_NOT_NAN		21
    682 
    683 #define SLJIT_JUMP			22
    684 #define SLJIT_FAST_CALL			23
    685 #define SLJIT_CALL0			24
    686 #define SLJIT_CALL1			25
    687 #define SLJIT_CALL2			26
    688 #define SLJIT_CALL3			27
    689 
    690 /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
    691 
    692 /* The target can be changed during runtime (see: sljit_set_jump_addr). */
    693 #define SLJIT_REWRITABLE_JUMP		0x1000
    694 
    695 /* Emit a jump instruction. The destination is not set, only the type of the jump.
    696     type must be between SLJIT_C_EQUAL and SLJIT_CALL3
    697     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
    698    Flags: - (never set any flags) for both conditional and unconditional jumps.
    699    Flags: destroy all flags for calls. */
    700 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type);
    701 
    702 /* Basic arithmetic comparison. In most architectures it is implemented as
    703    an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
    704    appropriate flags) followed by a sljit_emit_jump. However some
    705    architectures (i.e: MIPS) may employ special optimizations here. It is
    706    suggested to use this comparison form when appropriate.
    707     type must be between SLJIT_C_EQUAL and SLJIT_C_SIG_LESS_EQUAL
    708     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP or SLJIT_INT_OP
    709    Flags: destroy flags. */
    710 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
    711 	int src1, sljit_w src1w,
    712 	int src2, sljit_w src2w);
    713 
    714 /* Basic floating point comparison. In most architectures it is implemented as
    715    an SLJIT_FCMP operation (setting appropriate flags) followed by a
    716    sljit_emit_jump. However some architectures (i.e: MIPS) may employ
    717    special optimizations here. It is suggested to use this comparison form
    718    when appropriate.
    719     type must be between SLJIT_C_FLOAT_EQUAL and SLJIT_C_FLOAT_NOT_NAN
    720     type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
    721    Flags: destroy flags.
    722    Note: if either operand is NaN, the behaviour is undefined for
    723          type <= SLJIT_C_FLOAT_LESS_EQUAL. */
    724 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, int type,
    725 	int src1, sljit_w src1w,
    726 	int src2, sljit_w src2w);
    727 
    728 /* Set the destination of the jump to this label. */
    729 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
    730 /* Only for jumps defined with SLJIT_REWRITABLE_JUMP flag.
    731    Note: use sljit_emit_ijump for fixed jumps. */
    732 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
    733 
    734 /* Call function or jump anywhere. Both direct and indirect form
    735     type must be between SLJIT_JUMP and SLJIT_CALL3
    736     Direct form: set src to SLJIT_IMM() and srcw to the address
    737     Indirect form: any other valid addressing mode
    738    Flags: - (never set any flags) for unconditional jumps.
    739    Flags: destroy all flags for calls. */
    740 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw);
    741 
    742 /* If op == SLJIT_MOV:
    743      Set dst to 1 if condition is fulfilled, 0 otherwise
    744        type must be between SLJIT_C_EQUAL and SLJIT_C_FLOAT_NOT_NAN
    745      Flags: - (never set any flags)
    746    If op == SLJIT_OR
    747      Dst is used as src as well, and set its lowest bit to 1 if
    748      the condition is fulfilled. Otherwise it does nothing.
    749      Flags: E | K
    750    Note: sljit_emit_cond_value does nothing, if dst is SLJIT_UNUSED (regardless of op). */
    751 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type);
    752 
    753 /* Copies the base address of SLJIT_MEM1(SLJIT_LOCALS_REG)+offset to dst.
    754    Flags: - (never set any flags) */
    755 SLJIT_API_FUNC_ATTRIBUTE int sljit_get_local_base(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w offset);
    756 
    757 /* The constant can be changed runtime (see: sljit_set_const)
    758    Flags: - (never set any flags) */
    759 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value);
    760 
    761 /* After the code generation the address for label, jump and const instructions
    762    are computed. Since these structures are freed sljit_free_compiler, the
    763    addresses must be preserved by the user program elsewere. */
    764 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
    765 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
    766 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
    767 
    768 /* Only the address is required to rewrite the code. */
    769 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
    770 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant);
    771 
    772 /* --------------------------------------------------------------------- */
    773 /*  Miscellaneous utility functions                                      */
    774 /* --------------------------------------------------------------------- */
    775 
    776 #define SLJIT_MAJOR_VERSION	0
    777 #define SLJIT_MINOR_VERSION	88
    778 
    779 /* Get the human readable name of the platfrom.
    780    Can be useful for debugging on platforms like ARM, where ARM and
    781    Thumb2 functions can be mixed. */
    782 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
    783 
    784 /* Portble helper function to get an offset of a member. */
    785 #define SLJIT_OFFSETOF(base, member) ((sljit_w)(&((base*)0x10)->member) - 0x10)
    786 
    787 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
    788 /* This global lock is useful to compile common functions. */
    789 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
    790 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
    791 #endif
    792 
    793 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
    794 
    795 /* The sljit_stack is a utiliy feature of sljit, which allocates a
    796    writable memory region between base (inclusive) and limit (exclusive).
    797    Both base and limit is a pointer, and base is always <= than limit.
    798    This feature uses the "address space reserve" feature
    799    of modern operating systems. Basically we don't need to allocate a
    800    huge memory block in one step for the worst case, we can start with
    801    a smaller chunk and extend it later. Since the address space is
    802    reserved, the data never copied to other regions, thus it is safe
    803    to store pointers here. */
    804 
    805 /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
    806    Note: stack growing should not happen in small steps: 4k, 16k or even
    807      bigger growth is better.
    808    Note: this structure may not be supported by all operating systems.
    809      Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
    810      is not defined. */
    811 
    812 struct sljit_stack {
    813 	/* User data, anything can be stored here.
    814 	   Starting with the same value as base. */
    815 	sljit_uw top;
    816 	/* These members are read only. */
    817 	sljit_uw base;
    818 	sljit_uw limit;
    819 	sljit_uw max_limit;
    820 };
    821 
    822 /* Returns NULL if unsuccessful.
    823    Note: limit and max_limit contains the size for stack allocation
    824    Note: the top field is initialized to base. */
    825 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit);
    826 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack* stack);
    827 
    828 /* Can be used to increase (allocate) or decrease (free) the memory area.
    829    Returns with a non-zero value if unsuccessful. If new_limit is greater than
    830    max_limit, it will fail. It is very easy to implement a stack data structure,
    831    since the growth ratio can be added to the current limit, and sljit_stack_resize
    832    will do all the necessary checks. The fields of the stack are not changed if
    833    sljit_stack_resize fails. */
    834 SLJIT_API_FUNC_ATTRIBUTE sljit_w SLJIT_CALL sljit_stack_resize(struct sljit_stack* stack, sljit_uw new_limit);
    835 
    836 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
    837 
    838 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
    839 
    840 /* Get the entry address of a given function. */
    841 #define SLJIT_FUNC_OFFSET(func_name)	((sljit_w)func_name)
    842 
    843 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
    844 
    845 /* All JIT related code should be placed in the same context (library, binary, etc.). */
    846 
    847 #define SLJIT_FUNC_OFFSET(func_name)	((sljit_w)*(void**)func_name)
    848 
    849 /* For powerpc64, the function pointers point to a context descriptor. */
    850 struct sljit_function_context {
    851 	sljit_w addr;
    852 	sljit_w r2;
    853 	sljit_w r11;
    854 };
    855 
    856 /* Fill the context arguments using the addr and the function.
    857    If func_ptr is NULL, it will not be set to the address of context
    858    If addr is NULL, the function address also comes from the func pointer. */
    859 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func);
    860 
    861 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
    862 
    863 #endif /* _SLJIT_LIR_H_ */
    864