Debian Codegraph @

src/3rdparty/pcre2/deps/sljit/sljit_src/sljitNativeMIPS_32.c

raw
1 /*
2 * Stack-less Just-In-Time compiler
3 *
4 * Copyright Zoltan Herczeg ([email protected]). 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 32-bit arch dependent functions. */
28
29 static sljit_s32 emit_copysign(struct sljit_compiler *compiler, sljit_s32 op,
30 sljit_sw src1, sljit_sw src2, sljit_sw dst)
31 {
32 int is_32 = (op & SLJIT_32);
33 sljit_ins mfhc = MFC1, mthc = MTC1;
34 sljit_ins src1_r = FS(src1), src2_r = FS(src2), dst_r = FS(dst);
35
36 if (!is_32) {
37 switch (cpu_feature_list & CPU_FEATURE_FR) {
38 #if defined(SLJIT_MIPS_REV) && SLJIT_MIPS_REV >= 2
39 case CPU_FEATURE_FR:
40 mfhc = MFHC1;
41 mthc = MTHC1;
42 break;
43 #endif /* SLJIT_MIPS_REV >= 2 */
44 default:
45 src1_r |= (1 << 11);
46 src2_r |= (1 << 11);
47 dst_r |= (1 << 11);
48 break;
49 }
50 }
51
52 FAIL_IF(push_inst(compiler, mfhc | T(TMP_REG1) | src1_r, DR(TMP_REG1)));
53 FAIL_IF(push_inst(compiler, mfhc | T(TMP_REG2) | src2_r, DR(TMP_REG2)));
54 if (!is_32 && src1 != dst)
55 FAIL_IF(push_inst(compiler, MOV_fmt(FMT_S) | FS(src1) | FD(dst), MOVABLE_INS));
56 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
57 else
58 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
59 #endif /* MIPS III */
60 FAIL_IF(push_inst(compiler, XOR | T(TMP_REG1) | D(TMP_REG2) | S(TMP_REG2), DR(TMP_REG2)));
61 FAIL_IF(push_inst(compiler, SRL | T(TMP_REG2) | D(TMP_REG2) | SH_IMM(31), DR(TMP_REG2)));
62 FAIL_IF(push_inst(compiler, SLL | T(TMP_REG2) | D(TMP_REG2) | SH_IMM(31), DR(TMP_REG2)));
63 FAIL_IF(push_inst(compiler, XOR | T(TMP_REG2) | D(TMP_REG1) | S(TMP_REG1), DR(TMP_REG1)));
64 FAIL_IF(push_inst(compiler, mthc | T(TMP_REG1) | dst_r, MOVABLE_INS));
65 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
66 if (mthc == MTC1)
67 return push_inst(compiler, NOP, UNMOVABLE_INS);
68 #endif /* MIPS III */
69 return SLJIT_SUCCESS;
70 }
71
72 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_ar, sljit_sw imm)
73 {
74 if (!(imm & ~0xffff))
75 return push_inst(compiler, ORI | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
76
77 if (imm < 0 && imm >= SIMM_MIN)
78 return push_inst(compiler, ADDIU | SA(0) | TA(dst_ar) | IMM(imm), dst_ar);
79
80 FAIL_IF(push_inst(compiler, LUI | TA(dst_ar) | IMM(imm >> 16), dst_ar));
81 return (imm & 0xffff) ? push_inst(compiler, ORI | SA(dst_ar) | TA(dst_ar) | IMM(imm), dst_ar) : SLJIT_SUCCESS;
82 }
83
84 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value)
85 {
86 FAIL_IF(push_inst(compiler, LUI | T(dst) | IMM(init_value >> 16), DR(dst)));
87 return push_inst(compiler, ORI | S(dst) | T(dst) | IMM(init_value), DR(dst));
88 }
89
90 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
91 sljit_s32 freg, sljit_f64 value)
92 {
93 union {
94 struct {
95 #if defined(SLJIT_LITTLE_ENDIAN) && SLJIT_LITTLE_ENDIAN
96 sljit_s32 lo;
97 sljit_s32 hi;
98 #else /* !SLJIT_LITTLE_ENDIAN */
99 sljit_s32 hi;
100 sljit_s32 lo;
101 #endif /* SLJIT_LITTLE_ENDIAN */
102 } bin;
103 sljit_f64 value;
104 } u;
105
106 CHECK_ERROR();
107 CHECK(check_sljit_emit_fset64(compiler, freg, value));
108
109 u.value = value;
110
111 if (u.bin.lo != 0)
112 FAIL_IF(load_immediate(compiler, DR(TMP_REG1), u.bin.lo));
113 if (u.bin.hi != 0)
114 FAIL_IF(load_immediate(compiler, DR(TMP_REG2), u.bin.hi));
115
116 FAIL_IF(push_inst(compiler, MTC1 | (u.bin.lo != 0 ? T(TMP_REG1) : TA(0)) | FS(freg), MOVABLE_INS));
117 switch (cpu_feature_list & CPU_FEATURE_FR) {
118 #if defined(SLJIT_MIPS_REV) && SLJIT_MIPS_REV >= 2
119 case CPU_FEATURE_FR:
120 return push_inst(compiler, MTHC1 | (u.bin.hi != 0 ? T(TMP_REG2) : TA(0)) | FS(freg), MOVABLE_INS);
121 #endif /* SLJIT_MIPS_REV >= 2 */
122 default:
123 FAIL_IF(push_inst(compiler, MTC1 | (u.bin.hi != 0 ? T(TMP_REG2) : TA(0)) | FS(freg) | (1 << 11), MOVABLE_INS));
124 break;
125 }
126 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
127 FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
128 #endif /* MIPS III */
129 return SLJIT_SUCCESS;
130 }
131
132 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
133 sljit_s32 freg, sljit_s32 reg)
134 {
135 sljit_s32 reg2 = 0;
136 sljit_ins inst = FS(freg);
137 sljit_ins mthc = MTC1, mfhc = MFC1;
138 int is_32 = (op & SLJIT_32);
139
140 CHECK_ERROR();
141 CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));
142
143 op = GET_OPCODE(op);
144 if (reg & REG_PAIR_MASK) {
145 reg2 = REG_PAIR_SECOND(reg);
146 reg = REG_PAIR_FIRST(reg);
147
148 inst |= T(reg2);
149
150 if (op == SLJIT_COPY_TO_F64)
151 FAIL_IF(push_inst(compiler, MTC1 | inst, MOVABLE_INS));
152 else
153 FAIL_IF(push_inst(compiler, MFC1 | inst, DR(reg2)));
154
155 inst = FS(freg) | (1 << 11);
156 #if defined(SLJIT_MIPS_REV) && SLJIT_MIPS_REV >= 2
157 if (cpu_feature_list & CPU_FEATURE_FR) {
158 mthc = MTHC1;
159 mfhc = MFHC1;
160 inst = FS(freg);
161 }
162 #endif /* SLJIT_MIPS_REV >= 2 */
163 }
164
165 inst |= T(reg);
166 if (!is_32 && !reg2) {
167 switch (cpu_feature_list & CPU_FEATURE_FR) {
168 #if defined(SLJIT_MIPS_REV) && SLJIT_MIPS_REV >= 2
169 case CPU_FEATURE_FR:
170 mthc = MTHC1;
171 mfhc = MFHC1;
172 break;
173 #endif /* SLJIT_MIPS_REV >= 2 */
174 default:
175 inst |= (1 << 11);
176 break;
177 }
178 }
179
180 if (op == SLJIT_COPY_TO_F64)
181 FAIL_IF(push_inst(compiler, mthc | inst, MOVABLE_INS));
182 else
183 FAIL_IF(push_inst(compiler, mfhc | inst, DR(reg)));
184
185 #if !defined(SLJIT_MIPS_REV) || SLJIT_MIPS_REV <= 1
186 if (mthc == MTC1 || mfhc == MFC1)
187 return push_inst(compiler, NOP, UNMOVABLE_INS);
188 #endif /* MIPS III */
189 return SLJIT_SUCCESS;
190 }
191
192 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
193 {
194 sljit_ins *inst = (sljit_ins *)addr;
195 SLJIT_UNUSED_ARG(executable_offset);
196
197 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 0);
198 SLJIT_ASSERT((inst[0] & 0xffe00000) == LUI && (inst[1] & 0xfc000000) == ORI);
199 inst[0] = (inst[0] & 0xffff0000) | IMM(new_target >> 16);
200 inst[1] = (inst[1] & 0xffff0000) | IMM(new_target);
201 SLJIT_UPDATE_WX_FLAGS(inst, inst + 2, 1);
202 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
203 SLJIT_CACHE_FLUSH(inst, inst + 2);
204 }
205
206 static sljit_s32 call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_ins *ins_ptr, sljit_u32 *extra_space)
207 {
208 sljit_u32 is_tail_call = *extra_space & SLJIT_CALL_RETURN;
209 sljit_u32 offset = 0;
210 sljit_s32 float_arg_count = 0;
211 sljit_s32 word_arg_count = 0;
212 sljit_s32 types = 0;
213 sljit_ins prev_ins = NOP;
214 sljit_ins ins = NOP;
215 sljit_u8 offsets[4];
216 sljit_u8 *offsets_ptr = offsets;
217 #if defined(SLJIT_LITTLE_ENDIAN) && SLJIT_LITTLE_ENDIAN
218 sljit_ins f64_hi = TA(7), f64_lo = TA(6);
219 #else
220 sljit_ins f64_hi = TA(6), f64_lo = TA(7);
221 #endif /* SLJIT_LITTLE_ENDIAN */
222
223 SLJIT_ASSERT(reg_map[TMP_REG2] == 4 && freg_map[TMP_FREG1] == 12);
224
225 arg_types >>= SLJIT_ARG_SHIFT;
226
227 /* See ABI description in sljit_emit_enter. */
228
229 while (arg_types) {
230 types = (types << SLJIT_ARG_SHIFT) | (arg_types & SLJIT_ARG_MASK);
231 *offsets_ptr = (sljit_u8)offset;
232
233 switch (arg_types & SLJIT_ARG_MASK) {
234 case SLJIT_ARG_TYPE_F64:
235 if (offset & 0x7) {
236 offset += sizeof(sljit_sw);
237 *offsets_ptr = (sljit_u8)offset;
238 }
239
240 if (word_arg_count == 0 && float_arg_count <= 1)
241 *offsets_ptr = (sljit_u8)(254 + float_arg_count);
242
243 offset += sizeof(sljit_f64);
244 float_arg_count++;
245 break;
246 case SLJIT_ARG_TYPE_F32:
247 if (word_arg_count == 0 && float_arg_count <= 1)
248 *offsets_ptr = (sljit_u8)(254 + float_arg_count);
249
250 offset += sizeof(sljit_f32);
251 float_arg_count++;
252 break;
253 default:
254 offset += sizeof(sljit_sw);
255 word_arg_count++;
256 break;
257 }
258
259 arg_types >>= SLJIT_ARG_SHIFT;
260 offsets_ptr++;
261 }
262
263 /* Stack is aligned to 16 bytes. */
264 SLJIT_ASSERT(offset <= 8 * sizeof(sljit_sw));
265
266 if (offset > 4 * sizeof(sljit_sw) && (!is_tail_call || offset > compiler->args_size)) {
267 if (is_tail_call) {
268 offset = (offset + sizeof(sljit_sw) + 15) & ~(sljit_uw)0xf;
269 FAIL_IF(emit_stack_frame_release(compiler, (sljit_s32)offset, &prev_ins));
270 *extra_space = offset;
271 } else {
272 FAIL_IF(push_inst(compiler, ADDIU | S(SLJIT_SP) | T(SLJIT_SP) | IMM(-16), DR(SLJIT_SP)));
273 *extra_space = 16;
274 }
275 } else {
276 if (is_tail_call)
277 FAIL_IF(emit_stack_frame_release(compiler, 0, &prev_ins));
278 *extra_space = 0;
279 }
280
281 while (types) {
282 --offsets_ptr;
283
284 switch (types & SLJIT_ARG_MASK) {
285 case SLJIT_ARG_TYPE_F64:
286 if (*offsets_ptr < 4 * sizeof(sljit_sw)) {
287 if (prev_ins != NOP)
288 FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
289
290 /* Must be preceded by at least one other argument,
291 * and its starting offset must be 8 because of alignment. */
292 SLJIT_ASSERT((*offsets_ptr >> 2) == 2);
293 switch (cpu_feature_list & CPU_FEATURE_FR) {
294 #if defined(SLJIT_MIPS_REV) && SLJIT_MIPS_REV >= 2
295 case CPU_FEATURE_FR:
296 prev_ins = MFHC1 | f64_hi | FS(float_arg_count);
297 break;
298 #endif /* SLJIT_MIPS_REV >= 2 */
299 default:
300 prev_ins = MFC1 | f64_hi | FS(float_arg_count) | (1 << 11);
301 break;
302 }
303 ins = MFC1 | f64_lo | FS(float_arg_count);
304 } else if (*offsets_ptr < 254)
305 ins = SDC1 | S(SLJIT_SP) | FT(float_arg_count) | IMM(*offsets_ptr);
306 else if (*offsets_ptr == 254)
307 ins = MOV_fmt(FMT_D) | FS(SLJIT_FR0) | FD(TMP_FREG1);
308
309 float_arg_count--;
310 break;
311 case SLJIT_ARG_TYPE_F32:
312 if (*offsets_ptr < 4 * sizeof (sljit_sw))
313 ins = MFC1 | TA(4 + (*offsets_ptr >> 2)) | FS(float_arg_count);
314 else if (*offsets_ptr < 254)
315 ins = SWC1 | S(SLJIT_SP) | FT(float_arg_count) | IMM(*offsets_ptr);
316 else if (*offsets_ptr == 254)
317 ins = MOV_fmt(FMT_S) | FS(SLJIT_FR0) | FD(TMP_FREG1);
318
319 float_arg_count--;
320 break;
321 default:
322 if (*offsets_ptr >= 4 * sizeof (sljit_sw))
323 ins = SW | S(SLJIT_SP) | T(word_arg_count) | IMM(*offsets_ptr);
324 else if ((*offsets_ptr >> 2) != word_arg_count - 1)
325 ins = ADDU | S(word_arg_count) | TA(0) | DA(4 + (*offsets_ptr >> 2));
326 else if (*offsets_ptr == 0)
327 ins = ADDU | S(SLJIT_R0) | TA(0) | DA(4);
328
329 word_arg_count--;
330 break;
331 }
332
333 if (ins != NOP) {
334 if (prev_ins != NOP)
335 FAIL_IF(push_inst(compiler, prev_ins, MOVABLE_INS));
336 prev_ins = ins;
337 ins = NOP;
338 }
339
340 types >>= SLJIT_ARG_SHIFT;
341 }
342
343 *ins_ptr = prev_ins;
344
345 return SLJIT_SUCCESS;
346 }
347
348 SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
349 sljit_s32 arg_types)
350 {
351 struct sljit_jump *jump;
352 sljit_u32 extra_space = 0;
353 sljit_ins ins = NOP;
354
355 CHECK_ERROR_PTR();
356 CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
357
358 jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
359 PTR_FAIL_IF(!jump);
360 set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
361
362 if ((type & 0xff) != SLJIT_CALL_REG_ARG) {
363 extra_space = (sljit_u32)type;
364 PTR_FAIL_IF(call_with_args(compiler, arg_types, &ins, &extra_space));
365 } else if (type & SLJIT_CALL_RETURN)
366 PTR_FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
367
368 SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25);
369
370 if (ins == NOP && compiler->delay_slot != UNMOVABLE_INS)
371 jump->flags |= IS_MOVABLE;
372
373 if (!(type & SLJIT_CALL_RETURN) || extra_space > 0) {
374 jump->flags |= IS_JAL;
375
376 if ((type & 0xff) != SLJIT_CALL_REG_ARG)
377 jump->flags |= IS_CALL;
378
379 PTR_FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
380 } else
381 PTR_FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
382
383 jump->addr = compiler->size;
384 PTR_FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
385
386 /* Maximum number of instructions required for generating a constant. */
387 compiler->size += 2;
388
389 if (extra_space == 0)
390 return jump;
391
392 if (type & SLJIT_CALL_RETURN)
393 PTR_FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG,
394 SLJIT_MEM1(SLJIT_SP), (sljit_sw)(extra_space - sizeof(sljit_sw))));
395
396 if (type & SLJIT_CALL_RETURN)
397 PTR_FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
398
399 PTR_FAIL_IF(push_inst(compiler, ADDIU | S(SLJIT_SP) | T(SLJIT_SP) | IMM(extra_space),
400 (type & SLJIT_CALL_RETURN) ? UNMOVABLE_INS : DR(SLJIT_SP)));
401 return jump;
402 }
403
404 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
405 sljit_s32 arg_types,
406 sljit_s32 src, sljit_sw srcw)
407 {
408 sljit_u32 extra_space = (sljit_u32)type;
409 sljit_ins ins;
410
411 CHECK_ERROR();
412 CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
413
414 if (src & SLJIT_MEM) {
415 ADJUST_LOCAL_OFFSET(src, srcw);
416 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(PIC_ADDR_REG), src, srcw));
417 src = PIC_ADDR_REG;
418 srcw = 0;
419 }
420
421 if ((type & 0xff) == SLJIT_CALL_REG_ARG) {
422 if (type & SLJIT_CALL_RETURN) {
423 if (src >= SLJIT_FIRST_SAVED_REG && src <= (SLJIT_S0 - SLJIT_KEPT_SAVEDS_COUNT(compiler->options))) {
424 FAIL_IF(push_inst(compiler, ADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
425 src = PIC_ADDR_REG;
426 srcw = 0;
427 }
428
429 FAIL_IF(emit_stack_frame_release(compiler, 0, &ins));
430
431 if (ins != NOP)
432 FAIL_IF(push_inst(compiler, ins, MOVABLE_INS));
433 }
434
435 SLJIT_SKIP_CHECKS(compiler);
436 return sljit_emit_ijump(compiler, type, src, srcw);
437 }
438
439 SLJIT_ASSERT(DR(PIC_ADDR_REG) == 25);
440
441 if (src == SLJIT_IMM)
442 FAIL_IF(load_immediate(compiler, DR(PIC_ADDR_REG), srcw));
443 else if (src != PIC_ADDR_REG)
444 FAIL_IF(push_inst(compiler, ADDU | S(src) | TA(0) | D(PIC_ADDR_REG), DR(PIC_ADDR_REG)));
445
446 FAIL_IF(call_with_args(compiler, arg_types, &ins, &extra_space));
447
448 /* Register input. */
449 if (!(type & SLJIT_CALL_RETURN) || extra_space > 0)
450 FAIL_IF(push_inst(compiler, JALR | S(PIC_ADDR_REG) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
451 else
452 FAIL_IF(push_inst(compiler, JR | S(PIC_ADDR_REG), UNMOVABLE_INS));
453 FAIL_IF(push_inst(compiler, ins, UNMOVABLE_INS));
454
455 if (extra_space == 0)
456 return SLJIT_SUCCESS;
457
458 if (type & SLJIT_CALL_RETURN)
459 FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG,
460 SLJIT_MEM1(SLJIT_SP), (sljit_sw)(extra_space - sizeof(sljit_sw))));
461
462 if (type & SLJIT_CALL_RETURN)
463 FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
464
465 return push_inst(compiler, ADDIU | S(SLJIT_SP) | T(SLJIT_SP) | IMM(extra_space),
466 (type & SLJIT_CALL_RETURN) ? UNMOVABLE_INS : DR(SLJIT_SP));
467 }