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