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