Applied patch from Kazu Hirata <kazu@hxi.com> to fix disassembly of inc.l
[platform/upstream/binutils.git] / opcodes / h8300-dis.c
1 /* Disassemble h8300 instructions.
2    Copyright (C) 1993, 1998, 2000 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #define DEFINE_TABLE
19
20 #include "sysdep.h"
21 #define h8_opcodes h8ops
22 #include "opcode/h8300.h"
23 #include "dis-asm.h"
24 #include "opintl.h"
25
26
27 /* Run through the opcodes and sort them into order to make them easy
28    to disassemble.  */
29 static void
30 bfd_h8_disassemble_init ()
31 {
32   unsigned int i;
33   struct h8_opcode *p;
34
35   for (p = h8_opcodes; p->name; p++)
36     {
37       int n1 = 0;
38       int n2 = 0;
39
40       if ((int) p->data.nib[0] < 16)
41         {
42           n1 = (int) p->data.nib[0];
43         }
44       else
45         n1 = 0;
46       
47       if ((int) p->data.nib[1] < 16)
48         {
49           n2 = (int) p->data.nib[1];
50         }
51       else
52         n2 = 0;
53
54       /* Just make sure there are an even number of nibbles in it, and
55          that the count is the same as the length.  */
56       for (i = 0; p->data.nib[i] != E; i++)
57         /*EMPTY*/ ;
58       
59       if (i & 1)
60         abort ();
61       
62       p->length = i / 2;
63     }
64 }
65
66
67 unsigned int
68 bfd_h8_disassemble (addr, info, mode)
69      bfd_vma addr;
70      disassemble_info *info;
71      int mode;
72 {
73   /* Find the first entry in the table for this opcode.  */
74   static CONST char *regnames[] =
75     {
76       "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
77       "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"
78     };
79   static CONST char *wregnames[] =
80     {
81       "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
82       "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
83     };
84   
85   static CONST char *lregnames[] =
86     {
87       "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
88       "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
89     };
90
91   int rs = 0;
92   int rd = 0;
93   int rdisp = 0;
94   int abs = 0;
95   int bit = 0;
96   int plen = 0;
97   static boolean init = 0;
98   struct h8_opcode *q = h8_opcodes;
99   char CONST **pregnames = mode != 0 ? lregnames : wregnames;
100   int status;
101   int l;
102   unsigned char data[20];  
103   void *stream = info->stream;
104   fprintf_ftype fprintf = info->fprintf_func;
105
106   if (!init)
107     {
108       bfd_h8_disassemble_init ();
109       init = 1;
110     }
111
112   status = info->read_memory_func (addr, data, 2, info);
113   if (status != 0) 
114     {
115       info->memory_error_func (status, addr, info);
116       return -1;
117     }
118   
119   for (l = 2; status == 0 && l < 10; l += 2)
120     status = info->read_memory_func(addr+l, data+l, 2, info);
121
122   /* Find the exact opcode/arg combo.  */
123   while (q->name)
124     {
125       op_type *nib;
126       unsigned int len = 0;
127
128       nib = q->data.nib;
129       
130       while (1)
131         {
132           op_type looking_for = *nib;
133           int thisnib = data[len >> 1];
134           
135           thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
136           
137           if (looking_for < 16 && looking_for >=0) 
138             {
139               if (looking_for != thisnib) 
140                 goto fail;
141             }
142           else 
143             {
144               if ((int) looking_for & (int) B31)
145                 {
146                   if (! (((int) thisnib & 0x8) != 0)) 
147                     goto fail;
148                   looking_for = (op_type) ((int) looking_for & ~(int) B31);
149                 }
150               
151               if ((int) looking_for & (int) B30)
152                 {
153                   if (!(((int) thisnib & 0x8) == 0)) 
154                     goto fail;
155                   looking_for = (op_type) ((int) looking_for & ~(int) B30);
156                 }
157
158               if (looking_for & DBIT)
159                 {
160                   if ((looking_for & 2) != (thisnib & 2))
161                     goto fail;
162                   abs = (thisnib & 0x8) ? 2 : 1;
163                 }                 
164               else if (looking_for & (REG | IND|INC|DEC))
165                 {
166                   if (looking_for & SRC)
167                     rs = thisnib;
168                   else
169                     rd = thisnib;
170                 }
171               else if (looking_for & L_16)
172                 {
173                   abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
174                   plen = 16;
175                 }
176               else if (looking_for & ABSJMP)
177                 {
178                   abs =
179                     (data[1] << 16)
180                       | (data[2] << 8)
181                         | (data[3]);
182                 }
183               else if (looking_for & MEMIND)
184                 {
185                   abs = data[1];
186                 }
187               else if (looking_for & L_32)
188                 {
189                   int i = len >> 1;
190                   
191                   abs = (data[i] << 24)
192                     | (data[i + 1] << 16)
193                       | (data[i + 2] << 8)
194                         | (data[i+ 3]);
195
196                   plen = 32;
197                 }
198               else if (looking_for & L_24)
199                 {
200                   int i = len >> 1;
201                   abs = (data[i] << 16) | (data[i + 1] << 8)|  (data[i+2]);
202                   plen =24;
203                 }
204               else if (looking_for & IGNORE)
205                 {
206                   ;
207                 }
208               else if (looking_for & DISPREG)
209                 {
210                   rdisp = thisnib;
211                 }
212               else if (looking_for & KBIT)
213                 {
214                   switch (thisnib) 
215                     {
216                     case 9:
217                       abs = 4;
218                       break;
219                     case 8:
220                       abs = 2;
221                       break;
222                     case 0:
223                       abs = 1;
224                       break;
225                     default:
226                       goto fail;
227                     }
228                 }
229               else if (looking_for & L_8)
230                 {
231                   plen = 8;               
232                   abs = data[len >> 1];
233                 }
234               else if (looking_for & L_3)
235                 {
236                   bit = thisnib & 0x7;
237                 }
238               else if (looking_for & L_2)
239                 {
240                   plen = 2;
241                   abs = thisnib & 0x3;
242                 }
243               else if (looking_for & MACREG)
244                 {
245                   abs = (thisnib == 3);
246                 }
247               else if (looking_for == E)
248                 {
249                   int i;
250
251                   for (i = 0; i < q->length; i++)
252                     fprintf (stream, "%02x ", data[i]);
253                   
254                   for (; i < 6; i++)
255                     fprintf (stream, "   ");
256                   
257                   fprintf (stream, "%s\t", q->name);
258
259                   /* Gross.  Disgusting.  */
260                   if (strcmp (q->name, "ldm.l") == 0)
261                     {
262                       int count, high;
263
264                       count = (data[1] >> 4) & 0x3;
265                       high = data[3] & 0x7;
266
267                       fprintf (stream, "@sp+,er%d-er%d", high - count, high);
268                       return q->length;
269                     }
270
271                   if (strcmp (q->name, "stm.l") == 0)
272                     {
273                       int count, low;
274
275                       count = (data[1] >> 4) & 0x3;
276                       low = data[3] & 0x7;
277
278                       fprintf (stream, "er%d-er%d,@-sp", low, low + count);
279                       return q->length;
280                     }
281
282                   /* Fill in the args.  */
283                   {
284                     op_type *args = q->args.nib;
285                     int hadone = 0;
286
287                     while (*args != E)
288                       {
289                         int x = *args;
290                         
291                         if (hadone)
292                           fprintf (stream, ",");
293
294                         if (x & L_3)
295                           {
296                             fprintf (stream, "#0x%x", (unsigned) bit);
297                           }
298                         else if (x & (IMM|KBIT|DBIT))
299                           {
300                             /* Bletch.  For shal #2,er0 and friends.  */
301                             if (*(args+1) & SRC_IN_DST)
302                               abs = 2;
303
304                             fprintf (stream, "#0x%x", (unsigned) abs);
305                           }
306                         else if (x & REG)
307                           {
308                             int rn = (x & DST) ? rd : rs;
309                             
310                             switch (x & SIZE)
311                               {
312                               case L_8:
313                                 fprintf (stream, "%s", regnames[rn]);
314                                 break;
315                               case L_16:
316                                 fprintf (stream, "%s", wregnames[rn]);
317                                 break;
318                               case L_P:
319                               case L_32:
320                                 fprintf (stream, "%s", lregnames[rn]);
321                                 break;
322                               }
323                           }
324                         else if (x & MACREG)
325                           {
326                             fprintf (stream, "mac%c", abs ? 'l' : 'h');
327                           }
328                         else if (x & INC)
329                           {
330                             fprintf (stream, "@%s+", pregnames[rs]);
331                           }
332                         else if (x & DEC)
333                           {
334                             fprintf (stream, "@-%s", pregnames[rd]);
335                           }
336                         else if (x & IND)
337                           {
338                             int rn = (x & DST) ? rd : rs;
339                             fprintf (stream, "@%s", pregnames[rn]);
340                           }
341                         else if (x & ABS8MEM)
342                           {
343                             fprintf (stream, "@0x%x:8", (unsigned) abs);
344                           }
345                         else if (x & (ABS|ABSJMP))
346                           {
347                             fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
348                           }
349                         else if (x & MEMIND)
350                           {
351                             fprintf (stream, "@@%d (%x)", abs, abs);
352                           }
353                         else if (x & PCREL)
354                           {
355                             if (x & L_16) 
356                               {
357                                 abs += 2;
358                                 fprintf (stream, ".%s%d (%x)", (short) abs > 0 ? "+" : "", (short) abs,
359                                          addr + (short) abs + 2);
360                               }
361                             else
362                               {
363                                 fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
364                                          addr + (char) abs + 2);
365                               }
366                           }
367                         else if (x & DISP)
368                           {
369                             fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
370                           }
371                         else if (x & CCR)
372                           {
373                             fprintf (stream, "ccr");
374                           }
375                         else if (x & EXR)
376                           {
377                             fprintf (stream, "exr");
378                           }
379                         else
380                           /* xgettext:c-format */
381                           fprintf (stream, _("Hmmmm %x"), x);
382                         
383                         hadone = 1;
384                         args++;
385                       }
386                   }
387                   return q->length;
388                 }
389               else
390                 {
391                   /* xgettext:c-format */
392                   fprintf (stream, _("Don't understand %x \n"), looking_for);
393                 }
394             }
395           
396           len++;
397           nib++;
398         }
399       
400     fail:
401       q++;
402     }
403
404   /* Fell of the end.  */
405   fprintf (stream, "%02x %02x        .word\tH'%x,H'%x",
406            data[0], data[1],
407            data[0], data[1]);
408   return 2;
409 }
410
411 int 
412 print_insn_h8300 (addr, info)
413      bfd_vma addr; 
414      disassemble_info *info;
415 {
416   return bfd_h8_disassemble (addr, info , 0);
417 }
418
419 int 
420 print_insn_h8300h (addr, info)
421      bfd_vma addr;
422      disassemble_info *info;
423 {
424   return bfd_h8_disassemble (addr, info , 1);
425 }
426
427 int 
428 print_insn_h8300s (addr, info)
429      bfd_vma addr;
430      disassemble_info *info;
431 {
432   return bfd_h8_disassemble (addr, info , 2);
433 }