1 /* mmix-dis.c -- Disassemble MMIX instructions.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Hans-Peter Nilsson (hp@bitrange.com)
5 This file is part of the GNU opcodes library.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 #include "opcode/mmix.h"
25 #include "disassemble.h"
26 #include "libiberty.h"
33 opcodes_error_handler (_("bad case %d (%s) in %s:%d"), \
34 x, #x, __FILE__, __LINE__); \
42 opcodes_error_handler (_("internal: non-debugged code " \
43 "(test-case missing): %s:%d"), \
44 __FILE__, __LINE__); \
49 #define ROUND_MODE(n) \
50 ((n) == 1 ? "ROUND_OFF" : (n) == 2 ? "ROUND_UP" : \
51 (n) == 3 ? "ROUND_DOWN" : (n) == 4 ? "ROUND_NEAR" : \
54 #define INSN_IMMEDIATE_BIT (IMM_OFFSET_BIT << 24)
55 #define INSN_BACKWARD_OFFSET_BIT (1 << 24)
59 const char *reg_name[256];
60 const char *spec_reg_name[32];
62 /* Waste a little memory so we don't have to allocate each separately.
63 We could have an array with static contents for these, but on the
64 other hand, we don't have to. */
65 char basic_reg_name[256][sizeof ("$255")];
68 /* Initialize a target-specific array in INFO. */
71 initialize_mmix_dis_info (struct disassemble_info *info)
73 struct mmix_dis_info *minfop = malloc (sizeof (struct mmix_dis_info));
79 memset (minfop, 0, sizeof (*minfop));
81 /* Initialize register names from register symbols. If there's no
82 register section, then there are no register symbols. */
83 if ((info->section != NULL && info->section->owner != NULL)
84 || (info->symbols != NULL
85 && info->symbols[0] != NULL
86 && bfd_asymbol_bfd (info->symbols[0]) != NULL))
88 bfd *abfd = info->section && info->section->owner != NULL
89 ? info->section->owner
90 : bfd_asymbol_bfd (info->symbols[0]);
91 asection *reg_section = bfd_get_section_by_name (abfd, "*REG*");
93 if (reg_section != NULL)
95 /* The returned symcount *does* include the ending NULL. */
96 long symsize = bfd_get_symtab_upper_bound (abfd);
97 asymbol **syms = malloc (symsize);
106 nsyms = bfd_canonicalize_symtab (abfd, syms);
108 /* We use the first name for a register. If this is MMO, then
109 it's the name with the first sequence number, presumably the
110 first in the source. */
111 for (i = 0; i < nsyms && syms[i] != NULL; i++)
113 if (syms[i]->section == reg_section
114 && syms[i]->value < 256
115 && minfop->reg_name[syms[i]->value] == NULL)
116 minfop->reg_name[syms[i]->value] = syms[i]->name;
121 /* Fill in the rest with the canonical names. */
122 for (i = 0; i < 256; i++)
123 if (minfop->reg_name[i] == NULL)
125 sprintf (minfop->basic_reg_name[i], "$%ld", i);
126 minfop->reg_name[i] = minfop->basic_reg_name[i];
129 /* We assume it's actually a one-to-one mapping of number-to-name. */
130 for (i = 0; mmix_spec_regs[i].name != NULL; i++)
131 minfop->spec_reg_name[mmix_spec_regs[i].number] = mmix_spec_regs[i].name;
133 info->private_data = (void *) minfop;
137 /* A table indexed by the first byte is constructed as we disassemble each
138 tetrabyte. The contents is a pointer into mmix_insns reflecting the
139 first found entry with matching match-bits and lose-bits. Further
140 entries are considered one after one until the operand constraints
141 match or the match-bits and lose-bits do not match. Normally a
142 "further entry" will just show that there was no other match. */
144 static const struct mmix_opcode *
145 get_opcode (unsigned long insn)
147 static const struct mmix_opcode **opcodes = NULL;
148 const struct mmix_opcode *opcodep = mmix_opcodes;
149 unsigned int opcode_part = (insn >> 24) & 255;
152 opcodes = xcalloc (256, sizeof (struct mmix_opcode *));
154 opcodep = opcodes[opcode_part];
156 || (opcodep->match & insn) != opcodep->match
157 || (opcodep->lose & insn) != 0)
159 /* Search through the table. */
160 for (opcodep = mmix_opcodes; opcodep->name != NULL; opcodep++)
162 /* FIXME: Break out this into an initialization function. */
163 if ((opcodep->match & (opcode_part << 24)) == opcode_part
164 && (opcodep->lose & (opcode_part << 24)) == 0)
165 opcodes[opcode_part] = opcodep;
167 if ((opcodep->match & insn) == opcodep->match
168 && (opcodep->lose & insn) == 0)
173 if (opcodep->name == NULL)
176 /* Check constraints. If they don't match, loop through the next opcode
180 switch (opcodep->operands)
182 /* These have no restraint on what can be in the lower three
184 case mmix_operands_regs:
185 case mmix_operands_reg_yz:
186 case mmix_operands_regs_z_opt:
187 case mmix_operands_regs_z:
188 case mmix_operands_jmp:
189 case mmix_operands_pushgo:
190 case mmix_operands_pop:
191 case mmix_operands_sync:
192 case mmix_operands_x_regs_z:
193 case mmix_operands_neg:
194 case mmix_operands_pushj:
195 case mmix_operands_regaddr:
196 case mmix_operands_get:
197 case mmix_operands_set:
198 case mmix_operands_save:
199 case mmix_operands_unsave:
200 case mmix_operands_xyz_opt:
203 /* For a ROUND_MODE, the middle byte must be 0..4. */
204 case mmix_operands_roundregs_z:
205 case mmix_operands_roundregs:
207 int midbyte = (insn >> 8) & 255;
214 case mmix_operands_put:
215 /* A "PUT". If it is "immediate", then no restrictions,
216 otherwise we have to make sure the register number is < 32. */
217 if ((insn & INSN_IMMEDIATE_BIT)
218 || ((insn >> 16) & 255) < 32)
222 case mmix_operands_resume:
223 /* Middle bytes must be zero. */
224 if ((insn & 0x00ffff00) == 0)
229 BAD_CASE (opcodep->operands);
234 while ((opcodep->match & insn) == opcodep->match
235 && (opcodep->lose & insn) == 0);
237 /* If we got here, we had no match. */
241 /* The main disassembly function. */
244 print_insn_mmix (bfd_vma memaddr, struct disassemble_info *info)
246 unsigned char buffer[4];
248 unsigned int x, y, z;
249 const struct mmix_opcode *opcodep;
250 int status = (*info->read_memory_func) (memaddr, buffer, 4, info);
251 struct mmix_dis_info *minfop;
255 (*info->memory_error_func) (status, memaddr, info);
259 /* FIXME: Is -1 suitable? */
260 if (info->private_data == NULL
261 && ! initialize_mmix_dis_info (info))
264 minfop = (struct mmix_dis_info *) info->private_data;
269 insn = bfd_getb32 (buffer);
271 opcodep = get_opcode (insn);
275 (*info->fprintf_func) (info->stream, _("*unknown*"));
279 (*info->fprintf_func) (info->stream, "%s ", opcodep->name);
281 /* Present bytes in the order they are laid out in memory. */
282 info->display_endian = BFD_ENDIAN_BIG;
284 info->insn_info_valid = 1;
285 info->bytes_per_chunk = 4;
286 info->branch_delay_insns = 0;
288 switch (opcodep->type)
290 case mmix_type_normal:
291 case mmix_type_memaccess_block:
292 info->insn_type = dis_nonbranch;
295 case mmix_type_branch:
296 info->insn_type = dis_branch;
299 case mmix_type_condbranch:
300 info->insn_type = dis_condbranch;
303 case mmix_type_memaccess_octa:
304 info->insn_type = dis_dref;
308 case mmix_type_memaccess_tetra:
309 info->insn_type = dis_dref;
313 case mmix_type_memaccess_wyde:
314 info->insn_type = dis_dref;
318 case mmix_type_memaccess_byte:
319 info->insn_type = dis_dref;
324 info->insn_type = dis_jsr;
328 BAD_CASE(opcodep->type);
331 switch (opcodep->operands)
333 case mmix_operands_regs:
334 /* All registers: "$X,$Y,$Z". */
335 (*info->fprintf_func) (info->stream, "%s,%s,%s",
338 minfop->reg_name[z]);
341 case mmix_operands_reg_yz:
342 /* Like SETH - "$X,YZ". */
343 (*info->fprintf_func) (info->stream, "%s,0x%x",
344 minfop->reg_name[x], y * 256 + z);
347 case mmix_operands_regs_z_opt:
348 case mmix_operands_regs_z:
349 case mmix_operands_pushgo:
350 /* The regular "$X,$Y,$Z|Z". */
351 if (insn & INSN_IMMEDIATE_BIT)
352 (*info->fprintf_func) (info->stream, "%s,%s,%d",
353 minfop->reg_name[x], minfop->reg_name[y], z);
355 (*info->fprintf_func) (info->stream, "%s,%s,%s",
358 minfop->reg_name[z]);
361 case mmix_operands_jmp:
362 /* Address; only JMP. */
364 bfd_signed_vma offset = (x * 65536 + y * 256 + z) * 4;
366 if (insn & INSN_BACKWARD_OFFSET_BIT)
367 offset -= (256 * 65536) * 4;
369 info->target = memaddr + offset;
370 (*info->print_address_func) (memaddr + offset, info);
374 case mmix_operands_roundregs_z:
375 /* Two registers, like FLOT, possibly with rounding: "$X,$Z|Z"
376 "$X,ROUND_MODE,$Z|Z". */
379 if (insn & INSN_IMMEDIATE_BIT)
380 (*info->fprintf_func) (info->stream, "%s,%s,%d",
384 (*info->fprintf_func) (info->stream, "%s,%s,%s",
387 minfop->reg_name[z]);
391 if (insn & INSN_IMMEDIATE_BIT)
392 (*info->fprintf_func) (info->stream, "%s,%d",
393 minfop->reg_name[x], z);
395 (*info->fprintf_func) (info->stream, "%s,%s",
397 minfop->reg_name[z]);
401 case mmix_operands_pop:
402 /* Like POP - "X,YZ". */
403 (*info->fprintf_func) (info->stream, "%d,%d", x, y*256 + z);
406 case mmix_operands_roundregs:
407 /* Two registers, possibly with rounding: "$X,$Z" or
408 "$X,ROUND_MODE,$Z". */
410 (*info->fprintf_func) (info->stream, "%s,%s,%s",
413 minfop->reg_name[z]);
415 (*info->fprintf_func) (info->stream, "%s,%s",
417 minfop->reg_name[z]);
420 case mmix_operands_sync:
421 /* Like SYNC - "XYZ". */
422 (*info->fprintf_func) (info->stream, "%u",
423 x * 65536 + y * 256 + z);
426 case mmix_operands_x_regs_z:
427 /* Like SYNCD - "X,$Y,$Z|Z". */
428 if (insn & INSN_IMMEDIATE_BIT)
429 (*info->fprintf_func) (info->stream, "%d,%s,%d",
430 x, minfop->reg_name[y], z);
432 (*info->fprintf_func) (info->stream, "%d,%s,%s",
433 x, minfop->reg_name[y],
434 minfop->reg_name[z]);
437 case mmix_operands_neg:
438 /* Like NEG and NEGU - "$X,Y,$Z|Z". */
439 if (insn & INSN_IMMEDIATE_BIT)
440 (*info->fprintf_func) (info->stream, "%s,%d,%d",
441 minfop->reg_name[x], y, z);
443 (*info->fprintf_func) (info->stream, "%s,%d,%s",
444 minfop->reg_name[x], y,
445 minfop->reg_name[z]);
448 case mmix_operands_pushj:
449 case mmix_operands_regaddr:
450 /* Like GETA or branches - "$X,Address". */
452 bfd_signed_vma offset = (y * 256 + z) * 4;
454 if (insn & INSN_BACKWARD_OFFSET_BIT)
457 info->target = memaddr + offset;
459 (*info->fprintf_func) (info->stream, "%s,", minfop->reg_name[x]);
460 (*info->print_address_func) (memaddr + offset, info);
464 case mmix_operands_get:
465 /* GET - "X,spec_reg". */
466 (*info->fprintf_func) (info->stream, "%s,%s",
468 minfop->spec_reg_name[z]);
471 case mmix_operands_put:
472 /* PUT - "spec_reg,$Z|Z". */
473 if (insn & INSN_IMMEDIATE_BIT)
474 (*info->fprintf_func) (info->stream, "%s,%d",
475 minfop->spec_reg_name[x], z);
477 (*info->fprintf_func) (info->stream, "%s,%s",
478 minfop->spec_reg_name[x],
479 minfop->reg_name[z]);
482 case mmix_operands_set:
483 /* Two registers, "$X,$Y". */
484 (*info->fprintf_func) (info->stream, "%s,%s",
486 minfop->reg_name[y]);
489 case mmix_operands_save:
491 (*info->fprintf_func) (info->stream, "%s,0", minfop->reg_name[x]);
494 case mmix_operands_unsave:
495 /* UNSAVE - "0,$Z". */
496 (*info->fprintf_func) (info->stream, "0,%s", minfop->reg_name[z]);
499 case mmix_operands_xyz_opt:
500 /* Like SWYM or TRAP - "X,Y,Z". */
501 (*info->fprintf_func) (info->stream, "%d,%d,%d", x, y, z);
504 case mmix_operands_resume:
505 /* Just "Z", like RESUME. */
506 (*info->fprintf_func) (info->stream, "%d", z);
510 (*info->fprintf_func) (info->stream, _("*unknown operands type: %d*"),