include/elf:
[external/binutils.git] / opcodes / i370-dis.c
1 /* i370-dis.c -- Disassemble Instruction 370 (ESA/390) instructions
2    Copyright 1994, 2000, 2003 Free Software Foundation, Inc.
3    PowerPC version written by Ian Lance Taylor, Cygnus Support
4    Rewritten for i370 ESA/390 support by Linas Vepstas <linas@linas.org>
5
6 This file is part of GDB, GAS, and the GNU binutils.
7
8 GDB, GAS, and the GNU binutils are free software; you can redistribute
9 them and/or modify them under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either version
11 2, or (at your option) any later version.
12
13 GDB, GAS, and the GNU binutils are distributed in the hope that they
14 will be useful, but WITHOUT ANY WARRANTY; without even the implied
15 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
16 the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this file; see the file COPYING.  If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
21
22 #include <stdio.h>
23 #include "sysdep.h"
24 #include "dis-asm.h"
25 #include "opcode/i370.h"
26
27 /* This file provides several disassembler functions, all of which use
28    the disassembler interface defined in dis-asm.h.
29 */
30
31 int
32 print_insn_i370 (bfd_vma memaddr, struct disassemble_info *info)
33 {
34   bfd_byte buffer[8];
35   int status;
36   i370_insn_t insn;
37   const struct i370_opcode *opcode;
38   const struct i370_opcode *opcode_end;
39
40   status = (*info->read_memory_func) (memaddr, buffer, 6, info);
41   if (status != 0)
42     {
43       (*info->memory_error_func) (status, memaddr, info);
44       return -1;
45     }
46
47   /* Cast the bytes into the insn (in a host-endian indep way) */
48   insn.i[0] = (buffer[0] << 24) & 0xff000000;
49   insn.i[0] |= (buffer[1] << 16) & 0xff0000;
50   insn.i[0] |= (buffer[2] << 8) & 0xff00;
51   insn.i[0] |= buffer[3]  & 0xff;
52   insn.i[1] = (buffer[4] << 24) & 0xff000000;
53   insn.i[1] |= (buffer[5] << 16) & 0xff0000;
54
55   /* Find the first match in the opcode table.  We could speed this up
56      a bit by doing a binary search on the major opcode.  */
57   opcode_end = i370_opcodes + i370_num_opcodes;
58   for (opcode = i370_opcodes; opcode < opcode_end; opcode++)
59     {
60       const unsigned char *opindex;
61       const struct i370_operand *operand;
62       i370_insn_t masked;
63       int invalid;
64
65       /* Mask off operands, and look for a match ... */
66       masked = insn;
67       if (2 == opcode->len)
68         {
69           masked.i[0] >>= 16;
70           masked.i[0] &= 0xffff;
71         }
72       masked.i[0] &= opcode->mask.i[0];
73       if (masked.i[0] != opcode->opcode.i[0]) continue;
74
75       if (6 == opcode->len)
76         {
77           masked.i[1] &= opcode->mask.i[1];
78           if (masked.i[1] != opcode->opcode.i[1]) continue;
79         }
80
81       /* Found a match.  adjust a tad */
82       if (2 == opcode->len)
83         {
84           insn.i[0] >>= 16;
85           insn.i[0] &= 0xffff;
86         }
87
88       /* Make two passes over the operands.  First see if any of them
89          have extraction functions, and, if they do, make sure the
90          instruction is valid.  */
91       invalid = 0;
92       for (opindex = opcode->operands; *opindex != 0; opindex++)
93         {
94           operand = i370_operands + *opindex;
95           if (operand->extract)
96             (*operand->extract) (insn, &invalid);
97         }
98       if (invalid) continue;
99
100       /* The instruction is valid.  */
101       (*info->fprintf_func) (info->stream, "%s", opcode->name);
102       if (opcode->operands[0] != 0)
103         (*info->fprintf_func) (info->stream, "\t");
104
105       /* Now extract and print the operands.  */
106       for (opindex = opcode->operands; *opindex != 0; opindex++)
107         {
108           long value;
109
110           operand = i370_operands + *opindex;
111
112           /* Extract the value from the instruction.  */
113           if (operand->extract)
114             value = (*operand->extract) (insn, (int *) NULL);
115           else
116             {
117               value = (insn.i[0] >> operand->shift) & ((1 << operand->bits) - 1);
118             }
119
120           /* Print the operand as directed by the flags.  */
121           if ((operand->flags & I370_OPERAND_OPTIONAL) != 0)
122             {
123               if (value)
124                 (*info->fprintf_func) (info->stream, "(r%ld)", value);
125             }
126           else if ((operand->flags & I370_OPERAND_SBASE) != 0)
127             {
128               (*info->fprintf_func) (info->stream, "(r%ld)", value);
129             }
130           else if ((operand->flags & I370_OPERAND_INDEX) != 0)
131             {
132               if (value)
133                 (*info->fprintf_func) (info->stream, "(r%ld,", value);
134               else
135                 (*info->fprintf_func) (info->stream, "(,");
136             }
137           else if ((operand->flags & I370_OPERAND_LENGTH) != 0)
138             {
139               (*info->fprintf_func) (info->stream, "(%ld,", value);
140             }
141           else if ((operand->flags & I370_OPERAND_BASE) != 0)
142             (*info->fprintf_func) (info->stream, "r%ld)", value);
143           else if ((operand->flags & I370_OPERAND_GPR) != 0)
144             (*info->fprintf_func) (info->stream, "r%ld,", value);
145           else if ((operand->flags & I370_OPERAND_FPR) != 0)
146             (*info->fprintf_func) (info->stream, "f%ld,", value);
147           else if ((operand->flags & I370_OPERAND_RELATIVE) != 0)
148             (*info->fprintf_func) (info->stream, "%ld", value);
149           else
150             (*info->fprintf_func) (info->stream, " %ld, ", value);
151
152         }
153
154       return opcode->len;
155
156     }
157
158
159   /* We could not find a match.  */
160   (*info->fprintf_func) (info->stream, ".short 0x%02x%02x", buffer[0], buffer[1]);
161
162   return 2;
163 }