1 /* s390-dis.c -- Disassemble S390 instructions
2 Copyright 2000, 2001, 2002, 2003 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 static void init_disasm
PARAMS ((struct disassemble_info
*));
33 static unsigned int s390_extract_operand
34 PARAMS ((unsigned char *, const struct s390_operand
*));
36 /* Set up index table for first opcode byte. */
40 struct disassemble_info
*info
;
42 const struct s390_opcode
*opcode
;
43 const struct s390_opcode
*opcode_end
;
45 memset (opc_index
, 0, sizeof (opc_index
));
46 opcode_end
= s390_opcodes
+ s390_num_opcodes
;
47 for (opcode
= s390_opcodes
; opcode
< opcode_end
; opcode
++)
49 opc_index
[(int) opcode
->opcode
[0]] = opcode
- s390_opcodes
;
50 while ((opcode
< opcode_end
) &&
51 (opcode
[1].opcode
[0] == opcode
->opcode
[0]))
56 case bfd_mach_s390_31
:
57 current_arch_mask
= 1 << S390_OPCODE_ESA
;
59 case bfd_mach_s390_64
:
60 current_arch_mask
= 1 << S390_OPCODE_ZARCH
;
68 /* Extracts an operand value from an instruction. */
70 static inline unsigned int
71 s390_extract_operand (insn
, operand
)
73 const struct s390_operand
*operand
;
78 /* Extract fragments of the operand byte for byte. */
79 insn
+= operand
->shift
/ 8;
80 bits
= (operand
->shift
& 7) + operand
->bits
;
85 val
|= (unsigned int) *insn
++;
90 val
&= ((1U << (operand
->bits
- 1)) << 1) - 1;
92 /* Check for special long displacement case. */
93 if (operand
->bits
== 20 && operand
->shift
== 20)
94 val
= (val
& 0xff) << 12 | (val
& 0xfff00) >> 8;
96 /* Sign extend value if the operand is signed or pc relative. */
97 if ((operand
->flags
& (S390_OPERAND_SIGNED
| S390_OPERAND_PCREL
))
98 && (val
& (1U << (operand
->bits
- 1))))
99 val
|= (-1U << (operand
->bits
- 1)) << 1;
101 /* Double value if the operand is pc relative. */
102 if (operand
->flags
& S390_OPERAND_PCREL
)
105 /* Length x in an instructions has real length x+1. */
106 if (operand
->flags
& S390_OPERAND_LENGTH
)
111 /* Print a S390 instruction. */
114 print_insn_s390 (memaddr
, info
)
116 struct disassemble_info
*info
;
119 const struct s390_opcode
*opcode
;
120 const struct s390_opcode
*opcode_end
;
122 int status
, opsize
, bufsize
;
128 /* The output looks better if we put 6 bytes on a line. */
129 info
->bytes_per_line
= 6;
131 /* Every S390 instruction is max 6 bytes long. */
132 memset (buffer
, 0, 6);
133 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 6, info
);
136 for (bufsize
= 0; bufsize
< 6; bufsize
++)
137 if ((*info
->read_memory_func
) (memaddr
, buffer
, bufsize
+ 1, info
) != 0)
141 (*info
->memory_error_func
) (status
, memaddr
, info
);
144 /* Opsize calculation looks strange but it works
145 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
146 11xxxxxx -> 6 bytes. */
147 opsize
= ((((buffer
[0] >> 6) + 1) >> 1) + 1) << 1;
148 status
= opsize
> bufsize
;
153 opsize
= ((((buffer
[0] >> 6) + 1) >> 1) + 1) << 1;
158 /* Find the first match in the opcode table. */
159 opcode_end
= s390_opcodes
+ s390_num_opcodes
;
160 for (opcode
= s390_opcodes
+ opc_index
[(int) buffer
[0]];
161 (opcode
< opcode_end
) && (buffer
[0] == opcode
->opcode
[0]);
164 const struct s390_operand
*operand
;
165 const unsigned char *opindex
;
167 /* Check architecture. */
168 if (!(opcode
->modes
& current_arch_mask
))
170 /* Check signature of the opcode. */
171 if ((buffer
[1] & opcode
->mask
[1]) != opcode
->opcode
[1]
172 || (buffer
[2] & opcode
->mask
[2]) != opcode
->opcode
[2]
173 || (buffer
[3] & opcode
->mask
[3]) != opcode
->opcode
[3]
174 || (buffer
[4] & opcode
->mask
[4]) != opcode
->opcode
[4]
175 || (buffer
[5] & opcode
->mask
[5]) != opcode
->opcode
[5])
178 /* The instruction is valid. */
179 if (opcode
->operands
[0] != 0)
180 (*info
->fprintf_func
) (info
->stream
, "%s\t", opcode
->name
);
182 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
184 /* Extract the operands. */
186 for (opindex
= opcode
->operands
; *opindex
!= 0; opindex
++)
190 operand
= s390_operands
+ *opindex
;
191 value
= s390_extract_operand (buffer
, operand
);
193 if ((operand
->flags
& S390_OPERAND_INDEX
) && value
== 0)
195 if ((operand
->flags
& S390_OPERAND_BASE
) &&
196 value
== 0 && separator
== '(')
203 (*info
->fprintf_func
) (info
->stream
, "%c", separator
);
205 if (operand
->flags
& S390_OPERAND_GPR
)
206 (*info
->fprintf_func
) (info
->stream
, "%%r%i", value
);
207 else if (operand
->flags
& S390_OPERAND_FPR
)
208 (*info
->fprintf_func
) (info
->stream
, "%%f%i", value
);
209 else if (operand
->flags
& S390_OPERAND_AR
)
210 (*info
->fprintf_func
) (info
->stream
, "%%a%i", value
);
211 else if (operand
->flags
& S390_OPERAND_CR
)
212 (*info
->fprintf_func
) (info
->stream
, "%%c%i", value
);
213 else if (operand
->flags
& S390_OPERAND_PCREL
)
214 (*info
->print_address_func
) (memaddr
+ (int) value
, info
);
215 else if (operand
->flags
& S390_OPERAND_SIGNED
)
216 (*info
->fprintf_func
) (info
->stream
, "%i", (int) value
);
218 (*info
->fprintf_func
) (info
->stream
, "%i", value
);
220 if (operand
->flags
& S390_OPERAND_DISP
)
224 else if (operand
->flags
& S390_OPERAND_BASE
)
226 (*info
->fprintf_func
) (info
->stream
, ")");
233 /* Found instruction, printed it, return its size. */
236 /* No matching instruction found, fall through to hex print. */
241 value
= (unsigned int) buffer
[0];
242 value
= (value
<< 8) + (unsigned int) buffer
[1];
243 value
= (value
<< 8) + (unsigned int) buffer
[2];
244 value
= (value
<< 8) + (unsigned int) buffer
[3];
245 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%08x", value
);
248 else if (bufsize
>= 2)
250 value
= (unsigned int) buffer
[0];
251 value
= (value
<< 8) + (unsigned int) buffer
[1];
252 (*info
->fprintf_func
) (info
->stream
, ".short\t0x%04x", value
);
257 value
= (unsigned int) buffer
[0];
258 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", value
);