1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
11 *
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.
15 *
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.
25 */
26
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
28 {
29 return "ARM-64" SLJIT_CPUINFO;
30 }
31
32 /* Length of an instruction word */
33 typedef sljit_u32 sljit_ins;
34
35 #define TMP_ZERO (0)
36
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)
41
42 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
43 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
44
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
48 };
49
50 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
51 0, 0, 1, 2, 3, 4, 5, 6, 7
52 };
53
54 #define W_OP (1u << 31)
55 #define RD(rd) (reg_map[rd])
56 #define RT(rt) (reg_map[rt])
57 #define RN(rn) (reg_map[rn] << 5)
58 #define RT2(rt2) (reg_map[rt2] << 10)
59 #define RM(rm) (reg_map[rm] << 16)
60 #define VD(vd) (freg_map[vd])
61 #define VT(vt) (freg_map[vt])
62 #define VN(vn) (freg_map[vn] << 5)
63 #define VM(vm) (freg_map[vm] << 16)
64
65 /* --------------------------------------------------------------------- */
66 /* Instrucion forms */
67 /* --------------------------------------------------------------------- */
68
69 #define ADC 0x9a000000
70 #define ADD 0x8b000000
71 #define ADDE 0x8b200000
72 #define ADDI 0x91000000
73 #define AND 0x8a000000
74 #define ANDI 0x92000000
75 #define ASRV 0x9ac02800
76 #define B 0x14000000
77 #define B_CC 0x54000000
78 #define BL 0x94000000
79 #define BLR 0xd63f0000
80 #define BR 0xd61f0000
81 #define BRK 0xd4200000
82 #define CBZ 0xb4000000
83 #define CLZ 0xdac01000
84 #define CSEL 0x9a800000
85 #define CSINC 0x9a800400
86 #define EOR 0xca000000
87 #define EORI 0xd2000000
88 #define FABS 0x1e60c000
89 #define FADD 0x1e602800
90 #define FCMP 0x1e602000
91 #define FCVT 0x1e224000
92 #define FCVTZS 0x9e780000
93 #define FDIV 0x1e601800
94 #define FMOV 0x1e604000
95 #define FMUL 0x1e600800
96 #define FNEG 0x1e614000
97 #define FSUB 0x1e603800
98 #define LDRI 0xf9400000
99 #define LDP 0xa9400000
100 #define LDP_PRE 0xa9c00000
101 #define LDR_PRE 0xf8400c00
102 #define LSLV 0x9ac02000
103 #define LSRV 0x9ac02400
104 #define MADD 0x9b000000
105 #define MOVK 0xf2800000
106 #define MOVN 0x92800000
107 #define MOVZ 0xd2800000
108 #define NOP 0xd503201f
109 #define ORN 0xaa200000
110 #define ORR 0xaa000000
111 #define ORRI 0xb2000000
112 #define RET 0xd65f0000
113 #define SBC 0xda000000
114 #define SBFM 0x93000000
115 #define SCVTF 0x9e620000
116 #define SDIV 0x9ac00c00
117 #define SMADDL 0x9b200000
118 #define SMULH 0x9b403c00
119 #define STP 0xa9000000
120 #define STP_PRE 0xa9800000
121 #define STRB 0x38206800
122 #define STRBI 0x39000000
123 #define STRI 0xf9000000
124 #define STR_FI 0x3d000000
125 #define STR_FR 0x3c206800
126 #define STUR_FI 0x3c000000
127 #define STURBI 0x38000000
128 #define SUB 0xcb000000
129 #define SUBI 0xd1000000
130 #define SUBS 0xeb000000
131 #define UBFM 0xd3000000
132 #define UDIV 0x9ac00800
133 #define UMULH 0x9bc03c00
134
135 /* dest_reg is the absolute name of the register
136 Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins)137 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins)
138 {
139 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
140 FAIL_IF(!ptr);
141 *ptr = ins;
142 compiler->size++;
143 return SLJIT_SUCCESS;
144 }
145
emit_imm64_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_uw imm)146 static SLJIT_INLINE sljit_s32 emit_imm64_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
147 {
148 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
149 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 16) & 0xffff) << 5) | (1 << 21)));
150 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | (((imm >> 32) & 0xffff) << 5) | (2 << 21)));
151 return push_inst(compiler, MOVK | RD(dst) | ((imm >> 48) << 5) | (3 << 21));
152 }
153
modify_imm64_const(sljit_ins * inst,sljit_uw new_imm)154 static SLJIT_INLINE void modify_imm64_const(sljit_ins* inst, sljit_uw new_imm)
155 {
156 sljit_s32 dst = inst[0] & 0x1f;
157 SLJIT_ASSERT((inst[0] & 0xffe00000) == MOVZ && (inst[1] & 0xffe00000) == (MOVK | (1 << 21)));
158 inst[0] = MOVZ | dst | ((new_imm & 0xffff) << 5);
159 inst[1] = MOVK | dst | (((new_imm >> 16) & 0xffff) << 5) | (1 << 21);
160 inst[2] = MOVK | dst | (((new_imm >> 32) & 0xffff) << 5) | (2 << 21);
161 inst[3] = MOVK | dst | ((new_imm >> 48) << 5) | (3 << 21);
162 }
163
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code,sljit_sw executable_offset)164 static SLJIT_INLINE sljit_sw detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
165 {
166 sljit_sw diff;
167 sljit_uw target_addr;
168
169 if (jump->flags & SLJIT_REWRITABLE_JUMP) {
170 jump->flags |= PATCH_ABS64;
171 return 0;
172 }
173
174 if (jump->flags & JUMP_ADDR)
175 target_addr = jump->u.target;
176 else {
177 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
178 target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
179 }
180
181 diff = (sljit_sw)target_addr - (sljit_sw)(code_ptr + 4) - executable_offset;
182
183 if (jump->flags & IS_COND) {
184 diff += sizeof(sljit_ins);
185 if (diff <= 0xfffff && diff >= -0x100000) {
186 code_ptr[-5] ^= (jump->flags & IS_CBZ) ? (0x1 << 24) : 0x1;
187 jump->addr -= sizeof(sljit_ins);
188 jump->flags |= PATCH_COND;
189 return 5;
190 }
191 diff -= sizeof(sljit_ins);
192 }
193
194 if (diff <= 0x7ffffff && diff >= -0x8000000) {
195 jump->flags |= PATCH_B;
196 return 4;
197 }
198
199 if (target_addr < 0x100000000l) {
200 if (jump->flags & IS_COND)
201 code_ptr[-5] -= (2 << 5);
202 code_ptr[-2] = code_ptr[0];
203 return 2;
204 }
205
206 if (target_addr < 0x1000000000000l) {
207 if (jump->flags & IS_COND)
208 code_ptr[-5] -= (1 << 5);
209 jump->flags |= PATCH_ABS48;
210 code_ptr[-1] = code_ptr[0];
211 return 1;
212 }
213
214 jump->flags |= PATCH_ABS64;
215 return 0;
216 }
217
put_label_get_length(struct sljit_put_label * put_label,sljit_uw max_label)218 static SLJIT_INLINE sljit_sw put_label_get_length(struct sljit_put_label *put_label, sljit_uw max_label)
219 {
220 if (max_label < 0x100000000l) {
221 put_label->flags = 0;
222 return 2;
223 }
224
225 if (max_label < 0x1000000000000l) {
226 put_label->flags = 1;
227 return 1;
228 }
229
230 put_label->flags = 2;
231 return 0;
232 }
233
sljit_generate_code(struct sljit_compiler * compiler)234 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
235 {
236 struct sljit_memory_fragment *buf;
237 sljit_ins *code;
238 sljit_ins *code_ptr;
239 sljit_ins *buf_ptr;
240 sljit_ins *buf_end;
241 sljit_uw word_count;
242 sljit_uw next_addr;
243 sljit_sw executable_offset;
244 sljit_uw addr;
245 sljit_s32 dst;
246
247 struct sljit_label *label;
248 struct sljit_jump *jump;
249 struct sljit_const *const_;
250 struct sljit_put_label *put_label;
251
252 CHECK_ERROR_PTR();
253 CHECK_PTR(check_sljit_generate_code(compiler));
254 reverse_buf(compiler);
255
256 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
257 PTR_FAIL_WITH_EXEC_IF(code);
258 buf = compiler->buf;
259
260 code_ptr = code;
261 word_count = 0;
262 next_addr = 0;
263 executable_offset = SLJIT_EXEC_OFFSET(code);
264
265 label = compiler->labels;
266 jump = compiler->jumps;
267 const_ = compiler->consts;
268 put_label = compiler->put_labels;
269
270 do {
271 buf_ptr = (sljit_ins*)buf->memory;
272 buf_end = buf_ptr + (buf->used_size >> 2);
273 do {
274 *code_ptr = *buf_ptr++;
275 if (next_addr == word_count) {
276 SLJIT_ASSERT(!label || label->size >= word_count);
277 SLJIT_ASSERT(!jump || jump->addr >= word_count);
278 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
279 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
280
281 /* These structures are ordered by their address. */
282 if (label && label->size == word_count) {
283 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
284 label->size = code_ptr - code;
285 label = label->next;
286 }
287 if (jump && jump->addr == word_count) {
288 jump->addr = (sljit_uw)(code_ptr - 4);
289 code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
290 jump = jump->next;
291 }
292 if (const_ && const_->addr == word_count) {
293 const_->addr = (sljit_uw)code_ptr;
294 const_ = const_->next;
295 }
296 if (put_label && put_label->addr == word_count) {
297 SLJIT_ASSERT(put_label->label);
298 put_label->addr = (sljit_uw)(code_ptr - 3);
299 code_ptr -= put_label_get_length(put_label, (sljit_uw)(SLJIT_ADD_EXEC_OFFSET(code, executable_offset) + put_label->label->size));
300 put_label = put_label->next;
301 }
302 next_addr = compute_next_addr(label, jump, const_, put_label);
303 }
304 code_ptr ++;
305 word_count ++;
306 } while (buf_ptr < buf_end);
307
308 buf = buf->next;
309 } while (buf);
310
311 if (label && label->size == word_count) {
312 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
313 label->size = code_ptr - code;
314 label = label->next;
315 }
316
317 SLJIT_ASSERT(!label);
318 SLJIT_ASSERT(!jump);
319 SLJIT_ASSERT(!const_);
320 SLJIT_ASSERT(!put_label);
321 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
322
323 jump = compiler->jumps;
324 while (jump) {
325 do {
326 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
327 buf_ptr = (sljit_ins *)jump->addr;
328
329 if (jump->flags & PATCH_B) {
330 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
331 SLJIT_ASSERT((sljit_sw)addr <= 0x1ffffff && (sljit_sw)addr >= -0x2000000);
332 buf_ptr[0] = ((jump->flags & IS_BL) ? BL : B) | (addr & 0x3ffffff);
333 if (jump->flags & IS_COND)
334 buf_ptr[-1] -= (4 << 5);
335 break;
336 }
337 if (jump->flags & PATCH_COND) {
338 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
339 SLJIT_ASSERT((sljit_sw)addr <= 0x3ffff && (sljit_sw)addr >= -0x40000);
340 buf_ptr[0] = (buf_ptr[0] & ~0xffffe0) | ((addr & 0x7ffff) << 5);
341 break;
342 }
343
344 SLJIT_ASSERT((jump->flags & (PATCH_ABS48 | PATCH_ABS64)) || addr <= 0xffffffffl);
345 SLJIT_ASSERT((jump->flags & PATCH_ABS64) || addr <= 0xffffffffffffl);
346
347 dst = buf_ptr[0] & 0x1f;
348 buf_ptr[0] = MOVZ | dst | ((addr & 0xffff) << 5);
349 buf_ptr[1] = MOVK | dst | (((addr >> 16) & 0xffff) << 5) | (1 << 21);
350 if (jump->flags & (PATCH_ABS48 | PATCH_ABS64))
351 buf_ptr[2] = MOVK | dst | (((addr >> 32) & 0xffff) << 5) | (2 << 21);
352 if (jump->flags & PATCH_ABS64)
353 buf_ptr[3] = MOVK | dst | (((addr >> 48) & 0xffff) << 5) | (3 << 21);
354 } while (0);
355 jump = jump->next;
356 }
357
358 put_label = compiler->put_labels;
359 while (put_label) {
360 addr = put_label->label->addr;
361 buf_ptr = (sljit_ins *)put_label->addr;
362
363 buf_ptr[0] |= (addr & 0xffff) << 5;
364 buf_ptr[1] |= ((addr >> 16) & 0xffff) << 5;
365
366 if (put_label->flags >= 1)
367 buf_ptr[2] |= ((addr >> 32) & 0xffff) << 5;
368
369 if (put_label->flags >= 2)
370 buf_ptr[3] |= ((addr >> 48) & 0xffff) << 5;
371
372 put_label = put_label->next;
373 }
374
375 compiler->error = SLJIT_ERR_COMPILED;
376 compiler->executable_offset = executable_offset;
377 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
378
379 code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
380 code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
381
382 SLJIT_CACHE_FLUSH(code, code_ptr);
383 return code;
384 }
385
sljit_has_cpu_feature(sljit_s32 feature_type)386 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
387 {
388 switch (feature_type) {
389 case SLJIT_HAS_FPU:
390 #ifdef SLJIT_IS_FPU_AVAILABLE
391 return SLJIT_IS_FPU_AVAILABLE;
392 #else
393 /* Available by default. */
394 return 1;
395 #endif
396
397 case SLJIT_HAS_CLZ:
398 case SLJIT_HAS_CMOV:
399 case SLJIT_HAS_PREFETCH:
400 return 1;
401
402 default:
403 return 0;
404 }
405 }
406
407 /* --------------------------------------------------------------------- */
408 /* Core code generator functions. */
409 /* --------------------------------------------------------------------- */
410
411 #define COUNT_TRAILING_ZERO(value, result) \
412 result = 0; \
413 if (!(value & 0xffffffff)) { \
414 result += 32; \
415 value >>= 32; \
416 } \
417 if (!(value & 0xffff)) { \
418 result += 16; \
419 value >>= 16; \
420 } \
421 if (!(value & 0xff)) { \
422 result += 8; \
423 value >>= 8; \
424 } \
425 if (!(value & 0xf)) { \
426 result += 4; \
427 value >>= 4; \
428 } \
429 if (!(value & 0x3)) { \
430 result += 2; \
431 value >>= 2; \
432 } \
433 if (!(value & 0x1)) { \
434 result += 1; \
435 value >>= 1; \
436 }
437
438 #define LOGICAL_IMM_CHECK 0x100
439
logical_imm(sljit_sw imm,sljit_s32 len)440 static sljit_ins logical_imm(sljit_sw imm, sljit_s32 len)
441 {
442 sljit_s32 negated, ones, right;
443 sljit_uw mask, uimm;
444 sljit_ins ins;
445
446 if (len & LOGICAL_IMM_CHECK) {
447 len &= ~LOGICAL_IMM_CHECK;
448 if (len == 32 && (imm == 0 || imm == -1))
449 return 0;
450 if (len == 16 && ((sljit_s32)imm == 0 || (sljit_s32)imm == -1))
451 return 0;
452 }
453
454 SLJIT_ASSERT((len == 32 && imm != 0 && imm != -1)
455 || (len == 16 && (sljit_s32)imm != 0 && (sljit_s32)imm != -1));
456
457 uimm = (sljit_uw)imm;
458 while (1) {
459 if (len <= 0) {
460 SLJIT_UNREACHABLE();
461 return 0;
462 }
463
464 mask = ((sljit_uw)1 << len) - 1;
465 if ((uimm & mask) != ((uimm >> len) & mask))
466 break;
467 len >>= 1;
468 }
469
470 len <<= 1;
471
472 negated = 0;
473 if (uimm & 0x1) {
474 negated = 1;
475 uimm = ~uimm;
476 }
477
478 if (len < 64)
479 uimm &= ((sljit_uw)1 << len) - 1;
480
481 /* Unsigned right shift. */
482 COUNT_TRAILING_ZERO(uimm, right);
483
484 /* Signed shift. We also know that the highest bit is set. */
485 imm = (sljit_sw)~uimm;
486 SLJIT_ASSERT(imm < 0);
487
488 COUNT_TRAILING_ZERO(imm, ones);
489
490 if (~imm)
491 return 0;
492
493 if (len == 64)
494 ins = 1 << 22;
495 else
496 ins = (0x3f - ((len << 1) - 1)) << 10;
497
498 if (negated)
499 return ins | ((len - ones - 1) << 10) | ((len - ones - right) << 16);
500
501 return ins | ((ones - 1) << 10) | ((len - right) << 16);
502 }
503
504 #undef COUNT_TRAILING_ZERO
505
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw simm)506 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw simm)
507 {
508 sljit_uw imm = (sljit_uw)simm;
509 sljit_s32 i, zeros, ones, first;
510 sljit_ins bitmask;
511
512 /* Handling simple immediates first. */
513 if (imm <= 0xffff)
514 return push_inst(compiler, MOVZ | RD(dst) | (imm << 5));
515
516 if (simm < 0 && simm >= -0x10000)
517 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5));
518
519 if (imm <= 0xffffffffl) {
520 if ((imm & 0xffff) == 0)
521 return push_inst(compiler, MOVZ | RD(dst) | ((imm >> 16) << 5) | (1 << 21));
522 if ((imm & 0xffff0000l) == 0xffff0000)
523 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff) << 5));
524 if ((imm & 0xffff) == 0xffff)
525 return push_inst(compiler, (MOVN ^ W_OP) | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
526
527 bitmask = logical_imm(simm, 16);
528 if (bitmask != 0)
529 return push_inst(compiler, (ORRI ^ W_OP) | RD(dst) | RN(TMP_ZERO) | bitmask);
530
531 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((imm & 0xffff) << 5)));
532 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
533 }
534
535 bitmask = logical_imm(simm, 32);
536 if (bitmask != 0)
537 return push_inst(compiler, ORRI | RD(dst) | RN(TMP_ZERO) | bitmask);
538
539 if (simm < 0 && simm >= -0x100000000l) {
540 if ((imm & 0xffff) == 0xffff)
541 return push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
542
543 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((~imm & 0xffff) << 5)));
544 return push_inst(compiler, MOVK | RD(dst) | ((imm & 0xffff0000l) >> (16 - 5)) | (1 << 21));
545 }
546
547 /* A large amount of number can be constructed from ORR and MOVx, but computing them is costly. */
548
549 zeros = 0;
550 ones = 0;
551 for (i = 4; i > 0; i--) {
552 if ((simm & 0xffff) == 0)
553 zeros++;
554 if ((simm & 0xffff) == 0xffff)
555 ones++;
556 simm >>= 16;
557 }
558
559 simm = (sljit_sw)imm;
560 first = 1;
561 if (ones > zeros) {
562 simm = ~simm;
563 for (i = 0; i < 4; i++) {
564 if (!(simm & 0xffff)) {
565 simm >>= 16;
566 continue;
567 }
568 if (first) {
569 first = 0;
570 FAIL_IF(push_inst(compiler, MOVN | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
571 }
572 else
573 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((~simm & 0xffff) << 5) | (i << 21)));
574 simm >>= 16;
575 }
576 return SLJIT_SUCCESS;
577 }
578
579 for (i = 0; i < 4; i++) {
580 if (!(simm & 0xffff)) {
581 simm >>= 16;
582 continue;
583 }
584 if (first) {
585 first = 0;
586 FAIL_IF(push_inst(compiler, MOVZ | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
587 }
588 else
589 FAIL_IF(push_inst(compiler, MOVK | RD(dst) | ((simm & 0xffff) << 5) | (i << 21)));
590 simm >>= 16;
591 }
592 return SLJIT_SUCCESS;
593 }
594
595 #define ARG1_IMM 0x0010000
596 #define ARG2_IMM 0x0020000
597 #define INT_OP 0x0040000
598 #define SET_FLAGS 0x0080000
599 #define UNUSED_RETURN 0x0100000
600
601 #define CHECK_FLAGS(flag_bits) \
602 if (flags & SET_FLAGS) { \
603 inv_bits |= flag_bits; \
604 if (flags & UNUSED_RETURN) \
605 dst = TMP_ZERO; \
606 }
607
emit_op_imm(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 dst,sljit_sw arg1,sljit_sw arg2)608 static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_sw arg1, sljit_sw arg2)
609 {
610 /* dst must be register, TMP_REG1
611 arg1 must be register, TMP_REG1, imm
612 arg2 must be register, TMP_REG2, imm */
613 sljit_ins inv_bits = (flags & INT_OP) ? W_OP : 0;
614 sljit_ins inst_bits;
615 sljit_s32 op = (flags & 0xffff);
616 sljit_s32 reg;
617 sljit_sw imm, nimm;
618
619 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
620 /* Both are immediates. */
621 flags &= ~ARG1_IMM;
622 if (arg1 == 0 && op != SLJIT_ADD && op != SLJIT_SUB)
623 arg1 = TMP_ZERO;
624 else {
625 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
626 arg1 = TMP_REG1;
627 }
628 }
629
630 if (flags & (ARG1_IMM | ARG2_IMM)) {
631 reg = (flags & ARG2_IMM) ? arg1 : arg2;
632 imm = (flags & ARG2_IMM) ? arg2 : arg1;
633
634 switch (op) {
635 case SLJIT_MUL:
636 case SLJIT_NEG:
637 case SLJIT_CLZ:
638 case SLJIT_ADDC:
639 case SLJIT_SUBC:
640 /* No form with immediate operand (except imm 0, which
641 is represented by a ZERO register). */
642 break;
643 case SLJIT_MOV:
644 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
645 return load_immediate(compiler, dst, imm);
646 case SLJIT_NOT:
647 SLJIT_ASSERT(flags & ARG2_IMM);
648 FAIL_IF(load_immediate(compiler, dst, (flags & INT_OP) ? (~imm & 0xffffffff) : ~imm));
649 goto set_flags;
650 case SLJIT_SUB:
651 if (flags & ARG1_IMM)
652 break;
653 imm = -imm;
654 /* Fall through. */
655 case SLJIT_ADD:
656 if (imm == 0) {
657 CHECK_FLAGS(1 << 29);
658 return push_inst(compiler, ((op == SLJIT_ADD ? ADDI : SUBI) ^ inv_bits) | RD(dst) | RN(reg));
659 }
660 if (imm > 0 && imm <= 0xfff) {
661 CHECK_FLAGS(1 << 29);
662 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | (imm << 10));
663 }
664 nimm = -imm;
665 if (nimm > 0 && nimm <= 0xfff) {
666 CHECK_FLAGS(1 << 29);
667 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | (nimm << 10));
668 }
669 if (imm > 0 && imm <= 0xffffff && !(imm & 0xfff)) {
670 CHECK_FLAGS(1 << 29);
671 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22));
672 }
673 if (nimm > 0 && nimm <= 0xffffff && !(nimm & 0xfff)) {
674 CHECK_FLAGS(1 << 29);
675 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22));
676 }
677 if (imm > 0 && imm <= 0xffffff && !(flags & SET_FLAGS)) {
678 FAIL_IF(push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(reg) | ((imm >> 12) << 10) | (1 << 22)));
679 return push_inst(compiler, (ADDI ^ inv_bits) | RD(dst) | RN(dst) | ((imm & 0xfff) << 10));
680 }
681 if (nimm > 0 && nimm <= 0xffffff && !(flags & SET_FLAGS)) {
682 FAIL_IF(push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(reg) | ((nimm >> 12) << 10) | (1 << 22)));
683 return push_inst(compiler, (SUBI ^ inv_bits) | RD(dst) | RN(dst) | ((nimm & 0xfff) << 10));
684 }
685 break;
686 case SLJIT_AND:
687 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
688 if (!inst_bits)
689 break;
690 CHECK_FLAGS(3 << 29);
691 return push_inst(compiler, (ANDI ^ inv_bits) | RD(dst) | RN(reg) | inst_bits);
692 case SLJIT_OR:
693 case SLJIT_XOR:
694 inst_bits = logical_imm(imm, LOGICAL_IMM_CHECK | ((flags & INT_OP) ? 16 : 32));
695 if (!inst_bits)
696 break;
697 if (op == SLJIT_OR)
698 inst_bits |= ORRI;
699 else
700 inst_bits |= EORI;
701 FAIL_IF(push_inst(compiler, (inst_bits ^ inv_bits) | RD(dst) | RN(reg)));
702 goto set_flags;
703 case SLJIT_SHL:
704 if (flags & ARG1_IMM)
705 break;
706 if (flags & INT_OP) {
707 imm &= 0x1f;
708 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | ((-imm & 0x1f) << 16) | ((31 - imm) << 10)));
709 }
710 else {
711 imm &= 0x3f;
712 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | ((-imm & 0x3f) << 16) | ((63 - imm) << 10)));
713 }
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) | (imm << 16) | (31 << 10)));
724 }
725 else {
726 imm &= 0x3f;
727 FAIL_IF(push_inst(compiler, (UBFM ^ inv_bits) | RD(dst) | RN(arg1) | (1 << 22) | (imm << 16) | (63 << 10)));
728 }
729 goto set_flags;
730 default:
731 SLJIT_UNREACHABLE();
732 break;
733 }
734
735 if (flags & ARG2_IMM) {
736 if (arg2 == 0)
737 arg2 = TMP_ZERO;
738 else {
739 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
740 arg2 = TMP_REG2;
741 }
742 }
743 else {
744 if (arg1 == 0)
745 arg1 = TMP_ZERO;
746 else {
747 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
748 arg1 = TMP_REG1;
749 }
750 }
751 }
752
753 /* Both arguments are registers. */
754 switch (op) {
755 case SLJIT_MOV:
756 case SLJIT_MOV_P:
757 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
758 if (dst == arg2)
759 return SLJIT_SUCCESS;
760 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(arg2));
761 case SLJIT_MOV_U8:
762 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
763 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (7 << 10));
764 case SLJIT_MOV_S8:
765 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
766 if (!(flags & INT_OP))
767 inv_bits |= 1 << 22;
768 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (7 << 10));
769 case SLJIT_MOV_U16:
770 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
771 return push_inst(compiler, (UBFM ^ W_OP) | RD(dst) | RN(arg2) | (15 << 10));
772 case SLJIT_MOV_S16:
773 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
774 if (!(flags & INT_OP))
775 inv_bits |= 1 << 22;
776 return push_inst(compiler, (SBFM ^ inv_bits) | RD(dst) | RN(arg2) | (15 << 10));
777 case SLJIT_MOV_U32:
778 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
779 if ((flags & INT_OP) && dst == arg2)
780 return SLJIT_SUCCESS;
781 return push_inst(compiler, (ORR ^ W_OP) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
782 case SLJIT_MOV_S32:
783 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
784 if ((flags & INT_OP) && dst == arg2)
785 return SLJIT_SUCCESS;
786 return push_inst(compiler, SBFM | (1 << 22) | RD(dst) | RN(arg2) | (31 << 10));
787 case SLJIT_NOT:
788 SLJIT_ASSERT(arg1 == TMP_REG1);
789 FAIL_IF(push_inst(compiler, (ORN ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2)));
790 break; /* Set flags. */
791 case SLJIT_NEG:
792 SLJIT_ASSERT(arg1 == TMP_REG1);
793 if (flags & SET_FLAGS)
794 inv_bits |= 1 << 29;
795 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(TMP_ZERO) | RM(arg2));
796 case SLJIT_CLZ:
797 SLJIT_ASSERT(arg1 == TMP_REG1);
798 return push_inst(compiler, (CLZ ^ inv_bits) | RD(dst) | RN(arg2));
799 case SLJIT_ADD:
800 CHECK_FLAGS(1 << 29);
801 return push_inst(compiler, (ADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
802 case SLJIT_ADDC:
803 CHECK_FLAGS(1 << 29);
804 return push_inst(compiler, (ADC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
805 case SLJIT_SUB:
806 CHECK_FLAGS(1 << 29);
807 return push_inst(compiler, (SUB ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
808 case SLJIT_SUBC:
809 CHECK_FLAGS(1 << 29);
810 return push_inst(compiler, (SBC ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
811 case SLJIT_MUL:
812 if (!(flags & SET_FLAGS))
813 return push_inst(compiler, (MADD ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO));
814 if (flags & INT_OP) {
815 FAIL_IF(push_inst(compiler, SMADDL | RD(dst) | RN(arg1) | RM(arg2) | (31 << 10)));
816 FAIL_IF(push_inst(compiler, ADD | RD(TMP_LR) | RN(TMP_ZERO) | RM(dst) | (2 << 22) | (31 << 10)));
817 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
818 }
819 FAIL_IF(push_inst(compiler, SMULH | RD(TMP_LR) | RN(arg1) | RM(arg2)));
820 FAIL_IF(push_inst(compiler, MADD | RD(dst) | RN(arg1) | RM(arg2) | RT2(TMP_ZERO)));
821 return push_inst(compiler, SUBS | RD(TMP_ZERO) | RN(TMP_LR) | RM(dst) | (2 << 22) | (63 << 10));
822 case SLJIT_AND:
823 CHECK_FLAGS(3 << 29);
824 return push_inst(compiler, (AND ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2));
825 case SLJIT_OR:
826 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
827 break; /* Set flags. */
828 case SLJIT_XOR:
829 FAIL_IF(push_inst(compiler, (EOR ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
830 break; /* Set flags. */
831 case SLJIT_SHL:
832 FAIL_IF(push_inst(compiler, (LSLV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
833 break; /* Set flags. */
834 case SLJIT_LSHR:
835 FAIL_IF(push_inst(compiler, (LSRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
836 break; /* Set flags. */
837 case SLJIT_ASHR:
838 FAIL_IF(push_inst(compiler, (ASRV ^ inv_bits) | RD(dst) | RN(arg1) | RM(arg2)));
839 break; /* Set flags. */
840 default:
841 SLJIT_UNREACHABLE();
842 return SLJIT_SUCCESS;
843 }
844
845 set_flags:
846 if (flags & SET_FLAGS)
847 return push_inst(compiler, (SUBS ^ inv_bits) | RD(TMP_ZERO) | RN(dst) | RM(TMP_ZERO));
848 return SLJIT_SUCCESS;
849 }
850
851 #define STORE 0x10
852 #define SIGNED 0x20
853
854 #define BYTE_SIZE 0x0
855 #define HALF_SIZE 0x1
856 #define INT_SIZE 0x2
857 #define WORD_SIZE 0x3
858
859 #define MEM_SIZE_SHIFT(flags) ((flags) & 0x3)
860
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 tmp_reg)861 static sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
862 sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
863 {
864 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
865 sljit_u32 type = (shift << 30);
866
867 if (!(flags & STORE))
868 type |= (flags & SIGNED) ? 0x00800000 : 0x00400000;
869
870 SLJIT_ASSERT(arg & SLJIT_MEM);
871
872 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
873 argw &= 0x3;
874
875 if (argw == 0 || argw == shift)
876 return push_inst(compiler, STRB | type | RT(reg)
877 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
878
879 FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw << 10)));
880 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg));
881 }
882
883 arg &= REG_MASK;
884
885 if (arg == SLJIT_UNUSED) {
886 FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~(0xfff << shift)));
887
888 argw = (argw >> shift) & 0xfff;
889
890 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | (argw << 10));
891 }
892
893 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
894 if ((argw >> shift) <= 0xfff) {
895 return push_inst(compiler, STRBI | type | RT(reg) | RN(arg) | (argw << (10 - shift)));
896 }
897
898 if (argw <= 0xffffff) {
899 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(tmp_reg) | RN(arg) | ((argw >> 12) << 10)));
900
901 argw = ((argw & 0xfff) >> shift);
902 return push_inst(compiler, STRBI | type | RT(reg) | RN(tmp_reg) | (argw << 10));
903 }
904 }
905
906 if (argw <= 255 && argw >= -256)
907 return push_inst(compiler, STURBI | type | RT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
908
909 FAIL_IF(load_immediate(compiler, tmp_reg, argw));
910
911 return push_inst(compiler, STRB | type | RT(reg) | RN(arg) | RM(tmp_reg));
912 }
913
914 /* --------------------------------------------------------------------- */
915 /* Entry, exit */
916 /* --------------------------------------------------------------------- */
917
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)918 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
919 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
920 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
921 {
922 sljit_s32 args, i, tmp, offs, prev, saved_regs_size;
923
924 CHECK_ERROR();
925 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
926 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
927
928 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
929 if (saved_regs_size & 0x8)
930 saved_regs_size += sizeof(sljit_sw);
931
932 local_size = (local_size + 15) & ~0xf;
933 compiler->local_size = local_size + saved_regs_size;
934
935 FAIL_IF(push_inst(compiler, STP_PRE | RT(TMP_FP) | RT2(TMP_LR)
936 | RN(SLJIT_SP) | ((-(saved_regs_size >> 3) & 0x7f) << 15)));
937
938 #ifdef _WIN32
939 if (local_size >= 4096)
940 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | (1 << 10) | (1 << 22)));
941 else if (local_size > 256)
942 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(SLJIT_SP) | (local_size << 10)));
943 #endif
944
945 tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
946 prev = -1;
947 offs = 2 << 15;
948 for (i = SLJIT_S0; i >= tmp; i--) {
949 if (prev == -1) {
950 prev = i;
951 continue;
952 }
953 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
954 offs += 2 << 15;
955 prev = -1;
956 }
957
958 for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
959 if (prev == -1) {
960 prev = i;
961 continue;
962 }
963 FAIL_IF(push_inst(compiler, STP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
964 offs += 2 << 15;
965 prev = -1;
966 }
967
968 if (prev != -1)
969 FAIL_IF(push_inst(compiler, STRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
970
971
972 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_FP) | RN(SLJIT_SP) | (0 << 10)));
973
974 args = get_arg_count(arg_types);
975
976 if (args >= 1)
977 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S0) | RN(TMP_ZERO) | RM(SLJIT_R0)));
978 if (args >= 2)
979 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S1) | RN(TMP_ZERO) | RM(SLJIT_R1)));
980 if (args >= 3)
981 FAIL_IF(push_inst(compiler, ORR | RD(SLJIT_S2) | RN(TMP_ZERO) | RM(SLJIT_R2)));
982
983 #ifdef _WIN32
984 if (local_size >= 4096) {
985 if (local_size < 4 * 4096) {
986 /* No need for a loop. */
987 if (local_size >= 2 * 4096) {
988 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
989 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
990 local_size -= 4096;
991 }
992
993 if (local_size >= 2 * 4096) {
994 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
995 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
996 local_size -= 4096;
997 }
998
999 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1000 local_size -= 4096;
1001 }
1002 else {
1003 FAIL_IF(push_inst(compiler, MOVZ | RD(TMP_REG2) | (((local_size >> 12) - 1) << 5)));
1004 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1005 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (1 << 10) | (1 << 22)));
1006 FAIL_IF(push_inst(compiler, SUBI | (1 << 29) | RD(TMP_REG2) | RN(TMP_REG2) | (1 << 10)));
1007 FAIL_IF(push_inst(compiler, B_CC | ((((sljit_ins) -3) & 0x7ffff) << 5) | 0x1 /* not-equal */));
1008 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1009
1010 local_size &= 0xfff;
1011 }
1012
1013 if (local_size > 256) {
1014 FAIL_IF(push_inst(compiler, SUBI | RD(TMP_REG1) | RN(TMP_REG1) | (local_size << 10)));
1015 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1016 }
1017 else if (local_size > 0)
1018 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(TMP_REG1) | ((-local_size & 0x1ff) << 12)));
1019
1020 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1021 }
1022 else if (local_size > 256) {
1023 FAIL_IF(push_inst(compiler, LDRI | RT(TMP_ZERO) | RN(TMP_REG1)));
1024 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(TMP_REG1) | (0 << 10)));
1025 }
1026 else if (local_size > 0)
1027 FAIL_IF(push_inst(compiler, LDR_PRE | RT(TMP_ZERO) | RN(SLJIT_SP) | ((-local_size & 0x1ff) << 12)));
1028
1029 #else /* !_WIN32 */
1030
1031 /* The local_size does not include saved registers size. */
1032 if (local_size > 0xfff) {
1033 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1034 local_size &= 0xfff;
1035 }
1036 if (local_size != 0)
1037 FAIL_IF(push_inst(compiler, SUBI | RD(SLJIT_SP) | RN(SLJIT_SP) | (local_size << 10)));
1038
1039 #endif /* _WIN32 */
1040
1041 return SLJIT_SUCCESS;
1042 }
1043
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)1044 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
1045 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
1046 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
1047 {
1048 sljit_s32 saved_regs_size;
1049
1050 CHECK_ERROR();
1051 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
1052 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
1053
1054 saved_regs_size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
1055 if (saved_regs_size & 0x8)
1056 saved_regs_size += sizeof(sljit_sw);
1057
1058 compiler->local_size = saved_regs_size + ((local_size + 15) & ~0xf);
1059 return SLJIT_SUCCESS;
1060 }
1061
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1062 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
1063 {
1064 sljit_s32 local_size;
1065 sljit_s32 i, tmp, offs, prev, saved_regs_size;
1066
1067 CHECK_ERROR();
1068 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
1069
1070 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1071
1072 saved_regs_size = GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 2);
1073 if (saved_regs_size & 0x8)
1074 saved_regs_size += sizeof(sljit_sw);
1075
1076 local_size = compiler->local_size - saved_regs_size;
1077
1078 /* Load LR as early as possible. */
1079 if (local_size == 0)
1080 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1081 else if (local_size < 63 * sizeof(sljit_sw)) {
1082 FAIL_IF(push_inst(compiler, LDP_PRE | RT(TMP_FP) | RT2(TMP_LR)
1083 | RN(SLJIT_SP) | (local_size << (15 - 3))));
1084 }
1085 else {
1086 if (local_size > 0xfff) {
1087 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | ((local_size >> 12) << 10) | (1 << 22)));
1088 local_size &= 0xfff;
1089 }
1090 if (local_size)
1091 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (local_size << 10)));
1092
1093 FAIL_IF(push_inst(compiler, LDP | RT(TMP_FP) | RT2(TMP_LR) | RN(SLJIT_SP)));
1094 }
1095
1096 tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
1097 prev = -1;
1098 offs = 2 << 15;
1099 for (i = SLJIT_S0; i >= tmp; i--) {
1100 if (prev == -1) {
1101 prev = i;
1102 continue;
1103 }
1104 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1105 offs += 2 << 15;
1106 prev = -1;
1107 }
1108
1109 for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
1110 if (prev == -1) {
1111 prev = i;
1112 continue;
1113 }
1114 FAIL_IF(push_inst(compiler, LDP | RT(prev) | RT2(i) | RN(SLJIT_SP) | offs));
1115 offs += 2 << 15;
1116 prev = -1;
1117 }
1118
1119 if (prev != -1)
1120 FAIL_IF(push_inst(compiler, LDRI | RT(prev) | RN(SLJIT_SP) | (offs >> 5)));
1121
1122 /* These two can be executed in parallel. */
1123 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RN(SLJIT_SP) | (saved_regs_size << 10)));
1124 return push_inst(compiler, RET | RN(TMP_LR));
1125 }
1126
1127 /* --------------------------------------------------------------------- */
1128 /* Operators */
1129 /* --------------------------------------------------------------------- */
1130
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)1131 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
1132 {
1133 sljit_ins inv_bits = (op & SLJIT_I32_OP) ? W_OP : 0;
1134
1135 CHECK_ERROR();
1136 CHECK(check_sljit_emit_op0(compiler, op));
1137
1138 op = GET_OPCODE(op);
1139 switch (op) {
1140 case SLJIT_BREAKPOINT:
1141 return push_inst(compiler, BRK);
1142 case SLJIT_NOP:
1143 return push_inst(compiler, NOP);
1144 case SLJIT_LMUL_UW:
1145 case SLJIT_LMUL_SW:
1146 FAIL_IF(push_inst(compiler, ORR | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1147 FAIL_IF(push_inst(compiler, MADD | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1148 return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULH : SMULH) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1149 case SLJIT_DIVMOD_UW:
1150 case SLJIT_DIVMOD_SW:
1151 FAIL_IF(push_inst(compiler, (ORR ^ inv_bits) | RD(TMP_REG1) | RN(TMP_ZERO) | RM(SLJIT_R0)));
1152 FAIL_IF(push_inst(compiler, ((op == SLJIT_DIVMOD_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1)));
1153 FAIL_IF(push_inst(compiler, (MADD ^ inv_bits) | RD(SLJIT_R1) | RN(SLJIT_R0) | RM(SLJIT_R1) | RT2(TMP_ZERO)));
1154 return push_inst(compiler, (SUB ^ inv_bits) | RD(SLJIT_R1) | RN(TMP_REG1) | RM(SLJIT_R1));
1155 case SLJIT_DIV_UW:
1156 case SLJIT_DIV_SW:
1157 return push_inst(compiler, ((op == SLJIT_DIV_UW ? UDIV : SDIV) ^ inv_bits) | RD(SLJIT_R0) | RN(SLJIT_R0) | RM(SLJIT_R1));
1158 case SLJIT_ENDBR:
1159 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
1160 return SLJIT_SUCCESS;
1161 }
1162
1163 return SLJIT_SUCCESS;
1164 }
1165
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1166 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
1167 sljit_s32 dst, sljit_sw dstw,
1168 sljit_s32 src, sljit_sw srcw)
1169 {
1170 sljit_s32 dst_r, flags, mem_flags;
1171 sljit_s32 op_flags = GET_ALL_FLAGS(op);
1172
1173 CHECK_ERROR();
1174 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
1175 ADJUST_LOCAL_OFFSET(dst, dstw);
1176 ADJUST_LOCAL_OFFSET(src, srcw);
1177
1178 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1179
1180 op = GET_OPCODE(op);
1181 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
1182 /* Both operands are registers. */
1183 if (dst_r != TMP_REG1 && FAST_IS_REG(src))
1184 return emit_op_imm(compiler, op | ((op_flags & SLJIT_I32_OP) ? INT_OP : 0), dst_r, TMP_REG1, src);
1185
1186 switch (op) {
1187 case SLJIT_MOV:
1188 case SLJIT_MOV_P:
1189 mem_flags = WORD_SIZE;
1190 break;
1191 case SLJIT_MOV_U8:
1192 mem_flags = BYTE_SIZE;
1193 if (src & SLJIT_IMM)
1194 srcw = (sljit_u8)srcw;
1195 break;
1196 case SLJIT_MOV_S8:
1197 mem_flags = BYTE_SIZE | SIGNED;
1198 if (src & SLJIT_IMM)
1199 srcw = (sljit_s8)srcw;
1200 break;
1201 case SLJIT_MOV_U16:
1202 mem_flags = HALF_SIZE;
1203 if (src & SLJIT_IMM)
1204 srcw = (sljit_u16)srcw;
1205 break;
1206 case SLJIT_MOV_S16:
1207 mem_flags = HALF_SIZE | SIGNED;
1208 if (src & SLJIT_IMM)
1209 srcw = (sljit_s16)srcw;
1210 break;
1211 case SLJIT_MOV_U32:
1212 mem_flags = INT_SIZE;
1213 if (src & SLJIT_IMM)
1214 srcw = (sljit_u32)srcw;
1215 break;
1216 case SLJIT_MOV_S32:
1217 mem_flags = INT_SIZE | SIGNED;
1218 if (src & SLJIT_IMM)
1219 srcw = (sljit_s32)srcw;
1220 break;
1221 default:
1222 SLJIT_UNREACHABLE();
1223 mem_flags = 0;
1224 break;
1225 }
1226
1227 if (src & SLJIT_IMM)
1228 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1229 else if (!(src & SLJIT_MEM))
1230 dst_r = src;
1231 else
1232 FAIL_IF(emit_op_mem(compiler, mem_flags, dst_r, src, srcw, TMP_REG1));
1233
1234 if (dst & SLJIT_MEM)
1235 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1236 return SLJIT_SUCCESS;
1237 }
1238
1239 flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
1240 mem_flags = WORD_SIZE;
1241
1242 if (op_flags & SLJIT_I32_OP) {
1243 flags |= INT_OP;
1244 mem_flags = INT_SIZE;
1245 }
1246
1247 if (dst == SLJIT_UNUSED)
1248 flags |= UNUSED_RETURN;
1249
1250 if (src & SLJIT_MEM) {
1251 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src, srcw, TMP_REG2));
1252 src = TMP_REG2;
1253 }
1254
1255 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, src);
1256
1257 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1258 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1259 return SLJIT_SUCCESS;
1260 }
1261
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1262 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
1263 sljit_s32 dst, sljit_sw dstw,
1264 sljit_s32 src1, sljit_sw src1w,
1265 sljit_s32 src2, sljit_sw src2w)
1266 {
1267 sljit_s32 dst_r, flags, mem_flags;
1268
1269 CHECK_ERROR();
1270 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1271 ADJUST_LOCAL_OFFSET(dst, dstw);
1272 ADJUST_LOCAL_OFFSET(src1, src1w);
1273 ADJUST_LOCAL_OFFSET(src2, src2w);
1274
1275 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
1276 return SLJIT_SUCCESS;
1277
1278 dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
1279 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1280 mem_flags = WORD_SIZE;
1281
1282 if (op & SLJIT_I32_OP) {
1283 flags |= INT_OP;
1284 mem_flags = INT_SIZE;
1285 }
1286
1287 if (dst == SLJIT_UNUSED)
1288 flags |= UNUSED_RETURN;
1289
1290 if (src1 & SLJIT_MEM) {
1291 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, src1, src1w, TMP_REG1));
1292 src1 = TMP_REG1;
1293 }
1294
1295 if (src2 & SLJIT_MEM) {
1296 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG2, src2, src2w, TMP_REG2));
1297 src2 = TMP_REG2;
1298 }
1299
1300 if (src1 & SLJIT_IMM)
1301 flags |= ARG1_IMM;
1302 else
1303 src1w = src1;
1304
1305 if (src2 & SLJIT_IMM)
1306 flags |= ARG2_IMM;
1307 else
1308 src2w = src2;
1309
1310 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1311
1312 if (dst & SLJIT_MEM)
1313 return emit_op_mem(compiler, mem_flags | STORE, dst_r, dst, dstw, TMP_REG2);
1314 return SLJIT_SUCCESS;
1315 }
1316
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)1317 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
1318 sljit_s32 src, sljit_sw srcw)
1319 {
1320 CHECK_ERROR();
1321 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
1322 ADJUST_LOCAL_OFFSET(src, srcw);
1323
1324 switch (op) {
1325 case SLJIT_FAST_RETURN:
1326 if (FAST_IS_REG(src))
1327 FAIL_IF(push_inst(compiler, ORR | RD(TMP_LR) | RN(TMP_ZERO) | RM(src)));
1328 else
1329 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_LR, src, srcw, TMP_REG1));
1330
1331 return push_inst(compiler, RET | RN(TMP_LR));
1332 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
1333 return SLJIT_SUCCESS;
1334 case SLJIT_PREFETCH_L1:
1335 case SLJIT_PREFETCH_L2:
1336 case SLJIT_PREFETCH_L3:
1337 case SLJIT_PREFETCH_ONCE:
1338 SLJIT_ASSERT(reg_map[1] == 0 && reg_map[3] == 2 && reg_map[5] == 4);
1339
1340 /* The reg_map[op] should provide the appropriate constant. */
1341 if (op == SLJIT_PREFETCH_L1)
1342 op = 1;
1343 else if (op == SLJIT_PREFETCH_L2)
1344 op = 3;
1345 else if (op == SLJIT_PREFETCH_L3)
1346 op = 5;
1347 else
1348 op = 2;
1349
1350 /* Signed word sized load is the prefetch instruction. */
1351 return emit_op_mem(compiler, WORD_SIZE | SIGNED, op, src, srcw, TMP_REG1);
1352 }
1353
1354 return SLJIT_SUCCESS;
1355 }
1356
sljit_get_register_index(sljit_s32 reg)1357 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1358 {
1359 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1360 return reg_map[reg];
1361 }
1362
sljit_get_float_register_index(sljit_s32 reg)1363 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1364 {
1365 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1366 return freg_map[reg];
1367 }
1368
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1369 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1370 void *instruction, sljit_s32 size)
1371 {
1372 CHECK_ERROR();
1373 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1374
1375 return push_inst(compiler, *(sljit_ins*)instruction);
1376 }
1377
1378 /* --------------------------------------------------------------------- */
1379 /* Floating point operators */
1380 /* --------------------------------------------------------------------- */
1381
emit_fop_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)1382 static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
1383 {
1384 sljit_u32 shift = MEM_SIZE_SHIFT(flags);
1385 sljit_ins type = (shift << 30);
1386
1387 SLJIT_ASSERT(arg & SLJIT_MEM);
1388
1389 if (!(flags & STORE))
1390 type |= 0x00400000;
1391
1392 if (arg & OFFS_REG_MASK) {
1393 argw &= 3;
1394 if (argw == 0 || argw == shift)
1395 return push_inst(compiler, STR_FR | type | VT(reg)
1396 | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw ? (1 << 12) : 0));
1397
1398 FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG1) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | (argw << 10)));
1399 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1));
1400 }
1401
1402 arg &= REG_MASK;
1403
1404 if (arg == SLJIT_UNUSED) {
1405 FAIL_IF(load_immediate(compiler, TMP_REG1, argw & ~(0xfff << shift)));
1406
1407 argw = (argw >> shift) & 0xfff;
1408
1409 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | (argw << 10));
1410 }
1411
1412 if (argw >= 0 && (argw & ((1 << shift) - 1)) == 0) {
1413 if ((argw >> shift) <= 0xfff)
1414 return push_inst(compiler, STR_FI | type | VT(reg) | RN(arg) | (argw << (10 - shift)));
1415
1416 if (argw <= 0xffffff) {
1417 FAIL_IF(push_inst(compiler, ADDI | (1 << 22) | RD(TMP_REG1) | RN(arg) | ((argw >> 12) << 10)));
1418
1419 argw = ((argw & 0xfff) >> shift);
1420 return push_inst(compiler, STR_FI | type | VT(reg) | RN(TMP_REG1) | (argw << 10));
1421 }
1422 }
1423
1424 if (argw <= 255 && argw >= -256)
1425 return push_inst(compiler, STUR_FI | type | VT(reg) | RN(arg) | ((argw & 0x1ff) << 12));
1426
1427 FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
1428 return push_inst(compiler, STR_FR | type | VT(reg) | RN(arg) | RM(TMP_REG1));
1429 }
1430
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1431 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1432 sljit_s32 dst, sljit_sw dstw,
1433 sljit_s32 src, sljit_sw srcw)
1434 {
1435 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1436 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1437
1438 if (GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64)
1439 inv_bits |= W_OP;
1440
1441 if (src & SLJIT_MEM) {
1442 emit_fop_mem(compiler, (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE, TMP_FREG1, src, srcw);
1443 src = TMP_FREG1;
1444 }
1445
1446 FAIL_IF(push_inst(compiler, (FCVTZS ^ inv_bits) | RD(dst_r) | VN(src)));
1447
1448 if (dst & SLJIT_MEM)
1449 return emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_S32_FROM_F64) ? INT_SIZE : WORD_SIZE) | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1450 return SLJIT_SUCCESS;
1451 }
1452
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1453 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1454 sljit_s32 dst, sljit_sw dstw,
1455 sljit_s32 src, sljit_sw srcw)
1456 {
1457 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1458 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1459
1460 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1461 inv_bits |= W_OP;
1462
1463 if (src & SLJIT_MEM) {
1464 emit_op_mem(compiler, ((GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32) ? INT_SIZE : WORD_SIZE), TMP_REG1, src, srcw, TMP_REG1);
1465 src = TMP_REG1;
1466 } else if (src & SLJIT_IMM) {
1467 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1468 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1469 srcw = (sljit_s32)srcw;
1470 #endif
1471 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1472 src = TMP_REG1;
1473 }
1474
1475 FAIL_IF(push_inst(compiler, (SCVTF ^ inv_bits) | VD(dst_r) | RN(src)));
1476
1477 if (dst & SLJIT_MEM)
1478 return emit_fop_mem(compiler, ((op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE) | STORE, TMP_FREG1, dst, dstw);
1479 return SLJIT_SUCCESS;
1480 }
1481
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1482 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1483 sljit_s32 src1, sljit_sw src1w,
1484 sljit_s32 src2, sljit_sw src2w)
1485 {
1486 sljit_s32 mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1487 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1488
1489 if (src1 & SLJIT_MEM) {
1490 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1491 src1 = TMP_FREG1;
1492 }
1493
1494 if (src2 & SLJIT_MEM) {
1495 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1496 src2 = TMP_FREG2;
1497 }
1498
1499 return push_inst(compiler, (FCMP ^ inv_bits) | VN(src1) | VM(src2));
1500 }
1501
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1502 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1503 sljit_s32 dst, sljit_sw dstw,
1504 sljit_s32 src, sljit_sw srcw)
1505 {
1506 sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1507 sljit_ins inv_bits;
1508
1509 CHECK_ERROR();
1510
1511 SLJIT_COMPILE_ASSERT((INT_SIZE ^ 0x1) == WORD_SIZE, must_be_one_bit_difference);
1512 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1513
1514 inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1515 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1516
1517 if (src & SLJIT_MEM) {
1518 emit_fop_mem(compiler, (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32) ? (mem_flags ^ 0x1) : mem_flags, dst_r, src, srcw);
1519 src = dst_r;
1520 }
1521
1522 switch (GET_OPCODE(op)) {
1523 case SLJIT_MOV_F64:
1524 if (src != dst_r) {
1525 if (dst_r != TMP_FREG1)
1526 FAIL_IF(push_inst(compiler, (FMOV ^ inv_bits) | VD(dst_r) | VN(src)));
1527 else
1528 dst_r = src;
1529 }
1530 break;
1531 case SLJIT_NEG_F64:
1532 FAIL_IF(push_inst(compiler, (FNEG ^ inv_bits) | VD(dst_r) | VN(src)));
1533 break;
1534 case SLJIT_ABS_F64:
1535 FAIL_IF(push_inst(compiler, (FABS ^ inv_bits) | VD(dst_r) | VN(src)));
1536 break;
1537 case SLJIT_CONV_F64_FROM_F32:
1538 FAIL_IF(push_inst(compiler, FCVT | ((op & SLJIT_F32_OP) ? (1 << 22) : (1 << 15)) | VD(dst_r) | VN(src)));
1539 break;
1540 }
1541
1542 if (dst & SLJIT_MEM)
1543 return emit_fop_mem(compiler, mem_flags | STORE, dst_r, dst, dstw);
1544 return SLJIT_SUCCESS;
1545 }
1546
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1547 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1548 sljit_s32 dst, sljit_sw dstw,
1549 sljit_s32 src1, sljit_sw src1w,
1550 sljit_s32 src2, sljit_sw src2w)
1551 {
1552 sljit_s32 dst_r, mem_flags = (op & SLJIT_F32_OP) ? INT_SIZE : WORD_SIZE;
1553 sljit_ins inv_bits = (op & SLJIT_F32_OP) ? (1 << 22) : 0;
1554
1555 CHECK_ERROR();
1556 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1557 ADJUST_LOCAL_OFFSET(dst, dstw);
1558 ADJUST_LOCAL_OFFSET(src1, src1w);
1559 ADJUST_LOCAL_OFFSET(src2, src2w);
1560
1561 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1562 if (src1 & SLJIT_MEM) {
1563 emit_fop_mem(compiler, mem_flags, TMP_FREG1, src1, src1w);
1564 src1 = TMP_FREG1;
1565 }
1566 if (src2 & SLJIT_MEM) {
1567 emit_fop_mem(compiler, mem_flags, TMP_FREG2, src2, src2w);
1568 src2 = TMP_FREG2;
1569 }
1570
1571 switch (GET_OPCODE(op)) {
1572 case SLJIT_ADD_F64:
1573 FAIL_IF(push_inst(compiler, (FADD ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1574 break;
1575 case SLJIT_SUB_F64:
1576 FAIL_IF(push_inst(compiler, (FSUB ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1577 break;
1578 case SLJIT_MUL_F64:
1579 FAIL_IF(push_inst(compiler, (FMUL ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1580 break;
1581 case SLJIT_DIV_F64:
1582 FAIL_IF(push_inst(compiler, (FDIV ^ inv_bits) | VD(dst_r) | VN(src1) | VM(src2)));
1583 break;
1584 }
1585
1586 if (!(dst & SLJIT_MEM))
1587 return SLJIT_SUCCESS;
1588 return emit_fop_mem(compiler, mem_flags | STORE, TMP_FREG1, dst, dstw);
1589 }
1590
1591 /* --------------------------------------------------------------------- */
1592 /* Other instructions */
1593 /* --------------------------------------------------------------------- */
1594
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1595 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1596 {
1597 CHECK_ERROR();
1598 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1599 ADJUST_LOCAL_OFFSET(dst, dstw);
1600
1601 if (FAST_IS_REG(dst))
1602 return push_inst(compiler, ORR | RD(dst) | RN(TMP_ZERO) | RM(TMP_LR));
1603
1604 /* Memory. */
1605 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_LR, dst, dstw, TMP_REG1);
1606 }
1607
1608 /* --------------------------------------------------------------------- */
1609 /* Conditional instructions */
1610 /* --------------------------------------------------------------------- */
1611
get_cc(sljit_s32 type)1612 static sljit_uw get_cc(sljit_s32 type)
1613 {
1614 switch (type) {
1615 case SLJIT_EQUAL:
1616 case SLJIT_MUL_NOT_OVERFLOW:
1617 case SLJIT_EQUAL_F64:
1618 return 0x1;
1619
1620 case SLJIT_NOT_EQUAL:
1621 case SLJIT_MUL_OVERFLOW:
1622 case SLJIT_NOT_EQUAL_F64:
1623 return 0x0;
1624
1625 case SLJIT_LESS:
1626 case SLJIT_LESS_F64:
1627 return 0x2;
1628
1629 case SLJIT_GREATER_EQUAL:
1630 case SLJIT_GREATER_EQUAL_F64:
1631 return 0x3;
1632
1633 case SLJIT_GREATER:
1634 case SLJIT_GREATER_F64:
1635 return 0x9;
1636
1637 case SLJIT_LESS_EQUAL:
1638 case SLJIT_LESS_EQUAL_F64:
1639 return 0x8;
1640
1641 case SLJIT_SIG_LESS:
1642 return 0xa;
1643
1644 case SLJIT_SIG_GREATER_EQUAL:
1645 return 0xb;
1646
1647 case SLJIT_SIG_GREATER:
1648 return 0xd;
1649
1650 case SLJIT_SIG_LESS_EQUAL:
1651 return 0xc;
1652
1653 case SLJIT_OVERFLOW:
1654 case SLJIT_UNORDERED_F64:
1655 return 0x7;
1656
1657 case SLJIT_NOT_OVERFLOW:
1658 case SLJIT_ORDERED_F64:
1659 return 0x6;
1660
1661 default:
1662 SLJIT_UNREACHABLE();
1663 return 0xe;
1664 }
1665 }
1666
sljit_emit_label(struct sljit_compiler * compiler)1667 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1668 {
1669 struct sljit_label *label;
1670
1671 CHECK_ERROR_PTR();
1672 CHECK_PTR(check_sljit_emit_label(compiler));
1673
1674 if (compiler->last_label && compiler->last_label->size == compiler->size)
1675 return compiler->last_label;
1676
1677 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1678 PTR_FAIL_IF(!label);
1679 set_label(label, compiler);
1680 return label;
1681 }
1682
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1683 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1684 {
1685 struct sljit_jump *jump;
1686
1687 CHECK_ERROR_PTR();
1688 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1689
1690 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1691 PTR_FAIL_IF(!jump);
1692 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1693 type &= 0xff;
1694
1695 if (type < SLJIT_JUMP) {
1696 jump->flags |= IS_COND;
1697 PTR_FAIL_IF(push_inst(compiler, B_CC | (6 << 5) | get_cc(type)));
1698 }
1699 else if (type >= SLJIT_FAST_CALL)
1700 jump->flags |= IS_BL;
1701
1702 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1703 jump->addr = compiler->size;
1704 PTR_FAIL_IF(push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1)));
1705
1706 return jump;
1707 }
1708
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)1709 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1710 sljit_s32 arg_types)
1711 {
1712 CHECK_ERROR_PTR();
1713 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1714
1715 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1716 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1717 compiler->skip_checks = 1;
1718 #endif
1719
1720 return sljit_emit_jump(compiler, type);
1721 }
1722
emit_cmp_to0(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1723 static SLJIT_INLINE struct sljit_jump* emit_cmp_to0(struct sljit_compiler *compiler, sljit_s32 type,
1724 sljit_s32 src, sljit_sw srcw)
1725 {
1726 struct sljit_jump *jump;
1727 sljit_ins inv_bits = (type & SLJIT_I32_OP) ? W_OP : 0;
1728
1729 SLJIT_ASSERT((type & 0xff) == SLJIT_EQUAL || (type & 0xff) == SLJIT_NOT_EQUAL);
1730 ADJUST_LOCAL_OFFSET(src, srcw);
1731
1732 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1733 PTR_FAIL_IF(!jump);
1734 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1735 jump->flags |= IS_CBZ | IS_COND;
1736
1737 if (src & SLJIT_MEM) {
1738 PTR_FAIL_IF(emit_op_mem(compiler, inv_bits ? INT_SIZE : WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1739 src = TMP_REG1;
1740 }
1741 else if (src & SLJIT_IMM) {
1742 PTR_FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1743 src = TMP_REG1;
1744 }
1745
1746 SLJIT_ASSERT(FAST_IS_REG(src));
1747
1748 if ((type & 0xff) == SLJIT_EQUAL)
1749 inv_bits |= 1 << 24;
1750
1751 PTR_FAIL_IF(push_inst(compiler, (CBZ ^ inv_bits) | (6 << 5) | RT(src)));
1752 PTR_FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1753 jump->addr = compiler->size;
1754 PTR_FAIL_IF(push_inst(compiler, BR | RN(TMP_REG1)));
1755 return jump;
1756 }
1757
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1758 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1759 {
1760 struct sljit_jump *jump;
1761
1762 CHECK_ERROR();
1763 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1764 ADJUST_LOCAL_OFFSET(src, srcw);
1765
1766 if (!(src & SLJIT_IMM)) {
1767 if (src & SLJIT_MEM) {
1768 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
1769 src = TMP_REG1;
1770 }
1771 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(src));
1772 }
1773
1774 /* These jumps are converted to jump/call instructions when possible. */
1775 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1776 FAIL_IF(!jump);
1777 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
1778 jump->u.target = srcw;
1779
1780 FAIL_IF(emit_imm64_const(compiler, TMP_REG1, 0));
1781 jump->addr = compiler->size;
1782 return push_inst(compiler, ((type >= SLJIT_FAST_CALL) ? BLR : BR) | RN(TMP_REG1));
1783 }
1784
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)1785 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1786 sljit_s32 arg_types,
1787 sljit_s32 src, sljit_sw srcw)
1788 {
1789 CHECK_ERROR();
1790 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1791
1792 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1793 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1794 compiler->skip_checks = 1;
1795 #endif
1796
1797 return sljit_emit_ijump(compiler, type, src, srcw);
1798 }
1799
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)1800 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1801 sljit_s32 dst, sljit_sw dstw,
1802 sljit_s32 type)
1803 {
1804 sljit_s32 dst_r, src_r, flags, mem_flags;
1805 sljit_ins cc;
1806
1807 CHECK_ERROR();
1808 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1809 ADJUST_LOCAL_OFFSET(dst, dstw);
1810
1811 cc = get_cc(type & 0xff);
1812 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
1813
1814 if (GET_OPCODE(op) < SLJIT_ADD) {
1815 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(dst_r) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1816
1817 if (dst_r == TMP_REG1) {
1818 mem_flags = (GET_OPCODE(op) == SLJIT_MOV ? WORD_SIZE : INT_SIZE) | STORE;
1819 return emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG2);
1820 }
1821
1822 return SLJIT_SUCCESS;
1823 }
1824
1825 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1826 mem_flags = WORD_SIZE;
1827
1828 if (op & SLJIT_I32_OP) {
1829 flags |= INT_OP;
1830 mem_flags = INT_SIZE;
1831 }
1832
1833 src_r = dst;
1834
1835 if (dst & SLJIT_MEM) {
1836 FAIL_IF(emit_op_mem(compiler, mem_flags, TMP_REG1, dst, dstw, TMP_REG1));
1837 src_r = TMP_REG1;
1838 }
1839
1840 FAIL_IF(push_inst(compiler, CSINC | (cc << 12) | RD(TMP_REG2) | RN(TMP_ZERO) | RM(TMP_ZERO)));
1841 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src_r, TMP_REG2);
1842
1843 if (dst & SLJIT_MEM)
1844 return emit_op_mem(compiler, mem_flags | STORE, TMP_REG1, dst, dstw, TMP_REG2);
1845 return SLJIT_SUCCESS;
1846 }
1847
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)1848 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1849 sljit_s32 dst_reg,
1850 sljit_s32 src, sljit_sw srcw)
1851 {
1852 sljit_ins inv_bits = (dst_reg & SLJIT_I32_OP) ? W_OP : 0;
1853 sljit_ins cc;
1854
1855 CHECK_ERROR();
1856 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1857
1858 if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
1859 if (dst_reg & SLJIT_I32_OP)
1860 srcw = (sljit_s32)srcw;
1861 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1862 src = TMP_REG1;
1863 srcw = 0;
1864 }
1865
1866 cc = get_cc(type & 0xff);
1867 dst_reg &= ~SLJIT_I32_OP;
1868
1869 return push_inst(compiler, (CSEL ^ inv_bits) | (cc << 12) | RD(dst_reg) | RN(dst_reg) | RM(src));
1870 }
1871
sljit_emit_mem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 reg,sljit_s32 mem,sljit_sw memw)1872 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
1873 sljit_s32 reg,
1874 sljit_s32 mem, sljit_sw memw)
1875 {
1876 sljit_u32 sign = 0, inst;
1877
1878 CHECK_ERROR();
1879 CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
1880
1881 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1882 return SLJIT_ERR_UNSUPPORTED;
1883
1884 if (type & SLJIT_MEM_SUPP)
1885 return SLJIT_SUCCESS;
1886
1887 switch (type & 0xff) {
1888 case SLJIT_MOV:
1889 case SLJIT_MOV_P:
1890 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1891 break;
1892 case SLJIT_MOV_S8:
1893 sign = 1;
1894 case SLJIT_MOV_U8:
1895 inst = STURBI | (MEM_SIZE_SHIFT(BYTE_SIZE) << 30) | 0x400;
1896 break;
1897 case SLJIT_MOV_S16:
1898 sign = 1;
1899 case SLJIT_MOV_U16:
1900 inst = STURBI | (MEM_SIZE_SHIFT(HALF_SIZE) << 30) | 0x400;
1901 break;
1902 case SLJIT_MOV_S32:
1903 sign = 1;
1904 case SLJIT_MOV_U32:
1905 inst = STURBI | (MEM_SIZE_SHIFT(INT_SIZE) << 30) | 0x400;
1906 break;
1907 default:
1908 SLJIT_UNREACHABLE();
1909 inst = STURBI | (MEM_SIZE_SHIFT(WORD_SIZE) << 30) | 0x400;
1910 break;
1911 }
1912
1913 if (!(type & SLJIT_MEM_STORE))
1914 inst |= sign ? 0x00800000 : 0x00400000;
1915
1916 if (type & SLJIT_MEM_PRE)
1917 inst |= 0x800;
1918
1919 return push_inst(compiler, inst | RT(reg) | RN(mem & REG_MASK) | ((memw & 0x1ff) << 12));
1920 }
1921
sljit_emit_fmem(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 freg,sljit_s32 mem,sljit_sw memw)1922 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
1923 sljit_s32 freg,
1924 sljit_s32 mem, sljit_sw memw)
1925 {
1926 sljit_u32 inst;
1927
1928 CHECK_ERROR();
1929 CHECK(check_sljit_emit_fmem(compiler, type, freg, mem, memw));
1930
1931 if ((mem & OFFS_REG_MASK) || (memw > 255 || memw < -256))
1932 return SLJIT_ERR_UNSUPPORTED;
1933
1934 if (type & SLJIT_MEM_SUPP)
1935 return SLJIT_SUCCESS;
1936
1937 inst = STUR_FI | 0x80000400;
1938
1939 if (!(type & SLJIT_F32_OP))
1940 inst |= 0x40000000;
1941
1942 if (!(type & SLJIT_MEM_STORE))
1943 inst |= 0x00400000;
1944
1945 if (type & SLJIT_MEM_PRE)
1946 inst |= 0x800;
1947
1948 return push_inst(compiler, inst | VT(freg) | RN(mem & REG_MASK) | ((memw & 0x1ff) << 12));
1949 }
1950
sljit_get_local_base(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw offset)1951 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset)
1952 {
1953 sljit_s32 dst_reg;
1954 sljit_ins ins;
1955
1956 CHECK_ERROR();
1957 CHECK(check_sljit_get_local_base(compiler, dst, dstw, offset));
1958
1959 SLJIT_ASSERT (SLJIT_LOCALS_OFFSET_BASE == 0);
1960
1961 dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
1962
1963 if (offset <= 0xffffff && offset >= -0xffffff) {
1964 ins = ADDI;
1965 if (offset < 0) {
1966 offset = -offset;
1967 ins = SUBI;
1968 }
1969
1970 if (offset <= 0xfff)
1971 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | (offset << 10)));
1972 else {
1973 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(SLJIT_SP) | ((offset & 0xfff000) >> (12 - 10)) | (1 << 22)));
1974
1975 offset &= 0xfff;
1976 if (offset != 0)
1977 FAIL_IF(push_inst(compiler, ins | RD(dst_reg) | RN(dst_reg) | (offset << 10)));
1978 }
1979 }
1980 else {
1981 FAIL_IF(load_immediate (compiler, dst_reg, offset));
1982 /* Add extended register form. */
1983 FAIL_IF(push_inst(compiler, ADDE | (0x3 << 13) | RD(dst_reg) | RN(SLJIT_SP) | RM(dst_reg)));
1984 }
1985
1986 if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
1987 return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG1);
1988 return SLJIT_SUCCESS;
1989 }
1990
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)1991 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
1992 {
1993 struct sljit_const *const_;
1994 sljit_s32 dst_r;
1995
1996 CHECK_ERROR_PTR();
1997 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1998 ADJUST_LOCAL_OFFSET(dst, dstw);
1999
2000 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
2001 PTR_FAIL_IF(!const_);
2002 set_const(const_, compiler);
2003
2004 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2005 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, init_value));
2006
2007 if (dst & SLJIT_MEM)
2008 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2009 return const_;
2010 }
2011
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)2012 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
2013 {
2014 struct sljit_put_label *put_label;
2015 sljit_s32 dst_r;
2016
2017 CHECK_ERROR_PTR();
2018 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
2019 ADJUST_LOCAL_OFFSET(dst, dstw);
2020
2021 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
2022 PTR_FAIL_IF(emit_imm64_const(compiler, dst_r, 0));
2023
2024 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
2025 PTR_FAIL_IF(!put_label);
2026 set_put_label(put_label, compiler, 1);
2027
2028 if (dst & SLJIT_MEM)
2029 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
2030
2031 return put_label;
2032 }
2033
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)2034 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
2035 {
2036 sljit_ins* inst = (sljit_ins*)addr;
2037 modify_imm64_const(inst, new_target);
2038 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2039 SLJIT_CACHE_FLUSH(inst, inst + 4);
2040 }
2041
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)2042 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
2043 {
2044 sljit_ins* inst = (sljit_ins*)addr;
2045 modify_imm64_const(inst, new_constant);
2046 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
2047 SLJIT_CACHE_FLUSH(inst, inst + 4);
2048 }
2049