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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 880 - (show annotations) (download)
Sun Jan 15 17:23:37 2012 UTC (2 years, 3 months ago) by zherczeg
File MIME type: text/plain
File size: 14672 byte(s)
JIT compiler update (rename GENERAL to SAVED)
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 32-bit arch dependent functions. */
28
29 static int emit_do_imm(struct sljit_compiler *compiler, sljit_ub opcode, sljit_w imm)
30 {
31 sljit_ub *buf;
32
33 buf = (sljit_ub*)ensure_buf(compiler, 1 + 1 + sizeof(sljit_w));
34 FAIL_IF(!buf);
35 INC_SIZE(1 + sizeof(sljit_w));
36 *buf++ = opcode;
37 *(sljit_w*)buf = imm;
38 return SLJIT_SUCCESS;
39 }
40
41 static sljit_ub* generate_far_jump_code(struct sljit_jump *jump, sljit_ub *code_ptr, int type)
42 {
43 if (type == SLJIT_JUMP) {
44 *code_ptr++ = 0xe9;
45 jump->addr++;
46 }
47 else if (type >= SLJIT_FAST_CALL) {
48 *code_ptr++ = 0xe8;
49 jump->addr++;
50 }
51 else {
52 *code_ptr++ = 0x0f;
53 *code_ptr++ = get_jump_code(type);
54 jump->addr += 2;
55 }
56
57 if (jump->flags & JUMP_LABEL)
58 jump->flags |= PATCH_MW;
59 else
60 *(sljit_w*)code_ptr = jump->u.target - (jump->addr + 4);
61 code_ptr += 4;
62
63 return code_ptr;
64 }
65
66 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int saveds, int local_size)
67 {
68 int size;
69 sljit_ub *buf;
70
71 CHECK_ERROR();
72 check_sljit_emit_enter(compiler, args, temporaries, saveds, local_size);
73
74 compiler->temporaries = temporaries;
75 compiler->saveds = saveds;
76 compiler->args = args;
77 compiler->flags_saved = 0;
78
79 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
80 size = 1 + (saveds <= 3 ? saveds : 3) + (args > 0 ? (args * 2) : 0) + (args > 2 ? 2 : 0);
81 #else
82 size = 1 + (saveds <= 3 ? saveds : 3) + (args > 0 ? (2 + args * 3) : 0);
83 #endif
84 buf = (sljit_ub*)ensure_buf(compiler, 1 + size);
85 FAIL_IF(!buf);
86
87 INC_SIZE(size);
88 PUSH_REG(reg_map[TMP_REGISTER]);
89 #if !(defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
90 if (args > 0) {
91 *buf++ = 0x8b;
92 *buf++ = 0xc4 | (reg_map[TMP_REGISTER] << 3);
93 }
94 #endif
95 if (saveds > 2)
96 PUSH_REG(reg_map[SLJIT_SAVED_REG3]);
97 if (saveds > 1)
98 PUSH_REG(reg_map[SLJIT_SAVED_REG2]);
99 if (saveds > 0)
100 PUSH_REG(reg_map[SLJIT_SAVED_REG1]);
101
102 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
103 if (args > 0) {
104 *buf++ = 0x8b;
105 *buf++ = 0xc0 | (reg_map[SLJIT_SAVED_REG1] << 3) | reg_map[SLJIT_TEMPORARY_REG3];
106 }
107 if (args > 1) {
108 *buf++ = 0x8b;
109 *buf++ = 0xc0 | (reg_map[SLJIT_SAVED_REG2] << 3) | reg_map[SLJIT_TEMPORARY_REG2];
110 }
111 if (args > 2) {
112 *buf++ = 0x8b;
113 *buf++ = 0x44 | (reg_map[SLJIT_SAVED_REG3] << 3);
114 *buf++ = 0x24;
115 *buf++ = sizeof(sljit_w) * (3 + 2); /* saveds >= 3 as well. */
116 }
117 #else
118 if (args > 0) {
119 *buf++ = 0x8b;
120 *buf++ = 0x40 | (reg_map[SLJIT_SAVED_REG1] << 3) | reg_map[TMP_REGISTER];
121 *buf++ = sizeof(sljit_w) * 2;
122 }
123 if (args > 1) {
124 *buf++ = 0x8b;
125 *buf++ = 0x40 | (reg_map[SLJIT_SAVED_REG2] << 3) | reg_map[TMP_REGISTER];
126 *buf++ = sizeof(sljit_w) * 3;
127 }
128 if (args > 2) {
129 *buf++ = 0x8b;
130 *buf++ = 0x40 | (reg_map[SLJIT_SAVED_REG3] << 3) | reg_map[TMP_REGISTER];
131 *buf++ = sizeof(sljit_w) * 4;
132 }
133 #endif
134
135 local_size = (local_size + sizeof(sljit_uw) - 1) & ~(sizeof(sljit_uw) - 1);
136 compiler->temporaries_start = local_size;
137 if (temporaries > 3)
138 local_size += (temporaries - 3) * sizeof(sljit_uw);
139 compiler->saveds_start = local_size;
140 if (saveds > 3)
141 local_size += (saveds - 3) * sizeof(sljit_uw);
142
143 #ifdef _WIN32
144 if (local_size > 1024) {
145 FAIL_IF(emit_do_imm(compiler, 0xb8 + reg_map[SLJIT_TEMPORARY_REG1], local_size));
146 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(sljit_touch_stack)));
147 }
148 #endif
149
150 compiler->local_size = local_size;
151 if (local_size > 0)
152 return emit_non_cum_binary(compiler, 0x2b, 0x29, 0x5 << 3, 0x2d,
153 SLJIT_LOCALS_REG, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, local_size);
154
155 return SLJIT_SUCCESS;
156 }
157
158 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, int args, int temporaries, int saveds, int local_size)
159 {
160 CHECK_ERROR_VOID();
161 check_sljit_set_context(compiler, args, temporaries, saveds, local_size);
162
163 compiler->temporaries = temporaries;
164 compiler->saveds = saveds;
165 compiler->args = args;
166 compiler->local_size = (local_size + sizeof(sljit_uw) - 1) & ~(sizeof(sljit_uw) - 1);
167 compiler->temporaries_start = compiler->local_size;
168 if (temporaries > 3)
169 compiler->local_size += (temporaries - 3) * sizeof(sljit_uw);
170 compiler->saveds_start = compiler->local_size;
171 if (saveds > 3)
172 compiler->local_size += (saveds - 3) * sizeof(sljit_uw);
173 }
174
175 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int op, int src, sljit_w srcw)
176 {
177 int size;
178 sljit_ub *buf;
179
180 CHECK_ERROR();
181 check_sljit_emit_return(compiler, op, src, srcw);
182 SLJIT_ASSERT(compiler->args >= 0);
183
184 compiler->flags_saved = 0;
185 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
186
187 if (compiler->local_size > 0)
188 FAIL_IF(emit_cum_binary(compiler, 0x03, 0x01, 0x0 << 3, 0x05,
189 SLJIT_LOCALS_REG, 0, SLJIT_LOCALS_REG, 0, SLJIT_IMM, compiler->local_size));
190
191 size = 2 + (compiler->saveds <= 3 ? compiler->saveds : 3);
192 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
193 if (compiler->args > 2)
194 size += 2;
195 #else
196 if (compiler->args > 0)
197 size += 2;
198 #endif
199 buf = (sljit_ub*)ensure_buf(compiler, 1 + size);
200 FAIL_IF(!buf);
201
202 INC_SIZE(size);
203
204 if (compiler->saveds > 0)
205 POP_REG(reg_map[SLJIT_SAVED_REG1]);
206 if (compiler->saveds > 1)
207 POP_REG(reg_map[SLJIT_SAVED_REG2]);
208 if (compiler->saveds > 2)
209 POP_REG(reg_map[SLJIT_SAVED_REG3]);
210 POP_REG(reg_map[TMP_REGISTER]);
211 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
212 if (compiler->args > 2)
213 RETN(sizeof(sljit_w));
214 else
215 RET();
216 #else
217 if (compiler->args > 0)
218 RETN(compiler->args * sizeof(sljit_w));
219 else
220 RET();
221 #endif
222
223 return SLJIT_SUCCESS;
224 }
225
226 /* --------------------------------------------------------------------- */
227 /* Operators */
228 /* --------------------------------------------------------------------- */
229
230 /* Size contains the flags as well. */
231 static sljit_ub* emit_x86_instruction(struct sljit_compiler *compiler, int size,
232 /* The register or immediate operand. */
233 int a, sljit_w imma,
234 /* The general operand (not immediate). */
235 int b, sljit_w immb)
236 {
237 sljit_ub *buf;
238 sljit_ub *buf_ptr;
239 int flags = size & ~0xf;
240 int inst_size;
241
242 /* Both cannot be switched on. */
243 SLJIT_ASSERT((flags & (EX86_BIN_INS | EX86_SHIFT_INS)) != (EX86_BIN_INS | EX86_SHIFT_INS));
244 /* Size flags not allowed for typed instructions. */
245 SLJIT_ASSERT(!(flags & (EX86_BIN_INS | EX86_SHIFT_INS)) || (flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) == 0);
246 /* Both size flags cannot be switched on. */
247 SLJIT_ASSERT((flags & (EX86_BYTE_ARG | EX86_HALF_ARG)) != (EX86_BYTE_ARG | EX86_HALF_ARG));
248 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
249 /* SSE2 and immediate is not possible. */
250 SLJIT_ASSERT(!(a & SLJIT_IMM) || !(flags & EX86_SSE2));
251 #endif
252
253 size &= 0xf;
254 inst_size = size;
255
256 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
257 if (flags & EX86_PREF_F2)
258 inst_size++;
259 #endif
260 if (flags & EX86_PREF_66)
261 inst_size++;
262
263 /* Calculate size of b. */
264 inst_size += 1; /* mod r/m byte. */
265 if (b & SLJIT_MEM) {
266 if ((b & 0x0f) == SLJIT_UNUSED)
267 inst_size += sizeof(sljit_w);
268 else if (immb != 0 && !(b & 0xf0)) {
269 /* Immediate operand. */
270 if (immb <= 127 && immb >= -128)
271 inst_size += sizeof(sljit_b);
272 else
273 inst_size += sizeof(sljit_w);
274 }
275
276 if ((b & 0xf) == SLJIT_LOCALS_REG && !(b & 0xf0))
277 b |= SLJIT_LOCALS_REG << 4;
278
279 if ((b & 0xf0) != SLJIT_UNUSED)
280 inst_size += 1; /* SIB byte. */
281 }
282
283 /* Calculate size of a. */
284 if (a & SLJIT_IMM) {
285 if (flags & EX86_BIN_INS) {
286 if (imma <= 127 && imma >= -128) {
287 inst_size += 1;
288 flags |= EX86_BYTE_ARG;
289 } else
290 inst_size += 4;
291 }
292 else if (flags & EX86_SHIFT_INS) {
293 imma &= 0x1f;
294 if (imma != 1) {
295 inst_size ++;
296 flags |= EX86_BYTE_ARG;
297 }
298 } else if (flags & EX86_BYTE_ARG)
299 inst_size++;
300 else if (flags & EX86_HALF_ARG)
301 inst_size += sizeof(short);
302 else
303 inst_size += sizeof(sljit_w);
304 }
305 else
306 SLJIT_ASSERT(!(flags & EX86_SHIFT_INS) || a == SLJIT_PREF_SHIFT_REG);
307
308 buf = (sljit_ub*)ensure_buf(compiler, 1 + inst_size);
309 PTR_FAIL_IF(!buf);
310
311 /* Encoding the byte. */
312 INC_SIZE(inst_size);
313 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
314 if (flags & EX86_PREF_F2)
315 *buf++ = 0xf2;
316 #endif
317 if (flags & EX86_PREF_66)
318 *buf++ = 0x66;
319
320 buf_ptr = buf + size;
321
322 /* Encode mod/rm byte. */
323 if (!(flags & EX86_SHIFT_INS)) {
324 if ((flags & EX86_BIN_INS) && (a & SLJIT_IMM))
325 *buf = (flags & EX86_BYTE_ARG) ? 0x83 : 0x81;
326
327 if ((a & SLJIT_IMM) || (a == 0))
328 *buf_ptr = 0;
329 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
330 else if (!(flags & EX86_SSE2))
331 *buf_ptr = reg_map[a] << 3;
332 else
333 *buf_ptr = a << 3;
334 #else
335 else
336 *buf_ptr = reg_map[a] << 3;
337 #endif
338 }
339 else {
340 if (a & SLJIT_IMM) {
341 if (imma == 1)
342 *buf = 0xd1;
343 else
344 *buf = 0xc1;
345 } else
346 *buf = 0xd3;
347 *buf_ptr = 0;
348 }
349
350 if (!(b & SLJIT_MEM))
351 #if (defined SLJIT_SSE2 && SLJIT_SSE2)
352 *buf_ptr++ |= 0xc0 + ((!(flags & EX86_SSE2)) ? reg_map[b] : b);
353 #else
354 *buf_ptr++ |= 0xc0 + reg_map[b];
355 #endif
356 else if ((b & 0x0f) != SLJIT_UNUSED) {
357 if ((b & 0xf0) == SLJIT_UNUSED || (b & 0xf0) == (SLJIT_LOCALS_REG << 4)) {
358 if (immb != 0) {
359 if (immb <= 127 && immb >= -128)
360 *buf_ptr |= 0x40;
361 else
362 *buf_ptr |= 0x80;
363 }
364
365 if ((b & 0xf0) == SLJIT_UNUSED)
366 *buf_ptr++ |= reg_map[b & 0x0f];
367 else {
368 *buf_ptr++ |= 0x04;
369 *buf_ptr++ = reg_map[b & 0x0f] | (reg_map[(b >> 4) & 0x0f] << 3);
370 }
371
372 if (immb != 0) {
373 if (immb <= 127 && immb >= -128)
374 *buf_ptr++ = immb; /* 8 bit displacement. */
375 else {
376 *(sljit_w*)buf_ptr = immb; /* 32 bit displacement. */
377 buf_ptr += sizeof(sljit_w);
378 }
379 }
380 }
381 else {
382 *buf_ptr++ |= 0x04;
383 *buf_ptr++ = reg_map[b & 0x0f] | (reg_map[(b >> 4) & 0x0f] << 3) | (immb << 6);
384 }
385 }
386 else {
387 *buf_ptr++ |= 0x05;
388 *(sljit_w*)buf_ptr = immb; /* 32 bit displacement. */
389 buf_ptr += sizeof(sljit_w);
390 }
391
392 if (a & SLJIT_IMM) {
393 if (flags & EX86_BYTE_ARG)
394 *buf_ptr = imma;
395 else if (flags & EX86_HALF_ARG)
396 *(short*)buf_ptr = imma;
397 else if (!(flags & EX86_SHIFT_INS))
398 *(sljit_w*)buf_ptr = imma;
399 }
400
401 return !(flags & EX86_SHIFT_INS) ? buf : (buf + 1);
402 }
403
404 /* --------------------------------------------------------------------- */
405 /* Call / return instructions */
406 /* --------------------------------------------------------------------- */
407
408 static SLJIT_INLINE int call_with_args(struct sljit_compiler *compiler, int type)
409 {
410 sljit_ub *buf;
411
412 #if (defined SLJIT_X86_32_FASTCALL && SLJIT_X86_32_FASTCALL)
413 buf = (sljit_ub*)ensure_buf(compiler, type >= SLJIT_CALL3 ? 1 + 2 + 1 : 1 + 2);
414 FAIL_IF(!buf);
415 INC_SIZE(type >= SLJIT_CALL3 ? 2 + 1 : 2);
416
417 if (type >= SLJIT_CALL3)
418 PUSH_REG(reg_map[SLJIT_TEMPORARY_REG3]);
419 *buf++ = 0x8b;
420 *buf++ = 0xc0 | (reg_map[SLJIT_TEMPORARY_REG3] << 3) | reg_map[SLJIT_TEMPORARY_REG1];
421 #else
422 buf = (sljit_ub*)ensure_buf(compiler, type - SLJIT_CALL0 + 1);
423 FAIL_IF(!buf);
424 INC_SIZE(type - SLJIT_CALL0);
425 if (type >= SLJIT_CALL3)
426 PUSH_REG(reg_map[SLJIT_TEMPORARY_REG3]);
427 if (type >= SLJIT_CALL2)
428 PUSH_REG(reg_map[SLJIT_TEMPORARY_REG2]);
429 PUSH_REG(reg_map[SLJIT_TEMPORARY_REG1]);
430 #endif
431 return SLJIT_SUCCESS;
432 }
433
434 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int saveds, int local_size)
435 {
436 sljit_ub *buf;
437
438 CHECK_ERROR();
439 check_sljit_emit_fast_enter(compiler, dst, dstw, args, temporaries, saveds, local_size);
440
441 compiler->temporaries = temporaries;
442 compiler->saveds = saveds;
443 compiler->args = args;
444 compiler->local_size = (local_size + sizeof(sljit_uw) - 1) & ~(sizeof(sljit_uw) - 1);
445 compiler->temporaries_start = compiler->local_size;
446 if (temporaries > 3)
447 compiler->local_size += (temporaries - 3) * sizeof(sljit_uw);
448 compiler->saveds_start = compiler->local_size;
449 if (saveds > 3)
450 compiler->local_size += (saveds - 3) * sizeof(sljit_uw);
451
452 CHECK_EXTRA_REGS(dst, dstw, (void)0);
453
454 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) {
455 buf = (sljit_ub*)ensure_buf(compiler, 1 + 1);
456 FAIL_IF(!buf);
457
458 INC_SIZE(1);
459 POP_REG(reg_map[dst]);
460 return SLJIT_SUCCESS;
461 }
462 else if (dst & SLJIT_MEM) {
463 buf = emit_x86_instruction(compiler, 1, 0, 0, dst, dstw);
464 FAIL_IF(!buf);
465 *buf++ = 0x8f;
466 return SLJIT_SUCCESS;
467 }
468
469 /* For UNUSED dst. Uncommon, but possible. */
470 buf = (sljit_ub*)ensure_buf(compiler, 1 + 1);
471 FAIL_IF(!buf);
472
473 INC_SIZE(1);
474 POP_REG(reg_map[TMP_REGISTER]);
475 return SLJIT_SUCCESS;
476 }
477
478 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
479 {
480 sljit_ub *buf;
481
482 CHECK_ERROR();
483 check_sljit_emit_fast_return(compiler, src, srcw);
484
485 CHECK_EXTRA_REGS(src, srcw, (void)0);
486
487 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS) {
488 buf = (sljit_ub*)ensure_buf(compiler, 1 + 1 + 1);
489 FAIL_IF(!buf);
490
491 INC_SIZE(1 + 1);
492 PUSH_REG(reg_map[src]);
493 }
494 else if (src & SLJIT_MEM) {
495 buf = emit_x86_instruction(compiler, 1, 0, 0, src, srcw);
496 FAIL_IF(!buf);
497 *buf++ = 0xff;
498 *buf |= 6 << 3;
499
500 buf = (sljit_ub*)ensure_buf(compiler, 1 + 1);
501 FAIL_IF(!buf);
502 INC_SIZE(1);
503 }
504 else {
505 /* SLJIT_IMM. */
506 buf = (sljit_ub*)ensure_buf(compiler, 1 + 5 + 1);
507 FAIL_IF(!buf);
508
509 INC_SIZE(5 + 1);
510 *buf++ = 0x68;
511 *(sljit_w*)buf = srcw;
512 buf += sizeof(sljit_w);
513 }
514
515 RET();
516 return SLJIT_SUCCESS;
517 }

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12