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