/[pcre]/code/trunk/sljit/sljitNativeSPARC_common.c
ViewVC logotype

Contents of /code/trunk/sljit/sljitNativeSPARC_common.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1222 - (show annotations)
Mon Nov 19 08:04:03 2012 UTC (7 years, 3 months ago) by zherczeg
File MIME type: text/plain
File size: 42736 byte(s)
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 "SPARC" SLJIT_CPUINFO;
30 }
31
32 /* Length of an instruction word
33 Both for sparc-32 and sparc-64 */
34 typedef sljit_ui sljit_ins;
35
36 static void sparc_cache_flush(sljit_ins *from, sljit_ins *to)
37 {
38 if (SLJIT_UNLIKELY(from == to))
39 return;
40
41 do {
42 __asm__ volatile (
43 "flush %0\n"
44 : : "r"(from)
45 );
46 /* Operates at least on doubleword. */
47 from += 2;
48 } while (from < to);
49
50 if (from == to) {
51 /* Flush the last word. */
52 to --;
53 __asm__ volatile (
54 "flush %0\n"
55 : : "r"(to)
56 );
57 }
58 }
59
60 /* TMP_REG2 is not used by getput_arg */
61 #define TMP_REG1 (SLJIT_NO_REGISTERS + 1)
62 #define TMP_REG2 (SLJIT_NO_REGISTERS + 2)
63 #define TMP_REG3 (SLJIT_NO_REGISTERS + 3)
64 #define TMP_REG4 (SLJIT_NO_REGISTERS + 4)
65 #define LINK_REG (SLJIT_NO_REGISTERS + 5)
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 + 7] = {
71 0, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 14, 1, 24, 25, 26, 15
72 };
73
74 /* --------------------------------------------------------------------- */
75 /* Instrucion forms */
76 /* --------------------------------------------------------------------- */
77
78 #define D(d) (reg_map[d] << 25)
79 #define DA(d) ((d) << 25)
80 #define S1(s1) (reg_map[s1] << 14)
81 #define S2(s2) (reg_map[s2])
82 #define S1A(s1) ((s1) << 14)
83 #define S2A(s2) (s2)
84 #define IMM_ARG 0x2000
85 #define DOP(op) ((op) << 5)
86 #define IMM(imm) (((imm) & 0x1fff) | IMM_ARG)
87
88 #define DR(dr) (reg_map[dr])
89 #define OPC1(opcode) ((opcode) << 30)
90 #define OPC2(opcode) ((opcode) << 22)
91 #define OPC3(opcode) ((opcode) << 19)
92 #define SET_FLAGS OPC3(0x10)
93
94 #define ADD (OPC1(0x2) | OPC3(0x00))
95 #define ADDC (OPC1(0x2) | OPC3(0x08))
96 #define AND (OPC1(0x2) | OPC3(0x01))
97 #define ANDN (OPC1(0x2) | OPC3(0x05))
98 #define CALL (OPC1(0x1))
99 #define FABSS (OPC1(0x2) | OPC3(0x34) | DOP(0x09))
100 #define FADDD (OPC1(0x2) | OPC3(0x34) | DOP(0x42))
101 #define FADDS (OPC1(0x2) | OPC3(0x34) | DOP(0x41))
102 #define FCMPD (OPC1(0x2) | OPC3(0x35) | DOP(0x52))
103 #define FCMPS (OPC1(0x2) | OPC3(0x35) | DOP(0x51))
104 #define FDIVD (OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
105 #define FDIVS (OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
106 #define FMOVS (OPC1(0x2) | OPC3(0x34) | DOP(0x01))
107 #define FMULD (OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
108 #define FMULS (OPC1(0x2) | OPC3(0x34) | DOP(0x49))
109 #define FNEGS (OPC1(0x2) | OPC3(0x34) | DOP(0x05))
110 #define FSUBD (OPC1(0x2) | OPC3(0x34) | DOP(0x46))
111 #define FSUBS (OPC1(0x2) | OPC3(0x34) | DOP(0x45))
112 #define JMPL (OPC1(0x2) | OPC3(0x38))
113 #define NOP (OPC1(0x0) | OPC2(0x04))
114 #define OR (OPC1(0x2) | OPC3(0x02))
115 #define ORN (OPC1(0x2) | OPC3(0x06))
116 #define RDY (OPC1(0x2) | OPC3(0x28) | S1A(0))
117 #define RESTORE (OPC1(0x2) | OPC3(0x3d))
118 #define SAVE (OPC1(0x2) | OPC3(0x3c))
119 #define SETHI (OPC1(0x0) | OPC2(0x04))
120 #define SLL (OPC1(0x2) | OPC3(0x25))
121 #define SLLX (OPC1(0x2) | OPC3(0x25) | (1 << 12))
122 #define SRA (OPC1(0x2) | OPC3(0x27))
123 #define SRAX (OPC1(0x2) | OPC3(0x27) | (1 << 12))
124 #define SRL (OPC1(0x2) | OPC3(0x26))
125 #define SRLX (OPC1(0x2) | OPC3(0x26) | (1 << 12))
126 #define SUB (OPC1(0x2) | OPC3(0x04))
127 #define SUBC (OPC1(0x2) | OPC3(0x0c))
128 #define TA (OPC1(0x2) | OPC3(0x3a) | (8 << 25))
129 #define WRY (OPC1(0x2) | OPC3(0x30) | DA(0))
130 #define XOR (OPC1(0x2) | OPC3(0x03))
131 #define XNOR (OPC1(0x2) | OPC3(0x07))
132
133 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
134 #define MAX_DISP (0x1fffff)
135 #define MIN_DISP (-0x200000)
136 #define DISP_MASK (0x3fffff)
137
138 #define BICC (OPC1(0x0) | OPC2(0x2))
139 #define FBFCC (OPC1(0x0) | OPC2(0x6))
140 #define SLL_W SLL
141 #define SDIV (OPC1(0x2) | OPC3(0x0f))
142 #define SMUL (OPC1(0x2) | OPC3(0x0b))
143 #define UDIV (OPC1(0x2) | OPC3(0x0e))
144 #define UMUL (OPC1(0x2) | OPC3(0x0a))
145 #else
146 #define SLL_W SLLX
147 #endif
148
149 #define SIMM_MAX (0x0fff)
150 #define SIMM_MIN (-0x1000)
151
152 /* dest_reg is the absolute name of the register
153 Useful for reordering instructions in the delay slot. */
154 static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_si delay_slot)
155 {
156 sljit_ins *ptr;
157 SLJIT_ASSERT((delay_slot & DST_INS_MASK) == UNMOVABLE_INS
158 || (delay_slot & DST_INS_MASK) == MOVABLE_INS
159 || (delay_slot & DST_INS_MASK) == ((ins >> 25) & 0x1f));
160 ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
161 FAIL_IF(!ptr);
162 *ptr = ins;
163 compiler->size++;
164 compiler->delay_slot = delay_slot;
165 return SLJIT_SUCCESS;
166 }
167
168 static SLJIT_INLINE sljit_ins* optimize_jump(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
169 {
170 sljit_sw diff;
171 sljit_uw target_addr;
172 sljit_ins *inst;
173 sljit_ins saved_inst;
174
175 if (jump->flags & SLJIT_REWRITABLE_JUMP)
176 return code_ptr;
177
178 if (jump->flags & JUMP_ADDR)
179 target_addr = jump->u.target;
180 else {
181 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
182 target_addr = (sljit_uw)(code + jump->u.label->size);
183 }
184 inst = (sljit_ins*)jump->addr;
185
186 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
187 if (jump->flags & IS_CALL) {
188 /* Call is always patchable on sparc 32. */
189 jump->flags |= PATCH_CALL;
190 if (jump->flags & IS_MOVABLE) {
191 inst[0] = inst[-1];
192 inst[-1] = CALL;
193 jump->addr -= sizeof(sljit_ins);
194 return inst;
195 }
196 inst[0] = CALL;
197 inst[1] = NOP;
198 return inst + 1;
199 }
200 #else
201 /* Both calls and BPr instructions shall not pass this point. */
202 #error "Implementation required"
203 #endif
204
205 if (jump->flags & IS_COND)
206 inst--;
207
208 if (jump->flags & IS_MOVABLE) {
209 diff = ((sljit_sw)target_addr - (sljit_sw)(inst - 1)) >> 2;
210 if (diff <= MAX_DISP && diff >= MIN_DISP) {
211 jump->flags |= PATCH_B;
212 inst--;
213 if (jump->flags & IS_COND) {
214 saved_inst = inst[0];
215 inst[0] = inst[1] ^ (1 << 28);
216 inst[1] = saved_inst;
217 } else {
218 inst[1] = inst[0];
219 inst[0] = BICC | DA(0x8);
220 }
221 jump->addr = (sljit_uw)inst;
222 return inst + 1;
223 }
224 }
225
226 diff = ((sljit_sw)target_addr - (sljit_sw)(inst)) >> 2;
227 if (diff <= MAX_DISP && diff >= MIN_DISP) {
228 jump->flags |= PATCH_B;
229 if (jump->flags & IS_COND)
230 inst[0] ^= (1 << 28);
231 else
232 inst[0] = BICC | DA(0x8);
233 inst[1] = NOP;
234 jump->addr = (sljit_uw)inst;
235 return inst + 1;
236 }
237
238 return code_ptr;
239 }
240
241 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
242 {
243 struct sljit_memory_fragment *buf;
244 sljit_ins *code;
245 sljit_ins *code_ptr;
246 sljit_ins *buf_ptr;
247 sljit_ins *buf_end;
248 sljit_uw word_count;
249 sljit_uw addr;
250
251 struct sljit_label *label;
252 struct sljit_jump *jump;
253 struct sljit_const *const_;
254
255 CHECK_ERROR_PTR();
256 check_sljit_generate_code(compiler);
257 reverse_buf(compiler);
258
259 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
260 PTR_FAIL_WITH_EXEC_IF(code);
261 buf = compiler->buf;
262
263 code_ptr = code;
264 word_count = 0;
265 label = compiler->labels;
266 jump = compiler->jumps;
267 const_ = compiler->consts;
268 do {
269 buf_ptr = (sljit_ins*)buf->memory;
270 buf_end = buf_ptr + (buf->used_size >> 2);
271 do {
272 *code_ptr = *buf_ptr++;
273 SLJIT_ASSERT(!label || label->size >= word_count);
274 SLJIT_ASSERT(!jump || jump->addr >= word_count);
275 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
276 /* These structures are ordered by their address. */
277 if (label && label->size == word_count) {
278 /* Just recording the address. */
279 label->addr = (sljit_uw)code_ptr;
280 label->size = code_ptr - code;
281 label = label->next;
282 }
283 if (jump && jump->addr == word_count) {
284 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
285 jump->addr = (sljit_uw)(code_ptr - 3);
286 #else
287 jump->addr = (sljit_uw)(code_ptr - 6);
288 #endif
289 code_ptr = optimize_jump(jump, code_ptr, code);
290 jump = jump->next;
291 }
292 if (const_ && const_->addr == word_count) {
293 /* Just recording the address. */
294 const_->addr = (sljit_uw)code_ptr;
295 const_ = const_->next;
296 }
297 code_ptr ++;
298 word_count ++;
299 } while (buf_ptr < buf_end);
300
301 buf = buf->next;
302 } while (buf);
303
304 if (label && label->size == word_count) {
305 label->addr = (sljit_uw)code_ptr;
306 label->size = code_ptr - code;
307 label = label->next;
308 }
309
310 SLJIT_ASSERT(!label);
311 SLJIT_ASSERT(!jump);
312 SLJIT_ASSERT(!const_);
313 SLJIT_ASSERT(code_ptr - code <= (sljit_si)compiler->size);
314
315 jump = compiler->jumps;
316 while (jump) {
317 do {
318 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
319 buf_ptr = (sljit_ins*)jump->addr;
320
321 if (jump->flags & PATCH_CALL) {
322 addr = (sljit_sw)(addr - jump->addr) >> 2;
323 SLJIT_ASSERT((sljit_sw)addr <= 0x1fffffff && (sljit_sw)addr >= -0x20000000);
324 buf_ptr[0] = CALL | (addr & 0x3fffffff);
325 break;
326 }
327 if (jump->flags & PATCH_B) {
328 addr = (sljit_sw)(addr - jump->addr) >> 2;
329 SLJIT_ASSERT((sljit_sw)addr <= MAX_DISP && (sljit_sw)addr >= MIN_DISP);
330 buf_ptr[0] = (buf_ptr[0] & ~DISP_MASK) | (addr & DISP_MASK);
331 break;
332 }
333
334 /* Set the fields of immediate loads. */
335 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
336 buf_ptr[0] = (buf_ptr[0] & 0xffc00000) | ((addr >> 10) & 0x3fffff);
337 buf_ptr[1] = (buf_ptr[1] & 0xfffffc00) | (addr & 0x3ff);
338 #else
339 #error "Implementation required"
340 #endif
341 } while (0);
342 jump = jump->next;
343 }
344
345
346 compiler->error = SLJIT_ERR_COMPILED;
347 compiler->executable_size = compiler->size * sizeof(sljit_ins);
348 SLJIT_CACHE_FLUSH(code, code_ptr);
349 return code;
350 }
351
352 /* --------------------------------------------------------------------- */
353 /* Entry, exit */
354 /* --------------------------------------------------------------------- */
355
356 /* Creates an index in data_transfer_insts array. */
357 #define LOAD_DATA 0x01
358 #define WORD_DATA 0x00
359 #define BYTE_DATA 0x02
360 #define HALF_DATA 0x04
361 #define INT_DATA 0x06
362 #define SIGNED_DATA 0x08
363 /* Separates integer and floating point registers */
364 #define GPR_REG 0x0f
365 #define DOUBLE_DATA 0x10
366
367 #define MEM_MASK 0x1f
368
369 #define WRITE_BACK 0x00020
370 #define ARG_TEST 0x00040
371 #define ALT_KEEP_CACHE 0x00080
372 #define CUMULATIVE_OP 0x00100
373 #define IMM_OP 0x00200
374 #define SRC2_IMM 0x00400
375
376 #define REG_DEST 0x00800
377 #define REG2_SOURCE 0x01000
378 #define SLOW_SRC1 0x02000
379 #define SLOW_SRC2 0x04000
380 #define SLOW_DEST 0x08000
381
382 /* SET_FLAGS (0x10 << 19) also belong here! */
383
384 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
385 #include "sljitNativeSPARC_32.c"
386 #else
387 #include "sljitNativeSPARC_64.c"
388 #endif
389
390 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)
391 {
392 CHECK_ERROR();
393 check_sljit_emit_enter(compiler, args, scratches, saveds, local_size);
394
395 compiler->scratches = scratches;
396 compiler->saveds = saveds;
397 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
398 compiler->logical_local_size = local_size;
399 #endif
400
401 local_size += 23 * sizeof(sljit_sw);
402 local_size = (local_size + 7) & ~0x7;
403 compiler->local_size = local_size;
404
405 if (local_size <= SIMM_MAX) {
406 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | IMM(-local_size), UNMOVABLE_INS));
407 }
408 else {
409 FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
410 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_LOCALS_REG) | S1(SLJIT_LOCALS_REG) | S2(TMP_REG1), UNMOVABLE_INS));
411 }
412
413 if (args >= 1)
414 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG1) | S1(0) | S2A(24), DR(SLJIT_SAVED_REG1)));
415 if (args >= 2)
416 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG2) | S1(0) | S2A(25), DR(SLJIT_SAVED_REG2)));
417 if (args >= 3)
418 FAIL_IF(push_inst(compiler, OR | D(SLJIT_SAVED_REG3) | S1(0) | S2A(26), DR(SLJIT_SAVED_REG3)));
419
420 return SLJIT_SUCCESS;
421 }
422
423 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)
424 {
425 CHECK_ERROR_VOID();
426 check_sljit_set_context(compiler, args, scratches, saveds, local_size);
427
428 compiler->scratches = scratches;
429 compiler->saveds = saveds;
430 #if (defined SLJIT_DEBUG && SLJIT_DEBUG)
431 compiler->logical_local_size = local_size;
432 #endif
433
434 local_size += 23 * sizeof(sljit_sw);
435 compiler->local_size = (local_size + 7) & ~0x7;
436 }
437
438 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
439 {
440 CHECK_ERROR();
441 check_sljit_emit_return(compiler, op, src, srcw);
442
443 if (op != SLJIT_MOV || !(src <= TMP_REG3)) {
444 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
445 src = SLJIT_SCRATCH_REG1;
446 }
447
448 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
449 return push_inst(compiler, RESTORE | D(SLJIT_SCRATCH_REG1) | S1(src) | S2(0), UNMOVABLE_INS);
450 }
451
452 /* --------------------------------------------------------------------- */
453 /* Operators */
454 /* --------------------------------------------------------------------- */
455
456 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
457 #define ARCH_32_64(a, b) a
458 #else
459 #define ARCH_32_64(a, b) b
460 #endif
461
462 static SLJIT_CONST sljit_ins data_transfer_insts[16 + 4] = {
463 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
464 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
465 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
466 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
467 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
468 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
469 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
470 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
471
472 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
473 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
474 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
475 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
476 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
477 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
478 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
479 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
480
481 /* d s */ OPC1(3) | OPC3(0x27),
482 /* d l */ OPC1(3) | OPC3(0x23),
483 /* s s */ OPC1(3) | OPC3(0x24),
484 /* s l */ OPC1(3) | OPC3(0x20),
485 };
486
487 #undef ARCH_32_64
488
489 /* Can perform an operation using at most 1 instruction. */
490 static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
491 {
492 SLJIT_ASSERT(arg & SLJIT_MEM);
493
494 if (!(flags & WRITE_BACK) || !(arg & 0xf)) {
495 if ((!(arg & 0xf0) && argw <= SIMM_MAX && argw >= SIMM_MIN)
496 || ((arg & 0xf0) && (argw & 0x3) == 0)) {
497 /* Works for both absoulte and relative addresses (immediate case). */
498 if (SLJIT_UNLIKELY(flags & ARG_TEST))
499 return 1;
500 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
501 | ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg))
502 | S1(arg & 0xf) | ((arg & 0xf0) ? S2((arg >> 4) & 0xf) : IMM(argw)),
503 ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
504 return -1;
505 }
506 }
507 return 0;
508 }
509
510 /* See getput_arg below.
511 Note: can_cache is called only for binary operators. Those
512 operators always uses word arguments without write back. */
513 static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
514 {
515 SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
516
517 /* Simple operation except for updates. */
518 if (arg & 0xf0) {
519 argw &= 0x3;
520 SLJIT_ASSERT(argw);
521 next_argw &= 0x3;
522 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == next_argw)
523 return 1;
524 return 0;
525 }
526
527 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
528 return 1;
529 return 0;
530 }
531
532 /* Emit the necessary instructions. See can_cache above. */
533 static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
534 {
535 sljit_si base, arg2, delay_slot;
536 sljit_ins dest;
537
538 SLJIT_ASSERT(arg & SLJIT_MEM);
539 if (!(next_arg & SLJIT_MEM)) {
540 next_arg = 0;
541 next_argw = 0;
542 }
543
544 base = arg & 0xf;
545 if (SLJIT_UNLIKELY(arg & 0xf0)) {
546 argw &= 0x3;
547 SLJIT_ASSERT(argw != 0);
548
549 /* Using the cache. */
550 if (((SLJIT_MEM | (arg & 0xf0)) == compiler->cache_arg) && (argw == compiler->cache_argw))
551 arg2 = TMP_REG3;
552 else {
553 if ((arg & 0xf0) == (next_arg & 0xf0) && argw == (next_argw & 0x3)) {
554 compiler->cache_arg = SLJIT_MEM | (arg & 0xf0);
555 compiler->cache_argw = argw;
556 arg2 = TMP_REG3;
557 }
558 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && (reg << 4) != (arg & 0xf0))
559 arg2 = reg;
560 else /* It must be a mov operation, so tmp1 must be free to use. */
561 arg2 = TMP_REG1;
562 FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1((arg >> 4) & 0xf) | IMM_ARG | argw, DR(arg2)));
563 }
564 }
565 else {
566 /* Using the cache. */
567 if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
568 if (argw != compiler->cache_argw) {
569 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
570 compiler->cache_argw = argw;
571 }
572 arg2 = TMP_REG3;
573 } else {
574 if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
575 compiler->cache_arg = SLJIT_MEM;
576 compiler->cache_argw = argw;
577 arg2 = TMP_REG3;
578 }
579 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
580 arg2 = reg;
581 else /* It must be a mov operation, so tmp1 must be free to use. */
582 arg2 = TMP_REG1;
583 FAIL_IF(load_immediate(compiler, arg2, argw));
584 }
585 }
586
587 dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : DA(reg));
588 delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
589 if (!base)
590 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
591 if (!(flags & WRITE_BACK))
592 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
593 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot));
594 return push_inst(compiler, ADD | D(base) | S1(base) | S2(arg2), DR(base));
595 }
596
597 static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
598 {
599 if (getput_arg_fast(compiler, flags, reg, arg, argw))
600 return compiler->error;
601 compiler->cache_arg = 0;
602 compiler->cache_argw = 0;
603 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
604 }
605
606 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)
607 {
608 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
609 return compiler->error;
610 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
611 }
612
613 static sljit_si emit_op(struct sljit_compiler *compiler, sljit_si op, sljit_si flags,
614 sljit_si dst, sljit_sw dstw,
615 sljit_si src1, sljit_sw src1w,
616 sljit_si src2, sljit_sw src2w)
617 {
618 /* arg1 goes to TMP_REG1 or src reg
619 arg2 goes to TMP_REG2, imm or src reg
620 TMP_REG3 can be used for caching
621 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
622 sljit_si dst_r = TMP_REG2;
623 sljit_si src1_r;
624 sljit_sw src2_r = 0;
625 sljit_si sugg_src2_r = TMP_REG2;
626
627 if (!(flags & ALT_KEEP_CACHE)) {
628 compiler->cache_arg = 0;
629 compiler->cache_argw = 0;
630 }
631
632 if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
633 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
634 return SLJIT_SUCCESS;
635 }
636 else if (dst <= TMP_REG3) {
637 dst_r = dst;
638 flags |= REG_DEST;
639 if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
640 sugg_src2_r = dst_r;
641 }
642 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
643 flags |= SLOW_DEST;
644
645 if (flags & IMM_OP) {
646 if ((src2 & SLJIT_IMM) && src2w) {
647 if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
648 flags |= SRC2_IMM;
649 src2_r = src2w;
650 }
651 }
652 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
653 if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
654 flags |= SRC2_IMM;
655 src2_r = src1w;
656
657 /* And swap arguments. */
658 src1 = src2;
659 src1w = src2w;
660 src2 = SLJIT_IMM;
661 /* src2w = src2_r unneeded. */
662 }
663 }
664 }
665
666 /* Source 1. */
667 if (src1 <= TMP_REG3)
668 src1_r = src1;
669 else if (src1 & SLJIT_IMM) {
670 if (src1w) {
671 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
672 src1_r = TMP_REG1;
673 }
674 else
675 src1_r = 0;
676 }
677 else {
678 if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
679 FAIL_IF(compiler->error);
680 else
681 flags |= SLOW_SRC1;
682 src1_r = TMP_REG1;
683 }
684
685 /* Source 2. */
686 if (src2 <= TMP_REG3) {
687 src2_r = src2;
688 flags |= REG2_SOURCE;
689 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
690 dst_r = src2_r;
691 }
692 else if (src2 & SLJIT_IMM) {
693 if (!(flags & SRC2_IMM)) {
694 if (src2w) {
695 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
696 src2_r = sugg_src2_r;
697 }
698 else {
699 src2_r = 0;
700 if ((op >= SLJIT_MOV && op <= SLJIT_MOVU_SI) && (dst & SLJIT_MEM))
701 dst_r = 0;
702 }
703 }
704 }
705 else {
706 if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
707 FAIL_IF(compiler->error);
708 else
709 flags |= SLOW_SRC2;
710 src2_r = sugg_src2_r;
711 }
712
713 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
714 SLJIT_ASSERT(src2_r == TMP_REG2);
715 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
716 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
717 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
718 }
719 else {
720 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
721 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
722 }
723 }
724 else if (flags & SLOW_SRC1)
725 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
726 else if (flags & SLOW_SRC2)
727 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
728
729 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
730
731 if (dst & SLJIT_MEM) {
732 if (!(flags & SLOW_DEST)) {
733 getput_arg_fast(compiler, flags, dst_r, dst, dstw);
734 return compiler->error;
735 }
736 return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
737 }
738
739 return SLJIT_SUCCESS;
740 }
741
742 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
743 {
744 CHECK_ERROR();
745 check_sljit_emit_op0(compiler, op);
746
747 op = GET_OPCODE(op);
748 switch (op) {
749 case SLJIT_BREAKPOINT:
750 return push_inst(compiler, TA, UNMOVABLE_INS);
751 case SLJIT_NOP:
752 return push_inst(compiler, NOP, UNMOVABLE_INS);
753 case SLJIT_UMUL:
754 case SLJIT_SMUL:
755 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
756 FAIL_IF(push_inst(compiler, (op == SLJIT_UMUL ? UMUL : SMUL) | D(SLJIT_SCRATCH_REG1) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG1)));
757 return push_inst(compiler, RDY | D(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2));
758 #else
759 #error "Implementation required"
760 #endif
761 case SLJIT_UDIV:
762 case SLJIT_SDIV:
763 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
764 if (op == SLJIT_UDIV)
765 FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
766 else {
767 FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_SCRATCH_REG1) | IMM(31), DR(TMP_REG1)));
768 FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
769 }
770 FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_SCRATCH_REG1), DR(TMP_REG2)));
771 FAIL_IF(push_inst(compiler, (op == SLJIT_UDIV ? UDIV : SDIV) | D(SLJIT_SCRATCH_REG1) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG1)));
772 FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_SCRATCH_REG2) | S1(SLJIT_SCRATCH_REG1) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2)));
773 FAIL_IF(push_inst(compiler, SUB | D(SLJIT_SCRATCH_REG2) | S1(TMP_REG2) | S2(SLJIT_SCRATCH_REG2), DR(SLJIT_SCRATCH_REG2)));
774 return SLJIT_SUCCESS;
775 #else
776 #error "Implementation required"
777 #endif
778 }
779
780 return SLJIT_SUCCESS;
781 }
782
783 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
784 sljit_si dst, sljit_sw dstw,
785 sljit_si src, sljit_sw srcw)
786 {
787 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
788
789 CHECK_ERROR();
790 check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw);
791 ADJUST_LOCAL_OFFSET(dst, dstw);
792 ADJUST_LOCAL_OFFSET(src, srcw);
793
794 op = GET_OPCODE(op);
795 switch (op) {
796 case SLJIT_MOV:
797 case SLJIT_MOV_P:
798 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
799
800 case SLJIT_MOV_UI:
801 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
802
803 case SLJIT_MOV_SI:
804 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
805
806 case SLJIT_MOV_UB:
807 return emit_op(compiler, SLJIT_MOV_UB, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_ub)srcw : srcw);
808
809 case SLJIT_MOV_SB:
810 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);
811
812 case SLJIT_MOV_UH:
813 return emit_op(compiler, SLJIT_MOV_UH, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_uh)srcw : srcw);
814
815 case SLJIT_MOV_SH:
816 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);
817
818 case SLJIT_MOVU:
819 case SLJIT_MOVU_P:
820 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
821
822 case SLJIT_MOVU_UI:
823 return emit_op(compiler, SLJIT_MOV_UI, flags | INT_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
824
825 case SLJIT_MOVU_SI:
826 return emit_op(compiler, SLJIT_MOV_SI, flags | INT_DATA | SIGNED_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
827
828 case SLJIT_MOVU_UB:
829 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);
830
831 case SLJIT_MOVU_SB:
832 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);
833
834 case SLJIT_MOVU_UH:
835 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);
836
837 case SLJIT_MOVU_SH:
838 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);
839
840 case SLJIT_NOT:
841 case SLJIT_CLZ:
842 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
843
844 case SLJIT_NEG:
845 return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
846 }
847
848 return SLJIT_SUCCESS;
849 }
850
851 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
852 sljit_si dst, sljit_sw dstw,
853 sljit_si src1, sljit_sw src1w,
854 sljit_si src2, sljit_sw src2w)
855 {
856 sljit_si flags = GET_FLAGS(op) ? SET_FLAGS : 0;
857
858 CHECK_ERROR();
859 check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
860 ADJUST_LOCAL_OFFSET(dst, dstw);
861 ADJUST_LOCAL_OFFSET(src1, src1w);
862 ADJUST_LOCAL_OFFSET(src2, src2w);
863
864 op = GET_OPCODE(op);
865 switch (op) {
866 case SLJIT_ADD:
867 case SLJIT_ADDC:
868 case SLJIT_MUL:
869 case SLJIT_AND:
870 case SLJIT_OR:
871 case SLJIT_XOR:
872 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
873
874 case SLJIT_SUB:
875 case SLJIT_SUBC:
876 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
877
878 case SLJIT_SHL:
879 case SLJIT_LSHR:
880 case SLJIT_ASHR:
881 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
882 if (src2 & SLJIT_IMM)
883 src2w &= 0x1f;
884 #else
885 SLJIT_ASSERT_STOP();
886 #endif
887 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
888 }
889
890 return SLJIT_SUCCESS;
891 }
892
893 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
894 {
895 check_sljit_get_register_index(reg);
896 return reg_map[reg];
897 }
898
899 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
900 void *instruction, sljit_si size)
901 {
902 CHECK_ERROR();
903 check_sljit_emit_op_custom(compiler, instruction, size);
904 SLJIT_ASSERT(size == 4);
905
906 return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
907 }
908
909 /* --------------------------------------------------------------------- */
910 /* Floating point operators */
911 /* --------------------------------------------------------------------- */
912
913 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
914 {
915 return 1;
916 }
917
918 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 7))
919 #define SELECT_FOP(op, single, double) ((op & SLJIT_SINGLE_OP) ? single : double)
920
921 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
922 sljit_si dst, sljit_sw dstw,
923 sljit_si src, sljit_sw srcw)
924 {
925 sljit_si dst_fr;
926
927 CHECK_ERROR();
928 check_sljit_emit_fop1(compiler, op, dst, dstw, src, srcw);
929 SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
930
931 compiler->cache_arg = 0;
932 compiler->cache_argw = 0;
933
934 if (GET_OPCODE(op) == SLJIT_CMPD) {
935 if (dst > SLJIT_FLOAT_REG6) {
936 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, dst, dstw, src, srcw));
937 dst = TMP_FREG1;
938 }
939 else
940 dst <<= 1;
941
942 if (src > SLJIT_FLOAT_REG6) {
943 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src, srcw, 0, 0));
944 src = TMP_FREG2;
945 }
946 else
947 src <<= 1;
948
949 return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | S1A(dst) | S2A(src), FCC_IS_SET | MOVABLE_INS);
950 }
951
952 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG1 : (dst << 1);
953
954 if (src > SLJIT_FLOAT_REG6) {
955 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_fr, src, srcw, dst, dstw));
956 src = dst_fr;
957 }
958 else
959 src <<= 1;
960
961 switch (GET_OPCODE(op)) {
962 case SLJIT_MOVD:
963 if (src != dst_fr && dst_fr != TMP_FREG1) {
964 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr) | S2A(src), MOVABLE_INS));
965 if (!(op & SLJIT_SINGLE_OP))
966 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
967 }
968 break;
969 case SLJIT_NEGD:
970 FAIL_IF(push_inst(compiler, FNEGS | DA(dst_fr) | S2A(src), MOVABLE_INS));
971 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
972 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
973 break;
974 case SLJIT_ABSD:
975 FAIL_IF(push_inst(compiler, FABSS | DA(dst_fr) | S2A(src), MOVABLE_INS));
976 if (dst_fr != src && !(op & SLJIT_SINGLE_OP))
977 FAIL_IF(push_inst(compiler, FMOVS | DA(dst_fr | 1) | S2A(src | 1), MOVABLE_INS));
978 break;
979 }
980
981 if (dst_fr == TMP_FREG1) {
982 if (GET_OPCODE(op) == SLJIT_MOVD)
983 dst_fr = src;
984 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_fr, dst, dstw, 0, 0));
985 }
986
987 return SLJIT_SUCCESS;
988 }
989
990 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
991 sljit_si dst, sljit_sw dstw,
992 sljit_si src1, sljit_sw src1w,
993 sljit_si src2, sljit_sw src2w)
994 {
995 sljit_si dst_fr, flags = 0;
996
997 CHECK_ERROR();
998 check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w);
999
1000 compiler->cache_arg = 0;
1001 compiler->cache_argw = 0;
1002
1003 dst_fr = (dst > SLJIT_FLOAT_REG6) ? TMP_FREG2 : (dst << 1);
1004
1005 if (src1 > SLJIT_FLOAT_REG6) {
1006 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1007 FAIL_IF(compiler->error);
1008 src1 = TMP_FREG1;
1009 } else
1010 flags |= SLOW_SRC1;
1011 }
1012 else
1013 src1 <<= 1;
1014
1015 if (src2 > SLJIT_FLOAT_REG6) {
1016 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1017 FAIL_IF(compiler->error);
1018 src2 = TMP_FREG2;
1019 } else
1020 flags |= SLOW_SRC2;
1021 }
1022 else
1023 src2 <<= 1;
1024
1025 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1026 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1027 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1028 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1029 }
1030 else {
1031 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1032 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1033 }
1034 }
1035 else if (flags & SLOW_SRC1)
1036 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1037 else if (flags & SLOW_SRC2)
1038 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1039
1040 if (flags & SLOW_SRC1)
1041 src1 = TMP_FREG1;
1042 if (flags & SLOW_SRC2)
1043 src2 = TMP_FREG2;
1044
1045 switch (GET_OPCODE(op)) {
1046 case SLJIT_ADDD:
1047 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1048 break;
1049
1050 case SLJIT_SUBD:
1051 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1052 break;
1053
1054 case SLJIT_MULD:
1055 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1056 break;
1057
1058 case SLJIT_DIVD:
1059 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | DA(dst_fr) | S1A(src1) | S2A(src2), MOVABLE_INS));
1060 break;
1061 }
1062
1063 if (dst_fr == TMP_FREG2)
1064 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1065
1066 return SLJIT_SUCCESS;
1067 }
1068
1069 #undef FLOAT_DATA
1070 #undef SELECT_FOP
1071
1072 /* --------------------------------------------------------------------- */
1073 /* Other instructions */
1074 /* --------------------------------------------------------------------- */
1075
1076 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
1077 {
1078 CHECK_ERROR();
1079 check_sljit_emit_fast_enter(compiler, dst, dstw);
1080 ADJUST_LOCAL_OFFSET(dst, dstw);
1081
1082 /* For UNUSED dst. Uncommon, but possible. */
1083 if (dst == SLJIT_UNUSED)
1084 return SLJIT_SUCCESS;
1085
1086 if (dst <= TMP_REG3)
1087 return push_inst(compiler, OR | D(dst) | S1(0) | S2(LINK_REG), DR(dst));
1088
1089 /* Memory. */
1090 return emit_op_mem(compiler, WORD_DATA, LINK_REG, dst, dstw);
1091 }
1092
1093 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
1094 {
1095 CHECK_ERROR();
1096 check_sljit_emit_fast_return(compiler, src, srcw);
1097 ADJUST_LOCAL_OFFSET(src, srcw);
1098
1099 if (src <= TMP_REG3)
1100 FAIL_IF(push_inst(compiler, OR | D(LINK_REG) | S1(0) | S2(src), DR(LINK_REG)));
1101 else if (src & SLJIT_MEM)
1102 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, LINK_REG, src, srcw));
1103 else if (src & SLJIT_IMM)
1104 FAIL_IF(load_immediate(compiler, LINK_REG, srcw));
1105
1106 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(LINK_REG) | IMM(8), UNMOVABLE_INS));
1107 return push_inst(compiler, NOP, UNMOVABLE_INS);
1108 }
1109
1110 /* --------------------------------------------------------------------- */
1111 /* Conditional instructions */
1112 /* --------------------------------------------------------------------- */
1113
1114 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1115 {
1116 struct sljit_label *label;
1117
1118 CHECK_ERROR_PTR();
1119 check_sljit_emit_label(compiler);
1120
1121 if (compiler->last_label && compiler->last_label->size == compiler->size)
1122 return compiler->last_label;
1123
1124 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1125 PTR_FAIL_IF(!label);
1126 set_label(label, compiler);
1127 compiler->delay_slot = UNMOVABLE_INS;
1128 return label;
1129 }
1130
1131 static sljit_ins get_cc(sljit_si type)
1132 {
1133 switch (type) {
1134 case SLJIT_C_EQUAL:
1135 case SLJIT_C_MUL_NOT_OVERFLOW:
1136 return DA(0x1);
1137
1138 case SLJIT_C_NOT_EQUAL:
1139 case SLJIT_C_MUL_OVERFLOW:
1140 return DA(0x9);
1141
1142 case SLJIT_C_LESS:
1143 return DA(0x5);
1144
1145 case SLJIT_C_GREATER_EQUAL:
1146 return DA(0xd);
1147
1148 case SLJIT_C_GREATER:
1149 return DA(0xc);
1150
1151 case SLJIT_C_LESS_EQUAL:
1152 return DA(0x4);
1153
1154 case SLJIT_C_SIG_LESS:
1155 return DA(0x3);
1156
1157 case SLJIT_C_SIG_GREATER_EQUAL:
1158 return DA(0xb);
1159
1160 case SLJIT_C_SIG_GREATER:
1161 return DA(0xa);
1162
1163 case SLJIT_C_SIG_LESS_EQUAL:
1164 return DA(0x2);
1165
1166 case SLJIT_C_OVERFLOW:
1167 return DA(0x7);
1168
1169 case SLJIT_C_NOT_OVERFLOW:
1170 return DA(0xf);
1171
1172 case SLJIT_C_FLOAT_EQUAL:
1173 return DA(0x9);
1174
1175 case SLJIT_C_FLOAT_NOT_EQUAL: /* Unordered. */
1176 return DA(0x1);
1177
1178 case SLJIT_C_FLOAT_LESS:
1179 return DA(0x4);
1180
1181 case SLJIT_C_FLOAT_GREATER_EQUAL: /* Unordered. */
1182 return DA(0xc);
1183
1184 case SLJIT_C_FLOAT_LESS_EQUAL:
1185 return DA(0xd);
1186
1187 case SLJIT_C_FLOAT_GREATER: /* Unordered. */
1188 return DA(0x5);
1189
1190 case SLJIT_C_FLOAT_UNORDERED:
1191 return DA(0x7);
1192
1193 case SLJIT_C_FLOAT_ORDERED:
1194 return DA(0xf);
1195
1196 default:
1197 SLJIT_ASSERT_STOP();
1198 return DA(0x8);
1199 }
1200 }
1201
1202 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
1203 {
1204 struct sljit_jump *jump;
1205
1206 CHECK_ERROR_PTR();
1207 check_sljit_emit_jump(compiler, type);
1208
1209 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1210 PTR_FAIL_IF(!jump);
1211 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1212 type &= 0xff;
1213
1214 if (type < SLJIT_C_FLOAT_EQUAL) {
1215 jump->flags |= IS_COND;
1216 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1217 jump->flags |= IS_MOVABLE;
1218 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1219 PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1220 #else
1221 #error "Implementation required"
1222 #endif
1223 }
1224 else if (type < SLJIT_JUMP) {
1225 jump->flags |= IS_COND;
1226 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1227 jump->flags |= IS_MOVABLE;
1228 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1229 PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1230 #else
1231 #error "Implementation required"
1232 #endif
1233 } else {
1234 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1235 jump->flags |= IS_MOVABLE;
1236 if (type >= SLJIT_FAST_CALL)
1237 jump->flags |= IS_CALL;
1238 }
1239
1240 PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1241 PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(TMP_REG2) | IMM(0), UNMOVABLE_INS));
1242 jump->addr = compiler->size;
1243 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1244
1245 return jump;
1246 }
1247
1248 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
1249 {
1250 struct sljit_jump *jump = NULL;
1251 sljit_si src_r;
1252
1253 CHECK_ERROR();
1254 check_sljit_emit_ijump(compiler, type, src, srcw);
1255 ADJUST_LOCAL_OFFSET(src, srcw);
1256
1257 if (src <= TMP_REG3)
1258 src_r = src;
1259 else if (src & SLJIT_IMM) {
1260 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1261 FAIL_IF(!jump);
1262 set_jump(jump, compiler, JUMP_ADDR);
1263 jump->u.target = srcw;
1264 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1265 jump->flags |= IS_MOVABLE;
1266 if (type >= SLJIT_FAST_CALL)
1267 jump->flags |= IS_CALL;
1268
1269 FAIL_IF(emit_const(compiler, TMP_REG2, 0));
1270 src_r = TMP_REG2;
1271 }
1272 else {
1273 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG2, src, srcw));
1274 src_r = TMP_REG2;
1275 }
1276
1277 FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? LINK_REG : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1278 if (jump)
1279 jump->addr = compiler->size;
1280 return push_inst(compiler, NOP, UNMOVABLE_INS);
1281 }
1282
1283 SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
1284 sljit_si dst, sljit_sw dstw,
1285 sljit_si src, sljit_sw srcw,
1286 sljit_si type)
1287 {
1288 sljit_si reg, flags = (GET_FLAGS(op) ? SET_FLAGS : 0);
1289
1290 CHECK_ERROR();
1291 check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type);
1292 ADJUST_LOCAL_OFFSET(dst, dstw);
1293
1294 if (dst == SLJIT_UNUSED)
1295 return SLJIT_SUCCESS;
1296
1297 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1298 op = GET_OPCODE(op);
1299 reg = (op < SLJIT_ADD && dst <= TMP_REG3) ? dst : TMP_REG2;
1300
1301 compiler->cache_arg = 0;
1302 compiler->cache_argw = 0;
1303 if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
1304 ADJUST_LOCAL_OFFSET(src, srcw);
1305 FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
1306 src = TMP_REG1;
1307 srcw = 0;
1308 }
1309
1310 if (type < SLJIT_C_FLOAT_EQUAL)
1311 FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1312 else
1313 FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1314
1315 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1316 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1317
1318 if (op >= SLJIT_ADD)
1319 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE, dst, dstw, src, srcw, TMP_REG2, 0);
1320
1321 return (reg == TMP_REG2) ? emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw) : SLJIT_SUCCESS;
1322 #else
1323 #error "Implementation required"
1324 #endif
1325 }
1326
1327 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
1328 {
1329 sljit_si reg;
1330 struct sljit_const *const_;
1331
1332 CHECK_ERROR_PTR();
1333 check_sljit_emit_const(compiler, dst, dstw, init_value);
1334 ADJUST_LOCAL_OFFSET(dst, dstw);
1335
1336 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1337 PTR_FAIL_IF(!const_);
1338 set_const(const_, compiler);
1339
1340 reg = (dst <= TMP_REG3) ? dst : TMP_REG2;
1341
1342 PTR_FAIL_IF(emit_const(compiler, reg, init_value));
1343
1344 if (dst & SLJIT_MEM)
1345 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1346
1347 return const_;
1348 }

  ViewVC Help
Powered by ViewVC 1.1.5