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