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

  ViewVC Help
Powered by ViewVC 1.1.5