Improve sljit abi calling convention
[sljit.git] / sljit_src / sljitNativeRISCV_64.c
blob16a5f5f5571ccb3e4fbf7ec29fce01c942dab92f
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_sw high;
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 <= 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));
41 if ((imm & 0x800) != 0)
42 imm += 0x1000;
44 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
46 if ((imm & 0xfff) == 0)
47 return SLJIT_SUCCESS;
49 return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
52 /* Trailing zeroes could be used to produce shifted immediates. */
54 if (imm <= 0x7ffffffffffl && imm >= -0x80000000000l) {
55 high = imm >> 12;
57 if (imm & 0x800)
58 high = ~high;
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;
68 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(high & ~0xfff)));
70 if ((high & 0xfff) != 0)
71 FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));
74 FAIL_IF(push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(12)));
76 if ((imm & 0xfff) != 0)
77 return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
79 return SLJIT_SUCCESS;
82 high = imm >> 32;
83 imm = (sljit_s32)imm;
85 if ((imm & 0x80000000l) != 0)
86 high = ~high;
88 if (high <= 0x7ffff && high >= -0x80000) {
89 FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high << 12)));
90 high = 0x1000;
91 } else {
92 if ((high & 0x800) != 0)
93 high += 0x1000;
95 FAIL_IF(push_inst(compiler, LUI | RD(tmp_r) | (sljit_ins)(high & ~0xfff)));
96 high &= 0xfff;
99 if (imm <= SIMM_MAX && imm >= SIMM_MIN) {
100 FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm)));
101 imm = 0;
102 } else if (imm > S32_MAX) {
103 SLJIT_ASSERT((imm & 0x800) != 0);
105 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
106 imm = 0x1000 | (imm & 0xfff);
107 } else {
108 if ((imm & 0x800) != 0)
109 imm += 0x1000;
111 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
112 imm &= 0xfff;
115 if ((high & 0xfff) != 0)
116 FAIL_IF(push_inst(compiler, ADDI | RD(tmp_r) | RS1(tmp_r) | IMM_I(high)));
118 if (imm & 0x1000)
119 FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));
120 else if (imm != 0)
121 FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm)));
123 FAIL_IF(push_inst(compiler, SLLI | RD(tmp_r) | RS1(tmp_r) | IMM_I((high & 0x1000) ? 20 : 32)));
124 return push_inst(compiler, XOR | RD(dst_r) | RS1(dst_r) | RS2(tmp_r));
127 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
129 sljit_sw high;
131 if ((init_value & 0x800) != 0)
132 init_value += 0x1000;
134 high = init_value >> 32;
136 if ((init_value & 0x80000000l) != 0)
137 high = ~high;
139 if ((high & 0x800) != 0)
140 high += 0x1000;
142 FAIL_IF(push_inst(compiler, LUI | RD(TMP_REG3) | (sljit_ins)(high & ~0xfff)));
143 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(high)));
144 FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
145 FAIL_IF(push_inst(compiler, SLLI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(32)));
146 FAIL_IF(push_inst(compiler, XOR | RD(dst) | RS1(dst) | RS2(TMP_REG3)));
147 return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
150 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
152 sljit_ins *inst = (sljit_ins*)addr;
153 sljit_sw high;
154 SLJIT_UNUSED_ARG(executable_offset);
156 if ((new_target & 0x800) != 0)
157 new_target += 0x1000;
159 high = (sljit_sw)new_target >> 32;
161 if ((new_target & 0x80000000l) != 0)
162 high = ~high;
164 if ((high & 0x800) != 0)
165 high += 0x1000;
167 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
169 SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
170 inst[0] = (inst[0] & 0xfff) | (sljit_ins)(high & ~0xfff);
171 SLJIT_ASSERT((inst[1] & 0x707f) == ADDI);
172 inst[1] = (inst[1] & 0xfffff) | IMM_I(high);
173 SLJIT_ASSERT((inst[2] & 0x7f) == LUI);
174 inst[2] = (inst[2] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
175 SLJIT_ASSERT((inst[5] & 0x707f) == ADDI || (inst[5] & 0x707f) == JALR);
176 inst[5] = (inst[5] & 0xfffff) | IMM_I(new_target);
177 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
179 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
180 SLJIT_CACHE_FLUSH(inst, inst + 5);