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