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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 740 - (show annotations)
Mon Oct 31 06:10:14 2011 UTC (7 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 58664 byte(s)
Updating the JIT compiler
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 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name()
28 {
29 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
30 return "ppc-32";
31 #else
32 return "ppc-64";
33 #endif
34 }
35
36 /* Length of an instruction word.
37 Both for ppc-32 and ppc-64. */
38 typedef sljit_ui sljit_ins;
39
40 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
41 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
42 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
43 #define ZERO_REG (SLJIT_NO_REGISTERS + 4)
44 #define REAL_STACK_PTR (SLJIT_NO_REGISTERS + 5)
45
46 #define TMP_FREG1 (SLJIT_FLOAT_REG4 + 1)
47 #define TMP_FREG2 (SLJIT_FLOAT_REG4 + 2)
48
49 /* --------------------------------------------------------------------- */
50 /* Instrucion forms */
51 /* --------------------------------------------------------------------- */
52 #define D(d) (reg_map[d] << 21)
53 #define S(s) (reg_map[s] << 21)
54 #define A(a) (reg_map[a] << 16)
55 #define B(b) (reg_map[b] << 11)
56 #define C(c) (reg_map[c] << 6)
57 #define FD(fd) ((fd) << 21)
58 #define FA(fa) ((fa) << 16)
59 #define FB(fb) ((fb) << 11)
60 #define FC(fc) ((fc) << 6)
61 #define IMM(imm) ((imm) & 0xffff)
62 #define CRD(d) ((d) << 21)
63
64 /* Instruction bit sections.
65 OE and Rc flag (see ALT_SET_FLAGS). */
66 #define OERC(flags) (((flags & ALT_SET_FLAGS) >> 15) | ((flags & ALT_SET_FLAGS) >> 5))
67 /* Rc flag (see ALT_SET_FLAGS). */
68 #define RC(flags) ((flags & ALT_SET_FLAGS) >> 15)
69 #define HI(opcode) ((opcode) << 26)
70 #define LO(opcode) ((opcode) << 1)
71
72 #define ADD (HI(31) | LO(266))
73 #define ADDC (HI(31) | LO(10))
74 #define ADDE (HI(31) | LO(138))
75 #define ADDI (HI(14))
76 #define ADDIC (HI(13))
77 #define ADDIS (HI(15))
78 #define ADDME (HI(31) | LO(234))
79 #define AND (HI(31) | LO(28))
80 #define ANDI (HI(28))
81 #define ANDIS (HI(29))
82 #define Bx (HI(18))
83 #define BCx (HI(16))
84 #define BCCTR (HI(19) | LO(528) | (3 << 11))
85 #define BLR (HI(19) | LO(16) | (0x14 << 21))
86 #define CNTLZD (HI(31) | LO(58))
87 #define CNTLZW (HI(31) | LO(26))
88 #define CMPI (HI(11))
89 #define CMPL (HI(31) | LO(32))
90 #define CMPLI (HI(10))
91 #define CROR (HI(19) | LO(449))
92 #define EXTSB (HI(31) | LO(954))
93 #define EXTSH (HI(31) | LO(922))
94 #define EXTSW (HI(31) | LO(986))
95 #define FABS (HI(63) | LO(264))
96 #define FADD (HI(63) | LO(21))
97 #define FCMPU (HI(63) | LO(0))
98 #define FDIV (HI(63) | LO(18))
99 #define FMR (HI(63) | LO(72))
100 #define FMUL (HI(63) | LO(25))
101 #define FNEG (HI(63) | LO(40))
102 #define FSUB (HI(63) | LO(20))
103 #define LD (HI(58) | 0)
104 #define LFD (HI(50))
105 #define LFDUX (HI(31) | LO(631))
106 #define LFDX (HI(31) | LO(599))
107 #define LWZ (HI(32))
108 #define MFCR (HI(31) | LO(19))
109 #define MFLR (HI(31) | LO(339) | 0x80000)
110 #define MFXER (HI(31) | LO(339) | 0x10000)
111 #define MTCTR (HI(31) | LO(467) | 0x90000)
112 #define MTLR (HI(31) | LO(467) | 0x80000)
113 #define MTXER (HI(31) | LO(467) | 0x10000)
114 #define MULLD (HI(31) | LO(233))
115 #define MULLI (HI(7))
116 #define MULLW (HI(31) | LO(235))
117 #define NEG (HI(31) | LO(104))
118 #define NOP (HI(24))
119 #define NOR (HI(31) | LO(124))
120 #define OR (HI(31) | LO(444))
121 #define ORI (HI(24))
122 #define ORIS (HI(25))
123 #define RLDICL (HI(30))
124 #define RLWINM (HI(21))
125 #define SLD (HI(31) | LO(27))
126 #define SLW (HI(31) | LO(24))
127 #define SRAD (HI(31) | LO(794))
128 #define SRADI (HI(31) | LO(413 << 1))
129 #define SRAW (HI(31) | LO(792))
130 #define SRAWI (HI(31) | LO(824))
131 #define SRD (HI(31) | LO(539))
132 #define SRW (HI(31) | LO(536))
133 #define STD (HI(62) | 0)
134 #define STDU (HI(62) | 1)
135 #define STDUX (HI(31) | LO(181))
136 #define STFD (HI(54))
137 #define STFDUX (HI(31) | LO(759))
138 #define STFDX (HI(31) | LO(727))
139 #define STW (HI(36))
140 #define STWU (HI(37))
141 #define STWUX (HI(31) | LO(183))
142 #define SUBF (HI(31) | LO(40))
143 #define SUBFC (HI(31) | LO(8))
144 #define SUBFE (HI(31) | LO(136))
145 #define SUBFIC (HI(8))
146 #define XOR (HI(31) | LO(316))
147 #define XORI (HI(26))
148 #define XORIS (HI(27))
149
150 #define SIMM_MAX (0x7fff)
151 #define SIMM_MIN (-0x8000)
152 #define UIMM_MAX (0xffff)
153
154 /* SLJIT_LOCALS_REG is not the real stack register, since it must
155 point to the head of the stack chain. */
156 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 6] = {
157 0, 3, 4, 5, 6, 7, 29, 28, 27, 26, 25, 31, 8, 9, 10, 30, 1
158 };
159
160 static int push_inst(struct sljit_compiler *compiler, sljit_ins ins)
161 {
162 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
163 FAIL_IF(!ptr);
164 *ptr = ins;
165 compiler->size++;
166 return SLJIT_SUCCESS;
167 }
168
169 static SLJIT_INLINE int optimize_jump(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
170 {
171 sljit_w diff;
172 sljit_uw target_addr;
173
174 if (jump->flags & SLJIT_REWRITABLE_JUMP)
175 return 0;
176
177 if (jump->flags & JUMP_ADDR)
178 target_addr = jump->u.target;
179 else {
180 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
181 target_addr = (sljit_uw)(code + jump->u.label->size);
182 }
183 diff = ((sljit_w)target_addr - (sljit_w)(code_ptr)) & ~0x3l;
184
185 if (jump->flags & UNCOND_B) {
186 if (diff <= 0x01ffffff && diff >= -0x02000000) {
187 jump->flags |= PATCH_B;
188 return 1;
189 }
190 if (target_addr <= 0x03ffffff) {
191 jump->flags |= PATCH_B | ABSOLUTE_B;
192 return 1;
193 }
194 }
195 else {
196 if (diff <= 0x7fff && diff >= -0x8000) {
197 jump->flags |= PATCH_B;
198 return 1;
199 }
200 if (target_addr <= 0xffff) {
201 jump->flags |= PATCH_B | ABSOLUTE_B;
202 return 1;
203 }
204 }
205 return 0;
206 }
207
208 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
209 {
210 struct sljit_memory_fragment *buf;
211 sljit_ins *code;
212 sljit_ins *code_ptr;
213 sljit_ins *buf_ptr;
214 sljit_ins *buf_end;
215 sljit_uw word_count;
216 sljit_uw addr;
217
218 struct sljit_label *label;
219 struct sljit_jump *jump;
220 struct sljit_const *const_;
221
222 CHECK_ERROR_PTR();
223 check_sljit_generate_code(compiler);
224 reverse_buf(compiler);
225
226 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
227 compiler->size += (compiler->size & 0x1) + (sizeof(struct sljit_function_context) / sizeof(sljit_ins));
228 #endif
229 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
230 PTR_FAIL_WITH_EXEC_IF(code);
231 buf = compiler->buf;
232
233 code_ptr = code;
234 word_count = 0;
235 label = compiler->labels;
236 jump = compiler->jumps;
237 const_ = compiler->consts;
238 do {
239 buf_ptr = (sljit_ins*)buf->memory;
240 buf_end = buf_ptr + (buf->used_size >> 2);
241 do {
242 *code_ptr = *buf_ptr++;
243 SLJIT_ASSERT(!label || label->size >= word_count);
244 SLJIT_ASSERT(!jump || jump->addr >= word_count);
245 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
246 /* These structures are ordered by their address. */
247 if (label && label->size == word_count) {
248 /* Just recording the address. */
249 label->addr = (sljit_uw)code_ptr;
250 label->size = code_ptr - code;
251 label = label->next;
252 }
253 if (jump && jump->addr == word_count) {
254 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
255 jump->addr = (sljit_uw)(code_ptr - 3);
256 #else
257 jump->addr = (sljit_uw)(code_ptr - 6);
258 #endif
259 if (optimize_jump(jump, code_ptr, code)) {
260 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
261 code_ptr[-3] = code_ptr[0];
262 code_ptr -= 3;
263 #else
264 code_ptr[-6] = code_ptr[0];
265 code_ptr -= 6;
266 #endif
267 }
268 jump = jump->next;
269 }
270 if (const_ && const_->addr == word_count) {
271 /* Just recording the address. */
272 const_->addr = (sljit_uw)code_ptr;
273 const_ = const_->next;
274 }
275 code_ptr ++;
276 word_count ++;
277 } while (buf_ptr < buf_end);
278
279 buf = buf->next;
280 } while (buf);
281
282 if (label && label->size == word_count) {
283 label->addr = (sljit_uw)code_ptr;
284 label->size = code_ptr - code;
285 label = label->next;
286 }
287
288 SLJIT_ASSERT(!label);
289 SLJIT_ASSERT(!jump);
290 SLJIT_ASSERT(!const_);
291 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
292 SLJIT_ASSERT(code_ptr - code <= (int)compiler->size - ((compiler->size & 0x1) ? 3 : 2));
293 #else
294 SLJIT_ASSERT(code_ptr - code <= (int)compiler->size);
295 #endif
296
297 jump = compiler->jumps;
298 while (jump) {
299 do {
300 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
301 buf_ptr = (sljit_ins*)jump->addr;
302 if (jump->flags & PATCH_B) {
303 if (jump->flags & UNCOND_B) {
304 if (!(jump->flags & ABSOLUTE_B)) {
305 addr = addr - jump->addr;
306 SLJIT_ASSERT((sljit_w)addr <= 0x01ffffff && (sljit_w)addr >= -0x02000000);
307 *buf_ptr = Bx | (addr & 0x03fffffc) | ((*buf_ptr) & 0x1);
308 }
309 else {
310 SLJIT_ASSERT(addr <= 0x03ffffff);
311 *buf_ptr = Bx | (addr & 0x03fffffc) | 0x2 | ((*buf_ptr) & 0x1);
312 }
313 }
314 else {
315 if (!(jump->flags & ABSOLUTE_B)) {
316 addr = addr - jump->addr;
317 SLJIT_ASSERT((sljit_w)addr <= 0x7fff && (sljit_w)addr >= -0x8000);
318 *buf_ptr = BCx | (addr & 0xfffc) | ((*buf_ptr) & 0x03ff0001);
319 }
320 else {
321 addr = addr & ~0x3l;
322 SLJIT_ASSERT(addr <= 0xffff);
323 *buf_ptr = BCx | (addr & 0xfffc) | 0x2 | ((*buf_ptr) & 0x03ff0001);
324 }
325
326 }
327 break;
328 }
329 /* Set the fields of immediate loads. */
330 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
331 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
332 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
333 #else
334 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
335 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
336 buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
337 buf_ptr[4] = (buf_ptr[4] & 0xffff0000) | (addr & 0xffff);
338 #endif
339 } while (0);
340 jump = jump->next;
341 }
342
343 SLJIT_CACHE_FLUSH(code, code_ptr);
344 compiler->error = SLJIT_ERR_COMPILED;
345
346 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
347 if (((sljit_w)code_ptr) & 0x4)
348 code_ptr++;
349 sljit_set_function_context(NULL, (struct sljit_function_context*)code_ptr, (sljit_w)code, sljit_generate_code);
350 return code_ptr;
351 #else
352 return code;
353 #endif
354 }
355
356 /* inp_flags: */
357
358 /* Creates an index in data_transfer_insts array. */
359 #define WORD_DATA 0x00
360 #define BYTE_DATA 0x01
361 #define HALF_DATA 0x02
362 #define INT_DATA 0x03
363 #define SIGNED_DATA 0x04
364 #define LOAD_DATA 0x08
365 #define WRITE_BACK 0x10
366 #define INDEXED 0x20
367
368 #define MEM_MASK 0x3f
369
370 /* Other inp_flags. */
371
372 #define ARG_TEST 0x0100
373 #define ALT_FORM1 0x0200
374 #define ALT_FORM2 0x0400
375 #define ALT_FORM3 0x0800
376 #define ALT_FORM4 0x1000
377 #define ALT_FORM5 0x2000
378 /* Integer opertion and set flags -> requires exts on 64 bit systems. */
379 #define ALT_SIGN_EXT 0x4000
380 /* This flag affects the RC() and OERC() macros. */
381 #define ALT_SET_FLAGS 0x8000
382
383 /* Source and destination is register. */
384 #define REG_DEST 0x0001
385 #define REG1_SOURCE 0x0002
386 #define REG2_SOURCE 0x0004
387 /* getput_arg_fast returned true. */
388 #define FAST_DEST 0x0008
389 /* Multiple instructions are required. */
390 #define SLOW_DEST 0x0010
391 /* ALT_FORM1 0x0200
392 ALT_FORM2 0x0400
393 ALT_FORM3 0x0800
394 ALT_FORM4 0x1000
395 ALT_FORM5 0x2000
396 ALT_SIGN_EXT 0x4000
397 ALT_SET_FLAGS 0x8000 */
398
399 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
400 #include "sljitNativePPC_32.c"
401 #else
402 #include "sljitNativePPC_64.c"
403 #endif
404
405 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
406 #define STACK_STORE STW
407 #define STACK_LOAD LWZ
408 #else
409 #define STACK_STORE STD
410 #define STACK_LOAD LD
411 #endif
412
413 static int emit_op(struct sljit_compiler *compiler, int op, int inp_flags,
414 int dst, sljit_w dstw,
415 int src1, sljit_w src1w,
416 int src2, sljit_w src2w);
417
418 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
419 {
420 CHECK_ERROR();
421 check_sljit_emit_enter(compiler, args, temporaries, generals, local_size);
422
423 compiler->temporaries = temporaries;
424 compiler->generals = generals;
425 compiler->has_locals = local_size > 0;
426
427 FAIL_IF(push_inst(compiler, MFLR | D(0)));
428 if (compiler->has_locals)
429 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_LOCALS_REG) | A(REAL_STACK_PTR) | IMM(-(int)(sizeof(sljit_w))) ));
430 FAIL_IF(push_inst(compiler, STACK_STORE | S(ZERO_REG) | A(REAL_STACK_PTR) | IMM(-2 * (int)(sizeof(sljit_w))) ));
431 if (generals >= 1)
432 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_GENERAL_REG1) | A(REAL_STACK_PTR) | IMM(-3 * (int)(sizeof(sljit_w))) ));
433 if (generals >= 2)
434 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_GENERAL_REG2) | A(REAL_STACK_PTR) | IMM(-4 * (int)(sizeof(sljit_w))) ));
435 if (generals >= 3)
436 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_GENERAL_REG3) | A(REAL_STACK_PTR) | IMM(-5 * (int)(sizeof(sljit_w))) ));
437 if (generals >= 4)
438 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_GENERAL_EREG1) | A(REAL_STACK_PTR) | IMM(-6 * (int)(sizeof(sljit_w))) ));
439 if (generals >= 5)
440 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_GENERAL_EREG2) | A(REAL_STACK_PTR) | IMM(-7 * (int)(sizeof(sljit_w))) ));
441 FAIL_IF(push_inst(compiler, STACK_STORE | S(0) | A(REAL_STACK_PTR) | IMM(sizeof(sljit_w)) ));
442
443 FAIL_IF(push_inst(compiler, ADDI | D(ZERO_REG) | A(0) | 0));
444 if (args >= 1)
445 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG1) | A(SLJIT_GENERAL_REG1) | B(SLJIT_TEMPORARY_REG1)));
446 if (args >= 2)
447 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG2) | A(SLJIT_GENERAL_REG2) | B(SLJIT_TEMPORARY_REG2)));
448 if (args >= 3)
449 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG3) | A(SLJIT_GENERAL_REG3) | B(SLJIT_TEMPORARY_REG3)));
450
451 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
452 compiler->local_size = (2 + generals + 2) * sizeof(sljit_w) + local_size;
453 #else
454 compiler->local_size = (2 + generals + 7 + 8) * sizeof(sljit_w) + local_size;
455 #endif
456 compiler->local_size = (compiler->local_size + 15) & ~0xf;
457
458 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
459 if (compiler->local_size <= SIMM_MAX)
460 FAIL_IF(push_inst(compiler, STWU | S(REAL_STACK_PTR) | A(REAL_STACK_PTR) | IMM(-compiler->local_size)));
461 else {
462 FAIL_IF(load_immediate(compiler, 0, -compiler->local_size));
463 FAIL_IF(push_inst(compiler, STWUX | S(REAL_STACK_PTR) | A(REAL_STACK_PTR) | B(0)));
464 }
465 if (compiler->has_locals)
466 FAIL_IF(push_inst(compiler, ADDI | D(SLJIT_LOCALS_REG) | A(REAL_STACK_PTR) | IMM(2 * sizeof(sljit_w))));
467 #else
468 if (compiler->local_size <= SIMM_MAX)
469 FAIL_IF(push_inst(compiler, STDU | S(REAL_STACK_PTR) | A(REAL_STACK_PTR) | IMM(-compiler->local_size)));
470 else {
471 FAIL_IF(load_immediate(compiler, 0, -compiler->local_size));
472 FAIL_IF(push_inst(compiler, STDUX | S(REAL_STACK_PTR) | A(REAL_STACK_PTR) | B(0)));
473 }
474 if (compiler->has_locals)
475 FAIL_IF(push_inst(compiler, ADDI | D(SLJIT_LOCALS_REG) | A(REAL_STACK_PTR) | IMM((7 + 8) * sizeof(sljit_w))));
476 #endif
477
478 return SLJIT_SUCCESS;
479 }
480
481 SLJIT_API_FUNC_ATTRIBUTE void sljit_fake_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
482 {
483 CHECK_ERROR_VOID();
484 check_sljit_fake_enter(compiler, args, temporaries, generals, local_size);
485
486 compiler->temporaries = temporaries;
487 compiler->generals = generals;
488
489 compiler->has_locals = local_size > 0;
490 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
491 compiler->local_size = (2 + generals + 2) * sizeof(sljit_w) + local_size;
492 #else
493 compiler->local_size = (2 + generals + 7 + 8) * sizeof(sljit_w) + local_size;
494 #endif
495 compiler->local_size = (compiler->local_size + 15) & ~0xf;
496 }
497
498 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
499 {
500 CHECK_ERROR();
501 check_sljit_emit_return(compiler, src, srcw);
502
503 if (src != SLJIT_UNUSED && src != SLJIT_RETURN_REG)
504 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, SLJIT_RETURN_REG, 0, TMP_REG1, 0, src, srcw));
505
506 if (compiler->local_size <= SIMM_MAX)
507 FAIL_IF(push_inst(compiler, ADDI | D(REAL_STACK_PTR) | A(REAL_STACK_PTR) | IMM(compiler->local_size)));
508 else {
509 FAIL_IF(load_immediate(compiler, 0, compiler->local_size));
510 FAIL_IF(push_inst(compiler, ADD | D(REAL_STACK_PTR) | A(REAL_STACK_PTR) | B(0)));
511 }
512
513 FAIL_IF(push_inst(compiler, STACK_LOAD | D(0) | A(REAL_STACK_PTR) | IMM(sizeof(sljit_w))));
514 if (compiler->generals >= 5)
515 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_GENERAL_EREG2) | A(REAL_STACK_PTR) | IMM(-7 * (int)(sizeof(sljit_w))) ));
516 if (compiler->generals >= 4)
517 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_GENERAL_EREG1) | A(REAL_STACK_PTR) | IMM(-6 * (int)(sizeof(sljit_w))) ));
518 if (compiler->generals >= 3)
519 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_GENERAL_REG3) | A(REAL_STACK_PTR) | IMM(-5 * (int)(sizeof(sljit_w))) ));
520 if (compiler->generals >= 2)
521 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_GENERAL_REG2) | A(REAL_STACK_PTR) | IMM(-4 * (int)(sizeof(sljit_w))) ));
522 if (compiler->generals >= 1)
523 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_GENERAL_REG1) | A(REAL_STACK_PTR) | IMM(-3 * (int)(sizeof(sljit_w))) ));
524 FAIL_IF(push_inst(compiler, STACK_LOAD | D(ZERO_REG) | A(REAL_STACK_PTR) | IMM(-2 * (int)(sizeof(sljit_w))) ));
525 if (compiler->has_locals)
526 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_LOCALS_REG) | A(REAL_STACK_PTR) | IMM(-(int)(sizeof(sljit_w))) ));
527
528 FAIL_IF(push_inst(compiler, MTLR | S(0)));
529 FAIL_IF(push_inst(compiler, BLR));
530
531 return SLJIT_SUCCESS;
532 }
533
534 #undef STACK_STORE
535 #undef STACK_LOAD
536
537 /* --------------------------------------------------------------------- */
538 /* Operators */
539 /* --------------------------------------------------------------------- */
540
541 /* i/x - immediate/indexed form
542 n/w - no write-back / write-back (1 bit)
543 s/l - store/load (1 bit)
544 u/s - signed/unsigned (1 bit)
545 w/b/h/i - word/byte/half/int allowed (2 bit)
546 It contans 32 items, but not all are different. */
547
548 /* 64 bit only: [reg+imm] must be aligned to 4 bytes. */
549 #define ADDR_MODE2 0x10000
550 /* 64-bit only: there is no lwau instruction. */
551 #define UPDATE_REQ 0x20000
552
553 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
554 #define ARCH_DEPEND(a, b) a
555 #define GET_INST_CODE(inst) (inst)
556 #else
557 #define ARCH_DEPEND(a, b) b
558 #define GET_INST_CODE(index) ((inst) & ~(ADDR_MODE2 | UPDATE_REQ))
559 #endif
560
561 static SLJIT_CONST sljit_ins data_transfer_insts[64] = {
562
563 /* No write-back. */
564
565 /* i n s u w */ ARCH_DEPEND(HI(36) /* stw */, HI(62) | ADDR_MODE2 | 0x0 /* std */),
566 /* i n s u b */ HI(38) /* stb */,
567 /* i n s u h */ HI(44) /* sth*/,
568 /* i n s u i */ HI(36) /* stw */,
569
570 /* i n s s w */ ARCH_DEPEND(HI(36) /* stw */, HI(62) | ADDR_MODE2 | 0x0 /* std */),
571 /* i n s s b */ HI(38) /* stb */,
572 /* i n s s h */ HI(44) /* sth*/,
573 /* i n s s i */ HI(36) /* stw */,
574
575 /* i n l u w */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x0 /* ld */),
576 /* i n l u b */ HI(34) /* lbz */,
577 /* i n l u h */ HI(40) /* lhz */,
578 /* i n l u i */ HI(32) /* lwz */,
579
580 /* i n l s w */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x0 /* ld */),
581 /* i n l s b */ HI(34) /* lbz */ /* EXTS_REQ */,
582 /* i n l s h */ HI(42) /* lha */,
583 /* i n l s i */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x2 /* lwa */),
584
585 /* Write-back. */
586
587 /* i w s u w */ ARCH_DEPEND(HI(37) /* stwu */, HI(62) | ADDR_MODE2 | 0x1 /* stdu */),
588 /* i w s u b */ HI(39) /* stbu */,
589 /* i w s u h */ HI(45) /* sthu */,
590 /* i w s u i */ HI(37) /* stwu */,
591
592 /* i w s s w */ ARCH_DEPEND(HI(37) /* stwu */, HI(62) | ADDR_MODE2 | 0x1 /* stdu */),
593 /* i w s s b */ HI(39) /* stbu */,
594 /* i w s s h */ HI(45) /* sthu */,
595 /* i w s s i */ HI(37) /* stwu */,
596
597 /* i w l u w */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | 0x1 /* ldu */),
598 /* i w l u b */ HI(35) /* lbzu */,
599 /* i w l u h */ HI(41) /* lhzu */,
600 /* i w l u i */ HI(33) /* lwzu */,
601
602 /* i w l s w */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | 0x1 /* ldu */),
603 /* i w l s b */ HI(35) /* lbzu */ /* EXTS_REQ */,
604 /* i w l s h */ HI(43) /* lhau */,
605 /* i w l s i */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | UPDATE_REQ | 0x2 /* lwa */),
606
607 /* ---------- */
608 /* Indexed */
609 /* ---------- */
610
611 /* No write-back. */
612
613 /* x n s u w */ ARCH_DEPEND(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
614 /* x n s u b */ HI(31) | LO(215) /* stbx */,
615 /* x n s u h */ HI(31) | LO(407) /* sthx */,
616 /* x n s u i */ HI(31) | LO(151) /* stwx */,
617
618 /* x n s s w */ ARCH_DEPEND(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
619 /* x n s s b */ HI(31) | LO(215) /* stbx */,
620 /* x n s s h */ HI(31) | LO(407) /* sthx */,
621 /* x n s s i */ HI(31) | LO(151) /* stwx */,
622
623 /* x n l u w */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
624 /* x n l u b */ HI(31) | LO(87) /* lbzx */,
625 /* x n l u h */ HI(31) | LO(279) /* lhzx */,
626 /* x n l u i */ HI(31) | LO(23) /* lwzx */,
627
628 /* x n l s w */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
629 /* x n l s b */ HI(31) | LO(87) /* lbzx */ /* EXTS_REQ */,
630 /* x n l s h */ HI(31) | LO(343) /* lhax */,
631 /* x n l s i */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(341) /* lwax */),
632
633 /* Write-back. */
634
635 /* x w s u w */ ARCH_DEPEND(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
636 /* x w s u b */ HI(31) | LO(247) /* stbux */,
637 /* x w s u h */ HI(31) | LO(439) /* sthux */,
638 /* x w s u i */ HI(31) | LO(183) /* stwux */,
639
640 /* x w s s w */ ARCH_DEPEND(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
641 /* x w s s b */ HI(31) | LO(247) /* stbux */,
642 /* x w s s h */ HI(31) | LO(439) /* sthux */,
643 /* x w s s i */ HI(31) | LO(183) /* stwux */,
644
645 /* x w l u w */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
646 /* x w l u b */ HI(31) | LO(119) /* lbzux */,
647 /* x w l u h */ HI(31) | LO(311) /* lhzux */,
648 /* x w l u i */ HI(31) | LO(55) /* lwzux */,
649
650 /* x w l s w */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
651 /* x w l s b */ HI(31) | LO(119) /* lbzux */ /* EXTS_REQ */,
652 /* x w l s h */ HI(31) | LO(375) /* lhaux */,
653 /* x w l s i */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(373) /* lwaux */)
654
655 };
656
657 #undef ARCH_DEPEND
658
659 /* Simple cases, (no caching is required). */
660 static int getput_arg_fast(struct sljit_compiler *compiler, int inp_flags, int reg, int arg, sljit_w argw)
661 {
662 sljit_ins inst;
663 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
664 int tmp_reg;
665 #endif
666
667 SLJIT_ASSERT(arg & SLJIT_MEM);
668 if (!(arg & 0xf)) {
669 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
670 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
671 if (inp_flags & ARG_TEST)
672 return 1;
673
674 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
675 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
676 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | IMM(argw));
677 return -1;
678 }
679 #else
680 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
681 if (argw <= SIMM_MAX && argw >= SIMM_MIN &&
682 (!(inst & ADDR_MODE2) || (argw & 0x3) == 0)) {
683 if (inp_flags & ARG_TEST)
684 return 1;
685
686 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | IMM(argw));
687 return -1;
688 }
689 #endif
690 return (inp_flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
691 }
692
693 if (!(arg & 0xf0)) {
694 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
695 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
696 if (inp_flags & ARG_TEST)
697 return 1;
698
699 inst = data_transfer_insts[inp_flags & MEM_MASK];
700 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
701 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | IMM(argw));
702 return -1;
703 }
704 #else
705 inst = data_transfer_insts[inp_flags & MEM_MASK];
706 if (argw <= SIMM_MAX && argw >= SIMM_MIN && (!(inst & ADDR_MODE2) || (argw & 0x3) == 0)) {
707 if (inp_flags & ARG_TEST)
708 return 1;
709
710 if ((inp_flags & WRITE_BACK) && (inst & UPDATE_REQ)) {
711 tmp_reg = (inp_flags & LOAD_DATA) ? (arg & 0xf) : TMP_REG3;
712 if (push_inst(compiler, ADDI | D(tmp_reg) | A(arg & 0xf) | IMM(argw)))
713 return -1;
714 arg = tmp_reg | SLJIT_MEM;
715 argw = 0;
716 }
717 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | IMM(argw));
718 return -1;
719 }
720 #endif
721 }
722 else if (!(argw & 0x3)) {
723 if (inp_flags & ARG_TEST)
724 return 1;
725 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
726 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
727 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B((arg >> 4) & 0xf));
728 return -1;
729 }
730 return (inp_flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
731 }
732
733 /* See getput_arg below.
734 Note: can_cache is called only for binary operators. Those operator always
735 uses word arguments without write back. */
736 static int can_cache(int arg, sljit_w argw, int next_arg, sljit_w next_argw)
737 {
738 SLJIT_ASSERT(arg & SLJIT_MEM);
739 SLJIT_ASSERT(next_arg & SLJIT_MEM);
740
741 if (!(arg & 0xf)) {
742 if ((next_arg & SLJIT_MEM) && ((sljit_uw)argw - (sljit_uw)next_argw <= SIMM_MAX || (sljit_uw)next_argw - (sljit_uw)argw <= SIMM_MAX))
743 return 1;
744 return 0;
745 }
746
747 if (arg & 0xf0)
748 return 0;
749
750 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
751 if (arg == next_arg && (next_argw >= SIMM_MAX && next_argw <= SIMM_MIN))
752 return 1;
753 }
754
755 if (arg == next_arg && ((sljit_uw)argw - (sljit_uw)next_argw <= SIMM_MAX || (sljit_uw)next_argw - (sljit_uw)argw <= SIMM_MAX))
756 return 1;
757
758 return 0;
759 }
760
761 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
762 #define ADJUST_CACHED_IMM(imm) \
763 if ((inst & ADDR_MODE2) && (imm & 0x3)) { \
764 /* Adjust cached value. Fortunately this is really a rare case */ \
765 compiler->cache_argw += imm & 0x3; \
766 FAIL_IF(push_inst(compiler, ADDI | D(TMP_REG3) | A(TMP_REG3) | (imm & 0x3))); \
767 imm &= ~0x3; \
768 }
769 #else
770 #define ADJUST_CACHED_IMM(imm)
771 #endif
772
773 /* Emit the necessary instructions. See can_cache above. */
774 static int getput_arg(struct sljit_compiler *compiler, int inp_flags, int reg, int arg, sljit_w argw, int next_arg, sljit_w next_argw)
775 {
776 int tmp_r;
777 sljit_ins inst;
778
779 SLJIT_ASSERT(arg & SLJIT_MEM);
780
781 tmp_r = (inp_flags & LOAD_DATA) ? reg : TMP_REG3;
782 if ((arg & 0xf) == tmp_r) {
783 /* Special case for "mov reg, [reg, ... ]".
784 Caching would not happen anyway. */
785 tmp_r = TMP_REG3;
786 compiler->cache_arg = 0;
787 compiler->cache_argw = 0;
788 }
789
790 if (!(arg & 0xf)) {
791 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
792 if ((compiler->cache_arg & SLJIT_IMM) && (((sljit_uw)argw - (sljit_uw)compiler->cache_argw) <= SIMM_MAX || ((sljit_uw)compiler->cache_argw - (sljit_uw)argw) <= SIMM_MAX)) {
793 argw = argw - compiler->cache_argw;
794 ADJUST_CACHED_IMM(argw);
795 SLJIT_ASSERT(!(inst & UPDATE_REQ));
796 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3) | IMM(argw));
797 }
798
799 if ((next_arg & SLJIT_MEM) && (argw - next_argw <= SIMM_MAX || next_argw - argw <= SIMM_MAX)) {
800 SLJIT_ASSERT(inp_flags & LOAD_DATA);
801
802 compiler->cache_arg = SLJIT_IMM;
803 compiler->cache_argw = argw;
804 tmp_r = TMP_REG3;
805 }
806
807 FAIL_IF(load_immediate(compiler, tmp_r, argw));
808 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(tmp_r));
809 }
810
811 if (SLJIT_UNLIKELY(arg & 0xf0)) {
812 argw &= 0x3;
813 /* Otherwise getput_arg_fast would capture it. */
814 SLJIT_ASSERT(argw);
815 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
816 FAIL_IF(push_inst(compiler, RLWINM | S((arg >> 4) & 0xf) | A(tmp_r) | (argw << 11) | ((31 - argw) << 1)));
817 #else
818 FAIL_IF(push_inst(compiler, RLDI(tmp_r, (arg >> 4) & 0xf, argw, 63 - argw, 1)));
819 #endif
820 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
821 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
822 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(tmp_r));
823 }
824
825 inst = data_transfer_insts[inp_flags & MEM_MASK];
826
827 if (compiler->cache_arg == arg && ((sljit_uw)argw - (sljit_uw)compiler->cache_argw <= SIMM_MAX || (sljit_uw)compiler->cache_argw - (sljit_uw)argw <= SIMM_MAX)) {
828 SLJIT_ASSERT(!(inp_flags & WRITE_BACK));
829 argw = argw - compiler->cache_argw;
830 ADJUST_CACHED_IMM(argw);
831 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3) | IMM(argw));
832 }
833
834 if ((compiler->cache_arg & SLJIT_IMM) && compiler->cache_argw == argw) {
835 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
836 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
837 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(TMP_REG3));
838 }
839
840 if (argw == next_argw && (next_arg & SLJIT_MEM)) {
841 SLJIT_ASSERT(inp_flags & LOAD_DATA);
842 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
843
844 compiler->cache_arg = SLJIT_IMM;
845 compiler->cache_argw = argw;
846
847 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
848 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
849 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(TMP_REG3));
850 }
851
852 if (arg == next_arg && !(inp_flags & WRITE_BACK) && ((sljit_uw)argw - (sljit_uw)next_argw <= SIMM_MAX || (sljit_uw)next_argw - (sljit_uw)argw <= SIMM_MAX)) {
853 SLJIT_ASSERT(inp_flags & LOAD_DATA);
854 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
855 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | A(TMP_REG3) | B(arg & 0xf)));
856
857 compiler->cache_arg = arg;
858 compiler->cache_argw = argw;
859
860 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3));
861 }
862
863 /* Get the indexed version instead of the normal one. */
864 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
865 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
866 FAIL_IF(load_immediate(compiler, tmp_r, argw));
867 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(tmp_r));
868 }
869
870 static int emit_op(struct sljit_compiler *compiler, int op, int inp_flags,
871 int dst, sljit_w dstw,
872 int src1, sljit_w src1w,
873 int src2, sljit_w src2w)
874 {
875 /* arg1 goes to TMP_REG1 or src reg
876 arg2 goes to TMP_REG2, imm or src reg
877 TMP_REG3 can be used for caching
878 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
879 int dst_r;
880 int src1_r;
881 int src2_r;
882 int sugg_src2_r = TMP_REG2;
883 int flags = inp_flags & (ALT_FORM1 | ALT_FORM2 | ALT_FORM3 | ALT_FORM4 | ALT_FORM5 | ALT_SIGN_EXT | ALT_SET_FLAGS);
884
885 compiler->cache_arg = 0;
886 compiler->cache_argw = 0;
887
888 /* Destination check. */
889 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= ZERO_REG) {
890 dst_r = dst;
891 flags |= REG_DEST;
892 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
893 sugg_src2_r = dst_r;
894 }
895 else if (dst == SLJIT_UNUSED) {
896 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
897 return SLJIT_SUCCESS;
898 dst_r = TMP_REG2;
899 }
900 else {
901 SLJIT_ASSERT(dst & SLJIT_MEM);
902 if (getput_arg_fast(compiler, inp_flags | ARG_TEST, TMP_REG2, dst, dstw)) {
903 flags |= FAST_DEST;
904 dst_r = TMP_REG2;
905 }
906 else {
907 flags |= SLOW_DEST;
908 dst_r = 0;
909 }
910 }
911
912 /* Source 1. */
913 if (src1 >= SLJIT_TEMPORARY_REG1 && src1 <= ZERO_REG) {
914 src1_r = src1;
915 flags |= REG1_SOURCE;
916 }
917 else if (src1 & SLJIT_IMM) {
918 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
919 if ((inp_flags & 0x3) == INT_DATA) {
920 if (inp_flags & SIGNED_DATA)
921 src1w = (signed int)src1w;
922 else
923 src1w = (unsigned int)src1w;
924 }
925 #endif
926 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
927 src1_r = TMP_REG1;
928 }
929 else if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w)) {
930 FAIL_IF(compiler->error);
931 src1_r = TMP_REG1;
932 }
933 else
934 src1_r = 0;
935
936 /* Source 2. */
937 if (src2 >= SLJIT_TEMPORARY_REG1 && src2 <= ZERO_REG) {
938 src2_r = src2;
939 flags |= REG2_SOURCE;
940 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
941 dst_r = src2_r;
942 }
943 else if (src2 & SLJIT_IMM) {
944 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
945 if ((inp_flags & 0x3) == INT_DATA) {
946 if (inp_flags & SIGNED_DATA)
947 src2w = (signed int)src2w;
948 else
949 src2w = (unsigned int)src2w;
950 }
951 #endif
952 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
953 src2_r = sugg_src2_r;
954 }
955 else if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w)) {
956 FAIL_IF(compiler->error);
957 src2_r = sugg_src2_r;
958 }
959 else
960 src2_r = 0;
961
962 /* src1_r, src2_r and dst_r can be zero (=unprocessed).
963 All arguments are complex addressing modes, and it is a binary operator. */
964 if (src1_r == 0 && src2_r == 0 && dst_r == 0) {
965 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
966 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
967 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
968 }
969 else {
970 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
971 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
972 }
973 src1_r = TMP_REG1;
974 src2_r = TMP_REG2;
975 }
976 else if (src1_r == 0 && src2_r == 0) {
977 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
978 src1_r = TMP_REG1;
979 }
980 else if (src1_r == 0 && dst_r == 0) {
981 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
982 src1_r = TMP_REG1;
983 }
984 else if (src2_r == 0 && dst_r == 0) {
985 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
986 src2_r = sugg_src2_r;
987 }
988
989 if (dst_r == 0)
990 dst_r = TMP_REG2;
991
992 if (src1_r == 0) {
993 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, 0, 0));
994 src1_r = TMP_REG1;
995 }
996
997 if (src2_r == 0) {
998 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, 0, 0));
999 src2_r = sugg_src2_r;
1000 }
1001
1002 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1003
1004 if (flags & (FAST_DEST | SLOW_DEST)) {
1005 if (flags & FAST_DEST)
1006 FAIL_IF(getput_arg_fast(compiler, inp_flags, dst_r, dst, dstw));
1007 else
1008 FAIL_IF(getput_arg(compiler, inp_flags, dst_r, dst, dstw, 0, 0));
1009 }
1010 return SLJIT_SUCCESS;
1011 }
1012
1013 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op)
1014 {
1015 CHECK_ERROR();
1016 check_sljit_emit_op0(compiler, op);
1017
1018 op = GET_OPCODE(op);
1019 switch (op) {
1020 case SLJIT_BREAKPOINT:
1021 case SLJIT_NOP:
1022 return push_inst(compiler, NOP);
1023 break;
1024 }
1025
1026 return SLJIT_SUCCESS;
1027 }
1028
1029 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
1030 int dst, sljit_w dstw,
1031 int src, sljit_w srcw)
1032 {
1033 int inp_flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
1034
1035 CHECK_ERROR();
1036 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1037
1038 if ((src & SLJIT_IMM) && srcw == 0)
1039 src = ZERO_REG;
1040
1041 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1042 if (op & SLJIT_INT_OP) {
1043 inp_flags |= INT_DATA | SIGNED_DATA;
1044 if (src & SLJIT_IMM)
1045 srcw = (int)srcw;
1046 }
1047 #endif
1048 if (op & SLJIT_SET_O)
1049 FAIL_IF(push_inst(compiler, MTXER | S(ZERO_REG)));
1050
1051 switch (GET_OPCODE(op)) {
1052 case SLJIT_MOV:
1053 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1054
1055 case SLJIT_MOV_UI:
1056 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1057
1058 case SLJIT_MOV_SI:
1059 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1060
1061 case SLJIT_MOV_UB:
1062 return emit_op(compiler, SLJIT_MOV_UB, inp_flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned char)srcw : srcw);
1063
1064 case SLJIT_MOV_SB:
1065 return emit_op(compiler, SLJIT_MOV_SB, inp_flags | BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed char)srcw : srcw);
1066
1067 case SLJIT_MOV_UH:
1068 return emit_op(compiler, SLJIT_MOV_UH, inp_flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned short)srcw : srcw);
1069
1070 case SLJIT_MOV_SH:
1071 return emit_op(compiler, SLJIT_MOV_SH, inp_flags | HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed short)srcw : srcw);
1072
1073 case SLJIT_MOVU:
1074 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1075
1076 case SLJIT_MOVU_UI:
1077 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1078
1079 case SLJIT_MOVU_SI:
1080 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1081
1082 case SLJIT_MOVU_UB:
1083 return emit_op(compiler, SLJIT_MOV_UB, inp_flags | BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned char)srcw : srcw);
1084
1085 case SLJIT_MOVU_SB:
1086 return emit_op(compiler, SLJIT_MOV_SB, inp_flags | BYTE_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed char)srcw : srcw);
1087
1088 case SLJIT_MOVU_UH:
1089 return emit_op(compiler, SLJIT_MOV_UH, inp_flags | HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned short)srcw : srcw);
1090
1091 case SLJIT_MOVU_SH:
1092 return emit_op(compiler, SLJIT_MOV_SH, inp_flags | HALF_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed short)srcw : srcw);
1093
1094 case SLJIT_NOT:
1095 return emit_op(compiler, SLJIT_NOT, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1096
1097 case SLJIT_NEG:
1098 return emit_op(compiler, SLJIT_NEG, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1099
1100 case SLJIT_CLZ:
1101 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1102 return emit_op(compiler, SLJIT_CLZ, inp_flags | (!(op & SLJIT_INT_OP) ? 0 : ALT_FORM1), dst, dstw, TMP_REG1, 0, src, srcw);
1103 #else
1104 return emit_op(compiler, SLJIT_CLZ, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1105 #endif
1106 }
1107
1108 return SLJIT_SUCCESS;
1109 }
1110
1111 #define TEST_SL_IMM(src, srcw) \
1112 (((src) & SLJIT_IMM) && (srcw) <= SIMM_MAX && (srcw) >= SIMM_MIN)
1113
1114 #define TEST_UL_IMM(src, srcw) \
1115 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff))
1116
1117 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1118 #define TEST_SH_IMM(src, srcw) \
1119 (((src) & SLJIT_IMM) && !((srcw) & 0xffff) && (srcw) <= SLJIT_W(0x7fffffff) && (srcw) >= SLJIT_W(-0x80000000))
1120 #else
1121 #define TEST_SH_IMM(src, srcw) \
1122 (((src) & SLJIT_IMM) && !((srcw) & 0xffff))
1123 #endif
1124
1125 #define TEST_UH_IMM(src, srcw) \
1126 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff0000))
1127
1128 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1129 #define TEST_UI_IMM(src, srcw) \
1130 (((src) & SLJIT_IMM) && !((srcw) & ~0xffffffff))
1131 #else
1132 #define TEST_UI_IMM(src, srcw) \
1133 ((src) & SLJIT_IMM)
1134 #endif
1135
1136 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
1137 int dst, sljit_w dstw,
1138 int src1, sljit_w src1w,
1139 int src2, sljit_w src2w)
1140 {
1141 int inp_flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
1142
1143 CHECK_ERROR();
1144 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1145
1146 if ((src1 & SLJIT_IMM) && src1w == 0)
1147 src1 = ZERO_REG;
1148 if ((src2 & SLJIT_IMM) && src2w == 0)
1149 src2 = ZERO_REG;
1150
1151 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1152 if (op & SLJIT_INT_OP) {
1153 inp_flags |= INT_DATA | SIGNED_DATA;
1154 if (src1 & SLJIT_IMM)
1155 src1w = (src1w << 32) >> 32;
1156 if (src2 & SLJIT_IMM)
1157 src2w = (src2w << 32) >> 32;
1158 if (GET_FLAGS(op))
1159 inp_flags |= ALT_SIGN_EXT;
1160 }
1161 #endif
1162 if (op & SLJIT_SET_O)
1163 FAIL_IF(push_inst(compiler, MTXER | S(ZERO_REG)));
1164
1165 switch (GET_OPCODE(op)) {
1166 case SLJIT_ADD:
1167 if (!GET_FLAGS(op)) {
1168 if (TEST_SL_IMM(src2, src2w)) {
1169 compiler->imm = src2w & 0xffff;
1170 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1171 }
1172 if (TEST_SL_IMM(src1, src1w)) {
1173 compiler->imm = src1w & 0xffff;
1174 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1175 }
1176 if (TEST_SH_IMM(src2, src2w)) {
1177 compiler->imm = (src2w >> 16) & 0xffff;
1178 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1179 }
1180 if (TEST_SH_IMM(src1, src1w)) {
1181 compiler->imm = (src1w >> 16) & 0xffff;
1182 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1183 }
1184 }
1185 if (!(GET_FLAGS(op) & (SLJIT_SET_E | SLJIT_SET_O))) {
1186 if (TEST_SL_IMM(src2, src2w)) {
1187 compiler->imm = src2w & 0xffff;
1188 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1189 }
1190 if (TEST_SL_IMM(src1, src1w)) {
1191 compiler->imm = src1w & 0xffff;
1192 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
1193 }
1194 }
1195 return emit_op(compiler, SLJIT_ADD, inp_flags, dst, dstw, src1, src1w, src2, src2w);
1196
1197 case SLJIT_ADDC:
1198 return emit_op(compiler, SLJIT_ADDC, inp_flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
1199
1200 case SLJIT_SUB:
1201 if (!GET_FLAGS(op)) {
1202 if (TEST_SL_IMM(src2, -src2w)) {
1203 compiler->imm = (-src2w) & 0xffff;
1204 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1205 }
1206 if (TEST_SL_IMM(src1, src1w)) {
1207 compiler->imm = src1w & 0xffff;
1208 return emit_op(compiler, SLJIT_SUB, inp_flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1209 }
1210 if (TEST_SH_IMM(src2, -src2w)) {
1211 compiler->imm = ((-src2w) >> 16) & 0xffff;
1212 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1213 }
1214 }
1215 if (dst == SLJIT_UNUSED && !(GET_FLAGS(op) & ~(SLJIT_SET_E | SLJIT_SET_S))) {
1216 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1217 if (TEST_SL_IMM(src2, src2w)) {
1218 compiler->imm = src2w & 0xffff;
1219 return emit_op(compiler, SLJIT_SUB, inp_flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1220 }
1221 if (GET_FLAGS(op) == SLJIT_SET_E && TEST_SL_IMM(src1, src1w)) {
1222 compiler->imm = src1w & 0xffff;
1223 return emit_op(compiler, SLJIT_SUB, inp_flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1224 }
1225 }
1226 if (dst == SLJIT_UNUSED && GET_FLAGS(op) == SLJIT_SET_U) {
1227 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1228 if (TEST_UL_IMM(src2, src2w)) {
1229 compiler->imm = src2w & 0xffff;
1230 return emit_op(compiler, SLJIT_SUB, inp_flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1231 }
1232 return emit_op(compiler, SLJIT_SUB, inp_flags | ALT_FORM4, dst, dstw, src1, src1w, src2, src2w);
1233 }
1234 if (!(op & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O))) {
1235 if (TEST_SL_IMM(src2, -src2w)) {
1236 compiler->imm = (-src2w) & 0xffff;
1237 return emit_op(compiler, SLJIT_ADD, inp_flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1238 }
1239 }
1240 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1241 return emit_op(compiler, SLJIT_SUB, inp_flags | (!(op & SLJIT_SET_U) ? 0 : ALT_FORM5), dst, dstw, src1, src1w, src2, src2w);
1242
1243 case SLJIT_SUBC:
1244 return emit_op(compiler, SLJIT_SUBC, inp_flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
1245
1246 case SLJIT_MUL:
1247 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1248 if (op & SLJIT_INT_OP)
1249 inp_flags |= ALT_FORM2;
1250 #endif
1251 if (!GET_FLAGS(op)) {
1252 if (TEST_SL_IMM(src2, src2w)) {
1253 compiler->imm = src2w & 0xffff;
1254 return emit_op(compiler, SLJIT_MUL, inp_flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1255 }
1256 if (TEST_SL_IMM(src1, src1w)) {
1257 compiler->imm = src1w & 0xffff;
1258 return emit_op(compiler, SLJIT_MUL, inp_flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1259 }
1260 }
1261 return emit_op(compiler, SLJIT_MUL, inp_flags, dst, dstw, src1, src1w, src2, src2w);
1262
1263 case SLJIT_AND:
1264 case SLJIT_OR:
1265 case SLJIT_XOR:
1266 /* Commutative unsigned operations. */
1267 if (!GET_FLAGS(op) || GET_OPCODE(op) == SLJIT_AND) {
1268 if (TEST_UL_IMM(src2, src2w)) {
1269 compiler->imm = src2w;
1270 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1271 }
1272 if (TEST_UL_IMM(src1, src1w)) {
1273 compiler->imm = src1w;
1274 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1275 }
1276 if (TEST_UH_IMM(src2, src2w)) {
1277 compiler->imm = (src2w >> 16) & 0xffff;
1278 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1279 }
1280 if (TEST_UH_IMM(src1, src1w)) {
1281 compiler->imm = (src1w >> 16) & 0xffff;
1282 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1283 }
1284 }
1285 if (!GET_FLAGS(op) && GET_OPCODE(op) != SLJIT_AND) {
1286 if (TEST_UI_IMM(src2, src2w)) {
1287 compiler->imm = src2w;
1288 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1289 }
1290 if (TEST_UI_IMM(src1, src1w)) {
1291 compiler->imm = src1w;
1292 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
1293 }
1294 }
1295 return emit_op(compiler, GET_OPCODE(op), inp_flags, dst, dstw, src1, src1w, src2, src2w);
1296
1297 case SLJIT_SHL:
1298 case SLJIT_LSHR:
1299 case SLJIT_ASHR:
1300 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1301 if (op & SLJIT_INT_OP)
1302 inp_flags |= ALT_FORM2;
1303 #endif
1304 if (src2 & SLJIT_IMM) {
1305 compiler->imm = src2w;
1306 return emit_op(compiler, GET_OPCODE(op), inp_flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1307 }
1308 return emit_op(compiler, GET_OPCODE(op), inp_flags, dst, dstw, src1, src1w, src2, src2w);
1309 }
1310
1311 return SLJIT_SUCCESS;
1312 }
1313
1314 /* --------------------------------------------------------------------- */
1315 /* Floating point operators */
1316 /* --------------------------------------------------------------------- */
1317
1318 SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void)
1319 {
1320 /* Always available. */
1321 return 1;
1322 }
1323
1324 static int emit_fpu_data_transfer(struct sljit_compiler *compiler, int fpu_reg, int load, int arg, sljit_w argw)
1325 {
1326 SLJIT_ASSERT(arg & SLJIT_MEM);
1327
1328 /* Fast loads and stores. */
1329 if (!(arg & 0xf0)) {
1330 /* Both for (arg & 0xf) == SLJIT_UNUSED and (arg & 0xf) != SLJIT_UNUSED. */
1331 if (argw <= SIMM_MAX && argw >= SIMM_MIN)
1332 return push_inst(compiler, (load ? LFD : STFD) | FD(fpu_reg) | A(arg & 0xf) | IMM(argw));
1333 }
1334
1335 if (arg & 0xf0) {
1336 argw &= 0x3;
1337 if (argw) {
1338 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
1339 FAIL_IF(push_inst(compiler, RLWINM | S((arg >> 4) & 0xf) | A(TMP_REG2) | (argw << 11) | ((31 - argw) << 1)));
1340 #else
1341 FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, (arg >> 4) & 0xf, argw, 63 - argw, 1)));
1342 #endif
1343 return push_inst(compiler, (load ? LFDX : STFDX) | FD(fpu_reg) | A(arg & 0xf) | B(TMP_REG2));
1344 }
1345 return push_inst(compiler, (load ? LFDX : STFDX) | FD(fpu_reg) | A(arg & 0xf) | B((arg >> 4) & 0xf));
1346 }
1347
1348 /* Use cache. */
1349 if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN)
1350 return push_inst(compiler, (load ? LFD : STFD) | FD(fpu_reg) | A(TMP_REG3) | IMM(argw - compiler->cache_argw));
1351
1352 /* Put value to cache. */
1353 compiler->cache_arg = arg;
1354 compiler->cache_argw = argw;
1355
1356 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1357 if (!(arg & 0xf))
1358 return push_inst(compiler, (load ? LFDX : STFDX) | FD(fpu_reg) | A(0) | B(TMP_REG3));
1359 return push_inst(compiler, (load ? LFDUX : STFDUX) | FD(fpu_reg) | A(TMP_REG3) | B(arg & 0xf));
1360 }
1361
1362 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
1363 int dst, sljit_w dstw,
1364 int src, sljit_w srcw)
1365 {
1366 int dst_fr;
1367
1368 CHECK_ERROR();
1369 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1370
1371 compiler->cache_arg = 0;
1372 compiler->cache_argw = 0;
1373
1374 if (GET_OPCODE(op) == SLJIT_FCMP) {
1375 if (dst > SLJIT_FLOAT_REG4) {
1376 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 1, dst, dstw));
1377 dst = TMP_FREG1;
1378 }
1379 if (src > SLJIT_FLOAT_REG4) {
1380 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG2, 1, src, srcw));
1381 src = TMP_FREG2;
1382 }
1383 return push_inst(compiler, FCMPU | CRD(4) | FA(dst) | FB(src));
1384 }
1385
1386 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG1 : dst;
1387
1388 if (src > SLJIT_FLOAT_REG4) {
1389 FAIL_IF(emit_fpu_data_transfer(compiler, dst_fr, 1, src, srcw));
1390 src = dst_fr;
1391 }
1392
1393 switch (op) {
1394 case SLJIT_FMOV:
1395 if (src != dst_fr && dst_fr != TMP_FREG1)
1396 FAIL_IF(push_inst(compiler, FMR | FD(dst_fr) | FB(src)));
1397 break;
1398 case SLJIT_FNEG:
1399 FAIL_IF(push_inst(compiler, FNEG | FD(dst_fr) | FB(src)));
1400 break;
1401 case SLJIT_FABS:
1402 FAIL_IF(push_inst(compiler, FABS | FD(dst_fr) | FB(src)));
1403 break;
1404 }
1405
1406 if (dst_fr == TMP_FREG1)
1407 FAIL_IF(emit_fpu_data_transfer(compiler, src, 0, dst, dstw));
1408
1409 return SLJIT_SUCCESS;
1410 }
1411
1412 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
1413 int dst, sljit_w dstw,
1414 int src1, sljit_w src1w,
1415 int src2, sljit_w src2w)
1416 {
1417 int dst_fr;
1418
1419 CHECK_ERROR();
1420 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1421
1422 compiler->cache_arg = 0;
1423 compiler->cache_argw = 0;
1424
1425 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG1 : dst;
1426
1427 if (src2 > SLJIT_FLOAT_REG4) {
1428 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG2, 1, src2, src2w));
1429 src2 = TMP_FREG2;
1430 }
1431
1432 if (src1 > SLJIT_FLOAT_REG4) {
1433 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 1, src1, src1w));
1434 src1 = TMP_FREG1;
1435 }
1436
1437 switch (op) {
1438 case SLJIT_FADD:
1439 FAIL_IF(push_inst(compiler, FADD | FD(dst_fr) | FA(src1) | FB(src2)));
1440 break;
1441
1442 case SLJIT_FSUB:
1443 FAIL_IF(push_inst(compiler, FSUB | FD(dst_fr) | FA(src1) | FB(src2)));
1444 break;
1445
1446 case SLJIT_FMUL:
1447 FAIL_IF(push_inst(compiler, FMUL | FD(dst_fr) | FA(src1) | FC(src2) /* FMUL use FC as src2 */));
1448 break;
1449
1450 case SLJIT_FDIV:
1451 FAIL_IF(push_inst(compiler, FDIV | FD(dst_fr) | FA(src1) | FB(src2)));
1452 break;
1453 }
1454
1455 if (dst_fr == TMP_FREG1)
1456 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 0, dst, dstw));
1457
1458 return SLJIT_SUCCESS;
1459 }
1460
1461 /* --------------------------------------------------------------------- */
1462 /* Other instructions */
1463 /* --------------------------------------------------------------------- */
1464
1465 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int generals, int local_size)
1466 {
1467 CHECK_ERROR();
1468 check_sljit_emit_fast_enter(compiler, dst, dstw, args, temporaries, generals, local_size);
1469
1470 compiler->temporaries = temporaries;
1471 compiler->generals = generals;
1472
1473 compiler->has_locals = local_size > 0;
1474 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
1475 compiler->local_size = (2 + generals + 2) * sizeof(sljit_w) + local_size;
1476 #else
1477 compiler->local_size = (2 + generals + 7 + 8) * sizeof(sljit_w) + local_size;
1478 #endif
1479 compiler->local_size = (compiler->local_size + 15) & ~0xf;
1480
1481 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1482 return push_inst(compiler, MFLR | D(dst));
1483 else if (dst & SLJIT_MEM) {
1484 FAIL_IF(push_inst(compiler, MFLR | D(TMP_REG2)));
1485 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1486 }
1487
1488 return SLJIT_SUCCESS;
1489 }
1490
1491 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1492 {
1493 CHECK_ERROR();
1494 check_sljit_emit_fast_return(compiler, src, srcw);
1495
1496 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1497 FAIL_IF(push_inst(compiler, MTLR | S(src)));
1498 else {
1499 if (src & SLJIT_MEM)
1500 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1501 else if (src & SLJIT_IMM)
1502 FAIL_IF(load_immediate(compiler, TMP_REG2, srcw));
1503 FAIL_IF(push_inst(compiler, MTLR | S(TMP_REG2)));
1504 }
1505 return push_inst(compiler, BLR);
1506 }
1507
1508 /* --------------------------------------------------------------------- */
1509 /* Conditional instructions */
1510 /* --------------------------------------------------------------------- */
1511
1512 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1513 {
1514 struct sljit_label *label;
1515
1516 CHECK_ERROR_PTR();
1517 check_sljit_emit_label(compiler);
1518
1519 if (compiler->last_label && compiler->last_label->size == compiler->size)
1520 return compiler->last_label;
1521
1522 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1523 PTR_FAIL_IF(!label);
1524 set_label(label, compiler);
1525 return label;
1526 }
1527
1528 static sljit_ins get_bo_bi_flags(struct sljit_compiler *compiler, int type)
1529 {
1530 switch (type) {
1531 case SLJIT_C_EQUAL:
1532 return (12 << 21) | (2 << 16);
1533
1534 case SLJIT_C_NOT_EQUAL:
1535 return (4 << 21) | (2 << 16);
1536
1537 case SLJIT_C_LESS:
1538 case SLJIT_C_FLOAT_LESS:
1539 return (12 << 21) | ((4 + 0) << 16);
1540
1541 case SLJIT_C_GREATER_EQUAL:
1542 case SLJIT_C_FLOAT_GREATER_EQUAL:
1543 return (4 << 21) | ((4 + 0) << 16);
1544
1545 case SLJIT_C_GREATER:
1546 case SLJIT_C_FLOAT_GREATER:
1547 return (12 << 21) | ((4 + 1) << 16);
1548
1549 case SLJIT_C_LESS_EQUAL:
1550 case SLJIT_C_FLOAT_LESS_EQUAL:
1551 return (4 << 21) | ((4 + 1) << 16);
1552
1553 case SLJIT_C_SIG_LESS:
1554 return (12 << 21) | (0 << 16);
1555
1556 case SLJIT_C_SIG_GREATER_EQUAL:
1557 return (4 << 21) | (0 << 16);
1558
1559 case SLJIT_C_SIG_GREATER:
1560 return (12 << 21) | (1 << 16);
1561
1562 case SLJIT_C_SIG_LESS_EQUAL:
1563 return (4 << 21) | (1 << 16);
1564
1565 case SLJIT_C_OVERFLOW:
1566 case SLJIT_C_MUL_OVERFLOW:
1567 return (12 << 21) | (3 << 16);
1568
1569 case SLJIT_C_NOT_OVERFLOW:
1570 case SLJIT_C_MUL_NOT_OVERFLOW:
1571 return (4 << 21) | (3 << 16);
1572
1573 case SLJIT_C_FLOAT_EQUAL:
1574 return (12 << 21) | ((4 + 2) << 16);
1575
1576 case SLJIT_C_FLOAT_NOT_EQUAL:
1577 return (4 << 21) | ((4 + 2) << 16);
1578
1579 case SLJIT_C_FLOAT_NAN:
1580 return (12 << 21) | ((4 + 3) << 16);
1581
1582 case SLJIT_C_FLOAT_NOT_NAN:
1583 return (4 << 21) | ((4 + 3) << 16);
1584
1585 default:
1586 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
1587 return (20 << 21);
1588 }
1589 }
1590
1591 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type)
1592 {
1593 struct sljit_jump *jump;
1594 sljit_ins bo_bi_flags;
1595
1596 CHECK_ERROR_PTR();
1597 check_sljit_emit_jump(compiler, type);
1598
1599 bo_bi_flags = get_bo_bi_flags(compiler, type & 0xff);
1600 if (!bo_bi_flags)
1601 return NULL;
1602
1603 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1604 PTR_FAIL_IF(!jump);
1605 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1606 type &= 0xff;
1607
1608 /* In PPC, we don't need to touch the arguments. */
1609 if (type >= SLJIT_JUMP)
1610 jump->flags |= UNCOND_B;
1611
1612 PTR_FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1613 PTR_FAIL_IF(push_inst(compiler, MTCTR | S(TMP_REG1)));
1614 jump->addr = compiler->size;
1615 PTR_FAIL_IF(push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0)));
1616 return jump;
1617 }
1618
1619 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1620 {
1621 sljit_ins bo_bi_flags;
1622 struct sljit_jump *jump = NULL;
1623 int src_r;
1624
1625 CHECK_ERROR();
1626 check_sljit_emit_ijump(compiler, type, src, srcw);
1627
1628 bo_bi_flags = get_bo_bi_flags(compiler, type);
1629 FAIL_IF(!bo_bi_flags);
1630
1631 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1632 src_r = src;
1633 else if (src & SLJIT_IMM) {
1634 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1635 FAIL_IF(!jump);
1636 set_jump(jump, compiler, JUMP_ADDR | UNCOND_B);
1637 jump->u.target = srcw;
1638
1639 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1640 src_r = TMP_REG2;
1641 }
1642 else {
1643 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1644 src_r = TMP_REG2;
1645 }
1646
1647 FAIL_IF(push_inst(compiler, MTCTR | S(src_r)));
1648 if (jump)
1649 jump->addr = compiler->size;
1650 return push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0));
1651 }
1652
1653 /* Get a bit from CR, all other bits are zeroed. */
1654 #define GET_CR_BIT(bit, dst) \
1655 FAIL_IF(push_inst(compiler, MFCR | D(dst))); \
1656 FAIL_IF(push_inst(compiler, RLWINM | S(dst) | A(dst) | ((1 + (bit)) << 11) | (31 << 6) | (31 << 1)));
1657
1658 #define INVERT_BIT(dst) \
1659 FAIL_IF(push_inst(compiler, XORI | S(dst) | A(dst) | 0x1));
1660
1661 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1662 {
1663 int reg;
1664
1665 CHECK_ERROR();
1666 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1667
1668 if (dst == SLJIT_UNUSED)
1669 return SLJIT_SUCCESS;
1670
1671 reg = (op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1672
1673 switch (type) {
1674 case SLJIT_C_EQUAL:
1675 GET_CR_BIT(2, reg);
1676 break;
1677
1678 case SLJIT_C_NOT_EQUAL:
1679 GET_CR_BIT(2, reg);
1680 INVERT_BIT(reg);
1681 break;
1682
1683 case SLJIT_C_LESS:
1684 case SLJIT_C_FLOAT_LESS:
1685 GET_CR_BIT(4 + 0, reg);
1686 break;
1687
1688 case SLJIT_C_GREATER_EQUAL:
1689 case SLJIT_C_FLOAT_GREATER_EQUAL:
1690 GET_CR_BIT(4 + 0, reg);
1691 INVERT_BIT(reg);
1692 break;
1693
1694 case SLJIT_C_GREATER:
1695 case SLJIT_C_FLOAT_GREATER:
1696 GET_CR_BIT(4 + 1, reg);
1697 break;
1698
1699 case SLJIT_C_LESS_EQUAL:
1700 case SLJIT_C_FLOAT_LESS_EQUAL:
1701 GET_CR_BIT(4 + 1, reg);
1702 INVERT_BIT(reg);
1703 break;
1704
1705 case SLJIT_C_SIG_LESS:
1706 GET_CR_BIT(0, reg);
1707 break;
1708
1709 case SLJIT_C_SIG_GREATER_EQUAL:
1710 GET_CR_BIT(0, reg);
1711 INVERT_BIT(reg);
1712 break;
1713
1714 case SLJIT_C_SIG_GREATER:
1715 GET_CR_BIT(1, reg);
1716 break;
1717
1718 case SLJIT_C_SIG_LESS_EQUAL:
1719 GET_CR_BIT(1, reg);
1720 INVERT_BIT(reg);
1721 break;
1722
1723 case SLJIT_C_OVERFLOW:
1724 case SLJIT_C_MUL_OVERFLOW:
1725 GET_CR_BIT(3, reg);
1726 break;
1727
1728 case SLJIT_C_NOT_OVERFLOW:
1729 case SLJIT_C_MUL_NOT_OVERFLOW:
1730 GET_CR_BIT(3, reg);
1731 INVERT_BIT(reg);
1732 break;
1733
1734 case SLJIT_C_FLOAT_EQUAL:
1735 GET_CR_BIT(4 + 2, reg);
1736 break;
1737
1738 case SLJIT_C_FLOAT_NOT_EQUAL:
1739 GET_CR_BIT(4 + 2, reg);
1740 INVERT_BIT(reg);
1741 break;
1742
1743 case SLJIT_C_FLOAT_NAN:
1744 GET_CR_BIT(4 + 3, reg);
1745 break;
1746
1747 case SLJIT_C_FLOAT_NOT_NAN:
1748 GET_CR_BIT(4 + 3, reg);
1749 INVERT_BIT(reg);
1750 break;
1751
1752 default:
1753 SLJIT_ASSERT_STOP();
1754 break;
1755 }
1756
1757 if (GET_OPCODE(op) == SLJIT_OR)
1758 return emit_op(compiler, GET_OPCODE(op), GET_FLAGS(op) ? ALT_SET_FLAGS : 0, dst, dstw, dst, dstw, TMP_REG2, 0);
1759
1760 if (reg == TMP_REG2)
1761 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1762 return SLJIT_SUCCESS;
1763 }
1764
1765 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value)
1766 {
1767 struct sljit_const *const_;
1768 int reg;
1769
1770 CHECK_ERROR_PTR();
1771 check_sljit_emit_const(compiler, dst, dstw, init_value);
1772
1773 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1774 PTR_FAIL_IF(!const_);
1775 set_const(const_, compiler);
1776
1777 reg = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1778
1779 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1780
1781 if (dst & SLJIT_MEM)
1782 PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
1783 return const_;
1784 }

  ViewVC Help
Powered by ViewVC 1.1.5