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