Add initialiser for membership field
[external/binutils.git] / opcodes / mips-dis.c
1 /* Print mips instructions for GDB, the GNU debugger, or for objdump.
2    Copyright (c) 1989, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3    Free Software Foundation, Inc.
4    Contributed by Nobuyuki Hikichi(hikichi@sra.co.jp).
5
6 This file is part of GDB, GAS, and the GNU binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "sysdep.h"
23 #include "dis-asm.h"
24 #include "opcode/mips.h"
25 #include "opintl.h"
26
27 /* FIXME: These are needed to figure out if the code is mips16 or
28    not. The low bit of the address is often a good indicator.  No
29    symbol table is available when this code runs out in an embedded
30    system as when it is used for disassembler support in a monitor. */
31
32 #if !defined(EMBEDDED_ENV)
33 #define SYMTAB_AVAILABLE 1
34 #include "elf-bfd.h"
35 #include "elf/mips.h"
36 #endif
37
38 static int print_insn_mips16 PARAMS ((bfd_vma, struct disassemble_info *));
39 static void print_mips16_insn_arg
40   PARAMS ((int, const struct mips_opcode *, int, boolean, int, bfd_vma,
41            struct disassemble_info *));
42
43 /* Mips instructions are never longer than this many bytes.  */
44 #define MAXLEN 4
45
46 static void print_insn_arg PARAMS ((const char *, unsigned long, bfd_vma,
47                                     struct disassemble_info *));
48 static int _print_insn_mips PARAMS ((bfd_vma, unsigned long int,
49                                      struct disassemble_info *));
50
51 \f
52 /* FIXME: This should be shared with gdb somehow.  */
53 #define STD_REGISTER_NAMES      \
54     {   "zero", "at",   "v0",   "v1",   "a0",   "a1",   "a2",   "a3", \
55         "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",   "t7", \
56         "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "s6",   "s7", \
57         "t8",   "t9",   "k0",   "k1",   "gp",   "sp",   "s8",   "ra", \
58         "sr",   "lo",   "hi",   "bad",  "cause","pc",    \
59         "f0",   "f1",   "f2",   "f3",   "f4",   "f5",   "f6",   "f7", \
60         "f8",   "f9",   "f10",  "f11",  "f12",  "f13",  "f14",  "f15", \
61         "f16",  "f17",  "f18",  "f19",  "f20",  "f21",  "f22",  "f23",\
62         "f24",  "f25",  "f26",  "f27",  "f28",  "f29",  "f30",  "f31",\
63         "fsr",  "fir",  "fp",   "inx",  "rand", "tlblo","ctxt", "tlbhi",\
64         "epc",  "prid"\
65     }
66
67 static CONST char * CONST std_reg_names[] = STD_REGISTER_NAMES;
68
69 /* The mips16 register names.  */
70 static const char * const mips16_reg_names[] =
71 {
72   "s0", "s1", "v0", "v1", "a0", "a1", "a2", "a3"
73 };
74
75 /* Scalar register names. set_mips_isa_type() decides which register name
76    table to use.  */
77 static CONST char * CONST *reg_names = NULL;
78 \f
79 /* subroutine */
80 static void
81 print_insn_arg (d, l, pc, info)
82      const char *d;
83      register unsigned long int l;
84      bfd_vma pc;
85      struct disassemble_info *info;
86 {
87   int delta;
88
89   switch (*d)
90     {
91     case ',':
92     case '(':
93     case ')':
94       (*info->fprintf_func) (info->stream, "%c", *d);
95       break;
96
97     case 's':
98     case 'b':
99     case 'r':
100     case 'v':
101       (*info->fprintf_func) (info->stream, "$%s",
102                              reg_names[(l >> OP_SH_RS) & OP_MASK_RS]);
103       break;
104
105     case 't':
106     case 'w':
107       (*info->fprintf_func) (info->stream, "$%s",
108                              reg_names[(l >> OP_SH_RT) & OP_MASK_RT]);
109       break;
110
111     case 'i':
112     case 'u':
113       (*info->fprintf_func) (info->stream, "0x%x",
114                         (l >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE);
115       break;
116
117     case 'j': /* same as i, but sign-extended */
118     case 'o':
119       delta = (l >> OP_SH_DELTA) & OP_MASK_DELTA;
120       if (delta & 0x8000)
121         delta |= ~0xffff;
122       (*info->fprintf_func) (info->stream, "%d",
123                              delta);
124       break;
125
126     case 'h':
127       (*info->fprintf_func) (info->stream, "0x%x",
128                              (unsigned int) ((l >> OP_SH_PREFX)
129                                              & OP_MASK_PREFX));
130       break;
131
132     case 'k':
133       (*info->fprintf_func) (info->stream, "0x%x",
134                              (unsigned int) ((l >> OP_SH_CACHE)
135                                              & OP_MASK_CACHE));
136       break;
137
138     case 'a':
139       (*info->print_address_func)
140         (((pc & ~ (bfd_vma) 0x0fffffff)
141           | (((l >> OP_SH_TARGET) & OP_MASK_TARGET) << 2)),
142          info);
143       break;
144
145     case 'p':
146       /* sign extend the displacement */
147       delta = (l >> OP_SH_DELTA) & OP_MASK_DELTA;
148       if (delta & 0x8000)
149         delta |= ~0xffff;
150       (*info->print_address_func)
151         ((delta << 2) + pc + 4,
152          info);
153       break;
154
155     case 'd':
156       (*info->fprintf_func) (info->stream, "$%s",
157                              reg_names[(l >> OP_SH_RD) & OP_MASK_RD]);
158       break;
159
160     case 'U':
161       {
162       /* First check for both rd and rt being equal. */
163       int reg = (l >> OP_SH_RD) & OP_MASK_RD;
164       if (reg == ((l >> OP_SH_RT) & OP_MASK_RT))
165         (*info->fprintf_func) (info->stream, "$%s",
166                                reg_names[reg]);
167       else                        
168         {
169           /* If one is zero use the other. */
170           if (reg == 0)
171             (*info->fprintf_func) (info->stream, "$%s",
172                                    reg_names[(l >> OP_SH_RT) & OP_MASK_RT]);
173           else if (((l >> OP_SH_RT) & OP_MASK_RT) == 0)
174             (*info->fprintf_func) (info->stream, "$%s",
175                                    reg_names[reg]);
176           else /* Bogus, result depends on processor. */
177             (*info->fprintf_func) (info->stream, "$%s or $%s",
178                                    reg_names[reg],
179                                    reg_names[(l >> OP_SH_RT) & OP_MASK_RT]);
180           }
181       }
182       break;
183
184     case 'z':
185       (*info->fprintf_func) (info->stream, "$%s", reg_names[0]);
186       break;
187
188     case '<':
189       (*info->fprintf_func) (info->stream, "0x%x",
190                              (l >> OP_SH_SHAMT) & OP_MASK_SHAMT);
191       break;
192
193     case 'c':
194       (*info->fprintf_func) (info->stream, "0x%x",
195                              (l >> OP_SH_CODE) & OP_MASK_CODE);
196       break;
197
198     case 'q':
199       (*info->fprintf_func) (info->stream, "0x%x",
200                              (l >> OP_SH_CODE2) & OP_MASK_CODE2);
201       break;
202
203     case 'C':
204       (*info->fprintf_func) (info->stream, "0x%x",
205                              (l >> OP_SH_COPZ) & OP_MASK_COPZ);
206       break;
207
208     case 'B':
209       (*info->fprintf_func) (info->stream, "0x%x",
210                              (l >> OP_SH_CODE20) & OP_MASK_CODE20);
211       break;
212
213     case 'J':
214       (*info->fprintf_func) (info->stream, "0x%x",
215                              (l >> OP_SH_CODE19) & OP_MASK_CODE19);
216       break;
217
218     case 'S':
219     case 'V':
220       (*info->fprintf_func) (info->stream, "$f%d",
221                              (l >> OP_SH_FS) & OP_MASK_FS);
222       break;
223
224     case 'T':
225     case 'W':
226       (*info->fprintf_func) (info->stream, "$f%d",
227                              (l >> OP_SH_FT) & OP_MASK_FT);
228       break;
229
230     case 'D':
231       (*info->fprintf_func) (info->stream, "$f%d",
232                              (l >> OP_SH_FD) & OP_MASK_FD);
233       break;
234
235     case 'R':
236       (*info->fprintf_func) (info->stream, "$f%d",
237                              (l >> OP_SH_FR) & OP_MASK_FR);
238       break;
239
240     case 'E':
241       (*info->fprintf_func) (info->stream, "$%d",
242                              (l >> OP_SH_RT) & OP_MASK_RT);
243       break;
244
245     case 'G':
246       (*info->fprintf_func) (info->stream, "$%d",
247                              (l >> OP_SH_RD) & OP_MASK_RD);
248       break;
249
250     case 'N':
251       (*info->fprintf_func) (info->stream, "$fcc%d",
252                              (l >> OP_SH_BCC) & OP_MASK_BCC);
253       break;
254
255     case 'M':
256       (*info->fprintf_func) (info->stream, "$fcc%d",
257                              (l >> OP_SH_CCC) & OP_MASK_CCC);
258       break;
259
260     case 'P':
261       (*info->fprintf_func) (info->stream, "%d",
262                              (l >> OP_SH_PERFREG) & OP_MASK_PERFREG);
263       break;
264
265     case 'H':
266       (*info->fprintf_func) (info->stream, "%d", 
267                              (l >> OP_SH_SEL) & OP_MASK_SEL);
268       break;
269
270     default:
271       /* xgettext:c-format */
272       (*info->fprintf_func) (info->stream,
273                              _("# internal error, undefined modifier(%c)"),
274                              *d);
275       break;
276     }
277 }
278 \f
279 #if SYMTAB_AVAILABLE
280
281 /* Figure out the MIPS ISA and CPU based on the machine number.
282    FIXME: What does this have to do with SYMTAB_AVAILABLE?  */
283
284 static void
285 set_mips_isa_type (mach, isa, cputype)
286      int mach;
287      int *isa;
288      int *cputype;
289 {
290   int target_processor = 0;
291   int mips_isa = 0;
292
293   /* Use standard MIPS register names by default.  */
294   reg_names = std_reg_names;
295
296   switch (mach)
297     {
298     case bfd_mach_mips3000:
299       target_processor = CPU_R3000;
300       mips_isa = 1;
301       break;
302     case bfd_mach_mips3900:
303       target_processor = CPU_R3900;
304       mips_isa = 1;
305       break;
306     case bfd_mach_mips4000:
307       target_processor = CPU_R4000;
308       mips_isa = 3;
309       break;
310     case bfd_mach_mips4010:
311       target_processor = CPU_R4010;
312       mips_isa = 2;
313       break;
314     case bfd_mach_mips4100:
315       target_processor = CPU_VR4100;
316       mips_isa = 3;
317       break;
318     case bfd_mach_mips4111:
319       target_processor = CPU_VR4100; /* FIXME: Shouldn't this be CPU_R4111 ??? */
320       mips_isa = 3;
321       break;
322     case bfd_mach_mips4300:
323       target_processor = CPU_R4300;
324       mips_isa = 3;
325       break;
326     case bfd_mach_mips4400:
327       target_processor = CPU_R4400;
328       mips_isa = 3;
329       break;
330     case bfd_mach_mips4600:
331       target_processor = CPU_R4600;
332       mips_isa = 3;
333       break;
334     case bfd_mach_mips4650:
335       target_processor = CPU_R4650;
336       mips_isa = 3;
337       break;
338     case bfd_mach_mips4K:
339       target_processor = CPU_4K;
340       mips_isa = 2;
341       break;
342     case bfd_mach_mips5000:
343       target_processor = CPU_R5000;
344       mips_isa = 4;
345       break;
346     case bfd_mach_mips6000:
347       target_processor = CPU_R6000;
348       mips_isa = 2;
349       break;
350     case bfd_mach_mips8000:
351       target_processor = CPU_R8000;
352       mips_isa = 4;
353       break;
354     case bfd_mach_mips10000:
355       target_processor = CPU_R10000;
356       mips_isa = 4;
357       break;
358     case bfd_mach_mips16:
359       target_processor = CPU_MIPS16;
360       mips_isa = 3;
361       break;
362     default:
363       target_processor = CPU_R3000;
364       mips_isa = 3;
365       break;
366     }
367
368   *isa = mips_isa;
369   *cputype = target_processor;
370 }
371
372 #endif /* SYMTAB_AVAILABLE */
373
374 /* Print the mips instruction at address MEMADDR in debugged memory,
375    on using INFO.  Returns length of the instruction, in bytes, which is
376    always 4.  BIGENDIAN must be 1 if this is big-endian code, 0 if
377    this is little-endian code.  */
378
379 static int
380 _print_insn_mips (memaddr, word, info)
381      bfd_vma memaddr;
382      unsigned long int word;
383      struct disassemble_info *info;
384 {
385   register const struct mips_opcode *op;
386   int target_processor, mips_isa;
387   static boolean init = 0;
388   static const struct mips_opcode *mips_hash[OP_MASK_OP + 1];
389
390   /* Build a hash table to shorten the search time.  */
391   if (! init)
392     {
393       unsigned int i;
394
395       for (i = 0; i <= OP_MASK_OP; i++)
396         {
397           for (op = mips_opcodes; op < &mips_opcodes[NUMOPCODES]; op++)
398             {
399               if (op->pinfo == INSN_MACRO)
400                 continue;
401               if (i == ((op->match >> OP_SH_OP) & OP_MASK_OP))
402                 {
403                   mips_hash[i] = op;
404                   break;
405                 }
406             }
407         }
408
409       init = 1;
410     }
411
412 #if ! SYMTAB_AVAILABLE
413   /* This is running out on a target machine, not in a host tool.
414      FIXME: Where does mips_target_info come from?  */
415   target_processor = mips_target_info.processor;
416   mips_isa = mips_target_info.isa;
417 #else  
418   set_mips_isa_type (info->mach, &mips_isa, &target_processor);
419 #endif  
420
421   info->bytes_per_chunk = 4;
422   info->display_endian = info->endian;
423
424   op = mips_hash[(word >> OP_SH_OP) & OP_MASK_OP];
425   if (op != NULL)
426     {
427       for (; op < &mips_opcodes[NUMOPCODES]; op++)
428         {
429           if (op->pinfo != INSN_MACRO && (word & op->mask) == op->match)
430             {
431               register const char *d;
432
433               if (! OPCODE_IS_MEMBER (op, mips_isa, target_processor, 0))
434                 continue;
435
436               (*info->fprintf_func) (info->stream, "%s", op->name);
437
438               d = op->args;
439               if (d != NULL && *d != '\0')
440                 {
441                     (*info->fprintf_func) (info->stream, "\t");
442                   for (; *d != '\0'; d++)
443                       print_insn_arg (d, word, memaddr, info);
444                 }
445
446               return 4;
447             }
448         }
449     }
450
451   /* Handle undefined instructions.  */
452   (*info->fprintf_func) (info->stream, "0x%x", word);
453   return 4;
454 }
455
456
457 /* In an environment where we do not know the symbol type of the
458    instruction we are forced to assume that the low order bit of the
459    instructions' address may mark it as a mips16 instruction.  If we
460    are single stepping, or the pc is within the disassembled function,
461    this works.  Otherwise, we need a clue.  Sometimes.  */
462
463 int
464 print_insn_big_mips (memaddr, info)
465      bfd_vma memaddr;
466      struct disassemble_info *info;
467 {
468   bfd_byte buffer[4];
469   int status;
470
471 #if 1
472   /* FIXME: If odd address, this is CLEARLY a mips 16 instruction.  */
473   /* Only a few tools will work this way.  */
474   if (memaddr & 0x01)
475     return print_insn_mips16 (memaddr, info);
476 #endif  
477
478 #if SYMTAB_AVAILABLE
479   if (info->mach == 16
480       || (info->flavour == bfd_target_elf_flavour
481           && info->symbols != NULL
482           && ((*(elf_symbol_type **) info->symbols)->internal_elf_sym.st_other
483               == STO_MIPS16)))
484     return print_insn_mips16 (memaddr, info);
485 #endif  
486
487   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
488   if (status == 0)
489     return _print_insn_mips (memaddr, (unsigned long) bfd_getb32 (buffer),
490                              info);
491   else
492     {
493       (*info->memory_error_func) (status, memaddr, info);
494       return -1;
495     }
496 }
497
498 int
499 print_insn_little_mips (memaddr, info)
500      bfd_vma memaddr;
501      struct disassemble_info *info;
502 {
503   bfd_byte buffer[4];
504   int status;
505
506
507 #if 1
508   if (memaddr & 0x01)
509     return print_insn_mips16 (memaddr, info);
510 #endif  
511
512 #if SYMTAB_AVAILABLE
513   if (info->mach == 16
514       || (info->flavour == bfd_target_elf_flavour
515           && info->symbols != NULL
516           && ((*(elf_symbol_type **) info->symbols)->internal_elf_sym.st_other
517               == STO_MIPS16)))
518     return print_insn_mips16 (memaddr, info);
519 #endif  
520
521   status = (*info->read_memory_func) (memaddr, buffer, 4, info);
522   if (status == 0)
523     return _print_insn_mips (memaddr, (unsigned long) bfd_getl32 (buffer),
524                              info);
525   else
526     {
527       (*info->memory_error_func) (status, memaddr, info);
528       return -1;
529     }
530 }
531 \f
532 /* Disassemble mips16 instructions.  */
533
534 static int
535 print_insn_mips16 (memaddr, info)
536      bfd_vma memaddr;
537      struct disassemble_info *info;
538 {
539   int status;
540   bfd_byte buffer[2];
541   int length;
542   int insn;
543   boolean use_extend;
544   int extend = 0;
545   const struct mips_opcode *op, *opend;
546
547   info->bytes_per_chunk = 2;
548   info->display_endian = info->endian;
549
550   info->insn_info_valid = 1;
551   info->branch_delay_insns = 0;
552   info->data_size = 0;
553   info->insn_type = dis_nonbranch;
554   info->target = 0;
555   info->target2 = 0;
556
557   status = (*info->read_memory_func) (memaddr, buffer, 2, info);
558   if (status != 0)
559     {
560       (*info->memory_error_func) (status, memaddr, info);
561       return -1;
562     }
563
564   length = 2;
565
566   if (info->endian == BFD_ENDIAN_BIG)
567     insn = bfd_getb16 (buffer);
568   else
569     insn = bfd_getl16 (buffer);
570
571   /* Handle the extend opcode specially.  */
572   use_extend = false;
573   if ((insn & 0xf800) == 0xf000)
574     {
575       use_extend = true;
576       extend = insn & 0x7ff;
577
578       memaddr += 2;
579
580       status = (*info->read_memory_func) (memaddr, buffer, 2, info);
581       if (status != 0)
582         {
583           (*info->fprintf_func) (info->stream, "extend 0x%x",
584                                  (unsigned int) extend);
585           (*info->memory_error_func) (status, memaddr, info);
586           return -1;
587         }
588
589       if (info->endian == BFD_ENDIAN_BIG)
590         insn = bfd_getb16 (buffer);
591       else
592         insn = bfd_getl16 (buffer);
593
594       /* Check for an extend opcode followed by an extend opcode.  */
595       if ((insn & 0xf800) == 0xf000)
596         {
597           (*info->fprintf_func) (info->stream, "extend 0x%x",
598                                  (unsigned int) extend);
599           info->insn_type = dis_noninsn;
600           return length;
601         }
602
603       length += 2;
604     }
605
606   /* FIXME: Should probably use a hash table on the major opcode here.  */
607
608   opend = mips16_opcodes + bfd_mips16_num_opcodes;
609   for (op = mips16_opcodes; op < opend; op++)
610     {
611       if (op->pinfo != INSN_MACRO && (insn & op->mask) == op->match)
612         {
613           const char *s;
614
615           if (strchr (op->args, 'a') != NULL)
616             {
617               if (use_extend)
618                 {
619                   (*info->fprintf_func) (info->stream, "extend 0x%x",
620                                          (unsigned int) extend);
621                   info->insn_type = dis_noninsn;
622                   return length - 2;
623                 }
624
625               use_extend = false;
626
627               memaddr += 2;
628
629               status = (*info->read_memory_func) (memaddr, buffer, 2,
630                                                   info);
631               if (status == 0)
632                 {
633                   use_extend = true;
634                   if (info->endian == BFD_ENDIAN_BIG)
635                     extend = bfd_getb16 (buffer);
636                   else
637                     extend = bfd_getl16 (buffer);
638                   length += 2;
639                 }
640             }
641
642           (*info->fprintf_func) (info->stream, "%s", op->name);
643           if (op->args[0] != '\0')
644             (*info->fprintf_func) (info->stream, "\t");
645
646           for (s = op->args; *s != '\0'; s++)
647             {
648               if (*s == ','
649                   && s[1] == 'w'
650                   && (((insn >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX)
651                       == ((insn >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY)))
652                 {
653                   /* Skip the register and the comma.  */
654                   ++s;
655                   continue;
656                 }
657               if (*s == ','
658                   && s[1] == 'v'
659                   && (((insn >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ)
660                       == ((insn >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX)))
661                 {
662                   /* Skip the register and the comma.  */
663                   ++s;
664                   continue;
665                 }
666               print_mips16_insn_arg (*s, op, insn, use_extend, extend, memaddr,
667                                      info);
668             }
669
670           if ((op->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
671             {
672               info->branch_delay_insns = 1;
673               if (info->insn_type != dis_jsr)
674                 info->insn_type = dis_branch;
675             }
676
677           return length;
678         }
679     }
680
681   if (use_extend)
682     (*info->fprintf_func) (info->stream, "0x%x", extend | 0xf000);
683   (*info->fprintf_func) (info->stream, "0x%x", insn);
684   info->insn_type = dis_noninsn;
685
686   return length;
687 }
688
689 /* Disassemble an operand for a mips16 instruction.  */
690
691 static void
692 print_mips16_insn_arg (type, op, l, use_extend, extend, memaddr, info)
693      int type;
694      const struct mips_opcode *op;
695      int l;
696      boolean use_extend;
697      int extend;
698      bfd_vma memaddr;
699      struct disassemble_info *info;
700 {
701   switch (type)
702     {
703     case ',':
704     case '(':
705     case ')':
706       (*info->fprintf_func) (info->stream, "%c", type);
707       break;
708
709     case 'y':
710     case 'w':
711       (*info->fprintf_func) (info->stream, "$%s",
712                              mips16_reg_names[((l >> MIPS16OP_SH_RY)
713                                                & MIPS16OP_MASK_RY)]);
714       break;
715
716     case 'x':
717     case 'v':
718       (*info->fprintf_func) (info->stream, "$%s",
719                              mips16_reg_names[((l >> MIPS16OP_SH_RX)
720                                                & MIPS16OP_MASK_RX)]);
721       break;
722
723     case 'z':
724       (*info->fprintf_func) (info->stream, "$%s",
725                              mips16_reg_names[((l >> MIPS16OP_SH_RZ)
726                                                & MIPS16OP_MASK_RZ)]);
727       break;
728
729     case 'Z':
730       (*info->fprintf_func) (info->stream, "$%s",
731                              mips16_reg_names[((l >> MIPS16OP_SH_MOVE32Z)
732                                                & MIPS16OP_MASK_MOVE32Z)]);
733       break;
734
735     case '0':
736       (*info->fprintf_func) (info->stream, "$%s", reg_names[0]);
737       break;
738
739     case 'S':
740       (*info->fprintf_func) (info->stream, "$%s", reg_names[29]);
741       break;
742
743     case 'P':
744       (*info->fprintf_func) (info->stream, "$pc");
745       break;
746
747     case 'R':
748       (*info->fprintf_func) (info->stream, "$%s", reg_names[31]);
749       break;
750
751     case 'X':
752       (*info->fprintf_func) (info->stream, "$%s",
753                              reg_names[((l >> MIPS16OP_SH_REGR32)
754                                         & MIPS16OP_MASK_REGR32)]);
755       break;
756
757     case 'Y':
758       (*info->fprintf_func) (info->stream, "$%s",
759                              reg_names[MIPS16OP_EXTRACT_REG32R (l)]);
760       break;
761
762     case '<':
763     case '>':
764     case '[':
765     case ']':
766     case '4':
767     case '5':
768     case 'H':
769     case 'W':
770     case 'D':
771     case 'j':
772     case '6':
773     case '8':
774     case 'V':
775     case 'C':
776     case 'U':
777     case 'k':
778     case 'K':
779     case 'p':
780     case 'q':
781     case 'A':
782     case 'B':
783     case 'E':
784       {
785         int immed, nbits, shift, signedp, extbits, pcrel, extu, branch;
786
787         shift = 0;
788         signedp = 0;
789         extbits = 16;
790         pcrel = 0;
791         extu = 0;
792         branch = 0;
793         switch (type)
794           {
795           case '<':
796             nbits = 3;
797             immed = (l >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
798             extbits = 5;
799             extu = 1;
800             break;
801           case '>':
802             nbits = 3;
803             immed = (l >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
804             extbits = 5;
805             extu = 1;
806             break;
807           case '[':
808             nbits = 3;
809             immed = (l >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
810             extbits = 6;
811             extu = 1;
812             break;
813           case ']':
814             nbits = 3;
815             immed = (l >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
816             extbits = 6;
817             extu = 1;
818             break;
819           case '4':
820             nbits = 4;
821             immed = (l >> MIPS16OP_SH_IMM4) & MIPS16OP_MASK_IMM4;
822             signedp = 1;
823             extbits = 15;
824             break;
825           case '5':
826             nbits = 5;
827             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
828             info->insn_type = dis_dref;
829             info->data_size = 1;
830             break;
831           case 'H':
832             nbits = 5;
833             shift = 1;
834             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
835             info->insn_type = dis_dref;
836             info->data_size = 2;
837             break;
838           case 'W':
839             nbits = 5;
840             shift = 2;
841             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
842             if ((op->pinfo & MIPS16_INSN_READ_PC) == 0
843                 && (op->pinfo & MIPS16_INSN_READ_SP) == 0)
844               {
845                 info->insn_type = dis_dref;
846                 info->data_size = 4;
847               }
848             break;
849           case 'D':
850             nbits = 5;
851             shift = 3;
852             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
853             info->insn_type = dis_dref;
854             info->data_size = 8;
855             break;
856           case 'j':
857             nbits = 5;
858             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
859             signedp = 1;
860             break;
861           case '6':
862             nbits = 6;
863             immed = (l >> MIPS16OP_SH_IMM6) & MIPS16OP_MASK_IMM6;
864             break;
865           case '8':
866             nbits = 8;
867             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
868             break;
869           case 'V':
870             nbits = 8;
871             shift = 2;
872             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
873             /* FIXME: This might be lw, or it might be addiu to $sp or
874                $pc.  We assume it's load.  */
875             info->insn_type = dis_dref;
876             info->data_size = 4;
877             break;
878           case 'C':
879             nbits = 8;
880             shift = 3;
881             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
882             info->insn_type = dis_dref;
883             info->data_size = 8;
884             break;
885           case 'U':
886             nbits = 8;
887             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
888             extu = 1;
889             break;
890           case 'k':
891             nbits = 8;
892             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
893             signedp = 1;
894             break;
895           case 'K':
896             nbits = 8;
897             shift = 3;
898             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
899             signedp = 1;
900             break;
901           case 'p':
902             nbits = 8;
903             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
904             signedp = 1;
905             pcrel = 1;
906             branch = 1;
907             info->insn_type = dis_condbranch;
908             break;
909           case 'q':
910             nbits = 11;
911             immed = (l >> MIPS16OP_SH_IMM11) & MIPS16OP_MASK_IMM11;
912             signedp = 1;
913             pcrel = 1;
914             branch = 1;
915             info->insn_type = dis_branch;
916             break;
917           case 'A':
918             nbits = 8;
919             shift = 2;
920             immed = (l >> MIPS16OP_SH_IMM8) & MIPS16OP_MASK_IMM8;
921             pcrel = 1;
922             /* FIXME: This can be lw or la.  We assume it is lw.  */
923             info->insn_type = dis_dref;
924             info->data_size = 4;
925             break;
926           case 'B':
927             nbits = 5;
928             shift = 3;
929             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
930             pcrel = 1;
931             info->insn_type = dis_dref;
932             info->data_size = 8;
933             break;
934           case 'E':
935             nbits = 5;
936             shift = 2;
937             immed = (l >> MIPS16OP_SH_IMM5) & MIPS16OP_MASK_IMM5;
938             pcrel = 1;
939             break;
940           default:
941             abort ();
942           }
943
944         if (! use_extend)
945           {
946             if (signedp && immed >= (1 << (nbits - 1)))
947               immed -= 1 << nbits;
948             immed <<= shift;
949             if ((type == '<' || type == '>' || type == '[' || type == ']')
950                 && immed == 0)
951               immed = 8;
952           }
953         else
954           {
955             if (extbits == 16)
956               immed |= ((extend & 0x1f) << 11) | (extend & 0x7e0);
957             else if (extbits == 15)
958               immed |= ((extend & 0xf) << 11) | (extend & 0x7f0);
959             else
960               immed = ((extend >> 6) & 0x1f) | (extend & 0x20);
961             immed &= (1 << extbits) - 1;
962             if (! extu && immed >= (1 << (extbits - 1)))
963               immed -= 1 << extbits;
964           }
965
966         if (! pcrel)
967           (*info->fprintf_func) (info->stream, "%d", immed);
968         else
969           {
970             bfd_vma baseaddr;
971             bfd_vma val;
972
973             if (branch)
974               {
975                 immed *= 2;
976                 baseaddr = memaddr + 2;
977               }
978             else if (use_extend)
979               baseaddr = memaddr - 2;
980             else
981               {
982                 int status;
983                 bfd_byte buffer[2];
984
985                 baseaddr = memaddr;
986
987                 /* If this instruction is in the delay slot of a jr
988                    instruction, the base address is the address of the
989                    jr instruction.  If it is in the delay slot of jalr
990                    instruction, the base address is the address of the
991                    jalr instruction.  This test is unreliable: we have
992                    no way of knowing whether the previous word is
993                    instruction or data.  */
994                 status = (*info->read_memory_func) (memaddr - 4, buffer, 2,
995                                                     info);
996                 if (status == 0
997                     && (((info->endian == BFD_ENDIAN_BIG
998                           ? bfd_getb16 (buffer)
999                           : bfd_getl16 (buffer))
1000                          & 0xf800) == 0x1800))
1001                   baseaddr = memaddr - 4;
1002                 else
1003                   {
1004                     status = (*info->read_memory_func) (memaddr - 2, buffer,
1005                                                         2, info);
1006                     if (status == 0
1007                         && (((info->endian == BFD_ENDIAN_BIG
1008                               ? bfd_getb16 (buffer)
1009                               : bfd_getl16 (buffer))
1010                              & 0xf81f) == 0xe800))
1011                       baseaddr = memaddr - 2;
1012                   }
1013               }
1014             val = (baseaddr & ~ ((1 << shift) - 1)) + immed;
1015             (*info->print_address_func) (val, info);
1016             info->target = val;
1017           }
1018       }
1019       break;
1020
1021     case 'a':
1022       if (! use_extend)
1023         extend = 0;
1024       l = ((l & 0x1f) << 23) | ((l & 0x3e0) << 13) | (extend << 2);
1025       (*info->print_address_func) ((memaddr & 0xf0000000) | l, info);
1026       info->insn_type = dis_jsr;
1027       info->target = (memaddr & 0xf0000000) | l;
1028       info->branch_delay_insns = 1;
1029       break;
1030
1031     case 'l':
1032     case 'L':
1033       {
1034         int need_comma, amask, smask;
1035
1036         need_comma = 0;
1037
1038         l = (l >> MIPS16OP_SH_IMM6) & MIPS16OP_MASK_IMM6;
1039
1040         amask = (l >> 3) & 7;
1041
1042         if (amask > 0 && amask < 5)
1043           {
1044             (*info->fprintf_func) (info->stream, "$%s", reg_names[4]);
1045             if (amask > 1)
1046               (*info->fprintf_func) (info->stream, "-$%s",
1047                                      reg_names[amask + 3]);
1048             need_comma = 1;
1049           }
1050
1051         smask = (l >> 1) & 3;
1052         if (smask == 3)
1053           {
1054             (*info->fprintf_func) (info->stream, "%s??",
1055                                    need_comma ? "," : "");
1056             need_comma = 1;
1057           }
1058         else if (smask > 0)
1059           {
1060             (*info->fprintf_func) (info->stream, "%s$%s",
1061                                    need_comma ? "," : "",
1062                                    reg_names[16]);
1063             if (smask > 1)
1064               (*info->fprintf_func) (info->stream, "-$%s",
1065                                      reg_names[smask + 15]);
1066             need_comma = 1;
1067           }
1068
1069         if (l & 1)
1070           {
1071             (*info->fprintf_func) (info->stream, "%s$%s",
1072                                    need_comma ? "," : "",
1073                                    reg_names[31]);
1074             need_comma = 1;
1075           }
1076
1077         if (amask == 5 || amask == 6)
1078           {
1079             (*info->fprintf_func) (info->stream, "%s$f0",
1080                                    need_comma ? "," : "");
1081             if (amask == 6)
1082               (*info->fprintf_func) (info->stream, "-$f1");
1083           }
1084       }
1085       break;
1086
1087     default:
1088       abort ();
1089     }
1090 }