2 * Stack-less Just-In-Time compiler
4 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 /* x86 64-bit arch dependent functions. */
29 static sljit_si emit_load_imm64(struct sljit_compiler *compiler, sljit_si reg, sljit_sw imm)
33 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2 + sizeof(sljit_sw));
35 INC_SIZE(2 + sizeof(sljit_sw));
36 *inst++ = REX_W | ((reg_map[reg] <= 7) ? 0 : REX_B);
37 *inst++ = MOV_r_i32 + (reg_map[reg] & 0x7);
38 *(sljit_sw*)inst = imm;
42 static sljit_ub* generate_far_jump_code(struct sljit_jump *jump, sljit_ub *code_ptr, sljit_si type)
44 if (type < SLJIT_JUMP) {
46 *code_ptr++ = get_jump_code(type ^ 0x1) - 0x10;
50 SLJIT_COMPILE_ASSERT(reg_map[TMP_REG3] == 9, tmp3_is_9_first);
51 *code_ptr++ = REX_W | REX_B;
52 *code_ptr++ = MOV_r_i32 + 1;
53 jump->addr = (sljit_uw)code_ptr;
55 if (jump->flags & JUMP_LABEL)
56 jump->flags |= PATCH_MD;
58 *(sljit_sw*)code_ptr = jump->u.target;
60 code_ptr += sizeof(sljit_sw);
62 *code_ptr++ = GROUP_FF;
63 *code_ptr++ = (type >= SLJIT_FAST_CALL) ? (MOD_REG | CALL_rm | 1) : (MOD_REG | JMP_rm | 1);
68 static sljit_ub* generate_fixed_jump(sljit_ub *code_ptr, sljit_sw addr, sljit_si type)
70 sljit_sw delta = addr - ((sljit_sw)code_ptr + 1 + sizeof(sljit_si));
72 if (delta <= HALFWORD_MAX && delta >= HALFWORD_MIN) {
73 *code_ptr++ = (type == 2) ? CALL_i32 : JMP_i32;
74 *(sljit_sw*)code_ptr = delta;
77 SLJIT_COMPILE_ASSERT(reg_map[TMP_REG3] == 9, tmp3_is_9_second);
78 *code_ptr++ = REX_W | REX_B;
79 *code_ptr++ = MOV_r_i32 + 1;
80 *(sljit_sw*)code_ptr = addr;
81 code_ptr += sizeof(sljit_sw);
83 *code_ptr++ = GROUP_FF;
84 *code_ptr++ = (type == 2) ? (MOD_REG | CALL_rm | 1) : (MOD_REG | JMP_rm | 1);
90 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)
92 sljit_si size, pushed_size;
96 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
98 compiler->scratches = scratches;
99 compiler->saveds = saveds;
100 compiler->flags_saved = 0;
101 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
102 compiler->logical_local_size = local_size;
106 /* Including the return address saved by the call instruction. */
107 pushed_size = (saveds + 1) * sizeof(sljit_sw);
114 if (scratches >= 5) {
116 pushed_size += sizeof(sljit_sw);
121 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
126 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_EREG2] >= 8, saved_ereg2_is_hireg);
128 PUSH_REG(reg_lmap[SLJIT_SAVED_EREG2]);
131 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_EREG1] >= 8, saved_ereg1_is_hireg);
133 PUSH_REG(reg_lmap[SLJIT_SAVED_EREG1]);
137 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_REG3] >= 8, saved_reg3_is_hireg);
140 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_REG3] < 8, saved_reg3_is_loreg);
142 PUSH_REG(reg_lmap[SLJIT_SAVED_REG3]);
146 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_REG2] >= 8, saved_reg2_is_hireg);
149 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_REG2] < 8, saved_reg2_is_loreg);
151 PUSH_REG(reg_lmap[SLJIT_SAVED_REG2]);
154 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SAVED_REG1] < 8, saved_reg1_is_loreg);
155 PUSH_REG(reg_lmap[SLJIT_SAVED_REG1]);
158 if (scratches >= 5) {
159 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_TEMPORARY_EREG2] >= 8, temporary_ereg2_is_hireg);
161 PUSH_REG(reg_lmap[SLJIT_TEMPORARY_EREG2]);
169 *inst++ = MOD_REG | (reg_map[SLJIT_SAVED_REG1] << 3) | 0x7 /* rdi */;
172 *inst++ = REX_W | REX_R;
174 *inst++ = MOD_REG | (reg_lmap[SLJIT_SAVED_REG2] << 3) | 0x6 /* rsi */;
177 *inst++ = REX_W | REX_R;
179 *inst++ = MOD_REG | (reg_lmap[SLJIT_SAVED_REG3] << 3) | 0x2 /* rdx */;
185 *inst++ = MOD_REG | (reg_map[SLJIT_SAVED_REG1] << 3) | 0x1 /* rcx */;
190 *inst++ = MOD_REG | (reg_map[SLJIT_SAVED_REG2] << 3) | 0x2 /* rdx */;
193 *inst++ = REX_W | REX_B;
195 *inst++ = MOD_REG | (reg_map[SLJIT_SAVED_REG3] << 3) | 0x0 /* r8 */;
200 local_size = ((local_size + FIXED_LOCALS_OFFSET + pushed_size + 16 - 1) & ~(16 - 1)) - pushed_size;
201 compiler->local_size = local_size;
203 if (local_size > 1024) {
204 /* Allocate stack for the callback, which grows the stack. */
205 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4 + (3 + sizeof(sljit_si)));
207 INC_SIZE(4 + (3 + sizeof(sljit_si)));
209 *inst++ = GROUP_BINARY_83;
210 *inst++ = MOD_REG | SUB | 4;
211 /* Pushed size must be divisible by 8. */
212 SLJIT_ASSERT(!(pushed_size & 0x7));
213 if (pushed_size & 0x8) {
214 *inst++ = 5 * sizeof(sljit_sw);
215 local_size -= 5 * sizeof(sljit_sw);
217 *inst++ = 4 * sizeof(sljit_sw);
218 local_size -= 4 * sizeof(sljit_sw);
220 /* Second instruction */
221 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SCRATCH_REG1] < 8, temporary_reg1_is_loreg);
223 *inst++ = MOV_rm_i32;
224 *inst++ = MOD_REG | reg_lmap[SLJIT_SCRATCH_REG1];
225 *(sljit_si*)inst = local_size;
226 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
227 compiler->skip_checks = 1;
229 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_grow_stack)));
232 SLJIT_ASSERT(local_size > 0);
233 if (local_size <= 127) {
234 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
238 *inst++ = GROUP_BINARY_83;
239 *inst++ = MOD_REG | SUB | 4;
240 *inst++ = local_size;
243 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
247 *inst++ = GROUP_BINARY_81;
248 *inst++ = MOD_REG | SUB | 4;
249 *(sljit_si*)inst = local_size;
250 inst += sizeof(sljit_si);
253 /* Save xmm6 with MOVAPS instruction. */
254 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5);
258 *(sljit_si*)inst = 0x20247429;
261 return SLJIT_SUCCESS;
264 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)
266 sljit_si pushed_size;
269 check_sljit_set_context(compiler, args, scratches, saveds, local_size);
271 compiler->scratches = scratches;
272 compiler->saveds = saveds;
273 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
274 compiler->logical_local_size = local_size;
277 /* Including the return address saved by the call instruction. */
278 pushed_size = (saveds + 1) * sizeof(sljit_sw);
281 pushed_size += sizeof(sljit_sw);
283 compiler->local_size = ((local_size + FIXED_LOCALS_OFFSET + pushed_size + 16 - 1) & ~(16 - 1)) - pushed_size;
286 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
292 check_sljit_emit_return(compiler, op, src, srcw);
294 compiler->flags_saved = 0;
295 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
298 /* Restore xmm6 with MOVAPS instruction. */
299 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5);
303 *(sljit_si*)inst = 0x20247428;
305 SLJIT_ASSERT(compiler->local_size > 0);
306 if (compiler->local_size <= 127) {
307 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
311 *inst++ = GROUP_BINARY_83;
312 *inst++ = MOD_REG | ADD | 4;
313 *inst = compiler->local_size;
316 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
320 *inst++ = GROUP_BINARY_81;
321 *inst++ = MOD_REG | ADD | 4;
322 *(sljit_si*)inst = compiler->local_size;
325 size = 1 + compiler->saveds;
327 if (compiler->saveds >= 2)
328 size += compiler->saveds - 1;
330 if (compiler->saveds >= 4)
331 size += compiler->saveds - 3;
332 if (compiler->scratches >= 5)
335 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
341 if (compiler->scratches >= 5) {
343 POP_REG(reg_lmap[SLJIT_TEMPORARY_EREG2]);
346 if (compiler->saveds >= 1)
347 POP_REG(reg_map[SLJIT_SAVED_REG1]);
348 if (compiler->saveds >= 2) {
352 POP_REG(reg_lmap[SLJIT_SAVED_REG2]);
354 if (compiler->saveds >= 3) {
358 POP_REG(reg_lmap[SLJIT_SAVED_REG3]);
360 if (compiler->saveds >= 4) {
362 POP_REG(reg_lmap[SLJIT_SAVED_EREG1]);
364 if (compiler->saveds >= 5) {
366 POP_REG(reg_lmap[SLJIT_SAVED_EREG2]);
370 return SLJIT_SUCCESS;
373 /* --------------------------------------------------------------------- */
375 /* --------------------------------------------------------------------- */
377 static sljit_si emit_do_imm32(struct sljit_compiler *compiler, sljit_ub rex, sljit_ub opcode, sljit_sw imm)
380 sljit_si length = 1 + (rex ? 1 : 0) + sizeof(sljit_si);
382 inst = (sljit_ub*)ensure_buf(compiler, 1 + length);
388 *(sljit_si*)inst = imm;
389 return SLJIT_SUCCESS;
392 static sljit_ub* emit_x86_instruction(struct sljit_compiler *compiler, sljit_si size,
393 /* The register or immediate operand. */
394 sljit_si a, sljit_sw imma,
395 /* The general operand (not immediate). */
396 sljit_si b, sljit_sw immb)
401 sljit_si flags = size & ~0xf;
404 /* The immediate operand must be 32 bit. */
405 SLJIT_ASSERT(!(a & SLJIT_IMM) || compiler->mode32 || IS_HALFWORD(imma));
406 /* Both cannot be switched on. */
407 SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
408 /* Size flags not allowed for typed instructions. */
409 SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
410 /* Both size flags cannot be switched on. */
411 SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
412 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
413 /* SSE2 and immediate is not possible. */
414 SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
415 SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
416 && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
417 && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
423 if ((b & SLJIT_MEM) && !(b & OFFS_REG_MASK) && NOT_HALFWORD(immb)) {
424 if (emit_load_imm64(compiler, TMP_REG3, immb))
428 b |= TO_OFFS_REG(TMP_REG3);
433 if (!compiler->mode32 && !(flags & EX86_NO_REXW))
435 else if (flags & EX86_REX)
438 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
439 if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
442 if (flags & EX86_PREF_66)
445 /* Calculate size of b. */
446 inst_size += 1; /* mod r/m byte. */
448 if ((b & REG_MASK) == SLJIT_UNUSED)
449 inst_size += 1 + sizeof(sljit_si); /* SIB byte required to avoid RIP based addressing. */
451 if (reg_map[b & REG_MASK] >= 8)
453 if (immb != 0 && !(b & OFFS_REG_MASK)) {
454 /* Immediate operand. */
455 if (immb <= 127 && immb >= -128)
456 inst_size += sizeof(sljit_sb);
458 inst_size += sizeof(sljit_si);
462 if ((b & REG_MASK) == SLJIT_LOCALS_REG && !(b & OFFS_REG_MASK))
463 b |= TO_OFFS_REG(SLJIT_LOCALS_REG);
465 if ((b & OFFS_REG_MASK) != SLJIT_UNUSED) {
466 inst_size += 1; /* SIB byte. */
467 if (reg_map[OFFS_REG(b)] >= 8)
471 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
472 else if (!(flags & EX86_SSE2) && reg_map[b] >= 8)
475 else if (reg_map[b] >= 8)
480 if (flags & EX86_BIN_INS) {
481 if (imma <= 127 && imma >= -128) {
483 flags |= EX86_BYTE_ARG;
487 else if (flags & EX86_SHIFT_INS) {
488 imma &= compiler->mode32 ? 0x1f : 0x3f;
491 flags |= EX86_BYTE_ARG;
493 } else if (flags & EX86_BYTE_ARG)
495 else if (flags & EX86_HALF_ARG)
496 inst_size += sizeof(short);
498 inst_size += sizeof(sljit_si);
501 SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
502 /* reg_map[SLJIT_PREF_SHIFT_REG] is less than 8. */
503 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
504 if (!(flags & EX86_SSE2) && reg_map[a] >= 8)
515 inst = (sljit_ub*)ensure_buf(compiler, 1 + inst_size);
518 /* Encoding the byte. */
520 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
521 if (flags & EX86_PREF_F2)
523 if (flags & EX86_PREF_F3)
526 if (flags & EX86_PREF_66)
530 buf_ptr = inst + size;
532 /* Encode mod/rm byte. */
533 if (!(flags & EX86_SHIFT_INS)) {
534 if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
535 *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
537 if ((a & SLJIT_IMM) || (a == 0))
539 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
540 else if (!(flags & EX86_SSE2))
541 *buf_ptr = reg_lmap[a] << 3;
546 *buf_ptr = reg_lmap[a] << 3;
552 *inst = GROUP_SHIFT_1;
554 *inst = GROUP_SHIFT_N;
556 *inst = GROUP_SHIFT_CL;
560 if (!(b & SLJIT_MEM))
561 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
562 *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2)) ? reg_lmap[b] : b);
564 *buf_ptr++ |= MOD_REG + reg_lmap[b];
566 else if ((b & REG_MASK) != SLJIT_UNUSED) {
567 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_LOCALS_REG)) {
569 if (immb <= 127 && immb >= -128)
575 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
576 *buf_ptr++ |= reg_lmap[b & REG_MASK];
579 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3);
583 if (immb <= 127 && immb >= -128)
584 *buf_ptr++ = immb; /* 8 bit displacement. */
586 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
587 buf_ptr += sizeof(sljit_si);
593 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3) | (immb << 6);
599 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
600 buf_ptr += sizeof(sljit_si);
604 if (flags & EX86_BYTE_ARG)
606 else if (flags & EX86_HALF_ARG)
607 *(short*)buf_ptr = imma;
608 else if (!(flags & EX86_SHIFT_INS))
609 *(sljit_si*)buf_ptr = imma;
612 return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
615 /* --------------------------------------------------------------------- */
616 /* Call / return instructions */
617 /* --------------------------------------------------------------------- */
619 static SLJIT_INLINE sljit_si call_with_args(struct sljit_compiler *compiler, sljit_si type)
624 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SCRATCH_REG2] == 6 && reg_map[SLJIT_SCRATCH_REG1] < 8 && reg_map[SLJIT_SCRATCH_REG3] < 8, args_registers);
626 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
628 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
629 if (type >= SLJIT_CALL3) {
632 *inst++ = MOD_REG | (0x2 /* rdx */ << 3) | reg_lmap[SLJIT_SCRATCH_REG3];
636 *inst++ = MOD_REG | (0x7 /* rdi */ << 3) | reg_lmap[SLJIT_SCRATCH_REG1];
638 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_SCRATCH_REG2] == 2 && reg_map[SLJIT_SCRATCH_REG1] < 8 && reg_map[SLJIT_SCRATCH_REG3] < 8, args_registers);
640 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
642 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
643 if (type >= SLJIT_CALL3) {
644 *inst++ = REX_W | REX_R;
646 *inst++ = MOD_REG | (0x0 /* r8 */ << 3) | reg_lmap[SLJIT_SCRATCH_REG3];
650 *inst++ = MOD_REG | (0x1 /* rcx */ << 3) | reg_lmap[SLJIT_SCRATCH_REG1];
652 return SLJIT_SUCCESS;
655 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
660 check_sljit_emit_fast_enter(compiler, dst, dstw);
661 ADJUST_LOCAL_OFFSET(dst, dstw);
663 /* For UNUSED dst. Uncommon, but possible. */
664 if (dst == SLJIT_UNUSED)
667 if (FAST_IS_REG(dst)) {
668 if (reg_map[dst] < 8) {
669 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
672 POP_REG(reg_lmap[dst]);
673 return SLJIT_SUCCESS;
676 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2);
680 POP_REG(reg_lmap[dst]);
681 return SLJIT_SUCCESS;
684 /* REX_W is not necessary (src is not immediate). */
685 compiler->mode32 = 1;
686 inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
689 return SLJIT_SUCCESS;
692 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
697 check_sljit_emit_fast_return(compiler, src, srcw);
698 ADJUST_LOCAL_OFFSET(src, srcw);
700 if ((src & SLJIT_IMM) && NOT_HALFWORD(srcw)) {
701 FAIL_IF(emit_load_imm64(compiler, TMP_REG1, srcw));
705 if (FAST_IS_REG(src)) {
706 if (reg_map[src] < 8) {
707 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1 + 1);
711 PUSH_REG(reg_lmap[src]);
714 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2 + 1);
719 PUSH_REG(reg_lmap[src]);
722 else if (src & SLJIT_MEM) {
723 /* REX_W is not necessary (src is not immediate). */
724 compiler->mode32 = 1;
725 inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
730 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
735 SLJIT_ASSERT(IS_HALFWORD(srcw));
737 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5 + 1);
742 *(sljit_si*)inst = srcw;
743 inst += sizeof(sljit_si);
747 return SLJIT_SUCCESS;
751 /* --------------------------------------------------------------------- */
753 /* --------------------------------------------------------------------- */
755 static sljit_si emit_mov_int(struct sljit_compiler *compiler, sljit_si sign,
756 sljit_si dst, sljit_sw dstw,
757 sljit_si src, sljit_sw srcw)
762 compiler->mode32 = 0;
764 if (dst == SLJIT_UNUSED && !(src & SLJIT_MEM))
765 return SLJIT_SUCCESS; /* Empty instruction. */
767 if (src & SLJIT_IMM) {
768 if (FAST_IS_REG(dst)) {
769 if (sign || ((sljit_uw)srcw <= 0x7fffffff)) {
770 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_si)srcw, dst, dstw);
773 return SLJIT_SUCCESS;
775 return emit_load_imm64(compiler, dst, srcw);
777 compiler->mode32 = 1;
778 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_si)srcw, dst, dstw);
781 compiler->mode32 = 0;
782 return SLJIT_SUCCESS;
785 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
787 if ((dst & SLJIT_MEM) && FAST_IS_REG(src))
791 inst = emit_x86_instruction(compiler, 1, dst_r, 0, src, srcw);
793 *inst++ = MOVSXD_r_rm;
795 compiler->mode32 = 1;
796 FAIL_IF(emit_mov(compiler, dst_r, 0, src, srcw));
797 compiler->mode32 = 0;
801 if (dst & SLJIT_MEM) {
802 compiler->mode32 = 1;
803 inst = emit_x86_instruction(compiler, 1, dst_r, 0, dst, dstw);
806 compiler->mode32 = 0;
809 return SLJIT_SUCCESS;