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