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 SLJIT_ASSERT(dst_r != tmp_r);
32
33 if (imm <= SIMM_MAX && imm >= SIMM_MIN)
34 return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));
35
36 if (imm <= 0x7fffffffl && imm >= S32_MIN) {
37 if (imm > S32_MAX) {
38 SLJIT_ASSERT((imm & 0x800) != 0);
39 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
40 return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
41 }
42
43 if ((imm & 0x800) != 0)
44 imm += 0x1000;
45
46 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
47
48 if ((imm & 0xfff) == 0)
49 return SLJIT_SUCCESS;
50
51 return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
52 }
53
54 /* Trailing zeroes could be used to produce shifted immediates. */
55
56 if (imm <= 0x7ffffffffffl && imm >= -0x80000000000l) {
57 high = imm >> 12;
58
59 if (imm & 0x800)
60 high = ~high;
61
62 if (high > S32_MAX) {
63 SLJIT_ASSERT((high & 0x800) != 0);
64 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)0x80000000u));
65 FAIL_IF(push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));
66 } else {
67 if ((high & 0x800) != 0)
68 high += 0x1000;
69
70 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(high & ~0xfff)));
71
72 if ((high & 0xfff) != 0)
73 FAIL_IF(push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(high)));
74 }
75
76 FAIL_IF(push_inst(compiler, SLLI | RD(dst_r) | RS1(dst_r) | IMM_I(12)));
77
78 if ((imm & 0xfff) != 0)
79 return push_inst(compiler, XORI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
80
81 return SLJIT_SUCCESS;
82 }
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
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value,sljit_ins last_ins)129 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
130 {
131 sljit_sw high;
132
133 if ((init_value & 0x800) != 0)
134 init_value += 0x1000;
135
136 high = init_value >> 32;
137
138 if ((init_value & 0x80000000l) != 0)
139 high = ~high;
140
141 if ((high & 0x800) != 0)
142 high += 0x1000;
143
144 FAIL_IF(push_inst(compiler, LUI | RD(TMP_REG3) | (sljit_ins)(high & ~0xfff)));
145 FAIL_IF(push_inst(compiler, ADDI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(high)));
146 FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
147 FAIL_IF(push_inst(compiler, SLLI | RD(TMP_REG3) | RS1(TMP_REG3) | IMM_I(32)));
148 FAIL_IF(push_inst(compiler, XOR | RD(dst) | RS1(dst) | RS2(TMP_REG3)));
149 return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
150 }
151
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)152 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
153 {
154 sljit_ins *inst = (sljit_ins*)addr;
155 sljit_sw high;
156 SLJIT_UNUSED_ARG(executable_offset);
157
158 if ((new_target & 0x800) != 0)
159 new_target += 0x1000;
160
161 high = (sljit_sw)new_target >> 32;
162
163 if ((new_target & 0x80000000l) != 0)
164 high = ~high;
165
166 if ((high & 0x800) != 0)
167 high += 0x1000;
168
169 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
170
171 SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
172 inst[0] = (inst[0] & 0xfff) | (sljit_ins)(high & ~0xfff);
173 SLJIT_ASSERT((inst[1] & 0x707f) == ADDI);
174 inst[1] = (inst[1] & 0xfffff) | IMM_I(high);
175 SLJIT_ASSERT((inst[2] & 0x7f) == LUI);
176 inst[2] = (inst[2] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
177 SLJIT_ASSERT((inst[5] & 0x707f) == ADDI || (inst[5] & 0x707f) == JALR);
178 inst[5] = (inst[5] & 0xfffff) | IMM_I(new_target);
179 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
180
181 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
182 SLJIT_CACHE_FLUSH(inst, inst + 5);
183 }
184