1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright 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 /* This register is modified by calls, which affects the instruction
94    in the delay slot if it is used as a source register. */
95 #define TMP_LINK	(SLJIT_NUMBER_OF_REGISTERS + 5)
96 
97 #define TMP_FREG1	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
98 #define TMP_FREG2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
99 
100 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 6] = {
101 	0, 8, 9, 10, 11, 29, 28, 27, 23, 22, 21, 20, 19, 18, 17, 16, 26, 25, 24, 14, 1, 12, 13, 15
102 };
103 
104 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
105 	0, 0, 2, 4, 6, 8, 10, 12, 14
106 };
107 
108 /* --------------------------------------------------------------------- */
109 /*  Instrucion forms                                                     */
110 /* --------------------------------------------------------------------- */
111 
112 #define D(d)		(reg_map[d] << 25)
113 #define FD(d)		(freg_map[d] << 25)
114 #define FDN(d)		((freg_map[d] | 0x1) << 25)
115 #define DA(d)		((d) << 25)
116 #define S1(s1)		(reg_map[s1] << 14)
117 #define FS1(s1)		(freg_map[s1] << 14)
118 #define S1A(s1)		((s1) << 14)
119 #define S2(s2)		(reg_map[s2])
120 #define FS2(s2)		(freg_map[s2])
121 #define FS2N(s2)	(freg_map[s2] | 0x1)
122 #define S2A(s2)		(s2)
123 #define IMM_ARG		0x2000
124 #define DOP(op)		((op) << 5)
125 #define IMM(imm)	(((imm) & 0x1fff) | IMM_ARG)
126 
127 #define DR(dr)		(reg_map[dr])
128 #define OPC1(opcode)	((opcode) << 30)
129 #define OPC2(opcode)	((opcode) << 22)
130 #define OPC3(opcode)	((opcode) << 19)
131 #define SET_FLAGS	OPC3(0x10)
132 
133 #define ADD		(OPC1(0x2) | OPC3(0x00))
134 #define ADDC		(OPC1(0x2) | OPC3(0x08))
135 #define AND		(OPC1(0x2) | OPC3(0x01))
136 #define ANDN		(OPC1(0x2) | OPC3(0x05))
137 #define CALL		(OPC1(0x1))
138 #define FABSS		(OPC1(0x2) | OPC3(0x34) | DOP(0x09))
139 #define FADDD		(OPC1(0x2) | OPC3(0x34) | DOP(0x42))
140 #define FADDS		(OPC1(0x2) | OPC3(0x34) | DOP(0x41))
141 #define FCMPD		(OPC1(0x2) | OPC3(0x35) | DOP(0x52))
142 #define FCMPS		(OPC1(0x2) | OPC3(0x35) | DOP(0x51))
143 #define FDIVD		(OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
144 #define FDIVS		(OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
145 #define FDTOI		(OPC1(0x2) | OPC3(0x34) | DOP(0xd2))
146 #define FDTOS		(OPC1(0x2) | OPC3(0x34) | DOP(0xc6))
147 #define FITOD		(OPC1(0x2) | OPC3(0x34) | DOP(0xc8))
148 #define FITOS		(OPC1(0x2) | OPC3(0x34) | DOP(0xc4))
149 #define FMOVS		(OPC1(0x2) | OPC3(0x34) | DOP(0x01))
150 #define FMULD		(OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
151 #define FMULS		(OPC1(0x2) | OPC3(0x34) | DOP(0x49))
152 #define FNEGS		(OPC1(0x2) | OPC3(0x34) | DOP(0x05))
153 #define FSTOD		(OPC1(0x2) | OPC3(0x34) | DOP(0xc9))
154 #define FSTOI		(OPC1(0x2) | OPC3(0x34) | DOP(0xd1))
155 #define FSUBD		(OPC1(0x2) | OPC3(0x34) | DOP(0x46))
156 #define FSUBS		(OPC1(0x2) | OPC3(0x34) | DOP(0x45))
157 #define JMPL		(OPC1(0x2) | OPC3(0x38))
158 #define LDD		(OPC1(0x3) | OPC3(0x03))
159 #define LDUW		(OPC1(0x3) | OPC3(0x00))
160 #define NOP		(OPC1(0x0) | OPC2(0x04))
161 #define OR		(OPC1(0x2) | OPC3(0x02))
162 #define ORN		(OPC1(0x2) | OPC3(0x06))
163 #define RDY		(OPC1(0x2) | OPC3(0x28) | S1A(0))
164 #define RESTORE		(OPC1(0x2) | OPC3(0x3d))
165 #define SAVE		(OPC1(0x2) | OPC3(0x3c))
166 #define SETHI		(OPC1(0x0) | OPC2(0x04))
167 #define SLL		(OPC1(0x2) | OPC3(0x25))
168 #define SLLX		(OPC1(0x2) | OPC3(0x25) | (1 << 12))
169 #define SRA		(OPC1(0x2) | OPC3(0x27))
170 #define SRAX		(OPC1(0x2) | OPC3(0x27) | (1 << 12))
171 #define SRL		(OPC1(0x2) | OPC3(0x26))
172 #define SRLX		(OPC1(0x2) | OPC3(0x26) | (1 << 12))
173 #define STDF		(OPC1(0x3) | OPC3(0x27))
174 #define STF		(OPC1(0x3) | OPC3(0x24))
175 #define STW		(OPC1(0x3) | OPC3(0x04))
176 #define SUB		(OPC1(0x2) | OPC3(0x04))
177 #define SUBC		(OPC1(0x2) | OPC3(0x0c))
178 #define TA		(OPC1(0x2) | OPC3(0x3a) | (8 << 25))
179 #define WRY		(OPC1(0x2) | OPC3(0x30) | DA(0))
180 #define XOR		(OPC1(0x2) | OPC3(0x03))
181 #define XNOR		(OPC1(0x2) | OPC3(0x07))
182 
183 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
184 #define MAX_DISP	(0x1fffff)
185 #define MIN_DISP	(-0x200000)
186 #define DISP_MASK	(0x3fffff)
187 
188 #define BICC		(OPC1(0x0) | OPC2(0x2))
189 #define FBFCC		(OPC1(0x0) | OPC2(0x6))
190 #define SLL_W		SLL
191 #define SDIV		(OPC1(0x2) | OPC3(0x0f))
192 #define SMUL		(OPC1(0x2) | OPC3(0x0b))
193 #define UDIV		(OPC1(0x2) | OPC3(0x0e))
194 #define UMUL		(OPC1(0x2) | OPC3(0x0a))
195 #else
196 #define SLL_W		SLLX
197 #endif
198 
199 #define SIMM_MAX	(0x0fff)
200 #define SIMM_MIN	(-0x1000)
201 
202 /* dest_reg is the absolute name of the register
203    Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins,sljit_s32 delay_slot)204 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_s32 delay_slot)
205 {
206 	sljit_ins *ptr;
207 	SLJIT_ASSERT((delay_slot & DST_INS_MASK) == UNMOVABLE_INS
208 		|| (delay_slot & DST_INS_MASK) == MOVABLE_INS
209 		|| (delay_slot & DST_INS_MASK) == ((ins >> 25) & 0x1f));
210 	ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
211 	FAIL_IF(!ptr);
212 	*ptr = ins;
213 	compiler->size++;
214 	compiler->delay_slot = delay_slot;
215 	return SLJIT_SUCCESS;
216 }
217 
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code,sljit_sw executable_offset)218 static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
219 {
220 	sljit_sw diff;
221 	sljit_uw target_addr;
222 	sljit_ins *inst;
223 	sljit_ins saved_inst;
224 
225 	if (jump->flags & SLJIT_REWRITABLE_JUMP)
226 		return code_ptr;
227 
228 	if (jump->flags & JUMP_ADDR)
229 		target_addr = jump->u.target;
230 	else {
231 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
232 		target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
233 	}
234 	inst = (sljit_ins*)jump->addr;
235 
236 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
237 	if (jump->flags & IS_CALL) {
238 		/* Call is always patchable on sparc 32. */
239 		jump->flags |= PATCH_CALL;
240 		if (jump->flags & IS_MOVABLE) {
241 			inst[0] = inst[-1];
242 			inst[-1] = CALL;
243 			jump->addr -= sizeof(sljit_ins);
244 			return inst;
245 		}
246 		inst[0] = CALL;
247 		inst[1] = NOP;
248 		return inst + 1;
249 	}
250 #else
251 	/* Both calls and BPr instructions shall not pass this point. */
252 #error "Implementation required"
253 #endif
254 
255 	if (jump->flags & IS_COND)
256 		inst--;
257 
258 	diff = ((sljit_sw)target_addr - (sljit_sw)(inst - 1) - executable_offset) >> 2;
259 
260 	if (jump->flags & IS_MOVABLE) {
261 		if (diff <= MAX_DISP && diff >= MIN_DISP) {
262 			jump->flags |= PATCH_B;
263 			inst--;
264 			if (jump->flags & IS_COND) {
265 				saved_inst = inst[0];
266 				inst[0] = inst[1] ^ (1 << 28);
267 				inst[1] = saved_inst;
268 			} else {
269 				inst[1] = inst[0];
270 				inst[0] = BICC | DA(0x8);
271 			}
272 			jump->addr = (sljit_uw)inst;
273 			return inst + 1;
274 		}
275 	}
276 
277 	diff += sizeof(sljit_ins);
278 
279 	if (diff <= MAX_DISP && diff >= MIN_DISP) {
280 		jump->flags |= PATCH_B;
281 		if (jump->flags & IS_COND)
282 			inst[0] ^= (1 << 28);
283 		else
284 			inst[0] = BICC | DA(0x8);
285 		inst[1] = NOP;
286 		jump->addr = (sljit_uw)inst;
287 		return inst + 1;
288 	}
289 
290 	return code_ptr;
291 }
292 
sljit_generate_code(struct sljit_compiler * compiler)293 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
294 {
295 	struct sljit_memory_fragment *buf;
296 	sljit_ins *code;
297 	sljit_ins *code_ptr;
298 	sljit_ins *buf_ptr;
299 	sljit_ins *buf_end;
300 	sljit_uw word_count;
301 	sljit_uw next_addr;
302 	sljit_sw executable_offset;
303 	sljit_uw addr;
304 
305 	struct sljit_label *label;
306 	struct sljit_jump *jump;
307 	struct sljit_const *const_;
308 	struct sljit_put_label *put_label;
309 
310 	CHECK_ERROR_PTR();
311 	CHECK_PTR(check_sljit_generate_code(compiler));
312 	reverse_buf(compiler);
313 
314 	code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
315 	PTR_FAIL_WITH_EXEC_IF(code);
316 	buf = compiler->buf;
317 
318 	code_ptr = code;
319 	word_count = 0;
320 	next_addr = 0;
321 	executable_offset = SLJIT_EXEC_OFFSET(code);
322 
323 	label = compiler->labels;
324 	jump = compiler->jumps;
325 	const_ = compiler->consts;
326 	put_label = compiler->put_labels;
327 
328 	do {
329 		buf_ptr = (sljit_ins*)buf->memory;
330 		buf_end = buf_ptr + (buf->used_size >> 2);
331 		do {
332 			*code_ptr = *buf_ptr++;
333 			if (next_addr == word_count) {
334 				SLJIT_ASSERT(!label || label->size >= word_count);
335 				SLJIT_ASSERT(!jump || jump->addr >= word_count);
336 				SLJIT_ASSERT(!const_ || const_->addr >= word_count);
337 				SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
338 
339 				/* These structures are ordered by their address. */
340 				if (label && label->size == word_count) {
341 					/* Just recording the address. */
342 					label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
343 					label->size = code_ptr - code;
344 					label = label->next;
345 				}
346 				if (jump && jump->addr == word_count) {
347 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
348 					jump->addr = (sljit_uw)(code_ptr - 3);
349 #else
350 					jump->addr = (sljit_uw)(code_ptr - 6);
351 #endif
352 					code_ptr = detect_jump_type(jump, code_ptr, code, executable_offset);
353 					jump = jump->next;
354 				}
355 				if (const_ && const_->addr == word_count) {
356 					/* Just recording the address. */
357 					const_->addr = (sljit_uw)code_ptr;
358 					const_ = const_->next;
359 				}
360 				if (put_label && put_label->addr == word_count) {
361 					SLJIT_ASSERT(put_label->label);
362 					put_label->addr = (sljit_uw)code_ptr;
363 					put_label = put_label->next;
364 				}
365 				next_addr = compute_next_addr(label, jump, const_, put_label);
366 			}
367 			code_ptr ++;
368 			word_count ++;
369 		} while (buf_ptr < buf_end);
370 
371 		buf = buf->next;
372 	} while (buf);
373 
374 	if (label && label->size == word_count) {
375 		label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
376 		label->size = code_ptr - code;
377 		label = label->next;
378 	}
379 
380 	SLJIT_ASSERT(!label);
381 	SLJIT_ASSERT(!jump);
382 	SLJIT_ASSERT(!const_);
383 	SLJIT_ASSERT(!put_label);
384 	SLJIT_ASSERT(code_ptr - code <= (sljit_s32)compiler->size);
385 
386 	jump = compiler->jumps;
387 	while (jump) {
388 		do {
389 			addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
390 			buf_ptr = (sljit_ins *)jump->addr;
391 
392 			if (jump->flags & PATCH_CALL) {
393 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
394 				SLJIT_ASSERT((sljit_sw)addr <= 0x1fffffff && (sljit_sw)addr >= -0x20000000);
395 				buf_ptr[0] = CALL | (addr & 0x3fffffff);
396 				break;
397 			}
398 			if (jump->flags & PATCH_B) {
399 				addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
400 				SLJIT_ASSERT((sljit_sw)addr <= MAX_DISP && (sljit_sw)addr >= MIN_DISP);
401 				buf_ptr[0] = (buf_ptr[0] & ~DISP_MASK) | (addr & DISP_MASK);
402 				break;
403 			}
404 
405 			/* Set the fields of immediate loads. */
406 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
407 			SLJIT_ASSERT(((buf_ptr[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr[1] & 0xc1f83fff) == 0x80102000));
408 			buf_ptr[0] |= (addr >> 10) & 0x3fffff;
409 			buf_ptr[1] |= addr & 0x3ff;
410 #else
411 #error "Implementation required"
412 #endif
413 		} while (0);
414 		jump = jump->next;
415 	}
416 
417 	put_label = compiler->put_labels;
418 	while (put_label) {
419 		addr = put_label->label->addr;
420 		buf_ptr = (sljit_ins *)put_label->addr;
421 
422 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
423 		SLJIT_ASSERT(((buf_ptr[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr[1] & 0xc1f83fff) == 0x80102000));
424 		buf_ptr[0] |= (addr >> 10) & 0x3fffff;
425 		buf_ptr[1] |= addr & 0x3ff;
426 #else
427 #error "Implementation required"
428 #endif
429 		put_label = put_label->next;
430 	}
431 
432 	compiler->error = SLJIT_ERR_COMPILED;
433 	compiler->executable_offset = executable_offset;
434 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
435 
436 	code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
437 	code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
438 
439 	SLJIT_CACHE_FLUSH(code, code_ptr);
440 	return code;
441 }
442 
sljit_has_cpu_feature(sljit_s32 feature_type)443 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
444 {
445 	switch (feature_type) {
446 	case SLJIT_HAS_FPU:
447 #ifdef SLJIT_IS_FPU_AVAILABLE
448 		return SLJIT_IS_FPU_AVAILABLE;
449 #else
450 		/* Available by default. */
451 		return 1;
452 #endif
453 
454 	case SLJIT_HAS_ZERO_REGISTER:
455 		return 1;
456 
457 #if (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
458 	case SLJIT_HAS_CMOV:
459 		return 1;
460 #endif
461 
462 	default:
463 		return 0;
464 	}
465 }
466 
467 /* --------------------------------------------------------------------- */
468 /*  Entry, exit                                                          */
469 /* --------------------------------------------------------------------- */
470 
471 /* Creates an index in data_transfer_insts array. */
472 #define LOAD_DATA	0x01
473 #define WORD_DATA	0x00
474 #define BYTE_DATA	0x02
475 #define HALF_DATA	0x04
476 #define INT_DATA	0x06
477 #define SIGNED_DATA	0x08
478 /* Separates integer and floating point registers */
479 #define GPR_REG		0x0f
480 #define DOUBLE_DATA	0x10
481 #define SINGLE_DATA	0x12
482 
483 #define MEM_MASK	0x1f
484 
485 #define ARG_TEST	0x00020
486 #define ALT_KEEP_CACHE	0x00040
487 #define CUMULATIVE_OP	0x00080
488 #define IMM_OP		0x00100
489 #define SRC2_IMM	0x00200
490 
491 #define REG_DEST	0x00400
492 #define REG2_SOURCE	0x00800
493 #define SLOW_SRC1	0x01000
494 #define SLOW_SRC2	0x02000
495 #define SLOW_DEST	0x04000
496 
497 /* SET_FLAGS (0x10 << 19) also belong here! */
498 
499 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
500 #include "sljitNativeSPARC_32.c"
501 #else
502 #include "sljitNativeSPARC_64.c"
503 #endif
504 
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)505 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
506 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
507 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
508 {
509 	CHECK_ERROR();
510 	CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
511 	set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
512 
513 	local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
514 	compiler->local_size = local_size;
515 
516 	if (local_size <= SIMM_MAX) {
517 		FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | IMM(-local_size), UNMOVABLE_INS));
518 	}
519 	else {
520 		FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
521 		FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | S2(TMP_REG1), UNMOVABLE_INS));
522 	}
523 
524 	/* Arguments are in their appropriate registers. */
525 
526 	return SLJIT_SUCCESS;
527 }
528 
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)529 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
530 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
531 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
532 {
533 	CHECK_ERROR();
534 	CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
535 	set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
536 
537 	compiler->local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
538 	return SLJIT_SUCCESS;
539 }
540 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)541 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
542 {
543 	CHECK_ERROR();
544 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
545 
546 	if (op != SLJIT_MOV || !FAST_IS_REG(src)) {
547 		FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
548 		src = SLJIT_R0;
549 	}
550 
551 	FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
552 	return push_inst(compiler, RESTORE | D(SLJIT_R0) | S1(src) | S2(0), UNMOVABLE_INS);
553 }
554 
555 /* --------------------------------------------------------------------- */
556 /*  Operators                                                            */
557 /* --------------------------------------------------------------------- */
558 
559 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
560 #define ARCH_32_64(a, b)	a
561 #else
562 #define ARCH_32_64(a, b)	b
563 #endif
564 
565 static const sljit_ins data_transfer_insts[16 + 4] = {
566 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
567 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
568 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
569 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
570 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
571 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
572 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
573 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
574 
575 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
576 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
577 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
578 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
579 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
580 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
581 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
582 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
583 
584 /* d   s */ OPC1(3) | OPC3(0x27),
585 /* d   l */ OPC1(3) | OPC3(0x23),
586 /* s   s */ OPC1(3) | OPC3(0x24),
587 /* s   l */ OPC1(3) | OPC3(0x20),
588 };
589 
590 #undef ARCH_32_64
591 
592 /* 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)593 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
594 {
595 	SLJIT_ASSERT(arg & SLJIT_MEM);
596 
597 	if ((!(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN)
598 			|| ((arg & OFFS_REG_MASK) && (argw & 0x3) == 0)) {
599 		/* Works for both absoulte and relative addresses (immediate case). */
600 		if (SLJIT_UNLIKELY(flags & ARG_TEST))
601 			return 1;
602 		FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
603 			| ((flags & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg))
604 			| S1(arg & REG_MASK) | ((arg & OFFS_REG_MASK) ? S2(OFFS_REG(arg)) : IMM(argw)),
605 			((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
606 		return -1;
607 	}
608 	return 0;
609 }
610 
611 /* See getput_arg below.
612    Note: can_cache is called only for binary operators. Those
613    operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)614 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
615 {
616 	SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
617 
618 	/* Simple operation except for updates. */
619 	if (arg & OFFS_REG_MASK) {
620 		argw &= 0x3;
621 		SLJIT_ASSERT(argw);
622 		next_argw &= 0x3;
623 		if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == next_argw)
624 			return 1;
625 		return 0;
626 	}
627 
628 	if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
629 		return 1;
630 	return 0;
631 }
632 
633 /* 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)634 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)
635 {
636 	sljit_s32 base, arg2, delay_slot;
637 	sljit_ins dest;
638 
639 	SLJIT_ASSERT(arg & SLJIT_MEM);
640 	if (!(next_arg & SLJIT_MEM)) {
641 		next_arg = 0;
642 		next_argw = 0;
643 	}
644 
645 	base = arg & REG_MASK;
646 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
647 		argw &= 0x3;
648 
649 		/* Using the cache. */
650 		if (((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) && (argw == compiler->cache_argw))
651 			arg2 = TMP_REG3;
652 		else {
653 			if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == (next_argw & 0x3)) {
654 				compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
655 				compiler->cache_argw = argw;
656 				arg2 = TMP_REG3;
657 			}
658 			else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && reg != OFFS_REG(arg))
659 				arg2 = reg;
660 			else /* It must be a mov operation, so tmp1 must be free to use. */
661 				arg2 = TMP_REG1;
662 			FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1(OFFS_REG(arg)) | IMM_ARG | argw, DR(arg2)));
663 		}
664 	}
665 	else {
666 		/* Using the cache. */
667 		if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
668 			if (argw != compiler->cache_argw) {
669 				FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
670 				compiler->cache_argw = argw;
671 			}
672 			arg2 = TMP_REG3;
673 		} else {
674 			if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
675 				compiler->cache_arg = SLJIT_MEM;
676 				compiler->cache_argw = argw;
677 				arg2 = TMP_REG3;
678 			}
679 			else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
680 				arg2 = reg;
681 			else /* It must be a mov operation, so tmp1 must be free to use. */
682 				arg2 = TMP_REG1;
683 			FAIL_IF(load_immediate(compiler, arg2, argw));
684 		}
685 	}
686 
687 	dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg));
688 	delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
689 	if (!base)
690 		return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
691 	return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
692 }
693 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)694 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
695 {
696 	if (getput_arg_fast(compiler, flags, reg, arg, argw))
697 		return compiler->error;
698 	compiler->cache_arg = 0;
699 	compiler->cache_argw = 0;
700 	return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
701 }
702 
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)703 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)
704 {
705 	if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
706 		return compiler->error;
707 	return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
708 }
709 
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)710 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
711 	sljit_s32 dst, sljit_sw dstw,
712 	sljit_s32 src1, sljit_sw src1w,
713 	sljit_s32 src2, sljit_sw src2w)
714 {
715 	/* arg1 goes to TMP_REG1 or src reg
716 	   arg2 goes to TMP_REG2, imm or src reg
717 	   TMP_REG3 can be used for caching
718 	   result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
719 	sljit_s32 dst_r = TMP_REG2;
720 	sljit_s32 src1_r;
721 	sljit_sw src2_r = 0;
722 	sljit_s32 sugg_src2_r = TMP_REG2;
723 
724 	if (!(flags & ALT_KEEP_CACHE)) {
725 		compiler->cache_arg = 0;
726 		compiler->cache_argw = 0;
727 	}
728 
729 	if (dst != SLJIT_UNUSED) {
730 		if (FAST_IS_REG(dst)) {
731 			dst_r = dst;
732 			flags |= REG_DEST;
733 			if (op >= SLJIT_MOV && op <= SLJIT_MOV_P)
734 				sugg_src2_r = dst_r;
735 		}
736 		else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
737 			flags |= SLOW_DEST;
738 	}
739 
740 	if (flags & IMM_OP) {
741 		if ((src2 & SLJIT_IMM) && src2w) {
742 			if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
743 				flags |= SRC2_IMM;
744 				src2_r = src2w;
745 			}
746 		}
747 		if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
748 			if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
749 				flags |= SRC2_IMM;
750 				src2_r = src1w;
751 
752 				/* And swap arguments. */
753 				src1 = src2;
754 				src1w = src2w;
755 				src2 = SLJIT_IMM;
756 				/* src2w = src2_r unneeded. */
757 			}
758 		}
759 	}
760 
761 	/* Source 1. */
762 	if (FAST_IS_REG(src1))
763 		src1_r = src1;
764 	else if (src1 & SLJIT_IMM) {
765 		if (src1w) {
766 			FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
767 			src1_r = TMP_REG1;
768 		}
769 		else
770 			src1_r = 0;
771 	}
772 	else {
773 		if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
774 			FAIL_IF(compiler->error);
775 		else
776 			flags |= SLOW_SRC1;
777 		src1_r = TMP_REG1;
778 	}
779 
780 	/* Source 2. */
781 	if (FAST_IS_REG(src2)) {
782 		src2_r = src2;
783 		flags |= REG2_SOURCE;
784 		if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOV_P)
785 			dst_r = src2_r;
786 	}
787 	else if (src2 & SLJIT_IMM) {
788 		if (!(flags & SRC2_IMM)) {
789 			if (src2w) {
790 				FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
791 				src2_r = sugg_src2_r;
792 			}
793 			else {
794 				src2_r = 0;
795 				if ((op >= SLJIT_MOV && op <= SLJIT_MOV_P) && (dst & SLJIT_MEM))
796 					dst_r = 0;
797 			}
798 		}
799 	}
800 	else {
801 		if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
802 			FAIL_IF(compiler->error);
803 		else
804 			flags |= SLOW_SRC2;
805 		src2_r = sugg_src2_r;
806 	}
807 
808 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
809 		SLJIT_ASSERT(src2_r == TMP_REG2);
810 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
811 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
812 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
813 		}
814 		else {
815 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
816 			FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
817 		}
818 	}
819 	else if (flags & SLOW_SRC1)
820 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
821 	else if (flags & SLOW_SRC2)
822 		FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
823 
824 	FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
825 
826 	if (dst & SLJIT_MEM) {
827 		if (!(flags & SLOW_DEST)) {
828 			getput_arg_fast(compiler, flags, dst_r, dst, dstw);
829 			return compiler->error;
830 		}
831 		return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
832 	}
833 
834 	return SLJIT_SUCCESS;
835 }
836 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)837 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
838 {
839 	CHECK_ERROR();
840 	CHECK(check_sljit_emit_op0(compiler, op));
841 
842 	op = GET_OPCODE(op);
843 	switch (op) {
844 	case SLJIT_BREAKPOINT:
845 		return push_inst(compiler, TA, UNMOVABLE_INS);
846 	case SLJIT_NOP:
847 		return push_inst(compiler, NOP, UNMOVABLE_INS);
848 	case SLJIT_LMUL_UW:
849 	case SLJIT_LMUL_SW:
850 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
851 		FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? UMUL : SMUL) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
852 		return push_inst(compiler, RDY | D(SLJIT_R1), DR(SLJIT_R1));
853 #else
854 #error "Implementation required"
855 #endif
856 	case SLJIT_DIVMOD_UW:
857 	case SLJIT_DIVMOD_SW:
858 	case SLJIT_DIV_UW:
859 	case SLJIT_DIV_SW:
860 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
861 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
862 		if ((op | 0x2) == SLJIT_DIV_UW)
863 			FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
864 		else {
865 			FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_R0) | IMM(31), DR(TMP_REG1)));
866 			FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
867 		}
868 		if (op <= SLJIT_DIVMOD_SW)
869 			FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_R0), DR(TMP_REG2)));
870 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? UDIV : SDIV) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
871 		if (op >= SLJIT_DIV_UW)
872 			return SLJIT_SUCCESS;
873 		FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_R1) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R1)));
874 		return push_inst(compiler, SUB | D(SLJIT_R1) | S1(TMP_REG2) | S2(SLJIT_R1), DR(SLJIT_R1));
875 #else
876 #error "Implementation required"
877 #endif
878 	case SLJIT_ENDBR:
879 	case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
880 		return SLJIT_SUCCESS;
881 	}
882 
883 	return SLJIT_SUCCESS;
884 }
885 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)886 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
887 	sljit_s32 dst, sljit_sw dstw,
888 	sljit_s32 src, sljit_sw srcw)
889 {
890 	sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
891 
892 	CHECK_ERROR();
893 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
894 	ADJUST_LOCAL_OFFSET(dst, dstw);
895 	ADJUST_LOCAL_OFFSET(src, srcw);
896 
897 	op = GET_OPCODE(op);
898 	switch (op) {
899 	case SLJIT_MOV:
900 	case SLJIT_MOV_P:
901 		return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
902 
903 	case SLJIT_MOV_U32:
904 		return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
905 
906 	case SLJIT_MOV_S32:
907 		return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
908 
909 	case SLJIT_MOV_U8:
910 		return emit_op(compiler, SLJIT_MOV_U8, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
911 
912 	case SLJIT_MOV_S8:
913 		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);
914 
915 	case SLJIT_MOV_U16:
916 		return emit_op(compiler, SLJIT_MOV_U16, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
917 
918 	case SLJIT_MOV_S16:
919 		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);
920 
921 	case SLJIT_NOT:
922 	case SLJIT_CLZ:
923 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
924 
925 	case SLJIT_NEG:
926 		return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
927 	}
928 
929 	return SLJIT_SUCCESS;
930 }
931 
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)932 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
933 	sljit_s32 dst, sljit_sw dstw,
934 	sljit_s32 src1, sljit_sw src1w,
935 	sljit_s32 src2, sljit_sw src2w)
936 {
937 	sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
938 
939 	CHECK_ERROR();
940 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
941 	ADJUST_LOCAL_OFFSET(dst, dstw);
942 	ADJUST_LOCAL_OFFSET(src1, src1w);
943 	ADJUST_LOCAL_OFFSET(src2, src2w);
944 
945 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
946 		return SLJIT_SUCCESS;
947 
948 	op = GET_OPCODE(op);
949 	switch (op) {
950 	case SLJIT_ADD:
951 	case SLJIT_ADDC:
952 	case SLJIT_MUL:
953 	case SLJIT_AND:
954 	case SLJIT_OR:
955 	case SLJIT_XOR:
956 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
957 
958 	case SLJIT_SUB:
959 	case SLJIT_SUBC:
960 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
961 
962 	case SLJIT_SHL:
963 	case SLJIT_LSHR:
964 	case SLJIT_ASHR:
965 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
966 		if (src2 & SLJIT_IMM)
967 			src2w &= 0x1f;
968 #else
969 		SLJIT_UNREACHABLE();
970 #endif
971 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
972 	}
973 
974 	return SLJIT_SUCCESS;
975 }
976 
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)977 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
978 	sljit_s32 src, sljit_sw srcw)
979 {
980 	CHECK_ERROR();
981 	CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
982 	ADJUST_LOCAL_OFFSET(src, srcw);
983 
984 	switch (op) {
985 	case SLJIT_FAST_RETURN:
986 		if (FAST_IS_REG(src))
987 			FAIL_IF(push_inst(compiler, OR | D(TMP_LINK) | S1(0) | S2(src), DR(TMP_LINK)));
988 		else
989 			FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_LINK, src, srcw));
990 
991 		FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(TMP_LINK) | IMM(8), UNMOVABLE_INS));
992 		return push_inst(compiler, NOP, UNMOVABLE_INS);
993 	case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
994 	case SLJIT_PREFETCH_L1:
995 	case SLJIT_PREFETCH_L2:
996 	case SLJIT_PREFETCH_L3:
997 	case SLJIT_PREFETCH_ONCE:
998 		return SLJIT_SUCCESS;
999 	}
1000 
1001 	return SLJIT_SUCCESS;
1002 }
1003 
sljit_get_register_index(sljit_s32 reg)1004 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1005 {
1006 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1007 	return reg_map[reg];
1008 }
1009 
sljit_get_float_register_index(sljit_s32 reg)1010 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1011 {
1012 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1013 	return freg_map[reg];
1014 }
1015 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1016 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1017 	void *instruction, sljit_s32 size)
1018 {
1019 	CHECK_ERROR();
1020 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1021 
1022 	return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
1023 }
1024 
1025 /* --------------------------------------------------------------------- */
1026 /*  Floating point operators                                             */
1027 /* --------------------------------------------------------------------- */
1028 
1029 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
1030 #define SELECT_FOP(op, single, double) ((op & SLJIT_F32_OP) ? single : double)
1031 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
1032 
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)1033 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1034 	sljit_s32 dst, sljit_sw dstw,
1035 	sljit_s32 src, sljit_sw srcw)
1036 {
1037 	if (src & SLJIT_MEM) {
1038 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1039 		src = TMP_FREG1;
1040 	}
1041 
1042 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOI, FDTOI) | FD(TMP_FREG1) | FS2(src), MOVABLE_INS));
1043 
1044 	if (FAST_IS_REG(dst)) {
1045 		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));
1046 		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);
1047 	}
1048 
1049 	/* Store the integer value from a VFP register. */
1050 	return emit_op_mem2(compiler, SINGLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
1051 }
1052 
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)1053 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1054 	sljit_s32 dst, sljit_sw dstw,
1055 	sljit_s32 src, sljit_sw srcw)
1056 {
1057 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1058 
1059 	if (src & SLJIT_IMM) {
1060 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1061 		if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1062 			srcw = (sljit_s32)srcw;
1063 #endif
1064 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1065 		src = TMP_REG1;
1066 		srcw = 0;
1067 	}
1068 
1069 	if (FAST_IS_REG(src)) {
1070 		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));
1071 		src = SLJIT_MEM1(SLJIT_SP);
1072 		srcw = FLOAT_TMP_MEM_OFFSET;
1073 	}
1074 
1075 	FAIL_IF(emit_op_mem2(compiler, SINGLE_DATA | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1076 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FITOS, FITOD) | FD(dst_r) | FS2(TMP_FREG1), MOVABLE_INS));
1077 
1078 	if (dst & SLJIT_MEM)
1079 		return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
1080 	return SLJIT_SUCCESS;
1081 }
1082 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1083 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1084 	sljit_s32 src1, sljit_sw src1w,
1085 	sljit_s32 src2, sljit_sw src2w)
1086 {
1087 	if (src1 & SLJIT_MEM) {
1088 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1089 		src1 = TMP_FREG1;
1090 	}
1091 
1092 	if (src2 & SLJIT_MEM) {
1093 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1094 		src2 = TMP_FREG2;
1095 	}
1096 
1097 	return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | FS1(src1) | FS2(src2), FCC_IS_SET | MOVABLE_INS);
1098 }
1099 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1100 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1101 	sljit_s32 dst, sljit_sw dstw,
1102 	sljit_s32 src, sljit_sw srcw)
1103 {
1104 	sljit_s32 dst_r;
1105 
1106 	CHECK_ERROR();
1107 	compiler->cache_arg = 0;
1108 	compiler->cache_argw = 0;
1109 
1110 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1111 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1112 
1113 	if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
1114 		op ^= SLJIT_F32_OP;
1115 
1116 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1117 
1118 	if (src & SLJIT_MEM) {
1119 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
1120 		src = dst_r;
1121 	}
1122 
1123 	switch (GET_OPCODE(op)) {
1124 	case SLJIT_MOV_F64:
1125 		if (src != dst_r) {
1126 			if (dst_r != TMP_FREG1) {
1127 				FAIL_IF(push_inst(compiler, FMOVS | FD(dst_r) | FS2(src), MOVABLE_INS));
1128 				if (!(op & SLJIT_F32_OP))
1129 					FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1130 			}
1131 			else
1132 				dst_r = src;
1133 		}
1134 		break;
1135 	case SLJIT_NEG_F64:
1136 		FAIL_IF(push_inst(compiler, FNEGS | FD(dst_r) | FS2(src), MOVABLE_INS));
1137 		if (dst_r != src && !(op & SLJIT_F32_OP))
1138 			FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1139 		break;
1140 	case SLJIT_ABS_F64:
1141 		FAIL_IF(push_inst(compiler, FABSS | FD(dst_r) | FS2(src), MOVABLE_INS));
1142 		if (dst_r != src && !(op & SLJIT_F32_OP))
1143 			FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1144 		break;
1145 	case SLJIT_CONV_F64_FROM_F32:
1146 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOD, FDTOS) | FD(dst_r) | FS2(src), MOVABLE_INS));
1147 		op ^= SLJIT_F32_OP;
1148 		break;
1149 	}
1150 
1151 	if (dst & SLJIT_MEM)
1152 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0));
1153 	return SLJIT_SUCCESS;
1154 }
1155 
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)1156 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1157 	sljit_s32 dst, sljit_sw dstw,
1158 	sljit_s32 src1, sljit_sw src1w,
1159 	sljit_s32 src2, sljit_sw src2w)
1160 {
1161 	sljit_s32 dst_r, flags = 0;
1162 
1163 	CHECK_ERROR();
1164 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1165 	ADJUST_LOCAL_OFFSET(dst, dstw);
1166 	ADJUST_LOCAL_OFFSET(src1, src1w);
1167 	ADJUST_LOCAL_OFFSET(src2, src2w);
1168 
1169 	compiler->cache_arg = 0;
1170 	compiler->cache_argw = 0;
1171 
1172 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG2;
1173 
1174 	if (src1 & SLJIT_MEM) {
1175 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1176 			FAIL_IF(compiler->error);
1177 			src1 = TMP_FREG1;
1178 		} else
1179 			flags |= SLOW_SRC1;
1180 	}
1181 
1182 	if (src2 & SLJIT_MEM) {
1183 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1184 			FAIL_IF(compiler->error);
1185 			src2 = TMP_FREG2;
1186 		} else
1187 			flags |= SLOW_SRC2;
1188 	}
1189 
1190 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1191 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1192 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1193 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1194 		}
1195 		else {
1196 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1197 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1198 		}
1199 	}
1200 	else if (flags & SLOW_SRC1)
1201 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1202 	else if (flags & SLOW_SRC2)
1203 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1204 
1205 	if (flags & SLOW_SRC1)
1206 		src1 = TMP_FREG1;
1207 	if (flags & SLOW_SRC2)
1208 		src2 = TMP_FREG2;
1209 
1210 	switch (GET_OPCODE(op)) {
1211 	case SLJIT_ADD_F64:
1212 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1213 		break;
1214 
1215 	case SLJIT_SUB_F64:
1216 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1217 		break;
1218 
1219 	case SLJIT_MUL_F64:
1220 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1221 		break;
1222 
1223 	case SLJIT_DIV_F64:
1224 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1225 		break;
1226 	}
1227 
1228 	if (dst_r == TMP_FREG2)
1229 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1230 
1231 	return SLJIT_SUCCESS;
1232 }
1233 
1234 #undef FLOAT_DATA
1235 #undef SELECT_FOP
1236 
1237 /* --------------------------------------------------------------------- */
1238 /*  Other instructions                                                   */
1239 /* --------------------------------------------------------------------- */
1240 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1241 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1242 {
1243 	CHECK_ERROR();
1244 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1245 	ADJUST_LOCAL_OFFSET(dst, dstw);
1246 
1247 	if (FAST_IS_REG(dst))
1248 		return push_inst(compiler, OR | D(dst) | S1(0) | S2(TMP_LINK), UNMOVABLE_INS);
1249 
1250 	/* Memory. */
1251 	FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_LINK, dst, dstw));
1252 	compiler->delay_slot = UNMOVABLE_INS;
1253 	return SLJIT_SUCCESS;
1254 }
1255 
1256 /* --------------------------------------------------------------------- */
1257 /*  Conditional instructions                                             */
1258 /* --------------------------------------------------------------------- */
1259 
sljit_emit_label(struct sljit_compiler * compiler)1260 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1261 {
1262 	struct sljit_label *label;
1263 
1264 	CHECK_ERROR_PTR();
1265 	CHECK_PTR(check_sljit_emit_label(compiler));
1266 
1267 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1268 		return compiler->last_label;
1269 
1270 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1271 	PTR_FAIL_IF(!label);
1272 	set_label(label, compiler);
1273 	compiler->delay_slot = UNMOVABLE_INS;
1274 	return label;
1275 }
1276 
get_cc(sljit_s32 type)1277 static sljit_ins get_cc(sljit_s32 type)
1278 {
1279 	switch (type) {
1280 	case SLJIT_EQUAL:
1281 	case SLJIT_MUL_NOT_OVERFLOW:
1282 	case SLJIT_NOT_EQUAL_F64: /* Unordered. */
1283 		return DA(0x1);
1284 
1285 	case SLJIT_NOT_EQUAL:
1286 	case SLJIT_MUL_OVERFLOW:
1287 	case SLJIT_EQUAL_F64:
1288 		return DA(0x9);
1289 
1290 	case SLJIT_LESS:
1291 	case SLJIT_GREATER_F64: /* Unordered. */
1292 		return DA(0x5);
1293 
1294 	case SLJIT_GREATER_EQUAL:
1295 	case SLJIT_LESS_EQUAL_F64:
1296 		return DA(0xd);
1297 
1298 	case SLJIT_GREATER:
1299 	case SLJIT_GREATER_EQUAL_F64: /* Unordered. */
1300 		return DA(0xc);
1301 
1302 	case SLJIT_LESS_EQUAL:
1303 	case SLJIT_LESS_F64:
1304 		return DA(0x4);
1305 
1306 	case SLJIT_SIG_LESS:
1307 		return DA(0x3);
1308 
1309 	case SLJIT_SIG_GREATER_EQUAL:
1310 		return DA(0xb);
1311 
1312 	case SLJIT_SIG_GREATER:
1313 		return DA(0xa);
1314 
1315 	case SLJIT_SIG_LESS_EQUAL:
1316 		return DA(0x2);
1317 
1318 	case SLJIT_OVERFLOW:
1319 	case SLJIT_UNORDERED_F64:
1320 		return DA(0x7);
1321 
1322 	case SLJIT_NOT_OVERFLOW:
1323 	case SLJIT_ORDERED_F64:
1324 		return DA(0xf);
1325 
1326 	default:
1327 		SLJIT_UNREACHABLE();
1328 		return DA(0x8);
1329 	}
1330 }
1331 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1332 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1333 {
1334 	struct sljit_jump *jump;
1335 
1336 	CHECK_ERROR_PTR();
1337 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1338 
1339 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1340 	PTR_FAIL_IF(!jump);
1341 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1342 	type &= 0xff;
1343 
1344 	if (type < SLJIT_EQUAL_F64) {
1345 		jump->flags |= IS_COND;
1346 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1347 			jump->flags |= IS_MOVABLE;
1348 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1349 		PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1350 #else
1351 #error "Implementation required"
1352 #endif
1353 	}
1354 	else if (type < SLJIT_JUMP) {
1355 		jump->flags |= IS_COND;
1356 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1357 			jump->flags |= IS_MOVABLE;
1358 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1359 		PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1360 #else
1361 #error "Implementation required"
1362 #endif
1363 	}
1364 	else {
1365 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1366 			jump->flags |= IS_MOVABLE;
1367 		if (type >= SLJIT_FAST_CALL)
1368 			jump->flags |= IS_CALL;
1369 	}
1370 
1371 	PTR_FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1372 	PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(TMP_REG1) | IMM(0), UNMOVABLE_INS));
1373 	jump->addr = compiler->size;
1374 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1375 
1376 	return jump;
1377 }
1378 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)1379 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1380 	sljit_s32 arg_types)
1381 {
1382 	CHECK_ERROR_PTR();
1383 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1384 
1385 	PTR_FAIL_IF(call_with_args(compiler, arg_types, NULL));
1386 
1387 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1388 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1389 	compiler->skip_checks = 1;
1390 #endif
1391 
1392 	return sljit_emit_jump(compiler, type);
1393 }
1394 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1395 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1396 {
1397 	struct sljit_jump *jump = NULL;
1398 	sljit_s32 src_r;
1399 
1400 	CHECK_ERROR();
1401 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1402 	ADJUST_LOCAL_OFFSET(src, srcw);
1403 
1404 	if (FAST_IS_REG(src))
1405 		src_r = src;
1406 	else if (src & SLJIT_IMM) {
1407 		jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1408 		FAIL_IF(!jump);
1409 		set_jump(jump, compiler, JUMP_ADDR);
1410 		jump->u.target = srcw;
1411 
1412 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1413 			jump->flags |= IS_MOVABLE;
1414 		if (type >= SLJIT_FAST_CALL)
1415 			jump->flags |= IS_CALL;
1416 
1417 		FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1418 		src_r = TMP_REG1;
1419 	}
1420 	else {
1421 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw));
1422 		src_r = TMP_REG1;
1423 	}
1424 
1425 	FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1426 	if (jump)
1427 		jump->addr = compiler->size;
1428 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1429 }
1430 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)1431 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1432 	sljit_s32 arg_types,
1433 	sljit_s32 src, sljit_sw srcw)
1434 {
1435 	CHECK_ERROR();
1436 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1437 
1438 	if (src & SLJIT_MEM) {
1439 		ADJUST_LOCAL_OFFSET(src, srcw);
1440 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw));
1441 		src = TMP_REG1;
1442 	}
1443 
1444 	FAIL_IF(call_with_args(compiler, arg_types, &src));
1445 
1446 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1447 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1448 	compiler->skip_checks = 1;
1449 #endif
1450 
1451 	return sljit_emit_ijump(compiler, type, src, srcw);
1452 }
1453 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)1454 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1455 	sljit_s32 dst, sljit_sw dstw,
1456 	sljit_s32 type)
1457 {
1458 	sljit_s32 reg, flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1459 
1460 	CHECK_ERROR();
1461 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1462 	ADJUST_LOCAL_OFFSET(dst, dstw);
1463 
1464 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1465 	op = GET_OPCODE(op);
1466 	reg = (op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2;
1467 
1468 	compiler->cache_arg = 0;
1469 	compiler->cache_argw = 0;
1470 
1471 	if (op >= SLJIT_ADD && (dst & SLJIT_MEM))
1472 		FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, dst, dstw, dst, dstw));
1473 
1474 	type &= 0xff;
1475 	if (type < SLJIT_EQUAL_F64)
1476 		FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1477 	else
1478 		FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1479 
1480 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1481 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1482 
1483 	if (op >= SLJIT_ADD) {
1484 		flags |= CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE;
1485 		if (dst & SLJIT_MEM)
1486 			return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1487 		return emit_op(compiler, op, flags, dst, 0, dst, 0, TMP_REG2, 0);
1488 	}
1489 
1490 	if (!(dst & SLJIT_MEM))
1491 		return SLJIT_SUCCESS;
1492 
1493 	return emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw);
1494 #else
1495 #error "Implementation required"
1496 #endif
1497 }
1498 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)1499 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1500 	sljit_s32 dst_reg,
1501 	sljit_s32 src, sljit_sw srcw)
1502 {
1503 	CHECK_ERROR();
1504 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1505 
1506 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1507 	return sljit_emit_cmov_generic(compiler, type, dst_reg, src, srcw);;
1508 #else
1509 #error "Implementation required"
1510 #endif
1511 }
1512 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)1513 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
1514 {
1515 	struct sljit_const *const_;
1516 	sljit_s32 dst_r;
1517 
1518 	CHECK_ERROR_PTR();
1519 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1520 	ADJUST_LOCAL_OFFSET(dst, dstw);
1521 
1522 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1523 	PTR_FAIL_IF(!const_);
1524 	set_const(const_, compiler);
1525 
1526 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
1527 	PTR_FAIL_IF(emit_const(compiler, dst_r, init_value));
1528 
1529 	if (dst & SLJIT_MEM)
1530 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1531 	return const_;
1532 }
1533 
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1534 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1535 {
1536 	struct sljit_put_label *put_label;
1537 	sljit_s32 dst_r;
1538 
1539 	CHECK_ERROR_PTR();
1540 	CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
1541 	ADJUST_LOCAL_OFFSET(dst, dstw);
1542 
1543 	put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
1544 	PTR_FAIL_IF(!put_label);
1545 	set_put_label(put_label, compiler, 0);
1546 
1547 	dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
1548 	PTR_FAIL_IF(emit_const(compiler, dst_r, 0));
1549 
1550 	if (dst & SLJIT_MEM)
1551 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1552 	return put_label;
1553 }
1554