1/*
2 * RISC-V translation routines for the Zabha Standard Extension.
3 *
4 * Copyright (c) 2024 Alibaba Group
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#define REQUIRE_ZABHA(ctx) do {           \
20    if (!ctx->cfg_ptr->ext_zabha) {       \
21        return false;                     \
22    }                                     \
23} while (0)
24
25static bool trans_amoswap_b(DisasContext *ctx, arg_amoswap_b *a)
26{
27    REQUIRE_ZABHA(ctx);
28    return gen_amo(ctx, a, &tcg_gen_atomic_xchg_tl, MO_SB);
29}
30
31static bool trans_amoadd_b(DisasContext *ctx, arg_amoadd_b *a)
32{
33    REQUIRE_ZABHA(ctx);
34    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_add_tl, MO_SB);
35}
36
37static bool trans_amoxor_b(DisasContext *ctx, arg_amoxor_b *a)
38{
39    REQUIRE_ZABHA(ctx);
40    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_xor_tl, MO_SB);
41}
42
43static bool trans_amoand_b(DisasContext *ctx, arg_amoand_b *a)
44{
45    REQUIRE_ZABHA(ctx);
46    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_and_tl, MO_SB);
47}
48
49static bool trans_amoor_b(DisasContext *ctx, arg_amoor_b *a)
50{
51    REQUIRE_ZABHA(ctx);
52    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_or_tl, MO_SB);
53}
54
55static bool trans_amomin_b(DisasContext *ctx, arg_amomin_b *a)
56{
57    REQUIRE_ZABHA(ctx);
58    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_smin_tl, MO_SB);
59}
60
61static bool trans_amomax_b(DisasContext *ctx, arg_amomax_b *a)
62{
63    REQUIRE_ZABHA(ctx);
64    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_smax_tl, MO_SB);
65}
66
67static bool trans_amominu_b(DisasContext *ctx, arg_amominu_b *a)
68{
69    REQUIRE_ZABHA(ctx);
70    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_umin_tl, MO_SB);
71}
72
73static bool trans_amomaxu_b(DisasContext *ctx, arg_amomaxu_b *a)
74{
75    REQUIRE_ZABHA(ctx);
76    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_umax_tl, MO_SB);
77}
78
79static bool trans_amoswap_h(DisasContext *ctx, arg_amoswap_h *a)
80{
81    REQUIRE_ZABHA(ctx);
82    return gen_amo(ctx, a, &tcg_gen_atomic_xchg_tl, MO_TESW);
83}
84
85static bool trans_amoadd_h(DisasContext *ctx, arg_amoadd_h *a)
86{
87    REQUIRE_ZABHA(ctx);
88    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_add_tl, MO_TESW);
89}
90
91static bool trans_amoxor_h(DisasContext *ctx, arg_amoxor_h *a)
92{
93    REQUIRE_ZABHA(ctx);
94    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_xor_tl, MO_TESW);
95}
96
97static bool trans_amoand_h(DisasContext *ctx, arg_amoand_h *a)
98{
99    REQUIRE_ZABHA(ctx);
100    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_and_tl, MO_TESW);
101}
102
103static bool trans_amoor_h(DisasContext *ctx, arg_amoor_h *a)
104{
105    REQUIRE_ZABHA(ctx);
106    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_or_tl, MO_TESW);
107}
108
109static bool trans_amomin_h(DisasContext *ctx, arg_amomin_h *a)
110{
111    REQUIRE_ZABHA(ctx);
112    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_smin_tl, MO_TESW);
113}
114
115static bool trans_amomax_h(DisasContext *ctx, arg_amomax_h *a)
116{
117    REQUIRE_ZABHA(ctx);
118    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_smax_tl, MO_TESW);
119}
120
121static bool trans_amominu_h(DisasContext *ctx, arg_amominu_h *a)
122{
123    REQUIRE_ZABHA(ctx);
124    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_umin_tl, MO_TESW);
125}
126
127static bool trans_amomaxu_h(DisasContext *ctx, arg_amomaxu_h *a)
128{
129    REQUIRE_ZABHA(ctx);
130    return gen_amo(ctx, a, &tcg_gen_atomic_fetch_umax_tl, MO_TESW);
131}
132
133static bool trans_amocas_b(DisasContext *ctx, arg_amocas_b *a)
134{
135    REQUIRE_ZACAS(ctx);
136    REQUIRE_ZABHA(ctx);
137    return gen_cmpxchg(ctx, a, MO_SB);
138}
139
140static bool trans_amocas_h(DisasContext *ctx, arg_amocas_h *a)
141{
142    REQUIRE_ZACAS(ctx);
143    REQUIRE_ZABHA(ctx);
144    return gen_cmpxchg(ctx, a, MO_ALIGN | MO_TESW);
145}
146