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

  ViewVC Help
Powered by ViewVC 1.1.5