/[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 - (hide annotations) (download)
Wed Jul 9 04:41:15 2014 UTC (3 weeks, 2 days ago) by zherczeg
File MIME type: text/plain
File size: 21580 byte(s)
Minor 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     sljit_si args, sljit_si scratches, sljit_si saveds,
92     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 1491 check_sljit_emit_enter(compiler, args, scratches, saveds, fscratches, fsaveds, local_size);
99 ph10 662
100 zherczeg 1215 compiler->scratches = scratches;
101 zherczeg 880 compiler->saveds = saveds;
102 zherczeg 1491 compiler->fscratches = fscratches;
103     compiler->fsaveds = fsaveds;
104 ph10 662 compiler->flags_saved = 0;
105 zherczeg 955 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
106     compiler->logical_local_size = local_size;
107     #endif
108 ph10 662
109 zherczeg 740 /* Including the return address saved by the call instruction. */
110 zherczeg 1493 saved_register_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
111 zherczeg 1491
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 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + size);
116     FAIL_IF(!inst);
117 zherczeg 1491 INC_SIZE(size);
118     if (reg_map[i] >= 8)
119     *inst++ = REX_B;
120     PUSH_REG(reg_lmap[i]);
121     }
122 ph10 662
123 zherczeg 1491 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 ph10 662 INC_SIZE(size);
128 zherczeg 1491 if (reg_map[i] >= 8)
129 zherczeg 1201 *inst++ = REX_B;
130 zherczeg 1491 PUSH_REG(reg_lmap[i]);
131     }
132 ph10 662
133 zherczeg 1491 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 ph10 662 #ifndef _WIN64
141     if (args > 0) {
142 zherczeg 1201 *inst++ = REX_W;
143     *inst++ = MOV_r_rm;
144 zherczeg 1491 *inst++ = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x7 /* rdi */;
145 ph10 662 }
146     if (args > 1) {
147 zherczeg 1201 *inst++ = REX_W | REX_R;
148     *inst++ = MOV_r_rm;
149 zherczeg 1491 *inst++ = MOD_REG | (reg_lmap[SLJIT_S1] << 3) | 0x6 /* rsi */;
150 ph10 662 }
151     if (args > 2) {
152 zherczeg 1201 *inst++ = REX_W | REX_R;
153     *inst++ = MOV_r_rm;
154 zherczeg 1491 *inst++ = MOD_REG | (reg_lmap[SLJIT_S2] << 3) | 0x2 /* rdx */;
155 ph10 662 }
156     #else
157     if (args > 0) {
158 zherczeg 1201 *inst++ = REX_W;
159     *inst++ = MOV_r_rm;
160 zherczeg 1491 *inst++ = MOD_REG | (reg_map[SLJIT_S0] << 3) | 0x1 /* rcx */;
161 ph10 662 }
162     if (args > 1) {
163 zherczeg 1201 *inst++ = REX_W;
164     *inst++ = MOV_r_rm;
165 zherczeg 1491 *inst++ = MOD_REG | (reg_map[SLJIT_S1] << 3) | 0x2 /* rdx */;
166 ph10 662 }
167     if (args > 2) {
168 zherczeg 1201 *inst++ = REX_W | REX_B;
169     *inst++ = MOV_r_rm;
170 zherczeg 1491 *inst++ = MOD_REG | (reg_map[SLJIT_S2] << 3) | 0x0 /* r8 */;
171 ph10 662 }
172     #endif
173     }
174    
175 zherczeg 1493 local_size = ((local_size + FIXED_LOCALS_OFFSET + saved_register_size + 16 - 1) & ~(16 - 1)) - saved_register_size;
176 ph10 662 compiler->local_size = local_size;
177 zherczeg 1491
178 zherczeg 956 #ifdef _WIN64
179 ph10 662 if (local_size > 1024) {
180 zherczeg 956 /* Allocate stack for the callback, which grows the stack. */
181 zherczeg 1201 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 zherczeg 1491 /* Allocated size for registers must be divisible by 8. */
188 zherczeg 1493 SLJIT_ASSERT(!(saved_register_size & 0x7));
189 zherczeg 1491 /* Aligned to 16 byte. */
190 zherczeg 1493 if (saved_register_size & 0x8) {
191 zherczeg 1201 *inst++ = 5 * sizeof(sljit_sw);
192 zherczeg 1195 local_size -= 5 * sizeof(sljit_sw);
193 zherczeg 740 } else {
194 zherczeg 1201 *inst++ = 4 * sizeof(sljit_sw);
195 zherczeg 1195 local_size -= 4 * sizeof(sljit_sw);
196 zherczeg 740 }
197 zherczeg 1201 /* Second instruction */
198 zherczeg 1491 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R0] < 8, temporary_reg1_is_loreg);
199 zherczeg 1201 *inst++ = REX_W;
200     *inst++ = MOV_rm_i32;
201 zherczeg 1491 *inst++ = MOD_REG | reg_lmap[SLJIT_R0];
202 zherczeg 1201 *(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 zherczeg 956 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_grow_stack)));
207 ph10 662 }
208     #endif
209 zherczeg 1491
210 zherczeg 956 SLJIT_ASSERT(local_size > 0);
211     if (local_size <= 127) {
212 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
213     FAIL_IF(!inst);
214 zherczeg 956 INC_SIZE(4);
215 zherczeg 1201 *inst++ = REX_W;
216     *inst++ = GROUP_BINARY_83;
217     *inst++ = MOD_REG | SUB | 4;
218     *inst++ = local_size;
219 ph10 662 }
220 zherczeg 956 else {
221 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
222     FAIL_IF(!inst);
223 zherczeg 956 INC_SIZE(7);
224 zherczeg 1201 *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 zherczeg 956 }
230 zherczeg 1491
231 zherczeg 1201 #ifdef _WIN64
232 zherczeg 1491 /* 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 zherczeg 1201 #endif
241 ph10 662
242     return SLJIT_SUCCESS;
243     }
244    
245 zherczeg 1491 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 ph10 662 {
249 zherczeg 1493 sljit_si saved_register_size;
250 zherczeg 740
251 ph10 662 CHECK_ERROR_VOID();
252 zherczeg 1491 check_sljit_set_context(compiler, args, scratches, saveds, fscratches, fsaveds, local_size);
253 ph10 662
254 zherczeg 1215 compiler->scratches = scratches;
255 zherczeg 880 compiler->saveds = saveds;
256 zherczeg 1491 compiler->fscratches = fscratches;
257     compiler->fsaveds = fsaveds;
258 zherczeg 955 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
259     compiler->logical_local_size = local_size;
260     #endif
261    
262 zherczeg 740 /* Including the return address saved by the call instruction. */
263 zherczeg 1493 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 ph10 662 }
266    
267 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
268 ph10 662 {
269 zherczeg 1491 sljit_si i, tmp, size;
270 zherczeg 1201 sljit_ub *inst;
271 ph10 662
272     CHECK_ERROR();
273 zherczeg 875 check_sljit_emit_return(compiler, op, src, srcw);
274 ph10 662
275     compiler->flags_saved = 0;
276 zherczeg 875 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
277 ph10 662
278 zherczeg 1201 #ifdef _WIN64
279 zherczeg 1491 /* 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 zherczeg 1201 #endif
288 zherczeg 1491
289 zherczeg 956 SLJIT_ASSERT(compiler->local_size > 0);
290     if (compiler->local_size <= 127) {
291 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 4);
292     FAIL_IF(!inst);
293 zherczeg 956 INC_SIZE(4);
294 zherczeg 1201 *inst++ = REX_W;
295     *inst++ = GROUP_BINARY_83;
296     *inst++ = MOD_REG | ADD | 4;
297     *inst = compiler->local_size;
298 ph10 662 }
299 zherczeg 956 else {
300 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 7);
301     FAIL_IF(!inst);
302 zherczeg 956 INC_SIZE(7);
303 zherczeg 1201 *inst++ = REX_W;
304     *inst++ = GROUP_BINARY_81;
305     *inst++ = MOD_REG | ADD | 4;
306     *(sljit_si*)inst = compiler->local_size;
307 zherczeg 956 }
308 ph10 662
309 zherczeg 1491 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 ph10 662
320 zherczeg 1491 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 ph10 662 }
330    
331 zherczeg 1491 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
332     FAIL_IF(!inst);
333     INC_SIZE(1);
334 ph10 662 RET();
335     return SLJIT_SUCCESS;
336     }
337    
338     /* --------------------------------------------------------------------- */
339     /* Operators */
340     /* --------------------------------------------------------------------- */
341    
342 zherczeg 1195 static sljit_si emit_do_imm32(struct sljit_compiler *compiler, sljit_ub rex, sljit_ub opcode, sljit_sw imm)
343 ph10 662 {
344 zherczeg 1201 sljit_ub *inst;
345     sljit_si length = 1 + (rex ? 1 : 0) + sizeof(sljit_si);
346 ph10 662
347 zherczeg 1201 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 ph10 662 return SLJIT_SUCCESS;
355     }
356    
357 zherczeg 1195 static sljit_ub* emit_x86_instruction(struct sljit_compiler *compiler, sljit_si size,
358 ph10 662 /* The register or immediate operand. */
359 zherczeg 1195 sljit_si a, sljit_sw imma,
360 ph10 662 /* The general operand (not immediate). */
361 zherczeg 1195 sljit_si b, sljit_sw immb)
362 ph10 662 {
363 zherczeg 1201 sljit_ub *inst;
364 ph10 662 sljit_ub *buf_ptr;
365     sljit_ub rex = 0;
366 zherczeg 1195 sljit_si flags = size & ~0xf;
367     sljit_si inst_size;
368 ph10 662
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 zherczeg 1195 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 ph10 662
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 zherczeg 1195 if (flags & (EX86_PREF_F2 | EX86_PREF_F3))
392 ph10 662 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 zherczeg 1483 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 zherczeg 1491 b |= TO_OFFS_REG(SLJIT_SP);
411 zherczeg 1483 }
412    
413 zherczeg 1453 if ((b & REG_MASK) == SLJIT_UNUSED)
414 zherczeg 1195 inst_size += 1 + sizeof(sljit_si); /* SIB byte required to avoid RIP based addressing. */
415 ph10 662 else {
416 zherczeg 1453 if (reg_map[b & REG_MASK] >= 8)
417 ph10 662 rex |= REX_B;
418 zherczeg 1491
419     if (immb != 0 && (!(b & OFFS_REG_MASK) || (b & OFFS_REG_MASK) == TO_OFFS_REG(SLJIT_SP))) {
420 ph10 662 /* Immediate operand. */
421     if (immb <= 127 && immb >= -128)
422 zherczeg 1195 inst_size += sizeof(sljit_sb);
423 ph10 662 else
424 zherczeg 1195 inst_size += sizeof(sljit_si);
425 ph10 662 }
426 zherczeg 1491 else if (reg_lmap[b & REG_MASK] == 5)
427     inst_size += sizeof(sljit_sb);
428 ph10 662
429 zherczeg 1483 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 ph10 662 }
435     }
436 zherczeg 1483 else if (!(flags & EX86_SSE2_OP2) && reg_map[b] >= 8)
437 ph10 662 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 zherczeg 860 imma &= compiler->mode32 ? 0x1f : 0x3f;
449 ph10 662 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 zherczeg 1195 inst_size += sizeof(sljit_si);
459 ph10 662 }
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 zherczeg 1483 if (!(flags & EX86_SSE2_OP1) && reg_map[a] >= 8)
464 ph10 662 rex |= REX_R;
465     }
466    
467     if (rex)
468     inst_size++;
469    
470 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + inst_size);
471     PTR_FAIL_IF(!inst);
472 ph10 662
473     /* Encoding the byte. */
474     INC_SIZE(inst_size);
475     if (flags & EX86_PREF_F2)
476 zherczeg 1201 *inst++ = 0xf2;
477 zherczeg 1195 if (flags & EX86_PREF_F3)
478 zherczeg 1201 *inst++ = 0xf3;
479 ph10 662 if (flags & EX86_PREF_66)
480 zherczeg 1201 *inst++ = 0x66;
481 ph10 662 if (rex)
482 zherczeg 1201 *inst++ = rex;
483     buf_ptr = inst + size;
484 ph10 662
485     /* Encode mod/rm byte. */
486     if (!(flags & EX86_SHIFT_INS)) {
487     if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
488 zherczeg 1201 *inst = (flags & EX86_BYTE_ARG) ? GROUP_BINARY_83 : GROUP_BINARY_81;
489 ph10 662
490     if ((a & SLJIT_IMM) || (a == 0))
491     *buf_ptr = 0;
492 zherczeg 1483 else if (!(flags & EX86_SSE2_OP1))
493 ph10 662 *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 zherczeg 1201 *inst = GROUP_SHIFT_1;
501 ph10 662 else
502 zherczeg 1201 *inst = GROUP_SHIFT_N;
503 ph10 662 } else
504 zherczeg 1201 *inst = GROUP_SHIFT_CL;
505 ph10 662 *buf_ptr = 0;
506     }
507    
508     if (!(b & SLJIT_MEM))
509 zherczeg 1483 *buf_ptr++ |= MOD_REG + ((!(flags & EX86_SSE2_OP2)) ? reg_lmap[b] : b);
510 zherczeg 1453 else if ((b & REG_MASK) != SLJIT_UNUSED) {
511 zherczeg 1491 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 ph10 662 if (immb <= 127 && immb >= -128)
514     *buf_ptr |= 0x40;
515     else
516     *buf_ptr |= 0x80;
517     }
518    
519 zherczeg 1453 if ((b & OFFS_REG_MASK) == SLJIT_UNUSED)
520     *buf_ptr++ |= reg_lmap[b & REG_MASK];
521 ph10 662 else {
522     *buf_ptr++ |= 0x04;
523 zherczeg 1453 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3);
524 ph10 662 }
525    
526 zherczeg 1491 if (immb != 0 || reg_lmap[b & REG_MASK] == 5) {
527 ph10 662 if (immb <= 127 && immb >= -128)
528     *buf_ptr++ = immb; /* 8 bit displacement. */
529     else {
530 zherczeg 1195 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
531     buf_ptr += sizeof(sljit_si);
532 ph10 662 }
533     }
534     }
535     else {
536 zherczeg 1491 if (reg_lmap[b & REG_MASK] == 5)
537     *buf_ptr |= 0x40;
538 ph10 662 *buf_ptr++ |= 0x04;
539 zherczeg 1453 *buf_ptr++ = reg_lmap[b & REG_MASK] | (reg_lmap[OFFS_REG(b)] << 3) | (immb << 6);
540 zherczeg 1491 if (reg_lmap[b & REG_MASK] == 5)
541     *buf_ptr++ = 0;
542 ph10 662 }
543     }
544     else {
545     *buf_ptr++ |= 0x04;
546     *buf_ptr++ = 0x25;
547 zherczeg 1195 *(sljit_si*)buf_ptr = immb; /* 32 bit displacement. */
548     buf_ptr += sizeof(sljit_si);
549 ph10 662 }
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 zherczeg 1195 *(sljit_si*)buf_ptr = imma;
558 ph10 662 }
559    
560 zherczeg 1201 return !(flags & EX86_SHIFT_INS) ? inst : (inst + 1);
561 ph10 662 }
562    
563     /* --------------------------------------------------------------------- */
564     /* Call / return instructions */
565     /* --------------------------------------------------------------------- */
566    
567 zherczeg 1195 static SLJIT_INLINE sljit_si call_with_args(struct sljit_compiler *compiler, sljit_si type)
568 ph10 662 {
569 zherczeg 1201 sljit_ub *inst;
570 ph10 662
571     #ifndef _WIN64
572 zherczeg 1491 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R1] == 6 && reg_map[SLJIT_R0] < 8 && reg_map[SLJIT_R2] < 8, args_registers);
573 ph10 662
574 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
575     FAIL_IF(!inst);
576 ph10 662 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
577     if (type >= SLJIT_CALL3) {
578 zherczeg 1201 *inst++ = REX_W;
579     *inst++ = MOV_r_rm;
580 zherczeg 1491 *inst++ = MOD_REG | (0x2 /* rdx */ << 3) | reg_lmap[SLJIT_R2];
581 ph10 662 }
582 zherczeg 1201 *inst++ = REX_W;
583     *inst++ = MOV_r_rm;
584 zherczeg 1491 *inst++ = MOD_REG | (0x7 /* rdi */ << 3) | reg_lmap[SLJIT_R0];
585 ph10 662 #else
586 zherczeg 1491 SLJIT_COMPILE_ASSERT(reg_map[SLJIT_R1] == 2 && reg_map[SLJIT_R0] < 8 && reg_map[SLJIT_R2] < 8, args_registers);
587 ph10 662
588 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + ((type < SLJIT_CALL3) ? 3 : 6));
589     FAIL_IF(!inst);
590 ph10 662 INC_SIZE((type < SLJIT_CALL3) ? 3 : 6);
591     if (type >= SLJIT_CALL3) {
592 zherczeg 1201 *inst++ = REX_W | REX_R;
593     *inst++ = MOV_r_rm;
594 zherczeg 1491 *inst++ = MOD_REG | (0x0 /* r8 */ << 3) | reg_lmap[SLJIT_R2];
595 ph10 662 }
596 zherczeg 1201 *inst++ = REX_W;
597     *inst++ = MOV_r_rm;
598 zherczeg 1491 *inst++ = MOD_REG | (0x1 /* rcx */ << 3) | reg_lmap[SLJIT_R0];
599 ph10 662 #endif
600     return SLJIT_SUCCESS;
601     }
602    
603 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
604 ph10 662 {
605 zherczeg 1201 sljit_ub *inst;
606 ph10 662
607     CHECK_ERROR();
608 zherczeg 955 check_sljit_emit_fast_enter(compiler, dst, dstw);
609     ADJUST_LOCAL_OFFSET(dst, dstw);
610 ph10 662
611     /* For UNUSED dst. Uncommon, but possible. */
612     if (dst == SLJIT_UNUSED)
613 zherczeg 1453 dst = TMP_REG1;
614 ph10 662
615 zherczeg 1453 if (FAST_IS_REG(dst)) {
616 ph10 662 if (reg_map[dst] < 8) {
617 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
618     FAIL_IF(!inst);
619 ph10 662 INC_SIZE(1);
620     POP_REG(reg_lmap[dst]);
621 zherczeg 1215 return SLJIT_SUCCESS;
622 ph10 662 }
623    
624 zherczeg 1215 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2);
625 zherczeg 1201 FAIL_IF(!inst);
626 zherczeg 1215 INC_SIZE(2);
627     *inst++ = REX_B;
628     POP_REG(reg_lmap[dst]);
629     return SLJIT_SUCCESS;
630 ph10 662 }
631 zherczeg 1215
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 ph10 662 return SLJIT_SUCCESS;
638     }
639    
640 zherczeg 1195 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
641 ph10 662 {
642 zherczeg 1201 sljit_ub *inst;
643 ph10 662
644     CHECK_ERROR();
645     check_sljit_emit_fast_return(compiler, src, srcw);
646 zherczeg 955 ADJUST_LOCAL_OFFSET(src, srcw);
647 ph10 662
648     if ((src & SLJIT_IMM) && NOT_HALFWORD(srcw)) {
649 zherczeg 1453 FAIL_IF(emit_load_imm64(compiler, TMP_REG1, srcw));
650     src = TMP_REG1;
651 ph10 662 }
652    
653 zherczeg 1453 if (FAST_IS_REG(src)) {
654 ph10 662 if (reg_map[src] < 8) {
655 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1 + 1);
656     FAIL_IF(!inst);
657 ph10 662
658     INC_SIZE(1 + 1);
659     PUSH_REG(reg_lmap[src]);
660     }
661     else {
662 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 2 + 1);
663     FAIL_IF(!inst);
664 ph10 662
665     INC_SIZE(2 + 1);
666 zherczeg 1201 *inst++ = REX_B;
667 ph10 662 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 zherczeg 1201 inst = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
674     FAIL_IF(!inst);
675     *inst++ = GROUP_FF;
676     *inst |= PUSH_rm;
677 ph10 662
678 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 1);
679     FAIL_IF(!inst);
680 ph10 662 INC_SIZE(1);
681     }
682     else {
683     SLJIT_ASSERT(IS_HALFWORD(srcw));
684     /* SLJIT_IMM. */
685 zherczeg 1201 inst = (sljit_ub*)ensure_buf(compiler, 1 + 5 + 1);
686     FAIL_IF(!inst);
687 ph10 662
688     INC_SIZE(5 + 1);
689 zherczeg 1201 *inst++ = PUSH_i32;
690     *(sljit_si*)inst = srcw;
691     inst += sizeof(sljit_si);
692 ph10 662 }
693    
694     RET();
695     return SLJIT_SUCCESS;
696     }
697    
698    
699     /* --------------------------------------------------------------------- */
700     /* Extend input */
701     /* --------------------------------------------------------------------- */
702    
703 zherczeg 1195 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 ph10 662 {
707 zherczeg 1201 sljit_ub* inst;
708 zherczeg 1195 sljit_si dst_r;
709 ph10 662
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 zherczeg 1453 if (FAST_IS_REG(dst)) {
717 zherczeg 867 if (sign || ((sljit_uw)srcw <= 0x7fffffff)) {
718 zherczeg 1201 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 zherczeg 867 return SLJIT_SUCCESS;
722     }
723 ph10 662 return emit_load_imm64(compiler, dst, srcw);
724 zherczeg 867 }
725 ph10 662 compiler->mode32 = 1;
726 zherczeg 1201 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 ph10 662 compiler->mode32 = 0;
730     return SLJIT_SUCCESS;
731     }
732    
733 zherczeg 1453 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
734 ph10 662
735 zherczeg 1453 if ((dst & SLJIT_MEM) && FAST_IS_REG(src))
736 ph10 662 dst_r = src;
737     else {
738     if (sign) {
739 zherczeg 1201 inst = emit_x86_instruction(compiler, 1, dst_r, 0, src, srcw);
740     FAIL_IF(!inst);
741     *inst++ = MOVSXD_r_rm;
742 zherczeg 847 } else {
743     compiler->mode32 = 1;
744 zherczeg 867 FAIL_IF(emit_mov(compiler, dst_r, 0, src, srcw));
745 zherczeg 847 compiler->mode32 = 0;
746 ph10 662 }
747     }
748    
749     if (dst & SLJIT_MEM) {
750     compiler->mode32 = 1;
751 zherczeg 1201 inst = emit_x86_instruction(compiler, 1, dst_r, 0, dst, dstw);
752     FAIL_IF(!inst);
753     *inst = MOV_rm_r;
754 ph10 662 compiler->mode32 = 0;
755     }
756    
757     return SLJIT_SUCCESS;
758     }

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12