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