/[pcre]/code/trunk/sljit/sljitLir.c
ViewVC logotype

Contents of /code/trunk/sljit/sljitLir.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 740 - (show annotations)
Mon Oct 31 06:10:14 2011 UTC (7 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 43894 byte(s)
Updating the JIT compiler
1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright 2009-2010 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 * of conditions and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "sljitLir.h"
28
29 #define CHECK_ERROR() \
30 do { \
31 if (SLJIT_UNLIKELY(compiler->error)) \
32 return compiler->error; \
33 } while (0)
34
35 #define CHECK_ERROR_PTR() \
36 do { \
37 if (SLJIT_UNLIKELY(compiler->error)) \
38 return NULL; \
39 } while (0)
40
41 #define CHECK_ERROR_VOID() \
42 do { \
43 if (SLJIT_UNLIKELY(compiler->error)) \
44 return; \
45 } while (0)
46
47 #define FAIL_IF(expr) \
48 do { \
49 if (SLJIT_UNLIKELY(expr)) \
50 return compiler->error; \
51 } while (0)
52
53 #define PTR_FAIL_IF(expr) \
54 do { \
55 if (SLJIT_UNLIKELY(expr)) \
56 return NULL; \
57 } while (0)
58
59 #define FAIL_IF_NULL(ptr) \
60 do { \
61 if (SLJIT_UNLIKELY(!(ptr))) { \
62 compiler->error = SLJIT_ERR_ALLOC_FAILED; \
63 return SLJIT_ERR_ALLOC_FAILED; \
64 } \
65 } while (0)
66
67 #define PTR_FAIL_IF_NULL(ptr) \
68 do { \
69 if (SLJIT_UNLIKELY(!(ptr))) { \
70 compiler->error = SLJIT_ERR_ALLOC_FAILED; \
71 return NULL; \
72 } \
73 } while (0)
74
75 #define PTR_FAIL_WITH_EXEC_IF(ptr) \
76 do { \
77 if (SLJIT_UNLIKELY(!(ptr))) { \
78 compiler->error = SLJIT_ERR_EX_ALLOC_FAILED; \
79 return NULL; \
80 } \
81 } while (0)
82
83 #if !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
84
85 #define GET_OPCODE(op) \
86 ((op) & ~(SLJIT_INT_OP | SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))
87
88 #define GET_FLAGS(op) \
89 ((op) & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C))
90
91 #define GET_ALL_FLAGS(op) \
92 ((op) & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))
93
94 #define BUF_SIZE 4096
95
96 #if (defined SLJIT_32BIT_ARCHITECTURE && SLJIT_32BIT_ARCHITECTURE)
97 #define ABUF_SIZE 2048
98 #else
99 #define ABUF_SIZE 4096
100 #endif
101
102 /* Jump flags. */
103 #define JUMP_LABEL 0x1
104 #define JUMP_ADDR 0x2
105 /* SLJIT_REWRITABLE_JUMP is 0x1000. */
106
107 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
108 #define PATCH_MB 0x4
109 #define PATCH_MW 0x8
110 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
111 #define PATCH_MD 0x10
112 #endif
113 #endif
114
115 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
116 #define IS_BL 0x4
117 #define PATCH_B 0x8
118 #endif
119
120 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
121 #define CPOOL_SIZE 512
122 #endif
123
124 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
125 #define IS_CONDITIONAL 0x04
126 #define IS_BL 0x08
127 /* cannot be encoded as branch */
128 #define B_TYPE0 0x00
129 /* conditional + imm8 */
130 #define B_TYPE1 0x10
131 /* conditional + imm20 */
132 #define B_TYPE2 0x20
133 /* IT + imm24 */
134 #define B_TYPE3 0x30
135 /* imm11 */
136 #define B_TYPE4 0x40
137 /* imm24 */
138 #define B_TYPE5 0x50
139 /* BL + imm24 */
140 #define BL_TYPE6 0x60
141 /* 0xf00 cc code for branches */
142 #endif
143
144 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
145 #define UNCOND_B 0x04
146 #define PATCH_B 0x08
147 #define ABSOLUTE_B 0x10
148 #endif
149
150 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
151 #define IS_MOVABLE 0x04
152 #define IS_JAL 0x08
153 #define IS_BIT26_COND 0x10
154 #define IS_BIT16_COND 0x20
155
156 #define IS_COND (IS_BIT26_COND | IS_BIT16_COND)
157
158 #define PATCH_B 0x40
159 #define PATCH_J 0x80
160
161 /* instruction types */
162 #define UNMOVABLE_INS 0
163 /* 1 - 31 last destination register */
164 #define FCSR_FCC 32
165 /* no destination (i.e: store) */
166 #define MOVABLE_INS 33
167 #endif
168
169 #endif /* !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED) */
170
171 /* Utils can still be used even if SLJIT_CONFIG_UNSUPPORTED is set. */
172 #include "sljitUtils.c"
173
174 #if !(defined SLJIT_CONFIG_UNSUPPORTED && SLJIT_CONFIG_UNSUPPORTED)
175
176 #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
177 #include "sljitExecAllocator.c"
178 #endif
179
180 #if (defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) && !(defined SLJIT_SSE2 && SLJIT_SSE2)
181 #error SLJIT_SSE2_AUTO cannot be enabled without SLJIT_SSE2
182 #endif
183
184 /* --------------------------------------------------------------------- */
185 /* Public functions */
186 /* --------------------------------------------------------------------- */
187
188 #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)))
189 #define SLJIT_NEEDS_COMPILER_INIT 1
190 static int compiler_initialized = 0;
191 /* A thread safe initialization. */
192 static void init_compiler(void);
193 #endif
194
195
196 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void)
197 {
198 struct sljit_compiler *compiler = (struct sljit_compiler*)SLJIT_MALLOC_ZEROED(sizeof(struct sljit_compiler));
199 if (!compiler)
200 return NULL;
201
202 SLJIT_COMPILE_ASSERT(
203 sizeof(sljit_b) == 1 && sizeof(sljit_ub) == 1
204 && sizeof(sljit_h) == 2 && sizeof(sljit_uh) == 2
205 && sizeof(sljit_i) == 4 && sizeof(sljit_ui) == 4
206 && ((sizeof(sljit_w) == 4 && sizeof(sljit_uw) == 4) || (sizeof(sljit_w) == 8 && sizeof(sljit_uw) == 8)),
207 invalid_integer_types);
208
209 /* Only the non-zero members must be set. */
210 compiler->error = SLJIT_SUCCESS;
211
212 compiler->buf = (struct sljit_memory_fragment*)SLJIT_MALLOC(BUF_SIZE);
213 compiler->abuf = (struct sljit_memory_fragment*)SLJIT_MALLOC(ABUF_SIZE);
214
215 if (!compiler->buf || !compiler->abuf) {
216 if (compiler->buf)
217 SLJIT_FREE(compiler->buf);
218 if (compiler->abuf)
219 SLJIT_FREE(compiler->abuf);
220 SLJIT_FREE(compiler);
221 return NULL;
222 }
223
224 compiler->buf->next = NULL;
225 compiler->buf->used_size = 0;
226 compiler->abuf->next = NULL;
227 compiler->abuf->used_size = 0;
228
229 compiler->temporaries = -1;
230 compiler->generals = -1;
231
232 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
233 compiler->args = -1;
234 #endif
235
236 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
237 compiler->cpool = (sljit_uw*)SLJIT_MALLOC(CPOOL_SIZE * sizeof(sljit_uw) + CPOOL_SIZE * sizeof(sljit_ub));
238 if (!compiler->cpool) {
239 SLJIT_FREE(compiler->buf);
240 SLJIT_FREE(compiler->abuf);
241 SLJIT_FREE(compiler);
242 return NULL;
243 }
244 compiler->cpool_unique = (sljit_ub*)(compiler->cpool + CPOOL_SIZE);
245 compiler->cpool_diff = 0xffffffff;
246 #endif
247
248 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
249 compiler->delay_slot = UNMOVABLE_INS;
250 #endif
251
252 #if (defined SLJIT_NEEDS_COMPILER_INIT && SLJIT_NEEDS_COMPILER_INIT)
253 if (!compiler_initialized) {
254 init_compiler();
255 compiler_initialized = 1;
256 }
257 #endif
258
259 return compiler;
260 }
261
262 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler)
263 {
264 struct sljit_memory_fragment *buf;
265 struct sljit_memory_fragment *curr;
266
267 buf = compiler->buf;
268 while (buf) {
269 curr = buf;
270 buf = buf->next;
271 SLJIT_FREE(curr);
272 }
273
274 buf = compiler->abuf;
275 while (buf) {
276 curr = buf;
277 buf = buf->next;
278 SLJIT_FREE(curr);
279 }
280
281 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
282 SLJIT_FREE(compiler->cpool);
283 #endif
284 SLJIT_FREE(compiler);
285 }
286
287 #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
288 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
289 {
290 /* Remove thumb mode flag. */
291 SLJIT_FREE_EXEC((void*)((sljit_uw)code & ~0x1));
292 }
293 #elif (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
294 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
295 {
296 /* Resolve indirection. */
297 code = (void*)(*(sljit_uw*)code);
298 SLJIT_FREE_EXEC(code);
299 }
300 #else
301 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
302 {
303 SLJIT_FREE_EXEC(code);
304 }
305 #endif
306
307 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label)
308 {
309 if (SLJIT_LIKELY(!!jump) && SLJIT_LIKELY(!!label)) {
310 jump->flags &= ~JUMP_ADDR;
311 jump->flags |= JUMP_LABEL;
312 jump->u.label = label;
313 }
314 }
315
316 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target)
317 {
318 if (SLJIT_LIKELY(!!jump)) {
319 SLJIT_ASSERT(jump->flags & SLJIT_REWRITABLE_JUMP);
320
321 jump->flags &= ~JUMP_LABEL;
322 jump->flags |= JUMP_ADDR;
323 jump->u.target = target;
324 }
325 }
326
327 /* --------------------------------------------------------------------- */
328 /* Private functions */
329 /* --------------------------------------------------------------------- */
330
331 static void* ensure_buf(struct sljit_compiler *compiler, int size)
332 {
333 sljit_ub *ret;
334 struct sljit_memory_fragment *new_frag;
335
336 if (compiler->buf->used_size + size <= (int)(BUF_SIZE - sizeof(sljit_uw) - sizeof(void*))) {
337 ret = compiler->buf->memory + compiler->buf->used_size;
338 compiler->buf->used_size += size;
339 return ret;
340 }
341 new_frag = (struct sljit_memory_fragment*)SLJIT_MALLOC(BUF_SIZE);
342 PTR_FAIL_IF_NULL(new_frag);
343 new_frag->next = compiler->buf;
344 compiler->buf = new_frag;
345 new_frag->used_size = size;
346 return new_frag->memory;
347 }
348
349 static void* ensure_abuf(struct sljit_compiler *compiler, int size)
350 {
351 sljit_ub *ret;
352 struct sljit_memory_fragment *new_frag;
353
354 if (compiler->abuf->used_size + size <= (int)(ABUF_SIZE - sizeof(sljit_uw) - sizeof(void*))) {
355 ret = compiler->abuf->memory + compiler->abuf->used_size;
356 compiler->abuf->used_size += size;
357 return ret;
358 }
359 new_frag = (struct sljit_memory_fragment*)SLJIT_MALLOC(ABUF_SIZE);
360 PTR_FAIL_IF_NULL(new_frag);
361 new_frag->next = compiler->abuf;
362 compiler->abuf = new_frag;
363 new_frag->used_size = size;
364 return new_frag->memory;
365 }
366
367 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size)
368 {
369 CHECK_ERROR_PTR();
370
371 #if (defined SLJIT_64BIT_ARCHITECTURE && SLJIT_64BIT_ARCHITECTURE)
372 if (size <= 0 || size > 128)
373 return NULL;
374 size = (size + 7) & ~7;
375 #else
376 if (size <= 0 || size > 64)
377 return NULL;
378 size = (size + 3) & ~3;
379 #endif
380 return ensure_abuf(compiler, size);
381 }
382
383 static SLJIT_INLINE void reverse_buf(struct sljit_compiler *compiler)
384 {
385 struct sljit_memory_fragment *buf = compiler->buf;
386 struct sljit_memory_fragment *prev = NULL;
387 struct sljit_memory_fragment *tmp;
388
389 do {
390 tmp = buf->next;
391 buf->next = prev;
392 prev = buf;
393 buf = tmp;
394 } while (buf != NULL);
395
396 compiler->buf = prev;
397 }
398
399 static SLJIT_INLINE void set_label(struct sljit_label *label, struct sljit_compiler *compiler)
400 {
401 label->next = NULL;
402 label->size = compiler->size;
403 if (compiler->last_label)
404 compiler->last_label->next = label;
405 else
406 compiler->labels = label;
407 compiler->last_label = label;
408 }
409
410 static SLJIT_INLINE void set_jump(struct sljit_jump *jump, struct sljit_compiler *compiler, int flags)
411 {
412 jump->next = NULL;
413 jump->flags = flags;
414 if (compiler->last_jump)
415 compiler->last_jump->next = jump;
416 else
417 compiler->jumps = jump;
418 compiler->last_jump = jump;
419 }
420
421 static SLJIT_INLINE void set_const(struct sljit_const *const_, struct sljit_compiler *compiler)
422 {
423 const_->next = NULL;
424 const_->addr = compiler->size;
425 if (compiler->last_const)
426 compiler->last_const->next = const_;
427 else
428 compiler->consts = const_;
429 compiler->last_const = const_;
430 }
431
432 #define ADDRESSING_DEPENDS_ON(exp, reg) \
433 (((exp) & SLJIT_MEM) && (((exp) & 0xf) == reg || (((exp) >> 4) & 0xf) == reg))
434
435 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
436 #define FUNCTION_CHECK_OP() \
437 SLJIT_ASSERT(!GET_FLAGS(op) || !(op & SLJIT_KEEP_FLAGS)); \
438 switch (GET_OPCODE(op)) { \
439 case SLJIT_NOT: \
440 case SLJIT_CLZ: \
441 case SLJIT_AND: \
442 case SLJIT_OR: \
443 case SLJIT_XOR: \
444 case SLJIT_SHL: \
445 case SLJIT_LSHR: \
446 case SLJIT_ASHR: \
447 SLJIT_ASSERT(!(op & (SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C))); \
448 break; \
449 case SLJIT_NEG: \
450 SLJIT_ASSERT(!(op & (SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_C))); \
451 break; \
452 case SLJIT_MUL: \
453 SLJIT_ASSERT(!(op & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_C))); \
454 break; \
455 case SLJIT_FCMP: \
456 SLJIT_ASSERT(!(op & (SLJIT_INT_OP | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))); \
457 SLJIT_ASSERT((op & (SLJIT_SET_E | SLJIT_SET_S))); \
458 break; \
459 case SLJIT_ADD: \
460 SLJIT_ASSERT(!(op & (SLJIT_SET_S | SLJIT_SET_U))); \
461 break; \
462 case SLJIT_SUB: \
463 break; \
464 case SLJIT_ADDC: \
465 case SLJIT_SUBC: \
466 SLJIT_ASSERT(!(op & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O))); \
467 break; \
468 default: \
469 /* Nothing allowed */ \
470 SLJIT_ASSERT(!(op & (SLJIT_INT_OP | SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O | SLJIT_SET_C | SLJIT_KEEP_FLAGS))); \
471 break; \
472 }
473
474 #define FUNCTION_CHECK_IS_REG(r) \
475 ((r) == SLJIT_UNUSED || (r) == SLJIT_LOCALS_REG || \
476 ((r) >= SLJIT_TEMPORARY_REG1 && (r) <= SLJIT_TEMPORARY_REG3 && (r) <= SLJIT_TEMPORARY_REG1 - 1 + compiler->temporaries) || \
477 ((r) >= SLJIT_GENERAL_REG1 && (r) <= SLJIT_GENERAL_REG3 && (r) <= SLJIT_GENERAL_REG1 - 1 + compiler->generals)) \
478
479 #define FUNCTION_CHECK_SRC(p, i) \
480 SLJIT_ASSERT(compiler->temporaries != -1 && compiler->generals != -1); \
481 if (((p) >= SLJIT_TEMPORARY_REG1 && (p) <= SLJIT_TEMPORARY_REG1 - 1 + compiler->temporaries) || \
482 ((p) >= SLJIT_GENERAL_REG1 && (p) <= SLJIT_GENERAL_REG1 - 1 + compiler->generals) || \
483 (p) == SLJIT_LOCALS_REG) \
484 SLJIT_ASSERT(i == 0); \
485 else if ((p) == SLJIT_IMM) \
486 ; \
487 else if ((p) & SLJIT_MEM) { \
488 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & 0xf)); \
489 if ((p) & 0xf0) { \
490 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(((p) >> 4) & 0xf)); \
491 SLJIT_ASSERT(((p) & 0xf0) != (SLJIT_LOCALS_REG << 4) && !(i & ~0x3)); \
492 } else \
493 SLJIT_ASSERT((((p) >> 4) & 0xf) == 0); \
494 SLJIT_ASSERT(((p) >> 9) == 0); \
495 } \
496 else \
497 SLJIT_ASSERT_STOP();
498
499 #define FUNCTION_CHECK_DST(p, i) \
500 SLJIT_ASSERT(compiler->temporaries != -1 && compiler->generals != -1); \
501 if (((p) >= SLJIT_TEMPORARY_REG1 && (p) <= SLJIT_TEMPORARY_REG1 - 1 + compiler->temporaries) || \
502 ((p) >= SLJIT_GENERAL_REG1 && (p) <= SLJIT_GENERAL_REG1 - 1 + compiler->generals) || \
503 (p) == SLJIT_UNUSED) \
504 SLJIT_ASSERT(i == 0); \
505 else if ((p) & SLJIT_MEM) { \
506 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & 0xf)); \
507 if ((p) & 0xf0) { \
508 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(((p) >> 4) & 0xf)); \
509 SLJIT_ASSERT(((p) & 0xf0) != (SLJIT_LOCALS_REG << 4) && !(i & ~0x3)); \
510 } else \
511 SLJIT_ASSERT((((p) >> 4) & 0xf) == 0); \
512 SLJIT_ASSERT(((p) >> 9) == 0); \
513 } \
514 else \
515 SLJIT_ASSERT_STOP();
516
517 #define FUNCTION_FCHECK(p, i) \
518 if ((p) >= SLJIT_FLOAT_REG1 && (p) <= SLJIT_FLOAT_REG4) \
519 SLJIT_ASSERT(i == 0); \
520 else if ((p) & SLJIT_MEM) { \
521 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG((p) & 0xf)); \
522 if ((p) & 0xf0) { \
523 SLJIT_ASSERT(FUNCTION_CHECK_IS_REG(((p) >> 4) & 0xf)); \
524 SLJIT_ASSERT(((p) & 0xf0) != (SLJIT_LOCALS_REG << 4) && !(i & ~0x3)); \
525 } else \
526 SLJIT_ASSERT((((p) >> 4) & 0xf) == 0); \
527 SLJIT_ASSERT(((p) >> 9) == 0); \
528 } \
529 else \
530 SLJIT_ASSERT_STOP();
531
532 #define FUNCTION_CHECK_OP1() \
533 if (GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI) { \
534 SLJIT_ASSERT(!GET_ALL_FLAGS(op)); \
535 } \
536 if (GET_OPCODE(op) >= SLJIT_MOVU && GET_OPCODE(op) <= SLJIT_MOVU_SI) { \
537 SLJIT_ASSERT(!(src & SLJIT_MEM) || (src & 0xf) != SLJIT_LOCALS_REG); \
538 SLJIT_ASSERT(!(dst & SLJIT_MEM) || (dst & 0xf) != SLJIT_LOCALS_REG); \
539 if ((src & SLJIT_MEM) && (src & 0xf)) \
540 SLJIT_ASSERT((dst & 0xf) != (src & 0xf) && ((dst >> 4) & 0xf) != (src & 0xf)); \
541 }
542
543 #endif
544
545 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
546
547 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose)
548 {
549 compiler->verbose = verbose;
550 }
551
552 static char* reg_names[] = {
553 (char*)"<noreg>", (char*)"tmp_r1", (char*)"tmp_r2", (char*)"tmp_r3",
554 (char*)"tmp_er1", (char*)"tmp_er2", (char*)"gen_r1", (char*)"gen_r2",
555 (char*)"gen_r3", (char*)"gen_er1", (char*)"gen_er2", (char*)"stack_r"
556 };
557
558 static char* freg_names[] = {
559 (char*)"<noreg>", (char*)"float_r1", (char*)"float_r2", (char*)"float_r3", (char*)"float_r4"
560 };
561
562 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
563 #ifdef _WIN64
564 #define SLJIT_PRINT_D "I64"
565 #else
566 #define SLJIT_PRINT_D "l"
567 #endif
568 #else
569 #define SLJIT_PRINT_D ""
570 #endif
571
572 #define sljit_verbose_param(p, i) \
573 if ((p) & SLJIT_IMM) \
574 fprintf(compiler->verbose, "#%"SLJIT_PRINT_D"d", (i)); \
575 else if ((p) & SLJIT_MEM) { \
576 if ((p) & 0xf) { \
577 if (i) { \
578 if (((p) >> 4) & 0xf) \
579 fprintf(compiler->verbose, "[%s + %s * %d]", reg_names[(p) & 0xF], reg_names[((p) >> 4)& 0xF], 1 << (i)); \
580 else \
581 fprintf(compiler->verbose, "[%s + #%"SLJIT_PRINT_D"d]", reg_names[(p) & 0xF], (i)); \
582 } \
583 else { \
584 if (((p) >> 4) & 0xf) \
585 fprintf(compiler->verbose, "[%s + %s]", reg_names[(p) & 0xF], reg_names[((p) >> 4)& 0xF]); \
586 else \
587 fprintf(compiler->verbose, "[%s]", reg_names[(p) & 0xF]); \
588 } \
589 } \
590 else \
591 fprintf(compiler->verbose, "[#%"SLJIT_PRINT_D"d]", (i)); \
592 } else \
593 fprintf(compiler->verbose, "%s", reg_names[p]);
594 #define sljit_verbose_fparam(p, i) \
595 if ((p) & SLJIT_MEM) { \
596 if ((p) & 0xf) { \
597 if (i) { \
598 if (((p) >> 4) & 0xf) \
599 fprintf(compiler->verbose, "[%s + %s * %d]", reg_names[(p) & 0xF], reg_names[((p) >> 4)& 0xF], 1 << (i)); \
600 else \
601 fprintf(compiler->verbose, "[%s + #%"SLJIT_PRINT_D"d]", reg_names[(p) & 0xF], (i)); \
602 } \
603 else { \
604 if (((p) >> 4) & 0xF) \
605 fprintf(compiler->verbose, "[%s + %s]", reg_names[(p) & 0xF], reg_names[((p) >> 4)& 0xF]); \
606 else \
607 fprintf(compiler->verbose, "[%s]", reg_names[(p) & 0xF]); \
608 } \
609 } \
610 else \
611 fprintf(compiler->verbose, "[#%"SLJIT_PRINT_D"d]", (i)); \
612 } else \
613 fprintf(compiler->verbose, "%s", freg_names[p]);
614
615 static SLJIT_CONST char* op_names[] = {
616 /* op0 */
617 (char*)"breakpoint", (char*)"nop",
618 /* op1 */
619 (char*)"mov", (char*)"mov.ub", (char*)"mov.sb", (char*)"mov.uh",
620 (char*)"mov.sh", (char*)"mov.ui", (char*)"mov.si", (char*)"movu",
621 (char*)"movu.ub", (char*)"movu.sb", (char*)"movu.uh", (char*)"movu.sh",
622 (char*)"movu.ui", (char*)"movu.si", (char*)"not", (char*)"neg",
623 (char*)"clz",
624 /* op2 */
625 (char*)"add", (char*)"addc", (char*)"sub", (char*)"subc",
626 (char*)"mul", (char*)"and", (char*)"or", (char*)"xor",
627 (char*)"shl", (char*)"lshr", (char*)"ashr",
628 /* fop1 */
629 (char*)"fcmp", (char*)"fmov", (char*)"fneg", (char*)"fabs",
630 /* fop2 */
631 (char*)"fadd", (char*)"fsub", (char*)"fmul", (char*)"fdiv"
632 };
633
634 static char* jump_names[] = {
635 (char*)"c_equal", (char*)"c_not_equal",
636 (char*)"c_less", (char*)"c_greater_equal",
637 (char*)"c_greater", (char*)"c_less_equal",
638 (char*)"c_sig_less", (char*)"c_sig_greater_equal",
639 (char*)"c_sig_greater", (char*)"c_sig_less_equal",
640 (char*)"c_overflow", (char*)"c_not_overflow",
641 (char*)"c_mul_overflow", (char*)"c_mul_not_overflow",
642 (char*)"c_float_equal", (char*)"c_float_not_equal",
643 (char*)"c_float_less", (char*)"c_float_greater_equal",
644 (char*)"c_float_greater", (char*)"c_float_less_equal",
645 (char*)"c_float_nan", (char*)"c_float_not_nan",
646 (char*)"jump", (char*)"fast_call",
647 (char*)"call0", (char*)"call1", (char*)"call2", (char*)"call3"
648 };
649
650 #endif
651
652 /* --------------------------------------------------------------------- */
653 /* Arch dependent */
654 /* --------------------------------------------------------------------- */
655
656 static SLJIT_INLINE void check_sljit_generate_code(struct sljit_compiler *compiler)
657 {
658 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
659 struct sljit_jump *jump;
660 #endif
661 /* If debug and verbose are disabled, all arguments are unused. */
662 SLJIT_UNUSED_ARG(compiler);
663
664 SLJIT_ASSERT(compiler->size > 0);
665 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
666 jump = compiler->jumps;
667 while (jump) {
668 /* All jumps have target. */
669 SLJIT_ASSERT(jump->flags & (JUMP_LABEL | JUMP_ADDR));
670 jump = jump->next;
671 }
672 #endif
673 }
674
675 static SLJIT_INLINE void check_sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
676 {
677 /* If debug and verbose are disabled, all arguments are unused. */
678 SLJIT_UNUSED_ARG(compiler);
679 SLJIT_UNUSED_ARG(args);
680 SLJIT_UNUSED_ARG(temporaries);
681 SLJIT_UNUSED_ARG(generals);
682 SLJIT_UNUSED_ARG(local_size);
683
684 SLJIT_ASSERT(args >= 0 && args <= 3);
685 SLJIT_ASSERT(temporaries >= 0 && temporaries <= SLJIT_NO_TMP_REGISTERS);
686 SLJIT_ASSERT(generals >= 0 && generals <= SLJIT_NO_GEN_REGISTERS);
687 SLJIT_ASSERT(args <= generals);
688 SLJIT_ASSERT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
689 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
690 if (SLJIT_UNLIKELY(!!compiler->verbose))
691 fprintf(compiler->verbose, " enter args=%d temporaries=%d generals=%d local_size=%d\n", args, temporaries, generals, local_size);
692 #endif
693 }
694
695 static SLJIT_INLINE void check_sljit_fake_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
696 {
697 /* If debug and verbose are disabled, all arguments are unused. */
698 SLJIT_UNUSED_ARG(compiler);
699 SLJIT_UNUSED_ARG(args);
700 SLJIT_UNUSED_ARG(temporaries);
701 SLJIT_UNUSED_ARG(generals);
702 SLJIT_UNUSED_ARG(local_size);
703
704 SLJIT_ASSERT(args >= 0 && args <= 3);
705 SLJIT_ASSERT(temporaries >= 0 && temporaries <= SLJIT_NO_TMP_REGISTERS);
706 SLJIT_ASSERT(generals >= 0 && generals <= SLJIT_NO_GEN_REGISTERS);
707 SLJIT_ASSERT(args <= generals);
708 SLJIT_ASSERT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
709 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
710 if (SLJIT_UNLIKELY(!!compiler->verbose))
711 fprintf(compiler->verbose, " fake_enter args=%d temporaries=%d generals=%d local_size=%d\n", args, temporaries, generals, local_size);
712 #endif
713 }
714
715 static SLJIT_INLINE void check_sljit_emit_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
716 {
717 /* If debug and verbose are disabled, all arguments are unused. */
718 SLJIT_UNUSED_ARG(compiler);
719 SLJIT_UNUSED_ARG(src);
720 SLJIT_UNUSED_ARG(srcw);
721
722 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
723 if (src != SLJIT_UNUSED) {
724 FUNCTION_CHECK_SRC(src, srcw);
725 }
726 else
727 SLJIT_ASSERT(srcw == 0);
728 #endif
729 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
730 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
731 fprintf(compiler->verbose, " return ");
732 sljit_verbose_param(src, srcw);
733 fprintf(compiler->verbose, "\n");
734 }
735 #endif
736 }
737
738 static SLJIT_INLINE void check_sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int generals, int local_size)
739 {
740 /* If debug and verbose are disabled, all arguments are unused. */
741 SLJIT_UNUSED_ARG(compiler);
742 SLJIT_UNUSED_ARG(dst);
743 SLJIT_UNUSED_ARG(dstw);
744 SLJIT_UNUSED_ARG(args);
745 SLJIT_UNUSED_ARG(temporaries);
746 SLJIT_UNUSED_ARG(generals);
747 SLJIT_UNUSED_ARG(local_size);
748
749 SLJIT_ASSERT(args >= 0 && args <= 3);
750 SLJIT_ASSERT(temporaries >= 0 && temporaries <= SLJIT_NO_TMP_REGISTERS);
751 SLJIT_ASSERT(generals >= 0 && generals <= SLJIT_NO_GEN_REGISTERS);
752 SLJIT_ASSERT(args <= generals);
753 SLJIT_ASSERT(local_size >= 0 && local_size <= SLJIT_MAX_LOCAL_SIZE);
754 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
755 compiler->temporaries = temporaries;
756 compiler->generals = generals;
757 FUNCTION_CHECK_DST(dst, dstw);
758 compiler->temporaries = -1;
759 compiler->generals = -1;
760 #endif
761 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
762 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
763 fprintf(compiler->verbose, " fast_enter ");
764 sljit_verbose_param(dst, dstw);
765 fprintf(compiler->verbose, " args=%d temporaries=%d generals=%d local_size=%d\n", args, temporaries, generals, local_size);
766 }
767 #endif
768 }
769
770 static SLJIT_INLINE void check_sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
771 {
772 /* If debug and verbose are disabled, all arguments are unused. */
773 SLJIT_UNUSED_ARG(compiler);
774 SLJIT_UNUSED_ARG(src);
775 SLJIT_UNUSED_ARG(srcw);
776
777 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
778 FUNCTION_CHECK_SRC(src, srcw);
779 #endif
780 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
781 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
782 fprintf(compiler->verbose, " fast_return ");
783 sljit_verbose_param(src, srcw);
784 fprintf(compiler->verbose, "\n");
785 }
786 #endif
787 }
788
789 static SLJIT_INLINE void check_sljit_emit_op0(struct sljit_compiler *compiler, int op)
790 {
791 /* If debug and verbose are disabled, all arguments are unused. */
792 SLJIT_UNUSED_ARG(compiler);
793 SLJIT_UNUSED_ARG(op);
794
795 SLJIT_ASSERT(op >= SLJIT_BREAKPOINT && op <= SLJIT_NOP);
796 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
797 if (SLJIT_UNLIKELY(!!compiler->verbose))
798 fprintf(compiler->verbose, " %s\n", op_names[op]);
799 #endif
800 }
801
802 static SLJIT_INLINE void check_sljit_emit_op1(struct sljit_compiler *compiler, int op,
803 int dst, sljit_w dstw,
804 int src, sljit_w srcw)
805 {
806 /* If debug and verbose are disabled, all arguments are unused. */
807 SLJIT_UNUSED_ARG(compiler);
808 SLJIT_UNUSED_ARG(op);
809 SLJIT_UNUSED_ARG(dst);
810 SLJIT_UNUSED_ARG(dstw);
811 SLJIT_UNUSED_ARG(src);
812 SLJIT_UNUSED_ARG(srcw);
813
814 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_CLZ);
815 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
816 FUNCTION_CHECK_OP();
817 FUNCTION_CHECK_SRC(src, srcw);
818 FUNCTION_CHECK_DST(dst, dstw);
819 FUNCTION_CHECK_OP1();
820 #endif
821 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
822 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
823 fprintf(compiler->verbose, " %s%s%s%s%s%s%s%s ", !(op & SLJIT_INT_OP) ? "" : "i", op_names[GET_OPCODE(op)],
824 !(op & SLJIT_SET_E) ? "" : "E", !(op & SLJIT_SET_S) ? "" : "S", !(op & SLJIT_SET_U) ? "" : "U", !(op & SLJIT_SET_O) ? "" : "O", !(op & SLJIT_SET_C) ? "" : "C", !(op & SLJIT_KEEP_FLAGS) ? "" : "K");
825 sljit_verbose_param(dst, dstw);
826 fprintf(compiler->verbose, ", ");
827 sljit_verbose_param(src, srcw);
828 fprintf(compiler->verbose, "\n");
829 }
830 #endif
831 }
832
833 static SLJIT_INLINE void check_sljit_emit_op2(struct sljit_compiler *compiler, int op,
834 int dst, sljit_w dstw,
835 int src1, sljit_w src1w,
836 int src2, sljit_w src2w)
837 {
838 /* If debug and verbose are disabled, all arguments are unused. */
839 SLJIT_UNUSED_ARG(compiler);
840 SLJIT_UNUSED_ARG(op);
841 SLJIT_UNUSED_ARG(dst);
842 SLJIT_UNUSED_ARG(dstw);
843 SLJIT_UNUSED_ARG(src1);
844 SLJIT_UNUSED_ARG(src1w);
845 SLJIT_UNUSED_ARG(src2);
846 SLJIT_UNUSED_ARG(src2w);
847
848 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
849 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
850 compiler->skip_checks = 0;
851 return;
852 }
853 #endif
854
855 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_ADD && GET_OPCODE(op) <= SLJIT_ASHR);
856 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
857 FUNCTION_CHECK_OP();
858 FUNCTION_CHECK_SRC(src1, src1w);
859 FUNCTION_CHECK_SRC(src2, src2w);
860 FUNCTION_CHECK_DST(dst, dstw);
861 #endif
862 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
863 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
864 fprintf(compiler->verbose, " %s%s%s%s%s%s%s%s ", !(op & SLJIT_INT_OP) ? "" : "i", op_names[GET_OPCODE(op)],
865 !(op & SLJIT_SET_E) ? "" : "E", !(op & SLJIT_SET_S) ? "" : "S", !(op & SLJIT_SET_U) ? "" : "U", !(op & SLJIT_SET_O) ? "" : "O", !(op & SLJIT_SET_C) ? "" : "C", !(op & SLJIT_KEEP_FLAGS) ? "" : "K");
866 sljit_verbose_param(dst, dstw);
867 fprintf(compiler->verbose, ", ");
868 sljit_verbose_param(src1, src1w);
869 fprintf(compiler->verbose, ", ");
870 sljit_verbose_param(src2, src2w);
871 fprintf(compiler->verbose, "\n");
872 }
873 #endif
874 }
875
876 static SLJIT_INLINE void check_sljit_emit_fop1(struct sljit_compiler *compiler, int op,
877 int dst, sljit_w dstw,
878 int src, sljit_w srcw)
879 {
880 /* If debug and verbose are disabled, all arguments are unused. */
881 SLJIT_UNUSED_ARG(compiler);
882 SLJIT_UNUSED_ARG(op);
883 SLJIT_UNUSED_ARG(dst);
884 SLJIT_UNUSED_ARG(dstw);
885 SLJIT_UNUSED_ARG(src);
886 SLJIT_UNUSED_ARG(srcw);
887
888 SLJIT_ASSERT(sljit_is_fpu_available());
889 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_FCMP && GET_OPCODE(op) <= SLJIT_FABS);
890 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
891 FUNCTION_CHECK_OP();
892 FUNCTION_FCHECK(src, srcw);
893 FUNCTION_FCHECK(dst, dstw);
894 #endif
895 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
896 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
897 fprintf(compiler->verbose, " %s%s%s ", op_names[GET_OPCODE(op)],
898 !(op & SLJIT_SET_E) ? "" : "E", !(op & SLJIT_SET_S) ? "" : "S");
899 sljit_verbose_fparam(dst, dstw);
900 fprintf(compiler->verbose, ", ");
901 sljit_verbose_fparam(src, srcw);
902 fprintf(compiler->verbose, "\n");
903 }
904 #endif
905 }
906
907 static SLJIT_INLINE void check_sljit_emit_fop2(struct sljit_compiler *compiler, int op,
908 int dst, sljit_w dstw,
909 int src1, sljit_w src1w,
910 int src2, sljit_w src2w)
911 {
912 /* If debug and verbose are disabled, all arguments are unused. */
913 SLJIT_UNUSED_ARG(compiler);
914 SLJIT_UNUSED_ARG(op);
915 SLJIT_UNUSED_ARG(dst);
916 SLJIT_UNUSED_ARG(dstw);
917 SLJIT_UNUSED_ARG(src1);
918 SLJIT_UNUSED_ARG(src1w);
919 SLJIT_UNUSED_ARG(src2);
920 SLJIT_UNUSED_ARG(src2w);
921
922 SLJIT_ASSERT(sljit_is_fpu_available());
923 SLJIT_ASSERT(GET_OPCODE(op) >= SLJIT_FADD && GET_OPCODE(op) <= SLJIT_FDIV);
924 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
925 FUNCTION_CHECK_OP();
926 FUNCTION_FCHECK(src1, src1w);
927 FUNCTION_FCHECK(src2, src2w);
928 FUNCTION_FCHECK(dst, dstw);
929 #endif
930 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
931 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
932 fprintf(compiler->verbose, " %s ", op_names[GET_OPCODE(op)]);
933 sljit_verbose_fparam(dst, dstw);
934 fprintf(compiler->verbose, ", ");
935 sljit_verbose_fparam(src1, src1w);
936 fprintf(compiler->verbose, ", ");
937 sljit_verbose_fparam(src2, src2w);
938 fprintf(compiler->verbose, "\n");
939 }
940 #endif
941 }
942
943 static SLJIT_INLINE void check_sljit_emit_label(struct sljit_compiler *compiler)
944 {
945 /* If debug and verbose are disabled, all arguments are unused. */
946 SLJIT_UNUSED_ARG(compiler);
947
948 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
949 if (SLJIT_UNLIKELY(!!compiler->verbose))
950 fprintf(compiler->verbose, "label:\n");
951 #endif
952 }
953
954 static SLJIT_INLINE void check_sljit_emit_jump(struct sljit_compiler *compiler, int type)
955 {
956 /* If debug and verbose are disabled, all arguments are unused. */
957 SLJIT_UNUSED_ARG(compiler);
958 SLJIT_UNUSED_ARG(type);
959
960 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
961 if (SLJIT_UNLIKELY(compiler->skip_checks)) {
962 compiler->skip_checks = 0;
963 return;
964 }
965 #endif
966
967 SLJIT_ASSERT(!(type & ~(0xff | SLJIT_REWRITABLE_JUMP)));
968 SLJIT_ASSERT((type & 0xff) >= SLJIT_C_EQUAL && (type & 0xff) <= SLJIT_CALL3);
969 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
970 if (SLJIT_UNLIKELY(!!compiler->verbose))
971 fprintf(compiler->verbose, " jump%s <%s>\n", !(type & SLJIT_REWRITABLE_JUMP) ? "" : "R", jump_names[type & 0xff]);
972 #endif
973 }
974
975 static SLJIT_INLINE void check_sljit_emit_cmp(struct sljit_compiler *compiler, int type,
976 int src1, sljit_w src1w,
977 int src2, sljit_w src2w)
978 {
979 SLJIT_UNUSED_ARG(compiler);
980 SLJIT_UNUSED_ARG(type);
981 SLJIT_UNUSED_ARG(src1);
982 SLJIT_UNUSED_ARG(src1w);
983 SLJIT_UNUSED_ARG(src2);
984 SLJIT_UNUSED_ARG(src2w);
985
986 SLJIT_ASSERT(!(type & ~(0xff | SLJIT_INT_OP | SLJIT_REWRITABLE_JUMP)));
987 SLJIT_ASSERT((type & 0xff) >= SLJIT_C_EQUAL && (type & 0xff) <= SLJIT_C_SIG_LESS_EQUAL);
988 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
989 FUNCTION_CHECK_SRC(src1, src1w);
990 FUNCTION_CHECK_SRC(src2, src2w);
991 #endif
992 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
993 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
994 fprintf(compiler->verbose, " %scmp%s <%s> ", !(type & SLJIT_INT_OP) ? "" : "i", !(type & SLJIT_REWRITABLE_JUMP) ? "" : "R", jump_names[type & 0xff]);
995 sljit_verbose_param(src1, src1w);
996 fprintf(compiler->verbose, ", ");
997 sljit_verbose_param(src2, src2w);
998 fprintf(compiler->verbose, "\n");
999 }
1000 #endif
1001 }
1002
1003 static SLJIT_INLINE void check_sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1004 {
1005 /* If debug and verbose are disabled, all arguments are unused. */
1006 SLJIT_UNUSED_ARG(compiler);
1007 SLJIT_UNUSED_ARG(type);
1008 SLJIT_UNUSED_ARG(src);
1009 SLJIT_UNUSED_ARG(srcw);
1010
1011 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
1012 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1013 FUNCTION_CHECK_SRC(src, srcw);
1014 #endif
1015 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1016 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1017 fprintf(compiler->verbose, " ijump <%s> ", jump_names[type]);
1018 sljit_verbose_param(src, srcw);
1019 fprintf(compiler->verbose, "\n");
1020 }
1021 #endif
1022 }
1023
1024 static SLJIT_INLINE void check_sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1025 {
1026 /* If debug and verbose are disabled, all arguments are unused. */
1027 SLJIT_UNUSED_ARG(compiler);
1028 SLJIT_UNUSED_ARG(op);
1029 SLJIT_UNUSED_ARG(dst);
1030 SLJIT_UNUSED_ARG(dstw);
1031 SLJIT_UNUSED_ARG(type);
1032
1033 SLJIT_ASSERT(type >= SLJIT_C_EQUAL && type < SLJIT_JUMP);
1034 SLJIT_ASSERT(op == SLJIT_MOV || GET_OPCODE(op) == SLJIT_OR);
1035 SLJIT_ASSERT(GET_ALL_FLAGS(op) == 0 || GET_ALL_FLAGS(op) == SLJIT_SET_E || GET_ALL_FLAGS(op) == SLJIT_KEEP_FLAGS);
1036 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1037 FUNCTION_CHECK_DST(dst, dstw);
1038 #endif
1039 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1040 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1041 fprintf(compiler->verbose, " cond_set%s%s <%s> ", !(op & SLJIT_SET_E) ? "" : "E",
1042 !(op & SLJIT_KEEP_FLAGS) ? "" : "K", op_names[GET_OPCODE(op)]);
1043 sljit_verbose_param(dst, dstw);
1044 fprintf(compiler->verbose, ", <%s>\n", jump_names[type]);
1045 }
1046 #endif
1047 }
1048
1049 static SLJIT_INLINE void check_sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value)
1050 {
1051 /* If debug and verbose are disabled, all arguments are unused. */
1052 SLJIT_UNUSED_ARG(compiler);
1053 SLJIT_UNUSED_ARG(dst);
1054 SLJIT_UNUSED_ARG(dstw);
1055 SLJIT_UNUSED_ARG(init_value);
1056
1057 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1058 FUNCTION_CHECK_DST(dst, dstw);
1059 #endif
1060 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1061 if (SLJIT_UNLIKELY(!!compiler->verbose)) {
1062 fprintf(compiler->verbose, " const ");
1063 sljit_verbose_param(dst, dstw);
1064 fprintf(compiler->verbose, ", #%"SLJIT_PRINT_D"d\n", init_value);
1065 }
1066 #endif
1067 }
1068
1069 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
1070 #include "sljitNativeX86_common.c"
1071 #elif (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1072 #include "sljitNativeX86_common.c"
1073 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1074 #include "sljitNativeARM_v5.c"
1075 #elif (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1076 #include "sljitNativeARM_v5.c"
1077 #elif (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
1078 #include "sljitNativeARM_Thumb2.c"
1079 #elif (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
1080 #include "sljitNativePPC_common.c"
1081 #elif (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1082 #include "sljitNativePPC_common.c"
1083 #elif (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1084 #include "sljitNativeMIPS_common.c"
1085 #endif
1086
1087 #if !(defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1088 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
1089 int src1, sljit_w src1w,
1090 int src2, sljit_w src2w)
1091 {
1092 /* Default compare for most architectures. */
1093 int flags, tmp_src, condition;
1094 sljit_w tmp_srcw;
1095
1096 CHECK_ERROR_PTR();
1097 check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w);
1098
1099 condition = type & 0xff;
1100 if (SLJIT_UNLIKELY((src1 & SLJIT_IMM) && !(src2 & SLJIT_IMM))) {
1101 /* Immediate is prefered as second argument by most architectures. */
1102 switch (condition) {
1103 case SLJIT_C_LESS:
1104 condition = SLJIT_C_GREATER;
1105 break;
1106 case SLJIT_C_GREATER_EQUAL:
1107 condition = SLJIT_C_LESS_EQUAL;
1108 break;
1109 case SLJIT_C_GREATER:
1110 condition = SLJIT_C_LESS;
1111 break;
1112 case SLJIT_C_LESS_EQUAL:
1113 condition = SLJIT_C_GREATER_EQUAL;
1114 break;
1115 case SLJIT_C_SIG_LESS:
1116 condition = SLJIT_C_SIG_GREATER;
1117 break;
1118 case SLJIT_C_SIG_GREATER_EQUAL:
1119 condition = SLJIT_C_SIG_LESS_EQUAL;
1120 break;
1121 case SLJIT_C_SIG_GREATER:
1122 condition = SLJIT_C_SIG_LESS;
1123 break;
1124 case SLJIT_C_SIG_LESS_EQUAL:
1125 condition = SLJIT_C_SIG_GREATER_EQUAL;
1126 break;
1127 }
1128 type = condition | (type & (SLJIT_INT_OP | SLJIT_REWRITABLE_JUMP));
1129 tmp_src = src1;
1130 src1 = src2;
1131 src2 = tmp_src;
1132 tmp_srcw = src1w;
1133 src1w = src2w;
1134 src2w = tmp_srcw;
1135 }
1136
1137 if (condition <= SLJIT_C_NOT_ZERO)
1138 flags = SLJIT_SET_E;
1139 else if (condition <= SLJIT_C_LESS_EQUAL)
1140 flags = SLJIT_SET_U;
1141 else
1142 flags = SLJIT_SET_S;
1143
1144 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1145 compiler->skip_checks = 1;
1146 #endif
1147 PTR_FAIL_IF(sljit_emit_op2(compiler, SLJIT_SUB | flags | (type & SLJIT_INT_OP),
1148 SLJIT_UNUSED, 0, src1, src1w, src2, src2w));
1149 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1150 compiler->skip_checks = 1;
1151 #endif
1152 return sljit_emit_jump(compiler, condition | (type & SLJIT_REWRITABLE_JUMP));
1153 }
1154 #endif
1155
1156 #else /* SLJIT_CONFIG_UNSUPPORTED */
1157
1158 /* Empty function bodies for those machines, which are not (yet) supported. */
1159
1160 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name()
1161 {
1162 return "unsupported";
1163 }
1164
1165 SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void)
1166 {
1167 SLJIT_ASSERT_STOP();
1168 return NULL;
1169 }
1170
1171 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler)
1172 {
1173 SLJIT_UNUSED_ARG(compiler);
1174 SLJIT_ASSERT_STOP();
1175 }
1176
1177 SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, int size)
1178 {
1179 SLJIT_UNUSED_ARG(compiler);
1180 SLJIT_UNUSED_ARG(size);
1181 SLJIT_ASSERT_STOP();
1182 return NULL;
1183 }
1184
1185 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
1186 SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose)
1187 {
1188 SLJIT_UNUSED_ARG(compiler);
1189 SLJIT_UNUSED_ARG(verbose);
1190 SLJIT_ASSERT_STOP();
1191 }
1192 #endif
1193
1194 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
1195 {
1196 SLJIT_UNUSED_ARG(compiler);
1197 SLJIT_ASSERT_STOP();
1198 return NULL;
1199 }
1200
1201 SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code)
1202 {
1203 SLJIT_UNUSED_ARG(code);
1204 SLJIT_ASSERT_STOP();
1205 }
1206
1207 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
1208 {
1209 SLJIT_UNUSED_ARG(compiler);
1210 SLJIT_UNUSED_ARG(args);
1211 SLJIT_UNUSED_ARG(temporaries);
1212 SLJIT_UNUSED_ARG(generals);
1213 SLJIT_UNUSED_ARG(local_size);
1214 SLJIT_ASSERT_STOP();
1215 return SLJIT_ERR_UNSUPPORTED;
1216 }
1217
1218 SLJIT_API_FUNC_ATTRIBUTE void sljit_fake_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
1219 {
1220 SLJIT_UNUSED_ARG(compiler);
1221 SLJIT_UNUSED_ARG(args);
1222 SLJIT_UNUSED_ARG(temporaries);
1223 SLJIT_UNUSED_ARG(generals);
1224 SLJIT_UNUSED_ARG(local_size);
1225 SLJIT_ASSERT_STOP();
1226 }
1227
1228 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1229 {
1230 SLJIT_UNUSED_ARG(compiler);
1231 SLJIT_UNUSED_ARG(src);
1232 SLJIT_UNUSED_ARG(srcw);
1233 SLJIT_ASSERT_STOP();
1234 return SLJIT_ERR_UNSUPPORTED;
1235 }
1236
1237 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int generals, int local_size)
1238 {
1239 SLJIT_UNUSED_ARG(compiler);
1240 SLJIT_UNUSED_ARG(dst);
1241 SLJIT_UNUSED_ARG(dstw);
1242 SLJIT_UNUSED_ARG(args);
1243 SLJIT_UNUSED_ARG(temporaries);
1244 SLJIT_UNUSED_ARG(generals);
1245 SLJIT_UNUSED_ARG(local_size);
1246 SLJIT_ASSERT_STOP();
1247 return SLJIT_ERR_UNSUPPORTED;
1248 }
1249
1250 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1251 {
1252 SLJIT_UNUSED_ARG(compiler);
1253 SLJIT_UNUSED_ARG(src);
1254 SLJIT_UNUSED_ARG(srcw);
1255 SLJIT_ASSERT_STOP();
1256 return SLJIT_ERR_UNSUPPORTED;
1257 }
1258
1259 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op)
1260 {
1261 SLJIT_UNUSED_ARG(compiler);
1262 SLJIT_UNUSED_ARG(op);
1263 SLJIT_ASSERT_STOP();
1264 return SLJIT_ERR_UNSUPPORTED;
1265 }
1266
1267 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
1268 int dst, sljit_w dstw,
1269 int src, sljit_w srcw)
1270 {
1271 SLJIT_UNUSED_ARG(compiler);
1272 SLJIT_UNUSED_ARG(op);
1273 SLJIT_UNUSED_ARG(dst);
1274 SLJIT_UNUSED_ARG(dstw);
1275 SLJIT_UNUSED_ARG(src);
1276 SLJIT_UNUSED_ARG(srcw);
1277 SLJIT_ASSERT_STOP();
1278 return SLJIT_ERR_UNSUPPORTED;
1279 }
1280
1281 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
1282 int dst, sljit_w dstw,
1283 int src1, sljit_w src1w,
1284 int src2, sljit_w src2w)
1285 {
1286 SLJIT_UNUSED_ARG(compiler);
1287 SLJIT_UNUSED_ARG(op);
1288 SLJIT_UNUSED_ARG(dst);
1289 SLJIT_UNUSED_ARG(dstw);
1290 SLJIT_UNUSED_ARG(src1);
1291 SLJIT_UNUSED_ARG(src1w);
1292 SLJIT_UNUSED_ARG(src2);
1293 SLJIT_UNUSED_ARG(src2w);
1294 SLJIT_ASSERT_STOP();
1295 return SLJIT_ERR_UNSUPPORTED;
1296 }
1297
1298 SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void)
1299 {
1300 SLJIT_ASSERT_STOP();
1301 return 0;
1302 }
1303
1304 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
1305 int dst, sljit_w dstw,
1306 int src, sljit_w srcw)
1307 {
1308 SLJIT_UNUSED_ARG(compiler);
1309 SLJIT_UNUSED_ARG(op);
1310 SLJIT_UNUSED_ARG(dst);
1311 SLJIT_UNUSED_ARG(dstw);
1312 SLJIT_UNUSED_ARG(src);
1313 SLJIT_UNUSED_ARG(srcw);
1314 SLJIT_ASSERT_STOP();
1315 return SLJIT_ERR_UNSUPPORTED;
1316 }
1317
1318 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
1319 int dst, sljit_w dstw,
1320 int src1, sljit_w src1w,
1321 int src2, sljit_w src2w)
1322 {
1323 SLJIT_UNUSED_ARG(compiler);
1324 SLJIT_UNUSED_ARG(op);
1325 SLJIT_UNUSED_ARG(dst);
1326 SLJIT_UNUSED_ARG(dstw);
1327 SLJIT_UNUSED_ARG(src1);
1328 SLJIT_UNUSED_ARG(src1w);
1329 SLJIT_UNUSED_ARG(src2);
1330 SLJIT_UNUSED_ARG(src2w);
1331 SLJIT_ASSERT_STOP();
1332 return SLJIT_ERR_UNSUPPORTED;
1333 }
1334
1335 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1336 {
1337 SLJIT_UNUSED_ARG(compiler);
1338 SLJIT_ASSERT_STOP();
1339 return NULL;
1340 }
1341
1342 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type)
1343 {
1344 SLJIT_UNUSED_ARG(compiler);
1345 SLJIT_UNUSED_ARG(type);
1346 SLJIT_ASSERT_STOP();
1347 return NULL;
1348 }
1349
1350 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
1351 int src1, sljit_w src1w,
1352 int src2, sljit_w src2w)
1353 {
1354 SLJIT_UNUSED_ARG(compiler);
1355 SLJIT_UNUSED_ARG(type);
1356 SLJIT_UNUSED_ARG(src1);
1357 SLJIT_UNUSED_ARG(src1w);
1358 SLJIT_UNUSED_ARG(src2);
1359 SLJIT_UNUSED_ARG(src2w);
1360 SLJIT_ASSERT_STOP();
1361 return NULL;
1362 }
1363
1364 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label)
1365 {
1366 SLJIT_UNUSED_ARG(jump);
1367 SLJIT_UNUSED_ARG(label);
1368 SLJIT_ASSERT_STOP();
1369 }
1370
1371 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target)
1372 {
1373 SLJIT_UNUSED_ARG(jump);
1374 SLJIT_UNUSED_ARG(target);
1375 SLJIT_ASSERT_STOP();
1376 }
1377
1378 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1379 {
1380 SLJIT_UNUSED_ARG(compiler);
1381 SLJIT_UNUSED_ARG(type);
1382 SLJIT_UNUSED_ARG(src);
1383 SLJIT_UNUSED_ARG(srcw);
1384 SLJIT_ASSERT_STOP();
1385 return SLJIT_ERR_UNSUPPORTED;
1386 }
1387
1388 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1389 {
1390 SLJIT_UNUSED_ARG(compiler);
1391 SLJIT_UNUSED_ARG(op);
1392 SLJIT_UNUSED_ARG(dst);
1393 SLJIT_UNUSED_ARG(dstw);
1394 SLJIT_UNUSED_ARG(type);
1395 SLJIT_ASSERT_STOP();
1396 return SLJIT_ERR_UNSUPPORTED;
1397 }
1398
1399 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w initval)
1400 {
1401 SLJIT_UNUSED_ARG(compiler);
1402 SLJIT_UNUSED_ARG(dst);
1403 SLJIT_UNUSED_ARG(dstw);
1404 SLJIT_UNUSED_ARG(initval);
1405 SLJIT_ASSERT_STOP();
1406 return NULL;
1407 }
1408
1409 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
1410 {
1411 SLJIT_UNUSED_ARG(addr);
1412 SLJIT_UNUSED_ARG(new_addr);
1413 SLJIT_ASSERT_STOP();
1414 }
1415
1416 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant)
1417 {
1418 SLJIT_UNUSED_ARG(addr);
1419 SLJIT_UNUSED_ARG(new_constant);
1420 SLJIT_ASSERT_STOP();
1421 }
1422
1423 #endif

  ViewVC Help
Powered by ViewVC 1.1.5