/[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 847 - (show annotations)
Tue Jan 3 17:49:03 2012 UTC (7 years, 8 months ago) by zherczeg
File MIME type: text/plain
File size: 14948 byte(s)
fix signed/unsigned half load mismatches 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 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 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_SET_FLAGS))
167 return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
168 BIN_EXTS();
169 return push_inst(compiler, ADDC | OERC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
170
171 case SLJIT_ADDC:
172 if (flags & ALT_FORM1) {
173 FAIL_IF(push_inst(compiler, MFXER | S(0)));
174 FAIL_IF(push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2)));
175 return push_inst(compiler, MTXER | S(0));
176 }
177 BIN_EXTS();
178 return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
179
180 case SLJIT_SUB:
181 if (flags & ALT_FORM1) {
182 /* Flags not set: BIN_IMM_EXTS unnecessary. */
183 SLJIT_ASSERT(src2 == TMP_REG2);
184 return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
185 }
186 if (flags & ALT_FORM2) {
187 SLJIT_ASSERT(src2 == TMP_REG2);
188 return push_inst(compiler, CMPI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm);
189 }
190 if (flags & ALT_FORM3) {
191 SLJIT_ASSERT(src2 == TMP_REG2);
192 return push_inst(compiler, CMPLI | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm);
193 }
194 if (flags & ALT_FORM4)
195 return push_inst(compiler, CMPL | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2));
196 if (!(flags & ALT_SET_FLAGS))
197 return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
198 BIN_EXTS();
199 if (flags & ALT_FORM5)
200 FAIL_IF(push_inst(compiler, CMPL | CRD(4 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
201 return push_inst(compiler, SUBFC | OERC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
202
203 case SLJIT_SUBC:
204 if (flags & ALT_FORM1) {
205 FAIL_IF(push_inst(compiler, MFXER | S(0)));
206 FAIL_IF(push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1)));
207 return push_inst(compiler, MTXER | S(0));
208 }
209 BIN_EXTS();
210 return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
211
212 case SLJIT_MUL:
213 if (flags & ALT_FORM1) {
214 SLJIT_ASSERT(src2 == TMP_REG2);
215 return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
216 }
217 BIN_EXTS();
218 if (flags & ALT_FORM2)
219 return push_inst(compiler, MULLW | OERC(flags) | D(dst) | A(src2) | B(src1));
220 return push_inst(compiler, MULLD | OERC(flags) | D(dst) | A(src2) | B(src1));
221
222 case SLJIT_AND:
223 if (flags & ALT_FORM1) {
224 SLJIT_ASSERT(src2 == TMP_REG2);
225 return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
226 }
227 if (flags & ALT_FORM2) {
228 SLJIT_ASSERT(src2 == TMP_REG2);
229 return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
230 }
231 return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
232
233 case SLJIT_OR:
234 if (flags & ALT_FORM1) {
235 SLJIT_ASSERT(src2 == TMP_REG2);
236 return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
237 }
238 if (flags & ALT_FORM2) {
239 SLJIT_ASSERT(src2 == TMP_REG2);
240 return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
241 }
242 if (flags & ALT_FORM3) {
243 SLJIT_ASSERT(src2 == TMP_REG2);
244 FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
245 return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
246 }
247 return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
248
249 case SLJIT_XOR:
250 if (flags & ALT_FORM1) {
251 SLJIT_ASSERT(src2 == TMP_REG2);
252 return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
253 }
254 if (flags & ALT_FORM2) {
255 SLJIT_ASSERT(src2 == TMP_REG2);
256 return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
257 }
258 if (flags & ALT_FORM3) {
259 SLJIT_ASSERT(src2 == TMP_REG2);
260 FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
261 return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
262 }
263 return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
264
265 case SLJIT_SHL:
266 if (flags & ALT_FORM1) {
267 SLJIT_ASSERT(src2 == TMP_REG2);
268 if (flags & ALT_FORM2) {
269 compiler->imm &= 0x1f;
270 return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
271 }
272 else {
273 compiler->imm &= 0x3f;
274 return push_inst(compiler, RLDI(dst, src1, compiler->imm, 63 - compiler->imm, 1) | RC(flags));
275 }
276 }
277 if (flags & ALT_FORM2)
278 return push_inst(compiler, SLW | RC(flags) | S(src1) | A(dst) | B(src2));
279 return push_inst(compiler, SLD | RC(flags) | S(src1) | A(dst) | B(src2));
280
281 case SLJIT_LSHR:
282 if (flags & ALT_FORM1) {
283 SLJIT_ASSERT(src2 == TMP_REG2);
284 if (flags & ALT_FORM2) {
285 compiler->imm &= 0x1f;
286 return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
287 }
288 else {
289 compiler->imm &= 0x3f;
290 return push_inst(compiler, RLDI(dst, src1, 64 - compiler->imm, compiler->imm, 0) | RC(flags));
291 }
292 }
293 if (flags & ALT_FORM2)
294 return push_inst(compiler, SRW | RC(flags) | S(src1) | A(dst) | B(src2));
295 return push_inst(compiler, SRD | RC(flags) | S(src1) | A(dst) | B(src2));
296
297 case SLJIT_ASHR:
298 if (flags & ALT_FORM1) {
299 SLJIT_ASSERT(src2 == TMP_REG2);
300 if (flags & ALT_FORM2) {
301 compiler->imm &= 0x1f;
302 return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
303 }
304 else {
305 compiler->imm &= 0x3f;
306 return push_inst(compiler, SRADI | RC(flags) | S(src1) | A(dst) | ((compiler->imm & 0x1f) << 11) | ((compiler->imm & 0x20) >> 4));
307 }
308 }
309 if (flags & ALT_FORM2)
310 return push_inst(compiler, SRAW | RC(flags) | S(src1) | A(dst) | B(src2));
311 return push_inst(compiler, SRAD | RC(flags) | S(src1) | A(dst) | B(src2));
312
313 case SLJIT_MOV:
314 SLJIT_ASSERT(src1 == TMP_REG1);
315 if (dst != src2)
316 return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
317 return SLJIT_SUCCESS;
318
319 case SLJIT_MOV_UI:
320 case SLJIT_MOV_SI:
321 SLJIT_ASSERT(src1 == TMP_REG1);
322 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
323 if (op == SLJIT_MOV_SI)
324 return push_inst(compiler, EXTSW | S(src2) | A(dst));
325 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 0));
326 }
327 else if (dst != src2)
328 SLJIT_ASSERT_STOP();
329 return SLJIT_SUCCESS;
330
331 case SLJIT_MOV_UB:
332 case SLJIT_MOV_SB:
333 SLJIT_ASSERT(src1 == TMP_REG1);
334 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
335 if (op == SLJIT_MOV_SB)
336 return push_inst(compiler, EXTSB | S(src2) | A(dst));
337 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
338 }
339 else if ((flags & REG_DEST) && op == SLJIT_MOV_SB)
340 return push_inst(compiler, EXTSB | S(src2) | A(dst));
341 else if (dst != src2)
342 SLJIT_ASSERT_STOP();
343 return SLJIT_SUCCESS;
344
345 case SLJIT_MOV_UH:
346 case SLJIT_MOV_SH:
347 SLJIT_ASSERT(src1 == TMP_REG1);
348 if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
349 if (op == SLJIT_MOV_SH)
350 return push_inst(compiler, EXTSH | S(src2) | A(dst));
351 return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
352 }
353 else if (dst != src2)
354 SLJIT_ASSERT_STOP();
355 return SLJIT_SUCCESS;
356
357 case SLJIT_NOT:
358 SLJIT_ASSERT(src1 == TMP_REG1);
359 UN_EXTS();
360 return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
361
362 case SLJIT_NEG:
363 SLJIT_ASSERT(src1 == TMP_REG1);
364 UN_EXTS();
365 return push_inst(compiler, NEG | OERC(flags) | D(dst) | A(src2));
366
367 case SLJIT_CLZ:
368 SLJIT_ASSERT(src1 == TMP_REG1);
369 if (flags & ALT_FORM1)
370 return push_inst(compiler, CNTLZW | RC(flags) | S(src2) | A(dst));
371 return push_inst(compiler, CNTLZD | RC(flags) | S(src2) | A(dst));
372 }
373
374 SLJIT_ASSERT_STOP();
375 return SLJIT_SUCCESS;
376 }
377
378 static SLJIT_INLINE int emit_const(struct sljit_compiler *compiler, int reg, sljit_w init_value)
379 {
380 FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 48)));
381 FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value >> 32)));
382 FAIL_IF(PUSH_RLDICR(reg, 31));
383 FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(init_value >> 16)));
384 return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
385 }
386
387 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
388 {
389 sljit_ins *inst = (sljit_ins*)addr;
390
391 inst[0] = (inst[0] & 0xffff0000) | ((new_addr >> 48) & 0xffff);
392 inst[1] = (inst[1] & 0xffff0000) | ((new_addr >> 32) & 0xffff);
393 inst[3] = (inst[3] & 0xffff0000) | ((new_addr >> 16) & 0xffff);
394 inst[4] = (inst[4] & 0xffff0000) | (new_addr & 0xffff);
395 SLJIT_CACHE_FLUSH(inst, inst + 5);
396 }
397
398 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_w new_constant)
399 {
400 sljit_ins *inst = (sljit_ins*)addr;
401
402 inst[0] = (inst[0] & 0xffff0000) | ((new_constant >> 48) & 0xffff);
403 inst[1] = (inst[1] & 0xffff0000) | ((new_constant >> 32) & 0xffff);
404 inst[3] = (inst[3] & 0xffff0000) | ((new_constant >> 16) & 0xffff);
405 inst[4] = (inst[4] & 0xffff0000) | (new_constant & 0xffff);
406 SLJIT_CACHE_FLUSH(inst, inst + 5);
407 }
408
409 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_w addr, void* func)
410 {
411 sljit_w* ptrs;
412 if (func_ptr)
413 *func_ptr = (void*)context;
414 ptrs = (sljit_w*)func;
415 context->addr = addr ? addr : ptrs[0];
416 context->r2 = ptrs[1];
417 context->r11 = ptrs[2];
418 }

  ViewVC Help
Powered by ViewVC 1.1.5