Implement tail call on ARM.
[sljit.git] / sljit_src / sljitNativeARM_64.c
blob5916c3d2ea657c70ff117aa89704e1f09bff8db0
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 ((sljit_ins)1 << 31)
55 #define RD(rd) ((sljit_ins)reg_map[rd])
56 #define RT(rt) ((sljit_ins)reg_map[rt])
57 #define RN(rn) ((sljit_ins)reg_map[rn] << 5)
58 #define RT2(rt2) ((sljit_ins)reg_map[rt2] << 10)
59 #define RM(rm) ((sljit_ins)reg_map[rm] << 16)
60 #define VD(vd) ((sljit_ins)freg_map[vd])
61 #define VT(vt) ((sljit_ins)freg_map[vt])
62 #define VN(vn) ((sljit_ins)freg_map[vn] << 5)
63 #define VM(vm) ((sljit_ins)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) | ((sljit_ins)(imm & 0xffff) << 5)));
149 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 16) & 0xffff) << 5) | (1 << 21)));
150 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 32) & 0xffff) << 5) | (2 << 21)));
151 return push_inst(compiler, MOVK | RD(dst) | ((sljit_ins)(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 += SSIZE_OF(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 -= SSIZE_OF(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_sw addr;
235 sljit_u32 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 = (sljit_uw)(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 = (sljit_uw)(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 = (sljit_sw)((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 = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
321 SLJIT_ASSERT(addr <= 0x1ffffff && addr >= -0x2000000);
322 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (sljit_ins)(addr & 0x3ffffff);
323 if (jump->flags & IS_COND)
324 buf_ptr[-1] -= (4 << 5);
325 break;
327 if (jump->flags & PATCH_COND) {
328 addr = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
329 SLJIT_ASSERT(addr <= 0x3ffff && addr >= -0x40000);
330 buf_ptr[0] = (buf_ptr[0] & ~(sljit_ins)0xffffe0) | (sljit_ins)((addr & 0x7ffff) << 5);
331 break;
334 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || (sljit_uw)addr <= (sljit_uw)0xffffffff);
335 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || (sljit_uw)addr <= (sljit_uw)0xffffffffffff);
337 dst = buf_ptr[0] & 0x1f;
338 buf_ptr[0] = MOVZ | dst | (((sljit_ins)addr & 0xffff) << 5);
339 buf_ptr[1] = MOVK | dst | (((sljit_ins)(addr >> 16) & 0xffff) << 5) | (1 << 21);
340 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
341 buf_ptr[2] = MOVK | dst | (((sljit_ins)(addr >> 32) & 0xffff) << 5) | (2 << 21);
342 if (jump->flags & PATCH_ABS64)
343 buf_ptr[3] = MOVK | dst | ((sljit_ins)(addr >> 48) << 5) | (3 << 21);
344 } while (0);
345 jump = jump->next;
348 put_label = compiler->put_labels;
349 while (put_label) {
350 addr = (sljit_sw)put_label->label->addr;
351 buf_ptr = (sljit_ins*)put_label->addr;
353 buf_ptr[0] |= ((sljit_ins)addr & 0xffff) << 5;
354 buf_ptr[1] |= ((sljit_ins)(addr >> 16) & 0xffff) << 5;
356 if (put_label->flags >= 1)
357 buf_ptr[2] |= ((sljit_ins)(addr >> 32) & 0xffff) << 5;
359 if (put_label->flags >= 2)
360 buf_ptr[3] |= (sljit_ins)(addr >> 48) << 5;
362 put_label = put_label->next;
365 compiler->error = SLJIT_ERR_COMPILED;
366 compiler->executable_offset = executable_offset;
367 compiler->executable_size = (sljit_uw)(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 (sljit_ins)0x100
431 static sljit_ins logical_imm(sljit_sw imm, sljit_u32 len)
433 sljit_s32 negated;
434 sljit_u32 ones, right;
435 sljit_uw mask, uimm;
436 sljit_ins ins;
438 if (len & LOGICAL_IMM_CHECK) {
439 len &= ~LOGICAL_IMM_CHECK;
440 if (len == 32 && (imm == 0 || imm == -1))
441 return 0;
442 if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
443 return 0;
446 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
447 || (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
449 uimm = (sljit_uw)imm;
450 while (1) {
451 if (len <= 0) {
452 SLJIT_UNREACHABLE();
453 return 0;
456 mask = ((sljit_uw)1 << len) - 1;
457 if ((uimm & mask) != ((uimm >> len) & mask))
458 break;
459 len >>= 1;
462 len <<= 1;
464 negated = 0;
465 if (uimm & 0x1) {
466 negated = 1;
467 uimm = ~uimm;
470 if (len < 64)
471 uimm &= ((sljit_uw)1 << len) - 1;
473 /* Unsigned right shift. */
474 COUNT_TRAILING_ZERO(uimm, right);
476 /* Signed shift. We also know that the highest bit is set. */
477 imm = (sljit_sw)~uimm;
478 SLJIT_ASSERT(imm < 0);
480 COUNT_TRAILING_ZERO(imm, ones);
482 if (~imm)
483 return 0;
485 if (len == 64)
486 ins = 1 << 22;
487 else
488 ins = (0x3f - ((len << 1) - 1)) << 10;
490 if (negated)
491 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
493 return ins | ((ones - 1) << 10) | ((len - right) << 16);
496 #undef COUNT_TRAILING_ZERO
498 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
500 sljit_uw imm = (sljit_uw)simm;
501 sljit_u32 i, zeros, ones, first;
502 sljit_ins bitmask;
504 /* Handling simple immediates first. */
505 if (imm <= 0xffff)
506 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)imm << 5));
508 if (simm < 0 && simm >= -0x10000)
509 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
511 if (imm <= 0xffffffffl) {
512 if ((imm & 0xffff) == 0)
513 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)(imm >> 16) << 5) | (1 << 21));
514 if ((imm & 0xffff0000l) == 0xffff0000)
515 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
516 if ((imm & 0xffff) == 0xffff)
517 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
519 bitmask = logical_imm(simm, 16);
520 if (bitmask != 0)
521 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
523 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)imm & 0xffff) << 5)));
524 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
527 bitmask = logical_imm(simm, 32);
528 if (bitmask != 0)
529 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
531 if (simm < 0 && simm >= -0x100000000l) {
532 if ((imm & 0xffff) == 0xffff)
533 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
535 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5)));
536 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
539 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
541 zeros = 0;
542 ones = 0;
543 for (i = 4; i > 0; i--) {
544 if ((simm & 0xffff) == 0)
545 zeros++;
546 if ((simm & 0xffff) == 0xffff)
547 ones++;
548 simm >>= 16;
551 simm = (sljit_sw)imm;
552 first = 1;
553 if (ones > zeros) {
554 simm = ~simm;
555 for (i = 0; i < 4; i++) {
556 if (!(simm & 0xffff)) {
557 simm >>= 16;
558 continue;
560 if (first) {
561 first = 0;
562 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
564 else
565 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)~simm & 0xffff) << 5) | (i << 21)));
566 simm >>= 16;
568 return SLJIT_SUCCESS;
571 for (i = 0; i < 4; i++) {
572 if (!(simm & 0xffff)) {
573 simm >>= 16;
574 continue;
576 if (first) {
577 first = 0;
578 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
580 else
581 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
582 simm >>= 16;
584 return SLJIT_SUCCESS;
587 #define ARG1_IMM 0x0010000
588 #define ARG2_IMM 0x0020000
589 #define INT_OP 0x0040000
590 #define SET_FLAGS 0x0080000
591 #define UNUSED_RETURN 0x0100000
593 #define CHECK_FLAGS(flag_bits) \
594 if (flags & SET_FLAGS) { \
595 inv_bits |= flag_bits; \
596 if (flags & UNUSED_RETURN) \
597 dst = TMP_ZERO; \
600 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
602 /* dst must be register, TMP_REG1
603 arg1 must be register, TMP_REG1, imm
604 arg2 must be register, TMP_REG2, imm */
605 sljit_ins inv_bits = (flags & INT_OP) ? W_OP : 0;
606 sljit_ins inst_bits;
607 sljit_s32 op = (flags & 0xffff);
608 sljit_s32 reg;
609 sljit_sw imm, nimm;
611 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
612 /* Both are immediates. */
613 flags &= ~ARG1_IMM;
614 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
615 arg1 = TMP_ZERO;
616 else {
617 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
618 arg1 = TMP_REG1;
622 if (flags & (ARG1_IMM | ARG2_IMM)) {
623 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
624 imm = (flags & ARG2_IMM) ? arg2 : arg1;
626 switch (op) {
627 case SLJIT_MUL:
628 case SLJIT_NEG:
629 case SLJIT_CLZ:
630 case SLJIT_ADDC:
631 case SLJIT_SUBC:
632 /* No form with immediate operand (except imm 0, which
633 is represented by a ZERO register). */
634 break;
635 case SLJIT_MOV:
636 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
637 return load_immediate(compiler, dst, imm);
638 case SLJIT_NOT:
639 SLJIT_ASSERT(flags & ARG2_IMM);
640 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
641 goto set_flags;
642 case SLJIT_SUB:
643 if (flags & ARG1_IMM)
644 break;
645 imm = -imm;
646 /* Fall through. */
647 case SLJIT_ADD:
648 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
649 if (imm == 0) {
650 CHECK_FLAGS(1 << 29);
651 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
653 if (imm > 0 && imm <= 0xfff) {
654 CHECK_FLAGS(1 << 29);
655 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((sljit_ins)imm << 10));
657 nimm = -imm;
658 if (nimm > 0 && nimm <= 0xfff) {
659 CHECK_FLAGS(1 << 29);
660 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((sljit_ins)nimm << 10));
662 if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
663 CHECK_FLAGS(1 << 29);
664 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)imm >> 12) << 10) | (1 << 22));
666 if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
667 CHECK_FLAGS(1 << 29);
668 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)nimm >> 12) << 10) | (1 << 22));
670 if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
671 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)imm >> 12) << 10) | (1 << 22)));
672 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | (((sljit_ins)imm & 0xfff) << 10));
674 if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
675 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)nimm >> 12) << 10) | (1 << 22)));
676 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | (((sljit_ins)nimm & 0xfff) << 10));
678 break;
679 case SLJIT_AND:
680 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
681 if (!inst_bits)
682 break;
683 CHECK_FLAGS(3 << 29);
684 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
685 case SLJIT_OR:
686 case SLJIT_XOR:
687 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
688 if (!inst_bits)
689 break;
690 if (op == SLJIT_OR)
691 inst_bits |= ORRI;
692 else
693 inst_bits |= EORI;
694 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
695 goto set_flags;
696 case SLJIT_SHL:
697 if (flags & ARG1_IMM)
698 break;
699 if (flags & INT_OP) {
700 imm &= 0x1f;
701 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
702 | (((sljit_ins)-imm & 0x1f) << 16) | ((31 - (sljit_ins)imm) << 10)));
704 else {
705 imm &= 0x3f;
706 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22)
707 | (((sljit_ins)-imm & 0x3f) << 16) | ((63 - (sljit_ins)imm) << 10)));
709 goto set_flags;
710 case SLJIT_LSHR:
711 case SLJIT_ASHR:
712 if (flags & ARG1_IMM)
713 break;
714 if (op == SLJIT_ASHR)
715 inv_bits |= 1 << 30;
716 if (flags & INT_OP) {
717 imm &= 0x1f;
718 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
719 | ((sljit_ins)imm << 16) | (31 << 10)));
721 else {
722 imm &= 0x3f;
723 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
724 | (1 << 22) | ((sljit_ins)imm << 16) | (63 << 10)));
726 goto set_flags;
727 default:
728 SLJIT_UNREACHABLE();
729 break;
732 if (flags & ARG2_IMM) {
733 if (arg2 == 0)
734 arg2 = TMP_ZERO;
735 else {
736 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
737 arg2 = TMP_REG2;
740 else {
741 if (arg1 == 0)
742 arg1 = TMP_ZERO;
743 else {
744 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
745 arg1 = TMP_REG1;
750 /* Both arguments are registers. */
751 switch (op) {
752 case SLJIT_MOV:
753 case SLJIT_MOV_P:
754 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
755 if (dst == arg2)
756 return SLJIT_SUCCESS;
757 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
758 case SLJIT_MOV_U8:
759 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
760 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (7 << 10));
761 case SLJIT_MOV_S8:
762 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
763 if (!(flags & INT_OP))
764 inv_bits |= 1 << 22;
765 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
766 case SLJIT_MOV_U16:
767 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
768 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (15 << 10));
769 case SLJIT_MOV_S16:
770 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
771 if (!(flags & INT_OP))
772 inv_bits |= 1 << 22;
773 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
774 case SLJIT_MOV32:
775 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
776 if (dst == arg2)
777 return SLJIT_SUCCESS;
778 /* fallthrough */
779 case SLJIT_MOV_U32:
780 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
781 return push_inst(compiler, (ORR ^ W_OP) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
782 case SLJIT_MOV_S32:
783 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
784 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
785 case SLJIT_NOT:
786 SLJIT_ASSERT(arg1 == TMP_REG1);
787 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
788 break; /* Set flags. */
789 case SLJIT_NEG:
790 SLJIT_ASSERT(arg1 == TMP_REG1);
791 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
792 if (flags & SET_FLAGS)
793 inv_bits |= 1 << 29;
794 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
795 case SLJIT_CLZ:
796 SLJIT_ASSERT(arg1 == TMP_REG1);
797 return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
798 case SLJIT_ADD:
799 CHECK_FLAGS(1 << 29);
800 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
801 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
802 case SLJIT_ADDC:
803 CHECK_FLAGS(1 << 29);
804 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
805 case SLJIT_SUB:
806 CHECK_FLAGS(1 << 29);
807 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
808 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
809 case SLJIT_SUBC:
810 CHECK_FLAGS(1 << 29);
811 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
812 case SLJIT_MUL:
813 compiler->status_flags_state = 0;
814 if (!(flags & SET_FLAGS))
815 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
816 if (flags & INT_OP) {
817 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
818 FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
819 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
821 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
822 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
823 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
824 case SLJIT_AND:
825 CHECK_FLAGS(3 << 29);
826 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
827 case SLJIT_OR:
828 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
829 break; /* Set flags. */
830 case SLJIT_XOR:
831 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
832 break; /* Set flags. */
833 case SLJIT_SHL:
834 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
835 break; /* Set flags. */
836 case SLJIT_LSHR:
837 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
838 break; /* Set flags. */
839 case SLJIT_ASHR:
840 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
841 break; /* Set flags. */
842 default:
843 SLJIT_UNREACHABLE();
844 return SLJIT_SUCCESS;
847 set_flags:
848 if (flags & SET_FLAGS)
849 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
850 return SLJIT_SUCCESS;
853 #define STORE 0x10
854 #define SIGNED 0x20
856 #define BYTE_SIZE 0x0
857 #define HALF_SIZE 0x1
858 #define INT_SIZE 0x2
859 #define WORD_SIZE 0x3
861 #define MEM_SIZE_SHIFT(flags) ((sljit_ins)(flags) & 0x3)
863 static sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
864 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
866 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
867 sljit_u32 type = (shift << 30);
869 if (!(flags & STORE))
870 type |= (flags & SIGNED) ? 0x00800000 : 0x00400000;
872 SLJIT_ASSERT(arg & SLJIT_MEM);
874 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
875 argw &= 0x3;
877 if (argw == 0 || argw == shift)
878 return push_inst(compiler, STRB | type | RT(reg)
879 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
881 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
882 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg));
885 arg &= REG_MASK;
887 if (!arg) {
888 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~(0xfff << shift)));
890 argw = (argw >> shift) & 0xfff;
892 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
895 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
896 if ((argw >> shift) <= 0xfff)
897 return push_inst(compiler, STRBI | type | RT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
899 if (argw <= 0xffffff) {
900 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_reg) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
902 argw = ((argw & 0xfff) >> shift);
903 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
907 if (argw <= 255 && argw >= -256)
908 return push_inst(compiler, STURBI | type | RT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
910 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
912 return push_inst(compiler, STRB | type | RT(reg) | RN(arg) | RM(tmp_reg));
915 /* --------------------------------------------------------------------- */
916 /* Entry, exit */
917 /* --------------------------------------------------------------------- */
919 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
920 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
921 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
923 sljit_s32 prev, saved_regs_size, i, tmp;
924 sljit_s32 word_arg_count = 0;
925 sljit_ins offs;
927 CHECK_ERROR();
928 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
929 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
931 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
932 if (saved_regs_size & 0x8)
933 saved_regs_size += SSIZE_OF(sw);
935 local_size = (local_size + 15) & ~0xf;
936 compiler->local_size = local_size + saved_regs_size;
938 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
939 | RN(SLJIT_SP) | (sljit_ins)((-(saved_regs_size >> 3) & 0x7f) << 15)));
941 #ifdef _WIN32
942 if (local_size >= 4096)
943 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
944 else if (local_size > 256)
945 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
946 #endif
948 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
949 prev = -1;
950 offs = 2 << 15;
951 for (i = SLJIT_S0; i >= tmp; i--) {
952 if (prev == -1) {
953 prev = i;
954 continue;
956 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
957 offs += 2 << 15;
958 prev = -1;
961 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
962 if (prev == -1) {
963 prev = i;
964 continue;
966 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
967 offs += 2 << 15;
968 prev = -1;
971 if (prev != -1)
972 FAIL_IF(push_inst(compiler, STRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
974 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_FP) | RN(SLJIT_SP) | (0 << 10)));
976 arg_types >>= SLJIT_ARG_SHIFT;
978 while (arg_types > 0) {
979 if ((arg_types & SLJIT_ARG_MASK) < SLJIT_ARG_TYPE_F64) {
980 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0 - word_arg_count) | RN(TMP_ZERO) | RM(SLJIT_R0 + word_arg_count)));
981 word_arg_count++;
983 arg_types >>= SLJIT_ARG_SHIFT;
986 #ifdef _WIN32
987 if (local_size >= 4096) {
988 if (local_size < 4 * 4096) {
989 /* No need for a loop. */
990 if (local_size >= 2 * 4096) {
991 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
992 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
993 local_size -= 4096;
996 if (local_size >= 2 * 4096) {
997 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
998 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
999 local_size -= 4096;
1002 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1003 local_size -= 4096;
1005 else {
1006 FAIL_IF(push_inst(compiler, MOVZ | RD(TMP_REG2) | ((((sljit_ins)local_size >> 12) - 1) << 5)));
1007 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1008 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
1009 FAIL_IF(push_inst(compiler, SUBI | (1 << 29) | RD(TMP_REG2) | RN(TMP_REG2) | (1 << 10)));
1010 FAIL_IF(push_inst(compiler, B_CC | ((((sljit_ins) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1011 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1013 local_size &= 0xfff;
1016 if (local_size > 256) {
1017 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | ((sljit_ins)local_size << 10)));
1018 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1020 else if (local_size > 0)
1021 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(TMP_REG1) | (((sljit_ins)-local_size & 0x1ff) << 12)));
1023 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1025 else if (local_size > 256) {
1026 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1027 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1029 else if (local_size > 0)
1030 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(SLJIT_SP) | (((sljit_ins)-local_size & 0x1ff) << 12)));
1032 #else /* !_WIN32 */
1034 /* The local_size does not include saved registers size. */
1035 if (local_size > 0xfff) {
1036 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1037 local_size &= 0xfff;
1039 if (local_size != 0)
1040 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1042 #endif /* _WIN32 */
1044 return SLJIT_SUCCESS;
1047 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1048 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1049 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1051 sljit_s32 saved_regs_size;
1053 CHECK_ERROR();
1054 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1055 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1057 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
1058 if (saved_regs_size & 0x8)
1059 saved_regs_size += SSIZE_OF(sw);
1061 compiler->local_size = saved_regs_size + ((local_size + 15) & ~0xf);
1062 return SLJIT_SUCCESS;
1065 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler)
1067 sljit_s32 local_size;
1068 sljit_s32 prev, saved_regs_size, i, tmp;
1069 sljit_ins offs;
1071 saved_regs_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 2);
1072 if (saved_regs_size & 0x8)
1073 saved_regs_size += SSIZE_OF(sw);
1075 SLJIT_ASSERT((saved_regs_size & 0xf) == 0);
1077 local_size = compiler->local_size - saved_regs_size;
1079 /* Load LR as early as possible. */
1080 if (local_size > 0xfff) {
1081 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1082 local_size &= 0xfff;
1085 if (local_size == 0)
1086 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1087 else if (local_size <= 63 * SSIZE_OF(sw)) {
1088 FAIL_IF(push_inst(compiler, LDP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1089 | RN(SLJIT_SP) | ((sljit_ins)local_size << (15 - 3))));
1091 else {
1092 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1093 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1096 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1097 prev = -1;
1098 offs = 2 << 15;
1099 for (i = SLJIT_S0; i >= tmp; i--) {
1100 if (prev == -1) {
1101 prev = i;
1102 continue;
1104 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1105 offs += 2 << 15;
1106 prev = -1;
1109 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1110 if (prev == -1) {
1111 prev = i;
1112 continue;
1114 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1115 offs += 2 << 15;
1116 prev = -1;
1119 if (prev != -1)
1120 FAIL_IF(push_inst(compiler, LDRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
1122 /* This and the next call/jump instruction can be executed parallel. */
1123 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (sljit_ins)(saved_regs_size << 10));
1126 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1128 CHECK_ERROR();
1129 CHECK(check_sljit_emit_return_void(compiler));
1131 FAIL_IF(emit_stack_frame_release(compiler));
1133 return push_inst(compiler, RET | RN(TMP_LR));
1136 /* --------------------------------------------------------------------- */
1137 /* Operators */
1138 /* --------------------------------------------------------------------- */
1140 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1142 sljit_ins inv_bits = (op & SLJIT_32) ? W_OP : 0;
1144 CHECK_ERROR();
1145 CHECK(check_sljit_emit_op0(compiler, op));
1147 op = GET_OPCODE(op);
1148 switch (op) {
1149 case SLJIT_BREAKPOINT:
1150 return push_inst(compiler, BRK);
1151 case SLJIT_NOP:
1152 return push_inst(compiler, NOP);
1153 case SLJIT_LMUL_UW:
1154 case SLJIT_LMUL_SW:
1155 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1156 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1157 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1158 case SLJIT_DIVMOD_UW:
1159 case SLJIT_DIVMOD_SW:
1160 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1161 FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1162 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1163 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1164 case SLJIT_DIV_UW:
1165 case SLJIT_DIV_SW:
1166 return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1167 case SLJIT_ENDBR:
1168 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1169 return SLJIT_SUCCESS;
1172 return SLJIT_SUCCESS;
1175 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1176 sljit_s32 dst, sljit_sw dstw,
1177 sljit_s32 src, sljit_sw srcw)
1179 sljit_s32 dst_r, flags, mem_flags;
1180 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1182 CHECK_ERROR();
1183 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1184 ADJUST_LOCAL_OFFSET(dst, dstw);
1185 ADJUST_LOCAL_OFFSET(src, srcw);
1187 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1189 op = GET_OPCODE(op);
1190 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1191 /* Both operands are registers. */
1192 if (dst_r != TMP_REG1 && FAST_IS_REG(src))
1193 return emit_op_imm(compiler, op | ((op_flags & SLJIT_32) ? INT_OP : 0), dst_r, TMP_REG1, src);
1195 switch (op) {
1196 case SLJIT_MOV:
1197 case SLJIT_MOV_P:
1198 mem_flags = WORD_SIZE;
1199 break;
1200 case SLJIT_MOV_U8:
1201 mem_flags = BYTE_SIZE;
1202 if (src & SLJIT_IMM)
1203 srcw = (sljit_u8)srcw;
1204 break;
1205 case SLJIT_MOV_S8:
1206 mem_flags = BYTE_SIZE | SIGNED;
1207 if (src & SLJIT_IMM)
1208 srcw = (sljit_s8)srcw;
1209 break;
1210 case SLJIT_MOV_U16:
1211 mem_flags = HALF_SIZE;
1212 if (src & SLJIT_IMM)
1213 srcw = (sljit_u16)srcw;
1214 break;
1215 case SLJIT_MOV_S16:
1216 mem_flags = HALF_SIZE | SIGNED;
1217 if (src & SLJIT_IMM)
1218 srcw = (sljit_s16)srcw;
1219 break;
1220 case SLJIT_MOV_U32:
1221 mem_flags = INT_SIZE;
1222 if (src & SLJIT_IMM)
1223 srcw = (sljit_u32)srcw;
1224 break;
1225 case SLJIT_MOV_S32:
1226 case SLJIT_MOV32:
1227 mem_flags = INT_SIZE | SIGNED;
1228 if (src & SLJIT_IMM)
1229 srcw = (sljit_s32)srcw;
1230 break;
1231 default:
1232 SLJIT_UNREACHABLE();
1233 mem_flags = 0;
1234 break;
1237 if (src & SLJIT_IMM)
1238 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1239 else if (!(src & SLJIT_MEM))
1240 dst_r = src;
1241 else
1242 FAIL_IF(emit_op_mem(compiler, mem_flags, dst_r, src, srcw, TMP_REG1));
1244 if (dst & SLJIT_MEM)
1245 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1246 return SLJIT_SUCCESS;
1249 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1250 mem_flags = WORD_SIZE;
1252 if (op_flags & SLJIT_32) {
1253 flags |= INT_OP;
1254 mem_flags = INT_SIZE;
1257 if (src & SLJIT_MEM) {
1258 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src, srcw, TMP_REG2));
1259 src = TMP_REG2;
1262 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, src);
1264 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1265 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1266 return SLJIT_SUCCESS;
1269 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1270 sljit_s32 dst, sljit_sw dstw,
1271 sljit_s32 src1, sljit_sw src1w,
1272 sljit_s32 src2, sljit_sw src2w)
1274 sljit_s32 dst_r, flags, mem_flags;
1276 CHECK_ERROR();
1277 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1278 ADJUST_LOCAL_OFFSET(dst, dstw);
1279 ADJUST_LOCAL_OFFSET(src1, src1w);
1280 ADJUST_LOCAL_OFFSET(src2, src2w);
1282 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1283 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1284 mem_flags = WORD_SIZE;
1286 if (op & SLJIT_32) {
1287 flags |= INT_OP;
1288 mem_flags = INT_SIZE;
1291 if (dst == TMP_REG1)
1292 flags |= UNUSED_RETURN;
1294 if (src1 & SLJIT_MEM) {
1295 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, src1, src1w, TMP_REG1));
1296 src1 = TMP_REG1;
1299 if (src2 & SLJIT_MEM) {
1300 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src2, src2w, TMP_REG2));
1301 src2 = TMP_REG2;
1304 if (src1 & SLJIT_IMM)
1305 flags |= ARG1_IMM;
1306 else
1307 src1w = src1;
1309 if (src2 & SLJIT_IMM)
1310 flags |= ARG2_IMM;
1311 else
1312 src2w = src2;
1314 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1316 if (dst & SLJIT_MEM)
1317 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1318 return SLJIT_SUCCESS;
1321 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1322 sljit_s32 src1, sljit_sw src1w,
1323 sljit_s32 src2, sljit_sw src2w)
1325 CHECK_ERROR();
1326 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1328 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1329 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1330 compiler->skip_checks = 1;
1331 #endif
1332 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1335 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1336 sljit_s32 src, sljit_sw srcw)
1338 CHECK_ERROR();
1339 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1340 ADJUST_LOCAL_OFFSET(src, srcw);
1342 switch (op) {
1343 case SLJIT_FAST_RETURN:
1344 if (FAST_IS_REG(src))
1345 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1346 else
1347 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw, TMP_REG1));
1349 return push_inst(compiler, RET | RN(TMP_LR));
1350 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1351 return SLJIT_SUCCESS;
1352 case SLJIT_PREFETCH_L1:
1353 case SLJIT_PREFETCH_L2:
1354 case SLJIT_PREFETCH_L3:
1355 case SLJIT_PREFETCH_ONCE:
1356 SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1358 /* The reg_map[op] should provide the appropriate constant. */
1359 if (op == SLJIT_PREFETCH_L1)
1360 op = 1;
1361 else if (op == SLJIT_PREFETCH_L2)
1362 op = 3;
1363 else if (op == SLJIT_PREFETCH_L3)
1364 op = 5;
1365 else
1366 op = 2;
1368 /* Signed word sized load is the prefetch instruction. */
1369 return emit_op_mem(compiler, WORD_SIZE | SIGNED, op, src, srcw, TMP_REG1);
1372 return SLJIT_SUCCESS;
1375 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1377 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1378 return reg_map[reg];
1381 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1383 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1384 return freg_map[reg];
1387 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1388 void *instruction, sljit_u32 size)
1390 CHECK_ERROR();
1391 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1393 return push_inst(compiler, *(sljit_ins*)instruction);
1396 /* --------------------------------------------------------------------- */
1397 /* Floating point operators */
1398 /* --------------------------------------------------------------------- */
1400 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1402 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1403 sljit_ins type = (shift << 30);
1405 SLJIT_ASSERT(arg & SLJIT_MEM);
1407 if (!(flags & STORE))
1408 type |= 0x00400000;
1410 if (arg & OFFS_REG_MASK) {
1411 argw &= 3;
1412 if (argw == 0 || argw == shift)
1413 return push_inst(compiler, STR_FR | type | VT(reg)
1414 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1416 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
1417 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1));
1420 arg &= REG_MASK;
1422 if (!arg) {
1423 FAIL_IF(load_immediate(compiler, TMP_REG1, argw & ~(0xfff << shift)));
1425 argw = (argw >> shift) & 0xfff;
1427 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1430 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
1431 if ((argw >> shift) <= 0xfff)
1432 return push_inst(compiler, STR_FI | type | VT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
1434 if (argw <= 0xffffff) {
1435 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(TMP_REG1) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
1437 argw = ((argw & 0xfff) >> shift);
1438 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1442 if (argw <= 255 && argw >= -256)
1443 return push_inst(compiler, STUR_FI | type | VT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
1445 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1446 return push_inst(compiler, STR_FR | type | VT(reg) | RN(arg) | RM(TMP_REG1));
1449 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(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_REG1;
1454 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1456 if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1457 inv_bits |= W_OP;
1459 if (src & SLJIT_MEM) {
1460 emit_fop_mem(compiler, (op & SLJIT_32) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1461 src = TMP_FREG1;
1464 FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1466 if (dst & SLJIT_MEM)
1467 return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1468 return SLJIT_SUCCESS;
1471 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1472 sljit_s32 dst, sljit_sw dstw,
1473 sljit_s32 src, sljit_sw srcw)
1475 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1476 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1478 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1479 inv_bits |= W_OP;
1481 if (src & SLJIT_MEM) {
1482 emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw, TMP_REG1);
1483 src = TMP_REG1;
1484 } else if (src & SLJIT_IMM) {
1485 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1486 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1487 srcw = (sljit_s32)srcw;
1488 #endif
1489 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1490 src = TMP_REG1;
1493 FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1495 if (dst & SLJIT_MEM)
1496 return emit_fop_mem(compiler, ((op & SLJIT_32) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1497 return SLJIT_SUCCESS;
1500 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1501 sljit_s32 src1, sljit_sw src1w,
1502 sljit_s32 src2, sljit_sw src2w)
1504 sljit_s32 mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1505 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1507 if (src1 & SLJIT_MEM) {
1508 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1509 src1 = TMP_FREG1;
1512 if (src2 & SLJIT_MEM) {
1513 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1514 src2 = TMP_FREG2;
1517 return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1520 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1521 sljit_s32 dst, sljit_sw dstw,
1522 sljit_s32 src, sljit_sw srcw)
1524 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1525 sljit_ins inv_bits;
1527 CHECK_ERROR();
1529 SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x1) == WORD_SIZE, must_be_one_bit_difference);
1530 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1532 inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1533 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1535 if (src & SLJIT_MEM) {
1536 emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x1) : mem_flags, dst_r, src, srcw);
1537 src = dst_r;
1540 switch (GET_OPCODE(op)) {
1541 case SLJIT_MOV_F64:
1542 if (src != dst_r) {
1543 if (dst_r != TMP_FREG1)
1544 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1545 else
1546 dst_r = src;
1548 break;
1549 case SLJIT_NEG_F64:
1550 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1551 break;
1552 case SLJIT_ABS_F64:
1553 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1554 break;
1555 case SLJIT_CONV_F64_FROM_F32:
1556 FAIL_IF(push_inst(compiler, FCVT | (sljit_ins)((op & SLJIT_32) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1557 break;
1560 if (dst & SLJIT_MEM)
1561 return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1562 return SLJIT_SUCCESS;
1565 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1566 sljit_s32 dst, sljit_sw dstw,
1567 sljit_s32 src1, sljit_sw src1w,
1568 sljit_s32 src2, sljit_sw src2w)
1570 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1571 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1573 CHECK_ERROR();
1574 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1575 ADJUST_LOCAL_OFFSET(dst, dstw);
1576 ADJUST_LOCAL_OFFSET(src1, src1w);
1577 ADJUST_LOCAL_OFFSET(src2, src2w);
1579 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1580 if (src1 & SLJIT_MEM) {
1581 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1582 src1 = TMP_FREG1;
1584 if (src2 & SLJIT_MEM) {
1585 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1586 src2 = TMP_FREG2;
1589 switch (GET_OPCODE(op)) {
1590 case SLJIT_ADD_F64:
1591 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1592 break;
1593 case SLJIT_SUB_F64:
1594 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1595 break;
1596 case SLJIT_MUL_F64:
1597 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1598 break;
1599 case SLJIT_DIV_F64:
1600 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1601 break;
1604 if (!(dst & SLJIT_MEM))
1605 return SLJIT_SUCCESS;
1606 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1609 /* --------------------------------------------------------------------- */
1610 /* Other instructions */
1611 /* --------------------------------------------------------------------- */
1613 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1615 CHECK_ERROR();
1616 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1617 ADJUST_LOCAL_OFFSET(dst, dstw);
1619 if (FAST_IS_REG(dst))
1620 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1622 /* Memory. */
1623 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw, TMP_REG1);
1626 /* --------------------------------------------------------------------- */
1627 /* Conditional instructions */
1628 /* --------------------------------------------------------------------- */
1630 static sljit_ins get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1632 switch (type) {
1633 case SLJIT_EQUAL:
1634 case SLJIT_EQUAL_F64:
1635 return 0x1;
1637 case SLJIT_NOT_EQUAL:
1638 case SLJIT_NOT_EQUAL_F64:
1639 return 0x0;
1641 case SLJIT_LESS:
1642 case SLJIT_LESS_F64:
1643 return 0x2;
1645 case SLJIT_GREATER_EQUAL:
1646 case SLJIT_GREATER_EQUAL_F64:
1647 return 0x3;
1649 case SLJIT_GREATER:
1650 case SLJIT_GREATER_F64:
1651 return 0x9;
1653 case SLJIT_LESS_EQUAL:
1654 case SLJIT_LESS_EQUAL_F64:
1655 return 0x8;
1657 case SLJIT_SIG_LESS:
1658 return 0xa;
1660 case SLJIT_SIG_GREATER_EQUAL:
1661 return 0xb;
1663 case SLJIT_SIG_GREATER:
1664 return 0xd;
1666 case SLJIT_SIG_LESS_EQUAL:
1667 return 0xc;
1669 case SLJIT_OVERFLOW:
1670 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1671 return 0x0;
1673 case SLJIT_UNORDERED_F64:
1674 return 0x7;
1676 case SLJIT_NOT_OVERFLOW:
1677 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1678 return 0x1;
1680 case SLJIT_ORDERED_F64:
1681 return 0x6;
1683 default:
1684 SLJIT_UNREACHABLE();
1685 return 0xe;
1689 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1691 struct sljit_label *label;
1693 CHECK_ERROR_PTR();
1694 CHECK_PTR(check_sljit_emit_label(compiler));
1696 if (compiler->last_label && compiler->last_label->size == compiler->size)
1697 return compiler->last_label;
1699 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1700 PTR_FAIL_IF(!label);
1701 set_label(label, compiler);
1702 return label;
1705 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1707 struct sljit_jump *jump;
1709 CHECK_ERROR_PTR();
1710 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1712 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1713 PTR_FAIL_IF(!jump);
1714 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1715 type &= 0xff;
1717 if (type < SLJIT_JUMP) {
1718 jump->flags |= IS_COND;
1719 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(compiler, type)));
1721 else if (type >= SLJIT_FAST_CALL)
1722 jump->flags |= IS_BL;
1724 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1725 jump->addr = compiler->size;
1726 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1728 return jump;
1731 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1732 sljit_s32 arg_types)
1734 CHECK_ERROR_PTR();
1735 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1737 if (type & SLJIT_TAIL_CALL) {
1738 PTR_FAIL_IF(emit_stack_frame_release(compiler));
1739 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
1742 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1743 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1744 compiler->skip_checks = 1;
1745 #endif
1747 return sljit_emit_jump(compiler, type);
1750 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1751 sljit_s32 src, sljit_sw srcw)
1753 struct sljit_jump *jump;
1754 sljit_ins inv_bits = (type & SLJIT_32) ? W_OP : 0;
1756 SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1757 ADJUST_LOCAL_OFFSET(src, srcw);
1759 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1760 PTR_FAIL_IF(!jump);
1761 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1762 jump->flags |= IS_CBZ | IS_COND;
1764 if (src & SLJIT_MEM) {
1765 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1766 src = TMP_REG1;
1768 else if (src & SLJIT_IMM) {
1769 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1770 src = TMP_REG1;
1773 SLJIT_ASSERT(FAST_IS_REG(src));
1775 if ((type & 0xff) == SLJIT_EQUAL)
1776 inv_bits |= 1 << 24;
1778 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1779 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1780 jump->addr = compiler->size;
1781 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1782 return jump;
1785 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1787 struct sljit_jump *jump;
1789 CHECK_ERROR();
1790 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1791 ADJUST_LOCAL_OFFSET(src, srcw);
1793 if (!(src & SLJIT_IMM)) {
1794 if (src & SLJIT_MEM) {
1795 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1796 src = TMP_REG1;
1798 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1801 /* These jumps are converted to jump/call instructions when possible. */
1802 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1803 FAIL_IF(!jump);
1804 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1805 jump->u.target = (sljit_uw)srcw;
1807 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1808 jump->addr = compiler->size;
1809 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1812 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1813 sljit_s32 arg_types,
1814 sljit_s32 src, sljit_sw srcw)
1816 CHECK_ERROR();
1817 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1819 if (src & SLJIT_MEM) {
1820 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1821 src = TMP_REG1;
1824 if (type & SLJIT_TAIL_CALL) {
1825 if (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0) {
1826 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(src)));
1827 src = TMP_REG1;
1830 FAIL_IF(emit_stack_frame_release(compiler));
1831 type = SLJIT_JUMP;
1834 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1835 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1836 compiler->skip_checks = 1;
1837 #endif
1839 return sljit_emit_ijump(compiler, type, src, srcw);
1842 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1843 sljit_s32 dst, sljit_sw dstw,
1844 sljit_s32 type)
1846 sljit_s32 dst_r, src_r, flags, mem_flags;
1847 sljit_ins cc;
1849 CHECK_ERROR();
1850 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1851 ADJUST_LOCAL_OFFSET(dst, dstw);
1853 cc = get_cc(compiler, type & 0xff);
1854 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1856 if (GET_OPCODE(op) < SLJIT_ADD) {
1857 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1859 if (dst_r == TMP_REG1) {
1860 mem_flags = (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE;
1861 return emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG2);
1864 return SLJIT_SUCCESS;
1867 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1868 mem_flags = WORD_SIZE;
1870 if (op & SLJIT_32) {
1871 flags |= INT_OP;
1872 mem_flags = INT_SIZE;
1875 src_r = dst;
1877 if (dst & SLJIT_MEM) {
1878 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG1));
1879 src_r = TMP_REG1;
1882 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1883 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
1885 if (dst & SLJIT_MEM)
1886 return emit_op_mem(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1887 return SLJIT_SUCCESS;
1890 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1891 sljit_s32 dst_reg,
1892 sljit_s32 src, sljit_sw srcw)
1894 sljit_ins inv_bits = (dst_reg & SLJIT_32) ? W_OP : 0;
1895 sljit_ins cc;
1897 CHECK_ERROR();
1898 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1900 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
1901 if (dst_reg & SLJIT_32)
1902 srcw = (sljit_s32)srcw;
1903 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1904 src = TMP_REG1;
1905 srcw = 0;
1908 cc = get_cc(compiler, type & 0xff);
1909 dst_reg &= ~SLJIT_32;
1911 return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
1914 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1915 sljit_s32 reg,
1916 sljit_s32 mem, sljit_sw memw)
1918 sljit_u32 sign = 0, inst;
1920 CHECK_ERROR();
1921 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
1923 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1924 return SLJIT_ERR_UNSUPPORTED;
1926 if (type & SLJIT_MEM_SUPP)
1927 return SLJIT_SUCCESS;
1929 switch (type & 0xff) {
1930 case SLJIT_MOV:
1931 case SLJIT_MOV_P:
1932 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1933 break;
1934 case SLJIT_MOV_S8:
1935 sign = 1;
1936 case SLJIT_MOV_U8:
1937 inst = STURBI | (MEM_SIZE_SHIFT(BYTE_SIZE) << 30) | 0x400;
1938 break;
1939 case SLJIT_MOV_S16:
1940 sign = 1;
1941 case SLJIT_MOV_U16:
1942 inst = STURBI | (MEM_SIZE_SHIFT(HALF_SIZE) << 30) | 0x400;
1943 break;
1944 case SLJIT_MOV_S32:
1945 sign = 1;
1946 case SLJIT_MOV_U32:
1947 case SLJIT_MOV32:
1948 inst = STURBI | (MEM_SIZE_SHIFT(INT_SIZE) << 30) | 0x400;
1949 break;
1950 default:
1951 SLJIT_UNREACHABLE();
1952 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1953 break;
1956 if (!(type & SLJIT_MEM_STORE))
1957 inst |= sign ? 0x00800000 : 0x00400000;
1959 if (type & SLJIT_MEM_PRE)
1960 inst |= 0x800;
1962 return push_inst(compiler, inst | RT(reg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
1965 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
1966 sljit_s32 freg,
1967 sljit_s32 mem, sljit_sw memw)
1969 sljit_u32 inst;
1971 CHECK_ERROR();
1972 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
1974 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1975 return SLJIT_ERR_UNSUPPORTED;
1977 if (type & SLJIT_MEM_SUPP)
1978 return SLJIT_SUCCESS;
1980 inst = STUR_FI | 0x80000400;
1982 if (!(type & SLJIT_32))
1983 inst |= 0x40000000;
1985 if (!(type & SLJIT_MEM_STORE))
1986 inst |= 0x00400000;
1988 if (type & SLJIT_MEM_PRE)
1989 inst |= 0x800;
1991 return push_inst(compiler, inst | VT(freg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
1994 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset)
1996 sljit_s32 dst_reg;
1997 sljit_ins ins;
1999 CHECK_ERROR();
2000 CHECK(check_sljit_get_local_base(compiler, dst, dstw, offset));
2002 SLJIT_ASSERT (SLJIT_LOCALS_OFFSET_BASE == 0);
2004 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2006 if (offset <= 0xffffff && offset >= -0xffffff) {
2007 ins = ADDI;
2008 if (offset < 0) {
2009 offset = -offset;
2010 ins = SUBI;
2013 if (offset <= 0xfff)
2014 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)(offset << 10)));
2015 else {
2016 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)((offset & 0xfff000) >> (12 - 10)) | (1 << 22)));
2018 offset &= 0xfff;
2019 if (offset != 0)
2020 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(dst_reg) | (sljit_ins)(offset << 10)));
2023 else {
2024 FAIL_IF(load_immediate (compiler, dst_reg, offset));
2025 /* Add extended register form. */
2026 FAIL_IF(push_inst(compiler, ADDE | (0x3 << 13) | RD(dst_reg) | RN(SLJIT_SP) | RM(dst_reg)));
2029 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
2030 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG1);
2031 return SLJIT_SUCCESS;
2034 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2036 struct sljit_const *const_;
2037 sljit_s32 dst_r;
2039 CHECK_ERROR_PTR();
2040 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2041 ADJUST_LOCAL_OFFSET(dst, dstw);
2043 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2044 PTR_FAIL_IF(!const_);
2045 set_const(const_, compiler);
2047 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2048 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, (sljit_uw)init_value));
2050 if (dst & SLJIT_MEM)
2051 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2052 return const_;
2055 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2057 struct sljit_put_label *put_label;
2058 sljit_s32 dst_r;
2060 CHECK_ERROR_PTR();
2061 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2062 ADJUST_LOCAL_OFFSET(dst, dstw);
2064 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2065 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, 0));
2067 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2068 PTR_FAIL_IF(!put_label);
2069 set_put_label(put_label, compiler, 1);
2071 if (dst & SLJIT_MEM)
2072 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2074 return put_label;
2077 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2079 sljit_ins* inst = (sljit_ins*)addr;
2080 sljit_u32 dst;
2081 SLJIT_UNUSED_ARG(executable_offset);
2083 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2085 dst = inst[0] & 0x1f;
2086 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
2087 inst[0] = MOVZ | dst | (((sljit_u32)new_target & 0xffff) << 5);
2088 inst[1] = MOVK | dst | (((sljit_u32)(new_target >> 16) & 0xffff) << 5) | (1 << 21);
2089 inst[2] = MOVK | dst | (((sljit_u32)(new_target >> 32) & 0xffff) << 5) | (2 << 21);
2090 inst[3] = MOVK | dst | ((sljit_u32)(new_target >> 48) << 5) | (3 << 21);
2092 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2093 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2094 SLJIT_CACHE_FLUSH(inst, inst + 4);
2097 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2099 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);