Use .short to display unidentified instructions
[external/binutils.git] / opcodes / mcore-dis.c
1 /* Disassemble Motorolla M*Core instructions.
2    Copyright (C) 1993, 1999 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #include <stdio.h>
19 #define STATIC_TABLE
20 #define DEFINE_TABLE
21
22 #include "mcore-opc.h"
23 #include "dis-asm.h"
24
25 /* Mask for each mcore_opclass: */
26 static const unsigned short imsk[] =
27 {
28     /* O0  */ 0xFFFF,
29     /* OT  */ 0xFFFC,
30     /* O1  */ 0xFFF0,
31     /* OC  */ 0xFFE0,
32     /* O2  */ 0xFF00,
33     /* X1  */ 0xFFF0,
34     /* OI  */ 0xFE00,
35     /* OB  */ 0xFE00,
36               
37     /* OMa */ 0xFFF0,
38     /* SI  */ 0xFE00,
39     /* I7  */ 0xF800,
40     /* LS  */ 0xF000,
41     /* BR  */ 0xF800,
42     /* BL  */ 0xFF00,
43     /* LR  */ 0xF000,
44     /* LJ  */ 0xFF00,
45               
46     /* RM  */ 0xFFF0,
47     /* RQ  */ 0xFFF0,
48     /* JSR */ 0xFFF0,
49     /* JMP */ 0xFFF0,
50     /* OBRa*/ 0xFFF0,
51     /* OBRb*/ 0xFF80,
52     /* OBRc*/ 0xFF00,
53     /* OBR2*/ 0xFE00,
54               
55     /* O1R1*/ 0xFFF0,
56     /* OMb */ 0xFF80,
57     /* OMc */ 0xFF00,
58     /* SIa */ 0xFE00,
59
60 /* start-sanitize-m340 */
61   /* MULSH */ 0xFF00,
62 /* end-sanitize-m340 */
63                  
64     /* JC  */ 0,                /* JC,JU,JL don't appear in object */
65     /* JU  */ 0,
66     /* JL  */ 0,
67     /* RSI */ 0,
68     /* DO21*/ 0,
69     /* OB2 */ 0                 /* OB2 won't appear in object. */
70 };
71
72 static const char * grname[] =
73 {
74  "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
75  "r8",  "r9", "r10", "r11", "r12", "r13", "r14", "r15"
76 };
77
78 static const char X[] = "??";
79
80 static const char * crname[] =
81 {
82   "psr",  "vbr", "epsr", "fpsr", "epc",  "fpc",  "ss0",  "ss1",
83   "ss2",  "ss3", "ss4",  "gcr",  "gsr",     X,      X,      X,
84      X,      X,      X,      X,      X,     X,      X,      X,
85      X,      X,      X,      X,      X,     X,      X,      X
86 };
87
88 static const unsigned isiz[] = { 2, 0, 1, 0 };
89
90 int 
91 print_insn_mcore (memaddr, info)
92      bfd_vma memaddr;
93      struct disassemble_info * info;
94 {
95   unsigned char       ibytes[4];
96   fprintf_ftype       fprintf = info->fprintf_func;
97   void *              stream = info->stream;
98   unsigned short      inst;
99   mcore_opcode_info * op;
100   int                 status;
101
102   info->bytes_per_chunk = 2;
103
104   status = info->read_memory_func (memaddr, ibytes, 2, info);
105
106   if (status != 0) 
107     {
108       info->memory_error_func (status, memaddr, info);
109       return -1;
110     }
111
112 /* start-sanitize-m340 */
113   if (info->endian == BFD_ENDIAN_BIG)
114 /* end-sanitize-m340 */
115     inst = (ibytes[0] << 8) | ibytes[1];
116 /* start-sanitize-m340 */
117   else if (info->endian == BFD_ENDIAN_LITTLE)
118     inst = (ibytes[1] << 8) | ibytes[0];
119   else
120     abort ();
121 /* end-sanitize-m340 */
122
123   /* Just a linear search of the table.  */
124   for (op = mcore_table; op->name != 0; op ++)
125     if (op->inst == (inst & imsk[op->opclass]))
126       break;
127
128   if (op->name == 0)
129     fprintf (stream, ".short 0x%04x", inst);
130   else
131     {
132       const char * name = grname[inst & 0x0F];
133       
134       fprintf (stream, "%s", op->name);
135       
136       switch (op->opclass)
137         {
138         case O0: break;
139         case OT: fprintf (stream, "\t%d", inst & 0x3); break;
140         case O1:
141         case JMP:
142         case JSR: fprintf (stream, "\t%s", name); break;
143         case OC:  fprintf (stream, "\t%s, %s", name, crname[(inst >> 4) & 0x1F]); break;
144         case O1R1: fprintf (stream, "\t%s, r1", name); break;
145 /* start-sanitize-m340 */
146         case MULSH:
147 /* end-sanitize-m340 */
148         case O2: fprintf (stream, "\t%s, %s", name, grname[(inst >> 4) & 0xF]); break;
149         case X1: fprintf (stream, "\tr1, %s", name); break;
150         case OI: fprintf (stream, "\t%s, %d", name, ((inst >> 4) & 0x1F) + 1); break;
151         case RM: fprintf (stream, "\t%s-r15, (r0)", name); break;
152         case RQ: fprintf (stream, "\tr4-r7, (%s)", name); break;
153         case OB:
154         case OBRa:
155         case OBRb:
156         case OBRc:
157         case SI:
158         case SIa:
159         case OMa:
160         case OMb:
161         case OMc: fprintf (stream, "\t%s, %d", name, (inst >> 4) & 0x1F); break;
162         case I7: fprintf (stream, "\t%s, %d", name, (inst >> 4) & 0x7F); break;
163         case LS: fprintf (stream, "\t%s, (%s, %d)", grname[(inst >> 8) & 0xF],
164                           name, ((inst >> 4) & 0xF) << isiz[(inst >> 13) & 3]);
165           break;
166           
167         case BR:
168           {
169             long val = inst & 0x3FF;
170             
171             if (inst & 0x400)
172               val |= 0xFFFFFC00;
173             
174             fprintf (stream, "\t0x%x", memaddr + 2 + (val<<1));
175             
176             if (strcmp (op->name, "bsr") == 0)
177               {
178                 /* for bsr, we'll try to get a symbol for the target */
179                 val = memaddr + 2 + (val << 1);
180                 
181                 if (info->print_address_func && val != 0)
182                   {
183                     fprintf (stream, "\t// ");
184                     info->print_address_func (val, info);
185                   }
186               }
187           }
188           break;
189           
190         case BL:
191           {
192             long val;
193             val = (inst & 0x000F);
194             fprintf (stream, "\t%s, 0x%x",
195                     grname[(inst >> 4) & 0xF], memaddr - (val << 1));
196           }
197           break;
198           
199         case LR:
200           {
201             unsigned long val;
202             
203             val = (memaddr + 2 + ((inst & 0xFF) << 2)) & 0xFFFFFFFC;
204             
205             status = info->read_memory_func (val, ibytes, 4, info);
206             if (status != 0) 
207               {
208                 info->memory_error_func (status, memaddr, info);
209                 break;
210               }
211             
212 /* start-sanitize-m340 */
213             if (info->endian == BFD_ENDIAN_LITTLE)
214               val = (ibytes[3] << 24) | (ibytes[2] << 16)
215                 | (ibytes[1] << 8) | (ibytes[0]);
216             else
217 /* end-sanitize-m340 */
218               val = (ibytes[0] << 24) | (ibytes[1] << 16)
219                 | (ibytes[2] << 8) | (ibytes[3]);
220             
221             /* Removed [] around literal value to match ABI syntax 12/95.  */
222             fprintf (stream, "\t%s, 0x%X", grname[(inst >> 8) & 0xF], val);
223
224             if (val == 0)
225               fprintf (stream, "\t// from address pool at 0x%x",
226                        (memaddr + 2 + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
227           }
228           break;
229           
230         case LJ:
231           {
232             unsigned long val;
233             
234             val = (memaddr + 2 + ((inst & 0xFF) << 2)) & 0xFFFFFFFC;
235             
236             status = info->read_memory_func (val, ibytes, 4, info);
237             if (status != 0) 
238               {
239                 info->memory_error_func (status, memaddr, info);
240                 break;
241               }
242
243 /* start-sanitize-m340 */
244             if (info->endian == BFD_ENDIAN_LITTLE)
245               val = (ibytes[3] << 24) | (ibytes[2] << 16)
246                 | (ibytes[1] << 8) | (ibytes[0]);
247             else
248 /* end-sanitize-m340 */
249               val = (ibytes[0] << 24) | (ibytes[1] << 16)
250                 | (ibytes[2] << 8) | (ibytes[3]);
251             
252             /* Removed [] around literal value to match ABI syntax 12/95.  */
253             fprintf (stream, "\t0x%X", val);
254             /* For jmpi/jsri, we'll try to get a symbol for the target.  */
255             if (info->print_address_func && val != 0)
256               {
257                 fprintf (stream, "\t// ");
258                 info->print_address_func (val, info);
259               }
260             else
261               {
262                 fprintf (stream, "\t// from address pool at 0x%x",
263                          (memaddr + 2 + ((inst & 0xFF) << 2)) & 0xFFFFFFFC);
264               }
265           }
266           break;
267           
268         default:
269           /* if the disassembler lags the instruction set */
270           fprintf (stream, "\tundecoded operands, inst is 0x%04x", inst);
271           break;
272         }
273     }
274   
275   /* Say how many bytes we consumed? */
276   return 2;
277 }