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

  ViewVC Help
Powered by ViewVC 1.1.5