/[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 1215 - (show annotations)
Fri Nov 9 08:13:13 2012 UTC (7 years, 3 months ago) by zherczeg
File MIME type: text/plain
File size: 42730 byte(s)
Error occurred while calculating annotation data.
Large JIT compiler update again.
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 ALT_KEEP_CACHE 0x00080
371 #define CUMULATIVE_OP 0x00100
372 #define IMM_OP 0x00200
373 #define SRC2_IMM 0x00400
374
375 #define REG_DEST 0x00800
376 #define REG2_SOURCE 0x01000
377 #define SLOW_SRC1 0x02000
378 #define SLOW_SRC2 0x04000
379 #define SLOW_DEST 0x08000
380
381 /* SET_FLAGS (0x10 << 19) also belong here! */
382
383 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
384 #include "sljitNativeSPARC_32.c"
385 #else
386 #include "sljitNativeSPARC_64.c"
387 #endif
388
389 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
390 {
391 CHECK_ERROR();
392 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
393
394 compiler->scratches = scratches;
395 compiler->saveds = saveds;
396 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
397 compiler->logical_local_size = local_size;
398 #endif
399
400 local_size += 23 * sizeof(sljit_sw);
401 local_size = (local_size + 7) & ~0x7;
402 compiler->local_size = local_size;
403
404 if (local_size <= SIMM_MAX) {
405 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | IMM(-local_size), UNMOVABLE_INS));
406 }
407 else {
408 FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
409 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | S2(TMP_REG1), UNMOVABLE_INS));
410 }
411
412 if (args >= 1)
413 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG1) | S1(0) | S2A(24), DR(SLJIT_SAVED_REG1)));
414 if (args >= 2)
415 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG2) | S1(0) | S2A(25), DR(SLJIT_SAVED_REG2)));
416 if (args >= 3)
417 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG3) | S1(0) | S2A(26), DR(SLJIT_SAVED_REG3)));
418
419 return SLJIT_SUCCESS;
420 }
421
422 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si scratches, sljit_si saveds, sljit_si local_size)
423 {
424 CHECK_ERROR_VOID();
425 check_sljit_set_context(compiler, args, scratches, saveds, local_size);
426
427 compiler->scratches = scratches;
428 compiler->saveds = saveds;
429 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
430 compiler->logical_local_size = local_size;
431 #endif
432
433 local_size += 23 * sizeof(sljit_sw);
434 compiler->local_size = (local_size + 7) & ~0x7;
435 }
436
437 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
438 {
439 CHECK_ERROR();
440 check_sljit_emit_return(compiler, op, src, srcw);
441
442 if (op != SLJIT_MOV || !(src <= TMP_REG3)) {
443 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
444 src = SLJIT_SCRATCH_REG1;
445 }
446
447 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
448 return push_inst(compiler, RESTORE | D(SLJIT_SCRATCH_REG1) | S1(src) | S2(0), UNMOVABLE_INS);
449 }
450
451 /* --------------------------------------------------------------------- */
452 /* Operators */
453 /* --------------------------------------------------------------------- */
454
455 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
456 #define ARCH_32_64(a, b) a
457 #else
458 #define ARCH_32_64(a, b) b
459 #endif
460
461 static SLJIT_CONST sljit_ins data_transfer_insts[16 + 4] = {
462 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
463 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
464 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
465 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
466 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
467 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
468 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
469 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
470
471 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
472 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
473 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
474 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
475 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
476 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
477 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
478 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
479
480 /* d s */ OPC1(3) | OPC3(0x27),
481 /* d l */ OPC1(3) | OPC3(0x23),
482 /* s s */ OPC1(3) | OPC3(0x24),
483 /* s l */ OPC1(3) | OPC3(0x20),
484 };
485
486 #undef ARCH_32_64
487
488 /* Can perform an operation using at most 1 instruction. */
489 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
490 {
491 SLJIT_ASSERT(arg & SLJIT_MEM);
492
493 if (!(flags & WRITE_BACK) || !(arg & 0xf)) {
494 if ((!(arg & 0xf0) && argw <= SIMM_MAX && argw >= SIMM_MIN)
495 || ((arg & 0xf0) && (argw & 0x3) == 0)) {
496 /* Works for both absoulte and relative addresses (immediate case). */
497 if (SLJIT_UNLIKELY(flags & ARG_TEST))
498 return 1;
499 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
500 | ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg))
501 | S1(arg & 0xf) | ((arg & 0xf0) ? S2((arg >> 4) & 0xf) : IMM(argw)),
502 ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
503 return -1;
504 }
505 }
506 return 0;
507 }
508
509 /* See getput_arg below.
510 Note: can_cache is called only for binary operators. Those
511 operators always uses word arguments without write back. */
512 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
513 {
514 SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
515
516 /* Simple operation except for updates. */
517 if (arg & 0xf0) {
518 argw &= 0x3;
519 SLJIT_ASSERT(argw);
520 next_argw &= 0x3;
521 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == next_argw)
522 return 1;
523 return 0;
524 }
525
526 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
527 return 1;
528 return 0;
529 }
530
531 /* Emit the necessary instructions. See can_cache above. */
532 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)
533 {
534 sljit_si base, arg2, delay_slot;
535 sljit_ins dest;
536
537 SLJIT_ASSERT(arg & SLJIT_MEM);
538 if (!(next_arg & SLJIT_MEM)) {
539 next_arg = 0;
540 next_argw = 0;
541 }
542
543 base = arg & 0xf;
544 if (SLJIT_UNLIKELY(arg & 0xf0)) {
545 argw &= 0x3;
546 SLJIT_ASSERT(argw != 0);
547
548 /* Using the cache. */
549 if (((SLJIT_MEM | (arg & 0xf0)) == compiler->cache_arg) && (argw == compiler->cache_argw))
550 arg2 = TMP_REG3;
551 else {
552 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == (next_argw & 0x3)) {
553 compiler->cache_arg = SLJIT_MEM | (arg & 0xf0);
554 compiler->cache_argw = argw;
555 arg2 = TMP_REG3;
556 }
557 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && (reg << 4) != (arg & 0xf0))
558 arg2 = reg;
559 else /* It must be a mov operation, so tmp1 must be free to use. */
560 arg2 = TMP_REG1;
561 FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1((arg >> 4) & 0xf) | IMM_ARG | argw, DR(arg2)));
562 }
563 }
564 else {
565 /* Using the cache. */
566 if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
567 if (argw != compiler->cache_argw) {
568 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
569 compiler->cache_argw = argw;
570 }
571 arg2 = TMP_REG3;
572 } else {
573 if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
574 compiler->cache_arg = SLJIT_MEM;
575 compiler->cache_argw = argw;
576 arg2 = TMP_REG3;
577 }
578 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
579 arg2 = reg;
580 else /* It must be a mov operation, so tmp1 must be free to use. */
581 arg2 = TMP_REG1;
582 FAIL_IF(load_immediate(compiler, arg2, argw));
583 }
584 }
585
586 dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg));
587 delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
588 if (!base)
589 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
590 if (!(flags & WRITE_BACK))
591 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
592 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot));
593 return push_inst(compiler, ADD | D(base) | S1(base) | S2(arg2), DR(base));
594 }
595
596 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
597 {
598 if (getput_arg_fast(compiler, flags, reg, arg, argw))
599 return compiler->error;
600 compiler->cache_arg = 0;
601 compiler->cache_argw = 0;
602 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
603 }
604
605 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)
606 {
607 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
608 return compiler->error;
609 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
610 }
611
612 static sljit_si emit_op(struct sljit_compiler *compiler, sljit_si op, sljit_si flags,
613 sljit_si dst, sljit_sw dstw,
614 sljit_si src1, sljit_sw src1w,
615 sljit_si src2, sljit_sw src2w)
616 {
617 /* arg1 goes to TMP_REG1 or src reg
618 arg2 goes to TMP_REG2, imm or src reg
619 TMP_REG3 can be used for caching
620 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
621 sljit_si dst_r = TMP_REG2;
622 sljit_si src1_r;
623 sljit_sw src2_r = 0;
624 sljit_si sugg_src2_r = TMP_REG2;
625
626 if (!(flags & ALT_KEEP_CACHE)) {
627 compiler->cache_arg = 0;
628 compiler->cache_argw = 0;
629 }
630
631 if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
632 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
633 return SLJIT_SUCCESS;
634 }
635 else if (dst <= TMP_REG3) {
636 dst_r = dst;
637 flags |= REG_DEST;
638 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
639 sugg_src2_r = dst_r;
640 }
641 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
642 flags |= SLOW_DEST;
643
644 if (flags & IMM_OP) {
645 if ((src2 & SLJIT_IMM) && src2w) {
646 if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
647 flags |= SRC2_IMM;
648 src2_r = src2w;
649 }
650 }
651 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
652 if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
653 flags |= SRC2_IMM;
654 src2_r = src1w;
655
656 /* And swap arguments. */
657 src1 = src2;
658 src1w = src2w;
659 src2 = SLJIT_IMM;
660 /* src2w = src2_r unneeded. */
661 }
662 }
663 }
664
665 /* Source 1. */
666 if (src1 <= TMP_REG3)
667 src1_r = src1;
668 else if (src1 & SLJIT_IMM) {
669 if (src1w) {
670 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
671 src1_r = TMP_REG1;
672 }
673 else
674 src1_r = 0;
675 }
676 else {
677 if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
678 FAIL_IF(compiler->error);
679 else
680 flags |= SLOW_SRC1;
681 src1_r = TMP_REG1;
682 }
683
684 /* Source 2. */
685 if (src2 <= TMP_REG3) {
686 src2_r = src2;
687 flags |= REG2_SOURCE;
688 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
689 dst_r = src2_r;
690 }
691 else if (src2 & SLJIT_IMM) {
692 if (!(flags & SRC2_IMM)) {
693 if (src2w) {
694 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
695 src2_r = sugg_src2_r;
696 }
697 else {
698 src2_r = 0;
699 if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_SI) && (dst & SLJIT_MEM))
700 dst_r = 0;
701 }
702 }
703 }
704 else {
705 if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
706 FAIL_IF(compiler->error);
707 else
708 flags |= SLOW_SRC2;
709 src2_r = sugg_src2_r;
710 }
711
712 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
713 SLJIT_ASSERT(src2_r == TMP_REG2);
714 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
715 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
716 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
717 }
718 else {
719 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
720 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
721 }
722 }
723 else if (flags & SLOW_SRC1)
724 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
725 else if (flags & SLOW_SRC2)
726 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
727
728 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
729
730 if (dst & SLJIT_MEM) {
731 if (!(flags & SLOW_DEST)) {
732 getput_arg_fast(compiler, flags, dst_r, dst, dstw);
733 return compiler->error;
734 }
735 return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
736 }
737
738 return SLJIT_SUCCESS;
739 }
740
741 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
742 {
743 CHECK_ERROR();
744 check_sljit_emit_op0(compiler, op);
745
746 op = GET_OPCODE(op);
747 switch (op) {
748 case SLJIT_BREAKPOINT:
749 return push_inst(compiler, TA, UNMOVABLE_INS);
750 case SLJIT_NOP:
751 return push_inst(compiler, NOP, UNMOVABLE_INS);
752 case SLJIT_UMUL:
753 case SLJIT_SMUL:
754 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
755 FAIL_IF(push_inst(compiler, (op == SLJIT_UMUL ? UMUL : SMUL) | D(SLJIT_SCRATCH_REG1) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG1)));
756 return push_inst(compiler, RDY | D(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2));
757 #else
758 #error "Implementation required"
759 #endif
760 case SLJIT_UDIV:
761 case SLJIT_SDIV:
762 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
763 if (op == SLJIT_UDIV)
764 FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
765 else {
766 FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_SCRATCH_REG1) | IMM(31), DR(TMP_REG1)));
767 FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
768 }
769 FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_SCRATCH_REG1), DR(TMP_REG2)));
770 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? UDIV : SDIV) | D(SLJIT_SCRATCH_REG1) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG1)));
771 FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_SCRATCH_REG2) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2)));
772 FAIL_IF(push_inst(compiler, SUB | D(SLJIT_SCRATCH_REG2) | S1(TMP_REG2) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2)));
773 return SLJIT_SUCCESS;
774 #else
775 #error "Implementation required"
776 #endif
777 }
778
779 return SLJIT_SUCCESS;
780 }
781
782 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
783 sljit_si dst, sljit_sw dstw,
784 sljit_si src, sljit_sw srcw)
785 {
786 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
787
788 CHECK_ERROR();
789 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
790 ADJUST_LOCAL_OFFSET(dst, dstw);
791 ADJUST_LOCAL_OFFSET(src, srcw);
792
793 op = GET_OPCODE(op);
794 switch (op) {
795 case SLJIT_MOV:
796 case SLJIT_MOV_P:
797 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
798
799 case SLJIT_MOV_UI:
800 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
801
802 case SLJIT_MOV_SI:
803 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
804
805 case SLJIT_MOV_UB:
806 return emit_op(compiler, SLJIT_MOV_UB, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
807
808 case SLJIT_MOV_SB:
809 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);
810
811 case SLJIT_MOV_UH:
812 return emit_op(compiler, SLJIT_MOV_UH, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
813
814 case SLJIT_MOV_SH:
815 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);
816
817 case SLJIT_MOVU:
818 case SLJIT_MOVU_P:
819 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
820
821 case SLJIT_MOVU_UI:
822 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
823
824 case SLJIT_MOVU_SI:
825 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
826
827 case SLJIT_MOVU_UB:
828 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);
829
830 case SLJIT_MOVU_SB:
831 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);
832
833 case SLJIT_MOVU_UH:
834 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);
835
836 case SLJIT_MOVU_SH:
837 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);
838
839 case SLJIT_NOT:
840 case SLJIT_CLZ:
841 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
842
843 case SLJIT_NEG:
844 return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
845 }
846
847 return SLJIT_SUCCESS;
848 }
849
850 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
851 sljit_si dst, sljit_sw dstw,
852 sljit_si src1, sljit_sw src1w,
853 sljit_si src2, sljit_sw src2w)
854 {
855 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
856
857 CHECK_ERROR();
858 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
859 ADJUST_LOCAL_OFFSET(dst, dstw);
860 ADJUST_LOCAL_OFFSET(src1, src1w);
861 ADJUST_LOCAL_OFFSET(src2, src2w);
862
863 op = GET_OPCODE(op);
864 switch (op) {
865 case SLJIT_ADD:
866 case SLJIT_ADDC:
867 case SLJIT_MUL:
868 case SLJIT_AND:
869 case SLJIT_OR:
870 case SLJIT_XOR:
871 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
872
873 case SLJIT_SUB:
874 case SLJIT_SUBC:
875 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
876
877 case SLJIT_SHL:
878 case SLJIT_LSHR:
879 case SLJIT_ASHR:
880 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
881 if (src2 & SLJIT_IMM)
882 src2w &= 0x1f;
883 #else
884 SLJIT_ASSERT_STOP();
885 #endif
886 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
887 }
888
889 return SLJIT_SUCCESS;
890 }
891
892 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
893 {
894 check_sljit_get_register_index(reg);
895 return reg_map[reg];
896 }
897
898 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
899 void *instruction, sljit_si size)
900 {
901 CHECK_ERROR();
902 check_sljit_emit_op_custom(compiler, instruction, size);
903 SLJIT_ASSERT(size == 4);
904
905 return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
906 }
907
908 /* --------------------------------------------------------------------- */
909 /* Floating point operators */
910 /* --------------------------------------------------------------------- */
911
912 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
913 {
914 return 1;
915 }
916
917 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 7))
918 #define SELECT_FOP(op, single, double) ((op & SLJIT_SINGLE_OP) ? single : double)
919
920 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
921 sljit_si dst, sljit_sw dstw,
922 sljit_si src, sljit_sw srcw)
923 {
924 sljit_si dst_fr;
925
926 CHECK_ERROR();
927 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
928 SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
929
930 compiler->cache_arg = 0;
931 compiler->cache_argw = 0;
932
933 if (GET_OPCODE(op) == SLJIT_CMPD) {
934 if (dst > SLJIT_FLOAT_REG6) {
935 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, dst, dstw, src, srcw));
936 dst = TMP_FREG1;
937 }
938 else
939 dst <<= 1;
940
941 if (src > SLJIT_FLOAT_REG6) {
942 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src, srcw, 0, 0));
943 src = TMP_FREG2;
944 }
945 else
946 src <<= 1;
947
948 return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | S1A(dst) | S2A(src), FCC_IS_SET | MOVABLE_INS);
949 }
950
951 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG1 : (dst << 1);
952
953 if (src > SLJIT_FLOAT_REG6) {
954 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_fr, src, srcw, dst, dstw));
955 src = dst_fr;
956 }
957 else
958 src <<= 1;
959
960 switch (GET_OPCODE(op)) {
961 case SLJIT_MOVD:
962 if (src != dst_fr && dst_fr != TMP_FREG1) {
963 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr) | S2A(src), MOVABLE_INS));
964 if (!(op & SLJIT_SINGLE_OP))
965 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
966 }
967 break;
968 case SLJIT_NEGD:
969 FAIL_IF(push_inst(compiler, FNEGS | DA(dst_fr) | S2A(src), MOVABLE_INS));
970 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
971 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
972 break;
973 case SLJIT_ABSD:
974 FAIL_IF(push_inst(compiler, FABSS | DA(dst_fr) | S2A(src), MOVABLE_INS));
975 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
976 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
977 break;
978 }
979
980 if (dst_fr == TMP_FREG1) {
981 if (GET_OPCODE(op) == SLJIT_MOVD)
982 dst_fr = src;
983 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_fr, dst, dstw, 0, 0));
984 }
985
986 return SLJIT_SUCCESS;
987 }
988
989 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
990 sljit_si dst, sljit_sw dstw,
991 sljit_si src1, sljit_sw src1w,
992 sljit_si src2, sljit_sw src2w)
993 {
994 sljit_si dst_fr, flags = 0;
995
996 CHECK_ERROR();
997 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
998
999 compiler->cache_arg = 0;
1000 compiler->cache_argw = 0;
1001
1002 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG2 : (dst << 1);
1003
1004 if (src1 > SLJIT_FLOAT_REG6) {
1005 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1006 FAIL_IF(compiler->error);
1007 src1 = TMP_FREG1;
1008 } else
1009 flags |= SLOW_SRC1;
1010 }
1011 else
1012 src1 <<= 1;
1013
1014 if (src2 > SLJIT_FLOAT_REG6) {
1015 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1016 FAIL_IF(compiler->error);
1017 src2 = TMP_FREG2;
1018 } else
1019 flags |= SLOW_SRC2;
1020 }
1021 else
1022 src2 <<= 1;
1023
1024 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1025 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1026 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1027 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1028 }
1029 else {
1030 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1031 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1032 }
1033 }
1034 else if (flags & SLOW_SRC1)
1035 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1036 else if (flags & SLOW_SRC2)
1037 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1038
1039 if (flags & SLOW_SRC1)
1040 src1 = TMP_FREG1;
1041 if (flags & SLOW_SRC2)
1042 src2 = TMP_FREG2;
1043
1044 switch (GET_OPCODE(op)) {
1045 case SLJIT_ADDD:
1046 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1047 break;
1048
1049 case SLJIT_SUBD:
1050 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1051 break;
1052
1053 case SLJIT_MULD:
1054 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1055 break;
1056
1057 case SLJIT_DIVD:
1058 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1059 break;
1060 }
1061
1062 if (dst_fr == TMP_FREG2)
1063 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1064
1065 return SLJIT_SUCCESS;
1066 }
1067
1068 #undef FLOAT_DATA
1069 #undef SELECT_FOP
1070
1071 /* --------------------------------------------------------------------- */
1072 /* Other instructions */
1073 /* --------------------------------------------------------------------- */
1074
1075 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1076 {
1077 CHECK_ERROR();
1078 check_sljit_emit_fast_enter(compiler, dst, dstw);
1079 ADJUST_LOCAL_OFFSET(dst, dstw);
1080
1081 /* For UNUSED dst. Uncommon, but possible. */
1082 if (dst == SLJIT_UNUSED)
1083 return SLJIT_SUCCESS;
1084
1085 if (dst <= TMP_REG3)
1086 return push_inst(compiler, OR | D(dst) | S1(0) | S2(LINK_REG), DR(dst));
1087
1088 /* Memory. */
1089 return emit_op_mem(compiler, WORD_DATA, LINK_REG, dst, dstw);
1090 }
1091
1092 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1093 {
1094 CHECK_ERROR();
1095 check_sljit_emit_fast_return(compiler, src, srcw);
1096 ADJUST_LOCAL_OFFSET(src, srcw);
1097
1098 if (src <= TMP_REG3)
1099 FAIL_IF(push_inst(compiler, OR | D(LINK_REG) | S1(0) | S2(src), DR(LINK_REG)));
1100 else if (src & SLJIT_MEM)
1101 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, LINK_REG, src, srcw));
1102 else if (src & SLJIT_IMM)
1103 FAIL_IF(load_immediate(compiler, LINK_REG, srcw));
1104
1105 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(LINK_REG) | IMM(8), UNMOVABLE_INS));
1106 return push_inst(compiler, NOP, UNMOVABLE_INS);
1107 }
1108
1109 /* --------------------------------------------------------------------- */
1110 /* Conditional instructions */
1111 /* --------------------------------------------------------------------- */
1112
1113 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1114 {
1115 struct sljit_label *label;
1116
1117 CHECK_ERROR_PTR();
1118 check_sljit_emit_label(compiler);
1119
1120 if (compiler->last_label && compiler->last_label->size == compiler->size)
1121 return compiler->last_label;
1122
1123 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1124 PTR_FAIL_IF(!label);
1125 set_label(label, compiler);
1126 compiler->delay_slot = UNMOVABLE_INS;
1127 return label;
1128 }
1129
1130 static sljit_ins get_cc(sljit_si type)
1131 {
1132 switch (type) {
1133 case SLJIT_C_EQUAL:
1134 case SLJIT_C_MUL_NOT_OVERFLOW:
1135 return DA(0x1);
1136
1137 case SLJIT_C_NOT_EQUAL:
1138 case SLJIT_C_MUL_OVERFLOW:
1139 return DA(0x9);
1140
1141 case SLJIT_C_LESS:
1142 return DA(0x5);
1143
1144 case SLJIT_C_GREATER_EQUAL:
1145 return DA(0xd);
1146
1147 case SLJIT_C_GREATER:
1148 return DA(0xc);
1149
1150 case SLJIT_C_LESS_EQUAL:
1151 return DA(0x4);
1152
1153 case SLJIT_C_SIG_LESS:
1154 return DA(0x3);
1155
1156 case SLJIT_C_SIG_GREATER_EQUAL:
1157 return DA(0xb);
1158
1159 case SLJIT_C_SIG_GREATER:
1160 return DA(0xa);
1161
1162 case SLJIT_C_SIG_LESS_EQUAL:
1163 return DA(0x2);
1164
1165 case SLJIT_C_OVERFLOW:
1166 return DA(0x7);
1167
1168 case SLJIT_C_NOT_OVERFLOW:
1169 return DA(0xf);
1170
1171 case SLJIT_C_FLOAT_EQUAL:
1172 return DA(0x9);
1173
1174 case SLJIT_C_FLOAT_NOT_EQUAL: /* Unordered. */
1175 return DA(0x1);
1176
1177 case SLJIT_C_FLOAT_LESS:
1178 return DA(0x4);
1179
1180 case SLJIT_C_FLOAT_GREATER_EQUAL: /* Unordered. */
1181 return DA(0xc);
1182
1183 case SLJIT_C_FLOAT_LESS_EQUAL:
1184 return DA(0xd);
1185
1186 case SLJIT_C_FLOAT_GREATER: /* Unordered. */
1187 return DA(0x5);
1188
1189 case SLJIT_C_FLOAT_UNORDERED:
1190 return DA(0x7);
1191
1192 case SLJIT_C_FLOAT_ORDERED:
1193 return DA(0xf);
1194
1195 default:
1196 SLJIT_ASSERT_STOP();
1197 return DA(0x8);
1198 }
1199 }
1200
1201 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1202 {
1203 struct sljit_jump *jump;
1204
1205 CHECK_ERROR_PTR();
1206 check_sljit_emit_jump(compiler, type);
1207
1208 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1209 PTR_FAIL_IF(!jump);
1210 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1211 type &= 0xff;
1212
1213 if (type < SLJIT_C_FLOAT_EQUAL) {
1214 jump->flags |= IS_COND;
1215 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1216 jump->flags |= IS_MOVABLE;
1217 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1218 PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1219 #else
1220 #error "Implementation required"
1221 #endif
1222 }
1223 else if (type < SLJIT_JUMP) {
1224 jump->flags |= IS_COND;
1225 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1226 jump->flags |= IS_MOVABLE;
1227 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1228 PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1229 #else
1230 #error "Implementation required"
1231 #endif
1232 } else {
1233 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1234 jump->flags |= IS_MOVABLE;
1235 if (type >= SLJIT_FAST_CALL)
1236 jump->flags |= IS_CALL;
1237 }
1238
1239 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1240 PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(TMP_REG2) | IMM(0), UNMOVABLE_INS));
1241 jump->addr = compiler->size;
1242 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1243
1244 return jump;
1245 }
1246
1247 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1248 {
1249 struct sljit_jump *jump = NULL;
1250 sljit_si src_r;
1251
1252 CHECK_ERROR();
1253 check_sljit_emit_ijump(compiler, type, src, srcw);
1254 ADJUST_LOCAL_OFFSET(src, srcw);
1255
1256 if (src <= TMP_REG3)
1257 src_r = src;
1258 else if (src & SLJIT_IMM) {
1259 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1260 FAIL_IF(!jump);
1261 set_jump(jump, compiler, JUMP_ADDR);
1262 jump->u.target = srcw;
1263 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1264 jump->flags |= IS_MOVABLE;
1265 if (type >= SLJIT_FAST_CALL)
1266 jump->flags |= IS_CALL;
1267
1268 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1269 src_r = TMP_REG2;
1270 }
1271 else {
1272 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1273 src_r = TMP_REG2;
1274 }
1275
1276 FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1277 if (jump)
1278 jump->addr = compiler->size;
1279 return push_inst(compiler, NOP, UNMOVABLE_INS);
1280 }
1281
1282 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1283 sljit_si dst, sljit_sw dstw,
1284 sljit_si src, sljit_sw srcw,
1285 sljit_si type)
1286 {
1287 sljit_si reg, flags = (GET_FLAGS(op) ? SET_FLAGS : 0);
1288
1289 CHECK_ERROR();
1290 check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type);
1291 ADJUST_LOCAL_OFFSET(dst, dstw);
1292
1293 if (dst == SLJIT_UNUSED)
1294 return SLJIT_SUCCESS;
1295
1296 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1297 op = GET_OPCODE(op);
1298 reg = (op < SLJIT_ADD && dst <= TMP_REG3) ? dst : TMP_REG2;
1299
1300 compiler->cache_arg = 0;
1301 compiler->cache_argw = 0;
1302 if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
1303 ADJUST_LOCAL_OFFSET(src, srcw);
1304 FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
1305 src = TMP_REG1;
1306 srcw = 0;
1307 }
1308
1309 if (type < SLJIT_C_FLOAT_EQUAL)
1310 FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1311 else
1312 FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1313
1314 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1315 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1316
1317 if (op >= SLJIT_ADD)
1318 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE, dst, dstw, src, srcw, TMP_REG2, 0);
1319
1320 return (reg == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
1321 #else
1322 #error "Implementation required"
1323 #endif
1324 }
1325
1326 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1327 {
1328 sljit_si reg;
1329 struct sljit_const *const_;
1330
1331 CHECK_ERROR_PTR();
1332 check_sljit_emit_const(compiler, dst, dstw, init_value);
1333 ADJUST_LOCAL_OFFSET(dst, dstw);
1334
1335 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1336 PTR_FAIL_IF(!const_);
1337 set_const(const_, compiler);
1338
1339 reg = (dst <= TMP_REG3) ? dst : TMP_REG2;
1340
1341 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1342
1343 if (dst & SLJIT_MEM)
1344 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1345
1346 return const_;
1347 }

  ViewVC Help
Powered by ViewVC 1.1.5