/[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 1149 - (show annotations)
Sat Oct 20 21:33:38 2012 UTC (6 years, 10 months ago) by zherczeg
File MIME type: text/plain
File size: 63906 byte(s)
Major JIT compiler update with experimental Sparc 32 support.
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 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1174
1175 case SLJIT_MOV_UI:
1176 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1177
1178 case SLJIT_MOV_SI:
1179 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1180
1181 case SLJIT_MOV_UB:
1182 return EMIT_MOV(SLJIT_MOV_UB, BYTE_DATA, (unsigned char));
1183
1184 case SLJIT_MOV_SB:
1185 return EMIT_MOV(SLJIT_MOV_SB, BYTE_DATA | SIGNED_DATA, (signed char));
1186
1187 case SLJIT_MOV_UH:
1188 return EMIT_MOV(SLJIT_MOV_UH, HALF_DATA, (unsigned short));
1189
1190 case SLJIT_MOV_SH:
1191 return EMIT_MOV(SLJIT_MOV_SH, HALF_DATA | SIGNED_DATA, (signed short));
1192
1193 case SLJIT_MOVU:
1194 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1195
1196 case SLJIT_MOVU_UI:
1197 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1198
1199 case SLJIT_MOVU_SI:
1200 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1201
1202 case SLJIT_MOVU_UB:
1203 return EMIT_MOV(SLJIT_MOV_UB, BYTE_DATA | WRITE_BACK, (unsigned char));
1204
1205 case SLJIT_MOVU_SB:
1206 return EMIT_MOV(SLJIT_MOV_SB, BYTE_DATA | SIGNED_DATA | WRITE_BACK, (signed char));
1207
1208 case SLJIT_MOVU_UH:
1209 return EMIT_MOV(SLJIT_MOV_UH, HALF_DATA | WRITE_BACK, (unsigned short));
1210
1211 case SLJIT_MOVU_SH:
1212 return EMIT_MOV(SLJIT_MOV_SH, HALF_DATA | SIGNED_DATA | WRITE_BACK, (signed short));
1213
1214 case SLJIT_NOT:
1215 return emit_op(compiler, SLJIT_NOT, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1216
1217 case SLJIT_NEG:
1218 return emit_op(compiler, SLJIT_NEG, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1219
1220 case SLJIT_CLZ:
1221 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1222 return emit_op(compiler, SLJIT_CLZ, flags | (!(op & SLJIT_INT_OP) ? 0 : ALT_FORM1), dst, dstw, TMP_REG1, 0, src, srcw);
1223 #else
1224 return emit_op(compiler, SLJIT_CLZ, flags, dst, dstw, TMP_REG1, 0, src, srcw);
1225 #endif
1226 }
1227
1228 return SLJIT_SUCCESS;
1229 }
1230
1231 #undef EMIT_MOV
1232
1233 #define TEST_SL_IMM(src, srcw) \
1234 (((src) & SLJIT_IMM) && (srcw) <= SIMM_MAX && (srcw) >= SIMM_MIN)
1235
1236 #define TEST_UL_IMM(src, srcw) \
1237 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff))
1238
1239 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1240 #define TEST_SH_IMM(src, srcw) \
1241 (((src) & SLJIT_IMM) && !((srcw) & 0xffff) && (srcw) <= SLJIT_W(0x7fffffff) && (srcw) >= SLJIT_W(-0x80000000))
1242 #else
1243 #define TEST_SH_IMM(src, srcw) \
1244 (((src) & SLJIT_IMM) && !((srcw) & 0xffff))
1245 #endif
1246
1247 #define TEST_UH_IMM(src, srcw) \
1248 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff0000))
1249
1250 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1251 #define TEST_ADD_IMM(src, srcw) \
1252 (((src) & SLJIT_IMM) && (srcw) <= SLJIT_W(0x7fff7fff) && (srcw) >= SLJIT_W(-0x80000000))
1253 #else
1254 #define TEST_ADD_IMM(src, srcw) \
1255 ((src) & SLJIT_IMM)
1256 #endif
1257
1258 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1259 #define TEST_UI_IMM(src, srcw) \
1260 (((src) & SLJIT_IMM) && !((srcw) & ~0xffffffff))
1261 #else
1262 #define TEST_UI_IMM(src, srcw) \
1263 ((src) & SLJIT_IMM)
1264 #endif
1265
1266 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
1267 int dst, sljit_w dstw,
1268 int src1, sljit_w src1w,
1269 int src2, sljit_w src2w)
1270 {
1271 int flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
1272
1273 CHECK_ERROR();
1274 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1275 ADJUST_LOCAL_OFFSET(dst, dstw);
1276 ADJUST_LOCAL_OFFSET(src1, src1w);
1277 ADJUST_LOCAL_OFFSET(src2, src2w);
1278
1279 if ((src1 & SLJIT_IMM) && src1w == 0)
1280 src1 = ZERO_REG;
1281 if ((src2 & SLJIT_IMM) && src2w == 0)
1282 src2 = ZERO_REG;
1283
1284 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1285 if (op & SLJIT_INT_OP) {
1286 flags |= INT_DATA | SIGNED_DATA;
1287 if (src1 & SLJIT_IMM)
1288 src1w = (src1w << 32) >> 32;
1289 if (src2 & SLJIT_IMM)
1290 src2w = (src2w << 32) >> 32;
1291 if (GET_FLAGS(op))
1292 flags |= ALT_SIGN_EXT;
1293 }
1294 #endif
1295 if (op & SLJIT_SET_O)
1296 FAIL_IF(push_inst(compiler, MTXER | S(ZERO_REG)));
1297
1298 switch (GET_OPCODE(op)) {
1299 case SLJIT_ADD:
1300 if (!GET_FLAGS(op) && ((src1 | src2) & SLJIT_IMM)) {
1301 if (TEST_SL_IMM(src2, src2w)) {
1302 compiler->imm = src2w & 0xffff;
1303 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1304 }
1305 if (TEST_SL_IMM(src1, src1w)) {
1306 compiler->imm = src1w & 0xffff;
1307 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1308 }
1309 if (TEST_SH_IMM(src2, src2w)) {
1310 compiler->imm = (src2w >> 16) & 0xffff;
1311 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1312 }
1313 if (TEST_SH_IMM(src1, src1w)) {
1314 compiler->imm = (src1w >> 16) & 0xffff;
1315 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1316 }
1317 /* Range between -1 and -32768 is covered above. */
1318 if (TEST_ADD_IMM(src2, src2w)) {
1319 compiler->imm = src2w & 0xffffffff;
1320 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src1, src1w, TMP_REG2, 0);
1321 }
1322 if (TEST_ADD_IMM(src1, src1w)) {
1323 compiler->imm = src1w & 0xffffffff;
1324 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src2, src2w, TMP_REG2, 0);
1325 }
1326 }
1327 if (!(GET_FLAGS(op) & (SLJIT_SET_E | SLJIT_SET_O))) {
1328 if (TEST_SL_IMM(src2, src2w)) {
1329 compiler->imm = src2w & 0xffff;
1330 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1331 }
1332 if (TEST_SL_IMM(src1, src1w)) {
1333 compiler->imm = src1w & 0xffff;
1334 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
1335 }
1336 }
1337 return emit_op(compiler, SLJIT_ADD, flags, dst, dstw, src1, src1w, src2, src2w);
1338
1339 case SLJIT_ADDC:
1340 return emit_op(compiler, SLJIT_ADDC, flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
1341
1342 case SLJIT_SUB:
1343 if (!GET_FLAGS(op) && ((src1 | src2) & SLJIT_IMM)) {
1344 if (TEST_SL_IMM(src2, -src2w)) {
1345 compiler->imm = (-src2w) & 0xffff;
1346 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1347 }
1348 if (TEST_SL_IMM(src1, src1w)) {
1349 compiler->imm = src1w & 0xffff;
1350 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1351 }
1352 if (TEST_SH_IMM(src2, -src2w)) {
1353 compiler->imm = ((-src2w) >> 16) & 0xffff;
1354 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1355 }
1356 /* Range between -1 and -32768 is covered above. */
1357 if (TEST_ADD_IMM(src2, -src2w)) {
1358 compiler->imm = -src2w & 0xffffffff;
1359 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src1, src1w, TMP_REG2, 0);
1360 }
1361 }
1362 if (dst == SLJIT_UNUSED && (op & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U)) && !(op & (SLJIT_SET_O | SLJIT_SET_C))) {
1363 if (!(op & SLJIT_SET_U)) {
1364 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1365 if (TEST_SL_IMM(src2, src2w)) {
1366 compiler->imm = src2w & 0xffff;
1367 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1368 }
1369 if (GET_FLAGS(op) == SLJIT_SET_E && TEST_SL_IMM(src1, src1w)) {
1370 compiler->imm = src1w & 0xffff;
1371 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1372 }
1373 }
1374 if (!(op & (SLJIT_SET_E | SLJIT_SET_S))) {
1375 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1376 if (TEST_UL_IMM(src2, src2w)) {
1377 compiler->imm = src2w & 0xffff;
1378 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1379 }
1380 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM4, dst, dstw, src1, src1w, src2, src2w);
1381 }
1382 if ((src2 & SLJIT_IMM) && src2w >= 0 && src2w <= 0x7fff) {
1383 compiler->imm = src2w;
1384 return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2 | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1385 }
1386 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);
1387 }
1388 if (!(op & (SLJIT_SET_E | SLJIT_SET_S | SLJIT_SET_U | SLJIT_SET_O))) {
1389 if (TEST_SL_IMM(src2, -src2w)) {
1390 compiler->imm = (-src2w) & 0xffff;
1391 return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1392 }
1393 }
1394 /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
1395 return emit_op(compiler, SLJIT_SUB, flags | (!(op & SLJIT_SET_U) ? 0 : ALT_FORM6), dst, dstw, src1, src1w, src2, src2w);
1396
1397 case SLJIT_SUBC:
1398 return emit_op(compiler, SLJIT_SUBC, flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
1399
1400 case SLJIT_MUL:
1401 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1402 if (op & SLJIT_INT_OP)
1403 flags |= ALT_FORM2;
1404 #endif
1405 if (!GET_FLAGS(op)) {
1406 if (TEST_SL_IMM(src2, src2w)) {
1407 compiler->imm = src2w & 0xffff;
1408 return emit_op(compiler, SLJIT_MUL, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1409 }
1410 if (TEST_SL_IMM(src1, src1w)) {
1411 compiler->imm = src1w & 0xffff;
1412 return emit_op(compiler, SLJIT_MUL, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1413 }
1414 }
1415 return emit_op(compiler, SLJIT_MUL, flags, dst, dstw, src1, src1w, src2, src2w);
1416
1417 case SLJIT_AND:
1418 case SLJIT_OR:
1419 case SLJIT_XOR:
1420 /* Commutative unsigned operations. */
1421 if (!GET_FLAGS(op) || GET_OPCODE(op) == SLJIT_AND) {
1422 if (TEST_UL_IMM(src2, src2w)) {
1423 compiler->imm = src2w;
1424 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1425 }
1426 if (TEST_UL_IMM(src1, src1w)) {
1427 compiler->imm = src1w;
1428 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
1429 }
1430 if (TEST_UH_IMM(src2, src2w)) {
1431 compiler->imm = (src2w >> 16) & 0xffff;
1432 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
1433 }
1434 if (TEST_UH_IMM(src1, src1w)) {
1435 compiler->imm = (src1w >> 16) & 0xffff;
1436 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
1437 }
1438 }
1439 if (!GET_FLAGS(op) && GET_OPCODE(op) != SLJIT_AND) {
1440 if (TEST_UI_IMM(src2, src2w)) {
1441 compiler->imm = src2w;
1442 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
1443 }
1444 if (TEST_UI_IMM(src1, src1w)) {
1445 compiler->imm = src1w;
1446 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
1447 }
1448 }
1449 return emit_op(compiler, GET_OPCODE(op), flags, dst, dstw, src1, src1w, src2, src2w);
1450
1451 case SLJIT_SHL:
1452 case SLJIT_LSHR:
1453 case SLJIT_ASHR:
1454 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1455 if (op & SLJIT_INT_OP)
1456 flags |= ALT_FORM2;
1457 #endif
1458 if (src2 & SLJIT_IMM) {
1459 compiler->imm = src2w;
1460 return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
1461 }
1462 return emit_op(compiler, GET_OPCODE(op), flags, dst, dstw, src1, src1w, src2, src2w);
1463 }
1464
1465 return SLJIT_SUCCESS;
1466 }
1467
1468 SLJIT_API_FUNC_ATTRIBUTE int sljit_get_register_index(int reg)
1469 {
1470 check_sljit_get_register_index(reg);
1471 return reg_map[reg];
1472 }
1473
1474 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op_custom(struct sljit_compiler *compiler,
1475 void *instruction, int size)
1476 {
1477 CHECK_ERROR();
1478 check_sljit_emit_op_custom(compiler, instruction, size);
1479 SLJIT_ASSERT(size == 4);
1480
1481 return push_inst(compiler, *(sljit_ins*)instruction);
1482 }
1483
1484 /* --------------------------------------------------------------------- */
1485 /* Floating point operators */
1486 /* --------------------------------------------------------------------- */
1487
1488 SLJIT_API_FUNC_ATTRIBUTE int sljit_is_fpu_available(void)
1489 {
1490 /* Always available. */
1491 return 1;
1492 }
1493
1494 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
1495 int dst, sljit_w dstw,
1496 int src, sljit_w srcw)
1497 {
1498 int dst_fr;
1499
1500 CHECK_ERROR();
1501 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1502
1503 compiler->cache_arg = 0;
1504 compiler->cache_argw = 0;
1505
1506 if (GET_OPCODE(op) == SLJIT_FCMP) {
1507 if (dst > SLJIT_FLOAT_REG4) {
1508 FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, dst, dstw, src, srcw));
1509 dst = TMP_FREG1;
1510 }
1511
1512 if (src > SLJIT_FLOAT_REG4) {
1513 FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, src, srcw, 0, 0));
1514 src = TMP_FREG2;
1515 }
1516
1517 return push_inst(compiler, FCMPU | CRD(4) | FA(dst) | FB(src));
1518 }
1519
1520 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG1 : dst;
1521
1522 if (src > SLJIT_FLOAT_REG4) {
1523 FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, dst_fr, src, srcw, dst, dstw));
1524 src = dst_fr;
1525 }
1526
1527 switch (op) {
1528 case SLJIT_FMOV:
1529 if (src != dst_fr && dst_fr != TMP_FREG1)
1530 FAIL_IF(push_inst(compiler, FMR | FD(dst_fr) | FB(src)));
1531 break;
1532 case SLJIT_FNEG:
1533 FAIL_IF(push_inst(compiler, FNEG | FD(dst_fr) | FB(src)));
1534 break;
1535 case SLJIT_FABS:
1536 FAIL_IF(push_inst(compiler, FABS | FD(dst_fr) | FB(src)));
1537 break;
1538 }
1539
1540 if (dst_fr == TMP_FREG1) {
1541 if (op == SLJIT_FMOV)
1542 dst_fr = src;
1543 FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA, dst_fr, dst, dstw, 0, 0));
1544 }
1545
1546 return SLJIT_SUCCESS;
1547 }
1548
1549 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
1550 int dst, sljit_w dstw,
1551 int src1, sljit_w src1w,
1552 int src2, sljit_w src2w)
1553 {
1554 int dst_fr, flags = 0;
1555
1556 CHECK_ERROR();
1557 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1558
1559 compiler->cache_arg = 0;
1560 compiler->cache_argw = 0;
1561
1562 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG2 : dst;
1563
1564 if (src1 > SLJIT_FLOAT_REG4) {
1565 if (getput_arg_fast(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1566 FAIL_IF(compiler->error);
1567 src1 = TMP_FREG1;
1568 } else
1569 flags |= ALT_FORM1;
1570 }
1571
1572 if (src2 > SLJIT_FLOAT_REG4) {
1573 if (getput_arg_fast(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1574 FAIL_IF(compiler->error);
1575 src2 = TMP_FREG2;
1576 } else
1577 flags |= ALT_FORM2;
1578 }
1579
1580 if ((flags & (ALT_FORM1 | ALT_FORM2)) == (ALT_FORM1 | ALT_FORM2)) {
1581 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1582 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1583 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1584 }
1585 else {
1586 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1587 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1588 }
1589 }
1590 else if (flags & ALT_FORM1)
1591 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1592 else if (flags & ALT_FORM2)
1593 FAIL_IF(getput_arg(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1594
1595 if (flags & ALT_FORM1)
1596 src1 = TMP_FREG1;
1597 if (flags & ALT_FORM2)
1598 src2 = TMP_FREG2;
1599
1600 switch (op) {
1601 case SLJIT_FADD:
1602 FAIL_IF(push_inst(compiler, FADD | FD(dst_fr) | FA(src1) | FB(src2)));
1603 break;
1604
1605 case SLJIT_FSUB:
1606 FAIL_IF(push_inst(compiler, FSUB | FD(dst_fr) | FA(src1) | FB(src2)));
1607 break;
1608
1609 case SLJIT_FMUL:
1610 FAIL_IF(push_inst(compiler, FMUL | FD(dst_fr) | FA(src1) | FC(src2) /* FMUL use FC as src2 */));
1611 break;
1612
1613 case SLJIT_FDIV:
1614 FAIL_IF(push_inst(compiler, FDIV | FD(dst_fr) | FA(src1) | FB(src2)));
1615 break;
1616 }
1617
1618 if (dst_fr == TMP_FREG2)
1619 FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA, TMP_FREG2, dst, dstw, 0, 0));
1620
1621 return SLJIT_SUCCESS;
1622 }
1623
1624 /* --------------------------------------------------------------------- */
1625 /* Other instructions */
1626 /* --------------------------------------------------------------------- */
1627
1628 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw)
1629 {
1630 CHECK_ERROR();
1631 check_sljit_emit_fast_enter(compiler, dst, dstw);
1632 ADJUST_LOCAL_OFFSET(dst, dstw);
1633
1634 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1635 return push_inst(compiler, MFLR | D(dst));
1636 else if (dst & SLJIT_MEM) {
1637 FAIL_IF(push_inst(compiler, MFLR | D(TMP_REG2)));
1638 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1639 }
1640
1641 /* SLJIT_UNUSED is also possible, although highly unlikely. */
1642 return SLJIT_SUCCESS;
1643 }
1644
1645 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1646 {
1647 CHECK_ERROR();
1648 check_sljit_emit_fast_return(compiler, src, srcw);
1649 ADJUST_LOCAL_OFFSET(src, srcw);
1650
1651 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1652 FAIL_IF(push_inst(compiler, MTLR | S(src)));
1653 else {
1654 if (src & SLJIT_MEM)
1655 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1656 else if (src & SLJIT_IMM)
1657 FAIL_IF(load_immediate(compiler, TMP_REG2, srcw));
1658 FAIL_IF(push_inst(compiler, MTLR | S(TMP_REG2)));
1659 }
1660 return push_inst(compiler, BLR);
1661 }
1662
1663 /* --------------------------------------------------------------------- */
1664 /* Conditional instructions */
1665 /* --------------------------------------------------------------------- */
1666
1667 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1668 {
1669 struct sljit_label *label;
1670
1671 CHECK_ERROR_PTR();
1672 check_sljit_emit_label(compiler);
1673
1674 if (compiler->last_label && compiler->last_label->size == compiler->size)
1675 return compiler->last_label;
1676
1677 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1678 PTR_FAIL_IF(!label);
1679 set_label(label, compiler);
1680 return label;
1681 }
1682
1683 static sljit_ins get_bo_bi_flags(int type)
1684 {
1685 switch (type) {
1686 case SLJIT_C_EQUAL:
1687 return (12 << 21) | (2 << 16);
1688
1689 case SLJIT_C_NOT_EQUAL:
1690 return (4 << 21) | (2 << 16);
1691
1692 case SLJIT_C_LESS:
1693 case SLJIT_C_FLOAT_LESS:
1694 return (12 << 21) | ((4 + 0) << 16);
1695
1696 case SLJIT_C_GREATER_EQUAL:
1697 case SLJIT_C_FLOAT_GREATER_EQUAL:
1698 return (4 << 21) | ((4 + 0) << 16);
1699
1700 case SLJIT_C_GREATER:
1701 case SLJIT_C_FLOAT_GREATER:
1702 return (12 << 21) | ((4 + 1) << 16);
1703
1704 case SLJIT_C_LESS_EQUAL:
1705 case SLJIT_C_FLOAT_LESS_EQUAL:
1706 return (4 << 21) | ((4 + 1) << 16);
1707
1708 case SLJIT_C_SIG_LESS:
1709 return (12 << 21) | (0 << 16);
1710
1711 case SLJIT_C_SIG_GREATER_EQUAL:
1712 return (4 << 21) | (0 << 16);
1713
1714 case SLJIT_C_SIG_GREATER:
1715 return (12 << 21) | (1 << 16);
1716
1717 case SLJIT_C_SIG_LESS_EQUAL:
1718 return (4 << 21) | (1 << 16);
1719
1720 case SLJIT_C_OVERFLOW:
1721 case SLJIT_C_MUL_OVERFLOW:
1722 return (12 << 21) | (3 << 16);
1723
1724 case SLJIT_C_NOT_OVERFLOW:
1725 case SLJIT_C_MUL_NOT_OVERFLOW:
1726 return (4 << 21) | (3 << 16);
1727
1728 case SLJIT_C_FLOAT_EQUAL:
1729 return (12 << 21) | ((4 + 2) << 16);
1730
1731 case SLJIT_C_FLOAT_NOT_EQUAL:
1732 return (4 << 21) | ((4 + 2) << 16);
1733
1734 case SLJIT_C_FLOAT_UNORDERED:
1735 return (12 << 21) | ((4 + 3) << 16);
1736
1737 case SLJIT_C_FLOAT_ORDERED:
1738 return (4 << 21) | ((4 + 3) << 16);
1739
1740 default:
1741 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
1742 return (20 << 21);
1743 }
1744 }
1745
1746 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type)
1747 {
1748 struct sljit_jump *jump;
1749 sljit_ins bo_bi_flags;
1750
1751 CHECK_ERROR_PTR();
1752 check_sljit_emit_jump(compiler, type);
1753
1754 bo_bi_flags = get_bo_bi_flags(type & 0xff);
1755 if (!bo_bi_flags)
1756 return NULL;
1757
1758 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1759 PTR_FAIL_IF(!jump);
1760 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1761 type &= 0xff;
1762
1763 /* In PPC, we don't need to touch the arguments. */
1764 if (type >= SLJIT_JUMP)
1765 jump->flags |= UNCOND_B;
1766
1767 PTR_FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1768 PTR_FAIL_IF(push_inst(compiler, MTCTR | S(TMP_REG1)));
1769 jump->addr = compiler->size;
1770 PTR_FAIL_IF(push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0)));
1771 return jump;
1772 }
1773
1774 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1775 {
1776 struct sljit_jump *jump = NULL;
1777 int src_r;
1778
1779 CHECK_ERROR();
1780 check_sljit_emit_ijump(compiler, type, src, srcw);
1781 ADJUST_LOCAL_OFFSET(src, srcw);
1782
1783 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1784 src_r = src;
1785 else if (src & SLJIT_IMM) {
1786 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1787 FAIL_IF(!jump);
1788 set_jump(jump, compiler, JUMP_ADDR | UNCOND_B);
1789 jump->u.target = srcw;
1790
1791 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1792 src_r = TMP_REG2;
1793 }
1794 else {
1795 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1796 src_r = TMP_REG2;
1797 }
1798
1799 FAIL_IF(push_inst(compiler, MTCTR | S(src_r)));
1800 if (jump)
1801 jump->addr = compiler->size;
1802 return push_inst(compiler, BCCTR | (20 << 21) | (type >= SLJIT_FAST_CALL ? 1 : 0));
1803 }
1804
1805 /* Get a bit from CR, all other bits are zeroed. */
1806 #define GET_CR_BIT(bit, dst) \
1807 FAIL_IF(push_inst(compiler, MFCR | D(dst))); \
1808 FAIL_IF(push_inst(compiler, RLWINM | S(dst) | A(dst) | ((1 + (bit)) << 11) | (31 << 6) | (31 << 1)));
1809
1810 #define INVERT_BIT(dst) \
1811 FAIL_IF(push_inst(compiler, XORI | S(dst) | A(dst) | 0x1));
1812
1813 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1814 {
1815 int reg;
1816
1817 CHECK_ERROR();
1818 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1819 ADJUST_LOCAL_OFFSET(dst, dstw);
1820
1821 if (dst == SLJIT_UNUSED)
1822 return SLJIT_SUCCESS;
1823
1824 reg = (op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1825
1826 switch (type) {
1827 case SLJIT_C_EQUAL:
1828 GET_CR_BIT(2, reg);
1829 break;
1830
1831 case SLJIT_C_NOT_EQUAL:
1832 GET_CR_BIT(2, reg);
1833 INVERT_BIT(reg);
1834 break;
1835
1836 case SLJIT_C_LESS:
1837 case SLJIT_C_FLOAT_LESS:
1838 GET_CR_BIT(4 + 0, reg);
1839 break;
1840
1841 case SLJIT_C_GREATER_EQUAL:
1842 case SLJIT_C_FLOAT_GREATER_EQUAL:
1843 GET_CR_BIT(4 + 0, reg);
1844 INVERT_BIT(reg);
1845 break;
1846
1847 case SLJIT_C_GREATER:
1848 case SLJIT_C_FLOAT_GREATER:
1849 GET_CR_BIT(4 + 1, reg);
1850 break;
1851
1852 case SLJIT_C_LESS_EQUAL:
1853 case SLJIT_C_FLOAT_LESS_EQUAL:
1854 GET_CR_BIT(4 + 1, reg);
1855 INVERT_BIT(reg);
1856 break;
1857
1858 case SLJIT_C_SIG_LESS:
1859 GET_CR_BIT(0, reg);
1860 break;
1861
1862 case SLJIT_C_SIG_GREATER_EQUAL:
1863 GET_CR_BIT(0, reg);
1864 INVERT_BIT(reg);
1865 break;
1866
1867 case SLJIT_C_SIG_GREATER:
1868 GET_CR_BIT(1, reg);
1869 break;
1870
1871 case SLJIT_C_SIG_LESS_EQUAL:
1872 GET_CR_BIT(1, reg);
1873 INVERT_BIT(reg);
1874 break;
1875
1876 case SLJIT_C_OVERFLOW:
1877 case SLJIT_C_MUL_OVERFLOW:
1878 GET_CR_BIT(3, reg);
1879 break;
1880
1881 case SLJIT_C_NOT_OVERFLOW:
1882 case SLJIT_C_MUL_NOT_OVERFLOW:
1883 GET_CR_BIT(3, reg);
1884 INVERT_BIT(reg);
1885 break;
1886
1887 case SLJIT_C_FLOAT_EQUAL:
1888 GET_CR_BIT(4 + 2, reg);
1889 break;
1890
1891 case SLJIT_C_FLOAT_NOT_EQUAL:
1892 GET_CR_BIT(4 + 2, reg);
1893 INVERT_BIT(reg);
1894 break;
1895
1896 case SLJIT_C_FLOAT_UNORDERED:
1897 GET_CR_BIT(4 + 3, reg);
1898 break;
1899
1900 case SLJIT_C_FLOAT_ORDERED:
1901 GET_CR_BIT(4 + 3, reg);
1902 INVERT_BIT(reg);
1903 break;
1904
1905 default:
1906 SLJIT_ASSERT_STOP();
1907 break;
1908 }
1909
1910 if (GET_OPCODE(op) == SLJIT_OR)
1911 return emit_op(compiler, SLJIT_OR, GET_FLAGS(op) ? ALT_SET_FLAGS : 0, dst, dstw, dst, dstw, TMP_REG2, 0);
1912
1913 return (reg == TMP_REG2) ? emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0) : SLJIT_SUCCESS;
1914 }
1915
1916 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value)
1917 {
1918 struct sljit_const *const_;
1919 int reg;
1920
1921 CHECK_ERROR_PTR();
1922 check_sljit_emit_const(compiler, dst, dstw, init_value);
1923 ADJUST_LOCAL_OFFSET(dst, dstw);
1924
1925 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1926 PTR_FAIL_IF(!const_);
1927 set_const(const_, compiler);
1928
1929 reg = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1930
1931 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1932
1933 if (dst & SLJIT_MEM)
1934 PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
1935 return const_;
1936 }

  ViewVC Help
Powered by ViewVC 1.1.5