Support preload on ARM.
[sljit.git] / sljit_src / sljitNativeSPARC_common.c
blobc35eb1e189702582befa8fe66032b0a2b8fa50f6
1 /*
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
41 __asm (
42 /* if (from == to) return */
43 "cmp %i0, %i1\n"
44 "be .leave\n"
45 "nop\n"
47 /* loop until from >= to */
48 ".mainloop:\n"
49 "flush %i0\n"
50 "add %i0, 8, %i0\n"
51 "cmp %i0, %i1\n"
52 "bcs .mainloop\n"
53 "nop\n"
55 /* The comparison was done above. */
56 "bne .leave\n"
57 /* nop is not necessary here, since the
58 sub operation has no side effect. */
59 "sub %i0, 4, %i0\n"
60 "flush %i0\n"
61 ".leave:"
63 #else
64 if (SLJIT_UNLIKELY(from == to))
65 return;
67 do {
68 __asm__ volatile (
69 "flush %0\n"
70 : : "r"(from)
72 /* Operates at least on doubleword. */
73 from += 2;
74 } while (from < to);
76 if (from == to) {
77 /* Flush the last word. */
78 from --;
79 __asm__ volatile (
80 "flush %0\n"
81 : : "r"(from)
84 #endif
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)
95 #define TMP_FREG1 (0)
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)
111 #define S2A(s2) (s2)
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))
174 #define SLL_W SLL
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))
179 #else
180 #define SLL_W SLLX
181 #endif
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)
190 sljit_ins *ptr;
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));
195 FAIL_IF(!ptr);
196 *ptr = ins;
197 compiler->size++;
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)
204 sljit_sw diff;
205 sljit_uw target_addr;
206 sljit_ins *inst;
207 sljit_ins saved_inst;
209 if (jump->flags & SLJIT_REWRITABLE_JUMP)
210 return code_ptr;
212 if (jump->flags & JUMP_ADDR)
213 target_addr = jump->u.target;
214 else {
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) {
225 inst[0] = inst[-1];
226 inst[-1] = CALL;
227 jump->addr -= sizeof(sljit_ins);
228 return inst;
230 inst[0] = CALL;
231 inst[1] = NOP;
232 return inst + 1;
234 #else
235 /* Both calls and BPr instructions shall not pass this point. */
236 #error "Implementation required"
237 #endif
239 if (jump->flags & IS_COND)
240 inst--;
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;
247 inst--;
248 if (jump->flags & IS_COND) {
249 saved_inst = inst[0];
250 inst[0] = inst[1] ^ (1 << 28);
251 inst[1] = saved_inst;
252 } else {
253 inst[1] = inst[0];
254 inst[0] = BICC | DA(0x8);
256 jump->addr = (sljit_uw)inst;
257 return inst + 1;
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);
267 else
268 inst[0] = BICC | DA(0x8);
269 inst[1] = NOP;
270 jump->addr = (sljit_uw)inst;
271 return inst + 1;
274 return code_ptr;
277 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
279 struct sljit_memory_fragment *buf;
280 sljit_ins *code;
281 sljit_ins *code_ptr;
282 sljit_ins *buf_ptr;
283 sljit_ins *buf_end;
284 sljit_uw word_count;
285 sljit_sw executable_offset;
286 sljit_uw addr;
288 struct sljit_label *label;
289 struct sljit_jump *jump;
290 struct sljit_const *const_;
292 CHECK_ERROR_PTR();
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);
298 buf = compiler->buf;
300 code_ptr = code;
301 word_count = 0;
302 executable_offset = SLJIT_EXEC_OFFSET(code);
304 label = compiler->labels;
305 jump = compiler->jumps;
306 const_ = compiler->consts;
308 do {
309 buf_ptr = (sljit_ins*)buf->memory;
310 buf_end = buf_ptr + (buf->used_size >> 2);
311 do {
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;
321 label = label->next;
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);
326 #else
327 jump->addr = (sljit_uw)(code_ptr - 6);
328 #endif
329 code_ptr = detect_jump_type(jump, code_ptr, code, executable_offset);
330 jump = jump->next;
332 if (const_ && const_->addr == word_count) {
333 /* Just recording the address. */
334 const_->addr = (sljit_uw)code_ptr;
335 const_ = const_->next;
337 code_ptr ++;
338 word_count ++;
339 } while (buf_ptr < buf_end);
341 buf = buf->next;
342 } while (buf);
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;
347 label = label->next;
350 SLJIT_ASSERT(!label);
351 SLJIT_ASSERT(!jump);
352 SLJIT_ASSERT(!const_);
353 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)compiler->size);
355 jump = compiler->jumps;
356 while (jump) {
357 do {
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);
365 break;
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);
371 break;
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);
378 #else
379 #error "Implementation required"
380 #endif
381 } while (0);
382 jump = jump->next;
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);
394 return code;
397 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
399 switch (feature_type) {
400 case SLJIT_HAS_FPU:
401 #ifdef SLJIT_IS_FPU_AVAILABLE
402 return SLJIT_IS_FPU_AVAILABLE;
403 #else
404 /* Available by default. */
405 return 1;
406 #endif
408 #if (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
409 case SLJIT_HAS_CMOV:
410 return 1;
411 #endif
413 default:
414 return 0;
418 /* --------------------------------------------------------------------- */
419 /* Entry, exit */
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 */
430 #define GPR_REG 0x0f
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"
453 #else
454 #include "sljitNativeSPARC_64.c"
455 #endif
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)
461 CHECK_ERROR();
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));
471 else {
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)
485 CHECK_ERROR();
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)
495 CHECK_ERROR();
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));
500 src = SLJIT_R0;
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 /* --------------------------------------------------------------------- */
508 /* Operators */
509 /* --------------------------------------------------------------------- */
511 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
512 #define ARCH_32_64(a, b) a
513 #else
514 #define ARCH_32_64(a, b) b
515 #endif
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),
542 #undef ARCH_32_64
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))
554 return 1;
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));
559 return -1;
562 return 0;
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) {
574 argw &= 0x3;
575 SLJIT_ASSERT(argw);
576 next_argw &= 0x3;
577 if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == next_argw)
578 return 1;
579 return 0;
582 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
583 return 1;
584 return 0;
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;
591 sljit_ins dest;
593 SLJIT_ASSERT(arg & SLJIT_MEM);
594 if (!(next_arg & SLJIT_MEM)) {
595 next_arg = 0;
596 next_argw = 0;
599 base = arg & REG_MASK;
600 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
601 argw &= 0x3;
603 /* Using the cache. */
604 if (((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) && (argw == compiler->cache_argw))
605 arg2 = TMP_REG3;
606 else {
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;
610 arg2 = TMP_REG3;
612 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && reg != OFFS_REG(arg))
613 arg2 = reg;
614 else /* It must be a mov operation, so tmp1 must be free to use. */
615 arg2 = TMP_REG1;
616 FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1(OFFS_REG(arg)) | IMM_ARG | argw, DR(arg2)));
619 else {
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;
626 arg2 = TMP_REG3;
627 } else {
628 if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
629 compiler->cache_arg = SLJIT_MEM;
630 compiler->cache_argw = argw;
631 arg2 = TMP_REG3;
633 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
634 arg2 = reg;
635 else /* It must be a mov operation, so tmp1 must be free to use. */
636 arg2 = TMP_REG1;
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;
643 if (!base)
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;
677 sljit_s32 src1_r;
678 sljit_sw src2_r = 0;
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)) {
688 dst_r = dst;
689 flags |= REG_DEST;
690 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
691 sugg_src2_r = dst_r;
693 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
694 flags |= SLOW_DEST;
697 if (flags & IMM_OP) {
698 if ((src2 & SLJIT_IMM) && src2w) {
699 if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
700 flags |= SRC2_IMM;
701 src2_r = src2w;
704 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
705 if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
706 flags |= SRC2_IMM;
707 src2_r = src1w;
709 /* And swap arguments. */
710 src1 = src2;
711 src1w = src2w;
712 src2 = SLJIT_IMM;
713 /* src2w = src2_r unneeded. */
718 /* Source 1. */
719 if (FAST_IS_REG(src1))
720 src1_r = src1;
721 else if (src1 & SLJIT_IMM) {
722 if (src1w) {
723 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
724 src1_r = TMP_REG1;
726 else
727 src1_r = 0;
729 else {
730 if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
731 FAIL_IF(compiler->error);
732 else
733 flags |= SLOW_SRC1;
734 src1_r = TMP_REG1;
737 /* Source 2. */
738 if (FAST_IS_REG(src2)) {
739 src2_r = src2;
740 flags |= REG2_SOURCE;
741 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
742 dst_r = src2_r;
744 else if (src2 & SLJIT_IMM) {
745 if (!(flags & SRC2_IMM)) {
746 if (src2w) {
747 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
748 src2_r = sugg_src2_r;
750 else {
751 src2_r = 0;
752 if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_S32) && (dst & SLJIT_MEM))
753 dst_r = 0;
757 else {
758 if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
759 FAIL_IF(compiler->error);
760 else
761 flags |= SLOW_SRC2;
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));
771 else {
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)
796 CHECK_ERROR();
797 CHECK(check_sljit_emit_op0(compiler, op));
799 op = GET_OPCODE(op);
800 switch (op) {
801 case SLJIT_BREAKPOINT:
802 return push_inst(compiler, TA, UNMOVABLE_INS);
803 case SLJIT_NOP:
804 return push_inst(compiler, NOP, UNMOVABLE_INS);
805 case SLJIT_LMUL_UW:
806 case SLJIT_LMUL_SW:
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));
810 #else
811 #error "Implementation required"
812 #endif
813 case SLJIT_DIVMOD_UW:
814 case SLJIT_DIVMOD_SW:
815 case SLJIT_DIV_UW:
816 case SLJIT_DIV_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));
821 else {
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));
832 #else
833 #error "Implementation required"
834 #endif
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;
846 CHECK_ERROR();
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;
854 op = GET_OPCODE(op);
855 switch (op) {
856 case SLJIT_MOV:
857 case SLJIT_MOV_P:
858 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
860 case SLJIT_MOV_U32:
861 return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
863 case SLJIT_MOV_S32:
864 return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
866 case SLJIT_MOV_U8:
867 return emit_op(compiler, SLJIT_MOV_U8, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
869 case SLJIT_MOV_S8:
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);
872 case SLJIT_MOV_U16:
873 return emit_op(compiler, SLJIT_MOV_U16, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
875 case SLJIT_MOV_S16:
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);
878 case SLJIT_MOVU:
879 case SLJIT_MOVU_P:
880 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
882 case SLJIT_MOVU_U32:
883 return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
885 case SLJIT_MOVU_S32:
886 return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
888 case SLJIT_MOVU_U8:
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);
891 case SLJIT_MOVU_S8:
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);
894 case SLJIT_MOVU_U16:
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);
897 case SLJIT_MOVU_S16:
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);
900 case SLJIT_NOT:
901 case SLJIT_CLZ:
902 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
904 case SLJIT_NEG:
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;
918 CHECK_ERROR();
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;
927 op = GET_OPCODE(op);
928 switch (op) {
929 case SLJIT_ADD:
930 case SLJIT_ADDC:
931 case SLJIT_MUL:
932 case SLJIT_AND:
933 case SLJIT_OR:
934 case SLJIT_XOR:
935 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
937 case SLJIT_SUB:
938 case SLJIT_SUBC:
939 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
941 case SLJIT_SHL:
942 case SLJIT_LSHR:
943 case SLJIT_ASHR:
944 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
945 if (src2 & SLJIT_IMM)
946 src2w &= 0x1f;
947 #else
948 SLJIT_UNREACHABLE();
949 #endif
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));
959 return reg_map[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));
965 return reg << 1;
968 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
969 void *instruction, sljit_s32 size)
971 CHECK_ERROR();
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));
991 src = TMP_FREG1;
993 else
994 src <<= 1;
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;
1017 #endif
1018 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1019 src = TMP_REG1;
1020 srcw = 0;
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));
1043 src1 = TMP_FREG1;
1045 else
1046 src1 <<= 1;
1048 if (src2 & SLJIT_MEM) {
1049 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1050 src2 = TMP_FREG2;
1052 else
1053 src2 <<= 1;
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)
1062 sljit_s32 dst_r;
1064 CHECK_ERROR();
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)
1072 op ^= SLJIT_F32_OP;
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));
1078 src = dst_r;
1080 else
1081 src <<= 1;
1083 switch (GET_OPCODE(op)) {
1084 case SLJIT_MOV_F64:
1085 if (src != dst_r) {
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));
1091 else
1092 dst_r = src;
1094 break;
1095 case SLJIT_NEG_F64:
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));
1099 break;
1100 case SLJIT_ABS_F64:
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));
1104 break;
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));
1107 op ^= SLJIT_F32_OP;
1108 break;
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;
1123 CHECK_ERROR();
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);
1137 src1 = TMP_FREG1;
1138 } else
1139 flags |= SLOW_SRC1;
1141 else
1142 src1 <<= 1;
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);
1147 src2 = TMP_FREG2;
1148 } else
1149 flags |= SLOW_SRC2;
1151 else
1152 src2 <<= 1;
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));
1159 else {
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)
1170 src1 = TMP_FREG1;
1171 if (flags & SLOW_SRC2)
1172 src2 = TMP_FREG2;
1174 switch (GET_OPCODE(op)) {
1175 case SLJIT_ADD_F64:
1176 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1177 break;
1179 case SLJIT_SUB_F64:
1180 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1181 break;
1183 case SLJIT_MUL_F64:
1184 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1185 break;
1187 case SLJIT_DIV_F64:
1188 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1189 break;
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;
1198 #undef FLOAT_DATA
1199 #undef SELECT_FOP
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)
1207 CHECK_ERROR();
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));
1214 /* Memory. */
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)
1220 CHECK_ERROR();
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;
1243 CHECK_ERROR_PTR();
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;
1253 return label;
1256 static sljit_ins get_cc(sljit_s32 type)
1258 switch (type) {
1259 case SLJIT_EQUAL:
1260 case SLJIT_MUL_NOT_OVERFLOW:
1261 case SLJIT_NOT_EQUAL_F64: /* Unordered. */
1262 return DA(0x1);
1264 case SLJIT_NOT_EQUAL:
1265 case SLJIT_MUL_OVERFLOW:
1266 case SLJIT_EQUAL_F64:
1267 return DA(0x9);
1269 case SLJIT_LESS:
1270 case SLJIT_GREATER_F64: /* Unordered. */
1271 return DA(0x5);
1273 case SLJIT_GREATER_EQUAL:
1274 case SLJIT_LESS_EQUAL_F64:
1275 return DA(0xd);
1277 case SLJIT_GREATER:
1278 case SLJIT_GREATER_EQUAL_F64: /* Unordered. */
1279 return DA(0xc);
1281 case SLJIT_LESS_EQUAL:
1282 case SLJIT_LESS_F64:
1283 return DA(0x4);
1285 case SLJIT_SIG_LESS:
1286 return DA(0x3);
1288 case SLJIT_SIG_GREATER_EQUAL:
1289 return DA(0xb);
1291 case SLJIT_SIG_GREATER:
1292 return DA(0xa);
1294 case SLJIT_SIG_LESS_EQUAL:
1295 return DA(0x2);
1297 case SLJIT_OVERFLOW:
1298 case SLJIT_UNORDERED_F64:
1299 return DA(0x7);
1301 case SLJIT_NOT_OVERFLOW:
1302 case SLJIT_ORDERED_F64:
1303 return DA(0xf);
1305 default:
1306 SLJIT_UNREACHABLE();
1307 return DA(0x8);
1311 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1313 struct sljit_jump *jump;
1315 CHECK_ERROR_PTR();
1316 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1318 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1319 PTR_FAIL_IF(!jump);
1320 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1321 type &= 0xff;
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));
1329 #else
1330 #error "Implementation required"
1331 #endif
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));
1339 #else
1340 #error "Implementation required"
1341 #endif
1342 } else {
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));
1354 return jump;
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;
1360 sljit_s32 src_r;
1362 CHECK_ERROR();
1363 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1364 ADJUST_LOCAL_OFFSET(src, srcw);
1366 if (FAST_IS_REG(src))
1367 src_r = src;
1368 else if (src & SLJIT_IMM) {
1369 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1370 FAIL_IF(!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));
1379 src_r = TMP_REG2;
1381 else {
1382 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1383 src_r = TMP_REG2;
1386 FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1387 if (jump)
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,
1395 sljit_s32 type)
1397 sljit_s32 reg, flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1399 CHECK_ERROR();
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));
1412 src = TMP_REG1;
1413 srcw = 0;
1416 type &= 0xff;
1417 if (type < SLJIT_EQUAL_F64)
1418 FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1419 else
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;
1429 #else
1430 #error "Implementation required"
1431 #endif
1434 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1435 sljit_s32 dst_reg,
1436 sljit_s32 src, sljit_sw srcw)
1438 CHECK_ERROR();
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);;
1443 #else
1444 #error "Implementation required"
1445 #endif
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)
1450 sljit_s32 reg;
1451 struct sljit_const *const_;
1453 CHECK_ERROR_PTR();
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));
1467 return const_;