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