1 /*
2  *    Stack-less Just-In-Time compiler
3  *
4  *    Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification, are
7  * permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright notice, this list of
10  *      conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright notice, this list
13  *      of conditions and the following disclaimer in the documentation and/or other materials
14  *      provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /* ppc 64-bit arch dependent functions. */
28 
29 #if defined(__GNUC__) || (defined(__IBM_GCC_ASM) && __IBM_GCC_ASM)
30 #define ASM_SLJIT_CLZ(src, dst) \
31 	__asm__ volatile ( "cntlzd %0, %1" : "=r"(dst) : "r"(src) )
32 #elif defined(__xlc__)
33 #error "Please enable GCC syntax for inline assembly statements"
34 #else
35 #error "Must implement count leading zeroes"
36 #endif
37 
38 #define PUSH_RLDICR(reg, shift) \
39 	push_inst(compiler, RLDI(reg, reg, 63 - shift, shift, 1))
40 
load_immediate(struct sljit_compiler * compiler,sljit_s32 reg,sljit_sw imm)41 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
42 {
43 	sljit_uw tmp;
44 	sljit_uw shift;
45 	sljit_uw tmp2;
46 	sljit_uw shift2;
47 
48 	if (imm <= SIMM_MAX && imm >= SIMM_MIN)
49 		return push_inst(compiler, ADDI | D(reg) | A(0) | IMM(imm));
50 
51 	if (!(imm & ~0xffff))
52 		return push_inst(compiler, ORI | S(TMP_ZERO) | A(reg) | IMM(imm));
53 
54 	if (imm <= 0x7fffffffl && imm >= -0x80000000l) {
55 		FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 16)));
56 		return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm)) : SLJIT_SUCCESS;
57 	}
58 
59 	/* Count leading zeroes. */
60 	tmp = (imm >= 0) ? imm : ~imm;
61 	ASM_SLJIT_CLZ(tmp, shift);
62 	SLJIT_ASSERT(shift > 0);
63 	shift--;
64 	tmp = (imm << shift);
65 
66 	if ((tmp & ~0xffff000000000000ul) == 0) {
67 		FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
68 		shift += 15;
69 		return PUSH_RLDICR(reg, shift);
70 	}
71 
72 	if ((tmp & ~0xffffffff00000000ul) == 0) {
73 		FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(tmp >> 48)));
74 		FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp >> 32)));
75 		shift += 31;
76 		return PUSH_RLDICR(reg, shift);
77 	}
78 
79 	/* Cut out the 16 bit from immediate. */
80 	shift += 15;
81 	tmp2 = imm & ((1ul << (63 - shift)) - 1);
82 
83 	if (tmp2 <= 0xffff) {
84 		FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
85 		FAIL_IF(PUSH_RLDICR(reg, shift));
86 		return push_inst(compiler, ORI | S(reg) | A(reg) | tmp2);
87 	}
88 
89 	if (tmp2 <= 0xffffffff) {
90 		FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
91 		FAIL_IF(PUSH_RLDICR(reg, shift));
92 		FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | (tmp2 >> 16)));
93 		return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(tmp2)) : SLJIT_SUCCESS;
94 	}
95 
96 	ASM_SLJIT_CLZ(tmp2, shift2);
97 	tmp2 <<= shift2;
98 
99 	if ((tmp2 & ~0xffff000000000000ul) == 0) {
100 		FAIL_IF(push_inst(compiler, ADDI | D(reg) | A(0) | IMM(tmp >> 48)));
101 		shift2 += 15;
102 		shift += (63 - shift2);
103 		FAIL_IF(PUSH_RLDICR(reg, shift));
104 		FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | (tmp2 >> 48)));
105 		return PUSH_RLDICR(reg, shift2);
106 	}
107 
108 	/* The general version. */
109 	FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 48)));
110 	FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm >> 32)));
111 	FAIL_IF(PUSH_RLDICR(reg, 31));
112 	FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(imm >> 16)));
113 	return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm));
114 }
115 
116 /* Simplified mnemonics: clrldi. */
117 #define INS_CLEAR_LEFT(dst, src, from) \
118 	(RLDICL | S(src) | A(dst) | ((from) << 6) | (1 << 5))
119 
120 /* Sign extension for integer operations. */
121 #define UN_EXTS() \
122 	if ((flags & (ALT_SIGN_EXT | REG2_SOURCE)) == (ALT_SIGN_EXT | REG2_SOURCE)) { \
123 		FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
124 		src2 = TMP_REG2; \
125 	}
126 
127 #define BIN_EXTS() \
128 	if (flags & ALT_SIGN_EXT) { \
129 		if (flags & REG1_SOURCE) { \
130 			FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
131 			src1 = TMP_REG1; \
132 		} \
133 		if (flags & REG2_SOURCE) { \
134 			FAIL_IF(push_inst(compiler, EXTSW | S(src2) | A(TMP_REG2))); \
135 			src2 = TMP_REG2; \
136 		} \
137 	}
138 
139 #define BIN_IMM_EXTS() \
140 	if ((flags & (ALT_SIGN_EXT | REG1_SOURCE)) == (ALT_SIGN_EXT | REG1_SOURCE)) { \
141 		FAIL_IF(push_inst(compiler, EXTSW | S(src1) | A(TMP_REG1))); \
142 		src1 = TMP_REG1; \
143 	}
144 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_s32 src1,sljit_s32 src2)145 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
146 	sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
147 {
148 	switch (op) {
149 	case SLJIT_MOV:
150 	case SLJIT_MOV_P:
151 		SLJIT_ASSERT(src1 == TMP_REG1);
152 		if (dst != src2)
153 			return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
154 		return SLJIT_SUCCESS;
155 
156 	case SLJIT_MOV_U32:
157 	case SLJIT_MOV_S32:
158 		SLJIT_ASSERT(src1 == TMP_REG1);
159 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
160 			if (op == SLJIT_MOV_S32)
161 				return push_inst(compiler, EXTSW | S(src2) | A(dst));
162 			return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 0));
163 		}
164 		else {
165 			SLJIT_ASSERT(dst == src2);
166 		}
167 		return SLJIT_SUCCESS;
168 
169 	case SLJIT_MOV_U8:
170 	case SLJIT_MOV_S8:
171 		SLJIT_ASSERT(src1 == TMP_REG1);
172 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
173 			if (op == SLJIT_MOV_S8)
174 				return push_inst(compiler, EXTSB | S(src2) | A(dst));
175 			return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
176 		}
177 		else if ((flags & REG_DEST) && op == SLJIT_MOV_S8)
178 			return push_inst(compiler, EXTSB | S(src2) | A(dst));
179 		else {
180 			SLJIT_ASSERT(dst == src2);
181 		}
182 		return SLJIT_SUCCESS;
183 
184 	case SLJIT_MOV_U16:
185 	case SLJIT_MOV_S16:
186 		SLJIT_ASSERT(src1 == TMP_REG1);
187 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
188 			if (op == SLJIT_MOV_S16)
189 				return push_inst(compiler, EXTSH | S(src2) | A(dst));
190 			return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
191 		}
192 		else {
193 			SLJIT_ASSERT(dst == src2);
194 		}
195 		return SLJIT_SUCCESS;
196 
197 	case SLJIT_NOT:
198 		SLJIT_ASSERT(src1 == TMP_REG1);
199 		UN_EXTS();
200 		return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
201 
202 	case SLJIT_NEG:
203 		SLJIT_ASSERT(src1 == TMP_REG1);
204 
205 		if ((flags & (ALT_FORM1 | ALT_SIGN_EXT)) == (ALT_FORM1 | ALT_SIGN_EXT)) {
206 			FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
207 			FAIL_IF(push_inst(compiler, NEG | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(TMP_REG2)));
208 			return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
209 		}
210 
211 		UN_EXTS();
212 		/* Setting XER SO is not enough, CR SO is also needed. */
213 		return push_inst(compiler, NEG | OE((flags & ALT_FORM1) ? ALT_SET_FLAGS : 0) | RC(flags) | D(dst) | A(src2));
214 
215 	case SLJIT_CLZ:
216 		SLJIT_ASSERT(src1 == TMP_REG1);
217 		if (flags & ALT_FORM1)
218 			return push_inst(compiler, CNTLZW | S(src2) | A(dst));
219 		return push_inst(compiler, CNTLZD | S(src2) | A(dst));
220 
221 	case SLJIT_ADD:
222 		if (flags & ALT_FORM1) {
223 			if (flags & ALT_SIGN_EXT) {
224 				FAIL_IF(push_inst(compiler, RLDI(TMP_REG1, src1, 32, 31, 1)));
225 				src1 = TMP_REG1;
226 				FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
227 				src2 = TMP_REG2;
228 			}
229 			/* Setting XER SO is not enough, CR SO is also needed. */
230 			FAIL_IF(push_inst(compiler, ADD | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2)));
231 			if (flags & ALT_SIGN_EXT)
232 				return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
233 			return SLJIT_SUCCESS;
234 		}
235 
236 		if (flags & ALT_FORM2) {
237 			/* Flags does not set: BIN_IMM_EXTS unnecessary. */
238 			SLJIT_ASSERT(src2 == TMP_REG2);
239 
240 			if (flags & ALT_FORM3)
241 				return push_inst(compiler, ADDIS | D(dst) | A(src1) | compiler->imm);
242 
243 			if (flags & ALT_FORM4) {
244 				FAIL_IF(push_inst(compiler, ADDIS | D(dst) | A(src1) | (((compiler->imm >> 16) & 0xffff) + ((compiler->imm >> 15) & 0x1))));
245 				src1 = dst;
246 			}
247 
248 			return push_inst(compiler, ADDI | D(dst) | A(src1) | (compiler->imm & 0xffff));
249 		}
250 		if (flags & ALT_FORM3) {
251 			SLJIT_ASSERT(src2 == TMP_REG2);
252 			BIN_IMM_EXTS();
253 			return push_inst(compiler, ADDIC | D(dst) | A(src1) | compiler->imm);
254 		}
255 		if (flags & ALT_FORM4) {
256 			if (flags & ALT_FORM5)
257 				FAIL_IF(push_inst(compiler, ADDI | D(dst) | A(src1) | compiler->imm));
258 			else
259 				FAIL_IF(push_inst(compiler, ADD | D(dst) | A(src1) | B(src2)));
260 			return push_inst(compiler, CMPI | A(dst) | 0);
261 		}
262 		if (!(flags & ALT_SET_FLAGS))
263 			return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
264 		BIN_EXTS();
265 		if (flags & ALT_FORM5)
266 			return push_inst(compiler, ADDC | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
267 		return push_inst(compiler, ADD | RC(flags) | D(dst) | A(src1) | B(src2));
268 
269 	case SLJIT_ADDC:
270 		BIN_EXTS();
271 		return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
272 
273 	case SLJIT_SUB:
274 		if (flags & ALT_FORM1) {
275 			if (flags & ALT_FORM2) {
276 				FAIL_IF(push_inst(compiler, CMPLI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm));
277 				if (!(flags & ALT_FORM3))
278 					return SLJIT_SUCCESS;
279 				return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
280 			}
281 			FAIL_IF(push_inst(compiler, CMPL | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
282 			if (!(flags & ALT_FORM3))
283 				return SLJIT_SUCCESS;
284 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
285 		}
286 
287 		if (flags & ALT_FORM2) {
288 			if (flags & ALT_FORM3) {
289 				FAIL_IF(push_inst(compiler, CMPI | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | compiler->imm));
290 				if (!(flags & ALT_FORM4))
291 					return SLJIT_SUCCESS;
292 				return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
293 			}
294 			FAIL_IF(push_inst(compiler, CMP | CRD(0 | ((flags & ALT_SIGN_EXT) ? 0 : 1)) | A(src1) | B(src2)));
295 			if (!(flags & ALT_FORM4))
296 				return SLJIT_SUCCESS;
297 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
298 		}
299 
300 		if (flags & ALT_FORM3) {
301 			if (flags & ALT_SIGN_EXT) {
302 				FAIL_IF(push_inst(compiler, RLDI(TMP_REG1, src1, 32, 31, 1)));
303 				src1 = TMP_REG1;
304 				FAIL_IF(push_inst(compiler, RLDI(TMP_REG2, src2, 32, 31, 1)));
305 				src2 = TMP_REG2;
306 			}
307 			/* Setting XER SO is not enough, CR SO is also needed. */
308 			FAIL_IF(push_inst(compiler, SUBF | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1)));
309 			if (flags & ALT_SIGN_EXT)
310 				return push_inst(compiler, RLDI(dst, dst, 32, 32, 0));
311 			return SLJIT_SUCCESS;
312 		}
313 
314 		if (flags & ALT_FORM4) {
315 			/* Flags does not set: BIN_IMM_EXTS unnecessary. */
316 			SLJIT_ASSERT(src2 == TMP_REG2);
317 			return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
318 		}
319 
320 		if (!(flags & ALT_SET_FLAGS))
321 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
322 		BIN_EXTS();
323 		if (flags & ALT_FORM5)
324 			return push_inst(compiler, SUBFC | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
325 		return push_inst(compiler, SUBF | RC(flags) | D(dst) | A(src2) | B(src1));
326 
327 	case SLJIT_SUBC:
328 		BIN_EXTS();
329 		return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
330 
331 	case SLJIT_MUL:
332 		if (flags & ALT_FORM1) {
333 			SLJIT_ASSERT(src2 == TMP_REG2);
334 			return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
335 		}
336 		BIN_EXTS();
337 		if (flags & ALT_FORM2)
338 			return push_inst(compiler, MULLW | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
339 		return push_inst(compiler, MULLD | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
340 
341 	case SLJIT_AND:
342 		if (flags & ALT_FORM1) {
343 			SLJIT_ASSERT(src2 == TMP_REG2);
344 			return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
345 		}
346 		if (flags & ALT_FORM2) {
347 			SLJIT_ASSERT(src2 == TMP_REG2);
348 			return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
349 		}
350 		return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
351 
352 	case SLJIT_OR:
353 		if (flags & ALT_FORM1) {
354 			SLJIT_ASSERT(src2 == TMP_REG2);
355 			return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
356 		}
357 		if (flags & ALT_FORM2) {
358 			SLJIT_ASSERT(src2 == TMP_REG2);
359 			return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
360 		}
361 		if (flags & ALT_FORM3) {
362 			SLJIT_ASSERT(src2 == TMP_REG2);
363 			FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
364 			return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
365 		}
366 		return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
367 
368 	case SLJIT_XOR:
369 		if (flags & ALT_FORM1) {
370 			SLJIT_ASSERT(src2 == TMP_REG2);
371 			return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
372 		}
373 		if (flags & ALT_FORM2) {
374 			SLJIT_ASSERT(src2 == TMP_REG2);
375 			return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
376 		}
377 		if (flags & ALT_FORM3) {
378 			SLJIT_ASSERT(src2 == TMP_REG2);
379 			FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
380 			return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
381 		}
382 		return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
383 
384 	case SLJIT_SHL:
385 		if (flags & ALT_FORM1) {
386 			SLJIT_ASSERT(src2 == TMP_REG2);
387 			if (flags & ALT_FORM2) {
388 				compiler->imm &= 0x1f;
389 				return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
390 			}
391 			compiler->imm &= 0x3f;
392 			return push_inst(compiler, RLDI(dst, src1, compiler->imm, 63 - compiler->imm, 1) | RC(flags));
393 		}
394 		return push_inst(compiler, ((flags & ALT_FORM2) ? SLW : SLD) | RC(flags) | S(src1) | A(dst) | B(src2));
395 
396 	case SLJIT_LSHR:
397 		if (flags & ALT_FORM1) {
398 			SLJIT_ASSERT(src2 == TMP_REG2);
399 			if (flags & ALT_FORM2) {
400 				compiler->imm &= 0x1f;
401 				return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
402 			}
403 			compiler->imm &= 0x3f;
404 			return push_inst(compiler, RLDI(dst, src1, 64 - compiler->imm, compiler->imm, 0) | RC(flags));
405 		}
406 		return push_inst(compiler, ((flags & ALT_FORM2) ? SRW : SRD) | RC(flags) | S(src1) | A(dst) | B(src2));
407 
408 	case SLJIT_ASHR:
409 		if (flags & ALT_FORM1) {
410 			SLJIT_ASSERT(src2 == TMP_REG2);
411 			if (flags & ALT_FORM2) {
412 				compiler->imm &= 0x1f;
413 				return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
414 			}
415 			compiler->imm &= 0x3f;
416 			return push_inst(compiler, SRADI | RC(flags) | S(src1) | A(dst) | ((compiler->imm & 0x1f) << 11) | ((compiler->imm & 0x20) >> 4));
417 		}
418 		return push_inst(compiler, ((flags & ALT_FORM2) ? SRAW : SRAD) | RC(flags) | S(src1) | A(dst) | B(src2));
419 	}
420 
421 	SLJIT_UNREACHABLE();
422 	return SLJIT_SUCCESS;
423 }
424 
call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)425 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
426 {
427 	sljit_s32 arg_count = 0;
428 	sljit_s32 word_arg_count = 0;
429 	sljit_s32 types = 0;
430 	sljit_s32 reg = 0;
431 
432 	if (src)
433 		reg = *src & REG_MASK;
434 
435 	arg_types >>= SLJIT_DEF_SHIFT;
436 
437 	while (arg_types) {
438 		types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
439 
440 		switch (arg_types & SLJIT_DEF_MASK) {
441 		case SLJIT_ARG_TYPE_F32:
442 		case SLJIT_ARG_TYPE_F64:
443 			arg_count++;
444 			break;
445 		default:
446 			arg_count++;
447 			word_arg_count++;
448 
449 			if (arg_count != word_arg_count && arg_count == reg) {
450 				FAIL_IF(push_inst(compiler, OR | S(reg) | A(TMP_CALL_REG) | B(reg)));
451 				*src = TMP_CALL_REG;
452 			}
453 			break;
454 		}
455 
456 		arg_types >>= SLJIT_DEF_SHIFT;
457 	}
458 
459 	while (types) {
460 		switch (types & SLJIT_DEF_MASK) {
461 		case SLJIT_ARG_TYPE_F32:
462 		case SLJIT_ARG_TYPE_F64:
463 			arg_count--;
464 			break;
465 		default:
466 			if (arg_count != word_arg_count)
467 				FAIL_IF(push_inst(compiler, OR | S(word_arg_count) | A(arg_count) | B(word_arg_count)));
468 
469 			arg_count--;
470 			word_arg_count--;
471 			break;
472 		}
473 
474 		types >>= SLJIT_DEF_SHIFT;
475 	}
476 
477 	return SLJIT_SUCCESS;
478 }
479 
emit_const(struct sljit_compiler * compiler,sljit_s32 reg,sljit_sw init_value)480 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw init_value)
481 {
482 	FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 48)));
483 	FAIL_IF(push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value >> 32)));
484 	FAIL_IF(PUSH_RLDICR(reg, 31));
485 	FAIL_IF(push_inst(compiler, ORIS | S(reg) | A(reg) | IMM(init_value >> 16)));
486 	return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
487 }
488 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)489 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
490 {
491 	sljit_ins *inst = (sljit_ins*)addr;
492 	SLJIT_UNUSED_ARG(executable_offset);
493 
494 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
495 	inst[0] = (inst[0] & 0xffff0000) | ((new_target >> 48) & 0xffff);
496 	inst[1] = (inst[1] & 0xffff0000) | ((new_target >> 32) & 0xffff);
497 	inst[3] = (inst[3] & 0xffff0000) | ((new_target >> 16) & 0xffff);
498 	inst[4] = (inst[4] & 0xffff0000) | (new_target & 0xffff);
499 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
500 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
501 	SLJIT_CACHE_FLUSH(inst, inst + 5);
502 }
503 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)504 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
505 {
506 	sljit_set_jump_addr(addr, new_constant, executable_offset);
507 }
508