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 
27 /* mips 64-bit arch dependent functions. */
28 
emit_copysign(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 src1,sljit_s32 src2,sljit_s32 dst)29 static sljit_s32 emit_copysign(struct sljit_compiler *compiler, sljit_s32 op,
30 		sljit_s32 src1, sljit_s32 src2, sljit_s32 dst)
31 {
32 	FAIL_IF(push_inst(compiler, SELECT_OP(DMFC1, MFC1) | T(TMP_REG1) | FS(src1), DR(TMP_REG1)));
33 	FAIL_IF(push_inst(compiler, SELECT_OP(DMFC1, MFC1) | T(TMP_REG2) | FS(src2), DR(TMP_REG2)));
34 	FAIL_IF(push_inst(compiler, XOR | S(TMP_REG2) | T(TMP_REG1) | D(TMP_REG2), DR(TMP_REG2)));
35 	FAIL_IF(push_inst(compiler, SELECT_OP(DSRL32, SRL) | T(TMP_REG2) | D(TMP_REG2) | SH_IMM(31), DR(TMP_REG2)));
36 	FAIL_IF(push_inst(compiler, SELECT_OP(DSLL32, SLL) | T(TMP_REG2) | D(TMP_REG2) | SH_IMM(31), DR(TMP_REG2)));
37 	FAIL_IF(push_inst(compiler, XOR | S(TMP_REG1) | T(TMP_REG2) | D(TMP_REG1), DR(TMP_REG1)));
38 	FAIL_IF(push_inst(compiler, SELECT_OP(DMTC1, MTC1) | T(TMP_REG1) | FS(dst), MOVABLE_INS));
39 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
40 	if (!(op & SLJIT_32))
41 		return push_inst(compiler, NOP, UNMOVABLE_INS);
42 #endif /* MIPS III */
43 	return SLJIT_SUCCESS;
44 }
45 
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst_ar,sljit_sw imm)46 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_ar, sljit_sw imm)
47 {
48 	sljit_s32 shift = 32;
49 	sljit_s32 shift2;
50 	sljit_s32 inv = 0;
51 	sljit_ins ins;
52 	sljit_uw uimm;
53 
54 	if (!(imm & ~0xffff))
55 		return push_inst(compiler, ORI | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
56 
57 	if (imm < 0 && imm >= SIMM_MIN)
58 		return push_inst(compiler, ADDIU | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
59 
60 	if (imm <= 0x7fffffffl && imm >= -0x80000000l) {
61 		FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(imm >> 16), dst_ar));
62 		return (imm & 0xffff) ? push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar) : SLJIT_SUCCESS;
63 	}
64 
65 	/* Zero extended number. */
66 	uimm = (sljit_uw)imm;
67 	if (imm < 0) {
68 		uimm = ~(sljit_uw)imm;
69 		inv = 1;
70 	}
71 
72 	while (!(uimm & 0xff00000000000000l)) {
73 		shift -= 8;
74 		uimm <<= 8;
75 	}
76 
77 	if (!(uimm & 0xf000000000000000l)) {
78 		shift -= 4;
79 		uimm <<= 4;
80 	}
81 
82 	if (!(uimm & 0xc000000000000000l)) {
83 		shift -= 2;
84 		uimm <<= 2;
85 	}
86 
87 	if ((sljit_sw)uimm < 0) {
88 		uimm >>= 1;
89 		shift += 1;
90 	}
91 	SLJIT_ASSERT(((uimm & 0xc000000000000000l) == 0x4000000000000000l) && (shift > 0) && (shift <= 32));
92 
93 	if (inv)
94 		uimm = ~uimm;
95 
96 	FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(uimm >> 48), dst_ar));
97 	if (uimm & 0x0000ffff00000000l)
98 		FAIL_IF(push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(uimm >> 32), dst_ar));
99 
100 	imm &= (1l << shift) - 1;
101 	if (!(imm & ~0xffff)) {
102 		ins = (shift == 32) ? DSLL32 : DSLL;
103 		if (shift < 32)
104 			ins |= SH_IMM(shift);
105 		FAIL_IF(push_inst(compiler, ins | TA(dst_ar) | DA(dst_ar), dst_ar));
106 		return !(imm & 0xffff) ? SLJIT_SUCCESS : push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar);
107 	}
108 
109 	/* Double shifts needs to be performed. */
110 	uimm <<= 32;
111 	shift2 = shift - 16;
112 
113 	while (!(uimm & 0xf000000000000000l)) {
114 		shift2 -= 4;
115 		uimm <<= 4;
116 	}
117 
118 	if (!(uimm & 0xc000000000000000l)) {
119 		shift2 -= 2;
120 		uimm <<= 2;
121 	}
122 
123 	if (!(uimm & 0x8000000000000000l)) {
124 		shift2--;
125 		uimm <<= 1;
126 	}
127 
128 	SLJIT_ASSERT((uimm & 0x8000000000000000l) && (shift2 > 0) && (shift2 <= 16));
129 
130 	FAIL_IF(push_inst(compiler, DSLL | TA(dst_ar) | DA(dst_ar) | SH_IMM(shift - shift2), dst_ar));
131 	FAIL_IF(push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(uimm >> 48), dst_ar));
132 	FAIL_IF(push_inst(compiler, DSLL | TA(dst_ar) | DA(dst_ar) | SH_IMM(shift2), dst_ar));
133 
134 	imm &= (1l << shift2) - 1;
135 	return !(imm & 0xffff) ? SLJIT_SUCCESS : push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar);
136 }
137 
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value)138 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
139 {
140 	FAIL_IF(push_inst(compiler, LUI | T(dst) | IMM(init_value >> 48), DR(dst)));
141 	FAIL_IF(push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value >> 32), DR(dst)));
142 	FAIL_IF(push_inst(compiler, DSLL | T(dst) | D(dst) | SH_IMM(16), DR(dst)));
143 	FAIL_IF(push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value >> 16), DR(dst)));
144 	FAIL_IF(push_inst(compiler, DSLL | T(dst) | D(dst) | SH_IMM(16), DR(dst)));
145 	return push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value), DR(dst));
146 }
147 
sljit_emit_fset64(struct sljit_compiler * compiler,sljit_s32 freg,sljit_f64 value)148 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
149 	sljit_s32 freg, sljit_f64 value)
150 {
151 	union {
152 		sljit_sw imm;
153 		sljit_f64 value;
154 	} u;
155 
156 	CHECK_ERROR();
157 	CHECK(check_sljit_emit_fset64(compiler, freg, value));
158 
159 	u.value = value;
160 
161 	if (u.imm == 0) {
162 		FAIL_IF(push_inst(compiler, DMTC1 | TA(0) | FS(freg), MOVABLE_INS));
163 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
164 		FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
165 #endif /* MIPS III */
166 		return SLJIT_SUCCESS;
167 	}
168 
169 	FAIL_IF(load_immediate(compiler, DR(TMP_REG1), u.imm));
170 	FAIL_IF(push_inst(compiler, DMTC1 | T(TMP_REG1) | FS(freg), MOVABLE_INS));
171 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
172 	FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
173 #endif /* MIPS III */
174 	return SLJIT_SUCCESS;
175 }
176 
sljit_emit_fcopy(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 freg,sljit_s32 reg)177 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
178 	sljit_s32 freg, sljit_s32 reg)
179 {
180 	sljit_ins inst;
181 
182 	CHECK_ERROR();
183 	CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));
184 
185 	inst = T(reg) | FS(freg);
186 
187 	if (GET_OPCODE(op) == SLJIT_COPY_TO_F64)
188 		FAIL_IF(push_inst(compiler, SELECT_OP(DMTC1, MTC1) | inst, MOVABLE_INS));
189 	else
190 		FAIL_IF(push_inst(compiler, SELECT_OP(DMFC1, MFC1) | inst, DR(reg)));
191 
192 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
193 	if (!(op & SLJIT_32))
194 		return push_inst(compiler, NOP, UNMOVABLE_INS);
195 #endif /* MIPS III */
196 	return SLJIT_SUCCESS;
197 }
198 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)199 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
200 {
201 	sljit_ins *inst = (sljit_ins *)addr;
202 	SLJIT_UNUSED_ARG(executable_offset);
203 
204 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 6, 0);
205 	inst[0] = (inst[0] & 0xffff0000) | ((sljit_ins)(new_target >> 48) & 0xffff);
206 	inst[1] = (inst[1] & 0xffff0000) | ((sljit_ins)(new_target >> 32) & 0xffff);
207 	inst[3] = (inst[3] & 0xffff0000) | ((sljit_ins)(new_target >> 16) & 0xffff);
208 	inst[5] = (inst[5] & 0xffff0000) | ((sljit_ins)new_target & 0xffff);
209 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 6, 1);
210 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
211 	SLJIT_CACHE_FLUSH(inst, inst + 6);
212 }
213 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)214 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
215 {
216 	sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);
217 }
218 
call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_ins * ins_ptr)219 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_ins *ins_ptr)
220 {
221 	sljit_s32 arg_count = 0;
222 	sljit_s32 word_arg_count = 0;
223 	sljit_s32 float_arg_count = 0;
224 	sljit_s32 types = 0;
225 	sljit_ins prev_ins = *ins_ptr;
226 	sljit_ins ins = NOP;
227 
228 	SLJIT_ASSERT(reg_map[TMP_REG2] == 4 && freg_map[TMP_FREG1] == 12);
229 
230 	arg_types >>= SLJIT_ARG_SHIFT;
231 
232 	while (arg_types) {
233 		types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
234 
235 		switch (arg_types & SLJIT_ARG_MASK) {
236 		case SLJIT_ARG_TYPE_F64:
237 		case SLJIT_ARG_TYPE_F32:
238 			arg_count++;
239 			float_arg_count++;
240 			break;
241 		default:
242 			arg_count++;
243 			word_arg_count++;
244 			break;
245 		}
246 
247 		arg_types >>= SLJIT_ARG_SHIFT;
248 	}
249 
250 	while (types) {
251 		switch (types & SLJIT_ARG_MASK) {
252 		case SLJIT_ARG_TYPE_F64:
253 			if (arg_count != float_arg_count)
254 				ins = MOV_fmt(FMT_D) | FS(float_arg_count) | FD(arg_count);
255 			else if (arg_count == 1)
256 				ins = MOV_fmt(FMT_D) | FS(SLJIT_FR0) | FD(TMP_FREG1);
257 			arg_count--;
258 			float_arg_count--;
259 			break;
260 		case SLJIT_ARG_TYPE_F32:
261 			if (arg_count != float_arg_count)
262 				ins = MOV_fmt(FMT_S) | FS(float_arg_count) | FD(arg_count);
263 			else if (arg_count == 1)
264 				ins = MOV_fmt(FMT_S) | FS(SLJIT_FR0) | FD(TMP_FREG1);
265 			arg_count--;
266 			float_arg_count--;
267 			break;
268 		default:
269 			if (arg_count != word_arg_count)
270 				ins = DADDU | S(word_arg_count) | TA(0) | D(arg_count);
271 			else if (arg_count == 1)
272 				ins = DADDU | S(SLJIT_R0) | TA(0) | DA(4);
273 			arg_count--;
274 			word_arg_count--;
275 			break;
276 		}
277 
278 		if (ins != NOP) {
279 			if (prev_ins != NOP)
280 				FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
281 			prev_ins = ins;
282 			ins = NOP;
283 		}
284 
285 		types >>= SLJIT_ARG_SHIFT;
286 	}
287 
288 	*ins_ptr = prev_ins;
289 
290 	return SLJIT_SUCCESS;
291 }
292 
sljit_emit_call(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types)293 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
294 	sljit_s32 arg_types)
295 {
296 	struct sljit_jump *jump;
297 	sljit_ins ins = NOP;
298 
299 	CHECK_ERROR_PTR();
300 	CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
301 
302 	jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
303 	PTR_FAIL_IF(!jump);
304 	set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
305 
306 	if (type & SLJIT_CALL_RETURN)
307 		PTR_FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
308 
309 	if ((type & 0xff) != SLJIT_CALL_REG_ARG)
310 		PTR_FAIL_IF(call_with_args(compiler, arg_types, &ins));
311 
312 	SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25);
313 
314 	if (ins == NOP && compiler->delay_slot != UNMOVABLE_INS)
315 		jump->flags |= IS_MOVABLE;
316 
317 	if (!(type & SLJIT_CALL_RETURN)) {
318 		jump->flags |= IS_JAL;
319 
320 		if ((type & 0xff) != SLJIT_CALL_REG_ARG)
321 			jump->flags |= IS_CALL;
322 
323 		PTR_FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
324 	} else
325 		PTR_FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
326 
327 	jump->addr = compiler->size;
328 	PTR_FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
329 
330 	/* Maximum number of instructions required for generating a constant. */
331 	compiler->size += 6;
332 	return jump;
333 }
334 
sljit_emit_icall(struct sljit_compiler * compiler,sljit_s32 type,sljit_s32 arg_types,sljit_s32 src,sljit_sw srcw)335 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
336 	sljit_s32 arg_types,
337 	sljit_s32 src, sljit_sw srcw)
338 {
339 	sljit_ins ins = NOP;
340 
341 	CHECK_ERROR();
342 	CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
343 
344 	if (src & SLJIT_MEM) {
345 		ADJUST_LOCAL_OFFSET(src, srcw);
346 		FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(PIC_ADDR_REG), src, srcw));
347 		src = PIC_ADDR_REG;
348 		srcw = 0;
349 	}
350 
351 	if ((type & 0xff) == SLJIT_CALL_REG_ARG) {
352 		if (type & SLJIT_CALL_RETURN) {
353 			if (src >= SLJIT_FIRST_SAVED_REG && src <= (SLJIT_S0 - SLJIT_KEPT_SAVEDS_COUNT(compiler->options))) {
354 				FAIL_IF(push_inst(compiler, DADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
355 				src = PIC_ADDR_REG;
356 				srcw = 0;
357 			}
358 
359 			FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
360 
361 			if (ins != NOP)
362 				FAIL_IF(push_inst(compiler, ins, MOVABLE_INS));
363 		}
364 
365 		SLJIT_SKIP_CHECKS(compiler);
366 		return sljit_emit_ijump(compiler, type, src, srcw);
367 	}
368 
369 	SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25 && PIC_ADDR_REG == TMP_REG1);
370 
371 	if (src == SLJIT_IMM)
372 		FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
373 	else if (src != PIC_ADDR_REG)
374 		FAIL_IF(push_inst(compiler, DADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
375 
376 	if (type & SLJIT_CALL_RETURN)
377 		FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
378 
379 	FAIL_IF(call_with_args(compiler, arg_types, &ins));
380 
381 	/* Register input. */
382 	if (!(type & SLJIT_CALL_RETURN))
383 		FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
384 	else
385 		FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
386 	return push_inst(compiler, ins, UNMOVABLE_INS);
387 }
388