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

  ViewVC Help
Powered by ViewVC 1.1.5