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

  ViewVC Help
Powered by ViewVC 1.1.5