/[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 880 - (show annotations)
Sun Jan 15 17:23:37 2012 UTC (7 years, 8 months ago) by zherczeg
File MIME type: text/plain
File size: 62933 byte(s)
JIT compiler update (rename GENERAL to SAVED)
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 saveds, int local_size)
441 {
442 CHECK_ERROR();
443 check_sljit_emit_enter(compiler, args, temporaries, saveds, local_size);
444
445 compiler->temporaries = temporaries;
446 compiler->saveds = saveds;
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 (saveds >= 1)
454 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_SAVED_REG1) | A(REAL_STACK_PTR) | IMM(-3 * (int)(sizeof(sljit_w))) ));
455 if (saveds >= 2)
456 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_SAVED_REG2) | A(REAL_STACK_PTR) | IMM(-4 * (int)(sizeof(sljit_w))) ));
457 if (saveds >= 3)
458 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_SAVED_REG3) | A(REAL_STACK_PTR) | IMM(-5 * (int)(sizeof(sljit_w))) ));
459 if (saveds >= 4)
460 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_SAVED_EREG1) | A(REAL_STACK_PTR) | IMM(-6 * (int)(sizeof(sljit_w))) ));
461 if (saveds >= 5)
462 FAIL_IF(push_inst(compiler, STACK_STORE | S(SLJIT_SAVED_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_SAVED_REG1) | B(SLJIT_TEMPORARY_REG1)));
468 if (args >= 2)
469 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG2) | A(SLJIT_SAVED_REG2) | B(SLJIT_TEMPORARY_REG2)));
470 if (args >= 3)
471 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG3) | A(SLJIT_SAVED_REG3) | B(SLJIT_TEMPORARY_REG3)));
472
473 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
474 compiler->local_size = (2 + saveds + 2) * sizeof(sljit_w) + local_size;
475 #else
476 compiler->local_size = (2 + saveds + 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 saveds, int local_size)
504 {
505 CHECK_ERROR_VOID();
506 check_sljit_set_context(compiler, args, temporaries, saveds, local_size);
507
508 compiler->temporaries = temporaries;
509 compiler->saveds = saveds;
510
511 compiler->has_locals = local_size > 0;
512 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
513 compiler->local_size = (2 + saveds + 2) * sizeof(sljit_w) + local_size;
514 #else
515 compiler->local_size = (2 + saveds + 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 op, int src, sljit_w srcw)
521 {
522 CHECK_ERROR();
523 check_sljit_emit_return(compiler, op, src, srcw);
524
525 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
526
527 if (compiler->local_size <= SIMM_MAX)
528 FAIL_IF(push_inst(compiler, ADDI | D(REAL_STACK_PTR) | A(REAL_STACK_PTR) | IMM(compiler->local_size)));
529 else {
530 FAIL_IF(load_immediate(compiler, 0, compiler->local_size));
531 FAIL_IF(push_inst(compiler, ADD | D(REAL_STACK_PTR) | A(REAL_STACK_PTR) | B(0)));
532 }
533
534 FAIL_IF(push_inst(compiler, STACK_LOAD | D(0) | A(REAL_STACK_PTR) | IMM(sizeof(sljit_w))));
535 if (compiler->saveds >= 5)
536 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_SAVED_EREG2) | A(REAL_STACK_PTR) | IMM(-7 * (int)(sizeof(sljit_w))) ));
537 if (compiler->saveds >= 4)
538 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_SAVED_EREG1) | A(REAL_STACK_PTR) | IMM(-6 * (int)(sizeof(sljit_w))) ));
539 if (compiler->saveds >= 3)
540 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_SAVED_REG3) | A(REAL_STACK_PTR) | IMM(-5 * (int)(sizeof(sljit_w))) ));
541 if (compiler->saveds >= 2)
542 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_SAVED_REG2) | A(REAL_STACK_PTR) | IMM(-4 * (int)(sizeof(sljit_w))) ));
543 if (compiler->saveds >= 1)
544 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_SAVED_REG1) | A(REAL_STACK_PTR) | IMM(-3 * (int)(sizeof(sljit_w))) ));
545 FAIL_IF(push_inst(compiler, STACK_LOAD | D(ZERO_REG) | A(REAL_STACK_PTR) | IMM(-2 * (int)(sizeof(sljit_w))) ));
546 if (compiler->has_locals)
547 FAIL_IF(push_inst(compiler, STACK_LOAD | D(SLJIT_LOCALS_REG) | A(REAL_STACK_PTR) | IMM(-(int)(sizeof(sljit_w))) ));
548
549 FAIL_IF(push_inst(compiler, MTLR | S(0)));
550 FAIL_IF(push_inst(compiler, BLR));
551
552 return SLJIT_SUCCESS;
553 }
554
555 #undef STACK_STORE
556 #undef STACK_LOAD
557
558 /* --------------------------------------------------------------------- */
559 /* Operators */
560 /* --------------------------------------------------------------------- */
561
562 /* i/x - immediate/indexed form
563 n/w - no write-back / write-back (1 bit)
564 s/l - store/load (1 bit)
565 u/s - signed/unsigned (1 bit)
566 w/b/h/i - word/byte/half/int allowed (2 bit)
567 It contans 32 items, but not all are different. */
568
569 /* 64 bit only: [reg+imm] must be aligned to 4 bytes. */
570 #define ADDR_MODE2 0x10000
571 /* 64-bit only: there is no lwau instruction. */
572 #define UPDATE_REQ 0x20000
573
574 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
575 #define ARCH_DEPEND(a, b) a
576 #define GET_INST_CODE(inst) (inst)
577 #else
578 #define ARCH_DEPEND(a, b) b
579 #define GET_INST_CODE(index) ((inst) & ~(ADDR_MODE2 | UPDATE_REQ))
580 #endif
581
582 static SLJIT_CONST sljit_ins data_transfer_insts[64] = {
583
584 /* No write-back. */
585
586 /* i n s u w */ ARCH_DEPEND(HI(36) /* stw */, HI(62) | ADDR_MODE2 | 0x0 /* std */),
587 /* i n s u b */ HI(38) /* stb */,
588 /* i n s u h */ HI(44) /* sth*/,
589 /* i n s u i */ HI(36) /* stw */,
590
591 /* i n s s w */ ARCH_DEPEND(HI(36) /* stw */, HI(62) | ADDR_MODE2 | 0x0 /* std */),
592 /* i n s s b */ HI(38) /* stb */,
593 /* i n s s h */ HI(44) /* sth*/,
594 /* i n s s i */ HI(36) /* stw */,
595
596 /* i n l u w */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x0 /* ld */),
597 /* i n l u b */ HI(34) /* lbz */,
598 /* i n l u h */ HI(40) /* lhz */,
599 /* i n l u i */ HI(32) /* lwz */,
600
601 /* i n l s w */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x0 /* ld */),
602 /* i n l s b */ HI(34) /* lbz */ /* EXTS_REQ */,
603 /* i n l s h */ HI(42) /* lha */,
604 /* i n l s i */ ARCH_DEPEND(HI(32) /* lwz */, HI(58) | ADDR_MODE2 | 0x2 /* lwa */),
605
606 /* Write-back. */
607
608 /* i w s u w */ ARCH_DEPEND(HI(37) /* stwu */, HI(62) | ADDR_MODE2 | 0x1 /* stdu */),
609 /* i w s u b */ HI(39) /* stbu */,
610 /* i w s u h */ HI(45) /* sthu */,
611 /* i w s u i */ HI(37) /* stwu */,
612
613 /* i w s s w */ ARCH_DEPEND(HI(37) /* stwu */, HI(62) | ADDR_MODE2 | 0x1 /* stdu */),
614 /* i w s s b */ HI(39) /* stbu */,
615 /* i w s s h */ HI(45) /* sthu */,
616 /* i w s s i */ HI(37) /* stwu */,
617
618 /* i w l u w */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | 0x1 /* ldu */),
619 /* i w l u b */ HI(35) /* lbzu */,
620 /* i w l u h */ HI(41) /* lhzu */,
621 /* i w l u i */ HI(33) /* lwzu */,
622
623 /* i w l s w */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | 0x1 /* ldu */),
624 /* i w l s b */ HI(35) /* lbzu */ /* EXTS_REQ */,
625 /* i w l s h */ HI(43) /* lhau */,
626 /* i w l s i */ ARCH_DEPEND(HI(33) /* lwzu */, HI(58) | ADDR_MODE2 | UPDATE_REQ | 0x2 /* lwa */),
627
628 /* ---------- */
629 /* Indexed */
630 /* ---------- */
631
632 /* No write-back. */
633
634 /* x n s u w */ ARCH_DEPEND(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
635 /* x n s u b */ HI(31) | LO(215) /* stbx */,
636 /* x n s u h */ HI(31) | LO(407) /* sthx */,
637 /* x n s u i */ HI(31) | LO(151) /* stwx */,
638
639 /* x n s s w */ ARCH_DEPEND(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
640 /* x n s s b */ HI(31) | LO(215) /* stbx */,
641 /* x n s s h */ HI(31) | LO(407) /* sthx */,
642 /* x n s s i */ HI(31) | LO(151) /* stwx */,
643
644 /* x n l u w */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
645 /* x n l u b */ HI(31) | LO(87) /* lbzx */,
646 /* x n l u h */ HI(31) | LO(279) /* lhzx */,
647 /* x n l u i */ HI(31) | LO(23) /* lwzx */,
648
649 /* x n l s w */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
650 /* x n l s b */ HI(31) | LO(87) /* lbzx */ /* EXTS_REQ */,
651 /* x n l s h */ HI(31) | LO(343) /* lhax */,
652 /* x n l s i */ ARCH_DEPEND(HI(31) | LO(23) /* lwzx */, HI(31) | LO(341) /* lwax */),
653
654 /* Write-back. */
655
656 /* x w s u w */ ARCH_DEPEND(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
657 /* x w s u b */ HI(31) | LO(247) /* stbux */,
658 /* x w s u h */ HI(31) | LO(439) /* sthux */,
659 /* x w s u i */ HI(31) | LO(183) /* stwux */,
660
661 /* x w s s w */ ARCH_DEPEND(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
662 /* x w s s b */ HI(31) | LO(247) /* stbux */,
663 /* x w s s h */ HI(31) | LO(439) /* sthux */,
664 /* x w s s i */ HI(31) | LO(183) /* stwux */,
665
666 /* x w l u w */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
667 /* x w l u b */ HI(31) | LO(119) /* lbzux */,
668 /* x w l u h */ HI(31) | LO(311) /* lhzux */,
669 /* x w l u i */ HI(31) | LO(55) /* lwzux */,
670
671 /* x w l s w */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
672 /* x w l s b */ HI(31) | LO(119) /* lbzux */ /* EXTS_REQ */,
673 /* x w l s h */ HI(31) | LO(375) /* lhaux */,
674 /* x w l s i */ ARCH_DEPEND(HI(31) | LO(55) /* lwzux */, HI(31) | LO(373) /* lwaux */)
675
676 };
677
678 #undef ARCH_DEPEND
679
680 /* Simple cases, (no caching is required). */
681 static int getput_arg_fast(struct sljit_compiler *compiler, int inp_flags, int reg, int arg, sljit_w argw)
682 {
683 sljit_ins inst;
684 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
685 int tmp_reg;
686 #endif
687
688 SLJIT_ASSERT(arg & SLJIT_MEM);
689 if (!(arg & 0xf)) {
690 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
691 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
692 if (inp_flags & ARG_TEST)
693 return 1;
694
695 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
696 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
697 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | IMM(argw));
698 return -1;
699 }
700 #else
701 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
702 if (argw <= SIMM_MAX && argw >= SIMM_MIN &&
703 (!(inst & ADDR_MODE2) || (argw & 0x3) == 0)) {
704 if (inp_flags & ARG_TEST)
705 return 1;
706
707 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | IMM(argw));
708 return -1;
709 }
710 #endif
711 return (inp_flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
712 }
713
714 if (!(arg & 0xf0)) {
715 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
716 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
717 if (inp_flags & ARG_TEST)
718 return 1;
719
720 inst = data_transfer_insts[inp_flags & MEM_MASK];
721 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
722 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | IMM(argw));
723 return -1;
724 }
725 #else
726 inst = data_transfer_insts[inp_flags & MEM_MASK];
727 if (argw <= SIMM_MAX && argw >= SIMM_MIN && (!(inst & ADDR_MODE2) || (argw & 0x3) == 0)) {
728 if (inp_flags & ARG_TEST)
729 return 1;
730
731 if ((inp_flags & WRITE_BACK) && (inst & UPDATE_REQ)) {
732 tmp_reg = (inp_flags & LOAD_DATA) ? (arg & 0xf) : TMP_REG3;
733 if (push_inst(compiler, ADDI | D(tmp_reg) | A(arg & 0xf) | IMM(argw)))
734 return -1;
735 arg = tmp_reg | SLJIT_MEM;
736 argw = 0;
737 }
738 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | IMM(argw));
739 return -1;
740 }
741 #endif
742 }
743 else if (!(argw & 0x3)) {
744 if (inp_flags & ARG_TEST)
745 return 1;
746 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
747 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
748 push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B((arg >> 4) & 0xf));
749 return -1;
750 }
751 return (inp_flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
752 }
753
754 /* See getput_arg below.
755 Note: can_cache is called only for binary operators. Those operator always
756 uses word arguments without write back. */
757 static int can_cache(int arg, sljit_w argw, int next_arg, sljit_w next_argw)
758 {
759 SLJIT_ASSERT(arg & SLJIT_MEM);
760 SLJIT_ASSERT(next_arg & SLJIT_MEM);
761
762 if (!(arg & 0xf)) {
763 if ((next_arg & SLJIT_MEM) && ((sljit_uw)argw - (sljit_uw)next_argw <= SIMM_MAX || (sljit_uw)next_argw - (sljit_uw)argw <= SIMM_MAX))
764 return 1;
765 return 0;
766 }
767
768 if (arg & 0xf0)
769 return 0;
770
771 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
772 if (arg == next_arg && (next_argw >= SIMM_MAX && next_argw <= SIMM_MIN))
773 return 1;
774 }
775
776 if (arg == next_arg && ((sljit_uw)argw - (sljit_uw)next_argw <= SIMM_MAX || (sljit_uw)next_argw - (sljit_uw)argw <= SIMM_MAX))
777 return 1;
778
779 return 0;
780 }
781
782 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
783 #define ADJUST_CACHED_IMM(imm) \
784 if ((inst & ADDR_MODE2) && (imm & 0x3)) { \
785 /* Adjust cached value. Fortunately this is really a rare case */ \
786 compiler->cache_argw += imm & 0x3; \
787 FAIL_IF(push_inst(compiler, ADDI | D(TMP_REG3) | A(TMP_REG3) | (imm & 0x3))); \
788 imm &= ~0x3; \
789 }
790 #else
791 #define ADJUST_CACHED_IMM(imm)
792 #endif
793
794 /* Emit the necessary instructions. See can_cache above. */
795 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)
796 {
797 int tmp_r;
798 sljit_ins inst;
799
800 SLJIT_ASSERT(arg & SLJIT_MEM);
801
802 tmp_r = (inp_flags & LOAD_DATA) ? reg : TMP_REG3;
803 if ((arg & 0xf) == tmp_r) {
804 /* Special case for "mov reg, [reg, ... ]".
805 Caching would not happen anyway. */
806 tmp_r = TMP_REG3;
807 compiler->cache_arg = 0;
808 compiler->cache_argw = 0;
809 }
810
811 if (!(arg & 0xf)) {
812 inst = data_transfer_insts[(inp_flags & ~WRITE_BACK) & MEM_MASK];
813 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)) {
814 argw = argw - compiler->cache_argw;
815 ADJUST_CACHED_IMM(argw);
816 SLJIT_ASSERT(!(inst & UPDATE_REQ));
817 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3) | IMM(argw));
818 }
819
820 if ((next_arg & SLJIT_MEM) && (argw - next_argw <= SIMM_MAX || next_argw - argw <= SIMM_MAX)) {
821 SLJIT_ASSERT(inp_flags & LOAD_DATA);
822
823 compiler->cache_arg = SLJIT_IMM;
824 compiler->cache_argw = argw;
825 tmp_r = TMP_REG3;
826 }
827
828 FAIL_IF(load_immediate(compiler, tmp_r, argw));
829 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(tmp_r));
830 }
831
832 if (SLJIT_UNLIKELY(arg & 0xf0)) {
833 argw &= 0x3;
834 /* Otherwise getput_arg_fast would capture it. */
835 SLJIT_ASSERT(argw);
836 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
837 FAIL_IF(push_inst(compiler, RLWINM | S((arg >> 4) & 0xf) | A(tmp_r) | (argw << 11) | ((31 - argw) << 1)));
838 #else
839 FAIL_IF(push_inst(compiler, RLDI(tmp_r, (arg >> 4) & 0xf, argw, 63 - argw, 1)));
840 #endif
841 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
842 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
843 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(tmp_r));
844 }
845
846 inst = data_transfer_insts[inp_flags & MEM_MASK];
847
848 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)) {
849 SLJIT_ASSERT(!(inp_flags & WRITE_BACK));
850 argw = argw - compiler->cache_argw;
851 ADJUST_CACHED_IMM(argw);
852 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3) | IMM(argw));
853 }
854
855 if ((compiler->cache_arg & SLJIT_IMM) && compiler->cache_argw == argw) {
856 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
857 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
858 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(TMP_REG3));
859 }
860
861 if (argw == next_argw && (next_arg & SLJIT_MEM)) {
862 SLJIT_ASSERT(inp_flags & LOAD_DATA);
863 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
864
865 compiler->cache_arg = SLJIT_IMM;
866 compiler->cache_argw = argw;
867
868 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
869 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
870 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(TMP_REG3));
871 }
872
873 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)) {
874 SLJIT_ASSERT(inp_flags & LOAD_DATA);
875 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
876 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | A(TMP_REG3) | B(arg & 0xf)));
877
878 compiler->cache_arg = arg;
879 compiler->cache_argw = argw;
880
881 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(TMP_REG3));
882 }
883
884 /* Get the indexed version instead of the normal one. */
885 inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
886 SLJIT_ASSERT(!(inst & (ADDR_MODE2 | UPDATE_REQ)));
887 FAIL_IF(load_immediate(compiler, tmp_r, argw));
888 return push_inst(compiler, GET_INST_CODE(inst) | D(reg) | A(arg & 0xf) | B(tmp_r));
889 }
890
891 static int emit_op(struct sljit_compiler *compiler, int op, int inp_flags,
892 int dst, sljit_w dstw,
893 int src1, sljit_w src1w,
894 int src2, sljit_w src2w)
895 {
896 /* arg1 goes to TMP_REG1 or src reg
897 arg2 goes to TMP_REG2, imm or src reg
898 TMP_REG3 can be used for caching
899 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
900 int dst_r;
901 int src1_r;
902 int src2_r;
903 int sugg_src2_r = TMP_REG2;
904 int flags = inp_flags & (ALT_FORM1 | ALT_FORM2 | ALT_FORM3 | ALT_FORM4 | ALT_FORM5 | ALT_FORM6 | ALT_SIGN_EXT | ALT_SET_FLAGS);
905
906 compiler->cache_arg = 0;
907 compiler->cache_argw = 0;
908
909 /* Destination check. */
910 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= ZERO_REG) {
911 dst_r = dst;
912 flags |= REG_DEST;
913 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
914 sugg_src2_r = dst_r;
915 }
916 else if (dst == SLJIT_UNUSED) {
917 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
918 return SLJIT_SUCCESS;
919 dst_r = TMP_REG2;
920 }
921 else {
922 SLJIT_ASSERT(dst & SLJIT_MEM);
923 if (getput_arg_fast(compiler, inp_flags | ARG_TEST, TMP_REG2, dst, dstw)) {
924 flags |= FAST_DEST;
925 dst_r = TMP_REG2;
926 }
927 else {
928 flags |= SLOW_DEST;
929 dst_r = 0;
930 }
931 }
932
933 /* Source 1. */
934 if (src1 >= SLJIT_TEMPORARY_REG1 && src1 <= ZERO_REG) {
935 src1_r = src1;
936 flags |= REG1_SOURCE;
937 }
938 else if (src1 & SLJIT_IMM) {
939 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
940 if ((inp_flags & 0x3) == INT_DATA) {
941 if (inp_flags & SIGNED_DATA)
942 src1w = (signed int)src1w;
943 else
944 src1w = (unsigned int)src1w;
945 }
946 #endif
947 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
948 src1_r = TMP_REG1;
949 }
950 else if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w)) {
951 FAIL_IF(compiler->error);
952 src1_r = TMP_REG1;
953 }
954 else
955 src1_r = 0;
956
957 /* Source 2. */
958 if (src2 >= SLJIT_TEMPORARY_REG1 && src2 <= ZERO_REG) {
959 src2_r = src2;
960 flags |= REG2_SOURCE;
961 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
962 dst_r = src2_r;
963 }
964 else if (src2 & SLJIT_IMM) {
965 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
966 if ((inp_flags & 0x3) == INT_DATA) {
967 if (inp_flags & SIGNED_DATA)
968 src2w = (signed int)src2w;
969 else
970 src2w = (unsigned int)src2w;
971 }
972 #endif
973 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
974 src2_r = sugg_src2_r;
975 }
976 else if (getput_arg_fast(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w)) {
977 FAIL_IF(compiler->error);
978 src2_r = sugg_src2_r;
979 }
980 else
981 src2_r = 0;
982
983 /* src1_r, src2_r and dst_r can be zero (=unprocessed).
984 All arguments are complex addressing modes, and it is a binary operator. */
985 if (src1_r == 0 && src2_r == 0 && dst_r == 0) {
986 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
987 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
988 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
989 }
990 else {
991 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
992 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
993 }
994 src1_r = TMP_REG1;
995 src2_r = TMP_REG2;
996 }
997 else if (src1_r == 0 && src2_r == 0) {
998 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
999 src1_r = TMP_REG1;
1000 }
1001 else if (src1_r == 0 && dst_r == 0) {
1002 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
1003 src1_r = TMP_REG1;
1004 }
1005 else if (src2_r == 0 && dst_r == 0) {
1006 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
1007 src2_r = sugg_src2_r;
1008 }
1009
1010 if (dst_r == 0)
1011 dst_r = TMP_REG2;
1012
1013 if (src1_r == 0) {
1014 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, 0, 0));
1015 src1_r = TMP_REG1;
1016 }
1017
1018 if (src2_r == 0) {
1019 FAIL_IF(getput_arg(compiler, inp_flags | LOAD_DATA, sugg_src2_r, src2, src2w, 0, 0));
1020 src2_r = sugg_src2_r;
1021 }
1022
1023 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
1024
1025 if (flags & (FAST_DEST | SLOW_DEST)) {
1026 if (flags & FAST_DEST)
1027 FAIL_IF(getput_arg_fast(compiler, inp_flags, dst_r, dst, dstw));
1028 else
1029 FAIL_IF(getput_arg(compiler, inp_flags, dst_r, dst, dstw, 0, 0));
1030 }
1031 return SLJIT_SUCCESS;
1032 }
1033
1034 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op0(struct sljit_compiler *compiler, int op)
1035 {
1036 CHECK_ERROR();
1037 check_sljit_emit_op0(compiler, op);
1038
1039 switch (GET_OPCODE(op)) {
1040 case SLJIT_BREAKPOINT:
1041 case SLJIT_NOP:
1042 return push_inst(compiler, NOP);
1043 break;
1044 case SLJIT_UMUL:
1045 case SLJIT_SMUL:
1046 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG1)));
1047 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1048 FAIL_IF(push_inst(compiler, MULLD | D(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2)));
1049 return push_inst(compiler, (GET_OPCODE(op) == SLJIT_UMUL ? MULHDU : MULHD) | D(SLJIT_TEMPORARY_REG2) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2));
1050 #else
1051 FAIL_IF(push_inst(compiler, MULLW | D(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2)));
1052 return push_inst(compiler, (GET_OPCODE(op) == SLJIT_UMUL ? MULHWU : MULHW) | D(SLJIT_TEMPORARY_REG2) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2));
1053 #endif
1054 case SLJIT_UDIV:
1055 case SLJIT_SDIV:
1056 FAIL_IF(push_inst(compiler, OR | S(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG1)));
1057 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1058 if (op & SLJIT_INT_OP) {
1059 FAIL_IF(push_inst(compiler, (GET_OPCODE(op) == SLJIT_UDIV ? DIVWU : DIVW) | D(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2)));
1060 FAIL_IF(push_inst(compiler, MULLW | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG1) | B(SLJIT_TEMPORARY_REG2)));
1061 return push_inst(compiler, SUBF | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG2) | B(TMP_REG1));
1062 }
1063 FAIL_IF(push_inst(compiler, (GET_OPCODE(op) == SLJIT_UDIV ? DIVDU : DIVD) | D(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2)));
1064 FAIL_IF(push_inst(compiler, MULLD | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG1) | B(SLJIT_TEMPORARY_REG2)));
1065 return push_inst(compiler, SUBF | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG2) | B(TMP_REG1));
1066 #else
1067 FAIL_IF(push_inst(compiler, (GET_OPCODE(op) == SLJIT_UDIV ? DIVWU : DIVW) | D(SLJIT_TEMPORARY_REG1) | A(TMP_REG1) | B(SLJIT_TEMPORARY_REG2)));
1068 FAIL_IF(push_inst(compiler, MULLW | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG1) | B(SLJIT_TEMPORARY_REG2)));
1069 return push_inst(compiler, SUBF | D(SLJIT_TEMPORARY_REG2) | A(SLJIT_TEMPORARY_REG2) | B(TMP_REG1));
1070 #endif
1071 }
1072
1073 return SLJIT_SUCCESS;
1074 }
1075
1076 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op1(struct sljit_compiler *compiler, int op,
1077 int dst, sljit_w dstw,
1078 int src, sljit_w srcw)
1079 {
1080 int inp_flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
1081
1082 CHECK_ERROR();
1083 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1084
1085 if ((src & SLJIT_IMM) && srcw == 0)
1086 src = ZERO_REG;
1087
1088 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1089 if (op & SLJIT_INT_OP) {
1090 inp_flags |= INT_DATA | SIGNED_DATA;
1091 if (src & SLJIT_IMM)
1092 srcw = (int)srcw;
1093 }
1094 #endif
1095 if (op & SLJIT_SET_O)
1096 FAIL_IF(push_inst(compiler, MTXER | S(ZERO_REG)));
1097
1098 switch (GET_OPCODE(op)) {
1099 case SLJIT_MOV:
1100 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1101
1102 case SLJIT_MOV_UI:
1103 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1104
1105 case SLJIT_MOV_SI:
1106 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
1107
1108 case SLJIT_MOV_UB:
1109 return emit_op(compiler, SLJIT_MOV_UB, inp_flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned char)srcw : srcw);
1110
1111 case SLJIT_MOV_SB:
1112 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);
1113
1114 case SLJIT_MOV_UH:
1115 return emit_op(compiler, SLJIT_MOV_UH, inp_flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned short)srcw : srcw);
1116
1117 case SLJIT_MOV_SH:
1118 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);
1119
1120 case SLJIT_MOVU:
1121 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1122
1123 case SLJIT_MOVU_UI:
1124 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1125
1126 case SLJIT_MOVU_SI:
1127 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
1128
1129 case SLJIT_MOVU_UB:
1130 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);
1131
1132 case SLJIT_MOVU_SB:
1133 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);
1134
1135 case SLJIT_MOVU_UH:
1136 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);
1137
1138 case SLJIT_MOVU_SH:
1139 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);
1140
1141 case SLJIT_NOT:
1142 return emit_op(compiler, SLJIT_NOT, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1143
1144 case SLJIT_NEG:
1145 return emit_op(compiler, SLJIT_NEG, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1146
1147 case SLJIT_CLZ:
1148 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1149 return emit_op(compiler, SLJIT_CLZ, inp_flags | (!(op & SLJIT_INT_OP) ? 0 : ALT_FORM1), dst, dstw, TMP_REG1, 0, src, srcw);
1150 #else
1151 return emit_op(compiler, SLJIT_CLZ, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
1152 #endif
1153 }
1154
1155 return SLJIT_SUCCESS;
1156 }
1157
1158 #define TEST_SL_IMM(src, srcw) \
1159 (((src) & SLJIT_IMM) && (srcw) <= SIMM_MAX && (srcw) >= SIMM_MIN)
1160
1161 #define TEST_UL_IMM(src, srcw) \
1162 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff))
1163
1164 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1165 #define TEST_SH_IMM(src, srcw) \
1166 (((src) & SLJIT_IMM) && !((srcw) & 0xffff) && (srcw) <= SLJIT_W(0x7fffffff) && (srcw) >= SLJIT_W(-0x80000000))
1167 #else
1168 #define TEST_SH_IMM(src, srcw) \
1169 (((src) & SLJIT_IMM) && !((srcw) & 0xffff))
1170 #endif
1171
1172 #define TEST_UH_IMM(src, srcw) \
1173 (((src) & SLJIT_IMM) && !((srcw) & ~0xffff0000))
1174
1175 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1176 #define TEST_ADD_IMM(src, srcw) \
1177 (((src) & SLJIT_IMM) && (srcw) <= SLJIT_W(0x7fff7fff) && (srcw) >= SLJIT_W(-0x80000000))
1178 #else
1179 #define TEST_ADD_IMM(src, srcw) \
1180 ((src) & SLJIT_IMM)
1181 #endif
1182
1183 #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
1184 #define TEST_UI_IMM(src, srcw) \
1185 (((src) & SLJIT_IMM) && !((srcw) & ~0xffffffff))
1186 #else
1187 #define TEST_UI_IMM(src, srcw) \
1188 ((src) & SLJIT_IMM)
1189 #endif
1190
1191 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_op2(struct sljit_compiler *compiler, int op,
1192 int dst, sljit_w dstw,
1193 int src1, sljit_w src1w,
1194 int src2, sljit_w src2w)
1195 {
1196 int inp_flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
1197
1198 CHECK_ERROR();
1199 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, 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, int args, int temporaries, int saveds, int local_size)
1558 {
1559 CHECK_ERROR();
1560 check_sljit_emit_fast_enter(compiler, dst, dstw, args, temporaries, saveds, local_size);
1561
1562 compiler->temporaries = temporaries;
1563 compiler->saveds = saveds;
1564
1565 compiler->has_locals = local_size > 0;
1566 #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
1567 compiler->local_size = (2 + saveds + 2) * sizeof(sljit_w) + local_size;
1568 #else
1569 compiler->local_size = (2 + saveds + 7 + 8) * sizeof(sljit_w) + local_size;
1570 #endif
1571 compiler->local_size = (compiler->local_size + 15) & ~0xf;
1572
1573 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1574 return push_inst(compiler, MFLR | D(dst));
1575 else if (dst & SLJIT_MEM) {
1576 FAIL_IF(push_inst(compiler, MFLR | D(TMP_REG2)));
1577 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1578 }
1579
1580 return SLJIT_SUCCESS;
1581 }
1582
1583 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1584 {
1585 CHECK_ERROR();
1586 check_sljit_emit_fast_return(compiler, src, srcw);
1587
1588 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1589 FAIL_IF(push_inst(compiler, MTLR | S(src)));
1590 else {
1591 if (src & SLJIT_MEM)
1592 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1593 else if (src & SLJIT_IMM)
1594 FAIL_IF(load_immediate(compiler, TMP_REG2, srcw));
1595 FAIL_IF(push_inst(compiler, MTLR | S(TMP_REG2)));
1596 }
1597 return push_inst(compiler, BLR);
1598 }
1599
1600 /* --------------------------------------------------------------------- */
1601 /* Conditional instructions */
1602 /* --------------------------------------------------------------------- */
1603
1604 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1605 {
1606 struct sljit_label *label;
1607
1608 CHECK_ERROR_PTR();
1609 check_sljit_emit_label(compiler);
1610
1611 if (compiler->last_label && compiler->last_label->size == compiler->size)
1612 return compiler->last_label;
1613
1614 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1615 PTR_FAIL_IF(!label);
1616 set_label(label, compiler);
1617 return label;
1618 }
1619
1620 static sljit_ins get_bo_bi_flags(struct sljit_compiler *compiler, int type)
1621 {
1622 switch (type) {
1623 case SLJIT_C_EQUAL:
1624 return (12 << 21) | (2 << 16);
1625
1626 case SLJIT_C_NOT_EQUAL:
1627 return (4 << 21) | (2 << 16);
1628
1629 case SLJIT_C_LESS:
1630 case SLJIT_C_FLOAT_LESS:
1631 return (12 << 21) | ((4 + 0) << 16);
1632
1633 case SLJIT_C_GREATER_EQUAL:
1634 case SLJIT_C_FLOAT_GREATER_EQUAL:
1635 return (4 << 21) | ((4 + 0) << 16);
1636
1637 case SLJIT_C_GREATER:
1638 case SLJIT_C_FLOAT_GREATER:
1639 return (12 << 21) | ((4 + 1) << 16);
1640
1641 case SLJIT_C_LESS_EQUAL:
1642 case SLJIT_C_FLOAT_LESS_EQUAL:
1643 return (4 << 21) | ((4 + 1) << 16);
1644
1645 case SLJIT_C_SIG_LESS:
1646 return (12 << 21) | (0 << 16);
1647
1648 case SLJIT_C_SIG_GREATER_EQUAL:
1649 return (4 << 21) | (0 << 16);
1650
1651 case SLJIT_C_SIG_GREATER:
1652 return (12 << 21) | (1 << 16);
1653
1654 case SLJIT_C_SIG_LESS_EQUAL:
1655 return (4 << 21) | (1 << 16);
1656
1657 case SLJIT_C_OVERFLOW:
1658 case SLJIT_C_MUL_OVERFLOW:
1659 return (12 << 21) | (3 << 16);
1660
1661 case SLJIT_C_NOT_OVERFLOW:
1662 case SLJIT_C_MUL_NOT_OVERFLOW:
1663 return (4 << 21) | (3 << 16);
1664
1665 case SLJIT_C_FLOAT_EQUAL:
1666 return (12 << 21) | ((4 + 2) << 16);
1667
1668 case SLJIT_C_FLOAT_NOT_EQUAL:
1669 return (4 << 21) | ((4 + 2) << 16);
1670
1671 case SLJIT_C_FLOAT_NAN:
1672 return (12 << 21) | ((4 + 3) << 16);
1673
1674 case SLJIT_C_FLOAT_NOT_NAN:
1675 return (4 << 21) | ((4 + 3) << 16);
1676
1677 default:
1678 SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
1679 return (20 << 21);
1680 }
1681 }
1682
1683 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type)
1684 {
1685 struct sljit_jump *jump;
1686 sljit_ins bo_bi_flags;
1687
1688 CHECK_ERROR_PTR();
1689 check_sljit_emit_jump(compiler, type);
1690
1691 bo_bi_flags = get_bo_bi_flags(compiler, type & 0xff);
1692 if (!bo_bi_flags)
1693 return NULL;
1694
1695 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1696 PTR_FAIL_IF(!jump);
1697 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1698 type &= 0xff;
1699
1700 /* In PPC, we don't need to touch the arguments. */
1701 if (type >= SLJIT_JUMP)
1702 jump->flags |= UNCOND_B;
1703
1704 PTR_FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1705 PTR_FAIL_IF(push_inst(compiler, MTCTR | S(TMP_REG1)));
1706 jump->addr = compiler->size;
1707 PTR_FAIL_IF(push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0)));
1708 return jump;
1709 }
1710
1711 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1712 {
1713 sljit_ins bo_bi_flags;
1714 struct sljit_jump *jump = NULL;
1715 int src_r;
1716
1717 CHECK_ERROR();
1718 check_sljit_emit_ijump(compiler, type, src, srcw);
1719
1720 bo_bi_flags = get_bo_bi_flags(compiler, type);
1721 FAIL_IF(!bo_bi_flags);
1722
1723 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1724 src_r = src;
1725 else if (src & SLJIT_IMM) {
1726 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1727 FAIL_IF(!jump);
1728 set_jump(jump, compiler, JUMP_ADDR | UNCOND_B);
1729 jump->u.target = srcw;
1730
1731 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1732 src_r = TMP_REG2;
1733 }
1734 else {
1735 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1736 src_r = TMP_REG2;
1737 }
1738
1739 FAIL_IF(push_inst(compiler, MTCTR | S(src_r)));
1740 if (jump)
1741 jump->addr = compiler->size;
1742 return push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0));
1743 }
1744
1745 /* Get a bit from CR, all other bits are zeroed. */
1746 #define GET_CR_BIT(bit, dst) \
1747 FAIL_IF(push_inst(compiler, MFCR | D(dst))); \
1748 FAIL_IF(push_inst(compiler, RLWINM | S(dst) | A(dst) | ((1 + (bit)) << 11) | (31 << 6) | (31 << 1)));
1749
1750 #define INVERT_BIT(dst) \
1751 FAIL_IF(push_inst(compiler, XORI | S(dst) | A(dst) | 0x1));
1752
1753 SLJIT_API_FUNC_ATTRIBUTE int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1754 {
1755 int reg;
1756
1757 CHECK_ERROR();
1758 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1759
1760 if (dst == SLJIT_UNUSED)
1761 return SLJIT_SUCCESS;
1762
1763 reg = (op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1764
1765 switch (type) {
1766 case SLJIT_C_EQUAL:
1767 GET_CR_BIT(2, reg);
1768 break;
1769
1770 case SLJIT_C_NOT_EQUAL:
1771 GET_CR_BIT(2, reg);
1772 INVERT_BIT(reg);
1773 break;
1774
1775 case SLJIT_C_LESS:
1776 case SLJIT_C_FLOAT_LESS:
1777 GET_CR_BIT(4 + 0, reg);
1778 break;
1779
1780 case SLJIT_C_GREATER_EQUAL:
1781 case SLJIT_C_FLOAT_GREATER_EQUAL:
1782 GET_CR_BIT(4 + 0, reg);
1783 INVERT_BIT(reg);
1784 break;
1785
1786 case SLJIT_C_GREATER:
1787 case SLJIT_C_FLOAT_GREATER:
1788 GET_CR_BIT(4 + 1, reg);
1789 break;
1790
1791 case SLJIT_C_LESS_EQUAL:
1792 case SLJIT_C_FLOAT_LESS_EQUAL:
1793 GET_CR_BIT(4 + 1, reg);
1794 INVERT_BIT(reg);
1795 break;
1796
1797 case SLJIT_C_SIG_LESS:
1798 GET_CR_BIT(0, reg);
1799 break;
1800
1801 case SLJIT_C_SIG_GREATER_EQUAL:
1802 GET_CR_BIT(0, reg);
1803 INVERT_BIT(reg);
1804 break;
1805
1806 case SLJIT_C_SIG_GREATER:
1807 GET_CR_BIT(1, reg);
1808 break;
1809
1810 case SLJIT_C_SIG_LESS_EQUAL:
1811 GET_CR_BIT(1, reg);
1812 INVERT_BIT(reg);
1813 break;
1814
1815 case SLJIT_C_OVERFLOW:
1816 case SLJIT_C_MUL_OVERFLOW:
1817 GET_CR_BIT(3, reg);
1818 break;
1819
1820 case SLJIT_C_NOT_OVERFLOW:
1821 case SLJIT_C_MUL_NOT_OVERFLOW:
1822 GET_CR_BIT(3, reg);
1823 INVERT_BIT(reg);
1824 break;
1825
1826 case SLJIT_C_FLOAT_EQUAL:
1827 GET_CR_BIT(4 + 2, reg);
1828 break;
1829
1830 case SLJIT_C_FLOAT_NOT_EQUAL:
1831 GET_CR_BIT(4 + 2, reg);
1832 INVERT_BIT(reg);
1833 break;
1834
1835 case SLJIT_C_FLOAT_NAN:
1836 GET_CR_BIT(4 + 3, reg);
1837 break;
1838
1839 case SLJIT_C_FLOAT_NOT_NAN:
1840 GET_CR_BIT(4 + 3, reg);
1841 INVERT_BIT(reg);
1842 break;
1843
1844 default:
1845 SLJIT_ASSERT_STOP();
1846 break;
1847 }
1848
1849 if (GET_OPCODE(op) == SLJIT_OR)
1850 return emit_op(compiler, GET_OPCODE(op), GET_FLAGS(op) ? ALT_SET_FLAGS : 0, dst, dstw, dst, dstw, TMP_REG2, 0);
1851
1852 if (reg == TMP_REG2)
1853 return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1854 return SLJIT_SUCCESS;
1855 }
1856
1857 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value)
1858 {
1859 struct sljit_const *const_;
1860 int reg;
1861
1862 CHECK_ERROR_PTR();
1863 check_sljit_emit_const(compiler, dst, dstw, init_value);
1864
1865 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1866 PTR_FAIL_IF(!const_);
1867 set_const(const_, compiler);
1868
1869 reg = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1870
1871 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1872
1873 if (dst & SLJIT_MEM)
1874 PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
1875 return const_;
1876 }

  ViewVC Help
Powered by ViewVC 1.1.5