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

  ViewVC Help
Powered by ViewVC 1.1.5