1 /* s390-dis.c -- Disassemble S390 instructions
2 Copyright 2000, 2001 Free Software Foundation, Inc.
3 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
5 This file is part of GDB, GAS and the GNU binutils.
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.
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.
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
26 #include "opcode/s390.h"
28 static int init_flag = 0;
29 static int opc_index[256];
30 static int current_arch_mask = 0;
32 /* Set up index table for first opcode byte. */
36 struct disassemble_info *info ATTRIBUTE_UNUSED;
38 const struct s390_opcode *opcode;
39 const struct s390_opcode *opcode_end;
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++)
45 opc_index[(int) opcode->opcode[0]] = opcode - s390_opcodes;
46 while ((opcode < opcode_end) &&
47 (opcode[1].opcode[0] == opcode->opcode[0]))
52 case bfd_mach_s390_esa:
53 current_arch_mask = 1 << S390_OPCODE_ESA;
55 case bfd_mach_s390_esame:
56 current_arch_mask = 1 << S390_OPCODE_ESAME;
64 /* Extracts an operand value from an instruction. */
66 static inline unsigned int
67 s390_extract_operand (insn, operand)
69 const struct s390_operand *operand;
74 /* Extract fragments of the operand byte for byte. */
75 insn += operand->shift / 8;
76 bits = (operand->shift & 7) + operand->bits;
81 val |= (unsigned int) *insn++;
86 val &= ((1U << (operand->bits - 1)) << 1) - 1;
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;
93 /* Double value if the operand is pc relative. */
94 if (operand->flags & S390_OPERAND_PCREL)
97 /* Length x in an instructions has real length x+1. */
98 if (operand->flags & S390_OPERAND_LENGTH)
103 /* Print a S390 instruction. */
106 print_insn_s390 (memaddr, info)
108 struct disassemble_info *info;
111 const struct s390_opcode *opcode;
112 const struct s390_opcode *opcode_end;
114 int status, opsize, bufsize;
120 /* The output looks better if we put 6 bytes on a line. */
121 info->bytes_per_line = 6;
123 /* Every S390 instruction is max 6 bytes long. */
124 memset (buffer, 0, 6);
125 status = (*info->read_memory_func) (memaddr, buffer, 6, info);
128 for (bufsize = 0; bufsize < 6; bufsize++)
129 if ((*info->read_memory_func) (memaddr, buffer, bufsize + 1, info) != 0)
133 (*info->memory_error_func) (status, memaddr, info);
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;
145 opsize = ((((buffer[0] >> 6) + 1) >> 1) + 1) << 1;
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]);
156 const struct s390_operand *operand;
157 const unsigned char *opindex;
159 /* Check architecture. */
160 if (!(opcode->architecture & current_arch_mask))
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])
170 /* The instruction is valid. */
171 if (opcode->operands[0] != 0)
172 (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
174 (*info->fprintf_func) (info->stream, "%s", opcode->name);
176 /* Extract the operands. */
178 for (opindex = opcode->operands; *opindex != 0; opindex++)
182 operand = s390_operands + *opindex;
183 value = s390_extract_operand (buffer, operand);
185 if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
187 if ((operand->flags & S390_OPERAND_BASE) &&
188 value == 0 && separator == '(')
195 (*info->fprintf_func) (info->stream, "%c", separator);
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);
210 (*info->fprintf_func) (info->stream, "%i", value);
212 if (operand->flags & S390_OPERAND_DISP)
216 else if (operand->flags & S390_OPERAND_BASE)
218 (*info->fprintf_func) (info->stream, ")");
225 /* Found instruction, printed it, return its size. */
228 /* No matching instruction found, fall through to hex print. */
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);
240 else if (bufsize >= 2)
242 value = (unsigned int) buffer[0];
243 value = (value << 8) + (unsigned int) buffer[1];
244 (*info->fprintf_func) (info->stream, ".short\t0x%04x", value);
249 value = (unsigned int) buffer[0];
250 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", value);