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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5