/[pcre]/code/trunk/sljit/sljitNativeARM_Thumb2.c
ViewVC logotype

Contents of /code/trunk/sljit/sljitNativeARM_Thumb2.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1195 - (show annotations)
Thu Nov 1 15:21:27 2012 UTC (6 years, 9 months ago) by zherczeg
File MIME type: text/plain
File size: 61580 byte(s)
Another huge JIT compiler update.
1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright 2009-2012 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
27 SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void)
28 {
29 return "ARM-Thumb2" SLJIT_CPUINFO;
30 }
31
32 /* Length of an instruction word. */
33 typedef sljit_ui sljit_ins;
34
35 /* Last register + 1. */
36 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
37 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
38 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
39 #define TMP_PC (SLJIT_NO_REGISTERS + 4)
40
41 #define TMP_FREG1 (0)
42 #define TMP_FREG2 (SLJIT_FLOAT_REG6 + 1)
43
44 /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
45 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 5] = {
46 0, 0, 1, 2, 12, 5, 6, 7, 8, 10, 11, 13, 3, 4, 14, 15
47 };
48
49 #define COPY_BITS(src, from, to, bits) \
50 ((from >= to ? (src >> (from - to)) : (src << (to - from))) & (((1 << bits) - 1) << to))
51
52 /* Thumb16 encodings. */
53 #define RD3(rd) (reg_map[rd])
54 #define RN3(rn) (reg_map[rn] << 3)
55 #define RM3(rm) (reg_map[rm] << 6)
56 #define RDN3(rdn) (reg_map[rdn] << 8)
57 #define IMM3(imm) (imm << 6)
58 #define IMM8(imm) (imm)
59
60 /* Thumb16 helpers. */
61 #define SET_REGS44(rd, rn) \
62 ((reg_map[rn] << 3) | (reg_map[rd] & 0x7) | ((reg_map[rd] & 0x8) << 4))
63 #define IS_2_LO_REGS(reg1, reg2) \
64 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
65 #define IS_3_LO_REGS(reg1, reg2, reg3) \
66 (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
67
68 /* Thumb32 encodings. */
69 #define RD4(rd) (reg_map[rd] << 8)
70 #define RN4(rn) (reg_map[rn] << 16)
71 #define RM4(rm) (reg_map[rm])
72 #define RT4(rt) (reg_map[rt] << 12)
73 #define DD4(dd) ((dd) << 12)
74 #define DN4(dn) ((dn) << 16)
75 #define DM4(dm) (dm)
76 #define IMM5(imm) \
77 (COPY_BITS(imm, 2, 12, 3) | ((imm & 0x3) << 6))
78 #define IMM12(imm) \
79 (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff))
80
81 /* --------------------------------------------------------------------- */
82 /* Instrucion forms */
83 /* --------------------------------------------------------------------- */
84
85 /* dot '.' changed to _
86 I immediate form (possibly followed by number of immediate bits). */
87 #define ADCI 0xf1400000
88 #define ADCS 0x4140
89 #define ADC_W 0xeb400000
90 #define ADD 0x4400
91 #define ADDS 0x1800
92 #define ADDSI3 0x1c00
93 #define ADDSI8 0x3000
94 #define ADD_W 0xeb000000
95 #define ADDWI 0xf2000000
96 #define ADD_SP 0xb000
97 #define ADD_W 0xeb000000
98 #define ADD_WI 0xf1000000
99 #define ANDI 0xf0000000
100 #define ANDS 0x4000
101 #define AND_W 0xea000000
102 #define ASRS 0x4100
103 #define ASRSI 0x1000
104 #define ASR_W 0xfa40f000
105 #define ASR_WI 0xea4f0020
106 #define BICI 0xf0200000
107 #define BKPT 0xbe00
108 #define BLX 0x4780
109 #define BX 0x4700
110 #define CLZ 0xfab0f080
111 #define CMPI 0x2800
112 #define CMP_W 0xebb00f00
113 #define EORI 0xf0800000
114 #define EORS 0x4040
115 #define EOR_W 0xea800000
116 #define IT 0xbf00
117 #define LSLS 0x4080
118 #define LSLSI 0x0000
119 #define LSL_W 0xfa00f000
120 #define LSL_WI 0xea4f0000
121 #define LSRS 0x40c0
122 #define LSRSI 0x0800
123 #define LSR_W 0xfa20f000
124 #define LSR_WI 0xea4f0010
125 #define MOV 0x4600
126 #define MOVS 0x0000
127 #define MOVSI 0x2000
128 #define MOVT 0xf2c00000
129 #define MOVW 0xf2400000
130 #define MOV_W 0xea4f0000
131 #define MOV_WI 0xf04f0000
132 #define MUL 0xfb00f000
133 #define MVNS 0x43c0
134 #define MVN_W 0xea6f0000
135 #define MVN_WI 0xf06f0000
136 #define NOP 0xbf00
137 #define ORNI 0xf0600000
138 #define ORRI 0xf0400000
139 #define ORRS 0x4300
140 #define ORR_W 0xea400000
141 #define POP 0xbd00
142 #define POP_W 0xe8bd0000
143 #define PUSH 0xb500
144 #define PUSH_W 0xe92d0000
145 #define RSB_WI 0xf1c00000
146 #define RSBSI 0x4240
147 #define SBCI 0xf1600000
148 #define SBCS 0x4180
149 #define SBC_W 0xeb600000
150 #define SMULL 0xfb800000
151 #define STR_SP 0x9000
152 #define SUBS 0x1a00
153 #define SUBSI3 0x1e00
154 #define SUBSI8 0x3800
155 #define SUB_W 0xeba00000
156 #define SUBWI 0xf2a00000
157 #define SUB_SP 0xb080
158 #define SUB_WI 0xf1a00000
159 #define SXTB 0xb240
160 #define SXTB_W 0xfa4ff080
161 #define SXTH 0xb200
162 #define SXTH_W 0xfa0ff080
163 #define TST 0x4200
164 #define UMULL 0xfba00000
165 #define UXTB 0xb2c0
166 #define UXTB_W 0xfa5ff080
167 #define UXTH 0xb280
168 #define UXTH_W 0xfa1ff080
169 #define VABS_F32 0xeeb00ac0
170 #define VADD_F32 0xee300a00
171 #define VCMP_F32 0xeeb40a40
172 #define VDIV_F32 0xee800a00
173 #define VMOV_F32 0xeeb00a40
174 #define VMRS 0xeef1fa10
175 #define VMUL_F32 0xee200a00
176 #define VNEG_F32 0xeeb10a40
177 #define VSTR_F32 0xed000a00
178 #define VSUB_F32 0xee300a40
179
180 static sljit_si push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
181 {
182 sljit_uh *ptr;
183 SLJIT_ASSERT(!(inst & 0xffff0000));
184
185 ptr = (sljit_uh*)ensure_buf(compiler, sizeof(sljit_uh));
186 FAIL_IF(!ptr);
187 *ptr = inst;
188 compiler->size++;
189 return SLJIT_SUCCESS;
190 }
191
192 static sljit_si push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
193 {
194 sljit_uh *ptr = (sljit_uh*)ensure_buf(compiler, sizeof(sljit_ins));
195 FAIL_IF(!ptr);
196 *ptr++ = inst >> 16;
197 *ptr = inst;
198 compiler->size += 2;
199 return SLJIT_SUCCESS;
200 }
201
202 static SLJIT_INLINE sljit_si emit_imm32_const(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
203 {
204 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst) |
205 COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
206 return push_inst32(compiler, MOVT | RD4(dst) |
207 COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
208 }
209
210 static SLJIT_INLINE void modify_imm32_const(sljit_uh* inst, sljit_uw new_imm)
211 {
212 sljit_si dst = inst[1] & 0x0f00;
213 SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
214 inst[0] = (MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1);
215 inst[1] = dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff);
216 inst[2] = (MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1);
217 inst[3] = dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16);
218 }
219
220 static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_uh *code_ptr, sljit_uh *code)
221 {
222 sljit_sw diff;
223
224 if (jump->flags & SLJIT_REWRITABLE_JUMP)
225 return 0;
226
227 if (jump->flags & JUMP_ADDR) {
228 /* Branch to ARM code is not optimized yet. */
229 if (!(jump->u.target & 0x1))
230 return 0;
231 diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2)) >> 1;
232 }
233 else {
234 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
235 diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
236 }
237
238 if (jump->flags & IS_COND) {
239 SLJIT_ASSERT(!(jump->flags & IS_BL));
240 if (diff <= 127 && diff >= -128) {
241 jump->flags |= B_TYPE1;
242 return 5;
243 }
244 if (diff <= 524287 && diff >= -524288) {
245 jump->flags |= B_TYPE2;
246 return 4;
247 }
248 /* +1 comes from the prefix IT instruction. */
249 diff--;
250 if (diff <= 8388607 && diff >= -8388608) {
251 jump->flags |= B_TYPE3;
252 return 3;
253 }
254 }
255 else if (jump->flags & IS_BL) {
256 if (diff <= 8388607 && diff >= -8388608) {
257 jump->flags |= BL_TYPE6;
258 return 3;
259 }
260 }
261 else {
262 if (diff <= 1023 && diff >= -1024) {
263 jump->flags |= B_TYPE4;
264 return 4;
265 }
266 if (diff <= 8388607 && diff >= -8388608) {
267 jump->flags |= B_TYPE5;
268 return 3;
269 }
270 }
271
272 return 0;
273 }
274
275 static SLJIT_INLINE void inline_set_jump_addr(sljit_uw addr, sljit_uw new_addr, sljit_si flush)
276 {
277 sljit_uh* inst = (sljit_uh*)addr;
278 modify_imm32_const(inst, new_addr);
279 if (flush) {
280 SLJIT_CACHE_FLUSH(inst, inst + 3);
281 }
282 }
283
284 static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump)
285 {
286 sljit_si type = (jump->flags >> 4) & 0xf;
287 sljit_sw diff;
288 sljit_uh *jump_inst;
289 sljit_si s, j1, j2;
290
291 if (SLJIT_UNLIKELY(type == 0)) {
292 inline_set_jump_addr(jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
293 return;
294 }
295
296 if (jump->flags & JUMP_ADDR) {
297 SLJIT_ASSERT(jump->u.target & 0x1);
298 diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + 4)) >> 1;
299 }
300 else
301 diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + 4)) >> 1;
302 jump_inst = (sljit_uh*)jump->addr;
303
304 switch (type) {
305 case 1:
306 /* Encoding T1 of 'B' instruction */
307 SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
308 jump_inst[0] = 0xd000 | (jump->flags & 0xf00) | (diff & 0xff);
309 return;
310 case 2:
311 /* Encoding T3 of 'B' instruction */
312 SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
313 jump_inst[0] = 0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1);
314 jump_inst[1] = 0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | (diff & 0x7ff);
315 return;
316 case 3:
317 SLJIT_ASSERT(jump->flags & IS_COND);
318 *jump_inst++ = IT | ((jump->flags >> 4) & 0xf0) | 0x8;
319 diff--;
320 type = 5;
321 break;
322 case 4:
323 /* Encoding T2 of 'B' instruction */
324 SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
325 jump_inst[0] = 0xe000 | (diff & 0x7ff);
326 return;
327 }
328
329 SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
330
331 /* Really complex instruction form for branches. */
332 s = (diff >> 23) & 0x1;
333 j1 = (~(diff >> 21) ^ s) & 0x1;
334 j2 = (~(diff >> 22) ^ s) & 0x1;
335 jump_inst[0] = 0xf000 | (s << 10) | COPY_BITS(diff, 11, 0, 10);
336 jump_inst[1] = (j1 << 13) | (j2 << 11) | (diff & 0x7ff);
337
338 /* The others have a common form. */
339 if (type == 5) /* Encoding T4 of 'B' instruction */
340 jump_inst[1] |= 0x9000;
341 else if (type == 6) /* Encoding T1 of 'BL' instruction */
342 jump_inst[1] |= 0xd000;
343 else
344 SLJIT_ASSERT_STOP();
345 }
346
347 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
348 {
349 struct sljit_memory_fragment *buf;
350 sljit_uh *code;
351 sljit_uh *code_ptr;
352 sljit_uh *buf_ptr;
353 sljit_uh *buf_end;
354 sljit_uw half_count;
355
356 struct sljit_label *label;
357 struct sljit_jump *jump;
358 struct sljit_const *const_;
359
360 CHECK_ERROR_PTR();
361 check_sljit_generate_code(compiler);
362 reverse_buf(compiler);
363
364 code = (sljit_uh*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_uh));
365 PTR_FAIL_WITH_EXEC_IF(code);
366 buf = compiler->buf;
367
368 code_ptr = code;
369 half_count = 0;
370 label = compiler->labels;
371 jump = compiler->jumps;
372 const_ = compiler->consts;
373
374 do {
375 buf_ptr = (sljit_uh*)buf->memory;
376 buf_end = buf_ptr + (buf->used_size >> 1);
377 do {
378 *code_ptr = *buf_ptr++;
379 /* These structures are ordered by their address. */
380 SLJIT_ASSERT(!label || label->size >= half_count);
381 SLJIT_ASSERT(!jump || jump->addr >= half_count);
382 SLJIT_ASSERT(!const_ || const_->addr >= half_count);
383 if (label && label->size == half_count) {
384 label->addr = ((sljit_uw)code_ptr) | 0x1;
385 label->size = code_ptr - code;
386 label = label->next;
387 }
388 if (jump && jump->addr == half_count) {
389 jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
390 code_ptr -= detect_jump_type(jump, code_ptr, code);
391 jump = jump->next;
392 }
393 if (const_ && const_->addr == half_count) {
394 const_->addr = (sljit_uw)code_ptr;
395 const_ = const_->next;
396 }
397 code_ptr ++;
398 half_count ++;
399 } while (buf_ptr < buf_end);
400
401 buf = buf->next;
402 } while (buf);
403
404 if (label && label->size == half_count) {
405 label->addr = ((sljit_uw)code_ptr) | 0x1;
406 label->size = code_ptr - code;
407 label = label->next;
408 }
409
410 SLJIT_ASSERT(!label);
411 SLJIT_ASSERT(!jump);
412 SLJIT_ASSERT(!const_);
413 SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
414
415 jump = compiler->jumps;
416 while (jump) {
417 set_jump_instruction(jump);
418 jump = jump->next;
419 }
420
421 SLJIT_CACHE_FLUSH(code, code_ptr);
422 compiler->error = SLJIT_ERR_COMPILED;
423 compiler->executable_size = compiler->size * sizeof(sljit_uh);
424 /* Set thumb mode flag. */
425 return (void*)((sljit_uw)code | 0x1);
426 }
427
428 #define INVALID_IMM 0x80000000
429 static sljit_uw get_imm(sljit_uw imm)
430 {
431 /* Thumb immediate form. */
432 sljit_si counter;
433
434 if (imm <= 0xff)
435 return imm;
436
437 if ((imm & 0xffff) == (imm >> 16)) {
438 /* Some special cases. */
439 if (!(imm & 0xff00))
440 return (1 << 12) | (imm & 0xff);
441 if (!(imm & 0xff))
442 return (2 << 12) | ((imm >> 8) & 0xff);
443 if ((imm & 0xff00) == ((imm & 0xff) << 8))
444 return (3 << 12) | (imm & 0xff);
445 }
446
447 /* Assembly optimization: count leading zeroes? */
448 counter = 8;
449 if (!(imm & 0xffff0000)) {
450 counter += 16;
451 imm <<= 16;
452 }
453 if (!(imm & 0xff000000)) {
454 counter += 8;
455 imm <<= 8;
456 }
457 if (!(imm & 0xf0000000)) {
458 counter += 4;
459 imm <<= 4;
460 }
461 if (!(imm & 0xc0000000)) {
462 counter += 2;
463 imm <<= 2;
464 }
465 if (!(imm & 0x80000000)) {
466 counter += 1;
467 imm <<= 1;
468 }
469 /* Since imm >= 128, this must be true. */
470 SLJIT_ASSERT(counter <= 31);
471
472 if (imm & 0x00ffffff)
473 return INVALID_IMM; /* Cannot be encoded. */
474
475 return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
476 }
477
478 static sljit_si load_immediate(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
479 {
480 sljit_uw tmp;
481
482 if (imm >= 0x10000) {
483 tmp = get_imm(imm);
484 if (tmp != INVALID_IMM)
485 return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
486 tmp = get_imm(~imm);
487 if (tmp != INVALID_IMM)
488 return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
489 }
490
491 /* set low 16 bits, set hi 16 bits to 0. */
492 FAIL_IF(push_inst32(compiler, MOVW | RD4(dst) |
493 COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
494
495 /* set hi 16 bit if needed. */
496 if (imm >= 0x10000)
497 return push_inst32(compiler, MOVT | RD4(dst) |
498 COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
499 return SLJIT_SUCCESS;
500 }
501
502 #define ARG1_IMM 0x0010000
503 #define ARG2_IMM 0x0020000
504 #define KEEP_FLAGS 0x0040000
505 #define SET_MULOV 0x0080000
506 /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
507 #define SET_FLAGS 0x0100000
508 #define UNUSED_RETURN 0x0200000
509 #define SLOW_DEST 0x0400000
510 #define SLOW_SRC1 0x0800000
511 #define SLOW_SRC2 0x1000000
512
513 static sljit_si emit_op_imm(struct sljit_compiler *compiler, sljit_si flags, sljit_si dst, sljit_uw arg1, sljit_uw arg2)
514 {
515 /* dst must be register, TMP_REG1
516 arg1 must be register, TMP_REG1, imm
517 arg2 must be register, TMP_REG2, imm */
518 sljit_si reg;
519 sljit_uw imm, negated_imm;
520
521 if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
522 /* Both are immediates. */
523 flags &= ~ARG1_IMM;
524 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
525 arg1 = TMP_REG1;
526 }
527
528 if (flags & (ARG1_IMM | ARG2_IMM)) {
529 reg = (flags & ARG2_IMM) ? arg1 : arg2;
530 imm = (flags & ARG2_IMM) ? arg2 : arg1;
531
532 switch (flags & 0xffff) {
533 case SLJIT_MOV:
534 SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
535 return load_immediate(compiler, dst, imm);
536 case SLJIT_NOT:
537 if (!(flags & SET_FLAGS))
538 return load_immediate(compiler, dst, ~imm);
539 /* Since the flags should be set, we just fallback to the register mode.
540 Although I could do some clever things here, "NOT IMM" does not worth the efforts. */
541 break;
542 case SLJIT_CLZ:
543 /* No form with immediate operand. */
544 break;
545 case SLJIT_ADD:
546 negated_imm = (sljit_uw)-(sljit_sw)imm;
547 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(reg, dst)) {
548 if (imm <= 0x7)
549 return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
550 if (negated_imm <= 0x7)
551 return push_inst16(compiler, SUBSI3 | IMM3(negated_imm) | RD3(dst) | RN3(reg));
552 if (reg == dst) {
553 if (imm <= 0xff)
554 return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
555 if (negated_imm <= 0xff)
556 return push_inst16(compiler, SUBSI8 | IMM8(negated_imm) | RDN3(dst));
557 }
558 }
559 if (!(flags & SET_FLAGS)) {
560 if (imm <= 0xfff)
561 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
562 if (negated_imm <= 0xfff)
563 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(negated_imm));
564 }
565 imm = get_imm(imm);
566 if (imm != INVALID_IMM)
567 return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
568 break;
569 case SLJIT_ADDC:
570 imm = get_imm(imm);
571 if (imm != INVALID_IMM)
572 return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
573 break;
574 case SLJIT_SUB:
575 if (flags & ARG2_IMM) {
576 negated_imm = (sljit_uw)-(sljit_sw)imm;
577 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(reg, dst)) {
578 if (imm <= 0x7)
579 return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
580 if (negated_imm <= 0x7)
581 return push_inst16(compiler, ADDSI3 | IMM3(negated_imm) | RD3(dst) | RN3(reg));
582 if (reg == dst) {
583 if (imm <= 0xff)
584 return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
585 if (negated_imm <= 0xff)
586 return push_inst16(compiler, ADDSI8 | IMM8(negated_imm) | RDN3(dst));
587 }
588 if (imm <= 0xff && (flags & UNUSED_RETURN))
589 return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
590 }
591 if (!(flags & SET_FLAGS)) {
592 if (imm <= 0xfff)
593 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
594 if (negated_imm <= 0xfff)
595 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(negated_imm));
596 }
597 imm = get_imm(imm);
598 if (imm != INVALID_IMM)
599 return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
600 }
601 else {
602 if (!(flags & KEEP_FLAGS) && imm == 0 && IS_2_LO_REGS(reg, dst))
603 return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
604 imm = get_imm(imm);
605 if (imm != INVALID_IMM)
606 return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
607 }
608 break;
609 case SLJIT_SUBC:
610 if (flags & ARG2_IMM) {
611 imm = get_imm(imm);
612 if (imm != INVALID_IMM)
613 return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
614 }
615 break;
616 case SLJIT_MUL:
617 /* No form with immediate operand. */
618 break;
619 case SLJIT_AND:
620 imm = get_imm(imm);
621 if (imm != INVALID_IMM)
622 return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
623 imm = get_imm(~((flags & ARG2_IMM) ? arg2 : arg1));
624 if (imm != INVALID_IMM)
625 return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
626 break;
627 case SLJIT_OR:
628 imm = get_imm(imm);
629 if (imm != INVALID_IMM)
630 return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
631 imm = get_imm(~((flags & ARG2_IMM) ? arg2 : arg1));
632 if (imm != INVALID_IMM)
633 return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
634 break;
635 case SLJIT_XOR:
636 imm = get_imm(imm);
637 if (imm != INVALID_IMM)
638 return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
639 break;
640 case SLJIT_SHL:
641 if (flags & ARG2_IMM) {
642 imm &= 0x1f;
643 if (imm == 0) {
644 if (!(flags & SET_FLAGS))
645 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
646 if (IS_2_LO_REGS(dst, reg))
647 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
648 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
649 }
650 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
651 return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
652 return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
653 }
654 break;
655 case SLJIT_LSHR:
656 if (flags & ARG2_IMM) {
657 imm &= 0x1f;
658 if (imm == 0) {
659 if (!(flags & SET_FLAGS))
660 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
661 if (IS_2_LO_REGS(dst, reg))
662 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
663 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
664 }
665 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
666 return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
667 return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
668 }
669 break;
670 case SLJIT_ASHR:
671 if (flags & ARG2_IMM) {
672 imm &= 0x1f;
673 if (imm == 0) {
674 if (!(flags & SET_FLAGS))
675 return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
676 if (IS_2_LO_REGS(dst, reg))
677 return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
678 return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
679 }
680 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
681 return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
682 return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
683 }
684 break;
685 default:
686 SLJIT_ASSERT_STOP();
687 break;
688 }
689
690 if (flags & ARG2_IMM) {
691 FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
692 arg2 = TMP_REG2;
693 }
694 else {
695 FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
696 arg1 = TMP_REG1;
697 }
698 }
699
700 /* Both arguments are registers. */
701 switch (flags & 0xffff) {
702 case SLJIT_MOV:
703 case SLJIT_MOV_UI:
704 case SLJIT_MOV_SI:
705 case SLJIT_MOV_P:
706 case SLJIT_MOVU:
707 case SLJIT_MOVU_UI:
708 case SLJIT_MOVU_SI:
709 case SLJIT_MOVU_P:
710 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
711 return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
712 case SLJIT_MOV_UB:
713 case SLJIT_MOVU_UB:
714 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
715 if (IS_2_LO_REGS(dst, arg2))
716 return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
717 return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
718 case SLJIT_MOV_SB:
719 case SLJIT_MOVU_SB:
720 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
721 if (IS_2_LO_REGS(dst, arg2))
722 return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
723 return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
724 case SLJIT_MOV_UH:
725 case SLJIT_MOVU_UH:
726 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
727 if (IS_2_LO_REGS(dst, arg2))
728 return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
729 return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
730 case SLJIT_MOV_SH:
731 case SLJIT_MOVU_SH:
732 SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
733 if (IS_2_LO_REGS(dst, arg2))
734 return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
735 return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
736 case SLJIT_NOT:
737 SLJIT_ASSERT(arg1 == TMP_REG1);
738 if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
739 return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
740 return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
741 case SLJIT_CLZ:
742 SLJIT_ASSERT(arg1 == TMP_REG1);
743 FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
744 if (flags & SET_FLAGS) {
745 if (reg_map[dst] <= 7)
746 return push_inst16(compiler, CMPI | RDN3(dst));
747 return push_inst32(compiler, ADD_WI | SET_FLAGS | RN4(dst) | RD4(dst));
748 }
749 return SLJIT_SUCCESS;
750 case SLJIT_ADD:
751 if (!(flags & KEEP_FLAGS) && IS_3_LO_REGS(dst, arg1, arg2))
752 return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
753 if (dst == arg1 && !(flags & SET_FLAGS))
754 return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
755 return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
756 case SLJIT_ADDC:
757 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
758 return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
759 return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
760 case SLJIT_SUB:
761 if (!(flags & KEEP_FLAGS) && IS_3_LO_REGS(dst, arg1, arg2))
762 return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
763 return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
764 case SLJIT_SUBC:
765 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
766 return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
767 return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
768 case SLJIT_MUL:
769 if (!(flags & SET_FLAGS))
770 return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
771 SLJIT_ASSERT(reg_map[TMP_REG2] <= 7 && dst != TMP_REG2);
772 FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
773 /* cmp TMP_REG2, dst asr #31. */
774 return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
775 case SLJIT_AND:
776 if (!(flags & KEEP_FLAGS)) {
777 if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
778 return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
779 if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
780 return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
781 }
782 return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
783 case SLJIT_OR:
784 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
785 return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
786 return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
787 case SLJIT_XOR:
788 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
789 return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
790 return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
791 case SLJIT_SHL:
792 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
793 return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
794 return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
795 case SLJIT_LSHR:
796 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
797 return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
798 return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
799 case SLJIT_ASHR:
800 if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
801 return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
802 return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
803 }
804
805 SLJIT_ASSERT_STOP();
806 return SLJIT_SUCCESS;
807 }
808
809 #define STORE 0x01
810 #define SIGNED 0x02
811
812 #define WORD_SIZE 0x00
813 #define BYTE_SIZE 0x04
814 #define HALF_SIZE 0x08
815
816 #define UPDATE 0x10
817 #define ARG_TEST 0x20
818
819 #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
820 #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
821
822 /*
823 1st letter:
824 w = word
825 b = byte
826 h = half
827
828 2nd letter:
829 s = signed
830 u = unsigned
831
832 3rd letter:
833 l = load
834 s = store
835 */
836
837 static SLJIT_CONST sljit_uw sljit_mem16[12] = {
838 /* w u l */ 0x5800 /* ldr */,
839 /* w u s */ 0x5000 /* str */,
840 /* w s l */ 0x5800 /* ldr */,
841 /* w s s */ 0x5000 /* str */,
842
843 /* b u l */ 0x5c00 /* ldrb */,
844 /* b u s */ 0x5400 /* strb */,
845 /* b s l */ 0x5600 /* ldrsb */,
846 /* b s s */ 0x5400 /* strb */,
847
848 /* h u l */ 0x5a00 /* ldrh */,
849 /* h u s */ 0x5200 /* strh */,
850 /* h s l */ 0x5e00 /* ldrsh */,
851 /* h s s */ 0x5200 /* strh */,
852 };
853
854 static SLJIT_CONST sljit_uw sljit_mem16_imm5[12] = {
855 /* w u l */ 0x6800 /* ldr imm5 */,
856 /* w u s */ 0x6000 /* str imm5 */,
857 /* w s l */ 0x6800 /* ldr imm5 */,
858 /* w s s */ 0x6000 /* str imm5 */,
859
860 /* b u l */ 0x7800 /* ldrb imm5 */,
861 /* b u s */ 0x7000 /* strb imm5 */,
862 /* b s l */ 0x0000 /* not allowed */,
863 /* b s s */ 0x7000 /* strb imm5 */,
864
865 /* h u l */ 0x8800 /* ldrh imm5 */,
866 /* h u s */ 0x8000 /* strh imm5 */,
867 /* h s l */ 0x0000 /* not allowed */,
868 /* h s s */ 0x8000 /* strh imm5 */,
869 };
870
871 #define MEM_IMM8 0xc00
872 #define MEM_IMM12 0x800000
873 static SLJIT_CONST sljit_uw sljit_mem32[12] = {
874 /* w u l */ 0xf8500000 /* ldr.w */,
875 /* w u s */ 0xf8400000 /* str.w */,
876 /* w s l */ 0xf8500000 /* ldr.w */,
877 /* w s s */ 0xf8400000 /* str.w */,
878
879 /* b u l */ 0xf8100000 /* ldrb.w */,
880 /* b u s */ 0xf8000000 /* strb.w */,
881 /* b s l */ 0xf9100000 /* ldrsb.w */,
882 /* b s s */ 0xf8000000 /* strb.w */,
883
884 /* h u l */ 0xf8300000 /* ldrh.w */,
885 /* h u s */ 0xf8200000 /* strsh.w */,
886 /* h s l */ 0xf9300000 /* ldrsh.w */,
887 /* h s s */ 0xf8200000 /* strsh.w */,
888 };
889
890 /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
891 static sljit_si emit_set_delta(struct sljit_compiler *compiler, sljit_si dst, sljit_si reg, sljit_sw value)
892 {
893 if (value >= 0) {
894 if (value <= 0xfff)
895 return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
896 value = get_imm(value);
897 if (value != INVALID_IMM)
898 return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
899 }
900 else {
901 value = -value;
902 if (value <= 0xfff)
903 return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
904 value = get_imm(value);
905 if (value != INVALID_IMM)
906 return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
907 }
908 return SLJIT_ERR_UNSUPPORTED;
909 }
910
911 /* Can perform an operation using at most 1 instruction. */
912 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
913 {
914 sljit_si tmp;
915
916 SLJIT_ASSERT(arg & SLJIT_MEM);
917
918 if (SLJIT_UNLIKELY(flags & UPDATE)) {
919 if ((arg & 0xf) && !(arg & 0xf0) && argw <= 0xff && argw >= -0xff) {
920 flags &= ~UPDATE;
921 arg &= 0xf;
922 if (SLJIT_UNLIKELY(flags & ARG_TEST))
923 return 1;
924
925 if (argw >= 0)
926 argw |= 0x200;
927 else {
928 argw = -argw;
929 }
930 SLJIT_ASSERT(argw >= 0 && (argw & 0xff) <= 0xff);
931 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | 0x100 | argw));
932 return -1;
933 }
934 return (flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
935 }
936
937 if (SLJIT_UNLIKELY(arg & 0xf0)) {
938 argw &= 0x3;
939 tmp = (arg >> 4) & 0xf;
940 arg &= 0xf;
941 if (SLJIT_UNLIKELY(flags & ARG_TEST))
942 return 1;
943
944 if (!argw && IS_3_LO_REGS(reg, arg, tmp))
945 FAIL_IF(push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp)));
946 else
947 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp) | (argw << 4)));
948 return -1;
949 }
950
951 if (!(arg & 0xf) || argw > 0xfff || argw < -0xff)
952 return (flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
953
954 if (SLJIT_UNLIKELY(flags & ARG_TEST))
955 return 1;
956
957 arg &= 0xf;
958 if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
959 tmp = 3;
960 if (IS_WORD_SIZE(flags)) {
961 if (OFFSET_CHECK(0x1f, 2))
962 tmp = 2;
963 }
964 else if (flags & BYTE_SIZE)
965 {
966 if (OFFSET_CHECK(0x1f, 0))
967 tmp = 0;
968 }
969 else {
970 SLJIT_ASSERT(flags & HALF_SIZE);
971 if (OFFSET_CHECK(0x1f, 1))
972 tmp = 1;
973 }
974
975 if (tmp != 3) {
976 FAIL_IF(push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp))));
977 return -1;
978 }
979 }
980
981 /* SP based immediate. */
982 if (SLJIT_UNLIKELY(arg == SLJIT_LOCALS_REG) && OFFSET_CHECK(0xff, 2) && IS_WORD_SIZE(flags) && reg_map[reg] <= 7) {
983 FAIL_IF(push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2)));
984 return -1;
985 }
986
987 if (argw >= 0)
988 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw));
989 else
990 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw));
991 return -1;
992 }
993
994 /* see getput_arg below.
995 Note: can_cache is called only for binary operators. Those
996 operators always uses word arguments without write back. */
997 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
998 {
999 /* Simple operation except for updates. */
1000 if ((arg & 0xf0) || !(next_arg & SLJIT_MEM))
1001 return 0;
1002
1003 if (!(arg & 0xf)) {
1004 if ((sljit_uw)(argw - next_argw) <= 0xfff || (sljit_uw)(next_argw - argw) <= 0xfff)
1005 return 1;
1006 return 0;
1007 }
1008
1009 if (argw == next_argw)
1010 return 1;
1011
1012 if (arg == next_arg && ((sljit_uw)(argw - next_argw) <= 0xfff || (sljit_uw)(next_argw - argw) <= 0xfff))
1013 return 1;
1014
1015 return 0;
1016 }
1017
1018 /* Emit the necessary instructions. See can_cache above. */
1019 static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
1020 {
1021 sljit_si tmp_r;
1022 sljit_sw tmp;
1023
1024 SLJIT_ASSERT(arg & SLJIT_MEM);
1025 if (!(next_arg & SLJIT_MEM)) {
1026 next_arg = 0;
1027 next_argw = 0;
1028 }
1029
1030 tmp_r = (flags & STORE) ? TMP_REG3 : reg;
1031
1032 if (SLJIT_UNLIKELY(flags & UPDATE)) {
1033 flags &= ~UPDATE;
1034 /* Update only applies if a base register exists. */
1035 if (arg & 0xf) {
1036 /* There is no caching here. */
1037 tmp = (arg & 0xf0) >> 4;
1038 arg &= 0xf;
1039
1040 if (!tmp) {
1041 if (!(argw & ~0xfff)) {
1042 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw));
1043 return push_inst32(compiler, ADDWI | RD4(arg) | RN4(arg) | IMM12(argw));
1044 }
1045
1046 if (compiler->cache_arg == SLJIT_MEM) {
1047 if (argw == compiler->cache_argw) {
1048 tmp = TMP_REG3;
1049 argw = 0;
1050 }
1051 else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1052 FAIL_IF(compiler->error);
1053 compiler->cache_argw = argw;
1054 tmp = TMP_REG3;
1055 argw = 0;
1056 }
1057 }
1058
1059 if (argw) {
1060 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1061 compiler->cache_arg = SLJIT_MEM;
1062 compiler->cache_argw = argw;
1063 tmp = TMP_REG3;
1064 argw = 0;
1065 }
1066 }
1067
1068 argw &= 0x3;
1069 if (!argw && IS_3_LO_REGS(reg, arg, tmp)) {
1070 FAIL_IF(push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp)));
1071 return push_inst16(compiler, ADD | SET_REGS44(arg, tmp));
1072 }
1073 FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp) | (argw << 4)));
1074 return push_inst32(compiler, ADD_W | RD4(arg) | RN4(arg) | RM4(tmp) | (argw << 6));
1075 }
1076 }
1077
1078 SLJIT_ASSERT(!(arg & 0xf0));
1079
1080 if (compiler->cache_arg == arg) {
1081 if (!((argw - compiler->cache_argw) & ~0xfff))
1082 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | (argw - compiler->cache_argw));
1083 if (!((compiler->cache_argw - argw) & ~0xff))
1084 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(TMP_REG3) | (compiler->cache_argw - argw));
1085 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
1086 FAIL_IF(compiler->error);
1087 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | 0);
1088 }
1089 }
1090
1091 next_arg = (arg & 0xf) && (arg == next_arg);
1092 arg &= 0xf;
1093 if (arg && compiler->cache_arg == SLJIT_MEM && compiler->cache_argw == argw)
1094 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(TMP_REG3));
1095
1096 compiler->cache_argw = argw;
1097 if (next_arg && emit_set_delta(compiler, TMP_REG3, arg, argw) != SLJIT_ERR_UNSUPPORTED) {
1098 FAIL_IF(compiler->error);
1099 compiler->cache_arg = SLJIT_MEM | arg;
1100 arg = 0;
1101 }
1102 else {
1103 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1104 compiler->cache_arg = SLJIT_MEM;
1105
1106 if (next_arg) {
1107 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG3, arg)));
1108 compiler->cache_arg = SLJIT_MEM | arg;
1109 arg = 0;
1110 }
1111 }
1112
1113 if (arg)
1114 return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(TMP_REG3));
1115 return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | 0);
1116 }
1117
1118 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
1119 {
1120 if (getput_arg_fast(compiler, flags, reg, arg, argw))
1121 return compiler->error;
1122 compiler->cache_arg = 0;
1123 compiler->cache_argw = 0;
1124 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
1125 }
1126
1127 /* --------------------------------------------------------------------- */
1128 /* Entry, exit */
1129 /* --------------------------------------------------------------------- */
1130
1131 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler, sljit_si args, sljit_si temporaries, sljit_si saveds, sljit_si local_size)
1132 {
1133 sljit_si size;
1134 sljit_ins push;
1135
1136 CHECK_ERROR();
1137 check_sljit_emit_enter(compiler, args, temporaries, saveds, local_size);
1138
1139 compiler->temporaries = temporaries;
1140 compiler->saveds = saveds;
1141 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1142 compiler->logical_local_size = local_size;
1143 #endif
1144
1145 push = (1 << 4);
1146 if (saveds >= 5)
1147 push |= 1 << 11;
1148 if (saveds >= 4)
1149 push |= 1 << 10;
1150 if (saveds >= 3)
1151 push |= 1 << 8;
1152 if (saveds >= 2)
1153 push |= 1 << 7;
1154 if (saveds >= 1)
1155 push |= 1 << 6;
1156 if (temporaries >= 5)
1157 push |= 1 << 5;
1158 FAIL_IF(saveds >= 3
1159 ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
1160 : push_inst16(compiler, PUSH | push));
1161
1162 /* Stack must be aligned to 8 bytes: */
1163 size = (3 + saveds) * sizeof(sljit_uw);
1164 local_size += size;
1165 local_size = (local_size + 7) & ~7;
1166 local_size -= size;
1167 compiler->local_size = local_size;
1168 if (local_size > 0) {
1169 if (local_size <= (127 << 2))
1170 FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
1171 else
1172 FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_LOCALS_REG, SLJIT_LOCALS_REG, local_size));
1173 }
1174
1175 if (args >= 1)
1176 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SAVED_REG1, SLJIT_TEMPORARY_REG1)));
1177 if (args >= 2)
1178 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SAVED_REG2, SLJIT_TEMPORARY_REG2)));
1179 if (args >= 3)
1180 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SAVED_REG3, SLJIT_TEMPORARY_REG3)));
1181
1182 return SLJIT_SUCCESS;
1183 }
1184
1185 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_context(struct sljit_compiler *compiler, sljit_si args, sljit_si temporaries, sljit_si saveds, sljit_si local_size)
1186 {
1187 sljit_si size;
1188
1189 CHECK_ERROR_VOID();
1190 check_sljit_set_context(compiler, args, temporaries, saveds, local_size);
1191
1192 compiler->temporaries = temporaries;
1193 compiler->saveds = saveds;
1194 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
1195 compiler->logical_local_size = local_size;
1196 #endif
1197
1198 size = (3 + saveds) * sizeof(sljit_uw);
1199 local_size += size;
1200 local_size = (local_size + 7) & ~7;
1201 local_size -= size;
1202 compiler->local_size = local_size;
1203 }
1204
1205 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
1206 {
1207 sljit_ins pop;
1208
1209 CHECK_ERROR();
1210 check_sljit_emit_return(compiler, op, src, srcw);
1211
1212 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
1213
1214 if (compiler->local_size > 0) {
1215 if (compiler->local_size <= (127 << 2))
1216 FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
1217 else
1218 FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_LOCALS_REG, SLJIT_LOCALS_REG, compiler->local_size));
1219 }
1220
1221 pop = (1 << 4);
1222 if (compiler->saveds >= 5)
1223 pop |= 1 << 11;
1224 if (compiler->saveds >= 4)
1225 pop |= 1 << 10;
1226 if (compiler->saveds >= 3)
1227 pop |= 1 << 8;
1228 if (compiler->saveds >= 2)
1229 pop |= 1 << 7;
1230 if (compiler->saveds >= 1)
1231 pop |= 1 << 6;
1232 if (compiler->temporaries >= 5)
1233 pop |= 1 << 5;
1234 return compiler->saveds >= 3
1235 ? push_inst32(compiler, POP_W | (1 << 15) | pop)
1236 : push_inst16(compiler, POP | pop);
1237 }
1238
1239 /* --------------------------------------------------------------------- */
1240 /* Operators */
1241 /* --------------------------------------------------------------------- */
1242
1243 #ifdef __cplusplus
1244 extern "C" {
1245 #endif
1246
1247 #if defined(__GNUC__)
1248 extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
1249 extern int __aeabi_idivmod(int numerator, int denominator);
1250 #else
1251 #error "Software divmod functions are needed"
1252 #endif
1253
1254 #ifdef __cplusplus
1255 }
1256 #endif
1257
1258 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
1259 {
1260 CHECK_ERROR();
1261 check_sljit_emit_op0(compiler, op);
1262
1263 op = GET_OPCODE(op);
1264 switch (op) {
1265 case SLJIT_BREAKPOINT:
1266 push_inst16(compiler, BKPT);
1267 break;
1268 case SLJIT_NOP:
1269 push_inst16(compiler, NOP);
1270 break;
1271 case SLJIT_UMUL:
1272 case SLJIT_SMUL:
1273 return push_inst32(compiler, (op == SLJIT_UMUL ? UMULL : SMULL)
1274 | (reg_map[SLJIT_TEMPORARY_REG2] << 8)
1275 | (reg_map[SLJIT_TEMPORARY_REG1] << 12)
1276 | (reg_map[SLJIT_TEMPORARY_REG1] << 16)
1277 | reg_map[SLJIT_TEMPORARY_REG2]);
1278 case SLJIT_UDIV:
1279 case SLJIT_SDIV:
1280 if (compiler->temporaries >= 4) {
1281 FAIL_IF(push_inst32(compiler, 0xf84d2d04 /* str r2, [sp, #-4]! */));
1282 FAIL_IF(push_inst32(compiler, 0xf84dcd04 /* str ip, [sp, #-4]! */));
1283 } else if (compiler->temporaries >= 3)
1284 FAIL_IF(push_inst32(compiler, 0xf84d2d08 /* str r2, [sp, #-8]! */));
1285 #if defined(__GNUC__)
1286 FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
1287 (op == SLJIT_UDIV ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
1288 #else
1289 #error "Software divmod functions are needed"
1290 #endif
1291 if (compiler->temporaries >= 4) {
1292 FAIL_IF(push_inst32(compiler, 0xf85dcb04 /* ldr ip, [sp], #4 */));
1293 return push_inst32(compiler, 0xf85d2b04 /* ldr r2, [sp], #4 */);
1294 } else if (compiler->temporaries >= 3)
1295 return push_inst32(compiler, 0xf85d2b08 /* ldr r2, [sp], #8 */);
1296 return SLJIT_SUCCESS;
1297 }
1298
1299 return SLJIT_SUCCESS;
1300 }
1301
1302 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
1303 sljit_si dst, sljit_sw dstw,
1304 sljit_si src, sljit_sw srcw)
1305 {
1306 sljit_si dst_r, flags;
1307 sljit_si op_flags = GET_ALL_FLAGS(op);
1308
1309 CHECK_ERROR();
1310 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
1311 ADJUST_LOCAL_OFFSET(dst, dstw);
1312 ADJUST_LOCAL_OFFSET(src, srcw);
1313
1314 compiler->cache_arg = 0;
1315 compiler->cache_argw = 0;
1316
1317 dst_r = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG1;
1318
1319 op = GET_OPCODE(op);
1320 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) {
1321 switch (op) {
1322 case SLJIT_MOV:
1323 case SLJIT_MOV_UI:
1324 case SLJIT_MOV_SI:
1325 case SLJIT_MOV_P:
1326 flags = WORD_SIZE;
1327 break;
1328 case SLJIT_MOV_UB:
1329 flags = BYTE_SIZE;
1330 if (src & SLJIT_IMM)
1331 srcw = (sljit_ub)srcw;
1332 break;
1333 case SLJIT_MOV_SB:
1334 flags = BYTE_SIZE | SIGNED;
1335 if (src & SLJIT_IMM)
1336 srcw = (sljit_sb)srcw;
1337 break;
1338 case SLJIT_MOV_UH:
1339 flags = HALF_SIZE;
1340 if (src & SLJIT_IMM)
1341 srcw = (sljit_uh)srcw;
1342 break;
1343 case SLJIT_MOV_SH:
1344 flags = HALF_SIZE | SIGNED;
1345 if (src & SLJIT_IMM)
1346 srcw = (sljit_sh)srcw;
1347 break;
1348 case SLJIT_MOVU:
1349 case SLJIT_MOVU_UI:
1350 case SLJIT_MOVU_SI:
1351 case SLJIT_MOVU_P:
1352 flags = WORD_SIZE | UPDATE;
1353 break;
1354 case SLJIT_MOVU_UB:
1355 flags = BYTE_SIZE | UPDATE;
1356 if (src & SLJIT_IMM)
1357 srcw = (sljit_ub)srcw;
1358 break;
1359 case SLJIT_MOVU_SB:
1360 flags = BYTE_SIZE | SIGNED | UPDATE;
1361 if (src & SLJIT_IMM)
1362 srcw = (sljit_sb)srcw;
1363 break;
1364 case SLJIT_MOVU_UH:
1365 flags = HALF_SIZE | UPDATE;
1366 if (src & SLJIT_IMM)
1367 srcw = (sljit_uh)srcw;
1368 break;
1369 case SLJIT_MOVU_SH:
1370 flags = HALF_SIZE | SIGNED | UPDATE;
1371 if (src & SLJIT_IMM)
1372 srcw = (sljit_sh)srcw;
1373 break;
1374 default:
1375 SLJIT_ASSERT_STOP();
1376 flags = 0;
1377 break;
1378 }
1379
1380 if (src & SLJIT_IMM)
1381 FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
1382 else if (src & SLJIT_MEM) {
1383 if (getput_arg_fast(compiler, flags, dst_r, src, srcw))
1384 FAIL_IF(compiler->error);
1385 else
1386 FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw));
1387 } else {
1388 if (dst_r != TMP_REG1)
1389 return emit_op_imm(compiler, op, dst_r, TMP_REG1, src);
1390 dst_r = src;
1391 }
1392
1393 if (dst & SLJIT_MEM) {
1394 if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
1395 return compiler->error;
1396 else
1397 return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
1398 }
1399 return SLJIT_SUCCESS;
1400 }
1401
1402 if (op == SLJIT_NEG) {
1403 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1404 compiler->skip_checks = 1;
1405 #endif
1406 return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
1407 }
1408
1409 flags = (GET_FLAGS(op_flags) ? SET_FLAGS : 0) | ((op_flags & SLJIT_KEEP_FLAGS) ? KEEP_FLAGS : 0);
1410 if (src & SLJIT_MEM) {
1411 if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG2, src, srcw))
1412 FAIL_IF(compiler->error);
1413 else
1414 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src, srcw, dst, dstw));
1415 src = TMP_REG2;
1416 }
1417
1418 if (src & SLJIT_IMM)
1419 flags |= ARG2_IMM;
1420 else
1421 srcw = src;
1422
1423 emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw);
1424
1425 if (dst & SLJIT_MEM) {
1426 if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
1427 return compiler->error;
1428 else
1429 return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
1430 }
1431 return SLJIT_SUCCESS;
1432 }
1433
1434 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
1435 sljit_si dst, sljit_sw dstw,
1436 sljit_si src1, sljit_sw src1w,
1437 sljit_si src2, sljit_sw src2w)
1438 {
1439 sljit_si dst_r, flags;
1440
1441 CHECK_ERROR();
1442 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1443 ADJUST_LOCAL_OFFSET(dst, dstw);
1444 ADJUST_LOCAL_OFFSET(src1, src1w);
1445 ADJUST_LOCAL_OFFSET(src2, src2w);
1446
1447 compiler->cache_arg = 0;
1448 compiler->cache_argw = 0;
1449
1450 dst_r = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG1;
1451 flags = (GET_FLAGS(op) ? SET_FLAGS : 0) | ((op & SLJIT_KEEP_FLAGS) ? KEEP_FLAGS : 0);
1452
1453 if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, WORD_SIZE | STORE | ARG_TEST, TMP_REG1, dst, dstw))
1454 flags |= SLOW_DEST;
1455
1456 if (src1 & SLJIT_MEM) {
1457 if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG1, src1, src1w))
1458 FAIL_IF(compiler->error);
1459 else
1460 flags |= SLOW_SRC1;
1461 }
1462 if (src2 & SLJIT_MEM) {
1463 if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG2, src2, src2w))
1464 FAIL_IF(compiler->error);
1465 else
1466 flags |= SLOW_SRC2;
1467 }
1468
1469 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1470 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1471 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, src1, src1w));
1472 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, dst, dstw));
1473 }
1474 else {
1475 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, src2, src2w));
1476 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, dst, dstw));
1477 }
1478 }
1479 else if (flags & SLOW_SRC1)
1480 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, dst, dstw));
1481 else if (flags & SLOW_SRC2)
1482 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, dst, dstw));
1483
1484 if (src1 & SLJIT_MEM)
1485 src1 = TMP_REG1;
1486 if (src2 & SLJIT_MEM)
1487 src2 = TMP_REG2;
1488
1489 if (src1 & SLJIT_IMM)
1490 flags |= ARG1_IMM;
1491 else
1492 src1w = src1;
1493 if (src2 & SLJIT_IMM)
1494 flags |= ARG2_IMM;
1495 else
1496 src2w = src2;
1497
1498 if (dst == SLJIT_UNUSED)
1499 flags |= UNUSED_RETURN;
1500
1501 if (GET_OPCODE(op) == SLJIT_MUL && (op & SLJIT_SET_O))
1502 flags |= SET_MULOV;
1503
1504 emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
1505
1506 if (dst & SLJIT_MEM) {
1507 if (!(flags & SLOW_DEST)) {
1508 getput_arg_fast(compiler, WORD_SIZE | STORE, dst_r, dst, dstw);
1509 return compiler->error;
1510 }
1511 return getput_arg(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, 0, 0);
1512 }
1513 return SLJIT_SUCCESS;
1514 }
1515
1516 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
1517 {
1518 check_sljit_get_register_index(reg);
1519 return reg_map[reg];
1520 }
1521
1522 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
1523 void *instruction, sljit_si size)
1524 {
1525 CHECK_ERROR();
1526 check_sljit_emit_op_custom(compiler, instruction, size);
1527 SLJIT_ASSERT(size == 2 || size == 4);
1528
1529 if (size == 2)
1530 return push_inst16(compiler, *(sljit_uh*)instruction);
1531 return push_inst32(compiler, *(sljit_ins*)instruction);
1532 }
1533
1534 /* --------------------------------------------------------------------- */
1535 /* Floating point operators */
1536 /* --------------------------------------------------------------------- */
1537
1538 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
1539 {
1540 return 1;
1541 }
1542
1543 #define FPU_LOAD (1 << 20)
1544
1545 static sljit_si emit_fop_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
1546 {
1547 sljit_sw tmp;
1548 sljit_uw imm;
1549 sljit_sw inst = VSTR_F32 | (flags & (SLJIT_SINGLE_OP | FPU_LOAD));
1550
1551 SLJIT_ASSERT(arg & SLJIT_MEM);
1552
1553 /* Fast loads and stores. */
1554 if (SLJIT_UNLIKELY(arg & 0xf0)) {
1555 FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG2) | RN4(arg & 0xf) | RM4((arg & 0xf0) >> 4) | ((argw & 0x3) << 6)));
1556 arg = SLJIT_MEM | TMP_REG2;
1557 argw = 0;
1558 }
1559
1560 if ((arg & 0xf) && (argw & 0x3) == 0) {
1561 if (!(argw & ~0x3fc))
1562 return push_inst32(compiler, inst | 0x800000 | RN4(arg & 0xf) | DD4(reg) | (argw >> 2));
1563 if (!(-argw & ~0x3fc))
1564 return push_inst32(compiler, inst | RN4(arg & 0xf) | DD4(reg) | (-argw >> 2));
1565 }
1566
1567 SLJIT_ASSERT(!(arg & 0xf0));
1568 if (compiler->cache_arg == arg) {
1569 tmp = argw - compiler->cache_argw;
1570 if (!(tmp & ~0x3fc))
1571 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg) | (tmp >> 2));
1572 if (!(-tmp & ~0x3fc))
1573 return push_inst32(compiler, inst | RN4(TMP_REG3) | DD4(reg) | (-tmp >> 2));
1574 if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, tmp) != SLJIT_ERR_UNSUPPORTED) {
1575 FAIL_IF(compiler->error);
1576 compiler->cache_argw = argw;
1577 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg));
1578 }
1579 }
1580
1581 if (arg & 0xf) {
1582 if (emit_set_delta(compiler, TMP_REG1, arg & 0xf, argw) != SLJIT_ERR_UNSUPPORTED) {
1583 FAIL_IF(compiler->error);
1584 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
1585 }
1586 imm = get_imm(argw & ~0x3fc);
1587 if (imm != INVALID_IMM) {
1588 FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & 0xf) | imm));
1589 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1590 }
1591 imm = get_imm(-argw & ~0x3fc);
1592 if (imm != INVALID_IMM) {
1593 argw = -argw;
1594 FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & 0xf) | imm));
1595 return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
1596 }
1597 }
1598
1599 compiler->cache_arg = arg;
1600 compiler->cache_argw = argw;
1601
1602 if (SLJIT_UNLIKELY(!(arg & 0xf)))
1603 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1604 else {
1605 FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
1606 if (arg & 0xf)
1607 FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG3, (arg & 0xf))));
1608 }
1609 return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg));
1610 }
1611
1612 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
1613 sljit_si dst, sljit_sw dstw,
1614 sljit_si src, sljit_sw srcw)
1615 {
1616 sljit_si dst_r;
1617
1618 CHECK_ERROR();
1619 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1620 SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100), float_transfer_bit_error);
1621
1622 compiler->cache_arg = 0;
1623 compiler->cache_argw = 0;
1624 op ^= SLJIT_SINGLE_OP;
1625
1626 if (GET_OPCODE(op) == SLJIT_CMPD) {
1627 if (dst & SLJIT_MEM) {
1628 emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG1, dst, dstw);
1629 dst = TMP_FREG1;
1630 }
1631 if (src & SLJIT_MEM) {
1632 emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG2, src, srcw);
1633 src = TMP_FREG2;
1634 }
1635 FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst) | DM4(src)));
1636 return push_inst32(compiler, VMRS);
1637 }
1638
1639 dst_r = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG1 : dst;
1640 if (src & SLJIT_MEM) {
1641 emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, dst_r, src, srcw);
1642 src = dst_r;
1643 }
1644
1645 switch (GET_OPCODE(op)) {
1646 case SLJIT_MOVD:
1647 if (src != dst_r)
1648 FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
1649 break;
1650 case SLJIT_NEGD:
1651 FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
1652 break;
1653 case SLJIT_ABSD:
1654 FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
1655 break;
1656 }
1657
1658 if (dst & SLJIT_MEM)
1659 return emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP), TMP_FREG1, dst, dstw);
1660 return SLJIT_SUCCESS;
1661 }
1662
1663 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
1664 sljit_si dst, sljit_sw dstw,
1665 sljit_si src1, sljit_sw src1w,
1666 sljit_si src2, sljit_sw src2w)
1667 {
1668 sljit_si dst_r;
1669
1670 CHECK_ERROR();
1671 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1672
1673 compiler->cache_arg = 0;
1674 compiler->cache_argw = 0;
1675 op ^= SLJIT_SINGLE_OP;
1676
1677 dst_r = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG1 : dst;
1678 if (src1 & SLJIT_MEM) {
1679 emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
1680 src1 = TMP_FREG1;
1681 }
1682 if (src2 & SLJIT_MEM) {
1683 emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
1684 src2 = TMP_FREG2;
1685 }
1686
1687 switch (GET_OPCODE(op)) {
1688 case SLJIT_ADDD:
1689 FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1690 break;
1691 case SLJIT_SUBD:
1692 FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1693 break;
1694 case SLJIT_MULD:
1695 FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1696 break;
1697 case SLJIT_DIVD:
1698 FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
1699 break;
1700 }
1701
1702 if (dst & SLJIT_MEM)
1703 return emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP), TMP_FREG1, dst, dstw);
1704 return SLJIT_SUCCESS;
1705 }
1706
1707 #undef FPU_LOAD
1708
1709 /* --------------------------------------------------------------------- */
1710 /* Other instructions */
1711 /* --------------------------------------------------------------------- */
1712
1713 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1714 {
1715 CHECK_ERROR();
1716 check_sljit_emit_fast_enter(compiler, dst, dstw);
1717 ADJUST_LOCAL_OFFSET(dst, dstw);
1718
1719 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1720 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG3));
1721 else if (dst & SLJIT_MEM) {
1722 if (getput_arg_fast(compiler, WORD_SIZE | STORE, TMP_REG3, dst, dstw))
1723 return compiler->error;
1724 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, TMP_REG3)));
1725 compiler->cache_arg = 0;
1726 compiler->cache_argw = 0;
1727 return getput_arg(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, 0, 0);
1728 }
1729
1730 return SLJIT_SUCCESS;
1731 }
1732
1733 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1734 {
1735 CHECK_ERROR();
1736 check_sljit_emit_fast_return(compiler, src, srcw);
1737 ADJUST_LOCAL_OFFSET(src, srcw);
1738
1739 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1740 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG3, src)));
1741 else if (src & SLJIT_MEM) {
1742 if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG3, src, srcw))
1743 FAIL_IF(compiler->error);
1744 else {
1745 compiler->cache_arg = 0;
1746 compiler->cache_argw = 0;
1747 FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src, srcw, 0, 0));
1748 FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG3, TMP_REG2)));
1749 }
1750 }
1751 else if (src & SLJIT_IMM)
1752 FAIL_IF(load_immediate(compiler, TMP_REG3, srcw));
1753 return push_inst16(compiler, BLX | RN3(TMP_REG3));
1754 }
1755
1756 /* --------------------------------------------------------------------- */
1757 /* Conditional instructions */
1758 /* --------------------------------------------------------------------- */
1759
1760 static sljit_uw get_cc(sljit_si type)
1761 {
1762 switch (type) {
1763 case SLJIT_C_EQUAL:
1764 case SLJIT_C_MUL_NOT_OVERFLOW:
1765 case SLJIT_C_FLOAT_EQUAL:
1766 return 0x0;
1767
1768 case SLJIT_C_NOT_EQUAL:
1769 case SLJIT_C_MUL_OVERFLOW:
1770 case SLJIT_C_FLOAT_NOT_EQUAL:
1771 return 0x1;
1772
1773 case SLJIT_C_LESS:
1774 case SLJIT_C_FLOAT_LESS:
1775 return 0x3;
1776
1777 case SLJIT_C_GREATER_EQUAL:
1778 case SLJIT_C_FLOAT_GREATER_EQUAL:
1779 return 0x2;
1780
1781 case SLJIT_C_GREATER:
1782 case SLJIT_C_FLOAT_GREATER:
1783 return 0x8;
1784
1785 case SLJIT_C_LESS_EQUAL:
1786 case SLJIT_C_FLOAT_LESS_EQUAL:
1787 return 0x9;
1788
1789 case SLJIT_C_SIG_LESS:
1790 return 0xb;
1791
1792 case SLJIT_C_SIG_GREATER_EQUAL:
1793 return 0xa;
1794
1795 case SLJIT_C_SIG_GREATER:
1796 return 0xc;
1797
1798 case SLJIT_C_SIG_LESS_EQUAL:
1799 return 0xd;
1800
1801 case SLJIT_C_OVERFLOW:
1802 case SLJIT_C_FLOAT_UNORDERED:
1803 return 0x6;
1804
1805 case SLJIT_C_NOT_OVERFLOW:
1806 case SLJIT_C_FLOAT_ORDERED:
1807 return 0x7;
1808
1809 default: /* SLJIT_JUMP */
1810 return 0xe;
1811 }
1812 }
1813
1814 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1815 {
1816 struct sljit_label *label;
1817
1818 CHECK_ERROR_PTR();
1819 check_sljit_emit_label(compiler);
1820
1821 if (compiler->last_label && compiler->last_label->size == compiler->size)
1822 return compiler->last_label;
1823
1824 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1825 PTR_FAIL_IF(!label);
1826 set_label(label, compiler);
1827 return label;
1828 }
1829
1830 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1831 {
1832 struct sljit_jump *jump;
1833 sljit_si cc;
1834
1835 CHECK_ERROR_PTR();
1836 check_sljit_emit_jump(compiler, type);
1837
1838 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1839 PTR_FAIL_IF(!jump);
1840 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1841 type &= 0xff;
1842
1843 /* In ARM, we don't need to touch the arguments. */
1844 PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
1845 if (type < SLJIT_JUMP) {
1846 jump->flags |= IS_COND;
1847 cc = get_cc(type);
1848 jump->flags |= cc << 8;
1849 PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
1850 }
1851
1852 jump->addr = compiler->size;
1853 if (type <= SLJIT_JUMP)
1854 PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
1855 else {
1856 jump->flags |= IS_BL;
1857 PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
1858 }
1859
1860 return jump;
1861 }
1862
1863 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1864 {
1865 struct sljit_jump *jump;
1866
1867 CHECK_ERROR();
1868 check_sljit_emit_ijump(compiler, type, src, srcw);
1869 ADJUST_LOCAL_OFFSET(src, srcw);
1870
1871 /* In ARM, we don't need to touch the arguments. */
1872 if (src & SLJIT_IMM) {
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 = srcw;
1877
1878 FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
1879 jump->addr = compiler->size;
1880 FAIL_IF(push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1)));
1881 }
1882 else {
1883 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1884 return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
1885
1886 FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw));
1887 if (type >= SLJIT_FAST_CALL)
1888 return push_inst16(compiler, BLX | RN3(TMP_REG1));
1889 }
1890 return SLJIT_SUCCESS;
1891 }
1892
1893 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_cond_value(struct sljit_compiler *compiler, sljit_si op, sljit_si dst, sljit_sw dstw, sljit_si type)
1894 {
1895 sljit_si dst_r;
1896 sljit_uw cc;
1897
1898 CHECK_ERROR();
1899 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1900 ADJUST_LOCAL_OFFSET(dst, dstw);
1901
1902 if (dst == SLJIT_UNUSED)
1903 return SLJIT_SUCCESS;
1904
1905 cc = get_cc(type);
1906 if (GET_OPCODE(op) == SLJIT_OR && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) {
1907 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
1908 FAIL_IF(push_inst32(compiler, ORRI | RN4(dst) | RD4(dst) | 0x1));
1909 if (op & SLJIT_SET_E) {
1910 if (reg_map[dst] <= 7)
1911 return push_inst16(compiler, ORRS | RD3(dst) | RN3(dst));
1912 return push_inst32(compiler, ORR_W | SET_FLAGS | RD4(TMP_REG1) | RN4(dst) | RM4(dst));
1913 }
1914 return SLJIT_SUCCESS;
1915 }
1916
1917 dst_r = TMP_REG2;
1918 if (op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS && reg_map[dst] <= 7)
1919 dst_r = dst;
1920
1921 FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
1922 FAIL_IF(push_inst16(compiler, MOVSI | 0x1 | RDN3(dst_r)));
1923 FAIL_IF(push_inst16(compiler, MOVSI | 0x0 | RDN3(dst_r)));
1924
1925 if (dst_r == TMP_REG2) {
1926 if (GET_OPCODE(op) == SLJIT_OR) {
1927 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) || (defined SLJIT_DEBUG && SLJIT_DEBUG)
1928 compiler->skip_checks = 1;
1929 #endif
1930 return sljit_emit_op2(compiler, op, dst, dstw, dst, dstw, TMP_REG2, 0);
1931 }
1932 if (dst & SLJIT_MEM)
1933 return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw);
1934 else
1935 return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
1936 }
1937
1938 return SLJIT_SUCCESS;
1939 }
1940
1941 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1942 {
1943 struct sljit_const *const_;
1944 sljit_si dst_r;
1945
1946 CHECK_ERROR_PTR();
1947 check_sljit_emit_const(compiler, dst, dstw, init_value);
1948 ADJUST_LOCAL_OFFSET(dst, dstw);
1949
1950 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1951 PTR_FAIL_IF(!const_);
1952 set_const(const_, compiler);
1953
1954 dst_r = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG1;
1955 PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
1956
1957 if (dst & SLJIT_MEM)
1958 PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw));
1959 return const_;
1960 }
1961
1962 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
1963 {
1964 inline_set_jump_addr(addr, new_addr, 1);
1965 }
1966
1967 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
1968 {
1969 sljit_uh* inst = (sljit_uh*)addr;
1970 modify_imm32_const(inst, new_constant);
1971 SLJIT_CACHE_FLUSH(inst, inst + 3);
1972 }

  ViewVC Help
Powered by ViewVC 1.1.5