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 #ifdef __SOFTFP__
28 #define ARM_ABI_INFO " ABI:softfp"
29 #else
30 #define ARM_ABI_INFO " ABI:hardfp"
31 #endif
32 
sljit_get_platform_name(void)33 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
34 {
35 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
36 	return "ARMv7" SLJIT_CPUINFO ARM_ABI_INFO;
37 #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
38 	return "ARMv5" SLJIT_CPUINFO ARM_ABI_INFO;
39 #else
40 #error "Internal error: Unknown ARM architecture"
41 #endif
42 }
43 
44 /* Last register + 1. */
45 #define TMP_REG1	(SLJIT_NUMBER_OF_REGISTERS + 2)
46 #define TMP_REG2	(SLJIT_NUMBER_OF_REGISTERS + 3)
47 #define TMP_PC		(SLJIT_NUMBER_OF_REGISTERS + 4)
48 
49 #define TMP_FREG1	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
50 #define TMP_FREG2	(SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
51 
52 /* In ARM instruction words.
53    Cache lines are usually 32 byte aligned. */
54 #define CONST_POOL_ALIGNMENT	8
55 #define CONST_POOL_EMPTY	0xffffffff
56 
57 #define ALIGN_INSTRUCTION(ptr) \
58 	(sljit_uw*)(((sljit_uw)(ptr) + (CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1) & ~((CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1))
59 #define MAX_DIFFERENCE(max_diff) \
60 	(((max_diff) / (sljit_s32)sizeof(sljit_uw)) - (CONST_POOL_ALIGNMENT - 1))
61 
62 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
63 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
64 	0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
65 };
66 
67 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
68 	0, 0, 1, 2, 3, 4, 5, 6, 7
69 };
70 
71 #define RM(rm) (reg_map[rm])
72 #define RD(rd) (reg_map[rd] << 12)
73 #define RN(rn) (reg_map[rn] << 16)
74 
75 /* --------------------------------------------------------------------- */
76 /*  Instrucion forms                                                     */
77 /* --------------------------------------------------------------------- */
78 
79 /* The instruction includes the AL condition.
80    INST_NAME - CONDITIONAL remove this flag. */
81 #define COND_MASK	0xf0000000
82 #define CONDITIONAL	0xe0000000
83 #define PUSH_POOL	0xff000000
84 
85 #define ADC		0xe0a00000
86 #define ADD		0xe0800000
87 #define AND		0xe0000000
88 #define B		0xea000000
89 #define BIC		0xe1c00000
90 #define BL		0xeb000000
91 #define BLX		0xe12fff30
92 #define BX		0xe12fff10
93 #define CLZ		0xe16f0f10
94 #define CMN		0xe1600000
95 #define CMP		0xe1400000
96 #define BKPT		0xe1200070
97 #define EOR		0xe0200000
98 #define MOV		0xe1a00000
99 #define MUL		0xe0000090
100 #define MVN		0xe1e00000
101 #define NOP		0xe1a00000
102 #define ORR		0xe1800000
103 #define PUSH		0xe92d0000
104 #define POP		0xe8bd0000
105 #define RSB		0xe0600000
106 #define RSC		0xe0e00000
107 #define SBC		0xe0c00000
108 #define SMULL		0xe0c00090
109 #define SUB		0xe0400000
110 #define UMULL		0xe0800090
111 #define VABS_F32	0xeeb00ac0
112 #define VADD_F32	0xee300a00
113 #define VCMP_F32	0xeeb40a40
114 #define VCVT_F32_S32	0xeeb80ac0
115 #define VCVT_F64_F32	0xeeb70ac0
116 #define VCVT_S32_F32	0xeebd0ac0
117 #define VDIV_F32	0xee800a00
118 #define VMOV_F32	0xeeb00a40
119 #define VMOV		0xee000a10
120 #define VMOV2		0xec400a10
121 #define VMRS		0xeef1fa10
122 #define VMUL_F32	0xee200a00
123 #define VNEG_F32	0xeeb10a40
124 #define VSTR_F32	0xed000a00
125 #define VSUB_F32	0xee300a40
126 
127 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
128 /* Arm v7 specific instructions. */
129 #define MOVW		0xe3000000
130 #define MOVT		0xe3400000
131 #define SXTB		0xe6af0070
132 #define SXTH		0xe6bf0070
133 #define UXTB		0xe6ef0070
134 #define UXTH		0xe6ff0070
135 #endif
136 
137 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
138 
push_cpool(struct sljit_compiler * compiler)139 static sljit_s32 push_cpool(struct sljit_compiler *compiler)
140 {
141 	/* Pushing the constant pool into the instruction stream. */
142 	sljit_uw* inst;
143 	sljit_uw* cpool_ptr;
144 	sljit_uw* cpool_end;
145 	sljit_s32 i;
146 
147 	/* The label could point the address after the constant pool. */
148 	if (compiler->last_label && compiler->last_label->size == compiler->size)
149 		compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
150 
151 	SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
152 	inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
153 	FAIL_IF(!inst);
154 	compiler->size++;
155 	*inst = 0xff000000 | compiler->cpool_fill;
156 
157 	for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
158 		inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
159 		FAIL_IF(!inst);
160 		compiler->size++;
161 		*inst = 0;
162 	}
163 
164 	cpool_ptr = compiler->cpool;
165 	cpool_end = cpool_ptr + compiler->cpool_fill;
166 	while (cpool_ptr < cpool_end) {
167 		inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
168 		FAIL_IF(!inst);
169 		compiler->size++;
170 		*inst = *cpool_ptr++;
171 	}
172 	compiler->cpool_diff = CONST_POOL_EMPTY;
173 	compiler->cpool_fill = 0;
174 	return SLJIT_SUCCESS;
175 }
176 
push_inst(struct sljit_compiler * compiler,sljit_uw inst)177 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
178 {
179 	sljit_uw* ptr;
180 
181 	if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
182 		FAIL_IF(push_cpool(compiler));
183 
184 	ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
185 	FAIL_IF(!ptr);
186 	compiler->size++;
187 	*ptr = inst;
188 	return SLJIT_SUCCESS;
189 }
190 
push_inst_with_literal(struct sljit_compiler * compiler,sljit_uw inst,sljit_uw literal)191 static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
192 {
193 	sljit_uw* ptr;
194 	sljit_uw cpool_index = CPOOL_SIZE;
195 	sljit_uw* cpool_ptr;
196 	sljit_uw* cpool_end;
197 	sljit_u8* cpool_unique_ptr;
198 
199 	if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
200 		FAIL_IF(push_cpool(compiler));
201 	else if (compiler->cpool_fill > 0) {
202 		cpool_ptr = compiler->cpool;
203 		cpool_end = cpool_ptr + compiler->cpool_fill;
204 		cpool_unique_ptr = compiler->cpool_unique;
205 		do {
206 			if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
207 				cpool_index = cpool_ptr - compiler->cpool;
208 				break;
209 			}
210 			cpool_ptr++;
211 			cpool_unique_ptr++;
212 		} while (cpool_ptr < cpool_end);
213 	}
214 
215 	if (cpool_index == CPOOL_SIZE) {
216 		/* Must allocate a new entry in the literal pool. */
217 		if (compiler->cpool_fill < CPOOL_SIZE) {
218 			cpool_index = compiler->cpool_fill;
219 			compiler->cpool_fill++;
220 		}
221 		else {
222 			FAIL_IF(push_cpool(compiler));
223 			cpool_index = 0;
224 			compiler->cpool_fill = 1;
225 		}
226 	}
227 
228 	SLJIT_ASSERT((inst & 0xfff) == 0);
229 	ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
230 	FAIL_IF(!ptr);
231 	compiler->size++;
232 	*ptr = inst | cpool_index;
233 
234 	compiler->cpool[cpool_index] = literal;
235 	compiler->cpool_unique[cpool_index] = 0;
236 	if (compiler->cpool_diff == CONST_POOL_EMPTY)
237 		compiler->cpool_diff = compiler->size;
238 	return SLJIT_SUCCESS;
239 }
240 
push_inst_with_unique_literal(struct sljit_compiler * compiler,sljit_uw inst,sljit_uw literal)241 static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
242 {
243 	sljit_uw* ptr;
244 	if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
245 		FAIL_IF(push_cpool(compiler));
246 
247 	SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
248 	ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
249 	FAIL_IF(!ptr);
250 	compiler->size++;
251 	*ptr = inst | compiler->cpool_fill;
252 
253 	compiler->cpool[compiler->cpool_fill] = literal;
254 	compiler->cpool_unique[compiler->cpool_fill] = 1;
255 	compiler->cpool_fill++;
256 	if (compiler->cpool_diff == CONST_POOL_EMPTY)
257 		compiler->cpool_diff = compiler->size;
258 	return SLJIT_SUCCESS;
259 }
260 
prepare_blx(struct sljit_compiler * compiler)261 static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
262 {
263 	/* Place for at least two instruction (doesn't matter whether the first has a literal). */
264 	if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
265 		return push_cpool(compiler);
266 	return SLJIT_SUCCESS;
267 }
268 
emit_blx(struct sljit_compiler * compiler)269 static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
270 {
271 	/* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
272 	SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
273 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
274 
275 	return push_inst(compiler, BLX | RM(TMP_REG1));
276 }
277 
patch_pc_relative_loads(sljit_uw * last_pc_patch,sljit_uw * code_ptr,sljit_uw * const_pool,sljit_uw cpool_size)278 static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
279 {
280 	sljit_uw diff;
281 	sljit_uw ind;
282 	sljit_uw counter = 0;
283 	sljit_uw* clear_const_pool = const_pool;
284 	sljit_uw* clear_const_pool_end = const_pool + cpool_size;
285 
286 	SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
287 	/* Set unused flag for all literals in the constant pool.
288 	   I.e.: unused literals can belong to branches, which can be encoded as B or BL.
289 	   We can "compress" the constant pool by discarding these literals. */
290 	while (clear_const_pool < clear_const_pool_end)
291 		*clear_const_pool++ = (sljit_uw)(-1);
292 
293 	while (last_pc_patch < code_ptr) {
294 		/* Data transfer instruction with Rn == r15. */
295 		if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
296 			diff = const_pool - last_pc_patch;
297 			ind = (*last_pc_patch) & 0xfff;
298 
299 			/* Must be a load instruction with immediate offset. */
300 			SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
301 			if ((sljit_s32)const_pool[ind] < 0) {
302 				const_pool[ind] = counter;
303 				ind = counter;
304 				counter++;
305 			}
306 			else
307 				ind = const_pool[ind];
308 
309 			SLJIT_ASSERT(diff >= 1);
310 			if (diff >= 2 || ind > 0) {
311 				diff = (diff + ind - 2) << 2;
312 				SLJIT_ASSERT(diff <= 0xfff);
313 				*last_pc_patch = (*last_pc_patch & ~0xfff) | diff;
314 			}
315 			else
316 				*last_pc_patch = (*last_pc_patch & ~(0xfff | (1 << 23))) | 0x004;
317 		}
318 		last_pc_patch++;
319 	}
320 	return counter;
321 }
322 
323 /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
324 struct future_patch {
325 	struct future_patch* next;
326 	sljit_s32 index;
327 	sljit_s32 value;
328 };
329 
resolve_const_pool_index(struct sljit_compiler * compiler,struct future_patch ** first_patch,sljit_uw cpool_current_index,sljit_uw * cpool_start_address,sljit_uw * buf_ptr)330 static sljit_s32 resolve_const_pool_index(struct sljit_compiler *compiler, struct future_patch **first_patch, sljit_uw cpool_current_index, sljit_uw *cpool_start_address, sljit_uw *buf_ptr)
331 {
332 	sljit_s32 value;
333 	struct future_patch *curr_patch, *prev_patch;
334 
335 	SLJIT_UNUSED_ARG(compiler);
336 
337 	/* Using the values generated by patch_pc_relative_loads. */
338 	if (!*first_patch)
339 		value = (sljit_s32)cpool_start_address[cpool_current_index];
340 	else {
341 		curr_patch = *first_patch;
342 		prev_patch = NULL;
343 		while (1) {
344 			if (!curr_patch) {
345 				value = (sljit_s32)cpool_start_address[cpool_current_index];
346 				break;
347 			}
348 			if ((sljit_uw)curr_patch->index == cpool_current_index) {
349 				value = curr_patch->value;
350 				if (prev_patch)
351 					prev_patch->next = curr_patch->next;
352 				else
353 					*first_patch = curr_patch->next;
354 				SLJIT_FREE(curr_patch, compiler->allocator_data);
355 				break;
356 			}
357 			prev_patch = curr_patch;
358 			curr_patch = curr_patch->next;
359 		}
360 	}
361 
362 	if (value >= 0) {
363 		if ((sljit_uw)value > cpool_current_index) {
364 			curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
365 			if (!curr_patch) {
366 				while (*first_patch) {
367 					curr_patch = *first_patch;
368 					*first_patch = (*first_patch)->next;
369 					SLJIT_FREE(curr_patch, compiler->allocator_data);
370 				}
371 				return SLJIT_ERR_ALLOC_FAILED;
372 			}
373 			curr_patch->next = *first_patch;
374 			curr_patch->index = value;
375 			curr_patch->value = cpool_start_address[value];
376 			*first_patch = curr_patch;
377 		}
378 		cpool_start_address[value] = *buf_ptr;
379 	}
380 	return SLJIT_SUCCESS;
381 }
382 
383 #else
384 
push_inst(struct sljit_compiler * compiler,sljit_uw inst)385 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
386 {
387 	sljit_uw* ptr;
388 
389 	ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
390 	FAIL_IF(!ptr);
391 	compiler->size++;
392 	*ptr = inst;
393 	return SLJIT_SUCCESS;
394 }
395 
emit_imm(struct sljit_compiler * compiler,sljit_s32 reg,sljit_sw imm)396 static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
397 {
398 	FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
399 	return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
400 }
401 
402 #endif
403 
detect_jump_type(struct sljit_jump * jump,sljit_uw * code_ptr,sljit_uw * code,sljit_sw executable_offset)404 static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code, sljit_sw executable_offset)
405 {
406 	sljit_sw diff;
407 
408 	if (jump->flags & SLJIT_REWRITABLE_JUMP)
409 		return 0;
410 
411 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
412 	if (jump->flags & IS_BL)
413 		code_ptr--;
414 
415 	if (jump->flags & JUMP_ADDR)
416 		diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset);
417 	else {
418 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
419 		diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
420 	}
421 
422 	/* Branch to Thumb code has not been optimized yet. */
423 	if (diff & 0x3)
424 		return 0;
425 
426 	if (jump->flags & IS_BL) {
427 		if (diff <= 0x01ffffff && diff >= -0x02000000) {
428 			*code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
429 			jump->flags |= PATCH_B;
430 			return 1;
431 		}
432 	}
433 	else {
434 		if (diff <= 0x01ffffff && diff >= -0x02000000) {
435 			*code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
436 			jump->flags |= PATCH_B;
437 		}
438 	}
439 #else
440 	if (jump->flags & JUMP_ADDR)
441 		diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr - executable_offset);
442 	else {
443 		SLJIT_ASSERT(jump->flags & JUMP_LABEL);
444 		diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
445 	}
446 
447 	/* Branch to Thumb code has not been optimized yet. */
448 	if (diff & 0x3)
449 		return 0;
450 
451 	if (diff <= 0x01ffffff && diff >= -0x02000000) {
452 		code_ptr -= 2;
453 		*code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
454 		jump->flags |= PATCH_B;
455 		return 1;
456 	}
457 #endif
458 	return 0;
459 }
460 
inline_set_jump_addr(sljit_uw jump_ptr,sljit_sw executable_offset,sljit_uw new_addr,sljit_s32 flush_cache)461 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw jump_ptr, sljit_sw executable_offset, sljit_uw new_addr, sljit_s32 flush_cache)
462 {
463 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
464 	sljit_uw *ptr = (sljit_uw *)jump_ptr;
465 	sljit_uw *inst = (sljit_uw *)ptr[0];
466 	sljit_uw mov_pc = ptr[1];
467 	sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
468 	sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2) - executable_offset) >> 2);
469 
470 	if (diff <= 0x7fffff && diff >= -0x800000) {
471 		/* Turn to branch. */
472 		if (!bl) {
473 			inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
474 			if (flush_cache) {
475 				inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
476 				SLJIT_CACHE_FLUSH(inst, inst + 1);
477 			}
478 		} else {
479 			inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
480 			inst[1] = NOP;
481 			if (flush_cache) {
482 				inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
483 				SLJIT_CACHE_FLUSH(inst, inst + 2);
484 			}
485 		}
486 	} else {
487 		/* Get the position of the constant. */
488 		if (mov_pc & (1 << 23))
489 			ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
490 		else
491 			ptr = inst + 1;
492 
493 		if (*inst != mov_pc) {
494 			inst[0] = mov_pc;
495 			if (!bl) {
496 				if (flush_cache) {
497 					inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
498 					SLJIT_CACHE_FLUSH(inst, inst + 1);
499 				}
500 			} else {
501 				inst[1] = BLX | RM(TMP_REG1);
502 				if (flush_cache) {
503 					inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
504 					SLJIT_CACHE_FLUSH(inst, inst + 2);
505 				}
506 			}
507 		}
508 		*ptr = new_addr;
509 	}
510 #else
511 	sljit_uw *inst = (sljit_uw*)jump_ptr;
512 	SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
513 	inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
514 	inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
515 	if (flush_cache) {
516 		inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
517 		SLJIT_CACHE_FLUSH(inst, inst + 2);
518 	}
519 #endif
520 }
521 
522 static sljit_uw get_imm(sljit_uw imm);
523 
inline_set_const(sljit_uw addr,sljit_sw executable_offset,sljit_sw new_constant,sljit_s32 flush_cache)524 static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw executable_offset, sljit_sw new_constant, sljit_s32 flush_cache)
525 {
526 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
527 	sljit_uw *ptr = (sljit_uw*)addr;
528 	sljit_uw *inst = (sljit_uw*)ptr[0];
529 	sljit_uw ldr_literal = ptr[1];
530 	sljit_uw src2;
531 
532 	src2 = get_imm(new_constant);
533 	if (src2) {
534 		*inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
535 		if (flush_cache) {
536 			inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
537 			SLJIT_CACHE_FLUSH(inst, inst + 1);
538 		}
539 		return;
540 	}
541 
542 	src2 = get_imm(~new_constant);
543 	if (src2) {
544 		*inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
545 		if (flush_cache) {
546 			inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
547 			SLJIT_CACHE_FLUSH(inst, inst + 1);
548 		}
549 		return;
550 	}
551 
552 	if (ldr_literal & (1 << 23))
553 		ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
554 	else
555 		ptr = inst + 1;
556 
557 	if (*inst != ldr_literal) {
558 		*inst = ldr_literal;
559 		if (flush_cache) {
560 			inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
561 			SLJIT_CACHE_FLUSH(inst, inst + 1);
562 		}
563 	}
564 	*ptr = new_constant;
565 #else
566 	sljit_uw *inst = (sljit_uw*)addr;
567 	SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
568 	inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
569 	inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
570 	if (flush_cache) {
571 		inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
572 		SLJIT_CACHE_FLUSH(inst, inst + 2);
573 	}
574 #endif
575 }
576 
sljit_generate_code(struct sljit_compiler * compiler)577 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
578 {
579 	struct sljit_memory_fragment *buf;
580 	sljit_uw *code;
581 	sljit_uw *code_ptr;
582 	sljit_uw *buf_ptr;
583 	sljit_uw *buf_end;
584 	sljit_uw size;
585 	sljit_uw word_count;
586 	sljit_uw next_addr;
587 	sljit_sw executable_offset;
588 	sljit_sw addr;
589 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
590 	sljit_uw cpool_size;
591 	sljit_uw cpool_skip_alignment;
592 	sljit_uw cpool_current_index;
593 	sljit_uw *cpool_start_address;
594 	sljit_uw *last_pc_patch;
595 	struct future_patch *first_patch;
596 #endif
597 
598 	struct sljit_label *label;
599 	struct sljit_jump *jump;
600 	struct sljit_const *const_;
601 	struct sljit_put_label *put_label;
602 
603 	CHECK_ERROR_PTR();
604 	CHECK_PTR(check_sljit_generate_code(compiler));
605 	reverse_buf(compiler);
606 
607 	/* Second code generation pass. */
608 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
609 	size = compiler->size + (compiler->patches << 1);
610 	if (compiler->cpool_fill > 0)
611 		size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
612 #else
613 	size = compiler->size;
614 #endif
615 	code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw));
616 	PTR_FAIL_WITH_EXEC_IF(code);
617 	buf = compiler->buf;
618 
619 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
620 	cpool_size = 0;
621 	cpool_skip_alignment = 0;
622 	cpool_current_index = 0;
623 	cpool_start_address = NULL;
624 	first_patch = NULL;
625 	last_pc_patch = code;
626 #endif
627 
628 	code_ptr = code;
629 	word_count = 0;
630 	next_addr = 1;
631 	executable_offset = SLJIT_EXEC_OFFSET(code);
632 
633 	label = compiler->labels;
634 	jump = compiler->jumps;
635 	const_ = compiler->consts;
636 	put_label = compiler->put_labels;
637 
638 	if (label && label->size == 0) {
639 		label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
640 		label = label->next;
641 	}
642 
643 	do {
644 		buf_ptr = (sljit_uw*)buf->memory;
645 		buf_end = buf_ptr + (buf->used_size >> 2);
646 		do {
647 			word_count++;
648 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
649 			if (cpool_size > 0) {
650 				if (cpool_skip_alignment > 0) {
651 					buf_ptr++;
652 					cpool_skip_alignment--;
653 				}
654 				else {
655 					if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
656 						SLJIT_FREE_EXEC(code);
657 						compiler->error = SLJIT_ERR_ALLOC_FAILED;
658 						return NULL;
659 					}
660 					buf_ptr++;
661 					if (++cpool_current_index >= cpool_size) {
662 						SLJIT_ASSERT(!first_patch);
663 						cpool_size = 0;
664 						if (label && label->size == word_count) {
665 							/* Points after the current instruction. */
666 							label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
667 							label->size = code_ptr - code;
668 							label = label->next;
669 						}
670 					}
671 				}
672 			}
673 			else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
674 #endif
675 				*code_ptr = *buf_ptr++;
676 				if (next_addr == word_count) {
677 					SLJIT_ASSERT(!label || label->size >= word_count);
678 					SLJIT_ASSERT(!jump || jump->addr >= word_count);
679 					SLJIT_ASSERT(!const_ || const_->addr >= word_count);
680 					SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
681 
682 				/* These structures are ordered by their address. */
683 					if (jump && jump->addr == word_count) {
684 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
685 						if (detect_jump_type(jump, code_ptr, code, executable_offset))
686 							code_ptr--;
687 						jump->addr = (sljit_uw)code_ptr;
688 #else
689 						jump->addr = (sljit_uw)(code_ptr - 2);
690 						if (detect_jump_type(jump, code_ptr, code, executable_offset))
691 							code_ptr -= 2;
692 #endif
693 						jump = jump->next;
694 					}
695 					if (label && label->size == word_count) {
696 						/* code_ptr can be affected above. */
697 						label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
698 						label->size = (code_ptr + 1) - code;
699 						label = label->next;
700 					}
701 					if (const_ && const_->addr == word_count) {
702 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
703 						const_->addr = (sljit_uw)code_ptr;
704 #else
705 						const_->addr = (sljit_uw)(code_ptr - 1);
706 #endif
707 						const_ = const_->next;
708 					}
709 					if (put_label && put_label->addr == word_count) {
710 						SLJIT_ASSERT(put_label->label);
711 						put_label->addr = (sljit_uw)code_ptr;
712 						put_label = put_label->next;
713 					}
714 					next_addr = compute_next_addr(label, jump, const_, put_label);
715 				}
716 				code_ptr++;
717 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
718 			}
719 			else {
720 				/* Fortunately, no need to shift. */
721 				cpool_size = *buf_ptr++ & ~PUSH_POOL;
722 				SLJIT_ASSERT(cpool_size > 0);
723 				cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
724 				cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
725 				if (cpool_current_index > 0) {
726 					/* Unconditional branch. */
727 					*code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
728 					code_ptr = cpool_start_address + cpool_current_index;
729 				}
730 				cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
731 				cpool_current_index = 0;
732 				last_pc_patch = code_ptr;
733 			}
734 #endif
735 		} while (buf_ptr < buf_end);
736 		buf = buf->next;
737 	} while (buf);
738 
739 	SLJIT_ASSERT(!label);
740 	SLJIT_ASSERT(!jump);
741 	SLJIT_ASSERT(!const_);
742 	SLJIT_ASSERT(!put_label);
743 
744 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
745 	SLJIT_ASSERT(cpool_size == 0);
746 	if (compiler->cpool_fill > 0) {
747 		cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
748 		cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
749 		if (cpool_current_index > 0)
750 			code_ptr = cpool_start_address + cpool_current_index;
751 
752 		buf_ptr = compiler->cpool;
753 		buf_end = buf_ptr + compiler->cpool_fill;
754 		cpool_current_index = 0;
755 		while (buf_ptr < buf_end) {
756 			if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
757 				SLJIT_FREE_EXEC(code);
758 				compiler->error = SLJIT_ERR_ALLOC_FAILED;
759 				return NULL;
760 			}
761 			buf_ptr++;
762 			cpool_current_index++;
763 		}
764 		SLJIT_ASSERT(!first_patch);
765 	}
766 #endif
767 
768 	jump = compiler->jumps;
769 	while (jump) {
770 		buf_ptr = (sljit_uw *)jump->addr;
771 
772 		if (jump->flags & PATCH_B) {
773 			addr = (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
774 			if (!(jump->flags & JUMP_ADDR)) {
775 				SLJIT_ASSERT(jump->flags & JUMP_LABEL);
776 				SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - addr) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - addr) >= -0x02000000);
777 				*buf_ptr |= (((sljit_sw)jump->u.label->addr - addr) >> 2) & 0x00ffffff;
778 			}
779 			else {
780 				SLJIT_ASSERT(((sljit_sw)jump->u.target - addr) <= 0x01ffffff && ((sljit_sw)jump->u.target - addr) >= -0x02000000);
781 				*buf_ptr |= (((sljit_sw)jump->u.target - addr) >> 2) & 0x00ffffff;
782 			}
783 		}
784 		else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
785 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
786 			jump->addr = (sljit_uw)code_ptr;
787 			code_ptr[0] = (sljit_uw)buf_ptr;
788 			code_ptr[1] = *buf_ptr;
789 			inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
790 			code_ptr += 2;
791 #else
792 			inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
793 #endif
794 		}
795 		else {
796 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
797 			if (jump->flags & IS_BL)
798 				buf_ptr--;
799 			if (*buf_ptr & (1 << 23))
800 				buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
801 			else
802 				buf_ptr += 1;
803 			*buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
804 #else
805 			inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
806 #endif
807 		}
808 		jump = jump->next;
809 	}
810 
811 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
812 	const_ = compiler->consts;
813 	while (const_) {
814 		buf_ptr = (sljit_uw*)const_->addr;
815 		const_->addr = (sljit_uw)code_ptr;
816 
817 		code_ptr[0] = (sljit_uw)buf_ptr;
818 		code_ptr[1] = *buf_ptr;
819 		if (*buf_ptr & (1 << 23))
820 			buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
821 		else
822 			buf_ptr += 1;
823 		/* Set the value again (can be a simple constant). */
824 		inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
825 		code_ptr += 2;
826 
827 		const_ = const_->next;
828 	}
829 #endif
830 
831 	put_label = compiler->put_labels;
832 	while (put_label) {
833 		addr = put_label->label->addr;
834 		buf_ptr = (sljit_uw*)put_label->addr;
835 
836 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
837 		SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
838 		buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
839 #else
840 		SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
841 		buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
842 		buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
843 #endif
844 		put_label = put_label->next;
845 	}
846 
847 	SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
848 
849 	compiler->error = SLJIT_ERR_COMPILED;
850 	compiler->executable_offset = executable_offset;
851 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
852 
853 	code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
854 	code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
855 
856 	SLJIT_CACHE_FLUSH(code, code_ptr);
857 	return code;
858 }
859 
sljit_has_cpu_feature(sljit_s32 feature_type)860 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
861 {
862 	switch (feature_type) {
863 	case SLJIT_HAS_FPU:
864 #ifdef SLJIT_IS_FPU_AVAILABLE
865 		return SLJIT_IS_FPU_AVAILABLE;
866 #else
867 		/* Available by default. */
868 		return 1;
869 #endif
870 
871 	case SLJIT_HAS_CLZ:
872 	case SLJIT_HAS_CMOV:
873 		return 1;
874 
875 	default:
876 		return 0;
877 	}
878 }
879 
880 /* --------------------------------------------------------------------- */
881 /*  Entry, exit                                                          */
882 /* --------------------------------------------------------------------- */
883 
884 /* Creates an index in data_transfer_insts array. */
885 #define WORD_SIZE	0x00
886 #define BYTE_SIZE	0x01
887 #define HALF_SIZE	0x02
888 #define PRELOAD		0x03
889 #define SIGNED		0x04
890 #define LOAD_DATA	0x08
891 
892 /* Flag bits for emit_op. */
893 #define ALLOW_IMM	0x10
894 #define ALLOW_INV_IMM	0x20
895 #define ALLOW_ANY_IMM	(ALLOW_IMM | ALLOW_INV_IMM)
896 
897 /* s/l - store/load (1 bit)
898    u/s - signed/unsigned (1 bit)
899    w/b/h/N - word/byte/half/NOT allowed (2 bit)
900    Storing signed and unsigned values are the same operations. */
901 
902 static const sljit_uw data_transfer_insts[16] = {
903 /* s u w */ 0xe5000000 /* str */,
904 /* s u b */ 0xe5400000 /* strb */,
905 /* s u h */ 0xe10000b0 /* strh */,
906 /* s u N */ 0x00000000 /* not allowed */,
907 /* s s w */ 0xe5000000 /* str */,
908 /* s s b */ 0xe5400000 /* strb */,
909 /* s s h */ 0xe10000b0 /* strh */,
910 /* s s N */ 0x00000000 /* not allowed */,
911 
912 /* l u w */ 0xe5100000 /* ldr */,
913 /* l u b */ 0xe5500000 /* ldrb */,
914 /* l u h */ 0xe11000b0 /* ldrh */,
915 /* l u p */ 0xf5500000 /* preload */,
916 /* l s w */ 0xe5100000 /* ldr */,
917 /* l s b */ 0xe11000d0 /* ldrsb */,
918 /* l s h */ 0xe11000f0 /* ldrsh */,
919 /* l s N */ 0x00000000 /* not allowed */,
920 };
921 
922 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
923 	(data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
924 
925 /* Normal ldr/str instruction.
926    Type2: ldrsb, ldrh, ldrsh */
927 #define IS_TYPE1_TRANSFER(type) \
928 	(data_transfer_insts[(type) & 0xf] & 0x04000000)
929 #define TYPE2_TRANSFER_IMM(imm) \
930 	(((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
931 
932 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
933 	sljit_s32 dst, sljit_sw dstw,
934 	sljit_s32 src1, sljit_sw src1w,
935 	sljit_s32 src2, sljit_sw src2w);
936 
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)937 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
938 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
939 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
940 {
941 	sljit_s32 args, size, i, tmp;
942 	sljit_uw push;
943 
944 	CHECK_ERROR();
945 	CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
946 	set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
947 
948 	/* Push saved registers, temporary registers
949 	   stmdb sp!, {..., lr} */
950 	push = PUSH | (1 << 14);
951 
952 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
953 	for (i = SLJIT_S0; i >= tmp; i--)
954 		push |= 1 << reg_map[i];
955 
956 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
957 		push |= 1 << reg_map[i];
958 
959 	FAIL_IF(push_inst(compiler, push));
960 
961 	/* Stack must be aligned to 8 bytes: */
962 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
963 	local_size = ((size + local_size + 7) & ~7) - size;
964 	compiler->local_size = local_size;
965 	if (local_size > 0)
966 		FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
967 
968 	args = get_arg_count(arg_types);
969 
970 	if (args >= 1)
971 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0) | RM(SLJIT_R0)));
972 	if (args >= 2)
973 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S1) | RM(SLJIT_R1)));
974 	if (args >= 3)
975 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S2) | RM(SLJIT_R2)));
976 
977 	return SLJIT_SUCCESS;
978 }
979 
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)980 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
981 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
982 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
983 {
984 	sljit_s32 size;
985 
986 	CHECK_ERROR();
987 	CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
988 	set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
989 
990 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
991 	compiler->local_size = ((size + local_size + 7) & ~7) - size;
992 	return SLJIT_SUCCESS;
993 }
994 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)995 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
996 {
997 	sljit_s32 i, tmp;
998 	sljit_uw pop;
999 
1000 	CHECK_ERROR();
1001 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1002 
1003 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1004 
1005 	if (compiler->local_size > 0)
1006 		FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
1007 
1008 	/* Push saved registers, temporary registers
1009 	   ldmia sp!, {..., pc} */
1010 	pop = POP | (1 << 15);
1011 
1012 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1013 	for (i = SLJIT_S0; i >= tmp; i--)
1014 		pop |= 1 << reg_map[i];
1015 
1016 	for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1017 		pop |= 1 << reg_map[i];
1018 
1019 	return push_inst(compiler, pop);
1020 }
1021 
1022 /* --------------------------------------------------------------------- */
1023 /*  Operators                                                            */
1024 /* --------------------------------------------------------------------- */
1025 
1026 /* flags: */
1027   /* Arguments are swapped. */
1028 #define ARGS_SWAPPED	0x01
1029   /* Inverted immediate. */
1030 #define INV_IMM		0x02
1031   /* Source and destination is register. */
1032 #define MOVE_REG_CONV	0x04
1033   /* Unused return value. */
1034 #define UNUSED_RETURN	0x08
1035 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1036 #define SET_FLAGS	(1 << 20)
1037 /* dst: reg
1038    src1: reg
1039    src2: reg or imm (if allowed)
1040    SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1041 #define SRC2_IMM	(1 << 25)
1042 
1043 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1044 	SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1045 	if (compiler->shift_imm != 0x20) { \
1046 		SLJIT_ASSERT(src1 == TMP_REG1); \
1047 		SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1048 		\
1049 		if (compiler->shift_imm != 0) \
1050 			return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1051 				RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1052 		return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1053 	} \
1054 	return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
1055 		(reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1056 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_s32 src1,sljit_s32 src2)1057 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1058 	sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
1059 {
1060 	switch (GET_OPCODE(op)) {
1061 	case SLJIT_MOV:
1062 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1063 		if (dst != src2) {
1064 			if (src2 & SRC2_IMM) {
1065 				return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1066 			}
1067 			return push_inst(compiler, MOV | RD(dst) | RM(src2));
1068 		}
1069 		return SLJIT_SUCCESS;
1070 
1071 	case SLJIT_MOV_U8:
1072 	case SLJIT_MOV_S8:
1073 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1074 		if (flags & MOVE_REG_CONV) {
1075 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1076 			if (op == SLJIT_MOV_U8)
1077 				return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
1078 			FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
1079 			return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
1080 #else
1081 			return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1082 #endif
1083 		}
1084 		else if (dst != src2) {
1085 			SLJIT_ASSERT(src2 & SRC2_IMM);
1086 			return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1087 		}
1088 		return SLJIT_SUCCESS;
1089 
1090 	case SLJIT_MOV_U16:
1091 	case SLJIT_MOV_S16:
1092 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1093 		if (flags & MOVE_REG_CONV) {
1094 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1095 			FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
1096 			return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
1097 #else
1098 			return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1099 #endif
1100 		}
1101 		else if (dst != src2) {
1102 			SLJIT_ASSERT(src2 & SRC2_IMM);
1103 			return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1104 		}
1105 		return SLJIT_SUCCESS;
1106 
1107 	case SLJIT_NOT:
1108 		if (src2 & SRC2_IMM) {
1109 			return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
1110 		}
1111 		return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
1112 
1113 	case SLJIT_CLZ:
1114 		SLJIT_ASSERT(!(flags & INV_IMM));
1115 		SLJIT_ASSERT(!(src2 & SRC2_IMM));
1116 		FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1117 		return SLJIT_SUCCESS;
1118 
1119 	case SLJIT_ADD:
1120 		SLJIT_ASSERT(!(flags & INV_IMM));
1121 		if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1122 			return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1123 		return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1124 
1125 	case SLJIT_ADDC:
1126 		SLJIT_ASSERT(!(flags & INV_IMM));
1127 		return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1128 
1129 	case SLJIT_SUB:
1130 		SLJIT_ASSERT(!(flags & INV_IMM));
1131 		if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1132 			return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1133 		return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
1134 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1135 
1136 	case SLJIT_SUBC:
1137 		SLJIT_ASSERT(!(flags & INV_IMM));
1138 		return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
1139 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1140 
1141 	case SLJIT_MUL:
1142 		SLJIT_ASSERT(!(flags & INV_IMM));
1143 		SLJIT_ASSERT(!(src2 & SRC2_IMM));
1144 
1145 		if (!HAS_FLAGS(op))
1146 			return push_inst(compiler, MUL | (reg_map[dst] << 16) | (reg_map[src2] << 8) | reg_map[src1]);
1147 
1148 		FAIL_IF(push_inst(compiler, SMULL | (reg_map[TMP_REG1] << 16) | (reg_map[dst] << 12) | (reg_map[src2] << 8) | reg_map[src1]));
1149 
1150 		/* cmp TMP_REG1, dst asr #31. */
1151 		return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
1152 
1153 	case SLJIT_AND:
1154 		return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
1155 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1156 
1157 	case SLJIT_OR:
1158 		SLJIT_ASSERT(!(flags & INV_IMM));
1159 		return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1160 
1161 	case SLJIT_XOR:
1162 		SLJIT_ASSERT(!(flags & INV_IMM));
1163 		return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1164 
1165 	case SLJIT_SHL:
1166 		EMIT_SHIFT_INS_AND_RETURN(0);
1167 
1168 	case SLJIT_LSHR:
1169 		EMIT_SHIFT_INS_AND_RETURN(1);
1170 
1171 	case SLJIT_ASHR:
1172 		EMIT_SHIFT_INS_AND_RETURN(2);
1173 	}
1174 
1175 	SLJIT_UNREACHABLE();
1176 	return SLJIT_SUCCESS;
1177 }
1178 
1179 #undef EMIT_SHIFT_INS_AND_RETURN
1180 
1181 /* Tests whether the immediate can be stored in the 12 bit imm field.
1182    Returns with 0 if not possible. */
get_imm(sljit_uw imm)1183 static sljit_uw get_imm(sljit_uw imm)
1184 {
1185 	sljit_s32 rol;
1186 
1187 	if (imm <= 0xff)
1188 		return SRC2_IMM | imm;
1189 
1190 	if (!(imm & 0xff000000)) {
1191 		imm <<= 8;
1192 		rol = 8;
1193 	}
1194 	else {
1195 		imm = (imm << 24) | (imm >> 8);
1196 		rol = 0;
1197 	}
1198 
1199 	if (!(imm & 0xff000000)) {
1200 		imm <<= 8;
1201 		rol += 4;
1202 	}
1203 
1204 	if (!(imm & 0xf0000000)) {
1205 		imm <<= 4;
1206 		rol += 2;
1207 	}
1208 
1209 	if (!(imm & 0xc0000000)) {
1210 		imm <<= 2;
1211 		rol += 1;
1212 	}
1213 
1214 	if (!(imm & 0x00ffffff))
1215 		return SRC2_IMM | (imm >> 24) | (rol << 8);
1216 	else
1217 		return 0;
1218 }
1219 
1220 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
generate_int(struct sljit_compiler * compiler,sljit_s32 reg,sljit_uw imm,sljit_s32 positive)1221 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1222 {
1223 	sljit_uw mask;
1224 	sljit_uw imm1;
1225 	sljit_uw imm2;
1226 	sljit_s32 rol;
1227 
1228 	/* Step1: Search a zero byte (8 continous zero bit). */
1229 	mask = 0xff000000;
1230 	rol = 8;
1231 	while(1) {
1232 		if (!(imm & mask)) {
1233 			/* Rol imm by rol. */
1234 			imm = (imm << rol) | (imm >> (32 - rol));
1235 			/* Calculate arm rol. */
1236 			rol = 4 + (rol >> 1);
1237 			break;
1238 		}
1239 		rol += 2;
1240 		mask >>= 2;
1241 		if (mask & 0x3) {
1242 			/* rol by 8. */
1243 			imm = (imm << 8) | (imm >> 24);
1244 			mask = 0xff00;
1245 			rol = 24;
1246 			while (1) {
1247 				if (!(imm & mask)) {
1248 					/* Rol imm by rol. */
1249 					imm = (imm << rol) | (imm >> (32 - rol));
1250 					/* Calculate arm rol. */
1251 					rol = (rol >> 1) - 8;
1252 					break;
1253 				}
1254 				rol += 2;
1255 				mask >>= 2;
1256 				if (mask & 0x3)
1257 					return 0;
1258 			}
1259 			break;
1260 		}
1261 	}
1262 
1263 	/* The low 8 bit must be zero. */
1264 	SLJIT_ASSERT(!(imm & 0xff));
1265 
1266 	if (!(imm & 0xff000000)) {
1267 		imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1268 		imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1269 	}
1270 	else if (imm & 0xc0000000) {
1271 		imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1272 		imm <<= 8;
1273 		rol += 4;
1274 
1275 		if (!(imm & 0xff000000)) {
1276 			imm <<= 8;
1277 			rol += 4;
1278 		}
1279 
1280 		if (!(imm & 0xf0000000)) {
1281 			imm <<= 4;
1282 			rol += 2;
1283 		}
1284 
1285 		if (!(imm & 0xc0000000)) {
1286 			imm <<= 2;
1287 			rol += 1;
1288 		}
1289 
1290 		if (!(imm & 0x00ffffff))
1291 			imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1292 		else
1293 			return 0;
1294 	}
1295 	else {
1296 		if (!(imm & 0xf0000000)) {
1297 			imm <<= 4;
1298 			rol += 2;
1299 		}
1300 
1301 		if (!(imm & 0xc0000000)) {
1302 			imm <<= 2;
1303 			rol += 1;
1304 		}
1305 
1306 		imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1307 		imm <<= 8;
1308 		rol += 4;
1309 
1310 		if (!(imm & 0xf0000000)) {
1311 			imm <<= 4;
1312 			rol += 2;
1313 		}
1314 
1315 		if (!(imm & 0xc0000000)) {
1316 			imm <<= 2;
1317 			rol += 1;
1318 		}
1319 
1320 		if (!(imm & 0x00ffffff))
1321 			imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1322 		else
1323 			return 0;
1324 	}
1325 
1326 	FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
1327 	FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
1328 	return 1;
1329 }
1330 #endif
1331 
load_immediate(struct sljit_compiler * compiler,sljit_s32 reg,sljit_uw imm)1332 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1333 {
1334 	sljit_uw tmp;
1335 
1336 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1337 	if (!(imm & ~0xffff))
1338 		return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1339 #endif
1340 
1341 	/* Create imm by 1 inst. */
1342 	tmp = get_imm(imm);
1343 	if (tmp)
1344 		return push_inst(compiler, MOV | RD(reg) | tmp);
1345 
1346 	tmp = get_imm(~imm);
1347 	if (tmp)
1348 		return push_inst(compiler, MVN | RD(reg) | tmp);
1349 
1350 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1351 	/* Create imm by 2 inst. */
1352 	FAIL_IF(generate_int(compiler, reg, imm, 1));
1353 	FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1354 
1355 	/* Load integer. */
1356 	return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
1357 #else
1358 	FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
1359 	if (imm <= 0xffff)
1360 		return SLJIT_SUCCESS;
1361 	return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
1362 #endif
1363 }
1364 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)1365 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
1366 	sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
1367 {
1368 	sljit_uw imm, offset_reg;
1369 	sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
1370 
1371 	SLJIT_ASSERT (arg & SLJIT_MEM);
1372 	SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
1373 
1374 	if ((arg & REG_MASK) == SLJIT_UNUSED) {
1375 		if (is_type1_transfer) {
1376 			FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xfff));
1377 			argw &= 0xfff;
1378 		}
1379 		else {
1380 			FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xff));
1381 			argw &= 0xff;
1382 		}
1383 
1384 		return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
1385 			is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
1386 	}
1387 
1388 	if (arg & OFFS_REG_MASK) {
1389 		offset_reg = OFFS_REG(arg);
1390 		arg &= REG_MASK;
1391 		argw &= 0x3;
1392 
1393 		if (argw != 0 && !is_type1_transfer) {
1394 			FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | (argw << 7)));
1395 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
1396 		}
1397 
1398 		/* Bit 25: RM is offset. */
1399 		return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1400 			RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | (argw << 7)));
1401 	}
1402 
1403 	arg &= REG_MASK;
1404 
1405 	if (is_type1_transfer) {
1406 		if (argw > 0xfff) {
1407 			imm = get_imm(argw & ~0xfff);
1408 			if (imm) {
1409 				FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1410 				argw = argw & 0xfff;
1411 				arg = tmp_reg;
1412 			}
1413 		}
1414 		else if (argw < -0xfff) {
1415 			imm = get_imm(-argw & ~0xfff);
1416 			if (imm) {
1417 				FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1418 				argw = -(-argw & 0xfff);
1419 				arg = tmp_reg;
1420 			}
1421 		}
1422 
1423 		if (argw >= 0 && argw <= 0xfff)
1424 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
1425 
1426 		if (argw < 0 && argw >= -0xfff)
1427 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
1428 	}
1429 	else {
1430 		if (argw > 0xff) {
1431 			imm = get_imm(argw & ~0xff);
1432 			if (imm) {
1433 				FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1434 				argw = argw & 0xff;
1435 				arg = tmp_reg;
1436 			}
1437 		}
1438 		else if (argw < -0xff) {
1439 			imm = get_imm(-argw & ~0xff);
1440 			if (imm) {
1441 				FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1442 				argw = -(-argw & 0xff);
1443 				arg = tmp_reg;
1444 			}
1445 		}
1446 
1447 		if (argw >= 0 && argw <= 0xff)
1448 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1449 
1450 		if (argw < 0 && argw >= -0xff) {
1451 			argw = -argw;
1452 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1453 		}
1454 	}
1455 
1456 	FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1457 	return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1458 		RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
1459 }
1460 
emit_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 inp_flags,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1461 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1462 	sljit_s32 dst, sljit_sw dstw,
1463 	sljit_s32 src1, sljit_sw src1w,
1464 	sljit_s32 src2, sljit_sw src2w)
1465 {
1466 	/* src1 is reg or TMP_REG1
1467 	   src2 is reg, TMP_REG2, or imm
1468 	   result goes to TMP_REG2, so put result can use TMP_REG1. */
1469 
1470 	/* We prefers register and simple consts. */
1471 	sljit_s32 dst_reg;
1472 	sljit_s32 src1_reg;
1473 	sljit_s32 src2_reg;
1474 	sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1475 
1476 	/* Destination check. */
1477 	if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED))
1478 		flags |= UNUSED_RETURN;
1479 
1480 	SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
1481 
1482 	src2_reg = 0;
1483 
1484 	do {
1485 		if (!(inp_flags & ALLOW_IMM))
1486 			break;
1487 
1488 		if (src2 & SLJIT_IMM) {
1489 			src2_reg = get_imm(src2w);
1490 			if (src2_reg)
1491 				break;
1492 			if (inp_flags & ALLOW_INV_IMM) {
1493 				src2_reg = get_imm(~src2w);
1494 				if (src2_reg) {
1495 					flags |= INV_IMM;
1496 					break;
1497 				}
1498 			}
1499 			if (GET_OPCODE(op) == SLJIT_ADD) {
1500 				src2_reg = get_imm(-src2w);
1501 				if (src2_reg) {
1502 					op = SLJIT_SUB | GET_ALL_FLAGS(op);
1503 					break;
1504 				}
1505 			}
1506 			if (GET_OPCODE(op) == SLJIT_SUB) {
1507 				src2_reg = get_imm(-src2w);
1508 				if (src2_reg) {
1509 					op = SLJIT_ADD | GET_ALL_FLAGS(op);
1510 					break;
1511 				}
1512 			}
1513 		}
1514 
1515 		if (src1 & SLJIT_IMM) {
1516 			src2_reg = get_imm(src1w);
1517 			if (src2_reg) {
1518 				flags |= ARGS_SWAPPED;
1519 				src1 = src2;
1520 				src1w = src2w;
1521 				break;
1522 			}
1523 			if (inp_flags & ALLOW_INV_IMM) {
1524 				src2_reg = get_imm(~src1w);
1525 				if (src2_reg) {
1526 					flags |= ARGS_SWAPPED | INV_IMM;
1527 					src1 = src2;
1528 					src1w = src2w;
1529 					break;
1530 				}
1531 			}
1532 			if (GET_OPCODE(op) == SLJIT_ADD) {
1533 				src2_reg = get_imm(-src1w);
1534 				if (src2_reg) {
1535 					/* Note: add is commutative operation. */
1536 					src1 = src2;
1537 					src1w = src2w;
1538 					op = SLJIT_SUB | GET_ALL_FLAGS(op);
1539 					break;
1540 				}
1541 			}
1542 		}
1543 	} while(0);
1544 
1545 	/* Source 1. */
1546 	if (FAST_IS_REG(src1))
1547 		src1_reg = src1;
1548 	else if (src1 & SLJIT_MEM) {
1549 		FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
1550 		src1_reg = TMP_REG1;
1551 	}
1552 	else {
1553 		FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
1554 		src1_reg = TMP_REG1;
1555 	}
1556 
1557 	/* Destination. */
1558 	dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
1559 
1560 	if (op <= SLJIT_MOV_P) {
1561 		if (dst & SLJIT_MEM) {
1562 			if (inp_flags & BYTE_SIZE)
1563 				inp_flags &= ~SIGNED;
1564 
1565 			if (FAST_IS_REG(src2))
1566 				return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
1567 		}
1568 
1569 		if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
1570 			flags |= MOVE_REG_CONV;
1571 	}
1572 
1573 	/* Source 2. */
1574 	if (src2_reg == 0) {
1575 		src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
1576 
1577 		if (FAST_IS_REG(src2))
1578 			src2_reg = src2;
1579 		else if (src2 & SLJIT_MEM)
1580 			FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
1581 		else
1582 			FAIL_IF(load_immediate(compiler, src2_reg, src2w));
1583 	}
1584 
1585 	FAIL_IF(emit_single_op(compiler, op, flags, dst_reg, src1_reg, src2_reg));
1586 
1587 	if (!(dst & SLJIT_MEM))
1588 		return SLJIT_SUCCESS;
1589 
1590 	return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
1591 }
1592 
1593 #ifdef __cplusplus
1594 extern "C" {
1595 #endif
1596 
1597 #if defined(__GNUC__)
1598 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1599 extern int __aeabi_idivmod(int numerator, int denominator);
1600 #else
1601 #error "Software divmod functions are needed"
1602 #endif
1603 
1604 #ifdef __cplusplus
1605 }
1606 #endif
1607 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1608 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1609 {
1610 	sljit_sw saved_reg_list[3];
1611 	sljit_sw saved_reg_count;
1612 
1613 	CHECK_ERROR();
1614 	CHECK(check_sljit_emit_op0(compiler, op));
1615 
1616 	op = GET_OPCODE(op);
1617 	switch (op) {
1618 	case SLJIT_BREAKPOINT:
1619 		FAIL_IF(push_inst(compiler, BKPT));
1620 		break;
1621 	case SLJIT_NOP:
1622 		FAIL_IF(push_inst(compiler, NOP));
1623 		break;
1624 	case SLJIT_LMUL_UW:
1625 	case SLJIT_LMUL_SW:
1626 		return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1627 			| (reg_map[SLJIT_R1] << 16)
1628 			| (reg_map[SLJIT_R0] << 12)
1629 			| (reg_map[SLJIT_R0] << 8)
1630 			| reg_map[SLJIT_R1]);
1631 	case SLJIT_DIVMOD_UW:
1632 	case SLJIT_DIVMOD_SW:
1633 	case SLJIT_DIV_UW:
1634 	case SLJIT_DIV_SW:
1635 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1636 		SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1637 
1638 		saved_reg_count = 0;
1639 		if (compiler->scratches >= 4)
1640 			saved_reg_list[saved_reg_count++] = 3;
1641 		if (compiler->scratches >= 3)
1642 			saved_reg_list[saved_reg_count++] = 2;
1643 		if (op >= SLJIT_DIV_UW)
1644 			saved_reg_list[saved_reg_count++] = 1;
1645 
1646 		if (saved_reg_count > 0) {
1647 			FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
1648 						| (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1649 			if (saved_reg_count >= 2) {
1650 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1651 				FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
1652 			}
1653 			if (saved_reg_count >= 3) {
1654 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1655 				FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
1656 			}
1657 		}
1658 
1659 #if defined(__GNUC__)
1660 		FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1661 			((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1662 #else
1663 #error "Software divmod functions are needed"
1664 #endif
1665 
1666 		if (saved_reg_count > 0) {
1667 			if (saved_reg_count >= 3) {
1668 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1669 				FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
1670 			}
1671 			if (saved_reg_count >= 2) {
1672 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1673 				FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
1674 			}
1675 			return push_inst(compiler, 0xe49d0000 | (saved_reg_count >= 3 ? 16 : 8)
1676 						| (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1677 		}
1678 		return SLJIT_SUCCESS;
1679 	}
1680 
1681 	return SLJIT_SUCCESS;
1682 }
1683 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1684 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1685 	sljit_s32 dst, sljit_sw dstw,
1686 	sljit_s32 src, sljit_sw srcw)
1687 {
1688 	CHECK_ERROR();
1689 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1690 	ADJUST_LOCAL_OFFSET(dst, dstw);
1691 	ADJUST_LOCAL_OFFSET(src, srcw);
1692 
1693 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
1694 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1695 		if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
1696 			return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
1697 #endif
1698 		return SLJIT_SUCCESS;
1699 	}
1700 
1701 	switch (GET_OPCODE(op)) {
1702 	case SLJIT_MOV:
1703 	case SLJIT_MOV_U32:
1704 	case SLJIT_MOV_S32:
1705 	case SLJIT_MOV_P:
1706 		return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1707 
1708 	case SLJIT_MOV_U8:
1709 		return emit_op(compiler, SLJIT_MOV_U8, ALLOW_ANY_IMM | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
1710 
1711 	case SLJIT_MOV_S8:
1712 		return emit_op(compiler, SLJIT_MOV_S8, ALLOW_ANY_IMM | SIGNED | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
1713 
1714 	case SLJIT_MOV_U16:
1715 		return emit_op(compiler, SLJIT_MOV_U16, ALLOW_ANY_IMM | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
1716 
1717 	case SLJIT_MOV_S16:
1718 		return emit_op(compiler, SLJIT_MOV_S16, ALLOW_ANY_IMM | SIGNED | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
1719 
1720 	case SLJIT_NOT:
1721 		return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
1722 
1723 	case SLJIT_NEG:
1724 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1725 			|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1726 		compiler->skip_checks = 1;
1727 #endif
1728 		return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
1729 
1730 	case SLJIT_CLZ:
1731 		return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
1732 	}
1733 
1734 	return SLJIT_SUCCESS;
1735 }
1736 
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1737 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1738 	sljit_s32 dst, sljit_sw dstw,
1739 	sljit_s32 src1, sljit_sw src1w,
1740 	sljit_s32 src2, sljit_sw src2w)
1741 {
1742 	CHECK_ERROR();
1743 	CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1744 	ADJUST_LOCAL_OFFSET(dst, dstw);
1745 	ADJUST_LOCAL_OFFSET(src1, src1w);
1746 	ADJUST_LOCAL_OFFSET(src2, src2w);
1747 
1748 	if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1749 		return SLJIT_SUCCESS;
1750 
1751 	switch (GET_OPCODE(op)) {
1752 	case SLJIT_ADD:
1753 	case SLJIT_ADDC:
1754 	case SLJIT_SUB:
1755 	case SLJIT_SUBC:
1756 	case SLJIT_OR:
1757 	case SLJIT_XOR:
1758 		return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
1759 
1760 	case SLJIT_MUL:
1761 		return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1762 
1763 	case SLJIT_AND:
1764 		return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
1765 
1766 	case SLJIT_SHL:
1767 	case SLJIT_LSHR:
1768 	case SLJIT_ASHR:
1769 		if (src2 & SLJIT_IMM) {
1770 			compiler->shift_imm = src2w & 0x1f;
1771 			return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
1772 		}
1773 		else {
1774 			compiler->shift_imm = 0x20;
1775 			return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
1776 		}
1777 	}
1778 
1779 	return SLJIT_SUCCESS;
1780 }
1781 
sljit_get_register_index(sljit_s32 reg)1782 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1783 {
1784 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1785 	return reg_map[reg];
1786 }
1787 
sljit_get_float_register_index(sljit_s32 reg)1788 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1789 {
1790 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1791 	return (freg_map[reg] << 1);
1792 }
1793 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1794 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1795 	void *instruction, sljit_s32 size)
1796 {
1797 	CHECK_ERROR();
1798 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1799 
1800 	return push_inst(compiler, *(sljit_uw*)instruction);
1801 }
1802 
1803 /* --------------------------------------------------------------------- */
1804 /*  Floating point operators                                             */
1805 /* --------------------------------------------------------------------- */
1806 
1807 
1808 #define FPU_LOAD (1 << 20)
1809 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
1810 	((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
1811 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1812 	((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
1813 
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1814 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1815 {
1816 	sljit_uw imm;
1817 	sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1818 
1819 	SLJIT_ASSERT(arg & SLJIT_MEM);
1820 	arg &= ~SLJIT_MEM;
1821 
1822 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1823 		FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((argw & 0x3) << 7)));
1824 		arg = TMP_REG2;
1825 		argw = 0;
1826 	}
1827 
1828 	/* Fast loads and stores. */
1829 	if (arg) {
1830 		if (!(argw & ~0x3fc))
1831 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
1832 		if (!(-argw & ~0x3fc))
1833 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
1834 
1835 		imm = get_imm(argw & ~0x3fc);
1836 		if (imm) {
1837 			FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1838 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1839 		}
1840 		imm = get_imm(-argw & ~0x3fc);
1841 		if (imm) {
1842 			argw = -argw;
1843 			FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1844 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1845 		}
1846 	}
1847 
1848 	if (arg) {
1849 		FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1850 		FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
1851 	}
1852 	else
1853 		FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1854 
1855 	return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
1856 }
1857 
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1858 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1859 	sljit_s32 dst, sljit_sw dstw,
1860 	sljit_s32 src, sljit_sw srcw)
1861 {
1862 	op ^= SLJIT_F32_OP;
1863 
1864 	if (src & SLJIT_MEM) {
1865 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1866 		src = TMP_FREG1;
1867 	}
1868 
1869 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
1870 
1871 	if (FAST_IS_REG(dst))
1872 		return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (freg_map[TMP_FREG1] << 16));
1873 
1874 	/* Store the integer value from a VFP register. */
1875 	return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1876 }
1877 
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1878 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1879 	sljit_s32 dst, sljit_sw dstw,
1880 	sljit_s32 src, sljit_sw srcw)
1881 {
1882 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1883 
1884 	op ^= SLJIT_F32_OP;
1885 
1886 	if (FAST_IS_REG(src))
1887 		FAIL_IF(push_inst(compiler, VMOV | RD(src) | (freg_map[TMP_FREG1] << 16)));
1888 	else if (src & SLJIT_MEM) {
1889 		/* Load the integer value into a VFP register. */
1890 		FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1891 	}
1892 	else {
1893 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1894 		FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (freg_map[TMP_FREG1] << 16)));
1895 	}
1896 
1897 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
1898 
1899 	if (dst & SLJIT_MEM)
1900 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1901 	return SLJIT_SUCCESS;
1902 }
1903 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1904 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1905 	sljit_s32 src1, sljit_sw src1w,
1906 	sljit_s32 src2, sljit_sw src2w)
1907 {
1908 	op ^= SLJIT_F32_OP;
1909 
1910 	if (src1 & SLJIT_MEM) {
1911 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
1912 		src1 = TMP_FREG1;
1913 	}
1914 
1915 	if (src2 & SLJIT_MEM) {
1916 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
1917 		src2 = TMP_FREG2;
1918 	}
1919 
1920 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
1921 	return push_inst(compiler, VMRS);
1922 }
1923 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1924 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1925 	sljit_s32 dst, sljit_sw dstw,
1926 	sljit_s32 src, sljit_sw srcw)
1927 {
1928 	sljit_s32 dst_r;
1929 
1930 	CHECK_ERROR();
1931 
1932 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
1933 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1934 
1935 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1936 
1937 	if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1938 		op ^= SLJIT_F32_OP;
1939 
1940 	if (src & SLJIT_MEM) {
1941 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
1942 		src = dst_r;
1943 	}
1944 
1945 	switch (GET_OPCODE(op)) {
1946 	case SLJIT_MOV_F64:
1947 		if (src != dst_r) {
1948 			if (dst_r != TMP_FREG1)
1949 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1950 			else
1951 				dst_r = src;
1952 		}
1953 		break;
1954 	case SLJIT_NEG_F64:
1955 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1956 		break;
1957 	case SLJIT_ABS_F64:
1958 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1959 		break;
1960 	case SLJIT_CONV_F64_FROM_F32:
1961 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1962 		op ^= SLJIT_F32_OP;
1963 		break;
1964 	}
1965 
1966 	if (dst & SLJIT_MEM)
1967 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
1968 	return SLJIT_SUCCESS;
1969 }
1970 
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1971 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1972 	sljit_s32 dst, sljit_sw dstw,
1973 	sljit_s32 src1, sljit_sw src1w,
1974 	sljit_s32 src2, sljit_sw src2w)
1975 {
1976 	sljit_s32 dst_r;
1977 
1978 	CHECK_ERROR();
1979 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1980 	ADJUST_LOCAL_OFFSET(dst, dstw);
1981 	ADJUST_LOCAL_OFFSET(src1, src1w);
1982 	ADJUST_LOCAL_OFFSET(src2, src2w);
1983 
1984 	op ^= SLJIT_F32_OP;
1985 
1986 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1987 
1988 	if (src2 & SLJIT_MEM) {
1989 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
1990 		src2 = TMP_FREG2;
1991 	}
1992 
1993 	if (src1 & SLJIT_MEM) {
1994 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
1995 		src1 = TMP_FREG1;
1996 	}
1997 
1998 	switch (GET_OPCODE(op)) {
1999 	case SLJIT_ADD_F64:
2000 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2001 		break;
2002 
2003 	case SLJIT_SUB_F64:
2004 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2005 		break;
2006 
2007 	case SLJIT_MUL_F64:
2008 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2009 		break;
2010 
2011 	case SLJIT_DIV_F64:
2012 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2013 		break;
2014 	}
2015 
2016 	if (dst_r == TMP_FREG1)
2017 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
2018 
2019 	return SLJIT_SUCCESS;
2020 }
2021 
2022 #undef FPU_LOAD
2023 #undef EMIT_FPU_DATA_TRANSFER
2024 
2025 /* --------------------------------------------------------------------- */
2026 /*  Other instructions                                                   */
2027 /* --------------------------------------------------------------------- */
2028 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2029 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2030 {
2031 	CHECK_ERROR();
2032 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2033 	ADJUST_LOCAL_OFFSET(dst, dstw);
2034 
2035 	SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2036 
2037 	if (FAST_IS_REG(dst))
2038 		return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
2039 
2040 	/* Memory. */
2041 	return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
2042 }
2043 
sljit_emit_fast_return(struct sljit_compiler * compiler,sljit_s32 src,sljit_sw srcw)2044 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
2045 {
2046 	CHECK_ERROR();
2047 	CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
2048 	ADJUST_LOCAL_OFFSET(src, srcw);
2049 
2050 	SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2051 
2052 	if (FAST_IS_REG(src))
2053 		FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
2054 	else
2055 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
2056 
2057 	return push_inst(compiler, BX | RM(TMP_REG2));
2058 }
2059 
2060 /* --------------------------------------------------------------------- */
2061 /*  Conditional instructions                                             */
2062 /* --------------------------------------------------------------------- */
2063 
get_cc(sljit_s32 type)2064 static sljit_uw get_cc(sljit_s32 type)
2065 {
2066 	switch (type) {
2067 	case SLJIT_EQUAL:
2068 	case SLJIT_MUL_NOT_OVERFLOW:
2069 	case SLJIT_EQUAL_F64:
2070 		return 0x00000000;
2071 
2072 	case SLJIT_NOT_EQUAL:
2073 	case SLJIT_MUL_OVERFLOW:
2074 	case SLJIT_NOT_EQUAL_F64:
2075 		return 0x10000000;
2076 
2077 	case SLJIT_LESS:
2078 	case SLJIT_LESS_F64:
2079 		return 0x30000000;
2080 
2081 	case SLJIT_GREATER_EQUAL:
2082 	case SLJIT_GREATER_EQUAL_F64:
2083 		return 0x20000000;
2084 
2085 	case SLJIT_GREATER:
2086 	case SLJIT_GREATER_F64:
2087 		return 0x80000000;
2088 
2089 	case SLJIT_LESS_EQUAL:
2090 	case SLJIT_LESS_EQUAL_F64:
2091 		return 0x90000000;
2092 
2093 	case SLJIT_SIG_LESS:
2094 		return 0xb0000000;
2095 
2096 	case SLJIT_SIG_GREATER_EQUAL:
2097 		return 0xa0000000;
2098 
2099 	case SLJIT_SIG_GREATER:
2100 		return 0xc0000000;
2101 
2102 	case SLJIT_SIG_LESS_EQUAL:
2103 		return 0xd0000000;
2104 
2105 	case SLJIT_OVERFLOW:
2106 	case SLJIT_UNORDERED_F64:
2107 		return 0x60000000;
2108 
2109 	case SLJIT_NOT_OVERFLOW:
2110 	case SLJIT_ORDERED_F64:
2111 		return 0x70000000;
2112 
2113 	default:
2114 		SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
2115 		return 0xe0000000;
2116 	}
2117 }
2118 
sljit_emit_label(struct sljit_compiler * compiler)2119 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2120 {
2121 	struct sljit_label *label;
2122 
2123 	CHECK_ERROR_PTR();
2124 	CHECK_PTR(check_sljit_emit_label(compiler));
2125 
2126 	if (compiler->last_label && compiler->last_label->size == compiler->size)
2127 		return compiler->last_label;
2128 
2129 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2130 	PTR_FAIL_IF(!label);
2131 	set_label(label, compiler);
2132 	return label;
2133 }
2134 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)2135 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2136 {
2137 	struct sljit_jump *jump;
2138 
2139 	CHECK_ERROR_PTR();
2140 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
2141 
2142 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2143 	PTR_FAIL_IF(!jump);
2144 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2145 	type &= 0xff;
2146 
2147 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2148 
2149 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2150 	if (type >= SLJIT_FAST_CALL)
2151 		PTR_FAIL_IF(prepare_blx(compiler));
2152 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2153 		type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
2154 
2155 	if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2156 		jump->addr = compiler->size;
2157 		compiler->patches++;
2158 	}
2159 
2160 	if (type >= SLJIT_FAST_CALL) {
2161 		jump->flags |= IS_BL;
2162 		PTR_FAIL_IF(emit_blx(compiler));
2163 	}
2164 
2165 	if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2166 		jump->addr = compiler->size;
2167 #else
2168 	if (type >= SLJIT_FAST_CALL)
2169 		jump->flags |= IS_BL;
2170 	PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2171 	PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
2172 	jump->addr = compiler->size;
2173 #endif
2174 	return jump;
2175 }
2176 
2177 #ifdef __SOFTFP__
2178 
softfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)2179 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
2180 {
2181 	sljit_s32 stack_offset = 0;
2182 	sljit_s32 arg_count = 0;
2183 	sljit_s32 word_arg_offset = 0;
2184 	sljit_s32 float_arg_count = 0;
2185 	sljit_s32 types = 0;
2186 	sljit_s32 src_offset = 4 * sizeof(sljit_sw);
2187 	sljit_u8 offsets[4];
2188 
2189 	if (src && FAST_IS_REG(*src))
2190 		src_offset = reg_map[*src] * sizeof(sljit_sw);
2191 
2192 	arg_types >>= SLJIT_DEF_SHIFT;
2193 
2194 	while (arg_types) {
2195 		types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
2196 
2197 		switch (arg_types & SLJIT_DEF_MASK) {
2198 		case SLJIT_ARG_TYPE_F32:
2199 			offsets[arg_count] = (sljit_u8)stack_offset;
2200 			stack_offset += sizeof(sljit_f32);
2201 			arg_count++;
2202 			float_arg_count++;
2203 			break;
2204 		case SLJIT_ARG_TYPE_F64:
2205 			if (stack_offset & 0x7)
2206 				stack_offset += sizeof(sljit_sw);
2207 			offsets[arg_count] = (sljit_u8)stack_offset;
2208 			stack_offset += sizeof(sljit_f64);
2209 			arg_count++;
2210 			float_arg_count++;
2211 			break;
2212 		default:
2213 			offsets[arg_count] = (sljit_u8)stack_offset;
2214 			stack_offset += sizeof(sljit_sw);
2215 			arg_count++;
2216 			word_arg_offset += sizeof(sljit_sw);
2217 			break;
2218 		}
2219 
2220 		arg_types >>= SLJIT_DEF_SHIFT;
2221 	}
2222 
2223 	if (stack_offset > 16)
2224 		FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_offset - 16) + 0x7) & ~0x7)));
2225 
2226 	/* Process arguments in reversed direction. */
2227 	while (types) {
2228 		switch (types & SLJIT_DEF_MASK) {
2229 		case SLJIT_ARG_TYPE_F32:
2230 			arg_count--;
2231 			float_arg_count--;
2232 			stack_offset = offsets[arg_count];
2233 
2234 			if (stack_offset < 16) {
2235 				if (src_offset == stack_offset) {
2236 					FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2237 					*src = TMP_REG1;
2238 				}
2239 				FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
2240 			} else
2241 				FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2242 			break;
2243 		case SLJIT_ARG_TYPE_F64:
2244 			arg_count--;
2245 			float_arg_count--;
2246 			stack_offset = offsets[arg_count];
2247 
2248 			SLJIT_ASSERT((stack_offset & 0x7) == 0);
2249 
2250 			if (stack_offset < 16) {
2251 				if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
2252 					FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2253 					*src = TMP_REG1;
2254 				}
2255 				FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2256 			} else
2257 				FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2258 			break;
2259 		default:
2260 			arg_count--;
2261 			word_arg_offset -= sizeof(sljit_sw);
2262 			stack_offset = offsets[arg_count];
2263 
2264 			SLJIT_ASSERT(stack_offset >= word_arg_offset);
2265 
2266 			if (stack_offset != word_arg_offset) {
2267 				if (stack_offset < 16) {
2268 					if (src_offset == stack_offset) {
2269 						FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2270 						*src = TMP_REG1;
2271 					}
2272 					else if (src_offset == word_arg_offset) {
2273 						*src = 1 + (stack_offset >> 2);
2274 						src_offset = stack_offset;
2275 					}
2276 					FAIL_IF(push_inst(compiler, MOV | (stack_offset << 10) | (word_arg_offset >> 2)));
2277 				} else
2278 					FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (stack_offset - 16)));
2279 			}
2280 			break;
2281 		}
2282 
2283 		types >>= SLJIT_DEF_SHIFT;
2284 	}
2285 
2286 	return SLJIT_SUCCESS;
2287 }
2288 
softfloat_post_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2289 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2290 {
2291 	sljit_s32 stack_size = 0;
2292 
2293 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
2294 		FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
2295 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
2296 		FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2297 
2298 	arg_types >>= SLJIT_DEF_SHIFT;
2299 
2300 	while (arg_types) {
2301 		switch (arg_types & SLJIT_DEF_MASK) {
2302 		case SLJIT_ARG_TYPE_F32:
2303 			stack_size += sizeof(sljit_f32);
2304 			break;
2305 		case SLJIT_ARG_TYPE_F64:
2306 			if (stack_size & 0x7)
2307 				stack_size += sizeof(sljit_sw);
2308 			stack_size += sizeof(sljit_f64);
2309 			break;
2310 		default:
2311 			stack_size += sizeof(sljit_sw);
2312 			break;
2313 		}
2314 
2315 		arg_types >>= SLJIT_DEF_SHIFT;
2316 	}
2317 
2318 	if (stack_size <= 16)
2319 		return SLJIT_SUCCESS;
2320 
2321 	return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_size - 16) + 0x7) & ~0x7));
2322 }
2323 
2324 #else /* !__SOFTFP__ */
2325 
hardfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2326 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2327 {
2328 	sljit_u32 remap = 0;
2329 	sljit_u32 offset = 0;
2330 	sljit_u32 new_offset, mask;
2331 
2332 	/* Remove return value. */
2333 	arg_types >>= SLJIT_DEF_SHIFT;
2334 
2335 	while (arg_types) {
2336 		if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
2337 			new_offset = 0;
2338 			mask = 1;
2339 
2340 			while (remap & mask) {
2341 				new_offset++;
2342 				mask <<= 1;
2343 			}
2344 			remap |= mask;
2345 
2346 			if (offset != new_offset)
2347 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2348 					0, (new_offset >> 1) + 1, (offset >> 1) + 1, 0) | ((new_offset & 0x1) ? 0x400000 : 0)));
2349 
2350 			offset += 2;
2351 		}
2352 		else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2353 			new_offset = 0;
2354 			mask = 3;
2355 
2356 			while (remap & mask) {
2357 				new_offset += 2;
2358 				mask <<= 2;
2359 			}
2360 			remap |= mask;
2361 
2362 			if (offset != new_offset)
2363 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, SLJIT_F32_OP, (new_offset >> 1) + 1, (offset >> 1) + 1, 0)));
2364 
2365 			offset += 2;
2366 		}
2367 		arg_types >>= SLJIT_DEF_SHIFT;
2368 	}
2369 
2370 	return SLJIT_SUCCESS;
2371 }
2372 
2373 #endif /* __SOFTFP__ */
2374 
2375 #undef EMIT_FPU_OPERATION
2376 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)2377 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2378 	sljit_s32 arg_types)
2379 {
2380 #ifdef __SOFTFP__
2381 	struct sljit_jump *jump;
2382 #endif
2383 
2384 	CHECK_ERROR_PTR();
2385 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2386 
2387 #ifdef __SOFTFP__
2388 	PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2389 
2390 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2391 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2392 	compiler->skip_checks = 1;
2393 #endif
2394 
2395 	jump = sljit_emit_jump(compiler, type);
2396 	PTR_FAIL_IF(jump == NULL);
2397 
2398 	PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2399 	return jump;
2400 #else /* !__SOFTFP__ */
2401 	PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2402 
2403 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2404 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2405 	compiler->skip_checks = 1;
2406 #endif
2407 
2408 	return sljit_emit_jump(compiler, type);
2409 #endif /* __SOFTFP__ */
2410 }
2411 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2412 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2413 {
2414 	struct sljit_jump *jump;
2415 
2416 	CHECK_ERROR();
2417 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2418 	ADJUST_LOCAL_OFFSET(src, srcw);
2419 
2420 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2421 
2422 	if (!(src & SLJIT_IMM)) {
2423 		if (FAST_IS_REG(src)) {
2424 			SLJIT_ASSERT(reg_map[src] != 14);
2425 			return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2426 		}
2427 
2428 		SLJIT_ASSERT(src & SLJIT_MEM);
2429 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2430 		return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
2431 	}
2432 
2433 	/* These jumps are converted to jump/call instructions when possible. */
2434 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2435 	FAIL_IF(!jump);
2436 	set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2437 	jump->u.target = srcw;
2438 
2439 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2440 	if (type >= SLJIT_FAST_CALL)
2441 		FAIL_IF(prepare_blx(compiler));
2442 	FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0), 0));
2443 	if (type >= SLJIT_FAST_CALL)
2444 		FAIL_IF(emit_blx(compiler));
2445 #else
2446 	FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2447 	FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2448 #endif
2449 	jump->addr = compiler->size;
2450 	return SLJIT_SUCCESS;
2451 }
2452 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)2453 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2454 	sljit_s32 arg_types,
2455 	sljit_s32 src, sljit_sw srcw)
2456 {
2457 	CHECK_ERROR();
2458 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2459 
2460 #ifdef __SOFTFP__
2461 	if (src & SLJIT_MEM) {
2462 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2463 		src = TMP_REG1;
2464 	}
2465 
2466 	FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2467 
2468 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2469 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2470 	compiler->skip_checks = 1;
2471 #endif
2472 
2473 	FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2474 
2475 	return softfloat_post_call_with_args(compiler, arg_types);
2476 #else /* !__SOFTFP__ */
2477 	FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2478 
2479 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2480 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2481 	compiler->skip_checks = 1;
2482 #endif
2483 
2484 	return sljit_emit_ijump(compiler, type, src, srcw);
2485 #endif /* __SOFTFP__ */
2486 }
2487 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2488 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2489 	sljit_s32 dst, sljit_sw dstw,
2490 	sljit_s32 type)
2491 {
2492 	sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
2493 	sljit_uw cc, ins;
2494 
2495 	CHECK_ERROR();
2496 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2497 	ADJUST_LOCAL_OFFSET(dst, dstw);
2498 
2499 	op = GET_OPCODE(op);
2500 	cc = get_cc(type & 0xff);
2501 	dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2502 
2503 	if (op < SLJIT_ADD) {
2504 		FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
2505 		FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2506 		if (dst & SLJIT_MEM)
2507 			return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
2508 		return SLJIT_SUCCESS;
2509 	}
2510 
2511 	ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
2512 
2513 	if (dst & SLJIT_MEM)
2514 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
2515 
2516 	FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2517 
2518 	if (op == SLJIT_AND)
2519 		FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2520 
2521 	if (dst & SLJIT_MEM)
2522 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2523 
2524 	if (flags & SLJIT_SET_Z)
2525 		return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
2526 	return SLJIT_SUCCESS;
2527 }
2528 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2529 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2530 	sljit_s32 dst_reg,
2531 	sljit_s32 src, sljit_sw srcw)
2532 {
2533 	sljit_uw cc, tmp;
2534 
2535 	CHECK_ERROR();
2536 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2537 
2538 	dst_reg &= ~SLJIT_I32_OP;
2539 
2540 	cc = get_cc(type & 0xff);
2541 
2542 	if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2543 		tmp = get_imm(srcw);
2544 		if (tmp)
2545 			return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2546 
2547 		tmp = get_imm(~srcw);
2548 		if (tmp)
2549 			return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2550 
2551 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2552 		tmp = (sljit_uw) srcw;
2553 		FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
2554 		if (tmp <= 0xffff)
2555 			return SLJIT_SUCCESS;
2556 		return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
2557 #else
2558 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2559 		src = TMP_REG1;
2560 #endif
2561 	}
2562 
2563 	return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
2564 }
2565 
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)2566 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2567 	sljit_s32 reg,
2568 	sljit_s32 mem, sljit_sw memw)
2569 {
2570 	sljit_s32 flags;
2571 	sljit_uw is_type1_transfer, inst;
2572 
2573 	CHECK_ERROR();
2574 	CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2575 
2576 	is_type1_transfer = 1;
2577 
2578 	switch (type & 0xff) {
2579 	case SLJIT_MOV:
2580 	case SLJIT_MOV_U32:
2581 	case SLJIT_MOV_S32:
2582 	case SLJIT_MOV_P:
2583 		flags = WORD_SIZE;
2584 		break;
2585 	case SLJIT_MOV_U8:
2586 		flags = BYTE_SIZE;
2587 		break;
2588 	case SLJIT_MOV_S8:
2589 		if (!(type & SLJIT_MEM_STORE))
2590 			is_type1_transfer = 0;
2591 		flags = BYTE_SIZE | SIGNED;
2592 		break;
2593 	case SLJIT_MOV_U16:
2594 		is_type1_transfer = 0;
2595 		flags = HALF_SIZE;
2596 		break;
2597 	case SLJIT_MOV_S16:
2598 		is_type1_transfer = 0;
2599 		flags = HALF_SIZE | SIGNED;
2600 		break;
2601 	default:
2602 		SLJIT_UNREACHABLE();
2603 		flags = WORD_SIZE;
2604 		break;
2605 	}
2606 
2607 	if (!(type & SLJIT_MEM_STORE))
2608 		flags |= LOAD_DATA;
2609 
2610 	SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
2611 
2612 	if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2613 		if (!is_type1_transfer && memw != 0)
2614 			return SLJIT_ERR_UNSUPPORTED;
2615 	}
2616 	else {
2617 		if (is_type1_transfer) {
2618 			if (memw > 4095 && memw < -4095)
2619 				return SLJIT_ERR_UNSUPPORTED;
2620 		}
2621 		else {
2622 			if (memw > 255 && memw < -255)
2623 				return SLJIT_ERR_UNSUPPORTED;
2624 		}
2625 	}
2626 
2627 	if (type & SLJIT_MEM_SUPP)
2628 		return SLJIT_SUCCESS;
2629 
2630 	if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2631 		memw &= 0x3;
2632 
2633 		inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | (memw << 7));
2634 
2635 		if (is_type1_transfer)
2636 			inst |= (1 << 25);
2637 
2638 		if (type & SLJIT_MEM_PRE)
2639 			inst |= (1 << 21);
2640 		else
2641 			inst ^= (1 << 24);
2642 
2643 		return push_inst(compiler, inst);
2644 	}
2645 
2646 	inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
2647 
2648 	if (type & SLJIT_MEM_PRE)
2649 		inst |= (1 << 21);
2650 	else
2651 		inst ^= (1 << 24);
2652 
2653 	if (is_type1_transfer) {
2654 		if (memw >= 0)
2655 			inst |= (1 << 23);
2656 		else
2657 			memw = -memw;
2658 
2659 		return push_inst(compiler, inst | memw);
2660 	}
2661 
2662 	if (memw >= 0)
2663 		inst |= (1 << 23);
2664 	else
2665 		memw = -memw;
2666 
2667 	return push_inst(compiler, inst | TYPE2_TRANSFER_IMM(memw));
2668 }
2669 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2670 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2671 {
2672 	struct sljit_const *const_;
2673 	sljit_s32 dst_r;
2674 
2675 	CHECK_ERROR_PTR();
2676 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2677 	ADJUST_LOCAL_OFFSET(dst, dstw);
2678 
2679 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2680 
2681 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2682 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), init_value));
2683 	compiler->patches++;
2684 #else
2685 	PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
2686 #endif
2687 
2688 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2689 	PTR_FAIL_IF(!const_);
2690 	set_const(const_, compiler);
2691 
2692 	if (dst & SLJIT_MEM)
2693 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2694 	return const_;
2695 }
2696 
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2697 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2698 {
2699 	struct sljit_put_label *put_label;
2700 	sljit_s32 dst_r;
2701 
2702 	CHECK_ERROR_PTR();
2703 	CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2704 	ADJUST_LOCAL_OFFSET(dst, dstw);
2705 
2706 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2707 
2708 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2709 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
2710 	compiler->patches++;
2711 #else
2712 	PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
2713 #endif
2714 
2715 	put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2716 	PTR_FAIL_IF(!put_label);
2717 	set_put_label(put_label, compiler, 0);
2718 
2719 	if (dst & SLJIT_MEM)
2720 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2721 	return put_label;
2722 }
2723 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2724 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2725 {
2726 	inline_set_jump_addr(addr, executable_offset, new_target, 1);
2727 }
2728 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2729 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2730 {
2731 	inline_set_const(addr, executable_offset, new_constant, 1);
2732 }
2733