Support incoming arguments in scratch registers.
[sljit.git] / sljit_src / sljitNativeARM_64.c
blob6c590991f9c747e2012f8f4ad673c07e8d4e8c8a
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 /* dest_reg is the absolute name of the register
141 Useful for reordering instructions in the delay slot. */
142 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
144 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
145 FAIL_IF(!ptr);
146 *ptr = ins;
147 compiler->size++;
148 return SLJIT_SUCCESS;
151 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
153 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)(imm & 0xffff) << 5)));
154 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 16) & 0xffff) << 5) | (1 << 21)));
155 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)(imm >> 32) & 0xffff) << 5) | (2 << 21)));
156 return push_inst(compiler, MOVK | RD(dst) | ((sljit_ins)(imm >> 48) << 5) | (3 << 21));
159 static SLJIT_INLINE sljit_sw detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
161 sljit_sw diff;
162 sljit_uw target_addr;
164 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
165 jump->flags |= PATCH_ABS64;
166 return 0;
169 if (jump->flags & JUMP_ADDR)
170 target_addr = jump->u.target;
171 else {
172 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
173 target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
176 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
178 if (jump->flags & IS_COND) {
179 diff += SSIZE_OF(ins);
180 if (diff <= 0xfffff && diff >= -0x100000) {
181 code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
182 jump->addr -= sizeof(sljit_ins);
183 jump->flags |= PATCH_COND;
184 return 5;
186 diff -= SSIZE_OF(ins);
189 if (diff <= 0x7ffffff && diff >= -0x8000000) {
190 jump->flags |= PATCH_B;
191 return 4;
194 if (target_addr < 0x100000000l) {
195 if (jump->flags & IS_COND)
196 code_ptr[-5] -= (2 << 5);
197 code_ptr[-2] = code_ptr[0];
198 return 2;
201 if (target_addr < 0x1000000000000l) {
202 if (jump->flags & IS_COND)
203 code_ptr[-5] -= (1 << 5);
204 jump->flags |= PATCH_ABS48;
205 code_ptr[-1] = code_ptr[0];
206 return 1;
209 jump->flags |= PATCH_ABS64;
210 return 0;
213 static SLJIT_INLINE sljit_sw put_label_get_length(struct sljit_put_label *put_label, sljit_uw max_label)
215 if (max_label < 0x100000000l) {
216 put_label->flags = 0;
217 return 2;
220 if (max_label < 0x1000000000000l) {
221 put_label->flags = 1;
222 return 1;
225 put_label->flags = 2;
226 return 0;
229 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
231 struct sljit_memory_fragment *buf;
232 sljit_ins *code;
233 sljit_ins *code_ptr;
234 sljit_ins *buf_ptr;
235 sljit_ins *buf_end;
236 sljit_uw word_count;
237 sljit_uw next_addr;
238 sljit_sw executable_offset;
239 sljit_sw addr;
240 sljit_u32 dst;
242 struct sljit_label *label;
243 struct sljit_jump *jump;
244 struct sljit_const *const_;
245 struct sljit_put_label *put_label;
247 CHECK_ERROR_PTR();
248 CHECK_PTR(check_sljit_generate_code(compiler));
249 reverse_buf(compiler);
251 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins), compiler->exec_allocator_data);
252 PTR_FAIL_WITH_EXEC_IF(code);
253 buf = compiler->buf;
255 code_ptr = code;
256 word_count = 0;
257 next_addr = 0;
258 executable_offset = SLJIT_EXEC_OFFSET(code);
260 label = compiler->labels;
261 jump = compiler->jumps;
262 const_ = compiler->consts;
263 put_label = compiler->put_labels;
265 do {
266 buf_ptr = (sljit_ins*)buf->memory;
267 buf_end = buf_ptr + (buf->used_size >> 2);
268 do {
269 *code_ptr = *buf_ptr++;
270 if (next_addr == word_count) {
271 SLJIT_ASSERT(!label || label->size >= word_count);
272 SLJIT_ASSERT(!jump || jump->addr >= word_count);
273 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
274 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
276 /* These structures are ordered by their address. */
277 if (label && label->size == word_count) {
278 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
279 label->size = (sljit_uw)(code_ptr - code);
280 label = label->next;
282 if (jump && jump->addr == word_count) {
283 jump->addr = (sljit_uw)(code_ptr - 4);
284 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
285 jump = jump->next;
287 if (const_ && const_->addr == word_count) {
288 const_->addr = (sljit_uw)code_ptr;
289 const_ = const_->next;
291 if (put_label && put_label->addr == word_count) {
292 SLJIT_ASSERT(put_label->label);
293 put_label->addr = (sljit_uw)(code_ptr - 3);
294 code_ptr -= put_label_get_length(put_label, (sljit_uw)(SLJIT_ADD_EXEC_OFFSET(code, executable_offset) + put_label->label->size));
295 put_label = put_label->next;
297 next_addr = compute_next_addr(label, jump, const_, put_label);
299 code_ptr ++;
300 word_count ++;
301 } while (buf_ptr < buf_end);
303 buf = buf->next;
304 } while (buf);
306 if (label && label->size == word_count) {
307 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
308 label->size = (sljit_uw)(code_ptr - code);
309 label = label->next;
312 SLJIT_ASSERT(!label);
313 SLJIT_ASSERT(!jump);
314 SLJIT_ASSERT(!const_);
315 SLJIT_ASSERT(!put_label);
316 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
318 jump = compiler->jumps;
319 while (jump) {
320 do {
321 addr = (sljit_sw)((jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
322 buf_ptr = (sljit_ins *)jump->addr;
324 if (jump->flags & PATCH_B) {
325 addr = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
326 SLJIT_ASSERT(addr <= 0x1ffffff && addr >= -0x2000000);
327 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (sljit_ins)(addr & 0x3ffffff);
328 if (jump->flags & IS_COND)
329 buf_ptr[-1] -= (4 << 5);
330 break;
332 if (jump->flags & PATCH_COND) {
333 addr = (addr - (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
334 SLJIT_ASSERT(addr <= 0x3ffff && addr >= -0x40000);
335 buf_ptr[0] = (buf_ptr[0] & ~(sljit_ins)0xffffe0) | (sljit_ins)((addr & 0x7ffff) << 5);
336 break;
339 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || (sljit_uw)addr <= (sljit_uw)0xffffffff);
340 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || (sljit_uw)addr <= (sljit_uw)0xffffffffffff);
342 dst = buf_ptr[0] & 0x1f;
343 buf_ptr[0] = MOVZ | dst | (((sljit_ins)addr & 0xffff) << 5);
344 buf_ptr[1] = MOVK | dst | (((sljit_ins)(addr >> 16) & 0xffff) << 5) | (1 << 21);
345 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
346 buf_ptr[2] = MOVK | dst | (((sljit_ins)(addr >> 32) & 0xffff) << 5) | (2 << 21);
347 if (jump->flags & PATCH_ABS64)
348 buf_ptr[3] = MOVK | dst | ((sljit_ins)(addr >> 48) << 5) | (3 << 21);
349 } while (0);
350 jump = jump->next;
353 put_label = compiler->put_labels;
354 while (put_label) {
355 addr = (sljit_sw)put_label->label->addr;
356 buf_ptr = (sljit_ins*)put_label->addr;
358 buf_ptr[0] |= ((sljit_ins)addr & 0xffff) << 5;
359 buf_ptr[1] |= ((sljit_ins)(addr >> 16) & 0xffff) << 5;
361 if (put_label->flags >= 1)
362 buf_ptr[2] |= ((sljit_ins)(addr >> 32) & 0xffff) << 5;
364 if (put_label->flags >= 2)
365 buf_ptr[3] |= (sljit_ins)(addr >> 48) << 5;
367 put_label = put_label->next;
370 compiler->error = SLJIT_ERR_COMPILED;
371 compiler->executable_offset = executable_offset;
372 compiler->executable_size = (sljit_uw)(code_ptr - code) * sizeof(sljit_ins);
374 code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
375 code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
377 SLJIT_CACHE_FLUSH(code, code_ptr);
378 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
379 return code;
382 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
384 switch (feature_type) {
385 case SLJIT_HAS_FPU:
386 #ifdef SLJIT_IS_FPU_AVAILABLE
387 return SLJIT_IS_FPU_AVAILABLE;
388 #else
389 /* Available by default. */
390 return 1;
391 #endif
393 case SLJIT_HAS_CLZ:
394 case SLJIT_HAS_CMOV:
395 case SLJIT_HAS_PREFETCH:
396 return 1;
398 default:
399 return 0;
403 /* --------------------------------------------------------------------- */
404 /* Core code generator functions. */
405 /* --------------------------------------------------------------------- */
407 #define COUNT_TRAILING_ZERO(value, result) \
408 result = 0; \
409 if (!(value & 0xffffffff)) { \
410 result += 32; \
411 value >>= 32; \
413 if (!(value & 0xffff)) { \
414 result += 16; \
415 value >>= 16; \
417 if (!(value & 0xff)) { \
418 result += 8; \
419 value >>= 8; \
421 if (!(value & 0xf)) { \
422 result += 4; \
423 value >>= 4; \
425 if (!(value & 0x3)) { \
426 result += 2; \
427 value >>= 2; \
429 if (!(value & 0x1)) { \
430 result += 1; \
431 value >>= 1; \
434 #define LOGICAL_IMM_CHECK (sljit_ins)0x100
436 static sljit_ins logical_imm(sljit_sw imm, sljit_u32 len)
438 sljit_s32 negated;
439 sljit_u32 ones, right;
440 sljit_uw mask, uimm;
441 sljit_ins ins;
443 if (len & LOGICAL_IMM_CHECK) {
444 len &= ~LOGICAL_IMM_CHECK;
445 if (len == 32 && (imm == 0 || imm == -1))
446 return 0;
447 if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
448 return 0;
451 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
452 || (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
454 uimm = (sljit_uw)imm;
455 while (1) {
456 if (len <= 0) {
457 SLJIT_UNREACHABLE();
458 return 0;
461 mask = ((sljit_uw)1 << len) - 1;
462 if ((uimm & mask) != ((uimm >> len) & mask))
463 break;
464 len >>= 1;
467 len <<= 1;
469 negated = 0;
470 if (uimm & 0x1) {
471 negated = 1;
472 uimm = ~uimm;
475 if (len < 64)
476 uimm &= ((sljit_uw)1 << len) - 1;
478 /* Unsigned right shift. */
479 COUNT_TRAILING_ZERO(uimm, right);
481 /* Signed shift. We also know that the highest bit is set. */
482 imm = (sljit_sw)~uimm;
483 SLJIT_ASSERT(imm < 0);
485 COUNT_TRAILING_ZERO(imm, ones);
487 if (~imm)
488 return 0;
490 if (len == 64)
491 ins = 1 << 22;
492 else
493 ins = (0x3f - ((len << 1) - 1)) << 10;
495 if (negated)
496 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
498 return ins | ((ones - 1) << 10) | ((len - right) << 16);
501 #undef COUNT_TRAILING_ZERO
503 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
505 sljit_uw imm = (sljit_uw)simm;
506 sljit_u32 i, zeros, ones, first;
507 sljit_ins bitmask;
509 /* Handling simple immediates first. */
510 if (imm <= 0xffff)
511 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)imm << 5));
513 if (simm < 0 && simm >= -0x10000)
514 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
516 if (imm <= 0xffffffffl) {
517 if ((imm & 0xffff) == 0)
518 return push_inst(compiler, MOVZ | RD(dst) | ((sljit_ins)(imm >> 16) << 5) | (1 << 21));
519 if ((imm & 0xffff0000l) == 0xffff0000)
520 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5));
521 if ((imm & 0xffff) == 0xffff)
522 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
524 bitmask = logical_imm(simm, 16);
525 if (bitmask != 0)
526 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
528 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)imm & 0xffff) << 5)));
529 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
532 bitmask = logical_imm(simm, 32);
533 if (bitmask != 0)
534 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
536 if (simm < 0 && simm >= -0x100000000l) {
537 if ((imm & 0xffff) == 0xffff)
538 return push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
540 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)~imm & 0xffff) << 5)));
541 return push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)imm & 0xffff0000u) >> (16 - 5)) | (1 << 21));
544 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
546 zeros = 0;
547 ones = 0;
548 for (i = 4; i > 0; i--) {
549 if ((simm & 0xffff) == 0)
550 zeros++;
551 if ((simm & 0xffff) == 0xffff)
552 ones++;
553 simm >>= 16;
556 simm = (sljit_sw)imm;
557 first = 1;
558 if (ones > zeros) {
559 simm = ~simm;
560 for (i = 0; i < 4; i++) {
561 if (!(simm & 0xffff)) {
562 simm >>= 16;
563 continue;
565 if (first) {
566 first = 0;
567 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
569 else
570 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)~simm & 0xffff) << 5) | (i << 21)));
571 simm >>= 16;
573 return SLJIT_SUCCESS;
576 for (i = 0; i < 4; i++) {
577 if (!(simm & 0xffff)) {
578 simm >>= 16;
579 continue;
581 if (first) {
582 first = 0;
583 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
585 else
586 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((sljit_ins)simm & 0xffff) << 5) | (i << 21)));
587 simm >>= 16;
589 return SLJIT_SUCCESS;
592 #define ARG1_IMM 0x0010000
593 #define ARG2_IMM 0x0020000
594 #define INT_OP 0x0040000
595 #define SET_FLAGS 0x0080000
596 #define UNUSED_RETURN 0x0100000
598 #define CHECK_FLAGS(flag_bits) \
599 if (flags & SET_FLAGS) { \
600 inv_bits |= flag_bits; \
601 if (flags & UNUSED_RETURN) \
602 dst = TMP_ZERO; \
605 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
607 /* dst must be register, TMP_REG1
608 arg1 must be register, TMP_REG1, imm
609 arg2 must be register, TMP_REG2, imm */
610 sljit_ins inv_bits = (flags & INT_OP) ? W_OP : 0;
611 sljit_ins inst_bits;
612 sljit_s32 op = (flags & 0xffff);
613 sljit_s32 reg;
614 sljit_sw imm, nimm;
616 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
617 /* Both are immediates. */
618 flags &= ~ARG1_IMM;
619 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
620 arg1 = TMP_ZERO;
621 else {
622 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
623 arg1 = TMP_REG1;
627 if (flags & (ARG1_IMM | ARG2_IMM)) {
628 reg = (sljit_s32)((flags & ARG2_IMM) ? arg1 : arg2);
629 imm = (flags & ARG2_IMM) ? arg2 : arg1;
631 switch (op) {
632 case SLJIT_MUL:
633 case SLJIT_NEG:
634 case SLJIT_CLZ:
635 case SLJIT_ADDC:
636 case SLJIT_SUBC:
637 /* No form with immediate operand (except imm 0, which
638 is represented by a ZERO register). */
639 break;
640 case SLJIT_MOV:
641 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
642 return load_immediate(compiler, dst, imm);
643 case SLJIT_NOT:
644 SLJIT_ASSERT(flags & ARG2_IMM);
645 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
646 goto set_flags;
647 case SLJIT_SUB:
648 if (flags & ARG1_IMM)
649 break;
650 imm = -imm;
651 /* Fall through. */
652 case SLJIT_ADD:
653 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
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_NEG:
795 SLJIT_ASSERT(arg1 == TMP_REG1);
796 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
797 if (flags & SET_FLAGS)
798 inv_bits |= 1 << 29;
799 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
800 case SLJIT_CLZ:
801 SLJIT_ASSERT(arg1 == TMP_REG1);
802 return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
803 case SLJIT_ADD:
804 CHECK_FLAGS(1 << 29);
805 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
806 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
807 case SLJIT_ADDC:
808 CHECK_FLAGS(1 << 29);
809 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
810 case SLJIT_SUB:
811 CHECK_FLAGS(1 << 29);
812 compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD_SUB;
813 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
814 case SLJIT_SUBC:
815 CHECK_FLAGS(1 << 29);
816 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
817 case SLJIT_MUL:
818 compiler->status_flags_state = 0;
819 if (!(flags & SET_FLAGS))
820 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
821 if (flags & INT_OP) {
822 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
823 FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
824 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
826 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
827 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
828 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
829 case SLJIT_AND:
830 CHECK_FLAGS(3 << 29);
831 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
832 case SLJIT_OR:
833 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
834 break; /* Set flags. */
835 case SLJIT_XOR:
836 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
837 break; /* Set flags. */
838 case SLJIT_SHL:
839 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
840 break; /* Set flags. */
841 case SLJIT_LSHR:
842 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
843 break; /* Set flags. */
844 case SLJIT_ASHR:
845 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
846 break; /* Set flags. */
847 default:
848 SLJIT_UNREACHABLE();
849 return SLJIT_SUCCESS;
852 set_flags:
853 if (flags & SET_FLAGS)
854 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
855 return SLJIT_SUCCESS;
858 #define STORE 0x10
859 #define SIGNED 0x20
861 #define BYTE_SIZE 0x0
862 #define HALF_SIZE 0x1
863 #define INT_SIZE 0x2
864 #define WORD_SIZE 0x3
866 #define MEM_SIZE_SHIFT(flags) ((sljit_ins)(flags) & 0x3)
868 static sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
869 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
871 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
872 sljit_u32 type = (shift << 30);
874 if (!(flags & STORE))
875 type |= (flags & SIGNED) ? 0x00800000 : 0x00400000;
877 SLJIT_ASSERT(arg & SLJIT_MEM);
879 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
880 argw &= 0x3;
882 if (argw == 0 || argw == shift)
883 return push_inst(compiler, STRB | type | RT(reg)
884 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
886 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
887 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg));
890 arg &= REG_MASK;
892 if (!arg) {
893 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~(0xfff << shift)));
895 argw = (argw >> shift) & 0xfff;
897 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
900 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
901 if ((argw >> shift) <= 0xfff)
902 return push_inst(compiler, STRBI | type | RT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
904 if (argw <= 0xffffff) {
905 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_reg) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
907 argw = ((argw & 0xfff) >> shift);
908 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | ((sljit_ins)argw << 10));
912 if (argw <= 255 && argw >= -256)
913 return push_inst(compiler, STURBI | type | RT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
915 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
917 return push_inst(compiler, STRB | type | RT(reg) | RN(arg) | RM(tmp_reg));
920 /* --------------------------------------------------------------------- */
921 /* Entry, exit */
922 /* --------------------------------------------------------------------- */
924 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
925 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
926 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
928 sljit_s32 prev, fprev, saved_regs_size, i, tmp;
929 sljit_s32 word_arg_count = 0;
930 sljit_ins offs;
932 CHECK_ERROR();
933 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
934 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
936 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
937 saved_regs_size += GET_SAVED_FLOAT_REGISTERS_SIZE(fscratches, fsaveds, SSIZE_OF(f64));
939 local_size = (local_size + saved_regs_size + 0xf) & ~0xf;
940 compiler->local_size = local_size;
942 if (local_size <= 512) {
943 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
944 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
945 offs = (sljit_ins)(local_size - 2 * SSIZE_OF(sw)) << (15 - 3);
946 local_size = 0;
947 } else {
948 saved_regs_size = ((saved_regs_size - 2 * SSIZE_OF(sw)) + 0xf) & ~0xf;
950 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)saved_regs_size << 10)));
951 offs = (sljit_ins)(saved_regs_size - 2 * SSIZE_OF(sw)) << (15 - 3);
952 local_size -= saved_regs_size;
953 SLJIT_ASSERT(local_size > 0);
956 prev = -1;
958 tmp = SLJIT_S0 - saveds;
959 for (i = SLJIT_S0; i > tmp; i--) {
960 if (prev == -1) {
961 prev = i;
962 continue;
964 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
965 offs -= (sljit_ins)2 << 15;
966 prev = -1;
969 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
970 if (prev == -1) {
971 prev = i;
972 continue;
974 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
975 offs -= (sljit_ins)2 << 15;
976 prev = -1;
979 fprev = -1;
981 tmp = SLJIT_FS0 - fsaveds;
982 for (i = SLJIT_FS0; i > tmp; i--) {
983 if (fprev == -1) {
984 fprev = i;
985 continue;
987 FAIL_IF(push_inst(compiler, STP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
988 offs -= (sljit_ins)2 << 15;
989 fprev = -1;
992 for (i = fscratches; i >= SLJIT_FIRST_SAVED_FLOAT_REG; i--) {
993 if (fprev == -1) {
994 fprev = i;
995 continue;
997 FAIL_IF(push_inst(compiler, STP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
998 offs -= (sljit_ins)2 << 15;
999 fprev = -1;
1002 if (fprev != -1)
1003 FAIL_IF(push_inst(compiler, STRI_F64 | VT(fprev) | RN(SLJIT_SP) | (offs >> 5) | (1 << 10)));
1005 if (prev != -1)
1006 FAIL_IF(push_inst(compiler, STRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5) | ((fprev == -1) ? (1 << 10) : 0)));
1008 arg_types >>= SLJIT_ARG_SHIFT;
1010 #ifdef _WIN32
1011 if (local_size > 4096)
1012 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1013 #endif /* _WIN32 */
1015 tmp = 0;
1016 while (arg_types > 0) {
1017 if ((arg_types & SLJIT_ARG_MASK) < SLJIT_ARG_TYPE_F64) {
1018 if (!(arg_types & SLJIT_ARG_TYPE_SCRATCH_REG)) {
1019 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0 - tmp) | RN(TMP_ZERO) | RM(SLJIT_R0 + word_arg_count)));
1020 tmp++;
1022 word_arg_count++;
1024 arg_types >>= SLJIT_ARG_SHIFT;
1027 #ifdef _WIN32
1028 if (local_size > 4096) {
1029 if (local_size < 4 * 4096) {
1030 /* No need for a loop. */
1032 if (local_size >= 2 * 4096) {
1033 if (local_size >= 3 * 4096) {
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 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1039 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1042 else {
1043 FAIL_IF(push_inst(compiler, MOVZ | RD(TMP_REG1) | ((((sljit_ins)local_size >> 12) - 1) << 5)));
1044 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1045 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
1046 FAIL_IF(push_inst(compiler, SUBI | (1 << 29) | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10)));
1047 FAIL_IF(push_inst(compiler, B_CC | ((((sljit_ins) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1050 local_size &= 0xfff;
1052 if (local_size > 0)
1053 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(SLJIT_SP)));
1054 else
1055 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1058 if (local_size > 0) {
1059 if (local_size <= 512)
1060 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1061 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
1062 else {
1063 if (local_size >= 4096)
1064 local_size = (1 << (22 - 10));
1066 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1067 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1071 #else /* !_WIN32 */
1073 /* The local_size does not include saved registers size. */
1074 if (local_size != 0) {
1075 if (local_size > 0xfff) {
1076 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1077 local_size &= 0xfff;
1080 if (local_size > 512 || local_size == 0) {
1081 if (local_size != 0)
1082 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1084 FAIL_IF(push_inst(compiler, STP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1085 } else
1086 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1087 | RN(SLJIT_SP) | (sljit_ins)((-(local_size >> 3) & 0x7f) << 15)));
1090 #endif /* _WIN32 */
1092 return push_inst(compiler, ADDI | RD(TMP_FP) | RN(SLJIT_SP) | (0 << 10));
1095 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1096 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1097 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1099 sljit_s32 saved_regs_size;
1101 CHECK_ERROR();
1102 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1103 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1105 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
1106 saved_regs_size += GET_SAVED_FLOAT_REGISTERS_SIZE(fscratches, fsaveds, SSIZE_OF(f64));
1108 compiler->local_size = (local_size + saved_regs_size + 0xf) & ~0xf;
1109 return SLJIT_SUCCESS;
1112 static sljit_s32 emit_stack_frame_release(struct sljit_compiler *compiler)
1114 sljit_s32 local_size, prev, fprev, i, tmp;
1115 sljit_ins offs;
1117 local_size = compiler->local_size;
1119 if (local_size > 512 && local_size <= 512 + 496) {
1120 FAIL_IF(push_inst(compiler, LDP_POST | RT(TMP_FP) | RT2(TMP_LR)
1121 | RN(SLJIT_SP) | ((sljit_ins)(local_size - 512) << (15 - 3))));
1122 local_size = 512;
1123 } else
1124 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1126 if (local_size > 512) {
1127 local_size -= 512;
1128 if (local_size > 0xfff) {
1129 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP)
1130 | (((sljit_ins)local_size >> 12) << 10) | (1 << 22)));
1131 local_size &= 0xfff;
1134 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((sljit_ins)local_size << 10)));
1135 local_size = 512;
1138 offs = (sljit_ins)(local_size - 2 * SSIZE_OF(sw)) << (15 - 3);
1139 prev = -1;
1141 tmp = SLJIT_S0 - compiler->saveds;
1142 for (i = SLJIT_S0; i > tmp; i--) {
1143 if (prev == -1) {
1144 prev = i;
1145 continue;
1147 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1148 offs -= (sljit_ins)2 << 15;
1149 prev = -1;
1152 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1153 if (prev == -1) {
1154 prev = i;
1155 continue;
1157 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1158 offs -= (sljit_ins)2 << 15;
1159 prev = -1;
1162 fprev = -1;
1164 tmp = SLJIT_FS0 - compiler->fsaveds;
1165 for (i = SLJIT_FS0; i > tmp; i--) {
1166 if (fprev == -1) {
1167 fprev = i;
1168 continue;
1170 FAIL_IF(push_inst(compiler, LDP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
1171 offs -= (sljit_ins)2 << 15;
1172 fprev = -1;
1175 for (i = compiler->fscratches; i >= SLJIT_FIRST_SAVED_FLOAT_REG; i--) {
1176 if (fprev == -1) {
1177 fprev = i;
1178 continue;
1180 FAIL_IF(push_inst(compiler, LDP_F64 | VT(fprev) | VT2(i) | RN(SLJIT_SP) | offs));
1181 offs -= (sljit_ins)2 << 15;
1182 fprev = -1;
1185 if (fprev != -1)
1186 FAIL_IF(push_inst(compiler, LDRI_F64 | VT(fprev) | RN(SLJIT_SP) | (offs >> 5) | (1 << 10)));
1188 if (prev != -1)
1189 FAIL_IF(push_inst(compiler, LDRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5) | ((fprev == -1) ? (1 << 10) : 0)));
1191 /* This and the next call/jump instruction can be executed parallelly. */
1192 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (sljit_ins)(local_size << 10));
1195 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return_void(struct sljit_compiler *compiler)
1197 CHECK_ERROR();
1198 CHECK(check_sljit_emit_return_void(compiler));
1200 FAIL_IF(emit_stack_frame_release(compiler));
1202 return push_inst(compiler, RET | RN(TMP_LR));
1205 /* --------------------------------------------------------------------- */
1206 /* Operators */
1207 /* --------------------------------------------------------------------- */
1209 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1211 sljit_ins inv_bits = (op & SLJIT_32) ? W_OP : 0;
1213 CHECK_ERROR();
1214 CHECK(check_sljit_emit_op0(compiler, op));
1216 op = GET_OPCODE(op);
1217 switch (op) {
1218 case SLJIT_BREAKPOINT:
1219 return push_inst(compiler, BRK);
1220 case SLJIT_NOP:
1221 return push_inst(compiler, NOP);
1222 case SLJIT_LMUL_UW:
1223 case SLJIT_LMUL_SW:
1224 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1225 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1226 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1227 case SLJIT_DIVMOD_UW:
1228 case SLJIT_DIVMOD_SW:
1229 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1230 FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1231 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1232 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1233 case SLJIT_DIV_UW:
1234 case SLJIT_DIV_SW:
1235 return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1236 case SLJIT_ENDBR:
1237 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1238 return SLJIT_SUCCESS;
1241 return SLJIT_SUCCESS;
1244 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1245 sljit_s32 dst, sljit_sw dstw,
1246 sljit_s32 src, sljit_sw srcw)
1248 sljit_s32 dst_r, flags, mem_flags;
1249 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1251 CHECK_ERROR();
1252 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1253 ADJUST_LOCAL_OFFSET(dst, dstw);
1254 ADJUST_LOCAL_OFFSET(src, srcw);
1256 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1258 op = GET_OPCODE(op);
1259 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1260 /* Both operands are registers. */
1261 if (dst_r != TMP_REG1 && FAST_IS_REG(src))
1262 return emit_op_imm(compiler, op | ((op_flags & SLJIT_32) ? INT_OP : 0), dst_r, TMP_REG1, src);
1264 switch (op) {
1265 case SLJIT_MOV:
1266 case SLJIT_MOV_P:
1267 mem_flags = WORD_SIZE;
1268 break;
1269 case SLJIT_MOV_U8:
1270 mem_flags = BYTE_SIZE;
1271 if (src & SLJIT_IMM)
1272 srcw = (sljit_u8)srcw;
1273 break;
1274 case SLJIT_MOV_S8:
1275 mem_flags = BYTE_SIZE | SIGNED;
1276 if (src & SLJIT_IMM)
1277 srcw = (sljit_s8)srcw;
1278 break;
1279 case SLJIT_MOV_U16:
1280 mem_flags = HALF_SIZE;
1281 if (src & SLJIT_IMM)
1282 srcw = (sljit_u16)srcw;
1283 break;
1284 case SLJIT_MOV_S16:
1285 mem_flags = HALF_SIZE | SIGNED;
1286 if (src & SLJIT_IMM)
1287 srcw = (sljit_s16)srcw;
1288 break;
1289 case SLJIT_MOV_U32:
1290 mem_flags = INT_SIZE;
1291 if (src & SLJIT_IMM)
1292 srcw = (sljit_u32)srcw;
1293 break;
1294 case SLJIT_MOV_S32:
1295 case SLJIT_MOV32:
1296 mem_flags = INT_SIZE | SIGNED;
1297 if (src & SLJIT_IMM)
1298 srcw = (sljit_s32)srcw;
1299 break;
1300 default:
1301 SLJIT_UNREACHABLE();
1302 mem_flags = 0;
1303 break;
1306 if (src & SLJIT_IMM)
1307 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1308 else if (!(src & SLJIT_MEM))
1309 dst_r = src;
1310 else
1311 FAIL_IF(emit_op_mem(compiler, mem_flags, dst_r, src, srcw, TMP_REG1));
1313 if (dst & SLJIT_MEM)
1314 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1315 return SLJIT_SUCCESS;
1318 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1319 mem_flags = WORD_SIZE;
1321 if (op_flags & SLJIT_32) {
1322 flags |= INT_OP;
1323 mem_flags = INT_SIZE;
1326 if (src & SLJIT_MEM) {
1327 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src, srcw, TMP_REG2));
1328 src = TMP_REG2;
1331 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, src);
1333 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1334 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1335 return SLJIT_SUCCESS;
1338 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1339 sljit_s32 dst, sljit_sw dstw,
1340 sljit_s32 src1, sljit_sw src1w,
1341 sljit_s32 src2, sljit_sw src2w)
1343 sljit_s32 dst_r, flags, mem_flags;
1345 CHECK_ERROR();
1346 CHECK(check_sljit_emit_op2(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w));
1347 ADJUST_LOCAL_OFFSET(dst, dstw);
1348 ADJUST_LOCAL_OFFSET(src1, src1w);
1349 ADJUST_LOCAL_OFFSET(src2, src2w);
1351 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1352 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1353 mem_flags = WORD_SIZE;
1355 if (op & SLJIT_32) {
1356 flags |= INT_OP;
1357 mem_flags = INT_SIZE;
1360 if (dst == TMP_REG1)
1361 flags |= UNUSED_RETURN;
1363 if (src1 & SLJIT_MEM) {
1364 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, src1, src1w, TMP_REG1));
1365 src1 = TMP_REG1;
1368 if (src2 & SLJIT_MEM) {
1369 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src2, src2w, TMP_REG2));
1370 src2 = TMP_REG2;
1373 if (src1 & SLJIT_IMM)
1374 flags |= ARG1_IMM;
1375 else
1376 src1w = src1;
1378 if (src2 & SLJIT_IMM)
1379 flags |= ARG2_IMM;
1380 else
1381 src2w = src2;
1383 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1385 if (dst & SLJIT_MEM)
1386 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1387 return SLJIT_SUCCESS;
1390 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2u(struct sljit_compiler *compiler, sljit_s32 op,
1391 sljit_s32 src1, sljit_sw src1w,
1392 sljit_s32 src2, sljit_sw src2w)
1394 CHECK_ERROR();
1395 CHECK(check_sljit_emit_op2(compiler, op, 1, 0, 0, src1, src1w, src2, src2w));
1397 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1398 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1399 compiler->skip_checks = 1;
1400 #endif
1401 return sljit_emit_op2(compiler, op, TMP_REG1, 0, src1, src1w, src2, src2w);
1404 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1405 sljit_s32 src, sljit_sw srcw)
1407 CHECK_ERROR();
1408 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1409 ADJUST_LOCAL_OFFSET(src, srcw);
1411 switch (op) {
1412 case SLJIT_FAST_RETURN:
1413 if (FAST_IS_REG(src))
1414 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1415 else
1416 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw, TMP_REG1));
1418 return push_inst(compiler, RET | RN(TMP_LR));
1419 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1420 return SLJIT_SUCCESS;
1421 case SLJIT_PREFETCH_L1:
1422 case SLJIT_PREFETCH_L2:
1423 case SLJIT_PREFETCH_L3:
1424 case SLJIT_PREFETCH_ONCE:
1425 SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1427 /* The reg_map[op] should provide the appropriate constant. */
1428 if (op == SLJIT_PREFETCH_L1)
1429 op = 1;
1430 else if (op == SLJIT_PREFETCH_L2)
1431 op = 3;
1432 else if (op == SLJIT_PREFETCH_L3)
1433 op = 5;
1434 else
1435 op = 2;
1437 /* Signed word sized load is the prefetch instruction. */
1438 return emit_op_mem(compiler, WORD_SIZE | SIGNED, op, src, srcw, TMP_REG1);
1441 return SLJIT_SUCCESS;
1444 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1446 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1447 return reg_map[reg];
1450 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1452 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1453 return freg_map[reg];
1456 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1457 void *instruction, sljit_u32 size)
1459 CHECK_ERROR();
1460 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1462 return push_inst(compiler, *(sljit_ins*)instruction);
1465 /* --------------------------------------------------------------------- */
1466 /* Floating point operators */
1467 /* --------------------------------------------------------------------- */
1469 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1471 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1472 sljit_ins type = (shift << 30);
1474 SLJIT_ASSERT(arg & SLJIT_MEM);
1476 if (!(flags & STORE))
1477 type |= 0x00400000;
1479 if (arg & OFFS_REG_MASK) {
1480 argw &= 3;
1481 if (argw == 0 || argw == shift)
1482 return push_inst(compiler, STR_FR | type | VT(reg)
1483 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1485 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((sljit_ins)argw << 10)));
1486 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1));
1489 arg &= REG_MASK;
1491 if (!arg) {
1492 FAIL_IF(load_immediate(compiler, TMP_REG1, argw & ~(0xfff << shift)));
1494 argw = (argw >> shift) & 0xfff;
1496 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1499 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
1500 if ((argw >> shift) <= 0xfff)
1501 return push_inst(compiler, STR_FI | type | VT(reg) | RN(arg) | ((sljit_ins)argw << (10 - shift)));
1503 if (argw <= 0xffffff) {
1504 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(TMP_REG1) | RN(arg) | (((sljit_ins)argw >> 12) << 10)));
1506 argw = ((argw & 0xfff) >> shift);
1507 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | ((sljit_ins)argw << 10));
1511 if (argw <= 255 && argw >= -256)
1512 return push_inst(compiler, STUR_FI | type | VT(reg) | RN(arg) | (((sljit_ins)argw & 0x1ff) << 12));
1514 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1515 return push_inst(compiler, STR_FR | type | VT(reg) | RN(arg) | RM(TMP_REG1));
1518 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1519 sljit_s32 dst, sljit_sw dstw,
1520 sljit_s32 src, sljit_sw srcw)
1522 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1523 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1525 if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1526 inv_bits |= W_OP;
1528 if (src & SLJIT_MEM) {
1529 emit_fop_mem(compiler, (op & SLJIT_32) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1530 src = TMP_FREG1;
1533 FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1535 if (dst & SLJIT_MEM)
1536 return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1537 return SLJIT_SUCCESS;
1540 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1541 sljit_s32 dst, sljit_sw dstw,
1542 sljit_s32 src, sljit_sw srcw)
1544 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1545 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1547 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1548 inv_bits |= W_OP;
1550 if (src & SLJIT_MEM) {
1551 emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw, TMP_REG1);
1552 src = TMP_REG1;
1553 } else if (src & SLJIT_IMM) {
1554 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1555 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1556 srcw = (sljit_s32)srcw;
1557 #endif
1558 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1559 src = TMP_REG1;
1562 FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1564 if (dst & SLJIT_MEM)
1565 return emit_fop_mem(compiler, ((op & SLJIT_32) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1566 return SLJIT_SUCCESS;
1569 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1570 sljit_s32 src1, sljit_sw src1w,
1571 sljit_s32 src2, sljit_sw src2w)
1573 sljit_s32 mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1574 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1576 if (src1 & SLJIT_MEM) {
1577 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1578 src1 = TMP_FREG1;
1581 if (src2 & SLJIT_MEM) {
1582 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1583 src2 = TMP_FREG2;
1586 return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1589 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1590 sljit_s32 dst, sljit_sw dstw,
1591 sljit_s32 src, sljit_sw srcw)
1593 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1594 sljit_ins inv_bits;
1596 CHECK_ERROR();
1598 SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x1) == WORD_SIZE, must_be_one_bit_difference);
1599 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1601 inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1602 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1604 if (src & SLJIT_MEM) {
1605 emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x1) : mem_flags, dst_r, src, srcw);
1606 src = dst_r;
1609 switch (GET_OPCODE(op)) {
1610 case SLJIT_MOV_F64:
1611 if (src != dst_r) {
1612 if (dst_r != TMP_FREG1)
1613 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1614 else
1615 dst_r = src;
1617 break;
1618 case SLJIT_NEG_F64:
1619 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1620 break;
1621 case SLJIT_ABS_F64:
1622 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1623 break;
1624 case SLJIT_CONV_F64_FROM_F32:
1625 FAIL_IF(push_inst(compiler, FCVT | (sljit_ins)((op & SLJIT_32) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1626 break;
1629 if (dst & SLJIT_MEM)
1630 return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1631 return SLJIT_SUCCESS;
1634 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1635 sljit_s32 dst, sljit_sw dstw,
1636 sljit_s32 src1, sljit_sw src1w,
1637 sljit_s32 src2, sljit_sw src2w)
1639 sljit_s32 dst_r, mem_flags = (op & SLJIT_32) ? INT_SIZE : WORD_SIZE;
1640 sljit_ins inv_bits = (op & SLJIT_32) ? (1 << 22) : 0;
1642 CHECK_ERROR();
1643 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1644 ADJUST_LOCAL_OFFSET(dst, dstw);
1645 ADJUST_LOCAL_OFFSET(src1, src1w);
1646 ADJUST_LOCAL_OFFSET(src2, src2w);
1648 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1649 if (src1 & SLJIT_MEM) {
1650 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1651 src1 = TMP_FREG1;
1653 if (src2 & SLJIT_MEM) {
1654 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1655 src2 = TMP_FREG2;
1658 switch (GET_OPCODE(op)) {
1659 case SLJIT_ADD_F64:
1660 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1661 break;
1662 case SLJIT_SUB_F64:
1663 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1664 break;
1665 case SLJIT_MUL_F64:
1666 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1667 break;
1668 case SLJIT_DIV_F64:
1669 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1670 break;
1673 if (!(dst & SLJIT_MEM))
1674 return SLJIT_SUCCESS;
1675 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1678 /* --------------------------------------------------------------------- */
1679 /* Other instructions */
1680 /* --------------------------------------------------------------------- */
1682 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1684 CHECK_ERROR();
1685 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1686 ADJUST_LOCAL_OFFSET(dst, dstw);
1688 if (FAST_IS_REG(dst))
1689 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1691 /* Memory. */
1692 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw, TMP_REG1);
1695 /* --------------------------------------------------------------------- */
1696 /* Conditional instructions */
1697 /* --------------------------------------------------------------------- */
1699 static sljit_ins get_cc(struct sljit_compiler *compiler, sljit_s32 type)
1701 switch (type) {
1702 case SLJIT_EQUAL:
1703 case SLJIT_EQUAL_F64:
1704 return 0x1;
1706 case SLJIT_NOT_EQUAL:
1707 case SLJIT_NOT_EQUAL_F64:
1708 return 0x0;
1710 case SLJIT_LESS:
1711 case SLJIT_LESS_F64:
1712 return 0x2;
1714 case SLJIT_GREATER_EQUAL:
1715 case SLJIT_GREATER_EQUAL_F64:
1716 return 0x3;
1718 case SLJIT_GREATER:
1719 case SLJIT_GREATER_F64:
1720 return 0x9;
1722 case SLJIT_LESS_EQUAL:
1723 case SLJIT_LESS_EQUAL_F64:
1724 return 0x8;
1726 case SLJIT_SIG_LESS:
1727 return 0xa;
1729 case SLJIT_SIG_GREATER_EQUAL:
1730 return 0xb;
1732 case SLJIT_SIG_GREATER:
1733 return 0xd;
1735 case SLJIT_SIG_LESS_EQUAL:
1736 return 0xc;
1738 case SLJIT_OVERFLOW:
1739 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1740 return 0x0;
1741 /* fallthrough */
1743 case SLJIT_UNORDERED_F64:
1744 return 0x7;
1746 case SLJIT_NOT_OVERFLOW:
1747 if (!(compiler->status_flags_state & SLJIT_CURRENT_FLAGS_ADD_SUB))
1748 return 0x1;
1749 /* fallthrough */
1751 case SLJIT_ORDERED_F64:
1752 return 0x6;
1754 default:
1755 SLJIT_UNREACHABLE();
1756 return 0xe;
1760 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1762 struct sljit_label *label;
1764 CHECK_ERROR_PTR();
1765 CHECK_PTR(check_sljit_emit_label(compiler));
1767 if (compiler->last_label && compiler->last_label->size == compiler->size)
1768 return compiler->last_label;
1770 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1771 PTR_FAIL_IF(!label);
1772 set_label(label, compiler);
1773 return label;
1776 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1778 struct sljit_jump *jump;
1780 CHECK_ERROR_PTR();
1781 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1783 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1784 PTR_FAIL_IF(!jump);
1785 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1786 type &= 0xff;
1788 if (type < SLJIT_JUMP) {
1789 jump->flags |= IS_COND;
1790 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(compiler, type)));
1792 else if (type >= SLJIT_FAST_CALL)
1793 jump->flags |= IS_BL;
1795 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1796 jump->addr = compiler->size;
1797 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1799 return jump;
1802 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1803 sljit_s32 arg_types)
1805 CHECK_ERROR_PTR();
1806 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1808 if (type & SLJIT_CALL_RETURN) {
1809 PTR_FAIL_IF(emit_stack_frame_release(compiler));
1810 type = SLJIT_JUMP | (type & SLJIT_REWRITABLE_JUMP);
1813 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1814 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1815 compiler->skip_checks = 1;
1816 #endif
1818 return sljit_emit_jump(compiler, type);
1821 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1822 sljit_s32 src, sljit_sw srcw)
1824 struct sljit_jump *jump;
1825 sljit_ins inv_bits = (type & SLJIT_32) ? W_OP : 0;
1827 SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1828 ADJUST_LOCAL_OFFSET(src, srcw);
1830 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1831 PTR_FAIL_IF(!jump);
1832 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1833 jump->flags |= IS_CBZ | IS_COND;
1835 if (src & SLJIT_MEM) {
1836 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1837 src = TMP_REG1;
1839 else if (src & SLJIT_IMM) {
1840 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1841 src = TMP_REG1;
1844 SLJIT_ASSERT(FAST_IS_REG(src));
1846 if ((type & 0xff) == SLJIT_EQUAL)
1847 inv_bits |= 1 << 24;
1849 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1850 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1851 jump->addr = compiler->size;
1852 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1853 return jump;
1856 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1858 struct sljit_jump *jump;
1860 CHECK_ERROR();
1861 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1862 ADJUST_LOCAL_OFFSET(src, srcw);
1864 if (!(src & SLJIT_IMM)) {
1865 if (src & SLJIT_MEM) {
1866 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1867 src = TMP_REG1;
1869 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1872 /* These jumps are converted to jump/call instructions when possible. */
1873 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1874 FAIL_IF(!jump);
1875 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1876 jump->u.target = (sljit_uw)srcw;
1878 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1879 jump->addr = compiler->size;
1880 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1883 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1884 sljit_s32 arg_types,
1885 sljit_s32 src, sljit_sw srcw)
1887 CHECK_ERROR();
1888 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1889 ADJUST_LOCAL_OFFSET(src, srcw);
1891 if (src & SLJIT_MEM) {
1892 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1893 src = TMP_REG1;
1896 if (type & SLJIT_CALL_RETURN) {
1897 if (src >= SLJIT_FIRST_SAVED_REG && src <= SLJIT_S0) {
1898 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(src)));
1899 src = TMP_REG1;
1902 FAIL_IF(emit_stack_frame_release(compiler));
1903 type = SLJIT_JUMP;
1906 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1907 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1908 compiler->skip_checks = 1;
1909 #endif
1911 return sljit_emit_ijump(compiler, type, src, srcw);
1914 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1915 sljit_s32 dst, sljit_sw dstw,
1916 sljit_s32 type)
1918 sljit_s32 dst_r, src_r, flags, mem_flags;
1919 sljit_ins cc;
1921 CHECK_ERROR();
1922 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1923 ADJUST_LOCAL_OFFSET(dst, dstw);
1925 cc = get_cc(compiler, type & 0xff);
1926 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1928 if (GET_OPCODE(op) < SLJIT_ADD) {
1929 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1931 if (dst_r == TMP_REG1) {
1932 mem_flags = (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE;
1933 return emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG2);
1936 return SLJIT_SUCCESS;
1939 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1940 mem_flags = WORD_SIZE;
1942 if (op & SLJIT_32) {
1943 flags |= INT_OP;
1944 mem_flags = INT_SIZE;
1947 src_r = dst;
1949 if (dst & SLJIT_MEM) {
1950 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG1));
1951 src_r = TMP_REG1;
1954 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1955 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
1957 if (dst & SLJIT_MEM)
1958 return emit_op_mem(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1959 return SLJIT_SUCCESS;
1962 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1963 sljit_s32 dst_reg,
1964 sljit_s32 src, sljit_sw srcw)
1966 sljit_ins inv_bits = (dst_reg & SLJIT_32) ? W_OP : 0;
1967 sljit_ins cc;
1969 CHECK_ERROR();
1970 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1972 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
1973 if (dst_reg & SLJIT_32)
1974 srcw = (sljit_s32)srcw;
1975 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1976 src = TMP_REG1;
1977 srcw = 0;
1980 cc = get_cc(compiler, type & 0xff);
1981 dst_reg &= ~SLJIT_32;
1983 return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
1986 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1987 sljit_s32 reg,
1988 sljit_s32 mem, sljit_sw memw)
1990 sljit_u32 sign = 0, inst;
1992 CHECK_ERROR();
1993 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
1995 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1996 return SLJIT_ERR_UNSUPPORTED;
1998 if (type & SLJIT_MEM_SUPP)
1999 return SLJIT_SUCCESS;
2001 switch (type & 0xff) {
2002 case SLJIT_MOV:
2003 case SLJIT_MOV_P:
2004 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
2005 break;
2006 case SLJIT_MOV_S8:
2007 sign = 1;
2008 /* fallthrough */
2009 case SLJIT_MOV_U8:
2010 inst = STURBI | (MEM_SIZE_SHIFT(BYTE_SIZE) << 30) | 0x400;
2011 break;
2012 case SLJIT_MOV_S16:
2013 sign = 1;
2014 /* fallthrough */
2015 case SLJIT_MOV_U16:
2016 inst = STURBI | (MEM_SIZE_SHIFT(HALF_SIZE) << 30) | 0x400;
2017 break;
2018 case SLJIT_MOV_S32:
2019 sign = 1;
2020 /* fallthrough */
2021 case SLJIT_MOV_U32:
2022 case SLJIT_MOV32:
2023 inst = STURBI | (MEM_SIZE_SHIFT(INT_SIZE) << 30) | 0x400;
2024 break;
2025 default:
2026 SLJIT_UNREACHABLE();
2027 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
2028 break;
2031 if (!(type & SLJIT_MEM_STORE))
2032 inst |= sign ? 0x00800000 : 0x00400000;
2034 if (type & SLJIT_MEM_PRE)
2035 inst |= 0x800;
2037 return push_inst(compiler, inst | RT(reg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
2040 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
2041 sljit_s32 freg,
2042 sljit_s32 mem, sljit_sw memw)
2044 sljit_u32 inst;
2046 CHECK_ERROR();
2047 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
2049 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
2050 return SLJIT_ERR_UNSUPPORTED;
2052 if (type & SLJIT_MEM_SUPP)
2053 return SLJIT_SUCCESS;
2055 inst = STUR_FI | 0x80000400;
2057 if (!(type & SLJIT_32))
2058 inst |= 0x40000000;
2060 if (!(type & SLJIT_MEM_STORE))
2061 inst |= 0x00400000;
2063 if (type & SLJIT_MEM_PRE)
2064 inst |= 0x800;
2066 return push_inst(compiler, inst | VT(freg) | RN(mem & REG_MASK) | (sljit_ins)((memw & 0x1ff) << 12));
2069 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset)
2071 sljit_s32 dst_reg;
2072 sljit_ins ins;
2074 CHECK_ERROR();
2075 CHECK(check_sljit_get_local_base(compiler, dst, dstw, offset));
2076 ADJUST_LOCAL_OFFSET(SLJIT_MEM1(SLJIT_SP), offset);
2078 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
2080 /* Not all instruction forms support accessing SP register. */
2081 if (offset <= 0xffffff && offset >= -0xffffff) {
2082 ins = ADDI;
2083 if (offset < 0) {
2084 offset = -offset;
2085 ins = SUBI;
2088 if (offset <= 0xfff)
2089 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)(offset << 10)));
2090 else {
2091 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (sljit_ins)((offset & 0xfff000) >> (12 - 10)) | (1 << 22)));
2093 offset &= 0xfff;
2094 if (offset != 0)
2095 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(dst_reg) | (sljit_ins)(offset << 10)));
2098 else {
2099 FAIL_IF(load_immediate (compiler, dst_reg, offset));
2100 /* Add extended register form. */
2101 FAIL_IF(push_inst(compiler, ADDE | (0x3 << 13) | RD(dst_reg) | RN(SLJIT_SP) | RM(dst_reg)));
2104 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
2105 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG1);
2106 return SLJIT_SUCCESS;
2109 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
2111 struct sljit_const *const_;
2112 sljit_s32 dst_r;
2114 CHECK_ERROR_PTR();
2115 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
2116 ADJUST_LOCAL_OFFSET(dst, dstw);
2118 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2119 PTR_FAIL_IF(!const_);
2120 set_const(const_, compiler);
2122 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2123 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, (sljit_uw)init_value));
2125 if (dst & SLJIT_MEM)
2126 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2127 return const_;
2130 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2132 struct sljit_put_label *put_label;
2133 sljit_s32 dst_r;
2135 CHECK_ERROR_PTR();
2136 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2137 ADJUST_LOCAL_OFFSET(dst, dstw);
2139 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2140 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, 0));
2142 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2143 PTR_FAIL_IF(!put_label);
2144 set_put_label(put_label, compiler, 1);
2146 if (dst & SLJIT_MEM)
2147 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2149 return put_label;
2152 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2154 sljit_ins* inst = (sljit_ins*)addr;
2155 sljit_u32 dst;
2156 SLJIT_UNUSED_ARG(executable_offset);
2158 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 0);
2160 dst = inst[0] & 0x1f;
2161 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
2162 inst[0] = MOVZ | dst | (((sljit_u32)new_target & 0xffff) << 5);
2163 inst[1] = MOVK | dst | (((sljit_u32)(new_target >> 16) & 0xffff) << 5) | (1 << 21);
2164 inst[2] = MOVK | dst | (((sljit_u32)(new_target >> 32) & 0xffff) << 5) | (2 << 21);
2165 inst[3] = MOVK | dst | ((sljit_u32)(new_target >> 48) << 5) | (3 << 21);
2167 SLJIT_UPDATE_WX_FLAGS(inst, inst + 4, 1);
2168 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2169 SLJIT_CACHE_FLUSH(inst, inst + 4);
2172 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2174 sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);