1/* 2 * RISC-V translation routines for the Zb[abcs] Standard Extension. 3 * 4 * Copyright (c) 2020 Kito Cheng, kito.cheng@sifive.com 5 * Copyright (c) 2020 Frank Chang, frank.chang@sifive.com 6 * Copyright (c) 2021 Philipp Tomsich, philipp.tomsich@vrull.eu 7 * 8 * This program is free software; you can redistribute it and/or modify it 9 * under the terms and conditions of the GNU General Public License, 10 * version 2 or later, as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 15 * more details. 16 * 17 * You should have received a copy of the GNU General Public License along with 18 * this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21#define REQUIRE_ZBA(ctx) do { \ 22 if (!RISCV_CPU(ctx->cs)->cfg.ext_zba) { \ 23 return false; \ 24 } \ 25} while (0) 26 27#define REQUIRE_ZBB(ctx) do { \ 28 if (!RISCV_CPU(ctx->cs)->cfg.ext_zbb) { \ 29 return false; \ 30 } \ 31} while (0) 32 33#define REQUIRE_ZBC(ctx) do { \ 34 if (!RISCV_CPU(ctx->cs)->cfg.ext_zbc) { \ 35 return false; \ 36 } \ 37} while (0) 38 39#define REQUIRE_ZBS(ctx) do { \ 40 if (!RISCV_CPU(ctx->cs)->cfg.ext_zbs) { \ 41 return false; \ 42 } \ 43} while (0) 44 45static void gen_clz(TCGv ret, TCGv arg1) 46{ 47 tcg_gen_clzi_tl(ret, arg1, TARGET_LONG_BITS); 48} 49 50static bool trans_clz(DisasContext *ctx, arg_clz *a) 51{ 52 REQUIRE_ZBB(ctx); 53 return gen_unary(ctx, a, EXT_ZERO, gen_clz); 54} 55 56static void gen_ctz(TCGv ret, TCGv arg1) 57{ 58 tcg_gen_ctzi_tl(ret, arg1, TARGET_LONG_BITS); 59} 60 61static bool trans_ctz(DisasContext *ctx, arg_ctz *a) 62{ 63 REQUIRE_ZBB(ctx); 64 return gen_unary(ctx, a, EXT_ZERO, gen_ctz); 65} 66 67static bool trans_cpop(DisasContext *ctx, arg_cpop *a) 68{ 69 REQUIRE_ZBB(ctx); 70 return gen_unary(ctx, a, EXT_ZERO, tcg_gen_ctpop_tl); 71} 72 73static bool trans_andn(DisasContext *ctx, arg_andn *a) 74{ 75 REQUIRE_ZBB(ctx); 76 return gen_arith(ctx, a, EXT_NONE, tcg_gen_andc_tl); 77} 78 79static bool trans_orn(DisasContext *ctx, arg_orn *a) 80{ 81 REQUIRE_ZBB(ctx); 82 return gen_arith(ctx, a, EXT_NONE, tcg_gen_orc_tl); 83} 84 85static bool trans_xnor(DisasContext *ctx, arg_xnor *a) 86{ 87 REQUIRE_ZBB(ctx); 88 return gen_arith(ctx, a, EXT_NONE, tcg_gen_eqv_tl); 89} 90 91static bool trans_min(DisasContext *ctx, arg_min *a) 92{ 93 REQUIRE_ZBB(ctx); 94 return gen_arith(ctx, a, EXT_SIGN, tcg_gen_smin_tl); 95} 96 97static bool trans_max(DisasContext *ctx, arg_max *a) 98{ 99 REQUIRE_ZBB(ctx); 100 return gen_arith(ctx, a, EXT_SIGN, tcg_gen_smax_tl); 101} 102 103static bool trans_minu(DisasContext *ctx, arg_minu *a) 104{ 105 REQUIRE_ZBB(ctx); 106 return gen_arith(ctx, a, EXT_SIGN, tcg_gen_umin_tl); 107} 108 109static bool trans_maxu(DisasContext *ctx, arg_maxu *a) 110{ 111 REQUIRE_ZBB(ctx); 112 return gen_arith(ctx, a, EXT_SIGN, tcg_gen_umax_tl); 113} 114 115static bool trans_sext_b(DisasContext *ctx, arg_sext_b *a) 116{ 117 REQUIRE_ZBB(ctx); 118 return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext8s_tl); 119} 120 121static bool trans_sext_h(DisasContext *ctx, arg_sext_h *a) 122{ 123 REQUIRE_ZBB(ctx); 124 return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext16s_tl); 125} 126 127static void gen_sbop_mask(TCGv ret, TCGv shamt) 128{ 129 tcg_gen_movi_tl(ret, 1); 130 tcg_gen_shl_tl(ret, ret, shamt); 131} 132 133static void gen_bset(TCGv ret, TCGv arg1, TCGv shamt) 134{ 135 TCGv t = tcg_temp_new(); 136 137 gen_sbop_mask(t, shamt); 138 tcg_gen_or_tl(ret, arg1, t); 139 140 tcg_temp_free(t); 141} 142 143static bool trans_bset(DisasContext *ctx, arg_bset *a) 144{ 145 REQUIRE_ZBS(ctx); 146 return gen_shift(ctx, a, EXT_NONE, gen_bset); 147} 148 149static bool trans_bseti(DisasContext *ctx, arg_bseti *a) 150{ 151 REQUIRE_ZBS(ctx); 152 return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_bset); 153} 154 155static void gen_bclr(TCGv ret, TCGv arg1, TCGv shamt) 156{ 157 TCGv t = tcg_temp_new(); 158 159 gen_sbop_mask(t, shamt); 160 tcg_gen_andc_tl(ret, arg1, t); 161 162 tcg_temp_free(t); 163} 164 165static bool trans_bclr(DisasContext *ctx, arg_bclr *a) 166{ 167 REQUIRE_ZBS(ctx); 168 return gen_shift(ctx, a, EXT_NONE, gen_bclr); 169} 170 171static bool trans_bclri(DisasContext *ctx, arg_bclri *a) 172{ 173 REQUIRE_ZBS(ctx); 174 return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_bclr); 175} 176 177static void gen_binv(TCGv ret, TCGv arg1, TCGv shamt) 178{ 179 TCGv t = tcg_temp_new(); 180 181 gen_sbop_mask(t, shamt); 182 tcg_gen_xor_tl(ret, arg1, t); 183 184 tcg_temp_free(t); 185} 186 187static bool trans_binv(DisasContext *ctx, arg_binv *a) 188{ 189 REQUIRE_ZBS(ctx); 190 return gen_shift(ctx, a, EXT_NONE, gen_binv); 191} 192 193static bool trans_binvi(DisasContext *ctx, arg_binvi *a) 194{ 195 REQUIRE_ZBS(ctx); 196 return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_binv); 197} 198 199static void gen_bext(TCGv ret, TCGv arg1, TCGv shamt) 200{ 201 tcg_gen_shr_tl(ret, arg1, shamt); 202 tcg_gen_andi_tl(ret, ret, 1); 203} 204 205static bool trans_bext(DisasContext *ctx, arg_bext *a) 206{ 207 REQUIRE_ZBS(ctx); 208 return gen_shift(ctx, a, EXT_NONE, gen_bext); 209} 210 211static bool trans_bexti(DisasContext *ctx, arg_bexti *a) 212{ 213 REQUIRE_ZBS(ctx); 214 return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_bext); 215} 216 217static bool trans_ror(DisasContext *ctx, arg_ror *a) 218{ 219 REQUIRE_ZBB(ctx); 220 return gen_shift(ctx, a, EXT_NONE, tcg_gen_rotr_tl); 221} 222 223static bool trans_rori(DisasContext *ctx, arg_rori *a) 224{ 225 REQUIRE_ZBB(ctx); 226 return gen_shift_imm_fn(ctx, a, EXT_NONE, tcg_gen_rotri_tl); 227} 228 229static bool trans_rol(DisasContext *ctx, arg_rol *a) 230{ 231 REQUIRE_ZBB(ctx); 232 return gen_shift(ctx, a, EXT_NONE, tcg_gen_rotl_tl); 233} 234 235static void gen_rev8_32(TCGv ret, TCGv src1) 236{ 237 tcg_gen_bswap32_tl(ret, src1, TCG_BSWAP_OS); 238} 239 240static bool trans_rev8_32(DisasContext *ctx, arg_rev8_32 *a) 241{ 242 REQUIRE_32BIT(ctx); 243 REQUIRE_ZBB(ctx); 244 return gen_unary(ctx, a, EXT_NONE, gen_rev8_32); 245} 246 247static bool trans_rev8_64(DisasContext *ctx, arg_rev8_64 *a) 248{ 249 REQUIRE_64BIT(ctx); 250 REQUIRE_ZBB(ctx); 251 return gen_unary(ctx, a, EXT_NONE, tcg_gen_bswap_tl); 252} 253 254static void gen_orc_b(TCGv ret, TCGv source1) 255{ 256 TCGv tmp = tcg_temp_new(); 257 TCGv low7 = tcg_constant_tl(dup_const_tl(MO_8, 0x7f)); 258 259 /* Set msb in each byte if the byte was non-zero. */ 260 tcg_gen_and_tl(tmp, source1, low7); 261 tcg_gen_add_tl(tmp, tmp, low7); 262 tcg_gen_or_tl(tmp, tmp, source1); 263 264 /* Extract the msb to the lsb in each byte */ 265 tcg_gen_andc_tl(tmp, tmp, low7); 266 tcg_gen_shri_tl(tmp, tmp, 7); 267 268 /* Replicate the lsb of each byte across the byte. */ 269 tcg_gen_muli_tl(ret, tmp, 0xff); 270 271 tcg_temp_free(tmp); 272} 273 274static bool trans_orc_b(DisasContext *ctx, arg_orc_b *a) 275{ 276 REQUIRE_ZBB(ctx); 277 return gen_unary(ctx, a, EXT_ZERO, gen_orc_b); 278} 279 280#define GEN_SHADD(SHAMT) \ 281static void gen_sh##SHAMT##add(TCGv ret, TCGv arg1, TCGv arg2) \ 282{ \ 283 TCGv t = tcg_temp_new(); \ 284 \ 285 tcg_gen_shli_tl(t, arg1, SHAMT); \ 286 tcg_gen_add_tl(ret, t, arg2); \ 287 \ 288 tcg_temp_free(t); \ 289} 290 291GEN_SHADD(1) 292GEN_SHADD(2) 293GEN_SHADD(3) 294 295#define GEN_TRANS_SHADD(SHAMT) \ 296static bool trans_sh##SHAMT##add(DisasContext *ctx, arg_sh##SHAMT##add *a) \ 297{ \ 298 REQUIRE_ZBA(ctx); \ 299 return gen_arith(ctx, a, EXT_NONE, gen_sh##SHAMT##add); \ 300} 301 302GEN_TRANS_SHADD(1) 303GEN_TRANS_SHADD(2) 304GEN_TRANS_SHADD(3) 305 306static bool trans_zext_h_32(DisasContext *ctx, arg_zext_h_32 *a) 307{ 308 REQUIRE_32BIT(ctx); 309 REQUIRE_ZBB(ctx); 310 return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext16u_tl); 311} 312 313static bool trans_zext_h_64(DisasContext *ctx, arg_zext_h_64 *a) 314{ 315 REQUIRE_64BIT(ctx); 316 REQUIRE_ZBB(ctx); 317 return gen_unary(ctx, a, EXT_NONE, tcg_gen_ext16u_tl); 318} 319 320static void gen_clzw(TCGv ret, TCGv arg1) 321{ 322 TCGv t = tcg_temp_new(); 323 tcg_gen_shli_tl(t, arg1, 32); 324 tcg_gen_clzi_tl(ret, t, 32); 325 tcg_temp_free(t); 326} 327 328static bool trans_clzw(DisasContext *ctx, arg_clzw *a) 329{ 330 REQUIRE_64BIT(ctx); 331 REQUIRE_ZBB(ctx); 332 return gen_unary(ctx, a, EXT_NONE, gen_clzw); 333} 334 335static void gen_ctzw(TCGv ret, TCGv arg1) 336{ 337 tcg_gen_ori_tl(ret, arg1, (target_ulong)MAKE_64BIT_MASK(32, 32)); 338 tcg_gen_ctzi_tl(ret, ret, 64); 339} 340 341static bool trans_ctzw(DisasContext *ctx, arg_ctzw *a) 342{ 343 REQUIRE_64BIT(ctx); 344 REQUIRE_ZBB(ctx); 345 return gen_unary(ctx, a, EXT_NONE, gen_ctzw); 346} 347 348static bool trans_cpopw(DisasContext *ctx, arg_cpopw *a) 349{ 350 REQUIRE_64BIT(ctx); 351 REQUIRE_ZBB(ctx); 352 ctx->ol = MXL_RV32; 353 return gen_unary(ctx, a, EXT_ZERO, tcg_gen_ctpop_tl); 354} 355 356static void gen_rorw(TCGv ret, TCGv arg1, TCGv arg2) 357{ 358 TCGv_i32 t1 = tcg_temp_new_i32(); 359 TCGv_i32 t2 = tcg_temp_new_i32(); 360 361 /* truncate to 32-bits */ 362 tcg_gen_trunc_tl_i32(t1, arg1); 363 tcg_gen_trunc_tl_i32(t2, arg2); 364 365 tcg_gen_rotr_i32(t1, t1, t2); 366 367 /* sign-extend 64-bits */ 368 tcg_gen_ext_i32_tl(ret, t1); 369 370 tcg_temp_free_i32(t1); 371 tcg_temp_free_i32(t2); 372} 373 374static bool trans_rorw(DisasContext *ctx, arg_rorw *a) 375{ 376 REQUIRE_64BIT(ctx); 377 REQUIRE_ZBB(ctx); 378 ctx->ol = MXL_RV32; 379 return gen_shift(ctx, a, EXT_NONE, gen_rorw); 380} 381 382static bool trans_roriw(DisasContext *ctx, arg_roriw *a) 383{ 384 REQUIRE_64BIT(ctx); 385 REQUIRE_ZBB(ctx); 386 ctx->ol = MXL_RV32; 387 return gen_shift_imm_tl(ctx, a, EXT_NONE, gen_rorw); 388} 389 390static void gen_rolw(TCGv ret, TCGv arg1, TCGv arg2) 391{ 392 TCGv_i32 t1 = tcg_temp_new_i32(); 393 TCGv_i32 t2 = tcg_temp_new_i32(); 394 395 /* truncate to 32-bits */ 396 tcg_gen_trunc_tl_i32(t1, arg1); 397 tcg_gen_trunc_tl_i32(t2, arg2); 398 399 tcg_gen_rotl_i32(t1, t1, t2); 400 401 /* sign-extend 64-bits */ 402 tcg_gen_ext_i32_tl(ret, t1); 403 404 tcg_temp_free_i32(t1); 405 tcg_temp_free_i32(t2); 406} 407 408static bool trans_rolw(DisasContext *ctx, arg_rolw *a) 409{ 410 REQUIRE_64BIT(ctx); 411 REQUIRE_ZBB(ctx); 412 ctx->ol = MXL_RV32; 413 return gen_shift(ctx, a, EXT_NONE, gen_rolw); 414} 415 416#define GEN_SHADD_UW(SHAMT) \ 417static void gen_sh##SHAMT##add_uw(TCGv ret, TCGv arg1, TCGv arg2) \ 418{ \ 419 TCGv t = tcg_temp_new(); \ 420 \ 421 tcg_gen_ext32u_tl(t, arg1); \ 422 \ 423 tcg_gen_shli_tl(t, t, SHAMT); \ 424 tcg_gen_add_tl(ret, t, arg2); \ 425 \ 426 tcg_temp_free(t); \ 427} 428 429GEN_SHADD_UW(1) 430GEN_SHADD_UW(2) 431GEN_SHADD_UW(3) 432 433#define GEN_TRANS_SHADD_UW(SHAMT) \ 434static bool trans_sh##SHAMT##add_uw(DisasContext *ctx, \ 435 arg_sh##SHAMT##add_uw *a) \ 436{ \ 437 REQUIRE_64BIT(ctx); \ 438 REQUIRE_ZBA(ctx); \ 439 return gen_arith(ctx, a, EXT_NONE, gen_sh##SHAMT##add_uw); \ 440} 441 442GEN_TRANS_SHADD_UW(1) 443GEN_TRANS_SHADD_UW(2) 444GEN_TRANS_SHADD_UW(3) 445 446static void gen_add_uw(TCGv ret, TCGv arg1, TCGv arg2) 447{ 448 TCGv t = tcg_temp_new(); 449 tcg_gen_ext32u_tl(t, arg1); 450 tcg_gen_add_tl(ret, t, arg2); 451 tcg_temp_free(t); 452} 453 454static bool trans_add_uw(DisasContext *ctx, arg_add_uw *a) 455{ 456 REQUIRE_64BIT(ctx); 457 REQUIRE_ZBA(ctx); 458 return gen_arith(ctx, a, EXT_NONE, gen_add_uw); 459} 460 461static void gen_slli_uw(TCGv dest, TCGv src, target_long shamt) 462{ 463 tcg_gen_deposit_z_tl(dest, src, shamt, MIN(32, TARGET_LONG_BITS - shamt)); 464} 465 466static bool trans_slli_uw(DisasContext *ctx, arg_slli_uw *a) 467{ 468 REQUIRE_64BIT(ctx); 469 REQUIRE_ZBA(ctx); 470 return gen_shift_imm_fn(ctx, a, EXT_NONE, gen_slli_uw); 471} 472 473static bool trans_clmul(DisasContext *ctx, arg_clmul *a) 474{ 475 REQUIRE_ZBC(ctx); 476 return gen_arith(ctx, a, EXT_NONE, gen_helper_clmul); 477} 478 479static void gen_clmulh(TCGv dst, TCGv src1, TCGv src2) 480{ 481 gen_helper_clmulr(dst, src1, src2); 482 tcg_gen_shri_tl(dst, dst, 1); 483} 484 485static bool trans_clmulh(DisasContext *ctx, arg_clmulr *a) 486{ 487 REQUIRE_ZBC(ctx); 488 return gen_arith(ctx, a, EXT_NONE, gen_clmulh); 489} 490 491static bool trans_clmulr(DisasContext *ctx, arg_clmulh *a) 492{ 493 REQUIRE_ZBC(ctx); 494 return gen_arith(ctx, a, EXT_NONE, gen_helper_clmulr); 495} 496