xref: /openbsd-src/gnu/usr.bin/binutils/opcodes/h8300-dis.c (revision 007c2a4539b8b8aaa95c5e73e77620090abe113b)
12159047fSniklas /* Disassemble h8300 instructions.
2*007c2a45Smiod    Copyright 1993, 1994, 1996, 1998, 2000, 2001, 2002, 2003
3c074d1c9Sdrahn    Free Software Foundation, Inc.
42159047fSniklas 
52159047fSniklas This program is free software; you can redistribute it and/or modify
62159047fSniklas it under the terms of the GNU General Public License as published by
72159047fSniklas the Free Software Foundation; either version 2 of the License, or
82159047fSniklas (at your option) any later version.
92159047fSniklas 
102159047fSniklas This program is distributed in the hope that it will be useful,
112159047fSniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
122159047fSniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
132159047fSniklas GNU General Public License for more details.
142159047fSniklas 
152159047fSniklas You should have received a copy of the GNU General Public License
162159047fSniklas along with this program; if not, write to the Free Software
172159047fSniklas Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
182159047fSniklas 
192159047fSniklas #define DEFINE_TABLE
202159047fSniklas 
21b305b0f1Sespie #include "sysdep.h"
222159047fSniklas #define h8_opcodes h8ops
232159047fSniklas #include "opcode/h8300.h"
242159047fSniklas #include "dis-asm.h"
25b305b0f1Sespie #include "opintl.h"
26c074d1c9Sdrahn #include "libiberty.h"
27c074d1c9Sdrahn 
28c074d1c9Sdrahn struct h8_instruction
29c074d1c9Sdrahn {
30c074d1c9Sdrahn   int length;
31c074d1c9Sdrahn   const struct h8_opcode *opcode;
32c074d1c9Sdrahn };
33c074d1c9Sdrahn 
34c074d1c9Sdrahn struct h8_instruction *h8_instructions;
35c074d1c9Sdrahn 
36c074d1c9Sdrahn static void bfd_h8_disassemble_init PARAMS ((void));
37*007c2a45Smiod static void print_one_arg PARAMS ((disassemble_info *, bfd_vma, op_type,
38*007c2a45Smiod 				   int, int, int, int, const char **, int));
39*007c2a45Smiod static unsigned int bfd_h8_disassemble PARAMS ((bfd_vma,
40*007c2a45Smiod 						disassemble_info *,
41*007c2a45Smiod 						int));
42*007c2a45Smiod static void extract_immediate PARAMS ((FILE *,
43*007c2a45Smiod 				       op_type, int,
44*007c2a45Smiod 				       unsigned char *,
45*007c2a45Smiod 				       int *, int *,
46*007c2a45Smiod 				       const struct h8_opcode *));
472159047fSniklas 
482159047fSniklas /* Run through the opcodes and sort them into order to make them easy
49b55d4692Sfgsch    to disassemble.  */
50*007c2a45Smiod 
512159047fSniklas static void
bfd_h8_disassemble_init()522159047fSniklas bfd_h8_disassemble_init ()
532159047fSniklas {
542159047fSniklas   unsigned int i;
55c074d1c9Sdrahn   unsigned int nopcodes;
56c074d1c9Sdrahn   const struct h8_opcode *p;
57c074d1c9Sdrahn   struct h8_instruction *pi;
582159047fSniklas 
59c074d1c9Sdrahn   nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode);
60c074d1c9Sdrahn 
61c074d1c9Sdrahn   h8_instructions = (struct h8_instruction *)
62c074d1c9Sdrahn     xmalloc (nopcodes * sizeof (struct h8_instruction));
63c074d1c9Sdrahn 
64c074d1c9Sdrahn   for (p = h8_opcodes, pi = h8_instructions; p->name; p++, pi++)
652159047fSniklas     {
662159047fSniklas       int n1 = 0;
672159047fSniklas       int n2 = 0;
682159047fSniklas 
692159047fSniklas       if ((int) p->data.nib[0] < 16)
702159047fSniklas 	n1 = (int) p->data.nib[0];
712159047fSniklas       else
722159047fSniklas 	n1 = 0;
73b55d4692Sfgsch 
742159047fSniklas       if ((int) p->data.nib[1] < 16)
752159047fSniklas 	n2 = (int) p->data.nib[1];
762159047fSniklas       else
772159047fSniklas 	n2 = 0;
782159047fSniklas 
792159047fSniklas       /* Just make sure there are an even number of nibbles in it, and
80b55d4692Sfgsch 	 that the count is the same as the length.  */
81*007c2a45Smiod       for (i = 0; p->data.nib[i] != (op_type) E; i++)
82b55d4692Sfgsch 	;
83b55d4692Sfgsch 
842159047fSniklas       if (i & 1)
85*007c2a45Smiod 	{
86*007c2a45Smiod 	  fprintf (stderr, "Internal error, h8_disassemble_init.\n");
872159047fSniklas 	  abort ();
88*007c2a45Smiod 	}
89b55d4692Sfgsch 
90c074d1c9Sdrahn       pi->length = i / 2;
91c074d1c9Sdrahn       pi->opcode = p;
922159047fSniklas     }
932159047fSniklas 
94c074d1c9Sdrahn   /* Add entry for the NULL vector terminator.  */
95c074d1c9Sdrahn   pi->length = 0;
96c074d1c9Sdrahn   pi->opcode = p;
97c074d1c9Sdrahn }
98c074d1c9Sdrahn 
99*007c2a45Smiod static void
extract_immediate(stream,looking_for,thisnib,data,cst,len,q)100*007c2a45Smiod extract_immediate (stream, looking_for, thisnib, data, cst, len, q)
101*007c2a45Smiod      FILE *stream;
102*007c2a45Smiod      op_type looking_for;
103*007c2a45Smiod      int thisnib;
104*007c2a45Smiod      unsigned char *data;
105*007c2a45Smiod      int *cst, *len;
106*007c2a45Smiod      const struct h8_opcode *q;
1072159047fSniklas {
108*007c2a45Smiod   switch (looking_for & SIZE)
109*007c2a45Smiod     {
110*007c2a45Smiod     case L_2:
111*007c2a45Smiod       *len = 2;
112*007c2a45Smiod       *cst = thisnib & 3;
113*007c2a45Smiod 
114*007c2a45Smiod       /* DISP2 special treatment.  */
115*007c2a45Smiod       if ((looking_for & MODE) == DISP)
116*007c2a45Smiod 	{
117*007c2a45Smiod 	  if (OP_KIND (q->how) == O_MOVAB ||
118*007c2a45Smiod 	      OP_KIND (q->how) == O_MOVAW ||
119*007c2a45Smiod 	      OP_KIND (q->how) == O_MOVAL)
120*007c2a45Smiod 	    {
121*007c2a45Smiod 	      /* Handling for mova insn.  */
122*007c2a45Smiod 	      switch (q->args.nib[0] & MODE) {
123*007c2a45Smiod 	      case INDEXB:
124*007c2a45Smiod 	      default:
125*007c2a45Smiod 		break;
126*007c2a45Smiod 	      case INDEXW:
127*007c2a45Smiod 		*cst *= 2;
128*007c2a45Smiod 		break;
129*007c2a45Smiod 	      case INDEXL:
130*007c2a45Smiod 		*cst *= 4;
131*007c2a45Smiod 		break;
132*007c2a45Smiod 	      }
133*007c2a45Smiod 	    }
134*007c2a45Smiod 	  else
135*007c2a45Smiod 	    {
136*007c2a45Smiod 	      /* Handling for non-mova insn.  */
137*007c2a45Smiod 	      switch (OP_SIZE (q->how)) {
138*007c2a45Smiod 	      default: break;
139*007c2a45Smiod 	      case SW:
140*007c2a45Smiod 		*cst *= 2;
141*007c2a45Smiod 		break;
142*007c2a45Smiod 	      case SL:
143*007c2a45Smiod 		*cst *= 4;
144*007c2a45Smiod 		break;
145*007c2a45Smiod 	      }
146*007c2a45Smiod 	    }
147*007c2a45Smiod 	}
148*007c2a45Smiod       break;
149*007c2a45Smiod     case L_8:
150*007c2a45Smiod       *len = 8;
151*007c2a45Smiod       *cst = data[0];
152*007c2a45Smiod       break;
153*007c2a45Smiod     case L_16:
154*007c2a45Smiod     case L_16U:
155*007c2a45Smiod       *len = 16;
156*007c2a45Smiod       *cst = (data[0] << 8) + data [1];
157*007c2a45Smiod #if 0
158*007c2a45Smiod       if ((looking_for & SIZE) == L_16)
159*007c2a45Smiod 	*cst = (short) *cst;	/* sign extend */
160*007c2a45Smiod #endif
161*007c2a45Smiod       break;
162*007c2a45Smiod     case L_32:
163*007c2a45Smiod       *len = 32;
164*007c2a45Smiod       *cst = (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3];
165*007c2a45Smiod       break;
166*007c2a45Smiod     default:
167*007c2a45Smiod       *len = 0;
168*007c2a45Smiod       *cst = 0;
169*007c2a45Smiod       fprintf (stream, "DISP bad size\n");
170*007c2a45Smiod       break;
171*007c2a45Smiod     }
172*007c2a45Smiod }
173*007c2a45Smiod 
174c074d1c9Sdrahn static const char *regnames[] =
1752159047fSniklas {
1762159047fSniklas   "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
177b55d4692Sfgsch   "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
178b55d4692Sfgsch };
179c074d1c9Sdrahn static const char *wregnames[] =
1802159047fSniklas {
1812159047fSniklas   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
1822159047fSniklas   "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
1832159047fSniklas };
184c074d1c9Sdrahn static const char *lregnames[] =
1852159047fSniklas {
1862159047fSniklas   "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
1872159047fSniklas   "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
188b55d4692Sfgsch };
189*007c2a45Smiod static const char *cregnames[] =
190*007c2a45Smiod {
191*007c2a45Smiod   "ccr", "exr", "mach", "macl", "", "", "vbr", "sbr"
192*007c2a45Smiod };
193*007c2a45Smiod 
194*007c2a45Smiod static void
print_one_arg(info,addr,x,cst,cstlen,rdisp_n,rn,pregnames,len)195*007c2a45Smiod print_one_arg (info, addr, x, cst, cstlen, rdisp_n, rn, pregnames, len)
196*007c2a45Smiod      disassemble_info *info;
197*007c2a45Smiod      bfd_vma addr;
198*007c2a45Smiod      op_type x;
199*007c2a45Smiod      int cst, cstlen, rdisp_n, rn;
200*007c2a45Smiod      const char **pregnames;
201*007c2a45Smiod      int len;
202*007c2a45Smiod {
203*007c2a45Smiod   void *stream = info->stream;
204*007c2a45Smiod   fprintf_ftype outfn = info->fprintf_func;
205*007c2a45Smiod 
206*007c2a45Smiod   if ((x & SIZE) == L_3 ||
207*007c2a45Smiod       (x & SIZE) == L_3NZ)
208*007c2a45Smiod     {
209*007c2a45Smiod       outfn (stream, "#0x%x", (unsigned) cst);
210*007c2a45Smiod     }
211*007c2a45Smiod   else if ((x & MODE) == IMM)
212*007c2a45Smiod     {
213*007c2a45Smiod       outfn (stream, "#0x%x", (unsigned) cst);
214*007c2a45Smiod     }
215*007c2a45Smiod   else if ((x & MODE) == DBIT  ||
216*007c2a45Smiod 	   (x & MODE) == KBIT)
217*007c2a45Smiod     {
218*007c2a45Smiod       outfn (stream, "#%d", (unsigned) cst);
219*007c2a45Smiod     }
220*007c2a45Smiod   else if ((x & MODE) == CONST_2)
221*007c2a45Smiod     outfn (stream, "#2");
222*007c2a45Smiod   else if ((x & MODE) == CONST_4)
223*007c2a45Smiod     outfn (stream, "#4");
224*007c2a45Smiod   else if ((x & MODE) == CONST_8)
225*007c2a45Smiod     outfn (stream, "#8");
226*007c2a45Smiod   else if ((x & MODE) == CONST_16)
227*007c2a45Smiod     outfn (stream, "#16");
228*007c2a45Smiod   else if ((x & MODE) == REG)
229*007c2a45Smiod     {
230*007c2a45Smiod       switch (x & SIZE)
231*007c2a45Smiod 	{
232*007c2a45Smiod 	case L_8:
233*007c2a45Smiod 	  outfn (stream, "%s", regnames[rn]);
234*007c2a45Smiod 	  break;
235*007c2a45Smiod 	case L_16:
236*007c2a45Smiod 	case L_16U:
237*007c2a45Smiod 	  outfn (stream, "%s", wregnames[rn]);
238*007c2a45Smiod 	  break;
239*007c2a45Smiod 	case L_P:
240*007c2a45Smiod 	case L_32:
241*007c2a45Smiod 	  outfn (stream, "%s", lregnames[rn]);
242*007c2a45Smiod 	  break;
243*007c2a45Smiod 	}
244*007c2a45Smiod     }
245*007c2a45Smiod   else if ((x & MODE) == LOWREG)
246*007c2a45Smiod     {
247*007c2a45Smiod       switch (x & SIZE)
248*007c2a45Smiod 	{
249*007c2a45Smiod 	case L_8:
250*007c2a45Smiod 	  /* Always take low half of reg.  */
251*007c2a45Smiod 	  outfn (stream, "%s.b", regnames[rn < 8 ? rn + 8 : rn]);
252*007c2a45Smiod 	  break;
253*007c2a45Smiod 	case L_16:
254*007c2a45Smiod 	case L_16U:
255*007c2a45Smiod 	  /* Always take low half of reg.  */
256*007c2a45Smiod 	  outfn (stream, "%s.w", wregnames[rn < 8 ? rn : rn - 8]);
257*007c2a45Smiod 	  break;
258*007c2a45Smiod 	case L_P:
259*007c2a45Smiod 	case L_32:
260*007c2a45Smiod 	  outfn (stream, "%s.l", lregnames[rn]);
261*007c2a45Smiod 	  break;
262*007c2a45Smiod 	}
263*007c2a45Smiod     }
264*007c2a45Smiod   else if ((x & MODE) == POSTINC)
265*007c2a45Smiod     {
266*007c2a45Smiod       outfn (stream, "@%s+", pregnames[rn]);
267*007c2a45Smiod     }
268*007c2a45Smiod   else if ((x & MODE) == POSTDEC)
269*007c2a45Smiod     {
270*007c2a45Smiod       outfn (stream, "@%s-", pregnames[rn]);
271*007c2a45Smiod     }
272*007c2a45Smiod   else if ((x & MODE) == PREINC)
273*007c2a45Smiod     {
274*007c2a45Smiod       outfn (stream, "@+%s", pregnames[rn]);
275*007c2a45Smiod     }
276*007c2a45Smiod   else if ((x & MODE) == PREDEC)
277*007c2a45Smiod     {
278*007c2a45Smiod       outfn (stream, "@-%s", pregnames[rn]);
279*007c2a45Smiod     }
280*007c2a45Smiod   else if ((x & MODE) == IND)
281*007c2a45Smiod     {
282*007c2a45Smiod       outfn (stream, "@%s", pregnames[rn]);
283*007c2a45Smiod     }
284*007c2a45Smiod   else if ((x & MODE) == ABS || (x & ABSJMP))
285*007c2a45Smiod     {
286*007c2a45Smiod       outfn (stream, "@0x%x:%d", (unsigned) cst, cstlen);
287*007c2a45Smiod     }
288*007c2a45Smiod   else if ((x & MODE) == MEMIND)
289*007c2a45Smiod     {
290*007c2a45Smiod       outfn (stream, "@@%d (0x%x)", cst, cst);
291*007c2a45Smiod     }
292*007c2a45Smiod   else if ((x & MODE) == VECIND)
293*007c2a45Smiod     {
294*007c2a45Smiod       /* FIXME Multiplier should be 2 or 4, depending on processor mode,
295*007c2a45Smiod 	 by which is meant "normal" vs. "middle", "advanced", "maximum".  */
296*007c2a45Smiod 
297*007c2a45Smiod       int offset = (cst + 0x80) * 4;
298*007c2a45Smiod       outfn (stream, "@@%d (0x%x)", offset, offset);
299*007c2a45Smiod     }
300*007c2a45Smiod   else if ((x & MODE) == PCREL)
301*007c2a45Smiod     {
302*007c2a45Smiod       if ((x & SIZE) == L_16 ||
303*007c2a45Smiod 	  (x & SIZE) == L_16U)
304*007c2a45Smiod 	{
305*007c2a45Smiod 	  outfn (stream, ".%s%d (0x%x)",
306*007c2a45Smiod 		   (short) cst > 0 ? "+" : "",
307*007c2a45Smiod 		   (short) cst,
308*007c2a45Smiod 		   addr + (short) cst + len);
309*007c2a45Smiod 	}
310*007c2a45Smiod       else
311*007c2a45Smiod 	{
312*007c2a45Smiod 	  outfn (stream, ".%s%d (0x%x)",
313*007c2a45Smiod 		   (char) cst > 0 ? "+" : "",
314*007c2a45Smiod 		   (char) cst,
315*007c2a45Smiod 		   addr + (char) cst + len);
316*007c2a45Smiod 	}
317*007c2a45Smiod     }
318*007c2a45Smiod   else if ((x & MODE) == DISP)
319*007c2a45Smiod     {
320*007c2a45Smiod       outfn (stream, "@(0x%x:%d,%s)", cst, cstlen,
321*007c2a45Smiod 	       pregnames[rdisp_n]);
322*007c2a45Smiod     }
323*007c2a45Smiod   else if ((x & MODE) == INDEXB)
324*007c2a45Smiod     {
325*007c2a45Smiod       /* Always take low half of reg.  */
326*007c2a45Smiod       outfn (stream, "@(0x%x:%d,%s.b)", cst, cstlen,
327*007c2a45Smiod 	     regnames[rdisp_n < 8 ? rdisp_n + 8 : rdisp_n]);
328*007c2a45Smiod     }
329*007c2a45Smiod   else if ((x & MODE) == INDEXW)
330*007c2a45Smiod     {
331*007c2a45Smiod       /* Always take low half of reg.  */
332*007c2a45Smiod       outfn (stream, "@(0x%x:%d,%s.w)", cst, cstlen,
333*007c2a45Smiod 	       wregnames[rdisp_n < 8 ? rdisp_n : rdisp_n - 8]);
334*007c2a45Smiod     }
335*007c2a45Smiod   else if ((x & MODE) == INDEXL)
336*007c2a45Smiod     {
337*007c2a45Smiod       outfn (stream, "@(0x%x:%d,%s.l)", cst, cstlen,
338*007c2a45Smiod 	       lregnames[rdisp_n]);
339*007c2a45Smiod     }
340*007c2a45Smiod   else if (x & CTRL)
341*007c2a45Smiod     {
342*007c2a45Smiod       outfn (stream, cregnames[rn]);
343*007c2a45Smiod     }
344*007c2a45Smiod   else if ((x & MODE) == CCR)
345*007c2a45Smiod     {
346*007c2a45Smiod       outfn (stream, "ccr");
347*007c2a45Smiod     }
348*007c2a45Smiod   else if ((x & MODE) == EXR)
349*007c2a45Smiod     {
350*007c2a45Smiod       outfn (stream, "exr");
351*007c2a45Smiod     }
352*007c2a45Smiod   else if ((x & MODE) == MACREG)
353*007c2a45Smiod     {
354*007c2a45Smiod       outfn (stream, "mac%c", cst ? 'l' : 'h');
355*007c2a45Smiod     }
356*007c2a45Smiod   else
357*007c2a45Smiod     /* xgettext:c-format */
358*007c2a45Smiod     outfn (stream, _("Hmmmm 0x%x"), x);
359*007c2a45Smiod }
360*007c2a45Smiod 
361*007c2a45Smiod static unsigned int
bfd_h8_disassemble(addr,info,mach)362*007c2a45Smiod bfd_h8_disassemble (addr, info, mach)
363*007c2a45Smiod      bfd_vma addr;
364*007c2a45Smiod      disassemble_info *info;
365*007c2a45Smiod      int mach;
366*007c2a45Smiod {
367*007c2a45Smiod   /* Find the first entry in the table for this opcode.  */
368*007c2a45Smiod   int regno[3] = { 0, 0, 0 };
369*007c2a45Smiod   int dispregno[3] = { 0, 0, 0 };
370*007c2a45Smiod   int cst[3] = { 0, 0, 0 };
371*007c2a45Smiod   int cstlen[3] = { 0, 0, 0 };
372c074d1c9Sdrahn   static bfd_boolean init = 0;
373c074d1c9Sdrahn   const struct h8_instruction *qi;
374*007c2a45Smiod   char const **pregnames = mach != 0 ? lregnames : wregnames;
3752159047fSniklas   int status;
376*007c2a45Smiod   unsigned int l;
377*007c2a45Smiod   unsigned char data[MAX_CODE_NIBBLES];
3782159047fSniklas   void *stream = info->stream;
379*007c2a45Smiod   fprintf_ftype outfn = info->fprintf_func;
3802159047fSniklas 
3812159047fSniklas   if (!init)
3822159047fSniklas     {
3832159047fSniklas       bfd_h8_disassemble_init ();
3842159047fSniklas       init = 1;
3852159047fSniklas     }
3862159047fSniklas 
3872159047fSniklas   status = info->read_memory_func (addr, data, 2, info);
3882159047fSniklas   if (status != 0)
3892159047fSniklas     {
3902159047fSniklas       info->memory_error_func (status, addr, info);
3912159047fSniklas       return -1;
3922159047fSniklas     }
393b55d4692Sfgsch 
394*007c2a45Smiod   for (l = 2; status == 0 && l < sizeof (data) / 2; l += 2)
3952159047fSniklas     status = info->read_memory_func (addr + l, data + l, 2, info);
3962159047fSniklas 
397b55d4692Sfgsch   /* Find the exact opcode/arg combo.  */
398c074d1c9Sdrahn   for (qi = h8_instructions; qi->opcode->name; qi++)
3992159047fSniklas     {
400c074d1c9Sdrahn       const struct h8_opcode *q = qi->opcode;
401b55d4692Sfgsch       op_type *nib = q->data.nib;
4022159047fSniklas       unsigned int len = 0;
4032159047fSniklas 
4042159047fSniklas       while (1)
4052159047fSniklas 	{
4062159047fSniklas 	  op_type looking_for = *nib;
407*007c2a45Smiod 	  int thisnib = data[len / 2];
408*007c2a45Smiod 	  int opnr;
4092159047fSniklas 
410*007c2a45Smiod 	  thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib / 16) & 0xf);
411*007c2a45Smiod 	  opnr = ((looking_for & OP3) == OP3 ? 2
412*007c2a45Smiod 		  : (looking_for & DST) == DST ? 1 : 0);
4132159047fSniklas 
4142159047fSniklas 	  if (looking_for < 16 && looking_for >= 0)
4152159047fSniklas 	    {
4162159047fSniklas 	      if (looking_for != thisnib)
4172159047fSniklas 		goto fail;
4182159047fSniklas 	    }
4192159047fSniklas 	  else
4202159047fSniklas 	    {
4212159047fSniklas 	      if ((int) looking_for & (int) B31)
4222159047fSniklas 		{
423*007c2a45Smiod 		  if (!((thisnib & 0x8) != 0))
4242159047fSniklas 		    goto fail;
425b55d4692Sfgsch 
4262159047fSniklas 		  looking_for = (op_type) ((int) looking_for & ~(int) B31);
427*007c2a45Smiod 		  thisnib &= 0x7;
4282159047fSniklas 		}
429*007c2a45Smiod 	      else if ((int) looking_for & (int) B30)
4302159047fSniklas 		{
431*007c2a45Smiod 		  if (!((thisnib & 0x8) == 0))
4322159047fSniklas 		    goto fail;
433b55d4692Sfgsch 
4342159047fSniklas 		  looking_for = (op_type) ((int) looking_for & ~(int) B30);
4352159047fSniklas 		}
4362159047fSniklas 
437*007c2a45Smiod 	      if ((int) looking_for & (int) B21)
438*007c2a45Smiod 		{
439*007c2a45Smiod 		  if (!((thisnib & 0x4) != 0))
440*007c2a45Smiod 		    goto fail;
441*007c2a45Smiod 
442*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B21);
443*007c2a45Smiod 		  thisnib &= 0xb;
444*007c2a45Smiod 		}
445*007c2a45Smiod 	      else if ((int) looking_for & (int) B20)
446*007c2a45Smiod 		{
447*007c2a45Smiod 		  if (!((thisnib & 0x4) == 0))
448*007c2a45Smiod 		    goto fail;
449*007c2a45Smiod 
450*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B20);
451*007c2a45Smiod 		}
452*007c2a45Smiod 	      if ((int) looking_for & (int) B11)
453*007c2a45Smiod 		{
454*007c2a45Smiod 		  if (!((thisnib & 0x2) != 0))
455*007c2a45Smiod 		    goto fail;
456*007c2a45Smiod 
457*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B11);
458*007c2a45Smiod 		  thisnib &= 0xd;
459*007c2a45Smiod 		}
460*007c2a45Smiod 	      else if ((int) looking_for & (int) B10)
461*007c2a45Smiod 		{
462*007c2a45Smiod 		  if (!((thisnib & 0x2) == 0))
463*007c2a45Smiod 		    goto fail;
464*007c2a45Smiod 
465*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B10);
466*007c2a45Smiod 		}
467*007c2a45Smiod 
468*007c2a45Smiod 	      if ((int) looking_for & (int) B01)
469*007c2a45Smiod 		{
470*007c2a45Smiod 		  if (!((thisnib & 0x1) != 0))
471*007c2a45Smiod 		    goto fail;
472*007c2a45Smiod 
473*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B01);
474*007c2a45Smiod 		  thisnib &= 0xe;
475*007c2a45Smiod 		}
476*007c2a45Smiod 	      else if ((int) looking_for & (int) B00)
477*007c2a45Smiod 		{
478*007c2a45Smiod 		  if (!((thisnib & 0x1) == 0))
479*007c2a45Smiod 		    goto fail;
480*007c2a45Smiod 
481*007c2a45Smiod 		  looking_for = (op_type) ((int) looking_for & ~(int) B00);
482*007c2a45Smiod 		}
483*007c2a45Smiod 
484*007c2a45Smiod 	      if (looking_for & IGNORE)
485*007c2a45Smiod 		{
486*007c2a45Smiod 		  /* Hitachi has declared that IGNORE must be zero.  */
487*007c2a45Smiod 		  if (thisnib != 0)
488*007c2a45Smiod 		    goto fail;
489*007c2a45Smiod 		}
490*007c2a45Smiod 	      else if ((looking_for & MODE) == DATA)
491*007c2a45Smiod 		{
492*007c2a45Smiod 		  ;			/* Skip embedded data.  */
493*007c2a45Smiod 		}
494*007c2a45Smiod 	      else if ((looking_for & MODE) == DBIT)
4952159047fSniklas 		{
496b55d4692Sfgsch 		  /* Exclude adds/subs by looking at bit 0 and 2, and
497b55d4692Sfgsch                      make sure the operand size, either w or l,
498b55d4692Sfgsch                      matches by looking at bit 1.  */
499b55d4692Sfgsch 		  if ((looking_for & 7) != (thisnib & 7))
500b55d4692Sfgsch 		    goto fail;
501b55d4692Sfgsch 
502*007c2a45Smiod 		  cst[opnr] = (thisnib & 0x8) ? 2 : 1;
5032159047fSniklas 		}
504*007c2a45Smiod 	      else if ((looking_for & MODE) == DISP  ||
505*007c2a45Smiod 		       (looking_for & MODE) == ABS   ||
506*007c2a45Smiod 		       (looking_for & MODE) == PCREL ||
507*007c2a45Smiod 		       (looking_for & MODE) == INDEXB ||
508*007c2a45Smiod 		       (looking_for & MODE) == INDEXW ||
509*007c2a45Smiod 		       (looking_for & MODE) == INDEXL)
5102159047fSniklas 		{
511*007c2a45Smiod 		  extract_immediate (stream, looking_for, thisnib,
512*007c2a45Smiod 				     data + len / 2, cst + opnr,
513*007c2a45Smiod 				     cstlen + opnr, q);
514*007c2a45Smiod 		  /* Even address == bra, odd == bra/s.  */
515*007c2a45Smiod 		  if (q->how == O (O_BRAS, SB))
516*007c2a45Smiod 		    cst[opnr] -= 1;
5172159047fSniklas 		}
518*007c2a45Smiod 	      else if ((looking_for & MODE) == REG     ||
519*007c2a45Smiod 		       (looking_for & MODE) == LOWREG  ||
520*007c2a45Smiod 		       (looking_for & MODE) == IND     ||
521*007c2a45Smiod 		       (looking_for & MODE) == PREINC  ||
522*007c2a45Smiod 		       (looking_for & MODE) == POSTINC ||
523*007c2a45Smiod 		       (looking_for & MODE) == PREDEC  ||
524*007c2a45Smiod 		       (looking_for & MODE) == POSTDEC)
5252159047fSniklas 		{
526*007c2a45Smiod 		  regno[opnr] = thisnib;
5272159047fSniklas 		}
528*007c2a45Smiod 	      else if (looking_for & CTRL)	/* Control Register */
5292159047fSniklas 		{
530*007c2a45Smiod 		  thisnib &= 7;
531*007c2a45Smiod 		  if (((looking_for & MODE) == CCR  && (thisnib != C_CCR))  ||
532*007c2a45Smiod 		      ((looking_for & MODE) == EXR  && (thisnib != C_EXR))  ||
533*007c2a45Smiod 		      ((looking_for & MODE) == MACH && (thisnib != C_MACH)) ||
534*007c2a45Smiod 		      ((looking_for & MODE) == MACL && (thisnib != C_MACL)) ||
535*007c2a45Smiod 		      ((looking_for & MODE) == VBR  && (thisnib != C_VBR))  ||
536*007c2a45Smiod 		      ((looking_for & MODE) == SBR  && (thisnib != C_SBR)))
537*007c2a45Smiod 		    goto fail;
538*007c2a45Smiod 		  if (((looking_for & MODE) == CCR_EXR &&
539*007c2a45Smiod 		       (thisnib != C_CCR && thisnib != C_EXR)) ||
540*007c2a45Smiod 		      ((looking_for & MODE) == VBR_SBR &&
541*007c2a45Smiod 		       (thisnib != C_VBR && thisnib != C_SBR)) ||
542*007c2a45Smiod 		      ((looking_for & MODE) == MACREG &&
543*007c2a45Smiod 		       (thisnib != C_MACH && thisnib != C_MACL)))
544*007c2a45Smiod 		    goto fail;
545*007c2a45Smiod 		  if (((looking_for & MODE) == CC_EX_VB_SB &&
546*007c2a45Smiod 		       (thisnib != C_CCR && thisnib != C_EXR &&
547*007c2a45Smiod 			thisnib != C_VBR && thisnib != C_SBR)))
548*007c2a45Smiod 		    goto fail;
549b55d4692Sfgsch 
550*007c2a45Smiod 		  regno[opnr] = thisnib;
551*007c2a45Smiod 		}
552*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_5)
553*007c2a45Smiod 		{
554*007c2a45Smiod 		  cst[opnr] = data[len / 2] & 31;
555*007c2a45Smiod 		  cstlen[opnr] = 5;
556*007c2a45Smiod 		}
557*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_4)
558*007c2a45Smiod 		{
559*007c2a45Smiod 		  cst[opnr] = thisnib;
560*007c2a45Smiod 		  cstlen[opnr] = 4;
561*007c2a45Smiod 		}
562*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_16 ||
563*007c2a45Smiod 		       (looking_for & SIZE) == L_16U)
564*007c2a45Smiod 		{
565*007c2a45Smiod 		  cst[opnr] = (data[len / 2]) * 256 + data[(len + 2) / 2];
566*007c2a45Smiod 		  cstlen[opnr] = 16;
567*007c2a45Smiod 		}
568*007c2a45Smiod 	      else if ((looking_for & MODE) == MEMIND)
569*007c2a45Smiod 		{
570*007c2a45Smiod 		  cst[opnr] = data[1];
571*007c2a45Smiod 		}
572*007c2a45Smiod 	      else if ((looking_for & MODE) == VECIND)
573*007c2a45Smiod 		{
574*007c2a45Smiod 		  cst[opnr] = data[1] & 0x7f;
575*007c2a45Smiod 		}
576*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_32)
577*007c2a45Smiod 		{
578*007c2a45Smiod 		  int i = len / 2;
579*007c2a45Smiod 
580*007c2a45Smiod 		  cst[opnr] = ((data[i] << 24)
5812159047fSniklas 			       | (data[i + 1] << 16)
5822159047fSniklas 			       | (data[i + 2] << 8)
583*007c2a45Smiod 			       | (data[i + 3]));
5842159047fSniklas 
585*007c2a45Smiod 		  cstlen[opnr] = 32;
5862159047fSniklas 		}
587*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_24)
5882159047fSniklas 		{
589*007c2a45Smiod 		  int i = len / 2;
590b55d4692Sfgsch 
591*007c2a45Smiod 		  cst[opnr] =
592*007c2a45Smiod 		    (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
593*007c2a45Smiod 		  cstlen[opnr] = 24;
5942159047fSniklas 		}
5952159047fSniklas 	      else if (looking_for & IGNORE)
5962159047fSniklas 		{
597b55d4692Sfgsch 		  ;
5982159047fSniklas 		}
5992159047fSniklas 	      else if (looking_for & DISPREG)
6002159047fSniklas 		{
601*007c2a45Smiod 		  dispregno[opnr] = thisnib & 7;
6022159047fSniklas 		}
603*007c2a45Smiod 	      else if ((looking_for & MODE) == KBIT)
6042159047fSniklas 		{
6052159047fSniklas 		  switch (thisnib)
6062159047fSniklas 		    {
6072159047fSniklas 		    case 9:
608*007c2a45Smiod 		      cst[opnr] = 4;
6092159047fSniklas 		      break;
6102159047fSniklas 		    case 8:
611*007c2a45Smiod 		      cst[opnr] = 2;
6122159047fSniklas 		      break;
6132159047fSniklas 		    case 0:
614*007c2a45Smiod 		      cst[opnr] = 1;
6152159047fSniklas 		      break;
6160c6d0228Sniklas 		    default:
6170c6d0228Sniklas 		      goto fail;
6182159047fSniklas 		    }
6192159047fSniklas 		}
620*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_8)
6212159047fSniklas 		{
622*007c2a45Smiod 		  cstlen[opnr] = 8;
623*007c2a45Smiod 		  cst[opnr] = data[len / 2];
6242159047fSniklas 		}
625*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_3 ||
626*007c2a45Smiod 		       (looking_for & SIZE) == L_3NZ)
6272159047fSniklas 		{
628*007c2a45Smiod 		  cst[opnr] = thisnib & 0x7;
629*007c2a45Smiod 		  if (cst[opnr] == 0 && (looking_for & SIZE) == L_3NZ)
630*007c2a45Smiod 		    goto fail;
6312159047fSniklas 		}
632*007c2a45Smiod 	      else if ((looking_for & SIZE) == L_2)
6332159047fSniklas 		{
634*007c2a45Smiod 		  cstlen[opnr] = 2;
635*007c2a45Smiod 		  cst[opnr] = thisnib & 0x3;
6360c6d0228Sniklas 		}
637*007c2a45Smiod 	      else if ((looking_for & MODE) == MACREG)
6380c6d0228Sniklas 		{
639*007c2a45Smiod 		  cst[opnr] = (thisnib == 3);
6402159047fSniklas 		}
641*007c2a45Smiod 	      else if (looking_for == (op_type) E)
6422159047fSniklas 		{
6432159047fSniklas 		  int i;
6442159047fSniklas 
645c074d1c9Sdrahn 		  for (i = 0; i < qi->length; i++)
646*007c2a45Smiod 		    outfn (stream, "%02x ", data[i]);
647b55d4692Sfgsch 
6482159047fSniklas 		  for (; i < 6; i++)
649*007c2a45Smiod 		    outfn (stream, "   ");
650b55d4692Sfgsch 
651*007c2a45Smiod 		  outfn (stream, "%s\t", q->name);
6520c6d0228Sniklas 
6530c6d0228Sniklas 		  /* Gross.  Disgusting.  */
6540c6d0228Sniklas 		  if (strcmp (q->name, "ldm.l") == 0)
6550c6d0228Sniklas 		    {
6560c6d0228Sniklas 		      int count, high;
6570c6d0228Sniklas 
658*007c2a45Smiod 		      count = (data[1] / 16) & 0x3;
659*007c2a45Smiod 		      high = regno[1];
6600c6d0228Sniklas 
661*007c2a45Smiod 		      outfn (stream, "@sp+,er%d-er%d", high - count, high);
662c074d1c9Sdrahn 		      return qi->length;
6630c6d0228Sniklas 		    }
6640c6d0228Sniklas 
6650c6d0228Sniklas 		  if (strcmp (q->name, "stm.l") == 0)
6660c6d0228Sniklas 		    {
6670c6d0228Sniklas 		      int count, low;
6680c6d0228Sniklas 
669*007c2a45Smiod 		      count = (data[1] / 16) & 0x3;
670*007c2a45Smiod 		      low = regno[0];
6710c6d0228Sniklas 
672*007c2a45Smiod 		      outfn (stream, "er%d-er%d,@-sp", low, low + count);
673c074d1c9Sdrahn 		      return qi->length;
6740c6d0228Sniklas 		    }
675*007c2a45Smiod 		  if (strcmp (q->name, "rte/l") == 0
676*007c2a45Smiod 		      || strcmp (q->name, "rts/l") == 0)
677*007c2a45Smiod 		    {
678*007c2a45Smiod 		      if (regno[0] == 0)
679*007c2a45Smiod 			outfn (stream, "er%d", regno[1]);
680*007c2a45Smiod 		      else
681*007c2a45Smiod 			{
682*007c2a45Smiod 			  outfn (stream, "er%d-er%d", regno[1] - regno[0],
683*007c2a45Smiod 				 regno[1]);
684*007c2a45Smiod 			}
685*007c2a45Smiod 		      return qi->length;
686*007c2a45Smiod 		    }
687*007c2a45Smiod 		  if (strncmp (q->name, "mova", 4) == 0)
688*007c2a45Smiod 		    {
689*007c2a45Smiod 		      op_type *args = q->args.nib;
6900c6d0228Sniklas 
691*007c2a45Smiod 		      if (args[1] == (op_type) E)
692*007c2a45Smiod 			{
693*007c2a45Smiod 			  /* Short form.  */
694*007c2a45Smiod 			  print_one_arg (info, addr, args[0], cst[0],
695*007c2a45Smiod 					 cstlen[0], dispregno[0], regno[0],
696*007c2a45Smiod 					 pregnames, qi->length);
697*007c2a45Smiod 			  outfn (stream, ",er%d", dispregno[0]);
698*007c2a45Smiod 			}
699*007c2a45Smiod 		      else
700*007c2a45Smiod 			{
701*007c2a45Smiod 			  outfn (stream, "@(0x%x:%d,", cst[0], cstlen[0]);
702*007c2a45Smiod 			  print_one_arg (info, addr, args[1], cst[1],
703*007c2a45Smiod 					 cstlen[1], dispregno[1], regno[1],
704*007c2a45Smiod 					 pregnames, qi->length);
705*007c2a45Smiod 			  outfn (stream, ".%c),",
706*007c2a45Smiod 				 (args[0] & MODE) == INDEXB ? 'b' : 'w');
707*007c2a45Smiod 			  print_one_arg (info, addr, args[2], cst[2],
708*007c2a45Smiod 					 cstlen[2], dispregno[2], regno[2],
709*007c2a45Smiod 					 pregnames, qi->length);
710*007c2a45Smiod 			}
711*007c2a45Smiod 		      return qi->length;
712*007c2a45Smiod 		    }
713b55d4692Sfgsch 		  /* Fill in the args.  */
7142159047fSniklas 		  {
7152159047fSniklas 		    op_type *args = q->args.nib;
7162159047fSniklas 		    int hadone = 0;
717*007c2a45Smiod 		    int nargs;
7182159047fSniklas 
719*007c2a45Smiod 		    for (nargs = 0;
720*007c2a45Smiod 			 nargs < 3 && args[nargs] != (op_type) E;
721*007c2a45Smiod 			 nargs++)
7222159047fSniklas 		      {
723*007c2a45Smiod 			int x = args[nargs];
724b55d4692Sfgsch 
7252159047fSniklas 			if (hadone)
726*007c2a45Smiod 			  outfn (stream, ",");
7272159047fSniklas 
728*007c2a45Smiod 			print_one_arg (info, addr, x,
729*007c2a45Smiod 				       cst[nargs], cstlen[nargs],
730*007c2a45Smiod 				       dispregno[nargs], regno[nargs],
731*007c2a45Smiod 				       pregnames, qi->length);
732b55d4692Sfgsch 
7332159047fSniklas 			hadone = 1;
7342159047fSniklas 		      }
7352159047fSniklas 		  }
736b55d4692Sfgsch 
737c074d1c9Sdrahn 		  return qi->length;
7382159047fSniklas 		}
7392159047fSniklas 	      else
740b305b0f1Sespie 		/* xgettext:c-format */
741*007c2a45Smiod 		outfn (stream, _("Don't understand 0x%x \n"), looking_for);
7422159047fSniklas 	    }
7432159047fSniklas 
7442159047fSniklas 	  len++;
7452159047fSniklas 	  nib++;
7462159047fSniklas 	}
7472159047fSniklas 
7482159047fSniklas     fail:
749b55d4692Sfgsch       ;
7502159047fSniklas     }
7512159047fSniklas 
752b55d4692Sfgsch   /* Fell off the end.  */
753*007c2a45Smiod   outfn (stream, "%02x %02x        .word\tH'%x,H'%x",
7542159047fSniklas 	   data[0], data[1],
7552159047fSniklas 	   data[0], data[1]);
7562159047fSniklas   return 2;
7572159047fSniklas }
7582159047fSniklas 
7592159047fSniklas int
print_insn_h8300(addr,info)7602159047fSniklas print_insn_h8300 (addr, info)
7612159047fSniklas      bfd_vma addr;
7622159047fSniklas      disassemble_info *info;
7632159047fSniklas {
7642159047fSniklas   return bfd_h8_disassemble (addr, info, 0);
7652159047fSniklas }
7662159047fSniklas 
7672159047fSniklas int
print_insn_h8300h(addr,info)7682159047fSniklas print_insn_h8300h (addr, info)
7692159047fSniklas      bfd_vma addr;
7702159047fSniklas      disassemble_info *info;
7712159047fSniklas {
7722159047fSniklas   return bfd_h8_disassemble (addr, info, 1);
7732159047fSniklas }
7742159047fSniklas 
7750c6d0228Sniklas int
print_insn_h8300s(addr,info)7760c6d0228Sniklas print_insn_h8300s (addr, info)
7770c6d0228Sniklas      bfd_vma addr;
7780c6d0228Sniklas      disassemble_info *info;
7790c6d0228Sniklas {
7800c6d0228Sniklas   return bfd_h8_disassemble (addr, info, 2);
7810c6d0228Sniklas }
782