2 * Stack-less Just-In-Time compiler
4 * Copyright 2009-2012 Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void)
29 return "ARM-64" SLJIT_CPUINFO;
32 /* Length of an instruction word */
33 typedef sljit_ui sljit_ins;
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)
45 #define TMP_FREG2 (SLJIT_FLOAT_REG6 + 1)
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
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)
59 #define VN(vn) ((vn) << 5)
60 #define VM(vm) ((vm) << 16)
62 /* --------------------------------------------------------------------- */
63 /* Instrucion forms */
64 /* --------------------------------------------------------------------- */
66 #define ADC 0x9a000000
67 #define ADD 0x8b000000
68 #define ADDI 0x91000000
69 #define AND 0x8a000000
70 #define ANDI 0x92000000
71 #define ASRV 0x9ac02800
73 #define B_CC 0x54000000
75 #define BLR 0xd63f0000
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
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)
127 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
131 return SLJIT_SUCCESS;
134 static SLJIT_INLINE sljit_si emit_imm64_const(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
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));
142 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm)
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);
152 static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
155 sljit_uw target_addr;
157 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
158 jump->flags |= PATCH_ABS64;
162 if (jump->flags & JUMP_ADDR)
163 target_addr = jump->u.target;
165 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
166 target_addr = (sljit_uw)(code + jump->u.label->size);
168 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4);
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;
178 diff -= sizeof(sljit_ins);
181 if (diff <= 0x7ffffff && diff >= -0x8000000) {
182 jump->flags |= PATCH_B;
186 if (target_addr <= 0xffffffffl) {
187 if (jump->flags & IS_COND)
188 code_ptr[-5] -= (2 << 5);
189 code_ptr[-2] = code_ptr[0];
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];
200 jump->flags |= PATCH_ABS64;
204 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
206 struct sljit_memory_fragment *buf;
215 struct sljit_label *label;
216 struct sljit_jump *jump;
217 struct sljit_const *const_;
220 check_sljit_generate_code(compiler);
221 reverse_buf(compiler);
223 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
224 PTR_FAIL_WITH_EXEC_IF(code);
229 label = compiler->labels;
230 jump = compiler->jumps;
231 const_ = compiler->consts;
234 buf_ptr = (sljit_ins*)buf->memory;
235 buf_end = buf_ptr + (buf->used_size >> 2);
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;
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);
252 if (const_ && const_->addr == word_count) {
253 const_->addr = (sljit_uw)code_ptr;
254 const_ = const_->next;
258 } while (buf_ptr < buf_end);
263 if (label && label->size == word_count) {
264 label->addr = (sljit_uw)code_ptr;
265 label->size = code_ptr - code;
269 SLJIT_ASSERT(!label);
271 SLJIT_ASSERT(!const_);
272 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
274 jump = compiler->jumps;
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);
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);
294 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
295 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
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);
308 compiler->error = SLJIT_ERR_COMPILED;
309 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
310 SLJIT_CACHE_FLUSH(code, code_ptr);
314 /* --------------------------------------------------------------------- */
315 /* Core code generator functions. */
316 /* --------------------------------------------------------------------- */
318 #define COUNT_TRAILING_ZERO(value, result) \
320 if (!(value & 0xffffffff)) { \
324 if (!(value & 0xffff)) { \
328 if (!(value & 0xff)) { \
332 if (!(value & 0xf)) { \
336 if (!(value & 0x3)) { \
340 if (!(value & 0x1)) { \
345 #define LOGICAL_IMM_CHECK 0x100
347 static sljit_ins logical_imm(sljit_sw imm, sljit_si len)
349 sljit_si negated, ones, right;
353 if (len & LOGICAL_IMM_CHECK) {
354 len &= ~LOGICAL_IMM_CHECK;
355 if (len == 32 && (imm == 0 || imm == -1))
357 if (len == 16 && ((sljit_si)imm == 0 || (sljit_si)imm == -1))
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;
369 mask = ((sljit_uw)1 << len) - 1;
370 if ((uimm & mask) != ((uimm >> len) & mask))
384 uimm &= ((sljit_uw)1 << len) - 1;
386 /* Unsigned right shift. */
387 COUNT_TRAILING_ZERO(uimm, right);
389 /* Signed shift. We also know that the highest bit is set. */
390 imm = (sljit_sw)~uimm;
391 SLJIT_ASSERT(imm < 0);
393 COUNT_TRAILING_ZERO(imm, ones);
401 ins = (0x3f - ((len << 1) - 1)) << 10;
404 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
406 return ins | ((ones - 1) << 10) | ((len - right) << 16);
409 #undef COUNT_TRAILING_ZERO
411 static sljit_si load_immediate(struct sljit_compiler *compiler, sljit_si dst, sljit_sw simm)
413 sljit_uw imm = (sljit_uw)simm;
414 sljit_si i, zeros, ones, first;
418 return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
420 if (simm >= -0x10000 && simm < 0)
421 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
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);
430 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
433 bitmask = logical_imm(simm, 32);
435 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
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));
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));
448 /* A large amount of number can be constructed from ORR and MOVx,
449 but computing them is costly. We don't */
453 for (i = 4; i > 0; i--) {
454 if ((simm & 0xffff) == 0)
456 if ((simm & 0xffff) == 0xffff)
461 simm = (sljit_sw)imm;
465 for (i = 0; i < 4; i++) {
466 if (!(simm & 0xffff)) {
472 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
475 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
478 return SLJIT_SUCCESS;
481 for (i = 0; i < 4; i++) {
482 if (!(simm & 0xffff)) {
488 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
491 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
494 return SLJIT_SUCCESS;
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
506 #define CHECK_FLAGS(flag_bits) \
507 if (flags & SET_FLAGS) { \
508 inv_bits |= flag_bits; \
509 if (flags & UNUSED_RETURN) \
513 static sljit_si emit_op_imm(struct sljit_compiler *compiler, sljit_si flags, sljit_si dst, sljit_sw arg1, sljit_sw arg2)
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;
520 sljit_si op = (flags & 0xffff);
524 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
525 /* Both are immediates. */
527 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
530 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
535 if (flags & (ARG1_IMM | ARG2_IMM)) {
536 reg = (flags & ARG2_IMM) ? arg1 : arg2;
537 imm = (flags & ARG2_IMM) ? arg2 : arg1;
545 /* No form with immediate operand (except imm 0, which
546 is represented by a ZERO register). */
549 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
550 return load_immediate(compiler, dst, imm);
552 SLJIT_ASSERT(flags & ARG2_IMM);
553 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
556 if (flags & ARG1_IMM)
562 CHECK_FLAGS(1 << 29);
563 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
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));
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));
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));
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));
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));
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));
592 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
595 CHECK_FLAGS(3 << 29);
596 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
599 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
606 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
609 if (flags & ARG1_IMM)
611 if (flags & INT_OP) {
613 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
617 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
622 if (flags & ARG1_IMM)
624 if (op == SLJIT_ASHR)
626 if (flags & INT_OP) {
628 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10)));
632 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
640 if (flags & ARG2_IMM) {
644 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
652 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
658 /* Both arguments are registers. */
664 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
666 return SLJIT_SUCCESS;
667 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
670 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
671 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (7 << 10));
674 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
675 if (!(flags & INT_OP))
677 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
680 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
681 return push_inst(compiler, (UBFM ^ (1 << 31)) | RD(dst) | RN(arg2) | (15 << 10));
684 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
685 if (!(flags & INT_OP))
687 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
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));
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));
701 SLJIT_ASSERT(arg1 == TMP_REG1);
702 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
705 SLJIT_ASSERT(arg1 == TMP_REG1);
706 if (flags & SET_FLAGS)
708 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
710 SLJIT_ASSERT(arg1 == TMP_REG1);
711 FAIL_IF(push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2)));
714 CHECK_FLAGS(1 << 29);
715 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
717 CHECK_FLAGS(1 << 29);
718 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
720 CHECK_FLAGS(1 << 29);
721 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
723 CHECK_FLAGS(1 << 29);
724 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
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));
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));
737 CHECK_FLAGS(3 << 29);
738 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
740 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
743 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
746 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
749 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
752 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
757 return SLJIT_SUCCESS;
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;
769 #define ARG_TEST 0x08
771 #define BYTE_SIZE 0x000
772 #define HALF_SIZE 0x100
773 #define INT_SIZE 0x200
774 #define WORD_SIZE 0x300
776 #define MEM_SIZE_SHIFT(flags) ((flags) >> 8)
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] */,
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] */,
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]! */,
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] */,
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)
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));
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));
822 return SLJIT_ERR_UNSUPPORTED;
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)
828 sljit_ui shift = MEM_SIZE_SHIFT(flags);
830 SLJIT_ASSERT(arg & SLJIT_MEM);
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))
839 FAIL_IF(push_inst(compiler, sljit_mem_pre_simm[flags & 0x3]
840 | (shift << 30) | RT(reg) | RN(arg) | (argw << 12)));
846 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
848 if (argw && argw != shift)
851 if (SLJIT_UNLIKELY(flags & ARG_TEST))
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)));
860 if (argw >= 0 && (argw >> shift) <= 0xfff && (argw & ((1 << shift) - 1)) == 0) {
861 if (SLJIT_UNLIKELY(flags & ARG_TEST))
864 FAIL_IF(push_inst(compiler, sljit_mem_imm[flags & 0x3] | (shift << 30)
865 | RT(reg) | RN(arg) | (argw << (10 - shift))));
869 if (argw > 255 || argw < -256)
872 if (SLJIT_UNLIKELY(flags & ARG_TEST))
875 FAIL_IF(push_inst(compiler, sljit_mem_simm[flags & 0x3] | (shift << 30)
876 | RT(reg) | RN(arg) | ((argw & 0x1ff) << 12)));
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)
886 if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM))
889 if (!(arg & REG_MASK)) {
890 diff = argw - next_argw;
891 if (diff <= 0xfff && diff >= -0xfff)
896 if (argw == next_argw)
899 diff = argw - next_argw;
900 if (arg == next_arg && diff <= 0xfff && diff >= -0xfff)
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)
910 sljit_ui shift = MEM_SIZE_SHIFT(flags);
911 sljit_si tmp_r, other_r;
914 SLJIT_ASSERT(arg & SLJIT_MEM);
915 if (!(next_arg & SLJIT_MEM)) {
920 tmp_r = (flags & STORE) ? TMP_REG3 : reg;
922 if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) {
923 /* Update only applies if a base register exists. */
924 other_r = OFFS_REG(arg);
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)));
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));
934 else if (other_r != reg && argw < 0 && argw >= -0xffffff) {
936 if ((argw & 0xfff) != 0)
937 FAIL_IF(push_inst(compiler, SUBI | RD(other_r) | RN(other_r) | ((argw & 0xfff) << 10)));
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));
943 if (compiler->cache_arg == SLJIT_MEM) {
944 if (argw == compiler->cache_argw) {
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;
957 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
958 compiler->cache_arg = SLJIT_MEM;
959 compiler->cache_argw = argw;
965 /* No caching here. */
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));
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));
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));
981 if (arg & OFFS_REG_MASK) {
982 other_r = OFFS_REG(arg);
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));
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));
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)));
1005 diff = argw - next_argw;
1006 next_arg = (arg & REG_MASK) && (arg == next_arg) && diff <= 0xfff && diff >= -0xfff && diff != 0;
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));
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;
1026 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1027 compiler->cache_arg = SLJIT_MEM;
1030 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG3) | RN(TMP_REG3) | RM(arg)));
1031 compiler->cache_arg = SLJIT_MEM | 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));
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)
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);
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)
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);
1057 /* --------------------------------------------------------------------- */
1059 /* --------------------------------------------------------------------- */
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)
1064 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
1066 compiler->scratches = scratches;
1067 compiler->saveds = saveds;
1068 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1069 compiler->logical_local_size = local_size;
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;
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)));
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;
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)));
1089 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_LOCALS_REG) | RN(TMP_SP)));
1092 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15)));
1094 FAIL_IF(push_inst(compiler, STP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15)));
1096 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10)));
1098 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10)));
1100 FAIL_IF(push_inst(compiler, STRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10)));
1103 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG1) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG1)));
1105 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG2) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG2)));
1107 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_SAVED_REG3) | RN(TMP_ZERO) | RM(SLJIT_SCRATCH_REG3)));
1109 return SLJIT_SUCCESS;
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)
1115 check_sljit_set_context(compiler, args, scratches, saveds, local_size);
1117 compiler->scratches = scratches;
1118 compiler->saveds = saveds;
1119 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1120 compiler->logical_local_size = local_size;
1122 compiler->locals_offset = (2 + saveds) * sizeof(sljit_sw);
1123 compiler->local_size = (compiler->locals_offset + local_size + 15) & ~15;
1126 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
1128 sljit_si saveds, local_size;
1131 check_sljit_emit_return(compiler, op, src, srcw);
1133 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1135 saveds = compiler->saveds;
1138 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG1) | RT2(SLJIT_SAVED_REG2) | RN(TMP_SP) | (2 << 15)));
1140 FAIL_IF(push_inst(compiler, LDP | RT(SLJIT_SAVED_REG3) | RT2(SLJIT_SAVED_EREG1) | RN(TMP_SP) | (4 << 15)));
1142 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG1) | RN(TMP_SP) | (2 << 10)));
1144 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_REG3) | RN(TMP_SP) | (4 << 10)));
1146 FAIL_IF(push_inst(compiler, LDRI | RT(SLJIT_SAVED_EREG2) | RN(TMP_SP) | (6 << 10)));
1148 local_size = compiler->local_size;
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)));
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;
1161 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_SP) | RN(TMP_SP) | (local_size << 10)));
1164 FAIL_IF(push_inst(compiler, RET | RN(TMP_LR)));
1165 return SLJIT_SUCCESS;
1168 /* --------------------------------------------------------------------- */
1170 /* --------------------------------------------------------------------- */
1172 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
1174 sljit_ins inv_bits = (op & SLJIT_INT_OP) ? (1 << 31) : 0;
1177 check_sljit_emit_op0(compiler, op);
1179 op = GET_OPCODE(op);
1181 case SLJIT_BREAKPOINT:
1182 return push_inst(compiler, BRK);
1184 return push_inst(compiler, NOP);
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));
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));
1198 return SLJIT_SUCCESS;
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)
1205 sljit_si dst_r, flags, mem_flags;
1206 sljit_si op_flags = GET_ALL_FLAGS(op);
1209 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1210 ADJUST_LOCAL_OFFSET(dst, dstw);
1211 ADJUST_LOCAL_OFFSET(src, srcw);
1213 compiler->cache_arg = 0;
1214 compiler->cache_argw = 0;
1216 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1218 op = GET_OPCODE(op);
1219 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) {
1227 if (src & SLJIT_IMM)
1228 srcw = (sljit_ub)srcw;
1231 flags = BYTE_SIZE | SIGNED;
1232 if (src & SLJIT_IMM)
1233 srcw = (sljit_sb)srcw;
1237 if (src & SLJIT_IMM)
1238 srcw = (sljit_uh)srcw;
1241 flags = HALF_SIZE | SIGNED;
1242 if (src & SLJIT_IMM)
1243 srcw = (sljit_sh)srcw;
1247 if (src & SLJIT_IMM)
1248 srcw = (sljit_ui)srcw;
1251 flags = INT_SIZE | SIGNED;
1252 if (src & SLJIT_IMM)
1253 srcw = (sljit_si)srcw;
1257 flags = WORD_SIZE | UPDATE;
1260 flags = BYTE_SIZE | UPDATE;
1261 if (src & SLJIT_IMM)
1262 srcw = (sljit_ub)srcw;
1265 flags = BYTE_SIZE | SIGNED | UPDATE;
1266 if (src & SLJIT_IMM)
1267 srcw = (sljit_sb)srcw;
1270 flags = HALF_SIZE | UPDATE;
1271 if (src & SLJIT_IMM)
1272 srcw = (sljit_uh)srcw;
1275 flags = HALF_SIZE | SIGNED | UPDATE;
1276 if (src & SLJIT_IMM)
1277 srcw = (sljit_sh)srcw;
1280 flags = INT_SIZE | UPDATE;
1281 if (src & SLJIT_IMM)
1282 srcw = (sljit_ui)srcw;
1285 flags = INT_SIZE | SIGNED | UPDATE;
1286 if (src & SLJIT_IMM)
1287 srcw = (sljit_si)srcw;
1290 SLJIT_ASSERT_STOP();
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);
1301 FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw));
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);
1308 if (dst & SLJIT_MEM) {
1309 if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
1310 return compiler->error;
1312 return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
1314 return SLJIT_SUCCESS;
1317 flags = GET_FLAGS(op_flags) ? SET_FLAGS : 0;
1318 mem_flags = WORD_SIZE;
1319 if (op_flags & SLJIT_INT_OP) {
1321 mem_flags = INT_SIZE;
1324 if (dst == SLJIT_UNUSED)
1325 flags |= UNUSED_RETURN;
1327 if (src & SLJIT_MEM) {
1328 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src, srcw))
1329 FAIL_IF(compiler->error);
1331 FAIL_IF(getput_arg(compiler, mem_flags, TMP_REG2, src, srcw, dst, dstw));
1335 if (src & SLJIT_IMM) {
1337 if (op_flags & SLJIT_INT_OP)
1338 srcw = (sljit_si)srcw;
1342 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw);
1344 if (dst & SLJIT_MEM) {
1345 if (getput_arg_fast(compiler, mem_flags | STORE, dst_r, dst, dstw))
1346 return compiler->error;
1348 return getput_arg(compiler, mem_flags | STORE, dst_r, dst, dstw, 0, 0);
1350 return SLJIT_SUCCESS;
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)
1358 sljit_si dst_r, flags, mem_flags;
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);
1366 compiler->cache_arg = 0;
1367 compiler->cache_argw = 0;
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) {
1374 mem_flags = INT_SIZE;
1377 if (dst == SLJIT_UNUSED)
1378 flags |= UNUSED_RETURN;
1380 if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, mem_flags | STORE | ARG_TEST, TMP_REG1, dst, dstw))
1383 if (src1 & SLJIT_MEM) {
1384 if (getput_arg_fast(compiler, mem_flags, TMP_REG1, src1, src1w))
1385 FAIL_IF(compiler->error);
1389 if (src2 & SLJIT_MEM) {
1390 if (getput_arg_fast(compiler, mem_flags, TMP_REG2, src2, src2w))
1391 FAIL_IF(compiler->error);
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));
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));
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));
1411 if (src1 & SLJIT_MEM)
1413 if (src2 & SLJIT_MEM)
1416 if (src1 & SLJIT_IMM)
1420 if (src2 & SLJIT_IMM)
1425 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
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;
1432 return getput_arg(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, 0, 0);
1435 return SLJIT_SUCCESS;
1438 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
1440 check_sljit_get_register_index(reg);
1441 return reg_map[reg];
1444 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
1446 check_sljit_get_float_register_index(reg);
1450 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1451 void *instruction, sljit_si size)
1454 check_sljit_emit_op_custom(compiler, instruction, size);
1455 SLJIT_ASSERT(size == 4);
1457 return push_inst(compiler, *(sljit_ins*)instruction);
1460 /* --------------------------------------------------------------------- */
1461 /* Floating point operators */
1462 /* --------------------------------------------------------------------- */
1464 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
1466 #ifdef SLJIT_IS_FPU_AVAILABLE
1467 return SLJIT_IS_FPU_AVAILABLE;
1469 /* Available by default. */
1474 static sljit_si emit_fop_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
1476 sljit_ui shift = MEM_SIZE_SHIFT(flags);
1477 sljit_ins ins_bits = (shift << 30);
1481 SLJIT_ASSERT(arg & SLJIT_MEM);
1483 if (!(flags & STORE))
1484 ins_bits |= 1 << 22;
1486 if (arg & OFFS_REG_MASK) {
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);
1493 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg) | RM(other_r) | (argw << 10)));
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)));
1502 if (arg && argw <= 255 && argw >= -256)
1503 return push_inst(compiler, STUR_FI | ins_bits | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
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;
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));
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));
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)
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;
1535 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1537 compiler->cache_arg = 0;
1538 compiler->cache_argw = 0;
1540 if (GET_OPCODE(op) == SLJIT_CMPD) {
1541 if (dst & SLJIT_MEM) {
1542 emit_fop_mem(compiler, mem_flags, TMP_FREG1, dst, dstw);
1545 if (src & SLJIT_MEM) {
1546 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src, srcw);
1549 return push_inst(compiler, (FCMP ^ inv_bits) | VN(dst) | VM(src));
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);
1558 switch (GET_OPCODE(op)) {
1561 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1564 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1567 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1571 if (!(dst & SLJIT_MEM))
1572 return SLJIT_SUCCESS;
1573 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
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)
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;
1585 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1587 compiler->cache_arg = 0;
1588 compiler->cache_argw = 0;
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);
1595 if (src2 & SLJIT_MEM) {
1596 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1600 switch (GET_OPCODE(op)) {
1602 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1605 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1608 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1611 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1615 if (!(dst & SLJIT_MEM))
1616 return SLJIT_SUCCESS;
1617 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1620 /* --------------------------------------------------------------------- */
1621 /* Other instructions */
1622 /* --------------------------------------------------------------------- */
1624 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1627 check_sljit_emit_fast_enter(compiler, dst, dstw);
1628 ADJUST_LOCAL_OFFSET(dst, dstw);
1630 /* For UNUSED dst. Uncommon, but possible. */
1631 if (dst == SLJIT_UNUSED)
1632 return SLJIT_SUCCESS;
1634 if (dst <= REG_MASK)
1635 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1638 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw);
1641 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1644 check_sljit_emit_fast_return(compiler, src, srcw);
1645 ADJUST_LOCAL_OFFSET(src, srcw);
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));
1654 return push_inst(compiler, RET | RN(TMP_LR));
1657 /* --------------------------------------------------------------------- */
1658 /* Conditional instructions */
1659 /* --------------------------------------------------------------------- */
1661 static sljit_uw get_cc(sljit_si type)
1665 case SLJIT_C_MUL_NOT_OVERFLOW:
1666 case SLJIT_C_FLOAT_EQUAL:
1669 case SLJIT_C_NOT_EQUAL:
1670 case SLJIT_C_MUL_OVERFLOW:
1671 case SLJIT_C_FLOAT_NOT_EQUAL:
1675 case SLJIT_C_FLOAT_LESS:
1678 case SLJIT_C_GREATER_EQUAL:
1679 case SLJIT_C_FLOAT_GREATER_EQUAL:
1682 case SLJIT_C_GREATER:
1683 case SLJIT_C_FLOAT_GREATER:
1686 case SLJIT_C_LESS_EQUAL:
1687 case SLJIT_C_FLOAT_LESS_EQUAL:
1690 case SLJIT_C_SIG_LESS:
1693 case SLJIT_C_SIG_GREATER_EQUAL:
1696 case SLJIT_C_SIG_GREATER:
1699 case SLJIT_C_SIG_LESS_EQUAL:
1702 case SLJIT_C_OVERFLOW:
1703 case SLJIT_C_FLOAT_UNORDERED:
1706 case SLJIT_C_NOT_OVERFLOW:
1707 case SLJIT_C_FLOAT_ORDERED:
1711 SLJIT_ASSERT_STOP();
1716 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1718 struct sljit_label *label;
1721 check_sljit_emit_label(compiler);
1723 if (compiler->last_label && compiler->last_label->size == compiler->size)
1724 return compiler->last_label;
1726 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1727 PTR_FAIL_IF(!label);
1728 set_label(label, compiler);
1732 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1734 struct sljit_jump *jump;
1737 check_sljit_emit_jump(compiler, type);
1739 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1741 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1744 if (type < SLJIT_JUMP) {
1745 jump->flags |= IS_COND;
1746 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(type)));
1748 else if (type >= SLJIT_FAST_CALL)
1749 jump->flags |= IS_BL;
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)));
1758 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_si type,
1759 sljit_si src, sljit_sw srcw)
1761 struct sljit_jump *jump;
1762 sljit_ins inv_bits = (type & SLJIT_INT_OP) ? (1 << 31) : 0;
1764 SLJIT_ASSERT((type & 0xff) == SLJIT_C_EQUAL || (type & 0xff) == SLJIT_C_NOT_EQUAL);
1765 ADJUST_LOCAL_OFFSET(src, srcw);
1767 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1769 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1770 jump->flags |= IS_CBZ | IS_COND;
1772 if (src & SLJIT_MEM) {
1773 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw));
1776 else if (src & SLJIT_IMM) {
1777 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1780 SLJIT_ASSERT(FAST_IS_REG(src));
1782 if ((type & 0xff) == SLJIT_C_EQUAL)
1783 inv_bits |= 1 << 24;
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)));
1792 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1794 struct sljit_jump *jump;
1797 check_sljit_emit_ijump(compiler, type, src, srcw);
1798 ADJUST_LOCAL_OFFSET(src, srcw);
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));
1806 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1809 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1811 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1812 jump->u.target = srcw;
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));
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,
1824 sljit_si dst_r, flags, mem_flags;
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);
1832 if (dst == SLJIT_UNUSED)
1833 return SLJIT_SUCCESS;
1836 dst_r = (dst <= REG_MASK) ? dst : TMP_REG1;
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);
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) {
1851 mem_flags = INT_SIZE;
1854 if (src & SLJIT_MEM) {
1855 FAIL_IF(emit_op_mem2(compiler, mem_flags, TMP_REG1, src, srcw, dst, dstw));
1858 } else if (src & SLJIT_IMM)
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);
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);
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)
1871 struct sljit_const *const_;
1875 check_sljit_emit_const(compiler, dst, dstw, init_value);
1876 ADJUST_LOCAL_OFFSET(dst, dstw);
1878 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1879 PTR_FAIL_IF(!const_);
1880 set_const(const_, compiler);
1882 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1883 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value));
1885 if (dst & SLJIT_MEM)
1886 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw));
1890 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
1892 sljit_ins* inst = (sljit_ins*)addr;
1893 modify_imm64_const(inst, new_addr);
1894 SLJIT_CACHE_FLUSH(inst, inst + 4);
1897 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
1899 sljit_ins* inst = (sljit_ins*)addr;
1900 modify_imm64_const(inst, new_constant);
1901 SLJIT_CACHE_FLUSH(inst, inst + 4);