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 /* This register is modified by calls, which affects the instruction
94 in the delay slot if it is used as a source register. */
95 #define TMP_LINK (SLJIT_NUMBER_OF_REGISTERS + 5)
97 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
98 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
100 static const sljit_u8 reg_map
[SLJIT_NUMBER_OF_REGISTERS
+ 6] = {
101 0, 8, 9, 10, 11, 29, 28, 27, 23, 22, 21, 20, 19, 18, 17, 16, 26, 25, 24, 14, 1, 12, 13, 15
104 static const sljit_u8 freg_map
[SLJIT_NUMBER_OF_FLOAT_REGISTERS
+ 3] = {
105 0, 0, 2, 4, 6, 8, 10, 12, 14
108 /* --------------------------------------------------------------------- */
109 /* Instrucion forms */
110 /* --------------------------------------------------------------------- */
112 #define D(d) (reg_map[d] << 25)
113 #define FD(d) (freg_map[d] << 25)
114 #define FDN(d) ((freg_map[d] | 0x1) << 25)
115 #define DA(d) ((d) << 25)
116 #define S1(s1) (reg_map[s1] << 14)
117 #define FS1(s1) (freg_map[s1] << 14)
118 #define S1A(s1) ((s1) << 14)
119 #define S2(s2) (reg_map[s2])
120 #define FS2(s2) (freg_map[s2])
121 #define FS2N(s2) (freg_map[s2] | 0x1)
123 #define IMM_ARG 0x2000
124 #define DOP(op) ((op) << 5)
125 #define IMM(imm) (((imm) & 0x1fff) | IMM_ARG)
127 #define DR(dr) (reg_map[dr])
128 #define OPC1(opcode) ((opcode) << 30)
129 #define OPC2(opcode) ((opcode) << 22)
130 #define OPC3(opcode) ((opcode) << 19)
131 #define SET_FLAGS OPC3(0x10)
133 #define ADD (OPC1(0x2) | OPC3(0x00))
134 #define ADDC (OPC1(0x2) | OPC3(0x08))
135 #define AND (OPC1(0x2) | OPC3(0x01))
136 #define ANDN (OPC1(0x2) | OPC3(0x05))
137 #define CALL (OPC1(0x1))
138 #define FABSS (OPC1(0x2) | OPC3(0x34) | DOP(0x09))
139 #define FADDD (OPC1(0x2) | OPC3(0x34) | DOP(0x42))
140 #define FADDS (OPC1(0x2) | OPC3(0x34) | DOP(0x41))
141 #define FCMPD (OPC1(0x2) | OPC3(0x35) | DOP(0x52))
142 #define FCMPS (OPC1(0x2) | OPC3(0x35) | DOP(0x51))
143 #define FDIVD (OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
144 #define FDIVS (OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
145 #define FDTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd2))
146 #define FDTOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc6))
147 #define FITOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc8))
148 #define FITOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc4))
149 #define FMOVS (OPC1(0x2) | OPC3(0x34) | DOP(0x01))
150 #define FMULD (OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
151 #define FMULS (OPC1(0x2) | OPC3(0x34) | DOP(0x49))
152 #define FNEGS (OPC1(0x2) | OPC3(0x34) | DOP(0x05))
153 #define FSTOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc9))
154 #define FSTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd1))
155 #define FSUBD (OPC1(0x2) | OPC3(0x34) | DOP(0x46))
156 #define FSUBS (OPC1(0x2) | OPC3(0x34) | DOP(0x45))
157 #define JMPL (OPC1(0x2) | OPC3(0x38))
158 #define LDD (OPC1(0x3) | OPC3(0x03))
159 #define LDUW (OPC1(0x3) | OPC3(0x00))
160 #define NOP (OPC1(0x0) | OPC2(0x04))
161 #define OR (OPC1(0x2) | OPC3(0x02))
162 #define ORN (OPC1(0x2) | OPC3(0x06))
163 #define RDY (OPC1(0x2) | OPC3(0x28) | S1A(0))
164 #define RESTORE (OPC1(0x2) | OPC3(0x3d))
165 #define SAVE (OPC1(0x2) | OPC3(0x3c))
166 #define SETHI (OPC1(0x0) | OPC2(0x04))
167 #define SLL (OPC1(0x2) | OPC3(0x25))
168 #define SLLX (OPC1(0x2) | OPC3(0x25) | (1 << 12))
169 #define SRA (OPC1(0x2) | OPC3(0x27))
170 #define SRAX (OPC1(0x2) | OPC3(0x27) | (1 << 12))
171 #define SRL (OPC1(0x2) | OPC3(0x26))
172 #define SRLX (OPC1(0x2) | OPC3(0x26) | (1 << 12))
173 #define STDF (OPC1(0x3) | OPC3(0x27))
174 #define STF (OPC1(0x3) | OPC3(0x24))
175 #define STW (OPC1(0x3) | OPC3(0x04))
176 #define SUB (OPC1(0x2) | OPC3(0x04))
177 #define SUBC (OPC1(0x2) | OPC3(0x0c))
178 #define TA (OPC1(0x2) | OPC3(0x3a) | (8 << 25))
179 #define WRY (OPC1(0x2) | OPC3(0x30) | DA(0))
180 #define XOR (OPC1(0x2) | OPC3(0x03))
181 #define XNOR (OPC1(0x2) | OPC3(0x07))
183 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
184 #define MAX_DISP (0x1fffff)
185 #define MIN_DISP (-0x200000)
186 #define DISP_MASK (0x3fffff)
188 #define BICC (OPC1(0x0) | OPC2(0x2))
189 #define FBFCC (OPC1(0x0) | OPC2(0x6))
191 #define SDIV (OPC1(0x2) | OPC3(0x0f))
192 #define SMUL (OPC1(0x2) | OPC3(0x0b))
193 #define UDIV (OPC1(0x2) | OPC3(0x0e))
194 #define UMUL (OPC1(0x2) | OPC3(0x0a))
199 #define SIMM_MAX (0x0fff)
200 #define SIMM_MIN (-0x1000)
202 /* dest_reg is the absolute name of the register
203 Useful for reordering instructions in the delay slot. */
204 static sljit_s32
push_inst(struct sljit_compiler
*compiler
, sljit_ins ins
, sljit_s32 delay_slot
)
207 SLJIT_ASSERT((delay_slot
& DST_INS_MASK
) == UNMOVABLE_INS
208 || (delay_slot
& DST_INS_MASK
) == MOVABLE_INS
209 || (delay_slot
& DST_INS_MASK
) == ((ins
>> 25) & 0x1f));
210 ptr
= (sljit_ins
*)ensure_buf(compiler
, sizeof(sljit_ins
));
214 compiler
->delay_slot
= delay_slot
;
215 return SLJIT_SUCCESS
;
218 static SLJIT_INLINE sljit_ins
* detect_jump_type(struct sljit_jump
*jump
, sljit_ins
*code_ptr
, sljit_ins
*code
, sljit_sw executable_offset
)
221 sljit_uw target_addr
;
223 sljit_ins saved_inst
;
225 if (jump
->flags
& SLJIT_REWRITABLE_JUMP
)
228 if (jump
->flags
& JUMP_ADDR
)
229 target_addr
= jump
->u
.target
;
231 SLJIT_ASSERT(jump
->flags
& JUMP_LABEL
);
232 target_addr
= (sljit_uw
)(code
+ jump
->u
.label
->size
) + (sljit_uw
)executable_offset
;
234 inst
= (sljit_ins
*)jump
->addr
;
236 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
237 if (jump
->flags
& IS_CALL
) {
238 /* Call is always patchable on sparc 32. */
239 jump
->flags
|= PATCH_CALL
;
240 if (jump
->flags
& IS_MOVABLE
) {
243 jump
->addr
-= sizeof(sljit_ins
);
251 /* Both calls and BPr instructions shall not pass this point. */
252 #error "Implementation required"
255 if (jump
->flags
& IS_COND
)
258 diff
= ((sljit_sw
)target_addr
- (sljit_sw
)(inst
- 1) - executable_offset
) >> 2;
260 if (jump
->flags
& IS_MOVABLE
) {
261 if (diff
<= MAX_DISP
&& diff
>= MIN_DISP
) {
262 jump
->flags
|= PATCH_B
;
264 if (jump
->flags
& IS_COND
) {
265 saved_inst
= inst
[0];
266 inst
[0] = inst
[1] ^ (1 << 28);
267 inst
[1] = saved_inst
;
270 inst
[0] = BICC
| DA(0x8);
272 jump
->addr
= (sljit_uw
)inst
;
277 diff
+= sizeof(sljit_ins
);
279 if (diff
<= MAX_DISP
&& diff
>= MIN_DISP
) {
280 jump
->flags
|= PATCH_B
;
281 if (jump
->flags
& IS_COND
)
282 inst
[0] ^= (1 << 28);
284 inst
[0] = BICC
| DA(0x8);
286 jump
->addr
= (sljit_uw
)inst
;
293 SLJIT_API_FUNC_ATTRIBUTE
void* sljit_generate_code(struct sljit_compiler
*compiler
)
295 struct sljit_memory_fragment
*buf
;
302 sljit_sw executable_offset
;
305 struct sljit_label
*label
;
306 struct sljit_jump
*jump
;
307 struct sljit_const
*const_
;
308 struct sljit_put_label
*put_label
;
311 CHECK_PTR(check_sljit_generate_code(compiler
));
312 reverse_buf(compiler
);
314 code
= (sljit_ins
*)SLJIT_MALLOC_EXEC(compiler
->size
* sizeof(sljit_ins
), compiler
->exec_allocator_data
);
315 PTR_FAIL_WITH_EXEC_IF(code
);
321 executable_offset
= SLJIT_EXEC_OFFSET(code
);
323 label
= compiler
->labels
;
324 jump
= compiler
->jumps
;
325 const_
= compiler
->consts
;
326 put_label
= compiler
->put_labels
;
329 buf_ptr
= (sljit_ins
*)buf
->memory
;
330 buf_end
= buf_ptr
+ (buf
->used_size
>> 2);
332 *code_ptr
= *buf_ptr
++;
333 if (next_addr
== word_count
) {
334 SLJIT_ASSERT(!label
|| label
->size
>= word_count
);
335 SLJIT_ASSERT(!jump
|| jump
->addr
>= word_count
);
336 SLJIT_ASSERT(!const_
|| const_
->addr
>= word_count
);
337 SLJIT_ASSERT(!put_label
|| put_label
->addr
>= word_count
);
339 /* These structures are ordered by their address. */
340 if (label
&& label
->size
== word_count
) {
341 /* Just recording the address. */
342 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
343 label
->size
= code_ptr
- code
;
346 if (jump
&& jump
->addr
== word_count
) {
347 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
348 jump
->addr
= (sljit_uw
)(code_ptr
- 3);
350 jump
->addr
= (sljit_uw
)(code_ptr
- 6);
352 code_ptr
= detect_jump_type(jump
, code_ptr
, code
, executable_offset
);
355 if (const_
&& const_
->addr
== word_count
) {
356 /* Just recording the address. */
357 const_
->addr
= (sljit_uw
)code_ptr
;
358 const_
= const_
->next
;
360 if (put_label
&& put_label
->addr
== word_count
) {
361 SLJIT_ASSERT(put_label
->label
);
362 put_label
->addr
= (sljit_uw
)code_ptr
;
363 put_label
= put_label
->next
;
365 next_addr
= compute_next_addr(label
, jump
, const_
, put_label
);
369 } while (buf_ptr
< buf_end
);
374 if (label
&& label
->size
== word_count
) {
375 label
->addr
= (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
376 label
->size
= code_ptr
- code
;
380 SLJIT_ASSERT(!label
);
382 SLJIT_ASSERT(!const_
);
383 SLJIT_ASSERT(!put_label
);
384 SLJIT_ASSERT(code_ptr
- code
<= (sljit_s32
)compiler
->size
);
386 jump
= compiler
->jumps
;
389 addr
= (jump
->flags
& JUMP_LABEL
) ? jump
->u
.label
->addr
: jump
->u
.target
;
390 buf_ptr
= (sljit_ins
*)jump
->addr
;
392 if (jump
->flags
& PATCH_CALL
) {
393 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
394 SLJIT_ASSERT((sljit_sw
)addr
<= 0x1fffffff && (sljit_sw
)addr
>= -0x20000000);
395 buf_ptr
[0] = CALL
| (addr
& 0x3fffffff);
398 if (jump
->flags
& PATCH_B
) {
399 addr
= (sljit_sw
)(addr
- (sljit_uw
)SLJIT_ADD_EXEC_OFFSET(buf_ptr
, executable_offset
)) >> 2;
400 SLJIT_ASSERT((sljit_sw
)addr
<= MAX_DISP
&& (sljit_sw
)addr
>= MIN_DISP
);
401 buf_ptr
[0] = (buf_ptr
[0] & ~DISP_MASK
) | (addr
& DISP_MASK
);
405 /* Set the fields of immediate loads. */
406 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
407 SLJIT_ASSERT(((buf_ptr
[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr
[1] & 0xc1f83fff) == 0x80102000));
408 buf_ptr
[0] |= (addr
>> 10) & 0x3fffff;
409 buf_ptr
[1] |= addr
& 0x3ff;
411 #error "Implementation required"
417 put_label
= compiler
->put_labels
;
419 addr
= put_label
->label
->addr
;
420 buf_ptr
= (sljit_ins
*)put_label
->addr
;
422 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
423 SLJIT_ASSERT(((buf_ptr
[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr
[1] & 0xc1f83fff) == 0x80102000));
424 buf_ptr
[0] |= (addr
>> 10) & 0x3fffff;
425 buf_ptr
[1] |= addr
& 0x3ff;
427 #error "Implementation required"
429 put_label
= put_label
->next
;
432 compiler
->error
= SLJIT_ERR_COMPILED
;
433 compiler
->executable_offset
= executable_offset
;
434 compiler
->executable_size
= (code_ptr
- code
) * sizeof(sljit_ins
);
436 code
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code
, executable_offset
);
437 code_ptr
= (sljit_ins
*)SLJIT_ADD_EXEC_OFFSET(code_ptr
, executable_offset
);
439 SLJIT_CACHE_FLUSH(code
, code_ptr
);
440 SLJIT_UPDATE_WX_FLAGS(code
, code_ptr
, 1);
444 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_has_cpu_feature(sljit_s32 feature_type
)
446 switch (feature_type
) {
448 #ifdef SLJIT_IS_FPU_AVAILABLE
449 return SLJIT_IS_FPU_AVAILABLE
;
451 /* Available by default. */
455 case SLJIT_HAS_ZERO_REGISTER
:
458 #if (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
468 /* --------------------------------------------------------------------- */
470 /* --------------------------------------------------------------------- */
472 /* Creates an index in data_transfer_insts array. */
473 #define LOAD_DATA 0x01
474 #define WORD_DATA 0x00
475 #define BYTE_DATA 0x02
476 #define HALF_DATA 0x04
477 #define INT_DATA 0x06
478 #define SIGNED_DATA 0x08
479 /* Separates integer and floating point registers */
481 #define DOUBLE_DATA 0x10
482 #define SINGLE_DATA 0x12
484 #define MEM_MASK 0x1f
486 #define ARG_TEST 0x00020
487 #define ALT_KEEP_CACHE 0x00040
488 #define CUMULATIVE_OP 0x00080
489 #define IMM_OP 0x00100
490 #define SRC2_IMM 0x00200
492 #define REG_DEST 0x00400
493 #define REG2_SOURCE 0x00800
494 #define SLOW_SRC1 0x01000
495 #define SLOW_SRC2 0x02000
496 #define SLOW_DEST 0x04000
498 /* SET_FLAGS (0x10 << 19) also belong here! */
500 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
501 #include "sljitNativeSPARC_32.c"
503 #include "sljitNativeSPARC_64.c"
506 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_enter(struct sljit_compiler
*compiler
,
507 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
508 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
511 CHECK(check_sljit_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
512 set_emit_enter(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
514 local_size
= (local_size
+ SLJIT_LOCALS_OFFSET
+ 7) & ~0x7;
515 compiler
->local_size
= local_size
;
517 if (local_size
<= SIMM_MAX
) {
518 FAIL_IF(push_inst(compiler
, SAVE
| D(SLJIT_SP
) | S1(SLJIT_SP
) | IMM(-local_size
), UNMOVABLE_INS
));
521 FAIL_IF(load_immediate(compiler
, TMP_REG1
, -local_size
));
522 FAIL_IF(push_inst(compiler
, SAVE
| D(SLJIT_SP
) | S1(SLJIT_SP
) | S2(TMP_REG1
), UNMOVABLE_INS
));
525 /* Arguments are in their appropriate registers. */
527 return SLJIT_SUCCESS
;
530 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_set_context(struct sljit_compiler
*compiler
,
531 sljit_s32 options
, sljit_s32 arg_types
, sljit_s32 scratches
, sljit_s32 saveds
,
532 sljit_s32 fscratches
, sljit_s32 fsaveds
, sljit_s32 local_size
)
535 CHECK(check_sljit_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
));
536 set_set_context(compiler
, options
, arg_types
, scratches
, saveds
, fscratches
, fsaveds
, local_size
);
538 compiler
->local_size
= (local_size
+ SLJIT_LOCALS_OFFSET
+ 7) & ~0x7;
539 return SLJIT_SUCCESS
;
542 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_return(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 src
, sljit_sw srcw
)
545 CHECK(check_sljit_emit_return(compiler
, op
, src
, srcw
));
547 if (op
!= SLJIT_MOV
|| !FAST_IS_REG(src
)) {
548 FAIL_IF(emit_mov_before_return(compiler
, op
, src
, srcw
));
552 FAIL_IF(push_inst(compiler
, JMPL
| D(0) | S1A(31) | IMM(8), UNMOVABLE_INS
));
553 return push_inst(compiler
, RESTORE
| D(SLJIT_R0
) | S1(src
) | S2(0), UNMOVABLE_INS
);
556 /* --------------------------------------------------------------------- */
558 /* --------------------------------------------------------------------- */
560 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
561 #define ARCH_32_64(a, b) a
563 #define ARCH_32_64(a, b) b
566 static const sljit_ins data_transfer_insts
[16 + 4] = {
567 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
568 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
569 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
570 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
571 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
572 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
573 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
574 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
576 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
577 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
578 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
579 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
580 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
581 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
582 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
583 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
585 /* d s */ OPC1(3) | OPC3(0x27),
586 /* d l */ OPC1(3) | OPC3(0x23),
587 /* s s */ OPC1(3) | OPC3(0x24),
588 /* s l */ OPC1(3) | OPC3(0x20),
593 /* Can perform an operation using at most 1 instruction. */
594 static sljit_s32
getput_arg_fast(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
596 SLJIT_ASSERT(arg
& SLJIT_MEM
);
598 if ((!(arg
& OFFS_REG_MASK
) && argw
<= SIMM_MAX
&& argw
>= SIMM_MIN
)
599 || ((arg
& OFFS_REG_MASK
) && (argw
& 0x3) == 0)) {
600 /* Works for both absoulte and relative addresses (immediate case). */
601 if (SLJIT_UNLIKELY(flags
& ARG_TEST
))
603 FAIL_IF(push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
]
604 | ((flags
& MEM_MASK
) <= GPR_REG
? D(reg
) : FD(reg
))
605 | S1(arg
& REG_MASK
) | ((arg
& OFFS_REG_MASK
) ? S2(OFFS_REG(arg
)) : IMM(argw
)),
606 ((flags
& MEM_MASK
) <= GPR_REG
&& (flags
& LOAD_DATA
)) ? DR(reg
) : MOVABLE_INS
));
612 /* See getput_arg below.
613 Note: can_cache is called only for binary operators. Those
614 operators always uses word arguments without write back. */
615 static sljit_s32
can_cache(sljit_s32 arg
, sljit_sw argw
, sljit_s32 next_arg
, sljit_sw next_argw
)
617 SLJIT_ASSERT((arg
& SLJIT_MEM
) && (next_arg
& SLJIT_MEM
));
619 /* Simple operation except for updates. */
620 if (arg
& OFFS_REG_MASK
) {
624 if ((arg
& OFFS_REG_MASK
) == (next_arg
& OFFS_REG_MASK
) && argw
== next_argw
)
629 if (((next_argw
- argw
) <= SIMM_MAX
&& (next_argw
- argw
) >= SIMM_MIN
))
634 /* Emit the necessary instructions. See can_cache above. */
635 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
)
637 sljit_s32 base
, arg2
, delay_slot
;
640 SLJIT_ASSERT(arg
& SLJIT_MEM
);
641 if (!(next_arg
& SLJIT_MEM
)) {
646 base
= arg
& REG_MASK
;
647 if (SLJIT_UNLIKELY(arg
& OFFS_REG_MASK
)) {
650 /* Using the cache. */
651 if (((SLJIT_MEM
| (arg
& OFFS_REG_MASK
)) == compiler
->cache_arg
) && (argw
== compiler
->cache_argw
))
654 if ((arg
& OFFS_REG_MASK
) == (next_arg
& OFFS_REG_MASK
) && argw
== (next_argw
& 0x3)) {
655 compiler
->cache_arg
= SLJIT_MEM
| (arg
& OFFS_REG_MASK
);
656 compiler
->cache_argw
= argw
;
659 else if ((flags
& LOAD_DATA
) && ((flags
& MEM_MASK
) <= GPR_REG
) && reg
!= base
&& reg
!= OFFS_REG(arg
))
661 else /* It must be a mov operation, so tmp1 must be free to use. */
663 FAIL_IF(push_inst(compiler
, SLL_W
| D(arg2
) | S1(OFFS_REG(arg
)) | IMM_ARG
| argw
, DR(arg2
)));
667 /* Using the cache. */
668 if ((compiler
->cache_arg
== SLJIT_MEM
) && (argw
- compiler
->cache_argw
) <= SIMM_MAX
&& (argw
- compiler
->cache_argw
) >= SIMM_MIN
) {
669 if (argw
!= compiler
->cache_argw
) {
670 FAIL_IF(push_inst(compiler
, ADD
| D(TMP_REG3
) | S1(TMP_REG3
) | IMM(argw
- compiler
->cache_argw
), DR(TMP_REG3
)));
671 compiler
->cache_argw
= argw
;
675 if ((next_argw
- argw
) <= SIMM_MAX
&& (next_argw
- argw
) >= SIMM_MIN
) {
676 compiler
->cache_arg
= SLJIT_MEM
;
677 compiler
->cache_argw
= argw
;
680 else if ((flags
& LOAD_DATA
) && ((flags
& MEM_MASK
) <= GPR_REG
) && reg
!= base
)
682 else /* It must be a mov operation, so tmp1 must be free to use. */
684 FAIL_IF(load_immediate(compiler
, arg2
, argw
));
688 dest
= ((flags
& MEM_MASK
) <= GPR_REG
? D(reg
) : FD(reg
));
689 delay_slot
= ((flags
& MEM_MASK
) <= GPR_REG
&& (flags
& LOAD_DATA
)) ? DR(reg
) : MOVABLE_INS
;
691 return push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
] | dest
| S1(arg2
) | IMM(0), delay_slot
);
692 return push_inst(compiler
, data_transfer_insts
[flags
& MEM_MASK
] | dest
| S1(base
) | S2(arg2
), delay_slot
);
695 static SLJIT_INLINE sljit_s32
emit_op_mem(struct sljit_compiler
*compiler
, sljit_s32 flags
, sljit_s32 reg
, sljit_s32 arg
, sljit_sw argw
)
697 if (getput_arg_fast(compiler
, flags
, reg
, arg
, argw
))
698 return compiler
->error
;
699 compiler
->cache_arg
= 0;
700 compiler
->cache_argw
= 0;
701 return getput_arg(compiler
, flags
, reg
, arg
, argw
, 0, 0);
704 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
)
706 if (getput_arg_fast(compiler
, flags
, reg
, arg1
, arg1w
))
707 return compiler
->error
;
708 return getput_arg(compiler
, flags
, reg
, arg1
, arg1w
, arg2
, arg2w
);
711 static sljit_s32
emit_op(struct sljit_compiler
*compiler
, sljit_s32 op
, sljit_s32 flags
,
712 sljit_s32 dst
, sljit_sw dstw
,
713 sljit_s32 src1
, sljit_sw src1w
,
714 sljit_s32 src2
, sljit_sw src2w
)
716 /* arg1 goes to TMP_REG1 or src reg
717 arg2 goes to TMP_REG2, imm or src reg
718 TMP_REG3 can be used for caching
719 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
720 sljit_s32 dst_r
= TMP_REG2
;
723 sljit_s32 sugg_src2_r
= TMP_REG2
;
725 if (!(flags
& ALT_KEEP_CACHE
)) {
726 compiler
->cache_arg
= 0;
727 compiler
->cache_argw
= 0;
730 if (dst
!= SLJIT_UNUSED
) {
731 if (FAST_IS_REG(dst
)) {
734 if (op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
)
737 else if ((dst
& SLJIT_MEM
) && !getput_arg_fast(compiler
, flags
| ARG_TEST
, TMP_REG1
, dst
, dstw
))
741 if (flags
& IMM_OP
) {
742 if ((src2
& SLJIT_IMM
) && src2w
) {
743 if (src2w
<= SIMM_MAX
&& src2w
>= SIMM_MIN
) {
748 if (!(flags
& SRC2_IMM
) && (flags
& CUMULATIVE_OP
) && (src1
& SLJIT_IMM
) && src1w
) {
749 if (src1w
<= SIMM_MAX
&& src1w
>= SIMM_MIN
) {
753 /* And swap arguments. */
757 /* src2w = src2_r unneeded. */
763 if (FAST_IS_REG(src1
))
765 else if (src1
& SLJIT_IMM
) {
767 FAIL_IF(load_immediate(compiler
, TMP_REG1
, src1w
));
774 if (getput_arg_fast(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
))
775 FAIL_IF(compiler
->error
);
782 if (FAST_IS_REG(src2
)) {
784 flags
|= REG2_SOURCE
;
785 if (!(flags
& REG_DEST
) && op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
)
788 else if (src2
& SLJIT_IMM
) {
789 if (!(flags
& SRC2_IMM
)) {
791 FAIL_IF(load_immediate(compiler
, sugg_src2_r
, src2w
));
792 src2_r
= sugg_src2_r
;
796 if ((op
>= SLJIT_MOV
&& op
<= SLJIT_MOV_P
) && (dst
& SLJIT_MEM
))
802 if (getput_arg_fast(compiler
, flags
| LOAD_DATA
, sugg_src2_r
, src2
, src2w
))
803 FAIL_IF(compiler
->error
);
806 src2_r
= sugg_src2_r
;
809 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
810 SLJIT_ASSERT(src2_r
== TMP_REG2
);
811 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
812 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG2
, src2
, src2w
, src1
, src1w
));
813 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
816 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, src2
, src2w
));
817 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG2
, src2
, src2w
, dst
, dstw
));
820 else if (flags
& SLOW_SRC1
)
821 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, TMP_REG1
, src1
, src1w
, dst
, dstw
));
822 else if (flags
& SLOW_SRC2
)
823 FAIL_IF(getput_arg(compiler
, flags
| LOAD_DATA
, sugg_src2_r
, src2
, src2w
, dst
, dstw
));
825 FAIL_IF(emit_single_op(compiler
, op
, flags
, dst_r
, src1_r
, src2_r
));
827 if (dst
& SLJIT_MEM
) {
828 if (!(flags
& SLOW_DEST
)) {
829 getput_arg_fast(compiler
, flags
, dst_r
, dst
, dstw
);
830 return compiler
->error
;
832 return getput_arg(compiler
, flags
, dst_r
, dst
, dstw
, 0, 0);
835 return SLJIT_SUCCESS
;
838 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op0(struct sljit_compiler
*compiler
, sljit_s32 op
)
841 CHECK(check_sljit_emit_op0(compiler
, op
));
845 case SLJIT_BREAKPOINT
:
846 return push_inst(compiler
, TA
, UNMOVABLE_INS
);
848 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
851 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
852 FAIL_IF(push_inst(compiler
, (op
== SLJIT_LMUL_UW
? UMUL
: SMUL
) | D(SLJIT_R0
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R0
)));
853 return push_inst(compiler
, RDY
| D(SLJIT_R1
), DR(SLJIT_R1
));
855 #error "Implementation required"
857 case SLJIT_DIVMOD_UW
:
858 case SLJIT_DIVMOD_SW
:
861 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW
& 0x2) == 0 && SLJIT_DIV_UW
- 0x2 == SLJIT_DIVMOD_UW
, bad_div_opcode_assignments
);
862 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
863 if ((op
| 0x2) == SLJIT_DIV_UW
)
864 FAIL_IF(push_inst(compiler
, WRY
| S1(0), MOVABLE_INS
));
866 FAIL_IF(push_inst(compiler
, SRA
| D(TMP_REG1
) | S1(SLJIT_R0
) | IMM(31), DR(TMP_REG1
)));
867 FAIL_IF(push_inst(compiler
, WRY
| S1(TMP_REG1
), MOVABLE_INS
));
869 if (op
<= SLJIT_DIVMOD_SW
)
870 FAIL_IF(push_inst(compiler
, OR
| D(TMP_REG2
) | S1(0) | S2(SLJIT_R0
), DR(TMP_REG2
)));
871 FAIL_IF(push_inst(compiler
, ((op
| 0x2) == SLJIT_DIV_UW
? UDIV
: SDIV
) | D(SLJIT_R0
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R0
)));
872 if (op
>= SLJIT_DIV_UW
)
873 return SLJIT_SUCCESS
;
874 FAIL_IF(push_inst(compiler
, SMUL
| D(SLJIT_R1
) | S1(SLJIT_R0
) | S2(SLJIT_R1
), DR(SLJIT_R1
)));
875 return push_inst(compiler
, SUB
| D(SLJIT_R1
) | S1(TMP_REG2
) | S2(SLJIT_R1
), DR(SLJIT_R1
));
877 #error "Implementation required"
880 case SLJIT_SKIP_FRAMES_BEFORE_RETURN
:
881 return SLJIT_SUCCESS
;
884 return SLJIT_SUCCESS
;
887 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op1(struct sljit_compiler
*compiler
, sljit_s32 op
,
888 sljit_s32 dst
, sljit_sw dstw
,
889 sljit_s32 src
, sljit_sw srcw
)
891 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
894 CHECK(check_sljit_emit_op1(compiler
, op
, dst
, dstw
, src
, srcw
));
895 ADJUST_LOCAL_OFFSET(dst
, dstw
);
896 ADJUST_LOCAL_OFFSET(src
, srcw
);
902 return emit_op(compiler
, SLJIT_MOV
, flags
| WORD_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
905 return emit_op(compiler
, SLJIT_MOV_U32
, flags
| INT_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
908 return emit_op(compiler
, SLJIT_MOV_S32
, flags
| INT_DATA
| SIGNED_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
911 return emit_op(compiler
, SLJIT_MOV_U8
, flags
| BYTE_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u8
)srcw
: srcw
);
914 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
);
917 return emit_op(compiler
, SLJIT_MOV_U16
, flags
| HALF_DATA
, dst
, dstw
, TMP_REG1
, 0, src
, (src
& SLJIT_IMM
) ? (sljit_u16
)srcw
: srcw
);
920 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
);
924 return emit_op(compiler
, op
, flags
, dst
, dstw
, TMP_REG1
, 0, src
, srcw
);
927 return emit_op(compiler
, SLJIT_SUB
, flags
| IMM_OP
, dst
, dstw
, SLJIT_IMM
, 0, src
, srcw
);
930 return SLJIT_SUCCESS
;
933 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op2(struct sljit_compiler
*compiler
, sljit_s32 op
,
934 sljit_s32 dst
, sljit_sw dstw
,
935 sljit_s32 src1
, sljit_sw src1w
,
936 sljit_s32 src2
, sljit_sw src2w
)
938 sljit_s32 flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
941 CHECK(check_sljit_emit_op2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
942 ADJUST_LOCAL_OFFSET(dst
, dstw
);
943 ADJUST_LOCAL_OFFSET(src1
, src1w
);
944 ADJUST_LOCAL_OFFSET(src2
, src2w
);
946 if (dst
== SLJIT_UNUSED
&& !HAS_FLAGS(op
))
947 return SLJIT_SUCCESS
;
957 return emit_op(compiler
, op
, flags
| CUMULATIVE_OP
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
961 return emit_op(compiler
, op
, flags
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
966 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
967 if (src2
& SLJIT_IMM
)
972 return emit_op(compiler
, op
, flags
| IMM_OP
, dst
, dstw
, src1
, src1w
, src2
, src2w
);
975 return SLJIT_SUCCESS
;
978 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_src(struct sljit_compiler
*compiler
, sljit_s32 op
,
979 sljit_s32 src
, sljit_sw srcw
)
982 CHECK(check_sljit_emit_op_src(compiler
, op
, src
, srcw
));
983 ADJUST_LOCAL_OFFSET(src
, srcw
);
986 case SLJIT_FAST_RETURN
:
987 if (FAST_IS_REG(src
))
988 FAIL_IF(push_inst(compiler
, OR
| D(TMP_LINK
) | S1(0) | S2(src
), DR(TMP_LINK
)));
990 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
| LOAD_DATA
, TMP_LINK
, src
, srcw
));
992 FAIL_IF(push_inst(compiler
, JMPL
| D(0) | S1(TMP_LINK
) | IMM(8), UNMOVABLE_INS
));
993 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
994 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN
:
995 case SLJIT_PREFETCH_L1
:
996 case SLJIT_PREFETCH_L2
:
997 case SLJIT_PREFETCH_L3
:
998 case SLJIT_PREFETCH_ONCE
:
999 return SLJIT_SUCCESS
;
1002 return SLJIT_SUCCESS
;
1005 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_register_index(sljit_s32 reg
)
1007 CHECK_REG_INDEX(check_sljit_get_register_index(reg
));
1008 return reg_map
[reg
];
1011 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_get_float_register_index(sljit_s32 reg
)
1013 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg
));
1014 return freg_map
[reg
];
1017 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_custom(struct sljit_compiler
*compiler
,
1018 void *instruction
, sljit_s32 size
)
1021 CHECK(check_sljit_emit_op_custom(compiler
, instruction
, size
));
1023 return push_inst(compiler
, *(sljit_ins
*)instruction
, UNMOVABLE_INS
);
1026 /* --------------------------------------------------------------------- */
1027 /* Floating point operators */
1028 /* --------------------------------------------------------------------- */
1030 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
1031 #define SELECT_FOP(op, single, double) ((op & SLJIT_F32_OP) ? single : double)
1032 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
1034 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler
*compiler
, sljit_s32 op
,
1035 sljit_s32 dst
, sljit_sw dstw
,
1036 sljit_s32 src
, sljit_sw srcw
)
1038 if (src
& SLJIT_MEM
) {
1039 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src
, srcw
, dst
, dstw
));
1043 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSTOI
, FDTOI
) | FD(TMP_FREG1
) | FS2(src
), MOVABLE_INS
));
1045 if (FAST_IS_REG(dst
)) {
1046 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
));
1047 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
);
1050 /* Store the integer value from a VFP register. */
1051 return emit_op_mem2(compiler
, SINGLE_DATA
, TMP_FREG1
, dst
, dstw
, 0, 0);
1054 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler
*compiler
, sljit_s32 op
,
1055 sljit_s32 dst
, sljit_sw dstw
,
1056 sljit_s32 src
, sljit_sw srcw
)
1058 sljit_s32 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1060 if (src
& SLJIT_IMM
) {
1061 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1062 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_S32
)
1063 srcw
= (sljit_s32
)srcw
;
1065 FAIL_IF(load_immediate(compiler
, TMP_REG1
, srcw
));
1070 if (FAST_IS_REG(src
)) {
1071 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
));
1072 src
= SLJIT_MEM1(SLJIT_SP
);
1073 srcw
= FLOAT_TMP_MEM_OFFSET
;
1076 FAIL_IF(emit_op_mem2(compiler
, SINGLE_DATA
| LOAD_DATA
, TMP_FREG1
, src
, srcw
, dst
, dstw
));
1077 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FITOS
, FITOD
) | FD(dst_r
) | FS2(TMP_FREG1
), MOVABLE_INS
));
1079 if (dst
& SLJIT_MEM
)
1080 return emit_op_mem2(compiler
, FLOAT_DATA(op
), TMP_FREG1
, dst
, dstw
, 0, 0);
1081 return SLJIT_SUCCESS
;
1084 static SLJIT_INLINE sljit_s32
sljit_emit_fop1_cmp(struct sljit_compiler
*compiler
, sljit_s32 op
,
1085 sljit_s32 src1
, sljit_sw src1w
,
1086 sljit_s32 src2
, sljit_sw src2w
)
1088 if (src1
& SLJIT_MEM
) {
1089 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, src2
, src2w
));
1093 if (src2
& SLJIT_MEM
) {
1094 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, 0, 0));
1098 return push_inst(compiler
, SELECT_FOP(op
, FCMPS
, FCMPD
) | FS1(src1
) | FS2(src2
), FCC_IS_SET
| MOVABLE_INS
);
1101 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop1(struct sljit_compiler
*compiler
, sljit_s32 op
,
1102 sljit_s32 dst
, sljit_sw dstw
,
1103 sljit_s32 src
, sljit_sw srcw
)
1108 compiler
->cache_arg
= 0;
1109 compiler
->cache_argw
= 0;
1111 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP
== 0x100) && !(DOUBLE_DATA
& 0x2), float_transfer_bit_error
);
1112 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler
, op
, dst
, dstw
, src
, srcw
);
1114 if (GET_OPCODE(op
) == SLJIT_CONV_F64_FROM_F32
)
1117 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG1
;
1119 if (src
& SLJIT_MEM
) {
1120 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, dst_r
, src
, srcw
, dst
, dstw
));
1124 switch (GET_OPCODE(op
)) {
1127 if (dst_r
!= TMP_FREG1
) {
1128 FAIL_IF(push_inst(compiler
, FMOVS
| FD(dst_r
) | FS2(src
), MOVABLE_INS
));
1129 if (!(op
& SLJIT_F32_OP
))
1130 FAIL_IF(push_inst(compiler
, FMOVS
| FDN(dst_r
) | FS2N(src
), MOVABLE_INS
));
1137 FAIL_IF(push_inst(compiler
, FNEGS
| FD(dst_r
) | FS2(src
), MOVABLE_INS
));
1138 if (dst_r
!= src
&& !(op
& SLJIT_F32_OP
))
1139 FAIL_IF(push_inst(compiler
, FMOVS
| FDN(dst_r
) | FS2N(src
), MOVABLE_INS
));
1142 FAIL_IF(push_inst(compiler
, FABSS
| FD(dst_r
) | FS2(src
), MOVABLE_INS
));
1143 if (dst_r
!= src
&& !(op
& SLJIT_F32_OP
))
1144 FAIL_IF(push_inst(compiler
, FMOVS
| FDN(dst_r
) | FS2N(src
), MOVABLE_INS
));
1146 case SLJIT_CONV_F64_FROM_F32
:
1147 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSTOD
, FDTOS
) | FD(dst_r
) | FS2(src
), MOVABLE_INS
));
1152 if (dst
& SLJIT_MEM
)
1153 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
), dst_r
, dst
, dstw
, 0, 0));
1154 return SLJIT_SUCCESS
;
1157 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fop2(struct sljit_compiler
*compiler
, sljit_s32 op
,
1158 sljit_s32 dst
, sljit_sw dstw
,
1159 sljit_s32 src1
, sljit_sw src1w
,
1160 sljit_s32 src2
, sljit_sw src2w
)
1162 sljit_s32 dst_r
, flags
= 0;
1165 CHECK(check_sljit_emit_fop2(compiler
, op
, dst
, dstw
, src1
, src1w
, src2
, src2w
));
1166 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1167 ADJUST_LOCAL_OFFSET(src1
, src1w
);
1168 ADJUST_LOCAL_OFFSET(src2
, src2w
);
1170 compiler
->cache_arg
= 0;
1171 compiler
->cache_argw
= 0;
1173 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_FREG2
;
1175 if (src1
& SLJIT_MEM
) {
1176 if (getput_arg_fast(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
)) {
1177 FAIL_IF(compiler
->error
);
1183 if (src2
& SLJIT_MEM
) {
1184 if (getput_arg_fast(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
)) {
1185 FAIL_IF(compiler
->error
);
1191 if ((flags
& (SLOW_SRC1
| SLOW_SRC2
)) == (SLOW_SRC1
| SLOW_SRC2
)) {
1192 if (!can_cache(src1
, src1w
, src2
, src2w
) && can_cache(src1
, src1w
, dst
, dstw
)) {
1193 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, src1
, src1w
));
1194 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, dst
, dstw
));
1197 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, src2
, src2w
));
1198 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, dst
, dstw
));
1201 else if (flags
& SLOW_SRC1
)
1202 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG1
, src1
, src1w
, dst
, dstw
));
1203 else if (flags
& SLOW_SRC2
)
1204 FAIL_IF(getput_arg(compiler
, FLOAT_DATA(op
) | LOAD_DATA
, TMP_FREG2
, src2
, src2w
, dst
, dstw
));
1206 if (flags
& SLOW_SRC1
)
1208 if (flags
& SLOW_SRC2
)
1211 switch (GET_OPCODE(op
)) {
1213 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FADDS
, FADDD
) | FD(dst_r
) | FS1(src1
) | FS2(src2
), MOVABLE_INS
));
1217 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FSUBS
, FSUBD
) | FD(dst_r
) | FS1(src1
) | FS2(src2
), MOVABLE_INS
));
1221 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FMULS
, FMULD
) | FD(dst_r
) | FS1(src1
) | FS2(src2
), MOVABLE_INS
));
1225 FAIL_IF(push_inst(compiler
, SELECT_FOP(op
, FDIVS
, FDIVD
) | FD(dst_r
) | FS1(src1
) | FS2(src2
), MOVABLE_INS
));
1229 if (dst_r
== TMP_FREG2
)
1230 FAIL_IF(emit_op_mem2(compiler
, FLOAT_DATA(op
), TMP_FREG2
, dst
, dstw
, 0, 0));
1232 return SLJIT_SUCCESS
;
1238 /* --------------------------------------------------------------------- */
1239 /* Other instructions */
1240 /* --------------------------------------------------------------------- */
1242 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_fast_enter(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1245 CHECK(check_sljit_emit_fast_enter(compiler
, dst
, dstw
));
1246 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1248 if (FAST_IS_REG(dst
))
1249 return push_inst(compiler
, OR
| D(dst
) | S1(0) | S2(TMP_LINK
), UNMOVABLE_INS
);
1252 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
, TMP_LINK
, dst
, dstw
));
1253 compiler
->delay_slot
= UNMOVABLE_INS
;
1254 return SLJIT_SUCCESS
;
1257 /* --------------------------------------------------------------------- */
1258 /* Conditional instructions */
1259 /* --------------------------------------------------------------------- */
1261 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_label
* sljit_emit_label(struct sljit_compiler
*compiler
)
1263 struct sljit_label
*label
;
1266 CHECK_PTR(check_sljit_emit_label(compiler
));
1268 if (compiler
->last_label
&& compiler
->last_label
->size
== compiler
->size
)
1269 return compiler
->last_label
;
1271 label
= (struct sljit_label
*)ensure_abuf(compiler
, sizeof(struct sljit_label
));
1272 PTR_FAIL_IF(!label
);
1273 set_label(label
, compiler
);
1274 compiler
->delay_slot
= UNMOVABLE_INS
;
1278 static sljit_ins
get_cc(struct sljit_compiler
*compiler
, sljit_s32 type
)
1282 case SLJIT_NOT_EQUAL_F64
: /* Unordered. */
1285 case SLJIT_NOT_EQUAL
:
1286 case SLJIT_EQUAL_F64
:
1290 case SLJIT_GREATER_F64
: /* Unordered. */
1293 case SLJIT_GREATER_EQUAL
:
1294 case SLJIT_LESS_EQUAL_F64
:
1298 case SLJIT_GREATER_EQUAL_F64
: /* Unordered. */
1301 case SLJIT_LESS_EQUAL
:
1302 case SLJIT_LESS_F64
:
1305 case SLJIT_SIG_LESS
:
1308 case SLJIT_SIG_GREATER_EQUAL
:
1311 case SLJIT_SIG_GREATER
:
1314 case SLJIT_SIG_LESS_EQUAL
:
1317 case SLJIT_OVERFLOW
:
1318 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
1321 case SLJIT_UNORDERED_F64
:
1324 case SLJIT_NOT_OVERFLOW
:
1325 if (!(compiler
->status_flags_state
& SLJIT_CURRENT_FLAGS_ADD_SUB
))
1328 case SLJIT_ORDERED_F64
:
1332 SLJIT_UNREACHABLE();
1337 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_jump(struct sljit_compiler
*compiler
, sljit_s32 type
)
1339 struct sljit_jump
*jump
;
1342 CHECK_PTR(check_sljit_emit_jump(compiler
, type
));
1344 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1346 set_jump(jump
, compiler
, type
& SLJIT_REWRITABLE_JUMP
);
1349 if (type
< SLJIT_EQUAL_F64
) {
1350 jump
->flags
|= IS_COND
;
1351 if (((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
) && !(compiler
->delay_slot
& ICC_IS_SET
))
1352 jump
->flags
|= IS_MOVABLE
;
1353 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1354 PTR_FAIL_IF(push_inst(compiler
, BICC
| get_cc(compiler
, type
^ 1) | 5, UNMOVABLE_INS
));
1356 #error "Implementation required"
1359 else if (type
< SLJIT_JUMP
) {
1360 jump
->flags
|= IS_COND
;
1361 if (((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
) && !(compiler
->delay_slot
& FCC_IS_SET
))
1362 jump
->flags
|= IS_MOVABLE
;
1363 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1364 PTR_FAIL_IF(push_inst(compiler
, FBFCC
| get_cc(compiler
, type
^ 1) | 5, UNMOVABLE_INS
));
1366 #error "Implementation required"
1370 if ((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
)
1371 jump
->flags
|= IS_MOVABLE
;
1372 if (type
>= SLJIT_FAST_CALL
)
1373 jump
->flags
|= IS_CALL
;
1376 PTR_FAIL_IF(emit_const(compiler
, TMP_REG1
, 0));
1377 PTR_FAIL_IF(push_inst(compiler
, JMPL
| D(type
>= SLJIT_FAST_CALL
? TMP_LINK
: 0) | S1(TMP_REG1
) | IMM(0), UNMOVABLE_INS
));
1378 jump
->addr
= compiler
->size
;
1379 PTR_FAIL_IF(push_inst(compiler
, NOP
, UNMOVABLE_INS
));
1384 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_jump
* sljit_emit_call(struct sljit_compiler
*compiler
, sljit_s32 type
,
1385 sljit_s32 arg_types
)
1388 CHECK_PTR(check_sljit_emit_call(compiler
, type
, arg_types
));
1390 PTR_FAIL_IF(call_with_args(compiler
, arg_types
, NULL
));
1392 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1393 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1394 compiler
->skip_checks
= 1;
1397 return sljit_emit_jump(compiler
, type
);
1400 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_ijump(struct sljit_compiler
*compiler
, sljit_s32 type
, sljit_s32 src
, sljit_sw srcw
)
1402 struct sljit_jump
*jump
= NULL
;
1406 CHECK(check_sljit_emit_ijump(compiler
, type
, src
, srcw
));
1407 ADJUST_LOCAL_OFFSET(src
, srcw
);
1409 if (FAST_IS_REG(src
))
1411 else if (src
& SLJIT_IMM
) {
1412 jump
= (struct sljit_jump
*)ensure_abuf(compiler
, sizeof(struct sljit_jump
));
1414 set_jump(jump
, compiler
, JUMP_ADDR
);
1415 jump
->u
.target
= srcw
;
1417 if ((compiler
->delay_slot
& DST_INS_MASK
) != UNMOVABLE_INS
)
1418 jump
->flags
|= IS_MOVABLE
;
1419 if (type
>= SLJIT_FAST_CALL
)
1420 jump
->flags
|= IS_CALL
;
1422 FAIL_IF(emit_const(compiler
, TMP_REG1
, 0));
1426 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
| LOAD_DATA
, TMP_REG1
, src
, srcw
));
1430 FAIL_IF(push_inst(compiler
, JMPL
| D(type
>= SLJIT_FAST_CALL
? TMP_LINK
: 0) | S1(src_r
) | IMM(0), UNMOVABLE_INS
));
1432 jump
->addr
= compiler
->size
;
1433 return push_inst(compiler
, NOP
, UNMOVABLE_INS
);
1436 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_icall(struct sljit_compiler
*compiler
, sljit_s32 type
,
1437 sljit_s32 arg_types
,
1438 sljit_s32 src
, sljit_sw srcw
)
1441 CHECK(check_sljit_emit_icall(compiler
, type
, arg_types
, src
, srcw
));
1443 if (src
& SLJIT_MEM
) {
1444 ADJUST_LOCAL_OFFSET(src
, srcw
);
1445 FAIL_IF(emit_op_mem(compiler
, WORD_DATA
| LOAD_DATA
, TMP_REG1
, src
, srcw
));
1449 FAIL_IF(call_with_args(compiler
, arg_types
, &src
));
1451 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1452 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1453 compiler
->skip_checks
= 1;
1456 return sljit_emit_ijump(compiler
, type
, src
, srcw
);
1459 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_op_flags(struct sljit_compiler
*compiler
, sljit_s32 op
,
1460 sljit_s32 dst
, sljit_sw dstw
,
1463 sljit_s32 reg
, flags
= HAS_FLAGS(op
) ? SET_FLAGS
: 0;
1466 CHECK(check_sljit_emit_op_flags(compiler
, op
, dst
, dstw
, type
));
1467 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1469 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1470 op
= GET_OPCODE(op
);
1471 reg
= (op
< SLJIT_ADD
&& FAST_IS_REG(dst
)) ? dst
: TMP_REG2
;
1473 compiler
->cache_arg
= 0;
1474 compiler
->cache_argw
= 0;
1476 if (op
>= SLJIT_ADD
&& (dst
& SLJIT_MEM
))
1477 FAIL_IF(emit_op_mem2(compiler
, WORD_DATA
| LOAD_DATA
, TMP_REG1
, dst
, dstw
, dst
, dstw
));
1480 if (type
< SLJIT_EQUAL_F64
)
1481 FAIL_IF(push_inst(compiler
, BICC
| get_cc(compiler
, type
) | 3, UNMOVABLE_INS
));
1483 FAIL_IF(push_inst(compiler
, FBFCC
| get_cc(compiler
, type
) | 3, UNMOVABLE_INS
));
1485 FAIL_IF(push_inst(compiler
, OR
| D(reg
) | S1(0) | IMM(1), UNMOVABLE_INS
));
1486 FAIL_IF(push_inst(compiler
, OR
| D(reg
) | S1(0) | IMM(0), UNMOVABLE_INS
));
1488 if (op
>= SLJIT_ADD
) {
1489 flags
|= CUMULATIVE_OP
| IMM_OP
| ALT_KEEP_CACHE
;
1490 if (dst
& SLJIT_MEM
)
1491 return emit_op(compiler
, op
, flags
, dst
, dstw
, TMP_REG1
, 0, TMP_REG2
, 0);
1492 return emit_op(compiler
, op
, flags
, dst
, 0, dst
, 0, TMP_REG2
, 0);
1495 if (!(dst
& SLJIT_MEM
))
1496 return SLJIT_SUCCESS
;
1498 return emit_op_mem(compiler
, WORD_DATA
, TMP_REG2
, dst
, dstw
);
1500 #error "Implementation required"
1504 SLJIT_API_FUNC_ATTRIBUTE sljit_s32
sljit_emit_cmov(struct sljit_compiler
*compiler
, sljit_s32 type
,
1506 sljit_s32 src
, sljit_sw srcw
)
1509 CHECK(check_sljit_emit_cmov(compiler
, type
, dst_reg
, src
, srcw
));
1511 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1512 return sljit_emit_cmov_generic(compiler
, type
, dst_reg
, src
, srcw
);;
1514 #error "Implementation required"
1518 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_const
* sljit_emit_const(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
, sljit_sw init_value
)
1520 struct sljit_const
*const_
;
1524 CHECK_PTR(check_sljit_emit_const(compiler
, dst
, dstw
, init_value
));
1525 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1527 const_
= (struct sljit_const
*)ensure_abuf(compiler
, sizeof(struct sljit_const
));
1528 PTR_FAIL_IF(!const_
);
1529 set_const(const_
, compiler
);
1531 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
1532 PTR_FAIL_IF(emit_const(compiler
, dst_r
, init_value
));
1534 if (dst
& SLJIT_MEM
)
1535 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_DATA
, TMP_REG2
, dst
, dstw
));
1539 SLJIT_API_FUNC_ATTRIBUTE
struct sljit_put_label
* sljit_emit_put_label(struct sljit_compiler
*compiler
, sljit_s32 dst
, sljit_sw dstw
)
1541 struct sljit_put_label
*put_label
;
1545 CHECK_PTR(check_sljit_emit_put_label(compiler
, dst
, dstw
));
1546 ADJUST_LOCAL_OFFSET(dst
, dstw
);
1548 put_label
= (struct sljit_put_label
*)ensure_abuf(compiler
, sizeof(struct sljit_put_label
));
1549 PTR_FAIL_IF(!put_label
);
1550 set_put_label(put_label
, compiler
, 0);
1552 dst_r
= FAST_IS_REG(dst
) ? dst
: TMP_REG2
;
1553 PTR_FAIL_IF(emit_const(compiler
, dst_r
, 0));
1555 if (dst
& SLJIT_MEM
)
1556 PTR_FAIL_IF(emit_op_mem(compiler
, WORD_DATA
, TMP_REG2
, dst
, dstw
));