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

  ViewVC Help
Powered by ViewVC 1.1.5