apple: restrict calling pthread_jit_write_protect_np to macOS >= 11 (#111)
[sljit.git] / sljit_src / sljitNativeARM_64.c
blob3f0f5fcc30b15ebe3866cf75baa5e8871c7e9565
1 /*
2 * Stack-less Just-In-Time compiler
4 * Copyright 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 const char* sljit_get_platform_name(void)
29 return "ARM-64" SLJIT_CPUINFO;
32 /* Length of an instruction word */
33 typedef sljit_u32 sljit_ins;
35 #define TMP_ZERO (0)
37 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
38 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
39 #define TMP_LR (SLJIT_NUMBER_OF_REGISTERS + 4)
40 #define TMP_FP (SLJIT_NUMBER_OF_REGISTERS + 5)
42 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
43 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
45 /* r18 - platform register, currently not used */
46 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 8] = {
47 31, 0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17, 8, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 31, 9, 10, 30, 29
50 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
51 0, 0, 1, 2, 3, 4, 5, 6, 7
54 #define W_OP (1u << 31)
55 #define RD(rd) (reg_map[rd])
56 #define RT(rt) (reg_map[rt])
57 #define RN(rn) (reg_map[rn] << 5)
58 #define RT2(rt2) (reg_map[rt2] << 10)
59 #define RM(rm) (reg_map[rm] << 16)
60 #define VD(vd) (freg_map[vd])
61 #define VT(vt) (freg_map[vt])
62 #define VN(vn) (freg_map[vn] << 5)
63 #define VM(vm) (freg_map[vm] << 16)
65 /* --------------------------------------------------------------------- */
66 /* Instrucion forms */
67 /* --------------------------------------------------------------------- */
69 #define ADC 0x9a000000
70 #define ADD 0x8b000000
71 #define ADDE 0x8b200000
72 #define ADDI 0x91000000
73 #define AND 0x8a000000
74 #define ANDI 0x92000000
75 #define ASRV 0x9ac02800
76 #define B 0x14000000
77 #define B_CC 0x54000000
78 #define BL 0x94000000
79 #define BLR 0xd63f0000
80 #define BR 0xd61f0000
81 #define BRK 0xd4200000
82 #define CBZ 0xb4000000
83 #define CLZ 0xdac01000
84 #define CSEL 0x9a800000
85 #define CSINC 0x9a800400
86 #define EOR 0xca000000
87 #define EORI 0xd2000000
88 #define FABS 0x1e60c000
89 #define FADD 0x1e602800
90 #define FCMP 0x1e602000
91 #define FCVT 0x1e224000
92 #define FCVTZS 0x9e780000
93 #define FDIV 0x1e601800
94 #define FMOV 0x1e604000
95 #define FMUL 0x1e600800
96 #define FNEG 0x1e614000
97 #define FSUB 0x1e603800
98 #define LDRI 0xf9400000
99 #define LDP 0xa9400000
100 #define LDP_PRE 0xa9c00000
101 #define LDR_PRE 0xf8400c00
102 #define LSLV 0x9ac02000
103 #define LSRV 0x9ac02400
104 #define MADD 0x9b000000
105 #define MOVK 0xf2800000
106 #define MOVN 0x92800000
107 #define MOVZ 0xd2800000
108 #define NOP 0xd503201f
109 #define ORN 0xaa200000
110 #define ORR 0xaa000000
111 #define ORRI 0xb2000000
112 #define RET 0xd65f0000
113 #define SBC 0xda000000
114 #define SBFM 0x93000000
115 #define SCVTF 0x9e620000
116 #define SDIV 0x9ac00c00
117 #define SMADDL 0x9b200000
118 #define SMULH 0x9b403c00
119 #define STP 0xa9000000
120 #define STP_PRE 0xa9800000
121 #define STRB 0x38206800
122 #define STRBI 0x39000000
123 #define STRI 0xf9000000
124 #define STR_FI 0x3d000000
125 #define STR_FR 0x3c206800
126 #define STUR_FI 0x3c000000
127 #define STURBI 0x38000000
128 #define SUB 0xcb000000
129 #define SUBI 0xd1000000
130 #define SUBS 0xeb000000
131 #define UBFM 0xd3000000
132 #define UDIV 0x9ac00800
133 #define UMULH 0x9bc03c00
135 /* dest_reg is the absolute name of the register
136 Useful for reordering instructions in the delay slot. */
137 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
139 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
140 FAIL_IF(!ptr);
141 *ptr = ins;
142 compiler->size++;
143 return SLJIT_SUCCESS;
146 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
148 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
149 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21)));
150 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21)));
151 return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21));
154 static SLJIT_INLINE sljit_sw detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
156 sljit_sw diff;
157 sljit_uw target_addr;
159 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
160 jump->flags |= PATCH_ABS64;
161 return 0;
164 if (jump->flags & JUMP_ADDR)
165 target_addr = jump->u.target;
166 else {
167 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
168 target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
171 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
173 if (jump->flags & IS_COND) {
174 diff += sizeof(sljit_ins);
175 if (diff <= 0xfffff && diff >= -0x100000) {
176 code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
177 jump->addr -= sizeof(sljit_ins);
178 jump->flags |= PATCH_COND;
179 return 5;
181 diff -= sizeof(sljit_ins);
184 if (diff <= 0x7ffffff && diff >= -0x8000000) {
185 jump->flags |= PATCH_B;
186 return 4;
189 if (target_addr < 0x100000000l) {
190 if (jump->flags & IS_COND)
191 code_ptr[-5] -= (2 << 5);
192 code_ptr[-2] = code_ptr[0];
193 return 2;
196 if (target_addr < 0x1000000000000l) {
197 if (jump->flags & IS_COND)
198 code_ptr[-5] -= (1 << 5);
199 jump->flags |= PATCH_ABS48;
200 code_ptr[-1] = code_ptr[0];
201 return 1;
204 jump->flags |= PATCH_ABS64;
205 return 0;
208 static SLJIT_INLINE sljit_sw put_label_get_length(struct sljit_put_label *put_label, sljit_uw max_label)
210 if (max_label < 0x100000000l) {
211 put_label->flags = 0;
212 return 2;
215 if (max_label < 0x1000000000000l) {
216 put_label->flags = 1;
217 return 1;
220 put_label->flags = 2;
221 return 0;
224 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
226 struct sljit_memory_fragment *buf;
227 sljit_ins *code;
228 sljit_ins *code_ptr;
229 sljit_ins *buf_ptr;
230 sljit_ins *buf_end;
231 sljit_uw word_count;
232 sljit_uw next_addr;
233 sljit_sw executable_offset;
234 sljit_uw addr;
235 sljit_s32 dst;
237 struct sljit_label *label;
238 struct sljit_jump *jump;
239 struct sljit_const *const_;
240 struct sljit_put_label *put_label;
242 CHECK_ERROR_PTR();
243 CHECK_PTR(check_sljit_generate_code(compiler));
244 reverse_buf(compiler);
246 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins), compiler->exec_allocator_data);
247 PTR_FAIL_WITH_EXEC_IF(code);
248 buf = compiler->buf;
250 code_ptr = code;
251 word_count = 0;
252 next_addr = 0;
253 executable_offset = SLJIT_EXEC_OFFSET(code);
255 label = compiler->labels;
256 jump = compiler->jumps;
257 const_ = compiler->consts;
258 put_label = compiler->put_labels;
260 do {
261 buf_ptr = (sljit_ins*)buf->memory;
262 buf_end = buf_ptr + (buf->used_size >> 2);
263 do {
264 *code_ptr = *buf_ptr++;
265 if (next_addr == word_count) {
266 SLJIT_ASSERT(!label || label->size >= word_count);
267 SLJIT_ASSERT(!jump || jump->addr >= word_count);
268 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
269 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
271 /* These structures are ordered by their address. */
272 if (label && label->size == word_count) {
273 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
274 label->size = code_ptr - code;
275 label = label->next;
277 if (jump && jump->addr == word_count) {
278 jump->addr = (sljit_uw)(code_ptr - 4);
279 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
280 jump = jump->next;
282 if (const_ && const_->addr == word_count) {
283 const_->addr = (sljit_uw)code_ptr;
284 const_ = const_->next;
286 if (put_label && put_label->addr == word_count) {
287 SLJIT_ASSERT(put_label->label);
288 put_label->addr = (sljit_uw)(code_ptr - 3);
289 code_ptr -= put_label_get_length(put_label, (sljit_uw)(SLJIT_ADD_EXEC_OFFSET(code, executable_offset) + put_label->label->size));
290 put_label = put_label->next;
292 next_addr = compute_next_addr(label, jump, const_, put_label);
294 code_ptr ++;
295 word_count ++;
296 } while (buf_ptr < buf_end);
298 buf = buf->next;
299 } while (buf);
301 if (label && label->size == word_count) {
302 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
303 label->size = code_ptr - code;
304 label = label->next;
307 SLJIT_ASSERT(!label);
308 SLJIT_ASSERT(!jump);
309 SLJIT_ASSERT(!const_);
310 SLJIT_ASSERT(!put_label);
311 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
313 jump = compiler->jumps;
314 while (jump) {
315 do {
316 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
317 buf_ptr = (sljit_ins *)jump->addr;
319 if (jump->flags & PATCH_B) {
320 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
321 SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000);
322 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff);
323 if (jump->flags & IS_COND)
324 buf_ptr[-1] -= (4 << 5);
325 break;
327 if (jump->flags & PATCH_COND) {
328 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
329 SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000);
330 buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5);
331 break;
334 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
335 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
337 dst = buf_ptr[0] & 0x1f;
338 buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5);
339 buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21);
340 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
341 buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21);
342 if (jump->flags & PATCH_ABS64)
343 buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21);
344 } while (0);
345 jump = jump->next;
348 put_label = compiler->put_labels;
349 while (put_label) {
350 addr = put_label->label->addr;
351 buf_ptr = (sljit_ins *)put_label->addr;
353 buf_ptr[0] |= (addr & 0xffff) << 5;
354 buf_ptr[1] |= ((addr >> 16) & 0xffff) << 5;
356 if (put_label->flags >= 1)
357 buf_ptr[2] |= ((addr >> 32) & 0xffff) << 5;
359 if (put_label->flags >= 2)
360 buf_ptr[3] |= ((addr >> 48) & 0xffff) << 5;
362 put_label = put_label->next;
365 compiler->error = SLJIT_ERR_COMPILED;
366 compiler->executable_offset = executable_offset;
367 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
369 code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
370 code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
372 SLJIT_CACHE_FLUSH(code, code_ptr);
373 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
374 return code;
377 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
379 switch (feature_type) {
380 case SLJIT_HAS_FPU:
381 #ifdef SLJIT_IS_FPU_AVAILABLE
382 return SLJIT_IS_FPU_AVAILABLE;
383 #else
384 /* Available by default. */
385 return 1;
386 #endif
388 case SLJIT_HAS_CLZ:
389 case SLJIT_HAS_CMOV:
390 case SLJIT_HAS_PREFETCH:
391 return 1;
393 default:
394 return 0;
398 /* --------------------------------------------------------------------- */
399 /* Core code generator functions. */
400 /* --------------------------------------------------------------------- */
402 #define COUNT_TRAILING_ZERO(value, result) \
403 result = 0; \
404 if (!(value & 0xffffffff)) { \
405 result += 32; \
406 value >>= 32; \
408 if (!(value & 0xffff)) { \
409 result += 16; \
410 value >>= 16; \
412 if (!(value & 0xff)) { \
413 result += 8; \
414 value >>= 8; \
416 if (!(value & 0xf)) { \
417 result += 4; \
418 value >>= 4; \
420 if (!(value & 0x3)) { \
421 result += 2; \
422 value >>= 2; \
424 if (!(value & 0x1)) { \
425 result += 1; \
426 value >>= 1; \
429 #define LOGICAL_IMM_CHECK 0x100
431 static sljit_ins logical_imm(sljit_sw imm, sljit_s32 len)
433 sljit_s32 negated, ones, right;
434 sljit_uw mask, uimm;
435 sljit_ins ins;
437 if (len & LOGICAL_IMM_CHECK) {
438 len &= ~LOGICAL_IMM_CHECK;
439 if (len == 32 && (imm == 0 || imm == -1))
440 return 0;
441 if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
442 return 0;
445 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
446 || (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
448 uimm = (sljit_uw)imm;
449 while (1) {
450 if (len <= 0) {
451 SLJIT_UNREACHABLE();
452 return 0;
455 mask = ((sljit_uw)1 << len) - 1;
456 if ((uimm & mask) != ((uimm >> len) & mask))
457 break;
458 len >>= 1;
461 len <<= 1;
463 negated = 0;
464 if (uimm & 0x1) {
465 negated = 1;
466 uimm = ~uimm;
469 if (len < 64)
470 uimm &= ((sljit_uw)1 << len) - 1;
472 /* Unsigned right shift. */
473 COUNT_TRAILING_ZERO(uimm, right);
475 /* Signed shift. We also know that the highest bit is set. */
476 imm = (sljit_sw)~uimm;
477 SLJIT_ASSERT(imm < 0);
479 COUNT_TRAILING_ZERO(imm, ones);
481 if (~imm)
482 return 0;
484 if (len == 64)
485 ins = 1 << 22;
486 else
487 ins = (0x3f - ((len << 1) - 1)) << 10;
489 if (negated)
490 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
492 return ins | ((ones - 1) << 10) | ((len - right) << 16);
495 #undef COUNT_TRAILING_ZERO
497 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
499 sljit_uw imm = (sljit_uw)simm;
500 sljit_s32 i, zeros, ones, first;
501 sljit_ins bitmask;
503 /* Handling simple immediates first. */
504 if (imm <= 0xffff)
505 return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
507 if (simm < 0 && simm >= -0x10000)
508 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
510 if (imm <= 0xffffffffl) {
511 if ((imm & 0xffff) == 0)
512 return push_inst(compiler, MOVZ | RD(dst) | ((imm >> 16) << 5) | (1 << 21));
513 if ((imm & 0xffff0000l) == 0xffff0000)
514 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5));
515 if ((imm & 0xffff) == 0xffff)
516 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
518 bitmask = logical_imm(simm, 16);
519 if (bitmask != 0)
520 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
522 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
523 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
526 bitmask = logical_imm(simm, 32);
527 if (bitmask != 0)
528 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
530 if (simm < 0 && simm >= -0x100000000l) {
531 if ((imm & 0xffff) == 0xffff)
532 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
534 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)));
535 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
538 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
540 zeros = 0;
541 ones = 0;
542 for (i = 4; i > 0; i--) {
543 if ((simm & 0xffff) == 0)
544 zeros++;
545 if ((simm & 0xffff) == 0xffff)
546 ones++;
547 simm >>= 16;
550 simm = (sljit_sw)imm;
551 first = 1;
552 if (ones > zeros) {
553 simm = ~simm;
554 for (i = 0; i < 4; i++) {
555 if (!(simm & 0xffff)) {
556 simm >>= 16;
557 continue;
559 if (first) {
560 first = 0;
561 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
563 else
564 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
565 simm >>= 16;
567 return SLJIT_SUCCESS;
570 for (i = 0; i < 4; i++) {
571 if (!(simm & 0xffff)) {
572 simm >>= 16;
573 continue;
575 if (first) {
576 first = 0;
577 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
579 else
580 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
581 simm >>= 16;
583 return SLJIT_SUCCESS;
586 #define ARG1_IMM 0x0010000
587 #define ARG2_IMM 0x0020000
588 #define INT_OP 0x0040000
589 #define SET_FLAGS 0x0080000
590 #define UNUSED_RETURN 0x0100000
592 #define CHECK_FLAGS(flag_bits) \
593 if (flags & SET_FLAGS) { \
594 inv_bits |= flag_bits; \
595 if (flags & UNUSED_RETURN) \
596 dst = TMP_ZERO; \
599 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
601 /* dst must be register, TMP_REG1
602 arg1 must be register, TMP_REG1, imm
603 arg2 must be register, TMP_REG2, imm */
604 sljit_ins inv_bits = (flags & INT_OP) ? W_OP : 0;
605 sljit_ins inst_bits;
606 sljit_s32 op = (flags & 0xffff);
607 sljit_s32 reg;
608 sljit_sw imm, nimm;
610 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
611 /* Both are immediates. */
612 flags &= ~ARG1_IMM;
613 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
614 arg1 = TMP_ZERO;
615 else {
616 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
617 arg1 = TMP_REG1;
621 if (flags & (ARG1_IMM | ARG2_IMM)) {
622 reg = (flags & ARG2_IMM) ? arg1 : arg2;
623 imm = (flags & ARG2_IMM) ? arg2 : arg1;
625 switch (op) {
626 case SLJIT_MUL:
627 case SLJIT_NEG:
628 case SLJIT_CLZ:
629 case SLJIT_ADDC:
630 case SLJIT_SUBC:
631 /* No form with immediate operand (except imm 0, which
632 is represented by a ZERO register). */
633 break;
634 case SLJIT_MOV:
635 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
636 return load_immediate(compiler, dst, imm);
637 case SLJIT_NOT:
638 SLJIT_ASSERT(flags & ARG2_IMM);
639 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
640 goto set_flags;
641 case SLJIT_SUB:
642 if (flags & ARG1_IMM)
643 break;
644 imm = -imm;
645 /* Fall through. */
646 case SLJIT_ADD:
647 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
648 if (imm == 0) {
649 CHECK_FLAGS(1 << 29);
650 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
652 if (imm > 0 && imm <= 0xfff) {
653 CHECK_FLAGS(1 << 29);
654 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10));
656 nimm = -imm;
657 if (nimm > 0 && nimm <= 0xfff) {
658 CHECK_FLAGS(1 << 29);
659 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10));
661 if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
662 CHECK_FLAGS(1 << 29);
663 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22));
665 if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
666 CHECK_FLAGS(1 << 29);
667 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22));
669 if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
670 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)));
671 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10));
673 if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
674 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)));
675 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10));
677 break;
678 case SLJIT_AND:
679 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
680 if (!inst_bits)
681 break;
682 CHECK_FLAGS(3 << 29);
683 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
684 case SLJIT_OR:
685 case SLJIT_XOR:
686 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
687 if (!inst_bits)
688 break;
689 if (op == SLJIT_OR)
690 inst_bits |= ORRI;
691 else
692 inst_bits |= EORI;
693 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
694 goto set_flags;
695 case SLJIT_SHL:
696 if (flags & ARG1_IMM)
697 break;
698 if (flags & INT_OP) {
699 imm &= 0x1f;
700 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
702 else {
703 imm &= 0x3f;
704 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
706 goto set_flags;
707 case SLJIT_LSHR:
708 case SLJIT_ASHR:
709 if (flags & ARG1_IMM)
710 break;
711 if (op == SLJIT_ASHR)
712 inv_bits |= 1 << 30;
713 if (flags & INT_OP) {
714 imm &= 0x1f;
715 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (imm << 16) | (31 << 10)));
717 else {
718 imm &= 0x3f;
719 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
721 goto set_flags;
722 default:
723 SLJIT_UNREACHABLE();
724 break;
727 if (flags & ARG2_IMM) {
728 if (arg2 == 0)
729 arg2 = TMP_ZERO;
730 else {
731 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
732 arg2 = TMP_REG2;
735 else {
736 if (arg1 == 0)
737 arg1 = TMP_ZERO;
738 else {
739 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
740 arg1 = TMP_REG1;
745 /* Both arguments are registers. */
746 switch (op) {
747 case SLJIT_MOV:
748 case SLJIT_MOV_P:
749 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
750 if (dst == arg2)
751 return SLJIT_SUCCESS;
752 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
753 case SLJIT_MOV_U8:
754 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
755 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (7 << 10));
756 case SLJIT_MOV_S8:
757 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
758 if (!(flags & INT_OP))
759 inv_bits |= 1 << 22;
760 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
761 case SLJIT_MOV_U16:
762 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
763 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (15 << 10));
764 case SLJIT_MOV_S16:
765 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
766 if (!(flags & INT_OP))
767 inv_bits |= 1 << 22;
768 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
769 case SLJIT_MOV_U32:
770 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
771 if ((flags & INT_OP) && dst == arg2)
772 return SLJIT_SUCCESS;
773 return push_inst(compiler, (ORR ^ W_OP) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
774 case SLJIT_MOV_S32:
775 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
776 if ((flags & INT_OP) && dst == arg2)
777 return SLJIT_SUCCESS;
778 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
779 case SLJIT_NOT:
780 SLJIT_ASSERT(arg1 == TMP_REG1);
781 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
782 break; /* Set flags. */
783 case SLJIT_NEG:
784 SLJIT_ASSERT(arg1 == TMP_REG1);
785 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
786 if (flags & SET_FLAGS)
787 inv_bits |= 1 << 29;
788 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
789 case SLJIT_CLZ:
790 SLJIT_ASSERT(arg1 == TMP_REG1);
791 return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
792 case SLJIT_ADD:
793 CHECK_FLAGS(1 << 29);
794 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
795 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
796 case SLJIT_ADDC:
797 CHECK_FLAGS(1 << 29);
798 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
799 case SLJIT_SUB:
800 CHECK_FLAGS(1 << 29);
801 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
802 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
803 case SLJIT_SUBC:
804 CHECK_FLAGS(1 << 29);
805 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
806 case SLJIT_MUL:
807 compiler->status_flags_state = 0;
808 if (!(flags & SET_FLAGS))
809 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
810 if (flags & INT_OP) {
811 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
812 FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
813 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
815 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
816 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
817 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
818 case SLJIT_AND:
819 CHECK_FLAGS(3 << 29);
820 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
821 case SLJIT_OR:
822 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
823 break; /* Set flags. */
824 case SLJIT_XOR:
825 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
826 break; /* Set flags. */
827 case SLJIT_SHL:
828 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
829 break; /* Set flags. */
830 case SLJIT_LSHR:
831 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
832 break; /* Set flags. */
833 case SLJIT_ASHR:
834 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
835 break; /* Set flags. */
836 default:
837 SLJIT_UNREACHABLE();
838 return SLJIT_SUCCESS;
841 set_flags:
842 if (flags & SET_FLAGS)
843 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
844 return SLJIT_SUCCESS;
847 #define STORE 0x10
848 #define SIGNED 0x20
850 #define BYTE_SIZE 0x0
851 #define HALF_SIZE 0x1
852 #define INT_SIZE 0x2
853 #define WORD_SIZE 0x3
855 #define MEM_SIZE_SHIFT(flags) ((flags) & 0x3)
857 static sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
858 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
860 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
861 sljit_u32 type = (shift << 30);
863 if (!(flags & STORE))
864 type |= (flags & SIGNED) ? 0x00800000 : 0x00400000;
866 SLJIT_ASSERT(arg & SLJIT_MEM);
868 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
869 argw &= 0x3;
871 if (argw == 0 || argw == shift)
872 return push_inst(compiler, STRB | type | RT(reg)
873 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
875 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw << 10)));
876 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg));
879 arg &= REG_MASK;
881 if (arg == SLJIT_UNUSED) {
882 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~(0xfff << shift)));
884 argw = (argw >> shift) & 0xfff;
886 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | (argw << 10));
889 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
890 if ((argw >> shift) <= 0xfff) {
891 return push_inst(compiler, STRBI | type | RT(reg) | RN(arg) | (argw << (10 - shift)));
894 if (argw <= 0xffffff) {
895 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_reg) | RN(arg) | ((argw >> 12) << 10)));
897 argw = ((argw & 0xfff) >> shift);
898 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | (argw << 10));
902 if (argw <= 255 && argw >= -256)
903 return push_inst(compiler, STURBI | type | RT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
905 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
907 return push_inst(compiler, STRB | type | RT(reg) | RN(arg) | RM(tmp_reg));
910 /* --------------------------------------------------------------------- */
911 /* Entry, exit */
912 /* --------------------------------------------------------------------- */
914 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
915 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
916 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
918 sljit_s32 args, i, tmp, offs, prev, saved_regs_size;
920 CHECK_ERROR();
921 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
922 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
924 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
925 if (saved_regs_size & 0x8)
926 saved_regs_size += sizeof(sljit_sw);
928 local_size = (local_size + 15) & ~0xf;
929 compiler->local_size = local_size + saved_regs_size;
931 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
932 | RN(SLJIT_SP) | ((-(saved_regs_size >> 3) & 0x7f) << 15)));
934 #ifdef _WIN32
935 if (local_size >= 4096)
936 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
937 else if (local_size > 256)
938 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | (local_size << 10)));
939 #endif
941 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
942 prev = -1;
943 offs = 2 << 15;
944 for (i = SLJIT_S0; i >= tmp; i--) {
945 if (prev == -1) {
946 prev = i;
947 continue;
949 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
950 offs += 2 << 15;
951 prev = -1;
954 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
955 if (prev == -1) {
956 prev = i;
957 continue;
959 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
960 offs += 2 << 15;
961 prev = -1;
964 if (prev != -1)
965 FAIL_IF(push_inst(compiler, STRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
968 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_FP) | RN(SLJIT_SP) | (0 << 10)));
970 args = get_arg_count(arg_types);
972 if (args >= 1)
973 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0) | RN(TMP_ZERO) | RM(SLJIT_R0)));
974 if (args >= 2)
975 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S1) | RN(TMP_ZERO) | RM(SLJIT_R1)));
976 if (args >= 3)
977 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S2) | RN(TMP_ZERO) | RM(SLJIT_R2)));
979 #ifdef _WIN32
980 if (local_size >= 4096) {
981 if (local_size < 4 * 4096) {
982 /* No need for a loop. */
983 if (local_size >= 2 * 4096) {
984 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
985 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
986 local_size -= 4096;
989 if (local_size >= 2 * 4096) {
990 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
991 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
992 local_size -= 4096;
995 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
996 local_size -= 4096;
998 else {
999 FAIL_IF(push_inst(compiler, MOVZ | RD(TMP_REG2) | (((local_size >> 12) - 1) << 5)));
1000 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1001 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
1002 FAIL_IF(push_inst(compiler, SUBI | (1 << 29) | RD(TMP_REG2) | RN(TMP_REG2) | (1 << 10)));
1003 FAIL_IF(push_inst(compiler, B_CC | ((((sljit_ins) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1004 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1006 local_size &= 0xfff;
1009 if (local_size > 256) {
1010 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (local_size << 10)));
1011 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1013 else if (local_size > 0)
1014 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(TMP_REG1) | ((-local_size & 0x1ff) << 12)));
1016 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1018 else if (local_size > 256) {
1019 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1020 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1022 else if (local_size > 0)
1023 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(SLJIT_SP) | ((-local_size & 0x1ff) << 12)));
1025 #else /* !_WIN32 */
1027 /* The local_size does not include saved registers size. */
1028 if (local_size > 0xfff) {
1029 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1030 local_size &= 0xfff;
1032 if (local_size != 0)
1033 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (local_size << 10)));
1035 #endif /* _WIN32 */
1037 return SLJIT_SUCCESS;
1040 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1041 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1042 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1044 sljit_s32 saved_regs_size;
1046 CHECK_ERROR();
1047 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1048 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1050 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
1051 if (saved_regs_size & 0x8)
1052 saved_regs_size += sizeof(sljit_sw);
1054 compiler->local_size = saved_regs_size + ((local_size + 15) & ~0xf);
1055 return SLJIT_SUCCESS;
1058 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1060 sljit_s32 local_size;
1061 sljit_s32 i, tmp, offs, prev, saved_regs_size;
1063 CHECK_ERROR();
1064 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1066 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1068 saved_regs_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 2);
1069 if (saved_regs_size & 0x8)
1070 saved_regs_size += sizeof(sljit_sw);
1072 local_size = compiler->local_size - saved_regs_size;
1074 /* Load LR as early as possible. */
1075 if (local_size == 0)
1076 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1077 else if (local_size < 63 * sizeof(sljit_sw)) {
1078 FAIL_IF(push_inst(compiler, LDP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1079 | RN(SLJIT_SP) | (local_size << (15 - 3))));
1081 else {
1082 if (local_size > 0xfff) {
1083 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1084 local_size &= 0xfff;
1086 if (local_size)
1087 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (local_size << 10)));
1089 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1092 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1093 prev = -1;
1094 offs = 2 << 15;
1095 for (i = SLJIT_S0; i >= tmp; i--) {
1096 if (prev == -1) {
1097 prev = i;
1098 continue;
1100 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1101 offs += 2 << 15;
1102 prev = -1;
1105 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1106 if (prev == -1) {
1107 prev = i;
1108 continue;
1110 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1111 offs += 2 << 15;
1112 prev = -1;
1115 if (prev != -1)
1116 FAIL_IF(push_inst(compiler, LDRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
1118 /* These two can be executed in parallel. */
1119 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (saved_regs_size << 10)));
1120 return push_inst(compiler, RET | RN(TMP_LR));
1123 /* --------------------------------------------------------------------- */
1124 /* Operators */
1125 /* --------------------------------------------------------------------- */
1127 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1129 sljit_ins inv_bits = (op & SLJIT_I32_OP) ? W_OP : 0;
1131 CHECK_ERROR();
1132 CHECK(check_sljit_emit_op0(compiler, op));
1134 op = GET_OPCODE(op);
1135 switch (op) {
1136 case SLJIT_BREAKPOINT:
1137 return push_inst(compiler, BRK);
1138 case SLJIT_NOP:
1139 return push_inst(compiler, NOP);
1140 case SLJIT_LMUL_UW:
1141 case SLJIT_LMUL_SW:
1142 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1143 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1144 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1145 case SLJIT_DIVMOD_UW:
1146 case SLJIT_DIVMOD_SW:
1147 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1148 FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1149 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1150 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1151 case SLJIT_DIV_UW:
1152 case SLJIT_DIV_SW:
1153 return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1154 case SLJIT_ENDBR:
1155 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1156 return SLJIT_SUCCESS;
1159 return SLJIT_SUCCESS;
1162 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1163 sljit_s32 dst, sljit_sw dstw,
1164 sljit_s32 src, sljit_sw srcw)
1166 sljit_s32 dst_r, flags, mem_flags;
1167 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1169 CHECK_ERROR();
1170 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1171 ADJUST_LOCAL_OFFSET(dst, dstw);
1172 ADJUST_LOCAL_OFFSET(src, srcw);
1174 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1176 op = GET_OPCODE(op);
1177 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1178 /* Both operands are registers. */
1179 if (dst_r != TMP_REG1 && FAST_IS_REG(src))
1180 return emit_op_imm(compiler, op | ((op_flags & SLJIT_I32_OP) ? INT_OP : 0), dst_r, TMP_REG1, src);
1182 switch (op) {
1183 case SLJIT_MOV:
1184 case SLJIT_MOV_P:
1185 mem_flags = WORD_SIZE;
1186 break;
1187 case SLJIT_MOV_U8:
1188 mem_flags = BYTE_SIZE;
1189 if (src & SLJIT_IMM)
1190 srcw = (sljit_u8)srcw;
1191 break;
1192 case SLJIT_MOV_S8:
1193 mem_flags = BYTE_SIZE | SIGNED;
1194 if (src & SLJIT_IMM)
1195 srcw = (sljit_s8)srcw;
1196 break;
1197 case SLJIT_MOV_U16:
1198 mem_flags = HALF_SIZE;
1199 if (src & SLJIT_IMM)
1200 srcw = (sljit_u16)srcw;
1201 break;
1202 case SLJIT_MOV_S16:
1203 mem_flags = HALF_SIZE | SIGNED;
1204 if (src & SLJIT_IMM)
1205 srcw = (sljit_s16)srcw;
1206 break;
1207 case SLJIT_MOV_U32:
1208 mem_flags = INT_SIZE;
1209 if (src & SLJIT_IMM)
1210 srcw = (sljit_u32)srcw;
1211 break;
1212 case SLJIT_MOV_S32:
1213 mem_flags = INT_SIZE | SIGNED;
1214 if (src & SLJIT_IMM)
1215 srcw = (sljit_s32)srcw;
1216 break;
1217 default:
1218 SLJIT_UNREACHABLE();
1219 mem_flags = 0;
1220 break;
1223 if (src & SLJIT_IMM)
1224 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1225 else if (!(src & SLJIT_MEM))
1226 dst_r = src;
1227 else
1228 FAIL_IF(emit_op_mem(compiler, mem_flags, dst_r, src, srcw, TMP_REG1));
1230 if (dst & SLJIT_MEM)
1231 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1232 return SLJIT_SUCCESS;
1235 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1236 mem_flags = WORD_SIZE;
1238 if (op_flags & SLJIT_I32_OP) {
1239 flags |= INT_OP;
1240 mem_flags = INT_SIZE;
1243 if (dst == SLJIT_UNUSED)
1244 flags |= UNUSED_RETURN;
1246 if (src & SLJIT_MEM) {
1247 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src, srcw, TMP_REG2));
1248 src = TMP_REG2;
1251 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, src);
1253 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1254 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1255 return SLJIT_SUCCESS;
1258 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1259 sljit_s32 dst, sljit_sw dstw,
1260 sljit_s32 src1, sljit_sw src1w,
1261 sljit_s32 src2, sljit_sw src2w)
1263 sljit_s32 dst_r, flags, mem_flags;
1265 CHECK_ERROR();
1266 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1267 ADJUST_LOCAL_OFFSET(dst, dstw);
1268 ADJUST_LOCAL_OFFSET(src1, src1w);
1269 ADJUST_LOCAL_OFFSET(src2, src2w);
1271 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1272 return SLJIT_SUCCESS;
1274 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1275 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1276 mem_flags = WORD_SIZE;
1278 if (op & SLJIT_I32_OP) {
1279 flags |= INT_OP;
1280 mem_flags = INT_SIZE;
1283 if (dst == SLJIT_UNUSED)
1284 flags |= UNUSED_RETURN;
1286 if (src1 & SLJIT_MEM) {
1287 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, src1, src1w, TMP_REG1));
1288 src1 = TMP_REG1;
1291 if (src2 & SLJIT_MEM) {
1292 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src2, src2w, TMP_REG2));
1293 src2 = TMP_REG2;
1296 if (src1 & SLJIT_IMM)
1297 flags |= ARG1_IMM;
1298 else
1299 src1w = src1;
1301 if (src2 & SLJIT_IMM)
1302 flags |= ARG2_IMM;
1303 else
1304 src2w = src2;
1306 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1308 if (dst & SLJIT_MEM)
1309 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1310 return SLJIT_SUCCESS;
1313 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1314 sljit_s32 src, sljit_sw srcw)
1316 CHECK_ERROR();
1317 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1318 ADJUST_LOCAL_OFFSET(src, srcw);
1320 switch (op) {
1321 case SLJIT_FAST_RETURN:
1322 if (FAST_IS_REG(src))
1323 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1324 else
1325 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw, TMP_REG1));
1327 return push_inst(compiler, RET | RN(TMP_LR));
1328 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1329 return SLJIT_SUCCESS;
1330 case SLJIT_PREFETCH_L1:
1331 case SLJIT_PREFETCH_L2:
1332 case SLJIT_PREFETCH_L3:
1333 case SLJIT_PREFETCH_ONCE:
1334 SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1336 /* The reg_map[op] should provide the appropriate constant. */
1337 if (op == SLJIT_PREFETCH_L1)
1338 op = 1;
1339 else if (op == SLJIT_PREFETCH_L2)
1340 op = 3;
1341 else if (op == SLJIT_PREFETCH_L3)
1342 op = 5;
1343 else
1344 op = 2;
1346 /* Signed word sized load is the prefetch instruction. */
1347 return emit_op_mem(compiler, WORD_SIZE | SIGNED, op, src, srcw, TMP_REG1);
1350 return SLJIT_SUCCESS;
1353 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1355 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1356 return reg_map[reg];
1359 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1361 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1362 return freg_map[reg];
1365 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1366 void *instruction, sljit_s32 size)
1368 CHECK_ERROR();
1369 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1371 return push_inst(compiler, *(sljit_ins*)instruction);
1374 /* --------------------------------------------------------------------- */
1375 /* Floating point operators */
1376 /* --------------------------------------------------------------------- */
1378 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1380 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1381 sljit_ins type = (shift << 30);
1383 SLJIT_ASSERT(arg & SLJIT_MEM);
1385 if (!(flags & STORE))
1386 type |= 0x00400000;
1388 if (arg & OFFS_REG_MASK) {
1389 argw &= 3;
1390 if (argw == 0 || argw == shift)
1391 return push_inst(compiler, STR_FR | type | VT(reg)
1392 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1394 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw << 10)));
1395 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1));
1398 arg &= REG_MASK;
1400 if (arg == SLJIT_UNUSED) {
1401 FAIL_IF(load_immediate(compiler, TMP_REG1, argw & ~(0xfff << shift)));
1403 argw = (argw >> shift) & 0xfff;
1405 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | (argw << 10));
1408 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
1409 if ((argw >> shift) <= 0xfff)
1410 return push_inst(compiler, STR_FI | type | VT(reg) | RN(arg) | (argw << (10 - shift)));
1412 if (argw <= 0xffffff) {
1413 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(TMP_REG1) | RN(arg) | ((argw >> 12) << 10)));
1415 argw = ((argw & 0xfff) >> shift);
1416 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | (argw << 10));
1420 if (argw <= 255 && argw >= -256)
1421 return push_inst(compiler, STUR_FI | type | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
1423 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1424 return push_inst(compiler, STR_FR | type | VT(reg) | RN(arg) | RM(TMP_REG1));
1427 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1428 sljit_s32 dst, sljit_sw dstw,
1429 sljit_s32 src, sljit_sw srcw)
1431 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1432 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1434 if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1435 inv_bits |= W_OP;
1437 if (src & SLJIT_MEM) {
1438 emit_fop_mem(compiler, (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1439 src = TMP_FREG1;
1442 FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1444 if (dst & SLJIT_MEM)
1445 return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1446 return SLJIT_SUCCESS;
1449 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1450 sljit_s32 dst, sljit_sw dstw,
1451 sljit_s32 src, sljit_sw srcw)
1453 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1454 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1456 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1457 inv_bits |= W_OP;
1459 if (src & SLJIT_MEM) {
1460 emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw, TMP_REG1);
1461 src = TMP_REG1;
1462 } else if (src & SLJIT_IMM) {
1463 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1464 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1465 srcw = (sljit_s32)srcw;
1466 #endif
1467 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1468 src = TMP_REG1;
1471 FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1473 if (dst & SLJIT_MEM)
1474 return emit_fop_mem(compiler, ((op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1475 return SLJIT_SUCCESS;
1478 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1479 sljit_s32 src1, sljit_sw src1w,
1480 sljit_s32 src2, sljit_sw src2w)
1482 sljit_s32 mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1483 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1485 if (src1 & SLJIT_MEM) {
1486 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1487 src1 = TMP_FREG1;
1490 if (src2 & SLJIT_MEM) {
1491 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1492 src2 = TMP_FREG2;
1495 return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1498 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1499 sljit_s32 dst, sljit_sw dstw,
1500 sljit_s32 src, sljit_sw srcw)
1502 sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1503 sljit_ins inv_bits;
1505 CHECK_ERROR();
1507 SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x1) == WORD_SIZE, must_be_one_bit_difference);
1508 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1510 inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1511 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1513 if (src & SLJIT_MEM) {
1514 emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x1) : mem_flags, dst_r, src, srcw);
1515 src = dst_r;
1518 switch (GET_OPCODE(op)) {
1519 case SLJIT_MOV_F64:
1520 if (src != dst_r) {
1521 if (dst_r != TMP_FREG1)
1522 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1523 else
1524 dst_r = src;
1526 break;
1527 case SLJIT_NEG_F64:
1528 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1529 break;
1530 case SLJIT_ABS_F64:
1531 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1532 break;
1533 case SLJIT_CONV_F64_FROM_F32:
1534 FAIL_IF(push_inst(compiler, FCVT | ((op & SLJIT_F32_OP) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1535 break;
1538 if (dst & SLJIT_MEM)
1539 return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1540 return SLJIT_SUCCESS;
1543 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1544 sljit_s32 dst, sljit_sw dstw,
1545 sljit_s32 src1, sljit_sw src1w,
1546 sljit_s32 src2, sljit_sw src2w)
1548 sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1549 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1551 CHECK_ERROR();
1552 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1553 ADJUST_LOCAL_OFFSET(dst, dstw);
1554 ADJUST_LOCAL_OFFSET(src1, src1w);
1555 ADJUST_LOCAL_OFFSET(src2, src2w);
1557 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1558 if (src1 & SLJIT_MEM) {
1559 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1560 src1 = TMP_FREG1;
1562 if (src2 & SLJIT_MEM) {
1563 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1564 src2 = TMP_FREG2;
1567 switch (GET_OPCODE(op)) {
1568 case SLJIT_ADD_F64:
1569 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1570 break;
1571 case SLJIT_SUB_F64:
1572 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1573 break;
1574 case SLJIT_MUL_F64:
1575 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1576 break;
1577 case SLJIT_DIV_F64:
1578 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1579 break;
1582 if (!(dst & SLJIT_MEM))
1583 return SLJIT_SUCCESS;
1584 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1587 /* --------------------------------------------------------------------- */
1588 /* Other instructions */
1589 /* --------------------------------------------------------------------- */
1591 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1593 CHECK_ERROR();
1594 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1595 ADJUST_LOCAL_OFFSET(dst, dstw);
1597 if (FAST_IS_REG(dst))
1598 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1600 /* Memory. */
1601 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw, TMP_REG1);
1604 /* --------------------------------------------------------------------- */
1605 /* Conditional instructions */
1606 /* --------------------------------------------------------------------- */
1608 static sljit_uw get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1610 switch (type) {
1611 case SLJIT_EQUAL:
1612 case SLJIT_EQUAL_F64:
1613 return 0x1;
1615 case SLJIT_NOT_EQUAL:
1616 case SLJIT_NOT_EQUAL_F64:
1617 return 0x0;
1619 case SLJIT_LESS:
1620 case SLJIT_LESS_F64:
1621 return 0x2;
1623 case SLJIT_GREATER_EQUAL:
1624 case SLJIT_GREATER_EQUAL_F64:
1625 return 0x3;
1627 case SLJIT_GREATER:
1628 case SLJIT_GREATER_F64:
1629 return 0x9;
1631 case SLJIT_LESS_EQUAL:
1632 case SLJIT_LESS_EQUAL_F64:
1633 return 0x8;
1635 case SLJIT_SIG_LESS:
1636 return 0xa;
1638 case SLJIT_SIG_GREATER_EQUAL:
1639 return 0xb;
1641 case SLJIT_SIG_GREATER:
1642 return 0xd;
1644 case SLJIT_SIG_LESS_EQUAL:
1645 return 0xc;
1647 case SLJIT_OVERFLOW:
1648 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1649 return 0x0;
1651 case SLJIT_UNORDERED_F64:
1652 return 0x7;
1654 case SLJIT_NOT_OVERFLOW:
1655 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1656 return 0x1;
1658 case SLJIT_ORDERED_F64:
1659 return 0x6;
1661 default:
1662 SLJIT_UNREACHABLE();
1663 return 0xe;
1667 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1669 struct sljit_label *label;
1671 CHECK_ERROR_PTR();
1672 CHECK_PTR(check_sljit_emit_label(compiler));
1674 if (compiler->last_label && compiler->last_label->size == compiler->size)
1675 return compiler->last_label;
1677 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1678 PTR_FAIL_IF(!label);
1679 set_label(label, compiler);
1680 return label;
1683 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1685 struct sljit_jump *jump;
1687 CHECK_ERROR_PTR();
1688 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1690 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1691 PTR_FAIL_IF(!jump);
1692 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1693 type &= 0xff;
1695 if (type < SLJIT_JUMP) {
1696 jump->flags |= IS_COND;
1697 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(compiler, type)));
1699 else if (type >= SLJIT_FAST_CALL)
1700 jump->flags |= IS_BL;
1702 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1703 jump->addr = compiler->size;
1704 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1706 return jump;
1709 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1710 sljit_s32 arg_types)
1712 CHECK_ERROR_PTR();
1713 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1715 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1716 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1717 compiler->skip_checks = 1;
1718 #endif
1720 return sljit_emit_jump(compiler, type);
1723 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1724 sljit_s32 src, sljit_sw srcw)
1726 struct sljit_jump *jump;
1727 sljit_ins inv_bits = (type & SLJIT_I32_OP) ? W_OP : 0;
1729 SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1730 ADJUST_LOCAL_OFFSET(src, srcw);
1732 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1733 PTR_FAIL_IF(!jump);
1734 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1735 jump->flags |= IS_CBZ | IS_COND;
1737 if (src & SLJIT_MEM) {
1738 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1739 src = TMP_REG1;
1741 else if (src & SLJIT_IMM) {
1742 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1743 src = TMP_REG1;
1746 SLJIT_ASSERT(FAST_IS_REG(src));
1748 if ((type & 0xff) == SLJIT_EQUAL)
1749 inv_bits |= 1 << 24;
1751 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1752 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1753 jump->addr = compiler->size;
1754 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1755 return jump;
1758 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1760 struct sljit_jump *jump;
1762 CHECK_ERROR();
1763 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1764 ADJUST_LOCAL_OFFSET(src, srcw);
1766 if (!(src & SLJIT_IMM)) {
1767 if (src & SLJIT_MEM) {
1768 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1769 src = TMP_REG1;
1771 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1774 /* These jumps are converted to jump/call instructions when possible. */
1775 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1776 FAIL_IF(!jump);
1777 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1778 jump->u.target = srcw;
1780 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1781 jump->addr = compiler->size;
1782 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1785 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1786 sljit_s32 arg_types,
1787 sljit_s32 src, sljit_sw srcw)
1789 CHECK_ERROR();
1790 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1792 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1793 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1794 compiler->skip_checks = 1;
1795 #endif
1797 return sljit_emit_ijump(compiler, type, src, srcw);
1800 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1801 sljit_s32 dst, sljit_sw dstw,
1802 sljit_s32 type)
1804 sljit_s32 dst_r, src_r, flags, mem_flags;
1805 sljit_ins cc;
1807 CHECK_ERROR();
1808 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1809 ADJUST_LOCAL_OFFSET(dst, dstw);
1811 cc = get_cc(compiler, type & 0xff);
1812 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1814 if (GET_OPCODE(op) < SLJIT_ADD) {
1815 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1817 if (dst_r == TMP_REG1) {
1818 mem_flags = (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE;
1819 return emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG2);
1822 return SLJIT_SUCCESS;
1825 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1826 mem_flags = WORD_SIZE;
1828 if (op & SLJIT_I32_OP) {
1829 flags |= INT_OP;
1830 mem_flags = INT_SIZE;
1833 src_r = dst;
1835 if (dst & SLJIT_MEM) {
1836 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG1));
1837 src_r = TMP_REG1;
1840 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1841 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
1843 if (dst & SLJIT_MEM)
1844 return emit_op_mem(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1845 return SLJIT_SUCCESS;
1848 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1849 sljit_s32 dst_reg,
1850 sljit_s32 src, sljit_sw srcw)
1852 sljit_ins inv_bits = (dst_reg & SLJIT_I32_OP) ? W_OP : 0;
1853 sljit_ins cc;
1855 CHECK_ERROR();
1856 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1858 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
1859 if (dst_reg & SLJIT_I32_OP)
1860 srcw = (sljit_s32)srcw;
1861 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1862 src = TMP_REG1;
1863 srcw = 0;
1866 cc = get_cc(compiler, type & 0xff);
1867 dst_reg &= ~SLJIT_I32_OP;
1869 return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
1872 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1873 sljit_s32 reg,
1874 sljit_s32 mem, sljit_sw memw)
1876 sljit_u32 sign = 0, inst;
1878 CHECK_ERROR();
1879 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
1881 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1882 return SLJIT_ERR_UNSUPPORTED;
1884 if (type & SLJIT_MEM_SUPP)
1885 return SLJIT_SUCCESS;
1887 switch (type & 0xff) {
1888 case SLJIT_MOV:
1889 case SLJIT_MOV_P:
1890 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1891 break;
1892 case SLJIT_MOV_S8:
1893 sign = 1;
1894 case SLJIT_MOV_U8:
1895 inst = STURBI | (MEM_SIZE_SHIFT(BYTE_SIZE) << 30) | 0x400;
1896 break;
1897 case SLJIT_MOV_S16:
1898 sign = 1;
1899 case SLJIT_MOV_U16:
1900 inst = STURBI | (MEM_SIZE_SHIFT(HALF_SIZE) << 30) | 0x400;
1901 break;
1902 case SLJIT_MOV_S32:
1903 sign = 1;
1904 case SLJIT_MOV_U32:
1905 inst = STURBI | (MEM_SIZE_SHIFT(INT_SIZE) << 30) | 0x400;
1906 break;
1907 default:
1908 SLJIT_UNREACHABLE();
1909 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1910 break;
1913 if (!(type & SLJIT_MEM_STORE))
1914 inst |= sign ? 0x00800000 : 0x00400000;
1916 if (type & SLJIT_MEM_PRE)
1917 inst |= 0x800;
1919 return push_inst(compiler, inst | RT(reg) | RN(mem & REG_MASK) | ((memw & 0x1ff) << 12));
1922 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
1923 sljit_s32 freg,
1924 sljit_s32 mem, sljit_sw memw)
1926 sljit_u32 inst;
1928 CHECK_ERROR();
1929 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
1931 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1932 return SLJIT_ERR_UNSUPPORTED;
1934 if (type & SLJIT_MEM_SUPP)
1935 return SLJIT_SUCCESS;
1937 inst = STUR_FI | 0x80000400;
1939 if (!(type & SLJIT_F32_OP))
1940 inst |= 0x40000000;
1942 if (!(type & SLJIT_MEM_STORE))
1943 inst |= 0x00400000;
1945 if (type & SLJIT_MEM_PRE)
1946 inst |= 0x800;
1948 return push_inst(compiler, inst | VT(freg) | RN(mem & REG_MASK) | ((memw & 0x1ff) << 12));
1951 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset)
1953 sljit_s32 dst_reg;
1954 sljit_ins ins;
1956 CHECK_ERROR();
1957 CHECK(check_sljit_get_local_base(compiler, dst, dstw, offset));
1959 SLJIT_ASSERT (SLJIT_LOCALS_OFFSET_BASE == 0);
1961 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
1963 if (offset <= 0xffffff && offset >= -0xffffff) {
1964 ins = ADDI;
1965 if (offset < 0) {
1966 offset = -offset;
1967 ins = SUBI;
1970 if (offset <= 0xfff)
1971 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (offset << 10)));
1972 else {
1973 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | ((offset & 0xfff000) >> (12 - 10)) | (1 << 22)));
1975 offset &= 0xfff;
1976 if (offset != 0)
1977 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(dst_reg) | (offset << 10)));
1980 else {
1981 FAIL_IF(load_immediate (compiler, dst_reg, offset));
1982 /* Add extended register form. */
1983 FAIL_IF(push_inst(compiler, ADDE | (0x3 << 13) | RD(dst_reg) | RN(SLJIT_SP) | RM(dst_reg)));
1986 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1987 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG1);
1988 return SLJIT_SUCCESS;
1991 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
1993 struct sljit_const *const_;
1994 sljit_s32 dst_r;
1996 CHECK_ERROR_PTR();
1997 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1998 ADJUST_LOCAL_OFFSET(dst, dstw);
2000 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2001 PTR_FAIL_IF(!const_);
2002 set_const(const_, compiler);
2004 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2005 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value));
2007 if (dst & SLJIT_MEM)
2008 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2009 return const_;
2012 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2014 struct sljit_put_label *put_label;
2015 sljit_s32 dst_r;
2017 CHECK_ERROR_PTR();
2018 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2019 ADJUST_LOCAL_OFFSET(dst, dstw);
2021 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2022 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, 0));
2024 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2025 PTR_FAIL_IF(!put_label);
2026 set_put_label(put_label, compiler, 1);
2028 if (dst & SLJIT_MEM)
2029 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2031 return put_label;
2034 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2036 sljit_ins* inst = (sljit_ins*)addr;
2037 sljit_s32 dst;
2038 SLJIT_UNUSED_ARG(executable_offset);
2040 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2042 dst = inst[0] & 0x1f;
2043 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
2044 inst[0] = MOVZ | dst | ((new_target & 0xffff) << 5);
2045 inst[1] = MOVK | dst | (((new_target >> 16) & 0xffff) << 5) | (1 << 21);
2046 inst[2] = MOVK | dst | (((new_target >> 32) & 0xffff) << 5) | (2 << 21);
2047 inst[3] = MOVK | dst | ((new_target >> 48) << 5) | (3 << 21);
2049 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2050 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2051 SLJIT_CACHE_FLUSH(inst, inst + 4);
2054 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2056 sljit_set_jump_addr(addr, new_constant, executable_offset);