1 /* mmix-dis.c -- Disassemble MMIX instructions.
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3 Written by Hans-Peter Nilsson (hp@bitrange.com)
5 This file is part of GDB and the GNU binutils.
7 GDB and the GNU binutils are free software; you can redistribute
8 them and/or modify them under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
12 GDB and the GNU binutils are distributed in the hope that they
13 will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 the GNU General Public 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "opcode/mmix.h"
26 #include "libiberty.h"
34 _("Bad case %d (%s) in %s:%d\n"), \
35 x, #x, __FILE__, __LINE__); \
44 _("Internal: Non-debugged code (test-case missing): %s:%d"), \
45 __FILE__, __LINE__); \
50 #define ROUND_MODE(n) \
51 ((n) == 1 ? "ROUND_OFF" : (n) == 2 ? "ROUND_UP" : \
52 (n) == 3 ? "ROUND_DOWN" : (n) == 4 ? "ROUND_NEAR" : \
55 #define INSN_IMMEDIATE_BIT (IMM_OFFSET_BIT << 24)
56 #define INSN_BACKWARD_OFFSET_BIT (1 << 24)
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 static boolean initialize_mmix_dis_info
PARAMS ((struct disassemble_info
*));
70 static const struct mmix_opcode
*get_opcode
PARAMS ((unsigned long));
73 /* Initialize a target-specific array in INFO. */
76 initialize_mmix_dis_info (info
)
77 struct disassemble_info
*info
;
79 struct mmix_dis_info
*minfop
= malloc (sizeof (struct mmix_dis_info
));
85 memset (minfop
, 0, sizeof (*minfop
));
87 /* Initialize register names from register symbols. If there's no
88 register section, then there are no register symbols. */
89 if ((info
->section
!= NULL
&& info
->section
->owner
!= NULL
)
90 || (info
->symbols
!= NULL
91 && info
->symbols
[0] != NULL
92 && bfd_asymbol_bfd (info
->symbols
[0]) != NULL
))
94 bfd
*abfd
= info
->section
&& info
->section
->owner
!= NULL
95 ? info
->section
->owner
96 : bfd_asymbol_bfd (info
->symbols
[0]);
97 asection
*reg_section
= bfd_get_section_by_name (abfd
, "*REG*");
99 if (reg_section
!= NULL
)
101 /* The returned symcount *does* include the ending NULL. */
102 long symsize
= bfd_get_symtab_upper_bound (abfd
);
103 asymbol
**syms
= malloc (symsize
);
112 nsyms
= bfd_canonicalize_symtab (abfd
, syms
);
114 /* We use the first name for a register. If this is MMO, then
115 it's the name with the first sequence number, presumably the
116 first in the source. */
117 for (i
= 0; i
< nsyms
&& syms
[i
] != NULL
; i
++)
119 if (syms
[i
]->section
== reg_section
120 && syms
[i
]->value
< 256
121 && minfop
->reg_name
[syms
[i
]->value
] == NULL
)
122 minfop
->reg_name
[syms
[i
]->value
] = syms
[i
]->name
;
127 /* Fill in the rest with the canonical names. */
128 for (i
= 0; i
< 256; i
++)
129 if (minfop
->reg_name
[i
] == NULL
)
131 sprintf (minfop
->basic_reg_name
[i
], "$%d", i
);
132 minfop
->reg_name
[i
] = minfop
->basic_reg_name
[i
];
135 /* We assume it's actually a one-to-one mapping of number-to-name. */
136 for (i
= 0; mmix_spec_regs
[i
].name
!= NULL
; i
++)
137 minfop
->spec_reg_name
[mmix_spec_regs
[i
].number
] = mmix_spec_regs
[i
].name
;
139 info
->private_data
= (PTR
) minfop
;
143 /* A table indexed by the first byte is constructed as we disassemble each
144 tetrabyte. The contents is a pointer into mmix_insns reflecting the
145 first found entry with matching match-bits and lose-bits. Further
146 entries are considered one after one until the operand constraints
147 match or the match-bits and lose-bits do not match. Normally a
148 "further entry" will just show that there was no other match. */
150 static const struct mmix_opcode
*
154 static const struct mmix_opcode
**opcodes
= NULL
;
155 const struct mmix_opcode
*opcodep
= mmix_opcodes
;
156 unsigned int opcode_part
= (insn
>> 24) & 255;
158 opcodes
= xcalloc (256, sizeof (struct mmix_opcode
*));
160 opcodep
= opcodes
[opcode_part
];
162 || (opcodep
->match
& insn
) != opcodep
->match
163 || (opcodep
->lose
& insn
) != 0)
165 /* Search through the table. */
166 for (opcodep
= mmix_opcodes
; opcodep
->name
!= NULL
; opcodep
++)
168 /* FIXME: Break out this into an initialization function. */
169 if ((opcodep
->match
& (opcode_part
<< 24)) == opcode_part
170 && (opcodep
->lose
& (opcode_part
<< 24)) == 0)
171 opcodes
[opcode_part
] = opcodep
;
173 if ((opcodep
->match
& insn
) == opcodep
->match
174 && (opcodep
->lose
& insn
) == 0)
179 if (opcodep
->name
== NULL
)
182 /* Check constraints. If they don't match, loop through the next opcode
186 switch (opcodep
->operands
)
188 /* These have no restraint on what can be in the lower three
190 case mmix_operands_regs
:
191 case mmix_operands_reg_yz
:
192 case mmix_operands_regs_z_opt
:
193 case mmix_operands_regs_z
:
194 case mmix_operands_jmp
:
195 case mmix_operands_pushgo
:
196 case mmix_operands_pop
:
197 case mmix_operands_sync
:
198 case mmix_operands_x_regs_z
:
199 case mmix_operands_neg
:
200 case mmix_operands_pushj
:
201 case mmix_operands_regaddr
:
202 case mmix_operands_get
:
203 case mmix_operands_set
:
204 case mmix_operands_save
:
205 case mmix_operands_unsave
:
206 case mmix_operands_xyz_opt
:
209 /* For a ROUND_MODE, the middle byte must be 0..4. */
210 case mmix_operands_roundregs_z
:
211 case mmix_operands_roundregs
:
213 int midbyte
= (insn
>> 8) & 255;
219 case mmix_operands_put
:
220 /* A "PUT". If it is "immediate", then no restrictions,
221 otherwise we have to make sure the register number is < 32. */
222 if ((insn
& INSN_IMMEDIATE_BIT
)
223 || ((insn
>> 16) & 255) < 32)
227 case mmix_operands_resume
:
228 /* Middle bytes must be zero. */
229 if ((insn
& 0x00ffff00) == 0)
234 BAD_CASE (opcodep
->operands
);
239 while ((opcodep
->match
& insn
) == opcodep
->match
240 && (opcodep
->lose
& insn
) == 0);
242 /* If we got here, we had no match. */
246 /* The main disassembly function. */
249 print_insn_mmix (memaddr
, info
)
251 struct disassemble_info
*info
;
253 unsigned char buffer
[4];
255 unsigned int x
, y
, z
;
256 const struct mmix_opcode
*opcodep
;
257 int status
= (*info
->read_memory_func
) (memaddr
, buffer
, 4, info
);
258 struct mmix_dis_info
*minfop
;
262 (*info
->memory_error_func
) (status
, memaddr
, info
);
266 /* FIXME: Is -1 suitable? */
267 if (info
->private_data
== NULL
268 && ! initialize_mmix_dis_info (info
))
271 minfop
= (struct mmix_dis_info
*) info
->private_data
;
276 insn
= bfd_getb32 (buffer
);
278 opcodep
= get_opcode (insn
);
282 (*info
->fprintf_func
) (info
->stream
, _("*unknown*"));
286 (*info
->fprintf_func
) (info
->stream
, "%s ", opcodep
->name
);
288 /* Present bytes in the order they are laid out in memory. */
289 info
->display_endian
= BFD_ENDIAN_BIG
;
291 info
->insn_info_valid
= 1;
292 info
->bytes_per_chunk
= 4;
293 info
->branch_delay_insns
= 0;
295 switch (opcodep
->type
)
297 case mmix_type_normal
:
298 case mmix_type_memaccess_block
:
299 info
->insn_type
= dis_nonbranch
;
302 case mmix_type_branch
:
303 info
->insn_type
= dis_branch
;
306 case mmix_type_condbranch
:
307 info
->insn_type
= dis_condbranch
;
310 case mmix_type_memaccess_octa
:
311 info
->insn_type
= dis_dref
;
315 case mmix_type_memaccess_tetra
:
316 info
->insn_type
= dis_dref
;
320 case mmix_type_memaccess_wyde
:
321 info
->insn_type
= dis_dref
;
325 case mmix_type_memaccess_byte
:
326 info
->insn_type
= dis_dref
;
331 info
->insn_type
= dis_jsr
;
335 BAD_CASE(opcodep
->type
);
338 switch (opcodep
->operands
)
340 case mmix_operands_regs
:
341 /* All registers: "$X,$Y,$Z". */
342 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%s",
345 minfop
->reg_name
[z
]);
348 case mmix_operands_reg_yz
:
349 /* Like SETH - "$X,YZ". */
350 (*info
->fprintf_func
) (info
->stream
, "%s,0x%x",
351 minfop
->reg_name
[x
], y
* 256 + z
);
354 case mmix_operands_regs_z_opt
:
355 case mmix_operands_regs_z
:
356 case mmix_operands_pushgo
:
357 /* The regular "$X,$Y,$Z|Z". */
358 if (insn
& INSN_IMMEDIATE_BIT
)
359 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%d",
360 minfop
->reg_name
[x
], minfop
->reg_name
[y
], z
);
362 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%s",
365 minfop
->reg_name
[z
]);
368 case mmix_operands_jmp
:
369 /* Address; only JMP. */
371 bfd_signed_vma offset
= (x
* 65536 + y
* 256 + z
) * 4;
373 if (insn
& INSN_BACKWARD_OFFSET_BIT
)
374 offset
-= (256 * 65536) * 4;
376 info
->target
= memaddr
+ offset
;
377 (*info
->print_address_func
) (memaddr
+ offset
, info
);
381 case mmix_operands_roundregs_z
:
382 /* Two registers, like FLOT, possibly with rounding: "$X,$Z|Z"
383 "$X,ROUND_MODE,$Z|Z". */
386 if (insn
& INSN_IMMEDIATE_BIT
)
387 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%d",
391 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%s",
394 minfop
->reg_name
[z
]);
398 if (insn
& INSN_IMMEDIATE_BIT
)
399 (*info
->fprintf_func
) (info
->stream
, "%s,%d",
400 minfop
->reg_name
[x
], z
);
402 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
404 minfop
->reg_name
[z
]);
408 case mmix_operands_pop
:
409 /* Like POP - "X,YZ". */
410 (*info
->fprintf_func
) (info
->stream
, "%d,%d", x
, y
*256 + z
);
413 case mmix_operands_roundregs
:
414 /* Two registers, possibly with rounding: "$X,$Z" or
415 "$X,ROUND_MODE,$Z". */
417 (*info
->fprintf_func
) (info
->stream
, "%s,%s,%s",
420 minfop
->reg_name
[z
]);
422 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
424 minfop
->reg_name
[z
]);
427 case mmix_operands_sync
:
428 /* Like SYNC - "XYZ". */
429 (*info
->fprintf_func
) (info
->stream
, "%u",
430 x
* 65536 + y
* 256 + z
);
433 case mmix_operands_x_regs_z
:
434 /* Like SYNCD - "X,$Y,$Z|Z". */
435 if (insn
& INSN_IMMEDIATE_BIT
)
436 (*info
->fprintf_func
) (info
->stream
, "%d,%s,%d",
437 x
, minfop
->reg_name
[y
], z
);
439 (*info
->fprintf_func
) (info
->stream
, "%d,%s,%s",
440 x
, minfop
->reg_name
[y
],
441 minfop
->reg_name
[z
]);
444 case mmix_operands_neg
:
445 /* Like NEG and NEGU - "$X,Y,$Z|Z". */
446 if (insn
& INSN_IMMEDIATE_BIT
)
447 (*info
->fprintf_func
) (info
->stream
, "%s,%d,%d",
448 minfop
->reg_name
[x
], y
, z
);
450 (*info
->fprintf_func
) (info
->stream
, "%s,%d,%s",
451 minfop
->reg_name
[x
], y
,
452 minfop
->reg_name
[z
]);
455 case mmix_operands_pushj
:
456 case mmix_operands_regaddr
:
457 /* Like GETA or branches - "$X,Address". */
459 bfd_signed_vma offset
= (y
* 256 + z
) * 4;
461 if (insn
& INSN_BACKWARD_OFFSET_BIT
)
464 info
->target
= memaddr
+ offset
;
466 (*info
->fprintf_func
) (info
->stream
, "%s,", minfop
->reg_name
[x
]);
467 (*info
->print_address_func
) (memaddr
+ offset
, info
);
471 case mmix_operands_get
:
472 /* GET - "X,spec_reg". */
473 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
475 minfop
->spec_reg_name
[z
]);
478 case mmix_operands_put
:
479 /* PUT - "spec_reg,$Z|Z". */
480 if (insn
& INSN_IMMEDIATE_BIT
)
481 (*info
->fprintf_func
) (info
->stream
, "%s,%d",
482 minfop
->spec_reg_name
[x
], z
);
484 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
485 minfop
->spec_reg_name
[x
],
486 minfop
->reg_name
[z
]);
489 case mmix_operands_set
:
490 /* Two registers, "$X,$Y". */
491 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
493 minfop
->reg_name
[y
]);
496 case mmix_operands_save
:
498 (*info
->fprintf_func
) (info
->stream
, "%s,0", minfop
->reg_name
[x
]);
501 case mmix_operands_unsave
:
502 /* UNSAVE - "0,$Z". */
503 (*info
->fprintf_func
) (info
->stream
, "0,%s", minfop
->reg_name
[z
]);
506 case mmix_operands_xyz_opt
:
507 /* Like SWYM or TRAP - "X,Y,Z". */
508 (*info
->fprintf_func
) (info
->stream
, "%d,%d,%d", x
, y
, z
);
511 case mmix_operands_resume
:
512 /* Just "Z", like RESUME. */
513 (*info
->fprintf_func
) (info
->stream
, "%d", z
);
517 (*info
->fprintf_func
) (info
->stream
, _("*unknown operands type: %d*"),