1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* RISC-V translation routines for the Zc[b,mp,mt] Standard Extensions.
*
* Copyright (c) 2021-2022 PLCT Lab
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2 or later, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define REQUIRE_ZCB(ctx) do { \
if (!ctx->cfg_ptr->ext_zcb) \
return false; \
} while (0)
#define REQUIRE_ZCMP(ctx) do { \
if (!ctx->cfg_ptr->ext_zcmp) \
return false; \
} while (0)
#define REQUIRE_ZCMT(ctx) do { \
if (!ctx->cfg_ptr->ext_zcmt) \
return false; \
} while (0)
static bool trans_c_zext_b(DisasContext *ctx, arg_c_zext_b *a)
{
REQUIRE_ZCB(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext8u_tl);
}
static bool trans_c_zext_h(DisasContext *ctx, arg_c_zext_h *a)
{
REQUIRE_ZCB(ctx);
REQUIRE_ZBB(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext16u_tl);
}
static bool trans_c_sext_b(DisasContext *ctx, arg_c_sext_b *a)
{
REQUIRE_ZCB(ctx);
REQUIRE_ZBB(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext8s_tl);
}
static bool trans_c_sext_h(DisasContext *ctx, arg_c_sext_h *a)
{
REQUIRE_ZCB(ctx);
REQUIRE_ZBB(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext16s_tl);
}
static bool trans_c_zext_w(DisasContext *ctx, arg_c_zext_w *a)
{
REQUIRE_64BIT(ctx);
REQUIRE_ZCB(ctx);
REQUIRE_ZBA(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext32u_tl);
}
static bool trans_c_not(DisasContext *ctx, arg_c_not *a)
{
REQUIRE_ZCB(ctx);
return gen_unary(ctx, a, EXT_NONE, tcg_gen_not_tl);
}
static bool trans_c_mul(DisasContext *ctx, arg_c_mul *a)
{
REQUIRE_ZCB(ctx);
REQUIRE_M_OR_ZMMUL(ctx);
return gen_arith(ctx, a, EXT_NONE, tcg_gen_mul_tl, NULL);
}
static bool trans_c_lbu(DisasContext *ctx, arg_c_lbu *a)
{
REQUIRE_ZCB(ctx);
return gen_load(ctx, a, MO_UB);
}
static bool trans_c_lhu(DisasContext *ctx, arg_c_lhu *a)
{
REQUIRE_ZCB(ctx);
return gen_load(ctx, a, MO_UW);
}
static bool trans_c_lh(DisasContext *ctx, arg_c_lh *a)
{
REQUIRE_ZCB(ctx);
return gen_load(ctx, a, MO_SW);
}
static bool trans_c_sb(DisasContext *ctx, arg_c_sb *a)
{
REQUIRE_ZCB(ctx);
return gen_store(ctx, a, MO_UB);
}
static bool trans_c_sh(DisasContext *ctx, arg_c_sh *a)
{
REQUIRE_ZCB(ctx);
return gen_store(ctx, a, MO_UW);
}
#define X_S0 8
#define X_S1 9
#define X_Sn 16
static uint32_t decode_push_pop_list(DisasContext *ctx, target_ulong rlist)
{
uint32_t reg_bitmap = 0;
if (has_ext(ctx, RVE) && rlist > 6) {
return 0;
}
switch (rlist) {
case 15:
reg_bitmap |= 1 << (X_Sn + 11) ;
reg_bitmap |= 1 << (X_Sn + 10) ;
/* FALL THROUGH */
case 14:
reg_bitmap |= 1 << (X_Sn + 9) ;
/* FALL THROUGH */
case 13:
reg_bitmap |= 1 << (X_Sn + 8) ;
/* FALL THROUGH */
case 12:
reg_bitmap |= 1 << (X_Sn + 7) ;
/* FALL THROUGH */
case 11:
reg_bitmap |= 1 << (X_Sn + 6) ;
/* FALL THROUGH */
case 10:
reg_bitmap |= 1 << (X_Sn + 5) ;
/* FALL THROUGH */
case 9:
reg_bitmap |= 1 << (X_Sn + 4) ;
/* FALL THROUGH */
case 8:
reg_bitmap |= 1 << (X_Sn + 3) ;
/* FALL THROUGH */
case 7:
reg_bitmap |= 1 << (X_Sn + 2) ;
/* FALL THROUGH */
case 6:
reg_bitmap |= 1 << X_S1 ;
/* FALL THROUGH */
case 5:
reg_bitmap |= 1 << X_S0;
/* FALL THROUGH */
case 4:
reg_bitmap |= 1 << xRA;
break;
default:
break;
}
return reg_bitmap;
}
static bool gen_pop(DisasContext *ctx, arg_cmpp *a, bool ret, bool ret_val)
{
REQUIRE_ZCMP(ctx);
uint32_t reg_bitmap = decode_push_pop_list(ctx, a->urlist);
if (reg_bitmap == 0) {
return false;
}
MemOp memop = get_ol(ctx) == MXL_RV32 ? MO_TEUL : MO_TEUQ;
int reg_size = memop_size(memop);
target_ulong stack_adj = ROUND_UP(ctpop32(reg_bitmap) * reg_size, 16) +
a->spimm;
TCGv sp = dest_gpr(ctx, xSP);
TCGv addr = tcg_temp_new();
int i;
tcg_gen_addi_tl(addr, sp, stack_adj - reg_size);
for (i = X_Sn + 11; i >= 0; i--) {
if (reg_bitmap & (1 << i)) {
TCGv dest = dest_gpr(ctx, i);
tcg_gen_qemu_ld_tl(dest, addr, ctx->mem_idx, memop);
gen_set_gpr(ctx, i, dest);
tcg_gen_subi_tl(addr, addr, reg_size);
}
}
tcg_gen_addi_tl(sp, sp, stack_adj);
gen_set_gpr(ctx, xSP, sp);
if (ret_val) {
gen_set_gpr(ctx, xA0, ctx->zero);
}
if (ret) {
TCGv ret_addr = get_gpr(ctx, xRA, EXT_SIGN);
tcg_gen_mov_tl(cpu_pc, ret_addr);
tcg_gen_lookup_and_goto_ptr();
ctx->base.is_jmp = DISAS_NORETURN;
}
return true;
}
static bool trans_cm_push(DisasContext *ctx, arg_cm_push *a)
{
REQUIRE_ZCMP(ctx);
uint32_t reg_bitmap = decode_push_pop_list(ctx, a->urlist);
if (reg_bitmap == 0) {
return false;
}
MemOp memop = get_ol(ctx) == MXL_RV32 ? MO_TEUL : MO_TEUQ;
int reg_size = memop_size(memop);
target_ulong stack_adj = ROUND_UP(ctpop32(reg_bitmap) * reg_size, 16) +
a->spimm;
TCGv sp = dest_gpr(ctx, xSP);
TCGv addr = tcg_temp_new();
int i;
tcg_gen_subi_tl(addr, sp, reg_size);
for (i = X_Sn + 11; i >= 0; i--) {
if (reg_bitmap & (1 << i)) {
TCGv val = get_gpr(ctx, i, EXT_NONE);
tcg_gen_qemu_st_tl(val, addr, ctx->mem_idx, memop);
tcg_gen_subi_tl(addr, addr, reg_size);
}
}
tcg_gen_subi_tl(sp, sp, stack_adj);
gen_set_gpr(ctx, xSP, sp);
return true;
}
static bool trans_cm_pop(DisasContext *ctx, arg_cm_pop *a)
{
return gen_pop(ctx, a, false, false);
}
static bool trans_cm_popret(DisasContext *ctx, arg_cm_popret *a)
{
return gen_pop(ctx, a, true, false);
}
static bool trans_cm_popretz(DisasContext *ctx, arg_cm_popret *a)
{
return gen_pop(ctx, a, true, true);
}
static bool trans_cm_mva01s(DisasContext *ctx, arg_cm_mva01s *a)
{
REQUIRE_ZCMP(ctx);
TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
gen_set_gpr(ctx, xA0, src1);
gen_set_gpr(ctx, xA1, src2);
return true;
}
static bool trans_cm_mvsa01(DisasContext *ctx, arg_cm_mvsa01 *a)
{
REQUIRE_ZCMP(ctx);
if (a->rs1 == a->rs2) {
return false;
}
TCGv a0 = get_gpr(ctx, xA0, EXT_NONE);
TCGv a1 = get_gpr(ctx, xA1, EXT_NONE);
gen_set_gpr(ctx, a->rs1, a0);
gen_set_gpr(ctx, a->rs2, a1);
return true;
}
static bool trans_cm_jalt(DisasContext *ctx, arg_cm_jalt *a)
{
REQUIRE_ZCMT(ctx);
/*
* Update pc to current for the non-unwinding exception
* that might come from cpu_ld*_code() in the helper.
*/
gen_update_pc(ctx, 0);
gen_helper_cm_jalt(cpu_pc, cpu_env, tcg_constant_i32(a->index));
/* c.jt vs c.jalt depends on the index. */
if (a->index >= 32) {
TCGv succ_pc = dest_gpr(ctx, xRA);
gen_pc_plus_diff(succ_pc, ctx, ctx->cur_insn_len);
gen_set_gpr(ctx, xRA, succ_pc);
}
tcg_gen_lookup_and_goto_ptr();
ctx->base.is_jmp = DISAS_NORETURN;
return true;
}
|