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

  ViewVC Help
Powered by ViewVC 1.1.5