1 /* s390-dis.c -- Disassemble S390 instructions
2 Copyright (C) 2000, 2001 Free Software Foundation, Inc.
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 */
35 struct disassemble_info *info ATTRIBUTE_UNUSED;
37 const struct s390_opcode *opcode;
38 const struct s390_opcode *opcode_end;
40 memset(opc_index, 0, sizeof(opc_index));
41 opcode_end = s390_opcodes + s390_num_opcodes;
42 for (opcode = s390_opcodes; opcode < opcode_end; opcode++) {
43 opc_index[(int) opcode->opcode[0]] = opcode - s390_opcodes;
44 while ((opcode < opcode_end) &&
45 (opcode[1].opcode[0] == opcode->opcode[0]))
49 case bfd_mach_s390_esa:
50 current_arch_mask = 1 << S390_OPCODE_ESA;
52 case bfd_mach_s390_esame:
53 current_arch_mask = 1 << S390_OPCODE_ESAME;
61 /* Extracts an operand value from an instruction. */
63 static inline unsigned int
64 s390_extract_operand (insn, operand)
66 const struct s390_operand *operand;
71 /* extract fragments of the operand byte for byte */
72 insn += operand->shift/8;
73 bits = (operand->shift & 7) + operand->bits;
77 val |= (unsigned int) *insn++;
81 val &= ((1U << (operand->bits-1))<<1) - 1;
83 /* sign extend value if the operand is signed or pc relative */
84 if ((operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL)) &&
85 (val & (1U << (operand->bits-1))))
86 val |= (-1U << (operand->bits-1))<<1;
88 /* double value if the operand is pc relative */
89 if (operand->flags & S390_OPERAND_PCREL)
92 /* length x in an instructions has real length x+1 */
93 if (operand->flags & S390_OPERAND_LENGTH)
98 /* Print a S390 instruction. */
101 print_insn_s390 (memaddr, info)
103 struct disassemble_info *info;
106 const struct s390_opcode *opcode;
107 const struct s390_opcode *opcode_end;
109 int status, opsize, bufsize;
115 /* The output looks better if we put 6 bytes on a line. */
116 info->bytes_per_line = 6;
118 /* Every S390 instruction is max 6 bytes long. */
119 memset(buffer, 0, 6);
120 status = (*info->read_memory_func) (memaddr, buffer, 6, info);
122 for (bufsize = 0; bufsize < 6; bufsize++)
123 if ((*info->read_memory_func) (memaddr, buffer, bufsize+1, info) != 0)
126 (*info->memory_error_func) (status, memaddr, info);
129 /* Opsize calculation looks strange but it works
130 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
131 11xxxxxx -> 6 bytes. */
132 opsize = ((((buffer[0]>>6)+1)>>1)+1)<<1;
133 status = opsize > bufsize;
136 opsize = ((((buffer[0]>>6)+1)>>1)+1)<<1;
140 /* Find the first match in the opcode table. */
141 opcode_end = s390_opcodes + s390_num_opcodes;
142 for (opcode = s390_opcodes + opc_index[(int) buffer[0]];
143 (opcode < opcode_end) && (buffer[0] == opcode->opcode[0]);
145 const struct s390_operand *operand;
146 const unsigned char *opindex;
148 /* check architecture */
149 if (!(opcode->architecture & current_arch_mask))
151 /* check signature of the opcode */
152 if ((buffer[1] & opcode->mask[1]) != opcode->opcode[1] ||
153 (buffer[2] & opcode->mask[2]) != opcode->opcode[2] ||
154 (buffer[3] & opcode->mask[3]) != opcode->opcode[3] ||
155 (buffer[4] & opcode->mask[4]) != opcode->opcode[4] ||
156 (buffer[5] & opcode->mask[5]) != opcode->opcode[5])
159 /* the instruction is valid */
160 if (opcode->operands[0] != 0)
161 (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
163 (*info->fprintf_func) (info->stream, "%s", opcode->name);
165 /* Extract the operands. */
167 for (opindex = opcode->operands; *opindex != 0; opindex++) {
170 operand = s390_operands + *opindex;
171 value = s390_extract_operand(buffer, operand);
173 if ((operand->flags & S390_OPERAND_INDEX) && value == 0)
175 if ((operand->flags & S390_OPERAND_BASE) &&
176 value == 0 && separator == '(') {
182 (*info->fprintf_func) (info->stream, "%c", separator);
184 if (operand->flags & S390_OPERAND_GPR)
185 (*info->fprintf_func) (info->stream, "%%r%i", value);
186 else if (operand->flags & S390_OPERAND_FPR)
187 (*info->fprintf_func) (info->stream, "%%f%i", value);
188 else if (operand->flags & S390_OPERAND_AR)
189 (*info->fprintf_func) (info->stream, "%%a%i", value);
190 else if (operand->flags & S390_OPERAND_CR)
191 (*info->fprintf_func) (info->stream, "%%c%i", value);
192 else if (operand->flags & S390_OPERAND_PCREL)
193 (*info->print_address_func) (memaddr + (int) value, info);
194 else if (operand->flags & S390_OPERAND_SIGNED)
195 (*info->fprintf_func) (info->stream, "%i", (int) value);
197 (*info->fprintf_func) (info->stream, "%i", value);
199 if (operand->flags & S390_OPERAND_DISP) {
201 } else if (operand->flags & S390_OPERAND_BASE) {
202 (*info->fprintf_func) (info->stream, ")");
208 /* found instruction, printed it, return its size */
211 /* no matching instruction found, fall through to hex print */
215 value = (unsigned int) buffer[0];
216 value = (value << 8) + (unsigned int) buffer[1];
217 value = (value << 8) + (unsigned int) buffer[2];
218 value = (value << 8) + (unsigned int) buffer[3];
219 (*info->fprintf_func) (info->stream,".long\t0x%08x", value);
221 } else if (bufsize >= 2) {
222 value = (unsigned int) buffer[0];
223 value = (value << 8) + (unsigned int) buffer[1];
224 (*info->fprintf_func) (info->stream,".short\t0x%04x", value);
227 value = (unsigned int) buffer[0];
228 (*info->fprintf_func) (info->stream,".byte\t0x%02x", value);