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 							next_addr = compute_next_addr(label, jump, const_, put_label);
671 						}
672 					}
673 				}
674 			}
675 			else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
676 #endif
677 				*code_ptr = *buf_ptr++;
678 				if (next_addr == word_count) {
679 					SLJIT_ASSERT(!label || label->size >= word_count);
680 					SLJIT_ASSERT(!jump || jump->addr >= word_count);
681 					SLJIT_ASSERT(!const_ || const_->addr >= word_count);
682 					SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
683 
684 				/* These structures are ordered by their address. */
685 					if (jump && jump->addr == word_count) {
686 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
687 						if (detect_jump_type(jump, code_ptr, code, executable_offset))
688 							code_ptr--;
689 						jump->addr = (sljit_uw)code_ptr;
690 #else
691 						jump->addr = (sljit_uw)(code_ptr - 2);
692 						if (detect_jump_type(jump, code_ptr, code, executable_offset))
693 							code_ptr -= 2;
694 #endif
695 						jump = jump->next;
696 					}
697 					if (label && label->size == word_count) {
698 						/* code_ptr can be affected above. */
699 						label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
700 						label->size = (code_ptr + 1) - code;
701 						label = label->next;
702 					}
703 					if (const_ && const_->addr == word_count) {
704 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
705 						const_->addr = (sljit_uw)code_ptr;
706 #else
707 						const_->addr = (sljit_uw)(code_ptr - 1);
708 #endif
709 						const_ = const_->next;
710 					}
711 					if (put_label && put_label->addr == word_count) {
712 						SLJIT_ASSERT(put_label->label);
713 						put_label->addr = (sljit_uw)code_ptr;
714 						put_label = put_label->next;
715 					}
716 					next_addr = compute_next_addr(label, jump, const_, put_label);
717 				}
718 				code_ptr++;
719 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
720 			}
721 			else {
722 				/* Fortunately, no need to shift. */
723 				cpool_size = *buf_ptr++ & ~PUSH_POOL;
724 				SLJIT_ASSERT(cpool_size > 0);
725 				cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
726 				cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
727 				if (cpool_current_index > 0) {
728 					/* Unconditional branch. */
729 					*code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
730 					code_ptr = cpool_start_address + cpool_current_index;
731 				}
732 				cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
733 				cpool_current_index = 0;
734 				last_pc_patch = code_ptr;
735 			}
736 #endif
737 		} while (buf_ptr < buf_end);
738 		buf = buf->next;
739 	} while (buf);
740 
741 	SLJIT_ASSERT(!label);
742 	SLJIT_ASSERT(!jump);
743 	SLJIT_ASSERT(!const_);
744 	SLJIT_ASSERT(!put_label);
745 
746 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
747 	SLJIT_ASSERT(cpool_size == 0);
748 	if (compiler->cpool_fill > 0) {
749 		cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
750 		cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
751 		if (cpool_current_index > 0)
752 			code_ptr = cpool_start_address + cpool_current_index;
753 
754 		buf_ptr = compiler->cpool;
755 		buf_end = buf_ptr + compiler->cpool_fill;
756 		cpool_current_index = 0;
757 		while (buf_ptr < buf_end) {
758 			if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
759 				SLJIT_FREE_EXEC(code);
760 				compiler->error = SLJIT_ERR_ALLOC_FAILED;
761 				return NULL;
762 			}
763 			buf_ptr++;
764 			cpool_current_index++;
765 		}
766 		SLJIT_ASSERT(!first_patch);
767 	}
768 #endif
769 
770 	jump = compiler->jumps;
771 	while (jump) {
772 		buf_ptr = (sljit_uw *)jump->addr;
773 
774 		if (jump->flags & PATCH_B) {
775 			addr = (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
776 			if (!(jump->flags & JUMP_ADDR)) {
777 				SLJIT_ASSERT(jump->flags & JUMP_LABEL);
778 				SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - addr) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - addr) >= -0x02000000);
779 				*buf_ptr |= (((sljit_sw)jump->u.label->addr - addr) >> 2) & 0x00ffffff;
780 			}
781 			else {
782 				SLJIT_ASSERT(((sljit_sw)jump->u.target - addr) <= 0x01ffffff && ((sljit_sw)jump->u.target - addr) >= -0x02000000);
783 				*buf_ptr |= (((sljit_sw)jump->u.target - addr) >> 2) & 0x00ffffff;
784 			}
785 		}
786 		else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
787 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
788 			jump->addr = (sljit_uw)code_ptr;
789 			code_ptr[0] = (sljit_uw)buf_ptr;
790 			code_ptr[1] = *buf_ptr;
791 			inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
792 			code_ptr += 2;
793 #else
794 			inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
795 #endif
796 		}
797 		else {
798 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
799 			if (jump->flags & IS_BL)
800 				buf_ptr--;
801 			if (*buf_ptr & (1 << 23))
802 				buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
803 			else
804 				buf_ptr += 1;
805 			*buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
806 #else
807 			inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
808 #endif
809 		}
810 		jump = jump->next;
811 	}
812 
813 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
814 	const_ = compiler->consts;
815 	while (const_) {
816 		buf_ptr = (sljit_uw*)const_->addr;
817 		const_->addr = (sljit_uw)code_ptr;
818 
819 		code_ptr[0] = (sljit_uw)buf_ptr;
820 		code_ptr[1] = *buf_ptr;
821 		if (*buf_ptr & (1 << 23))
822 			buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
823 		else
824 			buf_ptr += 1;
825 		/* Set the value again (can be a simple constant). */
826 		inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
827 		code_ptr += 2;
828 
829 		const_ = const_->next;
830 	}
831 #endif
832 
833 	put_label = compiler->put_labels;
834 	while (put_label) {
835 		addr = put_label->label->addr;
836 		buf_ptr = (sljit_uw*)put_label->addr;
837 
838 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
839 		SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
840 		buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
841 #else
842 		SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
843 		buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
844 		buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
845 #endif
846 		put_label = put_label->next;
847 	}
848 
849 	SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
850 
851 	compiler->error = SLJIT_ERR_COMPILED;
852 	compiler->executable_offset = executable_offset;
853 	compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
854 
855 	code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
856 	code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
857 
858 	SLJIT_CACHE_FLUSH(code, code_ptr);
859 	return code;
860 }
861 
sljit_has_cpu_feature(sljit_s32 feature_type)862 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
863 {
864 	switch (feature_type) {
865 	case SLJIT_HAS_FPU:
866 #ifdef SLJIT_IS_FPU_AVAILABLE
867 		return SLJIT_IS_FPU_AVAILABLE;
868 #else
869 		/* Available by default. */
870 		return 1;
871 #endif
872 
873 	case SLJIT_HAS_CLZ:
874 	case SLJIT_HAS_CMOV:
875 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
876 	case SLJIT_HAS_PREFETCH:
877 #endif
878 		return 1;
879 
880 	default:
881 		return 0;
882 	}
883 }
884 
885 /* --------------------------------------------------------------------- */
886 /*  Entry, exit                                                          */
887 /* --------------------------------------------------------------------- */
888 
889 /* Creates an index in data_transfer_insts array. */
890 #define WORD_SIZE	0x00
891 #define BYTE_SIZE	0x01
892 #define HALF_SIZE	0x02
893 #define PRELOAD		0x03
894 #define SIGNED		0x04
895 #define LOAD_DATA	0x08
896 
897 /* Flag bits for emit_op. */
898 #define ALLOW_IMM	0x10
899 #define ALLOW_INV_IMM	0x20
900 #define ALLOW_ANY_IMM	(ALLOW_IMM | ALLOW_INV_IMM)
901 
902 /* s/l - store/load (1 bit)
903    u/s - signed/unsigned (1 bit)
904    w/b/h/N - word/byte/half/NOT allowed (2 bit)
905    Storing signed and unsigned values are the same operations. */
906 
907 static const sljit_uw data_transfer_insts[16] = {
908 /* s u w */ 0xe5000000 /* str */,
909 /* s u b */ 0xe5400000 /* strb */,
910 /* s u h */ 0xe10000b0 /* strh */,
911 /* s u N */ 0x00000000 /* not allowed */,
912 /* s s w */ 0xe5000000 /* str */,
913 /* s s b */ 0xe5400000 /* strb */,
914 /* s s h */ 0xe10000b0 /* strh */,
915 /* s s N */ 0x00000000 /* not allowed */,
916 
917 /* l u w */ 0xe5100000 /* ldr */,
918 /* l u b */ 0xe5500000 /* ldrb */,
919 /* l u h */ 0xe11000b0 /* ldrh */,
920 /* l u p */ 0xf5500000 /* preload */,
921 /* l s w */ 0xe5100000 /* ldr */,
922 /* l s b */ 0xe11000d0 /* ldrsb */,
923 /* l s h */ 0xe11000f0 /* ldrsh */,
924 /* l s N */ 0x00000000 /* not allowed */,
925 };
926 
927 #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
928 	(data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
929 
930 /* Normal ldr/str instruction.
931    Type2: ldrsb, ldrh, ldrsh */
932 #define IS_TYPE1_TRANSFER(type) \
933 	(data_transfer_insts[(type) & 0xf] & 0x04000000)
934 #define TYPE2_TRANSFER_IMM(imm) \
935 	(((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
936 
937 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
938 	sljit_s32 dst, sljit_sw dstw,
939 	sljit_s32 src1, sljit_sw src1w,
940 	sljit_s32 src2, sljit_sw src2w);
941 
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)942 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
943 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
944 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
945 {
946 	sljit_s32 args, size, i, tmp;
947 	sljit_uw push;
948 
949 	CHECK_ERROR();
950 	CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
951 	set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
952 
953 	/* Push saved registers, temporary registers
954 	   stmdb sp!, {..., lr} */
955 	push = PUSH | (1 << 14);
956 
957 	tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
958 	for (i = SLJIT_S0; i >= tmp; i--)
959 		push |= 1 << reg_map[i];
960 
961 	for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
962 		push |= 1 << reg_map[i];
963 
964 	FAIL_IF(push_inst(compiler, push));
965 
966 	/* Stack must be aligned to 8 bytes: */
967 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
968 	local_size = ((size + local_size + 7) & ~7) - size;
969 	compiler->local_size = local_size;
970 	if (local_size > 0)
971 		FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
972 
973 	args = get_arg_count(arg_types);
974 
975 	if (args >= 1)
976 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0) | RM(SLJIT_R0)));
977 	if (args >= 2)
978 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S1) | RM(SLJIT_R1)));
979 	if (args >= 3)
980 		FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S2) | RM(SLJIT_R2)));
981 
982 	return SLJIT_SUCCESS;
983 }
984 
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)985 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
986 	sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
987 	sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
988 {
989 	sljit_s32 size;
990 
991 	CHECK_ERROR();
992 	CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
993 	set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
994 
995 	size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
996 	compiler->local_size = ((size + local_size + 7) & ~7) - size;
997 	return SLJIT_SUCCESS;
998 }
999 
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1000 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1001 {
1002 	sljit_s32 i, tmp;
1003 	sljit_uw pop;
1004 
1005 	CHECK_ERROR();
1006 	CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1007 
1008 	FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1009 
1010 	if (compiler->local_size > 0)
1011 		FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
1012 
1013 	/* Push saved registers, temporary registers
1014 	   ldmia sp!, {..., pc} */
1015 	pop = POP | (1 << 15);
1016 
1017 	tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1018 	for (i = SLJIT_S0; i >= tmp; i--)
1019 		pop |= 1 << reg_map[i];
1020 
1021 	for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
1022 		pop |= 1 << reg_map[i];
1023 
1024 	return push_inst(compiler, pop);
1025 }
1026 
1027 /* --------------------------------------------------------------------- */
1028 /*  Operators                                                            */
1029 /* --------------------------------------------------------------------- */
1030 
1031 /* flags: */
1032   /* Arguments are swapped. */
1033 #define ARGS_SWAPPED	0x01
1034   /* Inverted immediate. */
1035 #define INV_IMM		0x02
1036   /* Source and destination is register. */
1037 #define MOVE_REG_CONV	0x04
1038   /* Unused return value. */
1039 #define UNUSED_RETURN	0x08
1040 /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
1041 #define SET_FLAGS	(1 << 20)
1042 /* dst: reg
1043    src1: reg
1044    src2: reg or imm (if allowed)
1045    SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
1046 #define SRC2_IMM	(1 << 25)
1047 
1048 #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
1049 	SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
1050 	if (compiler->shift_imm != 0x20) { \
1051 		SLJIT_ASSERT(src1 == TMP_REG1); \
1052 		SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
1053 		\
1054 		if (compiler->shift_imm != 0) \
1055 			return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
1056 				RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
1057 		return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
1058 	} \
1059 	return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
1060 		(reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
1061 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_s32 src1,sljit_s32 src2)1062 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
1063 	sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
1064 {
1065 	switch (GET_OPCODE(op)) {
1066 	case SLJIT_MOV:
1067 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1068 		if (dst != src2) {
1069 			if (src2 & SRC2_IMM) {
1070 				return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1071 			}
1072 			return push_inst(compiler, MOV | RD(dst) | RM(src2));
1073 		}
1074 		return SLJIT_SUCCESS;
1075 
1076 	case SLJIT_MOV_U8:
1077 	case SLJIT_MOV_S8:
1078 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1079 		if (flags & MOVE_REG_CONV) {
1080 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1081 			if (op == SLJIT_MOV_U8)
1082 				return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
1083 			FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
1084 			return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
1085 #else
1086 			return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
1087 #endif
1088 		}
1089 		else if (dst != src2) {
1090 			SLJIT_ASSERT(src2 & SRC2_IMM);
1091 			return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1092 		}
1093 		return SLJIT_SUCCESS;
1094 
1095 	case SLJIT_MOV_U16:
1096 	case SLJIT_MOV_S16:
1097 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
1098 		if (flags & MOVE_REG_CONV) {
1099 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1100 			FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
1101 			return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
1102 #else
1103 			return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
1104 #endif
1105 		}
1106 		else if (dst != src2) {
1107 			SLJIT_ASSERT(src2 & SRC2_IMM);
1108 			return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
1109 		}
1110 		return SLJIT_SUCCESS;
1111 
1112 	case SLJIT_NOT:
1113 		if (src2 & SRC2_IMM) {
1114 			return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
1115 		}
1116 		return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
1117 
1118 	case SLJIT_CLZ:
1119 		SLJIT_ASSERT(!(flags & INV_IMM));
1120 		SLJIT_ASSERT(!(src2 & SRC2_IMM));
1121 		FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
1122 		return SLJIT_SUCCESS;
1123 
1124 	case SLJIT_ADD:
1125 		SLJIT_ASSERT(!(flags & INV_IMM));
1126 		if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1127 			return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1128 		return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1129 
1130 	case SLJIT_ADDC:
1131 		SLJIT_ASSERT(!(flags & INV_IMM));
1132 		return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1133 
1134 	case SLJIT_SUB:
1135 		SLJIT_ASSERT(!(flags & INV_IMM));
1136 		if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
1137 			return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1138 		return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
1139 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1140 
1141 	case SLJIT_SUBC:
1142 		SLJIT_ASSERT(!(flags & INV_IMM));
1143 		return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
1144 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1145 
1146 	case SLJIT_MUL:
1147 		SLJIT_ASSERT(!(flags & INV_IMM));
1148 		SLJIT_ASSERT(!(src2 & SRC2_IMM));
1149 
1150 		if (!HAS_FLAGS(op))
1151 			return push_inst(compiler, MUL | (reg_map[dst] << 16) | (reg_map[src2] << 8) | reg_map[src1]);
1152 
1153 		FAIL_IF(push_inst(compiler, SMULL | (reg_map[TMP_REG1] << 16) | (reg_map[dst] << 12) | (reg_map[src2] << 8) | reg_map[src1]));
1154 
1155 		/* cmp TMP_REG1, dst asr #31. */
1156 		return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
1157 
1158 	case SLJIT_AND:
1159 		return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
1160 			| RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1161 
1162 	case SLJIT_OR:
1163 		SLJIT_ASSERT(!(flags & INV_IMM));
1164 		return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1165 
1166 	case SLJIT_XOR:
1167 		SLJIT_ASSERT(!(flags & INV_IMM));
1168 		return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
1169 
1170 	case SLJIT_SHL:
1171 		EMIT_SHIFT_INS_AND_RETURN(0);
1172 
1173 	case SLJIT_LSHR:
1174 		EMIT_SHIFT_INS_AND_RETURN(1);
1175 
1176 	case SLJIT_ASHR:
1177 		EMIT_SHIFT_INS_AND_RETURN(2);
1178 	}
1179 
1180 	SLJIT_UNREACHABLE();
1181 	return SLJIT_SUCCESS;
1182 }
1183 
1184 #undef EMIT_SHIFT_INS_AND_RETURN
1185 
1186 /* Tests whether the immediate can be stored in the 12 bit imm field.
1187    Returns with 0 if not possible. */
get_imm(sljit_uw imm)1188 static sljit_uw get_imm(sljit_uw imm)
1189 {
1190 	sljit_s32 rol;
1191 
1192 	if (imm <= 0xff)
1193 		return SRC2_IMM | imm;
1194 
1195 	if (!(imm & 0xff000000)) {
1196 		imm <<= 8;
1197 		rol = 8;
1198 	}
1199 	else {
1200 		imm = (imm << 24) | (imm >> 8);
1201 		rol = 0;
1202 	}
1203 
1204 	if (!(imm & 0xff000000)) {
1205 		imm <<= 8;
1206 		rol += 4;
1207 	}
1208 
1209 	if (!(imm & 0xf0000000)) {
1210 		imm <<= 4;
1211 		rol += 2;
1212 	}
1213 
1214 	if (!(imm & 0xc0000000)) {
1215 		imm <<= 2;
1216 		rol += 1;
1217 	}
1218 
1219 	if (!(imm & 0x00ffffff))
1220 		return SRC2_IMM | (imm >> 24) | (rol << 8);
1221 	else
1222 		return 0;
1223 }
1224 
1225 #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)1226 static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
1227 {
1228 	sljit_uw mask;
1229 	sljit_uw imm1;
1230 	sljit_uw imm2;
1231 	sljit_s32 rol;
1232 
1233 	/* Step1: Search a zero byte (8 continous zero bit). */
1234 	mask = 0xff000000;
1235 	rol = 8;
1236 	while(1) {
1237 		if (!(imm & mask)) {
1238 			/* Rol imm by rol. */
1239 			imm = (imm << rol) | (imm >> (32 - rol));
1240 			/* Calculate arm rol. */
1241 			rol = 4 + (rol >> 1);
1242 			break;
1243 		}
1244 		rol += 2;
1245 		mask >>= 2;
1246 		if (mask & 0x3) {
1247 			/* rol by 8. */
1248 			imm = (imm << 8) | (imm >> 24);
1249 			mask = 0xff00;
1250 			rol = 24;
1251 			while (1) {
1252 				if (!(imm & mask)) {
1253 					/* Rol imm by rol. */
1254 					imm = (imm << rol) | (imm >> (32 - rol));
1255 					/* Calculate arm rol. */
1256 					rol = (rol >> 1) - 8;
1257 					break;
1258 				}
1259 				rol += 2;
1260 				mask >>= 2;
1261 				if (mask & 0x3)
1262 					return 0;
1263 			}
1264 			break;
1265 		}
1266 	}
1267 
1268 	/* The low 8 bit must be zero. */
1269 	SLJIT_ASSERT(!(imm & 0xff));
1270 
1271 	if (!(imm & 0xff000000)) {
1272 		imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
1273 		imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
1274 	}
1275 	else if (imm & 0xc0000000) {
1276 		imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1277 		imm <<= 8;
1278 		rol += 4;
1279 
1280 		if (!(imm & 0xff000000)) {
1281 			imm <<= 8;
1282 			rol += 4;
1283 		}
1284 
1285 		if (!(imm & 0xf0000000)) {
1286 			imm <<= 4;
1287 			rol += 2;
1288 		}
1289 
1290 		if (!(imm & 0xc0000000)) {
1291 			imm <<= 2;
1292 			rol += 1;
1293 		}
1294 
1295 		if (!(imm & 0x00ffffff))
1296 			imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1297 		else
1298 			return 0;
1299 	}
1300 	else {
1301 		if (!(imm & 0xf0000000)) {
1302 			imm <<= 4;
1303 			rol += 2;
1304 		}
1305 
1306 		if (!(imm & 0xc0000000)) {
1307 			imm <<= 2;
1308 			rol += 1;
1309 		}
1310 
1311 		imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
1312 		imm <<= 8;
1313 		rol += 4;
1314 
1315 		if (!(imm & 0xf0000000)) {
1316 			imm <<= 4;
1317 			rol += 2;
1318 		}
1319 
1320 		if (!(imm & 0xc0000000)) {
1321 			imm <<= 2;
1322 			rol += 1;
1323 		}
1324 
1325 		if (!(imm & 0x00ffffff))
1326 			imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
1327 		else
1328 			return 0;
1329 	}
1330 
1331 	FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
1332 	FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
1333 	return 1;
1334 }
1335 #endif
1336 
load_immediate(struct sljit_compiler * compiler,sljit_s32 reg,sljit_uw imm)1337 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
1338 {
1339 	sljit_uw tmp;
1340 
1341 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1342 	if (!(imm & ~0xffff))
1343 		return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
1344 #endif
1345 
1346 	/* Create imm by 1 inst. */
1347 	tmp = get_imm(imm);
1348 	if (tmp)
1349 		return push_inst(compiler, MOV | RD(reg) | tmp);
1350 
1351 	tmp = get_imm(~imm);
1352 	if (tmp)
1353 		return push_inst(compiler, MVN | RD(reg) | tmp);
1354 
1355 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
1356 	/* Create imm by 2 inst. */
1357 	FAIL_IF(generate_int(compiler, reg, imm, 1));
1358 	FAIL_IF(generate_int(compiler, reg, ~imm, 0));
1359 
1360 	/* Load integer. */
1361 	return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
1362 #else
1363 	FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
1364 	if (imm <= 0xffff)
1365 		return SLJIT_SUCCESS;
1366 	return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
1367 #endif
1368 }
1369 
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)1370 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
1371 	sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
1372 {
1373 	sljit_uw imm, offset_reg;
1374 	sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
1375 
1376 	SLJIT_ASSERT (arg & SLJIT_MEM);
1377 	SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
1378 
1379 	if ((arg & REG_MASK) == SLJIT_UNUSED) {
1380 		if (is_type1_transfer) {
1381 			FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xfff));
1382 			argw &= 0xfff;
1383 		}
1384 		else {
1385 			FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xff));
1386 			argw &= 0xff;
1387 		}
1388 
1389 		return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
1390 			is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
1391 	}
1392 
1393 	if (arg & OFFS_REG_MASK) {
1394 		offset_reg = OFFS_REG(arg);
1395 		arg &= REG_MASK;
1396 		argw &= 0x3;
1397 
1398 		if (argw != 0 && !is_type1_transfer) {
1399 			FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | (argw << 7)));
1400 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
1401 		}
1402 
1403 		/* Bit 25: RM is offset. */
1404 		return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1405 			RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | (argw << 7)));
1406 	}
1407 
1408 	arg &= REG_MASK;
1409 
1410 	if (is_type1_transfer) {
1411 		if (argw > 0xfff) {
1412 			imm = get_imm(argw & ~0xfff);
1413 			if (imm) {
1414 				FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1415 				argw = argw & 0xfff;
1416 				arg = tmp_reg;
1417 			}
1418 		}
1419 		else if (argw < -0xfff) {
1420 			imm = get_imm(-argw & ~0xfff);
1421 			if (imm) {
1422 				FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1423 				argw = -(-argw & 0xfff);
1424 				arg = tmp_reg;
1425 			}
1426 		}
1427 
1428 		if (argw >= 0 && argw <= 0xfff)
1429 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
1430 
1431 		if (argw < 0 && argw >= -0xfff)
1432 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
1433 	}
1434 	else {
1435 		if (argw > 0xff) {
1436 			imm = get_imm(argw & ~0xff);
1437 			if (imm) {
1438 				FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
1439 				argw = argw & 0xff;
1440 				arg = tmp_reg;
1441 			}
1442 		}
1443 		else if (argw < -0xff) {
1444 			imm = get_imm(-argw & ~0xff);
1445 			if (imm) {
1446 				FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
1447 				argw = -(-argw & 0xff);
1448 				arg = tmp_reg;
1449 			}
1450 		}
1451 
1452 		if (argw >= 0 && argw <= 0xff)
1453 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1454 
1455 		if (argw < 0 && argw >= -0xff) {
1456 			argw = -argw;
1457 			return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
1458 		}
1459 	}
1460 
1461 	FAIL_IF(load_immediate(compiler, tmp_reg, argw));
1462 	return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
1463 		RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
1464 }
1465 
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)1466 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
1467 	sljit_s32 dst, sljit_sw dstw,
1468 	sljit_s32 src1, sljit_sw src1w,
1469 	sljit_s32 src2, sljit_sw src2w)
1470 {
1471 	/* src1 is reg or TMP_REG1
1472 	   src2 is reg, TMP_REG2, or imm
1473 	   result goes to TMP_REG2, so put result can use TMP_REG1. */
1474 
1475 	/* We prefers register and simple consts. */
1476 	sljit_s32 dst_reg;
1477 	sljit_s32 src1_reg;
1478 	sljit_s32 src2_reg;
1479 	sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1480 
1481 	/* Destination check. */
1482 	if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED))
1483 		flags |= UNUSED_RETURN;
1484 
1485 	SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
1486 
1487 	src2_reg = 0;
1488 
1489 	do {
1490 		if (!(inp_flags & ALLOW_IMM))
1491 			break;
1492 
1493 		if (src2 & SLJIT_IMM) {
1494 			src2_reg = get_imm(src2w);
1495 			if (src2_reg)
1496 				break;
1497 			if (inp_flags & ALLOW_INV_IMM) {
1498 				src2_reg = get_imm(~src2w);
1499 				if (src2_reg) {
1500 					flags |= INV_IMM;
1501 					break;
1502 				}
1503 			}
1504 			if (GET_OPCODE(op) == SLJIT_ADD) {
1505 				src2_reg = get_imm(-src2w);
1506 				if (src2_reg) {
1507 					op = SLJIT_SUB | GET_ALL_FLAGS(op);
1508 					break;
1509 				}
1510 			}
1511 			if (GET_OPCODE(op) == SLJIT_SUB) {
1512 				src2_reg = get_imm(-src2w);
1513 				if (src2_reg) {
1514 					op = SLJIT_ADD | GET_ALL_FLAGS(op);
1515 					break;
1516 				}
1517 			}
1518 		}
1519 
1520 		if (src1 & SLJIT_IMM) {
1521 			src2_reg = get_imm(src1w);
1522 			if (src2_reg) {
1523 				flags |= ARGS_SWAPPED;
1524 				src1 = src2;
1525 				src1w = src2w;
1526 				break;
1527 			}
1528 			if (inp_flags & ALLOW_INV_IMM) {
1529 				src2_reg = get_imm(~src1w);
1530 				if (src2_reg) {
1531 					flags |= ARGS_SWAPPED | INV_IMM;
1532 					src1 = src2;
1533 					src1w = src2w;
1534 					break;
1535 				}
1536 			}
1537 			if (GET_OPCODE(op) == SLJIT_ADD) {
1538 				src2_reg = get_imm(-src1w);
1539 				if (src2_reg) {
1540 					/* Note: add is commutative operation. */
1541 					src1 = src2;
1542 					src1w = src2w;
1543 					op = SLJIT_SUB | GET_ALL_FLAGS(op);
1544 					break;
1545 				}
1546 			}
1547 		}
1548 	} while(0);
1549 
1550 	/* Source 1. */
1551 	if (FAST_IS_REG(src1))
1552 		src1_reg = src1;
1553 	else if (src1 & SLJIT_MEM) {
1554 		FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
1555 		src1_reg = TMP_REG1;
1556 	}
1557 	else {
1558 		FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
1559 		src1_reg = TMP_REG1;
1560 	}
1561 
1562 	/* Destination. */
1563 	dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
1564 
1565 	if (op <= SLJIT_MOV_P) {
1566 		if (dst & SLJIT_MEM) {
1567 			if (inp_flags & BYTE_SIZE)
1568 				inp_flags &= ~SIGNED;
1569 
1570 			if (FAST_IS_REG(src2))
1571 				return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
1572 		}
1573 
1574 		if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
1575 			flags |= MOVE_REG_CONV;
1576 	}
1577 
1578 	/* Source 2. */
1579 	if (src2_reg == 0) {
1580 		src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
1581 
1582 		if (FAST_IS_REG(src2))
1583 			src2_reg = src2;
1584 		else if (src2 & SLJIT_MEM)
1585 			FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
1586 		else
1587 			FAIL_IF(load_immediate(compiler, src2_reg, src2w));
1588 	}
1589 
1590 	FAIL_IF(emit_single_op(compiler, op, flags, dst_reg, src1_reg, src2_reg));
1591 
1592 	if (!(dst & SLJIT_MEM))
1593 		return SLJIT_SUCCESS;
1594 
1595 	return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
1596 }
1597 
1598 #ifdef __cplusplus
1599 extern "C" {
1600 #endif
1601 
1602 #if defined(__GNUC__)
1603 extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
1604 extern int __aeabi_idivmod(int numerator, int denominator);
1605 #else
1606 #error "Software divmod functions are needed"
1607 #endif
1608 
1609 #ifdef __cplusplus
1610 }
1611 #endif
1612 
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1613 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1614 {
1615 	sljit_sw saved_reg_list[3];
1616 	sljit_sw saved_reg_count;
1617 
1618 	CHECK_ERROR();
1619 	CHECK(check_sljit_emit_op0(compiler, op));
1620 
1621 	op = GET_OPCODE(op);
1622 	switch (op) {
1623 	case SLJIT_BREAKPOINT:
1624 		FAIL_IF(push_inst(compiler, BKPT));
1625 		break;
1626 	case SLJIT_NOP:
1627 		FAIL_IF(push_inst(compiler, NOP));
1628 		break;
1629 	case SLJIT_LMUL_UW:
1630 	case SLJIT_LMUL_SW:
1631 		return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
1632 			| (reg_map[SLJIT_R1] << 16)
1633 			| (reg_map[SLJIT_R0] << 12)
1634 			| (reg_map[SLJIT_R0] << 8)
1635 			| reg_map[SLJIT_R1]);
1636 	case SLJIT_DIVMOD_UW:
1637 	case SLJIT_DIVMOD_SW:
1638 	case SLJIT_DIV_UW:
1639 	case SLJIT_DIV_SW:
1640 		SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
1641 		SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
1642 
1643 		saved_reg_count = 0;
1644 		if (compiler->scratches >= 4)
1645 			saved_reg_list[saved_reg_count++] = 3;
1646 		if (compiler->scratches >= 3)
1647 			saved_reg_list[saved_reg_count++] = 2;
1648 		if (op >= SLJIT_DIV_UW)
1649 			saved_reg_list[saved_reg_count++] = 1;
1650 
1651 		if (saved_reg_count > 0) {
1652 			FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
1653 						| (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
1654 			if (saved_reg_count >= 2) {
1655 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1656 				FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
1657 			}
1658 			if (saved_reg_count >= 3) {
1659 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1660 				FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
1661 			}
1662 		}
1663 
1664 #if defined(__GNUC__)
1665 		FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1666 			((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1667 #else
1668 #error "Software divmod functions are needed"
1669 #endif
1670 
1671 		if (saved_reg_count > 0) {
1672 			if (saved_reg_count >= 3) {
1673 				SLJIT_ASSERT(saved_reg_list[2] < 8);
1674 				FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
1675 			}
1676 			if (saved_reg_count >= 2) {
1677 				SLJIT_ASSERT(saved_reg_list[1] < 8);
1678 				FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
1679 			}
1680 			return push_inst(compiler, 0xe49d0000 | (saved_reg_count >= 3 ? 16 : 8)
1681 						| (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
1682 		}
1683 		return SLJIT_SUCCESS;
1684 	case SLJIT_ENDBR:
1685 	case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1686 		return SLJIT_SUCCESS;
1687 	}
1688 
1689 	return SLJIT_SUCCESS;
1690 }
1691 
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1692 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1693 	sljit_s32 dst, sljit_sw dstw,
1694 	sljit_s32 src, sljit_sw srcw)
1695 {
1696 	CHECK_ERROR();
1697 	CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1698 	ADJUST_LOCAL_OFFSET(dst, dstw);
1699 	ADJUST_LOCAL_OFFSET(src, srcw);
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_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1782 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1783 	sljit_s32 src, sljit_sw srcw)
1784 {
1785 	CHECK_ERROR();
1786 	CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1787 	ADJUST_LOCAL_OFFSET(src, srcw);
1788 
1789 	switch (op) {
1790 	case SLJIT_FAST_RETURN:
1791 		SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
1792 
1793 		if (FAST_IS_REG(src))
1794 			FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
1795 		else
1796 			FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
1797 
1798 		return push_inst(compiler, BX | RM(TMP_REG2));
1799 	case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1800 		return SLJIT_SUCCESS;
1801 	case SLJIT_PREFETCH_L1:
1802 	case SLJIT_PREFETCH_L2:
1803 	case SLJIT_PREFETCH_L3:
1804 	case SLJIT_PREFETCH_ONCE:
1805 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
1806 		SLJIT_ASSERT(src & SLJIT_MEM);
1807 		return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
1808 #else /* !SLJIT_CONFIG_ARM_V7 */
1809 		return SLJIT_SUCCESS;
1810 #endif /* SLJIT_CONFIG_ARM_V7 */
1811 	}
1812 
1813 	return SLJIT_SUCCESS;
1814 }
1815 
sljit_get_register_index(sljit_s32 reg)1816 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1817 {
1818 	CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1819 	return reg_map[reg];
1820 }
1821 
sljit_get_float_register_index(sljit_s32 reg)1822 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1823 {
1824 	CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1825 	return (freg_map[reg] << 1);
1826 }
1827 
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1828 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1829 	void *instruction, sljit_s32 size)
1830 {
1831 	CHECK_ERROR();
1832 	CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1833 
1834 	return push_inst(compiler, *(sljit_uw*)instruction);
1835 }
1836 
1837 /* --------------------------------------------------------------------- */
1838 /*  Floating point operators                                             */
1839 /* --------------------------------------------------------------------- */
1840 
1841 
1842 #define FPU_LOAD (1 << 20)
1843 #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
1844 	((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
1845 #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
1846 	((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
1847 
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1848 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1849 {
1850 	sljit_uw imm;
1851 	sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
1852 
1853 	SLJIT_ASSERT(arg & SLJIT_MEM);
1854 	arg &= ~SLJIT_MEM;
1855 
1856 	if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
1857 		FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((argw & 0x3) << 7)));
1858 		arg = TMP_REG2;
1859 		argw = 0;
1860 	}
1861 
1862 	/* Fast loads and stores. */
1863 	if (arg) {
1864 		if (!(argw & ~0x3fc))
1865 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
1866 		if (!(-argw & ~0x3fc))
1867 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
1868 
1869 		imm = get_imm(argw & ~0x3fc);
1870 		if (imm) {
1871 			FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1872 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1873 		}
1874 		imm = get_imm(-argw & ~0x3fc);
1875 		if (imm) {
1876 			argw = -argw;
1877 			FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
1878 			return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
1879 		}
1880 	}
1881 
1882 	if (arg) {
1883 		FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1884 		FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
1885 	}
1886 	else
1887 		FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
1888 
1889 	return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
1890 }
1891 
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)1892 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1893 	sljit_s32 dst, sljit_sw dstw,
1894 	sljit_s32 src, sljit_sw srcw)
1895 {
1896 	op ^= SLJIT_F32_OP;
1897 
1898 	if (src & SLJIT_MEM) {
1899 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
1900 		src = TMP_FREG1;
1901 	}
1902 
1903 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
1904 
1905 	if (FAST_IS_REG(dst))
1906 		return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (freg_map[TMP_FREG1] << 16));
1907 
1908 	/* Store the integer value from a VFP register. */
1909 	return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
1910 }
1911 
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)1912 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1913 	sljit_s32 dst, sljit_sw dstw,
1914 	sljit_s32 src, sljit_sw srcw)
1915 {
1916 	sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1917 
1918 	op ^= SLJIT_F32_OP;
1919 
1920 	if (FAST_IS_REG(src))
1921 		FAIL_IF(push_inst(compiler, VMOV | RD(src) | (freg_map[TMP_FREG1] << 16)));
1922 	else if (src & SLJIT_MEM) {
1923 		/* Load the integer value into a VFP register. */
1924 		FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
1925 	}
1926 	else {
1927 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1928 		FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (freg_map[TMP_FREG1] << 16)));
1929 	}
1930 
1931 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
1932 
1933 	if (dst & SLJIT_MEM)
1934 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
1935 	return SLJIT_SUCCESS;
1936 }
1937 
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1938 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1939 	sljit_s32 src1, sljit_sw src1w,
1940 	sljit_s32 src2, sljit_sw src2w)
1941 {
1942 	op ^= SLJIT_F32_OP;
1943 
1944 	if (src1 & SLJIT_MEM) {
1945 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
1946 		src1 = TMP_FREG1;
1947 	}
1948 
1949 	if (src2 & SLJIT_MEM) {
1950 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
1951 		src2 = TMP_FREG2;
1952 	}
1953 
1954 	FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
1955 	return push_inst(compiler, VMRS);
1956 }
1957 
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1958 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1959 	sljit_s32 dst, sljit_sw dstw,
1960 	sljit_s32 src, sljit_sw srcw)
1961 {
1962 	sljit_s32 dst_r;
1963 
1964 	CHECK_ERROR();
1965 
1966 	SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
1967 	SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1968 
1969 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1970 
1971 	if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
1972 		op ^= SLJIT_F32_OP;
1973 
1974 	if (src & SLJIT_MEM) {
1975 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
1976 		src = dst_r;
1977 	}
1978 
1979 	switch (GET_OPCODE(op)) {
1980 	case SLJIT_MOV_F64:
1981 		if (src != dst_r) {
1982 			if (dst_r != TMP_FREG1)
1983 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1984 			else
1985 				dst_r = src;
1986 		}
1987 		break;
1988 	case SLJIT_NEG_F64:
1989 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1990 		break;
1991 	case SLJIT_ABS_F64:
1992 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1993 		break;
1994 	case SLJIT_CONV_F64_FROM_F32:
1995 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
1996 		op ^= SLJIT_F32_OP;
1997 		break;
1998 	}
1999 
2000 	if (dst & SLJIT_MEM)
2001 		return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
2002 	return SLJIT_SUCCESS;
2003 }
2004 
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)2005 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
2006 	sljit_s32 dst, sljit_sw dstw,
2007 	sljit_s32 src1, sljit_sw src1w,
2008 	sljit_s32 src2, sljit_sw src2w)
2009 {
2010 	sljit_s32 dst_r;
2011 
2012 	CHECK_ERROR();
2013 	CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
2014 	ADJUST_LOCAL_OFFSET(dst, dstw);
2015 	ADJUST_LOCAL_OFFSET(src1, src1w);
2016 	ADJUST_LOCAL_OFFSET(src2, src2w);
2017 
2018 	op ^= SLJIT_F32_OP;
2019 
2020 	dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
2021 
2022 	if (src2 & SLJIT_MEM) {
2023 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
2024 		src2 = TMP_FREG2;
2025 	}
2026 
2027 	if (src1 & SLJIT_MEM) {
2028 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
2029 		src1 = TMP_FREG1;
2030 	}
2031 
2032 	switch (GET_OPCODE(op)) {
2033 	case SLJIT_ADD_F64:
2034 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2035 		break;
2036 
2037 	case SLJIT_SUB_F64:
2038 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2039 		break;
2040 
2041 	case SLJIT_MUL_F64:
2042 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2043 		break;
2044 
2045 	case SLJIT_DIV_F64:
2046 		FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
2047 		break;
2048 	}
2049 
2050 	if (dst_r == TMP_FREG1)
2051 		FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
2052 
2053 	return SLJIT_SUCCESS;
2054 }
2055 
2056 #undef FPU_LOAD
2057 #undef EMIT_FPU_DATA_TRANSFER
2058 
2059 /* --------------------------------------------------------------------- */
2060 /*  Other instructions                                                   */
2061 /* --------------------------------------------------------------------- */
2062 
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2063 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2064 {
2065 	CHECK_ERROR();
2066 	CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
2067 	ADJUST_LOCAL_OFFSET(dst, dstw);
2068 
2069 	SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
2070 
2071 	if (FAST_IS_REG(dst))
2072 		return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
2073 
2074 	/* Memory. */
2075 	return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
2076 }
2077 
2078 /* --------------------------------------------------------------------- */
2079 /*  Conditional instructions                                             */
2080 /* --------------------------------------------------------------------- */
2081 
get_cc(sljit_s32 type)2082 static sljit_uw get_cc(sljit_s32 type)
2083 {
2084 	switch (type) {
2085 	case SLJIT_EQUAL:
2086 	case SLJIT_MUL_NOT_OVERFLOW:
2087 	case SLJIT_EQUAL_F64:
2088 		return 0x00000000;
2089 
2090 	case SLJIT_NOT_EQUAL:
2091 	case SLJIT_MUL_OVERFLOW:
2092 	case SLJIT_NOT_EQUAL_F64:
2093 		return 0x10000000;
2094 
2095 	case SLJIT_LESS:
2096 	case SLJIT_LESS_F64:
2097 		return 0x30000000;
2098 
2099 	case SLJIT_GREATER_EQUAL:
2100 	case SLJIT_GREATER_EQUAL_F64:
2101 		return 0x20000000;
2102 
2103 	case SLJIT_GREATER:
2104 	case SLJIT_GREATER_F64:
2105 		return 0x80000000;
2106 
2107 	case SLJIT_LESS_EQUAL:
2108 	case SLJIT_LESS_EQUAL_F64:
2109 		return 0x90000000;
2110 
2111 	case SLJIT_SIG_LESS:
2112 		return 0xb0000000;
2113 
2114 	case SLJIT_SIG_GREATER_EQUAL:
2115 		return 0xa0000000;
2116 
2117 	case SLJIT_SIG_GREATER:
2118 		return 0xc0000000;
2119 
2120 	case SLJIT_SIG_LESS_EQUAL:
2121 		return 0xd0000000;
2122 
2123 	case SLJIT_OVERFLOW:
2124 	case SLJIT_UNORDERED_F64:
2125 		return 0x60000000;
2126 
2127 	case SLJIT_NOT_OVERFLOW:
2128 	case SLJIT_ORDERED_F64:
2129 		return 0x70000000;
2130 
2131 	default:
2132 		SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
2133 		return 0xe0000000;
2134 	}
2135 }
2136 
sljit_emit_label(struct sljit_compiler * compiler)2137 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
2138 {
2139 	struct sljit_label *label;
2140 
2141 	CHECK_ERROR_PTR();
2142 	CHECK_PTR(check_sljit_emit_label(compiler));
2143 
2144 	if (compiler->last_label && compiler->last_label->size == compiler->size)
2145 		return compiler->last_label;
2146 
2147 	label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
2148 	PTR_FAIL_IF(!label);
2149 	set_label(label, compiler);
2150 	return label;
2151 }
2152 
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)2153 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
2154 {
2155 	struct sljit_jump *jump;
2156 
2157 	CHECK_ERROR_PTR();
2158 	CHECK_PTR(check_sljit_emit_jump(compiler, type));
2159 
2160 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2161 	PTR_FAIL_IF(!jump);
2162 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
2163 	type &= 0xff;
2164 
2165 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2166 
2167 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2168 	if (type >= SLJIT_FAST_CALL)
2169 		PTR_FAIL_IF(prepare_blx(compiler));
2170 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
2171 		type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
2172 
2173 	if (jump->flags & SLJIT_REWRITABLE_JUMP) {
2174 		jump->addr = compiler->size;
2175 		compiler->patches++;
2176 	}
2177 
2178 	if (type >= SLJIT_FAST_CALL) {
2179 		jump->flags |= IS_BL;
2180 		PTR_FAIL_IF(emit_blx(compiler));
2181 	}
2182 
2183 	if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
2184 		jump->addr = compiler->size;
2185 #else
2186 	if (type >= SLJIT_FAST_CALL)
2187 		jump->flags |= IS_BL;
2188 	PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2189 	PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
2190 	jump->addr = compiler->size;
2191 #endif
2192 	return jump;
2193 }
2194 
2195 #ifdef __SOFTFP__
2196 
softfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)2197 static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
2198 {
2199 	sljit_s32 stack_offset = 0;
2200 	sljit_s32 arg_count = 0;
2201 	sljit_s32 word_arg_offset = 0;
2202 	sljit_s32 float_arg_count = 0;
2203 	sljit_s32 types = 0;
2204 	sljit_s32 src_offset = 4 * sizeof(sljit_sw);
2205 	sljit_u8 offsets[4];
2206 
2207 	if (src && FAST_IS_REG(*src))
2208 		src_offset = reg_map[*src] * sizeof(sljit_sw);
2209 
2210 	arg_types >>= SLJIT_DEF_SHIFT;
2211 
2212 	while (arg_types) {
2213 		types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
2214 
2215 		switch (arg_types & SLJIT_DEF_MASK) {
2216 		case SLJIT_ARG_TYPE_F32:
2217 			offsets[arg_count] = (sljit_u8)stack_offset;
2218 			stack_offset += sizeof(sljit_f32);
2219 			arg_count++;
2220 			float_arg_count++;
2221 			break;
2222 		case SLJIT_ARG_TYPE_F64:
2223 			if (stack_offset & 0x7)
2224 				stack_offset += sizeof(sljit_sw);
2225 			offsets[arg_count] = (sljit_u8)stack_offset;
2226 			stack_offset += sizeof(sljit_f64);
2227 			arg_count++;
2228 			float_arg_count++;
2229 			break;
2230 		default:
2231 			offsets[arg_count] = (sljit_u8)stack_offset;
2232 			stack_offset += sizeof(sljit_sw);
2233 			arg_count++;
2234 			word_arg_offset += sizeof(sljit_sw);
2235 			break;
2236 		}
2237 
2238 		arg_types >>= SLJIT_DEF_SHIFT;
2239 	}
2240 
2241 	if (stack_offset > 16)
2242 		FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_offset - 16) + 0x7) & ~0x7)));
2243 
2244 	/* Process arguments in reversed direction. */
2245 	while (types) {
2246 		switch (types & SLJIT_DEF_MASK) {
2247 		case SLJIT_ARG_TYPE_F32:
2248 			arg_count--;
2249 			float_arg_count--;
2250 			stack_offset = offsets[arg_count];
2251 
2252 			if (stack_offset < 16) {
2253 				if (src_offset == stack_offset) {
2254 					FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2255 					*src = TMP_REG1;
2256 				}
2257 				FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
2258 			} else
2259 				FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2260 			break;
2261 		case SLJIT_ARG_TYPE_F64:
2262 			arg_count--;
2263 			float_arg_count--;
2264 			stack_offset = offsets[arg_count];
2265 
2266 			SLJIT_ASSERT((stack_offset & 0x7) == 0);
2267 
2268 			if (stack_offset < 16) {
2269 				if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
2270 					FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2271 					*src = TMP_REG1;
2272 				}
2273 				FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
2274 			} else
2275 				FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
2276 			break;
2277 		default:
2278 			arg_count--;
2279 			word_arg_offset -= sizeof(sljit_sw);
2280 			stack_offset = offsets[arg_count];
2281 
2282 			SLJIT_ASSERT(stack_offset >= word_arg_offset);
2283 
2284 			if (stack_offset != word_arg_offset) {
2285 				if (stack_offset < 16) {
2286 					if (src_offset == stack_offset) {
2287 						FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
2288 						*src = TMP_REG1;
2289 					}
2290 					else if (src_offset == word_arg_offset) {
2291 						*src = 1 + (stack_offset >> 2);
2292 						src_offset = stack_offset;
2293 					}
2294 					FAIL_IF(push_inst(compiler, MOV | (stack_offset << 10) | (word_arg_offset >> 2)));
2295 				} else
2296 					FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (stack_offset - 16)));
2297 			}
2298 			break;
2299 		}
2300 
2301 		types >>= SLJIT_DEF_SHIFT;
2302 	}
2303 
2304 	return SLJIT_SUCCESS;
2305 }
2306 
softfloat_post_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2307 static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2308 {
2309 	sljit_s32 stack_size = 0;
2310 
2311 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
2312 		FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
2313 	if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
2314 		FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
2315 
2316 	arg_types >>= SLJIT_DEF_SHIFT;
2317 
2318 	while (arg_types) {
2319 		switch (arg_types & SLJIT_DEF_MASK) {
2320 		case SLJIT_ARG_TYPE_F32:
2321 			stack_size += sizeof(sljit_f32);
2322 			break;
2323 		case SLJIT_ARG_TYPE_F64:
2324 			if (stack_size & 0x7)
2325 				stack_size += sizeof(sljit_sw);
2326 			stack_size += sizeof(sljit_f64);
2327 			break;
2328 		default:
2329 			stack_size += sizeof(sljit_sw);
2330 			break;
2331 		}
2332 
2333 		arg_types >>= SLJIT_DEF_SHIFT;
2334 	}
2335 
2336 	if (stack_size <= 16)
2337 		return SLJIT_SUCCESS;
2338 
2339 	return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_size - 16) + 0x7) & ~0x7));
2340 }
2341 
2342 #else /* !__SOFTFP__ */
2343 
hardfloat_call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types)2344 static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
2345 {
2346 	sljit_u32 remap = 0;
2347 	sljit_u32 offset = 0;
2348 	sljit_u32 new_offset, mask;
2349 
2350 	/* Remove return value. */
2351 	arg_types >>= SLJIT_DEF_SHIFT;
2352 
2353 	while (arg_types) {
2354 		if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
2355 			new_offset = 0;
2356 			mask = 1;
2357 
2358 			while (remap & mask) {
2359 				new_offset++;
2360 				mask <<= 1;
2361 			}
2362 			remap |= mask;
2363 
2364 			if (offset != new_offset)
2365 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
2366 					0, (new_offset >> 1) + 1, (offset >> 1) + 1, 0) | ((new_offset & 0x1) ? 0x400000 : 0)));
2367 
2368 			offset += 2;
2369 		}
2370 		else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
2371 			new_offset = 0;
2372 			mask = 3;
2373 
2374 			while (remap & mask) {
2375 				new_offset += 2;
2376 				mask <<= 2;
2377 			}
2378 			remap |= mask;
2379 
2380 			if (offset != new_offset)
2381 				FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, SLJIT_F32_OP, (new_offset >> 1) + 1, (offset >> 1) + 1, 0)));
2382 
2383 			offset += 2;
2384 		}
2385 		arg_types >>= SLJIT_DEF_SHIFT;
2386 	}
2387 
2388 	return SLJIT_SUCCESS;
2389 }
2390 
2391 #endif /* __SOFTFP__ */
2392 
2393 #undef EMIT_FPU_OPERATION
2394 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)2395 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
2396 	sljit_s32 arg_types)
2397 {
2398 #ifdef __SOFTFP__
2399 	struct sljit_jump *jump;
2400 #endif
2401 
2402 	CHECK_ERROR_PTR();
2403 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
2404 
2405 #ifdef __SOFTFP__
2406 	PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
2407 
2408 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2409 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2410 	compiler->skip_checks = 1;
2411 #endif
2412 
2413 	jump = sljit_emit_jump(compiler, type);
2414 	PTR_FAIL_IF(jump == NULL);
2415 
2416 	PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
2417 	return jump;
2418 #else /* !__SOFTFP__ */
2419 	PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2420 
2421 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2422 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2423 	compiler->skip_checks = 1;
2424 #endif
2425 
2426 	return sljit_emit_jump(compiler, type);
2427 #endif /* __SOFTFP__ */
2428 }
2429 
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)2430 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
2431 {
2432 	struct sljit_jump *jump;
2433 
2434 	CHECK_ERROR();
2435 	CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
2436 	ADJUST_LOCAL_OFFSET(src, srcw);
2437 
2438 	SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
2439 
2440 	if (!(src & SLJIT_IMM)) {
2441 		if (FAST_IS_REG(src)) {
2442 			SLJIT_ASSERT(reg_map[src] != 14);
2443 			return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
2444 		}
2445 
2446 		SLJIT_ASSERT(src & SLJIT_MEM);
2447 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2448 		return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
2449 	}
2450 
2451 	/* These jumps are converted to jump/call instructions when possible. */
2452 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
2453 	FAIL_IF(!jump);
2454 	set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
2455 	jump->u.target = srcw;
2456 
2457 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2458 	if (type >= SLJIT_FAST_CALL)
2459 		FAIL_IF(prepare_blx(compiler));
2460 	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));
2461 	if (type >= SLJIT_FAST_CALL)
2462 		FAIL_IF(emit_blx(compiler));
2463 #else
2464 	FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
2465 	FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
2466 #endif
2467 	jump->addr = compiler->size;
2468 	return SLJIT_SUCCESS;
2469 }
2470 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)2471 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
2472 	sljit_s32 arg_types,
2473 	sljit_s32 src, sljit_sw srcw)
2474 {
2475 	CHECK_ERROR();
2476 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
2477 
2478 #ifdef __SOFTFP__
2479 	if (src & SLJIT_MEM) {
2480 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
2481 		src = TMP_REG1;
2482 	}
2483 
2484 	FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
2485 
2486 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2487 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2488 	compiler->skip_checks = 1;
2489 #endif
2490 
2491 	FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
2492 
2493 	return softfloat_post_call_with_args(compiler, arg_types);
2494 #else /* !__SOFTFP__ */
2495 	FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
2496 
2497 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
2498 		|| (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
2499 	compiler->skip_checks = 1;
2500 #endif
2501 
2502 	return sljit_emit_ijump(compiler, type, src, srcw);
2503 #endif /* __SOFTFP__ */
2504 }
2505 
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)2506 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
2507 	sljit_s32 dst, sljit_sw dstw,
2508 	sljit_s32 type)
2509 {
2510 	sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
2511 	sljit_uw cc, ins;
2512 
2513 	CHECK_ERROR();
2514 	CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
2515 	ADJUST_LOCAL_OFFSET(dst, dstw);
2516 
2517 	op = GET_OPCODE(op);
2518 	cc = get_cc(type & 0xff);
2519 	dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2520 
2521 	if (op < SLJIT_ADD) {
2522 		FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
2523 		FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2524 		if (dst & SLJIT_MEM)
2525 			return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
2526 		return SLJIT_SUCCESS;
2527 	}
2528 
2529 	ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
2530 
2531 	if (dst & SLJIT_MEM)
2532 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
2533 
2534 	FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
2535 
2536 	if (op == SLJIT_AND)
2537 		FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
2538 
2539 	if (dst & SLJIT_MEM)
2540 		FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
2541 
2542 	if (flags & SLJIT_SET_Z)
2543 		return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
2544 	return SLJIT_SUCCESS;
2545 }
2546 
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)2547 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
2548 	sljit_s32 dst_reg,
2549 	sljit_s32 src, sljit_sw srcw)
2550 {
2551 	sljit_uw cc, tmp;
2552 
2553 	CHECK_ERROR();
2554 	CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
2555 
2556 	dst_reg &= ~SLJIT_I32_OP;
2557 
2558 	cc = get_cc(type & 0xff);
2559 
2560 	if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
2561 		tmp = get_imm(srcw);
2562 		if (tmp)
2563 			return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2564 
2565 		tmp = get_imm(~srcw);
2566 		if (tmp)
2567 			return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
2568 
2569 #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
2570 		tmp = (sljit_uw) srcw;
2571 		FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
2572 		if (tmp <= 0xffff)
2573 			return SLJIT_SUCCESS;
2574 		return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
2575 #else
2576 		FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2577 		src = TMP_REG1;
2578 #endif
2579 	}
2580 
2581 	return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
2582 }
2583 
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)2584 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2585 	sljit_s32 reg,
2586 	sljit_s32 mem, sljit_sw memw)
2587 {
2588 	sljit_s32 flags;
2589 	sljit_uw is_type1_transfer, inst;
2590 
2591 	CHECK_ERROR();
2592 	CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2593 
2594 	is_type1_transfer = 1;
2595 
2596 	switch (type & 0xff) {
2597 	case SLJIT_MOV:
2598 	case SLJIT_MOV_U32:
2599 	case SLJIT_MOV_S32:
2600 	case SLJIT_MOV_P:
2601 		flags = WORD_SIZE;
2602 		break;
2603 	case SLJIT_MOV_U8:
2604 		flags = BYTE_SIZE;
2605 		break;
2606 	case SLJIT_MOV_S8:
2607 		if (!(type & SLJIT_MEM_STORE))
2608 			is_type1_transfer = 0;
2609 		flags = BYTE_SIZE | SIGNED;
2610 		break;
2611 	case SLJIT_MOV_U16:
2612 		is_type1_transfer = 0;
2613 		flags = HALF_SIZE;
2614 		break;
2615 	case SLJIT_MOV_S16:
2616 		is_type1_transfer = 0;
2617 		flags = HALF_SIZE | SIGNED;
2618 		break;
2619 	default:
2620 		SLJIT_UNREACHABLE();
2621 		flags = WORD_SIZE;
2622 		break;
2623 	}
2624 
2625 	if (!(type & SLJIT_MEM_STORE))
2626 		flags |= LOAD_DATA;
2627 
2628 	SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
2629 
2630 	if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2631 		if (!is_type1_transfer && memw != 0)
2632 			return SLJIT_ERR_UNSUPPORTED;
2633 	}
2634 	else {
2635 		if (is_type1_transfer) {
2636 			if (memw > 4095 || memw < -4095)
2637 				return SLJIT_ERR_UNSUPPORTED;
2638 		}
2639 		else {
2640 			if (memw > 255 || memw < -255)
2641 				return SLJIT_ERR_UNSUPPORTED;
2642 		}
2643 	}
2644 
2645 	if (type & SLJIT_MEM_SUPP)
2646 		return SLJIT_SUCCESS;
2647 
2648 	if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
2649 		memw &= 0x3;
2650 
2651 		inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | (memw << 7));
2652 
2653 		if (is_type1_transfer)
2654 			inst |= (1 << 25);
2655 
2656 		if (type & SLJIT_MEM_PRE)
2657 			inst |= (1 << 21);
2658 		else
2659 			inst ^= (1 << 24);
2660 
2661 		return push_inst(compiler, inst);
2662 	}
2663 
2664 	inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
2665 
2666 	if (type & SLJIT_MEM_PRE)
2667 		inst |= (1 << 21);
2668 	else
2669 		inst ^= (1 << 24);
2670 
2671 	if (is_type1_transfer) {
2672 		if (memw >= 0)
2673 			inst |= (1 << 23);
2674 		else
2675 			memw = -memw;
2676 
2677 		return push_inst(compiler, inst | memw);
2678 	}
2679 
2680 	if (memw >= 0)
2681 		inst |= (1 << 23);
2682 	else
2683 		memw = -memw;
2684 
2685 	return push_inst(compiler, inst | TYPE2_TRANSFER_IMM(memw));
2686 }
2687 
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)2688 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2689 {
2690 	struct sljit_const *const_;
2691 	sljit_s32 dst_r;
2692 
2693 	CHECK_ERROR_PTR();
2694 	CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2695 	ADJUST_LOCAL_OFFSET(dst, dstw);
2696 
2697 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2698 
2699 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2700 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), init_value));
2701 	compiler->patches++;
2702 #else
2703 	PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
2704 #endif
2705 
2706 	const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2707 	PTR_FAIL_IF(!const_);
2708 	set_const(const_, compiler);
2709 
2710 	if (dst & SLJIT_MEM)
2711 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2712 	return const_;
2713 }
2714 
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2715 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2716 {
2717 	struct sljit_put_label *put_label;
2718 	sljit_s32 dst_r;
2719 
2720 	CHECK_ERROR_PTR();
2721 	CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2722 	ADJUST_LOCAL_OFFSET(dst, dstw);
2723 
2724 	dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
2725 
2726 #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
2727 	PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
2728 	compiler->patches++;
2729 #else
2730 	PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
2731 #endif
2732 
2733 	put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2734 	PTR_FAIL_IF(!put_label);
2735 	set_put_label(put_label, compiler, 0);
2736 
2737 	if (dst & SLJIT_MEM)
2738 		PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
2739 	return put_label;
2740 }
2741 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2742 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2743 {
2744 	inline_set_jump_addr(addr, executable_offset, new_target, 1);
2745 }
2746 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2747 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2748 {
2749 	inline_set_const(addr, executable_offset, new_constant, 1);
2750 }
2751