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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12