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