chiark / gitweb /
Changelog entry for 2:8.35-7
[pcre3.git] / sljit / sljitNativeARM_64.c
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         return "ARM-64" SLJIT_CPUINFO;
30 }
31
32 /* Length of an instruction word */
33 typedef sljit_ui sljit_ins;
34
35 #define TMP_ZERO        0
36
37 #define TMP_REG1        (SLJIT_NO_REGISTERS + 1)
38 #define TMP_REG2        (SLJIT_NO_REGISTERS + 2)
39 #define TMP_REG3        (SLJIT_NO_REGISTERS + 3)
40 #define TMP_REG4        (SLJIT_NO_REGISTERS + 4)
41 #define TMP_LR          (SLJIT_NO_REGISTERS + 5)
42 #define TMP_SP          (SLJIT_NO_REGISTERS + 6)
43
44 #define TMP_FREG1       (0)
45 #define TMP_FREG2       (SLJIT_FLOAT_REG6 + 1)
46
47 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 7] = {
48   31, 0, 1, 2, 3, 4, 19, 20, 21, 22, 23, 29, 9, 10, 11, 12, 30, 31
49 };
50
51 #define W_OP (1 << 31)
52 #define RD(rd) (reg_map[rd])
53 #define RT(rt) (reg_map[rt])
54 #define RN(rn) (reg_map[rn] << 5)
55 #define RT2(rt2) (reg_map[rt2] << 10)
56 #define RM(rm) (reg_map[rm] << 16)
57 #define VD(vd) (vd)
58 #define VT(vt) (vt)
59 #define VN(vn) ((vn) << 5)
60 #define VM(vm) ((vm) << 16)
61
62 /* --------------------------------------------------------------------- */
63 /*  Instrucion forms                                                     */
64 /* --------------------------------------------------------------------- */
65
66 #define ADC 0x9a000000
67 #define ADD 0x8b000000
68 #define ADDI 0x91000000
69 #define AND 0x8a000000
70 #define ANDI 0x92000000
71 #define ASRV 0x9ac02800
72 #define B 0x14000000
73 #define B_CC 0x54000000
74 #define BL 0x94000000
75 #define BLR 0xd63f0000
76 #define BR 0xd61f0000
77 #define BRK 0xd4200000
78 #define CBZ 0xb4000000
79 #define CLZ 0xdac01000
80 #define CSINC 0x9a800400
81 #define EOR 0xca000000
82 #define EORI 0xd2000000
83 #define FABS 0x1e60c000
84 #define FADD 0x1e602800
85 #define FCMP 0x1e602000
86 #define FDIV 0x1e601800
87 #define FMOV 0x1e604000
88 #define FMUL 0x1e600800
89 #define FNEG 0x1e614000
90 #define FSUB 0x1e603800
91 #define LDRI 0xf9400000
92 #define LDP 0xa9400000
93 #define LDP_PST 0xa8c00000
94 #define LSLV 0x9ac02000
95 #define LSRV 0x9ac02400
96 #define MADD 0x9b000000
97 #define MOVK 0xf2800000
98 #define MOVN 0x92800000
99 #define MOVZ 0xd2800000
100 #define NOP 0xd503201f
101 #define ORN 0xaa200000
102 #define ORR 0xaa000000
103 #define ORRI 0xb2000000
104 #define RET 0xd65f0000
105 #define SBC 0xda000000
106 #define SBFM 0x93000000
107 #define SDIV 0x9ac00c00
108 #define SMADDL 0x9b200000
109 #define SMULH 0x9b403c00
110 #define STP 0xa9000000
111 #define STP_PRE 0xa9800000
112 #define STRI 0xf9000000
113 #define STR_FI 0x3d000000
114 #define STR_FR 0x3c206800
115 #define STUR_FI 0x3c000000
116 #define SUB 0xcb000000
117 #define SUBI 0xd1000000
118 #define SUBS 0xeb000000
119 #define UBFM 0xd3000000
120 #define UDIV 0x9ac00800
121 #define UMULH 0x9bc03c00
122
123 /* dest_reg is the absolute name of the register
124    Useful for reordering instructions in the delay slot. */
125 static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins)
126 {
127         sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
128         FAIL_IF(!ptr);
129         *ptr = ins;
130         compiler->size++;
131         return SLJIT_SUCCESS;
132 }
133
134 static SLJIT_INLINE sljit_si emit_imm64_const(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
135 {
136         FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
137         FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21)));
138         FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21)));
139         return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21));
140 }
141
142 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm)
143 {
144         sljit_si dst = inst[0] & 0x1f;
145         SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
146         inst[0] = MOVZ | dst | ((new_imm & 0xffff) << 5);
147         inst[1] = MOVK | dst | (((new_imm >> 16) & 0xffff) << 5) | (1 << 21);
148         inst[2] = MOVK | dst | (((new_imm >> 32) & 0xffff) << 5) | (2 << 21);
149         inst[3] = MOVK | dst | ((new_imm >> 48) << 5) | (3 << 21);
150 }
151
152 static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
153 {
154         sljit_sw diff;
155         sljit_uw target_addr;
156
157         if (jump->flags & SLJIT_REWRITABLE_JUMP) {
158                 jump->flags |= PATCH_ABS64;
159                 return 0;
160         }
161
162         if (jump->flags & JUMP_ADDR)
163                 target_addr = jump->u.target;
164         else {
165                 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
166                 target_addr = (sljit_uw)(code + jump->u.label->size);
167         }
168         diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4);
169
170         if (jump->flags & IS_COND) {
171                 diff += sizeof(sljit_ins);
172                 if (diff <= 0xfffff && diff >= -0x100000) {
173                         code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
174                         jump->addr -= sizeof(sljit_ins);
175                         jump->flags |= PATCH_COND;
176                         return 5;
177                 }
178                 diff -= sizeof(sljit_ins);
179         }
180
181         if (diff <= 0x7ffffff && diff >= -0x8000000) {
182                 jump->flags |= PATCH_B;
183                 return 4;
184         }
185
186         if (target_addr <= 0xffffffffl) {
187                 if (jump->flags & IS_COND)
188                         code_ptr[-5] -= (2 << 5);
189                 code_ptr[-2] = code_ptr[0];
190                 return 2;
191         }
192         if (target_addr <= 0xffffffffffffl) {
193                 if (jump->flags & IS_COND)
194                         code_ptr[-5] -= (1 << 5);
195                 jump->flags |= PATCH_ABS48;
196                 code_ptr[-1] = code_ptr[0];
197                 return 1;
198         }
199
200         jump->flags |= PATCH_ABS64;
201         return 0;
202 }
203
204 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
205 {
206         struct sljit_memory_fragment *buf;
207         sljit_ins *code;
208         sljit_ins *code_ptr;
209         sljit_ins *buf_ptr;
210         sljit_ins *buf_end;
211         sljit_uw word_count;
212         sljit_uw addr;
213         sljit_si dst;
214
215         struct sljit_label *label;
216         struct sljit_jump *jump;
217         struct sljit_const *const_;
218
219         CHECK_ERROR_PTR();
220         check_sljit_generate_code(compiler);
221         reverse_buf(compiler);
222
223         code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
224         PTR_FAIL_WITH_EXEC_IF(code);
225         buf = compiler->buf;
226
227         code_ptr = code;
228         word_count = 0;
229         label = compiler->labels;
230         jump = compiler->jumps;
231         const_ = compiler->consts;
232
233         do {
234                 buf_ptr = (sljit_ins*)buf->memory;
235                 buf_end = buf_ptr + (buf->used_size >> 2);
236                 do {
237                         *code_ptr = *buf_ptr++;
238                         /* These structures are ordered by their address. */
239                         SLJIT_ASSERT(!label || label->size >= word_count);
240                         SLJIT_ASSERT(!jump || jump->addr >= word_count);
241                         SLJIT_ASSERT(!const_ || const_->addr >= word_count);
242                         if (label && label->size == word_count) {
243                                 label->addr = (sljit_uw)code_ptr;
244                                 label->size = code_ptr - code;
245                                 label = label->next;
246                         }
247                         if (jump && jump->addr == word_count) {
248                                         jump->addr = (sljit_uw)(code_ptr - 4);
249                                         code_ptr -= detect_jump_type(jump, code_ptr, code);
250                                         jump = jump->next;
251                         }
252                         if (const_ && const_->addr == word_count) {
253                                 const_->addr = (sljit_uw)code_ptr;
254                                 const_ = const_->next;
255                         }
256                         code_ptr ++;
257                         word_count ++;
258                 } while (buf_ptr < buf_end);
259
260                 buf = buf->next;
261         } while (buf);
262
263         if (label && label->size == word_count) {
264                 label->addr = (sljit_uw)code_ptr;
265                 label->size = code_ptr - code;
266                 label = label->next;
267         }
268
269         SLJIT_ASSERT(!label);
270         SLJIT_ASSERT(!jump);
271         SLJIT_ASSERT(!const_);
272         SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
273
274         jump = compiler->jumps;
275         while (jump) {
276                 do {
277                         addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
278                         buf_ptr = (sljit_ins*)jump->addr;
279                         if (jump->flags & PATCH_B) {
280                                 addr = (sljit_sw)(addr - jump->addr) >> 2;
281                                 SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000);
282                                 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff);
283                                 if (jump->flags & IS_COND)
284                                         buf_ptr[-1] -= (4 << 5);
285                                 break;
286                         }
287                         if (jump->flags & PATCH_COND) {
288                                 addr = (sljit_sw)(addr - jump->addr) >> 2;
289                                 SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000);
290                                 buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5);
291                                 break;
292                         }
293
294                         SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
295                         SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
296
297                         dst = buf_ptr[0] & 0x1f;
298                         buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5);
299                         buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21);
300                         if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
301                                 buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21);
302                         if (jump->flags & PATCH_ABS64)
303                                 buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21);
304                 } while (0);
305                 jump = jump->next;
306         }
307
308         compiler->error = SLJIT_ERR_COMPILED;
309         compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
310         SLJIT_CACHE_FLUSH(code, code_ptr);
311         return code;
312 }
313
314 /* --------------------------------------------------------------------- */
315 /*  Core code generator functions.                                       */
316 /* --------------------------------------------------------------------- */
317
318 #define COUNT_TRAILING_ZERO(value, result) \
319         result = 0; \
320         if (!(value & 0xffffffff)) { \
321                 result += 32; \
322                 value >>= 32; \
323         } \
324         if (!(value & 0xffff)) { \
325                 result += 16; \
326                 value >>= 16; \
327         } \
328         if (!(value & 0xff)) { \
329                 result += 8; \
330                 value >>= 8; \
331         } \
332         if (!(value & 0xf)) { \
333                 result += 4; \
334                 value >>= 4; \
335         } \
336         if (!(value & 0x3)) { \
337                 result += 2; \
338                 value >>= 2; \
339         } \
340         if (!(value & 0x1)) { \
341                 result += 1; \
342                 value >>= 1; \
343         }
344
345 #define LOGICAL_IMM_CHECK 0x100
346
347 static sljit_ins logical_imm(sljit_sw imm, sljit_si len)
348 {
349         sljit_si negated, ones, right;
350         sljit_uw mask, uimm;
351         sljit_ins ins;
352
353         if (len & LOGICAL_IMM_CHECK) {
354                 len &= ~LOGICAL_IMM_CHECK;
355                 if (len == 32 && (imm == 0 || imm == -1))
356                         return 0;
357                 if (len == 16 && ((sljit_si)imm == 0 || (sljit_si)imm == -1))
358                         return 0;
359         }
360
361         SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
362                 || (len == 16 && (sljit_si)imm != 0 && (sljit_si)imm != -1));
363         uimm = (sljit_uw)imm;
364         while (1) {
365                 if (len <= 0) {
366                         SLJIT_ASSERT_STOP();
367                         return 0;
368                 }
369                 mask = ((sljit_uw)1 << len) - 1;
370                 if ((uimm & mask) != ((uimm >> len) & mask))
371                         break;
372                 len >>= 1;
373         }
374
375         len <<= 1;
376
377         negated = 0;
378         if (uimm & 0x1) {
379                 negated = 1;
380                 uimm = ~uimm;
381         }
382
383         if (len < 64)
384                 uimm &= ((sljit_uw)1 << len) - 1;
385
386         /* Unsigned right shift. */
387         COUNT_TRAILING_ZERO(uimm, right);
388
389         /* Signed shift. We also know that the highest bit is set. */
390         imm = (sljit_sw)~uimm;
391         SLJIT_ASSERT(imm < 0);
392
393         COUNT_TRAILING_ZERO(imm, ones);
394
395         if (~imm)
396                 return 0;
397
398         if (len == 64)
399                 ins = 1 << 22;
400         else
401                 ins = (0x3f - ((len << 1) - 1)) << 10;
402
403         if (negated)
404                 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
405
406         return ins | ((ones - 1) << 10) | ((len - right) << 16);
407 }
408
409 #undef COUNT_TRAILING_ZERO
410
411 static sljit_si load_immediate(struct sljit_compiler *compiler, sljit_si dst, sljit_sw simm)
412 {
413         sljit_uw imm = (sljit_uw)simm;
414         sljit_si i, zeros, ones, first;
415         sljit_ins bitmask;
416
417         if (imm <= 0xffff)
418                 return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
419
420         if (simm >= -0x10000 && simm < 0)
421                 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
422
423         if (imm <= 0xffffffffl) {
424                 if ((imm & 0xffff0000l) == 0xffff0000)
425                         return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5));
426                 if ((imm & 0xffff) == 0xffff)
427                         return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
428                 bitmask = logical_imm(simm, 16);
429                 if (bitmask != 0)
430                         return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
431         }
432         else {
433                 bitmask = logical_imm(simm, 32);
434                 if (bitmask != 0)
435                         return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
436         }
437
438         if (imm <= 0xffffffffl) {
439                 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
440                 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
441         }
442
443         if (simm >= -0x100000000l && simm < 0) {
444                 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)));
445                 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
446         }
447
448         /* A large amount of number can be constructed from ORR and MOVx,
449         but computing them is costly. We don't  */
450
451         zeros = 0;
452         ones = 0;
453         for (i = 4; i > 0; i--) {
454                 if ((simm & 0xffff) == 0)
455                         zeros++;
456                 if ((simm & 0xffff) == 0xffff)
457                         ones++;
458                 simm >>= 16;
459         }
460
461         simm = (sljit_sw)imm;
462         first = 1;
463         if (ones > zeros) {
464                 simm = ~simm;
465                 for (i = 0; i < 4; i++) {
466                         if (!(simm & 0xffff)) {
467                                 simm >>= 16;
468                                 continue;
469                         }
470                         if (first) {
471                                 first = 0;
472                                 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
473                         }
474                         else
475                                 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
476                         simm >>= 16;
477                 }
478                 return SLJIT_SUCCESS;
479         }
480
481         for (i = 0; i < 4; i++) {
482                 if (!(simm & 0xffff)) {
483                         simm >>= 16;
484                         continue;
485                 }
486                 if (first) {
487                         first = 0;
488                         FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
489                 }
490                 else
491                         FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
492                 simm >>= 16;
493         }
494         return SLJIT_SUCCESS;
495 }
496
497 #define ARG1_IMM        0x0010000
498 #define ARG2_IMM        0x0020000
499 #define INT_OP          0x0040000
500 #define SET_FLAGS       0x0080000
501 #define UNUSED_RETURN   0x0100000
502 #define SLOW_DEST       0x0200000
503 #define SLOW_SRC1       0x0400000
504 #define SLOW_SRC2       0x0800000
505
506 #define CHECK_FLAGS(flag_bits) \
507         if (flags & SET_FLAGS) { \
508                 inv_bits |= flag_bits; \
509                 if (flags & UNUSED_RETURN) \
510                         dst = TMP_ZERO; \
511         }
512
513 static sljit_si emit_op_imm(struct sljit_compiler *compiler, sljit_si flags, sljit_si dst, sljit_sw arg1, sljit_sw arg2)
514 {
515         /* dst must be register, TMP_REG1
516            arg1 must be register, TMP_REG1, imm
517            arg2 must be register, TMP_REG2, imm */
518         sljit_ins inv_bits = (flags & INT_OP) ? (1 << 31) : 0;
519         sljit_ins inst_bits;
520         sljit_si op = (flags & 0xffff);
521         sljit_si reg;
522         sljit_sw imm, nimm;
523
524         if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
525                 /* Both are immediates. */
526                 flags &= ~ARG1_IMM;
527                 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
528                         arg1 = TMP_ZERO;
529                 else {
530                         FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
531                         arg1 = TMP_REG1;
532                 }
533         }
534
535         if (flags & (ARG1_IMM | ARG2_IMM)) {
536                 reg = (flags & ARG2_IMM) ? arg1 : arg2;
537                 imm = (flags & ARG2_IMM) ? arg2 : arg1;
538
539                 switch (op) {
540                 case SLJIT_MUL:
541                 case SLJIT_NEG:
542                 case SLJIT_CLZ:
543                 case SLJIT_ADDC:
544                 case SLJIT_SUBC:
545                         /* No form with immediate operand (except imm 0, which
546                         is represented by a ZERO register). */
547                         break;
548                 case SLJIT_MOV:
549                         SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
550                         return load_immediate(compiler, dst, imm);
551                 case SLJIT_NOT:
552                         SLJIT_ASSERT(flags & ARG2_IMM);
553                         FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
554                         goto set_flags;
555                 case SLJIT_SUB:
556                         if (flags & ARG1_IMM)
557                                 break;
558                         imm = -imm;
559                         /* Fall through. */
560                 case SLJIT_ADD:
561                         if (imm == 0) {
562                                 CHECK_FLAGS(1 << 29);
563                                 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
564                         }
565                         if (imm > 0 && imm <= 0xfff) {
566                                 CHECK_FLAGS(1 << 29);
567                                 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10));
568                         }
569                         nimm = -imm;
570                         if (nimm > 0 && nimm <= 0xfff) {
571                                 CHECK_FLAGS(1 << 29);
572                                 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10));
573                         }
574                         if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
575                                 CHECK_FLAGS(1 << 29);
576                                 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22));
577                         }
578                         if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
579                                 CHECK_FLAGS(1 << 29);
580                                 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22));
581                         }
582                         if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
583                                 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)));
584                                 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10));
585                         }
586                         if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
587                                 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)));
588                                 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10));
589                         }
590                         break;
591                 case SLJIT_AND:
592                         inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
593                         if (!inst_bits)
594                                 break;
595                         CHECK_FLAGS(3 << 29);
596                         return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
597                 case SLJIT_OR:
598                 case SLJIT_XOR:
599                         inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
600                         if (!inst_bits)
601                                 break;
602                         if (op == SLJIT_OR)
603                                 inst_bits |= ORRI;
604                         else
605                                 inst_bits |= EORI;
606                         FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
607                         goto set_flags;
608                 case SLJIT_SHL:
609                         if (flags & ARG1_IMM)
610                                 break;
611                         if (flags & INT_OP) {
612                                 imm &= 0x1f;
613                                 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
614                         }
615                         else {
616                                 imm &= 0x3f;
617                                 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
618                         }
619                         goto set_flags;
620                 case SLJIT_LSHR:
621                 case SLJIT_ASHR:
622                         if (flags & ARG1_IMM)
623                                 break;
624                         if (op == SLJIT_ASHR)
625                                 inv_bits |= 1 << 30;
626                         if (flags & INT_OP) {
627                                 imm &= 0x1f;
628                                 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10)));
629                         }
630                         else {
631                                 imm &= 0x3f;
632                                 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
633                         }
634                         goto set_flags;
635                 default:
636                         SLJIT_ASSERT_STOP();
637                         break;
638                 }
639
640                 if (flags & ARG2_IMM) {
641                         if (arg2 == 0)
642                                 arg2 = TMP_ZERO;
643                         else {
644                                 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
645                                 arg2 = TMP_REG2;
646                         }
647                 }
648                 else {
649                         if (arg1 == 0)
650                                 arg1 = TMP_ZERO;
651                         else {
652                                 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
653                                 arg1 = TMP_REG1;
654                         }
655                 }
656         }
657
658         /* Both arguments are registers. */
659         switch (op) {
660         case SLJIT_MOV:
661         case SLJIT_MOV_P:
662         case SLJIT_MOVU:
663         case SLJIT_MOVU_P:
664                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
665                 if (dst == arg2)
666                         return SLJIT_SUCCESS;
667                 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
668         case SLJIT_MOV_UB:
669         case SLJIT_MOVU_UB:
670                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
671                 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (7 << 10));
672         case SLJIT_MOV_SB:
673         case SLJIT_MOVU_SB:
674                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
675                 if (!(flags & INT_OP))
676                         inv_bits |= 1 << 22;
677                 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
678         case SLJIT_MOV_UH:
679         case SLJIT_MOVU_UH:
680                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
681                 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (15 << 10));
682         case SLJIT_MOV_SH:
683         case SLJIT_MOVU_SH:
684                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
685                 if (!(flags & INT_OP))
686                         inv_bits |= 1 << 22;
687                 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
688         case SLJIT_MOV_UI:
689         case SLJIT_MOVU_UI:
690                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
691                 if ((flags & INT_OP) && dst == arg2)
692                         return SLJIT_SUCCESS;
693                 return push_inst(compiler, (ORR ^ (1 << 31)) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
694         case SLJIT_MOV_SI:
695         case SLJIT_MOVU_SI:
696                 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
697                 if ((flags & INT_OP) && dst == arg2)
698                         return SLJIT_SUCCESS;
699                 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
700         case SLJIT_NOT:
701                 SLJIT_ASSERT(arg1 == TMP_REG1);
702                 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
703                 goto set_flags;
704         case SLJIT_NEG:
705                 SLJIT_ASSERT(arg1 == TMP_REG1);
706                 if (flags & SET_FLAGS)
707                         inv_bits |= 1 << 29;
708                 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
709         case SLJIT_CLZ:
710                 SLJIT_ASSERT(arg1 == TMP_REG1);
711                 FAIL_IF(push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2)));
712                 goto set_flags;
713         case SLJIT_ADD:
714                 CHECK_FLAGS(1 << 29);
715                 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
716         case SLJIT_ADDC:
717                 CHECK_FLAGS(1 << 29);
718                 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
719         case SLJIT_SUB:
720                 CHECK_FLAGS(1 << 29);
721                 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
722         case SLJIT_SUBC:
723                 CHECK_FLAGS(1 << 29);
724                 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
725         case SLJIT_MUL:
726                 if (!(flags & SET_FLAGS))
727                         return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
728                 if (flags & INT_OP) {
729                         FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
730                         FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG4) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
731                         return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_REG4) | RM(dst) | (2 << 22) | (63 << 10));
732                 }
733                 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_REG4) | RN(arg1) | RM(arg2)));
734                 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
735                 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_REG4) | RM(dst) | (2 << 22) | (63 << 10));
736         case SLJIT_AND:
737                 CHECK_FLAGS(3 << 29);
738                 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
739         case SLJIT_OR:
740                 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
741                 goto set_flags;
742         case SLJIT_XOR:
743                 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
744                 goto set_flags;
745         case SLJIT_SHL:
746                 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
747                 goto set_flags;
748         case SLJIT_LSHR:
749                 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
750                 goto set_flags;
751         case SLJIT_ASHR:
752                 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
753                 goto set_flags;
754         }
755
756         SLJIT_ASSERT_STOP();
757         return SLJIT_SUCCESS;
758
759 set_flags:
760         if (flags & SET_FLAGS)
761                 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
762         return SLJIT_SUCCESS;
763 }
764
765 #define STORE           0x01
766 #define SIGNED          0x02
767
768 #define UPDATE          0x04
769 #define ARG_TEST        0x08
770
771 #define BYTE_SIZE       0x000
772 #define HALF_SIZE       0x100
773 #define INT_SIZE        0x200
774 #define WORD_SIZE       0x300
775
776 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
777
778 static SLJIT_CONST sljit_ins sljit_mem_imm[4] = {
779 /* u l */ 0x39400000 /* ldrb [reg,imm] */,
780 /* u s */ 0x39000000 /* strb [reg,imm] */,
781 /* s l */ 0x39800000 /* ldrsb [reg,imm] */,
782 /* s s */ 0x39000000 /* strb [reg,imm] */,
783 };
784
785 static SLJIT_CONST sljit_ins sljit_mem_simm[4] = {
786 /* u l */ 0x38400000 /* ldurb [reg,imm] */,
787 /* u s */ 0x38000000 /* sturb [reg,imm] */,
788 /* s l */ 0x38800000 /* ldursb [reg,imm] */,
789 /* s s */ 0x38000000 /* sturb [reg,imm] */,
790 };
791
792 static SLJIT_CONST sljit_ins sljit_mem_pre_simm[4] = {
793 /* u l */ 0x38400c00 /* ldrb [reg,imm]! */,
794 /* u s */ 0x38000c00 /* strb [reg,imm]! */,
795 /* s l */ 0x38800c00 /* ldrsb [reg,imm]! */,
796 /* s s */ 0x38000c00 /* strb [reg,imm]! */,
797 };
798
799 static SLJIT_CONST sljit_ins sljit_mem_reg[4] = {
800 /* u l */ 0x38606800 /* ldrb [reg,reg] */,
801 /* u s */ 0x38206800 /* strb [reg,reg] */,
802 /* s l */ 0x38a06800 /* ldrsb [reg,reg] */,
803 /* s s */ 0x38206800 /* strb [reg,reg] */,
804 };
805
806 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
807 static sljit_si emit_set_delta(struct sljit_compiler *compiler, sljit_si dst, sljit_si reg, sljit_sw value)
808 {
809         if (value >= 0) {
810                 if (value <= 0xfff)
811                         return push_inst(compiler, ADDI | RD(dst) | RN(reg) | (value << 10));
812                 if (value <= 0xffffff && !(value & 0xfff))
813                         return push_inst(compiler, ADDI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
814         }
815         else {
816                 value = -value;
817                 if (value <= 0xfff)
818                         return push_inst(compiler, SUBI | RD(dst) | RN(reg) | (value << 10));
819                 if (value <= 0xffffff && !(value & 0xfff))
820                         return push_inst(compiler, SUBI | (1 << 22) | RD(dst) | RN(reg) | (value >> 2));
821         }
822         return SLJIT_ERR_UNSUPPORTED;
823 }
824
825 /* Can perform an operation using at most 1 instruction. */
826 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
827 {
828         sljit_ui shift = MEM_SIZE_SHIFT(flags);
829
830         SLJIT_ASSERT(arg & SLJIT_MEM);
831
832         if (SLJIT_UNLIKELY(flags & UPDATE)) {
833                 if ((arg & REG_MASK) && !(arg & OFFS_REG_MASK) && argw <= 255 && argw >= -256) {
834                         if (SLJIT_UNLIKELY(flags & ARG_TEST))
835                                 return 1;
836
837                         arg &= REG_MASK;
838                         argw &= 0x1ff;
839                         FAIL_IF(push_inst(compiler, sljit_mem_pre_simm[flags & 0x3]
840                                 | (shift << 30) | RT(reg) | RN(arg) | (argw << 12)));
841                         return -1;
842                 }
843                 return 0;
844         }
845
846         if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
847                 argw &= 0x3;
848                 if (argw && argw != shift)
849                         return 0;
850
851                 if (SLJIT_UNLIKELY(flags & ARG_TEST))
852                         return 1;
853
854                 FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg)
855                         | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0)));
856                 return -1;
857         }
858
859         arg &= REG_MASK;
860         if (argw >= 0 && (argw >> shift) <= 0xfff && (argw & ((1 << shift) - 1)) == 0) {
861                 if (SLJIT_UNLIKELY(flags & ARG_TEST))
862                         return 1;
863
864                 FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
865                         | RT(reg) | RN(arg) | (argw << (10 - shift))));
866                 return -1;
867         }
868
869         if (argw > 255 || argw < -256)
870                 return 0;
871
872         if (SLJIT_UNLIKELY(flags & ARG_TEST))
873                 return 1;
874
875         FAIL_IF(push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
876                 | RT(reg) | RN(arg) | ((argw & 0x1ff) << 12)));
877         return -1;
878 }
879
880 /* see getput_arg below.
881    Note: can_cache is called only for binary operators. Those
882    operators always uses word arguments without write back. */
883 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
884 {
885         sljit_sw diff;
886         if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM))
887                 return 0;
888
889         if (!(arg & REG_MASK)) {
890                 diff = argw - next_argw;
891                 if (diff <= 0xfff && diff >= -0xfff)
892                         return 1;
893                 return 0;
894         }
895
896         if (argw == next_argw)
897                 return 1;
898
899         diff = argw - next_argw;
900         if (arg == next_arg && diff <= 0xfff && diff >= -0xfff)
901                 return 1;
902
903         return 0;
904 }
905
906 /* Emit the necessary instructions. See can_cache above. */
907 static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg,
908         sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
909 {
910         sljit_ui shift = MEM_SIZE_SHIFT(flags);
911         sljit_si tmp_r, other_r;
912         sljit_sw diff;
913
914         SLJIT_ASSERT(arg & SLJIT_MEM);
915         if (!(next_arg & SLJIT_MEM)) {
916                 next_arg = 0;
917                 next_argw = 0;
918         }
919
920         tmp_r = (flags & STORE) ? TMP_REG3 : reg;
921
922         if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) {
923                 /* Update only applies if a base register exists. */
924                 other_r = OFFS_REG(arg);
925                 if (!other_r) {
926                         other_r = arg & REG_MASK;
927                         if (other_r != reg && argw >= 0 && argw <= 0xffffff) {
928                                 if ((argw & 0xfff) != 0)
929                                         FAIL_IF(push_inst(compiler, ADDI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
930                                 if (argw >> 12)
931                                         FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
932                                 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
933                         }
934                         else if (other_r != reg && argw < 0 && argw >= -0xffffff) {
935                                 argw = -argw;
936                                 if ((argw & 0xfff) != 0)
937                                         FAIL_IF(push_inst(compiler, SUBI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
938                                 if (argw >> 12)
939                                         FAIL_IF(push_inst(compiler, SUBI | (1 << 22) | RD(other_r) | RN(other_r) | ((argw >> 12) << 10)));
940                                 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(other_r));
941                         }
942
943                         if (compiler->cache_arg == SLJIT_MEM) {
944                                 if (argw == compiler->cache_argw) {
945                                         other_r = TMP_REG3;
946                                         argw = 0;
947                                 }
948                                 else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
949                                         FAIL_IF(compiler->error);
950                                         compiler->cache_argw = argw;
951                                         other_r = TMP_REG3;
952                                         argw = 0;
953                                 }
954                         }
955
956                         if (argw) {
957                                 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
958                                 compiler->cache_arg = SLJIT_MEM;
959                                 compiler->cache_argw = argw;
960                                 other_r = TMP_REG3;
961                                 argw = 0;
962                         }
963                 }
964
965                 /* No caching here. */
966                 arg &= REG_MASK;
967                 argw &= 0x3;
968                 if (!argw || argw == shift) {
969                         FAIL_IF(push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(other_r) | (argw ? (1 << 12) : 0)));
970                         return push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r) | (argw << 10));
971                 }
972                 if (arg != reg) {
973                         FAIL_IF(push_inst(compiler, ADD | RD(arg) | RN(arg) | RM(other_r) | (argw << 10)));
974                         return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg));
975                 }
976                 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG4) | RN(arg) | RM(other_r) | (argw << 10)));
977                 FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(TMP_REG4)));
978                 return push_inst(compiler, ORR | RD(arg) | RN(TMP_ZERO) | RM(TMP_REG4));
979         }
980
981         if (arg & OFFS_REG_MASK) {
982                 other_r = OFFS_REG(arg);
983                 arg &= REG_MASK;
984                 FAIL_IF(push_inst(compiler, ADD | RD(tmp_r) | RN(arg) | RM(other_r) | ((argw & 0x3) << 10)));
985                 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(tmp_r));
986         }
987
988         if (compiler->cache_arg == arg) {
989                 diff = argw - compiler->cache_argw;
990                 if (diff <= 255 && diff >= -256)
991                         return push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
992                                 | RT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12));
993                 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, diff) != SLJIT_ERR_UNSUPPORTED) {
994                         FAIL_IF(compiler->error);
995                         return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg));
996                 }
997         }
998
999         if (argw >= 0 && argw <= 0xffffff && (argw & ((1 << shift) - 1)) == 0) {
1000                 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_r) | RN(arg & REG_MASK) | ((argw >> 12) << 10)));
1001                 return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
1002                         | RT(reg) | RN(tmp_r) | ((argw & 0xfff) << (10 - shift)));
1003         }
1004
1005         diff = argw - next_argw;
1006         next_arg = (arg & REG_MASK) && (arg == next_arg) && diff <= 0xfff && diff >= -0xfff && diff != 0;
1007         arg &= REG_MASK;
1008
1009         if (arg && compiler->cache_arg == SLJIT_MEM) {
1010                 if (compiler->cache_argw == argw)
1011                         return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1012                 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1013                         FAIL_IF(compiler->error);
1014                         compiler->cache_argw = argw;
1015                         return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1016                 }
1017         }
1018
1019         compiler->cache_argw = argw;
1020         if (next_arg && emit_set_delta(compiler, TMP_REG3, arg, argw) != SLJIT_ERR_UNSUPPORTED) {
1021                 FAIL_IF(compiler->error);
1022                 compiler->cache_arg = SLJIT_MEM | arg;
1023                 arg = 0;
1024         }
1025         else {
1026                 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1027                 compiler->cache_arg = SLJIT_MEM;
1028
1029                 if (next_arg) {
1030                         FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG3) | RN(TMP_REG3) | RM(arg)));
1031                         compiler->cache_arg = SLJIT_MEM | arg;
1032                         arg = 0;
1033                 }
1034         }
1035
1036         if (arg)
1037                 return push_inst(compiler, sljit_mem_reg[flags & 0x3] | (shift << 30) | RT(reg) | RN(arg) | RM(TMP_REG3));
1038         return push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30) | RT(reg) | RN(TMP_REG3));
1039 }
1040
1041 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
1042 {
1043         if (getput_arg_fast(compiler, flags, reg, arg, argw))
1044                 return compiler->error;
1045         compiler->cache_arg = 0;
1046         compiler->cache_argw = 0;
1047         return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
1048 }
1049
1050 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)
1051 {
1052         if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
1053                 return compiler->error;
1054         return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
1055 }
1056
1057 /* --------------------------------------------------------------------- */
1058 /*  Entry, exit                                                          */
1059 /* --------------------------------------------------------------------- */
1060
1061 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)
1062 {
1063         CHECK_ERROR();
1064         check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
1065
1066         compiler->scratches = scratches;
1067         compiler->saveds = saveds;
1068 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1069         compiler->logical_local_size = local_size;
1070 #endif
1071         compiler->locals_offset = (2 + saveds) * sizeof(sljit_sw);
1072         local_size = (compiler->locals_offset + local_size + 15) & ~15;
1073         compiler->local_size = local_size;
1074
1075         if (local_size <= (64 << 3))
1076                 FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR)
1077                         | RN(TMP_SP) | ((-(local_size >> 3) & 0x7f) << 15)));
1078         else {
1079                 local_size -= (64 << 3);
1080                 if (local_size > 0xfff) {
1081                         FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1082                         local_size &= 0xfff;
1083                 }
1084                 if (local_size)
1085                         FAIL_IF(push_inst(compiler, SUBI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10)));
1086                 FAIL_IF(push_inst(compiler, STP_PRE | 29 | RT2(TMP_LR) | RN(TMP_SP) | (0x40 << 15)));
1087         }
1088
1089         FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_LOCALS_REG) | RN(TMP_SP)));
1090
1091         if (saveds >= 2)
1092                 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15)));
1093         if (saveds >= 4)
1094                 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15)));
1095         if (saveds == 1)
1096                 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10)));
1097         if (saveds == 3)
1098                 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10)));
1099         if (saveds == 5)
1100                 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10)));
1101
1102         if (args >= 1)
1103                 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1)));
1104         if (args >= 2)
1105                 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG2) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG2)));
1106         if (args >= 3)
1107                 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG3) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG3)));
1108
1109         return SLJIT_SUCCESS;
1110 }
1111
1112 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)
1113 {
1114         CHECK_ERROR_VOID();
1115         check_sljit_set_context(compiler, args, scratches, saveds, local_size);
1116
1117         compiler->scratches = scratches;
1118         compiler->saveds = saveds;
1119 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1120         compiler->logical_local_size = local_size;
1121 #endif
1122         compiler->locals_offset = (2 + saveds) * sizeof(sljit_sw);
1123         compiler->local_size = (compiler->locals_offset + local_size + 15) & ~15;
1124 }
1125
1126 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
1127 {
1128         sljit_si saveds, local_size;
1129
1130         CHECK_ERROR();
1131         check_sljit_emit_return(compiler, op, src, srcw);
1132
1133         FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1134
1135         saveds = compiler->saveds;
1136
1137         if (saveds >= 2)
1138                 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15)));
1139         if (saveds >= 4)
1140                 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15)));
1141         if (saveds == 1)
1142                 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10)));
1143         if (saveds == 3)
1144                 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10)));
1145         if (saveds == 5)
1146                 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10)));
1147
1148         local_size = compiler->local_size;
1149
1150         if (local_size <= (62 << 3))
1151                 FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR)
1152                         | RN(TMP_SP) | (((local_size >> 3) & 0x7f) << 15)));
1153         else {
1154                 FAIL_IF(push_inst(compiler, LDP_PST | 29 | RT2(TMP_LR) | RN(TMP_SP) | (0x3e << 15)));
1155                 local_size -= (62 << 3);
1156                 if (local_size > 0xfff) {
1157                         FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1158                         local_size &= 0xfff;
1159                 }
1160                 if (local_size)
1161                         FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10)));
1162         }
1163
1164         FAIL_IF(push_inst(compiler, RET | RN(TMP_LR)));
1165         return SLJIT_SUCCESS;
1166 }
1167
1168 /* --------------------------------------------------------------------- */
1169 /*  Operators                                                            */
1170 /* --------------------------------------------------------------------- */
1171
1172 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
1173 {
1174         sljit_ins inv_bits = (op & SLJIT_INT_OP) ? (1 << 31) : 0;
1175
1176         CHECK_ERROR();
1177         check_sljit_emit_op0(compiler, op);
1178
1179         op = GET_OPCODE(op);
1180         switch (op) {
1181         case SLJIT_BREAKPOINT:
1182                 return push_inst(compiler, BRK);
1183         case SLJIT_NOP:
1184                 return push_inst(compiler, NOP);
1185         case SLJIT_UMUL:
1186         case SLJIT_SMUL:
1187                 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1)));
1188                 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_SCRATCH_REG1) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2) | RT2(TMP_ZERO)));
1189                 return push_inst(compiler, (op == SLJIT_SMUL ? SMULH : UMULH) | RD(SLJIT_SCRATCH_REG2) | RN(TMP_REG1) | RM(SLJIT_SCRATCH_REG2));
1190         case SLJIT_UDIV:
1191         case SLJIT_SDIV:
1192                 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1)));
1193                 FAIL_IF(push_inst(compiler, ((op == SLJIT_SDIV ? SDIV : UDIV) ^ inv_bits) | RD(SLJIT_SCRATCH_REG1) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2)));
1194                 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_SCRATCH_REG2) | RN(SLJIT_SCRATCH_REG1) | RM(SLJIT_SCRATCH_REG2) | RT2(TMP_ZERO)));
1195                 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_SCRATCH_REG2) | RN(TMP_REG1) | RM(SLJIT_SCRATCH_REG2));
1196         }
1197
1198         return SLJIT_SUCCESS;
1199 }
1200
1201 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
1202         sljit_si dst, sljit_sw dstw,
1203         sljit_si src, sljit_sw srcw)
1204 {
1205         sljit_si dst_r, flags, mem_flags;
1206         sljit_si op_flags = GET_ALL_FLAGS(op);
1207
1208         CHECK_ERROR();
1209         check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1210         ADJUST_LOCAL_OFFSET(dst, dstw);
1211         ADJUST_LOCAL_OFFSET(src, srcw);
1212
1213         compiler->cache_arg = 0;
1214         compiler->cache_argw = 0;
1215
1216         dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1217
1218         op = GET_OPCODE(op);
1219         if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) {
1220                 switch (op) {
1221                 case SLJIT_MOV:
1222                 case SLJIT_MOV_P:
1223                         flags = WORD_SIZE;
1224                         break;
1225                 case SLJIT_MOV_UB:
1226                         flags = BYTE_SIZE;
1227                         if (src & SLJIT_IMM)
1228                                 srcw = (sljit_ub)srcw;
1229                         break;
1230                 case SLJIT_MOV_SB:
1231                         flags = BYTE_SIZE | SIGNED;
1232                         if (src & SLJIT_IMM)
1233                                 srcw = (sljit_sb)srcw;
1234                         break;
1235                 case SLJIT_MOV_UH:
1236                         flags = HALF_SIZE;
1237                         if (src & SLJIT_IMM)
1238                                 srcw = (sljit_uh)srcw;
1239                         break;
1240                 case SLJIT_MOV_SH:
1241                         flags = HALF_SIZE | SIGNED;
1242                         if (src & SLJIT_IMM)
1243                                 srcw = (sljit_sh)srcw;
1244                         break;
1245                 case SLJIT_MOV_UI:
1246                         flags = INT_SIZE;
1247                         if (src & SLJIT_IMM)
1248                                 srcw = (sljit_ui)srcw;
1249                         break;
1250                 case SLJIT_MOV_SI:
1251                         flags = INT_SIZE | SIGNED;
1252                         if (src & SLJIT_IMM)
1253                                 srcw = (sljit_si)srcw;
1254                         break;
1255                 case SLJIT_MOVU:
1256                 case SLJIT_MOVU_P:
1257                         flags = WORD_SIZE | UPDATE;
1258                         break;
1259                 case SLJIT_MOVU_UB:
1260                         flags = BYTE_SIZE | UPDATE;
1261                         if (src & SLJIT_IMM)
1262                                 srcw = (sljit_ub)srcw;
1263                         break;
1264                 case SLJIT_MOVU_SB:
1265                         flags = BYTE_SIZE | SIGNED | UPDATE;
1266                         if (src & SLJIT_IMM)
1267                                 srcw = (sljit_sb)srcw;
1268                         break;
1269                 case SLJIT_MOVU_UH:
1270                         flags = HALF_SIZE | UPDATE;
1271                         if (src & SLJIT_IMM)
1272                                 srcw = (sljit_uh)srcw;
1273                         break;
1274                 case SLJIT_MOVU_SH:
1275                         flags = HALF_SIZE | SIGNED | UPDATE;
1276                         if (src & SLJIT_IMM)
1277                                 srcw = (sljit_sh)srcw;
1278                         break;
1279                 case SLJIT_MOVU_UI:
1280                         flags = INT_SIZE | UPDATE;
1281                         if (src & SLJIT_IMM)
1282                                 srcw = (sljit_ui)srcw;
1283                         break;
1284                 case SLJIT_MOVU_SI:
1285                         flags = INT_SIZE | SIGNED | UPDATE;
1286                         if (src & SLJIT_IMM)
1287                                 srcw = (sljit_si)srcw;
1288                         break;
1289                 default:
1290                         SLJIT_ASSERT_STOP();
1291                         flags = 0;
1292                         break;
1293                 }
1294
1295                 if (src & SLJIT_IMM)
1296                         FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1297                 else if (src & SLJIT_MEM) {
1298                         if (getput_arg_fast(compiler, flags, dst_r, src, srcw))
1299                                 FAIL_IF(compiler->error);
1300                         else
1301                                 FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw));
1302                 } else {
1303                         if (dst_r != TMP_REG1)
1304                                 return emit_op_imm(compiler, op | ((op_flags & SLJIT_INT_OP) ? INT_OP : 0), dst_r, TMP_REG1, src);
1305                         dst_r = src;
1306                 }
1307
1308                 if (dst & SLJIT_MEM) {
1309                         if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
1310                                 return compiler->error;
1311                         else
1312                                 return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
1313                 }
1314                 return SLJIT_SUCCESS;
1315         }
1316
1317         flags = GET_FLAGS(op_flags) ? SET_FLAGS : 0;
1318         mem_flags = WORD_SIZE;
1319         if (op_flags & SLJIT_INT_OP) {
1320                 flags |= INT_OP;
1321                 mem_flags = INT_SIZE;
1322         }
1323
1324         if (dst == SLJIT_UNUSED)
1325                 flags |= UNUSED_RETURN;
1326
1327         if (src & SLJIT_MEM) {
1328                 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src, srcw))
1329                         FAIL_IF(compiler->error);
1330                 else
1331                         FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src, srcw, dst, dstw));
1332                 src = TMP_REG2;
1333         }
1334
1335         if (src & SLJIT_IMM) {
1336                 flags |= ARG2_IMM;
1337                 if (op_flags & SLJIT_INT_OP)
1338                         srcw = (sljit_si)srcw;
1339         } else
1340                 srcw = src;
1341
1342         emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw);
1343
1344         if (dst & SLJIT_MEM) {
1345                 if (getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw))
1346                         return compiler->error;
1347                 else
1348                         return getput_arg(compiler, mem_flags | STORE, dst_r, dst, dstw, 0, 0);
1349         }
1350         return SLJIT_SUCCESS;
1351 }
1352
1353 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
1354         sljit_si dst, sljit_sw dstw,
1355         sljit_si src1, sljit_sw src1w,
1356         sljit_si src2, sljit_sw src2w)
1357 {
1358         sljit_si dst_r, flags, mem_flags;
1359
1360         CHECK_ERROR();
1361         check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1362         ADJUST_LOCAL_OFFSET(dst, dstw);
1363         ADJUST_LOCAL_OFFSET(src1, src1w);
1364         ADJUST_LOCAL_OFFSET(src2, src2w);
1365
1366         compiler->cache_arg = 0;
1367         compiler->cache_argw = 0;
1368
1369         dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1370         flags = GET_FLAGS(op) ? SET_FLAGS : 0;
1371         mem_flags = WORD_SIZE;
1372         if (op & SLJIT_INT_OP) {
1373                 flags |= INT_OP;
1374                 mem_flags = INT_SIZE;
1375         }
1376
1377         if (dst == SLJIT_UNUSED)
1378                 flags |= UNUSED_RETURN;
1379
1380         if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, mem_flags | STORE | ARG_TEST, TMP_REG1, dst, dstw))
1381                 flags |= SLOW_DEST;
1382
1383         if (src1 & SLJIT_MEM) {
1384                 if (getput_arg_fast(compiler, mem_flags, TMP_REG1, src1, src1w))
1385                         FAIL_IF(compiler->error);
1386                 else
1387                         flags |= SLOW_SRC1;
1388         }
1389         if (src2 & SLJIT_MEM) {
1390                 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src2, src2w))
1391                         FAIL_IF(compiler->error);
1392                 else
1393                         flags |= SLOW_SRC2;
1394         }
1395
1396         if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1397                 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1398                         FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, src1, src1w));
1399                         FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw));
1400                 }
1401                 else {
1402                         FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, src2, src2w));
1403                         FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw));
1404                 }
1405         }
1406         else if (flags & SLOW_SRC1)
1407                 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG1, src1, src1w, dst, dstw));
1408         else if (flags & SLOW_SRC2)
1409                 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src2, src2w, dst, dstw));
1410
1411         if (src1 & SLJIT_MEM)
1412                 src1 = TMP_REG1;
1413         if (src2 & SLJIT_MEM)
1414                 src2 = TMP_REG2;
1415
1416         if (src1 & SLJIT_IMM)
1417                 flags |= ARG1_IMM;
1418         else
1419                 src1w = src1;
1420         if (src2 & SLJIT_IMM)
1421                 flags |= ARG2_IMM;
1422         else
1423                 src2w = src2;
1424
1425         emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1426
1427         if (dst & SLJIT_MEM) {
1428                 if (!(flags & SLOW_DEST)) {
1429                         getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw);
1430                         return compiler->error;
1431                 }
1432                 return getput_arg(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0);
1433         }
1434
1435         return SLJIT_SUCCESS;
1436 }
1437
1438 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
1439 {
1440         check_sljit_get_register_index(reg);
1441         return reg_map[reg];
1442 }
1443
1444 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
1445 {
1446         check_sljit_get_float_register_index(reg);
1447         return reg;
1448 }
1449
1450 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1451         void *instruction, sljit_si size)
1452 {
1453         CHECK_ERROR();
1454         check_sljit_emit_op_custom(compiler, instruction, size);
1455         SLJIT_ASSERT(size == 4);
1456
1457         return push_inst(compiler, *(sljit_ins*)instruction);
1458 }
1459
1460 /* --------------------------------------------------------------------- */
1461 /*  Floating point operators                                             */
1462 /* --------------------------------------------------------------------- */
1463
1464 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
1465 {
1466 #ifdef SLJIT_IS_FPU_AVAILABLE
1467         return SLJIT_IS_FPU_AVAILABLE;
1468 #else
1469         /* Available by default. */
1470         return 1;
1471 #endif
1472 }
1473
1474 static sljit_si emit_fop_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
1475 {
1476         sljit_ui shift = MEM_SIZE_SHIFT(flags);
1477         sljit_ins ins_bits = (shift << 30);
1478         sljit_si other_r;
1479         sljit_sw diff;
1480
1481         SLJIT_ASSERT(arg & SLJIT_MEM);
1482
1483         if (!(flags & STORE))
1484                 ins_bits |= 1 << 22;
1485
1486         if (arg & OFFS_REG_MASK) {
1487                 argw &= 3;
1488                 if (!argw || argw == shift)
1489                         return push_inst(compiler, STR_FR | ins_bits | VT(reg)
1490                                 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1491                 other_r = OFFS_REG(arg);
1492                 arg &= REG_MASK;
1493                 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg) | RM(other_r) | (argw << 10)));
1494                 arg = TMP_REG1;
1495                 argw = 0;
1496         }
1497
1498         arg &= REG_MASK;
1499         if (arg && argw >= 0 && ((argw >> shift) <= 0xfff) && (argw & ((1 << shift) - 1)) == 0)
1500                 return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(arg) | (argw << (10 - shift)));
1501
1502         if (arg && argw <= 255 && argw >= -256)
1503                 return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
1504
1505         /* Slow cases */
1506         if (compiler->cache_arg == SLJIT_MEM && argw != compiler->cache_argw) {
1507                 diff = argw - compiler->cache_argw;
1508                 if (!arg && diff <= 255 && diff >= -256)
1509                         return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(TMP_REG3) | ((diff & 0x1ff) << 12));
1510                 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1511                         FAIL_IF(compiler->error);
1512                         compiler->cache_argw = argw;
1513                 }
1514         }
1515
1516         if (compiler->cache_arg != SLJIT_MEM || argw != compiler->cache_argw) {
1517                 compiler->cache_arg = SLJIT_MEM;
1518                 compiler->cache_argw = argw;
1519                 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1520         }
1521
1522         if (arg & REG_MASK)
1523                 return push_inst(compiler, STR_FR | ins_bits | VT(reg) | RN(arg) | RM(TMP_REG3));
1524         return push_inst(compiler, STR_FI | ins_bits | VT(reg) | RN(TMP_REG3));
1525 }
1526
1527 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1528         sljit_si dst, sljit_sw dstw,
1529         sljit_si src, sljit_sw srcw)
1530 {
1531         sljit_si dst_r, mem_flags = (op & SLJIT_SINGLE_OP) ? INT_SIZE : WORD_SIZE;
1532         sljit_ins inv_bits = (op & SLJIT_SINGLE_OP) ? (1 << 22) : 0;
1533
1534         CHECK_ERROR();
1535         check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1536
1537         compiler->cache_arg = 0;
1538         compiler->cache_argw = 0;
1539
1540         if (GET_OPCODE(op) == SLJIT_CMPD) {
1541                 if (dst & SLJIT_MEM) {
1542                         emit_fop_mem(compiler, mem_flags, TMP_FREG1, dst, dstw);
1543                         dst = TMP_FREG1;
1544                 }
1545                 if (src & SLJIT_MEM) {
1546                         emit_fop_mem(compiler, mem_flags, TMP_FREG2, src, srcw);
1547                         src = TMP_FREG2;
1548                 }
1549                 return push_inst(compiler, (FCMP ^ inv_bits) | VN(dst) | VM(src));
1550         }
1551
1552         dst_r = (dst <= REG_MASK) ? dst : TMP_FREG1;
1553         if (src & SLJIT_MEM) {
1554                 emit_fop_mem(compiler, mem_flags, dst_r, src, srcw);
1555                 src = dst_r;
1556         }
1557
1558         switch (GET_OPCODE(op)) {
1559         case SLJIT_MOVD:
1560                 if (src != dst_r)
1561                         FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1562                 break;
1563         case SLJIT_NEGD:
1564                 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1565                 break;
1566         case SLJIT_ABSD:
1567                 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1568                 break;
1569         }
1570
1571         if (!(dst & SLJIT_MEM))
1572                 return SLJIT_SUCCESS;
1573         return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1574 }
1575
1576 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1577         sljit_si dst, sljit_sw dstw,
1578         sljit_si src1, sljit_sw src1w,
1579         sljit_si src2, sljit_sw src2w)
1580 {
1581         sljit_si dst_r, mem_flags = (op & SLJIT_SINGLE_OP) ? INT_SIZE : WORD_SIZE;
1582         sljit_ins inv_bits = (op & SLJIT_SINGLE_OP) ? (1 << 22) : 0;
1583
1584         CHECK_ERROR();
1585         check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1586
1587         compiler->cache_arg = 0;
1588         compiler->cache_argw = 0;
1589
1590         dst_r = (dst <= REG_MASK) ? dst : TMP_FREG1;
1591         if (src1 & SLJIT_MEM) {
1592                 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1593                 src1 = TMP_FREG1;
1594         }
1595         if (src2 & SLJIT_MEM) {
1596                 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1597                 src2 = TMP_FREG2;
1598         }
1599
1600         switch (GET_OPCODE(op)) {
1601         case SLJIT_ADDD:
1602                 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1603                 break;
1604         case SLJIT_SUBD:
1605                 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1606                 break;
1607         case SLJIT_MULD:
1608                 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1609                 break;
1610         case SLJIT_DIVD:
1611                 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1612                 break;
1613         }
1614
1615         if (!(dst & SLJIT_MEM))
1616                 return SLJIT_SUCCESS;
1617         return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1618 }
1619
1620 /* --------------------------------------------------------------------- */
1621 /*  Other instructions                                                   */
1622 /* --------------------------------------------------------------------- */
1623
1624 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1625 {
1626         CHECK_ERROR();
1627         check_sljit_emit_fast_enter(compiler, dst, dstw);
1628         ADJUST_LOCAL_OFFSET(dst, dstw);
1629
1630         /* For UNUSED dst. Uncommon, but possible. */
1631         if (dst == SLJIT_UNUSED)
1632                 return SLJIT_SUCCESS;
1633
1634         if (dst <= REG_MASK)
1635                 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1636
1637         /* Memory. */
1638         return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw);
1639 }
1640
1641 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1642 {
1643         CHECK_ERROR();
1644         check_sljit_emit_fast_return(compiler, src, srcw);
1645         ADJUST_LOCAL_OFFSET(src, srcw);
1646
1647         if (src <= REG_MASK)
1648                 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1649         else if (src & SLJIT_MEM)
1650                 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw));
1651         else if (src & SLJIT_IMM)
1652                 FAIL_IF(load_immediate(compiler, TMP_LR, srcw));
1653
1654         return push_inst(compiler, RET | RN(TMP_LR));
1655 }
1656
1657 /* --------------------------------------------------------------------- */
1658 /*  Conditional instructions                                             */
1659 /* --------------------------------------------------------------------- */
1660
1661 static sljit_uw get_cc(sljit_si type)
1662 {
1663         switch (type) {
1664         case SLJIT_C_EQUAL:
1665         case SLJIT_C_MUL_NOT_OVERFLOW:
1666         case SLJIT_C_FLOAT_EQUAL:
1667                 return 0x1;
1668
1669         case SLJIT_C_NOT_EQUAL:
1670         case SLJIT_C_MUL_OVERFLOW:
1671         case SLJIT_C_FLOAT_NOT_EQUAL:
1672                 return 0x0;
1673
1674         case SLJIT_C_LESS:
1675         case SLJIT_C_FLOAT_LESS:
1676                 return 0x2;
1677
1678         case SLJIT_C_GREATER_EQUAL:
1679         case SLJIT_C_FLOAT_GREATER_EQUAL:
1680                 return 0x3;
1681
1682         case SLJIT_C_GREATER:
1683         case SLJIT_C_FLOAT_GREATER:
1684                 return 0x9;
1685
1686         case SLJIT_C_LESS_EQUAL:
1687         case SLJIT_C_FLOAT_LESS_EQUAL:
1688                 return 0x8;
1689
1690         case SLJIT_C_SIG_LESS:
1691                 return 0xa;
1692
1693         case SLJIT_C_SIG_GREATER_EQUAL:
1694                 return 0xb;
1695
1696         case SLJIT_C_SIG_GREATER:
1697                 return 0xd;
1698
1699         case SLJIT_C_SIG_LESS_EQUAL:
1700                 return 0xc;
1701
1702         case SLJIT_C_OVERFLOW:
1703         case SLJIT_C_FLOAT_UNORDERED:
1704                 return 0x7;
1705
1706         case SLJIT_C_NOT_OVERFLOW:
1707         case SLJIT_C_FLOAT_ORDERED:
1708                 return 0x6;
1709
1710         default:
1711                 SLJIT_ASSERT_STOP();
1712                 return 0xe;
1713         }
1714 }
1715
1716 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1717 {
1718         struct sljit_label *label;
1719
1720         CHECK_ERROR_PTR();
1721         check_sljit_emit_label(compiler);
1722
1723         if (compiler->last_label && compiler->last_label->size == compiler->size)
1724                 return compiler->last_label;
1725
1726         label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1727         PTR_FAIL_IF(!label);
1728         set_label(label, compiler);
1729         return label;
1730 }
1731
1732 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1733 {
1734         struct sljit_jump *jump;
1735
1736         CHECK_ERROR_PTR();
1737         check_sljit_emit_jump(compiler, type);
1738
1739         jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1740         PTR_FAIL_IF(!jump);
1741         set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1742         type &= 0xff;
1743
1744         if (type < SLJIT_JUMP) {
1745                 jump->flags |= IS_COND;
1746                 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(type)));
1747         }
1748         else if (type >= SLJIT_FAST_CALL)
1749                 jump->flags |= IS_BL;
1750
1751         PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1752         jump->addr = compiler->size;
1753         PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1754
1755         return jump;
1756 }
1757
1758 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_si type,
1759         sljit_si src, sljit_sw srcw)
1760 {
1761         struct sljit_jump *jump;
1762         sljit_ins inv_bits = (type & SLJIT_INT_OP) ? (1 << 31) : 0;
1763
1764         SLJIT_ASSERT((type & 0xff) == SLJIT_C_EQUAL || (type & 0xff) == SLJIT_C_NOT_EQUAL);
1765         ADJUST_LOCAL_OFFSET(src, srcw);
1766
1767         jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1768         PTR_FAIL_IF(!jump);
1769         set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1770         jump->flags |= IS_CBZ | IS_COND;
1771
1772         if (src & SLJIT_MEM) {
1773                 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw));
1774                 src = TMP_REG1;
1775         }
1776         else if (src & SLJIT_IMM) {
1777                 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1778                 src = TMP_REG1;
1779         }
1780         SLJIT_ASSERT(FAST_IS_REG(src));
1781
1782         if ((type & 0xff) == SLJIT_C_EQUAL)
1783                 inv_bits |= 1 << 24;
1784
1785         PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1786         PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1787         jump->addr = compiler->size;
1788         PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1789         return jump;
1790 }
1791
1792 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1793 {
1794         struct sljit_jump *jump;
1795
1796         CHECK_ERROR();
1797         check_sljit_emit_ijump(compiler, type, src, srcw);
1798         ADJUST_LOCAL_OFFSET(src, srcw);
1799
1800         /* In ARM, we don't need to touch the arguments. */
1801         if (!(src & SLJIT_IMM)) {
1802                 if (src & SLJIT_MEM) {
1803                         FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw));
1804                         src = TMP_REG1;
1805                 }
1806                 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1807         }
1808
1809         jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1810         FAIL_IF(!jump);
1811         set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1812         jump->u.target = srcw;
1813
1814         FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1815         jump->addr = compiler->size;
1816         return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1817 }
1818
1819 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1820         sljit_si dst, sljit_sw dstw,
1821         sljit_si src, sljit_sw srcw,
1822         sljit_si type)
1823 {
1824         sljit_si dst_r, flags, mem_flags;
1825         sljit_ins cc;
1826
1827         CHECK_ERROR();
1828         check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type);
1829         ADJUST_LOCAL_OFFSET(dst, dstw);
1830         ADJUST_LOCAL_OFFSET(src, srcw);
1831
1832         if (dst == SLJIT_UNUSED)
1833                 return SLJIT_SUCCESS;
1834
1835         cc = get_cc(type);
1836         dst_r = (dst <= REG_MASK) ? dst : TMP_REG1;
1837
1838         if (GET_OPCODE(op) < SLJIT_ADD) {
1839                 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1840                 if (dst_r != TMP_REG1)
1841                         return SLJIT_SUCCESS;
1842                 return emit_op_mem(compiler, (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE, TMP_REG1, dst, dstw);
1843         }
1844
1845         compiler->cache_arg = 0;
1846         compiler->cache_argw = 0;
1847         flags = GET_FLAGS(op) ? SET_FLAGS : 0;
1848         mem_flags = WORD_SIZE;
1849         if (op & SLJIT_INT_OP) {
1850                 flags |= INT_OP;
1851                 mem_flags = INT_SIZE;
1852         }
1853
1854         if (src & SLJIT_MEM) {
1855                 FAIL_IF(emit_op_mem2(compiler, mem_flags, TMP_REG1, src, srcw, dst, dstw));
1856                 src = TMP_REG1;
1857                 srcw = 0;
1858         } else if (src & SLJIT_IMM)
1859                 flags |= ARG1_IMM;
1860
1861         FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1862         emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src, TMP_REG2);
1863
1864         if (dst_r != TMP_REG1)
1865                 return SLJIT_SUCCESS;
1866         return emit_op_mem2(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0);
1867 }
1868
1869 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1870 {
1871         struct sljit_const *const_;
1872         sljit_si dst_r;
1873
1874         CHECK_ERROR_PTR();
1875         check_sljit_emit_const(compiler, dst, dstw, init_value);
1876         ADJUST_LOCAL_OFFSET(dst, dstw);
1877
1878         const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1879         PTR_FAIL_IF(!const_);
1880         set_const(const_, compiler);
1881
1882         dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1883         PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value));
1884
1885         if (dst & SLJIT_MEM)
1886                 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw));
1887         return const_;
1888 }
1889
1890 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
1891 {
1892         sljit_ins* inst = (sljit_ins*)addr;
1893         modify_imm64_const(inst, new_addr);
1894         SLJIT_CACHE_FLUSH(inst, inst + 4);
1895 }
1896
1897 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
1898 {
1899         sljit_ins* inst = (sljit_ins*)addr;
1900         modify_imm64_const(inst, new_constant);
1901         SLJIT_CACHE_FLUSH(inst, inst + 4);
1902 }