Rework x86-32 stack layout
[sljit.git] / sljit_src / sljitNativeRISCV_32.c
blob24b8dc39052c492c22cb790c36f735c4eb0764a6
1 /*
2 * Stack-less Just-In-Time compiler
4 * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
6 * Redistribution and use in source and binary forms, with or without modification, are
7 * permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice, this list of
10 * conditions and the following disclaimer.
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.
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.
27 static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst_r, sljit_sw imm, sljit_s32 tmp_r)
29 SLJIT_UNUSED_ARG(tmp_r);
31 if (imm <= SIMM_MAX && imm >= SIMM_MIN)
32 return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));
34 if (imm & 0x800)
35 imm += 0x1000;
37 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
39 if ((imm & 0xfff) == 0)
40 return SLJIT_SUCCESS;
42 return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
45 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
47 if ((init_value & 0x800) != 0)
48 init_value += 0x1000;
50 FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
51 return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
54 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
56 sljit_ins *inst = (sljit_ins*)addr;
57 SLJIT_UNUSED_ARG(executable_offset);
59 if ((new_target & 0x800) != 0)
60 new_target += 0x1000;
62 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
64 SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
65 inst[0] = (inst[0] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
66 SLJIT_ASSERT((inst[1] & 0x707f) == ADDI || (inst[1] & 0x707f) == JALR);
67 inst[1] = (inst[1] & 0xfffff) | IMM_I(new_target);
69 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
70 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
71 SLJIT_CACHE_FLUSH(inst, inst + 5);