• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
28 {
29 	return "SPARC" SLJIT_CPUINFO;
30 }
31 
32 /* Length of an instruction word
33    Both for sparc-32 and sparc-64 */
34 typedef sljit_u32 sljit_ins;
35 
36 #if (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL)
37 
sparc_cache_flush(sljit_ins * from,sljit_ins * to)38 static void sparc_cache_flush(sljit_ins *from, sljit_ins *to)
39 {
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"
46 
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"
54 
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:"
62 	);
63 #else
64 	if (SLJIT_UNLIKELY(from == to))
65 		return;
66 
67 	do {
68 		__asm__ volatile (
69 			"flush %0\n"
70 			: : "r"(from)
71 		);
72 		/* Operates at least on doubleword. */
73 		from += 2;
74 	} while (from < to);
75 
76 	if (from == to) {
77 		/* Flush the last word. */
78 		from --;
79 		__asm__ volatile (
80 			"flush %0\n"
81 			: : "r"(from)
82 		);
83 	}
84 #endif
85 }
86 
87 #endif /* (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL) */
88 
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)
94 
95 #define TMP_FREG1	(0)
96 #define TMP_FREG2	((SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1) << 1)
97 
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
100 };
101 
102 /* --------------------------------------------------------------------- */
103 /*  Instrucion forms                                                     */
104 /* --------------------------------------------------------------------- */
105 
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)
115 
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)
121 
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))
166 
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)
171 
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
182 
183 #define SIMM_MAX	(0x0fff)
184 #define SIMM_MIN	(-0x1000)
185 
186 /* dest_reg is the absolute name of the register
187    Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins,sljit_s32 delay_slot)188 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_s32 delay_slot)
189 {
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;
200 }
201 
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code)202 static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
203 {
204 	sljit_sw diff;
205 	sljit_uw target_addr;
206 	sljit_ins *inst;
207 	sljit_ins saved_inst;
208 
209 	if (jump->flags & SLJIT_REWRITABLE_JUMP)
210 		return code_ptr;
211 
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);
217 	}
218 	inst = (sljit_ins*)jump->addr;
219 
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;
229 		}
230 		inst[0] = CALL;
231 		inst[1] = NOP;
232 		return inst + 1;
233 	}
234 #else
235 	/* Both calls and BPr instructions shall not pass this point. */
236 #error "Implementation required"
237 #endif
238 
239 	if (jump->flags & IS_COND)
240 		inst--;
241 
242 	if (jump->flags & IS_MOVABLE) {
243 		diff = ((sljit_sw)target_addr - (sljit_sw)(inst - 1)) >> 2;
244 		if (diff <= MAX_DISP && diff >= MIN_DISP) {
245 			jump->flags |= PATCH_B;
246 			inst--;
247 			if (jump->flags & IS_COND) {
248 				saved_inst = inst[0];
249 				inst[0] = inst[1] ^ (1 << 28);
250 				inst[1] = saved_inst;
251 			} else {
252 				inst[1] = inst[0];
253 				inst[0] = BICC | DA(0x8);
254 			}
255 			jump->addr = (sljit_uw)inst;
256 			return inst + 1;
257 		}
258 	}
259 
260 	diff = ((sljit_sw)target_addr - (sljit_sw)(inst)) >> 2;
261 	if (diff <= MAX_DISP && diff >= MIN_DISP) {
262 		jump->flags |= PATCH_B;
263 		if (jump->flags & IS_COND)
264 			inst[0] ^= (1 << 28);
265 		else
266 			inst[0] = BICC | DA(0x8);
267 		inst[1] = NOP;
268 		jump->addr = (sljit_uw)inst;
269 		return inst + 1;
270 	}
271 
272 	return code_ptr;
273 }
274 
sljit_generate_code(struct sljit_compiler * compiler)275 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
276 {
277 	struct sljit_memory_fragment *buf;
278 	sljit_ins *code;
279 	sljit_ins *code_ptr;
280 	sljit_ins *buf_ptr;
281 	sljit_ins *buf_end;
282 	sljit_uw word_count;
283 	sljit_uw addr;
284 
285 	struct sljit_label *label;
286 	struct sljit_jump *jump;
287 	struct sljit_const *const_;
288 
289 	CHECK_ERROR_PTR();
290 	CHECK_PTR(check_sljit_generate_code(compiler));
291 	reverse_buf(compiler);
292 
293 	code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
294 	PTR_FAIL_WITH_EXEC_IF(code);
295 	buf = compiler->buf;
296 
297 	code_ptr = code;
298 	word_count = 0;
299 	label = compiler->labels;
300 	jump = compiler->jumps;
301 	const_ = compiler->consts;
302 	do {
303 		buf_ptr = (sljit_ins*)buf->memory;
304 		buf_end = buf_ptr + (buf->used_size >> 2);
305 		do {
306 			*code_ptr = *buf_ptr++;
307 			SLJIT_ASSERT(!label || label->size >= word_count);
308 			SLJIT_ASSERT(!jump || jump->addr >= word_count);
309 			SLJIT_ASSERT(!const_ || const_->addr >= word_count);
310 			/* These structures are ordered by their address. */
311 			if (label && label->size == word_count) {
312 				/* Just recording the address. */
313 				label->addr = (sljit_uw)code_ptr;
314 				label->size = code_ptr - code;
315 				label = label->next;
316 			}
317 			if (jump && jump->addr == word_count) {
318 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
319 				jump->addr = (sljit_uw)(code_ptr - 3);
320 #else
321 				jump->addr = (sljit_uw)(code_ptr - 6);
322 #endif
323 				code_ptr = detect_jump_type(jump, code_ptr, code);
324 				jump = jump->next;
325 			}
326 			if (const_ && const_->addr == word_count) {
327 				/* Just recording the address. */
328 				const_->addr = (sljit_uw)code_ptr;
329 				const_ = const_->next;
330 			}
331 			code_ptr ++;
332 			word_count ++;
333 		} while (buf_ptr < buf_end);
334 
335 		buf = buf->next;
336 	} while (buf);
337 
338 	if (label && label->size == word_count) {
339 		label->addr = (sljit_uw)code_ptr;
340 		label->size = code_ptr - code;
341 		label = label->next;
342 	}
343 
344 	SLJIT_ASSERT(!label);
345 	SLJIT_ASSERT(!jump);
346 	SLJIT_ASSERT(!const_);
347 	SLJIT_ASSERT(code_ptr - code <= (sljit_s32)compiler->size);
348 
349 	jump = compiler->jumps;
350 	while (jump) {
351 		do {
352 			addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
353 			buf_ptr = (sljit_ins*)jump->addr;
354 
355 			if (jump->flags & PATCH_CALL) {
356 				addr = (sljit_sw)(addr - jump->addr) >> 2;
357 				SLJIT_ASSERT((sljit_sw)addr <= 0x1fffffff && (sljit_sw)addr >= -0x20000000);
358 				buf_ptr[0] = CALL | (addr & 0x3fffffff);
359 				break;
360 			}
361 			if (jump->flags & PATCH_B) {
362 				addr = (sljit_sw)(addr - jump->addr) >> 2;
363 				SLJIT_ASSERT((sljit_sw)addr <= MAX_DISP && (sljit_sw)addr >= MIN_DISP);
364 				buf_ptr[0] = (buf_ptr[0] & ~DISP_MASK) | (addr & DISP_MASK);
365 				break;
366 			}
367 
368 			/* Set the fields of immediate loads. */
369 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
370 			buf_ptr[0] = (buf_ptr[0] & 0xffc00000) | ((addr >> 10) & 0x3fffff);
371 			buf_ptr[1] = (buf_ptr[1] & 0xfffffc00) | (addr & 0x3ff);
372 #else
373 #error "Implementation required"
374 #endif
375 		} while (0);
376 		jump = jump->next;
377 	}
378 
379 
380 	compiler->error = SLJIT_ERR_COMPILED;
381 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
382 	SLJIT_CACHE_FLUSH(code, code_ptr);
383 	return code;
384 }
385 
386 /* --------------------------------------------------------------------- */
387 /*  Entry, exit                                                          */
388 /* --------------------------------------------------------------------- */
389 
390 /* Creates an index in data_transfer_insts array. */
391 #define LOAD_DATA	0x01
392 #define WORD_DATA	0x00
393 #define BYTE_DATA	0x02
394 #define HALF_DATA	0x04
395 #define INT_DATA	0x06
396 #define SIGNED_DATA	0x08
397 /* Separates integer and floating point registers */
398 #define GPR_REG		0x0f
399 #define DOUBLE_DATA	0x10
400 #define SINGLE_DATA	0x12
401 
402 #define MEM_MASK	0x1f
403 
404 #define WRITE_BACK	0x00020
405 #define ARG_TEST	0x00040
406 #define ALT_KEEP_CACHE	0x00080
407 #define CUMULATIVE_OP	0x00100
408 #define IMM_OP		0x00200
409 #define SRC2_IMM	0x00400
410 
411 #define REG_DEST	0x00800
412 #define REG2_SOURCE	0x01000
413 #define SLOW_SRC1	0x02000
414 #define SLOW_SRC2	0x04000
415 #define SLOW_DEST	0x08000
416 
417 /* SET_FLAGS (0x10 << 19) also belong here! */
418 
419 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
420 #include "sljitNativeSPARC_32.c"
421 #else
422 #include "sljitNativeSPARC_64.c"
423 #endif
424 
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)425 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
426 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
427 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
428 {
429 	CHECK_ERROR();
430 	CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
431 	set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
432 
433 	local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
434 	compiler->local_size = local_size;
435 
436 	if (local_size <= SIMM_MAX) {
437 		FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | IMM(-local_size), UNMOVABLE_INS));
438 	}
439 	else {
440 		FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
441 		FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | S2(TMP_REG1), UNMOVABLE_INS));
442 	}
443 
444 	/* Arguments are in their appropriate registers. */
445 
446 	return SLJIT_SUCCESS;
447 }
448 
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 args,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)449 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
450 	sljit_s32 options, sljit_s32 args, sljit_s32 scratches, sljit_s32 saveds,
451 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
452 {
453 	CHECK_ERROR();
454 	CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
455 	set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
456 
457 	compiler->local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
458 	return SLJIT_SUCCESS;
459 }
460 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)461 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
462 {
463 	CHECK_ERROR();
464 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
465 
466 	if (op != SLJIT_MOV || !FAST_IS_REG(src)) {
467 		FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
468 		src = SLJIT_R0;
469 	}
470 
471 	FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
472 	return push_inst(compiler, RESTORE | D(SLJIT_R0) | S1(src) | S2(0), UNMOVABLE_INS);
473 }
474 
475 /* --------------------------------------------------------------------- */
476 /*  Operators                                                            */
477 /* --------------------------------------------------------------------- */
478 
479 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
480 #define ARCH_32_64(a, b)	a
481 #else
482 #define ARCH_32_64(a, b)	b
483 #endif
484 
485 static const sljit_ins data_transfer_insts[16 + 4] = {
486 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
487 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
488 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
489 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
490 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
491 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
492 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
493 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
494 
495 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
496 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
497 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
498 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
499 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
500 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
501 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
502 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
503 
504 /* d   s */ OPC1(3) | OPC3(0x27),
505 /* d   l */ OPC1(3) | OPC3(0x23),
506 /* s   s */ OPC1(3) | OPC3(0x24),
507 /* s   l */ OPC1(3) | OPC3(0x20),
508 };
509 
510 #undef ARCH_32_64
511 
512 /* Can perform an operation using at most 1 instruction. */
getput_arg_fast(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)513 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
514 {
515 	SLJIT_ASSERT(arg & SLJIT_MEM);
516 
517 	if (!(flags & WRITE_BACK) || !(arg & REG_MASK)) {
518 		if ((!(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN)
519 				|| ((arg & OFFS_REG_MASK) && (argw & 0x3) == 0)) {
520 			/* Works for both absoulte and relative addresses (immediate case). */
521 			if (SLJIT_UNLIKELY(flags & ARG_TEST))
522 				return 1;
523 			FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
524 				| ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg))
525 				| S1(arg & REG_MASK) | ((arg & OFFS_REG_MASK) ? S2(OFFS_REG(arg)) : IMM(argw)),
526 				((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
527 			return -1;
528 		}
529 	}
530 	return 0;
531 }
532 
533 /* See getput_arg below.
534    Note: can_cache is called only for binary operators. Those
535    operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)536 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
537 {
538 	SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
539 
540 	/* Simple operation except for updates. */
541 	if (arg & OFFS_REG_MASK) {
542 		argw &= 0x3;
543 		SLJIT_ASSERT(argw);
544 		next_argw &= 0x3;
545 		if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == next_argw)
546 			return 1;
547 		return 0;
548 	}
549 
550 	if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
551 		return 1;
552 	return 0;
553 }
554 
555 /* Emit the necessary instructions. See can_cache above. */
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)556 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)
557 {
558 	sljit_s32 base, arg2, delay_slot;
559 	sljit_ins dest;
560 
561 	SLJIT_ASSERT(arg & SLJIT_MEM);
562 	if (!(next_arg & SLJIT_MEM)) {
563 		next_arg = 0;
564 		next_argw = 0;
565 	}
566 
567 	base = arg & REG_MASK;
568 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
569 		argw &= 0x3;
570 		SLJIT_ASSERT(argw != 0);
571 
572 		/* Using the cache. */
573 		if (((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) && (argw == compiler->cache_argw))
574 			arg2 = TMP_REG3;
575 		else {
576 			if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == (next_argw & 0x3)) {
577 				compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
578 				compiler->cache_argw = argw;
579 				arg2 = TMP_REG3;
580 			}
581 			else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && reg != OFFS_REG(arg))
582 				arg2 = reg;
583 			else /* It must be a mov operation, so tmp1 must be free to use. */
584 				arg2 = TMP_REG1;
585 			FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1(OFFS_REG(arg)) | IMM_ARG | argw, DR(arg2)));
586 		}
587 	}
588 	else {
589 		/* Using the cache. */
590 		if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
591 			if (argw != compiler->cache_argw) {
592 				FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
593 				compiler->cache_argw = argw;
594 			}
595 			arg2 = TMP_REG3;
596 		} else {
597 			if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
598 				compiler->cache_arg = SLJIT_MEM;
599 				compiler->cache_argw = argw;
600 				arg2 = TMP_REG3;
601 			}
602 			else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
603 				arg2 = reg;
604 			else /* It must be a mov operation, so tmp1 must be free to use. */
605 				arg2 = TMP_REG1;
606 			FAIL_IF(load_immediate(compiler, arg2, argw));
607 		}
608 	}
609 
610 	dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg));
611 	delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
612 	if (!base)
613 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
614 	if (!(flags & WRITE_BACK))
615 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
616 	FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot));
617 	return push_inst(compiler, ADD | D(base) | S1(base) | S2(arg2), DR(base));
618 }
619 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)620 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
621 {
622 	if (getput_arg_fast(compiler, flags, reg, arg, argw))
623 		return compiler->error;
624 	compiler->cache_arg = 0;
625 	compiler->cache_argw = 0;
626 	return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
627 }
628 
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)629 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)
630 {
631 	if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
632 		return compiler->error;
633 	return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
634 }
635 
emit_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)636 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
637 	sljit_s32 dst, sljit_sw dstw,
638 	sljit_s32 src1, sljit_sw src1w,
639 	sljit_s32 src2, sljit_sw src2w)
640 {
641 	/* arg1 goes to TMP_REG1 or src reg
642 	   arg2 goes to TMP_REG2, imm or src reg
643 	   TMP_REG3 can be used for caching
644 	   result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
645 	sljit_s32 dst_r = TMP_REG2;
646 	sljit_s32 src1_r;
647 	sljit_sw src2_r = 0;
648 	sljit_s32 sugg_src2_r = TMP_REG2;
649 
650 	if (!(flags & ALT_KEEP_CACHE)) {
651 		compiler->cache_arg = 0;
652 		compiler->cache_argw = 0;
653 	}
654 
655 	if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
656 		if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32 && !(src2 & SLJIT_MEM))
657 			return SLJIT_SUCCESS;
658 	}
659 	else if (FAST_IS_REG(dst)) {
660 		dst_r = dst;
661 		flags |= REG_DEST;
662 		if (op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
663 			sugg_src2_r = dst_r;
664 	}
665 	else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
666 		flags |= SLOW_DEST;
667 
668 	if (flags & IMM_OP) {
669 		if ((src2 & SLJIT_IMM) && src2w) {
670 			if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
671 				flags |= SRC2_IMM;
672 				src2_r = src2w;
673 			}
674 		}
675 		if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
676 			if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
677 				flags |= SRC2_IMM;
678 				src2_r = src1w;
679 
680 				/* And swap arguments. */
681 				src1 = src2;
682 				src1w = src2w;
683 				src2 = SLJIT_IMM;
684 				/* src2w = src2_r unneeded. */
685 			}
686 		}
687 	}
688 
689 	/* Source 1. */
690 	if (FAST_IS_REG(src1))
691 		src1_r = src1;
692 	else if (src1 & SLJIT_IMM) {
693 		if (src1w) {
694 			FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
695 			src1_r = TMP_REG1;
696 		}
697 		else
698 			src1_r = 0;
699 	}
700 	else {
701 		if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
702 			FAIL_IF(compiler->error);
703 		else
704 			flags |= SLOW_SRC1;
705 		src1_r = TMP_REG1;
706 	}
707 
708 	/* Source 2. */
709 	if (FAST_IS_REG(src2)) {
710 		src2_r = src2;
711 		flags |= REG2_SOURCE;
712 		if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_S32)
713 			dst_r = src2_r;
714 	}
715 	else if (src2 & SLJIT_IMM) {
716 		if (!(flags & SRC2_IMM)) {
717 			if (src2w) {
718 				FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
719 				src2_r = sugg_src2_r;
720 			}
721 			else {
722 				src2_r = 0;
723 				if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_S32) && (dst & SLJIT_MEM))
724 					dst_r = 0;
725 			}
726 		}
727 	}
728 	else {
729 		if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
730 			FAIL_IF(compiler->error);
731 		else
732 			flags |= SLOW_SRC2;
733 		src2_r = sugg_src2_r;
734 	}
735 
736 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
737 		SLJIT_ASSERT(src2_r == TMP_REG2);
738 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
739 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
740 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
741 		}
742 		else {
743 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
744 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
745 		}
746 	}
747 	else if (flags & SLOW_SRC1)
748 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
749 	else if (flags & SLOW_SRC2)
750 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
751 
752 	FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
753 
754 	if (dst & SLJIT_MEM) {
755 		if (!(flags & SLOW_DEST)) {
756 			getput_arg_fast(compiler, flags, dst_r, dst, dstw);
757 			return compiler->error;
758 		}
759 		return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
760 	}
761 
762 	return SLJIT_SUCCESS;
763 }
764 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)765 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
766 {
767 	CHECK_ERROR();
768 	CHECK(check_sljit_emit_op0(compiler, op));
769 
770 	op = GET_OPCODE(op);
771 	switch (op) {
772 	case SLJIT_BREAKPOINT:
773 		return push_inst(compiler, TA, UNMOVABLE_INS);
774 	case SLJIT_NOP:
775 		return push_inst(compiler, NOP, UNMOVABLE_INS);
776 	case SLJIT_LMUL_UW:
777 	case SLJIT_LMUL_SW:
778 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
779 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? UMUL : SMUL) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
780 		return push_inst(compiler, RDY | D(SLJIT_R1), DR(SLJIT_R1));
781 #else
782 #error "Implementation required"
783 #endif
784 	case SLJIT_DIVMOD_UW:
785 	case SLJIT_DIVMOD_SW:
786 	case SLJIT_DIV_UW:
787 	case SLJIT_DIV_SW:
788 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
789 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
790 		if ((op | 0x2) == SLJIT_DIV_UW)
791 			FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
792 		else {
793 			FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_R0) | IMM(31), DR(TMP_REG1)));
794 			FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
795 		}
796 		if (op <= SLJIT_DIVMOD_SW)
797 			FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_R0), DR(TMP_REG2)));
798 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? UDIV : SDIV) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
799 		if (op >= SLJIT_DIV_UW)
800 			return SLJIT_SUCCESS;
801 		FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_R1) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R1)));
802 		return push_inst(compiler, SUB | D(SLJIT_R1) | S1(TMP_REG2) | S2(SLJIT_R1), DR(SLJIT_R1));
803 #else
804 #error "Implementation required"
805 #endif
806 	}
807 
808 	return SLJIT_SUCCESS;
809 }
810 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)811 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
812 	sljit_s32 dst, sljit_sw dstw,
813 	sljit_s32 src, sljit_sw srcw)
814 {
815 	sljit_s32 flags = GET_FLAGS(op) ? SET_FLAGS : 0;
816 
817 	CHECK_ERROR();
818 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
819 	ADJUST_LOCAL_OFFSET(dst, dstw);
820 	ADJUST_LOCAL_OFFSET(src, srcw);
821 
822 	op = GET_OPCODE(op);
823 	switch (op) {
824 	case SLJIT_MOV:
825 	case SLJIT_MOV_P:
826 		return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
827 
828 	case SLJIT_MOV_U32:
829 		return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
830 
831 	case SLJIT_MOV_S32:
832 		return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
833 
834 	case SLJIT_MOV_U8:
835 		return emit_op(compiler, SLJIT_MOV_U8, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
836 
837 	case SLJIT_MOV_S8:
838 		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);
839 
840 	case SLJIT_MOV_U16:
841 		return emit_op(compiler, SLJIT_MOV_U16, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
842 
843 	case SLJIT_MOV_S16:
844 		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);
845 
846 	case SLJIT_MOVU:
847 	case SLJIT_MOVU_P:
848 		return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
849 
850 	case SLJIT_MOVU_U32:
851 		return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
852 
853 	case SLJIT_MOVU_S32:
854 		return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
855 
856 	case SLJIT_MOVU_U8:
857 		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);
858 
859 	case SLJIT_MOVU_S8:
860 		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);
861 
862 	case SLJIT_MOVU_U16:
863 		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);
864 
865 	case SLJIT_MOVU_S16:
866 		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);
867 
868 	case SLJIT_NOT:
869 	case SLJIT_CLZ:
870 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
871 
872 	case SLJIT_NEG:
873 		return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
874 	}
875 
876 	return SLJIT_SUCCESS;
877 }
878 
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)879 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
880 	sljit_s32 dst, sljit_sw dstw,
881 	sljit_s32 src1, sljit_sw src1w,
882 	sljit_s32 src2, sljit_sw src2w)
883 {
884 	sljit_s32 flags = GET_FLAGS(op) ? SET_FLAGS : 0;
885 
886 	CHECK_ERROR();
887 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
888 	ADJUST_LOCAL_OFFSET(dst, dstw);
889 	ADJUST_LOCAL_OFFSET(src1, src1w);
890 	ADJUST_LOCAL_OFFSET(src2, src2w);
891 
892 	op = GET_OPCODE(op);
893 	switch (op) {
894 	case SLJIT_ADD:
895 	case SLJIT_ADDC:
896 	case SLJIT_MUL:
897 	case SLJIT_AND:
898 	case SLJIT_OR:
899 	case SLJIT_XOR:
900 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
901 
902 	case SLJIT_SUB:
903 	case SLJIT_SUBC:
904 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
905 
906 	case SLJIT_SHL:
907 	case SLJIT_LSHR:
908 	case SLJIT_ASHR:
909 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
910 		if (src2 & SLJIT_IMM)
911 			src2w &= 0x1f;
912 #else
913 		SLJIT_ASSERT_STOP();
914 #endif
915 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
916 	}
917 
918 	return SLJIT_SUCCESS;
919 }
920 
sljit_get_register_index(sljit_s32 reg)921 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
922 {
923 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
924 	return reg_map[reg];
925 }
926 
sljit_get_float_register_index(sljit_s32 reg)927 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
928 {
929 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
930 	return reg << 1;
931 }
932 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)933 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
934 	void *instruction, sljit_s32 size)
935 {
936 	CHECK_ERROR();
937 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
938 
939 	return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
940 }
941 
942 /* --------------------------------------------------------------------- */
943 /*  Floating point operators                                             */
944 /* --------------------------------------------------------------------- */
945 
sljit_is_fpu_available(void)946 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_is_fpu_available(void)
947 {
948 #ifdef SLJIT_IS_FPU_AVAILABLE
949 	return SLJIT_IS_FPU_AVAILABLE;
950 #else
951 	/* Available by default. */
952 	return 1;
953 #endif
954 }
955 
956 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
957 #define SELECT_FOP(op, single, double) ((op & SLJIT_F32_OP) ? single : double)
958 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
959 
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)960 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
961 	sljit_s32 dst, sljit_sw dstw,
962 	sljit_s32 src, sljit_sw srcw)
963 {
964 	if (src & SLJIT_MEM) {
965 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
966 		src = TMP_FREG1;
967 	}
968 	else
969 		src <<= 1;
970 
971 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOI, FDTOI) | DA(TMP_FREG1) | S2A(src), MOVABLE_INS));
972 
973 	if (dst == SLJIT_UNUSED)
974 		return SLJIT_SUCCESS;
975 
976 	if (FAST_IS_REG(dst)) {
977 		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));
978 		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);
979 	}
980 
981 	/* Store the integer value from a VFP register. */
982 	return emit_op_mem2(compiler, SINGLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
983 }
984 
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)985 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
986 	sljit_s32 dst, sljit_sw dstw,
987 	sljit_s32 src, sljit_sw srcw)
988 {
989 	sljit_s32 dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
990 
991 	if (src & SLJIT_IMM) {
992 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
993 		if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
994 			srcw = (sljit_s32)srcw;
995 #endif
996 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
997 		src = TMP_REG1;
998 		srcw = 0;
999 	}
1000 
1001 	if (FAST_IS_REG(src)) {
1002 		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));
1003 		src = SLJIT_MEM1(SLJIT_SP);
1004 		srcw = FLOAT_TMP_MEM_OFFSET;
1005 	}
1006 
1007 	FAIL_IF(emit_op_mem2(compiler, SINGLE_DATA | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1008 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FITOS, FITOD) | DA(dst_r) | S2A(TMP_FREG1), MOVABLE_INS));
1009 
1010 	if (dst & SLJIT_MEM)
1011 		return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
1012 	return SLJIT_SUCCESS;
1013 }
1014 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1015 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1016 	sljit_s32 src1, sljit_sw src1w,
1017 	sljit_s32 src2, sljit_sw src2w)
1018 {
1019 	if (src1 & SLJIT_MEM) {
1020 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1021 		src1 = TMP_FREG1;
1022 	}
1023 	else
1024 		src1 <<= 1;
1025 
1026 	if (src2 & SLJIT_MEM) {
1027 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1028 		src2 = TMP_FREG2;
1029 	}
1030 	else
1031 		src2 <<= 1;
1032 
1033 	return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | S1A(src1) | S2A(src2), FCC_IS_SET | MOVABLE_INS);
1034 }
1035 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1036 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1037 	sljit_s32 dst, sljit_sw dstw,
1038 	sljit_s32 src, sljit_sw srcw)
1039 {
1040 	sljit_s32 dst_r;
1041 
1042 	CHECK_ERROR();
1043 	compiler->cache_arg = 0;
1044 	compiler->cache_argw = 0;
1045 
1046 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1047 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1048 
1049 	if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
1050 		op ^= SLJIT_F32_OP;
1051 
1052 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
1053 
1054 	if (src & SLJIT_MEM) {
1055 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
1056 		src = dst_r;
1057 	}
1058 	else
1059 		src <<= 1;
1060 
1061 	switch (GET_OPCODE(op)) {
1062 	case SLJIT_MOV_F64:
1063 		if (src != dst_r) {
1064 			if (dst_r != TMP_FREG1) {
1065 				FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r) | S2A(src), MOVABLE_INS));
1066 				if (!(op & SLJIT_F32_OP))
1067 					FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r | 1) | S2A(src | 1), MOVABLE_INS));
1068 			}
1069 			else
1070 				dst_r = src;
1071 		}
1072 		break;
1073 	case SLJIT_NEG_F64:
1074 		FAIL_IF(push_inst(compiler, FNEGS | DA(dst_r) | S2A(src), MOVABLE_INS));
1075 		if (dst_r != src && !(op & SLJIT_F32_OP))
1076 			FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r | 1) | S2A(src | 1), MOVABLE_INS));
1077 		break;
1078 	case SLJIT_ABS_F64:
1079 		FAIL_IF(push_inst(compiler, FABSS | DA(dst_r) | S2A(src), MOVABLE_INS));
1080 		if (dst_r != src && !(op & SLJIT_F32_OP))
1081 			FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r | 1) | S2A(src | 1), MOVABLE_INS));
1082 		break;
1083 	case SLJIT_CONV_F64_FROM_F32:
1084 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOD, FDTOS) | DA(dst_r) | S2A(src), MOVABLE_INS));
1085 		op ^= SLJIT_F32_OP;
1086 		break;
1087 	}
1088 
1089 	if (dst & SLJIT_MEM)
1090 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0));
1091 	return SLJIT_SUCCESS;
1092 }
1093 
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1094 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1095 	sljit_s32 dst, sljit_sw dstw,
1096 	sljit_s32 src1, sljit_sw src1w,
1097 	sljit_s32 src2, sljit_sw src2w)
1098 {
1099 	sljit_s32 dst_r, flags = 0;
1100 
1101 	CHECK_ERROR();
1102 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1103 	ADJUST_LOCAL_OFFSET(dst, dstw);
1104 	ADJUST_LOCAL_OFFSET(src1, src1w);
1105 	ADJUST_LOCAL_OFFSET(src2, src2w);
1106 
1107 	compiler->cache_arg = 0;
1108 	compiler->cache_argw = 0;
1109 
1110 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG2;
1111 
1112 	if (src1 & SLJIT_MEM) {
1113 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1114 			FAIL_IF(compiler->error);
1115 			src1 = TMP_FREG1;
1116 		} else
1117 			flags |= SLOW_SRC1;
1118 	}
1119 	else
1120 		src1 <<= 1;
1121 
1122 	if (src2 & SLJIT_MEM) {
1123 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1124 			FAIL_IF(compiler->error);
1125 			src2 = TMP_FREG2;
1126 		} else
1127 			flags |= SLOW_SRC2;
1128 	}
1129 	else
1130 		src2 <<= 1;
1131 
1132 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1133 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1134 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1135 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1136 		}
1137 		else {
1138 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1139 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1140 		}
1141 	}
1142 	else if (flags & SLOW_SRC1)
1143 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1144 	else if (flags & SLOW_SRC2)
1145 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1146 
1147 	if (flags & SLOW_SRC1)
1148 		src1 = TMP_FREG1;
1149 	if (flags & SLOW_SRC2)
1150 		src2 = TMP_FREG2;
1151 
1152 	switch (GET_OPCODE(op)) {
1153 	case SLJIT_ADD_F64:
1154 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1155 		break;
1156 
1157 	case SLJIT_SUB_F64:
1158 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1159 		break;
1160 
1161 	case SLJIT_MUL_F64:
1162 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1163 		break;
1164 
1165 	case SLJIT_DIV_F64:
1166 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1167 		break;
1168 	}
1169 
1170 	if (dst_r == TMP_FREG2)
1171 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1172 
1173 	return SLJIT_SUCCESS;
1174 }
1175 
1176 #undef FLOAT_DATA
1177 #undef SELECT_FOP
1178 
1179 /* --------------------------------------------------------------------- */
1180 /*  Other instructions                                                   */
1181 /* --------------------------------------------------------------------- */
1182 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1183 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1184 {
1185 	CHECK_ERROR();
1186 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1187 	ADJUST_LOCAL_OFFSET(dst, dstw);
1188 
1189 	/* For UNUSED dst. Uncommon, but possible. */
1190 	if (dst == SLJIT_UNUSED)
1191 		return SLJIT_SUCCESS;
1192 
1193 	if (FAST_IS_REG(dst))
1194 		return push_inst(compiler, OR | D(dst) | S1(0) | S2(TMP_LINK), DR(dst));
1195 
1196 	/* Memory. */
1197 	return emit_op_mem(compiler, WORD_DATA, TMP_LINK, dst, dstw);
1198 }
1199 
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)1200 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
1201 {
1202 	CHECK_ERROR();
1203 	CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1204 	ADJUST_LOCAL_OFFSET(src, srcw);
1205 
1206 	if (FAST_IS_REG(src))
1207 		FAIL_IF(push_inst(compiler, OR | D(TMP_LINK) | S1(0) | S2(src), DR(TMP_LINK)));
1208 	else if (src & SLJIT_MEM)
1209 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_LINK, src, srcw));
1210 	else if (src & SLJIT_IMM)
1211 		FAIL_IF(load_immediate(compiler, TMP_LINK, srcw));
1212 
1213 	FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(TMP_LINK) | IMM(8), UNMOVABLE_INS));
1214 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1215 }
1216 
1217 /* --------------------------------------------------------------------- */
1218 /*  Conditional instructions                                             */
1219 /* --------------------------------------------------------------------- */
1220 
sljit_emit_label(struct sljit_compiler * compiler)1221 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1222 {
1223 	struct sljit_label *label;
1224 
1225 	CHECK_ERROR_PTR();
1226 	CHECK_PTR(check_sljit_emit_label(compiler));
1227 
1228 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1229 		return compiler->last_label;
1230 
1231 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1232 	PTR_FAIL_IF(!label);
1233 	set_label(label, compiler);
1234 	compiler->delay_slot = UNMOVABLE_INS;
1235 	return label;
1236 }
1237 
get_cc(sljit_s32 type)1238 static sljit_ins get_cc(sljit_s32 type)
1239 {
1240 	switch (type) {
1241 	case SLJIT_EQUAL:
1242 	case SLJIT_MUL_NOT_OVERFLOW:
1243 	case SLJIT_NOT_EQUAL_F64: /* Unordered. */
1244 		return DA(0x1);
1245 
1246 	case SLJIT_NOT_EQUAL:
1247 	case SLJIT_MUL_OVERFLOW:
1248 	case SLJIT_EQUAL_F64:
1249 		return DA(0x9);
1250 
1251 	case SLJIT_LESS:
1252 	case SLJIT_GREATER_F64: /* Unordered. */
1253 		return DA(0x5);
1254 
1255 	case SLJIT_GREATER_EQUAL:
1256 	case SLJIT_LESS_EQUAL_F64:
1257 		return DA(0xd);
1258 
1259 	case SLJIT_GREATER:
1260 	case SLJIT_GREATER_EQUAL_F64: /* Unordered. */
1261 		return DA(0xc);
1262 
1263 	case SLJIT_LESS_EQUAL:
1264 	case SLJIT_LESS_F64:
1265 		return DA(0x4);
1266 
1267 	case SLJIT_SIG_LESS:
1268 		return DA(0x3);
1269 
1270 	case SLJIT_SIG_GREATER_EQUAL:
1271 		return DA(0xb);
1272 
1273 	case SLJIT_SIG_GREATER:
1274 		return DA(0xa);
1275 
1276 	case SLJIT_SIG_LESS_EQUAL:
1277 		return DA(0x2);
1278 
1279 	case SLJIT_OVERFLOW:
1280 	case SLJIT_UNORDERED_F64:
1281 		return DA(0x7);
1282 
1283 	case SLJIT_NOT_OVERFLOW:
1284 	case SLJIT_ORDERED_F64:
1285 		return DA(0xf);
1286 
1287 	default:
1288 		SLJIT_ASSERT_STOP();
1289 		return DA(0x8);
1290 	}
1291 }
1292 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1293 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1294 {
1295 	struct sljit_jump *jump;
1296 
1297 	CHECK_ERROR_PTR();
1298 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1299 
1300 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1301 	PTR_FAIL_IF(!jump);
1302 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1303 	type &= 0xff;
1304 
1305 	if (type < SLJIT_EQUAL_F64) {
1306 		jump->flags |= IS_COND;
1307 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1308 			jump->flags |= IS_MOVABLE;
1309 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1310 		PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1311 #else
1312 #error "Implementation required"
1313 #endif
1314 	}
1315 	else if (type < SLJIT_JUMP) {
1316 		jump->flags |= IS_COND;
1317 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1318 			jump->flags |= IS_MOVABLE;
1319 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1320 		PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1321 #else
1322 #error "Implementation required"
1323 #endif
1324 	} else {
1325 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1326 			jump->flags |= IS_MOVABLE;
1327 		if (type >= SLJIT_FAST_CALL)
1328 			jump->flags |= IS_CALL;
1329 	}
1330 
1331 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1332 	PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(TMP_REG2) | IMM(0), UNMOVABLE_INS));
1333 	jump->addr = compiler->size;
1334 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1335 
1336 	return jump;
1337 }
1338 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1339 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1340 {
1341 	struct sljit_jump *jump = NULL;
1342 	sljit_s32 src_r;
1343 
1344 	CHECK_ERROR();
1345 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1346 	ADJUST_LOCAL_OFFSET(src, srcw);
1347 
1348 	if (FAST_IS_REG(src))
1349 		src_r = src;
1350 	else if (src & SLJIT_IMM) {
1351 		jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1352 		FAIL_IF(!jump);
1353 		set_jump(jump, compiler, JUMP_ADDR);
1354 		jump->u.target = srcw;
1355 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1356 			jump->flags |= IS_MOVABLE;
1357 		if (type >= SLJIT_FAST_CALL)
1358 			jump->flags |= IS_CALL;
1359 
1360 		FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1361 		src_r = TMP_REG2;
1362 	}
1363 	else {
1364 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1365 		src_r = TMP_REG2;
1366 	}
1367 
1368 	FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1369 	if (jump)
1370 		jump->addr = compiler->size;
1371 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1372 }
1373 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw,sljit_s32 type)1374 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1375 	sljit_s32 dst, sljit_sw dstw,
1376 	sljit_s32 src, sljit_sw srcw,
1377 	sljit_s32 type)
1378 {
1379 	sljit_s32 reg, flags = (GET_FLAGS(op) ? SET_FLAGS : 0);
1380 
1381 	CHECK_ERROR();
1382 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type));
1383 	ADJUST_LOCAL_OFFSET(dst, dstw);
1384 
1385 	if (dst == SLJIT_UNUSED)
1386 		return SLJIT_SUCCESS;
1387 
1388 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1389 	op = GET_OPCODE(op);
1390 	reg = (op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2;
1391 
1392 	compiler->cache_arg = 0;
1393 	compiler->cache_argw = 0;
1394 	if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
1395 		ADJUST_LOCAL_OFFSET(src, srcw);
1396 		FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
1397 		src = TMP_REG1;
1398 		srcw = 0;
1399 	}
1400 
1401 	type &= 0xff;
1402 	if (type < SLJIT_EQUAL_F64)
1403 		FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1404 	else
1405 		FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1406 
1407 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1408 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1409 
1410 	if (op >= SLJIT_ADD)
1411 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE, dst, dstw, src, srcw, TMP_REG2, 0);
1412 
1413 	return (reg == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
1414 #else
1415 #error "Implementation required"
1416 #endif
1417 }
1418 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)1419 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
1420 {
1421 	sljit_s32 reg;
1422 	struct sljit_const *const_;
1423 
1424 	CHECK_ERROR_PTR();
1425 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1426 	ADJUST_LOCAL_OFFSET(dst, dstw);
1427 
1428 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1429 	PTR_FAIL_IF(!const_);
1430 	set_const(const_, compiler);
1431 
1432 	reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
1433 
1434 	PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1435 
1436 	if (dst & SLJIT_MEM)
1437 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1438 	return const_;
1439 }
1440