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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 860 - (show annotations)
Mon Jan 9 20:12:58 2012 UTC (7 years, 8 months ago) by zherczeg
File MIME type: text/plain
File size: 15489 byte(s)
rename PCRE_SCHAR16 to PCRE_UCHAR16 and 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 /* ppc 64-bit arch dependent functions. */
28
29 #ifdef __GNUC__
30 #define ASM_SLJIT_CLZ(src, dst) \
31 asm volatile ( "cntlzd %0, %1" : "=r"(dst) : "r"(src) )
32 #else
33 #error "Must implement count leading zeroes"
34 #endif
35
36 #define RLDI(dst, src, sh, mb, type) \
37 (HI(30) | S(src) | A(dst) | ((type) << 2) | (((sh) & 0x1f) << 11) | (((sh) & 0x20) >> 4) | (((mb) & 0x1f) << 6) | ((mb) & 0x20))
38
39 #define PUSH_RLDICR(reg, shift) \
40 push_inst(compiler, RLDI(reg, reg, 63 - shift, shift, 1))
41
42 static int load_immediate(struct sljit_compiler *compiler, int reg, sljit_w imm)
43 {
44 sljit_uw tmp;
45 sljit_uw shift;
46 sljit_uw tmp2;
47 sljit_uw shift2;
48
49 if (imm <= SIMM_MAX && imm >= SIMM_MIN)
50 return push_inst(compiler, ADDI | D(reg) | A(0) | IMM(imm));
51
52 if (!(imm & ~0xffff))
53 return push_inst(compiler, ORI | S(ZERO_REG) | A(reg) | IMM(imm));
54
55 if (imm <= SLJIT_W(0x7fffffff) && imm >= SLJIT_W(-0x80000000)) {
56 FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 16)));
57 return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm)) : SLJIT_SUCCESS;
58 }
59
60 /* Count leading zeroes. */
61 tmp = (imm >= 0) ? imm : ~imm;
62 ASM_SLJIT_CLZ(tmp, shift);
63 SLJIT_ASSERT(shift > 0);
64 shift--;
65 tmp = (imm << shift);
66
67 if ((tmp & ~0xffff000000000000ul) == 0) {
68 FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
69 shift += 15;
70 return PUSH_RLDICR(reg, shift);
71 }
72
73 if ((tmp & ~0xffffffff00000000ul) == 0) {
74 FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(tmp >> 48)));
75 FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp >> 32)));
76 shift += 31;
77 return PUSH_RLDICR(reg, shift);
78 }
79
80 /* Cut out the 16 bit from immediate. */
81 shift += 15;
82 tmp2 = imm & ((1ul << (63 - shift)) - 1);
83
84 if (tmp2 <= 0xffff) {
85 FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
86 FAIL_IF(PUSH_RLDICR(reg, shift));
87 return push_inst(compiler, ORI | S(reg) | A(reg) | tmp2);
88 }
89
90 if (tmp2 <= 0xffffffff) {
91 FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
92 FAIL_IF(PUSH_RLDICR(reg, shift));
93 FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | (tmp2 >> 16)));
94 return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp2)) : SLJIT_SUCCESS;
95 }
96
97 ASM_SLJIT_CLZ(tmp2, shift2);
98 tmp2 <<= shift2;
99
100 if ((tmp2 & ~0xffff000000000000ul) == 0) {
101 FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
102 shift2 += 15;
103 shift += (63 - shift2);
104 FAIL_IF(PUSH_RLDICR(reg, shift));
105 FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | (tmp2 >> 48)));
106 return PUSH_RLDICR(reg, shift2);
107 }
108
109 /* The general version. */
110 FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 48)));
111 FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm >> 32)));
112 FAIL_IF(PUSH_RLDICR(reg, 31));
113 FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(imm >> 16)));
114 return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm));
115 }
116
117 /* Simplified mnemonics: clrldi. */
118 #define INS_CLEAR_LEFT(dst, src, from) \
119 (RLDICL | S(src) | A(dst) | ((from) << 6) | (1 << 5))
120
121 /* Sign extension for integer operations. */
122 #define UN_EXTS() \
123 if ((flags & (ALT_SIGN_EXT | REG2_SOURCE)) == (ALT_SIGN_EXT | REG2_SOURCE)) { \
124 FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
125 src2 = TMP_REG2; \
126 }
127
128 #define BIN_EXTS() \
129 if (flags & ALT_SIGN_EXT) { \
130 if (flags & REG1_SOURCE) { \
131 FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
132 src1 = TMP_REG1; \
133 } \
134 if (flags & REG2_SOURCE) { \
135 FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
136 src2 = TMP_REG2; \
137 } \
138 }
139
140 #define BIN_IMM_EXTS() \
141 if ((flags & (ALT_SIGN_EXT | REG1_SOURCE)) == (ALT_SIGN_EXT | REG1_SOURCE)) { \
142 FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
143 src1 = TMP_REG1; \
144 }
145
146 static SLJIT_INLINE int emit_single_op(struct sljit_compiler *compiler, int op, int flags,
147 int dst, int src1, int src2)
148 {
149 switch (op) {
150 case SLJIT_ADD:
151 if (flags & ALT_FORM1) {
152 /* Flags does not set: BIN_IMM_EXTS unnecessary. */
153 SLJIT_ASSERT(src2 == TMP_REG2);
154 return push_inst(compiler, ADDI | D(dst) | A(src1) | compiler->imm);
155 }
156 if (flags & ALT_FORM2) {
157 /* Flags does not set: BIN_IMM_EXTS unnecessary. */
158 SLJIT_ASSERT(src2 == TMP_REG2);
159 return push_inst(compiler, ADDIS | D(dst) | A(src1) | compiler->imm);
160 }
161 if (flags & ALT_FORM3) {
162 SLJIT_ASSERT(src2 == TMP_REG2);
163 BIN_IMM_EXTS();
164 return push_inst(compiler, ADDIC | D(dst) | A(src1) | compiler->imm);
165 }
166 if (flags & ALT_FORM4) {
167 /* Flags does not set: BIN_IMM_EXTS unnecessary. */
168 FAIL_IF(push_inst(compiler, ADDI | D(dst) | A(src1) | (compiler->imm & 0xffff)));
169 return push_inst(compiler, ADDIS | D(dst) | A(dst) | (((compiler->imm >> 16) & 0xffff) + ((compiler->imm >> 15) & 0x1)));
170 }
171 if (!(flags & ALT_SET_FLAGS))
172 return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
173 BIN_EXTS();
174 return push_inst(compiler, ADDC | OERC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
175
176 case SLJIT_ADDC:
177 if (flags & ALT_FORM1) {
178 FAIL_IF(push_inst(compiler, MFXER | S(0)));
179 FAIL_IF(push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2)));
180 return push_inst(compiler, MTXER | S(0));
181 }
182 BIN_EXTS();
183 return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
184
185 case SLJIT_SUB:
186 if (flags & ALT_FORM1) {
187 /* Flags does not set: BIN_IMM_EXTS unnecessary. */
188 SLJIT_ASSERT(src2 == TMP_REG2);
189 return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
190 }
191 if (flags & (ALT_FORM2 | ALT_FORM3)) {
192 SLJIT_ASSERT(src2 == TMP_REG2);
193 if (flags & ALT_FORM2)
194 FAIL_IF(push_inst(compiler, CMPI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm));
195 if (flags & ALT_FORM3)
196 return push_inst(compiler, CMPLI | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm);
197 return SLJIT_SUCCESS;
198 }
199 if (flags & (ALT_FORM4 | ALT_FORM5)) {
200 if (flags & ALT_FORM4)
201 FAIL_IF(push_inst(compiler, CMPL | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
202 if (flags & ALT_FORM5)
203 return push_inst(compiler, CMP | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2));
204 return SLJIT_SUCCESS;
205 }
206 if (!(flags & ALT_SET_FLAGS))
207 return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
208 BIN_EXTS();
209 if (flags & ALT_FORM6)
210 FAIL_IF(push_inst(compiler, CMPL | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
211 return push_inst(compiler, SUBFC | OERC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
212
213 case SLJIT_SUBC:
214 if (flags & ALT_FORM1) {
215 FAIL_IF(push_inst(compiler, MFXER | S(0)));
216 FAIL_IF(push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1)));
217 return push_inst(compiler, MTXER | S(0));
218 }
219 BIN_EXTS();
220 return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
221
222 case SLJIT_MUL:
223 if (flags & ALT_FORM1) {
224 SLJIT_ASSERT(src2 == TMP_REG2);
225 return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
226 }
227 BIN_EXTS();
228 if (flags & ALT_FORM2)
229 return push_inst(compiler, MULLW | OERC(flags) | D(dst) | A(src2) | B(src1));
230 return push_inst(compiler, MULLD | OERC(flags) | D(dst) | A(src2) | B(src1));
231
232 case SLJIT_AND:
233 if (flags & ALT_FORM1) {
234 SLJIT_ASSERT(src2 == TMP_REG2);
235 return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
236 }
237 if (flags & ALT_FORM2) {
238 SLJIT_ASSERT(src2 == TMP_REG2);
239 return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
240 }
241 return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
242
243 case SLJIT_OR:
244 if (flags & ALT_FORM1) {
245 SLJIT_ASSERT(src2 == TMP_REG2);
246 return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
247 }
248 if (flags & ALT_FORM2) {
249 SLJIT_ASSERT(src2 == TMP_REG2);
250 return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
251 }
252 if (flags & ALT_FORM3) {
253 SLJIT_ASSERT(src2 == TMP_REG2);
254 FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
255 return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
256 }
257 return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
258
259 case SLJIT_XOR:
260 if (flags & ALT_FORM1) {
261 SLJIT_ASSERT(src2 == TMP_REG2);
262 return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
263 }
264 if (flags & ALT_FORM2) {
265 SLJIT_ASSERT(src2 == TMP_REG2);
266 return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
267 }
268 if (flags & ALT_FORM3) {
269 SLJIT_ASSERT(src2 == TMP_REG2);
270 FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
271 return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
272 }
273 return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
274
275 case SLJIT_SHL:
276 if (flags & ALT_FORM1) {
277 SLJIT_ASSERT(src2 == TMP_REG2);
278 if (flags & ALT_FORM2) {
279 compiler->imm &= 0x1f;
280 return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
281 }
282 else {
283 compiler->imm &= 0x3f;
284 return push_inst(compiler, RLDI(dst, src1, compiler->imm, 63 - compiler->imm, 1) | RC(flags));
285 }
286 }
287 if (flags & ALT_FORM2)
288 return push_inst(compiler, SLW | RC(flags) | S(src1) | A(dst) | B(src2));
289 return push_inst(compiler, SLD | RC(flags) | S(src1) | A(dst) | B(src2));
290
291 case SLJIT_LSHR:
292 if (flags & ALT_FORM1) {
293 SLJIT_ASSERT(src2 == TMP_REG2);
294 if (flags & ALT_FORM2) {
295 compiler->imm &= 0x1f;
296 return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
297 }
298 else {
299 compiler->imm &= 0x3f;
300 return push_inst(compiler, RLDI(dst, src1, 64 - compiler->imm, compiler->imm, 0) | RC(flags));
301 }
302 }
303 if (flags & ALT_FORM2)
304 return push_inst(compiler, SRW | RC(flags) | S(src1) | A(dst) | B(src2));
305 return push_inst(compiler, SRD | RC(flags) | S(src1) | A(dst) | B(src2));
306
307 case SLJIT_ASHR:
308 if (flags & ALT_FORM1) {
309 SLJIT_ASSERT(src2 == TMP_REG2);
310 if (flags & ALT_FORM2) {
311 compiler->imm &= 0x1f;
312 return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
313 }
314 else {
315 compiler->imm &= 0x3f;
316 return push_inst(compiler, SRADI | RC(flags) | S(src1) | A(dst) | ((compiler->imm & 0x1f) << 11) | ((compiler->imm & 0x20) >> 4));
317 }
318 }
319 if (flags & ALT_FORM2)
320 return push_inst(compiler, SRAW | RC(flags) | S(src1) | A(dst) | B(src2));
321 return push_inst(compiler, SRAD | RC(flags) | S(src1) | A(dst) | B(src2));
322
323 case SLJIT_MOV:
324 SLJIT_ASSERT(src1 == TMP_REG1);
325 if (dst != src2)
326 return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
327 return SLJIT_SUCCESS;
328
329 case SLJIT_MOV_UI:
330 case SLJIT_MOV_SI:
331 SLJIT_ASSERT(src1 == TMP_REG1);
332 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
333 if (op == SLJIT_MOV_SI)
334 return push_inst(compiler, EXTSW | S(src2) | A(dst));
335 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 0));
336 }
337 else if (dst != src2)
338 SLJIT_ASSERT_STOP();
339 return SLJIT_SUCCESS;
340
341 case SLJIT_MOV_UB:
342 case SLJIT_MOV_SB:
343 SLJIT_ASSERT(src1 == TMP_REG1);
344 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
345 if (op == SLJIT_MOV_SB)
346 return push_inst(compiler, EXTSB | S(src2) | A(dst));
347 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
348 }
349 else if ((flags & REG_DEST) && op == SLJIT_MOV_SB)
350 return push_inst(compiler, EXTSB | S(src2) | A(dst));
351 else if (dst != src2)
352 SLJIT_ASSERT_STOP();
353 return SLJIT_SUCCESS;
354
355 case SLJIT_MOV_UH:
356 case SLJIT_MOV_SH:
357 SLJIT_ASSERT(src1 == TMP_REG1);
358 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
359 if (op == SLJIT_MOV_SH)
360 return push_inst(compiler, EXTSH | S(src2) | A(dst));
361 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
362 }
363 else if (dst != src2)
364 SLJIT_ASSERT_STOP();
365 return SLJIT_SUCCESS;
366
367 case SLJIT_NOT:
368 SLJIT_ASSERT(src1 == TMP_REG1);
369 UN_EXTS();
370 return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
371
372 case SLJIT_NEG:
373 SLJIT_ASSERT(src1 == TMP_REG1);
374 UN_EXTS();
375 return push_inst(compiler, NEG | OERC(flags) | D(dst) | A(src2));
376
377 case SLJIT_CLZ:
378 SLJIT_ASSERT(src1 == TMP_REG1);
379 if (flags & ALT_FORM1)
380 return push_inst(compiler, CNTLZW | RC(flags) | S(src2) | A(dst));
381 return push_inst(compiler, CNTLZD | RC(flags) | S(src2) | A(dst));
382 }
383
384 SLJIT_ASSERT_STOP();
385 return SLJIT_SUCCESS;
386 }
387
388 static SLJIT_INLINE int emit_const(struct sljit_compiler *compiler, int reg, sljit_w init_value)
389 {
390 FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 48)));
391 FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value >> 32)));
392 FAIL_IF(PUSH_RLDICR(reg, 31));
393 FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(init_value >> 16)));
394 return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
395 }
396
397 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
398 {
399 sljit_ins *inst = (sljit_ins*)addr;
400
401 inst[0] = (inst[0] & 0xffff0000) | ((new_addr >> 48) & 0xffff);
402 inst[1] = (inst[1] & 0xffff0000) | ((new_addr >> 32) & 0xffff);
403 inst[3] = (inst[3] & 0xffff0000) | ((new_addr >> 16) & 0xffff);
404 inst[4] = (inst[4] & 0xffff0000) | (new_addr & 0xffff);
405 SLJIT_CACHE_FLUSH(inst, inst + 5);
406 }
407
408 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant)
409 {
410 sljit_ins *inst = (sljit_ins*)addr;
411
412 inst[0] = (inst[0] & 0xffff0000) | ((new_constant >> 48) & 0xffff);
413 inst[1] = (inst[1] & 0xffff0000) | ((new_constant >> 32) & 0xffff);
414 inst[3] = (inst[3] & 0xffff0000) | ((new_constant >> 16) & 0xffff);
415 inst[4] = (inst[4] & 0xffff0000) | (new_constant & 0xffff);
416 SLJIT_CACHE_FLUSH(inst, inst + 5);
417 }
418
419 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func)
420 {
421 sljit_w* ptrs;
422 if (func_ptr)
423 *func_ptr = (void*)context;
424 ptrs = (sljit_w*)func;
425 context->addr = addr ? addr : ptrs[0];
426 context->r2 = ptrs[1];
427 context->r11 = ptrs[2];
428 }

  ViewVC Help
Powered by ViewVC 1.1.5