| 1 |
/* |
/* |
| 2 |
* Stack-less Just-In-Time compiler |
* Stack-less Just-In-Time compiler |
| 3 |
* |
* |
| 4 |
* Copyright 2009-2010 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved. |
* Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved. |
| 5 |
* |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without modification, are |
* Redistribution and use in source and binary forms, with or without modification, are |
| 7 |
* permitted provided that the following conditions are met: |
* permitted provided that the following conditions are met: |
| 24 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
*/ |
| 26 |
|
|
| 27 |
SLJIT_CONST char* sljit_get_platform_name() |
SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name() |
| 28 |
{ |
{ |
| 29 |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
return "x86" SLJIT_CPUINFO; |
|
return "x86-32"; |
|
|
#else |
|
|
return "x86-64"; |
|
|
#endif |
|
| 30 |
} |
} |
| 31 |
|
|
| 32 |
/* |
/* |
| 76 |
p = SLJIT_MEM1(SLJIT_LOCALS_REG); \ |
p = SLJIT_MEM1(SLJIT_LOCALS_REG); \ |
| 77 |
do; \ |
do; \ |
| 78 |
} \ |
} \ |
| 79 |
else if (p >= SLJIT_GENERAL_EREG1 && p <= SLJIT_GENERAL_EREG2) { \ |
else if (p >= SLJIT_SAVED_EREG1 && p <= SLJIT_SAVED_EREG2) { \ |
| 80 |
w = compiler->generals_start + (p - SLJIT_GENERAL_EREG1) * sizeof(sljit_w); \ |
w = compiler->saveds_start + (p - SLJIT_SAVED_EREG1) * sizeof(sljit_w); \ |
| 81 |
p = SLJIT_MEM1(SLJIT_LOCALS_REG); \ |
p = SLJIT_MEM1(SLJIT_LOCALS_REG); \ |
| 82 |
do; \ |
do; \ |
| 83 |
} |
} |
| 91 |
|
|
| 92 |
/* Note: r12 & 0x7 == 0b100, which decoded as SIB byte present |
/* Note: r12 & 0x7 == 0b100, which decoded as SIB byte present |
| 93 |
Note: avoid to use r12 and r13 for memory addessing |
Note: avoid to use r12 and r13 for memory addessing |
| 94 |
therefore r12 is better for GENERAL_EREG than GENERAL_REG. */ |
therefore r12 is better for SAVED_EREG than SAVED_REG. */ |
| 95 |
#ifndef _WIN64 |
#ifndef _WIN64 |
| 96 |
/* 1st passed in rdi, 2nd argument passed in rsi, 3rd in rdx. */ |
/* 1st passed in rdi, 2nd argument passed in rsi, 3rd in rdx. */ |
| 97 |
static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 4] = { |
static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 4] = { |
| 241 |
*code_ptr++ = 0xe9; |
*code_ptr++ = 0xe9; |
| 242 |
jump->addr++; |
jump->addr++; |
| 243 |
} |
} |
| 244 |
else if (type >= SLJIT_CALL0) { |
else if (type >= SLJIT_FAST_CALL) { |
| 245 |
short_jump = 0; |
short_jump = 0; |
| 246 |
*code_ptr++ = 0xe8; |
*code_ptr++ = 0xe8; |
| 247 |
jump->addr++; |
jump->addr++; |
| 271 |
return code_ptr; |
return code_ptr; |
| 272 |
} |
} |
| 273 |
|
|
| 274 |
void* sljit_generate_code(struct sljit_compiler *compiler) |
SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler) |
| 275 |
{ |
{ |
| 276 |
struct sljit_memory_fragment *buf; |
struct sljit_memory_fragment *buf; |
| 277 |
sljit_ub *code; |
sljit_ub *code; |
| 353 |
while (jump) { |
while (jump) { |
| 354 |
if (jump->flags & PATCH_MB) { |
if (jump->flags & PATCH_MB) { |
| 355 |
SLJIT_ASSERT((sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_b))) >= -128 && (sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_b))) <= 127); |
SLJIT_ASSERT((sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_b))) >= -128 && (sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_b))) <= 127); |
| 356 |
*(sljit_ub*)jump->addr = jump->u.label->addr - (jump->addr + sizeof(sljit_b)); |
*(sljit_ub*)jump->addr = (sljit_ub)(jump->u.label->addr - (jump->addr + sizeof(sljit_b))); |
| 357 |
} else if (jump->flags & PATCH_MW) { |
} else if (jump->flags & PATCH_MW) { |
| 358 |
if (jump->flags & JUMP_LABEL) { |
if (jump->flags & JUMP_LABEL) { |
| 359 |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 360 |
*(sljit_w*)jump->addr = jump->u.label->addr - (jump->addr + sizeof(sljit_w)); |
*(sljit_w*)jump->addr = (sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_w))); |
| 361 |
#else |
#else |
| 362 |
SLJIT_ASSERT((sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_hw))) >= -0x80000000ll && (sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_hw))) <= 0x7fffffffll); |
SLJIT_ASSERT((sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_hw))) >= -0x80000000ll && (sljit_w)(jump->u.label->addr - (jump->addr + sizeof(sljit_hw))) <= 0x7fffffffll); |
| 363 |
*(sljit_hw*)jump->addr = jump->u.label->addr - (jump->addr + sizeof(sljit_hw)); |
*(sljit_hw*)jump->addr = (sljit_hw)(jump->u.label->addr - (jump->addr + sizeof(sljit_hw))); |
| 364 |
#endif |
#endif |
| 365 |
} |
} |
| 366 |
else { |
else { |
| 367 |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 368 |
*(sljit_w*)jump->addr = jump->u.target - (jump->addr + sizeof(sljit_w)); |
*(sljit_w*)jump->addr = (sljit_w)(jump->u.target - (jump->addr + sizeof(sljit_w))); |
| 369 |
#else |
#else |
| 370 |
SLJIT_ASSERT((sljit_w)(jump->u.target - (jump->addr + sizeof(sljit_hw))) >= -0x80000000ll && (sljit_w)(jump->u.target - (jump->addr + sizeof(sljit_hw))) <= 0x7fffffffll); |
SLJIT_ASSERT((sljit_w)(jump->u.target - (jump->addr + sizeof(sljit_hw))) >= -0x80000000ll && (sljit_w)(jump->u.target - (jump->addr + sizeof(sljit_hw))) <= 0x7fffffffll); |
| 371 |
*(sljit_hw*)jump->addr = jump->u.target - (jump->addr + sizeof(sljit_hw)); |
*(sljit_hw*)jump->addr = (sljit_hw)(jump->u.target - (jump->addr + sizeof(sljit_hw))); |
| 372 |
#endif |
#endif |
| 373 |
} |
} |
| 374 |
} |
} |
| 383 |
/* Maybe we waste some space because of short jumps. */ |
/* Maybe we waste some space because of short jumps. */ |
| 384 |
SLJIT_ASSERT(code_ptr <= code + compiler->size); |
SLJIT_ASSERT(code_ptr <= code + compiler->size); |
| 385 |
compiler->error = SLJIT_ERR_COMPILED; |
compiler->error = SLJIT_ERR_COMPILED; |
| 386 |
|
compiler->executable_size = compiler->size; |
| 387 |
return (void*)code; |
return (void*)code; |
| 388 |
} |
} |
| 389 |
|
|
| 470 |
#include "sljitNativeX86_64.c" |
#include "sljitNativeX86_64.c" |
| 471 |
#endif |
#endif |
| 472 |
|
|
|
int sljit_emit_op0(struct sljit_compiler *compiler, int op) |
|
|
{ |
|
|
sljit_ub *buf; |
|
|
|
|
|
CHECK_ERROR(); |
|
|
check_sljit_emit_op0(compiler, op); |
|
|
|
|
|
op = GET_OPCODE(op); |
|
|
switch (op) { |
|
|
case SLJIT_BREAKPOINT: |
|
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
|
|
FAIL_IF(!buf); |
|
|
INC_SIZE(1); |
|
|
*buf = 0xcc; |
|
|
break; |
|
|
case SLJIT_NOP: |
|
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
|
|
FAIL_IF(!buf); |
|
|
INC_SIZE(1); |
|
|
*buf = 0x90; |
|
|
break; |
|
|
} |
|
|
|
|
|
return SLJIT_SUCCESS; |
|
|
} |
|
|
|
|
| 473 |
static int emit_mov(struct sljit_compiler *compiler, |
static int emit_mov(struct sljit_compiler *compiler, |
| 474 |
int dst, sljit_w dstw, |
int dst, sljit_w dstw, |
| 475 |
int src, sljit_w srcw) |
int src, sljit_w srcw) |
| 538 |
#define EMIT_MOV(compiler, dst, dstw, src, srcw) \ |
#define EMIT_MOV(compiler, dst, dstw, src, srcw) \ |
| 539 |
FAIL_IF(emit_mov(compiler, dst, dstw, src, srcw)); |
FAIL_IF(emit_mov(compiler, dst, dstw, src, srcw)); |
| 540 |
|
|
| 541 |
|
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op) |
| 542 |
|
{ |
| 543 |
|
sljit_ub *buf; |
| 544 |
|
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
| 545 |
|
int size; |
| 546 |
|
#endif |
| 547 |
|
|
| 548 |
|
CHECK_ERROR(); |
| 549 |
|
check_sljit_emit_op0(compiler, op); |
| 550 |
|
|
| 551 |
|
switch (GET_OPCODE(op)) { |
| 552 |
|
case SLJIT_BREAKPOINT: |
| 553 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
| 554 |
|
FAIL_IF(!buf); |
| 555 |
|
INC_SIZE(1); |
| 556 |
|
*buf = 0xcc; |
| 557 |
|
break; |
| 558 |
|
case SLJIT_NOP: |
| 559 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
| 560 |
|
FAIL_IF(!buf); |
| 561 |
|
INC_SIZE(1); |
| 562 |
|
*buf = 0x90; |
| 563 |
|
break; |
| 564 |
|
case SLJIT_UMUL: |
| 565 |
|
case SLJIT_SMUL: |
| 566 |
|
case SLJIT_UDIV: |
| 567 |
|
case SLJIT_SDIV: |
| 568 |
|
compiler->flags_saved = 0; |
| 569 |
|
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
| 570 |
|
#ifdef _WIN64 |
| 571 |
|
SLJIT_COMPILE_ASSERT( |
| 572 |
|
reg_map[SLJIT_TEMPORARY_REG1] == 0 |
| 573 |
|
&& reg_map[SLJIT_TEMPORARY_REG2] == 2 |
| 574 |
|
&& reg_map[TMP_REGISTER] > 7, |
| 575 |
|
invalid_register_assignment_for_div_mul); |
| 576 |
|
#else |
| 577 |
|
SLJIT_COMPILE_ASSERT( |
| 578 |
|
reg_map[SLJIT_TEMPORARY_REG1] == 0 |
| 579 |
|
&& reg_map[SLJIT_TEMPORARY_REG2] < 7 |
| 580 |
|
&& reg_map[TMP_REGISTER] == 2, |
| 581 |
|
invalid_register_assignment_for_div_mul); |
| 582 |
|
#endif |
| 583 |
|
compiler->mode32 = op & SLJIT_INT_OP; |
| 584 |
|
#endif |
| 585 |
|
|
| 586 |
|
op = GET_OPCODE(op); |
| 587 |
|
if (op == SLJIT_UDIV) { |
| 588 |
|
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || defined(_WIN64) |
| 589 |
|
EMIT_MOV(compiler, TMP_REGISTER, 0, SLJIT_TEMPORARY_REG2, 0); |
| 590 |
|
buf = emit_x86_instruction(compiler, 1, SLJIT_TEMPORARY_REG2, 0, SLJIT_TEMPORARY_REG2, 0); |
| 591 |
|
#else |
| 592 |
|
buf = emit_x86_instruction(compiler, 1, TMP_REGISTER, 0, TMP_REGISTER, 0); |
| 593 |
|
#endif |
| 594 |
|
FAIL_IF(!buf); |
| 595 |
|
*buf = 0x33; |
| 596 |
|
} |
| 597 |
|
|
| 598 |
|
if (op == SLJIT_SDIV) { |
| 599 |
|
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) || defined(_WIN64) |
| 600 |
|
EMIT_MOV(compiler, TMP_REGISTER, 0, SLJIT_TEMPORARY_REG2, 0); |
| 601 |
|
#endif |
| 602 |
|
|
| 603 |
|
/* CDQ instruction */ |
| 604 |
|
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 605 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
| 606 |
|
FAIL_IF(!buf); |
| 607 |
|
INC_SIZE(1); |
| 608 |
|
*buf = 0x99; |
| 609 |
|
#else |
| 610 |
|
if (compiler->mode32) { |
| 611 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 1); |
| 612 |
|
FAIL_IF(!buf); |
| 613 |
|
INC_SIZE(1); |
| 614 |
|
*buf = 0x99; |
| 615 |
|
} else { |
| 616 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 2); |
| 617 |
|
FAIL_IF(!buf); |
| 618 |
|
INC_SIZE(2); |
| 619 |
|
*buf++ = REX_W; |
| 620 |
|
*buf = 0x99; |
| 621 |
|
} |
| 622 |
|
#endif |
| 623 |
|
} |
| 624 |
|
|
| 625 |
|
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 626 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + 2); |
| 627 |
|
FAIL_IF(!buf); |
| 628 |
|
INC_SIZE(2); |
| 629 |
|
*buf++ = 0xf7; |
| 630 |
|
*buf = 0xc0 | ((op >= SLJIT_UDIV) ? reg_map[TMP_REGISTER] : reg_map[SLJIT_TEMPORARY_REG2]); |
| 631 |
|
#else |
| 632 |
|
#ifdef _WIN64 |
| 633 |
|
size = (!compiler->mode32 || op >= SLJIT_UDIV) ? 3 : 2; |
| 634 |
|
#else |
| 635 |
|
size = (!compiler->mode32) ? 3 : 2; |
| 636 |
|
#endif |
| 637 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + size); |
| 638 |
|
FAIL_IF(!buf); |
| 639 |
|
INC_SIZE(size); |
| 640 |
|
#ifdef _WIN64 |
| 641 |
|
if (!compiler->mode32) |
| 642 |
|
*buf++ = REX_W | ((op >= SLJIT_UDIV) ? REX_B : 0); |
| 643 |
|
else if (op >= SLJIT_UDIV) |
| 644 |
|
*buf++ = REX_B; |
| 645 |
|
*buf++ = 0xf7; |
| 646 |
|
*buf = 0xc0 | ((op >= SLJIT_UDIV) ? reg_lmap[TMP_REGISTER] : reg_lmap[SLJIT_TEMPORARY_REG2]); |
| 647 |
|
#else |
| 648 |
|
if (!compiler->mode32) |
| 649 |
|
*buf++ = REX_W; |
| 650 |
|
*buf++ = 0xf7; |
| 651 |
|
*buf = 0xc0 | reg_map[SLJIT_TEMPORARY_REG2]; |
| 652 |
|
#endif |
| 653 |
|
#endif |
| 654 |
|
switch (op) { |
| 655 |
|
case SLJIT_UMUL: |
| 656 |
|
*buf |= 4 << 3; |
| 657 |
|
break; |
| 658 |
|
case SLJIT_SMUL: |
| 659 |
|
*buf |= 5 << 3; |
| 660 |
|
break; |
| 661 |
|
case SLJIT_UDIV: |
| 662 |
|
*buf |= 6 << 3; |
| 663 |
|
break; |
| 664 |
|
case SLJIT_SDIV: |
| 665 |
|
*buf |= 7 << 3; |
| 666 |
|
break; |
| 667 |
|
} |
| 668 |
|
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) && !defined(_WIN64) |
| 669 |
|
EMIT_MOV(compiler, SLJIT_TEMPORARY_REG2, 0, TMP_REGISTER, 0); |
| 670 |
|
#endif |
| 671 |
|
break; |
| 672 |
|
} |
| 673 |
|
|
| 674 |
|
return SLJIT_SUCCESS; |
| 675 |
|
} |
| 676 |
|
|
| 677 |
#define ENCODE_PREFIX(prefix) \ |
#define ENCODE_PREFIX(prefix) \ |
| 678 |
do { \ |
do { \ |
| 679 |
code = (sljit_ub*)ensure_buf(compiler, 1 + 1); \ |
code = (sljit_ub*)ensure_buf(compiler, 1 + 1); \ |
| 959 |
sljit_ub* code; |
sljit_ub* code; |
| 960 |
int dst_r; |
int dst_r; |
| 961 |
|
|
| 962 |
|
SLJIT_UNUSED_ARG(op); |
| 963 |
if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) { |
if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) { |
| 964 |
/* Just set the zero flag. */ |
/* Just set the zero flag. */ |
| 965 |
EMIT_MOV(compiler, TMP_REGISTER, 0, src, srcw); |
EMIT_MOV(compiler, TMP_REGISTER, 0, src, srcw); |
| 1035 |
return SLJIT_SUCCESS; |
return SLJIT_SUCCESS; |
| 1036 |
} |
} |
| 1037 |
|
|
| 1038 |
int sljit_emit_op1(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op, |
| 1039 |
int dst, sljit_w dstw, |
int dst, sljit_w dstw, |
| 1040 |
int src, sljit_w srcw) |
int src, sljit_w srcw) |
| 1041 |
{ |
{ |
| 1063 |
compiler->mode32 = 0; |
compiler->mode32 = 0; |
| 1064 |
#endif |
#endif |
| 1065 |
|
|
| 1066 |
SLJIT_ASSERT(SLJIT_MOV + 7 == SLJIT_MOVU); |
SLJIT_COMPILE_ASSERT(SLJIT_MOV + 7 == SLJIT_MOVU, movu_offset); |
| 1067 |
if (op >= SLJIT_MOVU) { |
if (op >= SLJIT_MOVU) { |
| 1068 |
update = 1; |
update = 1; |
| 1069 |
op -= 7; |
op -= 7; |
| 1468 |
code = (sljit_ub*)ensure_buf(compiler, 1 + 4); |
code = (sljit_ub*)ensure_buf(compiler, 1 + 4); |
| 1469 |
FAIL_IF(!code); |
FAIL_IF(!code); |
| 1470 |
INC_CSIZE(4); |
INC_CSIZE(4); |
| 1471 |
*(sljit_hw*)code = src1w; |
*(sljit_hw*)code = (sljit_hw)src1w; |
| 1472 |
} |
} |
| 1473 |
else { |
else { |
| 1474 |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_IMM, src1w); |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_IMM, src1w); |
| 1511 |
code = (sljit_ub*)ensure_buf(compiler, 1 + 4); |
code = (sljit_ub*)ensure_buf(compiler, 1 + 4); |
| 1512 |
FAIL_IF(!code); |
FAIL_IF(!code); |
| 1513 |
INC_CSIZE(4); |
INC_CSIZE(4); |
| 1514 |
*(sljit_hw*)code = src2w; |
*(sljit_hw*)code = (sljit_hw)src2w; |
| 1515 |
} |
} |
| 1516 |
else { |
else { |
| 1517 |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_IMM, src1w); |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_IMM, src1w); |
| 1526 |
} |
} |
| 1527 |
else { |
else { |
| 1528 |
/* Neither argument is immediate. */ |
/* Neither argument is immediate. */ |
| 1529 |
if (depends_on(src2, dst_r)) |
if (ADDRESSING_DEPENDS_ON(src2, dst_r)) |
| 1530 |
dst_r = TMP_REGISTER; |
dst_r = TMP_REGISTER; |
| 1531 |
EMIT_MOV(compiler, dst_r, 0, src1, src1w); |
EMIT_MOV(compiler, dst_r, 0, src1, src1w); |
| 1532 |
code = emit_x86_instruction(compiler, 2, dst_r, 0, src2, src2w); |
code = emit_x86_instruction(compiler, 2, dst_r, 0, src2, src2w); |
| 1814 |
*code |= mode; |
*code |= mode; |
| 1815 |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
| 1816 |
} |
} |
| 1817 |
else if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS && dst != src2 && !depends_on(src2, dst)) { |
else if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS && dst != src2 && !ADDRESSING_DEPENDS_ON(src2, dst)) { |
| 1818 |
if (src1 != dst) |
if (src1 != dst) |
| 1819 |
EMIT_MOV(compiler, dst, 0, src1, src1w); |
EMIT_MOV(compiler, dst, 0, src1, src1w); |
| 1820 |
EMIT_MOV(compiler, TMP_REGISTER, 0, SLJIT_PREF_SHIFT_REG, 0); |
EMIT_MOV(compiler, TMP_REGISTER, 0, SLJIT_PREF_SHIFT_REG, 0); |
| 1825 |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
| 1826 |
} |
} |
| 1827 |
else { |
else { |
| 1828 |
/* This case is really difficult, since ecx can be used for |
/* This case is really difficult, since ecx itself may used for |
| 1829 |
addressing as well, and we must ensure to work even in that case. */ |
addressing, and we must ensure to work even in that case. */ |
| 1830 |
|
EMIT_MOV(compiler, TMP_REGISTER, 0, src1, src1w); |
| 1831 |
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
| 1832 |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_PREF_SHIFT_REG, 0); |
EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_PREF_SHIFT_REG, 0); |
| 1833 |
#else |
#else |
| 1834 |
/* [esp - 4] is reserved for eflags. */ |
/* [esp - 4] is reserved for eflags. */ |
| 1835 |
EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), -(int)(2 * sizeof(sljit_w)), SLJIT_PREF_SHIFT_REG, 0); |
EMIT_MOV(compiler, SLJIT_MEM1(SLJIT_LOCALS_REG), -(int)(2 * sizeof(sljit_w)), SLJIT_PREF_SHIFT_REG, 0); |
| 1836 |
#endif |
#endif |
|
|
|
|
EMIT_MOV(compiler, TMP_REGISTER, 0, src1, src1w); |
|
| 1837 |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, src2, src2w); |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, src2, src2w); |
| 1838 |
code = emit_x86_instruction(compiler, 1 | EX86_SHIFT_INS, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
code = emit_x86_instruction(compiler, 1 | EX86_SHIFT_INS, SLJIT_PREF_SHIFT_REG, 0, TMP_REGISTER, 0); |
| 1839 |
FAIL_IF(!code); |
FAIL_IF(!code); |
| 1840 |
*code |= mode; |
*code |= mode; |
|
|
|
| 1841 |
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
| 1842 |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REG2, 0); |
EMIT_MOV(compiler, SLJIT_PREF_SHIFT_REG, 0, TMP_REG2, 0); |
| 1843 |
#else |
#else |
| 1850 |
return SLJIT_SUCCESS; |
return SLJIT_SUCCESS; |
| 1851 |
} |
} |
| 1852 |
|
|
| 1853 |
int sljit_emit_op2(struct sljit_compiler *compiler, int op, |
static int emit_shift_with_flags(struct sljit_compiler *compiler, |
| 1854 |
|
sljit_ub mode, int set_flags, |
| 1855 |
|
int dst, sljit_w dstw, |
| 1856 |
|
int src1, sljit_w src1w, |
| 1857 |
|
int src2, sljit_w src2w) |
| 1858 |
|
{ |
| 1859 |
|
/* The CPU does not set flags if the shift count is 0. */ |
| 1860 |
|
if (src2 & SLJIT_IMM) { |
| 1861 |
|
#if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64) |
| 1862 |
|
if ((src2w & 0x3f) != 0 || (compiler->mode32 && (src2w & 0x1f) != 0)) |
| 1863 |
|
return emit_shift(compiler, mode, dst, dstw, src1, src1w, src2, src2w); |
| 1864 |
|
#else |
| 1865 |
|
if ((src2w & 0x1f) != 0) |
| 1866 |
|
return emit_shift(compiler, mode, dst, dstw, src1, src1w, src2, src2w); |
| 1867 |
|
#endif |
| 1868 |
|
if (!set_flags) |
| 1869 |
|
return emit_mov(compiler, dst, dstw, src1, src1w); |
| 1870 |
|
/* OR dst, src, 0 */ |
| 1871 |
|
return emit_cum_binary(compiler, 0x0b, 0x09, 0x1 << 3, 0x0d, |
| 1872 |
|
dst, dstw, src1, src1w, SLJIT_IMM, 0); |
| 1873 |
|
} |
| 1874 |
|
|
| 1875 |
|
if (!set_flags) |
| 1876 |
|
return emit_shift(compiler, mode, dst, dstw, src1, src1w, src2, src2w); |
| 1877 |
|
|
| 1878 |
|
if (!(dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)) |
| 1879 |
|
FAIL_IF(emit_cmp_binary(compiler, src1, src1w, SLJIT_IMM, 0)); |
| 1880 |
|
|
| 1881 |
|
FAIL_IF(emit_shift(compiler,mode, dst, dstw, src1, src1w, src2, src2w)); |
| 1882 |
|
|
| 1883 |
|
if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) |
| 1884 |
|
return emit_cmp_binary(compiler, dst, dstw, SLJIT_IMM, 0); |
| 1885 |
|
return SLJIT_SUCCESS; |
| 1886 |
|
} |
| 1887 |
|
|
| 1888 |
|
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op, |
| 1889 |
int dst, sljit_w dstw, |
int dst, sljit_w dstw, |
| 1890 |
int src1, sljit_w src1w, |
int src1, sljit_w src1w, |
| 1891 |
int src2, sljit_w src2w) |
int src2, sljit_w src2w) |
| 1964 |
return emit_cum_binary(compiler, 0x33, 0x31, 0x6 << 3, 0x35, |
return emit_cum_binary(compiler, 0x33, 0x31, 0x6 << 3, 0x35, |
| 1965 |
dst, dstw, src1, src1w, src2, src2w); |
dst, dstw, src1, src1w, src2, src2w); |
| 1966 |
case SLJIT_SHL: |
case SLJIT_SHL: |
| 1967 |
return emit_shift(compiler, 0x4 << 3, |
return emit_shift_with_flags(compiler, 0x4 << 3, GET_FLAGS(op), |
| 1968 |
dst, dstw, src1, src1w, src2, src2w); |
dst, dstw, src1, src1w, src2, src2w); |
| 1969 |
case SLJIT_LSHR: |
case SLJIT_LSHR: |
| 1970 |
return emit_shift(compiler, 0x5 << 3, |
return emit_shift_with_flags(compiler, 0x5 << 3, GET_FLAGS(op), |
| 1971 |
dst, dstw, src1, src1w, src2, src2w); |
dst, dstw, src1, src1w, src2, src2w); |
| 1972 |
case SLJIT_ASHR: |
case SLJIT_ASHR: |
| 1973 |
return emit_shift(compiler, 0x7 << 3, |
return emit_shift_with_flags(compiler, 0x7 << 3, GET_FLAGS(op), |
| 1974 |
dst, dstw, src1, src1w, src2, src2w); |
dst, dstw, src1, src1w, src2, src2w); |
| 1975 |
} |
} |
| 1976 |
|
|
| 1977 |
return SLJIT_SUCCESS; |
return SLJIT_SUCCESS; |
| 1978 |
} |
} |
| 1979 |
|
|
| 1980 |
|
SLJIT_API_FUNC_ATTRIBUTE int sljit_get_register_index(int reg) |
| 1981 |
|
{ |
| 1982 |
|
check_sljit_get_register_index(reg); |
| 1983 |
|
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 1984 |
|
if (reg == SLJIT_TEMPORARY_EREG1 || reg == SLJIT_TEMPORARY_EREG2 |
| 1985 |
|
|| reg == SLJIT_SAVED_EREG1 || reg == SLJIT_SAVED_EREG2) |
| 1986 |
|
return -1; |
| 1987 |
|
#endif |
| 1988 |
|
return reg_map[reg]; |
| 1989 |
|
} |
| 1990 |
|
|
| 1991 |
|
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op_custom(struct sljit_compiler *compiler, |
| 1992 |
|
void *instruction, int size) |
| 1993 |
|
{ |
| 1994 |
|
sljit_ub *buf; |
| 1995 |
|
|
| 1996 |
|
CHECK_ERROR(); |
| 1997 |
|
check_sljit_emit_op_custom(compiler, instruction, size); |
| 1998 |
|
SLJIT_ASSERT(size > 0 && size < 16); |
| 1999 |
|
|
| 2000 |
|
buf = (sljit_ub*)ensure_buf(compiler, 1 + size); |
| 2001 |
|
FAIL_IF(!buf); |
| 2002 |
|
INC_SIZE(size); |
| 2003 |
|
SLJIT_MEMMOVE(buf, instruction, size); |
| 2004 |
|
return SLJIT_SUCCESS; |
| 2005 |
|
} |
| 2006 |
|
|
| 2007 |
/* --------------------------------------------------------------------- */ |
/* --------------------------------------------------------------------- */ |
| 2008 |
/* Floating point operators */ |
/* Floating point operators */ |
| 2009 |
/* --------------------------------------------------------------------- */ |
/* --------------------------------------------------------------------- */ |
| 2015 |
#if (defined SLJIT_SSE2 && SLJIT_SSE2) |
#if (defined SLJIT_SSE2 && SLJIT_SSE2) |
| 2016 |
|
|
| 2017 |
/* Alignment + 2 * 16 bytes. */ |
/* Alignment + 2 * 16 bytes. */ |
| 2018 |
static int sse2_data[3 + 4 + 4]; |
static sljit_i sse2_data[3 + 4 + 4]; |
| 2019 |
static int *sse2_buffer; |
static sljit_i *sse2_buffer; |
| 2020 |
|
|
| 2021 |
static void init_compiler() |
static void init_compiler() |
| 2022 |
{ |
{ |
| 2024 |
int features = 0; |
int features = 0; |
| 2025 |
#endif |
#endif |
| 2026 |
|
|
| 2027 |
sse2_buffer = (int*)(((sljit_uw)sse2_data + 15) & ~0xf); |
sse2_buffer = (sljit_i*)(((sljit_uw)sse2_data + 15) & ~0xf); |
| 2028 |
sse2_buffer[0] = 0; |
sse2_buffer[0] = 0; |
| 2029 |
sse2_buffer[1] = 0x80000000; |
sse2_buffer[1] = 0x80000000; |
| 2030 |
sse2_buffer[4] = 0xffffffff; |
sse2_buffer[4] = 0xffffffff; |
| 2043 |
: |
: |
| 2044 |
: "%eax", "%ecx", "%edx" |
: "%eax", "%ecx", "%edx" |
| 2045 |
); |
); |
| 2046 |
#elif defined(_MSC_VER) |
#elif defined(_MSC_VER) || defined(__BORLANDC__) |
| 2047 |
/* Intel syntax. */ |
/* Intel syntax. */ |
| 2048 |
__asm { |
__asm { |
| 2049 |
mov eax, 1 |
mov eax, 1 |
| 2061 |
|
|
| 2062 |
#endif |
#endif |
| 2063 |
|
|
| 2064 |
int sljit_is_fpu_available(void) |
SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void) |
| 2065 |
{ |
{ |
| 2066 |
/* Always available. */ |
/* Always available. */ |
| 2067 |
return 1; |
return 1; |
| 2106 |
} |
} |
| 2107 |
|
|
| 2108 |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
| 2109 |
int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
| 2110 |
#else |
#else |
| 2111 |
static int sljit_emit_sse2_fop1(struct sljit_compiler *compiler, int op, |
static int sljit_emit_sse2_fop1(struct sljit_compiler *compiler, int op, |
| 2112 |
#endif |
#endif |
| 2168 |
} |
} |
| 2169 |
|
|
| 2170 |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
| 2171 |
int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
| 2172 |
#else |
#else |
| 2173 |
static int sljit_emit_sse2_fop2(struct sljit_compiler *compiler, int op, |
static int sljit_emit_sse2_fop2(struct sljit_compiler *compiler, int op, |
| 2174 |
#endif |
#endif |
| 2291 |
} |
} |
| 2292 |
|
|
| 2293 |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
| 2294 |
int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
| 2295 |
#else |
#else |
| 2296 |
static int sljit_emit_fpu_fop1(struct sljit_compiler *compiler, int op, |
static int sljit_emit_fpu_fop1(struct sljit_compiler *compiler, int op, |
| 2297 |
#endif |
#endif |
| 2356 |
} |
} |
| 2357 |
|
|
| 2358 |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
#if !(defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
| 2359 |
int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
| 2360 |
#else |
#else |
| 2361 |
static int sljit_emit_fpu_fop2(struct sljit_compiler *compiler, int op, |
static int sljit_emit_fpu_fop2(struct sljit_compiler *compiler, int op, |
| 2362 |
#endif |
#endif |
| 2434 |
|
|
| 2435 |
#if (defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
#if (defined SLJIT_SSE2_AUTO && SLJIT_SSE2_AUTO) |
| 2436 |
|
|
| 2437 |
int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op, |
| 2438 |
int dst, sljit_w dstw, |
int dst, sljit_w dstw, |
| 2439 |
int src, sljit_w srcw) |
int src, sljit_w srcw) |
| 2440 |
{ |
{ |
| 2444 |
return sljit_emit_fpu_fop1(compiler, op, dst, dstw, src, srcw); |
return sljit_emit_fpu_fop1(compiler, op, dst, dstw, src, srcw); |
| 2445 |
} |
} |
| 2446 |
|
|
| 2447 |
int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op, |
| 2448 |
int dst, sljit_w dstw, |
int dst, sljit_w dstw, |
| 2449 |
int src1, sljit_w src1w, |
int src1, sljit_w src1w, |
| 2450 |
int src2, sljit_w src2w) |
int src2, sljit_w src2w) |
| 2461 |
/* Conditional instructions */ |
/* Conditional instructions */ |
| 2462 |
/* --------------------------------------------------------------------- */ |
/* --------------------------------------------------------------------- */ |
| 2463 |
|
|
| 2464 |
struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler) |
SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler) |
| 2465 |
{ |
{ |
| 2466 |
sljit_ub *buf; |
sljit_ub *buf; |
| 2467 |
struct sljit_label *label; |
struct sljit_label *label; |
| 2490 |
return label; |
return label; |
| 2491 |
} |
} |
| 2492 |
|
|
| 2493 |
struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type) |
SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type) |
| 2494 |
{ |
{ |
| 2495 |
sljit_ub *buf; |
sljit_ub *buf; |
| 2496 |
struct sljit_jump *jump; |
struct sljit_jump *jump; |
| 2527 |
return jump; |
return jump; |
| 2528 |
} |
} |
| 2529 |
|
|
| 2530 |
int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw) |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw) |
| 2531 |
{ |
{ |
| 2532 |
sljit_ub *code; |
sljit_ub *code; |
| 2533 |
struct sljit_jump *jump; |
struct sljit_jump *jump; |
| 2604 |
code = emit_x86_instruction(compiler, 1, 0, 0, src, srcw); |
code = emit_x86_instruction(compiler, 1, 0, 0, src, srcw); |
| 2605 |
FAIL_IF(!code); |
FAIL_IF(!code); |
| 2606 |
*code++ = 0xff; |
*code++ = 0xff; |
| 2607 |
*code |= (type >= SLJIT_CALL0) ? (2 << 3) : (4 << 3); |
*code |= (type >= SLJIT_FAST_CALL) ? (2 << 3) : (4 << 3); |
| 2608 |
} |
} |
| 2609 |
return SLJIT_SUCCESS; |
return SLJIT_SUCCESS; |
| 2610 |
} |
} |
| 2611 |
|
|
| 2612 |
int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type) |
SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type) |
| 2613 |
{ |
{ |
| 2614 |
sljit_ub *buf; |
sljit_ub *buf; |
| 2615 |
sljit_ub cond_set = 0; |
sljit_ub cond_set = 0; |
| 2749 |
|
|
| 2750 |
*buf++ = 0x0f; |
*buf++ = 0x0f; |
| 2751 |
*buf++ = 0xb6; |
*buf++ = 0xb6; |
| 2752 |
if (dst >= SLJIT_GENERAL_REG1 && dst <= SLJIT_NO_REGISTERS) |
if (dst >= SLJIT_SAVED_REG1 && dst <= SLJIT_NO_REGISTERS) |
| 2753 |
*buf = 0xC0 | (reg_map[dst] << 3); |
*buf = 0xC0 | (reg_map[dst] << 3); |
| 2754 |
else { |
else { |
| 2755 |
*buf = 0xC0; |
*buf = 0xC0; |
| 2797 |
return SLJIT_SUCCESS; |
return SLJIT_SUCCESS; |
| 2798 |
} |
} |
| 2799 |
|
|
| 2800 |
struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value) |
SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value) |
| 2801 |
{ |
{ |
| 2802 |
sljit_ub *buf; |
sljit_ub *buf; |
| 2803 |
struct sljit_const *const_; |
struct sljit_const *const_; |
| 2843 |
return const_; |
return const_; |
| 2844 |
} |
} |
| 2845 |
|
|
| 2846 |
void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr) |
SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr) |
| 2847 |
{ |
{ |
| 2848 |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
#if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32) |
| 2849 |
*(sljit_w*)addr = new_addr - (addr + 4); |
*(sljit_w*)addr = new_addr - (addr + 4); |
| 2852 |
#endif |
#endif |
| 2853 |
} |
} |
| 2854 |
|
|
| 2855 |
void sljit_set_const(sljit_uw addr, sljit_w new_constant) |
SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant) |
| 2856 |
{ |
{ |
| 2857 |
*(sljit_w*)addr = new_constant; |
*(sljit_w*)addr = new_constant; |
| 2858 |
} |
} |