Improve x86 float compare
[sljit.git] / sljit_src / sljitNativeARM_64.c
blob86145cc7ba453bc6e40530218faac71b9a8d569b
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, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 15, 14, 13, 12, 11, 10, 9, 8, 30, 31
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 VT2(vt) ((sljit_ins)freg_map[vt] << 10)
63 #define VN(vn) ((sljit_ins)freg_map[vn] << 5)
64 #define VM(vm) ((sljit_ins)freg_map[vm] << 16)
66 /* --------------------------------------------------------------------- */
67 /* Instrucion forms */
68 /* --------------------------------------------------------------------- */
70 #define ADC 0x9a000000
71 #define ADD 0x8b000000
72 #define ADDE 0x8b200000
73 #define ADDI 0x91000000
74 #define AND 0x8a000000
75 #define ANDI 0x92000000
76 #define ASRV 0x9ac02800
77 #define B 0x14000000
78 #define B_CC 0x54000000
79 #define BL 0x94000000
80 #define BLR 0xd63f0000
81 #define BR 0xd61f0000
82 #define BRK 0xd4200000
83 #define CBZ 0xb4000000
84 #define CLZ 0xdac01000
85 #define CSEL 0x9a800000
86 #define CSINC 0x9a800400
87 #define EOR 0xca000000
88 #define EORI 0xd2000000
89 #define FABS 0x1e60c000
90 #define FADD 0x1e602800
91 #define FCMP 0x1e602000
92 #define FCVT 0x1e224000
93 #define FCVTZS 0x9e780000
94 #define FDIV 0x1e601800
95 #define FMOV 0x1e604000
96 #define FMUL 0x1e600800
97 #define FNEG 0x1e614000
98 #define FSUB 0x1e603800
99 #define LDRI 0xf9400000
100 #define LDRI_F64 0xfd400000
101 #define LDP 0xa9400000
102 #define LDP_F64 0x6d400000
103 #define LDP_POST 0xa8c00000
104 #define LDR_PRE 0xf8400c00
105 #define LSLV 0x9ac02000
106 #define LSRV 0x9ac02400
107 #define MADD 0x9b000000
108 #define MOVK 0xf2800000
109 #define MOVN 0x92800000
110 #define MOVZ 0xd2800000
111 #define NOP 0xd503201f
112 #define ORN 0xaa200000
113 #define ORR 0xaa000000
114 #define ORRI 0xb2000000
115 #define RET 0xd65f0000
116 #define SBC 0xda000000
117 #define SBFM 0x93000000
118 #define SCVTF 0x9e620000
119 #define SDIV 0x9ac00c00
120 #define SMADDL 0x9b200000
121 #define SMULH 0x9b403c00
122 #define STP 0xa9000000
123 #define STP_F64 0x6d000000
124 #define STP_PRE 0xa9800000
125 #define STRB 0x38206800
126 #define STRBI 0x39000000
127 #define STRI 0xf9000000
128 #define STRI_F64 0xfd000000
129 #define STR_FI 0x3d000000
130 #define STR_FR 0x3c206800
131 #define STUR_FI 0x3c000000
132 #define STURBI 0x38000000
133 #define SUB 0xcb000000
134 #define SUBI 0xd1000000
135 #define SUBS 0xeb000000
136 #define UBFM 0xd3000000
137 #define UDIV 0x9ac00800
138 #define UMULH 0x9bc03c00
140 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
142 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
143 FAIL_IF(!ptr);
144 *ptr = ins;
145 compiler->size++;
146 return SLJIT_SUCCESS;
149 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
151 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)(imm & 0xffff) << 5)));
152 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 16) & 0xffff) << 5) | (1 << 21)));
153 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 32) & 0xffff) << 5) | (2 << 21)));
154 return push_inst(compiler, MOVK | RD(dst) | ((sljit_ins)(imm >> 48) << 5) | (3 << 21));
157 static SLJIT_INLINE sljit_sw detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
159 sljit_sw diff;
160 sljit_uw target_addr;
162 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
163 jump->flags |= PATCH_ABS64;
164 return 0;
167 if (jump->flags & JUMP_ADDR)
168 target_addr = jump->u.target;
169 else {
170 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
171 target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
174 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
176 if (jump->flags & IS_COND) {
177 diff += SSIZE_OF(ins);
178 if (diff <= 0xfffff && diff >= -0x100000) {
179 code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
180 jump->addr -= sizeof(sljit_ins);
181 jump->flags |= PATCH_COND;
182 return 5;
184 diff -= SSIZE_OF(ins);
187 if (diff <= 0x7ffffff && diff >= -0x8000000) {
188 jump->flags |= PATCH_B;
189 return 4;
192 if (target_addr < 0x100000000l) {
193 if (jump->flags & IS_COND)
194 code_ptr[-5] -= (2 << 5);
195 code_ptr[-2] = code_ptr[0];
196 return 2;
199 if (target_addr < 0x1000000000000l) {
200 if (jump->flags & IS_COND)
201 code_ptr[-5] -= (1 << 5);
202 jump->flags |= PATCH_ABS48;
203 code_ptr[-1] = code_ptr[0];
204 return 1;
207 jump->flags |= PATCH_ABS64;
208 return 0;
211 static SLJIT_INLINE sljit_sw put_label_get_length(struct sljit_put_label *put_label, sljit_uw max_label)
213 if (max_label < 0x100000000l) {
214 put_label->flags = 0;
215 return 2;
218 if (max_label < 0x1000000000000l) {
219 put_label->flags = 1;
220 return 1;
223 put_label->flags = 2;
224 return 0;
227 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
229 struct sljit_memory_fragment *buf;
230 sljit_ins *code;
231 sljit_ins *code_ptr;
232 sljit_ins *buf_ptr;
233 sljit_ins *buf_end;
234 sljit_uw word_count;
235 sljit_uw next_addr;
236 sljit_sw executable_offset;
237 sljit_sw addr;
238 sljit_u32 dst;
240 struct sljit_label *label;
241 struct sljit_jump *jump;
242 struct sljit_const *const_;
243 struct sljit_put_label *put_label;
245 CHECK_ERROR_PTR();
246 CHECK_PTR(check_sljit_generate_code(compiler));
247 reverse_buf(compiler);
249 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins), compiler->exec_allocator_data);
250 PTR_FAIL_WITH_EXEC_IF(code);
251 buf = compiler->buf;
253 code_ptr = code;
254 word_count = 0;
255 next_addr = 0;
256 executable_offset = SLJIT_EXEC_OFFSET(code);
258 label = compiler->labels;
259 jump = compiler->jumps;
260 const_ = compiler->consts;
261 put_label = compiler->put_labels;
263 do {
264 buf_ptr = (sljit_ins*)buf->memory;
265 buf_end = buf_ptr + (buf->used_size >> 2);
266 do {
267 *code_ptr = *buf_ptr++;
268 if (next_addr == word_count) {
269 SLJIT_ASSERT(!label || label->size >= word_count);
270 SLJIT_ASSERT(!jump || jump->addr >= word_count);
271 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
272 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
274 /* These structures are ordered by their address. */
275 if (label && label->size == word_count) {
276 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
277 label->size = (sljit_uw)(code_ptr - code);
278 label = label->next;
280 if (jump && jump->addr == word_count) {
281 jump->addr = (sljit_uw)(code_ptr - 4);
282 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
283 jump = jump->next;
285 if (const_ && const_->addr == word_count) {
286 const_->addr = (sljit_uw)code_ptr;
287 const_ = const_->next;
289 if (put_label && put_label->addr == word_count) {
290 SLJIT_ASSERT(put_label->label);
291 put_label->addr = (sljit_uw)(code_ptr - 3);
292 code_ptr -= put_label_get_length(put_label, (sljit_uw)(SLJIT_ADD_EXEC_OFFSET(code, executable_offset) + put_label->label->size));
293 put_label = put_label->next;
295 next_addr = compute_next_addr(label, jump, const_, put_label);
297 code_ptr++;
298 word_count++;
299 } while (buf_ptr < buf_end);
301 buf = buf->next;
302 } while (buf);
304 if (label && label->size == word_count) {
305 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
306 label->size = (sljit_uw)(code_ptr - code);
307 label = label->next;
310 SLJIT_ASSERT(!label);
311 SLJIT_ASSERT(!jump);
312 SLJIT_ASSERT(!const_);
313 SLJIT_ASSERT(!put_label);
314 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
316 jump = compiler->jumps;
317 while (jump) {
318 do {
319 addr = (sljit_sw)((jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
320 buf_ptr = (sljit_ins *)jump->addr;
322 if (jump->flags & PATCH_B) {
323 addr = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
324 SLJIT_ASSERT(addr <= 0x1ffffff && addr >= -0x2000000);
325 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (sljit_ins)(addr & 0x3ffffff);
326 if (jump->flags & IS_COND)
327 buf_ptr[-1] -= (4 << 5);
328 break;
330 if (jump->flags & PATCH_COND) {
331 addr = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
332 SLJIT_ASSERT(addr <= 0x3ffff && addr >= -0x40000);
333 buf_ptr[0] = (buf_ptr[0] & ~(sljit_ins)0xffffe0) | (sljit_ins)((addr & 0x7ffff) << 5);
334 break;
337 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || (sljit_uw)addr <= (sljit_uw)0xffffffff);
338 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || (sljit_uw)addr <= (sljit_uw)0xffffffffffff);
340 dst = buf_ptr[0] & 0x1f;
341 buf_ptr[0] = MOVZ | dst | (((sljit_ins)addr & 0xffff) << 5);
342 buf_ptr[1] = MOVK | dst | (((sljit_ins)(addr >> 16) & 0xffff) << 5) | (1 << 21);
343 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
344 buf_ptr[2] = MOVK | dst | (((sljit_ins)(addr >> 32) & 0xffff) << 5) | (2 << 21);
345 if (jump->flags & PATCH_ABS64)
346 buf_ptr[3] = MOVK | dst | ((sljit_ins)(addr >> 48) << 5) | (3 << 21);
347 } while (0);
348 jump = jump->next;
351 put_label = compiler->put_labels;
352 while (put_label) {
353 addr = (sljit_sw)put_label->label->addr;
354 buf_ptr = (sljit_ins*)put_label->addr;
356 buf_ptr[0] |= ((sljit_ins)addr & 0xffff) << 5;
357 buf_ptr[1] |= ((sljit_ins)(addr >> 16) & 0xffff) << 5;
359 if (put_label->flags >= 1)
360 buf_ptr[2] |= ((sljit_ins)(addr >> 32) & 0xffff) << 5;
362 if (put_label->flags >= 2)
363 buf_ptr[3] |= (sljit_ins)(addr >> 48) << 5;
365 put_label = put_label->next;
368 compiler->error = SLJIT_ERR_COMPILED;
369 compiler->executable_offset = executable_offset;
370 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_ins);
372 code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
373 code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
375 SLJIT_CACHE_FLUSH(code, code_ptr);
376 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
377 return code;
380 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
382 switch (feature_type) {
383 case SLJIT_HAS_FPU:
384 #ifdef SLJIT_IS_FPU_AVAILABLE
385 return SLJIT_IS_FPU_AVAILABLE;
386 #else
387 /* Available by default. */
388 return 1;
389 #endif
391 case SLJIT_HAS_CLZ:
392 case SLJIT_HAS_CMOV:
393 case SLJIT_HAS_PREFETCH:
394 return 1;
396 default:
397 return 0;
401 /* --------------------------------------------------------------------- */
402 /* Core code generator functions. */
403 /* --------------------------------------------------------------------- */
405 #define COUNT_TRAILING_ZERO(value, result) \
406 result = 0; \
407 if (!(value & 0xffffffff)) { \
408 result += 32; \
409 value >>= 32; \
411 if (!(value & 0xffff)) { \
412 result += 16; \
413 value >>= 16; \
415 if (!(value & 0xff)) { \
416 result += 8; \
417 value >>= 8; \
419 if (!(value & 0xf)) { \
420 result += 4; \
421 value >>= 4; \
423 if (!(value & 0x3)) { \
424 result += 2; \
425 value >>= 2; \
427 if (!(value & 0x1)) { \
428 result += 1; \
429 value >>= 1; \
432 #define LOGICAL_IMM_CHECK (sljit_ins)0x100
434 static sljit_ins logical_imm(sljit_sw imm, sljit_u32 len)
436 sljit_s32 negated;
437 sljit_u32 ones, right;
438 sljit_uw mask, uimm;
439 sljit_ins ins;
441 if (len & LOGICAL_IMM_CHECK) {
442 len &= ~LOGICAL_IMM_CHECK;
443 if (len == 32 && (imm == 0 || imm == -1))
444 return 0;
445 if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
446 return 0;
449 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
450 || (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
452 uimm = (sljit_uw)imm;
453 while (1) {
454 if (len <= 0) {
455 SLJIT_UNREACHABLE();
456 return 0;
459 mask = ((sljit_uw)1 << len) - 1;
460 if ((uimm & mask) != ((uimm >> len) & mask))
461 break;
462 len >>= 1;
465 len <<= 1;
467 negated = 0;
468 if (uimm & 0x1) {
469 negated = 1;
470 uimm = ~uimm;
473 if (len < 64)
474 uimm &= ((sljit_uw)1 << len) - 1;
476 /* Unsigned right shift. */
477 COUNT_TRAILING_ZERO(uimm, right);
479 /* Signed shift. We also know that the highest bit is set. */
480 imm = (sljit_sw)~uimm;
481 SLJIT_ASSERT(imm < 0);
483 COUNT_TRAILING_ZERO(imm, ones);
485 if (~imm)
486 return 0;
488 if (len == 64)
489 ins = 1 << 22;
490 else
491 ins = (0x3f - ((len << 1) - 1)) << 10;
493 if (negated)
494 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
496 return ins | ((ones - 1) << 10) | ((len - right) << 16);
499 #undef COUNT_TRAILING_ZERO
501 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
503 sljit_uw imm = (sljit_uw)simm;
504 sljit_u32 i, zeros, ones, first;
505 sljit_ins bitmask;
507 /* Handling simple immediates first. */
508 if (imm <= 0xffff)
509 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)imm << 5));
511 if (simm < 0 && simm >= -0x10000)
512 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
514 if (imm <= 0xffffffffl) {
515 if ((imm & 0xffff) == 0)
516 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)(imm >> 16) << 5) | (1 << 21));
517 if ((imm & 0xffff0000l) == 0xffff0000)
518 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
519 if ((imm & 0xffff) == 0xffff)
520 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
522 bitmask = logical_imm(simm, 16);
523 if (bitmask != 0)
524 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
526 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)imm & 0xffff) << 5)));
527 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
530 bitmask = logical_imm(simm, 32);
531 if (bitmask != 0)
532 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
534 if (simm < 0 && simm >= -0x100000000l) {
535 if ((imm & 0xffff) == 0xffff)
536 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
538 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5)));
539 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
542 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
544 zeros = 0;
545 ones = 0;
546 for (i = 4; i > 0; i--) {
547 if ((simm & 0xffff) == 0)
548 zeros++;
549 if ((simm & 0xffff) == 0xffff)
550 ones++;
551 simm >>= 16;
554 simm = (sljit_sw)imm;
555 first = 1;
556 if (ones > zeros) {
557 simm = ~simm;
558 for (i = 0; i < 4; i++) {
559 if (!(simm & 0xffff)) {
560 simm >>= 16;
561 continue;
563 if (first) {
564 first = 0;
565 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
567 else
568 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)~simm & 0xffff) << 5) | (i << 21)));
569 simm >>= 16;
571 return SLJIT_SUCCESS;
574 for (i = 0; i < 4; i++) {
575 if (!(simm & 0xffff)) {
576 simm >>= 16;
577 continue;
579 if (first) {
580 first = 0;
581 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
583 else
584 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
585 simm >>= 16;
587 return SLJIT_SUCCESS;
590 #define ARG1_IMM 0x0010000
591 #define ARG2_IMM 0x0020000
592 #define INT_OP 0x0040000
593 #define SET_FLAGS 0x0080000
594 #define UNUSED_RETURN 0x0100000
596 #define CHECK_FLAGS(flag_bits) \
597 if (flags & SET_FLAGS) { \
598 inv_bits |= flag_bits; \
599 if (flags & UNUSED_RETURN) \
600 dst = TMP_ZERO; \
603 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
605 /* dst must be register, TMP_REG1
606 arg1 must be register, TMP_REG1, imm
607 arg2 must be register, TMP_REG2, imm */
608 sljit_ins inv_bits = (flags & INT_OP) ? W_OP : 0;
609 sljit_ins inst_bits;
610 sljit_s32 op = (flags & 0xffff);
611 sljit_s32 reg;
612 sljit_sw imm, nimm;
614 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
615 /* Both are immediates. */
616 flags &= ~ARG1_IMM;
617 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
618 arg1 = TMP_ZERO;
619 else {
620 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
621 arg1 = TMP_REG1;
625 if (flags & (ARG1_IMM | ARG2_IMM)) {
626 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
627 imm = (flags & ARG2_IMM) ? arg2 : arg1;
629 switch (op) {
630 case SLJIT_MUL:
631 case SLJIT_CLZ:
632 case SLJIT_ADDC:
633 case SLJIT_SUBC:
634 /* No form with immediate operand (except imm 0, which
635 is represented by a ZERO register). */
636 break;
637 case SLJIT_MOV:
638 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
639 return load_immediate(compiler, dst, imm);
640 case SLJIT_NOT:
641 SLJIT_ASSERT(flags & ARG2_IMM);
642 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
643 goto set_flags;
644 case SLJIT_SUB:
645 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
646 if (flags & ARG1_IMM)
647 break;
648 imm = -imm;
649 /* Fall through. */
650 case SLJIT_ADD:
651 if (op != SLJIT_SUB)
652 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
654 if (imm == 0) {
655 CHECK_FLAGS(1 << 29);
656 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
658 if (imm > 0 && imm <= 0xfff) {
659 CHECK_FLAGS(1 << 29);
660 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((sljit_ins)imm << 10));
662 nimm = -imm;
663 if (nimm > 0 && nimm <= 0xfff) {
664 CHECK_FLAGS(1 << 29);
665 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((sljit_ins)nimm << 10));
667 if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
668 CHECK_FLAGS(1 << 29);
669 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)imm >> 12) << 10) | (1 << 22));
671 if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
672 CHECK_FLAGS(1 << 29);
673 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)nimm >> 12) << 10) | (1 << 22));
675 if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
676 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)imm >> 12) << 10) | (1 << 22)));
677 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | (((sljit_ins)imm & 0xfff) << 10));
679 if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
680 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (((sljit_ins)nimm >> 12) << 10) | (1 << 22)));
681 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | (((sljit_ins)nimm & 0xfff) << 10));
683 break;
684 case SLJIT_AND:
685 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
686 if (!inst_bits)
687 break;
688 CHECK_FLAGS(3 << 29);
689 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
690 case SLJIT_OR:
691 case SLJIT_XOR:
692 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
693 if (!inst_bits)
694 break;
695 if (op == SLJIT_OR)
696 inst_bits |= ORRI;
697 else
698 inst_bits |= EORI;
699 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
700 goto set_flags;
701 case SLJIT_SHL:
702 if (flags & ARG1_IMM)
703 break;
704 if (flags & INT_OP) {
705 imm &= 0x1f;
706 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
707 | (((sljit_ins)-imm & 0x1f) << 16) | ((31 - (sljit_ins)imm) << 10)));
709 else {
710 imm &= 0x3f;
711 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22)
712 | (((sljit_ins)-imm & 0x3f) << 16) | ((63 - (sljit_ins)imm) << 10)));
714 goto set_flags;
715 case SLJIT_LSHR:
716 case SLJIT_ASHR:
717 if (flags & ARG1_IMM)
718 break;
719 if (op == SLJIT_ASHR)
720 inv_bits |= 1 << 30;
721 if (flags & INT_OP) {
722 imm &= 0x1f;
723 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
724 | ((sljit_ins)imm << 16) | (31 << 10)));
726 else {
727 imm &= 0x3f;
728 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1)
729 | (1 << 22) | ((sljit_ins)imm << 16) | (63 << 10)));
731 goto set_flags;
732 default:
733 SLJIT_UNREACHABLE();
734 break;
737 if (flags & ARG2_IMM) {
738 if (arg2 == 0)
739 arg2 = TMP_ZERO;
740 else {
741 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
742 arg2 = TMP_REG2;
745 else {
746 if (arg1 == 0)
747 arg1 = TMP_ZERO;
748 else {
749 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
750 arg1 = TMP_REG1;
755 /* Both arguments are registers. */
756 switch (op) {
757 case SLJIT_MOV:
758 case SLJIT_MOV_P:
759 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
760 if (dst == arg2)
761 return SLJIT_SUCCESS;
762 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
763 case SLJIT_MOV_U8:
764 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
765 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (7 << 10));
766 case SLJIT_MOV_S8:
767 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
768 if (!(flags & INT_OP))
769 inv_bits |= 1 << 22;
770 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
771 case SLJIT_MOV_U16:
772 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
773 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (15 << 10));
774 case SLJIT_MOV_S16:
775 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
776 if (!(flags & INT_OP))
777 inv_bits |= 1 << 22;
778 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
779 case SLJIT_MOV32:
780 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
781 if (dst == arg2)
782 return SLJIT_SUCCESS;
783 /* fallthrough */
784 case SLJIT_MOV_U32:
785 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
786 return push_inst(compiler, (ORR ^ W_OP) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
787 case SLJIT_MOV_S32:
788 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
789 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
790 case SLJIT_NOT:
791 SLJIT_ASSERT(arg1 == TMP_REG1);
792 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
793 break; /* Set flags. */
794 case SLJIT_CLZ:
795 SLJIT_ASSERT(arg1 == TMP_REG1);
796 return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
797 case SLJIT_ADD:
798 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
799 CHECK_FLAGS(1 << 29);
800 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
801 case SLJIT_ADDC:
802 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
803 CHECK_FLAGS(1 << 29);
804 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
805 case SLJIT_SUB:
806 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
807 CHECK_FLAGS(1 << 29);
808 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
809 case SLJIT_SUBC:
810 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
811 CHECK_FLAGS(1 << 29);
812 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
813 case SLJIT_MUL:
814 compiler->status_flags_state = 0;
815 if (!(flags & SET_FLAGS))
816 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
817 if (flags & INT_OP) {
818 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
819 FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
820 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
822 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
823 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
824 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
825 case SLJIT_AND:
826 CHECK_FLAGS(3 << 29);
827 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
828 case SLJIT_OR:
829 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
830 break; /* Set flags. */
831 case SLJIT_XOR:
832 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
833 break; /* Set flags. */
834 case SLJIT_SHL:
835 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
836 break; /* Set flags. */
837 case SLJIT_LSHR:
838 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
839 break; /* Set flags. */
840 case SLJIT_ASHR:
841 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
842 break; /* Set flags. */
843 default:
844 SLJIT_UNREACHABLE();
845 return SLJIT_SUCCESS;
848 set_flags:
849 if (flags & SET_FLAGS)
850 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
851 return SLJIT_SUCCESS;
854 #define STORE 0x10
855 #define SIGNED 0x20
857 #define BYTE_SIZE 0x0
858 #define HALF_SIZE 0x1
859 #define INT_SIZE 0x2
860 #define WORD_SIZE 0x3
862 #define MEM_SIZE_SHIFT(flags) ((sljit_ins)(flags) & 0x3)
864 static sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
865 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
867 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
868 sljit_u32 type = (shift << 30);
870 if (!(flags & STORE))
871 type |= (flags & SIGNED) ? 0x00800000 : 0x00400000;
873 SLJIT_ASSERT(arg & SLJIT_MEM);
875 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
876 argw &= 0x3;
878 if (argw == 0 || argw == shift)
879 return push_inst(compiler, STRB | type | RT(reg)
880 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
882 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
883 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg));
886 arg &= REG_MASK;
888 if (!arg) {
889 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~(0xfff << shift)));
891 argw = (argw >> shift) & 0xfff;
893 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
896 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
897 if ((argw >> shift) <= 0xfff)
898 return push_inst(compiler, STRBI | type | RT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
900 if (argw <= 0xffffff) {
901 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_reg) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
903 argw = ((argw & 0xfff) >> shift);
904 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
908 if (argw <= 255 && argw >= -256)
909 return push_inst(compiler, STURBI | type | RT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
911 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
913 return push_inst(compiler, STRB | type | RT(reg) | RN(arg) | RM(tmp_reg));
916 /* --------------------------------------------------------------------- */
917 /* Entry, exit */
918 /* --------------------------------------------------------------------- */
920 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
921 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
922 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
924 sljit_s32 prev, fprev, saved_regs_size, i, tmp;
925 sljit_s32 saved_arg_count = SLJIT_KEPT_SAVEDS_COUNT(options);
926 sljit_ins offs;
928 CHECK_ERROR();
929 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
930 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
932 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds - saved_arg_count, 2);
933 saved_regs_size += GET_SAVED_FLOAT_REGISTERS_SIZE(fscratches, fsaveds, SSIZE_OF(f64));
935 local_size = (local_size + saved_regs_size + 0xf) & ~0xf;
936 compiler->local_size = local_size;
938 if (local_size <= 512) {
939 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
940 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
941 offs = (sljit_ins)(local_size - 2 * SSIZE_OF(sw)) << (15 - 3);
942 local_size = 0;
943 } else {
944 saved_regs_size = ((saved_regs_size - 2 * SSIZE_OF(sw)) + 0xf) & ~0xf;
946 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)saved_regs_size << 10)));
947 offs = (sljit_ins)(saved_regs_size - 2 * SSIZE_OF(sw)) << (15 - 3);
948 local_size -= saved_regs_size;
949 SLJIT_ASSERT(local_size > 0);
952 prev = -1;
954 tmp = SLJIT_S0 - saveds;
955 for (i = SLJIT_S0 - saved_arg_count; i > tmp; i--) {
956 if (prev == -1) {
957 prev = i;
958 continue;
960 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
961 offs -= (sljit_ins)2 << 15;
962 prev = -1;
965 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
966 if (prev == -1) {
967 prev = i;
968 continue;
970 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
971 offs -= (sljit_ins)2 << 15;
972 prev = -1;
975 fprev = -1;
977 tmp = SLJIT_FS0 - fsaveds;
978 for (i = SLJIT_FS0; i > tmp; i--) {
979 if (fprev == -1) {
980 fprev = i;
981 continue;
983 FAIL_IF(push_inst(compiler, STP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
984 offs -= (sljit_ins)2 << 15;
985 fprev = -1;
988 for (i = fscratches; i >= SLJIT_FIRST_SAVED_FLOAT_REG; i--) {
989 if (fprev == -1) {
990 fprev = i;
991 continue;
993 FAIL_IF(push_inst(compiler, STP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
994 offs -= (sljit_ins)2 << 15;
995 fprev = -1;
998 if (fprev != -1)
999 FAIL_IF(push_inst(compiler, STRI_F64 | VT(fprev) | RN(SLJIT_SP) | (offs >> 5) | (1 << 10)));
1001 if (prev != -1)
1002 FAIL_IF(push_inst(compiler, STRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5) | ((fprev == -1) ? (1 << 10) : 0)));
1004 arg_types >>= SLJIT_ARG_SHIFT;
1006 #ifdef _WIN32
1007 if (local_size > 4096)
1008 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1009 #endif /* _WIN32 */
1011 tmp = SLJIT_R0;
1012 while (arg_types > 0) {
1013 if ((arg_types & SLJIT_ARG_MASK) < SLJIT_ARG_TYPE_F64) {
1014 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1015 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0 - saved_arg_count) | RN(TMP_ZERO) | RM(tmp)));
1016 saved_arg_count++;
1018 tmp++;
1020 arg_types >>= SLJIT_ARG_SHIFT;
1023 #ifdef _WIN32
1024 if (local_size > 4096) {
1025 if (local_size < 4 * 4096) {
1026 /* No need for a loop. */
1028 if (local_size >= 2 * 4096) {
1029 if (local_size >= 3 * 4096) {
1030 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1031 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1034 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1035 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1038 else {
1039 FAIL_IF(push_inst(compiler, MOVZ | RD(TMP_REG1) | ((((sljit_ins)local_size >> 12) - 1) << 5)));
1040 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1041 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1042 FAIL_IF(push_inst(compiler, SUBI | (1 << 29) | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10)));
1043 FAIL_IF(push_inst(compiler, B_CC | ((((sljit_ins) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1046 local_size &= 0xfff;
1048 if (local_size > 0)
1049 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1050 else
1051 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1054 if (local_size > 0) {
1055 if (local_size <= 512)
1056 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1057 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
1058 else {
1059 if (local_size >= 4096)
1060 local_size = (1 << (22 - 10));
1062 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1063 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1067 #else /* !_WIN32 */
1069 /* The local_size does not include saved registers size. */
1070 if (local_size != 0) {
1071 if (local_size > 0xfff) {
1072 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1073 local_size &= 0xfff;
1076 if (local_size > 512 || local_size == 0) {
1077 if (local_size != 0)
1078 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1080 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1081 } else
1082 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1083 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
1086 #endif /* _WIN32 */
1088 return push_inst(compiler, ADDI | RD(TMP_FP) | RN(SLJIT_SP) | (0 << 10));
1091 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1092 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1093 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1095 sljit_s32 saved_regs_size;
1097 CHECK_ERROR();
1098 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1099 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1101 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds - SLJIT_KEPT_SAVEDS_COUNT(options), 2);
1102 saved_regs_size += GET_SAVED_FLOAT_REGISTERS_SIZE(fscratches, fsaveds, SSIZE_OF(f64));
1104 compiler->local_size = (local_size + saved_regs_size + 0xf) & ~0xf;
1105 return SLJIT_SUCCESS;
1108 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler)
1110 sljit_s32 local_size, prev, fprev, i, tmp;
1111 sljit_ins offs;
1113 local_size = compiler->local_size;
1115 if (local_size > 512 && local_size <= 512 + 496) {
1116 FAIL_IF(push_inst(compiler, LDP_POST | RT(TMP_FP) | RT2(TMP_LR)
1117 | RN(SLJIT_SP) | ((sljit_ins)(local_size - 512) << (15 - 3))));
1118 local_size = 512;
1119 } else
1120 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1122 if (local_size > 512) {
1123 local_size -= 512;
1124 if (local_size > 0xfff) {
1125 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP)
1126 | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1127 local_size &= 0xfff;
1130 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1131 local_size = 512;
1134 offs = (sljit_ins)(local_size - 2 * SSIZE_OF(sw)) << (15 - 3);
1135 prev = -1;
1137 tmp = SLJIT_S0 - compiler->saveds;
1138 for (i = SLJIT_S0 - SLJIT_KEPT_SAVEDS_COUNT(compiler->options); i > tmp; i--) {
1139 if (prev == -1) {
1140 prev = i;
1141 continue;
1143 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1144 offs -= (sljit_ins)2 << 15;
1145 prev = -1;
1148 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1149 if (prev == -1) {
1150 prev = i;
1151 continue;
1153 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1154 offs -= (sljit_ins)2 << 15;
1155 prev = -1;
1158 fprev = -1;
1160 tmp = SLJIT_FS0 - compiler->fsaveds;
1161 for (i = SLJIT_FS0; i > tmp; i--) {
1162 if (fprev == -1) {
1163 fprev = i;
1164 continue;
1166 FAIL_IF(push_inst(compiler, LDP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
1167 offs -= (sljit_ins)2 << 15;
1168 fprev = -1;
1171 for (i = compiler->fscratches; i >= SLJIT_FIRST_SAVED_FLOAT_REG; i--) {
1172 if (fprev == -1) {
1173 fprev = i;
1174 continue;
1176 FAIL_IF(push_inst(compiler, LDP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
1177 offs -= (sljit_ins)2 << 15;
1178 fprev = -1;
1181 if (fprev != -1)
1182 FAIL_IF(push_inst(compiler, LDRI_F64 | VT(fprev) | RN(SLJIT_SP) | (offs >> 5) | (1 << 10)));
1184 if (prev != -1)
1185 FAIL_IF(push_inst(compiler, LDRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5) | ((fprev == -1) ? (1 << 10) : 0)));
1187 /* This and the next call/jump instruction can be executed parallelly. */
1188 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (sljit_ins)(local_size << 10));
1191 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1193 CHECK_ERROR();
1194 CHECK(check_sljit_emit_return_void(compiler));
1196 FAIL_IF(emit_stack_frame_release(compiler));
1198 return push_inst(compiler, RET | RN(TMP_LR));
1201 /* --------------------------------------------------------------------- */
1202 /* Operators */
1203 /* --------------------------------------------------------------------- */
1205 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1207 sljit_ins inv_bits = (op & SLJIT_32) ? W_OP : 0;
1209 CHECK_ERROR();
1210 CHECK(check_sljit_emit_op0(compiler, op));
1212 op = GET_OPCODE(op);
1213 switch (op) {
1214 case SLJIT_BREAKPOINT:
1215 return push_inst(compiler, BRK);
1216 case SLJIT_NOP:
1217 return push_inst(compiler, NOP);
1218 case SLJIT_LMUL_UW:
1219 case SLJIT_LMUL_SW:
1220 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1221 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1222 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1223 case SLJIT_DIVMOD_UW:
1224 case SLJIT_DIVMOD_SW:
1225 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1226 FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1227 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1228 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1229 case SLJIT_DIV_UW:
1230 case SLJIT_DIV_SW:
1231 return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1232 case SLJIT_ENDBR:
1233 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1234 return SLJIT_SUCCESS;
1237 return SLJIT_SUCCESS;
1240 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1241 sljit_s32 dst, sljit_sw dstw,
1242 sljit_s32 src, sljit_sw srcw)
1244 sljit_s32 dst_r, flags, mem_flags;
1245 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1247 CHECK_ERROR();
1248 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1249 ADJUST_LOCAL_OFFSET(dst, dstw);
1250 ADJUST_LOCAL_OFFSET(src, srcw);
1252 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1254 op = GET_OPCODE(op);
1255 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1256 /* Both operands are registers. */
1257 if (dst_r != TMP_REG1 && FAST_IS_REG(src))
1258 return emit_op_imm(compiler, op | ((op_flags & SLJIT_32) ? INT_OP : 0), dst_r, TMP_REG1, src);
1260 switch (op) {
1261 case SLJIT_MOV:
1262 case SLJIT_MOV_P:
1263 mem_flags = WORD_SIZE;
1264 break;
1265 case SLJIT_MOV_U8:
1266 mem_flags = BYTE_SIZE;
1267 if (src & SLJIT_IMM)
1268 srcw = (sljit_u8)srcw;
1269 break;
1270 case SLJIT_MOV_S8:
1271 mem_flags = BYTE_SIZE | SIGNED;
1272 if (src & SLJIT_IMM)
1273 srcw = (sljit_s8)srcw;
1274 break;
1275 case SLJIT_MOV_U16:
1276 mem_flags = HALF_SIZE;
1277 if (src & SLJIT_IMM)
1278 srcw = (sljit_u16)srcw;
1279 break;
1280 case SLJIT_MOV_S16:
1281 mem_flags = HALF_SIZE | SIGNED;
1282 if (src & SLJIT_IMM)
1283 srcw = (sljit_s16)srcw;
1284 break;
1285 case SLJIT_MOV_U32:
1286 mem_flags = INT_SIZE;
1287 if (src & SLJIT_IMM)
1288 srcw = (sljit_u32)srcw;
1289 break;
1290 case SLJIT_MOV_S32:
1291 case SLJIT_MOV32:
1292 mem_flags = INT_SIZE | SIGNED;
1293 if (src & SLJIT_IMM)
1294 srcw = (sljit_s32)srcw;
1295 break;
1296 default:
1297 SLJIT_UNREACHABLE();
1298 mem_flags = 0;
1299 break;
1302 if (src & SLJIT_IMM)
1303 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1304 else if (!(src & SLJIT_MEM))
1305 dst_r = src;
1306 else
1307 FAIL_IF(emit_op_mem(compiler, mem_flags, dst_r, src, srcw, TMP_REG1));
1309 if (dst & SLJIT_MEM)
1310 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1311 return SLJIT_SUCCESS;
1314 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1315 mem_flags = WORD_SIZE;
1317 if (op_flags & SLJIT_32) {
1318 flags |= INT_OP;
1319 mem_flags = INT_SIZE;
1322 if (src & SLJIT_MEM) {
1323 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src, srcw, TMP_REG2));
1324 src = TMP_REG2;
1327 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, src);
1329 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1330 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1331 return SLJIT_SUCCESS;
1334 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1335 sljit_s32 dst, sljit_sw dstw,
1336 sljit_s32 src1, sljit_sw src1w,
1337 sljit_s32 src2, sljit_sw src2w)
1339 sljit_s32 dst_r, flags, mem_flags;
1341 CHECK_ERROR();
1342 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1343 ADJUST_LOCAL_OFFSET(dst, dstw);
1344 ADJUST_LOCAL_OFFSET(src1, src1w);
1345 ADJUST_LOCAL_OFFSET(src2, src2w);
1347 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1348 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1349 mem_flags = WORD_SIZE;
1351 if (op & SLJIT_32) {
1352 flags |= INT_OP;
1353 mem_flags = INT_SIZE;
1356 if (dst == TMP_REG1)
1357 flags |= UNUSED_RETURN;
1359 if (src1 & SLJIT_MEM) {
1360 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, src1, src1w, TMP_REG1));
1361 src1 = TMP_REG1;
1364 if (src2 & SLJIT_MEM) {
1365 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src2, src2w, TMP_REG2));
1366 src2 = TMP_REG2;
1369 if (src1 & SLJIT_IMM)
1370 flags |= ARG1_IMM;
1371 else
1372 src1w = src1;
1374 if (src2 & SLJIT_IMM)
1375 flags |= ARG2_IMM;
1376 else
1377 src2w = src2;
1379 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1381 if (dst & SLJIT_MEM)
1382 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1383 return SLJIT_SUCCESS;
1386 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1387 sljit_s32 src1, sljit_sw src1w,
1388 sljit_s32 src2, sljit_sw src2w)
1390 CHECK_ERROR();
1391 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1393 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1394 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1395 compiler->skip_checks = 1;
1396 #endif
1397 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1400 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1401 sljit_s32 src, sljit_sw srcw)
1403 CHECK_ERROR();
1404 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1405 ADJUST_LOCAL_OFFSET(src, srcw);
1407 switch (op) {
1408 case SLJIT_FAST_RETURN:
1409 if (FAST_IS_REG(src))
1410 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1411 else
1412 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw, TMP_REG1));
1414 return push_inst(compiler, RET | RN(TMP_LR));
1415 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1416 return SLJIT_SUCCESS;
1417 case SLJIT_PREFETCH_L1:
1418 case SLJIT_PREFETCH_L2:
1419 case SLJIT_PREFETCH_L3:
1420 case SLJIT_PREFETCH_ONCE:
1421 SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1423 /* The reg_map[op] should provide the appropriate constant. */
1424 if (op == SLJIT_PREFETCH_L1)
1425 op = 1;
1426 else if (op == SLJIT_PREFETCH_L2)
1427 op = 3;
1428 else if (op == SLJIT_PREFETCH_L3)
1429 op = 5;
1430 else
1431 op = 2;
1433 /* Signed word sized load is the prefetch instruction. */
1434 return emit_op_mem(compiler, WORD_SIZE | SIGNED, op, src, srcw, TMP_REG1);
1437 return SLJIT_SUCCESS;
1440 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1442 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1443 return reg_map[reg];
1446 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1448 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1449 return freg_map[reg];
1452 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1453 void *instruction, sljit_u32 size)
1455 SLJIT_UNUSED_ARG(size);
1456 CHECK_ERROR();
1457 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1459 return push_inst(compiler, *(sljit_ins*)instruction);
1462 /* --------------------------------------------------------------------- */
1463 /* Floating point operators */
1464 /* --------------------------------------------------------------------- */
1466 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1468 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1469 sljit_ins type = (shift << 30);
1471 SLJIT_ASSERT(arg & SLJIT_MEM);
1473 if (!(flags & STORE))
1474 type |= 0x00400000;
1476 if (arg & OFFS_REG_MASK) {
1477 argw &= 3;
1478 if (argw == 0 || argw == shift)
1479 return push_inst(compiler, STR_FR | type | VT(reg)
1480 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1482 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
1483 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1));
1486 arg &= REG_MASK;
1488 if (!arg) {
1489 FAIL_IF(load_immediate(compiler, TMP_REG1, argw & ~(0xfff << shift)));
1491 argw = (argw >> shift) & 0xfff;
1493 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1496 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
1497 if ((argw >> shift) <= 0xfff)
1498 return push_inst(compiler, STR_FI | type | VT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
1500 if (argw <= 0xffffff) {
1501 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(TMP_REG1) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
1503 argw = ((argw & 0xfff) >> shift);
1504 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1508 if (argw <= 255 && argw >= -256)
1509 return push_inst(compiler, STUR_FI | type | VT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
1511 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1512 return push_inst(compiler, STR_FR | type | VT(reg) | RN(arg) | RM(TMP_REG1));
1515 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1516 sljit_s32 dst, sljit_sw dstw,
1517 sljit_s32 src, sljit_sw srcw)
1519 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1520 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1522 if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1523 inv_bits |= W_OP;
1525 if (src & SLJIT_MEM) {
1526 emit_fop_mem(compiler, (op & SLJIT_32) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1527 src = TMP_FREG1;
1530 FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1532 if (dst & SLJIT_MEM)
1533 return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1534 return SLJIT_SUCCESS;
1537 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1538 sljit_s32 dst, sljit_sw dstw,
1539 sljit_s32 src, sljit_sw srcw)
1541 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1542 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1544 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1545 inv_bits |= W_OP;
1547 if (src & SLJIT_MEM) {
1548 emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw, TMP_REG1);
1549 src = TMP_REG1;
1550 } else if (src & SLJIT_IMM) {
1551 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1552 srcw = (sljit_s32)srcw;
1554 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1555 src = TMP_REG1;
1558 FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1560 if (dst & SLJIT_MEM)
1561 return emit_fop_mem(compiler, ((op & SLJIT_32) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1562 return SLJIT_SUCCESS;
1565 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1566 sljit_s32 src1, sljit_sw src1w,
1567 sljit_s32 src2, sljit_sw src2w)
1569 sljit_s32 mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1570 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1572 if (src1 & SLJIT_MEM) {
1573 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1574 src1 = TMP_FREG1;
1577 if (src2 & SLJIT_MEM) {
1578 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1579 src2 = TMP_FREG2;
1582 return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1585 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1586 sljit_s32 dst, sljit_sw dstw,
1587 sljit_s32 src, sljit_sw srcw)
1589 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1590 sljit_ins inv_bits;
1592 CHECK_ERROR();
1594 SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x1) == WORD_SIZE, must_be_one_bit_difference);
1595 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1597 inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1598 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1600 if (src & SLJIT_MEM) {
1601 emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x1) : mem_flags, dst_r, src, srcw);
1602 src = dst_r;
1605 switch (GET_OPCODE(op)) {
1606 case SLJIT_MOV_F64:
1607 if (src != dst_r) {
1608 if (dst_r != TMP_FREG1)
1609 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1610 else
1611 dst_r = src;
1613 break;
1614 case SLJIT_NEG_F64:
1615 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1616 break;
1617 case SLJIT_ABS_F64:
1618 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1619 break;
1620 case SLJIT_CONV_F64_FROM_F32:
1621 FAIL_IF(push_inst(compiler, FCVT | (sljit_ins)((op & SLJIT_32) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1622 break;
1625 if (dst & SLJIT_MEM)
1626 return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1627 return SLJIT_SUCCESS;
1630 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1631 sljit_s32 dst, sljit_sw dstw,
1632 sljit_s32 src1, sljit_sw src1w,
1633 sljit_s32 src2, sljit_sw src2w)
1635 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1636 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1638 CHECK_ERROR();
1639 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1640 ADJUST_LOCAL_OFFSET(dst, dstw);
1641 ADJUST_LOCAL_OFFSET(src1, src1w);
1642 ADJUST_LOCAL_OFFSET(src2, src2w);
1644 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1645 if (src1 & SLJIT_MEM) {
1646 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1647 src1 = TMP_FREG1;
1649 if (src2 & SLJIT_MEM) {
1650 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1651 src2 = TMP_FREG2;
1654 switch (GET_OPCODE(op)) {
1655 case SLJIT_ADD_F64:
1656 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1657 break;
1658 case SLJIT_SUB_F64:
1659 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1660 break;
1661 case SLJIT_MUL_F64:
1662 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1663 break;
1664 case SLJIT_DIV_F64:
1665 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1666 break;
1669 if (!(dst & SLJIT_MEM))
1670 return SLJIT_SUCCESS;
1671 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1674 /* --------------------------------------------------------------------- */
1675 /* Other instructions */
1676 /* --------------------------------------------------------------------- */
1678 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1680 CHECK_ERROR();
1681 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1682 ADJUST_LOCAL_OFFSET(dst, dstw);
1684 if (FAST_IS_REG(dst))
1685 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1687 /* Memory. */
1688 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw, TMP_REG1);
1691 /* --------------------------------------------------------------------- */
1692 /* Conditional instructions */
1693 /* --------------------------------------------------------------------- */
1695 static sljit_ins get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1697 switch (type) {
1698 case SLJIT_EQUAL:
1699 case SLJIT_F_EQUAL:
1700 case SLJIT_ORDERED_EQUAL:
1701 case SLJIT_UNORDERED_OR_EQUAL: /* Not supported. */
1702 return 0x1;
1704 case SLJIT_NOT_EQUAL:
1705 case SLJIT_F_NOT_EQUAL:
1706 case SLJIT_UNORDERED_OR_NOT_EQUAL:
1707 case SLJIT_ORDERED_NOT_EQUAL: /* Not supported. */
1708 return 0x0;
1710 case SLJIT_CARRY:
1711 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
1712 return 0x3;
1713 /* fallthrough */
1715 case SLJIT_LESS:
1716 return 0x2;
1718 case SLJIT_NOT_CARRY:
1719 if (compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD)
1720 return 0x2;
1721 /* fallthrough */
1723 case SLJIT_GREATER_EQUAL:
1724 return 0x3;
1726 case SLJIT_GREATER:
1727 case SLJIT_UNORDERED_OR_GREATER:
1728 return 0x9;
1730 case SLJIT_LESS_EQUAL:
1731 case SLJIT_F_LESS_EQUAL:
1732 case SLJIT_ORDERED_LESS_EQUAL:
1733 return 0x8;
1735 case SLJIT_SIG_LESS:
1736 case SLJIT_UNORDERED_OR_LESS:
1737 return 0xa;
1739 case SLJIT_SIG_GREATER_EQUAL:
1740 case SLJIT_F_GREATER_EQUAL:
1741 case SLJIT_ORDERED_GREATER_EQUAL:
1742 return 0xb;
1744 case SLJIT_SIG_GREATER:
1745 case SLJIT_F_GREATER:
1746 case SLJIT_ORDERED_GREATER:
1747 return 0xd;
1749 case SLJIT_SIG_LESS_EQUAL:
1750 case SLJIT_UNORDERED_OR_LESS_EQUAL:
1751 return 0xc;
1753 case SLJIT_OVERFLOW:
1754 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
1755 return 0x0;
1756 /* fallthrough */
1758 case SLJIT_UNORDERED:
1759 return 0x7;
1761 case SLJIT_NOT_OVERFLOW:
1762 if (!(compiler->status_flags_state & (SLJIT_CURRENT_FLAGS_ADD | SLJIT_CURRENT_FLAGS_SUB)))
1763 return 0x1;
1764 /* fallthrough */
1766 case SLJIT_ORDERED:
1767 return 0x6;
1769 case SLJIT_F_LESS:
1770 case SLJIT_ORDERED_LESS:
1771 return 0x5;
1773 case SLJIT_UNORDERED_OR_GREATER_EQUAL:
1774 return 0x4;
1776 default:
1777 SLJIT_UNREACHABLE();
1778 return 0xe;
1782 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1784 struct sljit_label *label;
1786 CHECK_ERROR_PTR();
1787 CHECK_PTR(check_sljit_emit_label(compiler));
1789 if (compiler->last_label && compiler->last_label->size == compiler->size)
1790 return compiler->last_label;
1792 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1793 PTR_FAIL_IF(!label);
1794 set_label(label, compiler);
1795 return label;
1798 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1800 struct sljit_jump *jump;
1802 CHECK_ERROR_PTR();
1803 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1805 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1806 PTR_FAIL_IF(!jump);
1807 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1808 type &= 0xff;
1810 if (type < SLJIT_JUMP) {
1811 jump->flags |= IS_COND;
1812 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(compiler, type)));
1814 else if (type >= SLJIT_FAST_CALL)
1815 jump->flags |= IS_BL;
1817 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1818 jump->addr = compiler->size;
1819 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1821 return jump;
1824 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1825 sljit_s32 arg_types)
1827 SLJIT_UNUSED_ARG(arg_types);
1828 CHECK_ERROR_PTR();
1829 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1831 if (type & SLJIT_CALL_RETURN) {
1832 PTR_FAIL_IF(emit_stack_frame_release(compiler));
1833 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
1836 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1837 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1838 compiler->skip_checks = 1;
1839 #endif
1841 return sljit_emit_jump(compiler, type);
1844 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1845 sljit_s32 src, sljit_sw srcw)
1847 struct sljit_jump *jump;
1848 sljit_ins inv_bits = (type & SLJIT_32) ? W_OP : 0;
1850 SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1851 ADJUST_LOCAL_OFFSET(src, srcw);
1853 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1854 PTR_FAIL_IF(!jump);
1855 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1856 jump->flags |= IS_CBZ | IS_COND;
1858 if (src & SLJIT_MEM) {
1859 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1860 src = TMP_REG1;
1862 else if (src & SLJIT_IMM) {
1863 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1864 src = TMP_REG1;
1867 SLJIT_ASSERT(FAST_IS_REG(src));
1869 if ((type & 0xff) == SLJIT_EQUAL)
1870 inv_bits |= 1 << 24;
1872 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1873 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1874 jump->addr = compiler->size;
1875 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1876 return jump;
1879 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1881 struct sljit_jump *jump;
1883 CHECK_ERROR();
1884 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1885 ADJUST_LOCAL_OFFSET(src, srcw);
1887 if (!(src & SLJIT_IMM)) {
1888 if (src & SLJIT_MEM) {
1889 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1890 src = TMP_REG1;
1892 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1895 /* These jumps are converted to jump/call instructions when possible. */
1896 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1897 FAIL_IF(!jump);
1898 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1899 jump->u.target = (sljit_uw)srcw;
1901 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1902 jump->addr = compiler->size;
1903 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1906 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1907 sljit_s32 arg_types,
1908 sljit_s32 src, sljit_sw srcw)
1910 SLJIT_UNUSED_ARG(arg_types);
1911 CHECK_ERROR();
1912 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1913 ADJUST_LOCAL_OFFSET(src, srcw);
1915 if (src & SLJIT_MEM) {
1916 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1917 src = TMP_REG1;
1920 if (type & SLJIT_CALL_RETURN) {
1921 if (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0) {
1922 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(src)));
1923 src = TMP_REG1;
1926 FAIL_IF(emit_stack_frame_release(compiler));
1927 type = SLJIT_JUMP;
1930 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1931 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1932 compiler->skip_checks = 1;
1933 #endif
1935 return sljit_emit_ijump(compiler, type, src, srcw);
1938 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1939 sljit_s32 dst, sljit_sw dstw,
1940 sljit_s32 type)
1942 sljit_s32 dst_r, src_r, flags, mem_flags;
1943 sljit_ins cc;
1945 CHECK_ERROR();
1946 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1947 ADJUST_LOCAL_OFFSET(dst, dstw);
1949 cc = get_cc(compiler, type);
1950 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1952 if (GET_OPCODE(op) < SLJIT_ADD) {
1953 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1955 if (dst_r == TMP_REG1) {
1956 mem_flags = (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE;
1957 return emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG2);
1960 return SLJIT_SUCCESS;
1963 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1964 mem_flags = WORD_SIZE;
1966 if (op & SLJIT_32) {
1967 flags |= INT_OP;
1968 mem_flags = INT_SIZE;
1971 src_r = dst;
1973 if (dst & SLJIT_MEM) {
1974 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG1));
1975 src_r = TMP_REG1;
1978 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1979 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
1981 if (dst & SLJIT_MEM)
1982 return emit_op_mem(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1983 return SLJIT_SUCCESS;
1986 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1987 sljit_s32 dst_reg,
1988 sljit_s32 src, sljit_sw srcw)
1990 sljit_ins inv_bits = (dst_reg & SLJIT_32) ? W_OP : 0;
1991 sljit_ins cc;
1993 CHECK_ERROR();
1994 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1996 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
1997 if (dst_reg & SLJIT_32)
1998 srcw = (sljit_s32)srcw;
1999 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
2000 src = TMP_REG1;
2001 srcw = 0;
2004 cc = get_cc(compiler, type);
2005 dst_reg &= ~SLJIT_32;
2007 return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
2010 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
2011 sljit_s32 reg,
2012 sljit_s32 mem, sljit_sw memw)
2014 sljit_u32 sign = 0, inst;
2016 CHECK_ERROR();
2017 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
2019 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
2020 return SLJIT_ERR_UNSUPPORTED;
2022 if (type & SLJIT_MEM_SUPP)
2023 return SLJIT_SUCCESS;
2025 switch (type & 0xff) {
2026 case SLJIT_MOV:
2027 case SLJIT_MOV_P:
2028 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
2029 break;
2030 case SLJIT_MOV_S8:
2031 sign = 1;
2032 /* fallthrough */
2033 case SLJIT_MOV_U8:
2034 inst = STURBI | (MEM_SIZE_SHIFT(BYTE_SIZE) << 30) | 0x400;
2035 break;
2036 case SLJIT_MOV_S16:
2037 sign = 1;
2038 /* fallthrough */
2039 case SLJIT_MOV_U16:
2040 inst = STURBI | (MEM_SIZE_SHIFT(HALF_SIZE) << 30) | 0x400;
2041 break;
2042 case SLJIT_MOV_S32:
2043 sign = 1;
2044 /* fallthrough */
2045 case SLJIT_MOV_U32:
2046 case SLJIT_MOV32:
2047 inst = STURBI | (MEM_SIZE_SHIFT(INT_SIZE) << 30) | 0x400;
2048 break;
2049 default:
2050 SLJIT_UNREACHABLE();
2051 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
2052 break;
2055 if (!(type & SLJIT_MEM_STORE))
2056 inst |= sign ? 0x00800000 : 0x00400000;
2058 if (type & SLJIT_MEM_PRE)
2059 inst |= 0x800;
2061 return push_inst(compiler, inst | RT(reg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
2064 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
2065 sljit_s32 freg,
2066 sljit_s32 mem, sljit_sw memw)
2068 sljit_u32 inst;
2070 CHECK_ERROR();
2071 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
2073 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
2074 return SLJIT_ERR_UNSUPPORTED;
2076 if (type & SLJIT_MEM_SUPP)
2077 return SLJIT_SUCCESS;
2079 inst = STUR_FI | 0x80000400;
2081 if (!(type & SLJIT_32))
2082 inst |= 0x40000000;
2084 if (!(type & SLJIT_MEM_STORE))
2085 inst |= 0x00400000;
2087 if (type & SLJIT_MEM_PRE)
2088 inst |= 0x800;
2090 return push_inst(compiler, inst | VT(freg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
2093 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset)
2095 sljit_s32 dst_reg;
2096 sljit_ins ins;
2098 CHECK_ERROR();
2099 CHECK(check_sljit_get_local_base(compiler, dst, dstw, offset));
2100 ADJUST_LOCAL_OFFSET(SLJIT_MEM1(SLJIT_SP), offset);
2102 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2104 /* Not all instruction forms support accessing SP register. */
2105 if (offset <= 0xffffff && offset >= -0xffffff) {
2106 ins = ADDI;
2107 if (offset < 0) {
2108 offset = -offset;
2109 ins = SUBI;
2112 if (offset <= 0xfff)
2113 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)(offset << 10)));
2114 else {
2115 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)((offset & 0xfff000) >> (12 - 10)) | (1 << 22)));
2117 offset &= 0xfff;
2118 if (offset != 0)
2119 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(dst_reg) | (sljit_ins)(offset << 10)));
2122 else {
2123 FAIL_IF(load_immediate (compiler, dst_reg, offset));
2124 /* Add extended register form. */
2125 FAIL_IF(push_inst(compiler, ADDE | (0x3 << 13) | RD(dst_reg) | RN(SLJIT_SP) | RM(dst_reg)));
2128 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
2129 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG1);
2130 return SLJIT_SUCCESS;
2133 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2135 struct sljit_const *const_;
2136 sljit_s32 dst_r;
2138 CHECK_ERROR_PTR();
2139 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2140 ADJUST_LOCAL_OFFSET(dst, dstw);
2142 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2143 PTR_FAIL_IF(!const_);
2144 set_const(const_, compiler);
2146 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2147 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, (sljit_uw)init_value));
2149 if (dst & SLJIT_MEM)
2150 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2151 return const_;
2154 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2156 struct sljit_put_label *put_label;
2157 sljit_s32 dst_r;
2159 CHECK_ERROR_PTR();
2160 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2161 ADJUST_LOCAL_OFFSET(dst, dstw);
2163 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2164 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, 0));
2166 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2167 PTR_FAIL_IF(!put_label);
2168 set_put_label(put_label, compiler, 1);
2170 if (dst & SLJIT_MEM)
2171 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2173 return put_label;
2176 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2178 sljit_ins* inst = (sljit_ins*)addr;
2179 sljit_u32 dst;
2180 SLJIT_UNUSED_ARG(executable_offset);
2182 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2184 dst = inst[0] & 0x1f;
2185 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
2186 inst[0] = MOVZ | dst | (((sljit_u32)new_target & 0xffff) << 5);
2187 inst[1] = MOVK | dst | (((sljit_u32)(new_target >> 16) & 0xffff) << 5) | (1 << 21);
2188 inst[2] = MOVK | dst | (((sljit_u32)(new_target >> 32) & 0xffff) << 5) | (2 << 21);
2189 inst[3] = MOVK | dst | ((sljit_u32)(new_target >> 48) << 5) | (3 << 21);
2191 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2192 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2193 SLJIT_CACHE_FLUSH(inst, inst + 4);
2196 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2198 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);