/[pcre]/code/trunk/sljit/sljitNativeARM_32.c
ViewVC logotype

Contents of /code/trunk/sljit/sljitNativeARM_32.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5