• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE 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_UDIVMOD:
781 	case SLJIT_SDIVMOD:
782 	case SLJIT_UDIVI:
783 	case SLJIT_SDIVI:
784 		SLJIT_COMPILE_ASSERT((SLJIT_UDIVMOD & 0x2) == 0 && SLJIT_UDIVI - 0x2 == SLJIT_UDIVMOD, bad_div_opcode_assignments);
785 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
786 		if ((op | 0x2) == SLJIT_UDIVI)
787 			FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
788 		else {
789 			FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_R0) | IMM(31), DR(TMP_REG1)));
790 			FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
791 		}
792 		if (op <= SLJIT_SDIVMOD)
793 			FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_R0), DR(TMP_REG2)));
794 		FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_UDIVI ? UDIV : SDIV) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
795 		if (op >= SLJIT_UDIVI)
796 			return SLJIT_SUCCESS;
797 		FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_R1) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R1)));
798 		return push_inst(compiler, SUB | D(SLJIT_R1) | S1(TMP_REG2) | S2(SLJIT_R1), DR(SLJIT_R1));
799 #else
800 #error "Implementation required"
801 #endif
802 	}
803 
804 	return SLJIT_SUCCESS;
805 }
806 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_si op,sljit_si dst,sljit_sw dstw,sljit_si src,sljit_sw srcw)807 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
808 	sljit_si dst, sljit_sw dstw,
809 	sljit_si src, sljit_sw srcw)
810 {
811 	sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
812 
813 	CHECK_ERROR();
814 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
815 	ADJUST_LOCAL_OFFSET(dst, dstw);
816 	ADJUST_LOCAL_OFFSET(src, srcw);
817 
818 	op = GET_OPCODE(op);
819 	switch (op) {
820 	case SLJIT_MOV:
821 	case SLJIT_MOV_P:
822 		return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
823 
824 	case SLJIT_MOV_UI:
825 		return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
826 
827 	case SLJIT_MOV_SI:
828 		return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
829 
830 	case SLJIT_MOV_UB:
831 		return emit_op(compiler, SLJIT_MOV_UB, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
832 
833 	case SLJIT_MOV_SB:
834 		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);
835 
836 	case SLJIT_MOV_UH:
837 		return emit_op(compiler, SLJIT_MOV_UH, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
838 
839 	case SLJIT_MOV_SH:
840 		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);
841 
842 	case SLJIT_MOVU:
843 	case SLJIT_MOVU_P:
844 		return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
845 
846 	case SLJIT_MOVU_UI:
847 		return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
848 
849 	case SLJIT_MOVU_SI:
850 		return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
851 
852 	case SLJIT_MOVU_UB:
853 		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);
854 
855 	case SLJIT_MOVU_SB:
856 		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);
857 
858 	case SLJIT_MOVU_UH:
859 		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);
860 
861 	case SLJIT_MOVU_SH:
862 		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);
863 
864 	case SLJIT_NOT:
865 	case SLJIT_CLZ:
866 		return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
867 
868 	case SLJIT_NEG:
869 		return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
870 	}
871 
872 	return SLJIT_SUCCESS;
873 }
874 
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)875 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
876 	sljit_si dst, sljit_sw dstw,
877 	sljit_si src1, sljit_sw src1w,
878 	sljit_si src2, sljit_sw src2w)
879 {
880 	sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
881 
882 	CHECK_ERROR();
883 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
884 	ADJUST_LOCAL_OFFSET(dst, dstw);
885 	ADJUST_LOCAL_OFFSET(src1, src1w);
886 	ADJUST_LOCAL_OFFSET(src2, src2w);
887 
888 	op = GET_OPCODE(op);
889 	switch (op) {
890 	case SLJIT_ADD:
891 	case SLJIT_ADDC:
892 	case SLJIT_MUL:
893 	case SLJIT_AND:
894 	case SLJIT_OR:
895 	case SLJIT_XOR:
896 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
897 
898 	case SLJIT_SUB:
899 	case SLJIT_SUBC:
900 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
901 
902 	case SLJIT_SHL:
903 	case SLJIT_LSHR:
904 	case SLJIT_ASHR:
905 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
906 		if (src2 & SLJIT_IMM)
907 			src2w &= 0x1f;
908 #else
909 		SLJIT_ASSERT_STOP();
910 #endif
911 		return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
912 	}
913 
914 	return SLJIT_SUCCESS;
915 }
916 
sljit_get_register_index(sljit_si reg)917 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
918 {
919 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
920 	return reg_map[reg];
921 }
922 
sljit_get_float_register_index(sljit_si reg)923 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
924 {
925 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
926 	return reg << 1;
927 }
928 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_si size)929 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
930 	void *instruction, sljit_si size)
931 {
932 	CHECK_ERROR();
933 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
934 
935 	return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
936 }
937 
938 /* --------------------------------------------------------------------- */
939 /*  Floating point operators                                             */
940 /* --------------------------------------------------------------------- */
941 
sljit_is_fpu_available(void)942 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
943 {
944 #ifdef SLJIT_IS_FPU_AVAILABLE
945 	return SLJIT_IS_FPU_AVAILABLE;
946 #else
947 	/* Available by default. */
948 	return 1;
949 #endif
950 }
951 
952 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 7))
953 #define SELECT_FOP(op, single, double) ((op & SLJIT_SINGLE_OP) ? single : double)
954 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
955 
sljit_emit_fop1_convw_fromd(struct sljit_compiler * compiler,sljit_si op,sljit_si dst,sljit_sw dstw,sljit_si src,sljit_sw srcw)956 static SLJIT_INLINE sljit_si sljit_emit_fop1_convw_fromd(struct sljit_compiler *compiler, sljit_si op,
957 	sljit_si dst, sljit_sw dstw,
958 	sljit_si src, sljit_sw srcw)
959 {
960 	if (src & SLJIT_MEM) {
961 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
962 		src = TMP_FREG1;
963 	}
964 	else
965 		src <<= 1;
966 
967 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOI, FDTOI) | DA(TMP_FREG1) | S2A(src), MOVABLE_INS));
968 
969 	if (dst == SLJIT_UNUSED)
970 		return SLJIT_SUCCESS;
971 
972 	if (FAST_IS_REG(dst)) {
973 		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));
974 		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);
975 	}
976 
977 	/* Store the integer value from a VFP register. */
978 	return emit_op_mem2(compiler, SINGLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
979 }
980 
sljit_emit_fop1_convd_fromw(struct sljit_compiler * compiler,sljit_si op,sljit_si dst,sljit_sw dstw,sljit_si src,sljit_sw srcw)981 static SLJIT_INLINE sljit_si sljit_emit_fop1_convd_fromw(struct sljit_compiler *compiler, sljit_si op,
982 	sljit_si dst, sljit_sw dstw,
983 	sljit_si src, sljit_sw srcw)
984 {
985 	sljit_si dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
986 
987 	if (src & SLJIT_IMM) {
988 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
989 		if (GET_OPCODE(op) == SLJIT_CONVD_FROMI)
990 			srcw = (sljit_si)srcw;
991 #endif
992 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
993 		src = TMP_REG1;
994 		srcw = 0;
995 	}
996 
997 	if (FAST_IS_REG(src)) {
998 		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));
999 		src = SLJIT_MEM1(SLJIT_SP);
1000 		srcw = FLOAT_TMP_MEM_OFFSET;
1001 	}
1002 
1003 	FAIL_IF(emit_op_mem2(compiler, SINGLE_DATA | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1004 	FAIL_IF(push_inst(compiler, SELECT_FOP(op, FITOS, FITOD) | DA(dst_r) | S2A(TMP_FREG1), MOVABLE_INS));
1005 
1006 	if (dst & SLJIT_MEM)
1007 		return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
1008 	return SLJIT_SUCCESS;
1009 }
1010 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_si op,sljit_si src1,sljit_sw src1w,sljit_si src2,sljit_sw src2w)1011 static SLJIT_INLINE sljit_si sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_si op,
1012 	sljit_si src1, sljit_sw src1w,
1013 	sljit_si src2, sljit_sw src2w)
1014 {
1015 	if (src1 & SLJIT_MEM) {
1016 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1017 		src1 = TMP_FREG1;
1018 	}
1019 	else
1020 		src1 <<= 1;
1021 
1022 	if (src2 & SLJIT_MEM) {
1023 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1024 		src2 = TMP_FREG2;
1025 	}
1026 	else
1027 		src2 <<= 1;
1028 
1029 	return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | S1A(src1) | S2A(src2), FCC_IS_SET | MOVABLE_INS);
1030 }
1031 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_si op,sljit_si dst,sljit_sw dstw,sljit_si src,sljit_sw srcw)1032 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1033 	sljit_si dst, sljit_sw dstw,
1034 	sljit_si src, sljit_sw srcw)
1035 {
1036 	sljit_si dst_r;
1037 
1038 	CHECK_ERROR();
1039 	compiler->cache_arg = 0;
1040 	compiler->cache_argw = 0;
1041 
1042 	SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1043 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1044 
1045 	if (GET_OPCODE(op) == SLJIT_CONVD_FROMS)
1046 		op ^= SLJIT_SINGLE_OP;
1047 
1048 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG1;
1049 
1050 	if (src & SLJIT_MEM) {
1051 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
1052 		src = dst_r;
1053 	}
1054 	else
1055 		src <<= 1;
1056 
1057 	switch (GET_OPCODE(op)) {
1058 	case SLJIT_DMOV:
1059 		if (src != dst_r) {
1060 			if (dst_r != TMP_FREG1) {
1061 				FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r) | S2A(src), MOVABLE_INS));
1062 				if (!(op & SLJIT_SINGLE_OP))
1063 					FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r | 1) | S2A(src | 1), MOVABLE_INS));
1064 			}
1065 			else
1066 				dst_r = src;
1067 		}
1068 		break;
1069 	case SLJIT_DNEG:
1070 		FAIL_IF(push_inst(compiler, FNEGS | 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_DABS:
1075 		FAIL_IF(push_inst(compiler, FABSS | DA(dst_r) | S2A(src), MOVABLE_INS));
1076 		if (dst_r != src && !(op & SLJIT_SINGLE_OP))
1077 			FAIL_IF(push_inst(compiler, FMOVS | DA(dst_r | 1) | S2A(src | 1), MOVABLE_INS));
1078 		break;
1079 	case SLJIT_CONVD_FROMS:
1080 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOD, FDTOS) | DA(dst_r) | S2A(src), MOVABLE_INS));
1081 		op ^= SLJIT_SINGLE_OP;
1082 		break;
1083 	}
1084 
1085 	if (dst & SLJIT_MEM)
1086 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0));
1087 	return SLJIT_SUCCESS;
1088 }
1089 
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)1090 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1091 	sljit_si dst, sljit_sw dstw,
1092 	sljit_si src1, sljit_sw src1w,
1093 	sljit_si src2, sljit_sw src2w)
1094 {
1095 	sljit_si dst_r, flags = 0;
1096 
1097 	CHECK_ERROR();
1098 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1099 	ADJUST_LOCAL_OFFSET(dst, dstw);
1100 	ADJUST_LOCAL_OFFSET(src1, src1w);
1101 	ADJUST_LOCAL_OFFSET(src2, src2w);
1102 
1103 	compiler->cache_arg = 0;
1104 	compiler->cache_argw = 0;
1105 
1106 	dst_r = FAST_IS_REG(dst) ? (dst << 1) : TMP_FREG2;
1107 
1108 	if (src1 & SLJIT_MEM) {
1109 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1110 			FAIL_IF(compiler->error);
1111 			src1 = TMP_FREG1;
1112 		} else
1113 			flags |= SLOW_SRC1;
1114 	}
1115 	else
1116 		src1 <<= 1;
1117 
1118 	if (src2 & SLJIT_MEM) {
1119 		if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1120 			FAIL_IF(compiler->error);
1121 			src2 = TMP_FREG2;
1122 		} else
1123 			flags |= SLOW_SRC2;
1124 	}
1125 	else
1126 		src2 <<= 1;
1127 
1128 	if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1129 		if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1130 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1131 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1132 		}
1133 		else {
1134 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1135 			FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1136 		}
1137 	}
1138 	else if (flags & SLOW_SRC1)
1139 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1140 	else if (flags & SLOW_SRC2)
1141 		FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1142 
1143 	if (flags & SLOW_SRC1)
1144 		src1 = TMP_FREG1;
1145 	if (flags & SLOW_SRC2)
1146 		src2 = TMP_FREG2;
1147 
1148 	switch (GET_OPCODE(op)) {
1149 	case SLJIT_DADD:
1150 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1151 		break;
1152 
1153 	case SLJIT_DSUB:
1154 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1155 		break;
1156 
1157 	case SLJIT_DMUL:
1158 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1159 		break;
1160 
1161 	case SLJIT_DDIV:
1162 		FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_r) | S1A(src1) | S2A(src2), MOVABLE_INS));
1163 		break;
1164 	}
1165 
1166 	if (dst_r == TMP_FREG2)
1167 		FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1168 
1169 	return SLJIT_SUCCESS;
1170 }
1171 
1172 #undef FLOAT_DATA
1173 #undef SELECT_FOP
1174 
1175 /* --------------------------------------------------------------------- */
1176 /*  Other instructions                                                   */
1177 /* --------------------------------------------------------------------- */
1178 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_si dst,sljit_sw dstw)1179 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1180 {
1181 	CHECK_ERROR();
1182 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1183 	ADJUST_LOCAL_OFFSET(dst, dstw);
1184 
1185 	/* For UNUSED dst. Uncommon, but possible. */
1186 	if (dst == SLJIT_UNUSED)
1187 		return SLJIT_SUCCESS;
1188 
1189 	if (FAST_IS_REG(dst))
1190 		return push_inst(compiler, OR | D(dst) | S1(0) | S2(TMP_LINK), DR(dst));
1191 
1192 	/* Memory. */
1193 	return emit_op_mem(compiler, WORD_DATA, TMP_LINK, dst, dstw);
1194 }
1195 
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_si src,sljit_sw srcw)1196 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1197 {
1198 	CHECK_ERROR();
1199 	CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
1200 	ADJUST_LOCAL_OFFSET(src, srcw);
1201 
1202 	if (FAST_IS_REG(src))
1203 		FAIL_IF(push_inst(compiler, OR | D(TMP_LINK) | S1(0) | S2(src), DR(TMP_LINK)));
1204 	else if (src & SLJIT_MEM)
1205 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_LINK, src, srcw));
1206 	else if (src & SLJIT_IMM)
1207 		FAIL_IF(load_immediate(compiler, TMP_LINK, srcw));
1208 
1209 	FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(TMP_LINK) | IMM(8), UNMOVABLE_INS));
1210 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1211 }
1212 
1213 /* --------------------------------------------------------------------- */
1214 /*  Conditional instructions                                             */
1215 /* --------------------------------------------------------------------- */
1216 
sljit_emit_label(struct sljit_compiler * compiler)1217 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1218 {
1219 	struct sljit_label *label;
1220 
1221 	CHECK_ERROR_PTR();
1222 	CHECK_PTR(check_sljit_emit_label(compiler));
1223 
1224 	if (compiler->last_label && compiler->last_label->size == compiler->size)
1225 		return compiler->last_label;
1226 
1227 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1228 	PTR_FAIL_IF(!label);
1229 	set_label(label, compiler);
1230 	compiler->delay_slot = UNMOVABLE_INS;
1231 	return label;
1232 }
1233 
get_cc(sljit_si type)1234 static sljit_ins get_cc(sljit_si type)
1235 {
1236 	switch (type) {
1237 	case SLJIT_EQUAL:
1238 	case SLJIT_MUL_NOT_OVERFLOW:
1239 	case SLJIT_D_NOT_EQUAL: /* Unordered. */
1240 		return DA(0x1);
1241 
1242 	case SLJIT_NOT_EQUAL:
1243 	case SLJIT_MUL_OVERFLOW:
1244 	case SLJIT_D_EQUAL:
1245 		return DA(0x9);
1246 
1247 	case SLJIT_LESS:
1248 	case SLJIT_D_GREATER: /* Unordered. */
1249 		return DA(0x5);
1250 
1251 	case SLJIT_GREATER_EQUAL:
1252 	case SLJIT_D_LESS_EQUAL:
1253 		return DA(0xd);
1254 
1255 	case SLJIT_GREATER:
1256 	case SLJIT_D_GREATER_EQUAL: /* Unordered. */
1257 		return DA(0xc);
1258 
1259 	case SLJIT_LESS_EQUAL:
1260 	case SLJIT_D_LESS:
1261 		return DA(0x4);
1262 
1263 	case SLJIT_SIG_LESS:
1264 		return DA(0x3);
1265 
1266 	case SLJIT_SIG_GREATER_EQUAL:
1267 		return DA(0xb);
1268 
1269 	case SLJIT_SIG_GREATER:
1270 		return DA(0xa);
1271 
1272 	case SLJIT_SIG_LESS_EQUAL:
1273 		return DA(0x2);
1274 
1275 	case SLJIT_OVERFLOW:
1276 	case SLJIT_D_UNORDERED:
1277 		return DA(0x7);
1278 
1279 	case SLJIT_NOT_OVERFLOW:
1280 	case SLJIT_D_ORDERED:
1281 		return DA(0xf);
1282 
1283 	default:
1284 		SLJIT_ASSERT_STOP();
1285 		return DA(0x8);
1286 	}
1287 }
1288 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_si type)1289 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1290 {
1291 	struct sljit_jump *jump;
1292 
1293 	CHECK_ERROR_PTR();
1294 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
1295 
1296 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1297 	PTR_FAIL_IF(!jump);
1298 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1299 	type &= 0xff;
1300 
1301 	if (type < SLJIT_D_EQUAL) {
1302 		jump->flags |= IS_COND;
1303 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1304 			jump->flags |= IS_MOVABLE;
1305 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1306 		PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1307 #else
1308 #error "Implementation required"
1309 #endif
1310 	}
1311 	else if (type < SLJIT_JUMP) {
1312 		jump->flags |= IS_COND;
1313 		if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1314 			jump->flags |= IS_MOVABLE;
1315 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1316 		PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1317 #else
1318 #error "Implementation required"
1319 #endif
1320 	} else {
1321 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1322 			jump->flags |= IS_MOVABLE;
1323 		if (type >= SLJIT_FAST_CALL)
1324 			jump->flags |= IS_CALL;
1325 	}
1326 
1327 	PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1328 	PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(TMP_REG2) | IMM(0), UNMOVABLE_INS));
1329 	jump->addr = compiler->size;
1330 	PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1331 
1332 	return jump;
1333 }
1334 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_si type,sljit_si src,sljit_sw srcw)1335 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1336 {
1337 	struct sljit_jump *jump = NULL;
1338 	sljit_si src_r;
1339 
1340 	CHECK_ERROR();
1341 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1342 	ADJUST_LOCAL_OFFSET(src, srcw);
1343 
1344 	if (FAST_IS_REG(src))
1345 		src_r = src;
1346 	else if (src & SLJIT_IMM) {
1347 		jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1348 		FAIL_IF(!jump);
1349 		set_jump(jump, compiler, JUMP_ADDR);
1350 		jump->u.target = srcw;
1351 		if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1352 			jump->flags |= IS_MOVABLE;
1353 		if (type >= SLJIT_FAST_CALL)
1354 			jump->flags |= IS_CALL;
1355 
1356 		FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1357 		src_r = TMP_REG2;
1358 	}
1359 	else {
1360 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1361 		src_r = TMP_REG2;
1362 	}
1363 
1364 	FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1365 	if (jump)
1366 		jump->addr = compiler->size;
1367 	return push_inst(compiler, NOP, UNMOVABLE_INS);
1368 }
1369 
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)1370 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1371 	sljit_si dst, sljit_sw dstw,
1372 	sljit_si src, sljit_sw srcw,
1373 	sljit_si type)
1374 {
1375 	sljit_si reg, flags = (GET_FLAGS(op) ? SET_FLAGS : 0);
1376 
1377 	CHECK_ERROR();
1378 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type));
1379 	ADJUST_LOCAL_OFFSET(dst, dstw);
1380 
1381 	if (dst == SLJIT_UNUSED)
1382 		return SLJIT_SUCCESS;
1383 
1384 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1385 	op = GET_OPCODE(op);
1386 	reg = (op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2;
1387 
1388 	compiler->cache_arg = 0;
1389 	compiler->cache_argw = 0;
1390 	if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
1391 		ADJUST_LOCAL_OFFSET(src, srcw);
1392 		FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
1393 		src = TMP_REG1;
1394 		srcw = 0;
1395 	}
1396 
1397 	type &= 0xff;
1398 	if (type < SLJIT_D_EQUAL)
1399 		FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1400 	else
1401 		FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1402 
1403 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1404 	FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1405 
1406 	if (op >= SLJIT_ADD)
1407 		return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE, dst, dstw, src, srcw, TMP_REG2, 0);
1408 
1409 	return (reg == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
1410 #else
1411 #error "Implementation required"
1412 #endif
1413 }
1414 
sljit_emit_const(struct sljit_compiler * compiler,sljit_si dst,sljit_sw dstw,sljit_sw init_value)1415 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1416 {
1417 	sljit_si reg;
1418 	struct sljit_const *const_;
1419 
1420 	CHECK_ERROR_PTR();
1421 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1422 	ADJUST_LOCAL_OFFSET(dst, dstw);
1423 
1424 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1425 	PTR_FAIL_IF(!const_);
1426 	set_const(const_, compiler);
1427 
1428 	reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
1429 
1430 	PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1431 
1432 	if (dst & SLJIT_MEM)
1433 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1434 	return const_;
1435 }
1436