* s390-dis.c: Fix formatting.
[deliverable/binutils-gdb.git] / opcodes / s390-dis.c
1 /* s390-dis.c -- Disassemble S390 instructions
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
4
5 This file is part of GDB, GAS and the GNU binutils.
6
7 This program 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 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include "ansidecl.h"
24 #include "sysdep.h"
25 #include "dis-asm.h"
26 #include "opcode/s390.h"
27
28 static int init_flag = 0;
29 static int opc_index[256];
30 static int current_arch_mask = 0;
31
32 /* Set up index table for first opcode byte. */
33
34 static void
35 init_disasm (info)
36 struct disassemble_info *info ATTRIBUTE_UNUSED;
37 {
38 const struct s390_opcode *opcode;
39 const struct s390_opcode *opcode_end;
40
41 memset (opc_index, 0, sizeof (opc_index));
42 opcode_end = s390_opcodes + s390_num_opcodes;
43 for (opcode = s390_opcodes; opcode < opcode_end; opcode++)
44 {
45 opc_index[(int) opcode->opcode[0]] = opcode - s390_opcodes;
46 while ((opcode < opcode_end) &&
47 (opcode[1].opcode[0] == opcode->opcode[0]))
48 opcode++;
49 }
50 switch (info->mach)
51 {
52 case bfd_mach_s390_esa:
53 current_arch_mask = 1 << S390_OPCODE_ESA;
54 break;
55 case bfd_mach_s390_esame:
56 current_arch_mask = 1 << S390_OPCODE_ESAME;
57 break;
58 default:
59 abort ();
60 }
61 init_flag = 1;
62 }
63
64 /* Extracts an operand value from an instruction. */
65
66 static inline unsigned int
67 s390_extract_operand (insn, operand)
68 unsigned char *insn;
69 const struct s390_operand *operand;
70 {
71 unsigned int val;
72 int bits;
73
74 /* Extract fragments of the operand byte for byte. */
75 insn += operand->shift / 8;
76 bits = (operand->shift & 7) + operand->bits;
77 val = 0;
78 do
79 {
80 val <<= 8;
81 val |= (unsigned int) *insn++;
82 bits -= 8;
83 }
84 while (bits > 0);
85 val >>= -bits;
86 val &= ((1U << (operand->bits - 1)) << 1) - 1;
87
88 /* Sign extend value if the operand is signed or pc relative. */
89 if ((operand->flags & (S390_OPERAND_SIGNED | S390_OPERAND_PCREL))
90 && (val & (1U << (operand->bits - 1))))
91 val |= (-1U << (operand->bits - 1)) << 1;
92
93 /* Double value if the operand is pc relative. */
94 if (operand->flags & S390_OPERAND_PCREL)
95 val <<= 1;
96
97 /* Length x in an instructions has real length x+1. */
98 if (operand->flags & S390_OPERAND_LENGTH)
99 val++;
100 return val;
101 }
102
103 /* Print a S390 instruction. */
104
105 int
106 print_insn_s390 (memaddr, info)
107 bfd_vma memaddr;
108 struct disassemble_info *info;
109 {
110 bfd_byte buffer[6];
111 const struct s390_opcode *opcode;
112 const struct s390_opcode *opcode_end;
113 unsigned int value;
114 int status, opsize, bufsize;
115 char separator;
116
117 if (init_flag == 0)
118 init_disasm (info);
119
120 /* The output looks better if we put 6 bytes on a line. */
121 info->bytes_per_line = 6;
122
123 /* Every S390 instruction is max 6 bytes long. */
124 memset (buffer, 0, 6);
125 status = (*info->read_memory_func) (memaddr, buffer, 6, info);
126 if (status != 0)
127 {
128 for (bufsize = 0; bufsize < 6; bufsize++)
129 if ((*info->read_memory_func) (memaddr, buffer, bufsize + 1, info) != 0)
130 break;
131 if (bufsize <= 0)
132 {
133 (*info->memory_error_func) (status, memaddr, info);
134 return -1;
135 }
136 /* Opsize calculation looks strange but it works
137 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
138 11xxxxxx -> 6 bytes. */
139 opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
140 status = opsize > bufsize;
141 }
142 else
143 {
144 bufsize = 6;
145 opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
146 }
147
148 if (status == 0)
149 {
150 /* Find the first match in the opcode table. */
151 opcode_end = s390_opcodes + s390_num_opcodes;
152 for (opcode = s390_opcodes + opc_index[(int) buffer[0]];
153 (opcode < opcode_end) && (buffer[0] == opcode->opcode[0]);
154 opcode++)
155 {
156 const struct s390_operand *operand;
157 const unsigned char *opindex;
158
159 /* Check architecture. */
160 if (!(opcode->architecture & current_arch_mask))
161 continue;
162 /* Check signature of the opcode. */
163 if ((buffer[1] & opcode->mask[1]) != opcode->opcode[1]
164 || (buffer[2] & opcode->mask[2]) != opcode->opcode[2]
165 || (buffer[3] & opcode->mask[3]) != opcode->opcode[3]
166 || (buffer[4] & opcode->mask[4]) != opcode->opcode[4]
167 || (buffer[5] & opcode->mask[5]) != opcode->opcode[5])
168 continue;
169
170 /* The instruction is valid. */
171 if (opcode->operands[0] != 0)
172 (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
173 else
174 (*info->fprintf_func) (info->stream, "%s", opcode->name);
175
176 /* Extract the operands. */
177 separator = 0;
178 for (opindex = opcode->operands; *opindex != 0; opindex++)
179 {
180 unsigned int value;
181
182 operand = s390_operands + *opindex;
183 value = s390_extract_operand (buffer, operand);
184
185 if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
186 continue;
187 if ((operand->flags & S390_OPERAND_BASE) &&
188 value == 0 && separator == '(')
189 {
190 separator = ',';
191 continue;
192 }
193
194 if (separator)
195 (*info->fprintf_func) (info->stream, "%c", separator);
196
197 if (operand->flags & S390_OPERAND_GPR)
198 (*info->fprintf_func) (info->stream, "%%r%i", value);
199 else if (operand->flags & S390_OPERAND_FPR)
200 (*info->fprintf_func) (info->stream, "%%f%i", value);
201 else if (operand->flags & S390_OPERAND_AR)
202 (*info->fprintf_func) (info->stream, "%%a%i", value);
203 else if (operand->flags & S390_OPERAND_CR)
204 (*info->fprintf_func) (info->stream, "%%c%i", value);
205 else if (operand->flags & S390_OPERAND_PCREL)
206 (*info->print_address_func) (memaddr + (int) value, info);
207 else if (operand->flags & S390_OPERAND_SIGNED)
208 (*info->fprintf_func) (info->stream, "%i", (int) value);
209 else
210 (*info->fprintf_func) (info->stream, "%i", value);
211
212 if (operand->flags & S390_OPERAND_DISP)
213 {
214 separator = '(';
215 }
216 else if (operand->flags & S390_OPERAND_BASE)
217 {
218 (*info->fprintf_func) (info->stream, ")");
219 separator = ',';
220 }
221 else
222 separator = ',';
223 }
224
225 /* Found instruction, printed it, return its size. */
226 return opsize;
227 }
228 /* No matching instruction found, fall through to hex print. */
229 }
230
231 if (bufsize >= 4)
232 {
233 value = (unsigned int) buffer[0];
234 value = (value << 8) + (unsigned int) buffer[1];
235 value = (value << 8) + (unsigned int) buffer[2];
236 value = (value << 8) + (unsigned int) buffer[3];
237 (*info->fprintf_func) (info->stream, ".long\t0x%08x", value);
238 return 4;
239 }
240 else if (bufsize >= 2)
241 {
242 value = (unsigned int) buffer[0];
243 value = (value << 8) + (unsigned int) buffer[1];
244 (*info->fprintf_func) (info->stream, ".short\t0x%04x", value);
245 return 2;
246 }
247 else
248 {
249 value = (unsigned int) buffer[0];
250 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", value);
251 return 1;
252 }
253 }
This page took 0.035724 seconds and 5 git commands to generate.