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