18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* ppc-dis.c -- Disassemble PowerPC instructions 38c2ecf20Sopenharmony_ci Copyright (C) 1994-2016 Free Software Foundation, Inc. 48c2ecf20Sopenharmony_ci Written by Ian Lance Taylor, Cygnus Support 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciThis file is part of GDB, GAS, and the GNU binutils. 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <asm/cputable.h> 118c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h> 128c2ecf20Sopenharmony_ci#include "nonstdio.h" 138c2ecf20Sopenharmony_ci#include "ansidecl.h" 148c2ecf20Sopenharmony_ci#include "ppc.h" 158c2ecf20Sopenharmony_ci#include "dis-asm.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* This file provides several disassembler functions, all of which use 188c2ecf20Sopenharmony_ci the disassembler interface defined in dis-asm.h. Several functions 198c2ecf20Sopenharmony_ci are provided because this file handles disassembly for the PowerPC 208c2ecf20Sopenharmony_ci in both big and little endian mode and also for the POWER (RS/6000) 218c2ecf20Sopenharmony_ci chip. */ 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* Extract the operand value from the PowerPC or POWER instruction. */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic long 268c2ecf20Sopenharmony_cioperand_value_powerpc (const struct powerpc_operand *operand, 278c2ecf20Sopenharmony_ci unsigned long insn, ppc_cpu_t dialect) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci long value; 308c2ecf20Sopenharmony_ci int invalid; 318c2ecf20Sopenharmony_ci /* Extract the value from the instruction. */ 328c2ecf20Sopenharmony_ci if (operand->extract) 338c2ecf20Sopenharmony_ci value = (*operand->extract) (insn, dialect, &invalid); 348c2ecf20Sopenharmony_ci else 358c2ecf20Sopenharmony_ci { 368c2ecf20Sopenharmony_ci if (operand->shift >= 0) 378c2ecf20Sopenharmony_ci value = (insn >> operand->shift) & operand->bitm; 388c2ecf20Sopenharmony_ci else 398c2ecf20Sopenharmony_ci value = (insn << -operand->shift) & operand->bitm; 408c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_SIGNED) != 0) 418c2ecf20Sopenharmony_ci { 428c2ecf20Sopenharmony_ci /* BITM is always some number of zeros followed by some 438c2ecf20Sopenharmony_ci number of ones, followed by some number of zeros. */ 448c2ecf20Sopenharmony_ci unsigned long top = operand->bitm; 458c2ecf20Sopenharmony_ci /* top & -top gives the rightmost 1 bit, so this 468c2ecf20Sopenharmony_ci fills in any trailing zeros. */ 478c2ecf20Sopenharmony_ci top |= (top & -top) - 1; 488c2ecf20Sopenharmony_ci top &= ~(top >> 1); 498c2ecf20Sopenharmony_ci value = (value ^ top) - top; 508c2ecf20Sopenharmony_ci } 518c2ecf20Sopenharmony_ci } 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci return value; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* Determine whether the optional operand(s) should be printed. */ 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic int 598c2ecf20Sopenharmony_ciskip_optional_operands (const unsigned char *opindex, 608c2ecf20Sopenharmony_ci unsigned long insn, ppc_cpu_t dialect) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci const struct powerpc_operand *operand; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci for (; *opindex != 0; opindex++) 658c2ecf20Sopenharmony_ci { 668c2ecf20Sopenharmony_ci operand = &powerpc_operands[*opindex]; 678c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_NEXT) != 0 688c2ecf20Sopenharmony_ci || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0 698c2ecf20Sopenharmony_ci && operand_value_powerpc (operand, insn, dialect) != 708c2ecf20Sopenharmony_ci ppc_optional_operand_value (operand))) 718c2ecf20Sopenharmony_ci return 0; 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci return 1; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci/* Find a match for INSN in the opcode table, given machine DIALECT. 788c2ecf20Sopenharmony_ci A DIALECT of -1 is special, matching all machine opcode variations. */ 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic const struct powerpc_opcode * 818c2ecf20Sopenharmony_cilookup_powerpc (unsigned long insn, ppc_cpu_t dialect) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci const struct powerpc_opcode *opcode; 848c2ecf20Sopenharmony_ci const struct powerpc_opcode *opcode_end; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci opcode_end = powerpc_opcodes + powerpc_num_opcodes; 878c2ecf20Sopenharmony_ci /* Find the first match in the opcode table for this major opcode. */ 888c2ecf20Sopenharmony_ci for (opcode = powerpc_opcodes; opcode < opcode_end; ++opcode) 898c2ecf20Sopenharmony_ci { 908c2ecf20Sopenharmony_ci const unsigned char *opindex; 918c2ecf20Sopenharmony_ci const struct powerpc_operand *operand; 928c2ecf20Sopenharmony_ci int invalid; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci if ((insn & opcode->mask) != opcode->opcode 958c2ecf20Sopenharmony_ci || (dialect != (ppc_cpu_t) -1 968c2ecf20Sopenharmony_ci && ((opcode->flags & dialect) == 0 978c2ecf20Sopenharmony_ci || (opcode->deprecated & dialect) != 0))) 988c2ecf20Sopenharmony_ci continue; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* Check validity of operands. */ 1018c2ecf20Sopenharmony_ci invalid = 0; 1028c2ecf20Sopenharmony_ci for (opindex = opcode->operands; *opindex != 0; opindex++) 1038c2ecf20Sopenharmony_ci { 1048c2ecf20Sopenharmony_ci operand = powerpc_operands + *opindex; 1058c2ecf20Sopenharmony_ci if (operand->extract) 1068c2ecf20Sopenharmony_ci (*operand->extract) (insn, dialect, &invalid); 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci if (invalid) 1098c2ecf20Sopenharmony_ci continue; 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return opcode; 1128c2ecf20Sopenharmony_ci } 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return NULL; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci/* Print a PowerPC or POWER instruction. */ 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ciint print_insn_powerpc (unsigned long insn, unsigned long memaddr) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci const struct powerpc_opcode *opcode; 1228c2ecf20Sopenharmony_ci bool insn_is_short; 1238c2ecf20Sopenharmony_ci ppc_cpu_t dialect; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci dialect = PPC_OPCODE_PPC | PPC_OPCODE_COMMON 1268c2ecf20Sopenharmony_ci | PPC_OPCODE_64 | PPC_OPCODE_POWER4 | PPC_OPCODE_ALTIVEC; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_POWER5)) 1298c2ecf20Sopenharmony_ci dialect |= PPC_OPCODE_POWER5; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_CELL)) 1328c2ecf20Sopenharmony_ci dialect |= (PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_POWER6)) 1358c2ecf20Sopenharmony_ci dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_POWER7)) 1388c2ecf20Sopenharmony_ci dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7 1398c2ecf20Sopenharmony_ci | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_POWER8)) 1428c2ecf20Sopenharmony_ci dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7 1438c2ecf20Sopenharmony_ci | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM 1448c2ecf20Sopenharmony_ci | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX); 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (cpu_has_feature(CPU_FTRS_POWER9)) 1478c2ecf20Sopenharmony_ci dialect |= (PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7 1488c2ecf20Sopenharmony_ci | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9 | PPC_OPCODE_HTM 1498c2ecf20Sopenharmony_ci | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 1508c2ecf20Sopenharmony_ci | PPC_OPCODE_VSX | PPC_OPCODE_VSX3); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci /* Get the major opcode of the insn. */ 1538c2ecf20Sopenharmony_ci opcode = NULL; 1548c2ecf20Sopenharmony_ci insn_is_short = false; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci if (opcode == NULL) 1578c2ecf20Sopenharmony_ci opcode = lookup_powerpc (insn, dialect); 1588c2ecf20Sopenharmony_ci if (opcode == NULL && (dialect & PPC_OPCODE_ANY) != 0) 1598c2ecf20Sopenharmony_ci opcode = lookup_powerpc (insn, (ppc_cpu_t) -1); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci if (opcode != NULL) 1628c2ecf20Sopenharmony_ci { 1638c2ecf20Sopenharmony_ci const unsigned char *opindex; 1648c2ecf20Sopenharmony_ci const struct powerpc_operand *operand; 1658c2ecf20Sopenharmony_ci int need_comma; 1668c2ecf20Sopenharmony_ci int need_paren; 1678c2ecf20Sopenharmony_ci int skip_optional; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (opcode->operands[0] != 0) 1708c2ecf20Sopenharmony_ci printf("%-7s ", opcode->name); 1718c2ecf20Sopenharmony_ci else 1728c2ecf20Sopenharmony_ci printf("%s", opcode->name); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (insn_is_short) 1758c2ecf20Sopenharmony_ci /* The operands will be fetched out of the 16-bit instruction. */ 1768c2ecf20Sopenharmony_ci insn >>= 16; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci /* Now extract and print the operands. */ 1798c2ecf20Sopenharmony_ci need_comma = 0; 1808c2ecf20Sopenharmony_ci need_paren = 0; 1818c2ecf20Sopenharmony_ci skip_optional = -1; 1828c2ecf20Sopenharmony_ci for (opindex = opcode->operands; *opindex != 0; opindex++) 1838c2ecf20Sopenharmony_ci { 1848c2ecf20Sopenharmony_ci long value; 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci operand = powerpc_operands + *opindex; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* Operands that are marked FAKE are simply ignored. We 1898c2ecf20Sopenharmony_ci already made sure that the extract function considered 1908c2ecf20Sopenharmony_ci the instruction to be valid. */ 1918c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_FAKE) != 0) 1928c2ecf20Sopenharmony_ci continue; 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci /* If all of the optional operands have the value zero, 1958c2ecf20Sopenharmony_ci then don't print any of them. */ 1968c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0) 1978c2ecf20Sopenharmony_ci { 1988c2ecf20Sopenharmony_ci if (skip_optional < 0) 1998c2ecf20Sopenharmony_ci skip_optional = skip_optional_operands (opindex, insn, 2008c2ecf20Sopenharmony_ci dialect); 2018c2ecf20Sopenharmony_ci if (skip_optional) 2028c2ecf20Sopenharmony_ci continue; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci value = operand_value_powerpc (operand, insn, dialect); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (need_comma) 2088c2ecf20Sopenharmony_ci { 2098c2ecf20Sopenharmony_ci printf(","); 2108c2ecf20Sopenharmony_ci need_comma = 0; 2118c2ecf20Sopenharmony_ci } 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci /* Print the operand as directed by the flags. */ 2148c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_GPR) != 0 2158c2ecf20Sopenharmony_ci || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0)) 2168c2ecf20Sopenharmony_ci printf("r%ld", value); 2178c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_FPR) != 0) 2188c2ecf20Sopenharmony_ci printf("f%ld", value); 2198c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_VR) != 0) 2208c2ecf20Sopenharmony_ci printf("v%ld", value); 2218c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_VSR) != 0) 2228c2ecf20Sopenharmony_ci printf("vs%ld", value); 2238c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0) 2248c2ecf20Sopenharmony_ci print_address(memaddr + value); 2258c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0) 2268c2ecf20Sopenharmony_ci print_address(value & 0xffffffff); 2278c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_FSL) != 0) 2288c2ecf20Sopenharmony_ci printf("fsl%ld", value); 2298c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_FCR) != 0) 2308c2ecf20Sopenharmony_ci printf("fcr%ld", value); 2318c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_UDI) != 0) 2328c2ecf20Sopenharmony_ci printf("%ld", value); 2338c2ecf20Sopenharmony_ci else if ((operand->flags & PPC_OPERAND_CR_REG) != 0 2348c2ecf20Sopenharmony_ci && (((dialect & PPC_OPCODE_PPC) != 0) 2358c2ecf20Sopenharmony_ci || ((dialect & PPC_OPCODE_VLE) != 0))) 2368c2ecf20Sopenharmony_ci printf("cr%ld", value); 2378c2ecf20Sopenharmony_ci else if (((operand->flags & PPC_OPERAND_CR_BIT) != 0) 2388c2ecf20Sopenharmony_ci && (((dialect & PPC_OPCODE_PPC) != 0) 2398c2ecf20Sopenharmony_ci || ((dialect & PPC_OPCODE_VLE) != 0))) 2408c2ecf20Sopenharmony_ci { 2418c2ecf20Sopenharmony_ci static const char *cbnames[4] = { "lt", "gt", "eq", "so" }; 2428c2ecf20Sopenharmony_ci int cr; 2438c2ecf20Sopenharmony_ci int cc; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci cr = value >> 2; 2468c2ecf20Sopenharmony_ci if (cr != 0) 2478c2ecf20Sopenharmony_ci printf("4*cr%d+", cr); 2488c2ecf20Sopenharmony_ci cc = value & 3; 2498c2ecf20Sopenharmony_ci printf("%s", cbnames[cc]); 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci else 2528c2ecf20Sopenharmony_ci printf("%d", (int) value); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (need_paren) 2558c2ecf20Sopenharmony_ci { 2568c2ecf20Sopenharmony_ci printf(")"); 2578c2ecf20Sopenharmony_ci need_paren = 0; 2588c2ecf20Sopenharmony_ci } 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci if ((operand->flags & PPC_OPERAND_PARENS) == 0) 2618c2ecf20Sopenharmony_ci need_comma = 1; 2628c2ecf20Sopenharmony_ci else 2638c2ecf20Sopenharmony_ci { 2648c2ecf20Sopenharmony_ci printf("("); 2658c2ecf20Sopenharmony_ci need_paren = 1; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci } 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci /* We have found and printed an instruction. 2708c2ecf20Sopenharmony_ci If it was a short VLE instruction we have more to do. */ 2718c2ecf20Sopenharmony_ci if (insn_is_short) 2728c2ecf20Sopenharmony_ci { 2738c2ecf20Sopenharmony_ci memaddr += 2; 2748c2ecf20Sopenharmony_ci return 2; 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci else 2778c2ecf20Sopenharmony_ci /* Otherwise, return. */ 2788c2ecf20Sopenharmony_ci return 4; 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* We could not find a match. */ 2828c2ecf20Sopenharmony_ci printf(".long 0x%lx", insn); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci return 4; 2858c2ecf20Sopenharmony_ci} 286