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

  ViewVC Help
Powered by ViewVC 1.1.5