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

  ViewVC Help
Powered by ViewVC 1.1.5