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_r,sljit_sw imm,sljit_s32 tmp_r)27 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm, sljit_s32 tmp_r)
28 {
29 	sljit_sw high;
30 
31 	if (imm <= SIMM_MAX && imm >= SIMM_MIN)
32 		return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));
33 
34 	if (imm <= 0x7fffffffl && imm >= S32_MIN) {
35 		if (imm > S32_MAX) {
36 			SLJIT_ASSERT((imm & 0x800) != 0);
37 			FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
38 			return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
39 		}
40 
41 		if ((imm & 0x800) != 0)
42 			imm += 0x1000;
43 
44 		FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
45 
46 		if ((imm & 0xfff) == 0)
47 			return SLJIT_SUCCESS;
48 
49 		return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
50 	}
51 
52 	/* Trailing zeroes could be used to produce shifted immediates. */
53 
54 	if (imm <= 0x7ffffffffffl && imm >= -0x80000000000l) {
55 		high = imm >> 12;
56 
57 		if (imm & 0x800)
58 			high = ~high;
59 
60 		if (high > S32_MAX) {
61 			SLJIT_ASSERT((high & 0x800) != 0);
62 			FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
63 			FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));
64 		} else {
65 			if ((high & 0x800) != 0)
66 				high += 0x1000;
67 
68 			FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(high & ~0xfff)));
69 
70 			if ((high & 0xfff) != 0)
71 				FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));
72 		}
73 
74 		FAIL_IF(push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(12)));
75 
76 		if ((imm & 0xfff) != 0)
77 			return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
78 
79 		return SLJIT_SUCCESS;
80 	}
81 
82 	SLJIT_ASSERT(dst_r != tmp_r);
83 
84 	high = imm >> 32;
85 	imm = (sljit_s32)imm;
86 
87 	if ((imm & 0x80000000l) != 0)
88 		high = ~high;
89 
90 	if (high <= 0x7ffff && high >= -0x80000) {
91 		FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high << 12)));
92 		high = 0x1000;
93 	} else {
94 		if ((high & 0x800) != 0)
95 			high += 0x1000;
96 
97 		FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high & ~0xfff)));
98 		high &= 0xfff;
99 	}
100 
101 	if (imm <= SIMM_MAX && imm >= SIMM_MIN) {
102 		FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm)));
103 		imm = 0;
104 	} else if (imm > S32_MAX) {
105 		SLJIT_ASSERT((imm & 0x800) != 0);
106 
107 		FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
108 		imm = 0x1000 | (imm & 0xfff);
109 	} else {
110 		if ((imm & 0x800) != 0)
111 			imm += 0x1000;
112 
113 		FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
114 		imm &= 0xfff;
115 	}
116 
117 	if ((high & 0xfff) != 0)
118 		FAIL_IF(push_inst(compiler, ADDI | RD(tmp_r) | RS1(tmp_r) | IMM_I(high)));
119 
120 	if (imm & 0x1000)
121 		FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));
122 	else if (imm != 0)
123 		FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));
124 
125 	FAIL_IF(push_inst(compiler, SLLI | RD(tmp_r) | RS1(tmp_r) | IMM_I((high & 0x1000) ? 20 : 32)));
126 	return push_inst(compiler, XOR | RD(dst_r) | RS1(dst_r) | RS2(tmp_r));
127 }
128 
sljit_emit_fset64(struct sljit_compiler * compiler,sljit_s32 freg,sljit_f64 value)129 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
130 	sljit_s32 freg, sljit_f64 value)
131 {
132 	union {
133 		sljit_sw imm;
134 		sljit_f64 value;
135 	} u;
136 
137 	CHECK_ERROR();
138 	CHECK(check_sljit_emit_fset64(compiler, freg, value));
139 
140 	u.value = value;
141 
142 	if (u.imm == 0)
143 		return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_ZERO) | FRD(freg));
144 
145 	FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm, TMP_REG3));
146 	return push_inst(compiler, FMV_W_X | (1 << 25) | RS1(TMP_REG1) | FRD(freg));
147 }
148 
sljit_emit_fcopy(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 freg,sljit_s32 reg)149 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
150 	sljit_s32 freg, sljit_s32 reg)
151 {
152 	sljit_ins inst;
153 
154 	CHECK_ERROR();
155 	CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));
156 
157 	if (GET_OPCODE(op) == SLJIT_COPY_TO_F64)
158 		inst = FMV_W_X | RS1(reg) | FRD(freg);
159 	else
160 		inst = FMV_X_W | FRS1(freg) | RD(reg);
161 
162 	if (!(op & SLJIT_32))
163 		inst |= (sljit_ins)1 << 25;
164 
165 	return push_inst(compiler, inst);
166 }
167 
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value,sljit_ins last_ins)168 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
169 {
170 	sljit_sw high;
171 
172 	if ((init_value & 0x800) != 0)
173 		init_value += 0x1000;
174 
175 	high = init_value >> 32;
176 
177 	if ((init_value & 0x80000000l) != 0)
178 		high = ~high;
179 
180 	if ((high & 0x800) != 0)
181 		high += 0x1000;
182 
183 	FAIL_IF(push_inst(compiler, LUI | RD(TMP_REG3) | (sljit_ins)(high & ~0xfff)));
184 	FAIL_IF(push_inst(compiler, ADDI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(high)));
185 	FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
186 	FAIL_IF(push_inst(compiler, SLLI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(32)));
187 	FAIL_IF(push_inst(compiler, XOR | RD(dst) | RS1(dst) | RS2(TMP_REG3)));
188 	return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
189 }
190 
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)191 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
192 {
193 	sljit_ins *inst = (sljit_ins*)addr;
194 	sljit_sw high;
195 	SLJIT_UNUSED_ARG(executable_offset);
196 
197 	if ((new_target & 0x800) != 0)
198 		new_target += 0x1000;
199 
200 	high = (sljit_sw)new_target >> 32;
201 
202 	if ((new_target & 0x80000000l) != 0)
203 		high = ~high;
204 
205 	if ((high & 0x800) != 0)
206 		high += 0x1000;
207 
208 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
209 
210 	SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
211 	inst[0] = (inst[0] & 0xfff) | (sljit_ins)(high & ~0xfff);
212 	SLJIT_ASSERT((inst[1] & 0x707f) == ADDI);
213 	inst[1] = (inst[1] & 0xfffff) | IMM_I(high);
214 	SLJIT_ASSERT((inst[2] & 0x7f) == LUI);
215 	inst[2] = (inst[2] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
216 	SLJIT_ASSERT((inst[5] & 0x707f) == ADDI || (inst[5] & 0x707f) == JALR);
217 	inst[5] = (inst[5] & 0xfffff) | IMM_I(new_target);
218 	SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
219 
220 	inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
221 	SLJIT_CACHE_FLUSH(inst, inst + 5);
222 }
223