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_UNUSED_ARG(tmp_r);
30
31 if (imm <= SIMM_MAX && imm >= SIMM_MIN)
32 return push_inst(compiler, ADDI | RD(dst_r) | RS1(TMP_ZERO) | IMM_I(imm));
33
34 if (imm & 0x800)
35 imm += 0x1000;
36
37 FAIL_IF(push_inst(compiler, LUI | RD(dst_r) | (sljit_ins)(imm & ~0xfff)));
38
39 if ((imm & 0xfff) == 0)
40 return SLJIT_SUCCESS;
41
42 return push_inst(compiler, ADDI | RD(dst_r) | RS1(dst_r) | IMM_I(imm));
43 }
44
sljit_emit_fset64(struct sljit_compiler * compiler,sljit_s32 freg,sljit_f64 value)45 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fset64(struct sljit_compiler *compiler,
46 sljit_s32 freg, sljit_f64 value)
47 {
48 union {
49 sljit_s32 imm[2];
50 sljit_f64 value;
51 } u;
52
53 CHECK_ERROR();
54 CHECK(check_sljit_emit_fset64(compiler, freg, value));
55
56 u.value = value;
57
58 if (u.imm[0] != 0)
59 FAIL_IF(load_immediate(compiler, TMP_REG1, u.imm[0], TMP_REG3));
60 if (u.imm[1] != 0)
61 FAIL_IF(load_immediate(compiler, TMP_REG2, u.imm[1], TMP_REG3));
62
63 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));
64 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[0] != 0 ? TMP_REG1 : TMP_ZERO) | (8 << 7)));
65 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(u.imm[1] != 0 ? TMP_REG2 : TMP_ZERO) | (12 << 7)));
66 FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));
67 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));
68 }
69
sljit_emit_fcopy(struct sljit_compiler * compiler,sljit_s32 op,sljit_s32 freg,sljit_s32 reg)70 SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fcopy(struct sljit_compiler *compiler, sljit_s32 op,
71 sljit_s32 freg, sljit_s32 reg)
72 {
73 sljit_ins inst;
74 sljit_s32 reg2 = 0;
75
76 CHECK_ERROR();
77 CHECK(check_sljit_emit_fcopy(compiler, op, freg, reg));
78
79 if (op & SLJIT_32) {
80 if (op == SLJIT_COPY32_TO_F32)
81 inst = FMV_W_X | RS1(reg) | FRD(freg);
82 else
83 inst = FMV_X_W | FRS1(freg) | RD(reg);
84
85 return push_inst(compiler, inst);
86 }
87
88 FAIL_IF(push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(-16)));
89
90 if (reg & REG_PAIR_MASK) {
91 reg2 = REG_PAIR_SECOND(reg);
92 reg = REG_PAIR_FIRST(reg);
93 }
94
95 if (op == SLJIT_COPY_TO_F64) {
96 if (reg2 != 0)
97 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg2) | (8 << 7)));
98 else
99 FAIL_IF(push_inst(compiler, FSW | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));
100
101 FAIL_IF(push_inst(compiler, SW | RS1(SLJIT_SP) | RS2(reg) | (12 << 7)));
102 FAIL_IF(push_inst(compiler, FLD | FRD(freg) | RS1(SLJIT_SP) | IMM_I(8)));
103 } else {
104 FAIL_IF(push_inst(compiler, FSD | RS1(SLJIT_SP) | FRS2(freg) | (8 << 7)));
105
106 if (reg2 != 0)
107 FAIL_IF(push_inst(compiler, FMV_X_W | FRS1(freg) | RD(reg2)));
108
109 FAIL_IF(push_inst(compiler, LW | RD(reg) | RS1(SLJIT_SP) | IMM_I(12)));
110 }
111
112 return push_inst(compiler, ADDI | RD(SLJIT_SP) | RS1(SLJIT_SP) | IMM_I(16));
113 }
114
emit_const(struct sljit_compiler * compiler,sljit_s32 dst,sljit_sw init_value,sljit_ins last_ins)115 static SLJIT_INLINE sljit_s32 emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw init_value, sljit_ins last_ins)
116 {
117 if ((init_value & 0x800) != 0)
118 init_value += 0x1000;
119
120 FAIL_IF(push_inst(compiler, LUI | RD(dst) | (sljit_ins)(init_value & ~0xfff)));
121 return push_inst(compiler, last_ins | RS1(dst) | IMM_I(init_value));
122 }
123
sljit_set_jump_addr(sljit_uw addr,sljit_uw new_target,sljit_sw executable_offset)124 SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
125 {
126 sljit_ins *inst = (sljit_ins*)addr;
127 SLJIT_UNUSED_ARG(executable_offset);
128
129 if ((new_target & 0x800) != 0)
130 new_target += 0x1000;
131
132 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 0);
133
134 SLJIT_ASSERT((inst[0] & 0x7f) == LUI);
135 inst[0] = (inst[0] & 0xfff) | (sljit_ins)((sljit_sw)new_target & ~0xfff);
136 SLJIT_ASSERT((inst[1] & 0x707f) == ADDI || (inst[1] & 0x707f) == JALR);
137 inst[1] = (inst[1] & 0xfffff) | IMM_I(new_target);
138
139 SLJIT_UPDATE_WX_FLAGS(inst, inst + 5, 1);
140 inst = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
141 SLJIT_CACHE_FLUSH(inst, inst + 5);
142 }
143