2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 SLJIT_API_FUNC_ATTRIBUTE
const char* sljit_get_platform_name(void)
29 return "SPARC" SLJIT_CPUINFO
;
32 /* Length of an instruction word
33 Both for sparc-32 and sparc-64 */
34 typedef sljit_u32 sljit_ins
;
36 #if (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL)
38 static void sparc_cache_flush(sljit_ins
*from
, sljit_ins
*to
)
40 #if defined(__SUNPRO_C) && __SUNPRO_C < 0x590
42 /* if (from == to) return */
47 /* loop until from >= to */
55 /* The comparison was done above. */
57 /* nop is not necessary here, since the
58 sub operation has no side effect. */
64 if (SLJIT_UNLIKELY(from
== to
))
72 /* Operates at least on doubleword. */
77 /* Flush the last word. */
87 #endif /* (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL) */
89 /* TMP_REG2 is not used by getput_arg */
90 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
91 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
92 #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
93 #define TMP_LINK (SLJIT_NUMBER_OF_REGISTERS + 5)
96 #define TMP_FREG2 ((SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1) << 1)
98 static const sljit_u8 reg_map
[SLJIT_NUMBER_OF_REGISTERS
+ 6] = {
99 0, 8, 9, 10, 13, 29, 28, 27, 23, 22, 21, 20, 19, 18, 17, 16, 26, 25, 24, 14, 1, 11, 12, 15
102 /* --------------------------------------------------------------------- */
103 /* Instrucion forms */
104 /* --------------------------------------------------------------------- */
106 #define D(d) (reg_map[d] << 25)
107 #define DA(d) ((d) << 25)
108 #define S1(s1) (reg_map[s1] << 14)
109 #define S2(s2) (reg_map[s2])
110 #define S1A(s1) ((s1) << 14)
112 #define IMM_ARG 0x2000
113 #define DOP(op) ((op) << 5)
114 #define IMM(imm) (((imm) & 0x1fff) | IMM_ARG)
116 #define DR(dr) (reg_map[dr])
117 #define OPC1(opcode) ((opcode) << 30)
118 #define OPC2(opcode) ((opcode) << 22)
119 #define OPC3(opcode) ((opcode) << 19)
120 #define SET_FLAGS OPC3(0x10)
122 #define ADD (OPC1(0x2) | OPC3(0x00))
123 #define ADDC (OPC1(0x2) | OPC3(0x08))
124 #define AND (OPC1(0x2) | OPC3(0x01))
125 #define ANDN (OPC1(0x2) | OPC3(0x05))
126 #define CALL (OPC1(0x1))
127 #define FABSS (OPC1(0x2) | OPC3(0x34) | DOP(0x09))
128 #define FADDD (OPC1(0x2) | OPC3(0x34) | DOP(0x42))
129 #define FADDS (OPC1(0x2) | OPC3(0x34) | DOP(0x41))
130 #define FCMPD (OPC1(0x2) | OPC3(0x35) | DOP(0x52))
131 #define FCMPS (OPC1(0x2) | OPC3(0x35) | DOP(0x51))
132 #define FDIVD (OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
133 #define FDIVS (OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
134 #define FDTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd2))
135 #define FDTOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc6))
136 #define FITOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc8))
137 #define FITOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc4))
138 #define FMOVS (OPC1(0x2) | OPC3(0x34) | DOP(0x01))
139 #define FMULD (OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
140 #define FMULS (OPC1(0x2) | OPC3(0x34) | DOP(0x49))
141 #define FNEGS (OPC1(0x2) | OPC3(0x34) | DOP(0x05))
142 #define FSTOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc9))
143 #define FSTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd1))
144 #define FSUBD (OPC1(0x2) | OPC3(0x34) | DOP(0x46))
145 #define FSUBS (OPC1(0x2) | OPC3(0x34) | DOP(0x45))
146 #define JMPL (OPC1(0x2) | OPC3(0x38))
147 #define NOP (OPC1(0x0) | OPC2(0x04))
148 #define OR (OPC1(0x2) | OPC3(0x02))
149 #define ORN (OPC1(0x2) | OPC3(0x06))
150 #define RDY (OPC1(0x2) | OPC3(0x28) | S1A(0))
151 #define RESTORE (OPC1(0x2) | OPC3(0x3d))
152 #define SAVE (OPC1(0x2) | OPC3(0x3c))
153 #define SETHI (OPC1(0x0) | OPC2(0x04))
154 #define SLL (OPC1(0x2) | OPC3(0x25))
155 #define SLLX (OPC1(0x2) | OPC3(0x25) | (1 << 12))
156 #define SRA (OPC1(0x2) | OPC3(0x27))
157 #define SRAX (OPC1(0x2) | OPC3(0x27) | (1 << 12))
158 #define SRL (OPC1(0x2) | OPC3(0x26))
159 #define SRLX (OPC1(0x2) | OPC3(0x26) | (1 << 12))
160 #define SUB (OPC1(0x2) | OPC3(0x04))
161 #define SUBC (OPC1(0x2) | OPC3(0x0c))
162 #define TA (OPC1(0x2) | OPC3(0x3a) | (8 << 25))
163 #define WRY (OPC1(0x2) | OPC3(0x30) | DA(0))
164 #define XOR (OPC1(0x2) | OPC3(0x03))
165 #define XNOR (OPC1(0x2) | OPC3(0x07))
167 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
168 #define MAX_DISP (0x1fffff)
169 #define MIN_DISP (-0x200000)
170 #define DISP_MASK (0x3fffff)
172 #define BICC (OPC1(0x0) | OPC2(0x2))
173 #define FBFCC (OPC1(0x0) | OPC2(0x6))
175 #define SDIV (OPC1(0x2) | OPC3(0x0f))
176 #define SMUL (OPC1(0x2) | OPC3(0x0b))
177 #define UDIV (OPC1(0x2) | OPC3(0x0e))
178 #define UMUL (OPC1(0x2) | OPC3(0x0a))
183 #define SIMM_MAX (0x0fff)
184 #define SIMM_MIN (-0x1000)
186 /* dest_reg is the absolute name of the register
187 Useful for reordering instructions in the delay slot. */
188 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_ins ins
, sljit_s32 delay_slot
)
191 SLJIT_ASSERT((delay_slot
& DST_INS_MASK
) == UNMOVABLE_INS
192 || (delay_slot
& DST_INS_MASK
) == MOVABLE_INS
193 || (delay_slot
& DST_INS_MASK
) == ((ins
>> 25) & 0x1f));
194 ptr
= (sljit_ins
*)ensure_buf(compiler
, sizeof(sljit_ins
));
198 compiler
->delay_slot
= delay_slot
;
199 return SLJIT_SUCCESS
;
202 static SLJIT_INLINE sljit_ins
* detect_jump_type(struct sljit_jump
*jump
, sljit_ins
*code_ptr
, sljit_ins
*code
, sljit_sw executable_offset
)
205 sljit_uw target_addr
;
207 sljit_ins saved_inst
;
209 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
212 if (jump
->flags
& JUMP_ADDR
)
213 target_addr
= jump
->u
.target
;
215 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
216 target_addr
= (sljit_uw
)(code
+ jump
->u
.label
->size
) + (sljit_uw
)executable_offset
;
218 inst
= (sljit_ins
*)jump
->addr
;
220 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
221 if (jump
->flags
& IS_CALL
) {
222 /* Call is always patchable on sparc 32. */
223 jump
->flags
|= PATCH_CALL
;
224 if (jump
->flags
& IS_MOVABLE
) {
227 jump
->addr
-= sizeof(sljit_ins
);
235 /* Both calls and BPr instructions shall not pass this point. */
236 #error "Implementation required"
239 if (jump
->flags
& IS_COND
)
242 diff
= ((sljit_sw
)target_addr
- (sljit_sw
)(inst
- 1) - executable_offset
) >> 2;
244 if (jump
->flags
& IS_MOVABLE
) {
245 if (diff
<= MAX_DISP
&& diff
>= MIN_DISP
) {
246 jump
->flags
|= PATCH_B
;
248 if (jump
->flags
& IS_COND
) {
249 saved_inst
= inst
[0];
250 inst
[0] = inst
[1] ^ (1 << 28);
251 inst
[1] = saved_inst
;
254 inst
[0] = BICC
| DA(0x8);
256 jump
->addr
= (sljit_uw
)inst
;
261 diff
+= sizeof(sljit_ins
);
263 if (diff
<= MAX_DISP
&& diff
>= MIN_DISP
) {
264 jump
->flags
|= PATCH_B
;
265 if (jump
->flags
& IS_COND
)
266 inst
[0] ^= (1 << 28);
268 inst
[0] = BICC
| DA(0x8);
270 jump
->addr
= (sljit_uw
)inst
;
277 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
279 struct sljit_memory_fragment
*buf
;
285 sljit_sw executable_offset
;
288 struct sljit_label
*label
;
289 struct sljit_jump
*jump
;
290 struct sljit_const
*const_
;
293 CHECK_PTR(check_sljit_generate_code(compiler
));
294 reverse_buf(compiler
);
296 code
= (sljit_ins
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_ins
));
297 PTR_FAIL_WITH_EXEC_IF(code
);
302 executable_offset
= SLJIT_EXEC_OFFSET(code
);
304 label
= compiler
->labels
;
305 jump
= compiler
->jumps
;
306 const_
= compiler
->consts
;
309 buf_ptr
= (sljit_ins
*)buf
->memory
;
310 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
312 *code_ptr
= *buf_ptr
++;
313 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
314 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
315 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
316 /* These structures are ordered by their address. */
317 if (label
&& label
->size
== word_count
) {
318 /* Just recording the address. */
319 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
320 label
->size
= code_ptr
- code
;
323 if (jump
&& jump
->addr
== word_count
) {
324 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
325 jump
->addr
= (sljit_uw
)(code_ptr
- 3);
327 jump
->addr
= (sljit_uw
)(code_ptr
- 6);
329 code_ptr
= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
332 if (const_
&& const_
->addr
== word_count
) {
333 /* Just recording the address. */
334 const_
->addr
= (sljit_uw
)code_ptr
;
335 const_
= const_
->next
;
339 } while (buf_ptr
< buf_end
);
344 if (label
&& label
->size
== word_count
) {
345 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
346 label
->size
= code_ptr
- code
;
350 SLJIT_ASSERT(!label
);
352 SLJIT_ASSERT(!const_
);
353 SLJIT_ASSERT(code_ptr
- code
<= (sljit_s32
)compiler
->size
);
355 jump
= compiler
->jumps
;
358 addr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
359 buf_ptr
= (sljit_ins
*)jump
->addr
;
361 if (jump
->flags
& PATCH_CALL
) {
362 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
363 SLJIT_ASSERT((sljit_sw
)addr
<= 0x1fffffff && (sljit_sw
)addr
>= -0x20000000);
364 buf_ptr
[0] = CALL
| (addr
& 0x3fffffff);
367 if (jump
->flags
& PATCH_B
) {
368 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
369 SLJIT_ASSERT((sljit_sw
)addr
<= MAX_DISP
&& (sljit_sw
)addr
>= MIN_DISP
);
370 buf_ptr
[0] = (buf_ptr
[0] & ~DISP_MASK
) | (addr
& DISP_MASK
);
374 /* Set the fields of immediate loads. */
375 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
376 buf_ptr
[0] = (buf_ptr
[0] & 0xffc00000) | ((addr
>> 10) & 0x3fffff);
377 buf_ptr
[1] = (buf_ptr
[1] & 0xfffffc00) | (addr
& 0x3ff);
379 #error "Implementation required"
386 compiler
->error
= SLJIT_ERR_COMPILED
;
387 compiler
->executable_offset
= executable_offset
;
388 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_ins
);
390 code
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
391 code_ptr
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
393 SLJIT_CACHE_FLUSH(code
, code_ptr
);
397 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
399 switch (feature_type
) {
401 #ifdef SLJIT_IS_FPU_AVAILABLE
402 return SLJIT_IS_FPU_AVAILABLE
;
404 /* Available by default. */
408 #if (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
418 /* --------------------------------------------------------------------- */
420 /* --------------------------------------------------------------------- */
422 /* Creates an index in data_transfer_insts array. */
423 #define LOAD_DATA 0x01
424 #define WORD_DATA 0x00
425 #define BYTE_DATA 0x02
426 #define HALF_DATA 0x04
427 #define INT_DATA 0x06
428 #define SIGNED_DATA 0x08
429 /* Separates integer and floating point registers */
431 #define DOUBLE_DATA 0x10
432 #define SINGLE_DATA 0x12
434 #define MEM_MASK 0x1f
436 #define WRITE_BACK 0x00020
437 #define ARG_TEST 0x00040
438 #define ALT_KEEP_CACHE 0x00080
439 #define CUMULATIVE_OP 0x00100
440 #define IMM_OP 0x00200
441 #define SRC2_IMM 0x00400
443 #define REG_DEST 0x00800
444 #define REG2_SOURCE 0x01000
445 #define SLOW_SRC1 0x02000
446 #define SLOW_SRC2 0x04000
447 #define SLOW_DEST 0x08000
449 /* SET_FLAGS (0x10 << 19) also belong here! */
451 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
452 #include "sljitNativeSPARC_32.c"
454 #include "sljitNativeSPARC_64.c"
457 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
458 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
459 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
462 CHECK(check_sljit_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
463 set_emit_enter(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
465 local_size
= (local_size
+ SLJIT_LOCALS_OFFSET
+ 7) & ~0x7;
466 compiler
->local_size
= local_size
;
468 if (local_size
<= SIMM_MAX
) {
469 FAIL_IF(push_inst(compiler
, SAVE
| D(SLJIT_SP
) | S1(SLJIT_SP
) | IMM(-local_size
), UNMOVABLE_INS
));
472 FAIL_IF(load_immediate(compiler
, TMP_REG1
, -local_size
));
473 FAIL_IF(push_inst(compiler
, SAVE
| D(SLJIT_SP
) | S1(SLJIT_SP
) | S2(TMP_REG1
), UNMOVABLE_INS
));
476 /* Arguments are in their appropriate registers. */
478 return SLJIT_SUCCESS
;
481 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
482 sljit_s32 options
, sljit_s32 args
, sljit_s32 scratches
, sljit_s32 saveds
,
483 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
486 CHECK(check_sljit_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
487 set_set_context(compiler
, options
, args
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
489 compiler
->local_size
= (local_size
+ SLJIT_LOCALS_OFFSET
+ 7) & ~0x7;
490 return SLJIT_SUCCESS
;
493 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
496 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
498 if (op
!= SLJIT_MOV
|| !FAST_IS_REG(src
)) {
499 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
503 FAIL_IF(push_inst(compiler
, JMPL
| D(0) | S1A(31) | IMM(8), UNMOVABLE_INS
));
504 return push_inst(compiler
, RESTORE
| D(SLJIT_R0
) | S1(src
) | S2(0), UNMOVABLE_INS
);
507 /* --------------------------------------------------------------------- */
509 /* --------------------------------------------------------------------- */
511 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
512 #define ARCH_32_64(a, b) a
514 #define ARCH_32_64(a, b) b
517 static const sljit_ins data_transfer_insts
[16 + 4] = {
518 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
519 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
520 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
521 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
522 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
523 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
524 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
525 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
527 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
528 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
529 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
530 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
531 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
532 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
533 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
534 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
536 /* d s */ OPC1(3) | OPC3(0x27),
537 /* d l */ OPC1(3) | OPC3(0x23),
538 /* s s */ OPC1(3) | OPC3(0x24),
539 /* s l */ OPC1(3) | OPC3(0x20),
544 /* Can perform an operation using at most 1 instruction. */
545 static sljit_s32
getput_arg_fast(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
547 SLJIT_ASSERT(arg
& SLJIT_MEM
);
549 if (!(flags
& WRITE_BACK
) || !(arg
& REG_MASK
)) {
550 if ((!(arg
& OFFS_REG_MASK
) && argw
<= SIMM_MAX
&& argw
>= SIMM_MIN
)
551 || ((arg
& OFFS_REG_MASK
) && (argw
& 0x3) == 0)) {
552 /* Works for both absoulte and relative addresses (immediate case). */
553 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
555 FAIL_IF(push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
]
556 | ((flags
& MEM_MASK
) <= GPR_REG
? D(reg
) : DA(reg
))
557 | S1(arg
& REG_MASK
) | ((arg
& OFFS_REG_MASK
) ? S2(OFFS_REG(arg
)) : IMM(argw
)),
558 ((flags
& MEM_MASK
) <= GPR_REG
&& (flags
& LOAD_DATA
)) ? DR(reg
) : MOVABLE_INS
));
565 /* See getput_arg below.
566 Note: can_cache is called only for binary operators. Those
567 operators always uses word arguments without write back. */
568 static sljit_s32
can_cache(sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
570 SLJIT_ASSERT((arg
& SLJIT_MEM
) && (next_arg
& SLJIT_MEM
));
572 /* Simple operation except for updates. */
573 if (arg
& OFFS_REG_MASK
) {
577 if ((arg
& OFFS_REG_MASK
) == (next_arg
& OFFS_REG_MASK
) && argw
== next_argw
)
582 if (((next_argw
- argw
) <= SIMM_MAX
&& (next_argw
- argw
) >= SIMM_MIN
))
587 /* Emit the necessary instructions. See can_cache above. */
588 static sljit_s32
getput_arg(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
590 sljit_s32 base
, arg2
, delay_slot
;
593 SLJIT_ASSERT(arg
& SLJIT_MEM
);
594 if (!(next_arg
& SLJIT_MEM
)) {
599 base
= arg
& REG_MASK
;
600 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
603 /* Using the cache. */
604 if (((SLJIT_MEM
| (arg
& OFFS_REG_MASK
)) == compiler
->cache_arg
) && (argw
== compiler
->cache_argw
))
607 if ((arg
& OFFS_REG_MASK
) == (next_arg
& OFFS_REG_MASK
) && argw
== (next_argw
& 0x3)) {
608 compiler
->cache_arg
= SLJIT_MEM
| (arg
& OFFS_REG_MASK
);
609 compiler
->cache_argw
= argw
;
612 else if ((flags
& LOAD_DATA
) && ((flags
& MEM_MASK
) <= GPR_REG
) && reg
!= base
&& reg
!= OFFS_REG(arg
))
614 else /* It must be a mov operation, so tmp1 must be free to use. */
616 FAIL_IF(push_inst(compiler
, SLL_W
| D(arg2
) | S1(OFFS_REG(arg
)) | IMM_ARG
| argw
, DR(arg2
)));
620 /* Using the cache. */
621 if ((compiler
->cache_arg
== SLJIT_MEM
) && (argw
- compiler
->cache_argw
) <= SIMM_MAX
&& (argw
- compiler
->cache_argw
) >= SIMM_MIN
) {
622 if (argw
!= compiler
->cache_argw
) {
623 FAIL_IF(push_inst(compiler
, ADD
| D(TMP_REG3
) | S1(TMP_REG3
) | IMM(argw
- compiler
->cache_argw
), DR(TMP_REG3
)));
624 compiler
->cache_argw
= argw
;
628 if ((next_argw
- argw
) <= SIMM_MAX
&& (next_argw
- argw
) >= SIMM_MIN
) {
629 compiler
->cache_arg
= SLJIT_MEM
;
630 compiler
->cache_argw
= argw
;
633 else if ((flags
& LOAD_DATA
) && ((flags
& MEM_MASK
) <= GPR_REG
) && reg
!= base
)
635 else /* It must be a mov operation, so tmp1 must be free to use. */
637 FAIL_IF(load_immediate(compiler
, arg2
, argw
));
641 dest
= ((flags
& MEM_MASK
) <= GPR_REG
? D(reg
) : DA(reg
));
642 delay_slot
= ((flags
& MEM_MASK
) <= GPR_REG
&& (flags
& LOAD_DATA
)) ? DR(reg
) : MOVABLE_INS
;
644 return push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
] | dest
| S1(arg2
) | IMM(0), delay_slot
);
645 if (!(flags
& WRITE_BACK
))
646 return push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
] | dest
| S1(base
) | S2(arg2
), delay_slot
);
647 FAIL_IF(push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
] | dest
| S1(base
) | S2(arg2
), delay_slot
));
648 return push_inst(compiler
, ADD
| D(base
) | S1(base
) | S2(arg2
), DR(base
));
651 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
653 if (getput_arg_fast(compiler
, flags
, reg
, arg
, argw
))
654 return compiler
->error
;
655 compiler
->cache_arg
= 0;
656 compiler
->cache_argw
= 0;
657 return getput_arg(compiler
, flags
, reg
, arg
, argw
, 0, 0);
660 static SLJIT_INLINE sljit_s32
emit_op_mem2(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg1
, sljit_sw arg1w
, sljit_s32 arg2
, sljit_sw arg2w
)
662 if (getput_arg_fast(compiler
, flags
, reg
, arg1
, arg1w
))
663 return compiler
->error
;
664 return getput_arg(compiler
, flags
, reg
, arg1
, arg1w
, arg2
, arg2w
);
667 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 flags
,
668 sljit_s32 dst
, sljit_sw dstw
,
669 sljit_s32 src1
, sljit_sw src1w
,
670 sljit_s32 src2
, sljit_sw src2w
)
672 /* arg1 goes to TMP_REG1 or src reg
673 arg2 goes to TMP_REG2, imm or src reg
674 TMP_REG3 can be used for caching
675 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
676 sljit_s32 dst_r
= TMP_REG2
;
679 sljit_s32 sugg_src2_r
= TMP_REG2
;
681 if (!(flags
& ALT_KEEP_CACHE
)) {
682 compiler
->cache_arg
= 0;
683 compiler
->cache_argw
= 0;
686 if (dst
!= SLJIT_UNUSED
) {
687 if (FAST_IS_REG(dst
)) {
690 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOVU_S32
)
693 else if ((dst
& SLJIT_MEM
) && !getput_arg_fast(compiler
, flags
| ARG_TEST
, TMP_REG1
, dst
, dstw
))
697 if (flags
& IMM_OP
) {
698 if ((src2
& SLJIT_IMM
) && src2w
) {
699 if (src2w
<= SIMM_MAX
&& src2w
>= SIMM_MIN
) {
704 if (!(flags
& SRC2_IMM
) && (flags
& CUMULATIVE_OP
) && (src1
& SLJIT_IMM
) && src1w
) {
705 if (src1w
<= SIMM_MAX
&& src1w
>= SIMM_MIN
) {
709 /* And swap arguments. */
713 /* src2w = src2_r unneeded. */
719 if (FAST_IS_REG(src1
))
721 else if (src1
& SLJIT_IMM
) {
723 FAIL_IF(load_immediate(compiler
, TMP_REG1
, src1w
));
730 if (getput_arg_fast(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
))
731 FAIL_IF(compiler
->error
);
738 if (FAST_IS_REG(src2
)) {
740 flags
|= REG2_SOURCE
;
741 if (!(flags
& REG_DEST
) && op
>= SLJIT_MOV
&& op
<= SLJIT_MOVU_S32
)
744 else if (src2
& SLJIT_IMM
) {
745 if (!(flags
& SRC2_IMM
)) {
747 FAIL_IF(load_immediate(compiler
, sugg_src2_r
, src2w
));
748 src2_r
= sugg_src2_r
;
752 if ((op
>= SLJIT_MOV
&& op
<= SLJIT_MOVU_S32
) && (dst
& SLJIT_MEM
))
758 if (getput_arg_fast(compiler
, flags
| LOAD_DATA
, sugg_src2_r
, src2
, src2w
))
759 FAIL_IF(compiler
->error
);
762 src2_r
= sugg_src2_r
;
765 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
766 SLJIT_ASSERT(src2_r
== TMP_REG2
);
767 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
768 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG2
, src2
, src2w
, src1
, src1w
));
769 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
772 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, src2
, src2w
));
773 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
776 else if (flags
& SLOW_SRC1
)
777 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
778 else if (flags
& SLOW_SRC2
)
779 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, sugg_src2_r
, src2
, src2w
, dst
, dstw
));
781 FAIL_IF(emit_single_op(compiler
, op
, flags
, dst_r
, src1_r
, src2_r
));
783 if (dst
& SLJIT_MEM
) {
784 if (!(flags
& SLOW_DEST
)) {
785 getput_arg_fast(compiler
, flags
, dst_r
, dst
, dstw
);
786 return compiler
->error
;
788 return getput_arg(compiler
, flags
, dst_r
, dst
, dstw
, 0, 0);
791 return SLJIT_SUCCESS
;
794 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
797 CHECK(check_sljit_emit_op0(compiler
, op
));
801 case SLJIT_BREAKPOINT
:
802 return push_inst(compiler
, TA
, UNMOVABLE_INS
);
804 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
807 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
808 FAIL_IF(push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMUL
: SMUL
) | D(SLJIT_R0
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R0
)));
809 return push_inst(compiler
, RDY
| D(SLJIT_R1
), DR(SLJIT_R1
));
811 #error "Implementation required"
813 case SLJIT_DIVMOD_UW
:
814 case SLJIT_DIVMOD_SW
:
817 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
818 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
819 if ((op
| 0x2) == SLJIT_DIV_UW
)
820 FAIL_IF(push_inst(compiler
, WRY
| S1(0), MOVABLE_INS
));
822 FAIL_IF(push_inst(compiler
, SRA
| D(TMP_REG1
) | S1(SLJIT_R0
) | IMM(31), DR(TMP_REG1
)));
823 FAIL_IF(push_inst(compiler
, WRY
| S1(TMP_REG1
), MOVABLE_INS
));
825 if (op
<= SLJIT_DIVMOD_SW
)
826 FAIL_IF(push_inst(compiler
, OR
| D(TMP_REG2
) | S1(0) | S2(SLJIT_R0
), DR(TMP_REG2
)));
827 FAIL_IF(push_inst(compiler
, ((op
| 0x2) == SLJIT_DIV_UW
? UDIV
: SDIV
) | D(SLJIT_R0
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R0
)));
828 if (op
>= SLJIT_DIV_UW
)
829 return SLJIT_SUCCESS
;
830 FAIL_IF(push_inst(compiler
, SMUL
| D(SLJIT_R1
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R1
)));
831 return push_inst(compiler
, SUB
| D(SLJIT_R1
) | S1(TMP_REG2
) | S2(SLJIT_R1
), DR(SLJIT_R1
));
833 #error "Implementation required"
837 return SLJIT_SUCCESS
;
840 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
841 sljit_s32 dst
, sljit_sw dstw
,
842 sljit_s32 src
, sljit_sw srcw
)
844 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
847 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
848 ADJUST_LOCAL_OFFSET(dst
, dstw
);
849 ADJUST_LOCAL_OFFSET(src
, srcw
);
851 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
852 return SLJIT_SUCCESS
;
858 return emit_op(compiler
, SLJIT_MOV
, flags
| WORD_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
861 return emit_op(compiler
, SLJIT_MOV_U32
, flags
| INT_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
864 return emit_op(compiler
, SLJIT_MOV_S32
, flags
| INT_DATA
| SIGNED_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
867 return emit_op(compiler
, SLJIT_MOV_U8
, flags
| BYTE_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u8
)srcw
: srcw
);
870 return emit_op(compiler
, SLJIT_MOV_S8
, flags
| BYTE_DATA
| SIGNED_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s8
)srcw
: srcw
);
873 return emit_op(compiler
, SLJIT_MOV_U16
, flags
| HALF_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u16
)srcw
: srcw
);
876 return emit_op(compiler
, SLJIT_MOV_S16
, flags
| HALF_DATA
| SIGNED_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s16
)srcw
: srcw
);
880 return emit_op(compiler
, SLJIT_MOV
, flags
| WORD_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
883 return emit_op(compiler
, SLJIT_MOV_U32
, flags
| INT_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
886 return emit_op(compiler
, SLJIT_MOV_S32
, flags
| INT_DATA
| SIGNED_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
889 return emit_op(compiler
, SLJIT_MOV_U8
, flags
| BYTE_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u8
)srcw
: srcw
);
892 return emit_op(compiler
, SLJIT_MOV_S8
, flags
| BYTE_DATA
| SIGNED_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s8
)srcw
: srcw
);
895 return emit_op(compiler
, SLJIT_MOV_U16
, flags
| HALF_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u16
)srcw
: srcw
);
898 return emit_op(compiler
, SLJIT_MOV_S16
, flags
| HALF_DATA
| SIGNED_DATA
| WRITE_BACK
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_s16
)srcw
: srcw
);
902 return emit_op(compiler
, op
, flags
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
905 return emit_op(compiler
, SLJIT_SUB
, flags
| IMM_OP
, dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
908 return SLJIT_SUCCESS
;
911 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
912 sljit_s32 dst
, sljit_sw dstw
,
913 sljit_s32 src1
, sljit_sw src1w
,
914 sljit_s32 src2
, sljit_sw src2w
)
916 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
919 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
920 ADJUST_LOCAL_OFFSET(dst
, dstw
);
921 ADJUST_LOCAL_OFFSET(src1
, src1w
);
922 ADJUST_LOCAL_OFFSET(src2
, src2w
);
924 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
925 return SLJIT_SUCCESS
;
935 return emit_op(compiler
, op
, flags
| CUMULATIVE_OP
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
939 return emit_op(compiler
, op
, flags
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
944 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
945 if (src2
& SLJIT_IMM
)
950 return emit_op(compiler
, op
, flags
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
953 return SLJIT_SUCCESS
;
956 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
958 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
962 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
964 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
968 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
969 void *instruction
, sljit_s32 size
)
972 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
974 return push_inst(compiler
, *(sljit_ins
*)instruction
, UNMOVABLE_INS
);
977 /* --------------------------------------------------------------------- */
978 /* Floating point operators */
979 /* --------------------------------------------------------------------- */
981 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
982 #define SELECT_FOP(op, single, double) ((op & SLJIT_F32_OP) ? single : double)
983 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
985 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
986 sljit_s32 dst
, sljit_sw dstw
,
987 sljit_s32 src
, sljit_sw srcw
)
989 if (src
& SLJIT_MEM
) {
990 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src
, srcw
, dst
, dstw
));
996 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSTOI
, FDTOI
) | DA(TMP_FREG1
) | S2A(src
), MOVABLE_INS
));
998 if (FAST_IS_REG(dst
)) {
999 FAIL_IF(emit_op_mem2(compiler
, SINGLE_DATA
, TMP_FREG1
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
));
1000 return emit_op_mem2(compiler
, WORD_DATA
| LOAD_DATA
, dst
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
);
1003 /* Store the integer value from a VFP register. */
1004 return emit_op_mem2(compiler
, SINGLE_DATA
, TMP_FREG1
, dst
, dstw
, 0, 0);
1007 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1008 sljit_s32 dst
, sljit_sw dstw
,
1009 sljit_s32 src
, sljit_sw srcw
)
1011 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? (dst
<< 1) : TMP_FREG1
;
1013 if (src
& SLJIT_IMM
) {
1014 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1015 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1016 srcw
= (sljit_s32
)srcw
;
1018 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1023 if (FAST_IS_REG(src
)) {
1024 FAIL_IF(emit_op_mem2(compiler
, WORD_DATA
, src
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
, SLJIT_MEM1(SLJIT_SP
), FLOAT_TMP_MEM_OFFSET
));
1025 src
= SLJIT_MEM1(SLJIT_SP
);
1026 srcw
= FLOAT_TMP_MEM_OFFSET
;
1029 FAIL_IF(emit_op_mem2(compiler
, SINGLE_DATA
| LOAD_DATA
, TMP_FREG1
, src
, srcw
, dst
, dstw
));
1030 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FITOS
, FITOD
) | DA(dst_r
) | S2A(TMP_FREG1
), MOVABLE_INS
));
1032 if (dst
& SLJIT_MEM
)
1033 return emit_op_mem2(compiler
, FLOAT_DATA(op
), TMP_FREG1
, dst
, dstw
, 0, 0);
1034 return SLJIT_SUCCESS
;
1037 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1038 sljit_s32 src1
, sljit_sw src1w
,
1039 sljit_s32 src2
, sljit_sw src2w
)
1041 if (src1
& SLJIT_MEM
) {
1042 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, src2
, src2w
));
1048 if (src2
& SLJIT_MEM
) {
1049 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, 0, 0));
1055 return push_inst(compiler
, SELECT_FOP(op
, FCMPS
, FCMPD
) | S1A(src1
) | S2A(src2
), FCC_IS_SET
| MOVABLE_INS
);
1058 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1059 sljit_s32 dst
, sljit_sw dstw
,
1060 sljit_s32 src
, sljit_sw srcw
)
1065 compiler
->cache_arg
= 0;
1066 compiler
->cache_argw
= 0;
1068 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP
== 0x100) && !(DOUBLE_DATA
& 0x2), float_transfer_bit_error
);
1069 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1071 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_F32
)
1074 dst_r
= FAST_IS_REG(dst
) ? (dst
<< 1) : TMP_FREG1
;
1076 if (src
& SLJIT_MEM
) {
1077 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, dst_r
, src
, srcw
, dst
, dstw
));
1083 switch (GET_OPCODE(op
)) {
1086 if (dst_r
!= TMP_FREG1
) {
1087 FAIL_IF(push_inst(compiler
, FMOVS
| DA(dst_r
) | S2A(src
), MOVABLE_INS
));
1088 if (!(op
& SLJIT_F32_OP
))
1089 FAIL_IF(push_inst(compiler
, FMOVS
| DA(dst_r
| 1) | S2A(src
| 1), MOVABLE_INS
));
1096 FAIL_IF(push_inst(compiler
, FNEGS
| DA(dst_r
) | S2A(src
), MOVABLE_INS
));
1097 if (dst_r
!= src
&& !(op
& SLJIT_F32_OP
))
1098 FAIL_IF(push_inst(compiler
, FMOVS
| DA(dst_r
| 1) | S2A(src
| 1), MOVABLE_INS
));
1101 FAIL_IF(push_inst(compiler
, FABSS
| DA(dst_r
) | S2A(src
), MOVABLE_INS
));
1102 if (dst_r
!= src
&& !(op
& SLJIT_F32_OP
))
1103 FAIL_IF(push_inst(compiler
, FMOVS
| DA(dst_r
| 1) | S2A(src
| 1), MOVABLE_INS
));
1105 case SLJIT_CONV_F64_FROM_F32
:
1106 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSTOD
, FDTOS
) | DA(dst_r
) | S2A(src
), MOVABLE_INS
));
1111 if (dst
& SLJIT_MEM
)
1112 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
), dst_r
, dst
, dstw
, 0, 0));
1113 return SLJIT_SUCCESS
;
1116 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1117 sljit_s32 dst
, sljit_sw dstw
,
1118 sljit_s32 src1
, sljit_sw src1w
,
1119 sljit_s32 src2
, sljit_sw src2w
)
1121 sljit_s32 dst_r
, flags
= 0;
1124 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1125 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1126 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1127 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1129 compiler
->cache_arg
= 0;
1130 compiler
->cache_argw
= 0;
1132 dst_r
= FAST_IS_REG(dst
) ? (dst
<< 1) : TMP_FREG2
;
1134 if (src1
& SLJIT_MEM
) {
1135 if (getput_arg_fast(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
)) {
1136 FAIL_IF(compiler
->error
);
1144 if (src2
& SLJIT_MEM
) {
1145 if (getput_arg_fast(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
)) {
1146 FAIL_IF(compiler
->error
);
1154 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
1155 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
1156 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, src1
, src1w
));
1157 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, dst
, dstw
));
1160 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, src2
, src2w
));
1161 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, dst
, dstw
));
1164 else if (flags
& SLOW_SRC1
)
1165 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, dst
, dstw
));
1166 else if (flags
& SLOW_SRC2
)
1167 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, dst
, dstw
));
1169 if (flags
& SLOW_SRC1
)
1171 if (flags
& SLOW_SRC2
)
1174 switch (GET_OPCODE(op
)) {
1176 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FADDS
, FADDD
) | DA(dst_r
) | S1A(src1
) | S2A(src2
), MOVABLE_INS
));
1180 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSUBS
, FSUBD
) | DA(dst_r
) | S1A(src1
) | S2A(src2
), MOVABLE_INS
));
1184 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FMULS
, FMULD
) | DA(dst_r
) | S1A(src1
) | S2A(src2
), MOVABLE_INS
));
1188 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FDIVS
, FDIVD
) | DA(dst_r
) | S1A(src1
) | S2A(src2
), MOVABLE_INS
));
1192 if (dst_r
== TMP_FREG2
)
1193 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
), TMP_FREG2
, dst
, dstw
, 0, 0));
1195 return SLJIT_SUCCESS
;
1201 /* --------------------------------------------------------------------- */
1202 /* Other instructions */
1203 /* --------------------------------------------------------------------- */
1205 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1208 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1209 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1211 if (FAST_IS_REG(dst
))
1212 return push_inst(compiler
, OR
| D(dst
) | S1(0) | S2(TMP_LINK
), DR(dst
));
1215 return emit_op_mem(compiler
, WORD_DATA
, TMP_LINK
, dst
, dstw
);
1218 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_return(struct sljit_compiler
*compiler
, sljit_s32 src
, sljit_sw srcw
)
1221 CHECK(check_sljit_emit_fast_return(compiler
, src
, srcw
));
1222 ADJUST_LOCAL_OFFSET(src
, srcw
);
1224 if (FAST_IS_REG(src
))
1225 FAIL_IF(push_inst(compiler
, OR
| D(TMP_LINK
) | S1(0) | S2(src
), DR(TMP_LINK
)));
1226 else if (src
& SLJIT_MEM
)
1227 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
| LOAD_DATA
, TMP_LINK
, src
, srcw
));
1228 else if (src
& SLJIT_IMM
)
1229 FAIL_IF(load_immediate(compiler
, TMP_LINK
, srcw
));
1231 FAIL_IF(push_inst(compiler
, JMPL
| D(0) | S1(TMP_LINK
) | IMM(8), UNMOVABLE_INS
));
1232 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
1235 /* --------------------------------------------------------------------- */
1236 /* Conditional instructions */
1237 /* --------------------------------------------------------------------- */
1239 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
1241 struct sljit_label
*label
;
1244 CHECK_PTR(check_sljit_emit_label(compiler
));
1246 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
1247 return compiler
->last_label
;
1249 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
1250 PTR_FAIL_IF(!label
);
1251 set_label(label
, compiler
);
1252 compiler
->delay_slot
= UNMOVABLE_INS
;
1256 static sljit_ins
get_cc(sljit_s32 type
)
1260 case SLJIT_MUL_NOT_OVERFLOW
:
1261 case SLJIT_NOT_EQUAL_F64
: /* Unordered. */
1264 case SLJIT_NOT_EQUAL
:
1265 case SLJIT_MUL_OVERFLOW
:
1266 case SLJIT_EQUAL_F64
:
1270 case SLJIT_GREATER_F64
: /* Unordered. */
1273 case SLJIT_GREATER_EQUAL
:
1274 case SLJIT_LESS_EQUAL_F64
:
1278 case SLJIT_GREATER_EQUAL_F64
: /* Unordered. */
1281 case SLJIT_LESS_EQUAL
:
1282 case SLJIT_LESS_F64
:
1285 case SLJIT_SIG_LESS
:
1288 case SLJIT_SIG_GREATER_EQUAL
:
1291 case SLJIT_SIG_GREATER
:
1294 case SLJIT_SIG_LESS_EQUAL
:
1297 case SLJIT_OVERFLOW
:
1298 case SLJIT_UNORDERED_F64
:
1301 case SLJIT_NOT_OVERFLOW
:
1302 case SLJIT_ORDERED_F64
:
1306 SLJIT_UNREACHABLE();
1311 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
1313 struct sljit_jump
*jump
;
1316 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
1318 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1320 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1323 if (type
< SLJIT_EQUAL_F64
) {
1324 jump
->flags
|= IS_COND
;
1325 if (((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
) && !(compiler
->delay_slot
& ICC_IS_SET
))
1326 jump
->flags
|= IS_MOVABLE
;
1327 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1328 PTR_FAIL_IF(push_inst(compiler
, BICC
| get_cc(type
^ 1) | 5, UNMOVABLE_INS
));
1330 #error "Implementation required"
1333 else if (type
< SLJIT_JUMP
) {
1334 jump
->flags
|= IS_COND
;
1335 if (((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
) && !(compiler
->delay_slot
& FCC_IS_SET
))
1336 jump
->flags
|= IS_MOVABLE
;
1337 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1338 PTR_FAIL_IF(push_inst(compiler
, FBFCC
| get_cc(type
^ 1) | 5, UNMOVABLE_INS
));
1340 #error "Implementation required"
1343 if ((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
)
1344 jump
->flags
|= IS_MOVABLE
;
1345 if (type
>= SLJIT_FAST_CALL
)
1346 jump
->flags
|= IS_CALL
;
1349 PTR_FAIL_IF(emit_const(compiler
, TMP_REG2
, 0));
1350 PTR_FAIL_IF(push_inst(compiler
, JMPL
| D(type
>= SLJIT_FAST_CALL
? TMP_LINK
: 0) | S1(TMP_REG2
) | IMM(0), UNMOVABLE_INS
));
1351 jump
->addr
= compiler
->size
;
1352 PTR_FAIL_IF(push_inst(compiler
, NOP
, UNMOVABLE_INS
));
1357 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
1359 struct sljit_jump
*jump
= NULL
;
1363 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
1364 ADJUST_LOCAL_OFFSET(src
, srcw
);
1366 if (FAST_IS_REG(src
))
1368 else if (src
& SLJIT_IMM
) {
1369 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1371 set_jump(jump
, compiler
, JUMP_ADDR
);
1372 jump
->u
.target
= srcw
;
1373 if ((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
)
1374 jump
->flags
|= IS_MOVABLE
;
1375 if (type
>= SLJIT_FAST_CALL
)
1376 jump
->flags
|= IS_CALL
;
1378 FAIL_IF(emit_const(compiler
, TMP_REG2
, 0));
1382 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
| LOAD_DATA
, TMP_REG2
, src
, srcw
));
1386 FAIL_IF(push_inst(compiler
, JMPL
| D(type
>= SLJIT_FAST_CALL
? TMP_LINK
: 0) | S1(src_r
) | IMM(0), UNMOVABLE_INS
));
1388 jump
->addr
= compiler
->size
;
1389 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
1392 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
1393 sljit_s32 dst
, sljit_sw dstw
,
1394 sljit_s32 src
, sljit_sw srcw
,
1397 sljit_s32 reg
, flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1400 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, src
, srcw
, type
));
1401 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1403 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1404 op
= GET_OPCODE(op
);
1405 reg
= (op
< SLJIT_ADD
&& FAST_IS_REG(dst
)) ? dst
: TMP_REG2
;
1407 compiler
->cache_arg
= 0;
1408 compiler
->cache_argw
= 0;
1409 if (op
>= SLJIT_ADD
&& (src
& SLJIT_MEM
)) {
1410 ADJUST_LOCAL_OFFSET(src
, srcw
);
1411 FAIL_IF(emit_op_mem2(compiler
, WORD_DATA
| LOAD_DATA
, TMP_REG1
, src
, srcw
, dst
, dstw
));
1417 if (type
< SLJIT_EQUAL_F64
)
1418 FAIL_IF(push_inst(compiler
, BICC
| get_cc(type
) | 3, UNMOVABLE_INS
));
1420 FAIL_IF(push_inst(compiler
, FBFCC
| get_cc(type
) | 3, UNMOVABLE_INS
));
1422 FAIL_IF(push_inst(compiler
, OR
| D(reg
) | S1(0) | IMM(1), UNMOVABLE_INS
));
1423 FAIL_IF(push_inst(compiler
, OR
| D(reg
) | S1(0) | IMM(0), UNMOVABLE_INS
));
1425 if (op
>= SLJIT_ADD
)
1426 return emit_op(compiler
, op
, flags
| CUMULATIVE_OP
| IMM_OP
| ALT_KEEP_CACHE
, dst
, dstw
, src
, srcw
, TMP_REG2
, 0);
1428 return (reg
== TMP_REG2
) ? emit_op_mem(compiler
, WORD_DATA
, TMP_REG2
, dst
, dstw
) : SLJIT_SUCCESS
;
1430 #error "Implementation required"
1434 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
1436 sljit_s32 src
, sljit_sw srcw
)
1439 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
1441 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1442 return sljit_emit_cmov_generic(compiler
, type
, dst_reg
, src
, srcw
);;
1444 #error "Implementation required"
1448 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
1451 struct sljit_const
*const_
;
1454 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
1455 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1457 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
1458 PTR_FAIL_IF(!const_
);
1459 set_const(const_
, compiler
);
1461 reg
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
1463 PTR_FAIL_IF(emit_const(compiler
, reg
, init_value
));
1465 if (dst
& SLJIT_MEM
)
1466 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_DATA
, TMP_REG2
, dst
, dstw
));