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

  ViewVC Help
Powered by ViewVC 1.1.5