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