1 /* $NetBSD: sljitLir.h,v 1.5 2021/12/05 04:38:54 msaitoh Exp $ */ 2 3 /* 4 * Stack-less Just-In-Time compiler 5 * 6 * Copyright Zoltan Herczeg (hzmester (at) 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 In practice: 62 - This approach is very effective for interpreters 63 - One of the saved registers typically points to a stack interface 64 - It can jump to any exception handler anytime (even if it belongs 65 to another function) 66 - Hot paths can be modified during runtime reflecting the changes 67 of the fastest execution path of the dynamic language 68 - SLJIT supports complex memory addressing modes 69 - mainly position and context independent code (except some cases) 70 71 For valgrind users: 72 - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code" 73 */ 74 75 #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG) 76 #include "sljitConfig.h" 77 #endif 78 79 /* The following header file defines useful macros for fine tuning 80 sljit based code generators. They are listed in the beginning 81 of sljitConfigInternal.h */ 82 83 #include "sljitConfigInternal.h" 84 85 /* --------------------------------------------------------------------- */ 86 /* Error codes */ 87 /* --------------------------------------------------------------------- */ 88 89 /* Indicates no error. */ 90 #define SLJIT_SUCCESS 0 91 /* After the call of sljit_generate_code(), the error code of the compiler 92 is set to this value to avoid future sljit calls (in debug mode at least). 93 The complier should be freed after sljit_generate_code(). */ 94 #define SLJIT_ERR_COMPILED 1 95 /* Cannot allocate non executable memory. */ 96 #define SLJIT_ERR_ALLOC_FAILED 2 97 /* Cannot allocate executable memory. 98 Only for sljit_generate_code() */ 99 #define SLJIT_ERR_EX_ALLOC_FAILED 3 100 /* Return value for SLJIT_CONFIG_UNSUPPORTED placeholder architecture. */ 101 #define SLJIT_ERR_UNSUPPORTED 4 102 /* An ivalid argument is passed to any SLJIT function. */ 103 #define SLJIT_ERR_BAD_ARGUMENT 5 104 /* Dynamic code modification is not enabled. */ 105 #define SLJIT_ERR_DYN_CODE_MOD 6 106 107 /* --------------------------------------------------------------------- */ 108 /* Registers */ 109 /* --------------------------------------------------------------------- */ 110 111 /* 112 Scratch (R) registers: registers whose may not preserve their values 113 across function calls. 114 115 Saved (S) registers: registers whose preserve their values across 116 function calls. 117 118 The scratch and saved register sets are overlap. The last scratch register 119 is the first saved register, the one before the last is the second saved 120 register, and so on. 121 122 If an architecture provides two scratch and three saved registers, 123 its scratch and saved register sets are the following: 124 125 R0 | [S4] | R0 and S4 represent the same physical register 126 R1 | [S3] | R1 and S3 represent the same physical register 127 [R2] | S2 | R2 and S2 represent the same physical register 128 [R3] | S1 | R3 and S1 represent the same physical register 129 [R4] | S0 | R4 and S0 represent the same physical register 130 131 Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and 132 SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture. 133 134 Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 10 135 and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 5. However, 4 registers 136 are virtual on x86-32. See below. 137 138 The purpose of this definition is convenience. Although a register 139 is either scratch register or saved register, SLJIT allows accessing 140 them from the other set. For example, four registers can be used as 141 scratch registers and the fifth one as saved register on the architecture 142 above. Of course the last two scratch registers (R2 and R3) from this 143 four will be saved on the stack, because they are defined as saved 144 registers in the application binary interface. Still R2 and R3 can be 145 used for referencing to these registers instead of S2 and S1, which 146 makes easier to write platform independent code. Scratch registers 147 can be saved registers in a similar way, but these extra saved 148 registers will not be preserved across function calls! Hence the 149 application must save them on those platforms, where the number of 150 saved registers is too low. This can be done by copy them onto 151 the stack and restore them after a function call. 152 153 Note: To emphasize that registers assigned to R2-R4 are saved 154 registers, they are enclosed by square brackets. S3-S4 155 are marked in a similar way. 156 157 Note: sljit_emit_enter and sljit_set_context defines whether a register 158 is S or R register. E.g: when 3 scratches and 1 saved is mapped 159 by sljit_emit_enter, the allowed register set will be: R0-R2 and 160 S0. Although S2 is mapped to the same position as R2, it does not 161 available in the current configuration. Furthermore the R3 (S1) 162 register does not available as well. 163 */ 164 165 /* When SLJIT_UNUSED is specified as destination, the result is discarded. */ 166 #define SLJIT_UNUSED 0 167 168 /* Scratch registers. */ 169 #define SLJIT_R0 1 170 #define SLJIT_R1 2 171 #define SLJIT_R2 3 172 /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they 173 are allocated on the stack). These registers are called virtual 174 and cannot be used for memory addressing (cannot be part of 175 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such 176 limitation on other CPUs. See sljit_get_register_index(). */ 177 #define SLJIT_R3 4 178 #define SLJIT_R4 5 179 #define SLJIT_R5 6 180 #define SLJIT_R6 7 181 #define SLJIT_R7 8 182 #define SLJIT_R8 9 183 #define SLJIT_R9 10 184 /* All R registers provided by the architecture can be accessed by SLJIT_R(i) 185 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */ 186 #define SLJIT_R(i) (1 + (i)) 187 188 /* Saved registers. */ 189 #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS) 190 #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1) 191 #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2) 192 /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they 193 are allocated on the stack). These registers are called virtual 194 and cannot be used for memory addressing (cannot be part of 195 any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such 196 limitation on other CPUs. See sljit_get_register_index(). */ 197 #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3) 198 #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4) 199 #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5) 200 #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6) 201 #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7) 202 #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8) 203 #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9) 204 /* All S registers provided by the architecture can be accessed by SLJIT_S(i) 205 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */ 206 #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i)) 207 208 /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */ 209 #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1) 210 211 /* The SLJIT_SP provides direct access to the linear stack space allocated by 212 sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP). 213 The immediate offset is extended by the relative stack offset automatically. 214 The sljit_get_local_base can be used to obtain the absolute offset. */ 215 #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1) 216 217 /* Return with machine word. */ 218 219 #define SLJIT_RETURN_REG SLJIT_R0 220 221 /* x86 prefers specific registers for special purposes. In case of shift 222 by register it supports only SLJIT_R2 for shift argument 223 (which is the src2 argument of sljit_emit_op2). If another register is 224 used, sljit must exchange data between registers which cause a minor 225 slowdown. Other architectures has no such limitation. */ 226 227 #define SLJIT_PREF_SHIFT_REG SLJIT_R2 228 229 /* --------------------------------------------------------------------- */ 230 /* Floating point registers */ 231 /* --------------------------------------------------------------------- */ 232 233 /* Each floating point register can store a 32 or a 64 bit precision 234 value. The FR and FS register sets are overlap in the same way as R 235 and S register sets. See above. */ 236 237 /* Note: SLJIT_UNUSED as destination is not valid for floating point 238 operations, since they cannot be used for setting flags. */ 239 240 /* Floating point scratch registers. */ 241 #define SLJIT_FR0 1 242 #define SLJIT_FR1 2 243 #define SLJIT_FR2 3 244 #define SLJIT_FR3 4 245 #define SLJIT_FR4 5 246 #define SLJIT_FR5 6 247 /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i) 248 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */ 249 #define SLJIT_FR(i) (1 + (i)) 250 251 /* Floating point saved registers. */ 252 #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS) 253 #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1) 254 #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2) 255 #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3) 256 #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4) 257 #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5) 258 /* All S registers provided by the architecture can be accessed by SLJIT_FS(i) 259 The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */ 260 #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i)) 261 262 /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */ 263 #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1) 264 265 /* --------------------------------------------------------------------- */ 266 /* Main structures and functions */ 267 /* --------------------------------------------------------------------- */ 268 269 /* 270 The following structures are private, and can be changed in the 271 future. Keeping them here allows code inlining. 272 */ 273 274 struct sljit_memory_fragment { 275 struct sljit_memory_fragment *next; 276 sljit_uw used_size; 277 /* Must be aligned to sljit_sw. */ 278 sljit_u8 memory[1]; 279 }; 280 281 struct sljit_label { 282 struct sljit_label *next; 283 sljit_uw addr; 284 /* The maximum size difference. */ 285 sljit_uw size; 286 }; 287 288 struct sljit_jump { 289 struct sljit_jump *next; 290 sljit_uw addr; 291 sljit_sw flags; 292 union { 293 sljit_uw target; 294 struct sljit_label* label; 295 } u; 296 }; 297 298 struct sljit_const { 299 struct sljit_const *next; 300 sljit_uw addr; 301 }; 302 303 struct sljit_compiler { 304 sljit_s32 error; 305 sljit_s32 options; 306 307 struct sljit_label *labels; 308 struct sljit_jump *jumps; 309 struct sljit_const *consts; 310 struct sljit_label *last_label; 311 struct sljit_jump *last_jump; 312 struct sljit_const *last_const; 313 314 void *allocator_data; 315 struct sljit_memory_fragment *buf; 316 struct sljit_memory_fragment *abuf; 317 318 /* Used scratch registers. */ 319 sljit_s32 scratches; 320 /* Used saved registers. */ 321 sljit_s32 saveds; 322 /* Used float scratch registers. */ 323 sljit_s32 fscratches; 324 /* Used float saved registers. */ 325 sljit_s32 fsaveds; 326 /* Local stack size. */ 327 sljit_s32 local_size; 328 /* Code size. */ 329 sljit_uw size; 330 /* Relative offset of the executable mapping from the writable mapping. */ 331 sljit_uw executable_offset; 332 /* Executable size for statistical purposes. */ 333 sljit_uw executable_size; 334 335 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) 336 sljit_s32 args; 337 sljit_s32 locals_offset; 338 sljit_s32 saveds_offset; 339 #endif 340 341 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) 342 sljit_s32 mode32; 343 #ifdef _WIN64 344 sljit_s32 locals_offset; 345 #endif 346 #endif 347 348 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) 349 /* Constant pool handling. */ 350 sljit_uw *cpool; 351 sljit_u8 *cpool_unique; 352 sljit_uw cpool_diff; 353 sljit_uw cpool_fill; 354 /* Other members. */ 355 /* Contains pointer, "ldr pc, [...]" pairs. */ 356 sljit_uw patches; 357 #endif 358 359 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7) 360 /* Temporary fields. */ 361 sljit_uw shift_imm; 362 #endif 363 364 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64) 365 sljit_s32 cache_arg; 366 sljit_sw cache_argw; 367 #endif 368 369 #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC) 370 sljit_sw imm; 371 sljit_s32 cache_arg; 372 sljit_sw cache_argw; 373 #endif 374 375 #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS) 376 sljit_s32 delay_slot; 377 sljit_s32 cache_arg; 378 sljit_sw cache_argw; 379 #endif 380 381 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32) 382 sljit_s32 delay_slot; 383 sljit_s32 cache_arg; 384 sljit_sw cache_argw; 385 #endif 386 387 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX) 388 sljit_s32 cache_arg; 389 sljit_sw cache_argw; 390 #endif 391 392 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) 393 FILE* verbose; 394 #endif 395 396 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \ 397 || (defined SLJIT_DEBUG && SLJIT_DEBUG) 398 /* Flags specified by the last arithmetic instruction. 399 It contains the type of the variable flag. */ 400 sljit_s32 last_flags; 401 /* Local size passed to the functions. */ 402 sljit_s32 logical_local_size; 403 #endif 404 405 #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \ 406 || (defined SLJIT_DEBUG && SLJIT_DEBUG) \ 407 || (defined SLJIT_VERBOSE && SLJIT_VERBOSE) 408 /* Trust arguments when the API function is called. */ 409 sljit_s32 skip_checks; 410 #endif 411 }; 412 413 /* --------------------------------------------------------------------- */ 414 /* Main functions */ 415 /* --------------------------------------------------------------------- */ 416 417 /* Creates an sljit compiler. The allocator_data is required by some 418 custom memory managers. This pointer is passed to SLJIT_MALLOC 419 and SLJIT_FREE macros. Most allocators (including the default 420 one) ignores this value, and it is recommended to pass NULL 421 as a dummy value for allocator_data. 422 423 Returns NULL if failed. */ 424 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data); 425 426 /* Frees everything except the compiled machine code. */ 427 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler); 428 429 /* Returns the current error code. If an error is occurred, future sljit 430 calls which uses the same compiler argument returns early with the same 431 error code. Thus there is no need for checking the error after every 432 call, it is enough to do it before the code is compiled. Removing 433 these checks increases the performance of the compiling process. */ 434 static SLJIT_INLINE sljit_s32 sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; } 435 436 /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except 437 if an error was detected before. After the error code is set 438 the compiler behaves as if the allocation failure happened 439 during an sljit function call. This can greatly simplify error 440 checking, since only the compiler status needs to be checked 441 after the compilation. */ 442 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler); 443 444 /* 445 Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit, 446 and <= 128 bytes on 64 bit architectures. The memory area is owned by the 447 compiler, and freed by sljit_free_compiler. The returned pointer is 448 sizeof(sljit_sw) aligned. Excellent for allocating small blocks during 449 the compiling, and no need to worry about freeing them. The size is 450 enough to contain at most 16 pointers. If the size is outside of the range, 451 the function will return with NULL. However, this return value does not 452 indicate that there is no more memory (does not set the current error code 453 of the compiler to out-of-memory status). 454 */ 455 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_s32 size); 456 457 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) 458 /* Passing NULL disables verbose. */ 459 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose); 460 #endif 461 462 /* 463 Create executable code from the sljit instruction stream. This is the final step 464 of the code generation so no more instructions can be added after this call. 465 */ 466 467 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler); 468 469 /* Free executable code. */ 470 471 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code); 472 473 /* 474 When the protected executable allocator is used the JIT code is mapped 475 twice. The first mapping has read/write and the second mapping has read/exec 476 permissions. This function returns with the relative offset of the executable 477 mapping using the writable mapping as the base after the machine code is 478 successfully generated. The returned value is always 0 for the normal executable 479 allocator, since it uses only one mapping with read/write/exec permissions. 480 Dynamic code modifications requires this value. 481 482 Before a successful code generation, this function returns with 0. 483 */ 484 static SLJIT_INLINE sljit_sw sljit_get_executable_offset(struct sljit_compiler *compiler) { return compiler->executable_offset; } 485 486 /* 487 The executable memory consumption of the generated code can be retrieved by 488 this function. The returned value can be used for statistical purposes. 489 490 Before a successful code generation, this function returns with 0. 491 */ 492 static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; } 493 494 /* Instruction generation. Returns with any error code. If there is no 495 error, they return with SLJIT_SUCCESS. */ 496 497 /* 498 The executable code is a function call from the viewpoint of the C 499 language. The function calls must obey to the ABI (Application 500 Binary Interface) of the platform, which specify the purpose of 501 all machine registers and stack handling among other things. The 502 sljit_emit_enter function emits the necessary instructions for 503 setting up a new context for the executable code and moves function 504 arguments to the saved registers. Furthermore the options argument 505 can be used to pass configuration options to the compiler. The 506 available options are listed before sljit_emit_enter. 507 508 The number of sljit_sw arguments passed to the generated function 509 are specified in the "args" parameter. The number of arguments must 510 be less than or equal to 3. The first argument goes to SLJIT_S0, 511 the second goes to SLJIT_S1 and so on. The register set used by 512 the function must be declared as well. The number of scratch and 513 saved registers used by the function must be passed to sljit_emit_enter. 514 Only R registers between R0 and "scratches" argument can be used 515 later. E.g. if "scratches" is set to 2, the register set will be 516 limited to R0 and R1. The S registers and the floating point 517 registers ("fscratches" and "fsaveds") are specified in a similar 518 way. The sljit_emit_enter is also capable of allocating a stack 519 space for local variables. The "local_size" argument contains the 520 size in bytes of this local area and its staring address is stored 521 in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and 522 SLJIT_SP + local_size (exclusive) can be modified freely until 523 the function returns. The stack space is not initialized. 524 525 Note: the following conditions must met: 526 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS 527 0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS 528 scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS 529 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS 530 0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS 531 fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS 532 533 Note: every call of sljit_emit_enter and sljit_set_context 534 overwrites the previous context. 535 */ 536 537 /* The absolute address returned by sljit_get_local_base with 538 offset 0 is aligned to sljit_f64. Otherwise it is aligned to sljit_sw. */ 539 #define SLJIT_F64_ALIGNMENT 0x00000001 540 541 /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */ 542 #define SLJIT_MAX_LOCAL_SIZE 65536 543 544 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler, 545 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds, 546 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size); 547 548 /* The machine code has a context (which contains the local stack space size, 549 number of used registers, etc.) which initialized by sljit_emit_enter. Several 550 functions (like sljit_emit_return) requres this context to be able to generate 551 the appropriate code. However, some code fragments (like inline cache) may have 552 no normal entry point so their context is unknown for the compiler. Their context 553 can be provided to the compiler by the sljit_set_context function. 554 555 Note: every call of sljit_emit_enter and sljit_set_context overwrites 556 the previous context. */ 557 558 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler, 559 sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds, 560 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size); 561 562 /* Return from machine code. The op argument can be SLJIT_UNUSED which means the 563 function does not return with anything or any opcode between SLJIT_MOV and 564 SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op 565 is SLJIT_UNUSED, otherwise see below the description about source and 566 destination arguments. */ 567 568 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, 569 sljit_s32 src, sljit_sw srcw); 570 571 /* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and 572 even the stack frame is passed to the callee. The return address is preserved in 573 dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function 574 is sljit_p), and sljit_emit_fast_return can use this as a return value later. */ 575 576 /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine 577 instructions are needed. Excellent for small uility functions, where saving registers 578 and setting up a new stack frame would cost too much performance. However, it is still 579 possible to return to the address of the caller (or anywhere else). */ 580 581 /* Note: may destroy flags. */ 582 583 /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested, 584 since many architectures do clever branch prediction on call / return instruction pairs. */ 585 586 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw); 587 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw); 588 589 /* 590 Source and destination values for arithmetical instructions 591 imm - a simple immediate value (cannot be used as a destination) 592 reg - any of the registers (immediate argument must be 0) 593 [imm] - absolute immediate memory address 594 [reg+imm] - indirect memory address 595 [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3) 596 useful for (byte, half, int, sljit_sw) array access 597 (fully supported by both x86 and ARM architectures, and cheap operation on others) 598 */ 599 600 /* 601 IMPORATNT NOTE: memory access MUST be naturally aligned except 602 SLJIT_UNALIGNED macro is defined and its value is 1. 603 604 length | alignment 605 ---------+----------- 606 byte | 1 byte (any physical_address is accepted) 607 half | 2 byte (physical_address & 0x1 == 0) 608 int | 4 byte (physical_address & 0x3 == 0) 609 word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1 610 | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1 611 pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte 612 | on 64 bit machines) 613 614 Note: Different architectures have different addressing limitations. 615 A single instruction is enough for the following addressing 616 modes. Other adrressing modes are emulated by instruction 617 sequences. This information could help to improve those code 618 generators which focuses only a few architectures. 619 620 x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32) 621 [reg+(reg<<imm)] is supported 622 [imm], -2^32+1 <= imm <= 2^32-1 is supported 623 Write-back is not supported 624 arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed 625 bytes, any halfs or floating point values) 626 [reg+(reg<<imm)] is supported 627 Write-back is supported 628 arm-t2: [reg+imm], -255 <= imm <= 4095 629 [reg+(reg<<imm)] is supported 630 Write back is supported only for [reg+imm], where -255 <= imm <= 255 631 ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit 632 signed load on 64 bit requires immediates divisible by 4. 633 [reg+imm] is not supported for signed 8 bit values. 634 [reg+reg] is supported 635 Write-back is supported except for one instruction: 32 bit signed 636 load with [reg+imm] addressing mode on 64 bit. 637 mips: [reg+imm], -65536 <= imm <= 65535 638 sparc: [reg+imm], -4096 <= imm <= 4095 639 [reg+reg] is supported 640 */ 641 642 /* Register output: simply the name of the register. 643 For destination, you can use SLJIT_UNUSED as well. */ 644 #define SLJIT_MEM 0x80 645 #define SLJIT_MEM0() (SLJIT_MEM) 646 #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1)) 647 #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8)) 648 #define SLJIT_IMM 0x40 649 650 /* Set 32 bit operation mode (I) on 64 bit CPUs. This option is ignored on 651 32 bit CPUs. When this option is set for an arithmetic operation, only 652 the lower 32 bit of the input registers are used, and the CPU status 653 flags are set according to the 32 bit result. Although the higher 32 bit 654 of the input and the result registers are not defined by SLJIT, it might 655 be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU 656 requirements all source registers must be the result of those operations 657 where this option was also set. Memory loads read 32 bit values rather 658 than 64 bit ones. In other words 32 bit and 64 bit operations cannot 659 be mixed. The only exception is SLJIT_MOV32 and SLJIT_MOVU32 whose source 660 register can hold any 32 or 64 bit value, and it is converted to a 32 bit 661 compatible format first. This conversion is free (no instructions are 662 emitted) on most CPUs. A 32 bit value can also be converted to a 64 bit 663 value by SLJIT_MOV_S32 (sign extension) or SLJIT_MOV_U32 (zero extension). 664 665 Note: memory addressing always uses 64 bit values on 64 bit systems so 666 the result of a 32 bit operation must not be used with SLJIT_MEMx 667 macros. 668 669 This option is part of the instruction name, so there is no need to 670 manually set it. E.g: 671 672 SLJIT_ADD32 == (SLJIT_ADD | SLJIT_I32_OP) */ 673 #define SLJIT_I32_OP 0x100 674 675 /* Set F32 (single) precision mode for floating-point computation. This 676 option is similar to SLJIT_I32_OP, it just applies to floating point 677 registers. When this option is passed, the CPU performs 32 bit floating 678 point operations, rather than 64 bit one. Similar to SLJIT_I32_OP, all 679 register arguments must be the result of those operations where this 680 option was also set. 681 682 This option is part of the instruction name, so there is no need to 683 manually set it. E.g: 684 685 SLJIT_MOV_F32 = (SLJIT_MOV_F64 | SLJIT_F32_OP) 686 */ 687 #define SLJIT_F32_OP SLJIT_I32_OP 688 689 /* Many CPUs (x86, ARM, PPC) has status flags which can be set according 690 to the result of an operation. Other CPUs (MIPS) does not have status 691 flags, and results must be stored in registers. To cover both architecture 692 types efficiently only two flags are defined by SLJIT: 693 694 * Zero (equal) flag: it is set if the result is zero 695 * Variable flag: its value is defined by the last arithmetic operation 696 697 SLJIT instructions can set any or both of these flags. The value of 698 these flags is undefined if the instruction does not specify their value. 699 The description of each instruction contains the list of allowed flag 700 types. 701 702 Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence 703 704 sljit_op2(..., SLJIT_ADD, ...) 705 Both the zero and variable flags are undefined so their 706 they hold a random value after the operation is completed. 707 708 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...) 709 Sets the zero flag if the result is zero, clears it otherwise. 710 The variable flag is undefined. 711 712 sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...) 713 Sets the variable flag if an integer overflow occurs, clears 714 it otherwise. The zero flag is undefined. 715 716 sljit_op2(..., SLJIT_ADD | SLJIT_SET_NOT_OVERFLOW, ...) 717 Sets the variable flag if an integer overflow does NOT occur, 718 clears it otherwise. The zero flag is undefined. 719 720 sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...) 721 Sets the zero flag if the result is zero, clears it otherwise. 722 Sets the variable flag if unsigned overflow (carry) occurs, 723 clears it otherwise. 724 725 If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are 726 unchanged. 727 728 Using these flags can reduce the number of emitted instructions. E.g. a 729 fast loop can be implemented by decreasing a counter register and set the 730 zero flag to jump back if the counter register is not reached zero. 731 732 Motivation: although CPUs can set a large number of flags, usually their 733 values are ignored or only one of them is used. Emulating a large number 734 of flags on systems without flag register is complicated so SLJIT 735 instructions must specify the flag they want to use and only that flag 736 will be emulated. The last arithmetic instruction can be repeated if 737 multiple flags needs to be checked. 738 */ 739 740 /* Set Zero status flag. */ 741 #define SLJIT_SET_Z 0x0200 742 /* Set the variable status flag if condition is true. 743 See comparison types. */ 744 #define SLJIT_SET(condition) ((condition) << 10) 745 746 /* Notes: 747 - you cannot postpone conditional jump instructions except if noted that 748 the instruction does not set flags (See: SLJIT_KEEP_FLAGS). 749 - flag combinations: '|' means 'logical or'. */ 750 751 /* Starting index of opcodes for sljit_emit_op0. */ 752 #define SLJIT_OP0_BASE 0 753 754 /* Flags: - (does not modify flags) 755 Note: breakpoint instruction is not supported by all architectures (e.g. ppc) 756 It falls back to SLJIT_NOP in those cases. */ 757 #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0) 758 /* Flags: - (does not modify flags) 759 Note: may or may not cause an extra cycle wait 760 it can even decrease the runtime in a few cases. */ 761 #define SLJIT_NOP (SLJIT_OP0_BASE + 1) 762 /* Flags: - (may destroy flags) 763 Unsigned multiplication of SLJIT_R0 and SLJIT_R1. 764 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */ 765 #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2) 766 /* Flags: - (may destroy flags) 767 Signed multiplication of SLJIT_R0 and SLJIT_R1. 768 Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */ 769 #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3) 770 /* Flags: - (may destroy flags) 771 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1. 772 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1. 773 Note: if SLJIT_R1 is 0, the behaviour is undefined. */ 774 #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4) 775 #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_I32_OP) 776 /* Flags: - (may destroy flags) 777 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1. 778 The result is placed into SLJIT_R0 and the remainder into SLJIT_R1. 779 Note: if SLJIT_R1 is 0, the behaviour is undefined. 780 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00), 781 the behaviour is undefined. */ 782 #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5) 783 #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_I32_OP) 784 /* Flags: - (may destroy flags) 785 Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1. 786 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value. 787 Note: if SLJIT_R1 is 0, the behaviour is undefined. */ 788 #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6) 789 #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_I32_OP) 790 /* Flags: - (may destroy flags) 791 Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1. 792 The result is placed into SLJIT_R0. SLJIT_R1 preserves its value. 793 Note: if SLJIT_R1 is 0, the behaviour is undefined. 794 Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00), 795 the behaviour is undefined. */ 796 #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7) 797 #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_I32_OP) 798 799 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op); 800 801 /* Starting index of opcodes for sljit_emit_op1. */ 802 #define SLJIT_OP1_BASE 32 803 804 /* The MOV instruction transfer data from source to destination. 805 806 MOV instruction suffixes: 807 808 U8 - unsigned 8 bit data transfer 809 S8 - signed 8 bit data transfer 810 U16 - unsigned 16 bit data transfer 811 S16 - signed 16 bit data transfer 812 U32 - unsigned int (32 bit) data transfer 813 S32 - signed int (32 bit) data transfer 814 P - pointer (sljit_p) data transfer 815 816 U = move with update (pre form). If source or destination defined as 817 SLJIT_MEM1(r1) or SLJIT_MEM2(r1, r2), r1 is increased by the 818 offset part of the address. 819 820 Register arguments and base registers can only be used once for move 821 with update instructions. The shift value of SLJIT_MEM2 addressing 822 mode must also be 0. Reason: SLJIT_MOVU instructions are expected to 823 be in high-performance loops where complex instruction emulation 824 would be too costly. 825 826 Examples for invalid move with update instructions: 827 828 sljit_emit_op1(..., SLJIT_MOVU_U8, 829 SLJIT_R0, 0, SLJIT_MEM1(SLJIT_R0), 8); 830 sljit_emit_op1(..., SLJIT_MOVU_U8, 831 SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_R0, 0); 832 sljit_emit_op1(..., SLJIT_MOVU_U8, 833 SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 0, SLJIT_MEM1(SLJIT_R0), 8); 834 sljit_emit_op1(..., SLJIT_MOVU_U8, 835 SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 0, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0); 836 sljit_emit_op1(..., SLJIT_MOVU_U8, 837 SLJIT_R2, 0, SLJIT_MEM2(SLJIT_R0, SLJIT_R1), 1); 838 839 The following example is valid, since only the offset register is 840 used multiple times: 841 842 sljit_emit_op1(..., SLJIT_MOVU_U8, 843 SLJIT_MEM2(SLJIT_R0, SLJIT_R2), 0, SLJIT_MEM2(SLJIT_R1, SLJIT_R2), 0); 844 */ 845 846 /* Flags: - (does not modify flags) */ 847 #define SLJIT_MOV (SLJIT_OP1_BASE + 0) 848 /* Flags: - (does not modify flags) */ 849 #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1) 850 #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_I32_OP) 851 /* Flags: - (does not modify flags) */ 852 #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2) 853 #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_I32_OP) 854 /* Flags: - (does not modify flags) */ 855 #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3) 856 #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_I32_OP) 857 /* Flags: - (does not modify flags) */ 858 #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4) 859 #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_I32_OP) 860 /* Flags: - (does not modify flags) 861 Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */ 862 #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5) 863 /* Flags: - (does not modify flags) 864 Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */ 865 #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6) 866 /* Flags: - (does not modify flags) */ 867 #define SLJIT_MOV32 (SLJIT_MOV_S32 | SLJIT_I32_OP) 868 /* Flags: - (does not modify flags) */ 869 #define SLJIT_MOV_P (SLJIT_OP1_BASE + 7) 870 /* Flags: - (may destroy flags) */ 871 #define SLJIT_MOVU (SLJIT_OP1_BASE + 8) 872 /* Flags: - (may destroy flags) */ 873 #define SLJIT_MOVU_U8 (SLJIT_OP1_BASE + 9) 874 #define SLJIT_MOVU32_U8 (SLJIT_MOVU_U8 | SLJIT_I32_OP) 875 /* Flags: - (may destroy flags) */ 876 #define SLJIT_MOVU_S8 (SLJIT_OP1_BASE + 10) 877 #define SLJIT_MOVU32_S8 (SLJIT_MOVU_S8 | SLJIT_I32_OP) 878 /* Flags: - (may destroy flags) */ 879 #define SLJIT_MOVU_U16 (SLJIT_OP1_BASE + 11) 880 #define SLJIT_MOVU32_U16 (SLJIT_MOVU_U16 | SLJIT_I32_OP) 881 /* Flags: - (may destroy flags) */ 882 #define SLJIT_MOVU_S16 (SLJIT_OP1_BASE + 12) 883 #define SLJIT_MOVU32_S16 (SLJIT_MOVU_S16 | SLJIT_I32_OP) 884 /* Flags: - (may destroy flags) 885 Note: no SLJIT_MOVU32_U32 form, since it is the same as SLJIT_MOVU32 */ 886 #define SLJIT_MOVU_U32 (SLJIT_OP1_BASE + 13) 887 /* Flags: - (may destroy flags) 888 Note: no SLJIT_MOVU32_S32 form, since it is the same as SLJIT_MOVU32 */ 889 #define SLJIT_MOVU_S32 (SLJIT_OP1_BASE + 14) 890 /* Flags: - (may destroy flags) */ 891 #define SLJIT_MOVU32 (SLJIT_MOVU_S32 | SLJIT_I32_OP) 892 /* Flags: - (may destroy flags) */ 893 #define SLJIT_MOVU_P (SLJIT_OP1_BASE + 15) 894 /* Flags: Z */ 895 #define SLJIT_NOT (SLJIT_OP1_BASE + 16) 896 #define SLJIT_NOT32 (SLJIT_NOT | SLJIT_I32_OP) 897 /* Flags: Z | OVERFLOW */ 898 #define SLJIT_NEG (SLJIT_OP1_BASE + 17) 899 #define SLJIT_NEG32 (SLJIT_NEG | SLJIT_I32_OP) 900 /* Count leading zeroes 901 Flags: Z */ 902 #define SLJIT_CLZ (SLJIT_OP1_BASE + 18) 903 #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_I32_OP) 904 905 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op, 906 sljit_s32 dst, sljit_sw dstw, 907 sljit_s32 src, sljit_sw srcw); 908 909 /* Starting index of opcodes for sljit_emit_op2. */ 910 #define SLJIT_OP2_BASE 96 911 912 /* Flags: Z | OVERFLOW | CARRY */ 913 #define SLJIT_ADD (SLJIT_OP2_BASE + 0) 914 #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_I32_OP) 915 /* Flags: CARRY */ 916 #define SLJIT_ADDC (SLJIT_OP2_BASE + 1) 917 #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_I32_OP) 918 /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL 919 SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER 920 SIG_LESS_EQUAL | CARRY */ 921 #define SLJIT_SUB (SLJIT_OP2_BASE + 2) 922 #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_I32_OP) 923 /* Flags: CARRY */ 924 #define SLJIT_SUBC (SLJIT_OP2_BASE + 3) 925 #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_I32_OP) 926 /* Note: integer mul 927 Flags: MUL_OVERFLOW */ 928 #define SLJIT_MUL (SLJIT_OP2_BASE + 4) 929 #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_I32_OP) 930 /* Flags: Z */ 931 #define SLJIT_AND (SLJIT_OP2_BASE + 5) 932 #define SLJIT_AND32 (SLJIT_AND | SLJIT_I32_OP) 933 /* Flags: Z */ 934 #define SLJIT_OR (SLJIT_OP2_BASE + 6) 935 #define SLJIT_OR32 (SLJIT_OR | SLJIT_I32_OP) 936 /* Flags: Z */ 937 #define SLJIT_XOR (SLJIT_OP2_BASE + 7) 938 #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_I32_OP) 939 /* Flags: Z 940 Let bit_length be the length of the shift operation: 32 or 64. 941 If src2 is immediate, src2w is masked by (bit_length - 1). 942 Otherwise, if the content of src2 is outside the range from 0 943 to bit_length - 1, the result is undefined. */ 944 #define SLJIT_SHL (SLJIT_OP2_BASE + 8) 945 #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_I32_OP) 946 /* Flags: Z 947 Let bit_length be the length of the shift operation: 32 or 64. 948 If src2 is immediate, src2w is masked by (bit_length - 1). 949 Otherwise, if the content of src2 is outside the range from 0 950 to bit_length - 1, the result is undefined. */ 951 #define SLJIT_LSHR (SLJIT_OP2_BASE + 9) 952 #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_I32_OP) 953 /* Flags: Z 954 Let bit_length be the length of the shift operation: 32 or 64. 955 If src2 is immediate, src2w is masked by (bit_length - 1). 956 Otherwise, if the content of src2 is outside the range from 0 957 to bit_length - 1, the result is undefined. */ 958 #define SLJIT_ASHR (SLJIT_OP2_BASE + 10) 959 #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_I32_OP) 960 961 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op, 962 sljit_s32 dst, sljit_sw dstw, 963 sljit_s32 src1, sljit_sw src1w, 964 sljit_s32 src2, sljit_sw src2w); 965 966 /* Returns with non-zero if fpu is available. */ 967 968 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_is_fpu_available(void); 969 970 /* Starting index of opcodes for sljit_emit_fop1. */ 971 #define SLJIT_FOP1_BASE 128 972 973 /* Flags: - (does not modify flags) */ 974 #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0) 975 #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_F32_OP) 976 /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE] 977 SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int 978 Rounding mode when the destination is W or I: round towards zero. */ 979 /* Flags: - (does not modify flags) */ 980 #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1) 981 #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_F32_OP) 982 /* Flags: - (does not modify flags) */ 983 #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2) 984 #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_F32_OP) 985 /* Flags: - (does not modify flags) */ 986 #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3) 987 #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_F32_OP) 988 /* Flags: - (does not modify flags) */ 989 #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4) 990 #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_F32_OP) 991 /* Flags: - (does not modify flags) */ 992 #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5) 993 #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_F32_OP) 994 /* Note: dst is the left and src is the right operand for SLJIT_CMPD. 995 Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */ 996 #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 6) 997 #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_F32_OP) 998 /* Flags: - (does not modify flags) */ 999 #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 7) 1000 #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_F32_OP) 1001 /* Flags: - (does not modify flags) */ 1002 #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 8) 1003 #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_F32_OP) 1004 1005 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op, 1006 sljit_s32 dst, sljit_sw dstw, 1007 sljit_s32 src, sljit_sw srcw); 1008 1009 /* Starting index of opcodes for sljit_emit_fop2. */ 1010 #define SLJIT_FOP2_BASE 160 1011 1012 /* Flags: - (does not modify flags) */ 1013 #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0) 1014 #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_F32_OP) 1015 /* Flags: - (does not modify flags) */ 1016 #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1) 1017 #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_F32_OP) 1018 /* Flags: - (does not modify flags) */ 1019 #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2) 1020 #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_F32_OP) 1021 /* Flags: - (does not modify flags) */ 1022 #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3) 1023 #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_F32_OP) 1024 1025 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op, 1026 sljit_s32 dst, sljit_sw dstw, 1027 sljit_s32 src1, sljit_sw src1w, 1028 sljit_s32 src2, sljit_sw src2w); 1029 1030 /* Label and jump instructions. */ 1031 1032 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler); 1033 1034 /* Invert (negate) conditional type: xor (^) with 0x1 */ 1035 1036 /* Integer comparison types. */ 1037 #define SLJIT_EQUAL 0 1038 #define SLJIT_EQUAL32 (SLJIT_EQUAL | SLJIT_I32_OP) 1039 #define SLJIT_ZERO 0 1040 #define SLJIT_ZERO32 (SLJIT_ZERO | SLJIT_I32_OP) 1041 #define SLJIT_NOT_EQUAL 1 1042 #define SLJIT_NOT_EQUAL32 (SLJIT_NOT_EQUAL | SLJIT_I32_OP) 1043 #define SLJIT_NOT_ZERO 1 1044 #define SLJIT_NOT_ZERO32 (SLJIT_NOT_ZERO | SLJIT_I32_OP) 1045 1046 #define SLJIT_LESS 2 1047 #define SLJIT_LESS32 (SLJIT_LESS | SLJIT_I32_OP) 1048 #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS) 1049 #define SLJIT_GREATER_EQUAL 3 1050 #define SLJIT_GREATER_EQUAL32 (SLJIT_GREATER_EQUAL | SLJIT_I32_OP) 1051 #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_GREATER_EQUAL) 1052 #define SLJIT_GREATER 4 1053 #define SLJIT_GREATER32 (SLJIT_GREATER | SLJIT_I32_OP) 1054 #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER) 1055 #define SLJIT_LESS_EQUAL 5 1056 #define SLJIT_LESS_EQUAL32 (SLJIT_LESS_EQUAL | SLJIT_I32_OP) 1057 #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_LESS_EQUAL) 1058 #define SLJIT_SIG_LESS 6 1059 #define SLJIT_SIG_LESS32 (SLJIT_SIG_LESS | SLJIT_I32_OP) 1060 #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS) 1061 #define SLJIT_SIG_GREATER_EQUAL 7 1062 #define SLJIT_SIG_GREATER_EQUAL32 (SLJIT_SIG_GREATER_EQUAL | SLJIT_I32_OP) 1063 #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SET_SIG_GREATER_EQUAL) 1064 #define SLJIT_SIG_GREATER 8 1065 #define SLJIT_SIG_GREATER32 (SLJIT_SIG_GREATER | SLJIT_I32_OP) 1066 #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER) 1067 #define SLJIT_SIG_LESS_EQUAL 9 1068 #define SLJIT_SIG_LESS_EQUAL32 (SLJIT_SIG_LESS_EQUAL | SLJIT_I32_OP) 1069 #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_LESS_EQUAL) 1070 1071 #define SLJIT_OVERFLOW 10 1072 #define SLJIT_OVERFLOW32 (SLJIT_OVERFLOW | SLJIT_I32_OP) 1073 #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW) 1074 #define SLJIT_NOT_OVERFLOW 11 1075 #define SLJIT_NOT_OVERFLOW32 (SLJIT_NOT_OVERFLOW | SLJIT_I32_OP) 1076 #define SLJIT_SET_NOT_OVERFLOW SLJIT_SET(SLJIT_NOT_OVERFLOW) 1077 1078 #define SLJIT_MUL_OVERFLOW 12 1079 #define SLJIT_MUL_OVERFLOW32 (SLJIT_MUL_OVERFLOW | SLJIT_I32_OP) 1080 #define SLJIT_SET_MUL_OVERFLOW SLJIT_SET(SLJIT_MUL_OVERFLOW) 1081 #define SLJIT_MUL_NOT_OVERFLOW 13 1082 #define SLJIT_MUL_NOT_OVERFLOW32 (SLJIT_MUL_NOT_OVERFLOW | SLJIT_I32_OP) 1083 #define SLJIT_SET_MUL_NOT_OVERFLOW SLJIT_SET(SLJIT_MUL_NOT_OVERFLOW) 1084 1085 /* There is no SLJIT_CARRY or SLJIT_NOT_CARRY. */ 1086 #define SLJIT_SET_CARRY SLJIT_SET(14) 1087 1088 /* Floating point comparison types. */ 1089 #define SLJIT_EQUAL_F64 16 1090 #define SLJIT_EQUAL_F32 (SLJIT_EQUAL_F64 | SLJIT_F32_OP) 1091 #define SLJIT_SET_EQUAL_F SLJIT_SET(SLJIT_EQUAL_F64) 1092 #define SLJIT_NOT_EQUAL_F64 17 1093 #define SLJIT_NOT_EQUAL_F32 (SLJIT_NOT_EQUAL_F64 | SLJIT_F32_OP) 1094 #define SLJIT_SET_NOT_EQUAL_F SLJIT_SET(SLJIT_NOT_EQUAL_F64) 1095 #define SLJIT_LESS_F64 18 1096 #define SLJIT_LESS_F32 (SLJIT_LESS_F64 | SLJIT_F32_OP) 1097 #define SLJIT_SET_LESS_F SLJIT_SET(SLJIT_LESS_F64) 1098 #define SLJIT_GREATER_EQUAL_F64 19 1099 #define SLJIT_GREATER_EQUAL_F32 (SLJIT_GREATER_EQUAL_F64 | SLJIT_F32_OP) 1100 #define SLJIT_SET_GREATER_EQUAL_F SLJIT_SET(SLJIT_GREATER_EQUAL_F64) 1101 #define SLJIT_GREATER_F64 20 1102 #define SLJIT_GREATER_F32 (SLJIT_GREATER_F64 | SLJIT_F32_OP) 1103 #define SLJIT_SET_GREATER_F SLJIT_SET(SLJIT_GREATER_F64) 1104 #define SLJIT_LESS_EQUAL_F64 21 1105 #define SLJIT_LESS_EQUAL_F32 (SLJIT_LESS_EQUAL_F64 | SLJIT_F32_OP) 1106 #define SLJIT_SET_LESS_EQUAL_F SLJIT_SET(SLJIT_LESS_EQUAL_F64) 1107 #define SLJIT_UNORDERED_F64 22 1108 #define SLJIT_UNORDERED_F32 (SLJIT_UNORDERED_F64 | SLJIT_F32_OP) 1109 #define SLJIT_SET_UNORDERED_F SLJIT_SET(SLJIT_UNORDERED_F64) 1110 #define SLJIT_ORDERED_F64 23 1111 #define SLJIT_ORDERED_F32 (SLJIT_ORDERED_F64 | SLJIT_F32_OP) 1112 #define SLJIT_SET_ORDERED_F SLJIT_SET(SLJIT_ORDERED_F64) 1113 1114 /* Unconditional jump types. */ 1115 #define SLJIT_JUMP 24 1116 #define SLJIT_FAST_CALL 25 1117 #define SLJIT_CALL0 26 1118 #define SLJIT_CALL1 27 1119 #define SLJIT_CALL2 28 1120 #define SLJIT_CALL3 29 1121 1122 /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */ 1123 1124 /* The target can be changed during runtime (see: sljit_set_jump_addr). */ 1125 #define SLJIT_REWRITABLE_JUMP 0x1000 1126 1127 /* Emit a jump instruction. The destination is not set, only the type of the jump. 1128 type must be between SLJIT_EQUAL and SLJIT_CALL3 1129 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP 1130 1131 Flags: does not modify flags for conditional and unconditional 1132 jumps but destroy all flags for calls. */ 1133 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type); 1134 1135 /* Basic arithmetic comparison. In most architectures it is implemented as 1136 an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting 1137 appropriate flags) followed by a sljit_emit_jump. However some 1138 architectures (i.e: ARM64 or MIPS) may employ special optimizations here. 1139 It is suggested to use this comparison form when appropriate. 1140 type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL 1141 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP 1142 Flags: may destroy flags. */ 1143 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type, 1144 sljit_s32 src1, sljit_sw src1w, 1145 sljit_s32 src2, sljit_sw src2w); 1146 1147 /* Basic floating point comparison. In most architectures it is implemented as 1148 an SLJIT_FCMP operation (setting appropriate flags) followed by a 1149 sljit_emit_jump. However some architectures (i.e: MIPS) may employ 1150 special optimizations here. It is suggested to use this comparison form 1151 when appropriate. 1152 type must be between SLJIT_EQUAL_F64 and SLJIT_ORDERED_F32 1153 type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP 1154 Flags: destroy flags. 1155 Note: if either operand is NaN, the behaviour is undefined for 1156 types up to SLJIT_S_LESS_EQUAL. */ 1157 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_s32 type, 1158 sljit_s32 src1, sljit_sw src1w, 1159 sljit_s32 src2, sljit_sw src2w); 1160 1161 /* Set the destination of the jump to this label. */ 1162 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label); 1163 /* Set the destination address of the jump to this label. */ 1164 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target); 1165 1166 /* Call function or jump anywhere. Both direct and indirect form 1167 type must be between SLJIT_JUMP and SLJIT_CALL3 1168 Direct form: set src to SLJIT_IMM() and srcw to the address 1169 Indirect form: any other valid addressing mode 1170 1171 Flags: does not modify flags for unconditional jumps but 1172 destroy all flags for calls. */ 1173 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw); 1174 1175 /* Perform the operation using the conditional flags as the second argument. 1176 Type must always be between SLJIT_EQUAL and SLJIT_S_ORDERED. The value 1177 represented by the type is 1, if the condition represented by the type 1178 is fulfilled, and 0 otherwise. 1179 1180 If op == SLJIT_MOV, SLJIT_MOV_S32, SLJIT_MOV_U32: 1181 Set dst to the value represented by the type (0 or 1). 1182 Src must be SLJIT_UNUSED, and srcw must be 0 1183 Flags: - (does not modify flags) 1184 If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR 1185 Performs the binary operation using src as the first, and the value 1186 represented by type as the second argument. 1187 Important note: only dst=src and dstw=srcw is supported at the moment! 1188 Flags: Z (may destroy flags) 1189 Note: sljit_emit_op_flags does nothing, if dst is SLJIT_UNUSED (regardless of op). */ 1190 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op, 1191 sljit_s32 dst, sljit_sw dstw, 1192 sljit_s32 src, sljit_sw srcw, 1193 sljit_s32 type); 1194 1195 /* Copies the base address of SLJIT_SP + offset to dst. 1196 Flags: - (may destroy flags) */ 1197 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset); 1198 1199 /* The constant can be changed runtime (see: sljit_set_const) 1200 Flags: - (does not modify flags) */ 1201 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value); 1202 1203 /* After the code generation the address for label, jump and const instructions 1204 are computed. Since these structures are freed by sljit_free_compiler, the 1205 addresses must be preserved by the user program elsewere. */ 1206 static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; } 1207 static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; } 1208 static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; } 1209 1210 /* Only the address and executable offset are required to perform dynamic 1211 code modifications. See sljit_get_executable_offset function. */ 1212 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset); 1213 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset); 1214 1215 /* --------------------------------------------------------------------- */ 1216 /* Miscellaneous utility functions */ 1217 /* --------------------------------------------------------------------- */ 1218 1219 #define SLJIT_MAJOR_VERSION 0 1220 #define SLJIT_MINOR_VERSION 93 1221 1222 /* Get the human readable name of the platform. Can be useful on platforms 1223 like ARM, where ARM and Thumb2 functions can be mixed, and 1224 it is useful to know the type of the code generator. */ 1225 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void); 1226 1227 /* Portable helper function to get an offset of a member. */ 1228 #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10) 1229 1230 #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK) 1231 /* This global lock is useful to compile common functions. */ 1232 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void); 1233 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void); 1234 #endif 1235 1236 #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) 1237 1238 /* The sljit_stack is a utility extension of sljit, which provides 1239 a top-down stack. The stack starts at base and goes down to 1240 max_limit, so the memory region for this stack is between 1241 max_limit (inclusive) and base (exclusive). However the 1242 application can only use the region between limit (inclusive) 1243 and base (exclusive). The sljit_stack_resize can be used to 1244 extend this region up to max_limit. 1245 1246 This feature uses the "address space reserve" feature of modern 1247 operating systems, so instead of allocating a huge memory block 1248 applications can allocate a small region and extend it later 1249 without moving the memory area. Hence pointers can be stored 1250 in this area. */ 1251 1252 /* Note: base and max_limit fields are aligned to PAGE_SIZE bytes 1253 (usually 4 Kbyte or more). 1254 Note: stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. 1255 Note: this structure may not be supported by all operating systems. 1256 Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK 1257 is not defined. */ 1258 1259 struct sljit_stack { 1260 /* User data, anything can be stored here. 1261 Starting with the same value as base. */ 1262 sljit_u8 *top; 1263 /* These members are read only. */ 1264 sljit_u8 *base; 1265 sljit_u8 *limit; 1266 sljit_u8 *max_limit; 1267 }; 1268 1269 /* Returns NULL if unsuccessful. 1270 Note: max_limit contains the maximum stack size in bytes. 1271 Note: limit contains the starting stack size in bytes. 1272 Note: the top field is initialized to base. 1273 Note: see sljit_create_compiler for the explanation of allocator_data. */ 1274 SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data); 1275 SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack *stack, void *allocator_data); 1276 1277 /* Can be used to increase (allocate) or decrease (free) the memory area. 1278 Returns with a non-zero value if unsuccessful. If new_limit is greater than 1279 max_limit, it will fail. It is very easy to implement a stack data structure, 1280 since the growth ratio can be added to the current limit, and sljit_stack_resize 1281 will do all the necessary checks. The fields of the stack are not changed if 1282 sljit_stack_resize fails. */ 1283 SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_limit); 1284 1285 #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */ 1286 1287 #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) 1288 1289 /* Get the entry address of a given function. */ 1290 #define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name) 1291 1292 #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */ 1293 1294 /* All JIT related code should be placed in the same context (library, binary, etc.). */ 1295 1296 #define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name) 1297 1298 /* For powerpc64, the function pointers point to a context descriptor. */ 1299 struct sljit_function_context { 1300 sljit_sw addr; 1301 sljit_sw r2; 1302 sljit_sw r11; 1303 }; 1304 1305 /* Fill the context arguments using the addr and the function. 1306 If func_ptr is NULL, it will not be set to the address of context 1307 If addr is NULL, the function address also comes from the func pointer. */ 1308 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func); 1309 1310 #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */ 1311 1312 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) 1313 /* Free unused executable memory. The allocator keeps some free memory 1314 around to reduce the number of OS executable memory allocations. 1315 This improves performance since these calls are costly. However 1316 it is sometimes desired to free all unused memory regions, e.g. 1317 before the application terminates. */ 1318 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void); 1319 #endif 1320 1321 /* --------------------------------------------------------------------- */ 1322 /* CPU specific functions */ 1323 /* --------------------------------------------------------------------- */ 1324 1325 /* The following function is a helper function for sljit_emit_op_custom. 1326 It returns with the real machine register index ( >=0 ) of any SLJIT_R, 1327 SLJIT_S and SLJIT_SP registers. 1328 1329 Note: it returns with -1 for virtual registers (only on x86-32). */ 1330 1331 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg); 1332 1333 /* The following function is a helper function for sljit_emit_op_custom. 1334 It returns with the real machine register index of any SLJIT_FLOAT register. 1335 1336 Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */ 1337 1338 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg); 1339 1340 /* Any instruction can be inserted into the instruction stream by 1341 sljit_emit_op_custom. It has a similar purpose as inline assembly. 1342 The size parameter must match to the instruction size of the target 1343 architecture: 1344 1345 x86: 0 < size <= 15. The instruction argument can be byte aligned. 1346 Thumb2: if size == 2, the instruction argument must be 2 byte aligned. 1347 if size == 4, the instruction argument must be 4 byte aligned. 1348 Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */ 1349 1350 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler, 1351 void *instruction, sljit_s32 size); 1352 1353 /* Define the currently available CPU status flags. It is usually used after an 1354 sljit_emit_op_custom call to define which flags are set. */ 1355 1356 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_current_flags(struct sljit_compiler *compiler, 1357 sljit_s32 current_flags); 1358 1359 #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86) 1360 1361 /* Returns with non-zero if sse2 is available. */ 1362 1363 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_x86_is_sse2_available(void); 1364 1365 /* Returns with non-zero if cmov instruction is available. */ 1366 1367 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_x86_is_cmov_available(void); 1368 1369 /* Emit a conditional mov instruction on x86 CPUs. This instruction 1370 moves src to destination, if the condition is satisfied. Unlike 1371 other arithmetic instructions, destination must be a register. 1372 Before such instructions are emitted, cmov support should be 1373 checked by sljit_x86_is_cmov_available function. 1374 type must be between SLJIT_EQUAL and SLJIT_S_ORDERED 1375 dst_reg must be a valid register and it can be combined 1376 with SLJIT_I32_OP to perform 32 bit arithmetic 1377 Flags: - (does not modify flags) 1378 */ 1379 1380 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_x86_emit_cmov(struct sljit_compiler *compiler, 1381 sljit_s32 type, 1382 sljit_s32 dst_reg, 1383 sljit_s32 src, sljit_sw srcw); 1384 1385 #endif 1386 1387 #endif /* _SLJIT_LIR_H_ */ 1388