Frame static link: Handle null pointer.
[deliverable/binutils-gdb.git] / opcodes / ppc-dis.c
CommitLineData
252b5132 1/* ppc-dis.c -- Disassemble PowerPC instructions
6f2750fe 2 Copyright (C) 1994-2016 Free Software Foundation, Inc.
252b5132
RH
3 Written by Ian Lance Taylor, Cygnus Support
4
9b201bb5
NC
5 This file is part of the GNU opcodes library.
6
7 This library 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 3, or (at your option)
10 any later version.
11
12 It is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this file; see the file COPYING. If not, write to the
19 Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132 21
252b5132 22#include "sysdep.h"
df7b86aa 23#include <stdio.h>
252b5132 24#include "dis-asm.h"
b9c361e0 25#include "elf-bfd.h"
94caa966 26#include "elf/ppc.h"
69fe9ce5 27#include "opintl.h"
252b5132
RH
28#include "opcode/ppc.h"
29
30/* This file provides several disassembler functions, all of which use
31 the disassembler interface defined in dis-asm.h. Several functions
32 are provided because this file handles disassembly for the PowerPC
33 in both big and little endian mode and also for the POWER (RS/6000)
34 chip. */
fa452fa6
PB
35static int print_insn_powerpc (bfd_vma, struct disassemble_info *, int,
36 ppc_cpu_t);
252b5132 37
fa452fa6
PB
38struct dis_private
39{
40 /* Stash the result of parsing disassembler_options here. */
41 ppc_cpu_t dialect;
b240011a 42} private;
fa452fa6
PB
43
44#define POWERPC_DIALECT(INFO) \
45 (((struct dis_private *) ((INFO)->private_data))->dialect)
418c1742 46
69fe9ce5
AM
47struct ppc_mopt {
48 const char *opt;
49 ppc_cpu_t cpu;
50 ppc_cpu_t sticky;
51};
52
53struct ppc_mopt ppc_opts[] = {
bdc70b4a 54 { "403", (PPC_OPCODE_PPC | PPC_OPCODE_403),
69fe9ce5 55 0 },
bdc70b4a 56 { "405", (PPC_OPCODE_PPC | PPC_OPCODE_403 | PPC_OPCODE_405),
69fe9ce5 57 0 },
bdc70b4a
AM
58 { "440", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440
59 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
69fe9ce5 60 0 },
bdc70b4a
AM
61 { "464", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_440
62 | PPC_OPCODE_ISEL | PPC_OPCODE_RFMCI),
69fe9ce5 63 0 },
bdc70b4a
AM
64 { "476", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_440
65 | PPC_OPCODE_476 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5),
9fe54b1c 66 0 },
bdc70b4a 67 { "601", (PPC_OPCODE_PPC | PPC_OPCODE_601),
69fe9ce5 68 0 },
bdc70b4a 69 { "603", (PPC_OPCODE_PPC),
69fe9ce5 70 0 },
bdc70b4a 71 { "604", (PPC_OPCODE_PPC),
69fe9ce5 72 0 },
bdc70b4a 73 { "620", (PPC_OPCODE_PPC | PPC_OPCODE_64),
69fe9ce5 74 0 },
bdc70b4a 75 { "7400", (PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC),
69fe9ce5 76 0 },
bdc70b4a 77 { "7410", (PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC),
69fe9ce5 78 0 },
ef5a96d5 79 { "7450", (PPC_OPCODE_PPC | PPC_OPCODE_7450 | PPC_OPCODE_ALTIVEC),
69fe9ce5 80 0 },
bdc70b4a 81 { "7455", (PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC),
69fe9ce5 82 0 },
ef5a96d5 83 { "750cl", (PPC_OPCODE_PPC | PPC_OPCODE_750 | PPC_OPCODE_PPCPS)
69fe9ce5 84 , 0 },
ef5a96d5
AM
85 { "821", (PPC_OPCODE_PPC | PPC_OPCODE_860),
86 0 },
87 { "850", (PPC_OPCODE_PPC | PPC_OPCODE_860),
88 0 },
89 { "860", (PPC_OPCODE_PPC | PPC_OPCODE_860),
90 0 },
bdc70b4a
AM
91 { "a2", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_POWER4
92 | PPC_OPCODE_POWER5 | PPC_OPCODE_CACHELCK | PPC_OPCODE_64
93 | PPC_OPCODE_A2),
cdc51b07 94 0 },
bdc70b4a 95 { "altivec", (PPC_OPCODE_PPC),
c7a5aa9c 96 PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 },
69fe9ce5
AM
97 { "any", 0,
98 PPC_OPCODE_ANY },
bdc70b4a 99 { "booke", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE),
69fe9ce5 100 0 },
bdc70b4a 101 { "booke32", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE),
69fe9ce5 102 0 },
bdc70b4a
AM
103 { "cell", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
104 | PPC_OPCODE_CELL | PPC_OPCODE_ALTIVEC),
69fe9ce5 105 0 },
bdc70b4a 106 { "com", (PPC_OPCODE_COMMON),
69fe9ce5 107 0 },
bdc70b4a 108 { "e300", (PPC_OPCODE_PPC | PPC_OPCODE_E300),
69fe9ce5
AM
109 0 },
110 { "e500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE
111 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
112 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
e01d869a 113 | PPC_OPCODE_E500),
69fe9ce5
AM
114 0 },
115 { "e500mc", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
116 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
117 | PPC_OPCODE_E500MC),
118 0 },
0dc93057
AM
119 { "e500mc64", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
120 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
63d0fa4e
AM
121 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER5
122 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7),
0dc93057 123 0 },
aea77599
AM
124 { "e5500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
125 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
126 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
127 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
128 | PPC_OPCODE_POWER7),
129 0 },
130 { "e6500", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_ISEL
131 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
132 | PPC_OPCODE_E500MC | PPC_OPCODE_64 | PPC_OPCODE_ALTIVEC
133 | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_E6500 | PPC_OPCODE_POWER4
134 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_POWER7),
135 0 },
69fe9ce5
AM
136 { "e500x2", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_SPE
137 | PPC_OPCODE_ISEL | PPC_OPCODE_EFS | PPC_OPCODE_BRLOCK
138 | PPC_OPCODE_PMR | PPC_OPCODE_CACHELCK | PPC_OPCODE_RFMCI
e01d869a 139 | PPC_OPCODE_E500),
69fe9ce5
AM
140 0 },
141 { "efs", (PPC_OPCODE_PPC | PPC_OPCODE_EFS),
142 0 },
bdc70b4a 143 { "power4", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4),
69fe9ce5 144 0 },
bdc70b4a
AM
145 { "power5", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
146 | PPC_OPCODE_POWER5),
69fe9ce5 147 0 },
bdc70b4a
AM
148 { "power6", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
149 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC),
69fe9ce5 150 0 },
bdc70b4a
AM
151 { "power7", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
152 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
153 | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
69fe9ce5 154 0 },
5817ffd1
PB
155 { "power8", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
156 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
157 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM
158 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX),
159 0 },
a680de9a
PB
160 { "power9", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
161 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
162 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9
163 | PPC_OPCODE_HTM | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2
164 | PPC_OPCODE_VSX | PPC_OPCODE_VSX3 ),
165 0 },
bdc70b4a 166 { "ppc", (PPC_OPCODE_PPC),
69fe9ce5 167 0 },
bdc70b4a 168 { "ppc32", (PPC_OPCODE_PPC),
69fe9ce5 169 0 },
bdc70b4a 170 { "ppc64", (PPC_OPCODE_PPC | PPC_OPCODE_64),
69fe9ce5 171 0 },
bdc70b4a 172 { "ppc64bridge", (PPC_OPCODE_PPC | PPC_OPCODE_64_BRIDGE),
69fe9ce5
AM
173 0 },
174 { "ppcps", (PPC_OPCODE_PPC | PPC_OPCODE_PPCPS),
175 0 },
bdc70b4a 176 { "pwr", (PPC_OPCODE_POWER),
69fe9ce5 177 0 },
bdc70b4a 178 { "pwr2", (PPC_OPCODE_POWER | PPC_OPCODE_POWER2),
cdc51b07 179 0 },
bdc70b4a 180 { "pwr4", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4),
cdc51b07 181 0 },
bdc70b4a
AM
182 { "pwr5", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
183 | PPC_OPCODE_POWER5),
cdc51b07 184 0 },
bdc70b4a
AM
185 { "pwr5x", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
186 | PPC_OPCODE_POWER5),
cdc51b07 187 0 },
bdc70b4a
AM
188 { "pwr6", (PPC_OPCODE_PPC | PPC_OPCODE_64 | PPC_OPCODE_POWER4
189 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6 | PPC_OPCODE_ALTIVEC),
cdc51b07 190 0 },
bdc70b4a
AM
191 { "pwr7", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
192 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
193 | PPC_OPCODE_POWER7 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_VSX),
69fe9ce5 194 0 },
5817ffd1
PB
195 { "pwr8", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
196 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
197 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_HTM
198 | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2 | PPC_OPCODE_VSX),
199 0 },
a680de9a
PB
200 { "pwr9", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_64
201 | PPC_OPCODE_POWER4 | PPC_OPCODE_POWER5 | PPC_OPCODE_POWER6
202 | PPC_OPCODE_POWER7 | PPC_OPCODE_POWER8 | PPC_OPCODE_POWER9
203 | PPC_OPCODE_HTM | PPC_OPCODE_ALTIVEC | PPC_OPCODE_ALTIVEC2
204 | PPC_OPCODE_VSX | PPC_OPCODE_VSX3 ),
205 0 },
bdc70b4a 206 { "pwrx", (PPC_OPCODE_POWER | PPC_OPCODE_POWER2),
69fe9ce5
AM
207 0 },
208 { "spe", (PPC_OPCODE_PPC | PPC_OPCODE_EFS),
209 PPC_OPCODE_SPE },
bdc70b4a
AM
210 { "titan", (PPC_OPCODE_PPC | PPC_OPCODE_BOOKE | PPC_OPCODE_PMR
211 | PPC_OPCODE_RFMCI | PPC_OPCODE_TITAN),
ce3d2015 212 0 },
b9c361e0
JL
213 { "vle", (PPC_OPCODE_PPC | PPC_OPCODE_ISEL | PPC_OPCODE_VLE),
214 PPC_OPCODE_VLE },
bdc70b4a 215 { "vsx", (PPC_OPCODE_PPC),
a680de9a 216 PPC_OPCODE_VSX | PPC_OPCODE_VSX3 },
5817ffd1
PB
217 { "htm", (PPC_OPCODE_PPC),
218 PPC_OPCODE_HTM },
69fe9ce5
AM
219};
220
b9c361e0
JL
221/* Switch between Booke and VLE dialects for interlinked dumps. */
222static ppc_cpu_t
223get_powerpc_dialect (struct disassemble_info *info)
224{
225 ppc_cpu_t dialect = 0;
226
227 dialect = POWERPC_DIALECT (info);
228
229 /* Disassemble according to the section headers flags for VLE-mode. */
230 if (dialect & PPC_OPCODE_VLE
94caa966
AM
231 && info->section->owner != NULL
232 && bfd_get_flavour (info->section->owner) == bfd_target_elf_flavour
233 && elf_object_id (info->section->owner) == PPC32_ELF_DATA
234 && (elf_section_flags (info->section) & SHF_PPC_VLE) != 0)
b9c361e0
JL
235 return dialect;
236 else
237 return dialect & ~ PPC_OPCODE_VLE;
238}
239
69fe9ce5
AM
240/* Handle -m and -M options that set cpu type, and .machine arg. */
241
242ppc_cpu_t
776fc418 243ppc_parse_cpu (ppc_cpu_t ppc_cpu, ppc_cpu_t *sticky, const char *arg)
69fe9ce5 244{
69fe9ce5
AM
245 unsigned int i;
246
247 for (i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++)
248 if (strcmp (ppc_opts[i].opt, arg) == 0)
249 {
250 if (ppc_opts[i].sticky)
251 {
776fc418
AM
252 *sticky |= ppc_opts[i].sticky;
253 if ((ppc_cpu & ~*sticky) != 0)
69fe9ce5
AM
254 break;
255 }
256 ppc_cpu = ppc_opts[i].cpu;
257 break;
258 }
259 if (i >= sizeof (ppc_opts) / sizeof (ppc_opts[0]))
260 return 0;
261
776fc418 262 ppc_cpu |= *sticky;
69fe9ce5
AM
263 return ppc_cpu;
264}
265
266/* Determine which set of machines to disassemble for. */
418c1742 267
b240011a 268static void
fa452fa6 269powerpc_init_dialect (struct disassemble_info *info)
418c1742 270{
69fe9ce5 271 ppc_cpu_t dialect = 0;
776fc418 272 ppc_cpu_t sticky = 0;
69fe9ce5 273 char *arg;
fa452fa6
PB
274 struct dis_private *priv = calloc (sizeof (*priv), 1);
275
276 if (priv == NULL)
b240011a 277 priv = &private;
418c1742 278
776fc418
AM
279 switch (info->mach)
280 {
281 case bfd_mach_ppc_403:
282 case bfd_mach_ppc_403gc:
4f6ffcd3 283 dialect = ppc_parse_cpu (dialect, &sticky, "403");
776fc418
AM
284 break;
285 case bfd_mach_ppc_405:
4f6ffcd3 286 dialect = ppc_parse_cpu (dialect, &sticky, "405");
776fc418
AM
287 break;
288 case bfd_mach_ppc_601:
4f6ffcd3 289 dialect = ppc_parse_cpu (dialect, &sticky, "601");
776fc418
AM
290 break;
291 case bfd_mach_ppc_a35:
292 case bfd_mach_ppc_rs64ii:
293 case bfd_mach_ppc_rs64iii:
4f6ffcd3 294 dialect = ppc_parse_cpu (dialect, &sticky, "pwr2") | PPC_OPCODE_64;
776fc418
AM
295 break;
296 case bfd_mach_ppc_e500:
4f6ffcd3 297 dialect = ppc_parse_cpu (dialect, &sticky, "e500");
776fc418
AM
298 break;
299 case bfd_mach_ppc_e500mc:
4f6ffcd3 300 dialect = ppc_parse_cpu (dialect, &sticky, "e500mc");
776fc418
AM
301 break;
302 case bfd_mach_ppc_e500mc64:
4f6ffcd3 303 dialect = ppc_parse_cpu (dialect, &sticky, "e500mc64");
776fc418
AM
304 break;
305 case bfd_mach_ppc_e5500:
4f6ffcd3 306 dialect = ppc_parse_cpu (dialect, &sticky, "e5500");
776fc418
AM
307 break;
308 case bfd_mach_ppc_e6500:
4f6ffcd3 309 dialect = ppc_parse_cpu (dialect, &sticky, "e6500");
776fc418
AM
310 break;
311 case bfd_mach_ppc_titan:
4f6ffcd3 312 dialect = ppc_parse_cpu (dialect, &sticky, "titan");
776fc418
AM
313 break;
314 case bfd_mach_ppc_vle:
4f6ffcd3 315 dialect = ppc_parse_cpu (dialect, &sticky, "vle");
776fc418
AM
316 break;
317 default:
a680de9a 318 dialect = ppc_parse_cpu (dialect, &sticky, "power9") | PPC_OPCODE_ANY;
776fc418
AM
319 }
320
69fe9ce5
AM
321 arg = info->disassembler_options;
322 while (arg != NULL)
323 {
324 ppc_cpu_t new_cpu = 0;
325 char *end = strchr (arg, ',');
9b4e5766 326
69fe9ce5
AM
327 if (end != NULL)
328 *end = 0;
9b4e5766 329
776fc418 330 if ((new_cpu = ppc_parse_cpu (dialect, &sticky, arg)) != 0)
69fe9ce5
AM
331 dialect = new_cpu;
332 else if (strcmp (arg, "32") == 0)
7102e95e 333 dialect &= ~(ppc_cpu_t) PPC_OPCODE_64;
69fe9ce5 334 else if (strcmp (arg, "64") == 0)
bdc70b4a 335 dialect |= PPC_OPCODE_64;
69fe9ce5
AM
336 else
337 fprintf (stderr, _("warning: ignoring unknown -M%s option\n"), arg);
9622b051 338
69fe9ce5
AM
339 if (end != NULL)
340 *end++ = ',';
341 arg = end;
342 }
661bd698 343
fa452fa6
PB
344 info->private_data = priv;
345 POWERPC_DIALECT(info) = dialect;
b240011a
AM
346}
347
b9c361e0
JL
348#define PPC_OPCD_SEGS 64
349static unsigned short powerpc_opcd_indices[PPC_OPCD_SEGS+1];
350#define VLE_OPCD_SEGS 32
351static unsigned short vle_opcd_indices[VLE_OPCD_SEGS+1];
b240011a
AM
352
353/* Calculate opcode table indices to speed up disassembly,
354 and init dialect. */
355
356void
357disassemble_init_powerpc (struct disassemble_info *info)
358{
359 int i;
d6688282 360 unsigned short last;
fa452fa6 361
27c49e9a 362 if (powerpc_opcd_indices[PPC_OPCD_SEGS] == 0)
b240011a 363 {
b240011a 364
27c49e9a
AB
365 i = powerpc_num_opcodes;
366 while (--i >= 0)
367 {
368 unsigned op = PPC_OP (powerpc_opcodes[i].opcode);
369
370 powerpc_opcd_indices[op] = i;
371 }
372
373 last = powerpc_num_opcodes;
374 for (i = PPC_OPCD_SEGS; i > 0; --i)
375 {
376 if (powerpc_opcd_indices[i] == 0)
377 powerpc_opcd_indices[i] = last;
378 last = powerpc_opcd_indices[i];
379 }
380
381 i = vle_num_opcodes;
382 while (--i >= 0)
383 {
384 unsigned op = VLE_OP (vle_opcodes[i].opcode, vle_opcodes[i].mask);
385 unsigned seg = VLE_OP_TO_SEG (op);
386
387 vle_opcd_indices[seg] = i;
388 }
389
390 last = vle_num_opcodes;
391 for (i = VLE_OPCD_SEGS; i > 0; --i)
392 {
393 if (vle_opcd_indices[i] == 0)
394 vle_opcd_indices[i] = last;
395 last = vle_opcd_indices[i];
396 }
b9c361e0
JL
397 }
398
b240011a
AM
399 if (info->arch == bfd_arch_powerpc)
400 powerpc_init_dialect (info);
418c1742
MG
401}
402
403/* Print a big endian PowerPC instruction. */
252b5132
RH
404
405int
823bbe9d 406print_insn_big_powerpc (bfd_vma memaddr, struct disassemble_info *info)
252b5132 407{
b9c361e0 408 return print_insn_powerpc (memaddr, info, 1, get_powerpc_dialect (info));
252b5132
RH
409}
410
418c1742 411/* Print a little endian PowerPC instruction. */
252b5132
RH
412
413int
823bbe9d 414print_insn_little_powerpc (bfd_vma memaddr, struct disassemble_info *info)
252b5132 415{
b9c361e0 416 return print_insn_powerpc (memaddr, info, 0, get_powerpc_dialect (info));
252b5132
RH
417}
418
419/* Print a POWER (RS/6000) instruction. */
420
421int
823bbe9d 422print_insn_rs6000 (bfd_vma memaddr, struct disassemble_info *info)
252b5132
RH
423{
424 return print_insn_powerpc (memaddr, info, 1, PPC_OPCODE_POWER);
425}
426
ea192fa3
PB
427/* Extract the operand value from the PowerPC or POWER instruction. */
428
429static long
430operand_value_powerpc (const struct powerpc_operand *operand,
fa452fa6 431 unsigned long insn, ppc_cpu_t dialect)
ea192fa3
PB
432{
433 long value;
434 int invalid;
435 /* Extract the value from the instruction. */
436 if (operand->extract)
437 value = (*operand->extract) (insn, dialect, &invalid);
438 else
439 {
b9c361e0
JL
440 if (operand->shift >= 0)
441 value = (insn >> operand->shift) & operand->bitm;
442 else
443 value = (insn << -operand->shift) & operand->bitm;
ea192fa3
PB
444 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
445 {
446 /* BITM is always some number of zeros followed by some
b9c361e0 447 number of ones, followed by some number of zeros. */
ea192fa3
PB
448 unsigned long top = operand->bitm;
449 /* top & -top gives the rightmost 1 bit, so this
450 fills in any trailing zeros. */
451 top |= (top & -top) - 1;
452 top &= ~(top >> 1);
453 value = (value ^ top) - top;
454 }
455 }
456
457 return value;
458}
459
460/* Determine whether the optional operand(s) should be printed. */
461
462static int
463skip_optional_operands (const unsigned char *opindex,
fa452fa6 464 unsigned long insn, ppc_cpu_t dialect)
ea192fa3
PB
465{
466 const struct powerpc_operand *operand;
467
468 for (; *opindex != 0; opindex++)
469 {
470 operand = &powerpc_operands[*opindex];
471 if ((operand->flags & PPC_OPERAND_NEXT) != 0
472 || ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
11a0cf2e
PB
473 && operand_value_powerpc (operand, insn, dialect) !=
474 ppc_optional_operand_value (operand)))
ea192fa3
PB
475 return 0;
476 }
477
478 return 1;
479}
480
d6688282
AM
481/* Find a match for INSN in the opcode table, given machine DIALECT.
482 A DIALECT of -1 is special, matching all machine opcode variations. */
b9c361e0 483
d6688282
AM
484static const struct powerpc_opcode *
485lookup_powerpc (unsigned long insn, ppc_cpu_t dialect)
486{
487 const struct powerpc_opcode *opcode;
488 const struct powerpc_opcode *opcode_end;
489 unsigned long op;
490
491 /* Get the major opcode of the instruction. */
492 op = PPC_OP (insn);
493
494 /* Find the first match in the opcode table for this major opcode. */
495 opcode_end = powerpc_opcodes + powerpc_opcd_indices[op + 1];
496 for (opcode = powerpc_opcodes + powerpc_opcd_indices[op];
497 opcode < opcode_end;
498 ++opcode)
499 {
500 const unsigned char *opindex;
501 const struct powerpc_operand *operand;
502 int invalid;
503
504 if ((insn & opcode->mask) != opcode->opcode
505 || (dialect != (ppc_cpu_t) -1
506 && ((opcode->flags & dialect) == 0
507 || (opcode->deprecated & dialect) != 0)))
508 continue;
509
510 /* Check validity of operands. */
511 invalid = 0;
512 for (opindex = opcode->operands; *opindex != 0; opindex++)
513 {
514 operand = powerpc_operands + *opindex;
515 if (operand->extract)
516 (*operand->extract) (insn, dialect, &invalid);
517 }
518 if (invalid)
519 continue;
520
521 return opcode;
522 }
523
524 return NULL;
525}
526
b9c361e0
JL
527/* Find a match for INSN in the VLE opcode table. */
528
529static const struct powerpc_opcode *
530lookup_vle (unsigned long insn)
531{
532 const struct powerpc_opcode *opcode;
533 const struct powerpc_opcode *opcode_end;
534 unsigned op, seg;
535
536 op = PPC_OP (insn);
537 if (op >= 0x20 && op <= 0x37)
538 {
539 /* This insn has a 4-bit opcode. */
540 op &= 0x3c;
541 }
542 seg = VLE_OP_TO_SEG (op);
543
544 /* Find the first match in the opcode table for this major opcode. */
545 opcode_end = vle_opcodes + vle_opcd_indices[seg + 1];
546 for (opcode = vle_opcodes + vle_opcd_indices[seg];
547 opcode < opcode_end;
548 ++opcode)
549 {
550 unsigned long table_opcd = opcode->opcode;
551 unsigned long table_mask = opcode->mask;
552 bfd_boolean table_op_is_short = PPC_OP_SE_VLE(table_mask);
553 unsigned long insn2;
554 const unsigned char *opindex;
555 const struct powerpc_operand *operand;
556 int invalid;
557
558 insn2 = insn;
559 if (table_op_is_short)
560 insn2 >>= 16;
561 if ((insn2 & table_mask) != table_opcd)
562 continue;
563
564 /* Check validity of operands. */
565 invalid = 0;
566 for (opindex = opcode->operands; *opindex != 0; ++opindex)
567 {
568 operand = powerpc_operands + *opindex;
569 if (operand->extract)
570 (*operand->extract) (insn, (ppc_cpu_t)0, &invalid);
571 }
572 if (invalid)
573 continue;
574
575 return opcode;
576 }
577
578 return NULL;
579}
580
252b5132
RH
581/* Print a PowerPC or POWER instruction. */
582
583static int
823bbe9d
AM
584print_insn_powerpc (bfd_vma memaddr,
585 struct disassemble_info *info,
586 int bigendian,
fa452fa6 587 ppc_cpu_t dialect)
252b5132
RH
588{
589 bfd_byte buffer[4];
590 int status;
591 unsigned long insn;
592 const struct powerpc_opcode *opcode;
b9c361e0 593 bfd_boolean insn_is_short;
252b5132
RH
594
595 status = (*info->read_memory_func) (memaddr, buffer, 4, info);
596 if (status != 0)
597 {
b9c361e0
JL
598 /* The final instruction may be a 2-byte VLE insn. */
599 if ((dialect & PPC_OPCODE_VLE) != 0)
600 {
601 /* Clear buffer so unused bytes will not have garbage in them. */
602 buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
603 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
604 if (status != 0)
605 {
606 (*info->memory_error_func) (status, memaddr, info);
607 return -1;
608 }
609 }
610 else
611 {
612 (*info->memory_error_func) (status, memaddr, info);
613 return -1;
614 }
252b5132
RH
615 }
616
617 if (bigendian)
618 insn = bfd_getb32 (buffer);
619 else
620 insn = bfd_getl32 (buffer);
621
b9c361e0
JL
622 /* Get the major opcode of the insn. */
623 opcode = NULL;
624 insn_is_short = FALSE;
625 if ((dialect & PPC_OPCODE_VLE) != 0)
626 {
627 opcode = lookup_vle (insn);
628 if (opcode != NULL)
629 insn_is_short = PPC_OP_SE_VLE(opcode->mask);
630 }
631 if (opcode == NULL)
632 opcode = lookup_powerpc (insn, dialect);
d6688282
AM
633 if (opcode == NULL && (dialect & PPC_OPCODE_ANY) != 0)
634 opcode = lookup_powerpc (insn, (ppc_cpu_t) -1);
252b5132 635
d6688282 636 if (opcode != NULL)
252b5132 637 {
252b5132
RH
638 const unsigned char *opindex;
639 const struct powerpc_operand *operand;
252b5132
RH
640 int need_comma;
641 int need_paren;
ea192fa3 642 int skip_optional;
252b5132 643
252b5132 644 if (opcode->operands[0] != 0)
fdd12ef3
AM
645 (*info->fprintf_func) (info->stream, "%-7s ", opcode->name);
646 else
647 (*info->fprintf_func) (info->stream, "%s", opcode->name);
252b5132 648
b9c361e0
JL
649 if (insn_is_short)
650 /* The operands will be fetched out of the 16-bit instruction. */
651 insn >>= 16;
652
252b5132
RH
653 /* Now extract and print the operands. */
654 need_comma = 0;
655 need_paren = 0;
ea192fa3 656 skip_optional = -1;
252b5132
RH
657 for (opindex = opcode->operands; *opindex != 0; opindex++)
658 {
659 long value;
660
661 operand = powerpc_operands + *opindex;
662
663 /* Operands that are marked FAKE are simply ignored. We
664 already made sure that the extract function considered
665 the instruction to be valid. */
666 if ((operand->flags & PPC_OPERAND_FAKE) != 0)
667 continue;
668
ea192fa3
PB
669 /* If all of the optional operands have the value zero,
670 then don't print any of them. */
65b650b4
AM
671 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
672 {
673 if (skip_optional < 0)
674 skip_optional = skip_optional_operands (opindex, insn,
675 dialect);
676 if (skip_optional)
677 continue;
678 }
252b5132 679
ea192fa3
PB
680 value = operand_value_powerpc (operand, insn, dialect);
681
252b5132
RH
682 if (need_comma)
683 {
684 (*info->fprintf_func) (info->stream, ",");
685 need_comma = 0;
686 }
687
688 /* Print the operand as directed by the flags. */
fdd12ef3
AM
689 if ((operand->flags & PPC_OPERAND_GPR) != 0
690 || ((operand->flags & PPC_OPERAND_GPR_0) != 0 && value != 0))
252b5132
RH
691 (*info->fprintf_func) (info->stream, "r%ld", value);
692 else if ((operand->flags & PPC_OPERAND_FPR) != 0)
693 (*info->fprintf_func) (info->stream, "f%ld", value);
786e2c0f
C
694 else if ((operand->flags & PPC_OPERAND_VR) != 0)
695 (*info->fprintf_func) (info->stream, "v%ld", value);
9b4e5766
PB
696 else if ((operand->flags & PPC_OPERAND_VSR) != 0)
697 (*info->fprintf_func) (info->stream, "vs%ld", value);
252b5132
RH
698 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0)
699 (*info->print_address_func) (memaddr + value, info);
700 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
701 (*info->print_address_func) ((bfd_vma) value & 0xffffffff, info);
43e65147 702 else if ((operand->flags & PPC_OPERAND_FSL) != 0)
081ba1b3
AM
703 (*info->fprintf_func) (info->stream, "fsl%ld", value);
704 else if ((operand->flags & PPC_OPERAND_FCR) != 0)
705 (*info->fprintf_func) (info->stream, "fcr%ld", value);
706 else if ((operand->flags & PPC_OPERAND_UDI) != 0)
707 (*info->fprintf_func) (info->stream, "%ld", value);
b9c361e0
JL
708 else if ((operand->flags & PPC_OPERAND_CR_REG) != 0
709 && (((dialect & PPC_OPCODE_PPC) != 0)
710 || ((dialect & PPC_OPCODE_VLE) != 0)))
711 (*info->fprintf_func) (info->stream, "cr%ld", value);
712 else if (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
713 && (((dialect & PPC_OPCODE_PPC) != 0)
714 || ((dialect & PPC_OPCODE_VLE) != 0)))
252b5132 715 {
b9c361e0
JL
716 static const char *cbnames[4] = { "lt", "gt", "eq", "so" };
717 int cr;
718 int cc;
719
720 cr = value >> 2;
721 if (cr != 0)
722 (*info->fprintf_func) (info->stream, "4*cr%d+", cr);
723 cc = value & 3;
724 (*info->fprintf_func) (info->stream, "%s", cbnames[cc]);
252b5132 725 }
70dc4e32 726 else
d908c8af 727 (*info->fprintf_func) (info->stream, "%d", (int) value);
252b5132
RH
728
729 if (need_paren)
730 {
731 (*info->fprintf_func) (info->stream, ")");
732 need_paren = 0;
733 }
734
735 if ((operand->flags & PPC_OPERAND_PARENS) == 0)
736 need_comma = 1;
737 else
738 {
739 (*info->fprintf_func) (info->stream, "(");
740 need_paren = 1;
741 }
742 }
743
b9c361e0
JL
744 /* We have found and printed an instruction.
745 If it was a short VLE instruction we have more to do. */
746 if (insn_is_short)
747 {
748 memaddr += 2;
749 return 2;
750 }
751 else
752 /* Otherwise, return. */
753 return 4;
252b5132
RH
754 }
755
756 /* We could not find a match. */
757 (*info->fprintf_func) (info->stream, ".long 0x%lx", insn);
758
759 return 4;
760}
07dd56a9
NC
761
762void
823bbe9d 763print_ppc_disassembler_options (FILE *stream)
07dd56a9 764{
69fe9ce5
AM
765 unsigned int i, col;
766
767 fprintf (stream, _("\n\
07dd56a9 768The following PPC specific disassembler options are supported for use with\n\
69fe9ce5
AM
769the -M switch:\n"));
770
771 for (col = 0, i = 0; i < sizeof (ppc_opts) / sizeof (ppc_opts[0]); i++)
772 {
773 col += fprintf (stream, " %s,", ppc_opts[i].opt);
774 if (col > 66)
775 {
776 fprintf (stream, "\n");
777 col = 0;
778 }
779 }
780 fprintf (stream, " 32, 64\n");
07dd56a9 781}
This page took 0.766166 seconds and 4 git commands to generate.