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

  ViewVC Help
Powered by ViewVC 1.1.5