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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1493 - (show annotations)
Wed Jul 9 04:41:15 2014 UTC (5 years, 1 month ago) by zherczeg
File MIME type: text/plain
File size: 21580 byte(s)
Minor JIT compiler update.
1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * 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
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 /* x86 64-bit arch dependent functions. */
28
29 static sljit_si emit_load_imm64(struct sljit_compiler *compiler, sljit_si reg, sljit_sw imm)
30 {
31 sljit_ub *inst;
32
33 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2 + sizeof(sljit_sw));
34 FAIL_IF(!inst);
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;
39 return SLJIT_SUCCESS;
40 }
41
42 static sljit_ub* generate_far_jump_code(struct sljit_jump *jump, sljit_ub *code_ptr, sljit_si type)
43 {
44 if (type < SLJIT_JUMP) {
45 /* Invert type. */
46 *code_ptr++ = get_jump_code(type ^ 0x1) - 0x10;
47 *code_ptr++ = 10 + 3;
48 }
49
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;
54
55 if (jump->flags & JUMP_LABEL)
56 jump->flags |= PATCH_MD;
57 else
58 *(sljit_sw*)code_ptr = jump->u.target;
59
60 code_ptr += sizeof(sljit_sw);
61 *code_ptr++ = REX_B;
62 *code_ptr++ = GROUP_FF;
63 *code_ptr++ = (type >= SLJIT_FAST_CALL) ? (MOD_REG | CALL_rm | 1) : (MOD_REG | JMP_rm | 1);
64
65 return code_ptr;
66 }
67
68 static sljit_ub* generate_fixed_jump(sljit_ub *code_ptr, sljit_sw addr, sljit_si type)
69 {
70 sljit_sw delta = addr - ((sljit_sw)code_ptr + 1 + sizeof(sljit_si));
71
72 if (delta <= HALFWORD_MAX && delta >= HALFWORD_MIN) {
73 *code_ptr++ = (type == 2) ? CALL_i32 : JMP_i32;
74 *(sljit_sw*)code_ptr = delta;
75 }
76 else {
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);
82 *code_ptr++ = REX_B;
83 *code_ptr++ = GROUP_FF;
84 *code_ptr++ = (type == 2) ? (MOD_REG | CALL_rm | 1) : (MOD_REG | JMP_rm | 1);
85 }
86
87 return code_ptr;
88 }
89
90 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
91 sljit_si args, sljit_si scratches, sljit_si saveds,
92 sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
93 {
94 sljit_si i, tmp, size, saved_register_size;
95 sljit_ub *inst;
96
97 CHECK_ERROR();
98 check_sljit_emit_enter(compiler, args, scratches, saveds, fscratches, fsaveds, local_size);
99
100 compiler->scratches = scratches;
101 compiler->saveds = saveds;
102 compiler->fscratches = fscratches;
103 compiler->fsaveds = fsaveds;
104 compiler->flags_saved = 0;
105 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
106 compiler->logical_local_size = local_size;
107 #endif
108
109 /* Including the return address saved by the call instruction. */
110 saved_register_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
111
112 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
113 for (i = SLJIT_S0; i >= tmp; i--) {
114 size = reg_map[i] >= 8 ? 2 : 1;
115 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
116 FAIL_IF(!inst);
117 INC_SIZE(size);
118 if (reg_map[i] >= 8)
119 *inst++ = REX_B;
120 PUSH_REG(reg_lmap[i]);
121 }
122
123 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
124 size = reg_map[i] >= 8 ? 2 : 1;
125 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
126 FAIL_IF(!inst);
127 INC_SIZE(size);
128 if (reg_map[i] >= 8)
129 *inst++ = REX_B;
130 PUSH_REG(reg_lmap[i]);
131 }
132
133 if (args > 0) {
134 size = args * 3;
135 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
136 FAIL_IF(!inst);
137
138 INC_SIZE(size);
139
140 #ifndef _WIN64
141 if (args > 0) {
142 *inst++ = REX_W;
143 *inst++ = MOV_r_rm;
144 *inst++ = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x7 /* rdi */;
145 }
146 if (args > 1) {
147 *inst++ = REX_W | REX_R;
148 *inst++ = MOV_r_rm;
149 *inst++ = MOD_REG | (reg_lmap[SLJIT_S1] << 3) | 0x6 /* rsi */;
150 }
151 if (args > 2) {
152 *inst++ = REX_W | REX_R;
153 *inst++ = MOV_r_rm;
154 *inst++ = MOD_REG | (reg_lmap[SLJIT_S2] << 3) | 0x2 /* rdx */;
155 }
156 #else
157 if (args > 0) {
158 *inst++ = REX_W;
159 *inst++ = MOV_r_rm;
160 *inst++ = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x1 /* rcx */;
161 }
162 if (args > 1) {
163 *inst++ = REX_W;
164 *inst++ = MOV_r_rm;
165 *inst++ = MOD_REG | (reg_map[SLJIT_S1] << 3) | 0x2 /* rdx */;
166 }
167 if (args > 2) {
168 *inst++ = REX_W | REX_B;
169 *inst++ = MOV_r_rm;
170 *inst++ = MOD_REG | (reg_map[SLJIT_S2] << 3) | 0x0 /* r8 */;
171 }
172 #endif
173 }
174
175 local_size = ((local_size + FIXED_LOCALS_OFFSET + saved_register_size + 16 - 1) & ~(16 - 1)) - saved_register_size;
176 compiler->local_size = local_size;
177
178 #ifdef _WIN64
179 if (local_size > 1024) {
180 /* Allocate stack for the callback, which grows the stack. */
181 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4 + (3 + sizeof(sljit_si)));
182 FAIL_IF(!inst);
183 INC_SIZE(4 + (3 + sizeof(sljit_si)));
184 *inst++ = REX_W;
185 *inst++ = GROUP_BINARY_83;
186 *inst++ = MOD_REG | SUB | 4;
187 /* Allocated size for registers must be divisible by 8. */
188 SLJIT_ASSERT(!(saved_register_size & 0x7));
189 /* Aligned to 16 byte. */
190 if (saved_register_size & 0x8) {
191 *inst++ = 5 * sizeof(sljit_sw);
192 local_size -= 5 * sizeof(sljit_sw);
193 } else {
194 *inst++ = 4 * sizeof(sljit_sw);
195 local_size -= 4 * sizeof(sljit_sw);
196 }
197 /* Second instruction */
198 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R0] < 8, temporary_reg1_is_loreg);
199 *inst++ = REX_W;
200 *inst++ = MOV_rm_i32;
201 *inst++ = MOD_REG | reg_lmap[SLJIT_R0];
202 *(sljit_si*)inst = local_size;
203 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
204 compiler->skip_checks = 1;
205 #endif
206 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_grow_stack)));
207 }
208 #endif
209
210 SLJIT_ASSERT(local_size > 0);
211 if (local_size <= 127) {
212 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
213 FAIL_IF(!inst);
214 INC_SIZE(4);
215 *inst++ = REX_W;
216 *inst++ = GROUP_BINARY_83;
217 *inst++ = MOD_REG | SUB | 4;
218 *inst++ = local_size;
219 }
220 else {
221 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
222 FAIL_IF(!inst);
223 INC_SIZE(7);
224 *inst++ = REX_W;
225 *inst++ = GROUP_BINARY_81;
226 *inst++ = MOD_REG | SUB | 4;
227 *(sljit_si*)inst = local_size;
228 inst += sizeof(sljit_si);
229 }
230
231 #ifdef _WIN64
232 /* Save xmm6 register: movaps [rsp + 0x20], xmm6 */
233 if (fscratches >= 6 || fsaveds >= 1) {
234 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5);
235 FAIL_IF(!inst);
236 INC_SIZE(5);
237 *inst++ = GROUP_0F;
238 *(sljit_si*)inst = 0x20247429;
239 }
240 #endif
241
242 return SLJIT_SUCCESS;
243 }
244
245 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler,
246 sljit_si args, sljit_si scratches, sljit_si saveds,
247 sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
248 {
249 sljit_si saved_register_size;
250
251 CHECK_ERROR_VOID();
252 check_sljit_set_context(compiler, args, scratches, saveds, fscratches, fsaveds, local_size);
253
254 compiler->scratches = scratches;
255 compiler->saveds = saveds;
256 compiler->fscratches = fscratches;
257 compiler->fsaveds = fsaveds;
258 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
259 compiler->logical_local_size = local_size;
260 #endif
261
262 /* Including the return address saved by the call instruction. */
263 saved_register_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
264 compiler->local_size = ((local_size + FIXED_LOCALS_OFFSET + saved_register_size + 16 - 1) & ~(16 - 1)) - saved_register_size;
265 }
266
267 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
268 {
269 sljit_si i, tmp, size;
270 sljit_ub *inst;
271
272 CHECK_ERROR();
273 check_sljit_emit_return(compiler, op, src, srcw);
274
275 compiler->flags_saved = 0;
276 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
277
278 #ifdef _WIN64
279 /* Restore xmm6 register: movaps xmm6, [rsp + 0x20] */
280 if (compiler->fscratches >= 6 || compiler->fsaveds >= 1) {
281 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5);
282 FAIL_IF(!inst);
283 INC_SIZE(5);
284 *inst++ = GROUP_0F;
285 *(sljit_si*)inst = 0x20247428;
286 }
287 #endif
288
289 SLJIT_ASSERT(compiler->local_size > 0);
290 if (compiler->local_size <= 127) {
291 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
292 FAIL_IF(!inst);
293 INC_SIZE(4);
294 *inst++ = REX_W;
295 *inst++ = GROUP_BINARY_83;
296 *inst++ = MOD_REG | ADD | 4;
297 *inst = compiler->local_size;
298 }
299 else {
300 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
301 FAIL_IF(!inst);
302 INC_SIZE(7);
303 *inst++ = REX_W;
304 *inst++ = GROUP_BINARY_81;
305 *inst++ = MOD_REG | ADD | 4;
306 *(sljit_si*)inst = compiler->local_size;
307 }
308
309 tmp = compiler->scratches;
310 for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
311 size = reg_map[i] >= 8 ? 2 : 1;
312 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
313 FAIL_IF(!inst);
314 INC_SIZE(size);
315 if (reg_map[i] >= 8)
316 *inst++ = REX_B;
317 POP_REG(reg_lmap[i]);
318 }
319
320 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
321 for (i = tmp; i <= SLJIT_S0; i++) {
322 size = reg_map[i] >= 8 ? 2 : 1;
323 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
324 FAIL_IF(!inst);
325 INC_SIZE(size);
326 if (reg_map[i] >= 8)
327 *inst++ = REX_B;
328 POP_REG(reg_lmap[i]);
329 }
330
331 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
332 FAIL_IF(!inst);
333 INC_SIZE(1);
334 RET();
335 return SLJIT_SUCCESS;
336 }
337
338 /* --------------------------------------------------------------------- */
339 /* Operators */
340 /* --------------------------------------------------------------------- */
341
342 static sljit_si emit_do_imm32(struct sljit_compiler *compiler, sljit_ub rex, sljit_ub opcode, sljit_sw imm)
343 {
344 sljit_ub *inst;
345 sljit_si length = 1 + (rex ? 1 : 0) + sizeof(sljit_si);
346
347 inst = (sljit_ub*)ensure_buf(compiler, 1 + length);
348 FAIL_IF(!inst);
349 INC_SIZE(length);
350 if (rex)
351 *inst++ = rex;
352 *inst++ = opcode;
353 *(sljit_si*)inst = imm;
354 return SLJIT_SUCCESS;
355 }
356
357 static sljit_ub* emit_x86_instruction(struct sljit_compiler *compiler, sljit_si size,
358 /* The register or immediate operand. */
359 sljit_si a, sljit_sw imma,
360 /* The general operand (not immediate). */
361 sljit_si b, sljit_sw immb)
362 {
363 sljit_ub *inst;
364 sljit_ub *buf_ptr;
365 sljit_ub rex = 0;
366 sljit_si flags = size & ~0xf;
367 sljit_si inst_size;
368
369 /* The immediate operand must be 32 bit. */
370 SLJIT_ASSERT(!(a & SLJIT_IMM) || compiler->mode32 || IS_HALFWORD(imma));
371 /* Both cannot be switched on. */
372 SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
373 /* Size flags not allowed for typed instructions. */
374 SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
375 /* Both size flags cannot be switched on. */
376 SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
377 /* SSE2 and immediate is not possible. */
378 SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
379 SLJIT_ASSERT((flags & (EX86_PREF_F2 | EX86_PREF_F3)) != (EX86_PREF_F2 | EX86_PREF_F3)
380 && (flags & (EX86_PREF_F2 | EX86_PREF_66)) != (EX86_PREF_F2 | EX86_PREF_66)
381 && (flags & (EX86_PREF_F3 | EX86_PREF_66)) != (EX86_PREF_F3 | EX86_PREF_66));
382
383 size &= 0xf;
384 inst_size = size;
385
386 if (!compiler->mode32 && !(flags & EX86_NO_REXW))
387 rex |= REX_W;
388 else if (flags & EX86_REX)
389 rex |= REX;
390
391 if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
392 inst_size++;
393 if (flags & EX86_PREF_66)
394 inst_size++;
395
396 /* Calculate size of b. */
397 inst_size += 1; /* mod r/m byte. */
398 if (b & SLJIT_MEM) {
399 if (!(b & OFFS_REG_MASK)) {
400 if (NOT_HALFWORD(immb)) {
401 if (emit_load_imm64(compiler, TMP_REG3, immb))
402 return NULL;
403 immb = 0;
404 if (b & REG_MASK)
405 b |= TO_OFFS_REG(TMP_REG3);
406 else
407 b |= TMP_REG3;
408 }
409 else if (reg_lmap[b & REG_MASK] == 4)
410 b |= TO_OFFS_REG(SLJIT_SP);
411 }
412
413 if ((b & REG_MASK) == SLJIT_UNUSED)
414 inst_size += 1 + sizeof(sljit_si); /* SIB byte required to avoid RIP based addressing. */
415 else {
416 if (reg_map[b & REG_MASK] >= 8)
417 rex |= REX_B;
418
419 if (immb != 0 && (!(b & OFFS_REG_MASK) || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP))) {
420 /* Immediate operand. */
421 if (immb <= 127 && immb >= -128)
422 inst_size += sizeof(sljit_sb);
423 else
424 inst_size += sizeof(sljit_si);
425 }
426 else if (reg_lmap[b & REG_MASK] == 5)
427 inst_size += sizeof(sljit_sb);
428
429 if ((b & OFFS_REG_MASK) != SLJIT_UNUSED) {
430 inst_size += 1; /* SIB byte. */
431 if (reg_map[OFFS_REG(b)] >= 8)
432 rex |= REX_X;
433 }
434 }
435 }
436 else if (!(flags & EX86_SSE2_OP2) && reg_map[b] >= 8)
437 rex |= REX_B;
438
439 if (a & SLJIT_IMM) {
440 if (flags & EX86_BIN_INS) {
441 if (imma <= 127 && imma >= -128) {
442 inst_size += 1;
443 flags |= EX86_BYTE_ARG;
444 } else
445 inst_size += 4;
446 }
447 else if (flags & EX86_SHIFT_INS) {
448 imma &= compiler->mode32 ? 0x1f : 0x3f;
449 if (imma != 1) {
450 inst_size ++;
451 flags |= EX86_BYTE_ARG;
452 }
453 } else if (flags & EX86_BYTE_ARG)
454 inst_size++;
455 else if (flags & EX86_HALF_ARG)
456 inst_size += sizeof(short);
457 else
458 inst_size += sizeof(sljit_si);
459 }
460 else {
461 SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
462 /* reg_map[SLJIT_PREF_SHIFT_REG] is less than 8. */
463 if (!(flags & EX86_SSE2_OP1) && reg_map[a] >= 8)
464 rex |= REX_R;
465 }
466
467 if (rex)
468 inst_size++;
469
470 inst = (sljit_ub*)ensure_buf(compiler, 1 + inst_size);
471 PTR_FAIL_IF(!inst);
472
473 /* Encoding the byte. */
474 INC_SIZE(inst_size);
475 if (flags & EX86_PREF_F2)
476 *inst++ = 0xf2;
477 if (flags & EX86_PREF_F3)
478 *inst++ = 0xf3;
479 if (flags & EX86_PREF_66)
480 *inst++ = 0x66;
481 if (rex)
482 *inst++ = rex;
483 buf_ptr = inst + size;
484
485 /* Encode mod/rm byte. */
486 if (!(flags & EX86_SHIFT_INS)) {
487 if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
488 *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
489
490 if ((a & SLJIT_IMM) || (a == 0))
491 *buf_ptr = 0;
492 else if (!(flags & EX86_SSE2_OP1))
493 *buf_ptr = reg_lmap[a] << 3;
494 else
495 *buf_ptr = a << 3;
496 }
497 else {
498 if (a & SLJIT_IMM) {
499 if (imma == 1)
500 *inst = GROUP_SHIFT_1;
501 else
502 *inst = GROUP_SHIFT_N;
503 } else
504 *inst = GROUP_SHIFT_CL;
505 *buf_ptr = 0;
506 }
507
508 if (!(b & SLJIT_MEM))
509 *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_lmap[b] : b);
510 else if ((b & REG_MASK) != SLJIT_UNUSED) {
511 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP)) {
512 if (immb != 0 || reg_lmap[b & REG_MASK] == 5) {
513 if (immb <= 127 && immb >= -128)
514 *buf_ptr |= 0x40;
515 else
516 *buf_ptr |= 0x80;
517 }
518
519 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
520 *buf_ptr++ |= reg_lmap[b & REG_MASK];
521 else {
522 *buf_ptr++ |= 0x04;
523 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3);
524 }
525
526 if (immb != 0 || reg_lmap[b & REG_MASK] == 5) {
527 if (immb <= 127 && immb >= -128)
528 *buf_ptr++ = immb; /* 8 bit displacement. */
529 else {
530 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
531 buf_ptr += sizeof(sljit_si);
532 }
533 }
534 }
535 else {
536 if (reg_lmap[b & REG_MASK] == 5)
537 *buf_ptr |= 0x40;
538 *buf_ptr++ |= 0x04;
539 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3) | (immb << 6);
540 if (reg_lmap[b & REG_MASK] == 5)
541 *buf_ptr++ = 0;
542 }
543 }
544 else {
545 *buf_ptr++ |= 0x04;
546 *buf_ptr++ = 0x25;
547 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
548 buf_ptr += sizeof(sljit_si);
549 }
550
551 if (a & SLJIT_IMM) {
552 if (flags & EX86_BYTE_ARG)
553 *buf_ptr = imma;
554 else if (flags & EX86_HALF_ARG)
555 *(short*)buf_ptr = imma;
556 else if (!(flags & EX86_SHIFT_INS))
557 *(sljit_si*)buf_ptr = imma;
558 }
559
560 return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
561 }
562
563 /* --------------------------------------------------------------------- */
564 /* Call / return instructions */
565 /* --------------------------------------------------------------------- */
566
567 static SLJIT_INLINE sljit_si call_with_args(struct sljit_compiler *compiler, sljit_si type)
568 {
569 sljit_ub *inst;
570
571 #ifndef _WIN64
572 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R1] == 6 && reg_map[SLJIT_R0] < 8 && reg_map[SLJIT_R2] < 8, args_registers);
573
574 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
575 FAIL_IF(!inst);
576 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
577 if (type >= SLJIT_CALL3) {
578 *inst++ = REX_W;
579 *inst++ = MOV_r_rm;
580 *inst++ = MOD_REG | (0x2 /* rdx */ << 3) | reg_lmap[SLJIT_R2];
581 }
582 *inst++ = REX_W;
583 *inst++ = MOV_r_rm;
584 *inst++ = MOD_REG | (0x7 /* rdi */ << 3) | reg_lmap[SLJIT_R0];
585 #else
586 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R1] == 2 && reg_map[SLJIT_R0] < 8 && reg_map[SLJIT_R2] < 8, args_registers);
587
588 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
589 FAIL_IF(!inst);
590 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
591 if (type >= SLJIT_CALL3) {
592 *inst++ = REX_W | REX_R;
593 *inst++ = MOV_r_rm;
594 *inst++ = MOD_REG | (0x0 /* r8 */ << 3) | reg_lmap[SLJIT_R2];
595 }
596 *inst++ = REX_W;
597 *inst++ = MOV_r_rm;
598 *inst++ = MOD_REG | (0x1 /* rcx */ << 3) | reg_lmap[SLJIT_R0];
599 #endif
600 return SLJIT_SUCCESS;
601 }
602
603 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
604 {
605 sljit_ub *inst;
606
607 CHECK_ERROR();
608 check_sljit_emit_fast_enter(compiler, dst, dstw);
609 ADJUST_LOCAL_OFFSET(dst, dstw);
610
611 /* For UNUSED dst. Uncommon, but possible. */
612 if (dst == SLJIT_UNUSED)
613 dst = TMP_REG1;
614
615 if (FAST_IS_REG(dst)) {
616 if (reg_map[dst] < 8) {
617 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
618 FAIL_IF(!inst);
619 INC_SIZE(1);
620 POP_REG(reg_lmap[dst]);
621 return SLJIT_SUCCESS;
622 }
623
624 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2);
625 FAIL_IF(!inst);
626 INC_SIZE(2);
627 *inst++ = REX_B;
628 POP_REG(reg_lmap[dst]);
629 return SLJIT_SUCCESS;
630 }
631
632 /* REX_W is not necessary (src is not immediate). */
633 compiler->mode32 = 1;
634 inst = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
635 FAIL_IF(!inst);
636 *inst++ = POP_rm;
637 return SLJIT_SUCCESS;
638 }
639
640 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
641 {
642 sljit_ub *inst;
643
644 CHECK_ERROR();
645 check_sljit_emit_fast_return(compiler, src, srcw);
646 ADJUST_LOCAL_OFFSET(src, srcw);
647
648 if ((src & SLJIT_IMM) && NOT_HALFWORD(srcw)) {
649 FAIL_IF(emit_load_imm64(compiler, TMP_REG1, srcw));
650 src = TMP_REG1;
651 }
652
653 if (FAST_IS_REG(src)) {
654 if (reg_map[src] < 8) {
655 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1 + 1);
656 FAIL_IF(!inst);
657
658 INC_SIZE(1 + 1);
659 PUSH_REG(reg_lmap[src]);
660 }
661 else {
662 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2 + 1);
663 FAIL_IF(!inst);
664
665 INC_SIZE(2 + 1);
666 *inst++ = REX_B;
667 PUSH_REG(reg_lmap[src]);
668 }
669 }
670 else if (src & SLJIT_MEM) {
671 /* REX_W is not necessary (src is not immediate). */
672 compiler->mode32 = 1;
673 inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
674 FAIL_IF(!inst);
675 *inst++ = GROUP_FF;
676 *inst |= PUSH_rm;
677
678 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
679 FAIL_IF(!inst);
680 INC_SIZE(1);
681 }
682 else {
683 SLJIT_ASSERT(IS_HALFWORD(srcw));
684 /* SLJIT_IMM. */
685 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5 + 1);
686 FAIL_IF(!inst);
687
688 INC_SIZE(5 + 1);
689 *inst++ = PUSH_i32;
690 *(sljit_si*)inst = srcw;
691 inst += sizeof(sljit_si);
692 }
693
694 RET();
695 return SLJIT_SUCCESS;
696 }
697
698
699 /* --------------------------------------------------------------------- */
700 /* Extend input */
701 /* --------------------------------------------------------------------- */
702
703 static sljit_si emit_mov_int(struct sljit_compiler *compiler, sljit_si sign,
704 sljit_si dst, sljit_sw dstw,
705 sljit_si src, sljit_sw srcw)
706 {
707 sljit_ub* inst;
708 sljit_si dst_r;
709
710 compiler->mode32 = 0;
711
712 if (dst == SLJIT_UNUSED && !(src & SLJIT_MEM))
713 return SLJIT_SUCCESS; /* Empty instruction. */
714
715 if (src & SLJIT_IMM) {
716 if (FAST_IS_REG(dst)) {
717 if (sign || ((sljit_uw)srcw <= 0x7fffffff)) {
718 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_si)srcw, dst, dstw);
719 FAIL_IF(!inst);
720 *inst = MOV_rm_i32;
721 return SLJIT_SUCCESS;
722 }
723 return emit_load_imm64(compiler, dst, srcw);
724 }
725 compiler->mode32 = 1;
726 inst = emit_x86_instruction(compiler, 1, SLJIT_IMM, (sljit_sw)(sljit_si)srcw, dst, dstw);
727 FAIL_IF(!inst);
728 *inst = MOV_rm_i32;
729 compiler->mode32 = 0;
730 return SLJIT_SUCCESS;
731 }
732
733 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
734
735 if ((dst & SLJIT_MEM) && FAST_IS_REG(src))
736 dst_r = src;
737 else {
738 if (sign) {
739 inst = emit_x86_instruction(compiler, 1, dst_r, 0, src, srcw);
740 FAIL_IF(!inst);
741 *inst++ = MOVSXD_r_rm;
742 } else {
743 compiler->mode32 = 1;
744 FAIL_IF(emit_mov(compiler, dst_r, 0, src, srcw));
745 compiler->mode32 = 0;
746 }
747 }
748
749 if (dst & SLJIT_MEM) {
750 compiler->mode32 = 1;
751 inst = emit_x86_instruction(compiler, 1, dst_r, 0, dst, dstw);
752 FAIL_IF(!inst);
753 *inst = MOV_rm_r;
754 compiler->mode32 = 0;
755 }
756
757 return SLJIT_SUCCESS;
758 }

  ViewVC Help
Powered by ViewVC 1.1.5