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 32-bit arch dependent functions. */
29 static sljit_s32
emit_do_imm(struct sljit_compiler
*compiler
, sljit_u8 opcode
, sljit_sw imm
)
33 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 1 + sizeof(sljit_sw
));
35 INC_SIZE(1 + sizeof(sljit_sw
));
37 sljit_unaligned_store_sw(inst
, imm
);
41 static sljit_u8
* generate_far_jump_code(struct sljit_jump
*jump
, sljit_u8
*code_ptr
, sljit_s32 type
, sljit_sw executable_offset
)
43 if (type
== SLJIT_JUMP
) {
44 *code_ptr
++ = JMP_i32
;
47 else if (type
>= SLJIT_FAST_CALL
) {
48 *code_ptr
++ = CALL_i32
;
52 *code_ptr
++ = GROUP_0F
;
53 *code_ptr
++ = get_jump_code(type
);
57 if (jump
->flags
& JUMP_LABEL
)
58 jump
->flags
|= PATCH_MW
;
60 sljit_unaligned_store_sw(code_ptr
, jump
->u
.target
- (jump
->addr
+ 4) - (sljit_uw
)executable_offset
);
66 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
67 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
68 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
74 CHECK(check_sljit_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
75 set_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
77 compiler
->args
= args
;
78 compiler
->flags_saved
= 0;
80 size
= 1 + (scratches
> 7 ? (scratches
- 7) : 0) + (saveds
<= 3 ? saveds
: 3);
81 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
82 size
+= (args
> 0 ? (args
* 2) : 0) + (args
> 2 ? 2 : 0);
84 size
+= (args
> 0 ? (2 + args
* 3) : 0);
86 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + size
);
90 PUSH_REG(reg_map
[TMP_REG1
]);
91 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
94 *inst
++ = MOD_REG
| (reg_map
[TMP_REG1
] << 3) | 0x4 /* esp */;
97 if (saveds
> 2 || scratches
> 7)
98 PUSH_REG(reg_map
[SLJIT_S2
]);
99 if (saveds
> 1 || scratches
> 8)
100 PUSH_REG(reg_map
[SLJIT_S1
]);
101 if (saveds
> 0 || scratches
> 9)
102 PUSH_REG(reg_map
[SLJIT_S0
]);
104 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
107 *inst
++ = MOD_REG
| (reg_map
[SLJIT_S0
] << 3) | reg_map
[SLJIT_R2
];
111 *inst
++ = MOD_REG
| (reg_map
[SLJIT_S1
] << 3) | reg_map
[SLJIT_R1
];
115 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_S2
] << 3) | 0x4 /* esp */;
117 *inst
++ = sizeof(sljit_sw
) * (3 + 2); /* saveds >= 3 as well. */
122 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_S0
] << 3) | reg_map
[TMP_REG1
];
123 *inst
++ = sizeof(sljit_sw
) * 2;
127 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_S1
] << 3) | reg_map
[TMP_REG1
];
128 *inst
++ = sizeof(sljit_sw
) * 3;
132 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_S2
] << 3) | reg_map
[TMP_REG1
];
133 *inst
++ = sizeof(sljit_sw
) * 4;
137 SLJIT_COMPILE_ASSERT(SLJIT_LOCALS_OFFSET
>= (2 + 4) * sizeof(sljit_uw
), require_at_least_two_words
);
138 #if defined(__APPLE__)
139 /* Ignore pushed registers and SLJIT_LOCALS_OFFSET when computing the aligned local size. */
140 saveds
= (2 + (scratches
> 7 ? (scratches
- 7) : 0) + (saveds
<= 3 ? saveds
: 3)) * sizeof(sljit_uw
);
141 local_size
= ((SLJIT_LOCALS_OFFSET
+ saveds
+ local_size
+ 15) & ~15) - saveds
;
143 if (options
& SLJIT_DOUBLE_ALIGNMENT
) {
144 local_size
= SLJIT_LOCALS_OFFSET
+ ((local_size
+ 7) & ~7);
146 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 17);
151 inst
[1] = MOD_REG
| (reg_map
[TMP_REG1
] << 3) | reg_map
[SLJIT_SP
];
153 inst
[3] = MOD_REG
| (0 << 3) | reg_map
[SLJIT_SP
];
154 sljit_unaligned_store_sw(inst
+ 4, 0x4);
157 inst
[10] = GROUP_BINARY_81
;
158 inst
[11] = MOD_REG
| (5 << 3) | reg_map
[SLJIT_SP
];
159 sljit_unaligned_store_sw(inst
+ 12, 0x4);
160 inst
[16] = PUSH_r
+ reg_map
[TMP_REG1
];
163 local_size
= SLJIT_LOCALS_OFFSET
+ ((local_size
+ 3) & ~3);
166 compiler
->local_size
= local_size
;
168 if (local_size
> 1024) {
169 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
170 FAIL_IF(emit_do_imm(compiler
, MOV_r_i32
+ reg_map
[SLJIT_R0
], local_size
));
172 local_size
-= SLJIT_LOCALS_OFFSET
;
173 FAIL_IF(emit_do_imm(compiler
, MOV_r_i32
+ reg_map
[SLJIT_R0
], local_size
));
174 FAIL_IF(emit_non_cum_binary(compiler
, SUB_r_rm
, SUB_rm_r
, SUB
, SUB_EAX_i32
,
175 SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, SLJIT_LOCALS_OFFSET
));
177 FAIL_IF(sljit_emit_ijump(compiler
, SLJIT_CALL1
, SLJIT_IMM
, SLJIT_FUNC_OFFSET(sljit_grow_stack
)));
181 SLJIT_ASSERT(local_size
> 0);
182 return emit_non_cum_binary(compiler
, SUB_r_rm
, SUB_rm_r
, SUB
, SUB_EAX_i32
,
183 SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, local_size
);
186 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
187 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
188 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
191 CHECK(check_sljit_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
192 set_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
194 compiler
->args
= args
;
196 #if defined(__APPLE__)
197 saveds
= (2 + (scratches
> 7 ? (scratches
- 7) : 0) + (saveds
<= 3 ? saveds
: 3)) * sizeof(sljit_uw
);
198 compiler
->local_size
= ((SLJIT_LOCALS_OFFSET
+ saveds
+ local_size
+ 15) & ~15) - saveds
;
200 if (options
& SLJIT_DOUBLE_ALIGNMENT
)
201 compiler
->local_size
= SLJIT_LOCALS_OFFSET
+ ((local_size
+ 7) & ~7);
203 compiler
->local_size
= SLJIT_LOCALS_OFFSET
+ ((local_size
+ 3) & ~3);
205 return SLJIT_SUCCESS
;
208 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
214 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
215 SLJIT_ASSERT(compiler
->args
>= 0);
217 compiler
->flags_saved
= 0;
218 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
220 SLJIT_ASSERT(compiler
->local_size
> 0);
221 FAIL_IF(emit_cum_binary(compiler
, ADD_r_rm
, ADD_rm_r
, ADD
, ADD_EAX_i32
,
222 SLJIT_SP
, 0, SLJIT_SP
, 0, SLJIT_IMM
, compiler
->local_size
));
224 #if !defined(__APPLE__)
225 if (compiler
->options
& SLJIT_DOUBLE_ALIGNMENT
) {
226 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 3);
231 inst
[1] = (reg_map
[SLJIT_SP
] << 3) | 0x4 /* SIB */;
232 inst
[2] = (4 << 3) | reg_map
[SLJIT_SP
];
236 size
= 2 + (compiler
->scratches
> 7 ? (compiler
->scratches
- 7) : 0) +
237 (compiler
->saveds
<= 3 ? compiler
->saveds
: 3);
238 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
239 if (compiler
->args
> 2)
242 if (compiler
->args
> 0)
245 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + size
);
250 if (compiler
->saveds
> 0 || compiler
->scratches
> 9)
251 POP_REG(reg_map
[SLJIT_S0
]);
252 if (compiler
->saveds
> 1 || compiler
->scratches
> 8)
253 POP_REG(reg_map
[SLJIT_S1
]);
254 if (compiler
->saveds
> 2 || compiler
->scratches
> 7)
255 POP_REG(reg_map
[SLJIT_S2
]);
256 POP_REG(reg_map
[TMP_REG1
]);
257 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
258 if (compiler
->args
> 2)
259 RET_I16(sizeof(sljit_sw
));
266 return SLJIT_SUCCESS
;
269 /* --------------------------------------------------------------------- */
271 /* --------------------------------------------------------------------- */
273 /* Size contains the flags as well. */
274 static sljit_u8
* emit_x86_instruction(struct sljit_compiler
*compiler
, sljit_s32 size
,
275 /* The register or immediate operand. */
276 sljit_s32 a
, sljit_sw imma
,
277 /* The general operand (not immediate). */
278 sljit_s32 b
, sljit_sw immb
)
282 sljit_s32 flags
= size
& ~0xf;
285 /* Both cannot be switched on. */
286 SLJIT_ASSERT((flags
& (EX86_BIN_INS
| EX86_SHIFT_INS
)) != (EX86_BIN_INS
| EX86_SHIFT_INS
));
287 /* Size flags not allowed for typed instructions. */
288 SLJIT_ASSERT(!(flags
& (EX86_BIN_INS
| EX86_SHIFT_INS
)) || (flags
& (EX86_BYTE_ARG
| EX86_HALF_ARG
)) == 0);
289 /* Both size flags cannot be switched on. */
290 SLJIT_ASSERT((flags
& (EX86_BYTE_ARG
| EX86_HALF_ARG
)) != (EX86_BYTE_ARG
| EX86_HALF_ARG
));
291 /* SSE2 and immediate is not possible. */
292 SLJIT_ASSERT(!(a
& SLJIT_IMM
) || !(flags
& EX86_SSE2
));
293 SLJIT_ASSERT((flags
& (EX86_PREF_F2
| EX86_PREF_F3
)) != (EX86_PREF_F2
| EX86_PREF_F3
)
294 && (flags
& (EX86_PREF_F2
| EX86_PREF_66
)) != (EX86_PREF_F2
| EX86_PREF_66
)
295 && (flags
& (EX86_PREF_F3
| EX86_PREF_66
)) != (EX86_PREF_F3
| EX86_PREF_66
));
300 if (flags
& (EX86_PREF_F2
| EX86_PREF_F3
))
302 if (flags
& EX86_PREF_66
)
305 /* Calculate size of b. */
306 inst_size
+= 1; /* mod r/m byte. */
308 if ((b
& REG_MASK
) == SLJIT_UNUSED
)
309 inst_size
+= sizeof(sljit_sw
);
310 else if (immb
!= 0 && !(b
& OFFS_REG_MASK
)) {
311 /* Immediate operand. */
312 if (immb
<= 127 && immb
>= -128)
313 inst_size
+= sizeof(sljit_s8
);
315 inst_size
+= sizeof(sljit_sw
);
318 if ((b
& REG_MASK
) == SLJIT_SP
&& !(b
& OFFS_REG_MASK
))
319 b
|= TO_OFFS_REG(SLJIT_SP
);
321 if ((b
& OFFS_REG_MASK
) != SLJIT_UNUSED
)
322 inst_size
+= 1; /* SIB byte. */
325 /* Calculate size of a. */
327 if (flags
& EX86_BIN_INS
) {
328 if (imma
<= 127 && imma
>= -128) {
330 flags
|= EX86_BYTE_ARG
;
334 else if (flags
& EX86_SHIFT_INS
) {
338 flags
|= EX86_BYTE_ARG
;
340 } else if (flags
& EX86_BYTE_ARG
)
342 else if (flags
& EX86_HALF_ARG
)
343 inst_size
+= sizeof(short);
345 inst_size
+= sizeof(sljit_sw
);
348 SLJIT_ASSERT(!(flags
& EX86_SHIFT_INS
) || a
== SLJIT_PREF_SHIFT_REG
);
350 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + inst_size
);
353 /* Encoding the byte. */
355 if (flags
& EX86_PREF_F2
)
357 if (flags
& EX86_PREF_F3
)
359 if (flags
& EX86_PREF_66
)
362 buf_ptr
= inst
+ size
;
364 /* Encode mod/rm byte. */
365 if (!(flags
& EX86_SHIFT_INS
)) {
366 if ((flags
& EX86_BIN_INS
) && (a
& SLJIT_IMM
))
367 *inst
= (flags
& EX86_BYTE_ARG
) ? GROUP_BINARY_83
: GROUP_BINARY_81
;
369 if ((a
& SLJIT_IMM
) || (a
== 0))
371 else if (!(flags
& EX86_SSE2_OP1
))
372 *buf_ptr
= reg_map
[a
] << 3;
379 *inst
= GROUP_SHIFT_1
;
381 *inst
= GROUP_SHIFT_N
;
383 *inst
= GROUP_SHIFT_CL
;
387 if (!(b
& SLJIT_MEM
))
388 *buf_ptr
++ |= MOD_REG
+ ((!(flags
& EX86_SSE2_OP2
)) ? reg_map
[b
] : b
);
389 else if ((b
& REG_MASK
) != SLJIT_UNUSED
) {
390 if ((b
& OFFS_REG_MASK
) == SLJIT_UNUSED
|| (b
& OFFS_REG_MASK
) == TO_OFFS_REG(SLJIT_SP
)) {
392 if (immb
<= 127 && immb
>= -128)
398 if ((b
& OFFS_REG_MASK
) == SLJIT_UNUSED
)
399 *buf_ptr
++ |= reg_map
[b
& REG_MASK
];
402 *buf_ptr
++ = reg_map
[b
& REG_MASK
] | (reg_map
[OFFS_REG(b
)] << 3);
406 if (immb
<= 127 && immb
>= -128)
407 *buf_ptr
++ = immb
; /* 8 bit displacement. */
409 sljit_unaligned_store_sw(buf_ptr
, immb
); /* 32 bit displacement. */
410 buf_ptr
+= sizeof(sljit_sw
);
416 *buf_ptr
++ = reg_map
[b
& REG_MASK
] | (reg_map
[OFFS_REG(b
)] << 3) | (immb
<< 6);
421 sljit_unaligned_store_sw(buf_ptr
, immb
); /* 32 bit displacement. */
422 buf_ptr
+= sizeof(sljit_sw
);
426 if (flags
& EX86_BYTE_ARG
)
428 else if (flags
& EX86_HALF_ARG
)
429 sljit_unaligned_store_s16(buf_ptr
, imma
);
430 else if (!(flags
& EX86_SHIFT_INS
))
431 sljit_unaligned_store_sw(buf_ptr
, imma
);
434 return !(flags
& EX86_SHIFT_INS
) ? inst
: (inst
+ 1);
437 /* --------------------------------------------------------------------- */
438 /* Call / return instructions */
439 /* --------------------------------------------------------------------- */
441 static SLJIT_INLINE sljit_s32
call_with_args(struct sljit_compiler
*compiler
, sljit_s32 type
)
445 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
446 inst
= (sljit_u8
*)ensure_buf(compiler
, type
>= SLJIT_CALL3
? 1 + 2 + 1 : 1 + 2);
448 INC_SIZE(type
>= SLJIT_CALL3
? 2 + 1 : 2);
450 if (type
>= SLJIT_CALL3
)
451 PUSH_REG(reg_map
[SLJIT_R2
]);
453 *inst
++ = MOD_REG
| (reg_map
[SLJIT_R2
] << 3) | reg_map
[SLJIT_R0
];
455 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 4 * (type
- SLJIT_CALL0
));
457 INC_SIZE(4 * (type
- SLJIT_CALL0
));
460 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_R0
] << 3) | 0x4 /* SIB */;
461 *inst
++ = (0x4 /* none*/ << 3) | reg_map
[SLJIT_SP
];
463 if (type
>= SLJIT_CALL2
) {
465 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_R1
] << 3) | 0x4 /* SIB */;
466 *inst
++ = (0x4 /* none*/ << 3) | reg_map
[SLJIT_SP
];
467 *inst
++ = sizeof(sljit_sw
);
469 if (type
>= SLJIT_CALL3
) {
471 *inst
++ = MOD_DISP8
| (reg_map
[SLJIT_R2
] << 3) | 0x4 /* SIB */;
472 *inst
++ = (0x4 /* none*/ << 3) | reg_map
[SLJIT_SP
];
473 *inst
++ = 2 * sizeof(sljit_sw
);
476 return SLJIT_SUCCESS
;
479 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
484 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
485 ADJUST_LOCAL_OFFSET(dst
, dstw
);
487 CHECK_EXTRA_REGS(dst
, dstw
, (void)0);
489 /* For UNUSED dst. Uncommon, but possible. */
490 if (dst
== SLJIT_UNUSED
)
493 if (FAST_IS_REG(dst
)) {
494 /* Unused dest is possible here. */
495 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 1);
499 POP_REG(reg_map
[dst
]);
500 return SLJIT_SUCCESS
;
504 inst
= emit_x86_instruction(compiler
, 1, 0, 0, dst
, dstw
);
507 return SLJIT_SUCCESS
;
510 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_return(struct sljit_compiler
*compiler
, sljit_s32 src
, sljit_sw srcw
)
515 CHECK(check_sljit_emit_fast_return(compiler
, src
, srcw
));
516 ADJUST_LOCAL_OFFSET(src
, srcw
);
518 CHECK_EXTRA_REGS(src
, srcw
, (void)0);
520 if (FAST_IS_REG(src
)) {
521 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 1 + 1);
525 PUSH_REG(reg_map
[src
]);
527 else if (src
& SLJIT_MEM
) {
528 inst
= emit_x86_instruction(compiler
, 1, 0, 0, src
, srcw
);
533 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 1);
539 inst
= (sljit_u8
*)ensure_buf(compiler
, 1 + 5 + 1);
544 sljit_unaligned_store_sw(inst
, srcw
);
545 inst
+= sizeof(sljit_sw
);
549 return SLJIT_SUCCESS
;