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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 703 - (show annotations) (download)
Tue Sep 20 16:27:11 2011 UTC (3 years, 3 months ago) by zherczeg
File MIME type: text/plain
File size: 54514 byte(s)
Small, MIPS related fix in 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_CONST char* sljit_get_platform_name()
28 {
29 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
30 return "mips-32";
31 #else
32 #error "mips-64 is not yet supported"
33 #endif
34 }
35
36 /* Length of an instruction word
37 Both for mips-32 and mips-64 */
38 typedef sljit_ui sljit_ins;
39
40 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
41 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
42 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
43 #define REAL_STACK_PTR (SLJIT_NO_REGISTERS + 4)
44
45 /* TMP_EREG1 is used mainly for literal encoding on 64 bit. */
46 #define TMP_EREG1 24
47 #define TMP_EREG2 25
48
49 /* Flags are keept in volatile registers. */
50 #define EQUAL_FLAG 7
51 /* And carry flag as well. */
52 #define ULESS_FLAG 11
53 #define UGREATER_FLAG 12
54 #define LESS_FLAG 13
55 #define GREATER_FLAG 14
56 #define OVERFLOW_FLAG 15
57
58 #define UNORD_BIT 1
59 #define EQUAL_BIT 2
60 #define LESS_BIT 3
61 #define GREATER_BIT 4
62
63 #define TMP_FREG1 (SLJIT_FLOAT_REG4 + 1)
64 #define TMP_FREG2 (SLJIT_FLOAT_REG4 + 2)
65
66 /* --------------------------------------------------------------------- */
67 /* Instrucion forms */
68 /* --------------------------------------------------------------------- */
69
70 #define S(s) (reg_map[s] << 21)
71 #define T(t) (reg_map[t] << 16)
72 #define D(d) (reg_map[d] << 11)
73 /* Absolute registers. */
74 #define SA(s) ((s) << 21)
75 #define TA(t) ((t) << 16)
76 #define DA(d) ((d) << 11)
77 #define FT(t) ((t) << (16 + 1))
78 #define FS(s) ((s) << (11 + 1))
79 #define FD(d) ((d) << (6 + 1))
80 #define IMM(imm) ((imm) & 0xffff)
81 #define SH_IMM(imm) ((imm & 0x1f) << 6)
82
83 #define DR(dr) (reg_map[dr])
84 #define HI(opcode) ((opcode) << 26)
85 #define LO(opcode) (opcode)
86 #define FMT_D (17 << 21)
87
88 #define ABS_D (HI(17) | FMT_D | LO(5))
89 #define ADD_D (HI(17) | FMT_D | LO(0))
90 #define ADDU (HI(0) | LO(33))
91 #define ADDIU (HI(9))
92 #define AND (HI(0) | LO(36))
93 #define ANDI (HI(12))
94 #define B (HI(4))
95 #define BAL (HI(1) | (17 << 16))
96 #define BC1F (HI(17) | (8 << 21))
97 #define BC1T (HI(17) | (8 << 21) | (1 << 16))
98 #define BEQ (HI(4))
99 #define BGEZ (HI(1) | (1 << 16))
100 #define BGTZ (HI(7))
101 #define BLEZ (HI(6))
102 #define BLTZ (HI(1) | (0 << 16))
103 #define BNE (HI(5))
104 #define BREAK (HI(0) | LO(13))
105 #define C_UN_D (HI(17) | FMT_D | LO(49))
106 #define C_UEQ_D (HI(17) | FMT_D | LO(51))
107 #define C_ULT_D (HI(17) | FMT_D | LO(53))
108 #define CLZ (HI(28) | LO(32))
109 #define DIV_D (HI(17) | FMT_D | LO(3))
110 #define EXT (HI(31) | LO(0))
111 #define J (HI(2))
112 #define JAL (HI(3))
113 #define JALR (HI(0) | LO(9))
114 #define JR (HI(0) | LO(8))
115 #define LD (HI(55))
116 #define LDC1 (HI(53))
117 #define LUI (HI(15))
118 #define LW (HI(35))
119 #define NEG_D (HI(17) | FMT_D | LO(7))
120 #define MFHI (HI(0) | LO(16))
121 #define MFLO (HI(0) | LO(18))
122 #define MOV_D (HI(17) | FMT_D | LO(6))
123 #define CFC1 (HI(17) | (2 << 21))
124 #define MOVN (HI(0) | LO(11))
125 #define MOVZ (HI(0) | LO(10))
126 #define MUL (HI(28) | LO(2))
127 #define MUL_D (HI(17) | FMT_D | LO(2))
128 #define MULT (HI(0) | LO(24))
129 #define NOP (HI(0) | LO(0))
130 #define NOR (HI(0) | LO(39))
131 #define OR (HI(0) | LO(37))
132 #define ORI (HI(13))
133 #define SD (HI(63))
134 #define SDC1 (HI(61))
135 #define SEB (HI(31) | (16 << 6) | LO(32))
136 #define SEH (HI(31) | (24 << 6) | LO(32))
137 #define SLT (HI(0) | LO(42))
138 #define SLTI (HI(10))
139 #define SLTIU (HI(11))
140 #define SLTU (HI(0) | LO(43))
141 #define SLL (HI(0) | LO(0))
142 #define SLLV (HI(0) | LO(4))
143 #define SRL (HI(0) | LO(2))
144 #define SRLV (HI(0) | LO(6))
145 #define SRA (HI(0) | LO(3))
146 #define SRAV (HI(0) | LO(7))
147 #define SUB_D (HI(17) | FMT_D | LO(1))
148 #define SUBU (HI(0) | LO(35))
149 #define SW (HI(43))
150 #define XOR (HI(0) | LO(38))
151 #define XORI (HI(14))
152
153 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
154 #define ADDU_W ADDU
155 #define ADDIU_W ADDIU
156 #define EXT_W EXT
157 #define SLL_W SLL
158 #define SUBU_W SUBU
159 #else
160 #define ADDU_W DADDU
161 #define ADDIU_W DADDIU
162 #define EXT_W DEXT
163 #define SLL_W DSLL
164 #define SUBU_W DSUBU
165 #endif
166
167 #define SIMM_MAX (0x7fff)
168 #define SIMM_MIN (-0x8000)
169 #define UIMM_MAX (0xffff)
170
171 static SLJIT_CONST sljit_ub reg_map[SLJIT_NO_REGISTERS + 6] = {
172 0, 2, 5, 6, 3, 4, 17, 18, 19, 20, 21, 16, 8, 9, 10, 29
173 };
174
175 /* dest_reg is the absolute name of the register
176 Useful for reordering instructions in the delay slot. */
177 static int push_inst(struct sljit_compiler *compiler, sljit_ins ins, int delay_slot)
178 {
179 sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
180 FAIL_IF(!ptr);
181 *ptr = ins;
182 compiler->size++;
183 compiler->delay_slot = delay_slot;
184 return SLJIT_SUCCESS;
185 }
186
187 static SLJIT_INLINE sljit_ins invert_branch(int flags)
188 {
189 return (flags & IS_BIT26_COND) ? (1 << 26) : (1 << 16);
190 }
191
192 static SLJIT_INLINE sljit_ins* optimize_jump(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
193 {
194 sljit_w diff;
195 sljit_uw target_addr;
196 sljit_ins *inst;
197 sljit_ins saved_inst;
198
199 if (jump->flags & SLJIT_REWRITABLE_JUMP)
200 return code_ptr;
201
202 if (jump->flags & JUMP_ADDR)
203 target_addr = jump->u.target;
204 else {
205 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
206 target_addr = (sljit_uw)(code + jump->u.label->size);
207 }
208 inst = (sljit_ins*)jump->addr;
209 if (jump->flags & IS_COND)
210 inst--;
211
212 /* B instructions. */
213 if (jump->flags & IS_MOVABLE) {
214 diff = ((sljit_w)target_addr - (sljit_w)(inst)) >> 2;
215 if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
216 jump->flags |= PATCH_B;
217
218 if (!(jump->flags & IS_COND)) {
219 inst[0] = inst[-1];
220 inst[-1] = (jump->flags & IS_JAL) ? BAL : B;
221 jump->addr -= sizeof(sljit_ins);
222 return inst;
223 }
224 saved_inst = inst[0];
225 inst[0] = inst[-1];
226 inst[-1] = saved_inst ^ invert_branch(jump->flags);
227 jump->addr -= 2 * sizeof(sljit_ins);
228 return inst;
229 }
230 }
231
232 diff = ((sljit_w)target_addr - (sljit_w)(inst + 1)) >> 2;
233 if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
234 jump->flags |= PATCH_B;
235
236 if (!(jump->flags & IS_COND)) {
237 inst[0] = (jump->flags & IS_JAL) ? BAL : B;
238 inst[1] = NOP;
239 return inst + 1;
240 }
241 inst[0] = inst[0] ^ invert_branch(jump->flags);
242 inst[1] = NOP;
243 jump->addr -= sizeof(sljit_ins);
244 return inst + 1;
245 }
246
247 if (jump->flags & IS_COND) {
248 if ((target_addr & ~0xfffffff) == ((jump->addr + 3 * sizeof(sljit_ins)) & ~0xfffffff)) {
249 jump->flags |= PATCH_J;
250 inst[0] = (inst[0] & 0xffff0000) | 3;
251 inst[1] = NOP;
252 inst[2] = J;
253 inst[3] = NOP;
254 jump->addr += sizeof(sljit_ins);
255 return inst + 3;
256 }
257 return code_ptr;
258 }
259
260 /* J instuctions. */
261 if (jump->flags & IS_MOVABLE) {
262 if ((target_addr & ~0xfffffff) == (jump->addr & ~0xfffffff)) {
263 jump->flags |= PATCH_J;
264 inst[0] = inst[-1];
265 inst[-1] = (jump->flags & IS_JAL) ? JAL : J;
266 jump->addr -= sizeof(sljit_ins);
267 return inst;
268 }
269 }
270
271 if ((target_addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff)) {
272 jump->flags |= PATCH_J;
273 inst[0] = (jump->flags & IS_JAL) ? JAL : J;
274 inst[1] = NOP;
275 return inst + 1;
276 }
277
278 return code_ptr;
279 }
280
281 #ifdef __GNUC__
282 static __attribute__ ((noinline)) void sljit_cache_flush(void* code, void* code_ptr)
283 {
284 SLJIT_CACHE_FLUSH(code, code_ptr);
285 }
286 #endif
287
288 void* sljit_generate_code(struct sljit_compiler *compiler)
289 {
290 struct sljit_memory_fragment *buf;
291 sljit_ins *code;
292 sljit_ins *code_ptr;
293 sljit_ins *buf_ptr;
294 sljit_ins *buf_end;
295 sljit_uw word_count;
296 sljit_uw addr;
297
298 struct sljit_label *label;
299 struct sljit_jump *jump;
300 struct sljit_const *const_;
301
302 CHECK_ERROR_PTR();
303 check_sljit_generate_code(compiler);
304 reverse_buf(compiler);
305
306 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
307 PTR_FAIL_WITH_EXEC_IF(code);
308 buf = compiler->buf;
309
310 code_ptr = code;
311 word_count = 0;
312 label = compiler->labels;
313 jump = compiler->jumps;
314 const_ = compiler->consts;
315 do {
316 buf_ptr = (sljit_ins*)buf->memory;
317 buf_end = buf_ptr + (buf->used_size >> 2);
318 do {
319 *code_ptr = *buf_ptr++;
320 SLJIT_ASSERT(!label || label->size >= word_count);
321 SLJIT_ASSERT(!jump || jump->addr >= word_count);
322 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
323 /* These structures are ordered by their address. */
324 if (label && label->size == word_count) {
325 /* Just recording the address. */
326 label->addr = (sljit_uw)code_ptr;
327 label->size = code_ptr - code;
328 label = label->next;
329 }
330 if (jump && jump->addr == word_count) {
331 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
332 jump->addr = (sljit_uw)(code_ptr - 3);
333 #else
334 jump->addr = (sljit_uw)(code_ptr - 6);
335 #endif
336 code_ptr = optimize_jump(jump, code_ptr, code);
337 jump = jump->next;
338 }
339 if (const_ && const_->addr == word_count) {
340 /* Just recording the address. */
341 const_->addr = (sljit_uw)code_ptr;
342 const_ = const_->next;
343 }
344 code_ptr ++;
345 word_count ++;
346 } while (buf_ptr < buf_end);
347
348 buf = buf->next;
349 } while (buf);
350
351 if (label && label->size == word_count) {
352 label->addr = (sljit_uw)code_ptr;
353 label->size = code_ptr - code;
354 label = label->next;
355 }
356
357 SLJIT_ASSERT(!label);
358 SLJIT_ASSERT(!jump);
359 SLJIT_ASSERT(!const_);
360 SLJIT_ASSERT(code_ptr - code <= (int)compiler->size);
361
362 jump = compiler->jumps;
363 while (jump) {
364 do {
365 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
366 buf_ptr = (sljit_ins*)jump->addr;
367
368 if (jump->flags & PATCH_B) {
369 addr = (sljit_w)(addr - (jump->addr + sizeof(sljit_ins))) >> 2;
370 SLJIT_ASSERT((sljit_w)addr <= SIMM_MAX && (sljit_w)addr >= SIMM_MIN);
371 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | (addr & 0xffff);
372 break;
373 }
374 if (jump->flags & PATCH_J) {
375 SLJIT_ASSERT((addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff));
376 buf_ptr[0] |= (addr >> 2) & 0x03ffffff;
377 break;
378 }
379
380 /* Set the fields of immediate loads. */
381 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
382 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
383 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
384 #else
385 buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
386 buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
387 buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
388 buf_ptr[4] = (buf_ptr[4] & 0xffff0000) | (addr & 0xffff);
389 #endif
390 } while (0);
391 jump = jump->next;
392 }
393
394 compiler->error = SLJIT_ERR_COMPILED;
395 #ifndef __GNUC__
396 SLJIT_CACHE_FLUSH(code, code_ptr);
397 #else
398 /* GCC workaround for invalid code generation with -O2. */
399 sljit_cache_flush(code, code_ptr);
400 #endif
401 return code;
402 }
403
404 /* Creates an index in data_transfer_insts array. */
405 #define WORD_DATA 0x00
406 #define BYTE_DATA 0x01
407 #define HALF_DATA 0x02
408 #define INT_DATA 0x03
409 #define SIGNED_DATA 0x04
410 #define LOAD_DATA 0x08
411
412 #define MEM_MASK 0x0f
413
414 #define WRITE_BACK 0x00010
415 #define ARG_TEST 0x00020
416 #define CUMULATIVE_OP 0x00040
417 #define LOGICAL_OP 0x00080
418 #define IMM_OP 0x00100
419 #define SRC2_IMM 0x00200
420
421 #define UNUSED_DEST 0x00400
422 #define REG_DEST 0x00800
423 #define REG1_SOURCE 0x01000
424 #define REG2_SOURCE 0x02000
425 #define SLOW_SRC1 0x04000
426 #define SLOW_SRC2 0x08000
427 #define SLOW_DEST 0x10000
428
429 /* Only these flags are set. UNUSED_DEST is not set when no flags should be set. */
430 #define CHECK_FLAGS(list) \
431 (!(flags & UNUSED_DEST) || (op & GET_FLAGS(~(list))))
432
433 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
434 #include "sljitNativeMIPS_32.c"
435 #else
436 #include "sljitNativeMIPS_64.c"
437 #endif
438
439 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
440 #define STACK_STORE SW
441 #define STACK_LOAD LW
442 #else
443 #define STACK_STORE SD
444 #define STACK_LOAD LD
445 #endif
446
447 static int emit_op(struct sljit_compiler *compiler, int op, int inp_flags,
448 int dst, sljit_w dstw,
449 int src1, sljit_w src1w,
450 int src2, sljit_w src2w);
451
452 int sljit_emit_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
453 {
454 sljit_ins base;
455
456 CHECK_ERROR();
457 check_sljit_emit_enter(compiler, args, temporaries, generals, local_size);
458
459 compiler->temporaries = temporaries;
460 compiler->generals = generals;
461
462 compiler->has_locals = local_size > 0;
463 local_size += (generals + 2 + 4) * sizeof(sljit_w);
464 local_size = (local_size + 15) & ~0xf;
465 compiler->local_size = local_size;
466
467 if (local_size <= SIMM_MAX) {
468 /* Frequent case. */
469 FAIL_IF(push_inst(compiler, ADDIU_W | S(REAL_STACK_PTR) | T(REAL_STACK_PTR) | IMM(-local_size), DR(REAL_STACK_PTR)));
470 base = S(REAL_STACK_PTR);
471 }
472 else {
473 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
474 FAIL_IF(push_inst(compiler, ADDU_W | S(REAL_STACK_PTR) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
475 FAIL_IF(push_inst(compiler, SUBU_W | S(REAL_STACK_PTR) | T(TMP_REG1) | D(REAL_STACK_PTR), DR(REAL_STACK_PTR)));
476 base = S(TMP_REG2);
477 local_size = 0;
478 }
479
480 FAIL_IF(push_inst(compiler, STACK_STORE | base | TA(31) | IMM(local_size - 1 * (int)sizeof(sljit_w)), MOVABLE_INS));
481 if (compiler->has_locals)
482 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_LOCALS_REG) | IMM(local_size - 2 * (int)sizeof(sljit_w)), MOVABLE_INS));
483 if (generals >= 1)
484 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_GENERAL_REG1) | IMM(local_size - 3 * (int)sizeof(sljit_w)), MOVABLE_INS));
485 if (generals >= 2)
486 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_GENERAL_REG2) | IMM(local_size - 4 * (int)sizeof(sljit_w)), MOVABLE_INS));
487 if (generals >= 3)
488 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_GENERAL_REG3) | IMM(local_size - 5 * (int)sizeof(sljit_w)), MOVABLE_INS));
489 if (generals >= 4)
490 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_GENERAL_EREG1) | IMM(local_size - 6 * (int)sizeof(sljit_w)), MOVABLE_INS));
491 if (generals >= 5)
492 FAIL_IF(push_inst(compiler, STACK_STORE | base | T(SLJIT_GENERAL_EREG2) | IMM(local_size - 7 * (int)sizeof(sljit_w)), MOVABLE_INS));
493
494 if (compiler->has_locals)
495 FAIL_IF(push_inst(compiler, ADDIU_W | S(REAL_STACK_PTR) | T(SLJIT_LOCALS_REG) | IMM(4 * sizeof(sljit_w)), DR(SLJIT_LOCALS_REG)));
496
497 if (args >= 1)
498 FAIL_IF(push_inst(compiler, ADDU_W | SA(4) | TA(0) | D(SLJIT_GENERAL_REG1), DR(SLJIT_GENERAL_REG1)));
499 if (args >= 2)
500 FAIL_IF(push_inst(compiler, ADDU_W | SA(5) | TA(0) | D(SLJIT_GENERAL_REG2), DR(SLJIT_GENERAL_REG2)));
501 if (args >= 3)
502 FAIL_IF(push_inst(compiler, ADDU_W | SA(6) | TA(0) | D(SLJIT_GENERAL_REG3), DR(SLJIT_GENERAL_REG3)));
503
504 return SLJIT_SUCCESS;
505 }
506
507 void sljit_fake_enter(struct sljit_compiler *compiler, int args, int temporaries, int generals, int local_size)
508 {
509 CHECK_ERROR_VOID();
510 check_sljit_fake_enter(compiler, args, temporaries, generals, local_size);
511
512 compiler->temporaries = temporaries;
513 compiler->generals = generals;
514
515 compiler->has_locals = local_size > 0;
516 local_size += (generals + 2 + 4) * sizeof(sljit_w);
517 compiler->local_size = (local_size + 15) & ~0xf;
518 }
519
520 int sljit_emit_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
521 {
522 int local_size;
523 sljit_ins base;
524
525 CHECK_ERROR();
526 check_sljit_emit_return(compiler, src, srcw);
527
528 local_size = compiler->local_size;
529
530 if (src != SLJIT_UNUSED && src != SLJIT_RETURN_REG)
531 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, SLJIT_RETURN_REG, 0, TMP_REG1, 0, src, srcw));
532
533 if (local_size <= SIMM_MAX)
534 base = S(REAL_STACK_PTR);
535 else {
536 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
537 FAIL_IF(push_inst(compiler, ADDU_W | S(REAL_STACK_PTR) | T(TMP_REG1) | D(TMP_REG1), DR(TMP_REG1)));
538 base = S(TMP_REG1);
539 local_size = 0;
540 }
541
542 FAIL_IF(push_inst(compiler, STACK_LOAD | base | TA(31) | IMM(local_size - 1 * (int)sizeof(sljit_w)), 31));
543 if (compiler->generals >= 5)
544 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_GENERAL_EREG2) | IMM(local_size - 7 * (int)sizeof(sljit_w)), DR(SLJIT_GENERAL_EREG2)));
545 if (compiler->generals >= 4)
546 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_GENERAL_EREG1) | IMM(local_size - 6 * (int)sizeof(sljit_w)), DR(SLJIT_GENERAL_EREG1)));
547 if (compiler->generals >= 3)
548 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_GENERAL_REG3) | IMM(local_size - 5 * (int)sizeof(sljit_w)), DR(SLJIT_GENERAL_REG3)));
549 if (compiler->generals >= 2)
550 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_GENERAL_REG2) | IMM(local_size - 4 * (int)sizeof(sljit_w)), DR(SLJIT_GENERAL_REG2)));
551 if (compiler->generals >= 1)
552 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_GENERAL_REG1) | IMM(local_size - 3 * (int)sizeof(sljit_w)), DR(SLJIT_GENERAL_REG1)));
553 if (compiler->has_locals)
554 FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(SLJIT_LOCALS_REG) | IMM(local_size - 2 * (int)sizeof(sljit_w)), DR(SLJIT_LOCALS_REG)));
555
556 FAIL_IF(push_inst(compiler, JR | SA(31), UNMOVABLE_INS));
557 if (compiler->local_size <= SIMM_MAX)
558 return push_inst(compiler, ADDIU_W | S(REAL_STACK_PTR) | T(REAL_STACK_PTR) | IMM(compiler->local_size), UNMOVABLE_INS);
559 else
560 return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(REAL_STACK_PTR), UNMOVABLE_INS);
561 }
562
563 #undef STACK_STORE
564 #undef STACK_LOAD
565
566 /* --------------------------------------------------------------------- */
567 /* Operators */
568 /* --------------------------------------------------------------------- */
569
570 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
571 #define ARCH_DEPEND(a, b) a
572 #else
573 #define ARCH_DEPEND(a, b) b
574 #endif
575
576 static SLJIT_CONST sljit_ins data_transfer_insts[16] = {
577 /* s u w */ ARCH_DEPEND(HI(43) /* sw */, HI(63) /* sd */),
578 /* s u b */ HI(40) /* sb */,
579 /* s u h */ HI(41) /* sh*/,
580 /* s u i */ HI(43) /* sw */,
581
582 /* s s w */ ARCH_DEPEND(HI(43) /* sw */, HI(63) /* sd */),
583 /* s s b */ HI(40) /* sb */,
584 /* s s h */ HI(41) /* sh*/,
585 /* s s i */ HI(43) /* sw */,
586
587 /* l u w */ ARCH_DEPEND(HI(35) /* lw */, HI(55) /* ld */),
588 /* l u b */ HI(36) /* lbu */,
589 /* l u h */ HI(37) /* lhu */,
590 /* l u i */ ARCH_DEPEND(HI(35) /* lw */, HI(39) /* lwu */),
591
592 /* l s w */ ARCH_DEPEND(HI(35) /* lw */, HI(55) /* ld */),
593 /* l s b */ HI(32) /* lb */,
594 /* l s h */ HI(33) /* lh */,
595 /* l s i */ HI(35) /* lw */,
596 };
597
598 /* reg_ar is an absoulute register! */
599
600 /* Can perform an operation using at most 1 instruction. */
601 static int getput_arg_fast(struct sljit_compiler *compiler, int flags, int reg_ar, int arg, sljit_w argw)
602 {
603 SLJIT_ASSERT(arg & SLJIT_MEM);
604
605 if (!(flags & WRITE_BACK) && !(arg & 0xf0) && argw <= SIMM_MAX && argw >= SIMM_MIN) {
606 /* Works for both absoulte and relative addresses. */
607 if (SLJIT_UNLIKELY(flags & ARG_TEST))
608 return 1;
609 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(arg & 0xf) | TA(reg_ar) | IMM(argw), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS));
610 return -1;
611 }
612 return (flags & ARG_TEST) ? SLJIT_SUCCESS : 0;
613 }
614
615 /* See getput_arg below.
616 Note: can_cache is called only for binary operators. Those
617 operators always uses word arguments without write back. */
618 static int can_cache(int arg, sljit_w argw, int next_arg, sljit_w next_argw)
619 {
620 if (!(next_arg & SLJIT_MEM))
621 return 0;
622
623 /* Simple operation except for updates. */
624 if (arg & 0xf0) {
625 argw &= 0x3;
626 next_argw &= 0x3;
627 if (argw && argw == next_argw && (arg == next_arg || (arg & 0xf0) == (next_arg & 0xf0)))
628 return 1;
629 return 0;
630 }
631
632 if (arg == next_arg) {
633 if (((sljit_uw)(next_argw - argw) <= SIMM_MAX && (sljit_uw)(next_argw - argw) >= SIMM_MIN))
634 return 1;
635 return 0;
636 }
637
638 return 0;
639 }
640
641 /* Emit the necessary instructions. See can_cache above. */
642 static int getput_arg(struct sljit_compiler *compiler, int flags, int reg_ar, int arg, sljit_w argw, int next_arg, sljit_w next_argw)
643 {
644 int tmp_ar;
645 int base;
646
647 SLJIT_ASSERT(arg & SLJIT_MEM);
648 if (!(next_arg & SLJIT_MEM)) {
649 next_arg = 0;
650 next_argw = 0;
651 }
652
653 tmp_ar = (flags & LOAD_DATA) ? reg_ar : DR(TMP_REG3);
654 base = arg & 0xf;
655
656 if (SLJIT_UNLIKELY(arg & 0xf0)) {
657 argw &= 0x3;
658 if ((flags & WRITE_BACK) && reg_ar == DR(base)) {
659 SLJIT_ASSERT(!(flags & LOAD_DATA) && DR(TMP_REG1) != reg_ar);
660 FAIL_IF(push_inst(compiler, ADDU_W | SA(reg_ar) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
661 reg_ar = DR(TMP_REG1);
662 }
663
664 /* Using the cache. */
665 if (argw == compiler->cache_argw) {
666 if (!(flags & WRITE_BACK)) {
667 if (arg == compiler->cache_arg)
668 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
669 if ((SLJIT_MEM | (arg & 0xf0)) == compiler->cache_arg) {
670 if (arg == next_arg && argw == (next_argw & 0x3)) {
671 compiler->cache_arg = arg;
672 compiler->cache_argw = argw;
673 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
674 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
675 }
676 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | DA(tmp_ar), tmp_ar));
677 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
678 }
679 }
680 else {
681 if ((SLJIT_MEM | (arg & 0xf0)) == compiler->cache_arg) {
682 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
683 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
684 }
685 }
686 }
687
688 if (SLJIT_UNLIKELY(argw)) {
689 compiler->cache_arg = SLJIT_MEM | (arg & 0xf0);
690 compiler->cache_argw = argw;
691 FAIL_IF(push_inst(compiler, SLL_W | T((arg >> 4) & 0xf) | D(TMP_REG3) | SH_IMM(argw), DR(TMP_REG3)));
692 }
693
694 if (!(flags & WRITE_BACK)) {
695 if (arg == next_arg && argw == (next_argw & 0x3)) {
696 compiler->cache_arg = arg;
697 compiler->cache_argw = argw;
698 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? ((arg >> 4) & 0xf) : TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
699 tmp_ar = DR(TMP_REG3);
700 }
701 else
702 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? ((arg >> 4) & 0xf) : TMP_REG3) | DA(tmp_ar), tmp_ar));
703 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
704 }
705 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? ((arg >> 4) & 0xf) : TMP_REG3) | D(base), DR(base)));
706 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
707 }
708
709 if (SLJIT_UNLIKELY(flags & WRITE_BACK) && base) {
710 /* Update only applies if a base register exists. */
711 if (reg_ar == DR(base)) {
712 SLJIT_ASSERT(!(flags & LOAD_DATA) && DR(TMP_REG1) != reg_ar);
713 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
714 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar) | IMM(argw), MOVABLE_INS));
715 if (argw)
716 return push_inst(compiler, ADDIU_W | S(base) | T(base) | IMM(argw), DR(base));
717 return SLJIT_SUCCESS;
718 }
719 FAIL_IF(push_inst(compiler, ADDU_W | SA(reg_ar) | TA(0) | D(TMP_REG1), DR(TMP_REG1)));
720 reg_ar = DR(TMP_REG1);
721 }
722
723 if (argw <= SIMM_MAX && argw >= SIMM_MIN) {
724 if (argw)
725 FAIL_IF(push_inst(compiler, ADDIU_W | S(base) | T(base) | IMM(argw), DR(base)));
726 }
727 else {
728 if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
729 if (argw != compiler->cache_argw) {
730 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
731 compiler->cache_argw = argw;
732 }
733 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
734 }
735 else {
736 compiler->cache_arg = SLJIT_MEM;
737 compiler->cache_argw = argw;
738 FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
739 FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(base), DR(base)));
740 }
741 }
742 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(base) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
743 }
744
745 if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
746 if (argw != compiler->cache_argw) {
747 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
748 compiler->cache_argw = argw;
749 }
750 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
751 }
752
753 if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
754 if (argw != compiler->cache_argw)
755 FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
756 }
757 else {
758 compiler->cache_arg = SLJIT_MEM;
759 FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
760 }
761 compiler->cache_argw = argw;
762
763 if (!base)
764 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
765
766 if (arg == next_arg && next_argw - argw <= SIMM_MAX && next_argw - argw >= SIMM_MIN) {
767 compiler->cache_arg = arg;
768 FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | D(TMP_REG3), DR(TMP_REG3)));
769 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
770 }
771
772 FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | DA(tmp_ar), tmp_ar));
773 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), (flags & LOAD_DATA) ? reg_ar : MOVABLE_INS);
774 }
775
776 static SLJIT_INLINE int emit_op_mem(struct sljit_compiler *compiler, int flags, int reg_ar, int arg, sljit_w argw)
777 {
778 if (getput_arg_fast(compiler, flags, reg_ar, arg, argw))
779 return compiler->error;
780 compiler->cache_arg = 0;
781 compiler->cache_argw = 0;
782 return getput_arg(compiler, flags, reg_ar, arg, argw, 0, 0);
783 }
784
785 static int emit_op(struct sljit_compiler *compiler, int op, int flags,
786 int dst, sljit_w dstw,
787 int src1, sljit_w src1w,
788 int src2, sljit_w src2w)
789 {
790 /* arg1 goes to TMP_REG1 or src reg
791 arg2 goes to TMP_REG2, imm or src reg
792 TMP_REG3 can be used for caching
793 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
794 int dst_r = TMP_REG2;
795 int src1_r;
796 sljit_w src2_r = 0;
797 int sugg_src2_r = TMP_REG2;
798
799 compiler->cache_arg = 0;
800 compiler->cache_argw = 0;
801
802 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= TMP_REG3) {
803 dst_r = dst;
804 flags |= REG_DEST;
805 if (GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)
806 sugg_src2_r = dst_r;
807 }
808 else if (dst == SLJIT_UNUSED) {
809 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
810 return SLJIT_SUCCESS;
811 if (GET_FLAGS(op))
812 flags |= UNUSED_DEST;
813 }
814 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, DR(TMP_REG1), dst, dstw))
815 flags |= SLOW_DEST;
816
817 if (flags & IMM_OP) {
818 if ((src2 & SLJIT_IMM) && src2w) {
819 if ((!(flags & LOGICAL_OP) && (src2w <= SIMM_MAX && src2w >= SIMM_MIN))
820 || ((flags & LOGICAL_OP) && !(src2w & ~UIMM_MAX))) {
821 flags |= SRC2_IMM;
822 src2_r = src2w;
823 }
824 }
825 if ((src1 & SLJIT_IMM) && src1w && (flags & CUMULATIVE_OP) && !(flags & SRC2_IMM)) {
826 if ((!(flags & LOGICAL_OP) && (src1w <= SIMM_MAX && src1w >= SIMM_MIN))
827 || ((flags & LOGICAL_OP) && !(src1w & ~UIMM_MAX))) {
828 flags |= SRC2_IMM;
829 src2_r = src1w;
830
831 /* And swap arguments. */
832 src1 = src2;
833 src1w = src2w;
834 src2 = SLJIT_IMM;
835 /* src2w = src2_r unneeded. */
836 }
837 }
838 }
839
840 /* Source 1. */
841 if (src1 >= SLJIT_TEMPORARY_REG1 && src1 <= TMP_REG3) {
842 src1_r = src1;
843 flags |= REG1_SOURCE;
844 }
845 else if (src1 & SLJIT_IMM) {
846 if (src1w) {
847 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w));
848 src1_r = TMP_REG1;
849 }
850 else
851 src1_r = 0;
852 }
853 else {
854 if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w))
855 FAIL_IF(compiler->error);
856 else
857 flags |= SLOW_SRC1;
858 src1_r = TMP_REG1;
859 }
860
861 /* Source 2. */
862 if (src2 >= SLJIT_TEMPORARY_REG1 && src2 <= TMP_REG3) {
863 src2_r = src2;
864 flags |= REG2_SOURCE;
865 if (!(flags & REG_DEST) && GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)
866 dst_r = src2_r;
867 }
868 else if (src2 & SLJIT_IMM) {
869 if (!(flags & SRC2_IMM)) {
870 if (src2w || (GET_OPCODE(op) >= SLJIT_MOV && GET_OPCODE(op) <= SLJIT_MOVU_SI)) {
871 FAIL_IF(load_immediate(compiler, DR(sugg_src2_r), src2w));
872 src2_r = sugg_src2_r;
873 }
874 else
875 src2_r = 0;
876 }
877 }
878 else {
879 if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w))
880 FAIL_IF(compiler->error);
881 else
882 flags |= SLOW_SRC2;
883 src2_r = sugg_src2_r;
884 }
885
886 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
887 SLJIT_ASSERT(src2_r == TMP_REG2);
888 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
889 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, src1, src1w));
890 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
891 }
892 else {
893 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, src2, src2w));
894 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, dst, dstw));
895 }
896 }
897 else if (flags & SLOW_SRC1)
898 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
899 else if (flags & SLOW_SRC2)
900 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w, dst, dstw));
901
902 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
903
904 if (dst & SLJIT_MEM) {
905 if (!(flags & SLOW_DEST)) {
906 getput_arg_fast(compiler, flags, DR(dst_r), dst, dstw);
907 return compiler->error;
908 }
909 return getput_arg(compiler, flags, DR(dst_r), dst, dstw, 0, 0);
910 }
911
912 return SLJIT_SUCCESS;
913 }
914
915 int sljit_emit_op0(struct sljit_compiler *compiler, int op)
916 {
917 CHECK_ERROR();
918 check_sljit_emit_op0(compiler, op);
919
920 op = GET_OPCODE(op);
921 switch (op) {
922 case SLJIT_BREAKPOINT:
923 return push_inst(compiler, BREAK, UNMOVABLE_INS);
924 case SLJIT_NOP:
925 return push_inst(compiler, NOP, UNMOVABLE_INS);
926 }
927
928 return SLJIT_SUCCESS;
929 }
930
931 int sljit_emit_op1(struct sljit_compiler *compiler, int op,
932 int dst, sljit_w dstw,
933 int src, sljit_w srcw)
934 {
935 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
936 #define inp_flags 0
937 #endif
938
939 CHECK_ERROR();
940 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
941
942 SLJIT_ASSERT(SLJIT_MOV + 7 == SLJIT_MOVU);
943
944 switch (GET_OPCODE(op)) {
945 case SLJIT_MOV:
946 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
947
948 case SLJIT_MOV_UI:
949 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
950
951 case SLJIT_MOV_SI:
952 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
953
954 case SLJIT_MOV_UB:
955 return emit_op(compiler, SLJIT_MOV_UB, inp_flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned char)srcw : srcw);
956
957 case SLJIT_MOV_SB:
958 return emit_op(compiler, SLJIT_MOV_SB, inp_flags | BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed char)srcw : srcw);
959
960 case SLJIT_MOV_UH:
961 return emit_op(compiler, SLJIT_MOV_UH, inp_flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned short)srcw : srcw);
962
963 case SLJIT_MOV_SH:
964 return emit_op(compiler, SLJIT_MOV_SH, inp_flags | HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed short)srcw : srcw);
965
966 case SLJIT_MOVU:
967 return emit_op(compiler, SLJIT_MOV, inp_flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
968
969 case SLJIT_MOVU_UI:
970 return emit_op(compiler, SLJIT_MOV_UI, inp_flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
971
972 case SLJIT_MOVU_SI:
973 return emit_op(compiler, SLJIT_MOV_SI, inp_flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
974
975 case SLJIT_MOVU_UB:
976 return emit_op(compiler, SLJIT_MOV_UB, inp_flags | BYTE_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned char)srcw : srcw);
977
978 case SLJIT_MOVU_SB:
979 return emit_op(compiler, SLJIT_MOV_SB, inp_flags | BYTE_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed char)srcw : srcw);
980
981 case SLJIT_MOVU_UH:
982 return emit_op(compiler, SLJIT_MOV_UH, inp_flags | HALF_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (unsigned short)srcw : srcw);
983
984 case SLJIT_MOVU_SH:
985 return emit_op(compiler, SLJIT_MOV_SH, inp_flags | HALF_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (signed short)srcw : srcw);
986
987 case SLJIT_NOT:
988 return emit_op(compiler, op, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
989
990 case SLJIT_NEG:
991 return emit_op(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), inp_flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
992
993 case SLJIT_CLZ:
994 return emit_op(compiler, op, inp_flags, dst, dstw, TMP_REG1, 0, src, srcw);
995 }
996
997 return SLJIT_SUCCESS;
998 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
999 #undef inp_flags
1000 #endif
1001 }
1002
1003 int sljit_emit_op2(struct sljit_compiler *compiler, int op,
1004 int dst, sljit_w dstw,
1005 int src1, sljit_w src1w,
1006 int src2, sljit_w src2w)
1007 {
1008 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1009 #define inp_flags 0
1010 #endif
1011
1012 CHECK_ERROR();
1013 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1014
1015 switch (GET_OPCODE(op)) {
1016 case SLJIT_ADD:
1017 case SLJIT_ADDC:
1018 return emit_op(compiler, op, inp_flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1019
1020 case SLJIT_SUB:
1021 case SLJIT_SUBC:
1022 return emit_op(compiler, op, inp_flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1023
1024 case SLJIT_MUL:
1025 return emit_op(compiler, op, inp_flags | CUMULATIVE_OP, dst, dstw, src1, src1w, src2, src2w);
1026
1027 case SLJIT_AND:
1028 case SLJIT_OR:
1029 case SLJIT_XOR:
1030 return emit_op(compiler, op, inp_flags | CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1031
1032 case SLJIT_SHL:
1033 case SLJIT_LSHR:
1034 case SLJIT_ASHR:
1035 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1036 if (src2 & SLJIT_IMM)
1037 src2w &= 0x1f;
1038 #else
1039 if (src2 & SLJIT_IMM)
1040 src2w &= 0x3f;
1041 #endif
1042 return emit_op(compiler, op, inp_flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
1043 }
1044
1045 return SLJIT_SUCCESS;
1046 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1047 #undef inp_flags
1048 #endif
1049 }
1050
1051 /* --------------------------------------------------------------------- */
1052 /* Floating point operators */
1053 /* --------------------------------------------------------------------- */
1054
1055 int sljit_is_fpu_available(void)
1056 {
1057 #if 0
1058 sljit_w fir;
1059 asm ("cfc1 %0, $0" : "=r"(fir));
1060 return (fir >> 22) & 0x1;
1061 #endif
1062 /* Qemu says fir is 0 by default. */
1063 return 1;
1064 }
1065
1066 static int emit_fpu_data_transfer(struct sljit_compiler *compiler, int fpu_reg, int load, int arg, sljit_w argw)
1067 {
1068 int hi_reg;
1069
1070 SLJIT_ASSERT(arg & SLJIT_MEM);
1071
1072 /* Fast loads and stores. */
1073 if (!(arg & 0xf0)) {
1074 /* Both for (arg & 0xf) == SLJIT_UNUSED and (arg & 0xf) != SLJIT_UNUSED. */
1075 if (argw <= SIMM_MAX && argw >= SIMM_MIN)
1076 return push_inst(compiler, (load ? LDC1 : SDC1) | S(arg & 0xf) | FT(fpu_reg) | IMM(argw), MOVABLE_INS);
1077 }
1078
1079 if (arg & 0xf0) {
1080 argw &= 0x3;
1081 hi_reg = (arg >> 4) & 0xf;
1082 if (argw) {
1083 FAIL_IF(push_inst(compiler, SLL_W | T(hi_reg) | D(TMP_REG1) | SH_IMM(argw), DR(TMP_REG1)));
1084 hi_reg = TMP_REG1;
1085 }
1086 FAIL_IF(push_inst(compiler, ADDU_W | S(hi_reg) | T(arg & 0xf) | D(TMP_REG1), DR(TMP_REG1)));
1087 return push_inst(compiler, (load ? LDC1 : SDC1) | S(TMP_REG1) | FT(fpu_reg) | IMM(0), MOVABLE_INS);
1088 }
1089
1090 /* Use cache. */
1091 if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN)
1092 return push_inst(compiler, (load ? LDC1 : SDC1) | S(TMP_REG3) | FT(fpu_reg) | IMM(argw - compiler->cache_argw), MOVABLE_INS);
1093
1094 /* Put value to cache. */
1095 compiler->cache_arg = arg;
1096 compiler->cache_argw = argw;
1097
1098 FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
1099 if (arg & 0xf)
1100 FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(arg & 0xf) | D(TMP_REG3), DR(TMP_REG3)));
1101 return push_inst(compiler, (load ? LDC1 : SDC1) | S(TMP_REG3) | FT(fpu_reg) | IMM(0), MOVABLE_INS);
1102 }
1103
1104 int sljit_emit_fop1(struct sljit_compiler *compiler, int op,
1105 int dst, sljit_w dstw,
1106 int src, sljit_w srcw)
1107 {
1108 int dst_fr;
1109
1110 CHECK_ERROR();
1111 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
1112
1113 compiler->cache_arg = 0;
1114 compiler->cache_argw = 0;
1115
1116 if (GET_OPCODE(op) == SLJIT_FCMP) {
1117 if (dst > SLJIT_FLOAT_REG4) {
1118 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 1, dst, dstw));
1119 dst = TMP_FREG1;
1120 }
1121 if (src > SLJIT_FLOAT_REG4) {
1122 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG2, 1, src, srcw));
1123 src = TMP_FREG2;
1124 }
1125
1126 /* src and dst are swapped. */
1127 if (op & SLJIT_SET_E)
1128 FAIL_IF(push_inst(compiler, C_UEQ_D | FT(src) | FS(dst) | (EQUAL_BIT << 8), FCSR_FCC + EQUAL_BIT));
1129 if (op & SLJIT_SET_S) {
1130 FAIL_IF(push_inst(compiler, C_ULT_D | FT(src) | FS(dst) | (LESS_BIT << 8), FCSR_FCC + LESS_BIT));
1131 FAIL_IF(push_inst(compiler, C_ULT_D | FT(dst) | FS(src) | (GREATER_BIT << 8), FCSR_FCC + GREATER_BIT));
1132 }
1133 return push_inst(compiler, C_UN_D | FT(src) | FS(dst) | (UNORD_BIT << 8), FCSR_FCC + UNORD_BIT);
1134 }
1135
1136 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG1 : dst;
1137
1138 if (src > SLJIT_FLOAT_REG4) {
1139 FAIL_IF(emit_fpu_data_transfer(compiler, dst_fr, 1, src, srcw));
1140 src = dst_fr;
1141 }
1142
1143 switch (op) {
1144 case SLJIT_FMOV:
1145 if (src != dst_fr && dst_fr != TMP_FREG1)
1146 FAIL_IF(push_inst(compiler, MOV_D | FS(src) | FD(dst_fr), MOVABLE_INS));
1147 break;
1148 case SLJIT_FNEG:
1149 FAIL_IF(push_inst(compiler, NEG_D | FS(src) | FD(dst_fr), MOVABLE_INS));
1150 break;
1151 case SLJIT_FABS:
1152 FAIL_IF(push_inst(compiler, ABS_D | FS(src) | FD(dst_fr), MOVABLE_INS));
1153 break;
1154 }
1155
1156 if (dst_fr == TMP_FREG1)
1157 FAIL_IF(emit_fpu_data_transfer(compiler, src, 0, dst, dstw));
1158
1159 return SLJIT_SUCCESS;
1160 }
1161
1162 int sljit_emit_fop2(struct sljit_compiler *compiler, int op,
1163 int dst, sljit_w dstw,
1164 int src1, sljit_w src1w,
1165 int src2, sljit_w src2w)
1166 {
1167 int dst_fr;
1168
1169 CHECK_ERROR();
1170 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
1171
1172 compiler->cache_arg = 0;
1173 compiler->cache_argw = 0;
1174
1175 dst_fr = (dst > SLJIT_FLOAT_REG4) ? TMP_FREG1 : dst;
1176
1177 if (src2 > SLJIT_FLOAT_REG4) {
1178 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG2, 1, src2, src2w));
1179 src2 = TMP_FREG2;
1180 }
1181
1182 if (src1 > SLJIT_FLOAT_REG4) {
1183 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 1, src1, src1w));
1184 src1 = TMP_FREG1;
1185 }
1186
1187 switch (op) {
1188 case SLJIT_FADD:
1189 FAIL_IF(push_inst(compiler, ADD_D | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1190 break;
1191
1192 case SLJIT_FSUB:
1193 FAIL_IF(push_inst(compiler, SUB_D | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1194 break;
1195
1196 case SLJIT_FMUL:
1197 FAIL_IF(push_inst(compiler, MUL_D | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1198 break;
1199
1200 case SLJIT_FDIV:
1201 FAIL_IF(push_inst(compiler, DIV_D | FT(src2) | FS(src1) | FD(dst_fr), MOVABLE_INS));
1202 break;
1203 }
1204
1205 if (dst_fr == TMP_FREG1)
1206 FAIL_IF(emit_fpu_data_transfer(compiler, TMP_FREG1, 0, dst, dstw));
1207
1208 return SLJIT_SUCCESS;
1209 }
1210
1211 /* --------------------------------------------------------------------- */
1212 /* Other instructions */
1213 /* --------------------------------------------------------------------- */
1214
1215 int sljit_emit_fast_enter(struct sljit_compiler *compiler, int dst, sljit_w dstw, int args, int temporaries, int generals, int local_size)
1216 {
1217 CHECK_ERROR();
1218 check_sljit_emit_fast_enter(compiler, dst, dstw, args, temporaries, generals, local_size);
1219
1220 compiler->temporaries = temporaries;
1221 compiler->generals = generals;
1222
1223 compiler->has_locals = local_size > 0;
1224 local_size += (generals + 2 + 4) * sizeof(sljit_w);
1225 compiler->local_size = (local_size + 15) & ~0xf;
1226
1227 if (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS)
1228 return push_inst(compiler, ADDU_W | SA(31) | TA(0) | D(dst), DR(dst));
1229 else if (dst & SLJIT_MEM)
1230 return emit_op_mem(compiler, WORD_DATA, 31, dst, dstw);
1231 return SLJIT_SUCCESS;
1232 }
1233
1234 int sljit_emit_fast_return(struct sljit_compiler *compiler, int src, sljit_w srcw)
1235 {
1236 CHECK_ERROR();
1237 check_sljit_emit_fast_return(compiler, src, srcw);
1238
1239 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS)
1240 FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | DA(31), 31));
1241 else if (src & SLJIT_MEM)
1242 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, 31, src, srcw));
1243 else if (src & SLJIT_IMM)
1244 FAIL_IF(load_immediate(compiler, 31, srcw));
1245
1246 FAIL_IF(push_inst(compiler, JR | SA(31), UNMOVABLE_INS));
1247 return push_inst(compiler, NOP, UNMOVABLE_INS);
1248 }
1249
1250 /* --------------------------------------------------------------------- */
1251 /* Conditional instructions */
1252 /* --------------------------------------------------------------------- */
1253
1254 struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1255 {
1256 struct sljit_label *label;
1257
1258 CHECK_ERROR_PTR();
1259 check_sljit_emit_label(compiler);
1260
1261 if (compiler->last_label && compiler->last_label->size == compiler->size)
1262 return compiler->last_label;
1263
1264 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1265 PTR_FAIL_IF(!label);
1266 set_label(label, compiler);
1267 compiler->delay_slot = UNMOVABLE_INS;
1268 return label;
1269 }
1270
1271 #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
1272 #define JUMP_LENGTH 4
1273 #else
1274 #define JUMP_LENGTH 7
1275 #endif
1276
1277 #define BR_Z(src) \
1278 inst = BEQ | SA(src) | TA(0) | JUMP_LENGTH; \
1279 flags = IS_BIT26_COND; \
1280 delay_check = src;
1281
1282 #define BR_NZ(src) \
1283 inst = BNE | SA(src) | TA(0) | JUMP_LENGTH; \
1284 flags = IS_BIT26_COND; \
1285 delay_check = src;
1286
1287 #define BR_T(bit) \
1288 inst = BC1T | (bit << 18) | JUMP_LENGTH; \
1289 flags = IS_BIT16_COND; \
1290 delay_check = FCSR_FCC + bit;
1291
1292 #define BR_F(bit) \
1293 inst = BC1F | (bit << 18) | JUMP_LENGTH; \
1294 flags = IS_BIT16_COND; \
1295 delay_check = FCSR_FCC + bit;
1296
1297 struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, int type)
1298 {
1299 struct sljit_jump *jump;
1300 sljit_ins inst;
1301 int flags = 0;
1302 int delay_check = UNMOVABLE_INS;
1303
1304 CHECK_ERROR_PTR();
1305 check_sljit_emit_jump(compiler, type);
1306
1307 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1308 PTR_FAIL_IF(!jump);
1309 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1310 type &= 0xff;
1311
1312 switch (type) {
1313 case SLJIT_C_EQUAL:
1314 BR_NZ(EQUAL_FLAG);
1315 break;
1316 case SLJIT_C_NOT_EQUAL:
1317 BR_Z(EQUAL_FLAG);
1318 break;
1319 case SLJIT_C_LESS:
1320 BR_Z(ULESS_FLAG);
1321 break;
1322 case SLJIT_C_GREATER_EQUAL:
1323 BR_NZ(ULESS_FLAG);
1324 break;
1325 case SLJIT_C_GREATER:
1326 BR_Z(UGREATER_FLAG);
1327 break;
1328 case SLJIT_C_LESS_EQUAL:
1329 BR_NZ(UGREATER_FLAG);
1330 break;
1331 case SLJIT_C_SIG_LESS:
1332 BR_Z(LESS_FLAG);
1333 break;
1334 case SLJIT_C_SIG_GREATER_EQUAL:
1335 BR_NZ(LESS_FLAG);
1336 break;
1337 case SLJIT_C_SIG_GREATER:
1338 BR_Z(GREATER_FLAG);
1339 break;
1340 case SLJIT_C_SIG_LESS_EQUAL:
1341 BR_NZ(GREATER_FLAG);
1342 break;
1343 case SLJIT_C_OVERFLOW:
1344 case SLJIT_C_MUL_OVERFLOW:
1345 BR_Z(OVERFLOW_FLAG);
1346 break;
1347 case SLJIT_C_NOT_OVERFLOW:
1348 case SLJIT_C_MUL_NOT_OVERFLOW:
1349 BR_NZ(OVERFLOW_FLAG);
1350 break;
1351 case SLJIT_C_FLOAT_EQUAL:
1352 BR_F(EQUAL_BIT);
1353 break;
1354 case SLJIT_C_FLOAT_NOT_EQUAL:
1355 BR_T(EQUAL_BIT);
1356 break;
1357 case SLJIT_C_FLOAT_LESS:
1358 BR_F(LESS_BIT);
1359 break;
1360 case SLJIT_C_FLOAT_GREATER_EQUAL:
1361 BR_T(LESS_BIT);
1362 break;
1363 case SLJIT_C_FLOAT_GREATER:
1364 BR_F(GREATER_BIT);
1365 break;
1366 case SLJIT_C_FLOAT_LESS_EQUAL:
1367 BR_T(GREATER_BIT);
1368 break;
1369 case SLJIT_C_FLOAT_NAN:
1370 BR_F(UNORD_BIT);
1371 break;
1372 case SLJIT_C_FLOAT_NOT_NAN:
1373 BR_T(UNORD_BIT);
1374 break;
1375 default:
1376 /* Not conditional branch. */
1377 inst = 0;
1378 break;
1379 }
1380
1381 jump->flags |= flags;
1382 if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != delay_check))
1383 jump->flags |= IS_MOVABLE;
1384
1385 if (inst)
1386 PTR_FAIL_IF(push_inst(compiler, inst, UNMOVABLE_INS));
1387
1388 if (type >= SLJIT_CALL1)
1389 PTR_FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_TEMPORARY_REG1) | TA(0) | DA(4), 4));
1390
1391 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1392 if (type <= SLJIT_JUMP)
1393 PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1394 else {
1395 jump->flags |= IS_JAL;
1396 PTR_FAIL_IF(push_inst(compiler, JALR | S(TMP_REG2) | DA(31), UNMOVABLE_INS));
1397 }
1398 jump->addr = compiler->size;
1399 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1400 return jump;
1401 }
1402
1403 #define RESOLVE_IMM1() \
1404 if (src1 & SLJIT_IMM) { \
1405 if (src1w) { \
1406 PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w)); \
1407 src1 = TMP_REG1; \
1408 } \
1409 else \
1410 src1 = 0; \
1411 }
1412
1413 #define RESOLVE_IMM2() \
1414 if (src2 & SLJIT_IMM) { \
1415 if (src2w) { \
1416 PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG2), src2w)); \
1417 src2 = TMP_REG2; \
1418 } \
1419 else \
1420 src2 = 0; \
1421 }
1422
1423 struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, int type,
1424 int src1, sljit_w src1w,
1425 int src2, sljit_w src2w)
1426 {
1427 struct sljit_jump *jump;
1428 int flags;
1429 sljit_ins inst;
1430
1431 CHECK_ERROR_PTR();
1432 check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w);
1433
1434 compiler->cache_arg = 0;
1435 compiler->cache_argw = 0;
1436 flags = ((type & SLJIT_INT_OP) ? INT_DATA : WORD_DATA) | LOAD_DATA;
1437 if (src1 & SLJIT_MEM) {
1438 if (getput_arg_fast(compiler, flags, DR(TMP_REG1), src1, src1w))
1439 PTR_FAIL_IF(compiler->error);
1440 else
1441 PTR_FAIL_IF(getput_arg(compiler, flags, DR(TMP_REG1), src1, src1w, src2, src2w));
1442 src1 = TMP_REG1;
1443 }
1444 if (src2 & SLJIT_MEM) {
1445 if (getput_arg_fast(compiler, flags, DR(TMP_REG2), src2, src2w))
1446 PTR_FAIL_IF(compiler->error);
1447 else
1448 PTR_FAIL_IF(getput_arg(compiler, flags, DR(TMP_REG2), src2, src2w, 0, 0));
1449 src2 = TMP_REG2;
1450 }
1451
1452 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1453 PTR_FAIL_IF(!jump);
1454 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1455 type &= 0xff;
1456
1457 if (type <= SLJIT_C_NOT_EQUAL) {
1458 RESOLVE_IMM1();
1459 RESOLVE_IMM2();
1460 jump->flags |= IS_BIT26_COND;
1461 if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != DR(src1) && compiler->delay_slot != DR(src2)))
1462 jump->flags |= IS_MOVABLE;
1463 PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_C_EQUAL ? BNE : BEQ) | S(src1) | T(src2) | JUMP_LENGTH, UNMOVABLE_INS));
1464 }
1465 else if (type >= SLJIT_C_SIG_LESS && (((src1 & SLJIT_IMM) && (src1w == 0)) || ((src2 & SLJIT_IMM) && (src2w == 0)))) {
1466 inst = NOP;
1467 if ((src1 & SLJIT_IMM) && (src1w == 0)) {
1468 RESOLVE_IMM2();
1469 switch (type) {
1470 case SLJIT_C_SIG_LESS:
1471 inst = BLEZ;
1472 jump->flags |= IS_BIT26_COND;
1473 break;
1474 case SLJIT_C_SIG_GREATER_EQUAL:
1475 inst = BGTZ;
1476 jump->flags |= IS_BIT26_COND;
1477 break;
1478 case SLJIT_C_SIG_GREATER:
1479 inst = BGEZ;
1480 jump->flags |= IS_BIT16_COND;
1481 break;
1482 case SLJIT_C_SIG_LESS_EQUAL:
1483 inst = BLTZ;
1484 jump->flags |= IS_BIT16_COND;
1485 break;
1486 }
1487 src1 = src2;
1488 }
1489 else {
1490 RESOLVE_IMM1();
1491 switch (type) {
1492 case SLJIT_C_SIG_LESS:
1493 inst = BGEZ;
1494 jump->flags |= IS_BIT16_COND;
1495 break;
1496 case SLJIT_C_SIG_GREATER_EQUAL:
1497 inst = BLTZ;
1498 jump->flags |= IS_BIT16_COND;
1499 break;
1500 case SLJIT_C_SIG_GREATER:
1501 inst = BLEZ;
1502 jump->flags |= IS_BIT26_COND;
1503 break;
1504 case SLJIT_C_SIG_LESS_EQUAL:
1505 inst = BGTZ;
1506 jump->flags |= IS_BIT26_COND;
1507 break;
1508 }
1509 }
1510 PTR_FAIL_IF(push_inst(compiler, inst | S(src1) | JUMP_LENGTH, UNMOVABLE_INS));
1511 }
1512 else {
1513 if (type == SLJIT_C_LESS || type == SLJIT_C_GREATER_EQUAL || type == SLJIT_C_SIG_LESS || type == SLJIT_C_SIG_GREATER_EQUAL) {
1514 RESOLVE_IMM1();
1515 if ((src2 & SLJIT_IMM) && src2w <= SIMM_MAX && src2w >= SIMM_MIN)
1516 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTIU : SLTI) | S(src1) | T(TMP_REG1) | IMM(src2w), DR(TMP_REG1)));
1517 else {
1518 RESOLVE_IMM2();
1519 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTU : SLT) | S(src1) | T(src2) | D(TMP_REG1), DR(TMP_REG1)));
1520 }
1521 type = (type == SLJIT_C_LESS || type == SLJIT_C_SIG_LESS) ? SLJIT_C_NOT_EQUAL : SLJIT_C_EQUAL;
1522 }
1523 else {
1524 RESOLVE_IMM2();
1525 if ((src1 & SLJIT_IMM) && src1w <= SIMM_MAX && src1w >= SIMM_MIN)
1526 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTIU : SLTI) | S(src2) | T(TMP_REG1) | IMM(src1w), DR(TMP_REG1)));
1527 else {
1528 RESOLVE_IMM1();
1529 PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_C_LESS_EQUAL ? SLTU : SLT) | S(src2) | T(src1) | D(TMP_REG1), DR(TMP_REG1)));
1530 }
1531 type = (type == SLJIT_C_GREATER || type == SLJIT_C_SIG_GREATER) ? SLJIT_C_NOT_EQUAL : SLJIT_C_EQUAL;
1532 }
1533
1534 jump->flags |= IS_BIT26_COND;
1535 PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_C_EQUAL ? BNE : BEQ) | S(TMP_REG1) | TA(0) | JUMP_LENGTH, UNMOVABLE_INS));
1536 }
1537
1538 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1539 PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
1540 jump->addr = compiler->size;
1541 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1542 return jump;
1543 }
1544
1545 #undef RESOLVE_IMM1
1546 #undef RESOLVE_IMM2
1547
1548 #undef JUMP_LENGTH
1549 #undef BR_Z
1550 #undef BR_NZ
1551 #undef BR_T
1552 #undef BR_F
1553
1554 int sljit_emit_ijump(struct sljit_compiler *compiler, int type, int src, sljit_w srcw)
1555 {
1556 int src_r = TMP_REG2;
1557 struct sljit_jump *jump = NULL;
1558
1559 CHECK_ERROR();
1560 check_sljit_emit_ijump(compiler, type, src, srcw);
1561
1562 if (src >= SLJIT_TEMPORARY_REG1 && src <= SLJIT_NO_REGISTERS) {
1563 if (DR(src) < 4 || DR(src) > 6)
1564 src_r = src;
1565 else
1566 FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
1567 }
1568
1569 if (type >= SLJIT_CALL1)
1570 FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_TEMPORARY_REG1) | TA(0) | DA(4), 4));
1571
1572 if (src & SLJIT_IMM) {
1573 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1574 FAIL_IF(!jump);
1575 set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_CALL0) ? IS_JAL : 0));
1576 jump->u.target = srcw;
1577
1578 if (compiler->delay_slot != UNMOVABLE_INS)
1579 jump->flags |= IS_MOVABLE;
1580
1581 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1582 }
1583 else if (src & SLJIT_MEM)
1584 FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
1585
1586 if (type <= SLJIT_JUMP)
1587 FAIL_IF(push_inst(compiler, JR | S(src_r), UNMOVABLE_INS));
1588 else
1589 FAIL_IF(push_inst(compiler, JALR | S(src_r) | DA(31), UNMOVABLE_INS));
1590 if (jump)
1591 jump->addr = compiler->size;
1592 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1593 return SLJIT_SUCCESS;
1594 }
1595
1596 int sljit_emit_cond_value(struct sljit_compiler *compiler, int op, int dst, sljit_w dstw, int type)
1597 {
1598 int sugg_dst_ar, dst_ar;
1599
1600 CHECK_ERROR();
1601 check_sljit_emit_cond_value(compiler, op, dst, dstw, type);
1602
1603 if (dst == SLJIT_UNUSED)
1604 return SLJIT_SUCCESS;
1605
1606 sugg_dst_ar = DR((op == SLJIT_MOV && dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2);
1607
1608 switch (type) {
1609 case SLJIT_C_EQUAL:
1610 case SLJIT_C_NOT_EQUAL:
1611 FAIL_IF(push_inst(compiler, SLTIU | SA(EQUAL_FLAG) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1612 dst_ar = sugg_dst_ar;
1613 break;
1614 case SLJIT_C_LESS:
1615 case SLJIT_C_GREATER_EQUAL:
1616 dst_ar = ULESS_FLAG;
1617 break;
1618 case SLJIT_C_GREATER:
1619 case SLJIT_C_LESS_EQUAL:
1620 dst_ar = UGREATER_FLAG;
1621 break;
1622 case SLJIT_C_SIG_LESS:
1623 case SLJIT_C_SIG_GREATER_EQUAL:
1624 dst_ar = LESS_FLAG;
1625 break;
1626 case SLJIT_C_SIG_GREATER:
1627 case SLJIT_C_SIG_LESS_EQUAL:
1628 dst_ar = GREATER_FLAG;
1629 break;
1630 case SLJIT_C_OVERFLOW:
1631 case SLJIT_C_NOT_OVERFLOW:
1632 dst_ar = OVERFLOW_FLAG;
1633 break;
1634 case SLJIT_C_MUL_OVERFLOW:
1635 case SLJIT_C_MUL_NOT_OVERFLOW:
1636 FAIL_IF(push_inst(compiler, SLTIU | SA(OVERFLOW_FLAG) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1637 dst_ar = sugg_dst_ar;
1638 type ^= 0x1; /* Flip type bit for the XORI below. */
1639 break;
1640 default:
1641 if (type >= SLJIT_C_FLOAT_EQUAL && type <= SLJIT_C_FLOAT_NOT_NAN) {
1642 FAIL_IF(push_inst(compiler, CFC1 | TA(sugg_dst_ar) | DA(31), sugg_dst_ar));
1643 switch (type) {
1644 case SLJIT_C_FLOAT_EQUAL:
1645 case SLJIT_C_FLOAT_NOT_EQUAL:
1646 dst_ar = EQUAL_BIT + 24;
1647 break;
1648 case SLJIT_C_FLOAT_LESS:
1649 case SLJIT_C_FLOAT_GREATER_EQUAL:
1650 dst_ar = LESS_BIT + 24;
1651 break;
1652 case SLJIT_C_FLOAT_GREATER:
1653 case SLJIT_C_FLOAT_LESS_EQUAL:
1654 dst_ar = GREATER_BIT + 24;
1655 break;
1656 case SLJIT_C_FLOAT_NAN:
1657 case SLJIT_C_FLOAT_NOT_NAN:
1658 dst_ar = UNORD_BIT + 24;
1659 break;
1660 }
1661 FAIL_IF(push_inst(compiler, EXT_W | SA(sugg_dst_ar) | TA(sugg_dst_ar) | (dst_ar << 6), sugg_dst_ar));
1662 }
1663 dst_ar = sugg_dst_ar;
1664 break;
1665 }
1666
1667 if (type & 0x1) {
1668 FAIL_IF(push_inst(compiler, XORI | SA(dst_ar) | TA(sugg_dst_ar) | IMM(1), sugg_dst_ar));
1669 dst_ar = sugg_dst_ar;
1670 }
1671
1672 if (GET_OPCODE(op) == SLJIT_OR) {
1673 if (DR(TMP_REG2) != dst_ar)
1674 FAIL_IF(push_inst(compiler, ADDU_W | SA(dst_ar) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
1675 return emit_op(compiler, op, CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, dst, dstw, TMP_REG2, 0);
1676 }
1677
1678 if (dst & SLJIT_MEM)
1679 return emit_op_mem(compiler, WORD_DATA, dst_ar, dst, dstw);
1680
1681 if (sugg_dst_ar != dst_ar)
1682 return push_inst(compiler, ADDU_W | SA(dst_ar) | TA(0) | DA(sugg_dst_ar), sugg_dst_ar);
1683 return SLJIT_SUCCESS;
1684 }
1685
1686 struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, int dst, sljit_w dstw, sljit_w init_value)
1687 {
1688 struct sljit_const *const_;
1689 int reg;
1690
1691 CHECK_ERROR_PTR();
1692 check_sljit_emit_const(compiler, dst, dstw, init_value);
1693
1694 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1695 PTR_FAIL_IF(!const_);
1696 set_const(const_, compiler);
1697
1698 reg = (dst >= SLJIT_TEMPORARY_REG1 && dst <= SLJIT_NO_REGISTERS) ? dst : TMP_REG2;
1699
1700 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1701
1702 if (dst & SLJIT_MEM)
1703 PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
1704 return const_;
1705 }

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12