sljitLir.c revision 1.4 1 /* $NetBSD: sljitLir.c,v 1.4 2014/06/17 19:33:20 alnsn Exp $ */
2
3 /*
4 * Stack-less Just-In-Time compiler
5 *
6 * Copyright 2009-2012 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 #include "sljitLir.h"
30
31 #define CHECK_ERROR() \
32 do { \
33 if (SLJIT_UNLIKELY(compiler->error)) \
34 return compiler->error; \
35 } while (0)
36
37 #define CHECK_ERROR_PTR() \
38 do { \
39 if (SLJIT_UNLIKELY(compiler->error)) \
40 return NULL; \
41 } while (0)
42
43 #define CHECK_ERROR_VOID() \
44 do { \
45 if (SLJIT_UNLIKELY(compiler->error)) \
46 return; \
47 } while (0)
48
49 #define FAIL_IF(expr) \
50 do { \
51 if (SLJIT_UNLIKELY(expr)) \
52 return compiler->error; \
53 } while (0)
54
55 #define PTR_FAIL_IF(expr) \
56 do { \
57 if (SLJIT_UNLIKELY(expr)) \
58 return NULL; \
59 } while (0)
60
61 #define FAIL_IF_NULL(ptr) \
62 do { \
63 if (SLJIT_UNLIKELY(!(ptr))) { \
64 compiler->error = SLJIT_ERR_ALLOC_FAILED; \
65 return SLJIT_ERR_ALLOC_FAILED; \
66 } \
67 } while (0)
68
69 #define PTR_FAIL_IF_NULL(ptr) \
70 do { \
71 if (SLJIT_UNLIKELY(!(ptr))) { \
72 compiler->error = SLJIT_ERR_ALLOC_FAILED; \
73 return NULL; \
74 } \
75 } while (0)
76
77 #define PTR_FAIL_WITH_EXEC_IF(ptr) \
78 do { \
79 if (SLJIT_UNLIKELY(!(ptr))) { \
80 compiler->error = SLJIT_ERR_EX_ALLOC_FAILED; \
81 return NULL; \
82 } \
83 } while (0)
84
85 #if !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
86
87 #define GET_OPCODE(op) \
88 ((op) & ~(SLJIT_INT_OP | SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))
89
90 #define GET_FLAGS(op) \
91 ((op) & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C))
92
93 #define GET_ALL_FLAGS(op) \
94 ((op) & (SLJIT_INT_OP | SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))
95
96 #define TYPE_CAST_NEEDED(op) \
97 (((op) >= SLJIT_MOV_UB && (op) <= SLJIT_MOV_SH) || ((op) >= SLJIT_MOVU_UB && (op) <= SLJIT_MOVU_SH))
98
99 #define BUF_SIZE 4096
100
101 #if (defined SLJIT_32BIT_ARCHITECTURE && SLJIT_32BIT_ARCHITECTURE)
102 #define ABUF_SIZE 2048
103 #else
104 #define ABUF_SIZE 4096
105 #endif
106
107 /* Parameter parsing. */
108 #define REG_MASK 0x3f
109 #define OFFS_REG(reg) (((reg) >> 8) & REG_MASK)
110 #define OFFS_REG_MASK (REG_MASK << 8)
111 #define TO_OFFS_REG(reg) ((reg) << 8)
112 /* When reg cannot be unused. */
113 #define FAST_IS_REG(reg) ((reg) <= REG_MASK)
114 /* When reg can be unused. */
115 #define SLOW_IS_REG(reg) ((reg) > 0 && (reg) <= REG_MASK)
116
117 /* Jump flags. */
118 #define JUMP_LABEL 0x1
119 #define JUMP_ADDR 0x2
120 /* SLJIT_REWRITABLE_JUMP is 0x1000. */
121
122 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
123 # define PATCH_MB 0x4
124 # define PATCH_MW 0x8
125 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
126 # define PATCH_MD 0x10
127 #endif
128 #endif
129
130 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
131 # define IS_BL 0x4
132 # define PATCH_B 0x8
133 #endif
134
135 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
136 # define CPOOL_SIZE 512
137 #endif
138
139 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
140 # define IS_COND 0x04
141 # define IS_BL 0x08
142 /* conditional + imm8 */
143 # define PATCH_TYPE1 0x10
144 /* conditional + imm20 */
145 # define PATCH_TYPE2 0x20
146 /* IT + imm24 */
147 # define PATCH_TYPE3 0x30
148 /* imm11 */
149 # define PATCH_TYPE4 0x40
150 /* imm24 */
151 # define PATCH_TYPE5 0x50
152 /* BL + imm24 */
153 # define PATCH_BL 0x60
154 /* 0xf00 cc code for branches */
155 #endif
156
157 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
158 # define IS_COND 0x004
159 # define IS_CBZ 0x008
160 # define IS_BL 0x010
161 # define PATCH_B 0x020
162 # define PATCH_COND 0x040
163 # define PATCH_ABS48 0x080
164 # define PATCH_ABS64 0x100
165 #endif
166
167 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
168 # define IS_COND 0x004
169 # define IS_CALL 0x008
170 # define PATCH_B 0x010
171 # define PATCH_ABS_B 0x020
172 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
173 # define PATCH_ABS32 0x040
174 # define PATCH_ABS48 0x080
175 #endif
176 # define REMOVE_COND 0x100
177 #endif
178
179 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) || (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
180 # define IS_MOVABLE 0x004
181 # define IS_JAL 0x008
182 # define IS_CALL 0x010
183 # define IS_BIT26_COND 0x020
184 # define IS_BIT16_COND 0x040
185
186 # define IS_COND (IS_BIT26_COND | IS_BIT16_COND)
187
188 # define PATCH_B 0x080
189 # define PATCH_J 0x100
190
191 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
192 # define PATCH_ABS32 0x200
193 # define PATCH_ABS48 0x400
194 #endif
195
196 /* instruction types */
197 # define MOVABLE_INS 0
198 /* 1 - 31 last destination register */
199 /* no destination (i.e: store) */
200 # define UNMOVABLE_INS 32
201 /* FPU status register */
202 # define FCSR_FCC 33
203 #endif
204
205 #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
206 # define IS_JAL 0x04
207 # define IS_COND 0x08
208
209 # define PATCH_B 0x10
210 # define PATCH_J 0x20
211 #endif
212
213 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
214 # define IS_MOVABLE 0x04
215 # define IS_COND 0x08
216 # define IS_CALL 0x10
217
218 # define PATCH_B 0x20
219 # define PATCH_CALL 0x40
220
221 /* instruction types */
222 # define MOVABLE_INS 0
223 /* 1 - 31 last destination register */
224 /* no destination (i.e: store) */
225 # define UNMOVABLE_INS 32
226
227 # define DST_INS_MASK 0xff
228
229 /* ICC_SET is the same as SET_FLAGS. */
230 # define ICC_IS_SET (1 << 23)
231 # define FCC_IS_SET (1 << 24)
232 #endif
233
234 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
235 #define SLJIT_HAS_VARIABLE_LOCALS_OFFSET 1
236 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
237 #define FIXED_LOCALS_OFFSET (3 * sizeof(sljit_sw))
238 #endif
239 #endif
240
241 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
242 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
243 #ifdef _WIN64
244 #define FIXED_LOCALS_OFFSET ((4 + 2) * sizeof(sljit_sw))
245 #else
246 #define FIXED_LOCALS_OFFSET (sizeof(sljit_sw))
247 #endif
248 #endif
249
250 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
251 #define SLJIT_HAS_VARIABLE_LOCALS_OFFSET 1
252 #endif
253
254 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
255 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
256 #ifdef _AIX
257 #define FIXED_LOCALS_OFFSET ((6 + 8) * sizeof(sljit_sw))
258 #else
259 #define FIXED_LOCALS_OFFSET (2 * sizeof(sljit_sw))
260 #endif
261 #endif
262
263 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
264 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
265 #define FIXED_LOCALS_OFFSET ((6 + 8) * sizeof(sljit_sw))
266 #endif
267
268 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
269 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
270 #define FIXED_LOCALS_OFFSET (4 * sizeof(sljit_sw))
271 #endif
272
273 #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
274 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
275 #define FIXED_LOCALS_OFFSET 0
276 #endif
277
278 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
279 #define SLJIT_HAS_FIXED_LOCALS_OFFSET 1
280 #define FIXED_LOCALS_OFFSET (23 * sizeof(sljit_sw))
281 #endif
282
283 #if (defined SLJIT_HAS_VARIABLE_LOCALS_OFFSET && SLJIT_HAS_VARIABLE_LOCALS_OFFSET)
284
285 #define ADJUST_LOCAL_OFFSET(p, i) \
286 if ((p) == (SLJIT_MEM1(SLJIT_LOCALS_REG))) \
287 (i) += compiler->locals_offset;
288
289 #elif (defined SLJIT_HAS_FIXED_LOCALS_OFFSET && SLJIT_HAS_FIXED_LOCALS_OFFSET)
290
291 #define ADJUST_LOCAL_OFFSET(p, i) \
292 if ((p) == (SLJIT_MEM1(SLJIT_LOCALS_REG))) \
293 (i) += FIXED_LOCALS_OFFSET;
294
295 #else
296
297 #define ADJUST_LOCAL_OFFSET(p, i)
298
299 #endif
300
301 #endif /* !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) */
302
303 /* Utils can still be used even if SLJIT_CONFIG_UNSUPPORTED is set. */
304 #include "sljitUtils.c"
305
306 #if !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
307
308 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
309 #include "sljitExecAllocator.c"
310 #endif
311
312 #if (defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) && !(defined SLJIT_SSE2 && SLJIT_SSE2)
313 #error SLJIT_SSE2_AUTO cannot be enabled without SLJIT_SSE2
314 #endif
315
316 /* --------------------------------------------------------------------- */
317 /* Public functions */
318 /* --------------------------------------------------------------------- */
319
320 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || ((defined SLJIT_SSE2 && SLJIT_SSE2) && ((defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)))
321 #define SLJIT_NEEDS_COMPILER_INIT 1
322 static sljit_si compiler_initialized = 0;
323 /* A thread safe initialization. */
324 static void init_compiler(void);
325 #endif
326
327 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void)
328 {
329 struct sljit_compiler *compiler = (struct sljit_compiler*)SLJIT_MALLOC(sizeof(struct sljit_compiler));
330 if (!compiler)
331 return NULL;
332 SLJIT_ZEROMEM(compiler, sizeof(struct sljit_compiler));
333
334 SLJIT_COMPILE_ASSERT(
335 sizeof(sljit_sb) == 1 && sizeof(sljit_ub) == 1
336 && sizeof(sljit_sh) == 2 && sizeof(sljit_uh) == 2
337 && sizeof(sljit_si) == 4 && sizeof(sljit_ui) == 4
338 && (sizeof(sljit_p) == 4 || sizeof(sljit_p) == 8)
339 && sizeof(sljit_p) <= sizeof(sljit_sw)
340 && (sizeof(sljit_sw) == 4 || sizeof(sljit_sw) == 8)
341 && (sizeof(sljit_uw) == 4 || sizeof(sljit_uw) == 8),
342 invalid_integer_types);
343 SLJIT_COMPILE_ASSERT(SLJIT_INT_OP == SLJIT_SINGLE_OP,
344 int_op_and_single_op_must_be_the_same);
345 SLJIT_COMPILE_ASSERT(SLJIT_REWRITABLE_JUMP != SLJIT_SINGLE_OP,
346 rewritable_jump_and_single_op_must_not_be_the_same);
347
348 /* Only the non-zero members must be set. */
349 compiler->error = SLJIT_SUCCESS;
350
351 compiler->buf = (struct sljit_memory_fragment*)SLJIT_MALLOC(BUF_SIZE);
352 compiler->abuf = (struct sljit_memory_fragment*)SLJIT_MALLOC(ABUF_SIZE);
353
354 if (!compiler->buf || !compiler->abuf) {
355 if (compiler->buf)
356 SLJIT_FREE(compiler->buf);
357 if (compiler->abuf)
358 SLJIT_FREE(compiler->abuf);
359 SLJIT_FREE(compiler);
360 return NULL;
361 }
362
363 compiler->buf->next = NULL;
364 compiler->buf->used_size = 0;
365 compiler->abuf->next = NULL;
366 compiler->abuf->used_size = 0;
367
368 compiler->scratches = -1;
369 compiler->saveds = -1;
370
371 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
372 compiler->args = -1;
373 #endif
374
375 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
376 compiler->cpool = (sljit_uw*)SLJIT_MALLOC(CPOOL_SIZE * sizeof(sljit_uw) + CPOOL_SIZE * sizeof(sljit_ub));
377 if (!compiler->cpool) {
378 SLJIT_FREE(compiler->buf);
379 SLJIT_FREE(compiler->abuf);
380 SLJIT_FREE(compiler);
381 return NULL;
382 }
383 compiler->cpool_unique = (sljit_ub*)(compiler->cpool + CPOOL_SIZE);
384 compiler->cpool_diff = 0xffffffff;
385 #endif
386
387 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) || (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
388 compiler->delay_slot = UNMOVABLE_INS;
389 #endif
390
391 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
392 compiler->delay_slot = UNMOVABLE_INS;
393 #endif
394
395 #if (defined SLJIT_NEEDS_COMPILER_INIT && SLJIT_NEEDS_COMPILER_INIT)
396 if (!compiler_initialized) {
397 init_compiler();
398 compiler_initialized = 1;
399 }
400 #endif
401
402 return compiler;
403 }
404
405 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler)
406 {
407 struct sljit_memory_fragment *buf;
408 struct sljit_memory_fragment *curr;
409
410 buf = compiler->buf;
411 while (buf) {
412 curr = buf;
413 buf = buf->next;
414 SLJIT_FREE(curr);
415 }
416
417 buf = compiler->abuf;
418 while (buf) {
419 curr = buf;
420 buf = buf->next;
421 SLJIT_FREE(curr);
422 }
423
424 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
425 SLJIT_FREE(compiler->cpool);
426 #endif
427 SLJIT_FREE(compiler);
428 }
429
430 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
431 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
432 {
433 /* Remove thumb mode flag. */
434 SLJIT_FREE_EXEC((void*)((sljit_uw)code & ~0x1));
435 }
436 #elif (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
437 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
438 {
439 /* Resolve indirection. */
440 code = (void*)(*(sljit_uw*)code);
441 SLJIT_FREE_EXEC(code);
442 }
443 #else
444 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
445 {
446 SLJIT_FREE_EXEC(code);
447 }
448 #endif
449
450 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label)
451 {
452 if (SLJIT_LIKELY(!!jump) && SLJIT_LIKELY(!!label)) {
453 jump->flags &= ~JUMP_ADDR;
454 jump->flags |= JUMP_LABEL;
455 jump->u.label = label;
456 }
457 }
458
459 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target)
460 {
461 if (SLJIT_LIKELY(!!jump)) {
462 jump->flags &= ~JUMP_LABEL;
463 jump->flags |= JUMP_ADDR;
464 jump->u.target = target;
465 }
466 }
467
468 /* --------------------------------------------------------------------- */
469 /* Private functions */
470 /* --------------------------------------------------------------------- */
471
472 static void* ensure_buf(struct sljit_compiler *compiler, sljit_uw size)
473 {
474 sljit_ub *ret;
475 struct sljit_memory_fragment *new_frag;
476
477 SLJIT_ASSERT(size <= 256);
478 if (compiler->buf->used_size + size <= (BUF_SIZE - (sljit_uw)SLJIT_OFFSETOF(struct sljit_memory_fragment, memory))) {
479 ret = compiler->buf->memory + compiler->buf->used_size;
480 compiler->buf->used_size += size;
481 return ret;
482 }
483 new_frag = (struct sljit_memory_fragment*)SLJIT_MALLOC(BUF_SIZE);
484 PTR_FAIL_IF_NULL(new_frag);
485 new_frag->next = compiler->buf;
486 compiler->buf = new_frag;
487 new_frag->used_size = size;
488 return new_frag->memory;
489 }
490
491 static void* ensure_abuf(struct sljit_compiler *compiler, sljit_uw size)
492 {
493 sljit_ub *ret;
494 struct sljit_memory_fragment *new_frag;
495
496 SLJIT_ASSERT(size <= 256);
497 if (compiler->abuf->used_size + size <= (ABUF_SIZE - (sljit_uw)SLJIT_OFFSETOF(struct sljit_memory_fragment, memory))) {
498 ret = compiler->abuf->memory + compiler->abuf->used_size;
499 compiler->abuf->used_size += size;
500 return ret;
501 }
502 new_frag = (struct sljit_memory_fragment*)SLJIT_MALLOC(ABUF_SIZE);
503 PTR_FAIL_IF_NULL(new_frag);
504 new_frag->next = compiler->abuf;
505 compiler->abuf = new_frag;
506 new_frag->used_size = size;
507 return new_frag->memory;
508 }
509
510 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size)
511 {
512 CHECK_ERROR_PTR();
513
514 #if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
515 if (size <= 0 || size > 128)
516 return NULL;
517 size = (size + 7) & ~7;
518 #else
519 if (size <= 0 || size > 64)
520 return NULL;
521 size = (size + 3) & ~3;
522 #endif
523 return ensure_abuf(compiler, size);
524 }
525
526 static SLJIT_INLINE void reverse_buf(struct sljit_compiler *compiler)
527 {
528 struct sljit_memory_fragment *buf = compiler->buf;
529 struct sljit_memory_fragment *prev = NULL;
530 struct sljit_memory_fragment *tmp;
531
532 do {
533 tmp = buf->next;
534 buf->next = prev;
535 prev = buf;
536 buf = tmp;
537 } while (buf != NULL);
538
539 compiler->buf = prev;
540 }
541
542 static SLJIT_INLINE void set_label(struct sljit_label *label, struct sljit_compiler *compiler)
543 {
544 label->next = NULL;
545 label->size = compiler->size;
546 if (compiler->last_label)
547 compiler->last_label->next = label;
548 else
549 compiler->labels = label;
550 compiler->last_label = label;
551 }
552
553 static SLJIT_INLINE void set_jump(struct sljit_jump *jump, struct sljit_compiler *compiler, sljit_si flags)
554 {
555 jump->next = NULL;
556 jump->flags = flags;
557 if (compiler->last_jump)
558 compiler->last_jump->next = jump;
559 else
560 compiler->jumps = jump;
561 compiler->last_jump = jump;
562 }
563
564 static SLJIT_INLINE void set_const(struct sljit_const *const_, struct sljit_compiler *compiler)
565 {
566 const_->next = NULL;
567 const_->addr = compiler->size;
568 if (compiler->last_const)
569 compiler->last_const->next = const_;
570 else
571 compiler->consts = const_;
572 compiler->last_const = const_;
573 }
574
575 #define ADDRESSING_DEPENDS_ON(exp, reg) \
576 (((exp) & SLJIT_MEM) && (((exp) & REG_MASK) == reg || OFFS_REG(exp) == reg))
577
578 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
579 #define FUNCTION_CHECK_OP() \
580 SLJIT_ASSERT(!GET_FLAGS(op) || !(op & SLJIT_KEEP_FLAGS)); \
581 switch (GET_OPCODE(op)) { \
582 case SLJIT_NOT: \
583 case SLJIT_CLZ: \
584 case SLJIT_AND: \
585 case SLJIT_OR: \
586 case SLJIT_XOR: \
587 case SLJIT_SHL: \
588 case SLJIT_LSHR: \
589 case SLJIT_ASHR: \
590 SLJIT_ASSERT(!(op & (SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C))); \
591 break; \
592 case SLJIT_NEG: \
593 SLJIT_ASSERT(!(op & (SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_C))); \
594 break; \
595 case SLJIT_MUL: \
596 SLJIT_ASSERT(!(op & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_C))); \
597 break; \
598 case SLJIT_CMPD: \
599 SLJIT_ASSERT(!(op & (SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))); \
600 SLJIT_ASSERT((op & (SLJIT_SET_E | SLJIT_SET_S))); \
601 break; \
602 case SLJIT_ADD: \
603 SLJIT_ASSERT(!(op & (SLJIT_SET_U | SLJIT_SET_S))); \
604 break; \
605 case SLJIT_SUB: \
606 break; \
607 case SLJIT_ADDC: \
608 case SLJIT_SUBC: \
609 SLJIT_ASSERT(!(op & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O))); \
610 break; \
611 case SLJIT_BREAKPOINT: \
612 case SLJIT_NOP: \
613 case SLJIT_UMUL: \
614 case SLJIT_SMUL: \
615 case SLJIT_MOV: \
616 case SLJIT_MOV_UI: \
617 case SLJIT_MOV_P: \
618 case SLJIT_MOVU: \
619 case SLJIT_MOVU_UI: \
620 case SLJIT_MOVU_P: \
621 /* Nothing allowed */ \
622 SLJIT_ASSERT(!(op & (SLJIT_INT_OP | SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))); \
623 break; \
624 default: \
625 /* Only SLJIT_INT_OP or SLJIT_SINGLE_OP is allowed. */ \
626 SLJIT_ASSERT(!(op & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))); \
627 break; \
628 }
629
630 #define FUNCTION_CHECK_IS_REG(r) \
631 ((r) == SLJIT_UNUSED || \
632 ((r) >= SLJIT_SCRATCH_REG1 && (r) <= SLJIT_SCRATCH_REG1 - 1 + compiler->scratches) || \
633 ((r) >= SLJIT_SAVED_REG1 && (r) <= SLJIT_SAVED_REG1 - 1 + compiler->saveds))
634
635 #define FUNCTION_CHECK_SRC(p, i) \
636 SLJIT_ASSERT(compiler->scratches != -1 && compiler->saveds != -1); \
637 if (FUNCTION_CHECK_IS_REG(p)) \
638 SLJIT_ASSERT((i) == 0 && (p) != SLJIT_UNUSED); \
639 else if ((p) == SLJIT_IMM) \
640 ; \
641 else if ((p) == (SLJIT_MEM1(SLJIT_LOCALS_REG))) \
642 SLJIT_ASSERT((i) >= 0 && (i) < compiler->logical_local_size); \
643 else if ((p) & SLJIT_MEM) { \
644 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & REG_MASK)); \
645 if ((p) & OFFS_REG_MASK) { \
646 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(OFFS_REG(p))); \
647 SLJIT_ASSERT(!((i) & ~0x3)); \
648 } \
649 SLJIT_ASSERT(!((p) & ~(SLJIT_MEM | SLJIT_IMM | REG_MASK | OFFS_REG_MASK))); \
650 } \
651 else \
652 SLJIT_ASSERT_STOP();
653
654 #define FUNCTION_CHECK_DST(p, i) \
655 SLJIT_ASSERT(compiler->scratches != -1 && compiler->saveds != -1); \
656 if (FUNCTION_CHECK_IS_REG(p)) \
657 SLJIT_ASSERT((i) == 0); \
658 else if ((p) == (SLJIT_MEM1(SLJIT_LOCALS_REG))) \
659 SLJIT_ASSERT((i) >= 0 && (i) < compiler->logical_local_size); \
660 else if ((p) & SLJIT_MEM) { \
661 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & REG_MASK)); \
662 if ((p) & OFFS_REG_MASK) { \
663 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(OFFS_REG(p))); \
664 SLJIT_ASSERT(!((i) & ~0x3)); \
665 } \
666 SLJIT_ASSERT(!((p) & ~(SLJIT_MEM | SLJIT_IMM | REG_MASK | OFFS_REG_MASK))); \
667 } \
668 else \
669 SLJIT_ASSERT_STOP();
670
671 #define FUNCTION_FCHECK(p, i) \
672 if ((p) >= SLJIT_FLOAT_REG1 && (p) <= SLJIT_FLOAT_REG6) \
673 SLJIT_ASSERT(i == 0); \
674 else if ((p) & SLJIT_MEM) { \
675 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & REG_MASK)); \
676 if ((p) & OFFS_REG_MASK) { \
677 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(OFFS_REG(p))); \
678 SLJIT_ASSERT(((p) & OFFS_REG_MASK) != TO_OFFS_REG(SLJIT_LOCALS_REG) && !(i & ~0x3)); \
679 } else \
680 SLJIT_ASSERT(OFFS_REG(p) == 0); \
681 SLJIT_ASSERT(!((p) & ~(SLJIT_MEM | SLJIT_IMM | REG_MASK | OFFS_REG_MASK))); \
682 } \
683 else \
684 SLJIT_ASSERT_STOP();
685
686 #define FUNCTION_CHECK_OP1() \
687 if (GET_OPCODE(op) >= SLJIT_MOVU && GET_OPCODE(op) <= SLJIT_MOVU_P) { \
688 SLJIT_ASSERT(!(src & SLJIT_MEM) || (src & REG_MASK) != SLJIT_LOCALS_REG); \
689 SLJIT_ASSERT(!(dst & SLJIT_MEM) || (dst & REG_MASK) != SLJIT_LOCALS_REG); \
690 if ((src & SLJIT_MEM) && (src & REG_MASK)) \
691 SLJIT_ASSERT((dst & REG_MASK) != (src & REG_MASK) && OFFS_REG(dst) != (src & REG_MASK)); \
692 }
693
694 #endif
695
696 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
697
698 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose)
699 {
700 compiler->verbose = verbose;
701 }
702
703 static char* reg_names[] = {
704 (char*)"unused", (char*)"s1", (char*)"s2", (char*)"s3",
705 (char*)"se1", (char*)"se2", (char*)"p1", (char*)"p2",
706 (char*)"p3", (char*)"pe1", (char*)"pe2", (char*)"lc"
707 };
708
709 static char* freg_names[] = {
710 (char*)"unused", (char*)"f1", (char*)"f2", (char*)"f3",
711 (char*)"f4", (char*)"f5", (char*)"f6"
712 };
713
714 #if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
715 #ifdef _WIN64
716 # define SLJIT_PRINT_D "I64"
717 #else
718 # define SLJIT_PRINT_D "l"
719 #endif
720 #else
721 # define SLJIT_PRINT_D ""
722 #endif
723
724 #define sljit_verbose_param(p, i) \
725 if ((p) & SLJIT_IMM) \
726 fprintf(compiler->verbose, "#%" SLJIT_PRINT_D "d", (i)); \
727 else if ((p) & SLJIT_MEM) { \
728 if ((p) & REG_MASK) { \
729 if (i) { \
730 if ((p) & OFFS_REG_MASK) \
731 fprintf(compiler->verbose, "[%s + %s * %d]", reg_names[(p) & REG_MASK], reg_names[OFFS_REG(p)], 1 << (i)); \
732 else \
733 fprintf(compiler->verbose, "[%s + #%" SLJIT_PRINT_D "d]", reg_names[(p) & REG_MASK], (i)); \
734 } \
735 else { \
736 if ((p) & OFFS_REG_MASK) \
737 fprintf(compiler->verbose, "[%s + %s]", reg_names[(p) & REG_MASK], reg_names[OFFS_REG(p)]); \
738 else \
739 fprintf(compiler->verbose, "[%s]", reg_names[(p) & REG_MASK]); \
740 } \
741 } \
742 else \
743 fprintf(compiler->verbose, "[#%" SLJIT_PRINT_D "d]", (i)); \
744 } else \
745 fprintf(compiler->verbose, "%s", reg_names[p]);
746 #define sljit_verbose_fparam(p, i) \
747 if ((p) & SLJIT_MEM) { \
748 if ((p) & REG_MASK) { \
749 if (i) { \
750 if ((p) & OFFS_REG_MASK) \
751 fprintf(compiler->verbose, "[%s + %s * %d]", reg_names[(p) & REG_MASK], reg_names[OFFS_REG(p)], 1 << (i)); \
752 else \
753 fprintf(compiler->verbose, "[%s + #%" SLJIT_PRINT_D "d]", reg_names[(p) & REG_MASK], (i)); \
754 } \
755 else { \
756 if ((p) & OFFS_REG_MASK) \
757 fprintf(compiler->verbose, "[%s + %s]", reg_names[(p) & REG_MASK], reg_names[OFFS_REG(p)]); \
758 else \
759 fprintf(compiler->verbose, "[%s]", reg_names[(p) & REG_MASK]); \
760 } \
761 } \
762 else \
763 fprintf(compiler->verbose, "[#%" SLJIT_PRINT_D "d]", (i)); \
764 } else \
765 fprintf(compiler->verbose, "%s", freg_names[p]);
766
767 static SLJIT_CONST char* op_names[] = {
768 /* op0 */
769 (char*)"breakpoint", (char*)"nop",
770 (char*)"umul", (char*)"smul", (char*)"udiv", (char*)"sdiv",
771 /* op1 */
772 (char*)"mov", (char*)"mov.ub", (char*)"mov.sb", (char*)"mov.uh",
773 (char*)"mov.sh", (char*)"mov.ui", (char*)"mov.si", (char*)"mov.p",
774 (char*)"movu", (char*)"movu.ub", (char*)"movu.sb", (char*)"movu.uh",
775 (char*)"movu.sh", (char*)"movu.ui", (char*)"movu.si", (char*)"movu.p",
776 (char*)"not", (char*)"neg", (char*)"clz",
777 /* op2 */
778 (char*)"add", (char*)"addc", (char*)"sub", (char*)"subc",
779 (char*)"mul", (char*)"and", (char*)"or", (char*)"xor",
780 (char*)"shl", (char*)"lshr", (char*)"ashr",
781 /* fop1 */
782 (char*)"cmp", (char*)"mov", (char*)"neg", (char*)"abs",
783 /* fop2 */
784 (char*)"add", (char*)"sub", (char*)"mul", (char*)"div"
785 };
786
787 static char* jump_names[] = {
788 (char*)"equal", (char*)"not_equal",
789 (char*)"less", (char*)"greater_equal",
790 (char*)"greater", (char*)"less_equal",
791 (char*)"sig_less", (char*)"sig_greater_equal",
792 (char*)"sig_greater", (char*)"sig_less_equal",
793 (char*)"overflow", (char*)"not_overflow",
794 (char*)"mul_overflow", (char*)"mul_not_overflow",
795 (char*)"float_equal", (char*)"float_not_equal",
796 (char*)"float_less", (char*)"float_greater_equal",
797 (char*)"float_greater", (char*)"float_less_equal",
798 (char*)"float_unordered", (char*)"float_ordered",
799 (char*)"jump", (char*)"fast_call",
800 (char*)"call0", (char*)"call1", (char*)"call2", (char*)"call3"
801 };
802
803 #endif
804
805 /* --------------------------------------------------------------------- */
806 /* Arch dependent */
807 /* --------------------------------------------------------------------- */
808
809 static SLJIT_INLINE void check_sljit_generate_code(struct sljit_compiler *compiler)
810 {
811 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
812 struct sljit_jump *jump;
813 #endif
814 /* If debug and verbose are disabled, all arguments are unused. */
815 SLJIT_UNUSED_ARG(compiler);
816
817 SLJIT_ASSERT(compiler->size > 0);
818 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
819 jump = compiler->jumps;
820 while (jump) {
821 /* All jumps have target. */
822 SLJIT_ASSERT(jump->flags & (JUMP_LABEL | JUMP_ADDR));
823 jump = jump->next;
824 }
825 #endif
826 }
827
828 static SLJIT_INLINE void check_sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
829 {
830 /* If debug and verbose are disabled, all arguments are unused. */
831 SLJIT_UNUSED_ARG(compiler);
832 SLJIT_UNUSED_ARG(args);
833 SLJIT_UNUSED_ARG(scratches);
834 SLJIT_UNUSED_ARG(saveds);
835 SLJIT_UNUSED_ARG(local_size);
836
837 SLJIT_ASSERT(args >= 0 && args <= 3);
838 SLJIT_ASSERT(scratches >= 0 && scratches <= SLJIT_NO_TMP_REGISTERS);
839 SLJIT_ASSERT(saveds >= 0 && saveds <= SLJIT_NO_GEN_REGISTERS);
840 SLJIT_ASSERT(args <= saveds);
841 SLJIT_ASSERT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
842 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
843 if (SLJIT_UNLIKELY(!!compiler->verbose))
844 fprintf(compiler->verbose, " enter args=%d scratches=%d saveds=%d local_size=%d\n", args, scratches, saveds, local_size);
845 #endif
846 }
847
848 static SLJIT_INLINE void check_sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
849 {
850 /* If debug and verbose are disabled, all arguments are unused. */
851 SLJIT_UNUSED_ARG(compiler);
852 SLJIT_UNUSED_ARG(args);
853 SLJIT_UNUSED_ARG(scratches);
854 SLJIT_UNUSED_ARG(saveds);
855 SLJIT_UNUSED_ARG(local_size);
856
857 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
858 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
859 compiler->skip_checks = 0;
860 return;
861 }
862 #endif
863
864 SLJIT_ASSERT(args >= 0 && args <= 3);
865 SLJIT_ASSERT(scratches >= 0 && scratches <= SLJIT_NO_TMP_REGISTERS);
866 SLJIT_ASSERT(saveds >= 0 && saveds <= SLJIT_NO_GEN_REGISTERS);
867 SLJIT_ASSERT(args <= saveds);
868 SLJIT_ASSERT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
869 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
870 if (SLJIT_UNLIKELY(!!compiler->verbose))
871 fprintf(compiler->verbose, " set_context args=%d scratches=%d saveds=%d local_size=%d\n", args, scratches, saveds, local_size);
872 #endif
873 }
874
875 static SLJIT_INLINE void check_sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
876 {
877 /* If debug and verbose are disabled, all arguments are unused. */
878 SLJIT_UNUSED_ARG(compiler);
879 SLJIT_UNUSED_ARG(op);
880 SLJIT_UNUSED_ARG(src);
881 SLJIT_UNUSED_ARG(srcw);
882
883 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
884 if (op != SLJIT_UNUSED) {
885 SLJIT_ASSERT(op >= SLJIT_MOV && op <= SLJIT_MOV_P);
886 FUNCTION_CHECK_SRC(src, srcw);
887 }
888 else
889 SLJIT_ASSERT(src == 0 && srcw == 0);
890 #endif
891 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
892 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
893 if (op == SLJIT_UNUSED)
894 fprintf(compiler->verbose, " return\n");
895 else {
896 fprintf(compiler->verbose, " return %s ", op_names[op]);
897 sljit_verbose_param(src, srcw);
898 fprintf(compiler->verbose, "\n");
899 }
900 }
901 #endif
902 }
903
904 static SLJIT_INLINE void check_sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
905 {
906 /* If debug and verbose are disabled, all arguments are unused. */
907 SLJIT_UNUSED_ARG(compiler);
908 SLJIT_UNUSED_ARG(dst);
909 SLJIT_UNUSED_ARG(dstw);
910
911 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
912 FUNCTION_CHECK_DST(dst, dstw);
913 #endif
914 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
915 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
916 fprintf(compiler->verbose, " fast_enter ");
917 sljit_verbose_param(dst, dstw);
918 fprintf(compiler->verbose, "\n");
919 }
920 #endif
921 }
922
923 static SLJIT_INLINE void check_sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
924 {
925 /* If debug and verbose are disabled, all arguments are unused. */
926 SLJIT_UNUSED_ARG(compiler);
927 SLJIT_UNUSED_ARG(src);
928 SLJIT_UNUSED_ARG(srcw);
929
930 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
931 FUNCTION_CHECK_SRC(src, srcw);
932 #endif
933 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
934 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
935 fprintf(compiler->verbose, " fast_return ");
936 sljit_verbose_param(src, srcw);
937 fprintf(compiler->verbose, "\n");
938 }
939 #endif
940 }
941
942 static SLJIT_INLINE void check_sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
943 {
944 /* If debug and verbose are disabled, all arguments are unused. */
945 SLJIT_UNUSED_ARG(compiler);
946 SLJIT_UNUSED_ARG(op);
947
948 SLJIT_ASSERT((op >= SLJIT_BREAKPOINT && op <= SLJIT_SMUL)
949 || ((op & ~SLJIT_INT_OP) >= SLJIT_UDIV && (op & ~SLJIT_INT_OP) <= SLJIT_SDIV));
950 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
951 if (SLJIT_UNLIKELY(!!compiler->verbose))
952 fprintf(compiler->verbose, " %s%s\n", !(op & SLJIT_INT_OP) ? "" : "i", op_names[GET_OPCODE(op)]);
953 #endif
954 }
955
956 static SLJIT_INLINE void check_sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
957 sljit_si dst, sljit_sw dstw,
958 sljit_si src, sljit_sw srcw)
959 {
960 /* If debug and verbose are disabled, all arguments are unused. */
961 SLJIT_UNUSED_ARG(compiler);
962 SLJIT_UNUSED_ARG(op);
963 SLJIT_UNUSED_ARG(dst);
964 SLJIT_UNUSED_ARG(dstw);
965 SLJIT_UNUSED_ARG(src);
966 SLJIT_UNUSED_ARG(srcw);
967
968 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
969 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
970 compiler->skip_checks = 0;
971 return;
972 }
973 #endif
974
975 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_CLZ);
976 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
977 FUNCTION_CHECK_OP();
978 FUNCTION_CHECK_SRC(src, srcw);
979 FUNCTION_CHECK_DST(dst, dstw);
980 FUNCTION_CHECK_OP1();
981 #endif
982 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
983 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
984 fprintf(compiler->verbose, " %s%s%s%s%s%s%s%s ", !(op & SLJIT_INT_OP) ? "" : "i", op_names[GET_OPCODE(op)],
985 !(op & SLJIT_SET_E) ? "" : ".e", !(op & SLJIT_SET_U) ? "" : ".u", !(op & SLJIT_SET_S) ? "" : ".s",
986 !(op & SLJIT_SET_O) ? "" : ".o", !(op & SLJIT_SET_C) ? "" : ".c", !(op & SLJIT_KEEP_FLAGS) ? "" : ".k");
987 sljit_verbose_param(dst, dstw);
988 fprintf(compiler->verbose, ", ");
989 sljit_verbose_param(src, srcw);
990 fprintf(compiler->verbose, "\n");
991 }
992 #endif
993 }
994
995 static SLJIT_INLINE void check_sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
996 sljit_si dst, sljit_sw dstw,
997 sljit_si src1, sljit_sw src1w,
998 sljit_si src2, sljit_sw src2w)
999 {
1000 /* If debug and verbose are disabled, all arguments are unused. */
1001 SLJIT_UNUSED_ARG(compiler);
1002 SLJIT_UNUSED_ARG(op);
1003 SLJIT_UNUSED_ARG(dst);
1004 SLJIT_UNUSED_ARG(dstw);
1005 SLJIT_UNUSED_ARG(src1);
1006 SLJIT_UNUSED_ARG(src1w);
1007 SLJIT_UNUSED_ARG(src2);
1008 SLJIT_UNUSED_ARG(src2w);
1009
1010 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1011 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
1012 compiler->skip_checks = 0;
1013 return;
1014 }
1015 #endif
1016
1017 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_ADD && GET_OPCODE(op) <= SLJIT_ASHR);
1018 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1019 FUNCTION_CHECK_OP();
1020 FUNCTION_CHECK_SRC(src1, src1w);
1021 FUNCTION_CHECK_SRC(src2, src2w);
1022 FUNCTION_CHECK_DST(dst, dstw);
1023 #endif
1024 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1025 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1026 fprintf(compiler->verbose, " %s%s%s%s%s%s%s%s ", !(op & SLJIT_INT_OP) ? "" : "i", op_names[GET_OPCODE(op)],
1027 !(op & SLJIT_SET_E) ? "" : ".e", !(op & SLJIT_SET_U) ? "" : ".u", !(op & SLJIT_SET_S) ? "" : ".s",
1028 !(op & SLJIT_SET_O) ? "" : ".o", !(op & SLJIT_SET_C) ? "" : ".c", !(op & SLJIT_KEEP_FLAGS) ? "" : ".k");
1029 sljit_verbose_param(dst, dstw);
1030 fprintf(compiler->verbose, ", ");
1031 sljit_verbose_param(src1, src1w);
1032 fprintf(compiler->verbose, ", ");
1033 sljit_verbose_param(src2, src2w);
1034 fprintf(compiler->verbose, "\n");
1035 }
1036 #endif
1037 }
1038
1039 static SLJIT_INLINE void check_sljit_get_register_index(sljit_si reg)
1040 {
1041 SLJIT_UNUSED_ARG(reg);
1042 SLJIT_ASSERT(reg > 0 && reg <= SLJIT_NO_REGISTERS);
1043 }
1044
1045 static SLJIT_INLINE void check_sljit_get_float_register_index(sljit_si reg)
1046 {
1047 SLJIT_UNUSED_ARG(reg);
1048 SLJIT_ASSERT(reg > 0 && reg <= SLJIT_NO_FLOAT_REGISTERS);
1049 }
1050
1051 static SLJIT_INLINE void check_sljit_emit_op_custom(struct sljit_compiler *compiler,
1052 void *instruction, sljit_si size)
1053 {
1054 SLJIT_UNUSED_ARG(compiler);
1055 SLJIT_UNUSED_ARG(instruction);
1056 SLJIT_UNUSED_ARG(size);
1057 SLJIT_ASSERT(instruction);
1058 }
1059
1060 static SLJIT_INLINE void check_sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1061 sljit_si dst, sljit_sw dstw,
1062 sljit_si src, sljit_sw srcw)
1063 {
1064 /* If debug and verbose are disabled, all arguments are unused. */
1065 SLJIT_UNUSED_ARG(compiler);
1066 SLJIT_UNUSED_ARG(op);
1067 SLJIT_UNUSED_ARG(dst);
1068 SLJIT_UNUSED_ARG(dstw);
1069 SLJIT_UNUSED_ARG(src);
1070 SLJIT_UNUSED_ARG(srcw);
1071
1072 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1073 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
1074 compiler->skip_checks = 0;
1075 return;
1076 }
1077 #endif
1078
1079 SLJIT_ASSERT(sljit_is_fpu_available());
1080 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_CMPD && GET_OPCODE(op) <= SLJIT_ABSD);
1081 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1082 FUNCTION_CHECK_OP();
1083 FUNCTION_FCHECK(src, srcw);
1084 FUNCTION_FCHECK(dst, dstw);
1085 #endif
1086 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1087 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1088 fprintf(compiler->verbose, " %s%s%s%s ", op_names[GET_OPCODE(op)], (op & SLJIT_SINGLE_OP) ? "s" : "d",
1089 !(op & SLJIT_SET_E) ? "" : ".e", !(op & SLJIT_SET_S) ? "" : ".s");
1090 sljit_verbose_fparam(dst, dstw);
1091 fprintf(compiler->verbose, ", ");
1092 sljit_verbose_fparam(src, srcw);
1093 fprintf(compiler->verbose, "\n");
1094 }
1095 #endif
1096 }
1097
1098 static SLJIT_INLINE void check_sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1099 sljit_si dst, sljit_sw dstw,
1100 sljit_si src1, sljit_sw src1w,
1101 sljit_si src2, sljit_sw src2w)
1102 {
1103 /* If debug and verbose are disabled, all arguments are unused. */
1104 SLJIT_UNUSED_ARG(compiler);
1105 SLJIT_UNUSED_ARG(op);
1106 SLJIT_UNUSED_ARG(dst);
1107 SLJIT_UNUSED_ARG(dstw);
1108 SLJIT_UNUSED_ARG(src1);
1109 SLJIT_UNUSED_ARG(src1w);
1110 SLJIT_UNUSED_ARG(src2);
1111 SLJIT_UNUSED_ARG(src2w);
1112
1113 SLJIT_ASSERT(sljit_is_fpu_available());
1114 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_ADDD && GET_OPCODE(op) <= SLJIT_DIVD);
1115 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1116 FUNCTION_CHECK_OP();
1117 FUNCTION_FCHECK(src1, src1w);
1118 FUNCTION_FCHECK(src2, src2w);
1119 FUNCTION_FCHECK(dst, dstw);
1120 #endif
1121 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1122 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1123 fprintf(compiler->verbose, " %s%s ", op_names[GET_OPCODE(op)], (op & SLJIT_SINGLE_OP) ? "s" : "d");
1124 sljit_verbose_fparam(dst, dstw);
1125 fprintf(compiler->verbose, ", ");
1126 sljit_verbose_fparam(src1, src1w);
1127 fprintf(compiler->verbose, ", ");
1128 sljit_verbose_fparam(src2, src2w);
1129 fprintf(compiler->verbose, "\n");
1130 }
1131 #endif
1132 }
1133
1134 static SLJIT_INLINE void check_sljit_emit_label(struct sljit_compiler *compiler)
1135 {
1136 /* If debug and verbose are disabled, all arguments are unused. */
1137 SLJIT_UNUSED_ARG(compiler);
1138
1139 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1140 if (SLJIT_UNLIKELY(!!compiler->verbose))
1141 fprintf(compiler->verbose, "label:\n");
1142 #endif
1143 }
1144
1145 static SLJIT_INLINE void check_sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1146 {
1147 /* If debug and verbose are disabled, all arguments are unused. */
1148 SLJIT_UNUSED_ARG(compiler);
1149 SLJIT_UNUSED_ARG(type);
1150
1151 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1152 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
1153 compiler->skip_checks = 0;
1154 return;
1155 }
1156 #endif
1157
1158 SLJIT_ASSERT(!(type & ~(0xff | SLJIT_REWRITABLE_JUMP)));
1159 SLJIT_ASSERT((type & 0xff) >= SLJIT_C_EQUAL && (type & 0xff) <= SLJIT_CALL3);
1160 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1161 if (SLJIT_UNLIKELY(!!compiler->verbose))
1162 fprintf(compiler->verbose, " jump%s.%s\n", !(type & SLJIT_REWRITABLE_JUMP) ? "" : ".r", jump_names[type & 0xff]);
1163 #endif
1164 }
1165
1166 static SLJIT_INLINE void check_sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
1167 sljit_si src1, sljit_sw src1w,
1168 sljit_si src2, sljit_sw src2w)
1169 {
1170 SLJIT_UNUSED_ARG(compiler);
1171 SLJIT_UNUSED_ARG(type);
1172 SLJIT_UNUSED_ARG(src1);
1173 SLJIT_UNUSED_ARG(src1w);
1174 SLJIT_UNUSED_ARG(src2);
1175 SLJIT_UNUSED_ARG(src2w);
1176
1177 SLJIT_ASSERT(!(type & ~(0xff | SLJIT_REWRITABLE_JUMP | SLJIT_INT_OP)));
1178 SLJIT_ASSERT((type & 0xff) >= SLJIT_C_EQUAL && (type & 0xff) <= SLJIT_C_SIG_LESS_EQUAL);
1179 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1180 FUNCTION_CHECK_SRC(src1, src1w);
1181 FUNCTION_CHECK_SRC(src2, src2w);
1182 #endif
1183 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1184 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1185 fprintf(compiler->verbose, " %scmp%s.%s ", !(type & SLJIT_INT_OP) ? "" : "i", !(type & SLJIT_REWRITABLE_JUMP) ? "" : ".r", jump_names[type & 0xff]);
1186 sljit_verbose_param(src1, src1w);
1187 fprintf(compiler->verbose, ", ");
1188 sljit_verbose_param(src2, src2w);
1189 fprintf(compiler->verbose, "\n");
1190 }
1191 #endif
1192 }
1193
1194 static SLJIT_INLINE void check_sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1195 sljit_si src1, sljit_sw src1w,
1196 sljit_si src2, sljit_sw src2w)
1197 {
1198 SLJIT_UNUSED_ARG(compiler);
1199 SLJIT_UNUSED_ARG(type);
1200 SLJIT_UNUSED_ARG(src1);
1201 SLJIT_UNUSED_ARG(src1w);
1202 SLJIT_UNUSED_ARG(src2);
1203 SLJIT_UNUSED_ARG(src2w);
1204
1205 SLJIT_ASSERT(sljit_is_fpu_available());
1206 SLJIT_ASSERT(!(type & ~(0xff | SLJIT_REWRITABLE_JUMP | SLJIT_SINGLE_OP)));
1207 SLJIT_ASSERT((type & 0xff) >= SLJIT_C_FLOAT_EQUAL && (type & 0xff) <= SLJIT_C_FLOAT_ORDERED);
1208 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1209 FUNCTION_FCHECK(src1, src1w);
1210 FUNCTION_FCHECK(src2, src2w);
1211 #endif
1212 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1213 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1214 fprintf(compiler->verbose, " %scmp%s.%s ", (type & SLJIT_SINGLE_OP) ? "s" : "d",
1215 !(type & SLJIT_REWRITABLE_JUMP) ? "" : ".r", jump_names[type & 0xff]);
1216 sljit_verbose_fparam(src1, src1w);
1217 fprintf(compiler->verbose, ", ");
1218 sljit_verbose_fparam(src2, src2w);
1219 fprintf(compiler->verbose, "\n");
1220 }
1221 #endif
1222 }
1223
1224 static SLJIT_INLINE void check_sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1225 {
1226 /* If debug and verbose are disabled, all arguments are unused. */
1227 SLJIT_UNUSED_ARG(compiler);
1228 SLJIT_UNUSED_ARG(type);
1229 SLJIT_UNUSED_ARG(src);
1230 SLJIT_UNUSED_ARG(srcw);
1231
1232 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1233 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
1234 compiler->skip_checks = 0;
1235 return;
1236 }
1237 #endif
1238
1239 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
1240 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1241 FUNCTION_CHECK_SRC(src, srcw);
1242 #endif
1243 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1244 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1245 fprintf(compiler->verbose, " ijump.%s ", jump_names[type]);
1246 sljit_verbose_param(src, srcw);
1247 fprintf(compiler->verbose, "\n");
1248 }
1249 #endif
1250 }
1251
1252 static SLJIT_INLINE void check_sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1253 sljit_si dst, sljit_sw dstw,
1254 sljit_si src, sljit_sw srcw,
1255 sljit_si type)
1256 {
1257 /* If debug and verbose are disabled, all arguments are unused. */
1258 SLJIT_UNUSED_ARG(compiler);
1259 SLJIT_UNUSED_ARG(op);
1260 SLJIT_UNUSED_ARG(dst);
1261 SLJIT_UNUSED_ARG(dstw);
1262 SLJIT_UNUSED_ARG(src);
1263 SLJIT_UNUSED_ARG(srcw);
1264 SLJIT_UNUSED_ARG(type);
1265
1266 SLJIT_ASSERT(type >= SLJIT_C_EQUAL && type < SLJIT_JUMP);
1267 SLJIT_ASSERT(op == SLJIT_MOV || GET_OPCODE(op) == SLJIT_MOV_UI || GET_OPCODE(op) == SLJIT_MOV_SI
1268 || (GET_OPCODE(op) >= SLJIT_AND && GET_OPCODE(op) <= SLJIT_XOR));
1269 SLJIT_ASSERT((op & (SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O | SLJIT_SET_C)) == 0);
1270 SLJIT_ASSERT((op & (SLJIT_SET_E | SLJIT_KEEP_FLAGS)) != (SLJIT_SET_E | SLJIT_KEEP_FLAGS));
1271 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1272 if (GET_OPCODE(op) < SLJIT_ADD) {
1273 SLJIT_ASSERT(src == SLJIT_UNUSED && srcw == 0);
1274 } else {
1275 SLJIT_ASSERT(src == dst && srcw == dstw);
1276 }
1277 FUNCTION_CHECK_DST(dst, dstw);
1278 #endif
1279 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1280 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1281 fprintf(compiler->verbose, " %sflags.%s%s%s ", !(op & SLJIT_INT_OP) ? "" : "i",
1282 op_names[GET_OPCODE(op)], !(op & SLJIT_SET_E) ? "" : ".e", !(op & SLJIT_KEEP_FLAGS) ? "" : ".k");
1283 sljit_verbose_param(dst, dstw);
1284 if (src != SLJIT_UNUSED) {
1285 fprintf(compiler->verbose, ", ");
1286 sljit_verbose_param(src, srcw);
1287 }
1288 fprintf(compiler->verbose, ", %s\n", jump_names[type]);
1289 }
1290 #endif
1291 }
1292
1293 static SLJIT_INLINE void check_sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset)
1294 {
1295 SLJIT_UNUSED_ARG(compiler);
1296 SLJIT_UNUSED_ARG(dst);
1297 SLJIT_UNUSED_ARG(dstw);
1298 SLJIT_UNUSED_ARG(offset);
1299
1300 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1301 FUNCTION_CHECK_DST(dst, dstw);
1302 #endif
1303 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1304 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1305 fprintf(compiler->verbose, " local_base ");
1306 sljit_verbose_param(dst, dstw);
1307 fprintf(compiler->verbose, ", #%" SLJIT_PRINT_D "d\n", offset);
1308 }
1309 #endif
1310 }
1311
1312 static SLJIT_INLINE void check_sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1313 {
1314 /* If debug and verbose are disabled, all arguments are unused. */
1315 SLJIT_UNUSED_ARG(compiler);
1316 SLJIT_UNUSED_ARG(dst);
1317 SLJIT_UNUSED_ARG(dstw);
1318 SLJIT_UNUSED_ARG(init_value);
1319
1320 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1321 FUNCTION_CHECK_DST(dst, dstw);
1322 #endif
1323 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1324 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1325 fprintf(compiler->verbose, " const ");
1326 sljit_verbose_param(dst, dstw);
1327 fprintf(compiler->verbose, ", #%" SLJIT_PRINT_D "d\n", init_value);
1328 }
1329 #endif
1330 }
1331
1332 static SLJIT_INLINE sljit_si emit_mov_before_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
1333 {
1334 /* Return if don't need to do anything. */
1335 if (op == SLJIT_UNUSED)
1336 return SLJIT_SUCCESS;
1337
1338 #if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
1339 /* At the moment the pointer size is always equal to sljit_sw. May be changed in the future. */
1340 if (src == SLJIT_RETURN_REG && (op == SLJIT_MOV || op == SLJIT_MOV_P))
1341 return SLJIT_SUCCESS;
1342 #else
1343 if (src == SLJIT_RETURN_REG && (op == SLJIT_MOV || op == SLJIT_MOV_UI || op == SLJIT_MOV_SI || op == SLJIT_MOV_P))
1344 return SLJIT_SUCCESS;
1345 #endif
1346
1347 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1348 compiler->skip_checks = 1;
1349 #endif
1350 return sljit_emit_op1(compiler, op, SLJIT_RETURN_REG, 0, src, srcw);
1351 }
1352
1353 /* CPU description section */
1354
1355 #if (defined SLJIT_32BIT_ARCHITECTURE && SLJIT_32BIT_ARCHITECTURE)
1356 #define SLJIT_CPUINFO_PART1 " 32bit ("
1357 #elif (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
1358 #define SLJIT_CPUINFO_PART1 " 64bit ("
1359 #else
1360 #error "Internal error: CPU type info missing"
1361 #endif
1362
1363 #if (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
1364 #define SLJIT_CPUINFO_PART2 "little endian + "
1365 #elif (defined SLJIT_BIG_ENDIAN && SLJIT_BIG_ENDIAN)
1366 #define SLJIT_CPUINFO_PART2 "big endian + "
1367 #else
1368 #error "Internal error: CPU type info missing"
1369 #endif
1370
1371 #if (defined SLJIT_UNALIGNED && SLJIT_UNALIGNED)
1372 #define SLJIT_CPUINFO_PART3 "unaligned)"
1373 #else
1374 #define SLJIT_CPUINFO_PART3 "aligned)"
1375 #endif
1376
1377 #define SLJIT_CPUINFO SLJIT_CPUINFO_PART1 SLJIT_CPUINFO_PART2 SLJIT_CPUINFO_PART3
1378
1379 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
1380 # include "sljitNativeX86_common.c"
1381 #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1382 # include "sljitNativeX86_common.c"
1383 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1384 # include "sljitNativeARM_32.c"
1385 #elif (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1386 # include "sljitNativeARM_32.c"
1387 #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
1388 # include "sljitNativeARM_T2_32.c"
1389 #elif (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
1390 # include "sljitNativeARM_64.c"
1391 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
1392 # include "sljitNativePPC_common.c"
1393 #elif (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1394 # include "sljitNativePPC_common.c"
1395 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1396 # include "sljitNativeMIPS_common.c"
1397 #elif (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1398 # include "sljitNativeMIPS_common.c"
1399 #elif (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1400 # include "sljitNativeSPARC_common.c"
1401 #elif (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
1402 # include "sljitNativeTILEGX_64.c"
1403 #endif
1404
1405 #if !(defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32) && !(defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
1406
1407 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
1408 sljit_si src1, sljit_sw src1w,
1409 sljit_si src2, sljit_sw src2w)
1410 {
1411 /* Default compare for most architectures. */
1412 sljit_si flags, tmp_src, condition;
1413 sljit_sw tmp_srcw;
1414
1415 CHECK_ERROR_PTR();
1416 check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w);
1417
1418 condition = type & 0xff;
1419 #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
1420 if ((condition == SLJIT_C_EQUAL || condition == SLJIT_C_NOT_EQUAL)) {
1421 if ((src1 & SLJIT_IMM) && !src1w) {
1422 src1 = src2;
1423 src1w = src2w;
1424 src2 = SLJIT_IMM;
1425 src2w = 0;
1426 }
1427 if ((src2 & SLJIT_IMM) && !src2w)
1428 return emit_cmp_to0(compiler, type, src1, src1w);
1429 }
1430 #endif
1431
1432 if (SLJIT_UNLIKELY((src1 & SLJIT_IMM) && !(src2 & SLJIT_IMM))) {
1433 /* Immediate is prefered as second argument by most architectures. */
1434 switch (condition) {
1435 case SLJIT_C_LESS:
1436 condition = SLJIT_C_GREATER;
1437 break;
1438 case SLJIT_C_GREATER_EQUAL:
1439 condition = SLJIT_C_LESS_EQUAL;
1440 break;
1441 case SLJIT_C_GREATER:
1442 condition = SLJIT_C_LESS;
1443 break;
1444 case SLJIT_C_LESS_EQUAL:
1445 condition = SLJIT_C_GREATER_EQUAL;
1446 break;
1447 case SLJIT_C_SIG_LESS:
1448 condition = SLJIT_C_SIG_GREATER;
1449 break;
1450 case SLJIT_C_SIG_GREATER_EQUAL:
1451 condition = SLJIT_C_SIG_LESS_EQUAL;
1452 break;
1453 case SLJIT_C_SIG_GREATER:
1454 condition = SLJIT_C_SIG_LESS;
1455 break;
1456 case SLJIT_C_SIG_LESS_EQUAL:
1457 condition = SLJIT_C_SIG_GREATER_EQUAL;
1458 break;
1459 }
1460 type = condition | (type & (SLJIT_INT_OP | SLJIT_REWRITABLE_JUMP));
1461 tmp_src = src1;
1462 src1 = src2;
1463 src2 = tmp_src;
1464 tmp_srcw = src1w;
1465 src1w = src2w;
1466 src2w = tmp_srcw;
1467 }
1468
1469 if (condition <= SLJIT_C_NOT_ZERO)
1470 flags = SLJIT_SET_E;
1471 else if (condition <= SLJIT_C_LESS_EQUAL)
1472 flags = SLJIT_SET_U;
1473 else
1474 flags = SLJIT_SET_S;
1475
1476 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1477 compiler->skip_checks = 1;
1478 #endif
1479 PTR_FAIL_IF(sljit_emit_op2(compiler, SLJIT_SUB | flags | (type & SLJIT_INT_OP),
1480 SLJIT_UNUSED, 0, src1, src1w, src2, src2w));
1481 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1482 compiler->skip_checks = 1;
1483 #endif
1484 return sljit_emit_jump(compiler, condition | (type & SLJIT_REWRITABLE_JUMP));
1485 }
1486
1487 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1488 sljit_si src1, sljit_sw src1w,
1489 sljit_si src2, sljit_sw src2w)
1490 {
1491 sljit_si flags, condition;
1492
1493 check_sljit_emit_fcmp(compiler, type, src1, src1w, src2, src2w);
1494
1495 condition = type & 0xff;
1496 flags = (condition <= SLJIT_C_FLOAT_NOT_EQUAL) ? SLJIT_SET_E : SLJIT_SET_S;
1497 if (type & SLJIT_SINGLE_OP)
1498 flags |= SLJIT_SINGLE_OP;
1499
1500 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1501 compiler->skip_checks = 1;
1502 #endif
1503 sljit_emit_fop1(compiler, SLJIT_CMPD | flags, src1, src1w, src2, src2w);
1504
1505 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1506 compiler->skip_checks = 1;
1507 #endif
1508 return sljit_emit_jump(compiler, condition | (type & SLJIT_REWRITABLE_JUMP));
1509 }
1510
1511 #endif
1512
1513 #if !(defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) && !(defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1514
1515 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset)
1516 {
1517 CHECK_ERROR();
1518 check_sljit_get_local_base(compiler, dst, dstw, offset);
1519
1520 ADJUST_LOCAL_OFFSET(SLJIT_MEM1(SLJIT_LOCALS_REG), offset);
1521 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1522 compiler->skip_checks = 1;
1523 #endif
1524 if (offset != 0)
1525 return sljit_emit_op2(compiler, SLJIT_ADD | SLJIT_KEEP_FLAGS, dst, dstw, SLJIT_LOCALS_REG, 0, SLJIT_IMM, offset);
1526 return sljit_emit_op1(compiler, SLJIT_MOV, dst, dstw, SLJIT_LOCALS_REG, 0);
1527 }
1528
1529 #endif
1530
1531 #else /* SLJIT_CONFIG_UNSUPPORTED */
1532
1533 /* Empty function bodies for those machines, which are not (yet) supported. */
1534
1535 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void)
1536 {
1537 return "unsupported";
1538 }
1539
1540 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void)
1541 {
1542 SLJIT_ASSERT_STOP();
1543 return NULL;
1544 }
1545
1546 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler)
1547 {
1548 SLJIT_UNUSED_ARG(compiler);
1549 SLJIT_ASSERT_STOP();
1550 }
1551
1552 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size)
1553 {
1554 SLJIT_UNUSED_ARG(compiler);
1555 SLJIT_UNUSED_ARG(size);
1556 SLJIT_ASSERT_STOP();
1557 return NULL;
1558 }
1559
1560 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1561 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose)
1562 {
1563 SLJIT_UNUSED_ARG(compiler);
1564 SLJIT_UNUSED_ARG(verbose);
1565 SLJIT_ASSERT_STOP();
1566 }
1567 #endif
1568
1569 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
1570 {
1571 SLJIT_UNUSED_ARG(compiler);
1572 SLJIT_ASSERT_STOP();
1573 return NULL;
1574 }
1575
1576 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
1577 {
1578 SLJIT_UNUSED_ARG(code);
1579 SLJIT_ASSERT_STOP();
1580 }
1581
1582 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
1583 {
1584 SLJIT_UNUSED_ARG(compiler);
1585 SLJIT_UNUSED_ARG(args);
1586 SLJIT_UNUSED_ARG(scratches);
1587 SLJIT_UNUSED_ARG(saveds);
1588 SLJIT_UNUSED_ARG(local_size);
1589 SLJIT_ASSERT_STOP();
1590 return SLJIT_ERR_UNSUPPORTED;
1591 }
1592
1593 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
1594 {
1595 SLJIT_UNUSED_ARG(compiler);
1596 SLJIT_UNUSED_ARG(args);
1597 SLJIT_UNUSED_ARG(scratches);
1598 SLJIT_UNUSED_ARG(saveds);
1599 SLJIT_UNUSED_ARG(local_size);
1600 SLJIT_ASSERT_STOP();
1601 }
1602
1603 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
1604 {
1605 SLJIT_UNUSED_ARG(compiler);
1606 SLJIT_UNUSED_ARG(op);
1607 SLJIT_UNUSED_ARG(src);
1608 SLJIT_UNUSED_ARG(srcw);
1609 SLJIT_ASSERT_STOP();
1610 return SLJIT_ERR_UNSUPPORTED;
1611 }
1612
1613 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1614 {
1615 SLJIT_UNUSED_ARG(compiler);
1616 SLJIT_UNUSED_ARG(dst);
1617 SLJIT_UNUSED_ARG(dstw);
1618 SLJIT_ASSERT_STOP();
1619 return SLJIT_ERR_UNSUPPORTED;
1620 }
1621
1622 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1623 {
1624 SLJIT_UNUSED_ARG(compiler);
1625 SLJIT_UNUSED_ARG(src);
1626 SLJIT_UNUSED_ARG(srcw);
1627 SLJIT_ASSERT_STOP();
1628 return SLJIT_ERR_UNSUPPORTED;
1629 }
1630
1631 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
1632 {
1633 SLJIT_UNUSED_ARG(compiler);
1634 SLJIT_UNUSED_ARG(op);
1635 SLJIT_ASSERT_STOP();
1636 return SLJIT_ERR_UNSUPPORTED;
1637 }
1638
1639 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
1640 sljit_si dst, sljit_sw dstw,
1641 sljit_si src, sljit_sw srcw)
1642 {
1643 SLJIT_UNUSED_ARG(compiler);
1644 SLJIT_UNUSED_ARG(op);
1645 SLJIT_UNUSED_ARG(dst);
1646 SLJIT_UNUSED_ARG(dstw);
1647 SLJIT_UNUSED_ARG(src);
1648 SLJIT_UNUSED_ARG(srcw);
1649 SLJIT_ASSERT_STOP();
1650 return SLJIT_ERR_UNSUPPORTED;
1651 }
1652
1653 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
1654 sljit_si dst, sljit_sw dstw,
1655 sljit_si src1, sljit_sw src1w,
1656 sljit_si src2, sljit_sw src2w)
1657 {
1658 SLJIT_UNUSED_ARG(compiler);
1659 SLJIT_UNUSED_ARG(op);
1660 SLJIT_UNUSED_ARG(dst);
1661 SLJIT_UNUSED_ARG(dstw);
1662 SLJIT_UNUSED_ARG(src1);
1663 SLJIT_UNUSED_ARG(src1w);
1664 SLJIT_UNUSED_ARG(src2);
1665 SLJIT_UNUSED_ARG(src2w);
1666 SLJIT_ASSERT_STOP();
1667 return SLJIT_ERR_UNSUPPORTED;
1668 }
1669
1670 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
1671 {
1672 SLJIT_ASSERT_STOP();
1673 return reg;
1674 }
1675
1676 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1677 void *instruction, sljit_si size)
1678 {
1679 SLJIT_UNUSED_ARG(compiler);
1680 SLJIT_UNUSED_ARG(instruction);
1681 SLJIT_UNUSED_ARG(size);
1682 SLJIT_ASSERT_STOP();
1683 return SLJIT_ERR_UNSUPPORTED;
1684 }
1685
1686 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
1687 {
1688 SLJIT_ASSERT_STOP();
1689 return 0;
1690 }
1691
1692 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1693 sljit_si dst, sljit_sw dstw,
1694 sljit_si src, sljit_sw srcw)
1695 {
1696 SLJIT_UNUSED_ARG(compiler);
1697 SLJIT_UNUSED_ARG(op);
1698 SLJIT_UNUSED_ARG(dst);
1699 SLJIT_UNUSED_ARG(dstw);
1700 SLJIT_UNUSED_ARG(src);
1701 SLJIT_UNUSED_ARG(srcw);
1702 SLJIT_ASSERT_STOP();
1703 return SLJIT_ERR_UNSUPPORTED;
1704 }
1705
1706 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1707 sljit_si dst, sljit_sw dstw,
1708 sljit_si src1, sljit_sw src1w,
1709 sljit_si src2, sljit_sw src2w)
1710 {
1711 SLJIT_UNUSED_ARG(compiler);
1712 SLJIT_UNUSED_ARG(op);
1713 SLJIT_UNUSED_ARG(dst);
1714 SLJIT_UNUSED_ARG(dstw);
1715 SLJIT_UNUSED_ARG(src1);
1716 SLJIT_UNUSED_ARG(src1w);
1717 SLJIT_UNUSED_ARG(src2);
1718 SLJIT_UNUSED_ARG(src2w);
1719 SLJIT_ASSERT_STOP();
1720 return SLJIT_ERR_UNSUPPORTED;
1721 }
1722
1723 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1724 {
1725 SLJIT_UNUSED_ARG(compiler);
1726 SLJIT_ASSERT_STOP();
1727 return NULL;
1728 }
1729
1730 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1731 {
1732 SLJIT_UNUSED_ARG(compiler);
1733 SLJIT_UNUSED_ARG(type);
1734 SLJIT_ASSERT_STOP();
1735 return NULL;
1736 }
1737
1738 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
1739 sljit_si src1, sljit_sw src1w,
1740 sljit_si src2, sljit_sw src2w)
1741 {
1742 SLJIT_UNUSED_ARG(compiler);
1743 SLJIT_UNUSED_ARG(type);
1744 SLJIT_UNUSED_ARG(src1);
1745 SLJIT_UNUSED_ARG(src1w);
1746 SLJIT_UNUSED_ARG(src2);
1747 SLJIT_UNUSED_ARG(src2w);
1748 SLJIT_ASSERT_STOP();
1749 return NULL;
1750 }
1751
1752 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
1753 sljit_si src1, sljit_sw src1w,
1754 sljit_si src2, sljit_sw src2w)
1755 {
1756 SLJIT_UNUSED_ARG(compiler);
1757 SLJIT_UNUSED_ARG(type);
1758 SLJIT_UNUSED_ARG(src1);
1759 SLJIT_UNUSED_ARG(src1w);
1760 SLJIT_UNUSED_ARG(src2);
1761 SLJIT_UNUSED_ARG(src2w);
1762 SLJIT_ASSERT_STOP();
1763 return NULL;
1764 }
1765
1766 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label)
1767 {
1768 SLJIT_UNUSED_ARG(jump);
1769 SLJIT_UNUSED_ARG(label);
1770 SLJIT_ASSERT_STOP();
1771 }
1772
1773 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target)
1774 {
1775 SLJIT_UNUSED_ARG(jump);
1776 SLJIT_UNUSED_ARG(target);
1777 SLJIT_ASSERT_STOP();
1778 }
1779
1780 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1781 {
1782 SLJIT_UNUSED_ARG(compiler);
1783 SLJIT_UNUSED_ARG(type);
1784 SLJIT_UNUSED_ARG(src);
1785 SLJIT_UNUSED_ARG(srcw);
1786 SLJIT_ASSERT_STOP();
1787 return SLJIT_ERR_UNSUPPORTED;
1788 }
1789
1790 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1791 sljit_si dst, sljit_sw dstw,
1792 sljit_si src, sljit_sw srcw,
1793 sljit_si type)
1794 {
1795 SLJIT_UNUSED_ARG(compiler);
1796 SLJIT_UNUSED_ARG(op);
1797 SLJIT_UNUSED_ARG(dst);
1798 SLJIT_UNUSED_ARG(dstw);
1799 SLJIT_UNUSED_ARG(src);
1800 SLJIT_UNUSED_ARG(srcw);
1801 SLJIT_UNUSED_ARG(type);
1802 SLJIT_ASSERT_STOP();
1803 return SLJIT_ERR_UNSUPPORTED;
1804 }
1805
1806 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset)
1807 {
1808 SLJIT_UNUSED_ARG(compiler);
1809 SLJIT_UNUSED_ARG(dst);
1810 SLJIT_UNUSED_ARG(dstw);
1811 SLJIT_UNUSED_ARG(offset);
1812 SLJIT_ASSERT_STOP();
1813 return SLJIT_ERR_UNSUPPORTED;
1814 }
1815
1816 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw initval)
1817 {
1818 SLJIT_UNUSED_ARG(compiler);
1819 SLJIT_UNUSED_ARG(dst);
1820 SLJIT_UNUSED_ARG(dstw);
1821 SLJIT_UNUSED_ARG(initval);
1822 SLJIT_ASSERT_STOP();
1823 return NULL;
1824 }
1825
1826 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
1827 {
1828 SLJIT_UNUSED_ARG(addr);
1829 SLJIT_UNUSED_ARG(new_addr);
1830 SLJIT_ASSERT_STOP();
1831 }
1832
1833 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
1834 {
1835 SLJIT_UNUSED_ARG(addr);
1836 SLJIT_UNUSED_ARG(new_constant);
1837 SLJIT_ASSERT_STOP();
1838 }
1839
1840 #endif
1841