/[pcre]/code/trunk/sljit/sljitNativeSPARC_common.c
ViewVC logotype

Contents of /code/trunk/sljit/sljitNativeSPARC_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1195 - (show annotations)
Thu Nov 1 15:21:27 2012 UTC (6 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 42719 byte(s)
Another huge 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(void)
28 {
29 return "SPARC" SLJIT_CPUINFO;
30 }
31
32 /* Length of an instruction word
33 Both for sparc-32 and sparc-64 */
34 typedef sljit_ui sljit_ins;
35
36 static void sparc_cache_flush(sljit_ins *from, sljit_ins *to)
37 {
38 if (SLJIT_UNLIKELY(from == to))
39 return;
40
41 do {
42 __asm__ volatile (
43 "flush %0\n"
44 : : "r"(from)
45 );
46 /* Operates at least on doubleword. */
47 from += 2;
48 } while (from < to);
49
50 if (from == to) {
51 /* Flush the last word. */
52 to --;
53 __asm__ volatile (
54 "flush %0\n"
55 : : "r"(to)
56 );
57 }
58 }
59
60 /* TMP_REG2 is not used by getput_arg */
61 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
62 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
63 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
64 #define TMP_REG4 (SLJIT_NO_REGISTERS + 4)
65 #define LINK_REG (SLJIT_NO_REGISTERS + 5)
66
67 #define TMP_FREG1 (0)
68 #define TMP_FREG2 ((SLJIT_FLOAT_REG6 + 1) << 1)
69
70 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 7] = {
71 0, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 14, 1, 24, 25, 26, 15
72 };
73
74 /* --------------------------------------------------------------------- */
75 /* Instrucion forms */
76 /* --------------------------------------------------------------------- */
77
78 #define D(d) (reg_map[d] << 25)
79 #define DA(d) ((d) << 25)
80 #define S1(s1) (reg_map[s1] << 14)
81 #define S2(s2) (reg_map[s2])
82 #define S1A(s1) ((s1) << 14)
83 #define S2A(s2) (s2)
84 #define IMM_ARG 0x2000
85 #define DOP(op) ((op) << 5)
86 #define IMM(imm) (((imm) & 0x1fff) | IMM_ARG)
87
88 #define DR(dr) (reg_map[dr])
89 #define OPC1(opcode) ((opcode) << 30)
90 #define OPC2(opcode) ((opcode) << 22)
91 #define OPC3(opcode) ((opcode) << 19)
92 #define SET_FLAGS OPC3(0x10)
93
94 #define ADD (OPC1(0x2) | OPC3(0x00))
95 #define ADDC (OPC1(0x2) | OPC3(0x08))
96 #define AND (OPC1(0x2) | OPC3(0x01))
97 #define ANDN (OPC1(0x2) | OPC3(0x05))
98 #define CALL (OPC1(0x1))
99 #define FABSS (OPC1(0x2) | OPC3(0x34) | DOP(0x09))
100 #define FADDD (OPC1(0x2) | OPC3(0x34) | DOP(0x42))
101 #define FADDS (OPC1(0x2) | OPC3(0x34) | DOP(0x41))
102 #define FCMPD (OPC1(0x2) | OPC3(0x35) | DOP(0x52))
103 #define FCMPS (OPC1(0x2) | OPC3(0x35) | DOP(0x51))
104 #define FDIVD (OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
105 #define FDIVS (OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
106 #define FMOVS (OPC1(0x2) | OPC3(0x34) | DOP(0x01))
107 #define FMULD (OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
108 #define FMULS (OPC1(0x2) | OPC3(0x34) | DOP(0x49))
109 #define FNEGS (OPC1(0x2) | OPC3(0x34) | DOP(0x05))
110 #define FSUBD (OPC1(0x2) | OPC3(0x34) | DOP(0x46))
111 #define FSUBS (OPC1(0x2) | OPC3(0x34) | DOP(0x45))
112 #define JMPL (OPC1(0x2) | OPC3(0x38))
113 #define NOP (OPC1(0x0) | OPC2(0x04))
114 #define OR (OPC1(0x2) | OPC3(0x02))
115 #define ORN (OPC1(0x2) | OPC3(0x06))
116 #define RDY (OPC1(0x2) | OPC3(0x28) | S1A(0))
117 #define RESTORE (OPC1(0x2) | OPC3(0x3d))
118 #define SAVE (OPC1(0x2) | OPC3(0x3c))
119 #define SETHI (OPC1(0x0) | OPC2(0x04))
120 #define SLL (OPC1(0x2) | OPC3(0x25))
121 #define SLLX (OPC1(0x2) | OPC3(0x25) | (1 << 12))
122 #define SRA (OPC1(0x2) | OPC3(0x27))
123 #define SRAX (OPC1(0x2) | OPC3(0x27) | (1 << 12))
124 #define SRL (OPC1(0x2) | OPC3(0x26))
125 #define SRLX (OPC1(0x2) | OPC3(0x26) | (1 << 12))
126 #define SUB (OPC1(0x2) | OPC3(0x04))
127 #define SUBC (OPC1(0x2) | OPC3(0x0c))
128 #define TA (OPC1(0x2) | OPC3(0x3a) | (8 << 25))
129 #define WRY (OPC1(0x2) | OPC3(0x30) | DA(0))
130 #define XOR (OPC1(0x2) | OPC3(0x03))
131 #define XNOR (OPC1(0x2) | OPC3(0x07))
132
133 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
134 #define MAX_DISP (0x1fffff)
135 #define MIN_DISP (-0x200000)
136 #define DISP_MASK (0x3fffff)
137
138 #define BICC (OPC1(0x0) | OPC2(0x2))
139 #define FBFCC (OPC1(0x0) | OPC2(0x6))
140 #define SLL_W SLL
141 #define SDIV (OPC1(0x2) | OPC3(0x0f))
142 #define SMUL (OPC1(0x2) | OPC3(0x0b))
143 #define UDIV (OPC1(0x2) | OPC3(0x0e))
144 #define UMUL (OPC1(0x2) | OPC3(0x0a))
145 #else
146 #define SLL_W SLLX
147 #endif
148
149 #define SIMM_MAX (0x0fff)
150 #define SIMM_MIN (-0x1000)
151
152 /* dest_reg is the absolute name of the register
153 Useful for reordering instructions in the delay slot. */
154 static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_si delay_slot)
155 {
156 SLJIT_ASSERT((delay_slot & DST_INS_MASK) == UNMOVABLE_INS
157 || (delay_slot & DST_INS_MASK) == MOVABLE_INS
158 || (delay_slot & DST_INS_MASK) == ((ins >> 25) & 0x1f));
159 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
160 FAIL_IF(!ptr);
161 *ptr = ins;
162 compiler->size++;
163 compiler->delay_slot = delay_slot;
164 return SLJIT_SUCCESS;
165 }
166
167 static SLJIT_INLINE sljit_ins* optimize_jump(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
168 {
169 sljit_sw diff;
170 sljit_uw target_addr;
171 sljit_ins *inst;
172 sljit_ins saved_inst;
173
174 if (jump->flags & SLJIT_REWRITABLE_JUMP)
175 return code_ptr;
176
177 if (jump->flags & JUMP_ADDR)
178 target_addr = jump->u.target;
179 else {
180 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
181 target_addr = (sljit_uw)(code + jump->u.label->size);
182 }
183 inst = (sljit_ins*)jump->addr;
184
185 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
186 if (jump->flags & IS_CALL) {
187 /* Call is always patchable on sparc 32. */
188 jump->flags |= PATCH_CALL;
189 if (jump->flags & IS_MOVABLE) {
190 inst[0] = inst[-1];
191 inst[-1] = CALL;
192 jump->addr -= sizeof(sljit_ins);
193 return inst;
194 }
195 inst[0] = CALL;
196 inst[1] = NOP;
197 return inst + 1;
198 }
199 #else
200 /* Both calls and BPr instructions shall not pass this point. */
201 #error "Implementation required"
202 #endif
203
204 if (jump->flags & IS_COND)
205 inst--;
206
207 if (jump->flags & IS_MOVABLE) {
208 diff = ((sljit_sw)target_addr - (sljit_sw)(inst - 1)) >> 2;
209 if (diff <= MAX_DISP && diff >= MIN_DISP) {
210 jump->flags |= PATCH_B;
211 inst--;
212 if (jump->flags & IS_COND) {
213 saved_inst = inst[0];
214 inst[0] = inst[1] ^ (1 << 28);
215 inst[1] = saved_inst;
216 } else {
217 inst[1] = inst[0];
218 inst[0] = BICC | DA(0x8);
219 }
220 jump->addr = (sljit_uw)inst;
221 return inst + 1;
222 }
223 }
224
225 diff = ((sljit_sw)target_addr - (sljit_sw)(inst)) >> 2;
226 if (diff <= MAX_DISP && diff >= MIN_DISP) {
227 jump->flags |= PATCH_B;
228 if (jump->flags & IS_COND)
229 inst[0] ^= (1 << 28);
230 else
231 inst[0] = BICC | DA(0x8);
232 inst[1] = NOP;
233 jump->addr = (sljit_uw)inst;
234 return inst + 1;
235 }
236
237 return code_ptr;
238 }
239
240 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
241 {
242 struct sljit_memory_fragment *buf;
243 sljit_ins *code;
244 sljit_ins *code_ptr;
245 sljit_ins *buf_ptr;
246 sljit_ins *buf_end;
247 sljit_uw word_count;
248 sljit_uw addr;
249
250 struct sljit_label *label;
251 struct sljit_jump *jump;
252 struct sljit_const *const_;
253
254 CHECK_ERROR_PTR();
255 check_sljit_generate_code(compiler);
256 reverse_buf(compiler);
257
258 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
259 PTR_FAIL_WITH_EXEC_IF(code);
260 buf = compiler->buf;
261
262 code_ptr = code;
263 word_count = 0;
264 label = compiler->labels;
265 jump = compiler->jumps;
266 const_ = compiler->consts;
267 do {
268 buf_ptr = (sljit_ins*)buf->memory;
269 buf_end = buf_ptr + (buf->used_size >> 2);
270 do {
271 *code_ptr = *buf_ptr++;
272 SLJIT_ASSERT(!label || label->size >= word_count);
273 SLJIT_ASSERT(!jump || jump->addr >= word_count);
274 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
275 /* These structures are ordered by their address. */
276 if (label && label->size == word_count) {
277 /* Just recording the address. */
278 label->addr = (sljit_uw)code_ptr;
279 label->size = code_ptr - code;
280 label = label->next;
281 }
282 if (jump && jump->addr == word_count) {
283 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
284 jump->addr = (sljit_uw)(code_ptr - 3);
285 #else
286 jump->addr = (sljit_uw)(code_ptr - 6);
287 #endif
288 code_ptr = optimize_jump(jump, code_ptr, code);
289 jump = jump->next;
290 }
291 if (const_ && const_->addr == word_count) {
292 /* Just recording the address. */
293 const_->addr = (sljit_uw)code_ptr;
294 const_ = const_->next;
295 }
296 code_ptr ++;
297 word_count ++;
298 } while (buf_ptr < buf_end);
299
300 buf = buf->next;
301 } while (buf);
302
303 if (label && label->size == word_count) {
304 label->addr = (sljit_uw)code_ptr;
305 label->size = code_ptr - code;
306 label = label->next;
307 }
308
309 SLJIT_ASSERT(!label);
310 SLJIT_ASSERT(!jump);
311 SLJIT_ASSERT(!const_);
312 SLJIT_ASSERT(code_ptr - code <= (sljit_si)compiler->size);
313
314 jump = compiler->jumps;
315 while (jump) {
316 do {
317 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
318 buf_ptr = (sljit_ins*)jump->addr;
319
320 if (jump->flags & PATCH_CALL) {
321 addr = (sljit_sw)(addr - jump->addr) >> 2;
322 SLJIT_ASSERT((sljit_sw)addr <= 0x1fffffff && (sljit_sw)addr >= -0x20000000);
323 buf_ptr[0] = CALL | (addr & 0x3fffffff);
324 break;
325 }
326 if (jump->flags & PATCH_B) {
327 addr = (sljit_sw)(addr - jump->addr) >> 2;
328 SLJIT_ASSERT((sljit_sw)addr <= MAX_DISP && (sljit_sw)addr >= MIN_DISP);
329 buf_ptr[0] = (buf_ptr[0] & ~DISP_MASK) | (addr & DISP_MASK);
330 break;
331 }
332
333 /* Set the fields of immediate loads. */
334 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
335 buf_ptr[0] = (buf_ptr[0] & 0xffc00000) | ((addr >> 10) & 0x3fffff);
336 buf_ptr[1] = (buf_ptr[1] & 0xfffffc00) | (addr & 0x3ff);
337 #else
338 #error "Implementation required"
339 #endif
340 } while (0);
341 jump = jump->next;
342 }
343
344
345 compiler->error = SLJIT_ERR_COMPILED;
346 compiler->executable_size = compiler->size * sizeof(sljit_ins);
347 SLJIT_CACHE_FLUSH(code, code_ptr);
348 return code;
349 }
350
351 /* --------------------------------------------------------------------- */
352 /* Entry, exit */
353 /* --------------------------------------------------------------------- */
354
355 /* Creates an index in data_transfer_insts array. */
356 #define LOAD_DATA 0x01
357 #define WORD_DATA 0x00
358 #define BYTE_DATA 0x02
359 #define HALF_DATA 0x04
360 #define INT_DATA 0x06
361 #define SIGNED_DATA 0x08
362 /* Separates integer and floating point registers */
363 #define GPR_REG 0x0f
364 #define DOUBLE_DATA 0x10
365
366 #define MEM_MASK 0x1f
367
368 #define WRITE_BACK 0x00020
369 #define ARG_TEST 0x00040
370 #define CUMULATIVE_OP 0x00080
371 #define IMM_OP 0x00100
372 #define SRC2_IMM 0x00200
373
374 #define REG_DEST 0x00400
375 #define REG2_SOURCE 0x00800
376 #define SLOW_SRC1 0x01000
377 #define SLOW_SRC2 0x02000
378 #define SLOW_DEST 0x04000
379 /* SET_FLAGS (0x10 << 19) also belong here! */
380
381 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
382 #include "sljitNativeSPARC_32.c"
383 #else
384 #include "sljitNativeSPARC_64.c"
385 #endif
386
387 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si temporaries, sljit_si saveds, sljit_si local_size)
388 {
389 CHECK_ERROR();
390 check_sljit_emit_enter(compiler, args, temporaries, saveds, local_size);
391
392 compiler->temporaries = temporaries;
393 compiler->saveds = saveds;
394 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
395 compiler->logical_local_size = local_size;
396 #endif
397
398 local_size += 23 * sizeof(sljit_sw);
399 local_size = (local_size + 7) & ~0x7;
400 compiler->local_size = local_size;
401
402 if (local_size <= SIMM_MAX) {
403 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | IMM(-local_size), UNMOVABLE_INS));
404 }
405 else {
406 FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
407 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | S2(TMP_REG1), UNMOVABLE_INS));
408 }
409
410 if (args >= 1)
411 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG1) | S1(0) | S2A(24), DR(SLJIT_SAVED_REG1)));
412 if (args >= 2)
413 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG2) | S1(0) | S2A(25), DR(SLJIT_SAVED_REG2)));
414 if (args >= 3)
415 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG3) | S1(0) | S2A(26), DR(SLJIT_SAVED_REG3)));
416
417 return SLJIT_SUCCESS;
418 }
419
420 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si temporaries, sljit_si saveds, sljit_si local_size)
421 {
422 CHECK_ERROR_VOID();
423 check_sljit_set_context(compiler, args, temporaries, saveds, local_size);
424
425 compiler->temporaries = temporaries;
426 compiler->saveds = saveds;
427 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
428 compiler->logical_local_size = local_size;
429 #endif
430
431 local_size += 23 * sizeof(sljit_sw);
432 compiler->local_size = (local_size + 7) & ~0x7;
433 }
434
435 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
436 {
437 CHECK_ERROR();
438 check_sljit_emit_return(compiler, op, src, srcw);
439
440 if (op != SLJIT_MOV || !(src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)) {
441 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
442 src = SLJIT_TEMPORARY_REG1;
443 }
444
445 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
446 return push_inst(compiler, RESTORE | D(SLJIT_TEMPORARY_REG1) | S1(src) | S2(0), UNMOVABLE_INS);
447 }
448
449 /* --------------------------------------------------------------------- */
450 /* Operators */
451 /* --------------------------------------------------------------------- */
452
453 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
454 #define ARCH_32_64(a, b) a
455 #else
456 #define ARCH_32_64(a, b) b
457 #endif
458
459 static SLJIT_CONST sljit_ins data_transfer_insts[16 + 4] = {
460 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
461 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
462 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
463 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
464 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
465 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
466 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
467 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
468
469 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
470 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
471 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
472 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
473 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
474 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
475 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
476 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
477
478 /* d s */ OPC1(3) | OPC3(0x27),
479 /* d l */ OPC1(3) | OPC3(0x23),
480 /* s s */ OPC1(3) | OPC3(0x24),
481 /* s l */ OPC1(3) | OPC3(0x20),
482 };
483
484 #undef ARCH_32_64
485
486 /* Can perform an operation using at most 1 instruction. */
487 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
488 {
489 SLJIT_ASSERT(arg & SLJIT_MEM);
490
491 if (!(flags & WRITE_BACK)) {
492 if ((!(arg & 0xf0) && argw <= SIMM_MAX && argw >= SIMM_MIN)
493 || ((arg & 0xf0) && (argw & 0x3) == 0)) {
494 /* Works for both absoulte and relative addresses (immediate case). */
495 if (SLJIT_UNLIKELY(flags & ARG_TEST))
496 return 1;
497 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
498 | ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg))
499 | S1(arg & 0xf) | ((arg & 0xf0) ? S2((arg >> 4) & 0xf) : IMM(argw)),
500 ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
501 return -1;
502 }
503 }
504 return 0;
505 }
506
507 /* See getput_arg below.
508 Note: can_cache is called only for binary operators. Those
509 operators always uses word arguments without write back. */
510 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
511 {
512 SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
513
514 /* Simple operation except for updates. */
515 if (arg & 0xf0) {
516 argw &= 0x3;
517 SLJIT_ASSERT(argw);
518 next_argw &= 0x3;
519 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == next_argw)
520 return 1;
521 return 0;
522 }
523
524 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
525 return 1;
526 return 0;
527 }
528
529 /* Emit the necessary instructions. See can_cache above. */
530 static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
531 {
532 sljit_si base, arg2, delay_slot;
533 sljit_ins dest;
534
535 SLJIT_ASSERT(arg & SLJIT_MEM);
536 if (!(next_arg & SLJIT_MEM)) {
537 next_arg = 0;
538 next_argw = 0;
539 }
540
541 base = arg & 0xf;
542 if (SLJIT_UNLIKELY(arg & 0xf0)) {
543 argw &= 0x3;
544 SLJIT_ASSERT(argw != 0);
545
546 /* Using the cache. */
547 if (((SLJIT_MEM | (arg & 0xf0)) == compiler->cache_arg) && (argw == compiler->cache_argw))
548 arg2 = TMP_REG3;
549 else {
550 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == (next_argw & 0x3)) {
551 compiler->cache_arg = SLJIT_MEM | (arg & 0xf0);
552 compiler->cache_argw = argw;
553 arg2 = TMP_REG3;
554 }
555 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && (reg << 4) != (arg & 0xf0))
556 arg2 = reg;
557 else /* It must be a mov operation, so tmp1 must be free to use. */
558 arg2 = TMP_REG1;
559 FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1((arg >> 4) & 0xf) | IMM_ARG | argw, DR(arg2)));
560 }
561 }
562 else {
563 /* Using the cache. */
564 if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
565 if (argw != compiler->cache_argw) {
566 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
567 compiler->cache_argw = argw;
568 }
569 arg2 = TMP_REG3;
570 } else {
571 if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
572 compiler->cache_arg = SLJIT_MEM;
573 compiler->cache_argw = argw;
574 arg2 = TMP_REG3;
575 }
576 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
577 arg2 = reg;
578 else /* It must be a mov operation, so tmp1 must be free to use. */
579 arg2 = TMP_REG1;
580 FAIL_IF(load_immediate(compiler, arg2, argw));
581 }
582 }
583
584 dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg));
585 delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
586 if (!base)
587 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
588 if (!(flags & WRITE_BACK))
589 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
590 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot));
591 return push_inst(compiler, ADD | D(base) | S1(base) | S2(arg2), DR(base));
592 }
593
594 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
595 {
596 if (getput_arg_fast(compiler, flags, reg, arg, argw))
597 return compiler->error;
598 compiler->cache_arg = 0;
599 compiler->cache_argw = 0;
600 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
601 }
602
603 static SLJIT_INLINE sljit_si emit_op_mem2(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg1, sljit_sw arg1w, sljit_si arg2, sljit_sw arg2w)
604 {
605 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
606 return compiler->error;
607 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
608 }
609
610 static sljit_si emit_op(struct sljit_compiler *compiler, sljit_si op, sljit_si flags,
611 sljit_si dst, sljit_sw dstw,
612 sljit_si src1, sljit_sw src1w,
613 sljit_si src2, sljit_sw src2w)
614 {
615 /* arg1 goes to TMP_REG1 or src reg
616 arg2 goes to TMP_REG2, imm or src reg
617 TMP_REG3 can be used for caching
618 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
619 sljit_si dst_r = TMP_REG2;
620 sljit_si src1_r;
621 sljit_sw src2_r = 0;
622 sljit_si sugg_src2_r = TMP_REG2;
623
624 compiler->cache_arg = 0;
625 compiler->cache_argw = 0;
626
627 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= TMP_REG3) {
628 dst_r = dst;
629 flags |= REG_DEST;
630 if (GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)
631 sugg_src2_r = dst_r;
632 }
633 else if (dst == SLJIT_UNUSED) {
634 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
635 return SLJIT_SUCCESS;
636 }
637 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
638 flags |= SLOW_DEST;
639
640 if (flags & IMM_OP) {
641 if ((src2 & SLJIT_IMM) && src2w) {
642 if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
643 flags |= SRC2_IMM;
644 src2_r = src2w;
645 }
646 }
647 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
648 if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
649 flags |= SRC2_IMM;
650 src2_r = src1w;
651
652 /* And swap arguments. */
653 src1 = src2;
654 src1w = src2w;
655 src2 = SLJIT_IMM;
656 /* src2w = src2_r unneeded. */
657 }
658 }
659 }
660
661 /* Source 1. */
662 if (src1 >= SLJIT_TEMPORARY_REG1 && src1 <= TMP_REG3)
663 src1_r = src1;
664 else if (src1 & SLJIT_IMM) {
665 if (src1w) {
666 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
667 src1_r = TMP_REG1;
668 }
669 else
670 src1_r = 0;
671 }
672 else {
673 if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
674 FAIL_IF(compiler->error);
675 else
676 flags |= SLOW_SRC1;
677 src1_r = TMP_REG1;
678 }
679
680 /* Source 2. */
681 if (src2 >= SLJIT_TEMPORARY_REG1 && src2 <= TMP_REG3) {
682 src2_r = src2;
683 flags |= REG2_SOURCE;
684 if (!(flags & REG_DEST) && GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)
685 dst_r = src2_r;
686 }
687 else if (src2 & SLJIT_IMM) {
688 if (!(flags & SRC2_IMM)) {
689 if (src2w || (GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)) {
690 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
691 src2_r = sugg_src2_r;
692 }
693 else
694 src2_r = 0;
695 }
696 }
697 else {
698 if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
699 FAIL_IF(compiler->error);
700 else
701 flags |= SLOW_SRC2;
702 src2_r = sugg_src2_r;
703 }
704
705 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
706 SLJIT_ASSERT(src2_r == TMP_REG2);
707 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
708 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
709 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
710 }
711 else {
712 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
713 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
714 }
715 }
716 else if (flags & SLOW_SRC1)
717 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
718 else if (flags & SLOW_SRC2)
719 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
720
721 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
722
723 if (dst & SLJIT_MEM) {
724 if (!(flags & SLOW_DEST)) {
725 getput_arg_fast(compiler, flags, dst_r, dst, dstw);
726 return compiler->error;
727 }
728 return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
729 }
730
731 return SLJIT_SUCCESS;
732 }
733
734 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
735 {
736 CHECK_ERROR();
737 check_sljit_emit_op0(compiler, op);
738
739 op = GET_OPCODE(op);
740 switch (op) {
741 case SLJIT_BREAKPOINT:
742 return push_inst(compiler, TA, UNMOVABLE_INS);
743 case SLJIT_NOP:
744 return push_inst(compiler, NOP, UNMOVABLE_INS);
745 case SLJIT_UMUL:
746 case SLJIT_SMUL:
747 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
748 FAIL_IF(push_inst(compiler, (op == SLJIT_UMUL ? UMUL : SMUL) | D(SLJIT_TEMPORARY_REG1) | S1(SLJIT_TEMPORARY_REG1) | S2(SLJIT_TEMPORARY_REG2), DR(SLJIT_TEMPORARY_REG1)));
749 return push_inst(compiler, RDY | D(SLJIT_TEMPORARY_REG2), DR(SLJIT_TEMPORARY_REG2));
750 #else
751 #error "Implementation required"
752 #endif
753 case SLJIT_UDIV:
754 case SLJIT_SDIV:
755 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
756 if (op == SLJIT_UDIV)
757 FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
758 else {
759 FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_TEMPORARY_REG1) | IMM(31), DR(TMP_REG1)));
760 FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
761 }
762 FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_TEMPORARY_REG1), DR(TMP_REG2)));
763 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? UDIV : SDIV) | D(SLJIT_TEMPORARY_REG1) | S1(SLJIT_TEMPORARY_REG1) | S2(SLJIT_TEMPORARY_REG2), DR(SLJIT_TEMPORARY_REG1)));
764 FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_TEMPORARY_REG2) | S1(SLJIT_TEMPORARY_REG1) | S2(SLJIT_TEMPORARY_REG2), DR(SLJIT_TEMPORARY_REG2)));
765 FAIL_IF(push_inst(compiler, SUB | D(SLJIT_TEMPORARY_REG2) | S1(TMP_REG2) | S2(SLJIT_TEMPORARY_REG2), DR(SLJIT_TEMPORARY_REG2)));
766 return SLJIT_SUCCESS;
767 #else
768 #error "Implementation required"
769 #endif
770 }
771
772 return SLJIT_SUCCESS;
773 }
774
775 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
776 sljit_si dst, sljit_sw dstw,
777 sljit_si src, sljit_sw srcw)
778 {
779 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
780
781 CHECK_ERROR();
782 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
783 ADJUST_LOCAL_OFFSET(dst, dstw);
784 ADJUST_LOCAL_OFFSET(src, srcw);
785
786 op = GET_OPCODE(op);
787 switch (op) {
788 case SLJIT_MOV:
789 case SLJIT_MOV_P:
790 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
791
792 case SLJIT_MOV_UI:
793 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
794
795 case SLJIT_MOV_SI:
796 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
797
798 case SLJIT_MOV_UB:
799 return emit_op(compiler, SLJIT_MOV_UB, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
800
801 case SLJIT_MOV_SB:
802 return emit_op(compiler, SLJIT_MOV_SB, flags | BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sb)srcw : srcw);
803
804 case SLJIT_MOV_UH:
805 return emit_op(compiler, SLJIT_MOV_UH, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
806
807 case SLJIT_MOV_SH:
808 return emit_op(compiler, SLJIT_MOV_SH, flags | HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sh)srcw : srcw);
809
810 case SLJIT_MOVU:
811 case SLJIT_MOVU_P:
812 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
813
814 case SLJIT_MOVU_UI:
815 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
816
817 case SLJIT_MOVU_SI:
818 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
819
820 case SLJIT_MOVU_UB:
821 return emit_op(compiler, SLJIT_MOV_UB, flags | BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
822
823 case SLJIT_MOVU_SB:
824 return emit_op(compiler, SLJIT_MOV_SB, flags | BYTE_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sb)srcw : srcw);
825
826 case SLJIT_MOVU_UH:
827 return emit_op(compiler, SLJIT_MOV_UH, flags | HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
828
829 case SLJIT_MOVU_SH:
830 return emit_op(compiler, SLJIT_MOV_SH, flags | HALF_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_sh)srcw : srcw);
831
832 case SLJIT_NOT:
833 case SLJIT_CLZ:
834 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
835
836 case SLJIT_NEG:
837 return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
838 }
839
840 return SLJIT_SUCCESS;
841 }
842
843 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
844 sljit_si dst, sljit_sw dstw,
845 sljit_si src1, sljit_sw src1w,
846 sljit_si src2, sljit_sw src2w)
847 {
848 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
849
850 CHECK_ERROR();
851 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
852 ADJUST_LOCAL_OFFSET(dst, dstw);
853 ADJUST_LOCAL_OFFSET(src1, src1w);
854 ADJUST_LOCAL_OFFSET(src2, src2w);
855
856 op = GET_OPCODE(op);
857 switch (op) {
858 case SLJIT_ADD:
859 case SLJIT_ADDC:
860 case SLJIT_MUL:
861 case SLJIT_AND:
862 case SLJIT_OR:
863 case SLJIT_XOR:
864 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
865
866 case SLJIT_SUB:
867 case SLJIT_SUBC:
868 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
869
870 case SLJIT_SHL:
871 case SLJIT_LSHR:
872 case SLJIT_ASHR:
873 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
874 if (src2 & SLJIT_IMM)
875 src2w &= 0x1f;
876 #else
877 SLJIT_ASSERT_STOP();
878 #endif
879 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
880 }
881
882 return SLJIT_SUCCESS;
883 }
884
885 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
886 {
887 check_sljit_get_register_index(reg);
888 return reg_map[reg];
889 }
890
891 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
892 void *instruction, sljit_si size)
893 {
894 CHECK_ERROR();
895 check_sljit_emit_op_custom(compiler, instruction, size);
896 SLJIT_ASSERT(size == 4);
897
898 return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
899 }
900
901 /* --------------------------------------------------------------------- */
902 /* Floating point operators */
903 /* --------------------------------------------------------------------- */
904
905 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
906 {
907 return 1;
908 }
909
910 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 7))
911 #define SELECT_FOP(op, single, double) ((op & SLJIT_SINGLE_OP) ? single : double)
912
913 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
914 sljit_si dst, sljit_sw dstw,
915 sljit_si src, sljit_sw srcw)
916 {
917 sljit_si dst_fr;
918
919 CHECK_ERROR();
920 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
921 SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
922
923 compiler->cache_arg = 0;
924 compiler->cache_argw = 0;
925
926 if (GET_OPCODE(op) == SLJIT_CMPD) {
927 if (dst > SLJIT_FLOAT_REG6) {
928 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, dst, dstw, src, srcw));
929 dst = TMP_FREG1;
930 }
931 else
932 dst <<= 1;
933
934 if (src > SLJIT_FLOAT_REG6) {
935 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src, srcw, 0, 0));
936 src = TMP_FREG2;
937 }
938 else
939 src <<= 1;
940
941 return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | S1A(dst) | S2A(src), FCC_IS_SET | MOVABLE_INS);
942 }
943
944 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG1 : (dst << 1);
945
946 if (src > SLJIT_FLOAT_REG6) {
947 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_fr, src, srcw, dst, dstw));
948 src = dst_fr;
949 }
950 else
951 src <<= 1;
952
953 switch (GET_OPCODE(op)) {
954 case SLJIT_MOVD:
955 if (src != dst_fr && dst_fr != TMP_FREG1) {
956 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr) | S2A(src), MOVABLE_INS));
957 if (!(op & SLJIT_SINGLE_OP))
958 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
959 }
960 break;
961 case SLJIT_NEGD:
962 FAIL_IF(push_inst(compiler, FNEGS | DA(dst_fr) | S2A(src), MOVABLE_INS));
963 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
964 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
965 break;
966 case SLJIT_ABSD:
967 FAIL_IF(push_inst(compiler, FABSS | DA(dst_fr) | S2A(src), MOVABLE_INS));
968 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
969 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
970 break;
971 }
972
973 if (dst_fr == TMP_FREG1) {
974 if (GET_OPCODE(op) == SLJIT_MOVD)
975 dst_fr = src;
976 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_fr, dst, dstw, 0, 0));
977 }
978
979 return SLJIT_SUCCESS;
980 }
981
982 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
983 sljit_si dst, sljit_sw dstw,
984 sljit_si src1, sljit_sw src1w,
985 sljit_si src2, sljit_sw src2w)
986 {
987 sljit_si dst_fr, flags = 0;
988
989 CHECK_ERROR();
990 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
991
992 compiler->cache_arg = 0;
993 compiler->cache_argw = 0;
994
995 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG2 : (dst << 1);
996
997 if (src1 > SLJIT_FLOAT_REG6) {
998 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
999 FAIL_IF(compiler->error);
1000 src1 = TMP_FREG1;
1001 } else
1002 flags |= SLOW_SRC1;
1003 }
1004 else
1005 src1 <<= 1;
1006
1007 if (src2 > SLJIT_FLOAT_REG6) {
1008 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1009 FAIL_IF(compiler->error);
1010 src2 = TMP_FREG2;
1011 } else
1012 flags |= SLOW_SRC2;
1013 }
1014 else
1015 src2 <<= 1;
1016
1017 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1018 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1019 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1020 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1021 }
1022 else {
1023 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1024 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1025 }
1026 }
1027 else if (flags & SLOW_SRC1)
1028 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1029 else if (flags & SLOW_SRC2)
1030 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1031
1032 if (flags & SLOW_SRC1)
1033 src1 = TMP_FREG1;
1034 if (flags & SLOW_SRC2)
1035 src2 = TMP_FREG2;
1036
1037 switch (GET_OPCODE(op)) {
1038 case SLJIT_ADDD:
1039 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1040 break;
1041
1042 case SLJIT_SUBD:
1043 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1044 break;
1045
1046 case SLJIT_MULD:
1047 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1048 break;
1049
1050 case SLJIT_DIVD:
1051 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1052 break;
1053 }
1054
1055 if (dst_fr == TMP_FREG2)
1056 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1057
1058 return SLJIT_SUCCESS;
1059 }
1060
1061 #undef FLOAT_DATA
1062 #undef SELECT_FOP
1063
1064 /* --------------------------------------------------------------------- */
1065 /* Other instructions */
1066 /* --------------------------------------------------------------------- */
1067
1068 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1069 {
1070 CHECK_ERROR();
1071 check_sljit_emit_fast_enter(compiler, dst, dstw);
1072 ADJUST_LOCAL_OFFSET(dst, dstw);
1073
1074 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1075 return push_inst(compiler, OR | D(dst) | S1(0) | S2(LINK_REG), DR(dst));
1076 else if (dst & SLJIT_MEM)
1077 return emit_op_mem(compiler, WORD_DATA, LINK_REG, dst, dstw);
1078
1079 /* SLJIT_UNUSED is also possible, although highly unlikely. */
1080 return SLJIT_SUCCESS;
1081 }
1082
1083 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1084 {
1085 CHECK_ERROR();
1086 check_sljit_emit_fast_return(compiler, src, srcw);
1087 ADJUST_LOCAL_OFFSET(src, srcw);
1088
1089 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1090 FAIL_IF(push_inst(compiler, OR | D(LINK_REG) | S1(0) | S2(src), DR(LINK_REG)));
1091 else if (src & SLJIT_MEM)
1092 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, LINK_REG, src, srcw));
1093 else if (src & SLJIT_IMM)
1094 FAIL_IF(load_immediate(compiler, LINK_REG, srcw));
1095
1096 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(LINK_REG) | IMM(8), UNMOVABLE_INS));
1097 return push_inst(compiler, NOP, UNMOVABLE_INS);
1098 }
1099
1100 /* --------------------------------------------------------------------- */
1101 /* Conditional instructions */
1102 /* --------------------------------------------------------------------- */
1103
1104 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1105 {
1106 struct sljit_label *label;
1107
1108 CHECK_ERROR_PTR();
1109 check_sljit_emit_label(compiler);
1110
1111 if (compiler->last_label && compiler->last_label->size == compiler->size)
1112 return compiler->last_label;
1113
1114 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1115 PTR_FAIL_IF(!label);
1116 set_label(label, compiler);
1117 compiler->delay_slot = UNMOVABLE_INS;
1118 return label;
1119 }
1120
1121 static sljit_ins get_cc(sljit_si type)
1122 {
1123 switch (type) {
1124 case SLJIT_C_EQUAL:
1125 case SLJIT_C_MUL_NOT_OVERFLOW:
1126 return DA(0x1);
1127
1128 case SLJIT_C_NOT_EQUAL:
1129 case SLJIT_C_MUL_OVERFLOW:
1130 return DA(0x9);
1131
1132 case SLJIT_C_LESS:
1133 return DA(0x5);
1134
1135 case SLJIT_C_GREATER_EQUAL:
1136 return DA(0xd);
1137
1138 case SLJIT_C_GREATER:
1139 return DA(0xc);
1140
1141 case SLJIT_C_LESS_EQUAL:
1142 return DA(0x4);
1143
1144 case SLJIT_C_SIG_LESS:
1145 return DA(0x3);
1146
1147 case SLJIT_C_SIG_GREATER_EQUAL:
1148 return DA(0xb);
1149
1150 case SLJIT_C_SIG_GREATER:
1151 return DA(0xa);
1152
1153 case SLJIT_C_SIG_LESS_EQUAL:
1154 return DA(0x2);
1155
1156 case SLJIT_C_OVERFLOW:
1157 return DA(0x7);
1158
1159 case SLJIT_C_NOT_OVERFLOW:
1160 return DA(0xf);
1161
1162 case SLJIT_C_FLOAT_EQUAL:
1163 return DA(0x9);
1164
1165 case SLJIT_C_FLOAT_NOT_EQUAL: /* Unordered. */
1166 return DA(0x1);
1167
1168 case SLJIT_C_FLOAT_LESS:
1169 return DA(0x4);
1170
1171 case SLJIT_C_FLOAT_GREATER_EQUAL: /* Unordered. */
1172 return DA(0xc);
1173
1174 case SLJIT_C_FLOAT_LESS_EQUAL:
1175 return DA(0xd);
1176
1177 case SLJIT_C_FLOAT_GREATER: /* Unordered. */
1178 return DA(0x5);
1179
1180 case SLJIT_C_FLOAT_UNORDERED:
1181 return DA(0x7);
1182
1183 case SLJIT_C_FLOAT_ORDERED:
1184 return DA(0xf);
1185
1186 default:
1187 SLJIT_ASSERT_STOP();
1188 return DA(0x8);
1189 }
1190 }
1191
1192 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1193 {
1194 struct sljit_jump *jump;
1195
1196 CHECK_ERROR_PTR();
1197 check_sljit_emit_jump(compiler, type);
1198
1199 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1200 PTR_FAIL_IF(!jump);
1201 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1202 type &= 0xff;
1203
1204 if (type < SLJIT_C_FLOAT_EQUAL) {
1205 jump->flags |= IS_COND;
1206 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1207 jump->flags |= IS_MOVABLE;
1208 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1209 PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1210 #else
1211 #error "Implementation required"
1212 #endif
1213 }
1214 else if (type < SLJIT_JUMP) {
1215 jump->flags |= IS_COND;
1216 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1217 jump->flags |= IS_MOVABLE;
1218 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1219 PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1220 #else
1221 #error "Implementation required"
1222 #endif
1223 } else {
1224 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1225 jump->flags |= IS_MOVABLE;
1226 if (type >= SLJIT_FAST_CALL)
1227 jump->flags |= IS_CALL;
1228 }
1229
1230 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1231 PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(TMP_REG2) | IMM(0), UNMOVABLE_INS));
1232 jump->addr = compiler->size;
1233 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1234
1235 return jump;
1236 }
1237
1238 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1239 {
1240 struct sljit_jump *jump = NULL;
1241 sljit_si src_r;
1242
1243 CHECK_ERROR();
1244 check_sljit_emit_ijump(compiler, type, src, srcw);
1245 ADJUST_LOCAL_OFFSET(src, srcw);
1246
1247 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1248 src_r = src;
1249 else if (src & SLJIT_IMM) {
1250 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1251 FAIL_IF(!jump);
1252 set_jump(jump, compiler, JUMP_ADDR);
1253 jump->u.target = srcw;
1254 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1255 jump->flags |= IS_MOVABLE;
1256 if (type >= SLJIT_FAST_CALL)
1257 jump->flags |= IS_CALL;
1258
1259 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1260 src_r = TMP_REG2;
1261 }
1262 else {
1263 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1264 src_r = TMP_REG2;
1265 }
1266
1267 FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1268 if (jump)
1269 jump->addr = compiler->size;
1270 return push_inst(compiler, NOP, UNMOVABLE_INS);
1271 }
1272
1273 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_cond_value(struct sljit_compiler *compiler, sljit_si op, sljit_si dst, sljit_sw dstw, sljit_si type)
1274 {
1275 sljit_si reg;
1276
1277 CHECK_ERROR();
1278 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1279 ADJUST_LOCAL_OFFSET(dst, dstw);
1280
1281 if (dst == SLJIT_UNUSED)
1282 return SLJIT_SUCCESS;
1283
1284 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1285 reg = (op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1286
1287 if (type < SLJIT_C_FLOAT_EQUAL)
1288 FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1289 else
1290 FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1291
1292 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1293 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1294
1295 if (GET_OPCODE(op) == SLJIT_OR)
1296 return emit_op(compiler, SLJIT_OR, (GET_FLAGS(op) ? SET_FLAGS : 0) | CUMULATIVE_OP | IMM_OP, dst, dstw, dst, dstw, TMP_REG2, 0);
1297
1298 return (reg == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
1299 #else
1300 #error "Implementation required"
1301 #endif
1302 }
1303
1304 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1305 {
1306 sljit_si reg;
1307 struct sljit_const *const_;
1308
1309 CHECK_ERROR_PTR();
1310 check_sljit_emit_const(compiler, dst, dstw, init_value);
1311 ADJUST_LOCAL_OFFSET(dst, dstw);
1312
1313 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1314 PTR_FAIL_IF(!const_);
1315 set_const(const_, compiler);
1316
1317 reg = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1318
1319 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1320
1321 if (dst & SLJIT_MEM)
1322 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1323
1324 return const_;
1325 }

  ViewVC Help
Powered by ViewVC 1.1.5