1 /* 2 * OpenRISC translation 3 * 4 * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com> 5 * Feng Gao <gf91597@gmail.com> 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "qemu/osdep.h" 22 #include "cpu.h" 23 #include "exec/exec-all.h" 24 #include "disas/disas.h" 25 #include "tcg/tcg-op.h" 26 #include "qemu/log.h" 27 #include "qemu/bitops.h" 28 #include "qemu/qemu-print.h" 29 #include "exec/cpu_ldst.h" 30 #include "exec/translator.h" 31 32 #include "exec/helper-proto.h" 33 #include "exec/helper-gen.h" 34 35 #include "exec/log.h" 36 37 #define HELPER_H "helper.h" 38 #include "exec/helper-info.c.inc" 39 #undef HELPER_H 40 41 42 /* is_jmp field values */ 43 #define DISAS_EXIT DISAS_TARGET_0 /* force exit to main loop */ 44 #define DISAS_JUMP DISAS_TARGET_1 /* exit via jmp_pc/jmp_pc_imm */ 45 46 typedef struct DisasContext { 47 DisasContextBase base; 48 uint32_t mem_idx; 49 uint32_t tb_flags; 50 uint32_t delayed_branch; 51 uint32_t cpucfgr; 52 uint32_t avr; 53 54 /* If not -1, jmp_pc contains this value and so is a direct jump. */ 55 target_ulong jmp_pc_imm; 56 57 /* The temporary corresponding to register 0 for this compilation. */ 58 TCGv R0; 59 /* The constant zero. */ 60 TCGv zero; 61 } DisasContext; 62 63 static inline bool is_user(DisasContext *dc) 64 { 65 #ifdef CONFIG_USER_ONLY 66 return true; 67 #else 68 return !(dc->tb_flags & TB_FLAGS_SM); 69 #endif 70 } 71 72 /* Include the auto-generated decoder. */ 73 #include "decode-insns.c.inc" 74 75 static TCGv cpu_sr; 76 static TCGv cpu_regs[32]; 77 static TCGv cpu_pc; 78 static TCGv jmp_pc; /* l.jr/l.jalr temp pc */ 79 static TCGv cpu_ppc; 80 static TCGv cpu_sr_f; /* bf/bnf, F flag taken */ 81 static TCGv cpu_sr_cy; /* carry (unsigned overflow) */ 82 static TCGv cpu_sr_ov; /* signed overflow */ 83 static TCGv cpu_lock_addr; 84 static TCGv cpu_lock_value; 85 static TCGv_i32 fpcsr; 86 static TCGv_i64 cpu_mac; /* MACHI:MACLO */ 87 static TCGv_i32 cpu_dflag; 88 89 void openrisc_translate_init(void) 90 { 91 static const char * const regnames[] = { 92 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", 93 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", 94 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", 95 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", 96 }; 97 int i; 98 99 cpu_sr = tcg_global_mem_new(cpu_env, 100 offsetof(CPUOpenRISCState, sr), "sr"); 101 cpu_dflag = tcg_global_mem_new_i32(cpu_env, 102 offsetof(CPUOpenRISCState, dflag), 103 "dflag"); 104 cpu_pc = tcg_global_mem_new(cpu_env, 105 offsetof(CPUOpenRISCState, pc), "pc"); 106 cpu_ppc = tcg_global_mem_new(cpu_env, 107 offsetof(CPUOpenRISCState, ppc), "ppc"); 108 jmp_pc = tcg_global_mem_new(cpu_env, 109 offsetof(CPUOpenRISCState, jmp_pc), "jmp_pc"); 110 cpu_sr_f = tcg_global_mem_new(cpu_env, 111 offsetof(CPUOpenRISCState, sr_f), "sr_f"); 112 cpu_sr_cy = tcg_global_mem_new(cpu_env, 113 offsetof(CPUOpenRISCState, sr_cy), "sr_cy"); 114 cpu_sr_ov = tcg_global_mem_new(cpu_env, 115 offsetof(CPUOpenRISCState, sr_ov), "sr_ov"); 116 cpu_lock_addr = tcg_global_mem_new(cpu_env, 117 offsetof(CPUOpenRISCState, lock_addr), 118 "lock_addr"); 119 cpu_lock_value = tcg_global_mem_new(cpu_env, 120 offsetof(CPUOpenRISCState, lock_value), 121 "lock_value"); 122 fpcsr = tcg_global_mem_new_i32(cpu_env, 123 offsetof(CPUOpenRISCState, fpcsr), 124 "fpcsr"); 125 cpu_mac = tcg_global_mem_new_i64(cpu_env, 126 offsetof(CPUOpenRISCState, mac), 127 "mac"); 128 for (i = 0; i < 32; i++) { 129 cpu_regs[i] = tcg_global_mem_new(cpu_env, 130 offsetof(CPUOpenRISCState, 131 shadow_gpr[0][i]), 132 regnames[i]); 133 } 134 } 135 136 static void gen_exception(DisasContext *dc, unsigned int excp) 137 { 138 gen_helper_exception(cpu_env, tcg_constant_i32(excp)); 139 } 140 141 static void gen_illegal_exception(DisasContext *dc) 142 { 143 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next); 144 gen_exception(dc, EXCP_ILLEGAL); 145 dc->base.is_jmp = DISAS_NORETURN; 146 } 147 148 static bool check_v1_3(DisasContext *dc) 149 { 150 return dc->avr >= 0x01030000; 151 } 152 153 static bool check_of32s(DisasContext *dc) 154 { 155 return dc->cpucfgr & CPUCFGR_OF32S; 156 } 157 158 static bool check_of64a32s(DisasContext *dc) 159 { 160 return dc->cpucfgr & CPUCFGR_OF64A32S; 161 } 162 163 static TCGv cpu_R(DisasContext *dc, int reg) 164 { 165 if (reg == 0) { 166 return dc->R0; 167 } else { 168 return cpu_regs[reg]; 169 } 170 } 171 172 /* 173 * We're about to write to REG. On the off-chance that the user is 174 * writing to R0, re-instate the architectural register. 175 */ 176 static void check_r0_write(DisasContext *dc, int reg) 177 { 178 if (unlikely(reg == 0)) { 179 dc->R0 = cpu_regs[0]; 180 } 181 } 182 183 static void gen_ove_cy(DisasContext *dc) 184 { 185 if (dc->tb_flags & SR_OVE) { 186 gen_helper_ove_cy(cpu_env); 187 } 188 } 189 190 static void gen_ove_ov(DisasContext *dc) 191 { 192 if (dc->tb_flags & SR_OVE) { 193 gen_helper_ove_ov(cpu_env); 194 } 195 } 196 197 static void gen_ove_cyov(DisasContext *dc) 198 { 199 if (dc->tb_flags & SR_OVE) { 200 gen_helper_ove_cyov(cpu_env); 201 } 202 } 203 204 static void gen_add(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 205 { 206 TCGv t0 = tcg_temp_new(); 207 TCGv res = tcg_temp_new(); 208 209 tcg_gen_add2_tl(res, cpu_sr_cy, srca, dc->zero, srcb, dc->zero); 210 tcg_gen_xor_tl(cpu_sr_ov, srca, srcb); 211 tcg_gen_xor_tl(t0, res, srcb); 212 tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov); 213 214 tcg_gen_mov_tl(dest, res); 215 216 gen_ove_cyov(dc); 217 } 218 219 static void gen_addc(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 220 { 221 TCGv t0 = tcg_temp_new(); 222 TCGv res = tcg_temp_new(); 223 224 tcg_gen_add2_tl(res, cpu_sr_cy, srca, dc->zero, cpu_sr_cy, dc->zero); 225 tcg_gen_add2_tl(res, cpu_sr_cy, res, cpu_sr_cy, srcb, dc->zero); 226 tcg_gen_xor_tl(cpu_sr_ov, srca, srcb); 227 tcg_gen_xor_tl(t0, res, srcb); 228 tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov); 229 230 tcg_gen_mov_tl(dest, res); 231 232 gen_ove_cyov(dc); 233 } 234 235 static void gen_sub(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 236 { 237 TCGv res = tcg_temp_new(); 238 239 tcg_gen_sub_tl(res, srca, srcb); 240 tcg_gen_xor_tl(cpu_sr_cy, srca, srcb); 241 tcg_gen_xor_tl(cpu_sr_ov, res, srcb); 242 tcg_gen_and_tl(cpu_sr_ov, cpu_sr_ov, cpu_sr_cy); 243 tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_cy, srca, srcb); 244 245 tcg_gen_mov_tl(dest, res); 246 247 gen_ove_cyov(dc); 248 } 249 250 static void gen_mul(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 251 { 252 TCGv t0 = tcg_temp_new(); 253 254 tcg_gen_muls2_tl(dest, cpu_sr_ov, srca, srcb); 255 tcg_gen_sari_tl(t0, dest, TARGET_LONG_BITS - 1); 256 tcg_gen_negsetcond_tl(TCG_COND_NE, cpu_sr_ov, cpu_sr_ov, t0); 257 258 gen_ove_ov(dc); 259 } 260 261 static void gen_mulu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 262 { 263 tcg_gen_muls2_tl(dest, cpu_sr_cy, srca, srcb); 264 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_cy, cpu_sr_cy, 0); 265 266 gen_ove_cy(dc); 267 } 268 269 static void gen_div(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 270 { 271 TCGv t0 = tcg_temp_new(); 272 273 tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_ov, srcb, 0); 274 /* The result of divide-by-zero is undefined. 275 Suppress the host-side exception by dividing by 1. */ 276 tcg_gen_or_tl(t0, srcb, cpu_sr_ov); 277 tcg_gen_div_tl(dest, srca, t0); 278 279 tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov); 280 gen_ove_ov(dc); 281 } 282 283 static void gen_divu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb) 284 { 285 TCGv t0 = tcg_temp_new(); 286 287 tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_cy, srcb, 0); 288 /* The result of divide-by-zero is undefined. 289 Suppress the host-side exception by dividing by 1. */ 290 tcg_gen_or_tl(t0, srcb, cpu_sr_cy); 291 tcg_gen_divu_tl(dest, srca, t0); 292 293 gen_ove_cy(dc); 294 } 295 296 static void gen_muld(DisasContext *dc, TCGv srca, TCGv srcb) 297 { 298 TCGv_i64 t1 = tcg_temp_new_i64(); 299 TCGv_i64 t2 = tcg_temp_new_i64(); 300 301 tcg_gen_ext_tl_i64(t1, srca); 302 tcg_gen_ext_tl_i64(t2, srcb); 303 if (TARGET_LONG_BITS == 32) { 304 tcg_gen_mul_i64(cpu_mac, t1, t2); 305 tcg_gen_movi_tl(cpu_sr_ov, 0); 306 } else { 307 TCGv_i64 high = tcg_temp_new_i64(); 308 309 tcg_gen_muls2_i64(cpu_mac, high, t1, t2); 310 tcg_gen_sari_i64(t1, cpu_mac, 63); 311 tcg_gen_negsetcond_i64(TCG_COND_NE, t1, t1, high); 312 tcg_gen_trunc_i64_tl(cpu_sr_ov, t1); 313 314 gen_ove_ov(dc); 315 } 316 } 317 318 static void gen_muldu(DisasContext *dc, TCGv srca, TCGv srcb) 319 { 320 TCGv_i64 t1 = tcg_temp_new_i64(); 321 TCGv_i64 t2 = tcg_temp_new_i64(); 322 323 tcg_gen_extu_tl_i64(t1, srca); 324 tcg_gen_extu_tl_i64(t2, srcb); 325 if (TARGET_LONG_BITS == 32) { 326 tcg_gen_mul_i64(cpu_mac, t1, t2); 327 tcg_gen_movi_tl(cpu_sr_cy, 0); 328 } else { 329 TCGv_i64 high = tcg_temp_new_i64(); 330 331 tcg_gen_mulu2_i64(cpu_mac, high, t1, t2); 332 tcg_gen_setcondi_i64(TCG_COND_NE, high, high, 0); 333 tcg_gen_trunc_i64_tl(cpu_sr_cy, high); 334 335 gen_ove_cy(dc); 336 } 337 } 338 339 static void gen_mac(DisasContext *dc, TCGv srca, TCGv srcb) 340 { 341 TCGv_i64 t1 = tcg_temp_new_i64(); 342 TCGv_i64 t2 = tcg_temp_new_i64(); 343 344 tcg_gen_ext_tl_i64(t1, srca); 345 tcg_gen_ext_tl_i64(t2, srcb); 346 tcg_gen_mul_i64(t1, t1, t2); 347 348 /* Note that overflow is only computed during addition stage. */ 349 tcg_gen_xor_i64(t2, cpu_mac, t1); 350 tcg_gen_add_i64(cpu_mac, cpu_mac, t1); 351 tcg_gen_xor_i64(t1, t1, cpu_mac); 352 tcg_gen_andc_i64(t1, t1, t2); 353 354 #if TARGET_LONG_BITS == 32 355 tcg_gen_extrh_i64_i32(cpu_sr_ov, t1); 356 #else 357 tcg_gen_mov_i64(cpu_sr_ov, t1); 358 #endif 359 360 gen_ove_ov(dc); 361 } 362 363 static void gen_macu(DisasContext *dc, TCGv srca, TCGv srcb) 364 { 365 TCGv_i64 t1 = tcg_temp_new_i64(); 366 TCGv_i64 t2 = tcg_temp_new_i64(); 367 368 tcg_gen_extu_tl_i64(t1, srca); 369 tcg_gen_extu_tl_i64(t2, srcb); 370 tcg_gen_mul_i64(t1, t1, t2); 371 372 /* Note that overflow is only computed during addition stage. */ 373 tcg_gen_add_i64(cpu_mac, cpu_mac, t1); 374 tcg_gen_setcond_i64(TCG_COND_LTU, t1, cpu_mac, t1); 375 tcg_gen_trunc_i64_tl(cpu_sr_cy, t1); 376 377 gen_ove_cy(dc); 378 } 379 380 static void gen_msb(DisasContext *dc, TCGv srca, TCGv srcb) 381 { 382 TCGv_i64 t1 = tcg_temp_new_i64(); 383 TCGv_i64 t2 = tcg_temp_new_i64(); 384 385 tcg_gen_ext_tl_i64(t1, srca); 386 tcg_gen_ext_tl_i64(t2, srcb); 387 tcg_gen_mul_i64(t1, t1, t2); 388 389 /* Note that overflow is only computed during subtraction stage. */ 390 tcg_gen_xor_i64(t2, cpu_mac, t1); 391 tcg_gen_sub_i64(cpu_mac, cpu_mac, t1); 392 tcg_gen_xor_i64(t1, t1, cpu_mac); 393 tcg_gen_and_i64(t1, t1, t2); 394 395 #if TARGET_LONG_BITS == 32 396 tcg_gen_extrh_i64_i32(cpu_sr_ov, t1); 397 #else 398 tcg_gen_mov_i64(cpu_sr_ov, t1); 399 #endif 400 401 gen_ove_ov(dc); 402 } 403 404 static void gen_msbu(DisasContext *dc, TCGv srca, TCGv srcb) 405 { 406 TCGv_i64 t1 = tcg_temp_new_i64(); 407 TCGv_i64 t2 = tcg_temp_new_i64(); 408 409 tcg_gen_extu_tl_i64(t1, srca); 410 tcg_gen_extu_tl_i64(t2, srcb); 411 tcg_gen_mul_i64(t1, t1, t2); 412 413 /* Note that overflow is only computed during subtraction stage. */ 414 tcg_gen_setcond_i64(TCG_COND_LTU, t2, cpu_mac, t1); 415 tcg_gen_sub_i64(cpu_mac, cpu_mac, t1); 416 tcg_gen_trunc_i64_tl(cpu_sr_cy, t2); 417 418 gen_ove_cy(dc); 419 } 420 421 static bool trans_l_add(DisasContext *dc, arg_dab *a) 422 { 423 check_r0_write(dc, a->d); 424 gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 425 return true; 426 } 427 428 static bool trans_l_addc(DisasContext *dc, arg_dab *a) 429 { 430 check_r0_write(dc, a->d); 431 gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 432 return true; 433 } 434 435 static bool trans_l_sub(DisasContext *dc, arg_dab *a) 436 { 437 check_r0_write(dc, a->d); 438 gen_sub(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 439 return true; 440 } 441 442 static bool trans_l_and(DisasContext *dc, arg_dab *a) 443 { 444 check_r0_write(dc, a->d); 445 tcg_gen_and_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 446 return true; 447 } 448 449 static bool trans_l_or(DisasContext *dc, arg_dab *a) 450 { 451 check_r0_write(dc, a->d); 452 tcg_gen_or_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 453 return true; 454 } 455 456 static bool trans_l_xor(DisasContext *dc, arg_dab *a) 457 { 458 check_r0_write(dc, a->d); 459 tcg_gen_xor_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 460 return true; 461 } 462 463 static bool trans_l_sll(DisasContext *dc, arg_dab *a) 464 { 465 check_r0_write(dc, a->d); 466 tcg_gen_shl_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 467 return true; 468 } 469 470 static bool trans_l_srl(DisasContext *dc, arg_dab *a) 471 { 472 check_r0_write(dc, a->d); 473 tcg_gen_shr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 474 return true; 475 } 476 477 static bool trans_l_sra(DisasContext *dc, arg_dab *a) 478 { 479 check_r0_write(dc, a->d); 480 tcg_gen_sar_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 481 return true; 482 } 483 484 static bool trans_l_ror(DisasContext *dc, arg_dab *a) 485 { 486 check_r0_write(dc, a->d); 487 tcg_gen_rotr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 488 return true; 489 } 490 491 static bool trans_l_exths(DisasContext *dc, arg_da *a) 492 { 493 check_r0_write(dc, a->d); 494 tcg_gen_ext16s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a)); 495 return true; 496 } 497 498 static bool trans_l_extbs(DisasContext *dc, arg_da *a) 499 { 500 check_r0_write(dc, a->d); 501 tcg_gen_ext8s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a)); 502 return true; 503 } 504 505 static bool trans_l_exthz(DisasContext *dc, arg_da *a) 506 { 507 check_r0_write(dc, a->d); 508 tcg_gen_ext16u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a)); 509 return true; 510 } 511 512 static bool trans_l_extbz(DisasContext *dc, arg_da *a) 513 { 514 check_r0_write(dc, a->d); 515 tcg_gen_ext8u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a)); 516 return true; 517 } 518 519 static bool trans_l_cmov(DisasContext *dc, arg_dab *a) 520 { 521 check_r0_write(dc, a->d); 522 tcg_gen_movcond_tl(TCG_COND_NE, cpu_R(dc, a->d), cpu_sr_f, dc->zero, 523 cpu_R(dc, a->a), cpu_R(dc, a->b)); 524 return true; 525 } 526 527 static bool trans_l_ff1(DisasContext *dc, arg_da *a) 528 { 529 check_r0_write(dc, a->d); 530 tcg_gen_ctzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), -1); 531 tcg_gen_addi_tl(cpu_R(dc, a->d), cpu_R(dc, a->d), 1); 532 return true; 533 } 534 535 static bool trans_l_fl1(DisasContext *dc, arg_da *a) 536 { 537 check_r0_write(dc, a->d); 538 tcg_gen_clzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), TARGET_LONG_BITS); 539 tcg_gen_subfi_tl(cpu_R(dc, a->d), TARGET_LONG_BITS, cpu_R(dc, a->d)); 540 return true; 541 } 542 543 static bool trans_l_mul(DisasContext *dc, arg_dab *a) 544 { 545 check_r0_write(dc, a->d); 546 gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 547 return true; 548 } 549 550 static bool trans_l_mulu(DisasContext *dc, arg_dab *a) 551 { 552 check_r0_write(dc, a->d); 553 gen_mulu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 554 return true; 555 } 556 557 static bool trans_l_div(DisasContext *dc, arg_dab *a) 558 { 559 check_r0_write(dc, a->d); 560 gen_div(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 561 return true; 562 } 563 564 static bool trans_l_divu(DisasContext *dc, arg_dab *a) 565 { 566 check_r0_write(dc, a->d); 567 gen_divu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b)); 568 return true; 569 } 570 571 static bool trans_l_muld(DisasContext *dc, arg_ab *a) 572 { 573 gen_muld(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 574 return true; 575 } 576 577 static bool trans_l_muldu(DisasContext *dc, arg_ab *a) 578 { 579 gen_muldu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 580 return true; 581 } 582 583 static bool trans_l_j(DisasContext *dc, arg_l_j *a) 584 { 585 target_ulong tmp_pc = dc->base.pc_next + a->n * 4; 586 587 tcg_gen_movi_tl(jmp_pc, tmp_pc); 588 dc->jmp_pc_imm = tmp_pc; 589 dc->delayed_branch = 2; 590 return true; 591 } 592 593 static bool trans_l_jal(DisasContext *dc, arg_l_jal *a) 594 { 595 target_ulong tmp_pc = dc->base.pc_next + a->n * 4; 596 target_ulong ret_pc = dc->base.pc_next + 8; 597 598 tcg_gen_movi_tl(cpu_regs[9], ret_pc); 599 /* Optimize jal being used to load the PC for PIC. */ 600 if (tmp_pc != ret_pc) { 601 tcg_gen_movi_tl(jmp_pc, tmp_pc); 602 dc->jmp_pc_imm = tmp_pc; 603 dc->delayed_branch = 2; 604 } 605 return true; 606 } 607 608 static void do_bf(DisasContext *dc, arg_l_bf *a, TCGCond cond) 609 { 610 target_ulong tmp_pc = dc->base.pc_next + a->n * 4; 611 TCGv t_next = tcg_constant_tl(dc->base.pc_next + 8); 612 TCGv t_true = tcg_constant_tl(tmp_pc); 613 614 tcg_gen_movcond_tl(cond, jmp_pc, cpu_sr_f, dc->zero, t_true, t_next); 615 dc->delayed_branch = 2; 616 } 617 618 static bool trans_l_bf(DisasContext *dc, arg_l_bf *a) 619 { 620 do_bf(dc, a, TCG_COND_NE); 621 return true; 622 } 623 624 static bool trans_l_bnf(DisasContext *dc, arg_l_bf *a) 625 { 626 do_bf(dc, a, TCG_COND_EQ); 627 return true; 628 } 629 630 static bool trans_l_jr(DisasContext *dc, arg_l_jr *a) 631 { 632 tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b)); 633 dc->delayed_branch = 2; 634 return true; 635 } 636 637 static bool trans_l_jalr(DisasContext *dc, arg_l_jalr *a) 638 { 639 tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b)); 640 tcg_gen_movi_tl(cpu_regs[9], dc->base.pc_next + 8); 641 dc->delayed_branch = 2; 642 return true; 643 } 644 645 static bool trans_l_lwa(DisasContext *dc, arg_load *a) 646 { 647 TCGv ea; 648 649 check_r0_write(dc, a->d); 650 ea = tcg_temp_new(); 651 tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i); 652 tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, MO_TEUL); 653 tcg_gen_mov_tl(cpu_lock_addr, ea); 654 tcg_gen_mov_tl(cpu_lock_value, cpu_R(dc, a->d)); 655 return true; 656 } 657 658 static void do_load(DisasContext *dc, arg_load *a, MemOp mop) 659 { 660 TCGv ea; 661 662 check_r0_write(dc, a->d); 663 ea = tcg_temp_new(); 664 tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i); 665 tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, mop); 666 } 667 668 static bool trans_l_lwz(DisasContext *dc, arg_load *a) 669 { 670 do_load(dc, a, MO_TEUL); 671 return true; 672 } 673 674 static bool trans_l_lws(DisasContext *dc, arg_load *a) 675 { 676 do_load(dc, a, MO_TESL); 677 return true; 678 } 679 680 static bool trans_l_lbz(DisasContext *dc, arg_load *a) 681 { 682 do_load(dc, a, MO_UB); 683 return true; 684 } 685 686 static bool trans_l_lbs(DisasContext *dc, arg_load *a) 687 { 688 do_load(dc, a, MO_SB); 689 return true; 690 } 691 692 static bool trans_l_lhz(DisasContext *dc, arg_load *a) 693 { 694 do_load(dc, a, MO_TEUW); 695 return true; 696 } 697 698 static bool trans_l_lhs(DisasContext *dc, arg_load *a) 699 { 700 do_load(dc, a, MO_TESW); 701 return true; 702 } 703 704 static bool trans_l_swa(DisasContext *dc, arg_store *a) 705 { 706 TCGv ea, val; 707 TCGLabel *lab_fail, *lab_done; 708 709 ea = tcg_temp_new(); 710 tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i); 711 712 lab_fail = gen_new_label(); 713 lab_done = gen_new_label(); 714 tcg_gen_brcond_tl(TCG_COND_NE, ea, cpu_lock_addr, lab_fail); 715 716 val = tcg_temp_new(); 717 tcg_gen_atomic_cmpxchg_tl(val, cpu_lock_addr, cpu_lock_value, 718 cpu_R(dc, a->b), dc->mem_idx, MO_TEUL); 719 tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f, val, cpu_lock_value); 720 721 tcg_gen_br(lab_done); 722 723 gen_set_label(lab_fail); 724 tcg_gen_movi_tl(cpu_sr_f, 0); 725 726 gen_set_label(lab_done); 727 tcg_gen_movi_tl(cpu_lock_addr, -1); 728 return true; 729 } 730 731 static void do_store(DisasContext *dc, arg_store *a, MemOp mop) 732 { 733 TCGv t0 = tcg_temp_new(); 734 tcg_gen_addi_tl(t0, cpu_R(dc, a->a), a->i); 735 tcg_gen_qemu_st_tl(cpu_R(dc, a->b), t0, dc->mem_idx, mop); 736 } 737 738 static bool trans_l_sw(DisasContext *dc, arg_store *a) 739 { 740 do_store(dc, a, MO_TEUL); 741 return true; 742 } 743 744 static bool trans_l_sb(DisasContext *dc, arg_store *a) 745 { 746 do_store(dc, a, MO_UB); 747 return true; 748 } 749 750 static bool trans_l_sh(DisasContext *dc, arg_store *a) 751 { 752 do_store(dc, a, MO_TEUW); 753 return true; 754 } 755 756 static bool trans_l_nop(DisasContext *dc, arg_l_nop *a) 757 { 758 return true; 759 } 760 761 static bool trans_l_adrp(DisasContext *dc, arg_l_adrp *a) 762 { 763 if (!check_v1_3(dc)) { 764 return false; 765 } 766 check_r0_write(dc, a->d); 767 768 tcg_gen_movi_i32(cpu_R(dc, a->d), 769 (dc->base.pc_next & TARGET_PAGE_MASK) + 770 ((target_long)a->i << TARGET_PAGE_BITS)); 771 return true; 772 } 773 774 static bool trans_l_addi(DisasContext *dc, arg_rri *a) 775 { 776 check_r0_write(dc, a->d); 777 gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i)); 778 return true; 779 } 780 781 static bool trans_l_addic(DisasContext *dc, arg_rri *a) 782 { 783 check_r0_write(dc, a->d); 784 gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i)); 785 return true; 786 } 787 788 static bool trans_l_muli(DisasContext *dc, arg_rri *a) 789 { 790 check_r0_write(dc, a->d); 791 gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i)); 792 return true; 793 } 794 795 static bool trans_l_maci(DisasContext *dc, arg_l_maci *a) 796 { 797 gen_mac(dc, cpu_R(dc, a->a), tcg_constant_tl(a->i)); 798 return true; 799 } 800 801 static bool trans_l_andi(DisasContext *dc, arg_rrk *a) 802 { 803 check_r0_write(dc, a->d); 804 tcg_gen_andi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k); 805 return true; 806 } 807 808 static bool trans_l_ori(DisasContext *dc, arg_rrk *a) 809 { 810 check_r0_write(dc, a->d); 811 tcg_gen_ori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k); 812 return true; 813 } 814 815 static bool trans_l_xori(DisasContext *dc, arg_rri *a) 816 { 817 check_r0_write(dc, a->d); 818 tcg_gen_xori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->i); 819 return true; 820 } 821 822 static bool trans_l_mfspr(DisasContext *dc, arg_l_mfspr *a) 823 { 824 TCGv spr = tcg_temp_new(); 825 826 check_r0_write(dc, a->d); 827 828 if (translator_io_start(&dc->base)) { 829 if (dc->delayed_branch) { 830 tcg_gen_mov_tl(cpu_pc, jmp_pc); 831 tcg_gen_discard_tl(jmp_pc); 832 } else { 833 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4); 834 } 835 dc->base.is_jmp = DISAS_EXIT; 836 } 837 838 tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k); 839 gen_helper_mfspr(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d), spr); 840 return true; 841 } 842 843 static bool trans_l_mtspr(DisasContext *dc, arg_l_mtspr *a) 844 { 845 TCGv spr = tcg_temp_new(); 846 847 translator_io_start(&dc->base); 848 849 /* 850 * For SR, we will need to exit the TB to recognize the new 851 * exception state. For NPC, in theory this counts as a branch 852 * (although the SPR only exists for use by an ICE). Save all 853 * of the cpu state first, allowing it to be overwritten. 854 */ 855 if (dc->delayed_branch) { 856 tcg_gen_mov_tl(cpu_pc, jmp_pc); 857 tcg_gen_discard_tl(jmp_pc); 858 } else { 859 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4); 860 } 861 dc->base.is_jmp = DISAS_EXIT; 862 863 tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k); 864 gen_helper_mtspr(cpu_env, spr, cpu_R(dc, a->b)); 865 return true; 866 } 867 868 static bool trans_l_mac(DisasContext *dc, arg_ab *a) 869 { 870 gen_mac(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 871 return true; 872 } 873 874 static bool trans_l_msb(DisasContext *dc, arg_ab *a) 875 { 876 gen_msb(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 877 return true; 878 } 879 880 static bool trans_l_macu(DisasContext *dc, arg_ab *a) 881 { 882 gen_macu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 883 return true; 884 } 885 886 static bool trans_l_msbu(DisasContext *dc, arg_ab *a) 887 { 888 gen_msbu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b)); 889 return true; 890 } 891 892 static bool trans_l_slli(DisasContext *dc, arg_dal *a) 893 { 894 check_r0_write(dc, a->d); 895 tcg_gen_shli_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), 896 a->l & (TARGET_LONG_BITS - 1)); 897 return true; 898 } 899 900 static bool trans_l_srli(DisasContext *dc, arg_dal *a) 901 { 902 check_r0_write(dc, a->d); 903 tcg_gen_shri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), 904 a->l & (TARGET_LONG_BITS - 1)); 905 return true; 906 } 907 908 static bool trans_l_srai(DisasContext *dc, arg_dal *a) 909 { 910 check_r0_write(dc, a->d); 911 tcg_gen_sari_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), 912 a->l & (TARGET_LONG_BITS - 1)); 913 return true; 914 } 915 916 static bool trans_l_rori(DisasContext *dc, arg_dal *a) 917 { 918 check_r0_write(dc, a->d); 919 tcg_gen_rotri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), 920 a->l & (TARGET_LONG_BITS - 1)); 921 return true; 922 } 923 924 static bool trans_l_movhi(DisasContext *dc, arg_l_movhi *a) 925 { 926 check_r0_write(dc, a->d); 927 tcg_gen_movi_tl(cpu_R(dc, a->d), a->k << 16); 928 return true; 929 } 930 931 static bool trans_l_macrc(DisasContext *dc, arg_l_macrc *a) 932 { 933 check_r0_write(dc, a->d); 934 tcg_gen_trunc_i64_tl(cpu_R(dc, a->d), cpu_mac); 935 tcg_gen_movi_i64(cpu_mac, 0); 936 return true; 937 } 938 939 static bool trans_l_sfeq(DisasContext *dc, arg_ab *a) 940 { 941 tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f, 942 cpu_R(dc, a->a), cpu_R(dc, a->b)); 943 return true; 944 } 945 946 static bool trans_l_sfne(DisasContext *dc, arg_ab *a) 947 { 948 tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_f, 949 cpu_R(dc, a->a), cpu_R(dc, a->b)); 950 return true; 951 } 952 953 static bool trans_l_sfgtu(DisasContext *dc, arg_ab *a) 954 { 955 tcg_gen_setcond_tl(TCG_COND_GTU, cpu_sr_f, 956 cpu_R(dc, a->a), cpu_R(dc, a->b)); 957 return true; 958 } 959 960 static bool trans_l_sfgeu(DisasContext *dc, arg_ab *a) 961 { 962 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_sr_f, 963 cpu_R(dc, a->a), cpu_R(dc, a->b)); 964 return true; 965 } 966 967 static bool trans_l_sfltu(DisasContext *dc, arg_ab *a) 968 { 969 tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_f, 970 cpu_R(dc, a->a), cpu_R(dc, a->b)); 971 return true; 972 } 973 974 static bool trans_l_sfleu(DisasContext *dc, arg_ab *a) 975 { 976 tcg_gen_setcond_tl(TCG_COND_LEU, cpu_sr_f, 977 cpu_R(dc, a->a), cpu_R(dc, a->b)); 978 return true; 979 } 980 981 static bool trans_l_sfgts(DisasContext *dc, arg_ab *a) 982 { 983 tcg_gen_setcond_tl(TCG_COND_GT, cpu_sr_f, 984 cpu_R(dc, a->a), cpu_R(dc, a->b)); 985 return true; 986 } 987 988 static bool trans_l_sfges(DisasContext *dc, arg_ab *a) 989 { 990 tcg_gen_setcond_tl(TCG_COND_GE, cpu_sr_f, 991 cpu_R(dc, a->a), cpu_R(dc, a->b)); 992 return true; 993 } 994 995 static bool trans_l_sflts(DisasContext *dc, arg_ab *a) 996 { 997 tcg_gen_setcond_tl(TCG_COND_LT, cpu_sr_f, 998 cpu_R(dc, a->a), cpu_R(dc, a->b)); 999 return true; 1000 } 1001 1002 static bool trans_l_sfles(DisasContext *dc, arg_ab *a) 1003 { 1004 tcg_gen_setcond_tl(TCG_COND_LE, 1005 cpu_sr_f, cpu_R(dc, a->a), cpu_R(dc, a->b)); 1006 return true; 1007 } 1008 1009 static bool trans_l_sfeqi(DisasContext *dc, arg_ai *a) 1010 { 1011 tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_f, cpu_R(dc, a->a), a->i); 1012 return true; 1013 } 1014 1015 static bool trans_l_sfnei(DisasContext *dc, arg_ai *a) 1016 { 1017 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_f, cpu_R(dc, a->a), a->i); 1018 return true; 1019 } 1020 1021 static bool trans_l_sfgtui(DisasContext *dc, arg_ai *a) 1022 { 1023 tcg_gen_setcondi_tl(TCG_COND_GTU, cpu_sr_f, cpu_R(dc, a->a), a->i); 1024 return true; 1025 } 1026 1027 static bool trans_l_sfgeui(DisasContext *dc, arg_ai *a) 1028 { 1029 tcg_gen_setcondi_tl(TCG_COND_GEU, cpu_sr_f, cpu_R(dc, a->a), a->i); 1030 return true; 1031 } 1032 1033 static bool trans_l_sfltui(DisasContext *dc, arg_ai *a) 1034 { 1035 tcg_gen_setcondi_tl(TCG_COND_LTU, cpu_sr_f, cpu_R(dc, a->a), a->i); 1036 return true; 1037 } 1038 1039 static bool trans_l_sfleui(DisasContext *dc, arg_ai *a) 1040 { 1041 tcg_gen_setcondi_tl(TCG_COND_LEU, cpu_sr_f, cpu_R(dc, a->a), a->i); 1042 return true; 1043 } 1044 1045 static bool trans_l_sfgtsi(DisasContext *dc, arg_ai *a) 1046 { 1047 tcg_gen_setcondi_tl(TCG_COND_GT, cpu_sr_f, cpu_R(dc, a->a), a->i); 1048 return true; 1049 } 1050 1051 static bool trans_l_sfgesi(DisasContext *dc, arg_ai *a) 1052 { 1053 tcg_gen_setcondi_tl(TCG_COND_GE, cpu_sr_f, cpu_R(dc, a->a), a->i); 1054 return true; 1055 } 1056 1057 static bool trans_l_sfltsi(DisasContext *dc, arg_ai *a) 1058 { 1059 tcg_gen_setcondi_tl(TCG_COND_LT, cpu_sr_f, cpu_R(dc, a->a), a->i); 1060 return true; 1061 } 1062 1063 static bool trans_l_sflesi(DisasContext *dc, arg_ai *a) 1064 { 1065 tcg_gen_setcondi_tl(TCG_COND_LE, cpu_sr_f, cpu_R(dc, a->a), a->i); 1066 return true; 1067 } 1068 1069 static bool trans_l_sys(DisasContext *dc, arg_l_sys *a) 1070 { 1071 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next); 1072 gen_exception(dc, EXCP_SYSCALL); 1073 dc->base.is_jmp = DISAS_NORETURN; 1074 return true; 1075 } 1076 1077 static bool trans_l_trap(DisasContext *dc, arg_l_trap *a) 1078 { 1079 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next); 1080 gen_exception(dc, EXCP_TRAP); 1081 dc->base.is_jmp = DISAS_NORETURN; 1082 return true; 1083 } 1084 1085 static bool trans_l_msync(DisasContext *dc, arg_l_msync *a) 1086 { 1087 tcg_gen_mb(TCG_MO_ALL); 1088 return true; 1089 } 1090 1091 static bool trans_l_psync(DisasContext *dc, arg_l_psync *a) 1092 { 1093 return true; 1094 } 1095 1096 static bool trans_l_csync(DisasContext *dc, arg_l_csync *a) 1097 { 1098 return true; 1099 } 1100 1101 static bool trans_l_rfe(DisasContext *dc, arg_l_rfe *a) 1102 { 1103 if (is_user(dc)) { 1104 gen_illegal_exception(dc); 1105 } else { 1106 gen_helper_rfe(cpu_env); 1107 dc->base.is_jmp = DISAS_EXIT; 1108 } 1109 return true; 1110 } 1111 1112 static bool do_fp2(DisasContext *dc, arg_da *a, 1113 void (*fn)(TCGv, TCGv_env, TCGv)) 1114 { 1115 if (!check_of32s(dc)) { 1116 return false; 1117 } 1118 check_r0_write(dc, a->d); 1119 fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a)); 1120 gen_helper_update_fpcsr(cpu_env); 1121 return true; 1122 } 1123 1124 static bool do_fp3(DisasContext *dc, arg_dab *a, 1125 void (*fn)(TCGv, TCGv_env, TCGv, TCGv)) 1126 { 1127 if (!check_of32s(dc)) { 1128 return false; 1129 } 1130 check_r0_write(dc, a->d); 1131 fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b)); 1132 gen_helper_update_fpcsr(cpu_env); 1133 return true; 1134 } 1135 1136 static bool do_fpcmp(DisasContext *dc, arg_ab *a, 1137 void (*fn)(TCGv, TCGv_env, TCGv, TCGv), 1138 bool inv, bool swap) 1139 { 1140 if (!check_of32s(dc)) { 1141 return false; 1142 } 1143 if (swap) { 1144 fn(cpu_sr_f, cpu_env, cpu_R(dc, a->b), cpu_R(dc, a->a)); 1145 } else { 1146 fn(cpu_sr_f, cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b)); 1147 } 1148 if (inv) { 1149 tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1); 1150 } 1151 gen_helper_update_fpcsr(cpu_env); 1152 return true; 1153 } 1154 1155 static bool trans_lf_add_s(DisasContext *dc, arg_dab *a) 1156 { 1157 return do_fp3(dc, a, gen_helper_float_add_s); 1158 } 1159 1160 static bool trans_lf_sub_s(DisasContext *dc, arg_dab *a) 1161 { 1162 return do_fp3(dc, a, gen_helper_float_sub_s); 1163 } 1164 1165 static bool trans_lf_mul_s(DisasContext *dc, arg_dab *a) 1166 { 1167 return do_fp3(dc, a, gen_helper_float_mul_s); 1168 } 1169 1170 static bool trans_lf_div_s(DisasContext *dc, arg_dab *a) 1171 { 1172 return do_fp3(dc, a, gen_helper_float_div_s); 1173 } 1174 1175 static bool trans_lf_rem_s(DisasContext *dc, arg_dab *a) 1176 { 1177 return do_fp3(dc, a, gen_helper_float_rem_s); 1178 return true; 1179 } 1180 1181 static bool trans_lf_itof_s(DisasContext *dc, arg_da *a) 1182 { 1183 return do_fp2(dc, a, gen_helper_itofs); 1184 } 1185 1186 static bool trans_lf_ftoi_s(DisasContext *dc, arg_da *a) 1187 { 1188 return do_fp2(dc, a, gen_helper_ftois); 1189 } 1190 1191 static bool trans_lf_madd_s(DisasContext *dc, arg_dab *a) 1192 { 1193 if (!check_of32s(dc)) { 1194 return false; 1195 } 1196 check_r0_write(dc, a->d); 1197 gen_helper_float_madd_s(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d), 1198 cpu_R(dc, a->a), cpu_R(dc, a->b)); 1199 gen_helper_update_fpcsr(cpu_env); 1200 return true; 1201 } 1202 1203 static bool trans_lf_sfeq_s(DisasContext *dc, arg_ab *a) 1204 { 1205 return do_fpcmp(dc, a, gen_helper_float_eq_s, false, false); 1206 } 1207 1208 static bool trans_lf_sfne_s(DisasContext *dc, arg_ab *a) 1209 { 1210 return do_fpcmp(dc, a, gen_helper_float_eq_s, true, false); 1211 } 1212 1213 static bool trans_lf_sfgt_s(DisasContext *dc, arg_ab *a) 1214 { 1215 return do_fpcmp(dc, a, gen_helper_float_lt_s, false, true); 1216 } 1217 1218 static bool trans_lf_sfge_s(DisasContext *dc, arg_ab *a) 1219 { 1220 return do_fpcmp(dc, a, gen_helper_float_le_s, false, true); 1221 } 1222 1223 static bool trans_lf_sflt_s(DisasContext *dc, arg_ab *a) 1224 { 1225 return do_fpcmp(dc, a, gen_helper_float_lt_s, false, false); 1226 } 1227 1228 static bool trans_lf_sfle_s(DisasContext *dc, arg_ab *a) 1229 { 1230 return do_fpcmp(dc, a, gen_helper_float_le_s, false, false); 1231 } 1232 1233 static bool trans_lf_sfueq_s(DisasContext *dc, arg_ab *a) 1234 { 1235 if (!check_v1_3(dc)) { 1236 return false; 1237 } 1238 return do_fpcmp(dc, a, gen_helper_float_ueq_s, false, false); 1239 } 1240 1241 static bool trans_lf_sfult_s(DisasContext *dc, arg_ab *a) 1242 { 1243 if (!check_v1_3(dc)) { 1244 return false; 1245 } 1246 return do_fpcmp(dc, a, gen_helper_float_ult_s, false, false); 1247 } 1248 1249 static bool trans_lf_sfugt_s(DisasContext *dc, arg_ab *a) 1250 { 1251 if (!check_v1_3(dc)) { 1252 return false; 1253 } 1254 return do_fpcmp(dc, a, gen_helper_float_ult_s, false, true); 1255 } 1256 1257 static bool trans_lf_sfule_s(DisasContext *dc, arg_ab *a) 1258 { 1259 if (!check_v1_3(dc)) { 1260 return false; 1261 } 1262 return do_fpcmp(dc, a, gen_helper_float_ule_s, false, false); 1263 } 1264 1265 static bool trans_lf_sfuge_s(DisasContext *dc, arg_ab *a) 1266 { 1267 if (!check_v1_3(dc)) { 1268 return false; 1269 } 1270 return do_fpcmp(dc, a, gen_helper_float_ule_s, false, true); 1271 } 1272 1273 static bool trans_lf_sfun_s(DisasContext *dc, arg_ab *a) 1274 { 1275 if (!check_v1_3(dc)) { 1276 return false; 1277 } 1278 return do_fpcmp(dc, a, gen_helper_float_un_s, false, false); 1279 } 1280 1281 static bool check_pair(DisasContext *dc, int r, int p) 1282 { 1283 return r + 1 + p < 32; 1284 } 1285 1286 static void load_pair(DisasContext *dc, TCGv_i64 t, int r, int p) 1287 { 1288 tcg_gen_concat_i32_i64(t, cpu_R(dc, r + 1 + p), cpu_R(dc, r)); 1289 } 1290 1291 static void save_pair(DisasContext *dc, TCGv_i64 t, int r, int p) 1292 { 1293 tcg_gen_extr_i64_i32(cpu_R(dc, r + 1 + p), cpu_R(dc, r), t); 1294 } 1295 1296 static bool do_dp3(DisasContext *dc, arg_dab_pair *a, 1297 void (*fn)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64)) 1298 { 1299 TCGv_i64 t0, t1; 1300 1301 if (!check_of64a32s(dc) || 1302 !check_pair(dc, a->a, a->ap) || 1303 !check_pair(dc, a->b, a->bp) || 1304 !check_pair(dc, a->d, a->dp)) { 1305 return false; 1306 } 1307 check_r0_write(dc, a->d); 1308 1309 t0 = tcg_temp_new_i64(); 1310 t1 = tcg_temp_new_i64(); 1311 load_pair(dc, t0, a->a, a->ap); 1312 load_pair(dc, t1, a->b, a->bp); 1313 fn(t0, cpu_env, t0, t1); 1314 save_pair(dc, t0, a->d, a->dp); 1315 1316 gen_helper_update_fpcsr(cpu_env); 1317 return true; 1318 } 1319 1320 static bool do_dp2(DisasContext *dc, arg_da_pair *a, 1321 void (*fn)(TCGv_i64, TCGv_env, TCGv_i64)) 1322 { 1323 TCGv_i64 t0; 1324 1325 if (!check_of64a32s(dc) || 1326 !check_pair(dc, a->a, a->ap) || 1327 !check_pair(dc, a->d, a->dp)) { 1328 return false; 1329 } 1330 check_r0_write(dc, a->d); 1331 1332 t0 = tcg_temp_new_i64(); 1333 load_pair(dc, t0, a->a, a->ap); 1334 fn(t0, cpu_env, t0); 1335 save_pair(dc, t0, a->d, a->dp); 1336 1337 gen_helper_update_fpcsr(cpu_env); 1338 return true; 1339 } 1340 1341 static bool do_dpcmp(DisasContext *dc, arg_ab_pair *a, 1342 void (*fn)(TCGv, TCGv_env, TCGv_i64, TCGv_i64), 1343 bool inv, bool swap) 1344 { 1345 TCGv_i64 t0, t1; 1346 1347 if (!check_of64a32s(dc) || 1348 !check_pair(dc, a->a, a->ap) || 1349 !check_pair(dc, a->b, a->bp)) { 1350 return false; 1351 } 1352 1353 t0 = tcg_temp_new_i64(); 1354 t1 = tcg_temp_new_i64(); 1355 load_pair(dc, t0, a->a, a->ap); 1356 load_pair(dc, t1, a->b, a->bp); 1357 if (swap) { 1358 fn(cpu_sr_f, cpu_env, t1, t0); 1359 } else { 1360 fn(cpu_sr_f, cpu_env, t0, t1); 1361 } 1362 1363 if (inv) { 1364 tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1); 1365 } 1366 gen_helper_update_fpcsr(cpu_env); 1367 return true; 1368 } 1369 1370 static bool trans_lf_add_d(DisasContext *dc, arg_dab_pair *a) 1371 { 1372 return do_dp3(dc, a, gen_helper_float_add_d); 1373 } 1374 1375 static bool trans_lf_sub_d(DisasContext *dc, arg_dab_pair *a) 1376 { 1377 return do_dp3(dc, a, gen_helper_float_sub_d); 1378 } 1379 1380 static bool trans_lf_mul_d(DisasContext *dc, arg_dab_pair *a) 1381 { 1382 return do_dp3(dc, a, gen_helper_float_mul_d); 1383 } 1384 1385 static bool trans_lf_div_d(DisasContext *dc, arg_dab_pair *a) 1386 { 1387 return do_dp3(dc, a, gen_helper_float_div_d); 1388 } 1389 1390 static bool trans_lf_rem_d(DisasContext *dc, arg_dab_pair *a) 1391 { 1392 return do_dp3(dc, a, gen_helper_float_rem_d); 1393 } 1394 1395 static bool trans_lf_itof_d(DisasContext *dc, arg_da_pair *a) 1396 { 1397 return do_dp2(dc, a, gen_helper_itofd); 1398 } 1399 1400 static bool trans_lf_ftoi_d(DisasContext *dc, arg_da_pair *a) 1401 { 1402 return do_dp2(dc, a, gen_helper_ftoid); 1403 } 1404 1405 static bool trans_lf_stod_d(DisasContext *dc, arg_lf_stod_d *a) 1406 { 1407 TCGv_i64 t0; 1408 1409 if (!check_of64a32s(dc) || 1410 !check_pair(dc, a->d, a->dp)) { 1411 return false; 1412 } 1413 check_r0_write(dc, a->d); 1414 1415 t0 = tcg_temp_new_i64(); 1416 gen_helper_stod(t0, cpu_env, cpu_R(dc, a->a)); 1417 save_pair(dc, t0, a->d, a->dp); 1418 1419 gen_helper_update_fpcsr(cpu_env); 1420 return true; 1421 } 1422 1423 static bool trans_lf_dtos_d(DisasContext *dc, arg_lf_dtos_d *a) 1424 { 1425 TCGv_i64 t0; 1426 1427 if (!check_of64a32s(dc) || 1428 !check_pair(dc, a->a, a->ap)) { 1429 return false; 1430 } 1431 check_r0_write(dc, a->d); 1432 1433 t0 = tcg_temp_new_i64(); 1434 load_pair(dc, t0, a->a, a->ap); 1435 gen_helper_dtos(cpu_R(dc, a->d), cpu_env, t0); 1436 1437 gen_helper_update_fpcsr(cpu_env); 1438 return true; 1439 } 1440 1441 static bool trans_lf_madd_d(DisasContext *dc, arg_dab_pair *a) 1442 { 1443 TCGv_i64 t0, t1, t2; 1444 1445 if (!check_of64a32s(dc) || 1446 !check_pair(dc, a->a, a->ap) || 1447 !check_pair(dc, a->b, a->bp) || 1448 !check_pair(dc, a->d, a->dp)) { 1449 return false; 1450 } 1451 check_r0_write(dc, a->d); 1452 1453 t0 = tcg_temp_new_i64(); 1454 t1 = tcg_temp_new_i64(); 1455 t2 = tcg_temp_new_i64(); 1456 load_pair(dc, t0, a->d, a->dp); 1457 load_pair(dc, t1, a->a, a->ap); 1458 load_pair(dc, t2, a->b, a->bp); 1459 gen_helper_float_madd_d(t0, cpu_env, t0, t1, t2); 1460 save_pair(dc, t0, a->d, a->dp); 1461 1462 gen_helper_update_fpcsr(cpu_env); 1463 return true; 1464 } 1465 1466 static bool trans_lf_sfeq_d(DisasContext *dc, arg_ab_pair *a) 1467 { 1468 return do_dpcmp(dc, a, gen_helper_float_eq_d, false, false); 1469 } 1470 1471 static bool trans_lf_sfne_d(DisasContext *dc, arg_ab_pair *a) 1472 { 1473 return do_dpcmp(dc, a, gen_helper_float_eq_d, true, false); 1474 } 1475 1476 static bool trans_lf_sfgt_d(DisasContext *dc, arg_ab_pair *a) 1477 { 1478 return do_dpcmp(dc, a, gen_helper_float_lt_d, false, true); 1479 } 1480 1481 static bool trans_lf_sfge_d(DisasContext *dc, arg_ab_pair *a) 1482 { 1483 return do_dpcmp(dc, a, gen_helper_float_le_d, false, true); 1484 } 1485 1486 static bool trans_lf_sflt_d(DisasContext *dc, arg_ab_pair *a) 1487 { 1488 return do_dpcmp(dc, a, gen_helper_float_lt_d, false, false); 1489 } 1490 1491 static bool trans_lf_sfle_d(DisasContext *dc, arg_ab_pair *a) 1492 { 1493 return do_dpcmp(dc, a, gen_helper_float_le_d, false, false); 1494 } 1495 1496 static bool trans_lf_sfueq_d(DisasContext *dc, arg_ab_pair *a) 1497 { 1498 return do_dpcmp(dc, a, gen_helper_float_ueq_d, false, false); 1499 } 1500 1501 static bool trans_lf_sfule_d(DisasContext *dc, arg_ab_pair *a) 1502 { 1503 return do_dpcmp(dc, a, gen_helper_float_ule_d, false, false); 1504 } 1505 1506 static bool trans_lf_sfuge_d(DisasContext *dc, arg_ab_pair *a) 1507 { 1508 return do_dpcmp(dc, a, gen_helper_float_ule_d, false, true); 1509 } 1510 1511 static bool trans_lf_sfult_d(DisasContext *dc, arg_ab_pair *a) 1512 { 1513 return do_dpcmp(dc, a, gen_helper_float_ult_d, false, false); 1514 } 1515 1516 static bool trans_lf_sfugt_d(DisasContext *dc, arg_ab_pair *a) 1517 { 1518 return do_dpcmp(dc, a, gen_helper_float_ult_d, false, true); 1519 } 1520 1521 static bool trans_lf_sfun_d(DisasContext *dc, arg_ab_pair *a) 1522 { 1523 return do_dpcmp(dc, a, gen_helper_float_un_d, false, false); 1524 } 1525 1526 static void openrisc_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs) 1527 { 1528 DisasContext *dc = container_of(dcb, DisasContext, base); 1529 CPUOpenRISCState *env = cs->env_ptr; 1530 int bound; 1531 1532 dc->mem_idx = cpu_mmu_index(env, false); 1533 dc->tb_flags = dc->base.tb->flags; 1534 dc->delayed_branch = (dc->tb_flags & TB_FLAGS_DFLAG) != 0; 1535 dc->cpucfgr = env->cpucfgr; 1536 dc->avr = env->avr; 1537 dc->jmp_pc_imm = -1; 1538 1539 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4; 1540 dc->base.max_insns = MIN(dc->base.max_insns, bound); 1541 } 1542 1543 static void openrisc_tr_tb_start(DisasContextBase *db, CPUState *cs) 1544 { 1545 DisasContext *dc = container_of(db, DisasContext, base); 1546 1547 /* Allow the TCG optimizer to see that R0 == 0, 1548 when it's true, which is the common case. */ 1549 dc->zero = tcg_constant_tl(0); 1550 if (dc->tb_flags & TB_FLAGS_R0_0) { 1551 dc->R0 = dc->zero; 1552 } else { 1553 dc->R0 = cpu_regs[0]; 1554 } 1555 } 1556 1557 static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs) 1558 { 1559 DisasContext *dc = container_of(dcbase, DisasContext, base); 1560 1561 tcg_gen_insn_start(dc->base.pc_next, (dc->delayed_branch ? 1 : 0) 1562 | (dc->base.num_insns > 1 ? 2 : 0)); 1563 } 1564 1565 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs) 1566 { 1567 DisasContext *dc = container_of(dcbase, DisasContext, base); 1568 OpenRISCCPU *cpu = OPENRISC_CPU(cs); 1569 uint32_t insn = translator_ldl(&cpu->env, &dc->base, dc->base.pc_next); 1570 1571 if (!decode(dc, insn)) { 1572 gen_illegal_exception(dc); 1573 } 1574 dc->base.pc_next += 4; 1575 1576 /* When exiting the delay slot normally, exit via jmp_pc. 1577 * For DISAS_NORETURN, we have raised an exception and already exited. 1578 * For DISAS_EXIT, we found l.rfe in a delay slot. There's nothing 1579 * in the manual saying this is illegal, but it surely it should. 1580 * At least or1ksim overrides pcnext and ignores the branch. 1581 */ 1582 if (dc->delayed_branch 1583 && --dc->delayed_branch == 0 1584 && dc->base.is_jmp == DISAS_NEXT) { 1585 dc->base.is_jmp = DISAS_JUMP; 1586 } 1587 } 1588 1589 static void openrisc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs) 1590 { 1591 DisasContext *dc = container_of(dcbase, DisasContext, base); 1592 target_ulong jmp_dest; 1593 1594 /* If we have already exited the TB, nothing following has effect. */ 1595 if (dc->base.is_jmp == DISAS_NORETURN) { 1596 return; 1597 } 1598 1599 /* Adjust the delayed branch state for the next TB. */ 1600 if ((dc->tb_flags & TB_FLAGS_DFLAG ? 1 : 0) != (dc->delayed_branch != 0)) { 1601 tcg_gen_movi_i32(cpu_dflag, dc->delayed_branch != 0); 1602 } 1603 1604 /* For DISAS_TOO_MANY, jump to the next insn. */ 1605 jmp_dest = dc->base.pc_next; 1606 tcg_gen_movi_tl(cpu_ppc, jmp_dest - 4); 1607 1608 switch (dc->base.is_jmp) { 1609 case DISAS_JUMP: 1610 jmp_dest = dc->jmp_pc_imm; 1611 if (jmp_dest == -1) { 1612 /* The jump destination is indirect/computed; use jmp_pc. */ 1613 tcg_gen_mov_tl(cpu_pc, jmp_pc); 1614 tcg_gen_discard_tl(jmp_pc); 1615 tcg_gen_lookup_and_goto_ptr(); 1616 break; 1617 } 1618 /* The jump destination is direct; use jmp_pc_imm. 1619 However, we will have stored into jmp_pc as well; 1620 we know now that it wasn't needed. */ 1621 tcg_gen_discard_tl(jmp_pc); 1622 /* fallthru */ 1623 1624 case DISAS_TOO_MANY: 1625 if (translator_use_goto_tb(&dc->base, jmp_dest)) { 1626 tcg_gen_goto_tb(0); 1627 tcg_gen_movi_tl(cpu_pc, jmp_dest); 1628 tcg_gen_exit_tb(dc->base.tb, 0); 1629 break; 1630 } 1631 tcg_gen_movi_tl(cpu_pc, jmp_dest); 1632 tcg_gen_lookup_and_goto_ptr(); 1633 break; 1634 1635 case DISAS_EXIT: 1636 tcg_gen_exit_tb(NULL, 0); 1637 break; 1638 default: 1639 g_assert_not_reached(); 1640 } 1641 } 1642 1643 static void openrisc_tr_disas_log(const DisasContextBase *dcbase, 1644 CPUState *cs, FILE *logfile) 1645 { 1646 DisasContext *s = container_of(dcbase, DisasContext, base); 1647 1648 fprintf(logfile, "IN: %s\n", lookup_symbol(s->base.pc_first)); 1649 target_disas(logfile, cs, s->base.pc_first, s->base.tb->size); 1650 } 1651 1652 static const TranslatorOps openrisc_tr_ops = { 1653 .init_disas_context = openrisc_tr_init_disas_context, 1654 .tb_start = openrisc_tr_tb_start, 1655 .insn_start = openrisc_tr_insn_start, 1656 .translate_insn = openrisc_tr_translate_insn, 1657 .tb_stop = openrisc_tr_tb_stop, 1658 .disas_log = openrisc_tr_disas_log, 1659 }; 1660 1661 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns, 1662 target_ulong pc, void *host_pc) 1663 { 1664 DisasContext ctx; 1665 1666 translator_loop(cs, tb, max_insns, pc, host_pc, 1667 &openrisc_tr_ops, &ctx.base); 1668 } 1669 1670 void openrisc_cpu_dump_state(CPUState *cs, FILE *f, int flags) 1671 { 1672 OpenRISCCPU *cpu = OPENRISC_CPU(cs); 1673 CPUOpenRISCState *env = &cpu->env; 1674 int i; 1675 1676 qemu_fprintf(f, "PC=%08x\n", env->pc); 1677 for (i = 0; i < 32; ++i) { 1678 qemu_fprintf(f, "R%02d=%08x%c", i, cpu_get_gpr(env, i), 1679 (i % 4) == 3 ? '\n' : ' '); 1680 } 1681 } 1682