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