1 /* s390-dis.c -- Disassemble S390 instructions
2 Copyright 2000, 2001, 2002 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_ESAME
;
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 /* Sign extend value if the operand is signed or pc relative. */
93 if ((operand
->flags
& (S390_OPERAND_SIGNED
| S390_OPERAND_PCREL
))
94 && (val
& (1U << (operand
->bits
- 1))))
95 val
|= (-1U << (operand
->bits
- 1)) << 1;
97 /* Double value if the operand is pc relative. */
98 if (operand
->flags
& S390_OPERAND_PCREL
)
101 /* Length x in an instructions has real length x+1. */
102 if (operand
->flags
& S390_OPERAND_LENGTH
)
107 /* Print a S390 instruction. */
110 print_insn_s390 (memaddr
, info
)
112 struct disassemble_info
*info
;
115 const struct s390_opcode
*opcode
;
116 const struct s390_opcode
*opcode_end
;
118 int status
, opsize
, bufsize
;
124 /* The output looks better if we put 6 bytes on a line. */
125 info
->bytes_per_line
= 6;
127 /* Every S390 instruction is max 6 bytes long. */
128 memset (buffer
, 0, 6);
129 status
= (*info
->read_memory_func
) (memaddr
, buffer
, 6, info
);
132 for (bufsize
= 0; bufsize
< 6; bufsize
++)
133 if ((*info
->read_memory_func
) (memaddr
, buffer
, bufsize
+ 1, info
) != 0)
137 (*info
->memory_error_func
) (status
, memaddr
, info
);
140 /* Opsize calculation looks strange but it works
141 00xxxxxx -> 2 bytes, 01xxxxxx/10xxxxxx -> 4 bytes,
142 11xxxxxx -> 6 bytes. */
143 opsize
= ((((buffer
[0] >> 6) + 1) >> 1) + 1) << 1;
144 status
= opsize
> bufsize
;
149 opsize
= ((((buffer
[0] >> 6) + 1) >> 1) + 1) << 1;
154 /* Find the first match in the opcode table. */
155 opcode_end
= s390_opcodes
+ s390_num_opcodes
;
156 for (opcode
= s390_opcodes
+ opc_index
[(int) buffer
[0]];
157 (opcode
< opcode_end
) && (buffer
[0] == opcode
->opcode
[0]);
160 const struct s390_operand
*operand
;
161 const unsigned char *opindex
;
163 /* Check architecture. */
164 if (!(opcode
->architecture
& current_arch_mask
))
166 /* Check signature of the opcode. */
167 if ((buffer
[1] & opcode
->mask
[1]) != opcode
->opcode
[1]
168 || (buffer
[2] & opcode
->mask
[2]) != opcode
->opcode
[2]
169 || (buffer
[3] & opcode
->mask
[3]) != opcode
->opcode
[3]
170 || (buffer
[4] & opcode
->mask
[4]) != opcode
->opcode
[4]
171 || (buffer
[5] & opcode
->mask
[5]) != opcode
->opcode
[5])
174 /* The instruction is valid. */
175 if (opcode
->operands
[0] != 0)
176 (*info
->fprintf_func
) (info
->stream
, "%s\t", opcode
->name
);
178 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
180 /* Extract the operands. */
182 for (opindex
= opcode
->operands
; *opindex
!= 0; opindex
++)
186 operand
= s390_operands
+ *opindex
;
187 value
= s390_extract_operand (buffer
, operand
);
189 if ((operand
->flags
& S390_OPERAND_INDEX
) && value
== 0)
191 if ((operand
->flags
& S390_OPERAND_BASE
) &&
192 value
== 0 && separator
== '(')
199 (*info
->fprintf_func
) (info
->stream
, "%c", separator
);
201 if (operand
->flags
& S390_OPERAND_GPR
)
202 (*info
->fprintf_func
) (info
->stream
, "%%r%i", value
);
203 else if (operand
->flags
& S390_OPERAND_FPR
)
204 (*info
->fprintf_func
) (info
->stream
, "%%f%i", value
);
205 else if (operand
->flags
& S390_OPERAND_AR
)
206 (*info
->fprintf_func
) (info
->stream
, "%%a%i", value
);
207 else if (operand
->flags
& S390_OPERAND_CR
)
208 (*info
->fprintf_func
) (info
->stream
, "%%c%i", value
);
209 else if (operand
->flags
& S390_OPERAND_PCREL
)
210 (*info
->print_address_func
) (memaddr
+ (int) value
, info
);
211 else if (operand
->flags
& S390_OPERAND_SIGNED
)
212 (*info
->fprintf_func
) (info
->stream
, "%i", (int) value
);
214 (*info
->fprintf_func
) (info
->stream
, "%i", value
);
216 if (operand
->flags
& S390_OPERAND_DISP
)
220 else if (operand
->flags
& S390_OPERAND_BASE
)
222 (*info
->fprintf_func
) (info
->stream
, ")");
229 /* Found instruction, printed it, return its size. */
232 /* No matching instruction found, fall through to hex print. */
237 value
= (unsigned int) buffer
[0];
238 value
= (value
<< 8) + (unsigned int) buffer
[1];
239 value
= (value
<< 8) + (unsigned int) buffer
[2];
240 value
= (value
<< 8) + (unsigned int) buffer
[3];
241 (*info
->fprintf_func
) (info
->stream
, ".long\t0x%08x", value
);
244 else if (bufsize
>= 2)
246 value
= (unsigned int) buffer
[0];
247 value
= (value
<< 8) + (unsigned int) buffer
[1];
248 (*info
->fprintf_func
) (info
->stream
, ".short\t0x%04x", value
);
253 value
= (unsigned int) buffer
[0];
254 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", value
);