Automatic date update in version.in
[binutils-gdb.git] / opcodes / mmix-dis.c
blobbfd0d02d5ef98b235de8bbbb2548632828643cfd
1 /* mmix-dis.c -- Disassemble MMIX instructions.
2 Copyright (C) 2000-2017 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)
10 any later version.
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. */
22 #include "sysdep.h"
23 #include <stdio.h>
24 #include "opcode/mmix.h"
25 #include "disassemble.h"
26 #include "libiberty.h"
27 #include "bfd.h"
28 #include "opintl.h"
30 #define BAD_CASE(x) \
31 do \
32 { \
33 fprintf (stderr, \
34 _("Bad case %d (%s) in %s:%d\n"), \
35 x, #x, __FILE__, __LINE__); \
36 abort (); \
37 } \
38 while (0)
40 #define FATAL_DEBUG \
41 do \
42 { \
43 fprintf (stderr, \
44 _("Internal: Non-debugged code (test-case missing): %s:%d"),\
45 __FILE__, __LINE__); \
46 abort (); \
47 } \
48 while (0)
50 #define ROUND_MODE(n) \
51 ((n) == 1 ? "ROUND_OFF" : (n) == 2 ? "ROUND_UP" : \
52 (n) == 3 ? "ROUND_DOWN" : (n) == 4 ? "ROUND_NEAR" : \
53 _("(unknown)"))
55 #define INSN_IMMEDIATE_BIT (IMM_OFFSET_BIT << 24)
56 #define INSN_BACKWARD_OFFSET_BIT (1 << 24)
58 struct mmix_dis_info
60 const char *reg_name[256];
61 const char *spec_reg_name[32];
63 /* Waste a little memory so we don't have to allocate each separately.
64 We could have an array with static contents for these, but on the
65 other hand, we don't have to. */
66 char basic_reg_name[256][sizeof ("$255")];
69 /* Initialize a target-specific array in INFO. */
71 static bfd_boolean
72 initialize_mmix_dis_info (struct disassemble_info *info)
74 struct mmix_dis_info *minfop = malloc (sizeof (struct mmix_dis_info));
75 long i;
77 if (minfop == NULL)
78 return FALSE;
80 memset (minfop, 0, sizeof (*minfop));
82 /* Initialize register names from register symbols. If there's no
83 register section, then there are no register symbols. */
84 if ((info->section != NULL && info->section->owner != NULL)
85 || (info->symbols != NULL
86 && info->symbols[0] != NULL
87 && bfd_asymbol_bfd (info->symbols[0]) != NULL))
89 bfd *abfd = info->section && info->section->owner != NULL
90 ? info->section->owner
91 : bfd_asymbol_bfd (info->symbols[0]);
92 asection *reg_section = bfd_get_section_by_name (abfd, "*REG*");
94 if (reg_section != NULL)
96 /* The returned symcount *does* include the ending NULL. */
97 long symsize = bfd_get_symtab_upper_bound (abfd);
98 asymbol **syms = malloc (symsize);
99 long nsyms;
101 if (syms == NULL)
103 FATAL_DEBUG;
104 free (minfop);
105 return FALSE;
107 nsyms = bfd_canonicalize_symtab (abfd, syms);
109 /* We use the first name for a register. If this is MMO, then
110 it's the name with the first sequence number, presumably the
111 first in the source. */
112 for (i = 0; i < nsyms && syms[i] != NULL; i++)
114 if (syms[i]->section == reg_section
115 && syms[i]->value < 256
116 && minfop->reg_name[syms[i]->value] == NULL)
117 minfop->reg_name[syms[i]->value] = syms[i]->name;
122 /* Fill in the rest with the canonical names. */
123 for (i = 0; i < 256; i++)
124 if (minfop->reg_name[i] == NULL)
126 sprintf (minfop->basic_reg_name[i], "$%ld", i);
127 minfop->reg_name[i] = minfop->basic_reg_name[i];
130 /* We assume it's actually a one-to-one mapping of number-to-name. */
131 for (i = 0; mmix_spec_regs[i].name != NULL; i++)
132 minfop->spec_reg_name[mmix_spec_regs[i].number] = mmix_spec_regs[i].name;
134 info->private_data = (void *) minfop;
135 return TRUE;
138 /* A table indexed by the first byte is constructed as we disassemble each
139 tetrabyte. The contents is a pointer into mmix_insns reflecting the
140 first found entry with matching match-bits and lose-bits. Further
141 entries are considered one after one until the operand constraints
142 match or the match-bits and lose-bits do not match. Normally a
143 "further entry" will just show that there was no other match. */
145 static const struct mmix_opcode *
146 get_opcode (unsigned long insn)
148 static const struct mmix_opcode **opcodes = NULL;
149 const struct mmix_opcode *opcodep = mmix_opcodes;
150 unsigned int opcode_part = (insn >> 24) & 255;
152 if (opcodes == NULL)
153 opcodes = xcalloc (256, sizeof (struct mmix_opcode *));
155 opcodep = opcodes[opcode_part];
156 if (opcodep == NULL
157 || (opcodep->match & insn) != opcodep->match
158 || (opcodep->lose & insn) != 0)
160 /* Search through the table. */
161 for (opcodep = mmix_opcodes; opcodep->name != NULL; opcodep++)
163 /* FIXME: Break out this into an initialization function. */
164 if ((opcodep->match & (opcode_part << 24)) == opcode_part
165 && (opcodep->lose & (opcode_part << 24)) == 0)
166 opcodes[opcode_part] = opcodep;
168 if ((opcodep->match & insn) == opcodep->match
169 && (opcodep->lose & insn) == 0)
170 break;
174 if (opcodep->name == NULL)
175 return NULL;
177 /* Check constraints. If they don't match, loop through the next opcode
178 entries. */
181 switch (opcodep->operands)
183 /* These have no restraint on what can be in the lower three
184 bytes. */
185 case mmix_operands_regs:
186 case mmix_operands_reg_yz:
187 case mmix_operands_regs_z_opt:
188 case mmix_operands_regs_z:
189 case mmix_operands_jmp:
190 case mmix_operands_pushgo:
191 case mmix_operands_pop:
192 case mmix_operands_sync:
193 case mmix_operands_x_regs_z:
194 case mmix_operands_neg:
195 case mmix_operands_pushj:
196 case mmix_operands_regaddr:
197 case mmix_operands_get:
198 case mmix_operands_set:
199 case mmix_operands_save:
200 case mmix_operands_unsave:
201 case mmix_operands_xyz_opt:
202 return opcodep;
204 /* For a ROUND_MODE, the middle byte must be 0..4. */
205 case mmix_operands_roundregs_z:
206 case mmix_operands_roundregs:
208 int midbyte = (insn >> 8) & 255;
210 if (midbyte <= 4)
211 return opcodep;
213 break;
215 case mmix_operands_put:
216 /* A "PUT". If it is "immediate", then no restrictions,
217 otherwise we have to make sure the register number is < 32. */
218 if ((insn & INSN_IMMEDIATE_BIT)
219 || ((insn >> 16) & 255) < 32)
220 return opcodep;
221 break;
223 case mmix_operands_resume:
224 /* Middle bytes must be zero. */
225 if ((insn & 0x00ffff00) == 0)
226 return opcodep;
227 break;
229 default:
230 BAD_CASE (opcodep->operands);
233 opcodep++;
235 while ((opcodep->match & insn) == opcodep->match
236 && (opcodep->lose & insn) == 0);
238 /* If we got here, we had no match. */
239 return NULL;
242 /* The main disassembly function. */
245 print_insn_mmix (bfd_vma memaddr, struct disassemble_info *info)
247 unsigned char buffer[4];
248 unsigned long insn;
249 unsigned int x, y, z;
250 const struct mmix_opcode *opcodep;
251 int status = (*info->read_memory_func) (memaddr, buffer, 4, info);
252 struct mmix_dis_info *minfop;
254 if (status != 0)
256 (*info->memory_error_func) (status, memaddr, info);
257 return -1;
260 /* FIXME: Is -1 suitable? */
261 if (info->private_data == NULL
262 && ! initialize_mmix_dis_info (info))
263 return -1;
265 minfop = (struct mmix_dis_info *) info->private_data;
266 x = buffer[1];
267 y = buffer[2];
268 z = buffer[3];
270 insn = bfd_getb32 (buffer);
272 opcodep = get_opcode (insn);
274 if (opcodep == NULL)
276 (*info->fprintf_func) (info->stream, _("*unknown*"));
277 return 4;
280 (*info->fprintf_func) (info->stream, "%s ", opcodep->name);
282 /* Present bytes in the order they are laid out in memory. */
283 info->display_endian = BFD_ENDIAN_BIG;
285 info->insn_info_valid = 1;
286 info->bytes_per_chunk = 4;
287 info->branch_delay_insns = 0;
288 info->target = 0;
289 switch (opcodep->type)
291 case mmix_type_normal:
292 case mmix_type_memaccess_block:
293 info->insn_type = dis_nonbranch;
294 break;
296 case mmix_type_branch:
297 info->insn_type = dis_branch;
298 break;
300 case mmix_type_condbranch:
301 info->insn_type = dis_condbranch;
302 break;
304 case mmix_type_memaccess_octa:
305 info->insn_type = dis_dref;
306 info->data_size = 8;
307 break;
309 case mmix_type_memaccess_tetra:
310 info->insn_type = dis_dref;
311 info->data_size = 4;
312 break;
314 case mmix_type_memaccess_wyde:
315 info->insn_type = dis_dref;
316 info->data_size = 2;
317 break;
319 case mmix_type_memaccess_byte:
320 info->insn_type = dis_dref;
321 info->data_size = 1;
322 break;
324 case mmix_type_jsr:
325 info->insn_type = dis_jsr;
326 break;
328 default:
329 BAD_CASE(opcodep->type);
332 switch (opcodep->operands)
334 case mmix_operands_regs:
335 /* All registers: "$X,$Y,$Z". */
336 (*info->fprintf_func) (info->stream, "%s,%s,%s",
337 minfop->reg_name[x],
338 minfop->reg_name[y],
339 minfop->reg_name[z]);
340 break;
342 case mmix_operands_reg_yz:
343 /* Like SETH - "$X,YZ". */
344 (*info->fprintf_func) (info->stream, "%s,0x%x",
345 minfop->reg_name[x], y * 256 + z);
346 break;
348 case mmix_operands_regs_z_opt:
349 case mmix_operands_regs_z:
350 case mmix_operands_pushgo:
351 /* The regular "$X,$Y,$Z|Z". */
352 if (insn & INSN_IMMEDIATE_BIT)
353 (*info->fprintf_func) (info->stream, "%s,%s,%d",
354 minfop->reg_name[x], minfop->reg_name[y], z);
355 else
356 (*info->fprintf_func) (info->stream, "%s,%s,%s",
357 minfop->reg_name[x],
358 minfop->reg_name[y],
359 minfop->reg_name[z]);
360 break;
362 case mmix_operands_jmp:
363 /* Address; only JMP. */
365 bfd_signed_vma offset = (x * 65536 + y * 256 + z) * 4;
367 if (insn & INSN_BACKWARD_OFFSET_BIT)
368 offset -= (256 * 65536) * 4;
370 info->target = memaddr + offset;
371 (*info->print_address_func) (memaddr + offset, info);
373 break;
375 case mmix_operands_roundregs_z:
376 /* Two registers, like FLOT, possibly with rounding: "$X,$Z|Z"
377 "$X,ROUND_MODE,$Z|Z". */
378 if (y != 0)
380 if (insn & INSN_IMMEDIATE_BIT)
381 (*info->fprintf_func) (info->stream, "%s,%s,%d",
382 minfop->reg_name[x],
383 ROUND_MODE (y), z);
384 else
385 (*info->fprintf_func) (info->stream, "%s,%s,%s",
386 minfop->reg_name[x],
387 ROUND_MODE (y),
388 minfop->reg_name[z]);
390 else
392 if (insn & INSN_IMMEDIATE_BIT)
393 (*info->fprintf_func) (info->stream, "%s,%d",
394 minfop->reg_name[x], z);
395 else
396 (*info->fprintf_func) (info->stream, "%s,%s",
397 minfop->reg_name[x],
398 minfop->reg_name[z]);
400 break;
402 case mmix_operands_pop:
403 /* Like POP - "X,YZ". */
404 (*info->fprintf_func) (info->stream, "%d,%d", x, y*256 + z);
405 break;
407 case mmix_operands_roundregs:
408 /* Two registers, possibly with rounding: "$X,$Z" or
409 "$X,ROUND_MODE,$Z". */
410 if (y != 0)
411 (*info->fprintf_func) (info->stream, "%s,%s,%s",
412 minfop->reg_name[x],
413 ROUND_MODE (y),
414 minfop->reg_name[z]);
415 else
416 (*info->fprintf_func) (info->stream, "%s,%s",
417 minfop->reg_name[x],
418 minfop->reg_name[z]);
419 break;
421 case mmix_operands_sync:
422 /* Like SYNC - "XYZ". */
423 (*info->fprintf_func) (info->stream, "%u",
424 x * 65536 + y * 256 + z);
425 break;
427 case mmix_operands_x_regs_z:
428 /* Like SYNCD - "X,$Y,$Z|Z". */
429 if (insn & INSN_IMMEDIATE_BIT)
430 (*info->fprintf_func) (info->stream, "%d,%s,%d",
431 x, minfop->reg_name[y], z);
432 else
433 (*info->fprintf_func) (info->stream, "%d,%s,%s",
434 x, minfop->reg_name[y],
435 minfop->reg_name[z]);
436 break;
438 case mmix_operands_neg:
439 /* Like NEG and NEGU - "$X,Y,$Z|Z". */
440 if (insn & INSN_IMMEDIATE_BIT)
441 (*info->fprintf_func) (info->stream, "%s,%d,%d",
442 minfop->reg_name[x], y, z);
443 else
444 (*info->fprintf_func) (info->stream, "%s,%d,%s",
445 minfop->reg_name[x], y,
446 minfop->reg_name[z]);
447 break;
449 case mmix_operands_pushj:
450 case mmix_operands_regaddr:
451 /* Like GETA or branches - "$X,Address". */
453 bfd_signed_vma offset = (y * 256 + z) * 4;
455 if (insn & INSN_BACKWARD_OFFSET_BIT)
456 offset -= 65536 * 4;
458 info->target = memaddr + offset;
460 (*info->fprintf_func) (info->stream, "%s,", minfop->reg_name[x]);
461 (*info->print_address_func) (memaddr + offset, info);
463 break;
465 case mmix_operands_get:
466 /* GET - "X,spec_reg". */
467 (*info->fprintf_func) (info->stream, "%s,%s",
468 minfop->reg_name[x],
469 minfop->spec_reg_name[z]);
470 break;
472 case mmix_operands_put:
473 /* PUT - "spec_reg,$Z|Z". */
474 if (insn & INSN_IMMEDIATE_BIT)
475 (*info->fprintf_func) (info->stream, "%s,%d",
476 minfop->spec_reg_name[x], z);
477 else
478 (*info->fprintf_func) (info->stream, "%s,%s",
479 minfop->spec_reg_name[x],
480 minfop->reg_name[z]);
481 break;
483 case mmix_operands_set:
484 /* Two registers, "$X,$Y". */
485 (*info->fprintf_func) (info->stream, "%s,%s",
486 minfop->reg_name[x],
487 minfop->reg_name[y]);
488 break;
490 case mmix_operands_save:
491 /* SAVE - "$X,0". */
492 (*info->fprintf_func) (info->stream, "%s,0", minfop->reg_name[x]);
493 break;
495 case mmix_operands_unsave:
496 /* UNSAVE - "0,$Z". */
497 (*info->fprintf_func) (info->stream, "0,%s", minfop->reg_name[z]);
498 break;
500 case mmix_operands_xyz_opt:
501 /* Like SWYM or TRAP - "X,Y,Z". */
502 (*info->fprintf_func) (info->stream, "%d,%d,%d", x, y, z);
503 break;
505 case mmix_operands_resume:
506 /* Just "Z", like RESUME. */
507 (*info->fprintf_func) (info->stream, "%d", z);
508 break;
510 default:
511 (*info->fprintf_func) (info->stream, _("*unknown operands type: %d*"),
512 opcodep->operands);
513 break;
516 return 4;