1/*
2 * RISC-V translation routines for the Zk[nd,ne,nh,sed,sh] Standard Extension.
3 *
4 * Copyright (c) 2021 Ruibo Lu, luruibo2000@163.com
5 * Copyright (c) 2021 Zewen Ye, lustrew@foxmail.com
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#define REQUIRE_ZKND(ctx) do {                  \
21    if (!ctx->cfg_ptr->ext_zknd) {              \
22        return false;                           \
23    }                                           \
24} while (0)
25
26#define REQUIRE_ZKNE(ctx) do {                  \
27    if (!ctx->cfg_ptr->ext_zkne) {              \
28        return false;                           \
29    }                                           \
30} while (0)
31
32static bool gen_aes32_sm4(DisasContext *ctx, arg_k_aes *a,
33                          void (*func)(TCGv, TCGv, TCGv, TCGv))
34{
35    TCGv shamt = tcg_constant_tl(a->shamt);
36    TCGv dest = dest_gpr(ctx, a->rd);
37    TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
38    TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
39
40    func(dest, src1, src2, shamt);
41    gen_set_gpr(ctx, a->rd, dest);
42    return true;
43}
44
45static bool trans_aes32esmi(DisasContext *ctx, arg_aes32esmi *a)
46{
47    REQUIRE_32BIT(ctx);
48    REQUIRE_ZKNE(ctx);
49    return gen_aes32_sm4(ctx, a, gen_helper_aes32esmi);
50}
51
52static bool trans_aes32esi(DisasContext *ctx, arg_aes32esi *a)
53{
54    REQUIRE_32BIT(ctx);
55    REQUIRE_ZKNE(ctx);
56    return gen_aes32_sm4(ctx, a, gen_helper_aes32esi);
57}
58
59static bool trans_aes32dsmi(DisasContext *ctx, arg_aes32dsmi *a)
60{
61    REQUIRE_32BIT(ctx);
62    REQUIRE_ZKND(ctx);
63    return gen_aes32_sm4(ctx, a, gen_helper_aes32dsmi);
64}
65
66static bool trans_aes32dsi(DisasContext *ctx, arg_aes32dsi *a)
67{
68    REQUIRE_32BIT(ctx);
69    REQUIRE_ZKND(ctx);
70    return gen_aes32_sm4(ctx, a, gen_helper_aes32dsi);
71}
72
73static bool trans_aes64es(DisasContext *ctx, arg_aes64es *a)
74{
75    REQUIRE_64BIT(ctx);
76    REQUIRE_ZKNE(ctx);
77    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64es, NULL);
78}
79
80static bool trans_aes64esm(DisasContext *ctx, arg_aes64esm *a)
81{
82    REQUIRE_64BIT(ctx);
83    REQUIRE_ZKNE(ctx);
84    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64esm, NULL);
85}
86
87static bool trans_aes64ds(DisasContext *ctx, arg_aes64ds *a)
88{
89    REQUIRE_64BIT(ctx);
90    REQUIRE_ZKND(ctx);
91    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64ds, NULL);
92}
93
94static bool trans_aes64dsm(DisasContext *ctx, arg_aes64dsm *a)
95{
96    REQUIRE_64BIT(ctx);
97    REQUIRE_ZKND(ctx);
98    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64dsm, NULL);
99}
100
101static bool trans_aes64ks2(DisasContext *ctx, arg_aes64ks2 *a)
102{
103    REQUIRE_64BIT(ctx);
104    REQUIRE_EITHER_EXT(ctx, zknd, zkne);
105    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64ks2, NULL);
106}
107
108static bool trans_aes64ks1i(DisasContext *ctx, arg_aes64ks1i *a)
109{
110    REQUIRE_64BIT(ctx);
111    REQUIRE_EITHER_EXT(ctx, zknd, zkne);
112
113    if (a->imm > 0xA) {
114        return false;
115    }
116
117    return gen_arith_imm_tl(ctx, a, EXT_NONE, gen_helper_aes64ks1i, NULL);
118}
119
120static bool trans_aes64im(DisasContext *ctx, arg_aes64im *a)
121{
122    REQUIRE_64BIT(ctx);
123    REQUIRE_ZKND(ctx);
124    return gen_unary(ctx, a, EXT_NONE, gen_helper_aes64im);
125}
126