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

  ViewVC Help
Powered by ViewVC 1.1.5