/[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 662 - (show annotations) (download)
Mon Aug 22 14:35:22 2011 UTC (3 years, 2 months ago) by ph10
File MIME type: text/plain
File size: 14780 byte(s)
JIT compiler source

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12