2 * Stack-less Just-In-Time compiler
4 * Copyright 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_s32 emit_load_imm64(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
33 inst = (sljit_u8*)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_unaligned_store_sw(inst, imm);
42 static sljit_u8* generate_far_jump_code(struct sljit_jump *jump, sljit_u8 *code_ptr, sljit_s32 type)
44 int short_addr = !(jump->flags & SLJIT_REWRITABLE_JUMP) && !(jump->flags & JUMP_LABEL) && (jump->u.target <= 0xffffffff);
46 /* The relative jump below specialized for this case. */
47 SLJIT_ASSERT(reg_map[TMP_REG2] >= 8);
49 if (type < SLJIT_JUMP) {
51 *code_ptr++ = get_jump_code(type ^ 0x1) - 0x10;
52 *code_ptr++ = short_addr ? (6 + 3) : (10 + 3);
55 *code_ptr++ = short_addr ? REX_B : (REX_W | REX_B);
56 *code_ptr++ = MOV_r_i32 | reg_lmap[TMP_REG2];
57 jump->addr = (sljit_uw)code_ptr;
59 if (jump->flags & JUMP_LABEL)
60 jump->flags |= PATCH_MD;
62 sljit_unaligned_store_s32(code_ptr, (sljit_s32)jump->u.target);
64 sljit_unaligned_store_sw(code_ptr, jump->u.target);
66 code_ptr += short_addr ? sizeof(sljit_s32) : sizeof(sljit_sw);
69 *code_ptr++ = GROUP_FF;
70 *code_ptr++ = MOD_REG | (type >= SLJIT_FAST_CALL ? CALL_rm : JMP_rm) | reg_lmap[TMP_REG2];
75 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
76 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
77 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
79 sljit_s32 args, i, tmp, size, saved_register_size;
83 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
84 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
89 /* Two/four register slots for parameters plus space for xmm6 register if needed. */
90 if (fscratches >= 6 || fsaveds >= 1)
91 compiler->locals_offset = 6 * sizeof(sljit_sw);
93 compiler->locals_offset = ((scratches > 2) ? 4 : 2) * sizeof(sljit_sw);
96 /* Including the return address saved by the call instruction. */
97 saved_register_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
99 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
100 for (i = SLJIT_S0; i >= tmp; i--) {
101 size = reg_map[i] >= 8 ? 2 : 1;
102 inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
107 PUSH_REG(reg_lmap[i]);
110 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
111 size = reg_map[i] >= 8 ? 2 : 1;
112 inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
117 PUSH_REG(reg_lmap[i]);
120 args = get_arg_count(arg_types);
124 inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
133 inst[2] = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x7 /* rdi */;
137 inst[0] = REX_W | REX_R;
139 inst[2] = MOD_REG | (reg_lmap[SLJIT_S1] << 3) | 0x6 /* rsi */;
143 inst[0] = REX_W | REX_R;
145 inst[2] = MOD_REG | (reg_lmap[SLJIT_S2] << 3) | 0x2 /* rdx */;
151 inst[2] = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x1 /* rcx */;
157 inst[2] = MOD_REG | (reg_map[SLJIT_S1] << 3) | 0x2 /* rdx */;
161 inst[0] = REX_W | REX_B;
163 inst[2] = MOD_REG | (reg_map[SLJIT_S2] << 3) | 0x0 /* r8 */;
168 local_size = ((local_size + SLJIT_LOCALS_OFFSET + saved_register_size + 15) & ~15) - saved_register_size;
169 compiler->local_size = local_size;
172 if (local_size > 0) {
173 if (local_size <= 4 * 4096) {
174 if (local_size > 4096)
175 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096);
176 if (local_size > 2 * 4096)
177 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 2);
178 if (local_size > 3 * 4096)
179 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -4096 * 3);
182 EMIT_MOV(compiler, SLJIT_R0, 0, SLJIT_SP, 0);
183 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_IMM, (local_size - 1) >> 12);
185 SLJIT_ASSERT (reg_map[SLJIT_R0] == 0);
187 EMIT_MOV(compiler, TMP_REG2, 0, SLJIT_MEM1(SLJIT_R0), -4096);
188 FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
189 SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_IMM, 4096));
190 FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
191 TMP_REG1, 0, TMP_REG1, 0, SLJIT_IMM, 1));
193 inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
198 inst[1] = (sljit_s8) -19;
201 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_MEM1(SLJIT_SP), -local_size);
205 if (local_size > 0) {
206 FAIL_IF(emit_non_cum_binary(compiler, BINARY_OPCODE(SUB),
207 SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
211 /* Save xmm6 register: movaps [rsp + 0x20], xmm6 */
212 if (fscratches >= 6 || fsaveds >= 1) {
213 inst = (sljit_u8*)ensure_buf(compiler, 1 + 5);
217 sljit_unaligned_store_s32(inst, 0x20247429);
221 return SLJIT_SUCCESS;
224 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
225 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
226 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
228 sljit_s32 saved_register_size;
231 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
232 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
235 /* Two/four register slots for parameters plus space for xmm6 register if needed. */
236 if (fscratches >= 6 || fsaveds >= 1)
237 compiler->locals_offset = 6 * sizeof(sljit_sw);
239 compiler->locals_offset = ((scratches > 2) ? 4 : 2) * sizeof(sljit_sw);
242 /* Including the return address saved by the call instruction. */
243 saved_register_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
244 compiler->local_size = ((local_size + SLJIT_LOCALS_OFFSET + saved_register_size + 15) & ~15) - saved_register_size;
245 return SLJIT_SUCCESS;
248 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
250 sljit_s32 i, tmp, size;
254 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
256 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
259 /* Restore xmm6 register: movaps xmm6, [rsp + 0x20] */
260 if (compiler->fscratches >= 6 || compiler->fsaveds >= 1) {
261 inst = (sljit_u8*)ensure_buf(compiler, 1 + 5);
265 sljit_unaligned_store_s32(inst, 0x20247428);
269 if (compiler->local_size > 0) {
270 if (compiler->local_size <= 127) {
271 inst = (sljit_u8*)ensure_buf(compiler, 1 + 4);
275 *inst++ = GROUP_BINARY_83;
276 *inst++ = MOD_REG | ADD | 4;
277 *inst = compiler->local_size;
280 inst = (sljit_u8*)ensure_buf(compiler, 1 + 7);
284 *inst++ = GROUP_BINARY_81;
285 *inst++ = MOD_REG | ADD | 4;
286 sljit_unaligned_store_s32(inst, compiler->local_size);
290 tmp = compiler->scratches;
291 for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
292 size = reg_map[i] >= 8 ? 2 : 1;
293 inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
298 POP_REG(reg_lmap[i]);
301 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
302 for (i = tmp; i <= SLJIT_S0; i++) {
303 size = reg_map[i] >= 8 ? 2 : 1;
304 inst = (sljit_u8*)ensure_buf(compiler, 1 + size);
309 POP_REG(reg_lmap[i]);
312 inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
316 return SLJIT_SUCCESS;
319 /* --------------------------------------------------------------------- */
321 /* --------------------------------------------------------------------- */
323 static sljit_s32 emit_do_imm32(struct sljit_compiler *compiler, sljit_u8 rex, sljit_u8 opcode, sljit_sw imm)
326 sljit_s32 length = 1 + (rex ? 1 : 0) + sizeof(sljit_s32);
328 inst = (sljit_u8*)ensure_buf(compiler, 1 + length);
334 sljit_unaligned_store_s32(inst, imm);
335 return SLJIT_SUCCESS;
338 static sljit_u8* emit_x86_instruction(struct sljit_compiler *compiler, sljit_s32 size,
339 /* The register or immediate operand. */
340 sljit_s32 a, sljit_sw imma,
341 /* The general operand (not immediate). */
342 sljit_s32 b, sljit_sw immb)
347 sljit_s32 flags = size & ~0xf;
350 /* The immediate operand must be 32 bit. */
351 SLJIT_ASSERT(!(a & SLJIT_IMM) || compiler->mode32 || IS_HALFWORD(imma));
352 /* Both cannot be switched on. */
353 SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
354 /* Size flags not allowed for typed instructions. */
355 SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
356 /* Both size flags cannot be switched on. */
357 SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
358 /* SSE2 and immediate is not possible. */
359 SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
360 SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
361 && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
362 && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
367 if (!compiler->mode32 && !(flags & EX86_NO_REXW))
369 else if (flags & EX86_REX)
372 if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
374 if (flags & EX86_PREF_66)
377 /* Calculate size of b. */
378 inst_size += 1; /* mod r/m byte. */
380 if (!(b & OFFS_REG_MASK)) {
381 if (NOT_HALFWORD(immb)) {
382 PTR_FAIL_IF(emit_load_imm64(compiler, TMP_REG2, immb));
385 b |= TO_OFFS_REG(TMP_REG2);
389 else if (reg_lmap[b & REG_MASK] == 4)
390 b |= TO_OFFS_REG(SLJIT_SP);
393 if ((b & REG_MASK) == SLJIT_UNUSED)
394 inst_size += 1 + sizeof(sljit_s32); /* SIB byte required to avoid RIP based addressing. */
396 if (reg_map[b & REG_MASK] >= 8)
399 if (immb != 0 && (!(b & OFFS_REG_MASK) || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP))) {
400 /* Immediate operand. */
401 if (immb <= 127 && immb >= -128)
402 inst_size += sizeof(sljit_s8);
404 inst_size += sizeof(sljit_s32);
406 else if (reg_lmap[b & REG_MASK] == 5)
407 inst_size += sizeof(sljit_s8);
409 if ((b & OFFS_REG_MASK) != SLJIT_UNUSED) {
410 inst_size += 1; /* SIB byte. */
411 if (reg_map[OFFS_REG(b)] >= 8)
416 else if (!(flags & EX86_SSE2_OP2)) {
420 else if (freg_map[b] >= 8)
424 if (flags & EX86_BIN_INS) {
425 if (imma <= 127 && imma >= -128) {
427 flags |= EX86_BYTE_ARG;
431 else if (flags & EX86_SHIFT_INS) {
432 imma &= compiler->mode32 ? 0x1f : 0x3f;
435 flags |= EX86_BYTE_ARG;
437 } else if (flags & EX86_BYTE_ARG)
439 else if (flags & EX86_HALF_ARG)
440 inst_size += sizeof(short);
442 inst_size += sizeof(sljit_s32);
445 SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
446 /* reg_map[SLJIT_PREF_SHIFT_REG] is less than 8. */
447 if (!(flags & EX86_SSE2_OP1)) {
451 else if (freg_map[a] >= 8)
458 inst = (sljit_u8*)ensure_buf(compiler, 1 + inst_size);
461 /* Encoding the byte. */
463 if (flags & EX86_PREF_F2)
465 if (flags & EX86_PREF_F3)
467 if (flags & EX86_PREF_66)
471 buf_ptr = inst + size;
473 /* Encode mod/rm byte. */
474 if (!(flags & EX86_SHIFT_INS)) {
475 if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
476 *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
480 else if (!(flags & EX86_SSE2_OP1))
481 *buf_ptr = reg_lmap[a] << 3;
483 *buf_ptr = freg_lmap[a] << 3;
488 *inst = GROUP_SHIFT_1;
490 *inst = GROUP_SHIFT_N;
492 *inst = GROUP_SHIFT_CL;
496 if (!(b & SLJIT_MEM))
497 *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_lmap[b] : freg_lmap[b]);
498 else if ((b & REG_MASK) != SLJIT_UNUSED) {
499 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP)) {
500 if (immb != 0 || reg_lmap[b & REG_MASK] == 5) {
501 if (immb <= 127 && immb >= -128)
507 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
508 *buf_ptr++ |= reg_lmap[b & REG_MASK];
511 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3);
514 if (immb != 0 || reg_lmap[b & REG_MASK] == 5) {
515 if (immb <= 127 && immb >= -128)
516 *buf_ptr++ = immb; /* 8 bit displacement. */
518 sljit_unaligned_store_s32(buf_ptr, immb); /* 32 bit displacement. */
519 buf_ptr += sizeof(sljit_s32);
524 if (reg_lmap[b & REG_MASK] == 5)
527 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3) | (immb << 6);
528 if (reg_lmap[b & REG_MASK] == 5)
535 sljit_unaligned_store_s32(buf_ptr, immb); /* 32 bit displacement. */
536 buf_ptr += sizeof(sljit_s32);
540 if (flags & EX86_BYTE_ARG)
542 else if (flags & EX86_HALF_ARG)
543 sljit_unaligned_store_s16(buf_ptr, imma);
544 else if (!(flags & EX86_SHIFT_INS))
545 sljit_unaligned_store_s32(buf_ptr, imma);
548 return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
551 /* --------------------------------------------------------------------- */
552 /* Call / return instructions */
553 /* --------------------------------------------------------------------- */
557 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src_ptr, sljit_sw srcw)
559 sljit_s32 src = src_ptr ? (*src_ptr) : 0;
560 sljit_s32 word_arg_count = 0;
562 SLJIT_ASSERT(reg_map[SLJIT_R1] == 6 && reg_map[SLJIT_R3] == 1 && reg_map[TMP_REG1] == 2);
564 compiler->mode32 = 0;
566 /* Remove return value. */
567 arg_types >>= SLJIT_DEF_SHIFT;
570 if ((arg_types & SLJIT_DEF_MASK) < SLJIT_ARG_TYPE_F32)
572 arg_types >>= SLJIT_DEF_SHIFT;
575 if (word_arg_count == 0)
576 return SLJIT_SUCCESS;
578 if (src & SLJIT_MEM) {
579 ADJUST_LOCAL_OFFSET(src, srcw);
580 EMIT_MOV(compiler, TMP_REG2, 0, src, srcw);
583 else if (src == SLJIT_R2 && word_arg_count >= SLJIT_R2)
586 if (word_arg_count >= 3)
587 EMIT_MOV(compiler, TMP_REG1, 0, SLJIT_R2, 0);
588 return emit_mov(compiler, SLJIT_R2, 0, SLJIT_R0, 0);
593 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src_ptr, sljit_sw srcw)
595 sljit_s32 src = src_ptr ? (*src_ptr) : 0;
596 sljit_s32 arg_count = 0;
597 sljit_s32 word_arg_count = 0;
598 sljit_s32 float_arg_count = 0;
600 sljit_s32 data_trandfer = 0;
601 static sljit_u8 word_arg_regs[5] = { 0, SLJIT_R3, SLJIT_R1, SLJIT_R2, TMP_REG1 };
603 SLJIT_ASSERT(reg_map[SLJIT_R3] == 1 && reg_map[SLJIT_R1] == 2 && reg_map[SLJIT_R2] == 8 && reg_map[TMP_REG1] == 9);
605 compiler->mode32 = 0;
606 arg_types >>= SLJIT_DEF_SHIFT;
609 types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
611 switch (arg_types & SLJIT_DEF_MASK) {
612 case SLJIT_ARG_TYPE_F32:
613 case SLJIT_ARG_TYPE_F64:
617 if (arg_count != float_arg_count)
624 if (arg_count != word_arg_count || arg_count != word_arg_regs[arg_count]) {
627 if (src == word_arg_regs[arg_count]) {
628 EMIT_MOV(compiler, TMP_REG2, 0, src, 0);
635 arg_types >>= SLJIT_DEF_SHIFT;
639 return SLJIT_SUCCESS;
641 if (src & SLJIT_MEM) {
642 ADJUST_LOCAL_OFFSET(src, srcw);
643 EMIT_MOV(compiler, TMP_REG2, 0, src, srcw);
648 switch (types & SLJIT_DEF_MASK) {
649 case SLJIT_ARG_TYPE_F32:
650 if (arg_count != float_arg_count)
651 FAIL_IF(emit_sse2_load(compiler, 1, arg_count, float_arg_count, 0));
655 case SLJIT_ARG_TYPE_F64:
656 if (arg_count != float_arg_count)
657 FAIL_IF(emit_sse2_load(compiler, 0, arg_count, float_arg_count, 0));
662 if (arg_count != word_arg_count || arg_count != word_arg_regs[arg_count])
663 EMIT_MOV(compiler, word_arg_regs[arg_count], 0, word_arg_count, 0);
669 types >>= SLJIT_DEF_SHIFT;
672 return SLJIT_SUCCESS;
677 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
681 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
683 PTR_FAIL_IF(call_with_args(compiler, arg_types, NULL, 0));
685 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
686 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
687 compiler->skip_checks = 1;
690 return sljit_emit_jump(compiler, type);
693 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
695 sljit_s32 src, sljit_sw srcw)
698 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
700 FAIL_IF(call_with_args(compiler, arg_types, &src, srcw));
702 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
703 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
704 compiler->skip_checks = 1;
707 return sljit_emit_ijump(compiler, type, src, srcw);
710 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
715 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
716 ADJUST_LOCAL_OFFSET(dst, dstw);
718 /* For UNUSED dst. Uncommon, but possible. */
719 if (dst == SLJIT_UNUSED)
722 if (FAST_IS_REG(dst)) {
723 if (reg_map[dst] < 8) {
724 inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
727 POP_REG(reg_lmap[dst]);
728 return SLJIT_SUCCESS;
731 inst = (sljit_u8*)ensure_buf(compiler, 1 + 2);
735 POP_REG(reg_lmap[dst]);
736 return SLJIT_SUCCESS;
739 /* REX_W is not necessary (src is not immediate). */
740 compiler->mode32 = 1;
741 inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
744 return SLJIT_SUCCESS;
747 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
752 CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
753 ADJUST_LOCAL_OFFSET(src, srcw);
755 if (FAST_IS_REG(src)) {
756 if (reg_map[src] < 8) {
757 inst = (sljit_u8*)ensure_buf(compiler, 1 + 1 + 1);
761 PUSH_REG(reg_lmap[src]);
764 inst = (sljit_u8*)ensure_buf(compiler, 1 + 2 + 1);
769 PUSH_REG(reg_lmap[src]);
773 /* REX_W is not necessary (src is not immediate). */
774 compiler->mode32 = 1;
775 inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
780 inst = (sljit_u8*)ensure_buf(compiler, 1 + 1);
786 return SLJIT_SUCCESS;
789 /* --------------------------------------------------------------------- */
791 /* --------------------------------------------------------------------- */
793 static sljit_s32 emit_mov_int(struct sljit_compiler *compiler, sljit_s32 sign,
794 sljit_s32 dst, sljit_sw dstw,
795 sljit_s32 src, sljit_sw srcw)
800 compiler->mode32 = 0;
802 if (dst == SLJIT_UNUSED && !(src & SLJIT_MEM))
803 return SLJIT_SUCCESS; /* Empty instruction. */
805 if (src & SLJIT_IMM) {
806 if (FAST_IS_REG(dst)) {
807 if (sign || ((sljit_uw)srcw <= 0x7fffffff)) {
808 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_s32)srcw, dst, dstw);
811 return SLJIT_SUCCESS;
813 return emit_load_imm64(compiler, dst, srcw);
815 compiler->mode32 = 1;
816 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_s32)srcw, dst, dstw);
819 compiler->mode32 = 0;
820 return SLJIT_SUCCESS;
823 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
825 if ((dst & SLJIT_MEM) && FAST_IS_REG(src))
829 inst = emit_x86_instruction(compiler, 1, dst_r, 0, src, srcw);
831 *inst++ = MOVSXD_r_rm;
833 compiler->mode32 = 1;
834 FAIL_IF(emit_mov(compiler, dst_r, 0, src, srcw));
835 compiler->mode32 = 0;
839 if (dst & SLJIT_MEM) {
840 compiler->mode32 = 1;
841 inst = emit_x86_instruction(compiler, 1, dst_r, 0, dst, dstw);
844 compiler->mode32 = 0;
847 return SLJIT_SUCCESS;