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 /* ppc 32-bit arch dependent functions. */
28 
load_immediate(struct sljit_compiler * compiler,sljit_s32 reg,sljit_sw imm)29 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
30 {
31 	if (imm <= SIMM_MAX && imm >= SIMM_MIN)
32 		return push_inst(compiler, ADDI | D(reg) | A(0) | IMM(imm));
33 
34 	if (!(imm & ~0xffff))
35 		return push_inst(compiler, ORI | S(TMP_ZERO) | A(reg) | IMM(imm));
36 
37 	FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(imm >> 16)));
38 	return (imm & 0xffff) ? push_inst(compiler, ORI | S(reg) | A(reg) | IMM(imm)) : SLJIT_SUCCESS;
39 }
40 
41 #define INS_CLEAR_LEFT(dst, src, from) \
42 	(RLWINM | S(src) | A(dst) | ((from) << 6) | (31 << 1))
43 
emit_single_op(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 flags,sljit_s32 dst,sljit_s32 src1,sljit_s32 src2)44 static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
45 	sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
46 {
47 	switch (op) {
48 	case SLJIT_MOV:
49 	case SLJIT_MOV_U32:
50 	case SLJIT_MOV_S32:
51 	case SLJIT_MOV_P:
52 		SLJIT_ASSERT(src1 == TMP_REG1);
53 		if (dst != src2)
54 			return push_inst(compiler, OR | S(src2) | A(dst) | B(src2));
55 		return SLJIT_SUCCESS;
56 
57 	case SLJIT_MOV_U8:
58 	case SLJIT_MOV_S8:
59 		SLJIT_ASSERT(src1 == TMP_REG1);
60 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
61 			if (op == SLJIT_MOV_S8)
62 				return push_inst(compiler, EXTSB | S(src2) | A(dst));
63 			return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 24));
64 		}
65 		else if ((flags & REG_DEST) && op == SLJIT_MOV_S8)
66 			return push_inst(compiler, EXTSB | S(src2) | A(dst));
67 		else {
68 			SLJIT_ASSERT(dst == src2);
69 		}
70 		return SLJIT_SUCCESS;
71 
72 	case SLJIT_MOV_U16:
73 	case SLJIT_MOV_S16:
74 		SLJIT_ASSERT(src1 == TMP_REG1);
75 		if ((flags & (REG_DEST | REG2_SOURCE)) == (REG_DEST | REG2_SOURCE)) {
76 			if (op == SLJIT_MOV_S16)
77 				return push_inst(compiler, EXTSH | S(src2) | A(dst));
78 			return push_inst(compiler, INS_CLEAR_LEFT(dst, src2, 16));
79 		}
80 		else {
81 			SLJIT_ASSERT(dst == src2);
82 		}
83 		return SLJIT_SUCCESS;
84 
85 	case SLJIT_NOT:
86 		SLJIT_ASSERT(src1 == TMP_REG1);
87 		return push_inst(compiler, NOR | RC(flags) | S(src2) | A(dst) | B(src2));
88 
89 	case SLJIT_CLZ:
90 		SLJIT_ASSERT(src1 == TMP_REG1);
91 		return push_inst(compiler, CNTLZW | S(src2) | A(dst));
92 
93 	case SLJIT_ADD:
94 		if (flags & ALT_FORM1) {
95 			/* Setting XER SO is not enough, CR SO is also needed. */
96 			return push_inst(compiler, ADD | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
97 		}
98 
99 		if (flags & ALT_FORM2) {
100 			/* Flags does not set: BIN_IMM_EXTS unnecessary. */
101 			SLJIT_ASSERT(src2 == TMP_REG2);
102 
103 			if (flags & ALT_FORM3)
104 				return push_inst(compiler, ADDIS | D(dst) | A(src1) | compiler->imm);
105 
106 			if (flags & ALT_FORM4) {
107 				FAIL_IF(push_inst(compiler, ADDIS | D(dst) | A(src1) | (((compiler->imm >> 16) & 0xffff) + ((compiler->imm >> 15) & 0x1))));
108 				src1 = dst;
109 			}
110 
111 			return push_inst(compiler, ADDI | D(dst) | A(src1) | (compiler->imm & 0xffff));
112 		}
113 		if (flags & ALT_FORM3) {
114 			SLJIT_ASSERT(src2 == TMP_REG2);
115 			return push_inst(compiler, ADDIC | D(dst) | A(src1) | compiler->imm);
116 		}
117 		SLJIT_ASSERT(!(flags & ALT_FORM4));
118 		if (!(flags & ALT_SET_FLAGS))
119 			return push_inst(compiler, ADD | D(dst) | A(src1) | B(src2));
120 		if (flags & ALT_FORM5)
121 			return push_inst(compiler, ADDC | RC(ALT_SET_FLAGS) | D(dst) | A(src1) | B(src2));
122 		return push_inst(compiler, ADD | RC(flags) | D(dst) | A(src1) | B(src2));
123 
124 	case SLJIT_ADDC:
125 		return push_inst(compiler, ADDE | D(dst) | A(src1) | B(src2));
126 
127 	case SLJIT_SUB:
128 		if (flags & ALT_FORM1) {
129 			if (flags & ALT_FORM2) {
130 				FAIL_IF(push_inst(compiler, CMPLI | CRD(0) | A(src1) | compiler->imm));
131 				if (!(flags & ALT_FORM3))
132 					return SLJIT_SUCCESS;
133 				return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
134 			}
135 			FAIL_IF(push_inst(compiler, CMPL | CRD(0) | A(src1) | B(src2)));
136 			if (!(flags & ALT_FORM3))
137 				return SLJIT_SUCCESS;
138 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
139 		}
140 
141 		if (flags & ALT_FORM2) {
142 			if (flags & ALT_FORM3) {
143 				FAIL_IF(push_inst(compiler, CMPI | CRD(0) | A(src1) | compiler->imm));
144 				if (!(flags & ALT_FORM4))
145 					return SLJIT_SUCCESS;
146 				return push_inst(compiler, ADDI | D(dst) | A(src1) | (-compiler->imm & 0xffff));
147 			}
148 			FAIL_IF(push_inst(compiler, CMP | CRD(0) | A(src1) | B(src2)));
149 			if (!(flags & ALT_FORM4))
150 				return SLJIT_SUCCESS;
151 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
152 		}
153 
154 		if (flags & ALT_FORM3) {
155 			/* Setting XER SO is not enough, CR SO is also needed. */
156 			if (src1 != TMP_ZERO)
157 				return push_inst(compiler, SUBF | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
158 			return push_inst(compiler, NEG | OE(ALT_SET_FLAGS) | RC(ALT_SET_FLAGS) | D(dst) | A(src2));
159 		}
160 
161 		if (flags & ALT_FORM4) {
162 			/* Flags does not set: BIN_IMM_EXTS unnecessary. */
163 			SLJIT_ASSERT(src2 == TMP_REG2);
164 			return push_inst(compiler, SUBFIC | D(dst) | A(src1) | compiler->imm);
165 		}
166 
167 		if (!(flags & ALT_SET_FLAGS)) {
168 			SLJIT_ASSERT(src1 != TMP_ZERO);
169 			return push_inst(compiler, SUBF | D(dst) | A(src2) | B(src1));
170 		}
171 
172 		if (flags & ALT_FORM5)
173 			return push_inst(compiler, SUBFC | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
174 
175 		if (src1 != TMP_ZERO)
176 			return push_inst(compiler, SUBF | RC(ALT_SET_FLAGS) | D(dst) | A(src2) | B(src1));
177 		return push_inst(compiler, NEG | RC(ALT_SET_FLAGS) | D(dst) | A(src2));
178 
179 	case SLJIT_SUBC:
180 		return push_inst(compiler, SUBFE | D(dst) | A(src2) | B(src1));
181 
182 	case SLJIT_MUL:
183 		if (flags & ALT_FORM1) {
184 			SLJIT_ASSERT(src2 == TMP_REG2);
185 			return push_inst(compiler, MULLI | D(dst) | A(src1) | compiler->imm);
186 		}
187 		return push_inst(compiler, MULLW | OE(flags) | RC(flags) | D(dst) | A(src2) | B(src1));
188 
189 	case SLJIT_AND:
190 		if (flags & ALT_FORM1) {
191 			SLJIT_ASSERT(src2 == TMP_REG2);
192 			return push_inst(compiler, ANDI | S(src1) | A(dst) | compiler->imm);
193 		}
194 		if (flags & ALT_FORM2) {
195 			SLJIT_ASSERT(src2 == TMP_REG2);
196 			return push_inst(compiler, ANDIS | S(src1) | A(dst) | compiler->imm);
197 		}
198 		return push_inst(compiler, AND | RC(flags) | S(src1) | A(dst) | B(src2));
199 
200 	case SLJIT_OR:
201 		if (flags & ALT_FORM1) {
202 			SLJIT_ASSERT(src2 == TMP_REG2);
203 			return push_inst(compiler, ORI | S(src1) | A(dst) | compiler->imm);
204 		}
205 		if (flags & ALT_FORM2) {
206 			SLJIT_ASSERT(src2 == TMP_REG2);
207 			return push_inst(compiler, ORIS | S(src1) | A(dst) | compiler->imm);
208 		}
209 		if (flags & ALT_FORM3) {
210 			SLJIT_ASSERT(src2 == TMP_REG2);
211 			FAIL_IF(push_inst(compiler, ORI | S(src1) | A(dst) | IMM(compiler->imm)));
212 			return push_inst(compiler, ORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
213 		}
214 		return push_inst(compiler, OR | RC(flags) | S(src1) | A(dst) | B(src2));
215 
216 	case SLJIT_XOR:
217 		if (flags & ALT_FORM1) {
218 			SLJIT_ASSERT(src2 == TMP_REG2);
219 			return push_inst(compiler, XORI | S(src1) | A(dst) | compiler->imm);
220 		}
221 		if (flags & ALT_FORM2) {
222 			SLJIT_ASSERT(src2 == TMP_REG2);
223 			return push_inst(compiler, XORIS | S(src1) | A(dst) | compiler->imm);
224 		}
225 		if (flags & ALT_FORM3) {
226 			SLJIT_ASSERT(src2 == TMP_REG2);
227 			FAIL_IF(push_inst(compiler, XORI | S(src1) | A(dst) | IMM(compiler->imm)));
228 			return push_inst(compiler, XORIS | S(dst) | A(dst) | IMM(compiler->imm >> 16));
229 		}
230 		return push_inst(compiler, XOR | RC(flags) | S(src1) | A(dst) | B(src2));
231 
232 	case SLJIT_SHL:
233 		if (flags & ALT_FORM1) {
234 			SLJIT_ASSERT(src2 == TMP_REG2);
235 			compiler->imm &= 0x1f;
236 			return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11) | ((31 - compiler->imm) << 1));
237 		}
238 		return push_inst(compiler, SLW | RC(flags) | S(src1) | A(dst) | B(src2));
239 
240 	case SLJIT_LSHR:
241 		if (flags & ALT_FORM1) {
242 			SLJIT_ASSERT(src2 == TMP_REG2);
243 			compiler->imm &= 0x1f;
244 			return push_inst(compiler, RLWINM | RC(flags) | S(src1) | A(dst) | (((32 - compiler->imm) & 0x1f) << 11) | (compiler->imm << 6) | (31 << 1));
245 		}
246 		return push_inst(compiler, SRW | RC(flags) | S(src1) | A(dst) | B(src2));
247 
248 	case SLJIT_ASHR:
249 		if (flags & ALT_FORM1) {
250 			SLJIT_ASSERT(src2 == TMP_REG2);
251 			compiler->imm &= 0x1f;
252 			return push_inst(compiler, SRAWI | RC(flags) | S(src1) | A(dst) | (compiler->imm << 11));
253 		}
254 		return push_inst(compiler, SRAW | RC(flags) | S(src1) | A(dst) | B(src2));
255 	}
256 
257 	SLJIT_UNREACHABLE();
258 	return SLJIT_SUCCESS;
259 }
260 
emit_const(struct sljit_compiler * compiler,sljit_s32 reg,sljit_sw init_value)261 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw init_value)
262 {
263 	FAIL_IF(push_inst(compiler, ADDIS | D(reg) | A(0) | IMM(init_value >> 16)));
264 	return push_inst(compiler, ORI | S(reg) | A(reg) | IMM(init_value));
265 }
266 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)267 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
268 {
269 	sljit_ins *inst = (sljit_ins *)addr;
270 	SLJIT_UNUSED_ARG(executable_offset);
271 
272 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
273 	SLJIT_ASSERT((inst[0] & 0xfc1f0000) == ADDIS && (inst[1] & 0xfc000000) == ORI);
274 	inst[0] = (inst[0] & 0xffff0000) | ((new_target >> 16) & 0xffff);
275 	inst[1] = (inst[1] & 0xffff0000) | (new_target & 0xffff);
276 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
277 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
278 	SLJIT_CACHE_FLUSH(inst, inst + 2);
279 }
280 
sljit_set_const(sljit_uw addr,sljit_sw new_constant,sljit_sw executable_offset)281 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
282 {
283 	sljit_set_jump_addr(addr, (sljit_uw)new_constant, executable_offset);
284 }
285