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 
load_immediate(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw imm)27 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw imm)
28 {
29 	if (imm <= SIMM_MAX && imm >= SIMM_MIN)
30 		return push_inst(compiler, OR | D(dst) | S1(0) | IMM(imm), DR(dst));
31 
32 	FAIL_IF(push_inst(compiler, SETHI | D(dst) | ((imm >> 10) & 0x3fffff), DR(dst)));
33 	return (imm & 0x3ff) ? push_inst(compiler, OR | D(dst) | S1(dst) | IMM_ARG | (imm & 0x3ff), DR(dst)) : SLJIT_SUCCESS;
34 }
35 
36 #define ARG2(flags, src2) ((flags & SRC2_IMM) ? IMM(src2) : S2(src2))
37 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_u32 flags,sljit_s32 dst,sljit_s32 src1,sljit_sw src2)38 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_u32 flags,
39 	sljit_s32 dst, sljit_s32 src1, sljit_sw src2)
40 {
41 	SLJIT_COMPILE_ASSERT(ICC_IS_SET == SET_FLAGS, icc_is_set_and_set_flags_must_be_the_same);
42 
43 	switch (op) {
44 	case SLJIT_MOV:
45 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
46 		if (dst != src2)
47 			return push_inst(compiler, OR | D(dst) | S1(0) | S2(src2), DR(dst));
48 		return SLJIT_SUCCESS;
49 
50 	case SLJIT_MOV_U8:
51 	case SLJIT_MOV_S8:
52 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
53 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
54 			if (op == SLJIT_MOV_U8)
55 				return push_inst(compiler, AND | D(dst) | S1(src2) | IMM(0xff), DR(dst));
56 			FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src2) | IMM(24), DR(dst)));
57 			return push_inst(compiler, SRA | D(dst) | S1(dst) | IMM(24), DR(dst));
58 		}
59 		SLJIT_ASSERT(dst == src2);
60 		return SLJIT_SUCCESS;
61 
62 	case SLJIT_MOV_U16:
63 	case SLJIT_MOV_S16:
64 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
65 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
66 			FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src2) | IMM(16), DR(dst)));
67 			return push_inst(compiler, (op == SLJIT_MOV_S16 ? SRA : SRL) | D(dst) | S1(dst) | IMM(16), DR(dst));
68 		}
69 		SLJIT_ASSERT(dst == src2);
70 		return SLJIT_SUCCESS;
71 
72 	case SLJIT_NOT:
73 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
74 		return push_inst(compiler, XNOR | (flags & SET_FLAGS) | D(dst) | S1(0) | S2(src2), DRF(dst, flags));
75 
76 	case SLJIT_CLZ:
77 		SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & SRC2_IMM));
78 		FAIL_IF(push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(src2) | S2(0), SET_FLAGS));
79 		FAIL_IF(push_inst(compiler, OR | D(TMP_REG1) | S1(0) | S2(src2), DR(TMP_REG1)));
80 		FAIL_IF(push_inst(compiler, BICC | DA(0x1) | (7 & DISP_MASK), UNMOVABLE_INS));
81 		FAIL_IF(push_inst(compiler, OR | D(dst) | S1(0) | IMM(32), UNMOVABLE_INS));
82 		FAIL_IF(push_inst(compiler, OR | D(dst) | S1(0) | IMM(-1), DR(dst)));
83 
84 		/* Loop. */
85 		FAIL_IF(push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(TMP_REG1) | S2(0), SET_FLAGS));
86 		FAIL_IF(push_inst(compiler, SLL | D(TMP_REG1) | S1(TMP_REG1) | IMM(1), DR(TMP_REG1)));
87 		FAIL_IF(push_inst(compiler, BICC | DA(0xe) | ((sljit_ins)-2 & DISP_MASK), UNMOVABLE_INS));
88 		return push_inst(compiler, ADD | D(dst) | S1(dst) | IMM(1), UNMOVABLE_INS);
89 
90 	case SLJIT_ADD:
91 		compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
92 		return push_inst(compiler, ADD | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
93 
94 	case SLJIT_ADDC:
95 		compiler->status_flags_state = SLJIT_CURRENT_FLAGS_ADD;
96 		return push_inst(compiler, ADDC | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
97 
98 	case SLJIT_SUB:
99 		compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
100 		return push_inst(compiler, SUB | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
101 
102 	case SLJIT_SUBC:
103 		compiler->status_flags_state = SLJIT_CURRENT_FLAGS_SUB;
104 		return push_inst(compiler, SUBC | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
105 
106 	case SLJIT_MUL:
107 		compiler->status_flags_state = 0;
108 		FAIL_IF(push_inst(compiler, SMUL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
109 		if (!(flags & SET_FLAGS))
110 			return SLJIT_SUCCESS;
111 		FAIL_IF(push_inst(compiler, SRA | D(TMP_REG1) | S1(dst) | IMM(31), DR(TMP_REG1)));
112 		FAIL_IF(push_inst(compiler, RDY | D(TMP_LINK), DR(TMP_LINK)));
113 		return push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(TMP_REG1) | S2(TMP_LINK), MOVABLE_INS | SET_FLAGS);
114 
115 	case SLJIT_AND:
116 		return push_inst(compiler, AND | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
117 
118 	case SLJIT_OR:
119 		return push_inst(compiler, OR | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
120 
121 	case SLJIT_XOR:
122 		return push_inst(compiler, XOR | (flags & SET_FLAGS) | D(dst) | S1(src1) | ARG2(flags, src2), DRF(dst, flags));
123 
124 	case SLJIT_SHL:
125 		FAIL_IF(push_inst(compiler, SLL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
126 		return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
127 
128 	case SLJIT_LSHR:
129 		FAIL_IF(push_inst(compiler, SRL | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
130 		return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
131 
132 	case SLJIT_ASHR:
133 		FAIL_IF(push_inst(compiler, SRA | D(dst) | S1(src1) | ARG2(flags, src2), DR(dst)));
134 		return !(flags & SET_FLAGS) ? SLJIT_SUCCESS : push_inst(compiler, SUB | SET_FLAGS | D(0) | S1(dst) | S2(0), SET_FLAGS);
135 	}
136 
137 	SLJIT_UNREACHABLE();
138 	return SLJIT_SUCCESS;
139 }
140 
call_with_args(struct sljit_compiler * compiler,sljit_s32 arg_types,sljit_s32 * src)141 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
142 {
143 	sljit_s32 reg_index = 8;
144 	sljit_s32 word_reg_index = 8;
145 	sljit_s32 float_arg_index = 1;
146 	sljit_s32 double_arg_count = 0;
147 	sljit_u32 float_offset = (16 + 6) * sizeof(sljit_sw);
148 	sljit_s32 types = 0;
149 	sljit_s32 reg = 0;
150 	sljit_s32 move_to_tmp2 = 0;
151 
152 	if (src)
153 		reg = reg_map[*src & REG_MASK];
154 
155 	arg_types >>= SLJIT_ARG_SHIFT;
156 
157 	while (arg_types) {
158 		types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
159 
160 		switch (arg_types & SLJIT_ARG_MASK) {
161 		case SLJIT_ARG_TYPE_F64:
162 			float_arg_index++;
163 			double_arg_count++;
164 			if (reg_index == reg || reg_index + 1 == reg)
165 				move_to_tmp2 = 1;
166 			reg_index += 2;
167 			break;
168 		case SLJIT_ARG_TYPE_F32:
169 			float_arg_index++;
170 			if (reg_index == reg)
171 				move_to_tmp2 = 1;
172 			reg_index++;
173 			break;
174 		default:
175 			if (reg_index != word_reg_index && reg_index == reg)
176 				move_to_tmp2 = 1;
177 			reg_index++;
178 			word_reg_index++;
179 			break;
180 		}
181 
182 		arg_types >>= SLJIT_ARG_SHIFT;
183 	}
184 
185 	if (move_to_tmp2) {
186 		if (reg < 14)
187 			FAIL_IF(push_inst(compiler, OR | D(TMP_REG1) | S1(0) | S2A(reg), DR(TMP_REG1)));
188 		*src = TMP_REG1;
189 	}
190 
191 	arg_types = types;
192 
193 	while (arg_types) {
194 		switch (arg_types & SLJIT_ARG_MASK) {
195 		case SLJIT_ARG_TYPE_F64:
196 			float_arg_index--;
197 			if (float_arg_index == 4 && double_arg_count == 4) {
198 				/* The address is not doubleword aligned, so two instructions are required to store the double. */
199 				FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | S1(SLJIT_SP) | IMM((16 + 7) * sizeof(sljit_sw)), MOVABLE_INS));
200 				FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | (1 << 25) | S1(SLJIT_SP) | IMM((16 + 8) * sizeof(sljit_sw)), MOVABLE_INS));
201 			}
202 			else
203 				FAIL_IF(push_inst(compiler, STDF | FD(float_arg_index) | S1(SLJIT_SP) | IMM(float_offset), MOVABLE_INS));
204 			float_offset -= sizeof(sljit_f64);
205 			break;
206 		case SLJIT_ARG_TYPE_F32:
207 			float_arg_index--;
208 			FAIL_IF(push_inst(compiler, STF | FD(float_arg_index) | S1(SLJIT_SP) | IMM(float_offset), MOVABLE_INS));
209 			float_offset -= sizeof(sljit_f64);
210 			break;
211 		default:
212 			break;
213 		}
214 
215 		arg_types >>= SLJIT_ARG_SHIFT;
216 	}
217 
218 	float_offset = (16 + 6) * sizeof(sljit_sw);
219 
220 	while (types) {
221 		switch (types & SLJIT_ARG_MASK) {
222 		case SLJIT_ARG_TYPE_F64:
223 			reg_index -= 2;
224 			if (reg_index < 14) {
225 				if ((reg_index & 0x1) != 0) {
226 					FAIL_IF(push_inst(compiler, LDUW | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
227 					if (reg_index < 8 + 6 - 1)
228 						FAIL_IF(push_inst(compiler, LDUW | DA(reg_index + 1) | S1(SLJIT_SP) | IMM(float_offset + sizeof(sljit_sw)), reg_index + 1));
229 				}
230 				else
231 					FAIL_IF(push_inst(compiler, LDD | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
232 			}
233 			float_offset -= sizeof(sljit_f64);
234 			break;
235 		case SLJIT_ARG_TYPE_F32:
236 			reg_index--;
237 			if (reg_index < 8 + 6)
238 				FAIL_IF(push_inst(compiler, LDUW | DA(reg_index) | S1(SLJIT_SP) | IMM(float_offset), reg_index));
239 			float_offset -= sizeof(sljit_f64);
240 			break;
241 		default:
242 			reg_index--;
243 			word_reg_index--;
244 
245 			if (reg_index != word_reg_index) {
246 				if (reg_index < 14)
247 					FAIL_IF(push_inst(compiler, OR | DA(reg_index) | S1(0) | S2A(word_reg_index), reg_index));
248 				else
249 					FAIL_IF(push_inst(compiler, STW | DA(word_reg_index) | S1(SLJIT_SP) | IMM(92), word_reg_index));
250 			}
251 			break;
252 		}
253 
254 		types >>= SLJIT_ARG_SHIFT;
255 	}
256 
257 	return SLJIT_SUCCESS;
258 }
259 
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value)260 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
261 {
262 	FAIL_IF(push_inst(compiler, SETHI | D(dst) | ((init_value >> 10) & 0x3fffff), DR(dst)));
263 	return push_inst(compiler, OR | D(dst) | S1(dst) | IMM_ARG | (init_value & 0x3ff), DR(dst));
264 }
265 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)266 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
267 {
268 	sljit_ins *inst = (sljit_ins *)addr;
269 	SLJIT_UNUSED_ARG(executable_offset);
270 
271 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
272 	SLJIT_ASSERT(((inst[0] & 0xc1c00000) == 0x01000000) && ((inst[1] & 0xc1f82000) == 0x80102000));
273 	inst[0] = (inst[0] & 0xffc00000) | ((new_target >> 10) & 0x3fffff);
274 	inst[1] = (inst[1] & 0xfffffc00) | (new_target & 0x3ff);
275 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
276 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
277 	SLJIT_CACHE_FLUSH(inst, inst + 2);
278 }
279 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)280 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
281 {
282 	sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);
283 }
284