Mon Jan 20 12:48:57 1997 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[external/binutils.git] / opcodes / m68k-dis.c
1 /* Print Motorola 68k instructions.
2    Copyright 1986, 87, 89, 91, 92, 93, 94, 95, 96, 1997
3    Free Software Foundation, Inc.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #include "dis-asm.h"
20 #include "floatformat.h"
21 #include <libiberty.h>
22
23 #include "opcode/m68k.h"
24
25 /* Local function prototypes */
26
27 static int
28 fetch_arg PARAMS ((unsigned char *, int, int, disassemble_info *));
29
30 static void
31 print_base PARAMS ((int, bfd_vma, disassemble_info*));
32
33 static unsigned char *
34 print_indexed PARAMS ((int, unsigned char *, bfd_vma, disassemble_info *));
35
36 static int
37 print_insn_arg PARAMS ((const char *, unsigned char *, unsigned char *,
38                         bfd_vma, disassemble_info *));
39
40 CONST char * CONST fpcr_names[] = {
41   "", "%fpiar", "%fpsr", "%fpiar/%fpsr", "%fpcr",
42   "%fpiar/%fpcr", "%fpsr/%fpcr", "%fpiar/%fpsr/%fpcr"};
43
44 static char *const reg_names[] = {
45   "%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7",
46   "%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%fp", "%sp",
47   "%ps", "%pc"};
48
49 /* Sign-extend an (unsigned char). */
50 #if __STDC__ == 1
51 #define COERCE_SIGNED_CHAR(ch) ((signed char)(ch))
52 #else
53 #define COERCE_SIGNED_CHAR(ch) ((int)(((ch) ^ 0x80) & 0xFF) - 128)
54 #endif
55
56 /* Get a 1 byte signed integer.  */
57 #define NEXTBYTE(p)  (p += 2, FETCH_DATA (info, p), COERCE_SIGNED_CHAR(p[-1]))
58
59 /* Get a 2 byte signed integer.  */
60 #define COERCE16(x) ((int) (((x) ^ 0x8000) - 0x8000))
61 #define NEXTWORD(p)  \
62   (p += 2, FETCH_DATA (info, p), \
63    COERCE16 ((p[-2] << 8) + p[-1]))
64
65 /* Get a 4 byte signed integer.  */
66 #define COERCE32(x) ((int) (((x) ^ 0x80000000) - 0x80000000))
67 #define NEXTLONG(p)  \
68   (p += 4, FETCH_DATA (info, p), \
69    (COERCE32 ((((((p[-4] << 8) + p[-3]) << 8) + p[-2]) << 8) + p[-1])))
70
71 /* NEXTSINGLE and NEXTDOUBLE handle alignment problems, but not
72  * byte-swapping or other float format differences.  FIXME! */
73
74 union number {
75     double d;
76     float f;
77     char c[10];
78 };
79
80 #define NEXTSINGLE(val, p) \
81   { unsigned int i; union number u;\
82     FETCH_DATA (info, p + sizeof (float));\
83     for (i = 0; i < sizeof(float); i++) u.c[i] = *p++; \
84     val = u.f; }
85
86 #define NEXTDOUBLE(val, p) \
87   { unsigned int i; union number u;\
88     FETCH_DATA (info, p + sizeof (double));\
89     for (i = 0; i < sizeof(double); i++) u.c[i] = *p++; \
90     val = u.d; }
91
92 /* Need a function to convert from extended to double precision... */
93 #define NEXTEXTEND(p) \
94   (p += 12, FETCH_DATA (info, p), 0.0)
95
96 /* Need a function to convert from packed to double
97    precision.   Actually, it's easier to print a
98    packed number than a double anyway, so maybe
99    there should be a special case to handle this... */
100 #define NEXTPACKED(p) \
101   (p += 12, FETCH_DATA (info, p), 0.0)
102
103 \f
104 /* Maximum length of an instruction.  */
105 #define MAXLEN 22
106
107 #include <setjmp.h>
108
109 struct private
110 {
111   /* Points to first byte not fetched.  */
112   bfd_byte *max_fetched;
113   bfd_byte the_buffer[MAXLEN];
114   bfd_vma insn_start;
115   jmp_buf bailout;
116 };
117
118 /* Make sure that bytes from INFO->PRIVATE_DATA->BUFFER (inclusive)
119    to ADDR (exclusive) are valid.  Returns 1 for success, longjmps
120    on error.  */
121 #define FETCH_DATA(info, addr) \
122   ((addr) <= ((struct private *)(info->private_data))->max_fetched \
123    ? 1 : fetch_data ((info), (addr)))
124
125 static int
126 fetch_data (info, addr)
127      struct disassemble_info *info;
128      bfd_byte *addr;
129 {
130   int status;
131   struct private *priv = (struct private *)info->private_data;
132   bfd_vma start = priv->insn_start + (priv->max_fetched - priv->the_buffer);
133
134   status = (*info->read_memory_func) (start,
135                                       priv->max_fetched,
136                                       addr - priv->max_fetched,
137                                       info);
138   if (status != 0)
139     {
140       (*info->memory_error_func) (status, start, info);
141       longjmp (priv->bailout, 1);
142     }
143   else
144     priv->max_fetched = addr;
145   return 1;
146 }
147 \f
148 /* This function is used to print to the bit-bucket. */
149 static int
150 #ifdef __STDC__
151 dummy_printer (FILE * file, const char * format, ...)
152 #else
153 dummy_printer (file) FILE *file;
154 #endif
155  { return 0; }
156
157 void
158 dummy_print_address (vma, info)
159      bfd_vma vma;
160      struct disassemble_info *info;
161 {
162 }
163
164 /* Print the m68k instruction at address MEMADDR in debugged memory,
165    on INFO->STREAM.  Returns length of the instruction, in bytes.  */
166
167 int
168 print_insn_m68k (memaddr, info)
169      bfd_vma memaddr;
170      disassemble_info *info;
171 {
172   register int i;
173   register unsigned char *p;
174   unsigned char *save_p;
175   register const char *d;
176   register unsigned long bestmask;
177   const struct m68k_opcode *best = 0;
178   struct private priv;
179   bfd_byte *buffer = priv.the_buffer;
180   fprintf_ftype save_printer = info->fprintf_func;
181   void (*save_print_address) PARAMS((bfd_vma, struct disassemble_info*))
182     = info->print_address_func;
183   int major_opcode;
184   static int numopcodes[16];
185   static const struct m68k_opcode **opcodes[16];
186
187   if (!opcodes[0])
188     {
189       /* Speed up the matching by sorting the opcode table on the upper
190          four bits of the opcode.  */
191       const struct m68k_opcode **opc_pointer[16];
192
193       /* First count how many opcodes are in each of the sixteen buckets.  */
194       for (i = 0; i < m68k_numopcodes; i++)
195         numopcodes[(m68k_opcodes[i].opcode >> 28) & 15]++;
196
197       /* Then create a sorted table of pointers that point into the
198          unsorted table.  */
199       opc_pointer[0] = ((const struct m68k_opcode **)
200                         xmalloc (sizeof (struct m68k_opcode *)
201                                  * m68k_numopcodes));
202       opcodes[0] = opc_pointer[0];
203       for (i = 1; i < 16; i++)
204         {
205           opc_pointer[i] = opc_pointer[i - 1] + numopcodes[i - 1];
206           opcodes[i] = opc_pointer[i];
207         }
208
209       for (i = 0; i < m68k_numopcodes; i++)
210         *opc_pointer[(m68k_opcodes[i].opcode >> 28) & 15]++ = &m68k_opcodes[i];
211
212     }
213
214   info->private_data = (PTR) &priv;
215   priv.max_fetched = priv.the_buffer;
216   priv.insn_start = memaddr;
217   if (setjmp (priv.bailout) != 0)
218     /* Error return.  */
219     return -1;
220
221   bestmask = 0;
222   FETCH_DATA (info, buffer + 2);
223   major_opcode = (buffer[0] >> 4) & 15;
224   for (i = 0; i < numopcodes[major_opcode]; i++)
225     {
226       const struct m68k_opcode *opc = opcodes[major_opcode][i];
227       unsigned long opcode = opc->opcode;
228       unsigned long match = opc->match;
229
230       if (((0xff & buffer[0] & (match >> 24)) == (0xff & (opcode >> 24)))
231           && ((0xff & buffer[1] & (match >> 16)) == (0xff & (opcode >> 16)))
232           /* Only fetch the next two bytes if we need to.  */
233           && (((0xffff & match) == 0)
234               ||
235               (FETCH_DATA (info, buffer + 4)
236                && ((0xff & buffer[2] & (match >> 8)) == (0xff & (opcode >> 8)))
237                && ((0xff & buffer[3] & match) == (0xff & opcode)))
238               ))
239         {
240           /* Don't use for printout the variants of divul and divsl
241              that have the same register number in two places.
242              The more general variants will match instead.  */
243           for (d = opc->args; *d; d += 2)
244             if (d[1] == 'D')
245               break;
246
247           /* Don't use for printout the variants of most floating
248              point coprocessor instructions which use the same
249              register number in two places, as above. */
250           if (*d == '\0')
251             for (d = opc->args; *d; d += 2)
252               if (d[1] == 't')
253                 break;
254
255           /* Don't match fmovel with more than one register; wait for
256              fmoveml.  */
257           if (*d == '\0')
258             {
259               for (d = opc->args; *d; d += 2)
260                 {
261                   if (d[0] == 's' && d[1] == '8')
262                     {
263                       int val;
264
265                       val = fetch_arg (buffer, d[1], 3, info);
266                       if ((val & (val - 1)) != 0)
267                         break;
268                     }
269                 }
270             }
271
272           if (*d == '\0' && match > bestmask)
273             {
274               best = opc;
275               bestmask = match;
276             }
277         }
278     }
279
280   if (best == 0)
281     goto invalid;
282
283   /* Point at first word of argument data,
284      and at descriptor for first argument.  */
285   p = buffer + 2;
286   
287   /* Figure out how long the fixed-size portion of the instruction is.
288      The only place this is stored in the opcode table is
289      in the arguments--look for arguments which specify fields in the 2nd
290      or 3rd words of the instruction.  */
291   for (d = best->args; *d; d += 2)
292     {
293       /* I don't think it is necessary to be checking d[0] here; I suspect
294          all this could be moved to the case statement below.  */
295       if (d[0] == '#')
296         {
297           if (d[1] == 'l' && p - buffer < 6)
298             p = buffer + 6;
299           else if (p - buffer < 4 && d[1] != 'C' && d[1] != '8' )
300             p = buffer + 4;
301         }
302       if ((d[0] == 'L' || d[0] == 'l') && d[1] == 'w' && p - buffer < 4)
303         p = buffer + 4;
304       switch (d[1])
305         {
306         case '1':
307         case '2':
308         case '3':
309         case '7':
310         case '8':
311         case '9':
312         case 'i':
313           if (p - buffer < 4)
314             p = buffer + 4;
315           break;
316         case '4':
317         case '5':
318         case '6':
319           if (p - buffer < 6)
320             p = buffer + 6;
321           break;
322         default:
323           break;
324         }
325     }
326   /* Some opcodes like pflusha and lpstop are exceptions; they take no
327      arguments but are two words long.  Recognize them by looking at
328      the lower 16 bits of the mask.  */
329   if (p - buffer < 4 && (best->match & 0xFFFF) != 0)
330     p = buffer + 4;
331   
332   FETCH_DATA (info, p);
333   
334   d = best->args;
335
336   /* We can the operands twice.  The first time we don't print anything,
337      but look for errors. */
338
339   save_p = p;
340   info->print_address_func = dummy_print_address;
341   info->fprintf_func = (fprintf_ftype)dummy_printer;
342   for ( ; *d; d += 2)
343     {
344       int eaten = print_insn_arg (d, buffer, p, memaddr + p - buffer, info);
345       if (eaten >= 0)
346         p += eaten;
347       else if (eaten == -1)
348         goto invalid;
349       else
350         {
351           (*info->fprintf_func)(info->stream,
352                                 "<internal error in opcode table: %s %s>\n",
353                                 best->name,
354                                 best->args);
355           goto invalid;
356         }
357
358     }
359   p = save_p;
360   info->fprintf_func = save_printer;
361   info->print_address_func = save_print_address;
362
363   d = best->args;
364
365   (*info->fprintf_func) (info->stream, "%s", best->name);
366
367   if (*d)
368     (*info->fprintf_func) (info->stream, " ");
369
370   while (*d)
371     {
372       p += print_insn_arg (d, buffer, p, memaddr + p - buffer, info);
373       d += 2;
374       if (*d && *(d - 2) != 'I' && *d != 'k')
375         (*info->fprintf_func) (info->stream, ",");
376     }
377   return p - buffer;
378
379  invalid:
380   /* Handle undefined instructions.  */
381   info->fprintf_func = save_printer;
382   info->print_address_func = save_print_address;
383   (*info->fprintf_func) (info->stream, "0%o",
384                          (buffer[0] << 8) + buffer[1]);
385   return 2;
386 }
387
388 /* Returns number of bytes "eaten" by the operand, or
389    return -1 if an invalid operand was found, or -2 if
390    an opcode tabe error was found. */
391
392 static int
393 print_insn_arg (d, buffer, p0, addr, info)
394      const char *d;
395      unsigned char *buffer;
396      unsigned char *p0;
397      bfd_vma addr;              /* PC for this arg to be relative to */
398      disassemble_info *info;
399 {
400   register int val = 0;
401   register int place = d[1];
402   register unsigned char *p = p0;
403   int regno;
404   register CONST char *regname;
405   register unsigned char *p1;
406   double flval;
407   int flt_p;
408
409   switch (*d)
410     {
411     case 'c':           /* cache identifier */
412       {
413         static char *const cacheFieldName[] = { "nc", "dc", "ic", "bc" };
414         val = fetch_arg (buffer, place, 2, info);
415         (*info->fprintf_func) (info->stream, cacheFieldName[val]);
416         break;
417       }
418
419     case 'a':           /* address register indirect only. Cf. case '+'. */
420       {
421         (*info->fprintf_func)
422           (info->stream,
423            "%s@",
424            reg_names [fetch_arg (buffer, place, 3, info) + 8]);
425         break;
426       }
427
428     case '_':           /* 32-bit absolute address for move16. */
429       {
430         val = NEXTLONG (p);
431         (*info->print_address_func) (val, info);
432         break;
433       }
434
435     case 'C':
436       (*info->fprintf_func) (info->stream, "%%ccr");
437       break;
438
439     case 'S':
440       (*info->fprintf_func) (info->stream, "%%sr");
441       break;
442
443     case 'U':
444       (*info->fprintf_func) (info->stream, "%%usp");
445       break;
446
447     case 'J':
448       {
449         static const struct { char *name; int value; } names[]
450           = {{"%sfc", 0x000}, {"%dfc", 0x001}, {"%cacr", 0x002},
451              {"%tc",  0x003}, {"%itt0",0x004}, {"%itt1", 0x005},
452              {"%dtt0",0x006}, {"%dtt1",0x007}, {"%buscr",0x008},
453              {"%usp", 0x800}, {"%vbr", 0x801}, {"%caar", 0x802},
454              {"%msp", 0x803}, {"%ibsp", 0x804},
455
456              /* Should we be calling this psr like we do in case 'Y'?  */
457              {"%mmusr",0x805},
458
459              {"%urp", 0x806}, {"%srp", 0x807}, {"%pcr", 0x808}};
460
461         val = fetch_arg (buffer, place, 12, info);
462         for (regno = sizeof names / sizeof names[0] - 1; regno >= 0; regno--)
463           if (names[regno].value == val)
464             {
465               (*info->fprintf_func) (info->stream, "%s", names[regno].name);
466               break;
467             }
468         if (regno < 0)
469           (*info->fprintf_func) (info->stream, "%d", val);
470       }
471       break;
472
473     case 'Q':
474       val = fetch_arg (buffer, place, 3, info);
475       /* 0 means 8, except for the bkpt instruction... */
476       if (val == 0 && d[1] != 's')
477         val = 8;
478       (*info->fprintf_func) (info->stream, "#%d", val);
479       break;
480
481     case 'M':
482       val = fetch_arg (buffer, place, 8, info);
483       if (val & 0x80)
484         val = val - 0x100;
485       (*info->fprintf_func) (info->stream, "#%d", val);
486       break;
487
488     case 'T':
489       val = fetch_arg (buffer, place, 4, info);
490       (*info->fprintf_func) (info->stream, "#%d", val);
491       break;
492
493     case 'D':
494       (*info->fprintf_func) (info->stream, "%s",
495                              reg_names[fetch_arg (buffer, place, 3, info)]);
496       break;
497
498     case 'A':
499       (*info->fprintf_func)
500         (info->stream, "%s",
501          reg_names[fetch_arg (buffer, place, 3, info) + 010]);
502       break;
503
504     case 'R':
505       (*info->fprintf_func)
506         (info->stream, "%s",
507          reg_names[fetch_arg (buffer, place, 4, info)]);
508       break;
509
510     case 'r':
511       regno = fetch_arg (buffer, place, 4, info);
512       if (regno > 7)
513         (*info->fprintf_func) (info->stream, "%s@", reg_names[regno]);
514       else
515         (*info->fprintf_func) (info->stream, "@(%s)", reg_names[regno]);
516       break;
517
518     case 'F':
519       (*info->fprintf_func)
520         (info->stream, "%%fp%d",
521          fetch_arg (buffer, place, 3, info));
522       break;
523
524     case 'O':
525       val = fetch_arg (buffer, place, 6, info);
526       if (val & 0x20)
527         (*info->fprintf_func) (info->stream, "%s", reg_names [val & 7]);
528       else
529         (*info->fprintf_func) (info->stream, "%d", val);
530       break;
531
532     case '+':
533       (*info->fprintf_func)
534         (info->stream, "%s@+",
535          reg_names[fetch_arg (buffer, place, 3, info) + 8]);
536       break;
537
538     case '-':
539       (*info->fprintf_func)
540         (info->stream, "%s@-",
541          reg_names[fetch_arg (buffer, place, 3, info) + 8]);
542       break;
543
544     case 'k':
545       if (place == 'k')
546         (*info->fprintf_func)
547           (info->stream, "{%s}",
548            reg_names[fetch_arg (buffer, place, 3, info)]);
549       else if (place == 'C')
550         {
551           val = fetch_arg (buffer, place, 7, info);
552           if ( val > 63 )               /* This is a signed constant. */
553             val -= 128;
554           (*info->fprintf_func) (info->stream, "{#%d}", val);
555         }
556       else
557         return -2;
558       break;
559
560     case '#':
561     case '^':
562       p1 = buffer + (*d == '#' ? 2 : 4);
563       if (place == 's')
564         val = fetch_arg (buffer, place, 4, info);
565       else if (place == 'C')
566         val = fetch_arg (buffer, place, 7, info);
567       else if (place == '8')
568         val = fetch_arg (buffer, place, 3, info);
569       else if (place == '3')
570         val = fetch_arg (buffer, place, 8, info);
571       else if (place == 'b')
572         val = NEXTBYTE (p1);
573       else if (place == 'w' || place == 'W')
574         val = NEXTWORD (p1);
575       else if (place == 'l')
576         val = NEXTLONG (p1);
577       else
578         return -2;
579       (*info->fprintf_func) (info->stream, "#%d", val);
580       break;
581
582     case 'B':
583       if (place == 'b')
584         val = NEXTBYTE (p);
585       else if (place == 'B')
586         val = COERCE_SIGNED_CHAR(buffer[1]);
587       else if (place == 'w' || place == 'W')
588         val = NEXTWORD (p);
589       else if (place == 'l' || place == 'L' || place == 'C')
590         val = NEXTLONG (p);
591       else if (place == 'g')
592         {
593           val = NEXTBYTE (buffer);
594           if (val == 0)
595             val = NEXTWORD (p);
596           else if (val == -1)
597             val = NEXTLONG (p);
598         }
599       else if (place == 'c')
600         {
601           if (buffer[1] & 0x40)         /* If bit six is one, long offset */
602             val = NEXTLONG (p);
603           else
604             val = NEXTWORD (p);
605         }
606       else
607         return -2;
608
609       (*info->print_address_func) (addr + val, info);
610       break;
611
612     case 'd':
613       val = NEXTWORD (p);
614       (*info->fprintf_func)
615         (info->stream, "%s@(%d)",
616          reg_names[fetch_arg (buffer, place, 3, info)], val);
617       break;
618
619     case 's':
620       (*info->fprintf_func) (info->stream, "%s",
621                              fpcr_names[fetch_arg (buffer, place, 3, info)]);
622       break;
623
624     case 'I':
625       /* Get coprocessor ID... */
626       val = fetch_arg (buffer, 'd', 3, info);
627       
628       if (val != 1)                             /* Unusual coprocessor ID? */
629         (*info->fprintf_func) (info->stream, "(cpid=%d) ", val);
630       break;
631
632     case '*':
633     case '~':
634     case '%':
635     case ';':
636     case '@':
637     case '!':
638     case '$':
639     case '?':
640     case '/':
641     case '&':
642     case '`':
643     case '|':
644     case '<':
645     case '>':
646
647       if (place == 'd')
648         {
649           val = fetch_arg (buffer, 'x', 6, info);
650           val = ((val & 7) << 3) + ((val >> 3) & 7);
651         }
652       else
653         val = fetch_arg (buffer, 's', 6, info);
654
655       /* Get register number assuming address register.  */
656       regno = (val & 7) + 8;
657       regname = reg_names[regno];
658       switch (val >> 3)
659         {
660         case 0:
661           (*info->fprintf_func) (info->stream, "%s", reg_names[val]);
662           break;
663
664         case 1:
665           (*info->fprintf_func) (info->stream, "%s", regname);
666           break;
667
668         case 2:
669           (*info->fprintf_func) (info->stream, "%s@", regname);
670           break;
671
672         case 3:
673           (*info->fprintf_func) (info->stream, "%s@+", regname);
674           break;
675
676         case 4:
677           (*info->fprintf_func) (info->stream, "%s@-", regname);
678           break;
679
680         case 5:
681           val = NEXTWORD (p);
682           (*info->fprintf_func) (info->stream, "%s@(%d)", regname, val);
683           break;
684
685         case 6:
686           p = print_indexed (regno, p, addr, info);
687           break;
688
689         case 7:
690           switch (val & 7)
691             {
692             case 0:
693               val = NEXTWORD (p);
694               (*info->print_address_func) (val, info);
695               break;
696
697             case 1:
698               val = NEXTLONG (p);
699               (*info->print_address_func) (val, info);
700               break;
701
702             case 2:
703               val = NEXTWORD (p);
704               (*info->print_address_func) (addr + val, info);
705               break;
706
707             case 3:
708               p = print_indexed (-1, p, addr, info);
709               break;
710
711             case 4:
712               flt_p = 1;        /* Assume it's a float... */
713               switch( place )
714               {
715                 case 'b':
716                   val = NEXTBYTE (p);
717                   flt_p = 0;
718                   break;
719
720                 case 'w':
721                   val = NEXTWORD (p);
722                   flt_p = 0;
723                   break;
724
725                 case 'l':
726                   val = NEXTLONG (p);
727                   flt_p = 0;
728                   break;
729
730                 case 'f':
731                   NEXTSINGLE(flval, p);
732                   break;
733
734                 case 'F':
735                   NEXTDOUBLE(flval, p);
736                   break;
737
738                 case 'x':
739                   FETCH_DATA (info, p + 12);
740                   floatformat_to_double (&floatformat_m68881_ext,
741                                          (char *) p, &flval);
742                   p += 12;
743                   break;
744
745                 case 'p':
746                   flval = NEXTPACKED(p);
747                   break;
748
749                 default:
750                   return -1;
751               }
752               if ( flt_p )      /* Print a float? */
753                 (*info->fprintf_func) (info->stream, "#%g", flval);
754               else
755                 (*info->fprintf_func) (info->stream, "#%d", val);
756               break;
757
758             default:
759               return -1;
760             }
761         }
762       break;
763
764     case 'L':
765     case 'l':
766         if (place == 'w')
767           {
768             char doneany;
769             p1 = buffer + 2;
770             val = NEXTWORD (p1);
771             /* Move the pointer ahead if this point is farther ahead
772                than the last.  */
773             p = p1 > p ? p1 : p;
774             if (val == 0)
775               {
776                 (*info->fprintf_func) (info->stream, "#0");
777                 break;
778               }
779             if (*d == 'l')
780               {
781                 register int newval = 0;
782                 for (regno = 0; regno < 16; ++regno)
783                   if (val & (0x8000 >> regno))
784                     newval |= 1 << regno;
785                 val = newval;
786               }
787             val &= 0xffff;
788             doneany = 0;
789             for (regno = 0; regno < 16; ++regno)
790               if (val & (1 << regno))
791                 {
792                   int first_regno;
793                   if (doneany)
794                     (*info->fprintf_func) (info->stream, "/");
795                   doneany = 1;
796                   (*info->fprintf_func) (info->stream, "%s", reg_names[regno]);
797                   first_regno = regno;
798                   while (val & (1 << (regno + 1)))
799                     ++regno;
800                   if (regno > first_regno)
801                     (*info->fprintf_func) (info->stream, "-%s",
802                                            reg_names[regno]);
803                 }
804           }
805         else if (place == '3')
806           {
807             /* `fmovem' insn.  */
808             char doneany;
809             val = fetch_arg (buffer, place, 8, info);
810             if (val == 0)
811               {
812                 (*info->fprintf_func) (info->stream, "#0");
813                 break;
814               }
815             if (*d == 'l')
816               {
817                 register int newval = 0;
818                 for (regno = 0; regno < 8; ++regno)
819                   if (val & (0x80 >> regno))
820                     newval |= 1 << regno;
821                 val = newval;
822               }
823             val &= 0xff;
824             doneany = 0;
825             for (regno = 0; regno < 8; ++regno)
826               if (val & (1 << regno))
827                 {
828                   int first_regno;
829                   if (doneany)
830                     (*info->fprintf_func) (info->stream, "/");
831                   doneany = 1;
832                   (*info->fprintf_func) (info->stream, "%%fp%d", regno);
833                   first_regno = regno;
834                   while (val & (1 << (regno + 1)))
835                     ++regno;
836                   if (regno > first_regno)
837                     (*info->fprintf_func) (info->stream, "-%%fp%d", regno);
838                 }
839           }
840         else if (place == '8')
841           {
842             /* fmoveml for FP status registers */
843             (*info->fprintf_func) (info->stream, "%s",
844                                    fpcr_names[fetch_arg (buffer, place, 3,
845                                                          info)]);
846           }
847         else
848           return -2;
849       break;
850
851     case 'X':
852       place = '8';
853     case 'Y':
854     case 'Z':
855     case 'W':
856     case '0':
857     case '1':
858     case '2':
859     case '3':
860       {
861         int val = fetch_arg (buffer, place, 5, info);
862         char *name = 0;
863         switch (val)
864           {
865           case 2: name = "%tt0"; break;
866           case 3: name = "%tt1"; break;
867           case 0x10: name = "%tc"; break;
868           case 0x11: name = "%drp"; break;
869           case 0x12: name = "%srp"; break;
870           case 0x13: name = "%crp"; break;
871           case 0x14: name = "%cal"; break;
872           case 0x15: name = "%val"; break;
873           case 0x16: name = "%scc"; break;
874           case 0x17: name = "%ac"; break;
875           case 0x18: name = "%psr"; break;
876           case 0x19: name = "%pcsr"; break;
877           case 0x1c:
878           case 0x1d:
879             {
880               int break_reg = ((buffer[3] >> 2) & 7);
881               (*info->fprintf_func)
882                 (info->stream, val == 0x1c ? "%%bad%d" : "%%bac%d",
883                  break_reg);
884             }
885             break;
886           default:
887             (*info->fprintf_func) (info->stream, "<mmu register %d>", val);
888           }
889         if (name)
890           (*info->fprintf_func) (info->stream, "%s", name);
891       }
892       break;
893
894     case 'f':
895       {
896         int fc = fetch_arg (buffer, place, 5, info);
897         if (fc == 1)
898           (*info->fprintf_func) (info->stream, "%%dfc");
899         else if (fc == 0)
900           (*info->fprintf_func) (info->stream, "%%sfc");
901         else
902           (*info->fprintf_func) (info->stream, "<function code %d>", fc);
903       }
904       break;
905
906     case 'V':
907       (*info->fprintf_func) (info->stream, "%%val");
908       break;
909
910     case 't':
911       {
912         int level = fetch_arg (buffer, place, 3, info);
913         (*info->fprintf_func) (info->stream, "%d", level);
914       }
915       break;
916
917     default:
918       return -2;
919     }
920
921   return p - p0;
922 }
923
924 /* Fetch BITS bits from a position in the instruction specified by CODE.
925    CODE is a "place to put an argument", or 'x' for a destination
926    that is a general address (mode and register).
927    BUFFER contains the instruction.  */
928
929 static int
930 fetch_arg (buffer, code, bits, info)
931      unsigned char *buffer;
932      int code;
933      int bits;
934      disassemble_info *info;
935 {
936   register int val = 0;
937   switch (code)
938     {
939     case 's':
940       val = buffer[1];
941       break;
942
943     case 'd':                   /* Destination, for register or quick.  */
944       val = (buffer[0] << 8) + buffer[1];
945       val >>= 9;
946       break;
947
948     case 'x':                   /* Destination, for general arg */
949       val = (buffer[0] << 8) + buffer[1];
950       val >>= 6;
951       break;
952
953     case 'k':
954       FETCH_DATA (info, buffer + 3);
955       val = (buffer[3] >> 4);
956       break;
957
958     case 'C':
959       FETCH_DATA (info, buffer + 3);
960       val = buffer[3];
961       break;
962
963     case '1':
964       FETCH_DATA (info, buffer + 3);
965       val = (buffer[2] << 8) + buffer[3];
966       val >>= 12;
967       break;
968
969     case '2':
970       FETCH_DATA (info, buffer + 3);
971       val = (buffer[2] << 8) + buffer[3];
972       val >>= 6;
973       break;
974
975     case '3':
976     case 'j':
977       FETCH_DATA (info, buffer + 3);
978       val = (buffer[2] << 8) + buffer[3];
979       break;
980
981     case '4':
982       FETCH_DATA (info, buffer + 5);
983       val = (buffer[4] << 8) + buffer[5];
984       val >>= 12;
985       break;
986
987     case '5':
988       FETCH_DATA (info, buffer + 5);
989       val = (buffer[4] << 8) + buffer[5];
990       val >>= 6;
991       break;
992
993     case '6':
994       FETCH_DATA (info, buffer + 5);
995       val = (buffer[4] << 8) + buffer[5];
996       break;
997
998     case '7':
999       FETCH_DATA (info, buffer + 3);
1000       val = (buffer[2] << 8) + buffer[3];
1001       val >>= 7;
1002       break;
1003       
1004     case '8':
1005       FETCH_DATA (info, buffer + 3);
1006       val = (buffer[2] << 8) + buffer[3];
1007       val >>= 10;
1008       break;
1009
1010     case '9':
1011       FETCH_DATA (info, buffer + 3);
1012       val = (buffer[2] << 8) + buffer[3];
1013       val >>= 5;
1014       break;
1015
1016     case 'e':
1017       val = (buffer[1] >> 6);
1018       break;
1019
1020     default:
1021       abort ();
1022     }
1023
1024   switch (bits)
1025     {
1026     case 2:
1027       return val & 3;
1028     case 3:
1029       return val & 7;
1030     case 4:
1031       return val & 017;
1032     case 5:
1033       return val & 037;
1034     case 6:
1035       return val & 077;
1036     case 7:
1037       return val & 0177;
1038     case 8:
1039       return val & 0377;
1040     case 12:
1041       return val & 07777;
1042     default:
1043       abort ();
1044     }
1045 }
1046
1047 /* Print an indexed argument.  The base register is BASEREG (-1 for pc).
1048    P points to extension word, in buffer.
1049    ADDR is the nominal core address of that extension word.  */
1050
1051 static unsigned char *
1052 print_indexed (basereg, p, addr, info)
1053      int basereg;
1054      unsigned char *p;
1055      bfd_vma addr;
1056      disassemble_info *info;
1057 {
1058   register int word;
1059   static char *const scales[] = {"", ":2", ":4", ":8"};
1060   bfd_vma base_disp;
1061   bfd_vma outer_disp;
1062   char buf[40];
1063   char vmabuf[50];
1064
1065   word = NEXTWORD (p);
1066
1067   /* Generate the text for the index register.
1068      Where this will be output is not yet determined.  */
1069   sprintf (buf, "%s:%c%s",
1070            reg_names[(word >> 12) & 0xf],
1071            (word & 0x800) ? 'l' : 'w',
1072            scales[(word >> 9) & 3]);
1073
1074   /* Handle the 68000 style of indexing.  */
1075
1076   if ((word & 0x100) == 0)
1077     {
1078       word &= 0xff;
1079       if ((word & 0x80) != 0)
1080         word -= 0x100;
1081       if (basereg == -1)
1082         word += addr;
1083       print_base (basereg, word, info);
1084       (*info->fprintf_func) (info->stream, ",%s)", buf);
1085       return p;
1086     }
1087
1088   /* Handle the generalized kind.  */
1089   /* First, compute the displacement to add to the base register.  */
1090
1091   if (word & 0200)
1092     {
1093       if (basereg == -1)
1094         basereg = -3;
1095       else
1096         basereg = -2;
1097     }
1098   if (word & 0100)
1099     buf[0] = '\0';
1100   base_disp = 0;
1101   switch ((word >> 4) & 3)
1102     {
1103     case 2:
1104       base_disp = NEXTWORD (p);
1105       break;
1106     case 3:
1107       base_disp = NEXTLONG (p);
1108     }
1109   if (basereg == -1)
1110     base_disp += addr;
1111
1112   /* Handle single-level case (not indirect) */
1113
1114   if ((word & 7) == 0)
1115     {
1116       print_base (basereg, base_disp, info);
1117       if (buf[0] != '\0')
1118         (*info->fprintf_func) (info->stream, ",%s", buf);
1119       (*info->fprintf_func) (info->stream, ")");
1120       return p;
1121     }
1122
1123   /* Two level.  Compute displacement to add after indirection.  */
1124
1125   outer_disp = 0;
1126   switch (word & 3)
1127     {
1128     case 2:
1129       outer_disp = NEXTWORD (p);
1130       break;
1131     case 3:
1132       outer_disp = NEXTLONG (p);
1133     }
1134
1135   print_base (basereg, base_disp, info);
1136   if ((word & 4) == 0 && buf[0] != '\0')
1137     {
1138       (*info->fprintf_func) (info->stream, ",%s", buf);
1139       buf[0] = '\0';
1140     }
1141   sprintf_vma (vmabuf, outer_disp);
1142   (*info->fprintf_func) (info->stream, ")@(%s", vmabuf);
1143   if (buf[0] != '\0')
1144     (*info->fprintf_func) (info->stream, ",%s", buf);
1145   (*info->fprintf_func) (info->stream, ")");
1146
1147   return p;
1148 }
1149
1150 /* Print a base register REGNO and displacement DISP, on INFO->STREAM.
1151    REGNO = -1 for pc, -2 for none (suppressed).  */
1152
1153 static void
1154 print_base (regno, disp, info)
1155      int regno;
1156      bfd_vma disp;
1157      disassemble_info *info;
1158 {
1159   if (regno == -1)
1160     {
1161       (*info->fprintf_func) (info->stream, "%%pc@(");
1162       (*info->print_address_func) (disp, info);
1163     }
1164   else
1165     {
1166       char buf[50];
1167
1168       if (regno == -2)
1169         (*info->fprintf_func) (info->stream, "@(");
1170       else if (regno == -3)
1171         (*info->fprintf_func) (info->stream, "%%zpc@(");
1172       else
1173         (*info->fprintf_func) (info->stream, "%s@(", reg_names[regno]);
1174
1175       sprintf_vma (buf, disp);
1176       (*info->fprintf_func) (info->stream, "%s", buf);
1177     }
1178 }