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