| 1 | /* Print instructions for the Motorola 88000, for GDB and GNU Binutils. | 
|---|
| 2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1993, 1998, 2000 | 
|---|
| 3 | Free Software Foundation, Inc. | 
|---|
| 4 | Contributed by Data General Corporation, November 1989. | 
|---|
| 5 | Partially derived from an earlier printcmd.c. | 
|---|
| 6 |  | 
|---|
| 7 | This file is part of GDB and the GNU Binutils. | 
|---|
| 8 |  | 
|---|
| 9 | This program is free software; you can redistribute it and/or modify | 
|---|
| 10 | it under the terms of the GNU General Public License as published by | 
|---|
| 11 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 12 | (at your option) any later version. | 
|---|
| 13 |  | 
|---|
| 14 | This program is distributed in the hope that it will be useful, | 
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | GNU General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU General Public License | 
|---|
| 20 | along with this program; if not, write to the Free Software | 
|---|
| 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */ | 
|---|
| 22 |  | 
|---|
| 23 | #include "sysdep.h" | 
|---|
| 24 | #include "dis-asm.h" | 
|---|
| 25 | #include "opcode/m88k.h" | 
|---|
| 26 | #include "opintl.h" | 
|---|
| 27 |  | 
|---|
| 28 | INSTAB  *hashtable[HASHVAL] = {0}; | 
|---|
| 29 |  | 
|---|
| 30 | static int | 
|---|
| 31 | m88kdis PARAMS ((bfd_vma, unsigned long, struct disassemble_info *)); | 
|---|
| 32 |  | 
|---|
| 33 | static void | 
|---|
| 34 | printop PARAMS ((struct disassemble_info *, OPSPEC *, | 
|---|
| 35 | unsigned long, bfd_vma, int)); | 
|---|
| 36 |  | 
|---|
| 37 | static void | 
|---|
| 38 | init_disasm PARAMS ((void)); | 
|---|
| 39 |  | 
|---|
| 40 | static void | 
|---|
| 41 | install PARAMS ((INSTAB *instptr)); | 
|---|
| 42 |  | 
|---|
| 43 | /* | 
|---|
| 44 | *               Disassemble an M88000 Instruction | 
|---|
| 45 | * | 
|---|
| 46 | * | 
|---|
| 47 | *       This module decodes the instruction at memaddr. | 
|---|
| 48 | * | 
|---|
| 49 | *                       Revision History | 
|---|
| 50 | * | 
|---|
| 51 | *       Revision 1.0    11/08/85        Creation date by Motorola | 
|---|
| 52 | *                       05/11/89        R. Trawick adapted to GDB interface. | 
|---|
| 53 | *                       07/12/93        Ian Lance Taylor updated to | 
|---|
| 54 | *                                       binutils interface. | 
|---|
| 55 | */ | 
|---|
| 56 |  | 
|---|
| 57 | int | 
|---|
| 58 | print_insn_m88k (memaddr, info) | 
|---|
| 59 | bfd_vma memaddr; | 
|---|
| 60 | struct disassemble_info *info; | 
|---|
| 61 | { | 
|---|
| 62 | bfd_byte buffer[4]; | 
|---|
| 63 | int status; | 
|---|
| 64 |  | 
|---|
| 65 | /* Instruction addresses may have low two bits set. Clear them.       */ | 
|---|
| 66 | memaddr &=~ (bfd_vma) 3; | 
|---|
| 67 |  | 
|---|
| 68 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | 
|---|
| 69 | if (status != 0) | 
|---|
| 70 | { | 
|---|
| 71 | (*info->memory_error_func) (status, memaddr, info); | 
|---|
| 72 | return -1; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | return m88kdis (memaddr, bfd_getb32 (buffer), info); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | /* | 
|---|
| 79 | * disassemble the instruction in 'instruction'. | 
|---|
| 80 | * 'pc' should be the address of this instruction, it will | 
|---|
| 81 | *   be used to print the target address if this is a relative jump or call | 
|---|
| 82 | * the disassembled instruction is written to 'info'. | 
|---|
| 83 | * The function returns the length of this instruction in bytes. | 
|---|
| 84 | */ | 
|---|
| 85 |  | 
|---|
| 86 | static int | 
|---|
| 87 | m88kdis (pc, instruction, info) | 
|---|
| 88 | bfd_vma pc; | 
|---|
| 89 | unsigned long instruction; | 
|---|
| 90 | struct disassemble_info *info; | 
|---|
| 91 | { | 
|---|
| 92 | static int ihashtab_initialized = 0; | 
|---|
| 93 | unsigned int opcode; | 
|---|
| 94 | INSTAB *entry_ptr; | 
|---|
| 95 | int opmask; | 
|---|
| 96 | unsigned int class; | 
|---|
| 97 |  | 
|---|
| 98 | if (! ihashtab_initialized) | 
|---|
| 99 | init_disasm (); | 
|---|
| 100 |  | 
|---|
| 101 | /* create the appropriate mask to isolate the opcode */ | 
|---|
| 102 | opmask = DEFMASK; | 
|---|
| 103 | class = instruction & DEFMASK; | 
|---|
| 104 | if ((class >= SFU0) && (class <= SFU7)) | 
|---|
| 105 | { | 
|---|
| 106 | if (instruction < SFU1) | 
|---|
| 107 | opmask = CTRLMASK; | 
|---|
| 108 | else | 
|---|
| 109 | opmask = SFUMASK; | 
|---|
| 110 | } | 
|---|
| 111 | else if (class == RRR) | 
|---|
| 112 | opmask = RRRMASK; | 
|---|
| 113 | else if (class == RRI10) | 
|---|
| 114 | opmask = RRI10MASK; | 
|---|
| 115 |  | 
|---|
| 116 | /* isolate the opcode */ | 
|---|
| 117 | opcode = instruction & opmask; | 
|---|
| 118 |  | 
|---|
| 119 | /* search the hash table with the isolated opcode */ | 
|---|
| 120 | for (entry_ptr = hashtable[opcode % HASHVAL]; | 
|---|
| 121 | (entry_ptr != NULL) && (entry_ptr->opcode != opcode); | 
|---|
| 122 | entry_ptr = entry_ptr->next) | 
|---|
| 123 | ; | 
|---|
| 124 |  | 
|---|
| 125 | if (entry_ptr == NULL) | 
|---|
| 126 | (*info->fprintf_func) (info->stream, "word\t%08x", instruction); | 
|---|
| 127 | else | 
|---|
| 128 | { | 
|---|
| 129 | (*info->fprintf_func) (info->stream, "%s", entry_ptr->mnemonic); | 
|---|
| 130 | printop (info, &(entry_ptr->op1), instruction, pc, 1); | 
|---|
| 131 | printop (info, &(entry_ptr->op2), instruction, pc, 0); | 
|---|
| 132 | printop (info, &(entry_ptr->op3), instruction, pc, 0); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | return 4; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 139 | /* | 
|---|
| 140 | *                      Decode an Operand of an Instruction | 
|---|
| 141 | * | 
|---|
| 142 | *                       Functional Description | 
|---|
| 143 | * | 
|---|
| 144 | *       This module formats and writes an operand of an instruction to info | 
|---|
| 145 | *       based on the operand specification.  When the first flag is set this | 
|---|
| 146 | *       is the first operand of an instruction.  Undefined operand types | 
|---|
| 147 | *       cause a <dis error> message. | 
|---|
| 148 | * | 
|---|
| 149 | *                       Parameters | 
|---|
| 150 | *       disassemble_info        where the operand may be printed | 
|---|
| 151 | *       OPSPEC  *opptr          Pointer to an operand specification | 
|---|
| 152 | *       UINT    inst            Instruction from which operand is extracted | 
|---|
| 153 | *       UINT    pc              PC of instruction; used for pc-relative disp. | 
|---|
| 154 | *       int     first           Flag which if nonzero indicates the first | 
|---|
| 155 | *                               operand of an instruction | 
|---|
| 156 | * | 
|---|
| 157 | *                       Output | 
|---|
| 158 | * | 
|---|
| 159 | *       The operand specified is extracted from the instruction and is | 
|---|
| 160 | *       written to buf in the format specified. The operand is preceded | 
|---|
| 161 | *       by a comma if it is not the first operand of an instruction and it | 
|---|
| 162 | *       is not a register indirect form.  Registers are preceded by 'r' and | 
|---|
| 163 | *       hex values by '0x'. | 
|---|
| 164 | * | 
|---|
| 165 | *                       Revision History | 
|---|
| 166 | * | 
|---|
| 167 | *       Revision 1.0    11/08/85        Creation date | 
|---|
| 168 | */ | 
|---|
| 169 |  | 
|---|
| 170 | static void | 
|---|
| 171 | printop (info, opptr, inst, pc, first) | 
|---|
| 172 | struct disassemble_info *info; | 
|---|
| 173 | OPSPEC *opptr; | 
|---|
| 174 | unsigned long inst; | 
|---|
| 175 | bfd_vma pc; | 
|---|
| 176 | int first; | 
|---|
| 177 | { | 
|---|
| 178 | int extracted_field; | 
|---|
| 179 | char *cond_mask_sym; | 
|---|
| 180 |  | 
|---|
| 181 | if (opptr->width == 0) | 
|---|
| 182 | return; | 
|---|
| 183 |  | 
|---|
| 184 | if (! first) | 
|---|
| 185 | { | 
|---|
| 186 | switch (opptr->type) | 
|---|
| 187 | { | 
|---|
| 188 | case REGSC: | 
|---|
| 189 | case CONT: | 
|---|
| 190 | break; | 
|---|
| 191 | default: | 
|---|
| 192 | (*info->fprintf_func) (info->stream, ","); | 
|---|
| 193 | break; | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | switch (opptr->type) | 
|---|
| 198 | { | 
|---|
| 199 | case CRREG: | 
|---|
| 200 | (*info->fprintf_func) (info->stream, "cr%d", | 
|---|
| 201 | UEXT (inst, opptr->offset, opptr->width)); | 
|---|
| 202 | break; | 
|---|
| 203 |  | 
|---|
| 204 | case FCRREG: | 
|---|
| 205 | (*info->fprintf_func) (info->stream, "fcr%d", | 
|---|
| 206 | UEXT (inst, opptr->offset, opptr->width)); | 
|---|
| 207 | break; | 
|---|
| 208 |  | 
|---|
| 209 | case REGSC: | 
|---|
| 210 | (*info->fprintf_func) (info->stream, "[r%d]", | 
|---|
| 211 | UEXT (inst, opptr->offset, opptr->width)); | 
|---|
| 212 | break; | 
|---|
| 213 |  | 
|---|
| 214 | case REG: | 
|---|
| 215 | (*info->fprintf_func) (info->stream, "r%d", | 
|---|
| 216 | UEXT (inst, opptr->offset, opptr->width)); | 
|---|
| 217 | break; | 
|---|
| 218 |  | 
|---|
| 219 | case XREG: | 
|---|
| 220 | (*info->fprintf_func) (info->stream, "x%d", | 
|---|
| 221 | UEXT (inst, opptr->offset, opptr->width)); | 
|---|
| 222 | break; | 
|---|
| 223 |  | 
|---|
| 224 | case HEX: | 
|---|
| 225 | extracted_field = UEXT (inst, opptr->offset, opptr->width); | 
|---|
| 226 | if (extracted_field == 0) | 
|---|
| 227 | (*info->fprintf_func) (info->stream, "0"); | 
|---|
| 228 | else | 
|---|
| 229 | (*info->fprintf_func) (info->stream, "0x%02x", extracted_field); | 
|---|
| 230 | break; | 
|---|
| 231 |  | 
|---|
| 232 | case DEC: | 
|---|
| 233 | extracted_field = UEXT (inst, opptr->offset, opptr->width); | 
|---|
| 234 | (*info->fprintf_func) (info->stream, "%d", extracted_field); | 
|---|
| 235 | break; | 
|---|
| 236 |  | 
|---|
| 237 | case CONDMASK: | 
|---|
| 238 | extracted_field = UEXT (inst, opptr->offset, opptr->width); | 
|---|
| 239 | switch (extracted_field & 0x0f) | 
|---|
| 240 | { | 
|---|
| 241 | case 0x1: cond_mask_sym = "gt0"; break; | 
|---|
| 242 | case 0x2: cond_mask_sym = "eq0"; break; | 
|---|
| 243 | case 0x3: cond_mask_sym = "ge0"; break; | 
|---|
| 244 | case 0xc: cond_mask_sym = "lt0"; break; | 
|---|
| 245 | case 0xd: cond_mask_sym = "ne0"; break; | 
|---|
| 246 | case 0xe: cond_mask_sym = "le0"; break; | 
|---|
| 247 | default: cond_mask_sym = NULL; break; | 
|---|
| 248 | } | 
|---|
| 249 | if (cond_mask_sym != NULL) | 
|---|
| 250 | (*info->fprintf_func) (info->stream, "%s", cond_mask_sym); | 
|---|
| 251 | else | 
|---|
| 252 | (*info->fprintf_func) (info->stream, "%x", extracted_field); | 
|---|
| 253 | break; | 
|---|
| 254 |  | 
|---|
| 255 | case PCREL: | 
|---|
| 256 | (*info->print_address_func) | 
|---|
| 257 | (pc + (4 * (SEXT (inst, opptr->offset, opptr->width))), | 
|---|
| 258 | info); | 
|---|
| 259 | break; | 
|---|
| 260 |  | 
|---|
| 261 | case CONT: | 
|---|
| 262 | (*info->fprintf_func) (info->stream, "%d,r%d", | 
|---|
| 263 | UEXT (inst, opptr->offset, 5), | 
|---|
| 264 | UEXT (inst, (opptr->offset) + 5, 5)); | 
|---|
| 265 | break; | 
|---|
| 266 |  | 
|---|
| 267 | case BF: | 
|---|
| 268 | (*info->fprintf_func) (info->stream, "%d<%d>", | 
|---|
| 269 | UEXT (inst, (opptr->offset) + 5, 5), | 
|---|
| 270 | UEXT (inst, opptr->offset, 5)); | 
|---|
| 271 | break; | 
|---|
| 272 |  | 
|---|
| 273 | default: | 
|---|
| 274 | /* xgettext:c-format */ | 
|---|
| 275 | (*info->fprintf_func) (info->stream, _("# <dis error: %08x>"), inst); | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | /* | 
|---|
| 280 | *                 Initialize the Disassembler Instruction Table | 
|---|
| 281 | * | 
|---|
| 282 | *       Initialize the hash table and instruction table for the disassembler. | 
|---|
| 283 | *       This should be called once before the first call to disasm(). | 
|---|
| 284 | * | 
|---|
| 285 | *                       Parameters | 
|---|
| 286 | * | 
|---|
| 287 | *                       Output | 
|---|
| 288 | * | 
|---|
| 289 | *       If the debug option is selected, certain statistics about the hashing | 
|---|
| 290 | *       distribution are written to stdout. | 
|---|
| 291 | * | 
|---|
| 292 | *                       Revision History | 
|---|
| 293 | * | 
|---|
| 294 | *       Revision 1.0    11/08/85        Creation date | 
|---|
| 295 | */ | 
|---|
| 296 |  | 
|---|
| 297 | static void | 
|---|
| 298 | init_disasm () | 
|---|
| 299 | { | 
|---|
| 300 | int i, size; | 
|---|
| 301 |  | 
|---|
| 302 | for (i = 0; i < HASHVAL; i++) | 
|---|
| 303 | hashtable[i] = NULL; | 
|---|
| 304 |  | 
|---|
| 305 | size = sizeof (instructions) / sizeof (INSTAB); | 
|---|
| 306 | for (i = 0; i < size; i++) | 
|---|
| 307 | install (&instructions[i]); | 
|---|
| 308 | } | 
|---|
| 309 |  | 
|---|
| 310 | /* | 
|---|
| 311 | *       Insert an instruction into the disassembler table by hashing the | 
|---|
| 312 | *       opcode and inserting it into the linked list for that hash value. | 
|---|
| 313 | * | 
|---|
| 314 | *                       Parameters | 
|---|
| 315 | * | 
|---|
| 316 | *       INSTAB *instptr         Pointer to the entry in the instruction table | 
|---|
| 317 | *                               to be installed | 
|---|
| 318 | * | 
|---|
| 319 | *       Revision 1.0    11/08/85        Creation date | 
|---|
| 320 | *                       05/11/89        R. TRAWICK ADAPTED FROM MOTOROLA | 
|---|
| 321 | */ | 
|---|
| 322 |  | 
|---|
| 323 | static void | 
|---|
| 324 | install (instptr) | 
|---|
| 325 | INSTAB *instptr; | 
|---|
| 326 | { | 
|---|
| 327 | unsigned int i; | 
|---|
| 328 |  | 
|---|
| 329 | i = (instptr->opcode) % HASHVAL; | 
|---|
| 330 | instptr->next = hashtable[i]; | 
|---|
| 331 | hashtable[i] = instptr; | 
|---|
| 332 | } | 
|---|