1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright 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
sljit_get_platform_name(void)27 SLJIT_API_FUNC_ATTRIBUTE 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_u32 sljit_ins;
35
36 #if (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL)
37
sparc_cache_flush(sljit_ins * from,sljit_ins * to)38 static void sparc_cache_flush(sljit_ins *from, sljit_ins *to)
39 {
40 #if defined(__SUNPRO_C) && __SUNPRO_C < 0x590
41 __asm (
42 /* if (from == to) return */
43 "cmp %i0, %i1\n"
44 "be .leave\n"
45 "nop\n"
46
47 /* loop until from >= to */
48 ".mainloop:\n"
49 "flush %i0\n"
50 "add %i0, 8, %i0\n"
51 "cmp %i0, %i1\n"
52 "bcs .mainloop\n"
53 "nop\n"
54
55 /* The comparison was done above. */
56 "bne .leave\n"
57 /* nop is not necessary here, since the
58 sub operation has no side effect. */
59 "sub %i0, 4, %i0\n"
60 "flush %i0\n"
61 ".leave:"
62 );
63 #else
64 if (SLJIT_UNLIKELY(from == to))
65 return;
66
67 do {
68 __asm__ volatile (
69 "flush %0\n"
70 : : "r"(from)
71 );
72 /* Operates at least on doubleword. */
73 from += 2;
74 } while (from < to);
75
76 if (from == to) {
77 /* Flush the last word. */
78 from --;
79 __asm__ volatile (
80 "flush %0\n"
81 : : "r"(from)
82 );
83 }
84 #endif
85 }
86
87 #endif /* (defined SLJIT_CACHE_FLUSH_OWN_IMPL && SLJIT_CACHE_FLUSH_OWN_IMPL) */
88
89 /* TMP_REG2 is not used by getput_arg */
90 #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
91 #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
92 #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
93 /* This register is modified by calls, which affects the instruction
94 in the delay slot if it is used as a source register. */
95 #define TMP_LINK (SLJIT_NUMBER_OF_REGISTERS + 5)
96
97 #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
98 #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
99
100 static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 6] = {
101 0, 8, 9, 10, 11, 29, 28, 27, 23, 22, 21, 20, 19, 18, 17, 16, 26, 25, 24, 14, 1, 12, 13, 15
102 };
103
104 static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
105 0, 0, 2, 4, 6, 8, 10, 12, 14
106 };
107
108 /* --------------------------------------------------------------------- */
109 /* Instrucion forms */
110 /* --------------------------------------------------------------------- */
111
112 #define D(d) (reg_map[d] << 25)
113 #define FD(d) (freg_map[d] << 25)
114 #define FDN(d) ((freg_map[d] | 0x1) << 25)
115 #define DA(d) ((d) << 25)
116 #define S1(s1) (reg_map[s1] << 14)
117 #define FS1(s1) (freg_map[s1] << 14)
118 #define S1A(s1) ((s1) << 14)
119 #define S2(s2) (reg_map[s2])
120 #define FS2(s2) (freg_map[s2])
121 #define FS2N(s2) (freg_map[s2] | 0x1)
122 #define S2A(s2) (s2)
123 #define IMM_ARG 0x2000
124 #define DOP(op) ((op) << 5)
125 #define IMM(imm) (((imm) & 0x1fff) | IMM_ARG)
126
127 #define DR(dr) (reg_map[dr])
128 #define OPC1(opcode) ((opcode) << 30)
129 #define OPC2(opcode) ((opcode) << 22)
130 #define OPC3(opcode) ((opcode) << 19)
131 #define SET_FLAGS OPC3(0x10)
132
133 #define ADD (OPC1(0x2) | OPC3(0x00))
134 #define ADDC (OPC1(0x2) | OPC3(0x08))
135 #define AND (OPC1(0x2) | OPC3(0x01))
136 #define ANDN (OPC1(0x2) | OPC3(0x05))
137 #define CALL (OPC1(0x1))
138 #define FABSS (OPC1(0x2) | OPC3(0x34) | DOP(0x09))
139 #define FADDD (OPC1(0x2) | OPC3(0x34) | DOP(0x42))
140 #define FADDS (OPC1(0x2) | OPC3(0x34) | DOP(0x41))
141 #define FCMPD (OPC1(0x2) | OPC3(0x35) | DOP(0x52))
142 #define FCMPS (OPC1(0x2) | OPC3(0x35) | DOP(0x51))
143 #define FDIVD (OPC1(0x2) | OPC3(0x34) | DOP(0x4e))
144 #define FDIVS (OPC1(0x2) | OPC3(0x34) | DOP(0x4d))
145 #define FDTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd2))
146 #define FDTOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc6))
147 #define FITOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc8))
148 #define FITOS (OPC1(0x2) | OPC3(0x34) | DOP(0xc4))
149 #define FMOVS (OPC1(0x2) | OPC3(0x34) | DOP(0x01))
150 #define FMULD (OPC1(0x2) | OPC3(0x34) | DOP(0x4a))
151 #define FMULS (OPC1(0x2) | OPC3(0x34) | DOP(0x49))
152 #define FNEGS (OPC1(0x2) | OPC3(0x34) | DOP(0x05))
153 #define FSTOD (OPC1(0x2) | OPC3(0x34) | DOP(0xc9))
154 #define FSTOI (OPC1(0x2) | OPC3(0x34) | DOP(0xd1))
155 #define FSUBD (OPC1(0x2) | OPC3(0x34) | DOP(0x46))
156 #define FSUBS (OPC1(0x2) | OPC3(0x34) | DOP(0x45))
157 #define JMPL (OPC1(0x2) | OPC3(0x38))
158 #define LDD (OPC1(0x3) | OPC3(0x03))
159 #define LDUW (OPC1(0x3) | OPC3(0x00))
160 #define NOP (OPC1(0x0) | OPC2(0x04))
161 #define OR (OPC1(0x2) | OPC3(0x02))
162 #define ORN (OPC1(0x2) | OPC3(0x06))
163 #define RDY (OPC1(0x2) | OPC3(0x28) | S1A(0))
164 #define RESTORE (OPC1(0x2) | OPC3(0x3d))
165 #define SAVE (OPC1(0x2) | OPC3(0x3c))
166 #define SETHI (OPC1(0x0) | OPC2(0x04))
167 #define SLL (OPC1(0x2) | OPC3(0x25))
168 #define SLLX (OPC1(0x2) | OPC3(0x25) | (1 << 12))
169 #define SRA (OPC1(0x2) | OPC3(0x27))
170 #define SRAX (OPC1(0x2) | OPC3(0x27) | (1 << 12))
171 #define SRL (OPC1(0x2) | OPC3(0x26))
172 #define SRLX (OPC1(0x2) | OPC3(0x26) | (1 << 12))
173 #define STDF (OPC1(0x3) | OPC3(0x27))
174 #define STF (OPC1(0x3) | OPC3(0x24))
175 #define STW (OPC1(0x3) | OPC3(0x04))
176 #define SUB (OPC1(0x2) | OPC3(0x04))
177 #define SUBC (OPC1(0x2) | OPC3(0x0c))
178 #define TA (OPC1(0x2) | OPC3(0x3a) | (8 << 25))
179 #define WRY (OPC1(0x2) | OPC3(0x30) | DA(0))
180 #define XOR (OPC1(0x2) | OPC3(0x03))
181 #define XNOR (OPC1(0x2) | OPC3(0x07))
182
183 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
184 #define MAX_DISP (0x1fffff)
185 #define MIN_DISP (-0x200000)
186 #define DISP_MASK (0x3fffff)
187
188 #define BICC (OPC1(0x0) | OPC2(0x2))
189 #define FBFCC (OPC1(0x0) | OPC2(0x6))
190 #define SLL_W SLL
191 #define SDIV (OPC1(0x2) | OPC3(0x0f))
192 #define SMUL (OPC1(0x2) | OPC3(0x0b))
193 #define UDIV (OPC1(0x2) | OPC3(0x0e))
194 #define UMUL (OPC1(0x2) | OPC3(0x0a))
195 #else
196 #define SLL_W SLLX
197 #endif
198
199 #define SIMM_MAX (0x0fff)
200 #define SIMM_MIN (-0x1000)
201
202 /* dest_reg is the absolute name of the register
203 Useful for reordering instructions in the delay slot. */
push_inst(struct sljit_compiler * compiler,sljit_ins ins,sljit_s32 delay_slot)204 static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_s32 delay_slot)
205 {
206 sljit_ins *ptr;
207 SLJIT_ASSERT((delay_slot & DST_INS_MASK) == UNMOVABLE_INS
208 || (delay_slot & DST_INS_MASK) == MOVABLE_INS
209 || (delay_slot & DST_INS_MASK) == ((ins >> 25) & 0x1f));
210 ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
211 FAIL_IF(!ptr);
212 *ptr = ins;
213 compiler->size++;
214 compiler->delay_slot = delay_slot;
215 return SLJIT_SUCCESS;
216 }
217
detect_jump_type(struct sljit_jump * jump,sljit_ins * code_ptr,sljit_ins * code,sljit_sw executable_offset)218 static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
219 {
220 sljit_sw diff;
221 sljit_uw target_addr;
222 sljit_ins *inst;
223 sljit_ins saved_inst;
224
225 if (jump->flags & SLJIT_REWRITABLE_JUMP)
226 return code_ptr;
227
228 if (jump->flags & JUMP_ADDR)
229 target_addr = jump->u.target;
230 else {
231 SLJIT_ASSERT(jump->flags & JUMP_LABEL);
232 target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
233 }
234 inst = (sljit_ins*)jump->addr;
235
236 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
237 if (jump->flags & IS_CALL) {
238 /* Call is always patchable on sparc 32. */
239 jump->flags |= PATCH_CALL;
240 if (jump->flags & IS_MOVABLE) {
241 inst[0] = inst[-1];
242 inst[-1] = CALL;
243 jump->addr -= sizeof(sljit_ins);
244 return inst;
245 }
246 inst[0] = CALL;
247 inst[1] = NOP;
248 return inst + 1;
249 }
250 #else
251 /* Both calls and BPr instructions shall not pass this point. */
252 #error "Implementation required"
253 #endif
254
255 if (jump->flags & IS_COND)
256 inst--;
257
258 diff = ((sljit_sw)target_addr - (sljit_sw)(inst - 1) - executable_offset) >> 2;
259
260 if (jump->flags & IS_MOVABLE) {
261 if (diff <= MAX_DISP && diff >= MIN_DISP) {
262 jump->flags |= PATCH_B;
263 inst--;
264 if (jump->flags & IS_COND) {
265 saved_inst = inst[0];
266 inst[0] = inst[1] ^ (1 << 28);
267 inst[1] = saved_inst;
268 } else {
269 inst[1] = inst[0];
270 inst[0] = BICC | DA(0x8);
271 }
272 jump->addr = (sljit_uw)inst;
273 return inst + 1;
274 }
275 }
276
277 diff += sizeof(sljit_ins);
278
279 if (diff <= MAX_DISP && diff >= MIN_DISP) {
280 jump->flags |= PATCH_B;
281 if (jump->flags & IS_COND)
282 inst[0] ^= (1 << 28);
283 else
284 inst[0] = BICC | DA(0x8);
285 inst[1] = NOP;
286 jump->addr = (sljit_uw)inst;
287 return inst + 1;
288 }
289
290 return code_ptr;
291 }
292
sljit_generate_code(struct sljit_compiler * compiler)293 SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
294 {
295 struct sljit_memory_fragment *buf;
296 sljit_ins *code;
297 sljit_ins *code_ptr;
298 sljit_ins *buf_ptr;
299 sljit_ins *buf_end;
300 sljit_uw word_count;
301 sljit_uw next_addr;
302 sljit_sw executable_offset;
303 sljit_uw addr;
304
305 struct sljit_label *label;
306 struct sljit_jump *jump;
307 struct sljit_const *const_;
308 struct sljit_put_label *put_label;
309
310 CHECK_ERROR_PTR();
311 CHECK_PTR(check_sljit_generate_code(compiler));
312 reverse_buf(compiler);
313
314 code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins), compiler->exec_allocator_data);
315 PTR_FAIL_WITH_EXEC_IF(code);
316 buf = compiler->buf;
317
318 code_ptr = code;
319 word_count = 0;
320 next_addr = 0;
321 executable_offset = SLJIT_EXEC_OFFSET(code);
322
323 label = compiler->labels;
324 jump = compiler->jumps;
325 const_ = compiler->consts;
326 put_label = compiler->put_labels;
327
328 do {
329 buf_ptr = (sljit_ins*)buf->memory;
330 buf_end = buf_ptr + (buf->used_size >> 2);
331 do {
332 *code_ptr = *buf_ptr++;
333 if (next_addr == word_count) {
334 SLJIT_ASSERT(!label || label->size >= word_count);
335 SLJIT_ASSERT(!jump || jump->addr >= word_count);
336 SLJIT_ASSERT(!const_ || const_->addr >= word_count);
337 SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
338
339 /* These structures are ordered by their address. */
340 if (label && label->size == word_count) {
341 /* Just recording the address. */
342 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
343 label->size = code_ptr - code;
344 label = label->next;
345 }
346 if (jump && jump->addr == word_count) {
347 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
348 jump->addr = (sljit_uw)(code_ptr - 3);
349 #else
350 jump->addr = (sljit_uw)(code_ptr - 6);
351 #endif
352 code_ptr = detect_jump_type(jump, code_ptr, code, executable_offset);
353 jump = jump->next;
354 }
355 if (const_ && const_->addr == word_count) {
356 /* Just recording the address. */
357 const_->addr = (sljit_uw)code_ptr;
358 const_ = const_->next;
359 }
360 if (put_label && put_label->addr == word_count) {
361 SLJIT_ASSERT(put_label->label);
362 put_label->addr = (sljit_uw)code_ptr;
363 put_label = put_label->next;
364 }
365 next_addr = compute_next_addr(label, jump, const_, put_label);
366 }
367 code_ptr ++;
368 word_count ++;
369 } while (buf_ptr < buf_end);
370
371 buf = buf->next;
372 } while (buf);
373
374 if (label && label->size == word_count) {
375 label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
376 label->size = code_ptr - code;
377 label = label->next;
378 }
379
380 SLJIT_ASSERT(!label);
381 SLJIT_ASSERT(!jump);
382 SLJIT_ASSERT(!const_);
383 SLJIT_ASSERT(!put_label);
384 SLJIT_ASSERT(code_ptr - code <= (sljit_s32)compiler->size);
385
386 jump = compiler->jumps;
387 while (jump) {
388 do {
389 addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
390 buf_ptr = (sljit_ins *)jump->addr;
391
392 if (jump->flags & PATCH_CALL) {
393 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
394 SLJIT_ASSERT((sljit_sw)addr <= 0x1fffffff && (sljit_sw)addr >= -0x20000000);
395 buf_ptr[0] = CALL | (addr & 0x3fffffff);
396 break;
397 }
398 if (jump->flags & PATCH_B) {
399 addr = (sljit_sw)(addr - (sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset)) >> 2;
400 SLJIT_ASSERT((sljit_sw)addr <= MAX_DISP && (sljit_sw)addr >= MIN_DISP);
401 buf_ptr[0] = (buf_ptr[0] & ~DISP_MASK) | (addr & DISP_MASK);
402 break;
403 }
404
405 /* Set the fields of immediate loads. */
406 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
407 SLJIT_ASSERT(((buf_ptr[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr[1] & 0xc1f83fff) == 0x80102000));
408 buf_ptr[0] |= (addr >> 10) & 0x3fffff;
409 buf_ptr[1] |= addr & 0x3ff;
410 #else
411 #error "Implementation required"
412 #endif
413 } while (0);
414 jump = jump->next;
415 }
416
417 put_label = compiler->put_labels;
418 while (put_label) {
419 addr = put_label->label->addr;
420 buf_ptr = (sljit_ins *)put_label->addr;
421
422 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
423 SLJIT_ASSERT(((buf_ptr[0] & 0xc1cfffff) == 0x01000000) && ((buf_ptr[1] & 0xc1f83fff) == 0x80102000));
424 buf_ptr[0] |= (addr >> 10) & 0x3fffff;
425 buf_ptr[1] |= addr & 0x3ff;
426 #else
427 #error "Implementation required"
428 #endif
429 put_label = put_label->next;
430 }
431
432 compiler->error = SLJIT_ERR_COMPILED;
433 compiler->executable_offset = executable_offset;
434 compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
435
436 code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
437 code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
438
439 SLJIT_CACHE_FLUSH(code, code_ptr);
440 SLJIT_UPDATE_WX_FLAGS(code, code_ptr, 1);
441 return code;
442 }
443
sljit_has_cpu_feature(sljit_s32 feature_type)444 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
445 {
446 switch (feature_type) {
447 case SLJIT_HAS_FPU:
448 #ifdef SLJIT_IS_FPU_AVAILABLE
449 return SLJIT_IS_FPU_AVAILABLE;
450 #else
451 /* Available by default. */
452 return 1;
453 #endif
454
455 case SLJIT_HAS_ZERO_REGISTER:
456 return 1;
457
458 #if (defined SLJIT_CONFIG_SPARC_64 && SLJIT_CONFIG_SPARC_64)
459 case SLJIT_HAS_CMOV:
460 return 1;
461 #endif
462
463 default:
464 return 0;
465 }
466 }
467
468 /* --------------------------------------------------------------------- */
469 /* Entry, exit */
470 /* --------------------------------------------------------------------- */
471
472 /* Creates an index in data_transfer_insts array. */
473 #define LOAD_DATA 0x01
474 #define WORD_DATA 0x00
475 #define BYTE_DATA 0x02
476 #define HALF_DATA 0x04
477 #define INT_DATA 0x06
478 #define SIGNED_DATA 0x08
479 /* Separates integer and floating point registers */
480 #define GPR_REG 0x0f
481 #define DOUBLE_DATA 0x10
482 #define SINGLE_DATA 0x12
483
484 #define MEM_MASK 0x1f
485
486 #define ARG_TEST 0x00020
487 #define ALT_KEEP_CACHE 0x00040
488 #define CUMULATIVE_OP 0x00080
489 #define IMM_OP 0x00100
490 #define SRC2_IMM 0x00200
491
492 #define REG_DEST 0x00400
493 #define REG2_SOURCE 0x00800
494 #define SLOW_SRC1 0x01000
495 #define SLOW_SRC2 0x02000
496 #define SLOW_DEST 0x04000
497
498 /* SET_FLAGS (0x10 << 19) also belong here! */
499
500 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
501 #include "sljitNativeSPARC_32.c"
502 #else
503 #include "sljitNativeSPARC_64.c"
504 #endif
505
sljit_emit_enter(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)506 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
507 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
508 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
509 {
510 CHECK_ERROR();
511 CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
512 set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
513
514 local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
515 compiler->local_size = local_size;
516
517 if (local_size <= SIMM_MAX) {
518 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | IMM(-local_size), UNMOVABLE_INS));
519 }
520 else {
521 FAIL_IF(load_immediate(compiler, TMP_REG1, -local_size));
522 FAIL_IF(push_inst(compiler, SAVE | D(SLJIT_SP) | S1(SLJIT_SP) | S2(TMP_REG1), UNMOVABLE_INS));
523 }
524
525 /* Arguments are in their appropriate registers. */
526
527 return SLJIT_SUCCESS;
528 }
529
sljit_set_context(struct sljit_compiler * compiler,sljit_s32 options,sljit_s32 arg_types,sljit_s32 scratches,sljit_s32 saveds,sljit_s32 fscratches,sljit_s32 fsaveds,sljit_s32 local_size)530 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
531 sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
532 sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
533 {
534 CHECK_ERROR();
535 CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
536 set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
537
538 compiler->local_size = (local_size + SLJIT_LOCALS_OFFSET + 7) & ~0x7;
539 return SLJIT_SUCCESS;
540 }
541
sljit_emit_return(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)542 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
543 {
544 CHECK_ERROR();
545 CHECK(check_sljit_emit_return(compiler, op, src, srcw));
546
547 if (op != SLJIT_MOV || !FAST_IS_REG(src)) {
548 FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
549 src = SLJIT_R0;
550 }
551
552 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1A(31) | IMM(8), UNMOVABLE_INS));
553 return push_inst(compiler, RESTORE | D(SLJIT_R0) | S1(src) | S2(0), UNMOVABLE_INS);
554 }
555
556 /* --------------------------------------------------------------------- */
557 /* Operators */
558 /* --------------------------------------------------------------------- */
559
560 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
561 #define ARCH_32_64(a, b) a
562 #else
563 #define ARCH_32_64(a, b) b
564 #endif
565
566 static const sljit_ins data_transfer_insts[16 + 4] = {
567 /* u w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
568 /* u w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
569 /* u b s */ OPC1(3) | OPC3(0x05) /* stb */,
570 /* u b l */ OPC1(3) | OPC3(0x01) /* ldub */,
571 /* u h s */ OPC1(3) | OPC3(0x06) /* sth */,
572 /* u h l */ OPC1(3) | OPC3(0x02) /* lduh */,
573 /* u i s */ OPC1(3) | OPC3(0x04) /* stw */,
574 /* u i l */ OPC1(3) | OPC3(0x00) /* lduw */,
575
576 /* s w s */ ARCH_32_64(OPC1(3) | OPC3(0x04) /* stw */, OPC1(3) | OPC3(0x0e) /* stx */),
577 /* s w l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x0b) /* ldx */),
578 /* s b s */ OPC1(3) | OPC3(0x05) /* stb */,
579 /* s b l */ OPC1(3) | OPC3(0x09) /* ldsb */,
580 /* s h s */ OPC1(3) | OPC3(0x06) /* sth */,
581 /* s h l */ OPC1(3) | OPC3(0x0a) /* ldsh */,
582 /* s i s */ OPC1(3) | OPC3(0x04) /* stw */,
583 /* s i l */ ARCH_32_64(OPC1(3) | OPC3(0x00) /* lduw */, OPC1(3) | OPC3(0x08) /* ldsw */),
584
585 /* d s */ OPC1(3) | OPC3(0x27),
586 /* d l */ OPC1(3) | OPC3(0x23),
587 /* s s */ OPC1(3) | OPC3(0x24),
588 /* s l */ OPC1(3) | OPC3(0x20),
589 };
590
591 #undef ARCH_32_64
592
593 /* Can perform an operation using at most 1 instruction. */
getput_arg_fast(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)594 static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
595 {
596 SLJIT_ASSERT(arg & SLJIT_MEM);
597
598 if ((!(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN)
599 || ((arg & OFFS_REG_MASK) && (argw & 0x3) == 0)) {
600 /* Works for both absoulte and relative addresses (immediate case). */
601 if (SLJIT_UNLIKELY(flags & ARG_TEST))
602 return 1;
603 FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK]
604 | ((flags & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg))
605 | S1(arg & REG_MASK) | ((arg & OFFS_REG_MASK) ? S2(OFFS_REG(arg)) : IMM(argw)),
606 ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS));
607 return -1;
608 }
609 return 0;
610 }
611
612 /* See getput_arg below.
613 Note: can_cache is called only for binary operators. Those
614 operators always uses word arguments without write back. */
can_cache(sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)615 static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
616 {
617 SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
618
619 /* Simple operation except for updates. */
620 if (arg & OFFS_REG_MASK) {
621 argw &= 0x3;
622 SLJIT_ASSERT(argw);
623 next_argw &= 0x3;
624 if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == next_argw)
625 return 1;
626 return 0;
627 }
628
629 if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
630 return 1;
631 return 0;
632 }
633
634 /* Emit the necessary instructions. See can_cache above. */
getput_arg(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw,sljit_s32 next_arg,sljit_sw next_argw)635 static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
636 {
637 sljit_s32 base, arg2, delay_slot;
638 sljit_ins dest;
639
640 SLJIT_ASSERT(arg & SLJIT_MEM);
641 if (!(next_arg & SLJIT_MEM)) {
642 next_arg = 0;
643 next_argw = 0;
644 }
645
646 base = arg & REG_MASK;
647 if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
648 argw &= 0x3;
649
650 /* Using the cache. */
651 if (((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) && (argw == compiler->cache_argw))
652 arg2 = TMP_REG3;
653 else {
654 if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == (next_argw & 0x3)) {
655 compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
656 compiler->cache_argw = argw;
657 arg2 = TMP_REG3;
658 }
659 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base && reg != OFFS_REG(arg))
660 arg2 = reg;
661 else /* It must be a mov operation, so tmp1 must be free to use. */
662 arg2 = TMP_REG1;
663 FAIL_IF(push_inst(compiler, SLL_W | D(arg2) | S1(OFFS_REG(arg)) | IMM_ARG | argw, DR(arg2)));
664 }
665 }
666 else {
667 /* Using the cache. */
668 if ((compiler->cache_arg == SLJIT_MEM) && (argw - compiler->cache_argw) <= SIMM_MAX && (argw - compiler->cache_argw) >= SIMM_MIN) {
669 if (argw != compiler->cache_argw) {
670 FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | S1(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
671 compiler->cache_argw = argw;
672 }
673 arg2 = TMP_REG3;
674 } else {
675 if ((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN) {
676 compiler->cache_arg = SLJIT_MEM;
677 compiler->cache_argw = argw;
678 arg2 = TMP_REG3;
679 }
680 else if ((flags & LOAD_DATA) && ((flags & MEM_MASK) <= GPR_REG) && reg != base)
681 arg2 = reg;
682 else /* It must be a mov operation, so tmp1 must be free to use. */
683 arg2 = TMP_REG1;
684 FAIL_IF(load_immediate(compiler, arg2, argw));
685 }
686 }
687
688 dest = ((flags & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg));
689 delay_slot = ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? DR(reg) : MOVABLE_INS;
690 if (!base)
691 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(arg2) | IMM(0), delay_slot);
692 return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | dest | S1(base) | S2(arg2), delay_slot);
693 }
694
emit_op_mem(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg,sljit_sw argw)695 static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
696 {
697 if (getput_arg_fast(compiler, flags, reg, arg, argw))
698 return compiler->error;
699 compiler->cache_arg = 0;
700 compiler->cache_argw = 0;
701 return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
702 }
703
emit_op_mem2(struct sljit_compiler * compiler,sljit_s32 flags,sljit_s32 reg,sljit_s32 arg1,sljit_sw arg1w,sljit_s32 arg2,sljit_sw arg2w)704 static SLJIT_INLINE sljit_s32 emit_op_mem2(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg1, sljit_sw arg1w, sljit_s32 arg2, sljit_sw arg2w)
705 {
706 if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
707 return compiler->error;
708 return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
709 }
710
emit_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)711 static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
712 sljit_s32 dst, sljit_sw dstw,
713 sljit_s32 src1, sljit_sw src1w,
714 sljit_s32 src2, sljit_sw src2w)
715 {
716 /* arg1 goes to TMP_REG1 or src reg
717 arg2 goes to TMP_REG2, imm or src reg
718 TMP_REG3 can be used for caching
719 result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
720 sljit_s32 dst_r = TMP_REG2;
721 sljit_s32 src1_r;
722 sljit_sw src2_r = 0;
723 sljit_s32 sugg_src2_r = TMP_REG2;
724
725 if (!(flags & ALT_KEEP_CACHE)) {
726 compiler->cache_arg = 0;
727 compiler->cache_argw = 0;
728 }
729
730 if (dst != SLJIT_UNUSED) {
731 if (FAST_IS_REG(dst)) {
732 dst_r = dst;
733 flags |= REG_DEST;
734 if (op >= SLJIT_MOV && op <= SLJIT_MOV_P)
735 sugg_src2_r = dst_r;
736 }
737 else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, TMP_REG1, dst, dstw))
738 flags |= SLOW_DEST;
739 }
740
741 if (flags & IMM_OP) {
742 if ((src2 & SLJIT_IMM) && src2w) {
743 if (src2w <= SIMM_MAX && src2w >= SIMM_MIN) {
744 flags |= SRC2_IMM;
745 src2_r = src2w;
746 }
747 }
748 if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
749 if (src1w <= SIMM_MAX && src1w >= SIMM_MIN) {
750 flags |= SRC2_IMM;
751 src2_r = src1w;
752
753 /* And swap arguments. */
754 src1 = src2;
755 src1w = src2w;
756 src2 = SLJIT_IMM;
757 /* src2w = src2_r unneeded. */
758 }
759 }
760 }
761
762 /* Source 1. */
763 if (FAST_IS_REG(src1))
764 src1_r = src1;
765 else if (src1 & SLJIT_IMM) {
766 if (src1w) {
767 FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
768 src1_r = TMP_REG1;
769 }
770 else
771 src1_r = 0;
772 }
773 else {
774 if (getput_arg_fast(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w))
775 FAIL_IF(compiler->error);
776 else
777 flags |= SLOW_SRC1;
778 src1_r = TMP_REG1;
779 }
780
781 /* Source 2. */
782 if (FAST_IS_REG(src2)) {
783 src2_r = src2;
784 flags |= REG2_SOURCE;
785 if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOV_P)
786 dst_r = src2_r;
787 }
788 else if (src2 & SLJIT_IMM) {
789 if (!(flags & SRC2_IMM)) {
790 if (src2w) {
791 FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
792 src2_r = sugg_src2_r;
793 }
794 else {
795 src2_r = 0;
796 if ((op >= SLJIT_MOV && op <= SLJIT_MOV_P) && (dst & SLJIT_MEM))
797 dst_r = 0;
798 }
799 }
800 }
801 else {
802 if (getput_arg_fast(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w))
803 FAIL_IF(compiler->error);
804 else
805 flags |= SLOW_SRC2;
806 src2_r = sugg_src2_r;
807 }
808
809 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
810 SLJIT_ASSERT(src2_r == TMP_REG2);
811 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
812 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
813 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
814 }
815 else {
816 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
817 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
818 }
819 }
820 else if (flags & SLOW_SRC1)
821 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
822 else if (flags & SLOW_SRC2)
823 FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
824
825 FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
826
827 if (dst & SLJIT_MEM) {
828 if (!(flags & SLOW_DEST)) {
829 getput_arg_fast(compiler, flags, dst_r, dst, dstw);
830 return compiler->error;
831 }
832 return getput_arg(compiler, flags, dst_r, dst, dstw, 0, 0);
833 }
834
835 return SLJIT_SUCCESS;
836 }
837
sljit_emit_op0(struct sljit_compiler * compiler,sljit_s32 op)838 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
839 {
840 CHECK_ERROR();
841 CHECK(check_sljit_emit_op0(compiler, op));
842
843 op = GET_OPCODE(op);
844 switch (op) {
845 case SLJIT_BREAKPOINT:
846 return push_inst(compiler, TA, UNMOVABLE_INS);
847 case SLJIT_NOP:
848 return push_inst(compiler, NOP, UNMOVABLE_INS);
849 case SLJIT_LMUL_UW:
850 case SLJIT_LMUL_SW:
851 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
852 FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? UMUL : SMUL) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
853 return push_inst(compiler, RDY | D(SLJIT_R1), DR(SLJIT_R1));
854 #else
855 #error "Implementation required"
856 #endif
857 case SLJIT_DIVMOD_UW:
858 case SLJIT_DIVMOD_SW:
859 case SLJIT_DIV_UW:
860 case SLJIT_DIV_SW:
861 SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
862 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
863 if ((op | 0x2) == SLJIT_DIV_UW)
864 FAIL_IF(push_inst(compiler, WRY | S1(0), MOVABLE_INS));
865 else {
866 FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(SLJIT_R0) | IMM(31), DR(TMP_REG1)));
867 FAIL_IF(push_inst(compiler, WRY | S1(TMP_REG1), MOVABLE_INS));
868 }
869 if (op <= SLJIT_DIVMOD_SW)
870 FAIL_IF(push_inst(compiler, OR | D(TMP_REG2) | S1(0) | S2(SLJIT_R0), DR(TMP_REG2)));
871 FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? UDIV : SDIV) | D(SLJIT_R0) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R0)));
872 if (op >= SLJIT_DIV_UW)
873 return SLJIT_SUCCESS;
874 FAIL_IF(push_inst(compiler, SMUL | D(SLJIT_R1) | S1(SLJIT_R0) | S2(SLJIT_R1), DR(SLJIT_R1)));
875 return push_inst(compiler, SUB | D(SLJIT_R1) | S1(TMP_REG2) | S2(SLJIT_R1), DR(SLJIT_R1));
876 #else
877 #error "Implementation required"
878 #endif
879 case SLJIT_ENDBR:
880 case SLJIT_SKIP_FRAMES_BEFORE_RETURN:
881 return SLJIT_SUCCESS;
882 }
883
884 return SLJIT_SUCCESS;
885 }
886
sljit_emit_op1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)887 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
888 sljit_s32 dst, sljit_sw dstw,
889 sljit_s32 src, sljit_sw srcw)
890 {
891 sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
892
893 CHECK_ERROR();
894 CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
895 ADJUST_LOCAL_OFFSET(dst, dstw);
896 ADJUST_LOCAL_OFFSET(src, srcw);
897
898 op = GET_OPCODE(op);
899 switch (op) {
900 case SLJIT_MOV:
901 case SLJIT_MOV_P:
902 return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
903
904 case SLJIT_MOV_U32:
905 return emit_op(compiler, SLJIT_MOV_U32, flags | INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
906
907 case SLJIT_MOV_S32:
908 return emit_op(compiler, SLJIT_MOV_S32, flags | INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
909
910 case SLJIT_MOV_U8:
911 return emit_op(compiler, SLJIT_MOV_U8, flags | BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
912
913 case SLJIT_MOV_S8:
914 return emit_op(compiler, SLJIT_MOV_S8, flags | BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
915
916 case SLJIT_MOV_U16:
917 return emit_op(compiler, SLJIT_MOV_U16, flags | HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
918
919 case SLJIT_MOV_S16:
920 return emit_op(compiler, SLJIT_MOV_S16, flags | HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
921
922 case SLJIT_NOT:
923 case SLJIT_CLZ:
924 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
925
926 case SLJIT_NEG:
927 return emit_op(compiler, SLJIT_SUB, flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
928 }
929
930 return SLJIT_SUCCESS;
931 }
932
sljit_emit_op2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)933 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
934 sljit_s32 dst, sljit_sw dstw,
935 sljit_s32 src1, sljit_sw src1w,
936 sljit_s32 src2, sljit_sw src2w)
937 {
938 sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
939
940 CHECK_ERROR();
941 CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
942 ADJUST_LOCAL_OFFSET(dst, dstw);
943 ADJUST_LOCAL_OFFSET(src1, src1w);
944 ADJUST_LOCAL_OFFSET(src2, src2w);
945
946 if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
947 return SLJIT_SUCCESS;
948
949 op = GET_OPCODE(op);
950 switch (op) {
951 case SLJIT_ADD:
952 case SLJIT_ADDC:
953 case SLJIT_MUL:
954 case SLJIT_AND:
955 case SLJIT_OR:
956 case SLJIT_XOR:
957 return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
958
959 case SLJIT_SUB:
960 case SLJIT_SUBC:
961 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
962
963 case SLJIT_SHL:
964 case SLJIT_LSHR:
965 case SLJIT_ASHR:
966 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
967 if (src2 & SLJIT_IMM)
968 src2w &= 0x1f;
969 #else
970 SLJIT_UNREACHABLE();
971 #endif
972 return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
973 }
974
975 return SLJIT_SUCCESS;
976 }
977
sljit_emit_op_src(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src,sljit_sw srcw)978 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_src(struct sljit_compiler *compiler, sljit_s32 op,
979 sljit_s32 src, sljit_sw srcw)
980 {
981 CHECK_ERROR();
982 CHECK(check_sljit_emit_op_src(compiler, op, src, srcw));
983 ADJUST_LOCAL_OFFSET(src, srcw);
984
985 switch (op) {
986 case SLJIT_FAST_RETURN:
987 if (FAST_IS_REG(src))
988 FAIL_IF(push_inst(compiler, OR | D(TMP_LINK) | S1(0) | S2(src), DR(TMP_LINK)));
989 else
990 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_LINK, src, srcw));
991
992 FAIL_IF(push_inst(compiler, JMPL | D(0) | S1(TMP_LINK) | IMM(8), UNMOVABLE_INS));
993 return push_inst(compiler, NOP, UNMOVABLE_INS);
994 case SLJIT_SKIP_FRAMES_BEFORE_FAST_RETURN:
995 case SLJIT_PREFETCH_L1:
996 case SLJIT_PREFETCH_L2:
997 case SLJIT_PREFETCH_L3:
998 case SLJIT_PREFETCH_ONCE:
999 return SLJIT_SUCCESS;
1000 }
1001
1002 return SLJIT_SUCCESS;
1003 }
1004
sljit_get_register_index(sljit_s32 reg)1005 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
1006 {
1007 CHECK_REG_INDEX(check_sljit_get_register_index(reg));
1008 return reg_map[reg];
1009 }
1010
sljit_get_float_register_index(sljit_s32 reg)1011 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
1012 {
1013 CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
1014 return freg_map[reg];
1015 }
1016
sljit_emit_op_custom(struct sljit_compiler * compiler,void * instruction,sljit_s32 size)1017 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
1018 void *instruction, sljit_s32 size)
1019 {
1020 CHECK_ERROR();
1021 CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
1022
1023 return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
1024 }
1025
1026 /* --------------------------------------------------------------------- */
1027 /* Floating point operators */
1028 /* --------------------------------------------------------------------- */
1029
1030 #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
1031 #define SELECT_FOP(op, single, double) ((op & SLJIT_F32_OP) ? single : double)
1032 #define FLOAT_TMP_MEM_OFFSET (22 * sizeof(sljit_sw))
1033
sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1034 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
1035 sljit_s32 dst, sljit_sw dstw,
1036 sljit_s32 src, sljit_sw srcw)
1037 {
1038 if (src & SLJIT_MEM) {
1039 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1040 src = TMP_FREG1;
1041 }
1042
1043 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOI, FDTOI) | FD(TMP_FREG1) | FS2(src), MOVABLE_INS));
1044
1045 if (FAST_IS_REG(dst)) {
1046 FAIL_IF(emit_op_mem2(compiler, SINGLE_DATA, TMP_FREG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
1047 return emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, dst, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET);
1048 }
1049
1050 /* Store the integer value from a VFP register. */
1051 return emit_op_mem2(compiler, SINGLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
1052 }
1053
sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1054 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
1055 sljit_s32 dst, sljit_sw dstw,
1056 sljit_s32 src, sljit_sw srcw)
1057 {
1058 sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1059
1060 if (src & SLJIT_IMM) {
1061 #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
1062 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
1063 srcw = (sljit_s32)srcw;
1064 #endif
1065 FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
1066 src = TMP_REG1;
1067 srcw = 0;
1068 }
1069
1070 if (FAST_IS_REG(src)) {
1071 FAIL_IF(emit_op_mem2(compiler, WORD_DATA, src, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
1072 src = SLJIT_MEM1(SLJIT_SP);
1073 srcw = FLOAT_TMP_MEM_OFFSET;
1074 }
1075
1076 FAIL_IF(emit_op_mem2(compiler, SINGLE_DATA | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
1077 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FITOS, FITOD) | FD(dst_r) | FS2(TMP_FREG1), MOVABLE_INS));
1078
1079 if (dst & SLJIT_MEM)
1080 return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
1081 return SLJIT_SUCCESS;
1082 }
1083
sljit_emit_fop1_cmp(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1084 static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
1085 sljit_s32 src1, sljit_sw src1w,
1086 sljit_s32 src2, sljit_sw src2w)
1087 {
1088 if (src1 & SLJIT_MEM) {
1089 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1090 src1 = TMP_FREG1;
1091 }
1092
1093 if (src2 & SLJIT_MEM) {
1094 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
1095 src2 = TMP_FREG2;
1096 }
1097
1098 return push_inst(compiler, SELECT_FOP(op, FCMPS, FCMPD) | FS1(src1) | FS2(src2), FCC_IS_SET | MOVABLE_INS);
1099 }
1100
sljit_emit_fop1(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src,sljit_sw srcw)1101 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
1102 sljit_s32 dst, sljit_sw dstw,
1103 sljit_s32 src, sljit_sw srcw)
1104 {
1105 sljit_s32 dst_r;
1106
1107 CHECK_ERROR();
1108 compiler->cache_arg = 0;
1109 compiler->cache_argw = 0;
1110
1111 SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
1112 SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
1113
1114 if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
1115 op ^= SLJIT_F32_OP;
1116
1117 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
1118
1119 if (src & SLJIT_MEM) {
1120 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
1121 src = dst_r;
1122 }
1123
1124 switch (GET_OPCODE(op)) {
1125 case SLJIT_MOV_F64:
1126 if (src != dst_r) {
1127 if (dst_r != TMP_FREG1) {
1128 FAIL_IF(push_inst(compiler, FMOVS | FD(dst_r) | FS2(src), MOVABLE_INS));
1129 if (!(op & SLJIT_F32_OP))
1130 FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1131 }
1132 else
1133 dst_r = src;
1134 }
1135 break;
1136 case SLJIT_NEG_F64:
1137 FAIL_IF(push_inst(compiler, FNEGS | FD(dst_r) | FS2(src), MOVABLE_INS));
1138 if (dst_r != src && !(op & SLJIT_F32_OP))
1139 FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1140 break;
1141 case SLJIT_ABS_F64:
1142 FAIL_IF(push_inst(compiler, FABSS | FD(dst_r) | FS2(src), MOVABLE_INS));
1143 if (dst_r != src && !(op & SLJIT_F32_OP))
1144 FAIL_IF(push_inst(compiler, FMOVS | FDN(dst_r) | FS2N(src), MOVABLE_INS));
1145 break;
1146 case SLJIT_CONV_F64_FROM_F32:
1147 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSTOD, FDTOS) | FD(dst_r) | FS2(src), MOVABLE_INS));
1148 op ^= SLJIT_F32_OP;
1149 break;
1150 }
1151
1152 if (dst & SLJIT_MEM)
1153 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0));
1154 return SLJIT_SUCCESS;
1155 }
1156
sljit_emit_fop2(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 src1,sljit_sw src1w,sljit_s32 src2,sljit_sw src2w)1157 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
1158 sljit_s32 dst, sljit_sw dstw,
1159 sljit_s32 src1, sljit_sw src1w,
1160 sljit_s32 src2, sljit_sw src2w)
1161 {
1162 sljit_s32 dst_r, flags = 0;
1163
1164 CHECK_ERROR();
1165 CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
1166 ADJUST_LOCAL_OFFSET(dst, dstw);
1167 ADJUST_LOCAL_OFFSET(src1, src1w);
1168 ADJUST_LOCAL_OFFSET(src2, src2w);
1169
1170 compiler->cache_arg = 0;
1171 compiler->cache_argw = 0;
1172
1173 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG2;
1174
1175 if (src1 & SLJIT_MEM) {
1176 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
1177 FAIL_IF(compiler->error);
1178 src1 = TMP_FREG1;
1179 } else
1180 flags |= SLOW_SRC1;
1181 }
1182
1183 if (src2 & SLJIT_MEM) {
1184 if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
1185 FAIL_IF(compiler->error);
1186 src2 = TMP_FREG2;
1187 } else
1188 flags |= SLOW_SRC2;
1189 }
1190
1191 if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
1192 if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
1193 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
1194 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1195 }
1196 else {
1197 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
1198 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1199 }
1200 }
1201 else if (flags & SLOW_SRC1)
1202 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
1203 else if (flags & SLOW_SRC2)
1204 FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
1205
1206 if (flags & SLOW_SRC1)
1207 src1 = TMP_FREG1;
1208 if (flags & SLOW_SRC2)
1209 src2 = TMP_FREG2;
1210
1211 switch (GET_OPCODE(op)) {
1212 case SLJIT_ADD_F64:
1213 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADDD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1214 break;
1215
1216 case SLJIT_SUB_F64:
1217 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUBD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1218 break;
1219
1220 case SLJIT_MUL_F64:
1221 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMULD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1222 break;
1223
1224 case SLJIT_DIV_F64:
1225 FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIVD) | FD(dst_r) | FS1(src1) | FS2(src2), MOVABLE_INS));
1226 break;
1227 }
1228
1229 if (dst_r == TMP_FREG2)
1230 FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
1231
1232 return SLJIT_SUCCESS;
1233 }
1234
1235 #undef FLOAT_DATA
1236 #undef SELECT_FOP
1237
1238 /* --------------------------------------------------------------------- */
1239 /* Other instructions */
1240 /* --------------------------------------------------------------------- */
1241
sljit_emit_fast_enter(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1242 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1243 {
1244 CHECK_ERROR();
1245 CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
1246 ADJUST_LOCAL_OFFSET(dst, dstw);
1247
1248 if (FAST_IS_REG(dst))
1249 return push_inst(compiler, OR | D(dst) | S1(0) | S2(TMP_LINK), UNMOVABLE_INS);
1250
1251 /* Memory. */
1252 FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_LINK, dst, dstw));
1253 compiler->delay_slot = UNMOVABLE_INS;
1254 return SLJIT_SUCCESS;
1255 }
1256
1257 /* --------------------------------------------------------------------- */
1258 /* Conditional instructions */
1259 /* --------------------------------------------------------------------- */
1260
sljit_emit_label(struct sljit_compiler * compiler)1261 SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
1262 {
1263 struct sljit_label *label;
1264
1265 CHECK_ERROR_PTR();
1266 CHECK_PTR(check_sljit_emit_label(compiler));
1267
1268 if (compiler->last_label && compiler->last_label->size == compiler->size)
1269 return compiler->last_label;
1270
1271 label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
1272 PTR_FAIL_IF(!label);
1273 set_label(label, compiler);
1274 compiler->delay_slot = UNMOVABLE_INS;
1275 return label;
1276 }
1277
get_cc(sljit_s32 type)1278 static sljit_ins get_cc(sljit_s32 type)
1279 {
1280 switch (type) {
1281 case SLJIT_EQUAL:
1282 case SLJIT_MUL_NOT_OVERFLOW:
1283 case SLJIT_NOT_EQUAL_F64: /* Unordered. */
1284 return DA(0x1);
1285
1286 case SLJIT_NOT_EQUAL:
1287 case SLJIT_MUL_OVERFLOW:
1288 case SLJIT_EQUAL_F64:
1289 return DA(0x9);
1290
1291 case SLJIT_LESS:
1292 case SLJIT_GREATER_F64: /* Unordered. */
1293 return DA(0x5);
1294
1295 case SLJIT_GREATER_EQUAL:
1296 case SLJIT_LESS_EQUAL_F64:
1297 return DA(0xd);
1298
1299 case SLJIT_GREATER:
1300 case SLJIT_GREATER_EQUAL_F64: /* Unordered. */
1301 return DA(0xc);
1302
1303 case SLJIT_LESS_EQUAL:
1304 case SLJIT_LESS_F64:
1305 return DA(0x4);
1306
1307 case SLJIT_SIG_LESS:
1308 return DA(0x3);
1309
1310 case SLJIT_SIG_GREATER_EQUAL:
1311 return DA(0xb);
1312
1313 case SLJIT_SIG_GREATER:
1314 return DA(0xa);
1315
1316 case SLJIT_SIG_LESS_EQUAL:
1317 return DA(0x2);
1318
1319 case SLJIT_OVERFLOW:
1320 case SLJIT_UNORDERED_F64:
1321 return DA(0x7);
1322
1323 case SLJIT_NOT_OVERFLOW:
1324 case SLJIT_ORDERED_F64:
1325 return DA(0xf);
1326
1327 default:
1328 SLJIT_UNREACHABLE();
1329 return DA(0x8);
1330 }
1331 }
1332
sljit_emit_jump(struct sljit_compiler * compiler,sljit_s32 type)1333 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
1334 {
1335 struct sljit_jump *jump;
1336
1337 CHECK_ERROR_PTR();
1338 CHECK_PTR(check_sljit_emit_jump(compiler, type));
1339
1340 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1341 PTR_FAIL_IF(!jump);
1342 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
1343 type &= 0xff;
1344
1345 if (type < SLJIT_EQUAL_F64) {
1346 jump->flags |= IS_COND;
1347 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & ICC_IS_SET))
1348 jump->flags |= IS_MOVABLE;
1349 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1350 PTR_FAIL_IF(push_inst(compiler, BICC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1351 #else
1352 #error "Implementation required"
1353 #endif
1354 }
1355 else if (type < SLJIT_JUMP) {
1356 jump->flags |= IS_COND;
1357 if (((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS) && !(compiler->delay_slot & FCC_IS_SET))
1358 jump->flags |= IS_MOVABLE;
1359 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1360 PTR_FAIL_IF(push_inst(compiler, FBFCC | get_cc(type ^ 1) | 5, UNMOVABLE_INS));
1361 #else
1362 #error "Implementation required"
1363 #endif
1364 }
1365 else {
1366 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1367 jump->flags |= IS_MOVABLE;
1368 if (type >= SLJIT_FAST_CALL)
1369 jump->flags |= IS_CALL;
1370 }
1371
1372 PTR_FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1373 PTR_FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(TMP_REG1) | IMM(0), UNMOVABLE_INS));
1374 jump->addr = compiler->size;
1375 PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
1376
1377 return jump;
1378 }
1379
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)1380 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
1381 sljit_s32 arg_types)
1382 {
1383 CHECK_ERROR_PTR();
1384 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
1385
1386 PTR_FAIL_IF(call_with_args(compiler, arg_types, NULL));
1387
1388 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1389 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1390 compiler->skip_checks = 1;
1391 #endif
1392
1393 return sljit_emit_jump(compiler, type);
1394 }
1395
sljit_emit_ijump(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 src,sljit_sw srcw)1396 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
1397 {
1398 struct sljit_jump *jump = NULL;
1399 sljit_s32 src_r;
1400
1401 CHECK_ERROR();
1402 CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
1403 ADJUST_LOCAL_OFFSET(src, srcw);
1404
1405 if (FAST_IS_REG(src))
1406 src_r = src;
1407 else if (src & SLJIT_IMM) {
1408 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
1409 FAIL_IF(!jump);
1410 set_jump(jump, compiler, JUMP_ADDR);
1411 jump->u.target = srcw;
1412
1413 if ((compiler->delay_slot & DST_INS_MASK) != UNMOVABLE_INS)
1414 jump->flags |= IS_MOVABLE;
1415 if (type >= SLJIT_FAST_CALL)
1416 jump->flags |= IS_CALL;
1417
1418 FAIL_IF(emit_const(compiler, TMP_REG1, 0));
1419 src_r = TMP_REG1;
1420 }
1421 else {
1422 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw));
1423 src_r = TMP_REG1;
1424 }
1425
1426 FAIL_IF(push_inst(compiler, JMPL | D(type >= SLJIT_FAST_CALL ? TMP_LINK : 0) | S1(src_r) | IMM(0), UNMOVABLE_INS));
1427 if (jump)
1428 jump->addr = compiler->size;
1429 return push_inst(compiler, NOP, UNMOVABLE_INS);
1430 }
1431
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)1432 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
1433 sljit_s32 arg_types,
1434 sljit_s32 src, sljit_sw srcw)
1435 {
1436 CHECK_ERROR();
1437 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
1438
1439 if (src & SLJIT_MEM) {
1440 ADJUST_LOCAL_OFFSET(src, srcw);
1441 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, src, srcw));
1442 src = TMP_REG1;
1443 }
1444
1445 FAIL_IF(call_with_args(compiler, arg_types, &src));
1446
1447 #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
1448 || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
1449 compiler->skip_checks = 1;
1450 #endif
1451
1452 return sljit_emit_ijump(compiler, type, src, srcw);
1453 }
1454
sljit_emit_op_flags(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 dst,sljit_sw dstw,sljit_s32 type)1455 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
1456 sljit_s32 dst, sljit_sw dstw,
1457 sljit_s32 type)
1458 {
1459 sljit_s32 reg, flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
1460
1461 CHECK_ERROR();
1462 CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
1463 ADJUST_LOCAL_OFFSET(dst, dstw);
1464
1465 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1466 op = GET_OPCODE(op);
1467 reg = (op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2;
1468
1469 compiler->cache_arg = 0;
1470 compiler->cache_argw = 0;
1471
1472 if (op >= SLJIT_ADD && (dst & SLJIT_MEM))
1473 FAIL_IF(emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, TMP_REG1, dst, dstw, dst, dstw));
1474
1475 type &= 0xff;
1476 if (type < SLJIT_EQUAL_F64)
1477 FAIL_IF(push_inst(compiler, BICC | get_cc(type) | 3, UNMOVABLE_INS));
1478 else
1479 FAIL_IF(push_inst(compiler, FBFCC | get_cc(type) | 3, UNMOVABLE_INS));
1480
1481 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(1), UNMOVABLE_INS));
1482 FAIL_IF(push_inst(compiler, OR | D(reg) | S1(0) | IMM(0), UNMOVABLE_INS));
1483
1484 if (op >= SLJIT_ADD) {
1485 flags |= CUMULATIVE_OP | IMM_OP | ALT_KEEP_CACHE;
1486 if (dst & SLJIT_MEM)
1487 return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
1488 return emit_op(compiler, op, flags, dst, 0, dst, 0, TMP_REG2, 0);
1489 }
1490
1491 if (!(dst & SLJIT_MEM))
1492 return SLJIT_SUCCESS;
1493
1494 return emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw);
1495 #else
1496 #error "Implementation required"
1497 #endif
1498 }
1499
sljit_emit_cmov(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 dst_reg,sljit_s32 src,sljit_sw srcw)1500 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
1501 sljit_s32 dst_reg,
1502 sljit_s32 src, sljit_sw srcw)
1503 {
1504 CHECK_ERROR();
1505 CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
1506
1507 #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
1508 return sljit_emit_cmov_generic(compiler, type, dst_reg, src, srcw);;
1509 #else
1510 #error "Implementation required"
1511 #endif
1512 }
1513
sljit_emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw,sljit_sw init_value)1514 SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
1515 {
1516 struct sljit_const *const_;
1517 sljit_s32 dst_r;
1518
1519 CHECK_ERROR_PTR();
1520 CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
1521 ADJUST_LOCAL_OFFSET(dst, dstw);
1522
1523 const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
1524 PTR_FAIL_IF(!const_);
1525 set_const(const_, compiler);
1526
1527 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
1528 PTR_FAIL_IF(emit_const(compiler, dst_r, init_value));
1529
1530 if (dst & SLJIT_MEM)
1531 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1532 return const_;
1533 }
1534
sljit_emit_put_label(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw dstw)1535 SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
1536 {
1537 struct sljit_put_label *put_label;
1538 sljit_s32 dst_r;
1539
1540 CHECK_ERROR_PTR();
1541 CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
1542 ADJUST_LOCAL_OFFSET(dst, dstw);
1543
1544 put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
1545 PTR_FAIL_IF(!put_label);
1546 set_put_label(put_label, compiler, 0);
1547
1548 dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
1549 PTR_FAIL_IF(emit_const(compiler, dst_r, 0));
1550
1551 if (dst & SLJIT_MEM)
1552 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA, TMP_REG2, dst, dstw));
1553 return put_label;
1554 }
1555