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

  ViewVC Help
Powered by ViewVC 1.1.5